Artifact b27d994a8d3dbd18fbeeb4ea424d1cd21191823c5dc60f069daf0ef9460e1eb7:
- File
src/conf.h
— part of check-in
[38c2e6220a]
at
2026-11-16 21:25:35
on branch trunk
— Suit counts how often each skill, agent and command is used
The counts come from the records a harness already keeps of its own work, read afterwards. What those records look like is data in the wardrobe's harness section: where the files are, where a record's time and folder are, and rules that say what an invocation looks like. No harness is named in suit's code. docs/harnesses.md has the rules for the first harness, checked against real records.
A streaming reader walks each JSON line against the rules in 64 KiB chunks, keeps at most the first 512 bytes of any string and stops at 64 levels, so a line of any length cannot grow its memory. It has its own fuzz target.
Suit keeps its own tally per machine under usage/ in its home folder, outside any wardrobe: one line per invocation with time, harness, kind, name, project and call id. A call copied into a resumed or forked record is counted once, by its id. The tally outlives the harness's own clean-up and drops what is older than 400 days.
suit usage scan reads what is new; a first scan reads files changed in the last 45 days and later scans read only new bytes. suit usage prints the table for a window, default 30 days, and --tsv gives the same rows for other tools. suit tend with no name runs the scan after tending; a failed scan is reported and changes nothing else. suit-run is untouched and the speed budget passes as before.
Measured: a generated tree of 200 files and 200 MiB scans in about half a second and a second scan opens no record file. On a real tree of 2547 files, read-only into a scratch home: 3756 invocations in 60 seconds, 10 MiB at peak.
A new suite, test/usage.sh, with 236 cases, is part of the gate.
2855 unit checks (was 1473), 620 shell cases, 1153 store cases, 846 placement cases, 378 project cases and 236 usage cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (41 minutes). 100% coverage in all 28 source files. One existing unit line changed: a table of fault points gained entries. (user: dmestas, size: 6162) [annotate] [blame] [check-ins using]
#ifndef SUIT_CONF_H #define SUIT_CONF_H #include <stddef.h> #include <stdint.h> constexpr size_t CONF_LINE_MAX = 4096; constexpr size_t CONF_FILE_MAX = 1048576; constexpr size_t CONF_NAME_MAX = 128; constexpr uint32_t CONF_HARNESS_MAX = 64; constexpr uint32_t CONF_OUTFIT_MAX = 512; constexpr uint32_t CONF_WEAR_MAX = 256; constexpr uint32_t CONF_NEED_MAX = 256; constexpr uint32_t CONF_UPSTREAM_MAX = 128; constexpr uint32_t CONF_ENTRY_MAX = 8192; constexpr size_t CONF_INTO_MAX = 256; // an upstream's `into` constexpr uint32_t CONF_SECTION_MAX = CONF_HARNESS_MAX + CONF_OUTFIT_MAX + CONF_WEAR_MAX + CONF_NEED_MAX + CONF_UPSTREAM_MAX; // Session records (docs/usage.md): what a harness's records look like, as data. constexpr uint32_t CONF_SESSIONS_MAX = 8; // `sessions` keys in a harness constexpr uint32_t CONF_USE_MAX = 16; // `use KIND` keys in a harness constexpr size_t USAGE_KEY_MAX = 32; // a field name in a path constexpr size_t USAGE_STEPS_MAX = 8; // steps in a path; a "[]" is one step constexpr size_t USAGE_VALUE_MAX = 64; // a where VALUE, and an A or a B of between constexpr uint32_t USAGE_WHERE_MAX = 4; // where tests in a rule constexpr size_t USAGE_NAME_MAX = 200; // a kept name, in bytes // A path from a record's top: field names, and "each element of this array". // The slots past USAGE_STEPS_MAX are for the parser's own use (name_list below). typedef struct { uint8_t n; bool each[USAGE_STEPS_MAX + 2]; uint8_t len[USAGE_STEPS_MAX + 2]; char key[USAGE_STEPS_MAX + 2][USAGE_KEY_MAX + 1]; } UsagePath; typedef enum { USE_SKILL, USE_AGENT, USE_COMMAND, USE_KIND_COUNT } UseKind; // One `use KIND = RULE`: IN [where FIELD=VALUE ...] name PATH [between A B] [id PATH]. typedef struct { UseKind kind; UsagePath in; uint32_t nwhere; UsagePath where_path[USAGE_WHERE_MAX]; char where_value[USAGE_WHERE_MAX][USAGE_VALUE_MAX + 1]; UsagePath name; UsagePath name_list; // name, then each element, then its "text" bool between; char a[USAGE_VALUE_MAX + 1]; char b[USAGE_VALUE_MAX + 1]; bool has_id; UsagePath id; } UsageRule; // The word for KIND: skill, agent or command. const char *use_kind_name(UseKind kind); // Parse TEXT, the value of a `use KIND` key, into R. False with a message in // MSG (CAP bytes) saying what is wrong. [[nodiscard]] bool conf_rule_parse(UsageRule *r, UseKind kind, const char *text, char *msg, size_t cap); // Parse a path of field names joined by dots into P. EACH says whether a part // may end in "[]"; DOT says whether "." alone (no steps) is a path. False if it is not one. [[nodiscard]] bool conf_path_parse(UsagePath *p, const char *text, bool each, bool dot); // Wards that take part in placing (docs/conf-format.md, "More than one wardrobe"). constexpr uint32_t WARDROBE_MAX = 16; constexpr size_t WARDROBE_PATH_MAX = 4096; // One wardrobe that takes part: its name, its store's folder and its config's path. typedef struct { char name[CONF_NAME_MAX + 1]; char store[WARDROBE_PATH_MAX]; char conf[WARDROBE_PATH_MAX]; } Ward; // The wardrobes that take part, in byte order of name. A Section's ward indexes w. typedef struct { uint32_t n; Ward w[WARDROBE_MAX]; } Wards; typedef enum { KIND_HARNESS, KIND_OUTFIT, KIND_WEAR, KIND_NEED, KIND_UPSTREAM, KIND_COUNT } Kind; // Every string points into Conf.buf. A list value is stored with its words // separated by single spaces; walk it with conf_list_next. typedef struct { const char *key; const char *val; int line; } Entry; // harness, outfit, upstream: arg[0] is the name. // need: arg[0] is the harness, arg[1] the name. // wear: arg[0] is the scope, arg[1] the target ("" if none), arg[2] the // machine name ("" if none). typedef struct { Kind kind; const char *arg[3]; int line; uint32_t first; uint32_t count; uint8_t ward; // the wardrobe whose config defines the section; 0 for one file } Section; typedef struct { uint32_t nsections; uint32_t nentries; uint32_t kind_count[KIND_COUNT]; Section sections[CONF_SECTION_MAX]; Entry entries[CONF_ENTRY_MAX]; size_t len; // bytes of the files held, at most CONF_FILE_MAX size_t used; // bytes of buf in use: each file's bytes and one more char buf[CONF_FILE_MAX + 1 + WARDROBE_MAX]; } Conf; // line is 0 when the error concerns the whole file. ward is the wardrobe whose // file it is (0 when only one file is read). typedef struct { int line; uint32_t ward; char msg[256]; } ConfError; // Parse DATA (LEN bytes) into C. On failure C holds nothing usable and ERR // says what and where. [[nodiscard]] bool conf_parse(Conf *c, const char *data, size_t len, ConfError *err); [[nodiscard]] bool conf_load(Conf *c, const char *path, ConfError *err); // conf_load without the checks of references and cycles, for a file that is one // of several: what it names may be defined in another. Follow with conf_merge. [[nodiscard]] bool conf_parse_part(Conf *c, const char *data, size_t len, ConfError *err); [[nodiscard]] bool conf_load_part(Conf *c, const char *path, ConfError *err); // Empty the combined set C. void conf_merge_init(Conf *c); // Add SRC, a file read with conf_load_part, to the combined set DST as wardrobe // WARD of W. A section already in DST, or a limit passed by the combined set, // is an error in WARD's file. [[nodiscard]] bool conf_merge(Conf *dst, const Conf *src, uint32_t ward, const Wards *w, ConfError *err); // Check the references and the include cycles of the combined set C. [[nodiscard]] bool conf_check(const Conf *c, ConfError *err); // Write the canonical dump to FD: sections and entries in stable order, one // fact per line. Reorders C in place. False if a write fails (errno set). [[nodiscard]] bool conf_dump(Conf *c, int fd); // True if the LEN bytes are a name: 1 to 128 of A-Z a-z 0-9 . _ - (and * // if GLOB). bool conf_name_ok(const char *s, size_t len, bool glob); // Step through a list value. Returns false at the end. bool conf_list_next(const char **cursor, const char **word, size_t *len); #endif