Public: anyone can read this; the crew can change it.

Split work() in src/crew/runner.ts into steps that take their state as a plain object #16813

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

work() in src/crew/runner.ts is down from about 1,160 lines to 788, and runner.ts from 2,736 to 2,123. Behaviour is unchanged. The 31 test files that import runner.ts plus the new step tests pass (385 tests), tsc --noEmit is clean, and gate.ts --fix reported nothing.

Notes

opened this

work() in src/crew/runner.ts is down from about 1,160 lines to 788, and runner.ts from 2,736 to 2,123. Behaviour is unchanged. The 31 test files that import runner.ts plus the new step tests pass (385 tests), tsc --noEmit is clean, and gate.ts --fix reported nothing. I did not run the full suite, and the forge's check will.

The cut is partial. work() is still a single 788-line function and the issue's "split it into steps" is only half done. The setup, the agent invocation and the landing are still inline in it.

What moved where. Each step takes its state as a plain object and returns plain data. No step imports runner.ts, and runner.ts re-exports the names tests and other modules already import (settleReview, reviewInputs, conflictFilesOf, convergeFollowing).

  • run_outcome.ts holds the Outcome and Meter types the steps share.
  • run_spend.ts has settleSpend: picking the account, the wallet, lent-credit and ceiling checks, and the budget. A refusal comes back as an outcome.
  • run_limits.ts has limitEnding: a provider refusal becomes a pause, or "another account takes over", or the time the limit lifts.
  • run_verdict.ts has verdictOf, which handles a decline (including "already done on the base") or a review's verdict, and now holds settleReview.
  • run_retry.ts has retryNetwork and outagePause, which were closures inside work().
  • run_prompt.ts has promptFor, which builds the task prompt, and also holds reviewInputs, lastTryOf, conflictFilesOf and convergeFollowing.
  • AGENTS.md now names this boundary.

I did not run bun scripts/similar.ts, so nothing was checked against existing functions and there is nothing to report as reused. The steps are cut-and-paste moves of the existing code.

Tests. test/run-steps.test.ts covers retry, outage pause, limit endings, verdicts, settleReview, conflictFilesOf and lastTryOf on their own. settleSpend and promptFor have no direct tests; the executeAgentRun tests only exercise them through work().

Not done.

  • The remaining body of work() (clone and credential setup, the agent invocation closure, landing and the finally cleanup) is still inline. The next cuts would be a prepareRun step and a settleRun step.
  • I did not touch prepareClone or landWork.

To try. bun test test/run-steps.test.ts.

🤖 Generated with Claude Code

Closes #16789

4 earlier comments
commented

work() in src/crew/runner.ts is down from about 1,160 lines to 788, and runner.ts from 2,736 to 2,123. Behaviour is unchanged. The 31 test files that import runner.ts plus the new step tests pass (385 tests), tsc --noEmit is clean, and gate.ts --fix reported nothing. I did not run the full suite, and the forge's check will.

The cut is partial. work() is still a single 788-line function and the issue's "split it into steps" is only half done. The setup, the agent invocation and the landing are still inline in it.

What moved where. Each step takes its state as a plain object and returns plain data. No step imports runner.ts, and runner.ts re-exports the names tests and other modules already import (settleReview, reviewInputs, conflictFilesOf, convergeFollowing).

  • run_outcome.ts holds the Outcome and Meter types the steps share.
  • run_spend.ts has settleSpend: picking the account, the wallet, lent-credit and ceiling checks, and the budget. A refusal comes back as an outcome.
  • run_limits.ts has limitEnding: a provider refusal becomes a pause, or "another account takes over", or the time the limit lifts.
  • run_verdict.ts has verdictOf, which handles a decline (including "already done on the base") or a review's verdict, and now holds settleReview.
  • run_retry.ts has retryNetwork and outagePause, which were closures inside work().
  • run_prompt.ts has promptFor, which builds the task prompt, and also holds reviewInputs, lastTryOf, conflictFilesOf and convergeFollowing.
  • AGENTS.md now names this boundary.

