Skip to content

Fix #1783: resolve builder identity from the spawn environment, not only cwd - #1784

Merged
amrmelsayed merged 10 commits into
mainfrom
builder/bugfix-1783
Oct 12, 2026
Merged

amrmelsayed merged 10 commits into
mainfrom
builder/bugfix-1783

Conversation

@amrmelsayed

@amrmelsayed amrmelsayed commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A builder that ran cd <main root> && afx send architect (following the CLAUDE.md "run afx from the main workspace root" rule) resolved as a non-builder, so plain architect routed to main instead of its spawning architect, and the architect:<name> spoofing guard was skipped. Builders now carry their identity in their spawn environment, so it resolves from any cwd.

Fixes #1783

Root Cause

detectCurrentBuilderId() and detectWorkspaceRoot() (packages/codev/src/agent-farm/commands/send.ts) derived identity only from process.cwd() matching /.builders/<id>. Builder terminals carried no identity in their env, so from the main root the sender fell through to architectSenderId() (bare architect). Every identity consumer (send, reset, interrupt, self-refresh, whoami) shares those two functions.

Fix

  • .builder-start.sh (both the prompt-mode and worktree-mode script builders) exports CODEV_BUILDER_WORKTREE=<worktree>. --resume regenerates the script, so existing builders gain it on resume.
  • identityPath() in send.ts prefers that env var (when it names a .builders/<id> path) over the cwd. The value is still only a path: detectCurrentBuilderId verifies it against global.db exactly as before, and an unregistered worktree still throws (afx send: detectCurrentBuilderId silently falls back to bare worktree name on state.db read failure → builder messages misroute to main #1094), never launders into a non-builder.
  • sanitizeAgentEnv strips the var, so a Tower started from a builder shell cannot leak that builder's identity into other terminals.
  • vitest-setup.ts clears it so suites run inside a builder session stay hermetic.
  • Docs: unchanged. The "run afx only from the main workspace root" rule stands as written. Most afx commands resolve a builder worktree as its own workspace, so the rule is right; this fix is what lets a builder follow it, because afx send from the main root now reaches its spawning architect.

Test Plan

  • Regression test bugfix-1783-builder-identity-env.test.ts: fails 3/6 without the fix (workspace-root resolution, env-over-cwd precedence, unregistered-env throws), 6/6 with it
  • Launch-script assertions in spawn-worktree.test.ts (fresh, resume, worktree mode export the var, with quoting)
  • Build passes, tsc --noEmit clean
  • Full codev suite: 6236 passed, 48 skipped

…nly cwd

.builder-start.sh exports CODEV_BUILDER_WORKTREE; detectCurrentBuilderId and
detectWorkspaceRoot prefer it over the cwd (still verified against global.db), so a
builder running 'cd <root> && afx send architect' routes to its spawning architect.
sanitizeAgentEnv strips the var; vitest-setup clears it. Docs narrow the
'afx only from the main root' rule for a builder's own afx send.
@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Integration review (main) — high tier, 3-way CMAP on 048c482

Codex APPROVE · Claude APPROVE · Gemini skipped (agy produced no output; non-blocking). CI 7/7 green. Branch is one merge behind main (#1780, no shared files).

Verified

  • CODEV_BUILDER_WORKTREE carries a path that detectCurrentBuilderId still verifies against global.db, so the afx send: detectCurrentBuilderId silently falls back to bare worktree name on state.db read failure → builder messages misroute to main #1094 boundary holds: a self-asserted or stale value cannot be laundered into a builder identity, it throws. The regex gate on the env value (absolute path containing .builders/) is the right minimal check before the DB lookup.
  • Both launch scripts export it; --resume regenerates the script, and afx workspace recover delegates to afx spawn --resume, so revived lanes get it. In-flight builders gain it on their next resume; until then the doc change covers them, which is why both halves belong in one PR.
  • sanitizeAgentEnv strips it, so a Tower started from a builder's shell cannot hand that identity to every terminal it spawns. Good catch by the builder.
  • whoami resolves through send.ts's detectCurrentBuilderId, so it is covered without a separate edit.
  • Docs: CLAUDE.md / AGENTS.md byte-identical on the branch; scar-rules.yaml canonical updated; all four shipped afx skill copies ({.claude,.codex} × {root,skeleton}) carry the same paragraph; codev-skeleton/roles/architect.md and both roles/builder.md updated; arch-critical.md edited in place with no growth.
  • The new export line is inert to the .builder-start.sh parsers (Claude checked commandNameOf consumers).

Two small changes before the gate word (cheap, in scope)

  1. The skeleton afx SKILL.md copies tell adopters "once spawned or resumed on a version with afx send: a builder running from the main workspace root resolves as a non-builder, so plain 'architect' routes to main instead of its owner (CLAUDE.md rule sends it there) #1783" — a bare issue number they cannot resolve. Reword without the number in all four copies (keep the four byte-identical where the parity tests require it).
  2. Add the stale-env test: CODEV_BUILDER_WORKTREE naming a worktree whose directory no longer exists on disk (row gone) throws BuilderIdResolutionError rather than resolving as a non-builder. Same code path as the "no builder row" case already covered, but it is the likelier real trigger.

Follow-ups filed / extended

@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Addressed integration review. New head: f277d603e47ff8c68ea080b539bb36409b6e9622 (one commit on top, no amend).

  1. afx SKILL.md note no longer cites the issue number: "Builders spawned or resumed on a current version also carry it in CODEV_BUILDER_WORKTREE, so it resolves from any cwd." Applied to all four copies; {.claude,.codex} pairs are byte-identical in root and skeleton.
  2. New test in bugfix-1783-builder-identity-env.test.ts: CODEV_BUILDER_WORKTREE names a worktree that afx cleanup removed (directory deleted and builder row gone). Run from the workspace root, it throws BuilderIdResolutionError naming the worktree, never a non-builder. 9/9 pass.

Note on scope: if only the directory is deleted and the builder row remains, resolution returns the row's builder id. It does not throw and does not fall back to non-builder. That matches the existing cwd path, which has never checked the disk. Adding a disk-existence check would change behaviour, so I left it out. It's a one-line existsSync guard if you want it.

@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Ruling recorded for the edge the builder raised: when CODEV_BUILDER_WORKTREE names a worktree whose directory is gone but whose registry row remains, the lookup returns that builder's id (no throw, no non-builder fallback). Kept deliberately: the registry is the source of truth for a live builder, and a dir-gone/row-present lane is a half-torn-down state for cleanup tooling to fix, not identity resolution. Head stays f277d60.

…of rule-plus-exception

The rule now names the commands it protects; the builder afx send case is its own
sentence. Builder role and afx skill note trimmed to one sentence. Doc-only.
@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Doc-only follow-up at the owner's request. New head: c2798b370f6d3d3eb32f85153a178c9af64a2e77. Supersedes f277d603e.

The owner found the "rule, then an exception to it" wording confusing. The root-only rule now names the commands it protects, and the builder case is its own sentence:

Run afx spawn and afx cleanup only from the main workspace root, never from inside a builder worktree — spawning from a worktree nests builders and breaks the workspace. Builders run afx send from their own worktree so their identity resolves.

  • CLAUDE.md / AGENTS.md (byte-identical) and both architect role copies carry the two sentences.
  • The scar-rules.yaml afx-from-root canonical is now the first sentence only.
  • arch-critical.md: "run afx spawn/cleanup from the main workspace root only."
  • Builder role (2 copies) and afx skill (4 copies) are trimmed to one sentence: run afx send from your own worktree, so architect reaches your spawning architect rather than main.

All copy pairs are byte-identical. The doc-guarding tests (scar-rules, parity, skills) pass: 26 files, 459 tests. No code changes.

@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Architect review, round 3 (head c2798b3)

The head moved off f277d60 for an owner-requested wording change. Re-verified on the new head:

  • Diff f277d60..c2798b3 is doc-only: 13 files, 16+/14-, all Markdown plus codev/resources/scar-rules.yaml (the canonical string for afx-from-root) and the builder thread log. No runtime code changed, so the round-2 integration findings on f277d60 stand.
  • Wording: the rule now names afx spawn and afx cleanup as root-only, and the builder afx send case is a plain follow-on sentence instead of an exception clause. Clearer, and it no longer reads as if every afx subcommand is root-only.
  • Twin parity at c2798b3: CLAUDE.md and AGENTS.md are byte-identical. The .claude/.codex afx skill pairs are identical to each other at both root and skeleton. Root vs skeleton skill copies differ, but they already differ on main (pre-existing drift, not introduced here).
  • Scar rule: the shortened canonical appears verbatim on every must_appear_on surface; the extra builder sentence sits after it, which the exact-string carriage check allows.

Architect approval carries to c2798b3. Waiting on the full check set on this head, then the owner's pr-gate word.

Most afx commands resolve a builder worktree as its own workspace (findWorkspaceRoot),
so narrowing the rule to spawn/cleanup was wrong. With identity now carried in
CODEV_BUILDER_WORKTREE, builders can follow the rule as written, so no exception is needed.
@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Owner-directed follow-up. New head: 7434bb862d4ecf8d9d2c7a9306b4d78eb6026803. Supersedes c2798b370.

All doc edits are reverted to the base. The PR is now code, tests and the thread log only (8 files).

The previous commit narrowed the root-only rule to afx spawn/afx cleanup, and that was wrong. findWorkspaceRoot (packages/codev/src/agent-farm/utils/config.ts:76) treats a builder worktree, which has its own codev/, as the workspace. So most afx commands misbehave there, not just those two, and the blanket rule is correct.

No exception is needed. With identity carried in CODEV_BUILDER_WORKTREE, a builder following the rule as written (afx send from the main root) reaches its spawning architect. Builders already running get the variable when resumed (afx spawn --resume); until then their behaviour is unchanged.

The doc-guarding tests pass (26 files, 459 tests). The PR description is updated.

@amrmelsayed
amrmelsayed merged commit 01d30f2 into main Oct 12, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant