Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-architecture-mv/patches/48d8027553f32d37 # aien-architecture-mv Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## crumb: repository namespaces, `crumb check`, (key, generation) fencing, fail-closed enforcement (core#924, DRA-97) merged `48d8027553f32d37` #381 · opened 2026-11-07 05:22 UTC · closed 2026-11-07 07:20 UTC · merged 2026-11-07 07:20 UTC Imported from GitHub aien-dev/aien-architecture-mv#381, by drakestapleton Base `c9dd6f37b6b83fad5e45662ee6ab423a8b74551d` · Head `8261a7314def4b3b85977954713690f2043c4aca` Merged on GitHub as `4c195641588b356bbf698942f1994e13b3c44370`; reachable from main `bccab5ea7a17be6c9ab30b1bd05db4898a4601be` (observed 2026-11-18T16:37:24Z) drakestapleton wrote on 2026-11-07 05:22 UTC # crumb: repository namespaces, `crumb check`, (key, generation) fencing, fail-closed enforcement (core#924, DRA-97) Refs aien-dev/aien-sovereign-core-mv#924, #587; Linear DRA-97; ledger L900. ## What changes (tools/crumb/crumb.c, docs) - Layout 2 store: `/LAYOUT` (`crumb-coord-layout/2`), records under `/repos//{dirs,agents,continuations,ledger}`; root STORE_ID unchanged. Repo id = `CRUMB_REPO_ID` > `git config crumb.repoId` > origin URL `/` (host URLs only, never the path). `crumb store-init` creates layout 2 deliberately and refuses a root holding flat records, and proves the old flat record dirs empty in every mode before publishing LAYOUT (desk review B8, `f2df4b4d`). Layout-1 stores are never reinterpreted as layout 2, but they are not "unchanged": the overlap rule, TTL range, strict schema in enforcement and the missing-lock refusal (RFC-0002 2.8) apply to them too. Relative override roots anchor at the worktree top-level. - `crumb check [--generation N] [--key K]`: read-only under the store lock (creates/prunes nothing); exit 0 only when a valid lease on target or an ancestor is held by exactly `` and no other agent holds one; 2 for absent / expired / held-by / key or generation mismatch; 1 I/O, usage, malformed; 3 store identity / layout / repo id; 4 hostile file. - Lease `generation` fencing token (per key, strictly increasing on each new acquisition, kept on renewal); `claim` prints it; claims refuse overlap with another agent's valid lease on the key, an ancestor or a descendant. The token is (key, generation): the Lean model of the protocol (crumb924 evidence proof-lean, `obs_number_only_check_accepts`) showed a bare number can match a later ancestor lease; `--key` closes it (test N18). - Enforcement mode (`--enforce`, `CRUMB_ENFORCE=1`, `git config crumb.enforce true`): strict registered STORE_ID, layout 2 + repo id, no `.crumb.local` fallback, every opendir/read/parse failure exit 1 (no silent empty state), strict lease schema (malformed = exit 1, never pruned or taken over), agent ids `^[A-Za-z0-9][A-Za-z0-9_.@-]{0,159}$` used verbatim (no `_` aliases), one live session per agent id, failed dir sync is an error. - Docs: RFC-0002 sections 2.7/2.8 (layout 2, migration rule, exit codes), 3.1, 8, 9; AGENT_WORKFLOW enforced-repo workflow and onboarding. - Desk review of #381 at `0f32e086` (MERGE_AFTER_FIX, B8 + G1): B8 store-init fix by the queen's CrumbInitFix (`f2df4b4d`, cherry-picked as asked; its report: hive/queen/mission/desk/crumb-init-fix/REPORT.md). G1 CodeQL: `06c94f59` (bounded writes), `29c8f5eb` (N28 test hardening), `b8e52719` (one checked opener), `8261a731` (store-init emptiness proof walks by descriptor: openat/unlinkat on the parent descriptor, after CodeQL flagged the lstat-then-rmdir in `f2df4b4d` as cpp/toctou-race-condition). Details in the CodeQL section below. ## Tests (Spark spark-b87b, `taskset -c 16-17 nice -n 10`) - `make -C tools/crumb test` at `8261a731`: 343 PASS 0 FAIL 0 NOT_RUN, rc 0 (lead rerun, receipt lead-make-test-`8261a731`.log with HEAD and binary sha256 header). At `0f32e086`: 325 PASS (test.sh 153, test-continuation.sh 8, test-hostile.sh 31, test-namespace.sh 133). - x86_64 build like CI (x86_64-linux-gnu-gcc 13.3, -Wall -Wextra -Werror) rc 0; all 4 suites on that binary under qemu-user: 343 PASS 0 FAIL. - Negative control for the G1 tests: test-namespace.sh at `b8e52719` against the `f2df4b4d` binary: 5 new checks FAIL (symlinked/FIFO .crumb.local, link reads, `..` root), the 3 controls PASS. B8 (N29): 8 of 10 FAIL on `0f32e086` (CrumbInitFix report). Baseline main `c9dd6f37` in this worktree: 192 PASS 0 FAIL. - test-namespace.sh covers: cross-repository same relpath on a shared root (no collision), two independent clones of one repo (8-way concurrent claim race: exactly one exit 0), linked worktrees, overlap (ancestor/descendant), generations and `check --generation/--key`, expiry boundary (`CRUMB_TEST_NOW`: ttl valid, ttl+1 expired), check absent/expired/held-by and read-only (store byte-identical), agent id validation, aliases and duplicate live session, malformed lease schema, chmod-000 store dir, STORE_ID mismatch (exit 3), hostile record (exit 4), legacy fallback refused, layout 1 never reinterpreted, repo id rules, relative root, Hermes session id with `_`. - Negative control: test-namespace.sh against main's binary (git archive `c9dd6f37`): 9 PASS / 66 FAIL of the first 75 (the 9 are controls of unchanged behaviour); N18 keyed-token checks FAIL against `0f2c9b8` (pre `--key`). - Gates: `tests/gates/run.sh crumb-verify` rc 0 at `8261a731` (28 crumbs current; receipt gate-crumb-verify-`8261a731`.log), `crumb-tool-test` rc 0 (343/0) at `b8e52719`. x86_64-linux-gnu-gcc -Wall -Wextra -Werror build rc 0 at `8261a731`. Older: doctrine, zero-legacy, capreg, script-tests rc 0 at `0f2c9b8` (omc pinned `734f0a39`...). - Lean model (separate evidence, not this repo): mutual exclusion, check soundness, fencing monotonicity/stale-token refusal, expiry boundary; all `lean_verify` trust standard. ## NOT_RUN qualification-status gate (needs ../omega sibling); CodeQL on `8261a731` until hosted CI reports (on `b8e52719` it went from 25 new alerts to 1, the toctou fixed in `8261a731`) (it cannot run on the Spark); AIEN Alpha (tag-only job, SKIPPED); checkpoint/resume against a layout-2 store; chmod-000 test as root. Evidence: Spark ~/work/crumb924/evidence/crumb-c-g1/ (lead-make-test-`8261a731`.log, gate-crumb-verify-`8261a731`.log, gate-crumb-verify.log, gate-crumb-tool-test.log, x86_64-qemu-tests.log, negative-control-`f2df4b4d`.log); older heads in evidence/crumb-c/ (lead-make-test-b7b.log = `0f32e086`; lead-gate-crumb-verify.log = `d70b7d5`). Follow-up after merge: bump core `crumb.lock` to the merge sha (separate PR). ## CodeQL on this PR: what changed and why (G1) Threat model. `crumb` is a local CLI with no privilege boundary: it runs as the operator, and every path it opens derives from the store the operator selected (`CRUMB_COORD_ROOT`, `git config crumb.coordinationRoot`, the directory argument, `HOME`), from git's own output, or from names read from that store. The 20 `cpp/path-injection` alerts follow exactly that intended flow (environment/argv to file open). They are not vulnerabilities, and this PR does not say they were. The 5 `cpp/unbounded-write` alerts were `sprintf` into buffers malloc'd from the input lengths: they could not overflow, but nothing in the code checked that. Change. - Bounded writes. All 5 `sprintf` calls are gone. Paths are now built with `xsnprintf`: `snprintf` with the allocated size and a checked return. An encoding error or truncation is an internal error (exit 1), never a silently cut path. The other heap path formats (`pjoin`, temp names, `agent@host`, the cc.sh path) use the same helper. `root_canon` no longer formats at all (see below). - One checked opener. Every former `open()`/`fopen()` call (slurp, fcreate, read_bounded, create_temp, /dev/urandom, fsync_dir, root_lock, lane reports) now goes through `crumb_open` / `crumb_open_follow`, and none is left in `crumb.c`. The opener: - refuses an empty path or a path with a `..` component before opening anything (`PATH_REFUSED`, exit 1, never treated as "absent"); - always adds `O_CLOEXEC`; - adds `O_NOFOLLOW` except where a final link must be followed: working-tree files git can track as links, git's `info/exclude`, lane reports, `/dev/urandom`, and the directory opened only to `fsync` it (that open is now `O_DIRECTORY`; a store root named through a symlink still works); - opens with `openat(AT_FDCWD, ...)`. The store-file sites keep their existing `O_NOFOLLOW|O_NONBLOCK` + `lstat`/`fstat` behaviour. New: fresh temp files from `fcreate` refuse a planted link, and `.crumb.local` (RFC-0001 fallback, layout-1 import, `list`) is read with the store-record reader. A symlinked or FIFO `.crumb.local` is `REFUSED_RECORD` (exit 4). Before, it was read through the link, and a FIFO hung. - No accepted input is refused. `..` is resolved where paths are resolved, so the opener's refusal is a backstop that well-formed input never reaches: - a relative root was already canonicalised; an absolute root with a `..` component now is too (realpath of the part that exists, the rest lexically); - an absolute root without `..` is still used exactly as given; - `CRUMB_COORD_ROOT` outside a repository, `HOME` (backfill) and git's `--git-path` output (`../../.git/info/exclude` from a subdirectory) are canonicalised the same way. About the alerts closing. `openat` is not a sink in CodeQL's `TaintedPath` query, which models `fopen`/`open` and their wrappers. So the path-injection alerts close because the opens were routed through one place, not because CodeQL proved anything new. Please judge the change on what it adds: one opener that refuses `..` and applies `O_NOFOLLOW` (where a site does not need to follow) and `O_CLOEXEC` to every open. That is uniform hardening, not a fix for an exploitable bug. Tests: `test-namespace.sh` N30 (symlinked/FIFO `.crumb.local` refused, link/target/store untouched, regular-file control) and N31 (`..` in `CRUMB_COORD_ROOT` / `crumb.coordinationRoot` canonicalised, same-store and symlinked-root controls). Against the unfixed `f2df4b4d` binary, 5 of the 8 new checks fail and the 3 controls pass. The N28 GIT_DIR/enforce checks now assert that `.crumb.local` is byte-identical, instead of the absence of a file that `claim` never creates. ## Independent review rounds (profile reviewer, gpt-6-astra; verdicts in the crumb924 lead dir reviews/) - Round 1 at `d70b7d58`: B1 symlinked record dirs skipped, B2 `_root/` record collision, B3 cross-session renew after heartbeat expiry, B4 check without .lock, B5 TTL accepted beyond the reader's range, B6 counter could decrease, N1-N3. Fixed in `2506ac54` + `ce562675` (tests N20-N27; negative control 30 FAIL on `d70b7d58`). - Round 2 at `ce562675`: B7 repository-discovery failure fell back to legacy `.crumb.local`. Fixed in `9caffb1c` (GIT_DISCOVERY_FAILED in every mode before any write; N28, 11 checks). - Round 3 at `9caffb1c`: B7 PARTIAL (the non-repository diagnostic was matched as a substring, so a file name or GIT_DIR containing the words still fell back). Fixed in `0f32e086` (whole-diagnostic match, GIT_DIR unset; +3 checks, 2 FAIL on `9caffb1c`). - Round 4 at `0f32e086`: no blocking defect (signed verdict COMMENT; reviews/arch-crumb-c.verdict.md in the crumb924 lead dir). - Desk exact-PR review #381 at `0f32e086`: MERGE_AFTER_FIX, B8 + G1 (hive/queen/mission/desk/aien-architecture-mv-381.verdict.txt). Fixed at `8261a731` as above; needs a fresh exact-head review. Agent: crumb924 (Hermes worker, claude-opus-5-5, anthropic), lead of ledger L900; subagents crumb924-crumb-c, crumb924-g1, crumb924-telemetry, crumb924-session-bridge, crumb924-omp, crumb924-lean (same model). B8 commit `f2df4b4d` by CrumbInitFix (omp subagent of omp-queen-spark). github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 714 on 2026-11-07 05:23 UTC ## CodeQL / Unbounded write This 'call to sprintf' with input from an environment variable may overflow the destination. This 'call to sprintf' with input from string read by fread may overflow the destination. Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 789 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to create_temp(tmp), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to create_temp(tmp), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 936 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to read_bounded(path), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to read_bounded(path), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 877 on 2026-11-07 05:23 UTC ## CodeQL / Unbounded write This 'call to sprintf' with input from an environment variable may overflow the destination. This 'call to sprintf' with input from string read by fread may overflow the destination. Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1100 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1091 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1194 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1206 on 2026-11-07 05:23 UTC ## CodeQL / Unbounded write This 'call to sprintf' with input from a command-line argument may overflow the destination. This 'call to sprintf' with input from an environment variable may overflow the destination. This 'call to sprintf' with input from an environment variable may overflow the destination. This 'call to sprintf' with input from string read by fread may overflow the destination. Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1208 on 2026-11-07 05:23 UTC ## CodeQL / Unbounded write This 'call to sprintf' with input from a command-line argument may overflow the destination. This 'call to sprintf' with input from an environment variable may overflow the destination. This 'call to sprintf' with input from an environment variable may overflow the destination. This 'call to sprintf' with input from string read by fread may overflow the destination. Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1343 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to rec_load(path), which calls read_bounded(path), which calls open(__file). This argument to a file access function is derived from user input (an environment variable) and then passed to rec_load(path), which calls read_bounded(path), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to rec_load(path), which calls read_bounded(path), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1565 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1575 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (a command-line argument) and then passed to read_bounded(path), which calls open(__file). This argument to a file access function is derived from user input (an environment variable) and then passed to read_bounded(path), which calls open(__file). This argument to a file access function is derived from user input (an environment variable) and then passed to read_bounded(path), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to read_bounded(path), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 2259 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to env_store(root), which ends up calling open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 1822 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to env_store(root), which ends up calling open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 2942 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 2943 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 2999 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] reviewed (COMMENTED) on 2026-11-07 05:23 UTC github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 2883 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to ident_gate(store), which calls ensure_marker_locked(store), which calls publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 3014 on 2026-11-07 05:23 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to publish_once(dir), which calls fsync_dir(dir), which calls open(__file). This argument to a file access function is derived from user input (string read by fread) and then passed to publish_once(dir), which calls fsync_dir(dir), which calls open(__file). Show more details github-advanced-security[bot] commented on `tools/crumb/crumb.c` line 2976 on 2026-11-07 06:04 UTC ## CodeQL / Time-of-check time-of-use filesystem race condition The filename being operated upon was previously checked , but the underlying file may have been changed since then. The filename being operated upon was previously checked , but the underlying file may have been changed since then. The filename being operated upon was previously checked , but the underlying file may have been changed since then. Show more details github-advanced-security[bot] reviewed (COMMENTED) on 2026-11-07 06:04 UTC ### Apply it ``` git checkout -b patch-48d8027553f32d37 c9dd6f37b6b83fad5e45662ee6ab423a8b74551d curl -fsSL https://interplane.io/drakestapleton/aien-architecture-mv/patches/48d8027553f32d37.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aien-architecture-mv 381 8261a7314def4b3b85977954713690f2043c4aca ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aien-architecture-mv","number":381,"url":"https://github.com/aien-dev/aien-architecture-mv/pull/381","export_sha256":"e0ab76730762f7bd707f3b0c07e133f83d4287854fada828261004f8d4bcaa1a","imported_utc":"2026-11-18T16:37:24Z"} ``` ### Changes ``` diff --git a/docs/CRUMB_PROTOCOL_RFC_0002.md b/docs/CRUMB_PROTOCOL_RFC_0002.md index 3b3d7b5..eeaf60f 100644 --- a/docs/CRUMB_PROTOCOL_RFC_0002.md +++ b/docs/CRUMB_PROTOCOL_RFC_0002.md @@ -67,6 +67,27 @@ CONTINUATIONS newest continuation ids whose scope touches this directory (sect ``` followed by the RFC-0001 `.crumb` summary (purpose, invariants, exports, related). +### 2.7 Repository namespaces (store layout 2; crumb924, aien-sovereign-core#924) +Section 2.3 names a resource by **repository identity + repository-relative path**, but a layout-1 store has no repository identity: two different repositories pointed at one `CRUMB_COORD_ROOT` collide on equal relative paths, and independent clones of one repository can only share coordination by sharing a flat store. Layout 2 adds the identity. + +- **Repository id.** `CRUMB_REPO_ID` if set, else `git config crumb.repoId`, else the `origin` remote URL normalised to lowercase `/` (`git@github.com:o/r.git`, `ssh://git@host/o/r.git`, `https://github.com/o/r(.git)(/)`). It is **never** derived from a filesystem path: a local-path or `file://` origin yields no id. Pattern `^[A-Za-z0-9][A-Za-z0-9._-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)?$`, at most 200 characters; the second component may not be `dirs`, `agents`, `continuations` or `ledger` (namespace `x/dirs` would otherwise nest inside namespace `x`). An explicit id is used verbatim (write it lowercase so it matches the derived form). A bad explicit id is `REPO_ID_INVALID`, a missing one on a layout-2 root `REPO_ID_MISSING` (both exit 3). +- **Layout.** `/STORE_ID` (unchanged, section 3.1), `/LAYOUT` holding exactly `crumb-coord-layout/2\n`, `/.lock` (one transaction lock for the whole root), records under `/repos//{dirs,agents,continuations,ledger}` with the section 2.2 shapes. A `LAYOUT` with any other content is `STORE_LAYOUT_UNSUPPORTED` (exit 3, never rewritten). In layout 2 agent ids are validated (section 2.8) and used verbatim as `agents/.json` (no `_` aliases), and `.crumb.local` is never imported (a present one is named on stderr). +- **Creation is deliberate.** Only `crumb store-init [dir]` makes a root layout 2: under the root lock it creates or verifies `STORE_ID` (registered: must match, never created), then, in every mode (enforcement on or off), proves `dirs/`, `agents/` and `continuations/` empty before anything else changes: each must be absent or a real directory whose whole tree, walked with `lstat`, holds only real empty subdirectories. A layout-1 record (`*.json`) is `STORE_LAYOUT_V1_DATA`; a symbolic link, any other file, or a failure to stat, open, read or remove an entry is `STORE_LAYOUT_V1_UNPROVEN` (both exit 1, `LAYOUT` not written, nothing removed): an inspection that could not see everything is never taken for an empty store. Only after all three are proved are their empty trees removed (a failed removal also refuses), then `LAYOUT` is published once (`link(2)`, never replaced) and `repos/` created. It is idempotent on a layout-2 root. No other command creates or changes `LAYOUT`; every mutation and `check` re-reads it under the lock (`STORE_LAYOUT_CHANGED` if it changed meanwhile). +- **Layout 1 keeps working as before** (compatibility mode, the default for every existing store, including `/crumb/v1`): same records, same paths, same output, with these deliberate changes that apply to layout 1 too: the lease generation (section 2.8), the overlap rule of section 2.8 (`claim` now also refuses when another agent holds a valid lease on an ancestor or descendant key, not only on the same key), a ttl above 315360000 seconds is refused (`bad ttl`), a renewal never lowers the `generations` counter, and `crumb check` on a used store (marker or record directories present) without `.lock` is exit 1. It is never silently reinterpreted as layout 2. A layout-2 root on which an older tool wrote flat `dirs/` or `agents/` records is `STORE_LAYOUT_MIXED`: a warning normally, exit 3 in enforcement mode. +- **Directory record names (layout 2).** The record for key directory `d` is `dirs/.json`, the repository root (`.`) is `dirs/_root.json`, and a first component that starts with `_` is stored with one more `_` (`_root/a.c` lives in `dirs/__root.json`, `_x/y/z.c` in `dirs/__x/y.json`), so a repository directory named `_root` never shares the root's record and every record name maps back to exactly one key directory. A record name no key maps to (a single leading `_` other than `_root.json`, or anything under `dirs/_root/`; an earlier layout-2 build could write such names) is `MALFORMED_RECORD` in enforcement mode (exit 1) and a stderr warning otherwise; it is never reinterpreted. Layout 1 keeps its historical mapping, where `_root/` shares `dirs/_root.json` with the root. +- **Sharing one authority.** Independent clones of one repository (and their linked worktrees) share one lock table by pointing `CRUMB_COORD_ROOT` or `git config crumb.coordinationRoot` at the same **absolute** directory: their repository id makes them contend; different repositories with identical relative paths never collide. A relative override is anchored at the worktree top-level (not the process cwd) and canonicalised, so the cwd never changes the store; use an absolute path to share a root across clones. An absolute root is used exactly as given unless it has a `..` component, which is canonicalised the same way (realpath of the part that exists, the rest lexically), so no store path ever carries `..`. +- **Migration rule.** A layout-1 store is never converted in place. To move a repository to a shared namespaced root: (1) pick a new, empty absolute directory; (2) in one clone: `git config crumb.coordinationRoot `, `crumb store-init .`, `git config crumb.storeId "$(crumb store-id .)"`; (3) in every other clone of every participating repository set the same `crumb.coordinationRoot` and `crumb.storeId` (and `crumb.repoId` where the origin URL is not `host//`); (4) re-claim live work in the new root (old leases stay in the old store and are not carried over: close them there); (5) turn on enforcement (section 2.8). Reusing an old root in place requires closing every lease and moving its `dirs/`, `agents/`, `continuations/` aside first; `store-init` refuses otherwise. + +### 2.8 `crumb check`, lease generations and enforcement mode (crumb924) +- **Lease generation (fencing token).** Every lease carries `generation` (u64 >= 1). Per key it strictly increases on each new acquisition (first claim, re-acquire after expiry, takeover after release); a renewal by the current valid holder keeps it. The highest generation ever issued for a key is kept in the directory record's `generations` map, which outlives the lease and never decreases (a renewal keeps a larger counter; after 2^64-1 the key is exhausted, exit 1, never wrapped). In enforcement mode a lease whose generation differs from its key's counter is `MALFORMED_LEASE` (the tool always writes them equal). `claim` prints `... generation ` at the end of its `CLAIMED` line. +- **Overlap.** `claim` refuses (exit 2, the usual `BLOCKED held by ...` line, suffixed `[overlapping lease on ]`) when another agent holds a valid lease on the same key, an ancestor (`src` covers `src/a.c`; `.` is the whole repository) or a descendant. An agent may nest leases inside its own. This applies in every layout, layout 1 included (section 2.7). +- **ttl.** `claim` and `update` take a ttl of decimal seconds, at most 315360000 (3650 days; no sign, no overflow), in every mode; enforcement mode also refuses 0. It is refused before anything is written, and it is exactly the range the strict reader accepts, so a ttl the tool writes can never make its own records malformed. +- **`crumb check [--generation N] [--key K]`.** The pre-write guard (the session bridge calls it before every write into an enforced repository). Read-only: a shared `fcntl` lock on an existing `/.lock` (never created) for a consistent read; nothing is created, swept or pruned. A store that is in use (layout 2, enforcement mode, or a layout-1 root holding `STORE_ID`, `dirs/` or `agents/`; every mutation creates `.lock` before any of them) but has no `.lock` is `STORE_LOCK_MISSING` (exit 1), never a HELD read without the lock; only a never-used compatibility root without `.lock` is plain absence (`REFUSED absent`). `` is a file or directory relative to the repository root (`.` = all of it), resolved like `claim`. Exit 0 only when a valid lease on the target or an ancestor key is held by exactly ``, no other agent holds a valid lease on the target or an ancestor, and, with `--generation N` and `--key K`, the nearest such lease has generation N and key K: stdout `HELD by key generation expires `. Exit 2 otherwise, stdout `REFUSED ...` with reason `absent`, `expired`, `held-by `, `key !=` or `generation !=`. Exit 1 for I/O, usage, a malformed record or lease (also outside enforcement mode), 3 store identity / layout / repository id, 4 hostile store file. A lease is valid while `now - acquired_at <= ttl_seconds` (equality is still valid; it expires at `acquired_at + ttl_seconds`). The generation lets a writer re-check, right before or after it writes, that the lease it was granted is still the current one. Generations are counted per key, so a bare number is ambiguous when the covering lease changes key (an ancestor lease can later carry the same number; found by the Lean model of this protocol): a writer re-presents the whole token, `--key K --generation N` exactly as printed on the HELD line. +- **Enforcement mode** (`--enforce`, `CRUMB_ENFORCE=1`, or `git config crumb.enforce true`; none of them can turn it off once another turned it on). `crumb.enforce` is read with git's own boolean conversion (`git config --bool`: a valueless `enforce` key, `yes`, `on`, `1` are true); a value git refuses as a boolean, or a failed read, is an error (exit 1), never "absent". Implies strict store identity (section 3.1) and adds: layout 2 and a repository id are required (`STORE_LAYOUT_V1`, `REPO_ID_MISSING`, exit 3); no `.crumb.local` fallback outside a repository (`ENFORCE_NO_REPOSITORY`, exit 1); every directory walk, read or parse failure is an error (`STORE_UNREADABLE`, exit 1; outside enforcement the same failure is a stderr warning, never silent); a symbolic link anywhere in the record tree is refused (`REFUSED_RECORD`, exit 4), never followed and never skipped (outside enforcement an unwalked link is a stderr warning); the strict lease schema (`holder` and `session` non-empty strings, `acquired_at` exactly `YYYY-MM-DDTHH:MM:SSZ`, `ttl_seconds` an integer in 1..315360000, `generation` an integer >= 1 equal to its key's `generations` counter, a normalised key stored in its own directory record, `generations` an object of integers) is checked before anything is swept, so a malformed lease is `MALFORMED_LEASE` (exit 1) and is never pruned or taken over; agent ids must match `^[A-Za-z0-9][A-Za-z0-9_.@-]{0,159}$` and are used verbatim (`AGENT_ID_INVALID`, exit 1); an agent id names one live session: `claim`, `update` or `close` presenting the id from another session (`CRUMB_SESSION`, else `@`) while that id has a live heartbeat from that session, or a live lease whose recorded `session` is that session, is refused (`BLOCKED agent is live in session ...`, exit 2): an expired heartbeat never hands a live lease to another session, and only the lease's own session renews or closes it; an existing agent record must have `agent` and `session` non-empty strings, `updated_at` exactly `YYYY-MM-DDTHH:MM:SSZ` and `ttl_seconds` in 1..315360000, else `MALFORMED_RECORD` (exit 1; malformed session metadata is never treated as absent); `claim` and `update` need a positive ttl. +- **Repository discovery fails closed** (every mode, crumb924 review B7). Whether a directory is outside git is decided only from positive evidence: `git rev-parse --show-toplevel` (C locale) exits 128, `GIT_DIR` is unset, and its whole stderr is exactly git's end-of-search diagnostic (`fatal: not a git repository (or any of the parent directories): .git`, or the mount-point form followed by the filesystem-boundary line); a substring match is not enough, because other git diagnostics name files whose names could contain those words (this includes a search stopped by `GIT_CEILING_DIRECTORIES` or a filesystem boundary), or, with `GIT_DIR` unset, git does not succeed and no `.git` entry exists in the directory or any ancestor. Only then does a command take the RFC-0001 `.crumb.local` path (section 4), or, in enforcement mode, refuse with `ENFORCE_NO_REPOSITORY`. Any other discovery failure (a malformed or unreadable `.git/config`, a `.git` file or `GIT_DIR` that names no repository, dubious ownership, a directory inside `.git` with no work tree, git not runnable inside a repository, a reported top level that cannot be resolved or does not contain the directory) is `GIT_DISCOVERY_FAILED` (exit 1) for every coordination command, before anything is read from or written to the store or `.crumb.local`: such a repository may enable enforcement through its own `.git/config`, which git could not read, so neither enforcement nor its absence can be assumed and the legacy fallback would create a second authority beside the shared store. +- **Test clock.** `CRUMB_TEST_NOW=` replaces the wall clock for every timestamp and expiry decision. It exists only so the test suite can hit the expiry boundary exactly; never set it in production. +- **Still advisory at the filesystem.** Crumb cannot stop a process that writes without asking; enforcement means the agent runtimes' write gate (the swarm bridge) asks `crumb check` and blocks on anything but exit 0. + ## 3. Mutation discipline (transaction safety) tmp + `rename()` prevents torn files but not lost updates (A and B both read version 10, both write version 11, one lock vanishes). Every mutation of the shared store runs: @@ -83,17 +104,18 @@ Two agents pointed at two different coordination roots each believe they hold th - **Marker.** `/STORE_ID` holds exactly `crumb-coord-store-id/1 <32 lowercase hex>\n` (56 bytes), drawn from `/dev/urandom` and written under the transaction lock (tmp, `fsync`, `link(2)`, directory `fsync`), so two first writers cannot both win. Either tool may create it; both read it the same way, and neither rewrites an existing marker. - **Registration.** The expected id is `CRUMB_EXPECT_STORE_ID` when set and non-empty, else `git config crumb.storeId` (one value for every linked worktree of the repository). `crumb store-id [dir]` prints the store's id and is the onboarding step: `git config crumb.storeId "$(crumb store-id .)"`. The Rust side takes the same id as `--expect-store-id`. - **Three modes.** - 1. *Unregistered (compatibility, the default).* No expected id. Every command works as before; the first mutation creates the marker, and while a marker exists but no id is registered each coordination command prints one stderr line, `STORE_UNREGISTERED (dry run): ... register it: git config crumb.storeId `. Standard output and every `dirs/` and `agents/` record are byte-identical to the pre-#234 tool. + 1. *Unregistered (compatibility, the default).* No expected id. Every command works as before; the first mutation creates the marker, and while a marker exists but no id is registered each coordination command prints one stderr line, `STORE_UNREGISTERED (dry run): ... register it: git config crumb.storeId `. Standard output and every `dirs/` and `agents/` record are byte-identical to the pre-#234 tool (apart from the lease generation added by section 2.8: the ` generation ` suffix of `CLAIMED`, each lease's `generation`, the record's `generations` map; and apart from the further deliberate behaviour changes listed under "Layout 1 keeps working as before" in section 2.7 and the repository-discovery rule of section 2.8, which change exit codes and refusals, not the record format). 2. *Registered.* An expected id is set. Before any read or write, and again under the transaction lock before a mutation, the root's marker must match. It never creates a marker. 3. *Strict.* `crumb --strict ` or `CRUMB_STRICT_STORE_ID=1` additionally refuses an unregistered root before creating anything (`store-id` stays allowed, for onboarding). - **Migration.** Run unregistered and watch for the dry-run line; register each repository with `git config crumb.storeId`; turn on strict mode for onboarded swarm agents so a misconfigured root fails closed. The legacy `.crumb.local` path outside a repository (section 4) is unaffected. - **Exit codes.** `2` is unchanged (held by another agent). `3` is a store identity refusal, named `STORE_ID_MISMATCH` (the root is another store: split coordination roots), `STORE_ID_MISSING` (an id is expected but the root has no marker), `STORE_ID_UNREADABLE`, `STORE_ID_CONFIG_INVALID` (the configured id is not 32 lowercase hex) or `STORE_UNREGISTERED` (strict mode). `4` is `REFUSED_RECORD`: a store file is a FIFO, socket, device, directory or symbolic link, sits under a symlinked directory, or is larger than the cap. The message names the file and says to fix or move it aside; the file is never replaced. -- **Reads and caps.** Every store file `crumb` reads (records, continuations, `STORE_ID`; `.lock` is opened the same way but never read) is opened `O_NOFOLLOW | O_NONBLOCK` after an `lstat`, checked again with `fstat`, and read up to a cap: 1 MiB (1048576 bytes) for a record, 4096 bytes for the marker. A record over 1 MiB is refused on write too, so the tool never writes what it would refuse to read. Record walks do not descend into symlinked directories. +- **Reads and caps.** Every store file `crumb` reads (records, continuations, `STORE_ID`; `.lock` is opened the same way but never read) is opened `O_NOFOLLOW | O_NONBLOCK` after an `lstat`, checked again with `fstat`, and read up to a cap: 1 MiB (1048576 bytes) for a record, 4096 bytes for the marker. A record over 1 MiB is refused on write too, so the tool never writes what it would refuse to read. Record walks do not descend into symlinked directories. Every file `crumb` opens goes through one opener (`crumb_open`): an empty path or one with a `..` component is refused before the open (`PATH_REFUSED`, exit 1; configured roots, `HOME` and git's paths are canonicalised where they are resolved, so this is a backstop), `O_CLOEXEC` is always set, and `O_NOFOLLOW` too, except for the working tree's own files (`.crumb`, `.gitignore`, `.crumbignore`, git's `info/exclude`, `docs/crumbs/BACKFILL.md`), lane reports, `/dev/urandom` and a directory opened only to `fsync` it. The RFC-0001 `.crumb.local` is read like a store record (`O_NOFOLLOW | O_NONBLOCK`, `lstat` and `fstat`, a 64 MiB cap): a symbolic link, FIFO or other special file there is `REFUSED_RECORD` (exit 4), never read through. - **Independence from the mission board.** These ids name one repository's coordination store. A swarm mission board ([synapse#83](https://github.com/aien-spark/synapse/issues/83)) has its own root and is not one of these stores; registering a repository never registers, joins or reads a board, and a board never satisfies `crumb.storeId`. ## 4. Legacy `.crumb.local` (RFC-0001 physical form) - New writes go only to the shared store (inside a repository). +- `.crumb.local` is written only for a directory positively established to be outside any repository; a repository-discovery error is `GIT_DISCOVERY_FAILED` (exit 1), never a fallback (section 2.8). - Reads merge: shared store first, then any `.crumb.local` present in the invoking worktree; an unexpired legacy scent or lock is shown with `legacy:` and an `imported_from: ` provenance. Legacy state never overrides a newer shared record for the same key. - `crumb seed` / first shared write in a directory imports an existing `.crumb.local` once (provenance kept) and leaves the file on disk; nothing is deleted. - `.gitignore` handling for `.crumb.local` stays, for the transition and for non-repository directories. @@ -145,7 +167,7 @@ Why checkpoint/resume may start outside the C binary: the logic exists today as ## 8. Scope boundary -This RFC closes the gap for **all linked worktrees of one repository on one filesystem**. Independent clones (another machine, CI, another Spark node) do not share a common git directory and are out of scope. The store is reached through one small internal interface (`store_read`, `store_write`, `store_lock`, `store_root`) so a later `DaemonStore` or distributed backend can replace `GitCommonStore` without touching command code. No transport is chosen now. +This RFC closes the gap for **all linked worktrees of one repository on one filesystem**. Independent clones (another machine, CI, another Spark node) do not share a common git directory; on one filesystem they share coordination only through an explicitly configured shared root with layout 2 (section 2.7), and across machines they remain out of scope. The store is reached through one small internal interface (`store_read`, `store_write`, `store_lock`, `store_root`) so a later `DaemonStore` or distributed backend can replace `GitCommonStore` without touching command code. No transport is chosen now. ## 9. Tests (the feature is not done until these run) @@ -161,6 +183,7 @@ This RFC closes the gap for **all linked worktrees of one repository on one file | T8 | Legacy `.crumb.local` read/import with provenance, never overriding newer shared state | same | PASS (`make -C tools/crumb test`, 0 FAIL) | | T9 | Continuation revalidation: checkpoint at C, advance the repo, resume reports STALE/SUPERSEDED and status NEEDS_REVIEW | `tools/crumb/test-continuation.sh` | PASS (8 checks; STALE repo HEAD + NEEDS_REVIEW; SKIP prints NOT_RUN where jq or cc.sh is absent) | | E1 | `crumb explain`: every section header, nearest `.crumb`, commits, locks and whispers, continuation surfaced by `owned_paths` and by `scope`, unrelated continuation ignored, missing path fails | `tools/crumb/test.sh` | PASS (15 checks) | +| N1-N31 | Sections 2.7/2.8: store-init, cross-repository (same relative path, shared root), two independent clones (8-way race: exactly one holder), linked worktrees, overlap, generations and `check --generation`, expiry boundary (`CRUMB_TEST_NOW`), check absent/expired/held-by and read-only, agent id validation, aliases and duplicate sessions, malformed lease schema, inaccessible directory, STORE_ID mismatch, hostile record, legacy fallback refused, layout 1 never reinterpreted, repository id rules, relative root, concurrent and overlapping claims, key-qualified fencing token, Hermes session ids (N19); review fixes N20-N27: symlinked record directories refused, `_root/` record mapping, per-session lease ownership and strict agent records, missing `.lock`, ttl range, generation high-water mark, git booleans for `crumb.enforce`, repository id first character; N28: repository-discovery failure (malformed and unreadable `.git/config`, dangling `.git` file, inside `.git`) is `GIT_DISCOVERY_FAILED` for claim/update/whisper/close/check/sniff/list with no `.crumb.local` and a byte-identical shared store, with genuine non-repository and `GIT_CEILING_DIRECTORIES` fallback controls; N29: `store-init` without enforcement refuses a layout-1 root whose records sit in unreadable `dirs/`+`agents/`, an unreadable descendant directory, behind a descendant or top-level symbolic link, or beside a non-record file (exit 1, no `LAYOUT`, records byte-identical and the lease still blocking once restored; permission cases NOT_RUN as root), with an emptied-root control; N30: a symlinked or FIFO `.crumb.local` (fallback and layout-1 import) is `REFUSED_RECORD` (exit 4), link, target and store untouched, with a regular-file control; N31: a configured root with a `..` component is canonicalised (`store-init` names the canonical root and creates no stray directory), with same-store and symlinked-root controls | `tools/crumb/test-namespace.sh` | 151 checks (results in the merging PR; N28 against `ce562675`: 7 of 11 fail, the 4 that pass are setup and the non-repository controls; negative control against `c9dd6f37`: 66 of the first 75 fail, the 9 that pass are controls of unchanged behaviour; the 5 N18 checks need `--key`, which main lacks; N20-N27 against `d70b7d58`: see the merging PR; N29 against `0f32e086`: 8 of 10 fail, the 2 that pass are the top-level-symlink case its scan already refused; N30-N31 against `f2df4b4d`: 5 of 8 fail, the 3 that pass are the regular-file, same-store and symlinked-root controls) | Results are reported PASS / FAIL / NOT_RUN per test in the merging PR; nothing unrun is called PASS. diff --git a/docs/crumbs/AGENT_WORKFLOW.md b/docs/crumbs/AGENT_WORKFLOW.md index 473107e..3deea96 100644 --- a/docs/crumbs/AGENT_WORKFLOW.md +++ b/docs/crumbs/AGENT_WORKFLOW.md @@ -18,6 +18,22 @@ crumb close modify "" # after pushing ``` `` is a stable session id you choose once (e.g. `claude-l6b-7f32`), never a process id. Targets are repository-relative; it does not matter which worktree you run the command in. +## Enforced repositories: one identity per session, check before every write (RFC-0002 sections 2.7, 2.8) +In a repository with `git config crumb.enforce true` (or `CRUMB_ENFORCE=1`) the write gate of your runtime runs `crumb check ` before each write and blocks on anything but exit 0. So: +- Your `` is `.` (matches `^[A-Za-z0-9][A-Za-z0-9_.@-]{0,159}$`, used verbatim). Never share it with another session: a second live session presenting it is refused. +- Claim before you write: `crumb claim . "" `. A directory lease covers everything below it; a claim that overlaps another agent's lease (same path, an ancestor or a descendant) exits 2. Note the `generation ` it prints. +- `crumb check . [--generation N] [--key K]` answers `HELD ... generation expires