Artifact 83a3c195018f2f943f2ffceb8808bfa0b4f26bddd473e61699248e49f5e5c53b:
- File
src/attention.c
— part of check-in
[3598f9d78d]
at
2026-11-14 13:15:53
on branch trunk
— Phase 8a: needs are checked, listed, and shown until they are done
A need is a step a harness must do for itself, such as registering a slot in its own settings. suit never edits those settings; it says what is needed and can tell when it has been done.
The done test has two forms and nothing else: PATH exists, and PATH contains "TEXT". suit needs lists every need as done, not done or cannot tell, and changes nothing. suit need HARNESS NAME prints the prompt's exact bytes so it can be handed to the harness.
After each apply the attention file holds one item for each need not done and none for those that are. Other kinds of item are left alone and keep their order, and nothing is written when nothing changed. If that update fails, apply writes no stamp, so the next run tries again. A bad need stops apply before it places anything. suit off removes the items.
suit-run is untouched and the speed budget still passes.
An eighth fuzz target covers the done-test parser.
1355 unit checks, 479 shell cases, 669 store cases, 745 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (34 minutes). 100% coverage in all 24 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 6835) [annotate] [blame] [check-ins using]
#include "attention.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> static char cur[ATTENTION_FILE_MAX + 1]; static char out[ATTENTION_FILE_MAX + 1]; // Read the file into cur. A file that is not there is empty. static int load(const char *path, size_t *len) { *len = 0; int fd = sys_open(path, O_RDONLY, 0); if (fd < 0 && errno == ENOENT) return 0; if (fd < 0) { say(STDERR_FILENO, "suit: cannot open %s: %s\n", path, strerror(errno)); return 1; } ssize_t n = 1; while (*len <= ATTENTION_FILE_MAX && n > 0) { n = sys_read(fd, cur + *len, ATTENTION_FILE_MAX + 1 - *len); if (n < 0 && errno == EINTR) { n = 1; continue; } *len += n > 0 ? (size_t)n : 0; } int err = errno; (void)sys_close(fd); if (n < 0) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(err)); return 1; } if (*len > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s is over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } return 0; } // Whether the header line LINE ("KIND NAME" or "KIND NAME: ...") is the item // of that kind (any if KIND is null) for NAME. static bool item_is(const char *line, size_t n, const char *kind, const char *name) { const char *sp = memchr(line, ' ', n); if (sp == nullptr) return false; size_t kl = (size_t)(sp - line); if (kind != nullptr && (strlen(kind) != kl || memcmp(line, kind, kl) != 0)) return false; const char *w = sp + 1; size_t room = n - kl - 1; size_t wl = 0; while (wl < room && w[wl] != ':') wl++; return wl == strlen(name) && memcmp(w, name, wl) == 0; } // Copy cur to out without the items of KIND and NAME. Returns the length of // out, or SIZE_MAX... cannot overflow: out is as big as cur. static size_t filter(size_t len, const char *kind, const char *name) { size_t o = 0; size_t p = 0; bool keep = true; while (p < len) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ') keep = !item_is(cur + p, end - p, kind, name); if (keep) { memcpy(out + o, cur + p, end - p); o += end - p; out[o++] = '\n'; } p = end + 1; } return o; } // Write OUT, or remove the file when OUT is empty. CHANGED says whether OUT // differs from what was read. static int store_out(const char *path, size_t o, size_t len) { if (o == len && memcmp(out, cur, o) == 0) return 0; if (o == 0) { if (sys_unlink(path) != 0 && errno != ENOENT) { say(STDERR_FILENO, "suit: cannot remove %s: %s\n", path, strerror(errno)); return 1; } return 0; } int err = sys_write_atomic(path, out, o); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); return err == 0 ? 0 : 1; } int attention_remove(const char *path, const char *kind, const char *name) { size_t len; if (load(path, &len) != 0) return 1; return store_out(path, filter(len, kind, name), len); } int attention_add(const char *path, const char *kind, const char *name, const char *header, const char *details) { size_t len; if (load(path, &len) != 0) return 1; size_t o = filter(len, kind, name); size_t more = strlen(header) + 1 + (details != nullptr ? strlen(details) * 3 : 0); if (o + more > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s would be over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } o += (size_t)snprintf(out + o, sizeof out - o, "%s\n", header); for (const char *d = details; d != nullptr && *d != '\0';) { size_t n = strcspn(d, "\n"); o += (size_t)snprintf(out + o, sizeof out - o, " %.*s\n", (int)n, d); ensure(d[n] == '\n'); d += n + 1; } return store_out(path, o, len); } int attention_sync(const char *path, const char *kind, size_t n, const char *const *names, const char *const *headers) { ensure(n <= ATTENTION_SYNC_MAX); size_t len; if (load(path, &len) != 0) return 1; bool seen[ATTENTION_SYNC_MAX] = {}; size_t o = 0; bool keep = true; for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ') { keep = true; size_t kl = strlen(kind); if (end - p > kl && memcmp(cur + p, kind, kl) == 0 && cur[p + kl] == ' ') { size_t want = 0; while (want < n && !(item_is(cur + p, end - p, kind, names[want]) && strlen(headers[want]) == end - p && memcmp(headers[want], cur + p, end - p) == 0)) want++; keep = want < n && !seen[want]; if (keep) seen[want] = true; } } if (keep) { memcpy(out + o, cur + p, end - p); o += end - p; out[o++] = '\n'; } p = end + 1; } for (size_t i = 0; i < n; i++) { if (seen[i]) continue; if (o + strlen(headers[i]) + 1 > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s would be over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } o += (size_t)snprintf(out + o, sizeof out - o, "%s\n", headers[i]); } return store_out(path, o, len); } int attention_print(const char *path) { size_t len; if (load(path, &len) != 0) return 1; if (len == 0) return 0; say(STDOUT_FILENO, "needs you:\n"); for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; say(STDOUT_FILENO, "%.*s\n", (int)(end - p), cur + p); p = end + 1; } return 0; } int attention_count(const char *path, unsigned *count) { size_t len; *count = 0; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (end > p && cur[p] != ' ') (*count)++; p = end + 1; } return 0; } int attention_has(const char *path, const char *kind, const char *name, bool *has) { size_t len; *has = false; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len && !*has; p++) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; *has = cur[p] != ' ' && item_is(cur + p, end - p, kind, name); p = end; } return 0; }