Repository navigation
fix(session): return a schema run's result at the step limit (0.5.7) - #268
Conversation
A schema run that reached its step cap got the text-only max-steps prompt, which forbids every tool call, including StructuredOutput. A run with its result file already written then failed with step_limit (aictrl execution 50fca695: result.json written at call 104, lost at the 120-step cap). In schema mode the last step now asks for StructuredOutput with the file already written, with tool choice forced where the model accepts it. Runs without --output-schema keep the text-only last step. Release 0.6.0. refs #267 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Br5cc1EsMnL19BcXrbDvUC
| tools, | ||
| model, | ||
| toolChoice: contract ? structuredToolChoice(model, reminder) : undefined, | ||
| toolChoice: contract ? structuredToolChoice(model, notice) : undefined, |
There was a problem hiding this comment.
🟡 Last-step "no other tool will run" unenforced on auto.
🤖 Fix with your agent
Fix this code review finding (aictrl-dev/cli PR #268, packages/cli/src/session/prompt.ts:866):
Problem: Last-step "no other tool will run" unenforced on auto
Detail: On the last step of a schema run the new notice tells the model "no other tool call will run", but the harness only enforces that through `toolChoice`. `structuredToolChoice` returns `"auto"` for every model `ProviderTransform.supportsForcedToolChoice` rejects (Claude Sonnet/Opus 5.5 and Fable/Mythos 5.1, transform.ts L69-75), and the request still carries the full work-tool set (`tools` at L864 is unchanged), so on those models a last-step `bash`/`write`/… call is executed by `SessionProcessor` with its side effects; only the next loop iteration fails `step_limit`. Exposure is unchanged from before this PR (the old last step also offered every tool), so this is a hardening gap in the new path rather than a regression.
Repro: Given `--output-schema` with a step cap on a model without forced tool choice (e.g. claude-sonnet-5-5), when the last-step response calls `bash` instead of `StructuredOutput`, then the command runs before the run fails `step_limit`.
Suggested fix: When `isLastStep && contract`, send only the protocol tool to `processor.process` (e.g. `tools: isLastStep && contract ? { StructuredOutput: tools.StructuredOutput } : tools`) so the "auto" case behaves like the forced one, or soften the wording to "no other tool call will get another turn" so the notice matches what the harness enforces.
Implement the fix on the PR head branch and add a regression test that fails before the fix and passes after.
Why this matters
On the last step of a schema run the new notice tells the model "no other tool call will run", but the harness only enforces that through toolChoice. structuredToolChoice returns "auto" for every model ProviderTransform.supportsForcedToolChoice rejects (Claude Sonnet/Opus 5.5 and Fable/Mythos 5.1, transform.ts L69-75), and the request still carries the full work-tool set (tools at L864 is unchanged), so on those models a last-step bash/write/… call is executed by SessionProcessor with its side effects; only the next loop iteration fails step_limit. Exposure is unchanged from before this PR (the old last step also offered every tool), so this is a hardening gap in the new path rather than a regression.
Repro: Given --output-schema with a step cap on a model without forced tool choice (e.g. claude-sonnet-5-5), when the last-step response calls bash instead of StructuredOutput, then the command runs before the run fails step_limit.
...(isLastStep && !contract
? [
{
role: ProviderTransform.isGeminiTarget(model) ? ("user" as const) : ("assistant" as const),
content: MAX_STEPS,
},
]
: []),
],
tools,
model,
toolChoice: contract ? structuredToolChoice(model, notice) : undefined,
structured: contract|
|
||
| ### Behaviour changes | ||
|
|
||
| - **Schema runs return their result at the step limit**: with `--output-schema`, the last step before the agent's step cap now asks the model to call `StructuredOutput` with the result file it has written, with tool choice forced to it where the model accepts forced tool choice. Before, the last step sent the text-only max-steps prompt, which forbade every tool call, including `StructuredOutput`, so a run that reached the cap failed with `step_limit` even when its result file was ready. Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3. Runs without `--output-schema` keep the text-only last step. (#267) |
There was a problem hiding this comment.
⚪ CHANGELOG cites issue #267, not PR #268 + closes.
| - **Schema runs return their result at the step limit**: with `--output-schema`, the last step before the agent's step cap now asks the model to call `StructuredOutput` with the result file it has written, with tool choice forced to it where the model accepts forced tool choice. Before, the last step sent the text-only max-steps prompt, which forbade every tool call, including `StructuredOutput`, so a run that reached the cap failed with `step_limit` even when its result file was ready. Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3. Runs without `--output-schema` keep the text-only last step. (#267) | |
| End the 0.6.0 entry with `(#268, closes #267)` instead of `(#267)`, matching the existing convention. |
🤖 Fix with your agent
Fix this code review finding (aictrl-dev/cli PR #268, CHANGELOG.md:7):
Problem: CHANGELOG cites issue #267, not PR #268 + closes
Detail: The 0.6.0 entry ends with `(#267)`, which is the open bug issue this PR fixes ("Fix: at the step limit, schema mode should force a final StructuredOutput turn"), not the PR number. Every other entry in this file cites the PR first and references the issue with `closes #N`/`refs #N` (e.g. `(#233, refs #214, #229)`, `(#179, closes #135)`). As written, the entry points readers at the bug and does not link the change that resolves it.
Suggested fix: End the 0.6.0 entry with `(#268, closes #267)` instead of `(#267)`, matching the existing convention.
Implement the fix on the PR head branch and add a regression test that fails before the fix and passes after.
Why this matters
The 0.6.0 entry ends with (#267), which is the open bug issue this PR fixes ("Fix: at the step limit, schema mode should force a final StructuredOutput turn"), not the PR number. Every other entry in this file cites the PR first and references the issue with closes #N/refs #N (e.g. (#233, refs #214, #229), (#179, closes #135)). As written, the entry points readers at the bug and does not link the change that resolves it.
### Behaviour changes
- **Schema runs return their result at the step limit**: with `--output-schema`, the last step before the agent's step cap now asks the model to call `StructuredOutput` with the result file it has written, with tool choice forced to it where the model accepts forced tool choice. Before, the last step sent the text-only max-steps prompt, which forbade every tool call, including `StructuredOutput`, so a run that reached the cap failed with `step_limit` even when its result file was ready. Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3. Runs without `--output-schema` keep the text-only last step. (#267)
Code reviewVerdict: Looks good — only minor / nit comments below. · 🔴 0 · 🟠 0 · 🟡 1 · ⚪ 1 · 0/2 resolved
🤖 Fix all 2 open findings with your agent📋 Out-of-diff findings (2)
Reviewed 6 files · 0 inline · view all 2 findings ↗ · AI-generated — verify findings before acting · AI guidance and feedback aictrl · AI code review for fast-moving teams · aictrl.dev |
refs #267 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Br5cc1EsMnL19BcXrbDvUC
A model without forced tool choice (Claude Sonnet/Opus 5.5) could still call a work tool on the last step, and it would run before the run failed step_limit. The last step now sends only the protocol tools. The changelog cites the PR first, refs the issue. refs #267 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Br5cc1EsMnL19BcXrbDvUC
Review response — PR #268I checked both findings from the automatic review against the PR head. Both are real; Issues addressed (pushed to this PR)
Checks: Review claims verified false (no change needed)None. Not addressed hereNone. |
|
|
||
| ### Fixes | ||
|
|
||
| - **Schema runs return their result at the step limit**: with `--output-schema`, the last step before the agent's step cap now asks the model to call `StructuredOutput` with the result file it has written, with tool choice forced to it where the model accepts forced tool choice. Before, the last step sent the text-only max-steps prompt, which forbade every tool call, including `StructuredOutput`, so a run that reached the cap failed with `step_limit` even when its result file was ready. Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3. Runs without `--output-schema` keep the text-only last step. (#267) |
There was a problem hiding this comment.
⚪ CHANGELOG: last-step failure is not always `step_limit`.
| - **Schema runs return their result at the step limit**: with `--output-schema`, the last step before the agent's step cap now asks the model to call `StructuredOutput` with the result file it has written, with tool choice forced to it where the model accepts forced tool choice. Before, the last step sent the text-only max-steps prompt, which forbade every tool call, including `StructuredOutput`, so a run that reached the cap failed with `step_limit` even when its result file was ready. Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3. Runs without `--output-schema` keep the text-only last step. (#267) | |
| Reword the sentence to name the three reasons, e.g.: "Validation and the corrective budget still apply; a run with no valid result on that step still exits 3 with `step_limit` while retries remain, or `missing`/`exhausted` once the corrective budget is spent." |
🤖 Fix with your agent
Fix this code review finding (aictrl-dev/cli PR #268, CHANGELOG.md:7):
Problem: CHANGELOG: last-step failure is not always `step_limit`
Detail: "Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3" is only true while corrective retries remain. When the last step ends without a valid result and the budget is already spent (e.g. `retryCount: 0`, or retries consumed on earlier steps), the post-process branch in `SessionPrompt.run` (packages/cli/src/session/prompt.ts:918-929) breaks with `outcome.reason` = `missing` (prose-only finish → `missed` → `rejection(...)`, attempts+1, rejected+1) or `exhausted` (StructuredOutput rejected on file validation → `onReject`), before the loop ever reaches `step > maxSteps` and assigns `step_limit` (prompt.ts:426-438). Exit code 3 is common to all three. The release note therefore misdirects consumers that match on the `structured_output` outcome reason (the same claim appears as an acceptance criterion in #267). Not a regression: before this PR the last step took the same `missing`/`exhausted` paths when retries were spent.
Suggested fix: Reword the sentence to name the three reasons, e.g.: "Validation and the corrective budget still apply; a run with no valid result on that step still exits 3 with `step_limit` while retries remain, or `missing`/`exhausted` once the corrective budget is spent."
Implement the fix on the PR head branch and add a regression test that fails before the fix and passes after.
Why this matters
"Validation and the corrective budget still apply; a run with no valid result on that step still fails with step_limit and exit 3" is only true while corrective retries remain. When the last step ends without a valid result and the budget is already spent (e.g. retryCount: 0, or retries consumed on earlier steps), the post-process branch in SessionPrompt.run (packages/cli/src/session/prompt.ts:918-929) breaks with outcome.reason = missing (prose-only finish → missed → rejection(...), attempts+1, rejected+1) or exhausted (StructuredOutput rejected on file validation → onReject), before the loop ever reaches step > maxSteps and assigns step_limit (prompt.ts:426-438). Exit code 3 is common to all three. The release note therefore misdirects consumers that match on the structured_output outcome reason (the same claim appears as an acceptance criterion in #267). Not a regression: before this PR the last step took the same missing/exhausted paths when retries were spent.
## 0.5.7 (2026-12-01)
### Fixes
- **Schema runs return their result at the step limit**: with `--output-schema`, the last step before the agent's step cap now asks the model to call `StructuredOutput` with the result file it has written, with tool choice forced to it where the model accepts forced tool choice. Before, the last step sent the text-only max-steps prompt, which forbade every tool call, including `StructuredOutput`, so a run that reached the cap failed with `step_limit` even when its result file was ready. Validation and the corrective budget still apply; a run with no valid result on that step still fails with `step_limit` and exit 3. Runs without `--output-schema` keep the text-only last step. (#267)
Code reviewVerdict: Looks good — only minor / nit comments below. · 🔴 0 · 🟠 0 · 🟡 0 · ⚪ 1 · 0/1 resolved
🤖 Fix all 1 open findings with your agent📋 Out-of-diff findings (1)
Reviewed 6 files · 0 inline · view all 1 findings ↗ · AI-generated — verify findings before acting · AI guidance and feedback aictrl · AI code review for fast-moving teams · aictrl.dev |
Intent
A schema run (
--output-schema) that reaches its step cap can't return a result it has already written. On the last step, the CLI sendsprompt/max-steps.txt, which forbids every tool call, andStructuredOutputis the only way to return the result. The run fails withstep_limitno matter what is on disk.In production on 1 Dec, an aictrl PR Explanation on GLM-5.4 (execution 50fca695) wrote
work/explanation/result.jsonat tool call 104. It then kept revising until the executor's 120-step cap, and its result was lost. Recent PR Explanations used 87 to 124 tool calls, so reaching the cap is normal for this kind of run. This PR addresses issue 267 and releases the change as 0.5.7.What changes
packages/cli/src/session/prompt.ts: in schema mode, the last step sends a system reminder instead ofmax-steps.txt: "The step limit is reached: this is the last step, and no other tool call will run. Call StructuredOutput now with the path of the result file you have written, as it stands."StructuredOutputthrough the existingstructuredToolChoice. Models that reject forced tool choice, such as Claude Sonnet 5.5 and Opus 5.5, getautoand the same reminder.failed / step_limitand exit 3.--output-schema, the last step is unchanged: text only, usingmax-steps.txt.EVENTS.md: thestep_limitreason says the last step asks only forStructuredOutput.packages/cli/package.jsonand thebun.lockworkspace entry go to 0.5.7;CHANGELOG.mdgets a0.5.7 (2026-12-01)entry under Fixes.Expected Impact
--output-schema, behave as before.Test Plan
test/session/output-schema.test.ts, run against the fake provider:StructuredOutputtool choice on the last step.StructuredOutput, the step-limit reminder and no max-steps text, and is accepted.auto) gets the same reminder on its last step and is accepted.prompt.tschange and pass with it.test/session/prompt-finish-guards.test.ts: the non-schema last step still sendsMAXIMUM STEPS REACHED.bun test test/session: 347 pass.RELEASING.md, plusrun-output-schemaandagent: 179 pass.bun run typecheck.RELEASING.md: CI, the native current-versus-previous comparison, publishing and the released smoke refresh. These run after review.Risks
automodel uses its last step on another tool and fails withstep_limit. That is the same result as today.step_limit, as before.refs #267
🤖 Generated with Claude Code