I did not run bun scripts/similar.ts, so nothing was checked against existing functions and there is nothing to report as reused. The steps are cut-and-paste moves of the existing code.

Tests. test/run-steps.test.ts covers retry, outage pause, limit endings, verdicts, settleReview, conflictFilesOf and lastTryOf on their own. settleSpend and promptFor have no direct tests; the executeAgentRun tests only exercise them through work().

Not done.

  • The remaining body of work() (clone and credential setup, the agent invocation closure, landing and the finally cleanup) is still inline. The next cuts would be a prepareRun step and a settleRun step.
  • I did not touch prepareClone or landWork.

To try. bun test test/run-steps.test.ts.

🤖 Generated with Claude Code

commented

I merged main into the branch, resolved the src/crew/runner.ts conflict and pushed. tsc --noEmit is clean, and the 29 test files that touch runner.ts, meter.ts, land_work.ts or the new steps pass (330 tests, 0 failures). I didn't run the full suite, and gate.ts --fix reported nothing.

The conflict was with #16785, which split other parts of runner.ts into meter.ts, clone.ts, land_work.ts, steer.ts and credential_env.ts. Those cuts and mine don't overlap, but both sides had moved the Outcome and Meter types.

  • Resolution: I took main's runner.ts and re-applied my extractions on top of it. The work() body and the cut-out helpers are the same ones as before the merge.
  • Types: The steps now import Outcome and Meter from main's meter.ts. My run_outcome.ts is deleted, and AGENTS.md names meter.ts instead.
  • Cleanups:
    • I removed leftover empty import { } from lines in runner.ts that my earlier pruning had produced.
    • I removed a helper script (.cache-prune.py) that I had committed by mistake.

With both splits in, runner.ts is 1,434 lines.

commented

work() in src/crew/runner.ts is now only the order of the steps; the prepare, wrap-up and settle steps are split out, and settleSpend and promptFor have direct tests. bun x tsc --noEmit is clean and the 34 test files that mention the runner or a step pass. I did not run the whole suite.

1. The cut is finished. Three steps left work(). Each takes its state as a plain object and returns plain data:

  • run_prepare.ts (prepareAgent): the environment, sandbox, run temp dir and devices, the model choice, the deadline, and the metered invoke closure.
  • run_wrapup.ts (wrapUp): the reserve turn after a budget stop. It returns either the run's ending or the whole answer to judge, plus stoppedAt.
  • run_settle.ts (settleRun): the cancel check, the refused token, a decline or a verdict, and otherwise the landing with its outcome.

runner.ts goes from 1434 to 1088 lines, and work() is no longer the ~790-line function the review counted.

  • The finally still frees what a failed setup half-made. The prepare step writes devices, gateway door, temp dir and model secrets into a Held object as it goes, and the runner keeps that object.
  • wrapUpReserve moved to run_prepare.ts and runner.ts re-exports it.
  • The moved code is cut-and-paste apart from names. AGENTS.md lists the three new steps. It also now says a step imports only the CrewOptions type from runner.ts. That was already true of land_work.ts, and the three new steps do the same.

2. Tests in test/run-steps.test.ts.

  • settleSpend is tested for:
    • the plain start: the budget, the account noted on the meter, and the agent's budget against the default;
    • no model account, and the wallet cap reached;
    • the per-run ceiling lowering the budget, the daily ceiling spent, and a per-run ceiling too small;
    • a granted account being used;
    • every account resting, as a pause and then as a failure after MAX_PAUSES;
    • the breathing hold, which holds the crew's run but not a person's;
    • on a forge somebody else owns: spent lent credit, lent credit capping the budget, and the per-run and daily limits.
  • promptFor is tested for the issue, its body and discussion reaching the agent, the conflict files read off the summary, and the "cut short" opener on a paused run. I did not cover its review, planner or foreign-pull branches.
  • wrapUp also got two tests: no wrap-up without a budget stop, and one more turn with the spend summed.

