Artifact 02e411a327293c330667809ad6cb2736b5262e209fb515c7d4fa4dbe04ab5d01:
- File
src/usage.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: 4562) [annotate] [blame] [check-ins using]
#ifndef SUIT_USAGE_H #define SUIT_USAGE_H #include "conf.h" #include <stddef.h> #include <stdint.h> // The record reader (docs/usage.md): walks the JSON Lines a harness keeps of // its sessions and reports, for each `use` rule, what the rule finds. It knows // nothing of any harness; a rule says where to look. It uses no memory that // grows with a line: a file is read in chunks of USAGE_CHUNK bytes, a string is // kept only up to USAGE_STR_KEEP bytes, nesting is cut off at USAGE_DEPTH_MAX // and every other buffer has a fixed size named here. constexpr size_t USAGE_CHUNK = 65536; constexpr size_t USAGE_STR_KEEP = 512; // the first bytes of a string that are kept constexpr size_t USAGE_DEPTH_MAX = 64; // containers inside one another constexpr uint32_t USAGE_LINE_HITS_MAX = 32; // matches kept from one line constexpr uint32_t USAGE_CAND_MAX = 8; // names one rule takes from one matched thing constexpr size_t USAGE_ID_MAX = 128; constexpr size_t USAGE_TIME_MAX = 40; constexpr size_t USAGE_PROJECT_MAX = USAGE_STR_KEEP - 1; constexpr uint32_t USAGE_VALUES_MAX = CONF_USE_MAX * USAGE_WHERE_MAX; // Per rule: the path in, USAGE_WHERE_MAX tests, two name paths and the id; and the time and the project. constexpr uint32_t USAGE_PER_RULE = 1 + USAGE_WHERE_MAX + 3; constexpr uint32_t USAGE_TRACK_MAX = 2 + CONF_USE_MAX * USAGE_PER_RULE; // One thing a rule found. TIME, ID and PROJECT are empty when the record has // none (or it is longer than the constants above). typedef struct { UseKind kind; char name[USAGE_NAME_MAX + 1]; char id[USAGE_ID_MAX + 1]; char time[USAGE_TIME_MAX + 1]; char project[USAGE_PROJECT_MAX + 1]; } UsageEvent; typedef void (*UsageSink)(void *state, const UsageEvent *ev); // Where lines come from: a file read in chunks, or a block of memory. After a // failed read ERR holds the errno. typedef struct { int fd; // -1: memory const uint8_t *mem; uint64_t memlen; uint64_t base; // offset of buf[0] size_t at; size_t have; int err; uint8_t buf[USAGE_CHUNK]; } UsageSource; void usage_source_fd(UsageSource *s, int fd); void usage_source_mem(UsageSource *s, const void *data, size_t len); typedef struct { const UsagePath *path; uint8_t n; } UsageTrack; typedef struct { uint8_t st[USAGE_TRACK_MAX]; uint16_t arrived; } UsageLevel; typedef struct { bool ok; // false: the name was found but is not a name char name[USAGE_NAME_MAX + 1]; } UsageCand; typedef struct { uint32_t where_ok; uint32_t ncand; UsageCand cand[USAGE_CAND_MAX]; uint8_t id_state; char id[USAGE_ID_MAX + 1]; } UsageCap; typedef struct { UseKind kind; char name[USAGE_NAME_MAX + 1]; char id[USAGE_ID_MAX + 1]; } UsageHit; // The rule set and the work areas of one reader. Large: keep it static. typedef struct { const UsageRule *rules; uint32_t nrules; UsageTrack track[USAGE_TRACK_MAX]; uint32_t ntrack; const char *vals[USAGE_VALUES_MAX]; // every where VALUE once, for the pre-test uint8_t vlen[USAGE_VALUES_MAX]; uint32_t nv; size_t vmax; uint64_t rmask[CONF_USE_MAX]; // the values each rule needs, one bit each bool always; // a rule has no where: every line gets walked UsageSource *src; uint64_t seen; uint8_t tail[USAGE_VALUE_MAX]; size_t ntail; bool pass; UsageLevel lv[USAGE_DEPTH_MAX + 1]; bool is_obj[USAGE_DEPTH_MAX]; UsageCap cap[CONF_USE_MAX]; UsageHit hits[USAGE_LINE_HITS_MAX]; uint32_t nhits; uint32_t line_skipped; char str[USAGE_STR_KEEP]; char key[USAGE_KEY_MAX + 1]; char time[USAGE_TIME_MAX + 1]; char project[USAGE_PROJECT_MAX + 1]; uint64_t lines; // lines walked uint64_t skipped; // matches that are no event, and lines that were not JSON } Usage; // Prepare U for N rules (at most CONF_USE_MAX). TIME and PROJECT say where in a // record those are; either may be null. The rules must outlive U. void usage_init(Usage *u, const UsageRule *rules, uint32_t n, const UsagePath *time, const UsagePath *project); // Read every complete line of S from offset START, calling SINK for each thing // found. *END is the offset of the first byte not read: the start of a last // line that has no newline yet, or the end of the data. False if a read failed // (S->err). Lines that are not JSON, and lines too deeply nested, are skipped. [[nodiscard]] bool usage_read(Usage *u, UsageSource *s, uint64_t start, uint64_t *end, UsageSink sink, void *state); #endif