Skip to content

fix(session): return a schema run's result at the step limit (0.5.7) - #268

Merged
byapparov merged 3 commits into
mainfrom
fix/267-step-limit-structured-output
Dec 1, 2026
Merged

byapparov merged 3 commits into
mainfrom
fix/267-step-limit-structured-output

Conversation

@byapparov

@byapparov byapparov commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

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 sends prompt/max-steps.txt, which forbids every tool call, and StructuredOutput is the only way to return the result. The run fails with step_limit no matter what is on disk.

In production on 1 Dec, an aictrl PR Explanation on GLM-5.4 (execution 50fca695) wrote work/explanation/result.json at 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 of max-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."
    • This replaces any pending corrective reminder.
    • The step forces tool choice to StructuredOutput through the existing structuredToolChoice. Models that reject forced tool choice, such as Claude Sonnet 5.5 and Opus 5.5, get auto and the same reminder.
    • Validation and the corrective budget are unchanged. A rejected or missing result on that step still ends with failed / step_limit and exit 3.
    • Without --output-schema, the last step is unchanged: text only, using max-steps.txt.
  • EVENTS.md: the step_limit reason says the last step asks only for StructuredOutput.
  • Release 0.5.7:
    • packages/cli/package.json and the bun.lock workspace entry go to 0.5.7;
    • CHANGELOG.md gets a 0.5.7 (2026-12-01) entry under Fixes.

Expected Impact

  • A schema run that hits its step cap now returns the result file it wrote, as long as the file validates. Before, the run failed.
  • Schema runs that finish before the cap, and all runs without --output-schema, behave as before.
  • For aictrl, a follow-up app PR moves the executor pin from 0.5.6 to 0.5.7, so PR Explanations near the 120-step cap stop failing.

Test Plan

  • test/session/output-schema.test.ts, run against the fake provider:
    • Changed: the existing step-cap failure test now also asserts a forced StructuredOutput tool choice on the last step.
    • New: a run that spends step 1 without a result gets a step-2 request with forced StructuredOutput, the step-limit reminder and no max-steps text, and is accepted.
    • New: Sonnet 5.5 (auto) gets the same reminder on its last step and is accepted.
    • All three tests fail without the prompt.ts change and pass with it.
  • test/session/prompt-finish-guards.test.ts: the non-schema last step still sends MAXIMUM STEPS REACHED.
  • bun test test/session: 347 pass.
  • The release regressions listed in RELEASING.md, plus run-output-schema and agent: 179 pass.
  • bun run typecheck.
  • Release gates in RELEASING.md: CI, the native current-versus-previous comparison, publishing and the released smoke refresh. These run after review.

Risks

  • A model that ignores the reminder on an auto model uses its last step on another tool and fails with step_limit. That is the same result as today.
  • On the last step, the model can only point at a file it has already written. If it never wrote one, the run fails with step_limit, as before.

refs #267

🤖 Generated with Claude Code

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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread CHANGELOG.md Outdated

### 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ CHANGELOG cites issue #267, not PR #268 + closes.

Suggested change
- **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)

@aictrl-dev

aictrl-dev Bot commented Dec 1, 2026

Copy link
Copy Markdown

Code review

Verdict: Looks good — only minor / nit comments below. · 🔴 0 · 🟠 0 · 🟡 1 · ⚪ 1 · 0/2 resolved

🤖 Fix all 2 open findings with your agent
Fix the following code review findings on aictrl-dev/cli PR #268 (head branch).
Run the relevant tests/linters after each change.

1. CHANGELOG.md:7 — 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.
2. packages/cli/src/session/prompt.ts:866 — 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.
📋 Out-of-diff findings (2)
Sev Location Finding
⚪ CHANGELOG.md:7 CHANGELOG cites issue #267, not PR #268 + closes
🟡 packages/cli/src/session/prompt.ts:866 Last-step "no other tool will run" unenforced on auto

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
@byapparov byapparov self-assigned this Dec 1, 2026
@byapparov byapparov added the bug Something isn't working label Dec 1, 2026
@byapparov byapparov changed the title fix(session): return a schema run's result at the step limit (0.6.0) fix(session): return a schema run's result at the step limit (0.5.7) Dec 1, 2026
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
@byapparov

Copy link
Copy Markdown
Member Author

Review response — PR #268

I checked both findings from the automatic review against the PR head. Both are real; 65cbb8ccc3 fixes them.

Issues addressed (pushed to this PR)

  • Last-step notice unenforced on auto tool choice — packages/cli/src/session/prompt.ts:

    • A schema run's last step now sends only the protocol tools: StructuredOutput and the internal invalid repair tool. On models without forced tool choice (Claude Sonnet/Opus 5.5, Fable/Mythos 5.1), a work tool can no longer run on that step.
    • Tests assert that the last-step request carries only StructuredOutput, on both the forced and the auto paths. Both fail without the change.
  • CHANGELOG cites the issue, not the PR — CHANGELOG.md: the entry now ends (#268, refs #267), following the file's PR-first convention. It uses refs rather than closes, because the issue is closed by hand after release.

    Since this review, the entry has also moved to 0.5.7 under Fixes: the release is a patch, not 0.6.0.

Checks: bun test test/session 347 pass, bun run typecheck clean.

Review claims verified false (no change needed)

None.

Not addressed here

None.

@byapparov
byapparov merged commit 6b07791 into main Dec 1, 2026
5 checks passed
@byapparov
byapparov deleted the fix/267-step-limit-structured-output branch December 1, 2026 14:48
@byapparov
byapparov restored the fix/267-step-limit-structured-output branch December 1, 2026 14:48
Comment thread CHANGELOG.md Outdated

### 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ CHANGELOG: last-step failure is not always `step_limit`.

Suggested change
- **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)

@aictrl-dev

aictrl-dev Bot commented Dec 1, 2026

Copy link
Copy Markdown

Code review

Verdict: Looks good — only minor / nit comments below. · 🔴 0 · 🟠 0 · 🟡 0 · ⚪ 1 · 0/1 resolved

  • ⚪ CHANGELOG.md:7 — CHANGELOG: last-step failure is not always `step_limit`
🤖 Fix all 1 open findings with your agent
Fix the following code review findings on aictrl-dev/cli PR #268 (head branch).
Run the relevant tests/linters after each change.

1. CHANGELOG.md:7 — 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."
📋 Out-of-diff findings (1)
Sev Location Finding
⚪ CHANGELOG.md:7 CHANGELOG: last-step failure is not always `step_limit`

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant