Artifact e701c0599e7d21cb7374faf5c082b51ac26ae4c9adfb14716821a29bd2ba86d9:
- File
src/curate_text.c
— part of check-in
[cef6a0ad45]
at
2026-11-17 07:37:01
on branch trunk
— suit curate writes a page for deciding what each asset is worth keeping
suit curate [--since DAYS] [--out FILE] runs a usage scan and writes one self-contained HTML page, mode 0600, atomically, to curate.html in suit's home folder or to FILE. It changes no wardrobe file, config, stamp or ledger. The page names projects and stays on this machine; it loads nothing from the network and has no inline handlers.
Each skill, agent and command in the wardrobes is a row: uses in the window and in each of the last four weeks, last used, the projects it was used in, its description and what that costs every session, where it is worn, whether it is in the attic, and whether it is the owner's or an upstream's. Origin comes from the entries of the check-in the upstream's record names, so an upstream's skills beside the owner's own in skills/ are told apart. A name in two wardrobes is tagged with its wardrobe.
Each row starts from a suggestion with its reason: retire when unused in the window, unless its files changed within it; limit when three or more uses all came from one known project; otherwise keep. A folder that is not a known project cannot be limited to, and the page says how to make it one. The owner picks keep, limit or retire per row, with keys or in bulk on what is shown, and copies or saves the choices as a short text file. Applying it is a separate step, not in this check-in. The format is in docs/curate.md: one line per asset, `keep KIND WARDROBE NAME`, `retire KIND WARDROBE NAME`, or `personal KIND WARDROBE NAME PROJECT`.
The page is src/curate.html, turned into a C array at build time with od and awk, so the release build needs no other tool. Values are written as JSON with markup characters, U+2028 and U+2029 escaped and invalid UTF-8 replaced. The page script has its own test, run under node where node is installed and reported as skipped elsewhere. A new fuzz target covers the front-matter reader and the JSON writer.
Page sizes: 37 KB for the test fixture, 252 KB for 500 assets, 805 KB at the 4096-asset bound.
A new suite, test/curate.sh, with 288 cases, is part of the gate.
2993 unit checks (was 2855), 620 shell cases, 1153 store cases, 846 placement cases, 378 project cases, 236 usage cases and 288 curate cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (66 minutes, on a loaded machine; one earlier run failed only the loaded speed measurement, at 1.06 to 1.11 ms, and this run passed it at 0.99 ms). 100% coverage in all 31 source files. No existing case was changed. (user: dmestas, size: 7922) [annotate] [blame] [check-ins using]
#include "curate.h" #include "platform.h" #include <stdio.h> #include <string.h> // ---- the front matter ------------------------------------------------------------------ typedef struct { char *out; size_t n; bool space; } Text; // Add the LEN bytes of S, a run of white space becoming one space and none at either end. static void text_add(Text *t, const char *s, size_t len) { for (size_t i = 0; i < len; i++) { char c = s[i]; if (c == ' ' || c == '\t' || c == '\r') { t->space = t->n > 0; } else { if (t->space) t->out[t->n++] = ' '; t->space = false; t->out[t->n++] = c; } } } static size_t line_end(const char *d, size_t from, size_t to) { const char *nl = memchr(d + from, '\n', to - from); return nl != nullptr ? (size_t)(nl - d) : to; } static bool is_fence(const char *line, size_t len) { while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == ' ' || line[len - 1] == '\t')) len--; return len == 3 && memcmp(line, "---", 3) == 0; } static bool is_indented(const char *line, size_t len) { return len > 0 && (line[0] == ' ' || line[0] == '\t'); } // The text of a quoted value that starts at S[0] (after its opening quote) and may go on // over later indented lines: returns the offset in BODY where the next line starts. typedef struct { const char *body; size_t end; // the end of the front matter size_t next; // the start of the next line to read } Lines; static void quoted(Text *t, Lines *ls, const char *s, size_t len, char quote) { for (;;) { for (size_t i = 0; i < len; i++) { if (quote == '"' && s[i] == '\\' && i + 1 < len) { char c = s[++i]; if (c == 'n' || c == 't') text_add(t, " ", 1); else if (c == '"' || c == '\\') text_add(t, &c, 1); else text_add(t, s + i - 1, 2); } else if (quote == '\'' && s[i] == '\'' && i + 1 < len && s[i + 1] == '\'') { text_add(t, "'", 1); i++; } else if (s[i] == quote) { return; } else { text_add(t, s + i, 1); } } // The value goes on over the next line only if that is indented. if (ls->next >= ls->end) return; size_t stop = line_end(ls->body, ls->next, ls->end); if (!is_indented(ls->body + ls->next, stop - ls->next)) return; text_add(t, " ", 1); s = ls->body + ls->next; len = stop - ls->next; ls->next = stop + 1; } } // The value of a block scalar or a plain one: the lines after the key that are indented (for a // block, blank lines too). static void continued(Text *t, Lines *ls, bool block) { while (ls->next < ls->end) { size_t stop = line_end(ls->body, ls->next, ls->end); const char *line = ls->body + ls->next; size_t len = stop - ls->next; bool blank = true; for (size_t i = 0; i < len; i++) blank &= line[i] == ' ' || line[i] == '\t' || line[i] == '\r'; if (!(is_indented(line, len) || (block && blank))) return; text_add(t, line, len); text_add(t, " ", 1); ls->next = stop + 1; } } static bool block_header(const char *v, size_t len) { if (len == 0 || (v[0] != '>' && v[0] != '|')) return false; for (size_t i = 1; i < len; i++) if (strchr("+-0123456789", v[i]) == nullptr) return false; return true; } size_t curate_front_description(const char *data, size_t len, char *out) { out[0] = '\0'; size_t at = len >= 3 && memcmp(data, "\xEF\xBB\xBF", 3) == 0 ? 3 : 0; size_t stop = line_end(data, at, len); if (!is_fence(data + at, stop - at)) return 0; size_t body = stop + 1; size_t end = body; bool closed = false; while (end < len && !closed) { stop = line_end(data, end, len); closed = is_fence(data + end, stop - end); if (!closed) end = stop + 1; } if (!closed) return 0; Text t = {.out = out}; Lines ls = {.body = data, .end = end, .next = body}; while (ls.next < end) { size_t from = ls.next; stop = line_end(data, from, end); ls.next = stop + 1; static const char key[] = "description:"; size_t klen = sizeof key - 1; const char *line = data + from; size_t llen = stop - from; bool is_key = llen >= klen && memcmp(line, key, klen) == 0 && (llen == klen || line[klen] == ' ' || line[klen] == '\t' || line[klen] == '\r'); if (!is_key) continue; const char *v = line + klen; size_t vlen = llen - klen; while (vlen > 0 && (*v == ' ' || *v == '\t')) { v++; vlen--; } while (vlen > 0 && (v[vlen - 1] == ' ' || v[vlen - 1] == '\t' || v[vlen - 1] == '\r')) vlen--; if (block_header(v, vlen)) { continued(&t, &ls, true); } else if (vlen > 0 && (v[0] == '"' || v[0] == '\'')) { quoted(&t, &ls, v + 1, vlen - 1, v[0]); } else { text_add(&t, v, vlen); continued(&t, &ls, false); } break; } out[t.n] = '\0'; return t.n; } size_t curate_cut(const char *s, size_t len, size_t max) { if (len <= max) return len; size_t c = max; while (c > 0 && ((unsigned char)s[c] & 0xC0) == 0x80) c--; return c; } // ---- JSON ------------------------------------------------------------------------------ void curate_put(CurateBuf *b, const char *s, size_t n) { if (b->over || b->len + n > b->cap) { b->over = true; return; } memcpy(b->p + b->len, s, n); b->len += n; } static void put(CurateBuf *b, const char *s) { curate_put(b, s, strlen(s)); } // The length of the UTF-8 character at S (N bytes left), or 0 if the bytes are not one. static size_t utf8_len(const unsigned char *s, size_t n) { unsigned char c = s[0]; size_t need = 1; uint32_t cp; uint32_t min = 0; if (c >= 0x80) { if (c >= 0xC2 && c <= 0xDF) { need = 2; cp = c & 0x1Fu; min = 0x80; } else if ((c & 0xF0) == 0xE0) { need = 3; cp = c & 0x0Fu; min = 0x800; } else if (c >= 0xF0 && c <= 0xF4) { need = 4; cp = c & 0x07u; min = 0x10000; } else { return 0; } if (n < need) return 0; for (size_t i = 1; i < need; i++) { if ((s[i] & 0xC0) != 0x80) return 0; cp = cp << 6 | (s[i] & 0x3Fu); } if (cp < min || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) return 0; } return need; } void curate_json_string(CurateBuf *b, const char *s, size_t len) { put(b, "\""); const unsigned char *u = (const unsigned char *)s; for (size_t i = 0; i < len;) { size_t l = utf8_len(u + i, len - i); unsigned char c = u[i]; if (l == 0) { put(b, "\xEF\xBF\xBD"); i++; } else if (l == 3 && c == 0xE2 && u[i + 1] == 0x80 && (u[i + 2] == 0xA8 || u[i + 2] == 0xA9)) { put(b, u[i + 2] == 0xA8 ? "\\u2028" : "\\u2029"); i += 3; } else if (l > 1) { curate_put(b, s + i, l); i += l; } else { if (c == '"') put(b, "\\\""); else if (c == '\\') put(b, "\\\\"); else if (c == '<') put(b, "\\u003c"); else if (c == '>') put(b, "\\u003e"); else if (c == '&') put(b, "\\u0026"); else if (c < 0x20) { char e[8]; [[maybe_unused]] int n = snprintf(e, sizeof e, "\\u%04x", c); ensure(n == 6); put(b, e); } else { curate_put(b, s + i, 1); } i++; } } put(b, "\""); }