Skip to content

[Air #1950] feat: afx send --message-file/--exit-code/--retry, cleanup --keep-ignored, spawn --max-load, porch -C - #1954

Merged
waleedkadous merged 10 commits into
mainfrom
builder/air-1950
Nov 23, 2026
Merged

waleedkadous merged 10 commits into
mainfrom
builder/air-1950

Conversation

@waleedkadous

@waleedkadous waleedkadous commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds four small CLI features that replace sequences architects were typing by hand. afx send gains --message-file, opt-in --exit-code and a safe --retry. afx cleanup gains --keep-ignored, afx spawn gains --max-load/--wait, and porch gains -C <dir>. It also fixes a small adjacent SDK bug: a refused connection to Tower was never recognised.

Closes #1950

What Changed

1. afx send

  • --message-file <path>: the message body is read from a file and never goes through a shell. Only the single line ending that terminates the file is dropped. It is separate from --file, which appends a file; the two can be combined, and the 48 KB limit applies to the result.
  • --exit-code (opt-in): 0 delivered or scheduled, 1 failed or refused, 3 held, 4 delivered but unverified. Without the flag, exit codes are unchanged (a held message still exits 0).
    • Delivered-but-unverified does exist in the source. Tower reports it as verified: false, an unverifiedCause (no-echo or input-raced), or degraded (the write was not serialized). All three already printed warnings; under --exit-code they exit 4.
    • With --all, the exit is the worst outcome across recipients, in this order: failed, unverified, held, then scheduled or delivered. It exits 1 when there are no builders to reach.
  • --retry <n>: up to N retries in total, only when the request provably never reached Tower (connection refused).
    • The /health pre-check sends nothing, so any failure of it may be retried.
    • The send is never retried after a held message, a refusal from Tower, or a timeout.
    • Back-off is 1s, 2s, 4s, then 8s, and each retry is announced on stderr.

2. afx cleanup --keep-ignored

Before the worktree is removed, it copies to ~/.agent-farm/kept/<workspace-name>/<builder-id>/:

  • every untracked file that is not ignored;
  • ignored files that match the keep list. The default is codev/projects/**, codev/state/**, .builder-*, tmp/**, scratch/**; cleanup.keepIgnored in .codev/config.json replaces it.

Rules:

  • Files are selected by git itself (ls-files --others, plus --ignored with :(glob) pathspecs), so git never walks node_modules.
  • Dependency and build output (node_modules, dist, build, target, virtualenvs and the like) is never copied, whatever the list says. Neither are symlinks.
  • Every copy is checked by SHA-256.
  • If the kept folder already exists, cleanup stops with an error naming it, before anything is touched.
  • If any copy or checksum fails, the partial kept folder is removed and cleanup stops before the worktree, the branch or the registry row is removed.
  • On success it prints the kept folder, the number of files, and how many matches were left out as dependency or build output.

3. afx spawn --max-load <n> [--wait <seconds>]

  • Reads the one-minute load average before anything is created. At or above N it exits 75 with the load and the limit: no branch, no worktree, no registry row.
  • --wait re-reads every 15 seconds up to that long, with a last read at the deadline.
  • On native Windows it fails as unsupported with exit 1.

4. porch -C <dir>

  • Works like git -C: it goes before the command, can be repeated (each -C resolves against the last), applies to every subcommand, and prints the resolved directory on stderr.
  • It changes the process's working directory, so project auto-detection, checks and git commits behave exactly as if porch had been started there.
  • approve and skip-phase still require --a-human-explicitly-approved-this.

Docs

  • The afx and porch skills, in all four copies of each (.claude, .codex, and their codev-skeleton/ twins).
  • resources/commands/agent-farm.md and overview.md in both codev/ and codev-skeleton/.
  • The shipped-skill hash history was regenerated. CLAUDE.md and AGENTS.md are unchanged.

Adjacent bug fixed in place

The SDK never recognised a refused connection. TowerClient.request() (and two sibling call sites) looked for ECONNREFUSED in err.message. Node's fetch throws TypeError('fetch failed') and puts ECONNREFUSED on err.cause, so callers got fetch failed instead of Tower not running. The check now reads err.cause as well, and request() and sendMessage() carry a typed connectionRefused flag, which --retry relies on. Verified against a real refused port: {"ok":false,"error":"Tower not running","connectionRefused":true}.

Key Decisions

  • Exit code 4 covers all three forms of delivered-but-unverified (verified: false, unverifiedCause, degraded). Each means the message is recorded as delivered and will never be re-sent, but may not have arrived clean.
  • Severity under --all: failed > unverified > held > scheduled/delivered. An unverified message needs a human to check it, while a held one will deliver on its own.
  • One --retry budget for the whole invocation (--all included), so a dead Tower costs at most N back-offs in total.
  • Retry only on a typed connectionRefused flag, not by string-matching the error.
  • The keep copy runs after the session is killed and before removal. The kept-folder and config checks run before anything is touched. A failed copy removes its own partial folder, so a re-run is not blocked by the refuse-to-overwrite rule.
  • Symlinks are skipped, not followed. A worktree's links point back into the workspace, and following one could copy a secret such as .env.
  • --keep-ignored only acts where cleanup removes a worktree (bugfix and task builders). For other builders it says there is nothing to keep.
  • --max-load refusals that retrying cannot fix exit 1, not 75. That covers Windows and bad arguments, so a script that retries on 75 cannot loop forever.
  • porch -C uses process.chdir, so every cwd-keyed behaviour matches running porch from that directory.

Test Plan

  • Unit tests added:

    File Covers
    air-1950-send-exit-retry.test.ts (34) Each outcome's exit code under --exit-code through the real CLI; unchanged exits without it, including --all with a failure; worst-of under --all, and zero recipients; --retry resends after a refused connection, but never after held, a refusal or a timeout; waits out a down Tower; --message-file as written (quotes, $HOME, backticks, inner and trailing whitespace; only the final line ending, CRLF included, dropped), combined with --file, both-ways refusal, 48 KB on the combined result
    air-1950-keep-files.test.ts (9) A real git repo: selection, copying and verification, refusal to overwrite an existing folder, a checksum mismatch removes the partial copy, nested repos, symlinks skipped
    air-1950-cleanup-keep-ignored-e2e.test.ts (2) The real cleanup() with the real keep module, a real git worktree and a real cleanup.keepIgnored in .codev/config.json: the right files land before removal is attempted (and .env, node_modules and keep-listed dependency output do not); a second cleanup refuses on the existing folder and touches nothing
    air-1950-cleanup-keep-ignored.test.ts (5) A checksum mismatch aborts with worktree, branch and row intact; an existing kept folder aborts before the session is touched; the keep runs after the kill and before removal
    air-1950-spawn-max-load.test.ts (11) Over the limit, the CLI exits 75 and never reaches spawn(); --wait cadence and deadline; Windows unsupported; argument validation
    air-1950-porch-dash-c.test.ts (7) porch -C <wt> status --json gives output identical to running inside the worktree; repeated -C; approve and skip-phase still refused without the flag; bad or missing directory
    sdk tower-client.test.ts (+3) Node's fetch failed with ECONNREFUSED on cause counts as refused, as does a non-Error throw; a timeout does not
  • Build passes, both locally (porch build check) and on CI.

  • All tests pass on CI: GitHub 8/8 checks at 666c0965c, and again at the final head 2ebbefbbd. Locally the full suite does not pass on this machine; see Flaky Tests. The porch local tests check was skipped under the owner's standing ruling main-32: GitHub's green run stands in for the local suite when the local failures are unrelated, each passes alone, and CI on the same code is green.

  • Checked by hand with the built binaries, from a scratch directory:

    • porch -C <worktree> status printed [porch -C: running in …] and resolved project 1950.
    • porch -C <missing> status exited 1 naming the path.
    • afx spawn 99999 --protocol air --max-load 0.01 exited 75 ("load average is 20.55 … Nothing was created"); with --wait 1 it re-checked once, then exited 75. The scratch directory stayed empty.
    • The SDK sendMessage to a port with nothing listening returned connectionRefused: true.
  • Native Windows (on-demand workflow, suites on):

  • Not run against the real thing: afx cleanup --keep-ignored on a real builder (it needs a disposable bugfix builder; the end-to-end test runs the real cleanup() and keep code on a real git worktree) and afx send --retry against a stopped Tower (stopping Tower needs the owner's permission).

Review Notes

  • CMAP, round 1: codex REQUEST_CHANGES, claude APPROVE. All addressed in e9049abfd:

    • codex: the branch was 20 commits behind main (merged; only the generated skill-hash file conflicted, regenerated after the merge); --message-file used trimEnd() (now drops only the file's final line ending, which would otherwise submit under --raw --no-enter); the Windows workflow was not linked (dispatched above).
    • claude: added an end-to-end cleanup() test; the abort message now says the session was already stopped; dependency and build output left out is counted; --all with no builders exits 1 under --exit-code; the nested-directory walk no longer needs Node 20.1; the non-Error refusal branch is tested.
  • Attribution: the first feature commit (b6dbb1377) carries a Co-Authored-By trailer, against this repo's commit rules. It is already pushed, and removing it means rewriting history, so it was left for the owner to decide. Later commits and this body carry no attribution.

  • Where to look: send.ts (outcome classification and the retry loop), lib/keep-files.ts (the copy and verify, and abort-before-removal in cleanup.ts), and the SDK's isConnectionRefused.

  • Lessons:

    • Before giving a code its own meaning, check what the source can actually return. The issue's "delivered-but-unverified" case does exist, and in three forms, not one.
    • A check that string-matches an error message can silently never fire. Node's fetch moves the useful error code onto cause. Prefer a typed flag set where the error is classified.
    • Letting git pick the files (ls-files with :(glob) pathspecs) avoids re-implementing gitignore semantics and keeps git out of dependency trees.
  • Noticed, not changed: porch status --json with an auto-detected project prints [auto-detected …] on stdout before the JSON. This was true before this PR and is left out of scope.

Flaky Tests

The local porch tests check (npm test -- --exclude='**/e2e/**') failed three times on tests this PR does not touch. Every failure was a test hitting its time limit.

  • Run 1 (load about 20–30): 15 of 8,438 tests failed, each hitting the ~5s test timeout, in 9 files:

    • src/commands/consult/__tests__/claude-cli-lane.test.ts (4)
    • src/commands/consult/__tests__/bugfix-1754-dotenv-auth.test.ts (2)
    • src/commands/consult/__tests__/tripwire-wiring.test.ts (2)
    • src/commands/consult/__tests__/agy-auth-cache.test.ts (1)
    • src/agent-farm/__tests__/bugfix-1267-launch-loop.test.ts (2)
    • src/agent-farm/__tests__/pir-1233-session-launch-loop.test.ts (1)
    • src/agent-farm/__tests__/ide-command.test.ts (1)
    • src/__tests__/doctor.test.ts (1)
    • src/__tests__/bugfix-1455-pr-create-concept.test.ts (1)

    Run on their own, all 9 files pass (221/221).

  • Run 2 (the one retry the architect allowed): porch's summary does not name the failing tests, and the load reached 44 during it.

  • Run 3 (asked for by the architect once the machine was quiet): the load was 7.7 at the start and 59 at the end, so the suite itself saturates this machine. vitest's results cache names 5 failed files, all from the list above, each taking 15–48s instead of a few seconds: bugfix-1267-launch-loop, ide-command, pir-1233-session-launch-loop, agy-auth-cache, tripwire-wiring. All five air-1950-* test files passed.

  • CI on a clean runner (666c0965c): all green. The codev unit suite ran the same 422 files with no failures (409 passed, 13 skipped), including all 9 files above. The CLI integration, Tower integration, browser and package install checks also passed.

None of these tests were skipped or reconfigured. After the owner's ruling, the porch local tests check was skipped for one porch done run only, through this worktree's gitignored .codev/config.local.json, and the skip was removed straight after; nothing under .codev is committed.

waleedkadous and others added 4 commits November 23, 2026 10:53
…p --keep-ignored, spawn --max-load, porch -C

- afx send: --message-file reads the body from a file; opt-in --exit-code
  (0 delivered/scheduled, 1 failed/refused, 3 held, 4 delivered but unverified;
  worst across --all); --retry resends only on a refused connection.
- sdk: recognise ECONNREFUSED on err.cause (Node fetch reports 'fetch failed'),
  and carry a typed connectionRefused flag through request()/sendMessage().
- afx cleanup --keep-ignored: copy untracked + keep-listed ignored files to
  ~/.agent-farm/kept/<workspace>/<builder-id>/ with SHA-256 verification,
  refusing an existing folder and aborting before any removal on failure.
- afx spawn --max-load/--wait: exit 75 before anything is created.
- porch -C <dir>: run as if started in <dir>, like git -C.
- Docs: afx/porch skills and command references in both trees.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@waleedkadous waleedkadous changed the title [Air #1950] afx send --message-file/--exit-code/--retry, afx cleanup --keep-ignored, afx spawn --max-load, porch -C [Air #1950] feat: afx send --message-file/--exit-code/--retry, cleanup --keep-ignored, spawn --max-load, porch -C Nov 23, 2026
# Conflicts:
#	packages/codev/src/lib/shipped-skill-hashes.ts
…eep-ignored reporting, zero-recipient exit

- --message-file drops only the single line ending that terminates the file (was trimEnd()).
- --keep-ignored counts matches left out as dependency/build output; the abort message says the
  session was already stopped; nested directories walked by hand (Node 20.0 has no recursive readdir).
- afx send --all --exit-code exits 1 when there are no builders to reach.
- End-to-end cleanup() test with the real keep module, a real git worktree and a configured keep list.
- Docs and skills updated; shipped-skill hashes regenerated after merging main.
On a real Windows runner the gate correctly refuses (no load average), so the
load-path cases must force a POSIX platform through the host-platform seam.
@waleedkadous
waleedkadous marked this pull request as ready for review November 23, 2026 21:50
@waleedkadous
waleedkadous merged commit 98f6508 into main Nov 23, 2026
8 checks passed
waleedkadous added a commit that referenced this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

afx/porch: four small CLI additions (afx send --message-file/--exit-code/--retry, afx cleanup --keep-ignored, afx spawn --max-load, porch -C)

1 participant