Skip to content

refactor(subagents): run the subagent loop on the shared turn engine - #2113

Merged
will-lamerton merged 2 commits into
mainfrom
refactor/subagent-turn-engine
Nov 11, 2026
Merged

will-lamerton merged 2 commits into
mainfrom
refactor/subagent-turn-engine

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

Step 3 of moving the agent loops onto the shared turn engine (#2097 plain, #2102 ACP). runSubagentConversation's while (true) loop now runs on runTurnEngine. Subagents keep their current behaviour: the engine features they did not have are switched off through a new policy object. Turning the guards on is a separate follow-up with a changeset.

Engine additions

  • TurnEnginePolicy (every flag defaults to on, so plain and ACP do not change): parseTextToolCalls, emptyTurnGuard, truncatedTurnGuard, repeatedToolCallGuard, and recordToolFreeTurns. When recordToolFreeTurns is off, a turn with no tool calls is returned and not recorded, so the final turn is never compacted.
  • onToolCallsEmitted(toolCalls): runs before the turn is recorded. Subagents run their own repeated-call check and argument token estimate here, at the same point as before. That point comes before the assistant message is recorded and before compaction.
  • onTurnRecorded(): runs after the assistant message is recorded and compaction has run, before the abort check. Subagents reset streaming and update the live session view here.
  • prepareTurn may be async, for the subagent's 50ms render yield. An abort that shows up as a thrown model call now carries that error (aborted.error), so a subagent rethrows what the client threw, as it did before. The abort check after compaction now sits inside the recorded-turn block. For plain and ACP every turn is recorded, so nothing changes for them.

Subagent adapter

Each part of the old loop moved into an adapter hook unchanged:

subagent loop piece now in
iterations++, the running progress event, the 50ms yield, capturing provider and model prepareTurn
streaming into the session view and the live token count beginModelCall
onApiCallComplete usage and the assistant transcript onModelResponse
pass-through partition (executeToolCall refuses tools the subagent can't use with its own message) partitionToolCalls
the repeated-call SubagentLoopStopError (same message and partial output), and the token estimate for arguments onToolCallsEmitted
per-call abort, progress, the read-scope executeToolCall, and the incremental session-view update executeTools

The policy turns off text parsing, the empty and truncated guards, the engine's repeated guard and the turn ceiling (maxTurns: Infinity). It also stops content-only turns from being recorded. How a run ends: completed and no-response emit complete and return the answer, or '' for no reply. An abort emits error and throws Aborted, or rethrows the client's error. A model error is rethrown. The remaining result kinds can't happen under this policy and throw.

Ignoring whitespace, subagent-executor.ts is +190/−100. The rest of the raw diff is re-indentation into the adapter.

Behaviour

No change intended. The contract spec now runs every scenario through subagents as well. Where they don't have the guards, it asserts what they do instead: an empty, malformed or truncated reply is the answer; an unknown tool gets the subagent's own "not available" result; there is no turn ceiling. The extended contract spec passes against the old subagent-executor.ts from main and against the new one.

Tests

  • turn-engine.contract.spec.ts: plain, ACP and subagent runners (30 scenarios).
  • turn-engine.spec.ts: new tests for each policy flag, the order of the two new hooks, and a throw from a hook. The existing abort test now also asserts the carried error.
  • No existing subagent, ACP or plain spec was changed.
  • Ran tsc --noEmit, biome check, knip (both passes), and AVA over source/agent-loop, source/plain, source/acp, source/subagents (590) and source/hooks (513), plus tools/agent-tool and no-ink-fast-paths. All passed after merging the latest main.

No changeset: this is a refactor with no user-visible change.

Next in the sequence

  1. A fix with a changeset: "no reply from the model" becomes an error in every frontend.
  2. Subagents get the engine guards (empty/malformed/truncated, the shared repeated guard, a turn ceiling), with a changeset. In the contract spec this removes the guarded: false exceptions.
  3. Finite-only usage counting, with a changeset (this affects plain).
  4. The TUI loop, in two PRs.

🤖 Generated with Claude Code

will-lamerton and others added 2 commits November 11, 2026 16:57
runSubagentConversation's while(true) loop becomes a TurnEngineAdapter.
Progress events, the live session view, usage reporting, the transcript,
token estimates and per-call execution move into adapter hooks unchanged.

Subagents keep today's behaviour through a new TurnEnginePolicy: no text
tool-call parsing, no empty/truncated/repeated engine guards, no turn
ceiling, and content-only replies returned rather than recorded (so the
final turn is not compacted). Their own repeated-call check runs in the
new onToolCallsEmitted hook, before the turn is recorded, as before; the
session view update runs in onTurnRecorded, after compaction. prepareTurn
may now be async for the 50ms render yield, and an abort that surfaces as
a thrown model call carries that error.

The contract spec now runs every scenario through subagents too, asserting
what they do where they run without the guards. It passes against the old
loop and the new one.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

No changeset found

This PR does not add a changeset, so it will not appear in the changelog or trigger a release.

If the change is user-facing, add one:

pnpm changeset

Pick a bump (patch / minor / major) and write the changelog entry in our usual voice ("Added X... Thanks to @you. Closes #123."), then commit the generated .changeset/*.md file.

If this PR is docs-only or a chore that needs no release note, you can ignore this — or run pnpm changeset --empty to record that intentionally.

@github-actions

Copy link
Copy Markdown
Contributor

nc-review could not produce a verdict this run (agent exit 1). This is a problem with the review agent, not with your pull request. A maintainer can retry with /re-review.

@will-lamerton
will-lamerton merged commit 0cac34d into main Nov 11, 2026
19 checks passed
@will-lamerton
will-lamerton deleted the refactor/subagent-turn-engine branch November 11, 2026 18:45
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…ano-Collective#2113)

runSubagentConversation's while(true) loop becomes a TurnEngineAdapter.
Progress events, the live session view, usage reporting, the transcript,
token estimates and per-call execution move into adapter hooks unchanged.

Subagents keep today's behaviour through a new TurnEnginePolicy: no text
tool-call parsing, no empty/truncated/repeated engine guards, no turn
ceiling, and content-only replies returned rather than recorded (so the
final turn is not compacted). Their own repeated-call check runs in the
new onToolCallsEmitted hook, before the turn is recorded, as before; the
session view update runs in onTurnRecorded, after compaction. prepareTurn
may now be async for the 50ms render yield, and an abort that surfaces as
a thrown model call carries that error.

The contract spec now runs every scenario through subagents too, asserting
what they do where they run without the guards. It passes against the old
loop and the new one.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

1 participant