crumb: derive generated data on demand instead of committing it (RFC-0003 amendment 1)
merged9ae5814c9d13db88 #271 · opened 2026-10-18 22:37 UTC · closed 2026-10-18 23:33 UTC · merged 2026-10-18 23:33 UTC
Imported from GitHub aien-dev/aien-architecture-mv#271, by drakestapleton
Labels: ready-to-merge
Base c9782c0bc4b80535cb5b1b7992cd6d6274fc7c28 · Head 8474dc4939d9225f2f453c13a93bdcb2f53cba97
Merged on GitHub as 12ad0141b41ec67ef5aa51cb4e1815894c1083bb; reachable from main bccab5ea7a17be6c9ab30b1bd05db4898a4601be (observed 2026-11-18T16:37:31Z)
drakestapleton wrote on 2026-10-18 22:37 UTC
What this fixes
Almost every open PR goes DIRTY as soon as any other PR merges, with a conflict only in generated .crumb files. Root cause: the generated block in every committed .crumb rolls up the whole tree below it (source_tree, children_root, digest) and carries two stamps (generated_at_commit, compiled_at). Every PR therefore rewrites the same lines in the root .crumb (and in every crumbed directory it touches), and GitHub's three-way merge cannot merge them.
Evidence (aien-sovereign-core-mv, 2026-10-18):
#608 changes only scripts/test-install-release.sh and conflicts with main only in .crumb, in the block children_root .. source_tree (all nine keys of the root generated block).
#605 conflicts only in .crumb and tools/aien-local-ci/.crumb.
Repro script on the Spark (~/workspace/swarm-crumbfix/repro-crumb-conflict.sh): scratch copy of sc main a75b7c25, two PRs per pair compiled per LOCAL_PIPELINE steps 2-4, first one squash-lands, second checked with git merge-tree. With the pinned tool 94fffbcf: RED, both pairs conflict (different directories: .crumb; same directory: .crumb tools/aien-local-ci/.crumb).
The change (RFC-0003 amendment 1, ACCEPTED 2026-10-18)
crumb migrate . (one time per repo): removes extensions.generated from every crumb and writes "extensions": {"generated_policy": "on-demand"} into the root .crumb. Semantic kernel untouched.
In an on-demand repo: a committed generated block is STALE (verify exit 1), compile strips it, verify prints one line with the root digest computed from the tree, crumb digest [dir] prints any generated block on demand.
Root digest after migration equals the digest the root crumb held before (sc: sha256:d1061244... both ways), so the provenance value is kept; the judge records it from the verify line (aien-test verify already takes the first stdout line as the crumb note).
No marker = legacy: behaviour unchanged.
Verified on the Spark
make -C tools/crumb test: 190 PASS, 0 FAIL. New AM1 cases include the conflict itself (RED on a legacy repo, GREEN after migrate, merged tree verifies with no recompile, a re-added generated block fails verify). Run against the old tool, the AM1 cases FAIL.
Legacy compatibility: crumb verify . on main of all 9 crumbed repos gives identical output and exit code with the old tool (94fffbcf) and this one. crumb compile after a source edit on sc: every .crumb byte-identical apart from compiled_at.
Repro with this tool after crumb migrate: GREEN. Neither PR carries a .crumb change, both pairs merge cleanly, and crumb verify . on the merged main prints OK with no recompile.
Not in this PR
No repository is migrated here (aien-architecture itself stays legacy; its crumbs are recompiled for this change, as the last commit).
Per-repo migration plan and the example migration PR for sovereign-core are linked from the handoff note ~/handoffs/2026-10-18-swarm-crumb-conflict.md on the Spark.
Decision: ACCEPTED by Drake (owner) on 2026-10-18: "Accept: merge #271, then migrate repo by repo once the current merges land." Merging the tool alone changes nothing for any repository; each repository changes only when its migration PR lands.
Do not merge from this agent; the desk merges.
Update 2026-10-18 (Hermes desk)
Status PROPOSED -> ACCEPTED in the RFC and CRUMB_PROTOCOL.md.
CodeQL cpp/world-writable-file-creation fixed: files are created through one helper with mode 0644 (also covers the three older call sites flagged on main).
github-advanced-security[bot] reviewed (COMMENTED) on 2026-10-18 22:38 UTC
github-advanced-security[bot] commented on tools/crumb/crumb.c line 2068 on 2026-10-18 22:38 UTC
CodeQL / File created without restricting permissions
A file may be created here with mode 0666, which would make it world-writable.
Drake (owner) accepted RFC-0003 Amendment 1 on 2026-10-18: "Accept: merge #271, then migrate repo by repo once the current merges land."
Recorded in this PR: the amendment status moves from PROPOSED to ACCEPTED (docs/CRUMB_PROTOCOL_RFC_0003.md, docs/CRUMB_PROTOCOL.md). Same push fixes the CodeQL finding on this PR (cpp/world-writable-file-creation in the new write_crumb): every file the tool creates now goes through one helper that opens it with mode 0644, which also covers the three older call sites flagged on main. make -C tools/crumb test passes; a .crumb created under umask 000 is now 644 instead of 666.
Next, in order: Spark verdict on the new head, merge, then one migration PR per repository.
Apply it
git checkout -b patch-9ae5814c9d13db88c9782c0bc4b80535cb5b1b7992cd6d6274fc7c28
curl -fsSL https://interplane.io/drakestapleton/aien-architecture-mv/patches/9ae5814c9d13db88.patch | git am
diff --git a/.crumb b/.crumb
index 5b7143d..290febc 100644
--- a/.crumb
+++ b/.crumb
@@ -21,8 +21,8 @@
"generated": {
"children": [
{
- "digest": "dc9687a0c68a563246103ba455ac3dd4591089c3c23da3382c43dbc073bb175e",
- "last_commit": "e70afff",
+ "digest": "eba25716efe9b9c8022ba2711986630ac3a3a69499df89faa061d7918e22f002",
+ "last_commit": "4a28ae4",
"name": "docs",
"purpose": "Directory docs. Purpose not yet described by a human."
},
@@ -39,21 +39,21 @@
"purpose": "Directory scripts. Purpose not yet described by a human."
},
{
- "digest": "afa286ef45ae5a0a0fd880a27288f50f82138a2a092a0c3684c59597d20c9070",
- "last_commit": "7291eb3",
+ "digest": "c440b2e2a4f90f9e2058dfda841e641ed5ce127682ce2aaebda34016b5f24efd",
+ "last_commit": "4a28ae4",
"name": "tools",
"purpose": "Directory tools. Purpose not yet described by a human."
}
],
- "children_root": "sha256:49abcaf492f4a1b7c2a771c83f8de97a39c88de3dba96ea14910831bfe11eb10",
- "compiled_at": "2026-10-18T21:09:55Z",
+ "children_root": "sha256:43981e7d8b83e9d61cf5924eebedcf3476365acaf8b8f0461e739933ca621a20",
+ "compiled_at": "2026-10-18T23:15:13Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:e55c84d781cde00dbf007f9b093aa73147202e4264fa916d78a115b0bf2b378b",
+ "digest": "sha256:b634825497d267b5dd77893a464d3bab990fab14366b6458d2f50b1db30abc7c",
"evidence_files": 0,
"evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"files": 316,
- "generated_at_commit": "e70afffb36ec6002ca9eb85595af9857fcb54374",
- "source_tree": "sha256:3782f56007e667023672762e9ccbe37239f4c3155c57702415894416dcd28471"
+ "generated_at_commit": "4a28ae418dc4cef124f04642f273e6720ff22b4b",
+ "source_tree": "sha256:978cc8e7a18d8ef371bf34ff199bda2f7b8a0242341e471386c729ddc9206aa1"
},
"owner_lane": "",
"provenance": {
diff --git a/docs/.crumb b/docs/.crumb
index ad74c6c..9a664db 100644
--- a/docs/.crumb
+++ b/docs/.crumb
@@ -49,8 +49,8 @@
"purpose": "Directory docs/archive/plans. Purpose not yet described by a human."
},
{
- "digest": "ace122c86c42992a65ab53566fd1f514734e9762ef6d19363fc4a21cfc018bb7",
- "last_commit": "ac6754c",
+ "digest": "018797942e46edb7c720cda4539bebaab6238692e84cd6281ed42d5893006863",
+ "last_commit": "5fbfd88",
"name": "crumbs",
"purpose": "Directory docs/crumbs. Purpose not yet described by a human."
},
@@ -68,7 +68,7 @@
},
{
"digest": "2220df2445397168ec9ed1aafbd8fd7c4ee5de88a42bc763a7d3267521725f9b",
- "last_commit": "13b8df5",
+ "last_commit": "e70afff",
"name": "research",
"purpose": "Directory docs/research. Purpose not yet described by a human."
},
@@ -79,15 +79,15 @@
"purpose": "Directory docs/specs. Purpose not yet described by a human."
}
],
- "children_root": "sha256:e2da2c1de8b46e7d9fd28529f9afba0a3062e58d1a0c9057308b69298092db7e",
- "compiled_at": "2026-10-18T19:26:30Z",
+ "children_root": "sha256:61b556b25bb6d973ed4b46631e50ee2986526f5a75419d9eddb79d4e73773c20",
+ "compiled_at": "2026-10-18T23:15:13Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:dc9687a0c68a563246103ba455ac3dd4591089c3c23da3382c43dbc073bb175e",
+ "digest": "sha256:eba25716efe9b9c8022ba2711986630ac3a3a69499df89faa061d7918e22f002",
"evidence_files": 0,
"evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"files": 179,
- "generated_at_commit": "13b8df5e2ad59659a1a8a55e4708dbad80b8f8a7",
- "source_tree": "sha256:3c3b2c5a05b6a58c96b7ce5e01b1123c1409ce0701ef7c35a1d54822b90dcb63"
+ "generated_at_commit": "4a28ae418dc4cef124f04642f273e6720ff22b4b",
+ "source_tree": "sha256:4246558b7e04cad9755dfbb32fb4ff4fcb4cf91fb4ba39db31b616a2b6c6c273"
},
"owner_lane": "",
"provenance": {
diff --git a/docs/CRUMB_PROTOCOL.md b/docs/CRUMB_PROTOCOL.md
index 43d1f13..52e8afe 100644
--- a/docs/CRUMB_PROTOCOL.md
+++ b/docs/CRUMB_PROTOCOL.md
@@ -21,7 +21,7 @@ If the Rust crate and RFC-0001 disagree, RFC-0001 wins. The tool leaves legacy f
RFC-0002 (`docs/CRUMB_PROTOCOL_RFC_0002.md`) extends this protocol with a repository-wide coordination plane shared by all worktrees, `crumb explain`, and Continuation Crumbs for session handoff; agent workflow in `docs/crumbs/AGENT_WORKFLOW.md`.
-RFC-0003 (`docs/CRUMB_PROTOCOL_RFC_0003.md`) adds the Crumb Compiler: structural crumb data is generated (`crumb compile`), checked by `crumb verify` in CI, and read through `crumb context`.
+RFC-0003 (`docs/CRUMB_PROTOCOL_RFC_0003.md`) adds the Crumb Compiler: structural crumb data is generated (`crumb compile`), checked by `crumb verify` in CI, and read through `crumb context`. Its amendment 1 (2026-10-18, ACCEPTED) stops committing that generated data: an opted-in repository derives it on demand (`crumb verify`, `crumb digest`), so unrelated PRs no longer conflict in `.crumb` files.
## The two files
diff --git a/docs/CRUMB_PROTOCOL_RFC_0003.md b/docs/CRUMB_PROTOCOL_RFC_0003.md
index 27e52b4..d01861d 100644
--- a/docs/CRUMB_PROTOCOL_RFC_0003.md
+++ b/docs/CRUMB_PROTOCOL_RFC_0003.md
@@ -71,7 +71,7 @@ Caveat for stages 4-5: Omega's program representation is still narrow (unary-u64
## 9. Tests
-`make -C tools/crumb test` runs `test.sh` and `test-continuation.sh`. The RFC-0003 cases are the lines starting `C3`.
+`make -C tools/crumb test` runs `test.sh` and `test-continuation.sh`. The RFC-0003 cases are the lines starting `C3`; amendment 1 cases start `AM1`.
| Test | Result |
|---|---|
@@ -85,3 +85,30 @@ Caveat for stages 4-5: Omega's program representation is still narrow (unary-u64
| Differential against `crumb-compile.sh` on a scratch repo and on this repo (both tools compile from nothing, so `last_commit` is fresh in both): generated blocks identical, each tool verifies the other's output, whole file byte-identical apart from `compiled_at` | PASS (reported NOT_RUN if `jq` is missing) |
| Number literals other than plain integers inside the semantic kernel (jq canonicalises them, the C tool keeps them verbatim) | NOT_RUN (UNVERIFIED) |
| Paths containing spaces or newlines (the shell reference splits them; the C tool does not) | NOT_RUN (UNVERIFIED) |
+
+## Amendment 1 (2026-10-18): generated data is derived on demand, not committed
+
+**Status:** ACCEPTED by Drake (owner), 2026-10-18: "Accept: merge #271, then migrate repo by repo once the current merges land." Changes a decision of this RFC (section 2 and the CI rule of section 6: "the generated block is committed and `verify` compares it"). Each repository migrates in its own PR after this tool lands. The tool change is backward compatible, so merging the tool alone changes nothing for any repository.
+
+### Problem, with evidence
+
+Every PR rewrites the root `.crumb`. The root's `source_tree` covers every tracked file, `children_root` and `digest` roll up every child digest, and `generated_at_commit` and `compiled_at` are rewritten by every compile. These keys sit next to each other at the end of the generated block, so two PRs that touch unrelated files both change the same lines. GitHub computes mergeability with a plain three-way merge, so the second PR goes DIRTY the moment the first merges. The same happens inside any crumbed directory both PRs touch (its `source_tree`, `files` and stamps).
+
+Seen on aien-sovereign-core-mv, 2026-10-18: #608 changes only `scripts/test-install-release.sh` and conflicts with main only in `.crumb`, inside the block `children_root` .. `source_tree`; #605 conflicts only in `.crumb` and `tools/aien-local-ci/.crumb`. The scripted repro (`swarm-crumbfix/repro-crumb-conflict.sh` on the Spark) reproduces both shapes on a scratch copy of sovereign-core main `a75b7c25` with the pinned tool `94fffbcf`. With sovereign-core #657 (merge only PRs that are current with main) every merge therefore forces a rebase, a recompile and a full re-judge of every other open PR.
+
+The committed values carry no information that is not already in the commit: `digest` is a pure function of the tracked tree (D2, D3), and git's own tree id already binds that tree. No program outside `tools/crumb` reads them (searched all 11 judged repositories on 2026-10-18: only the tool, its tests and its proof scripts read `extensions.generated`). ADR 0029 (Verified Crumbs) is not affected; it excludes `.crumb` explicitly.
+
+### Decision
+
+1. A repository opts in by carrying `"extensions": {"generated_policy": "on-demand"}` in its root `.crumb`. `crumb migrate .` writes the marker and removes `extensions.generated` from every crumb, once, in one commit. Nothing else in any crumb changes; the semantic kernel is untouched (D5).
+2. In an on-demand repository the committed `.crumb` holds only what people write (the semantic kernel, `extensions.seed`, `extensions.proposed`, `extensions.provenance`). A committed `extensions.generated` is STALE: `verify` exits 1 and names the crumb, and `compile` strips it. This keeps an old tool from quietly bringing the conflicts back.
+3. Digests are still computed, exactly as sections 2 and 3 define them, every time they are needed: `crumb verify` prints `crumb verify: OK: <n> crumbs current at HEAD <sha7>, generated data on demand, root digest sha256:<hex>`, and `crumb digest [dir]` prints the full generated block of any directory. The root digest of a migrated tree equals the digest the root crumb held before migration, so nothing is lost: the judge records the printed root digest with the SHA it judged (the first stdout line is already the readiness note in `aien-test verify`, `crumb_status`).
+4. Deleting a crumb still changes the root digest (D7), and is also a plain line in the PR diff.
+5. A repository without the marker is legacy: every command behaves byte for byte as before (test `AM1 legacy ...` checks the verify line). This is what lets the shared sandbox tool on the Spark move first, and each repository migrate in its own PR.
+
+### Consequences
+
+- Two PRs that touch different files never conflict in a crumb. Two PRs only conflict in a crumb when both edit its hand-written text, which is a real conflict a person must resolve.
+- A PR that changes no hand-written crumb text carries no `.crumb` change and no `crumb compile` commit. `LOCAL_PIPELINE.md` steps 2 to 4 become a no-op unless a crumb was edited; `rebase-crumb.sh` and `pr-gate.sh` work unchanged (verify still prints a line with `OK`).
+- Agents that read one crumb to learn its children use `crumb digest <dir>` or `crumb context <path>` instead of reading `extensions.generated` from the file.
+- Rejected alternatives, in order: dropping only the roll-up (keeps the conflict for any two PRs in the same crumbed directory, which the repro shows with `tools/aien-local-ci`); auto-rebasing crumb-only conflicts (removes the manual step but not the rebase-and-re-judge on every merge); a `.gitattributes` merge driver (GitHub's server-side merge and its mergeability check do not run custom drivers, so the PR stays DIRTY).
diff --git a/docs/crumbs/.crumb b/docs/crumbs/.crumb
index 5391b99..5957d65 100644
--- a/docs/crumbs/.crumb
+++ b/docs/crumbs/.crumb
@@ -7,14 +7,14 @@
"generated": {
"children": [],
"children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
- "compiled_at": "2026-10-14T01:16:44Z",
+ "compiled_at": "2026-10-18T22:37:04Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:ace122c86c42992a65ab53566fd1f514734e9762ef6d19363fc4a21cfc018bb7",
+ "digest": "sha256:018797942e46edb7c720cda4539bebaab6238692e84cd6281ed42d5893006863",
"evidence_files": 0,
"evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"files": 5,
- "generated_at_commit": "27ac46f5a8c8950002d48535f838c0982c9e5d35",
- "source_tree": "sha256:65e27a33c5ff5123f4f7696433c2e5ba79eeef2e2a5831af699f96b580d12608"
+ "generated_at_commit": "5fbfd883a6bdd816ffa6bd82a2de5011b2cd1539",
+ "source_tree": "sha256:c64e3fa5b7a7355dc58a31086e387393ab59be9e000eb1cff09aff0fbb4bdf1a"
},
"owner_lane": "",
"seed": {
diff --git a/docs/crumbs/LOCAL_PIPELINE.md b/docs/crumbs/LOCAL_PIPELINE.md
index ab72bd7..63cdfa0 100644
--- a/docs/crumbs/LOCAL_PIPELINE.md
+++ b/docs/crumbs/LOCAL_PIPELINE.md
@@ -30,3 +30,7 @@ The pipeline worker (`aien-test worker`, ADR 0033 amendment) follows the same ta
6. Any source edit after that commit reopens step 1. A rebase onto a moved `main` goes through `scripts/rebase-crumb.sh`, which takes main's crumbs, recompiles, and commits the crumbs again as the new last commit (proof 20).
Measured on this repository and on sovereign-core main, 2026-10-11: `scripts/pipeline-proofs/RESULTS-2026-10-11.md`.
+
+### On-demand repositories (RFC-0003 amendment 1)
+
+A repository whose root `.crumb` carries `"extensions": {"generated_policy": "on-demand"}` commits no generated data. Steps 2 to 4 still run, but `crumb compile` rewrites nothing unless a crumb carries a committed `extensions.generated` block, so a PR that edits no crumb text has no crumb commit and its final SHA is its last source commit. `crumb verify .` prints the root digest computed from the tree; that line is what the judge records with the SHA. A rebase onto a moved `main` can no longer conflict in a crumb unless both sides edited the same hand-written text. Migrate a repository once with `crumb migrate .` (one commit, crumbs only), together with the bump of the repository's crumb tool pin.
diff --git a/tools/.crumb b/tools/.crumb
index fee0aa8..ddee179 100644
--- a/tools/.crumb
+++ b/tools/.crumb
@@ -13,21 +13,21 @@
"generated": {
"children": [
{
- "digest": "46303512a5bede09aaecf195930c326d80f38fc670dcf3fe2b585aa1989505fa",
- "last_commit": "7291eb3",
+ "digest": "f735474c6a42025d6fc3963c8f812de06bc54419ad192d2270ad5b3543e02644",
+ "last_commit": "4a28ae4",
"name": "crumb",
"purpose": "Directory tools/crumb. Purpose not yet described by a human."
}
],
- "children_root": "sha256:3528e983fc9cadf34fb3623db8eff77b0c2e110e62e472db3c0019dc8526dfa1",
- "compiled_at": "2026-10-16T11:11:15Z",
+ "children_root": "sha256:0fca8abfa615f91bec489fa551104339c891505ed54f8b28b232ab6fa1ffab19",
+ "compiled_at": "2026-10-18T23:15:13Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:afa286ef45ae5a0a0fd880a27288f50f82138a2a092a0c3684c59597d20c9070",
+ "digest": "sha256:c440b2e2a4f90f9e2058dfda841e641ed5ce127682ce2aaebda34016b5f24efd",
"evidence_files": 0,
"evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"files": 7,
- "generated_at_commit": "dccc00620d24481bf4b12f6b567c2d496f4e652a",
- "source_tree": "sha256:2e78ac3a2dea300d11eba0a0693b591903e4836769d5d9f2a468241e5208b73d"
+ "generated_at_commit": "4a28ae418dc4cef124f04642f273e6720ff22b4b",
+ "source_tree": "sha256:3aae76fffcb0ffbb52de877e2c5254ce3000e2b42e479206e76719f63fdfc1bb"
},
"owner_lane": "",
"provenance": {
diff --git a/tools/crumb/.crumb b/tools/crumb/.crumb
index 9a6326e..fcd1d65 100644
--- a/tools/crumb/.crumb
+++ b/tools/crumb/.crumb
@@ -7,14 +7,14 @@
"generated": {
"children": [],
"children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
- "compiled_at": "2026-10-16T11:11:15Z",
+ "compiled_at": "2026-10-18T23:15:13Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:46303512a5bede09aaecf195930c326d80f38fc670dcf3fe2b585aa1989505fa",
+ "digest": "sha256:f735474c6a42025d6fc3963c8f812de06bc54419ad192d2270ad5b3543e02644",
"evidence_files": 0,
"evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"files": 7,
- "generated_at_commit": "dccc00620d24481bf4b12f6b567c2d496f4e652a",
- "source_tree": "sha256:2e78ac3a2dea300d11eba0a0693b591903e4836769d5d9f2a468241e5208b73d"
+ "generated_at_commit": "4a28ae418dc4cef124f04642f273e6720ff22b4b",
+ "source_tree": "sha256:3aae76fffcb0ffbb52de877e2c5254ce3000e2b42e479206e76719f63fdfc1bb"
},
"owner_lane": "",
"provenance": {
diff --git a/tools/crumb/crumb.c b/tools/crumb/crumb.c
index fc755ef..c272f29 100644
--- a/tools/crumb/crumb.c
+++ b/tools/crumb/crumb.c
@@ -259,10 +259,19 @@ static char *slurp(const char *path) {
fclose(f);
return b.p;
}
+/* Every file this tool creates is mode 0644 (minus umask), never 0666: fopen() would create it world-writable
+ * under a permissive umask (CodeQL cpp/world-writable-file-creation). append=1 opens for appending. */
+static FILE *fcreate(const char *path, int append) {
+ int fd = open(path, O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC), 0644);
+ if (fd < 0) return NULL;
+ FILE *f = fdopen(fd, append ? "a" : "wb");
+ if (!f) close(fd);
+ return f;
+}
static void spit(const char *path, const char *data) {
size_t tn = strlen(path) + 32; char *tmp = xmalloc(tn);
snprintf(tmp, tn, "%s.tmp%ld", path, (long)getpid());
- FILE *f = fopen(tmp, "wb");
+ FILE *f = fcreate(tmp, 0);
if (!f) die("cannot write %s: %s", tmp, strerror(errno));
if (fputs(data, f) < 0 || fclose(f) != 0) die("write failed: %s", tmp);
if (rename(tmp, path) != 0) die("rename failed: %s", path);
@@ -339,7 +348,7 @@ static void git_exclude_local(const char *dir) { /* keep .crumb.local out of `
char *full = gp[0] == '/' ? xstrdup(gp) : pjoin(dir, gp);
char *txt = slurp(full);
if (txt) { char *c = xstrdup(txt); for (char *l = strtok(c, "\n"); l; l = strtok(NULL, "\n")) if (!strcmp(l, ".crumb.local")) return; }
- FILE *f = fopen(full, "a"); if (!f) return;
+ FILE *f = fcreate(full, 1); if (!f) return;
fputs(txt && *txt && txt[strlen(txt) - 1] != '\n' ? "\n.crumb.local\n" : ".crumb.local\n", f); fclose(f);
}
static void local_save(Local *l) {
@@ -2048,7 +2057,30 @@ static void find_crumbs(const char *top, const char *rel, CDir **out, size_t *n,
closedir(dp); free(full);
}
-/* mode: 'c' compile, 'v' verify, 's' status. Returns number of stale crumbs; *total set. */
+/* RFC-0003 amendment 1 (2026-10-18): generated data is derived on demand, never committed.
+ * A repository opts in with "extensions": {"generated_policy": "on-demand"} in its ROOT .crumb (written once by
+ * `crumb migrate`). Then every committed crumb holds only what a human writes; a committed extensions.generated
+ * block is STALE (it is the roll-up that made two unrelated PRs rewrite the same root lines), compile strips it,
+ * verify prints the root digest computed from the tree at HEAD, and `crumb digest` prints any generated block.
+ * Without the marker the repository is legacy and every command behaves exactly as before the amendment. */
+#define GEN_POLICY_KEY "generated_policy"
+#define GEN_POLICY_ON_DEMAND "on-demand"
+static const char *g_digest_dir = NULL; /* mode 'd': repo-relative directory whose generated block to print */
+static int root_on_demand(const char *top) {
+ J *rc = load_crumb(top); J *x = rc ? jget(rc, "extensions") : NULL;
+ const char *p = x && x->t == JOBJ ? jstrval(x, GEN_POLICY_KEY) : NULL;
+ return p && !strcmp(p, GEN_POLICY_ON_DEMAND);
+}
+static void write_crumb(const char *full, J *cr) { /* D9: key-sorted, temp file then rename */
+ jsort(cr);
+ char *out = jdump(cr), *tmp = xmalloc(strlen(full) + 32); snprintf(tmp, strlen(full) + 32, "%s.tmp.%ld", full, (long)getpid());
+ FILE *f = fcreate(tmp, 0); if (!f) die("cannot write %s", tmp);
+ if (fputs(out, f) < 0 || fclose(f) != 0) die("write failed: %s", tmp);
+ if (rename(tmp, full)) die("rename failed: %s", full);
+}
+
+/* mode: 'c' compile, 'v' verify, 's' status, 'm' migrate (to on-demand), 'd' digest (print one generated block).
+ * Returns number of stale crumbs; *total set. */
static int compile_engine(char mode, const char *root, int quiet, int *total_out, char *head_out) {
Comp c; memset(&c, 0, sizeof c);
char *rq = shq(root), cmd[PATH_MAX + 80]; snprintf(cmd, sizeof cmd, "git -C %s rev-parse --show-toplevel 2>/dev/null", rq);
@@ -2077,13 +2109,34 @@ static int compile_engine(char mode, const char *root, int quiet, int *total_out
if (!dup) ds[w++] = ds[i]; } nd = w; }
for (size_t i = 0; i < nd; i++) { int n = 0; if (!strcmp(ds[i].d, ".")) n = -1; else for (const char *s = ds[i].d; *s; s++) if (*s == '/') n++; ds[i].depth = n; }
qsort(ds, nd, sizeof *ds, cdcmp);
- int total = 0, changed = 0, stale = 0; char stamp[21]; now_iso(stamp);
+ int total = 0, changed = 0, stale = 0, found = 0; char stamp[21]; now_iso(stamp);
+ int ondemand = mode == 'm' || root_on_demand(top);
for (size_t i = 0; i < nd; i++) {
const char *d = ds[i].d; char *cp = pjoin(d, ".crumb"), *full = pjoin(top, cp); struct stat sb;
if (stat(full, &sb) || !S_ISREG(sb.st_mode)) continue;
total++;
char dg[65]; J *nw = comp_object(&c, d, dg); dig_set(&c, d, dg);
+ if (mode == 'd') { /* on demand: the block compile would have committed, with this HEAD as its stamp */
+ if (g_digest_dir && !strcmp(d, g_digest_dir)) { jset(nw, "generated_at_commit", jstr(head)); jsort(nw); fputs(jdump(nw), stdout); found = 1; }
+ continue;
+ }
char *txt = slurp(full); J *cr = txt ? jparse(txt) : NULL;
+ if (ondemand) { /* amendment 1: only the hand-written crumb is committed */
+ J *x = cr && cr->t == JOBJ ? jget(cr, "extensions") : NULL;
+ int has_gen = x && x->t == JOBJ && jget(x, "generated");
+ int need_mark = mode == 'm' && !strcmp(d, ".") && !root_on_demand(top);
+ if (!cr || cr->t != JOBJ) {
+ stale++; if (!quiet) printf("INVALID %s (not a JSON object)\n", d);
+ if (mode == 'c' || mode == 'm') die("cannot compile %s: not a JSON object", cp);
+ continue;
+ }
+ if (!has_gen && !need_mark) { if (mode == 's' && !quiet) printf("CURRENT %s\n", d); continue; }
+ if (mode == 's' || mode == 'v') { stale++; if (!quiet) printf("STALE %s (committed generated data; run: crumb compile)\n", d); continue; }
+ if (has_gen) { jdel(x, "generated"); if (x->n == 0) jdel(cr, "extensions"); }
+ if (need_mark) { J *rx = jget(cr, "extensions"); if (!rx || rx->t != JOBJ) { rx = jnew(JOBJ); jset(cr, "extensions", rx); } jset(rx, GEN_POLICY_KEY, jstr(GEN_POLICY_ON_DEMAND)); }
+ write_crumb(full, cr); changed++; if (!quiet) printf("STRIPPED %s\n", d);
+ continue;
+ }
J *old = jnew(JOBJ);
if (cr && cr->t == JOBJ) { J *x = jget(cr, "extensions"), *g = x && x->t == JOBJ ? jget(x, "generated") : NULL;
if (g && g->t == JOBJ) { old = jclone(g); jdel(old, "generated_at_commit"); jdel(old, "compiled_at"); } }
@@ -2100,7 +2153,7 @@ static int compile_engine(char mode, const char *root, int quiet, int *total_out
J *g = jclone(nw); jset(g, "generated_at_commit", jstr(head)); jset(g, "compiled_at", jstr(stamp));
jset(x, "generated", g); jsort(cr);
char *out = jdump(cr), *tmp = xmalloc(strlen(full) + 32); snprintf(tmp, strlen(full) + 32, "%s.tmp.%ld", full, (long)getpid());
- FILE *f = fopen(tmp, "wb"); if (!f) die("cannot write %s", tmp);
+ FILE *f = fcreate(tmp, 0); if (!f) die("cannot write %s", tmp);
if (fputs(out, f) < 0 || fclose(f) != 0) die("write failed: %s", tmp);
if (rename(tmp, full)) die("rename failed: %s", full);
changed++; if (!quiet) printf("COMPILED %s\n", d);
@@ -2108,6 +2161,17 @@ static int compile_engine(char mode, const char *root, int quiet, int *total_out
}
if (total_out) *total_out = total;
char h7[8]; memcpy(h7, head, 7); h7[7] = 0;
+ const char *rd = dig_of(&c, "."); char rds[80]; snprintf(rds, sizeof rds, "sha256:%s", rd ? rd : "none");
+ if (mode == 'd') { if (!found) die("crumb digest: %s holds no compiled .crumb", g_digest_dir ? g_digest_dir : "?"); return 0; }
+ if (!quiet && ondemand) { /* one line: verify.rs takes the first stdout line as the readiness note */
+ if (mode == 'c') printf("crumb compile: %d crumbs, %d rewritten, HEAD %s, generated data on demand, root digest %s\n", total, changed, h7, rds);
+ else if (mode == 'm') printf("crumb migrate: %d crumbs, %d rewritten, generated data is now derived on demand, root digest %s\n", total, changed, rds);
+ else if (mode == 'v') {
+ if (stale) printf("crumb verify: FAIL: %d of %d crumbs stale; run: crumb compile\n", stale, total);
+ else printf("crumb verify: OK: %d crumbs current at HEAD %s, generated data on demand, root digest %s\n", total, h7, rds);
+ } else printf("crumb status: %d crumbs, %d not current\n", total, stale);
+ return stale;
+ }
if (!quiet) {
if (mode == 'c') printf("crumb compile: %d crumbs, %d rewritten, HEAD %s\n", total, changed, h7);
else if (mode == 'v') {
@@ -2118,6 +2182,14 @@ static int compile_engine(char mode, const char *root, int quiet, int *total_out
return stale;
}
static int cmd_compile(char mode, const char *root) { int t; int s = compile_engine(mode, root, 0, &t, NULL); return mode == 'v' && s ? 1 : 0; }
+static int cmd_digest(const char *dir) { /* amendment 1: print a directory's generated block, computed now */
+ char r[PATH_MAX]; if (!realpath(dir, r)) die("no such directory: %s", dir);
+ char *rq = shq(r), cmd[PATH_MAX + 80]; snprintf(cmd, sizeof cmd, "git -C %s rev-parse --show-toplevel 2>/dev/null", rq);
+ char *top = cap_cmd(cmd, NULL); chomp(top);
+ if (!*top) die("crumb digest: not inside a git repository: %s", dir);
+ size_t tl = strlen(top); g_digest_dir = r[tl] ? xstrdup(r + tl + 1) : xstrdup(".");
+ int t; compile_engine('d', top, 1, &t, NULL); return 0;
+}
static int cmd_propose(const char *dir, const char *purpose) { /* D5: only extensions.proposed, never .purpose */
char *cp = pjoin(dir, ".crumb"), *txt = slurp(cp);
if (!txt) die("no .crumb in %s", dir);
@@ -2242,6 +2314,8 @@ static void usage(void) {
" compile [root] RFC-0003: regenerate structural crumb data bottom-up (Merkle digests)\n"
" verify [root] exit 1 if any committed crumb differs from what compile would write\n"
" status [root] CURRENT / STALE / UNCOMPILED per crumb\n"
+ " migrate [root] RFC-0003 am. 1: strip committed generated data, mark the root on-demand\n"
+ " digest [dir] print the generated block of dir, computed from the tree now\n"
" propose <dir> <purpose> write extensions.proposed.purpose (never .purpose)\n"
" context [path] entry point: tree, what to read, conflicts, continuation, freshness\n"
" checkpoint <agent> [--reason R] new Continuation Crumb (wraps cc.sh); then checkpoint --seal <id> [agent]\n"
@@ -2279,6 +2353,8 @@ int main(int argc, char **argv) {
if (!strcmp(c, "compile") && a <= 1) return cmd_compile('c', a ? v[0] : ".");
if (!strcmp(c, "verify") && a <= 1) return cmd_compile('v', a ? v[0] : ".");
if (!strcmp(c, "status") && a <= 1) return cmd_compile('s', a ? v[0] : ".");
+ if (!strcmp(c, "migrate") && a <= 1) return cmd_compile('m', a ? v[0] : ".");
+ if (!strcmp(c, "digest") && a <= 1) return cmd_digest(a ? v[0] : ".");
if (!strcmp(c, "propose") && a == 2) return cmd_propose(v[0], v[1]);
if (!strcmp(c, "context") && a <= 1) return cmd_context(a ? v[0] : ".");
if (!strcmp(c, "checkpoint")) return cmd_checkpoint(a, v);
diff --git a/tools/crumb/test.sh b/tools/crumb/test.sh
index 6938285..fe1494b 100644
--- a/tools/crumb/test.sh
+++ b/tools/crumb/test.sh
@@ -224,6 +224,45 @@ git add .gitignore .crumb src keep; git -c user.name=t -c user.email=t@t commit
echo 'int q;' >> target/debug/build/x/out/auth/a.rs; echo 'more' > out/cache/more.c
"$C" verify . >/dev/null && ok "355 changing files inside build dirs does not stale anything" || bad "355 build-dir change staled crumbs"
cd "$SAVE" || exit 1
+# ---- RFC-0003 amendment 1 (2026-10-18): generated data derived on demand, never committed ----
+# The regression this guards: two PRs touching unrelated files both rewrite the root crumb's roll-up lines
+# (source_tree, children_root, digest, stamps), so after the first squash-merges the second CONFLICTS on GitHub.
+SAVE=$(pwd); AM="$T/am1"; rm -rf "$AM"; mkdir -p "$AM/src/deep" "$AM/docs" "$AM/evidence" && cd "$AM" || exit 1
+gc() { git -c user.name=t -c user.email=t@t -c commit.gpgsign=false "$@"; }
+git init -q -b main . && echo 'int a;' > src/a.c && echo 'int b;' > src/deep/b.c && echo n > docs/n.md && echo r > evidence/r.json && echo top > README
+"$C" seed . >/dev/null; git add -A; "$C" compile . >/dev/null; git add -A; gc commit -qm "legacy compiled"
+[ "$("$C" verify .)" = "crumb verify: OK: 4 crumbs current" ] && ok "AM1 legacy repo (no root marker): verify output unchanged" || bad "AM1 legacy verify output changed"
+twoprs() { # twoprs <tag>: PR A edits src/a.c, PR B edits docs/n.md, each compiled per protocol; A squash-lands; prints B's conflicts
+ for b in A B; do gc checkout -q -b "$1$b" main; [ $b = A ] && echo "/* $1 */" >> src/a.c || echo "$1" >> docs/n.md
+ gc commit -qam "$1$b edit"; "$C" compile . >/dev/null; [ -n "$(git status --porcelain)" ] && { git add -A; gc commit -qm "crumb compile"; }; gc checkout -q main; done
+ gc merge -q --squash "$1A" >/dev/null 2>&1; gc commit -qm "squash $1A"
+ git merge-tree --write-tree --name-only main "$1B" | sed -n '2,/^$/p' | grep -v '^$' | tr '\n' ' '
+}
+conf=$(twoprs leg); [ -n "$conf" ] && ok "AM1 RED reproduced on legacy: unrelated PR conflicts after the first lands (in: $conf)" || bad "AM1 legacy conflict not reproduced (test cannot go red)"
+gc reset -q --hard HEAD~1
+legacy_root=$(jq -r .extensions.generated.digest .crumb); sem0=$(for f in $(git ls-files '*.crumb' '**/.crumb'); do jq -cS '{purpose,layer,invariants,exports,related,boundaries}' "$f"; done)
+"$C" migrate . | grep -q '^crumb migrate: 4 crumbs, 4 rewritten' && ok "AM1 migrate rewrites every crumb once" || bad "AM1 migrate"
+"$C" migrate . | grep -q ', 0 rewritten' && ok "AM1 migrate is idempotent" || bad "AM1 migrate idempotent"
+[ "$(git ls-files '*.crumb' '**/.crumb' | xargs cat | grep -c '"generated"')" = 0 ] && ok "AM1 no committed crumb carries extensions.generated" || bad "AM1 generated left behind"
+[ "$(jq -r .extensions.generated_policy .crumb)" = on-demand ] && ok "AM1 root crumb carries generated_policy on-demand" || bad "AM1 root marker"
+[ "$sem0" = "$(for f in $(git ls-files '*.crumb' '**/.crumb'); do jq -cS '{purpose,layer,invariants,exports,related,boundaries}' "$f"; done)" ] && ok "AM1 migrate never touches the semantic kernel" || bad "AM1 kernel changed"
+v=$("$C" verify .); [ $? -eq 0 ] && echo "$v" | grep -q "^crumb verify: OK: 4 crumbs current at HEAD .*root digest $legacy_root$" && ok "AM1 verify OK and prints the same root digest the legacy root crumb held (provenance kept)" || bad "AM1 verify/root digest ($v vs $legacy_root)"
+[ "$(echo "$v" | wc -l)" = 1 ] && ok "AM1 verify prints one line (verify.rs readiness note)" || bad "AM1 verify lines"
+[ "$("$C" digest . | jq -r .digest)" = "$legacy_root" ] && "$C" digest src | jq -e '.children | map(.name) == ["deep"]' >/dev/null && ok "AM1 digest prints the generated block on demand" || bad "AM1 digest"
+gc commit -qam "crumb migrate"
+conf=$(twoprs od); [ -z "$conf" ] && ok "AM1 GREEN: after migration the unrelated PR merges cleanly" || bad "AM1 on-demand still conflicts (in: $conf)"
+gc merge -q --squash odB >/dev/null 2>&1 && gc commit -qm "squash odB"; "$C" verify . >/dev/null && ok "AM1 GREEN: merged main verifies with no recompile" || bad "AM1 merged main does not verify"
+[ -z "$(git diff --name-only HEAD~2 HEAD | grep '\.crumb$')" ] && ok "AM1 neither PR changed any .crumb" || bad "AM1 PRs touched crumbs"
+d1=$("$C" digest . | jq -r .digest); echo x >> src/deep/b.c; "$C" verify . >/dev/null && [ "$("$C" digest . | jq -r .digest)" != "$d1" ] && ok "AM1 a source change keeps verify OK and moves the computed root digest" || bad "AM1 source change"
+git checkout -q -- src/deep/b.c
+git show HEAD~3:src/.crumb > src/.crumb; out=$("$C" verify .); rc=$?
+[ $rc -eq 1 ] && echo "$out" | grep -q '^STALE src (committed generated data' && ok "AM1 a re-added generated block (old tool) fails verify, exit 1" || bad "AM1 re-added generated block passed ($rc)"
+"$C" compile . | grep -q 'STRIPPED src' && "$C" verify . >/dev/null && ok "AM1 compile strips it and verify is OK again" || bad "AM1 compile strip"
+"$C" status . | grep -c '^CURRENT' | grep -qx 4 && "$C" context src | grep -q '^Crumbs CURRENT at HEAD' && ok "AM1 status and context report CURRENT" || bad "AM1 status/context"
+d1=$("$C" digest . | jq -r .digest); git rm -q docs/.crumb; "$C" verify . >/dev/null && [ "$("$C" digest . | jq -r .digest)" != "$d1" ] && ok "AM1 deleting a crumb moves the root digest (D7 deletion still visible)" || bad "AM1 crumb deletion"
+gc reset -q --hard HEAD; echo '{broken' > docs/.crumb; "$C" verify . >/dev/null; [ $? -eq 1 ] && ok "AM1 a crumb that is not JSON fails verify" || bad "AM1 broken crumb passed"
+git checkout -q -- docs/.crumb
+cd "$SAVE" || exit 1
# differential vs the shell reference (needs jq, git, sha256sum)
REF="$HERE/crumb-compile.sh"
if command -v jq >/dev/null 2>&1 && [ -f "$REF" ]; then