I changed one existing test, test/crew-devices.test.ts. It read runner.ts's source for strings that moved, so I pointed it at the files that now hold them.

3. Tidy.

  • runner.ts imports are pruned to what it uses, with no multi-line shapes or stray blank lines left.
  • run_prompt.ts imports are ordered: node first, then ../, then ./. forkParentFor now sits with its repos.ts import. The new step files got the same ordering.
  • The repo has no formatter, so I did this with a throwaway script, since deleted.
  • bun scripts/similar.ts ran and says "nothing like it under src/". bun scripts/gate.ts --fix rewrapped comments, which are committed. Its two notes on the new test helpers user and costing are similarity hints, not failures. I left them because each test needs its own fixtures.

Not done. The brief assembly and the clone steps stay in work(). The issue's named steps are covered, so I did not open follow-ups. No dependency was added, and no page or terminal surface changed.

Try it. bun test test/run-steps.test.ts test/crew-devices.test.ts, then bun x tsc --noEmit.

🤖 Generated with Claude Code

commented

I merged main into the branch and pushed it. The merge now applies cleanly. I only ran test/run-steps.test.ts and test/crew-accounts.test.ts (40 pass, 0 fail) and bun x tsc --noEmit. I did not wait for the wider suite, so the forge's check on this push is the first full run of the merged tree.

The conflict. src/crew/runner.ts was the only file that conflicted. main made one change there: the "resting" message from #16498 now ends with accountsSaid(app, repo), which names the accounts. This branch had already moved that message into src/crew/run_spend.ts. I kept this branch's runner.ts and put the accountsSaid change, with its import, in run_spend.ts. Behaviour matches main.

Other changes in the merge:

  • bun scripts/gate.ts --fix rewrapped comments in six files that came from main (src/actions/infra.ts, src/http/mcp/tools/infra.ts, src/tui/screens/infra.ts, test/cloud-connect.test.ts, test/crew-accounts.test.ts, test/talk-cost-retired.test.ts). They went into the merge commit.
  • The pre-push hook refused the push because the merged diff touches pages and no commit said how to try it. I added an empty Try: commit saying this pull itself changes no page. That is the only reason the commit exists.
  • The gate also flagged test/run-steps.test.ts helpers user and costing as near-duplicates of helpers in test/tui-egress.test.ts and test/forge-budget-verdict.test.ts. I left them as they are.

To try it. Run bun test test/run-steps.test.ts test/crew-accounts.test.ts.

commented

I merged main into the branch and pushed it. The gate now passes its refusing rules. It still prints two advisory notes that I left alone. tsc --noEmit is clean and test/run-steps.test.ts passes (32 tests). I did not run the wider suite, so the forge's check on this push is the first full run of the merged tree.

The conflict. main changed one line in src/crew/runner.ts: it now imports lastWeekFor from ./last_week.ts instead of ./autonomy.ts. This branch had already moved that import into src/crew/run_prompt.ts, so I kept this branch's runner.ts and made the same import change in run_prompt.ts.

The earlier failed check. It was refused for two reasons, and both are fixed:

  • Rewrap noise. My previous merge ran gate.ts --fix, which rewrapped comments I hadn't written. I restored main's versions of the six files it touched: src/actions/infra.ts, src/http/mcp/tools/infra.ts, src/tui/screens/infra.ts, test/cloud-connect.test.ts, test/crew-accounts.test.ts and test/talk-cost-retired.test.ts.
  • Both doors. The merge commit now has a No door: line, because this pull is an internal split of work() with no page, route, tool or screen change.

Left alone. The gate still asks "one implementation?" about two helpers in test/run-steps.test.ts, user and costing. They resemble make in test/tui-egress.test.ts and spend in test/forge-budget-verdict.test.ts. The gate treats these as notes, not refusals, so I didn't merge them.

To try it. Run bun test test/run-steps.test.ts.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs