Artifact 1a7dfa1f2544584e5134459634ed22b5f556d2510c1c2071925d46745c0d951c:
- File
src/stamp.c
— part of check-in
[cb7d42f351]
at
2026-11-17 10:01:38
on branch trunk
— The no-change run watches what apply acts on, not every file in the stores
The stamp listed every file and folder in every store, and the compare costs one stat per path, so the per-minute run grew with the wardrobes. On a fixture of two wardrobes with 300 skills of five files, 40 agents and 20 commands it watched 1890 paths and cost 7.1 to 7.6 ms quiet and 12.6 to 13.4 ms loaded, against a budget of 1 ms.
Apply links skill folders, agent and command files, and slot and file sources, and never reads them. From a store it reads the content of only the wardrobe's suit.conf and the sources of a joined instructions file, and nothing is placed by copy. An edit inside a linked skill, or to an agent's or command's content, is already seen by the harness through the link; apply would place the same links. So those are no longer watched.
The stamp now watches the store folder and each folder in it; each wardrobe's folder, suit.conf, and skills, agents and commands folders (whose own entry changes when something in them is added, removed or renamed); entries in those folders that are links, whose target can come or go without the folder changing; every instructions, file and slot path of every outfit, whether or not it exists; need prompts; and the ledger, machine name, roots and project lists as before. docs/decisions.md lists every input apply reads and why each is or is not watched, and replaces the 12 Nov sentence that made an edit anywhere in a store a reason to apply.
On the same fixture it watches 24 paths: 0.26 to 0.28 ms quiet, 0.48 to 0.50 ms loaded. The count no longer depends on files inside skills. Over 128 paths apply stops with a clear error and keeps the old stamp.
The stamp starts with a format line, suit stamp 2. A stamp from before makes the compare report it changed, and the apply that follows rewrites it. suit and suit-run must be updated together: an older suit-run calls the new stamp bad.
Known: on Linux a change within one clock tick of the stamp's own stat can be missed until the next change, as before.
Ten placement cases and five shell lines changed expectation: an edit inside a store no longer causes an apply, a store with many files or deep folders no longer exceeds a limit, and the stamp has one more line. Each case is kept.
2993 unit checks, 625 shell cases (was 620), 1153 store cases, 985 placement cases (was 846), 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 (40 minutes). 100% coverage in all 31 source files. (user: dmestas, size: 10025) [annotate] [blame] [check-ins using]
#include "stamp.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <string.h> #include <unistd.h> static char file_buf[STAMP_FILE_MAX + 1]; // The first line of a stamp this suit writes. A stamp without it was written by // a suit that watched every file of the store: the compare calls it a change, so // the apply that follows writes this one (docs/decisions.md, 2026-11-17). static const char STAMP_FORMAT[] = "suit stamp 2\n"; constexpr size_t STAMP_FORMAT_LEN = sizeof STAMP_FORMAT - 1; // Join A, B and C into OUT without stdio, so the run path pulls in no // formatting code. False if it does not fit in CAP. static bool join3(char *out, size_t cap, const char *a, const char *b, const char *c) { size_t la = strlen(a), lb = strlen(b), lc = strlen(c); if (la + lb + lc >= cap) return false; memcpy(out, a, la); memcpy(out + la, b, lb); memcpy(out + la + lb, c, lc + 1); return true; } // Fill OUT with "$SUIT_HOME/NAME" ("$HOME/.suit/NAME" without SUIT_HOME). // Returns the length of the home folder part, or 0 after saying why. size_t suit_home_path(char *out, size_t cap, const char *name) { const char *home = sys_getenv("SUIT_HOME"); const char *infix = "/"; if (home == nullptr || home[0] == '\0') { home = sys_getenv("HOME"); infix = "/.suit/"; if (home == nullptr || home[0] == '\0') { say(STDERR_FILENO, "suit: neither SUIT_HOME nor HOME is set\n"); return 0; } } if (!join3(out, cap, home, infix, name)) { say(STDERR_FILENO, "suit: home path is longer than %zu bytes\n", cap - 1); return 0; } return strlen(home) + strlen(infix) - 1; } // ---- stamp ---------------------------------------------------------------- // Append one stamp line. False if the buffer is full. static bool append(size_t *len, const char *path, bool missing, const struct stat *st) { size_t room = sizeof file_buf - *len; int n; if (missing) n = snprintf(file_buf + *len, room, "- - - %s\n", path); else n = snprintf(file_buf + *len, room, "%lld %lld %lld %s\n", (long long)st->st_size, (long long)platform_mtime_sec(st), (long long)platform_mtime_nsec(st), path); ensure(n > 0); if ((size_t)n >= room) return false; *len += (size_t)n; return true; } static bool path_ok(const char *p) { size_t n = strlen(p); return n > 0 && n < STAMP_PATH_MAX && strchr(p, '\n') == nullptr; } // Build the whole stamp in file_buf. Returns its length, or -1 after saying why. static ssize_t build_stamp(int npaths, char *const *paths) { memcpy(file_buf, STAMP_FORMAT, STAMP_FORMAT_LEN); size_t len = STAMP_FORMAT_LEN; for (int i = 0; i < npaths; i++) { const char *p = paths[i]; if (!path_ok(p)) { say(STDERR_FILENO, "suit: stamp: path %d is empty, has a newline, or is " "longer than %zu bytes\n", i + 1, STAMP_PATH_MAX - 1); return -1; } struct stat st = {}; bool missing = false; if (sys_stat(p, &st) != 0) { if (errno != ENOENT && errno != ENOTDIR) { say(STDERR_FILENO, "suit: stamp: cannot stat %s: %s\n", p, strerror(errno)); return -1; } missing = true; } if (!append(&len, p, missing, &st)) { say(STDERR_FILENO, "suit: stamp is larger than %zu bytes\n", STAMP_FILE_MAX); return -1; } } return (ssize_t)len; } // Write the stamp to TEMP and move it over FINAL. On failure TEMP is removed. static bool replace_file(const char *temp, const char *final, size_t len) { int fd = sys_open(temp, O_WRONLY | O_CREAT | O_EXCL, 0644); if (fd < 0) return false; if (!sys_write_all(fd, file_buf, len) || sys_fsync(fd) != 0 || sys_close(fd) != 0 || sys_rename(temp, final) != 0) { int err = errno; (void)sys_unlink(temp); errno = err; return false; } return true; } int stamp_write(int npaths, char *const *paths) { char final_path[STAMP_PATH_MAX]; char temp_path[STAMP_PATH_MAX + 32]; size_t home_len = suit_home_path(final_path, sizeof final_path, "stamp"); if (home_len == 0) return 1; ssize_t len = build_stamp(npaths, paths); if (len < 0) return 1; final_path[home_len] = '\0'; if (sys_mkdir(final_path, 0755) != 0 && errno != EEXIST) { say(STDERR_FILENO, "suit: cannot create %s: %s\n", final_path, strerror(errno)); return 1; } final_path[home_len] = '/'; [[maybe_unused]] int n = snprintf(temp_path, sizeof temp_path, "%s.tmp.%d", final_path, (int)getpid()); ensure(n > 0 && (size_t)n < sizeof temp_path); if (!replace_file(temp_path, final_path, (size_t)len)) { say(STDERR_FILENO, "suit: cannot write %s: %s\n", final_path, strerror(errno)); return 1; } return 0; } // ---- run ------------------------------------------------------------------ // Parse a decimal field ending at a space. Returns the position after the // space, or nullptr. A nullptr S gives nullptr. static char *read_number(char *s, int64_t *out) { if (s == nullptr || *s < '0' || *s > '9') return nullptr; int64_t v = 0; for (; *s >= '0' && *s <= '9'; s++) { if (v > (INT64_MAX - 9) / 10) return nullptr; v = v * 10 + (*s - '0'); } if (*s != ' ') return nullptr; *out = v; return s + 1; } static int bad_stamp(int line, const char *why) { say(STDERR_FILENO, "suit: stamp:%d: %s\n", line, why); return EXIT_BAD_STAMP; } // Read the stamp into file_buf. Returns its length, -1 for an error (errno // set), -2 if there is no stamp, -3 if it is over the limit. static ssize_t read_stamp(const char *path) { int fd = sys_open(path, O_RDONLY, 0); if (fd < 0) return errno == ENOENT ? -2 : -1; size_t len = 0; ssize_t result = -3; while (len <= STAMP_FILE_MAX) { ssize_t n = sys_read(fd, file_buf + len, STAMP_FILE_MAX + 1 - len); if (n < 0 && errno == EINTR) continue; if (n <= 0) { result = n < 0 ? -1 : (ssize_t)len; break; } len += (size_t)n; } int err = errno; (void)sys_close(fd); errno = err; return result; } // Stat paths that share a folder through one open handle on it, so the // kernel walks the folder's path once instead of once per file. typedef struct { char dir[STAMP_PATH_MAX]; size_t len; // 0: no folder remembered yet int fd; // -1: not opened; opened on the second path in a folder } DirCache; static int stat_path(DirCache *dc, const char *path, struct stat *st) { const char *slash = strrchr(path, '/'); if (slash == nullptr || slash == path || slash[1] == '\0') return sys_stat(path, st); size_t len = (size_t)(slash - path); if (dc->len != len || memcmp(dc->dir, path, len) != 0) { if (dc->fd >= 0) (void)sys_close(dc->fd); dc->fd = -1; dc->len = len; memcpy(dc->dir, path, len); return sys_stat(path, st); } if (dc->fd < 0) { dc->dir[len] = '\0'; dc->fd = sys_open(dc->dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0); if (dc->fd < 0) return sys_stat(path, st); } return sys_fstatat(dc->fd, slash + 1, st); } // True if the path still matches what the stamp line recorded. static bool unchanged(DirCache *dc, const char *path, bool recorded_missing, int64_t size, int64_t sec, int64_t nsec) { struct stat st; if (stat_path(dc, path, &st) != 0) return recorded_missing && (errno == ENOENT || errno == ENOTDIR); return !recorded_missing && st.st_size == size && platform_mtime_sec(&st) == sec && platform_mtime_nsec(&st) == nsec; } int stamp_check(bool report) { DirCache dir_cache; // dir is written before it is read dir_cache.len = 0; dir_cache.fd = -1; char path[STAMP_PATH_MAX]; if (suit_home_path(path, sizeof path, "stamp") == 0) return 1; ssize_t got = read_stamp(path); if (got == -2) { say(STDERR_FILENO, "suit: no stamp at %s; run `suit stamp PATH...` first\n", path); return EXIT_NO_STAMP; } if (got == -3) return bad_stamp(1, "stamp is larger than the limit"); if (got < 0) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(errno)); return 1; } char *p = file_buf; char *end = file_buf + got; int line = 0; // Without the format line, the first line is read as a path line of the // previous format and, if it is one, the whole stamp is a change. bool previous = (size_t)got < STAMP_FORMAT_LEN || memcmp(file_buf, STAMP_FORMAT, STAMP_FORMAT_LEN) != 0; if (!previous) { p += STAMP_FORMAT_LEN; line = 1; } int rc = EXIT_UNCHANGED; while (p < end) { char *nl = memchr(p, '\n', (size_t)(end - p)); line++; if (nl == nullptr) { rc = bad_stamp(line, "line has no newline"); break; } *nl = '\0'; char *s = p; p = nl + 1; bool missing = strncmp(s, "- - - ", 6) == 0; int64_t size = 0, sec = 0, nsec = 0; if (missing) s += 6; else s = read_number(read_number(read_number(s, &size), &sec), &nsec); if (s == nullptr) { rc = bad_stamp(line, "expected SIZE MTIME_SEC MTIME_NSEC PATH"); break; } if (s[0] == '\0') { rc = bad_stamp(line, "path is empty"); break; } if (previous) { if (report) say(STDOUT_FILENO, "changed: %s\n", path); rc = EXIT_CHANGED; break; } if (!unchanged(&dir_cache, s, missing, size, sec, nsec)) { if (report) say(STDOUT_FILENO, "changed: %s\n", s); rc = EXIT_CHANGED; } } if (dir_cache.fd >= 0) (void)sys_close(dir_cache.fd); return rc; }