Artifact 14fce7d9c3c69881c8c1e352fcd6e10a718d8bb61ec0ad72a7bf2d9c39d1d839:
- File
test/fuzz_usage.c
— 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: 2673) [annotate] [blame] [check-ins using]
// Fuzz target for the session record reader: any bytes, read as lines against // a fixed set of rules, must never crash, read out of bounds or run forever. #include "conf.h" #include "platform.h" #include "usage.h" #include <stdint.h> #include <string.h> constexpr size_t INPUT_MAX = (2u << 20) + 1; static const struct { UseKind kind; const char *text; } rule_text[] = { {USE_SKILL, "message.content[] where type=tool_use name=Skill name input.skill id id"}, {USE_AGENT, "message.content[] where type=tool_use name=Agent name input.subagent_type id id"}, {USE_AGENT, "message.content[] where type=tool_use name=Task name input.subagent_type id id"}, {USE_COMMAND, ". where type=user name message.content between <command-name>/ </command-name>"}, {USE_SKILL, "a[].b.c name d"}, }; static UsageRule rules[sizeof rule_text / sizeof rule_text[0]]; static UsagePath time_path; static UsagePath project_path; static Usage reader; static UsageSource source; static uint8_t line[INPUT_MAX + 1]; [[maybe_unused]] static uint64_t events; static bool ready; int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); static void sink(void *state, [[maybe_unused]] const UsageEvent *ev) { (void)state; ensure(ev->name[0] != '\0' && strlen(ev->name) <= USAGE_NAME_MAX); ensure(strlen(ev->id) <= USAGE_ID_MAX && strlen(ev->time) <= USAGE_TIME_MAX); ensure(strlen(ev->project) <= USAGE_PROJECT_MAX); events++; } int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if (!ready) { char msg[256]; for (size_t i = 0; i < sizeof rule_text / sizeof rule_text[0]; i++) { [[maybe_unused]] bool ok = conf_rule_parse(&rules[i], rule_text[i].kind, rule_text[i].text, msg, sizeof msg); ensure(ok); } ensure(conf_path_parse(&time_path, "timestamp", false, false)); ensure(conf_path_parse(&project_path, "cwd", false, false)); usage_init(&reader, rules, sizeof rules / sizeof rules[0], &time_path, &project_path); ready = true; } uint64_t end; // As it comes: a last line with no newline is not read. usage_source_mem(&source, data, size); [[maybe_unused]] bool first = usage_read(&reader, &source, 0, &end, sink, nullptr); ensure(first && end <= size); // With a newline after it: the whole input is one more line. if (size < INPUT_MAX - 1) { memcpy(line, data, size); line[size] = '\n'; usage_source_mem(&source, line, size + 1); [[maybe_unused]] bool second = usage_read(&reader, &source, 0, &end, sink, nullptr); ensure(second && end == size + 1); } return 0; }