Skip to content

feat(subagents): apply the turn engine's retry guards and a turn ceiling - #2125

Merged
will-lamerton merged 1 commit into
mainfrom
feat/subagent-retry-guards
Nov 11, 2026
Merged

will-lamerton merged 1 commit into
mainfrom
feat/subagent-retry-guards

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

This PR turns on, for subagents, the guards they skipped when they moved onto the turn engine in #2113 (decision 4: "subagents adopting the guards: yes, as a deliberate separate PR with a changeset"). They now get the same retry limits and turn ceiling as --plain and ACP.

What was wrong

#2113 kept subagents' old behaviour by switching engine features off. That behaviour had gaps:

  • Empty reply. A reply with no text and no tool calls was handed to the parent as a successful empty result. This is common with reasoning models that spend their budget thinking.
  • XML/JSON fallback. On a model without native tool calling, tool calls written as text were never parsed. A subagent on such a model could not call tools at all, and its tool-call text came back as the "answer".
  • Truncated reply. A reply cut off at the output-token limit was returned as the answer. That is the same silent failure maxTruncatedTurns fixed for --plain.
  • No turn ceiling. A subagent cycling between different calls (A, B, A, B...) ran until the parent cancelled it.

Change

  • Subagent policy: everything on. That means text tool-call parsing, the empty, malformed and truncated guards, the shared repeated-call guard, and maxTurns: getSubagentMaxTurns().
    • The subagent's own repeated-call counter is removed. The shared guard counts calls the same way (one signature over every emitted call, same cap). Two things change:
      • The message now starts "Model repeated…" instead of "Subagent repeated…". Existing specs match on "repeated the same tool call" and still pass.
      • The check runs after the turn is recorded rather than before. This matches plain and ACP.
    • Unknown tools still go through executeToolCall, which refuses them with the subagent-specific "not available to this subagent" result.
  • Stops: every guard stop, and a final tool-free turn that still calls tools, throws SubagentLoopStopError. The parent therefore gets success: false, an error naming the setting, and the partial output, as the repeated-call stop already did.
  • New config nanocoder.headless.subagentMaxTurns: the turn ceiling for each delegated run, minimum 1. When unset it is the headless maxTurns (default 200, and NANOCODER_MAX_TURNS applies). The default is never lower than the headless one. getSubagentMaxTurns() lives in config/index.ts.
    • loadHeadlessConfig now reads the file block even when the env var is set. The env var still overrides maxTurns exactly as before.
    • The schema is regenerated (schemas/agents.config.schema.json).
  • Engine: the recordToolFreeTurns policy flag is removed, since only subagents used it. One consequence is that a subagent's final answer is now recorded, so auto-compaction can run after it, as it does in plain and ACP. The onTurnRecorded session-view update now also shows the final assistant message.
  • Docs: docs/features/subagents.md (Loop Protection lists every limit and the ceiling), docs/configuration/index.md (Headless table gains subagentMaxTurns, and the Retry Limits text no longer says ACP and subagents are exempt; that was stale), and the RetryLimitsConfig comments.

Tests

  • In the contract spec, subagents now run every scenario under the same expectations as plain and ACP. The guarded: false exceptions from refactor(subagents): run the subagent loop on the shared turn engine #2113 are gone.
  • subagent-executor.spec.ts:
    • a subagent that keeps calling tools stops at subagentMaxTurns, after a final turn that offers no tools, and returns its partial output
    • an empty reply is nudged and not returned
  • config/index.spec.ts:
    • the subagent limit follows maxTurns and the env var when unset
    • subagentMaxTurns loads, wins over the env var, and is clamped to at least 1
  • The engine spec's policy test drops the removed flag.
  • Ran tsc --noEmit, biome check, knip (both passes), and the schema generator. AVA one directory at a time: subagents 109, agent-loop 48, config 465, acp 358, plain 86, tools/agent-tool + verify 91.

Changeset: minor (new behaviour plus a new config option).

Next

Finite-only usage counting (decision 1, with a changeset), then the two TUI PRs.

🤖 Generated with Claude Code

Subagents ran on the shared turn engine with its guards switched off.
They now get the full policy: empty turns are nudged, malformed XML/JSON
tool calls are fed back for correction (text tool calls are parsed at
all), truncated replies are continued, the shared repeated-call guard
replaces the subagent's own copy, and the run is bounded by a turn
ceiling whose last turn asks for a tool-free answer.

The ceiling is the headless maxTurns unless the new
nanocoder.headless.subagentMaxTurns sets its own. Every guard stop
fails the run with SubagentLoopStopError, so partial output still
reaches the parent. The engine's recordToolFreeTurns policy flag,
which only subagents used, is removed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added the area:docs Documentation label Nov 11, 2026
@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 27e56ee into main Nov 11, 2026
19 checks passed
@will-lamerton
will-lamerton deleted the feat/subagent-retry-guards branch November 11, 2026 19:26
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…ing (Nano-Collective#2125)

Subagents ran on the shared turn engine with its guards switched off.
They now get the full policy: empty turns are nudged, malformed XML/JSON
tool calls are fed back for correction (text tool calls are parsed at
all), truncated replies are continued, the shared repeated-call guard
replaces the subagent's own copy, and the run is bounded by a turn
ceiling whose last turn asks for a tool-free answer.

The ceiling is the headless maxTurns unless the new
nanocoder.headless.subagentMaxTurns sets its own. Every guard stop
fails the run with SubagentLoopStopError, so partial output still
reaches the parent. The engine's recordToolFreeTurns policy flag,
which only subagents used, is removed.

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

area:docs Documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant