From 36324f2b6b0b742e4fc412417eb92b1cf450b849 Mon Sep 17 00:00:00 2001 From: Bulat Yapparov Date: Tue, 1 Dec 2026 14:09:45 +0000 Subject: [PATCH 1/3] fix(session): force StructuredOutput on the last step of schema runs 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 Claude-Session: https://claude.ai/code/session_01Br5cc1EsMnL19BcXrbDvUC --- CHANGELOG.md | 6 +++++ EVENTS.md | 3 +++ bun.lock | 2 +- packages/cli/package.json | 2 +- packages/cli/src/session/prompt.ts | 14 ++++++++--- .../cli/test/session/output-schema.test.ts | 25 +++++++++++++++++++ 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b4d043..258ec46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.6.0 (2026-12-01) + +### 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) + ## 0.5.6 (2026-11-30) ### Features diff --git a/EVENTS.md b/EVENTS.md index 78dc5ec..77dba22 100644 --- a/EVENTS.md +++ b/EVENTS.md @@ -739,6 +739,9 @@ Failure reasons are: - `missing`: prose finishes or rejected tool calls spent the budget, or the session ended normally without an accepted result. - `step_limit`: the agent reached its configured step cap without a result. + The last step before the cap asks only for `StructuredOutput`, with tool + choice forced to it where the model accepts forced tool choice, so a result + file already written can still be returned. - `aborted`: the invocation was cancelled, including SIGINT/SIGTERM. - `error`: a provider or session error, including stream idle timeout or an invalid accepted result received from a remote session. diff --git a/bun.lock b/bun.lock index 9aa6fc5..63921b3 100644 --- a/bun.lock +++ b/bun.lock @@ -22,7 +22,7 @@ }, "packages/cli": { "name": "@aictrl/cli", - "version": "0.5.6", + "version": "0.6.0", "bin": { "aictrl": "./bin/aictrl", }, diff --git a/packages/cli/package.json b/packages/cli/package.json index 18b014c..9eeeefd 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "$schema": "https://json.schemastore.org/package.json", - "version": "0.5.6", + "version": "0.6.0", "name": "@aictrl/cli", "description": "The headless agent harness for CI, cron jobs, and container workflows", "type": "module", diff --git a/packages/cli/src/session/prompt.ts b/packages/cli/src/session/prompt.ts index e993add..175f512 100644 --- a/packages/cli/src/session/prompt.ts +++ b/packages/cli/src/session/prompt.ts @@ -61,6 +61,11 @@ const STRUCTURED_OUTPUT_MISSING = const STRUCTURED_OUTPUT_REJECTED = `a tool call was rejected; ${STRUCTURED_OUTPUT_MISSING}` +// The last step of a schema run. The text-only max-steps prompt would forbid +// StructuredOutput, the only way to return a result already on disk (#267). +const STRUCTURED_OUTPUT_LAST_STEP = + "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." + const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. Write your final result as JSON to a file in the working directory, then call the StructuredOutput tool with that file's path. Do NOT respond with plain text.` export namespace SessionPrompt { @@ -732,6 +737,7 @@ export namespace SessionPrompt { // normal processing const isLastStep = step >= maxSteps + const notice = isLastStep && contract ? STRUCTURED_OUTPUT_LAST_STEP : reminder const processor = SessionProcessor.create({ assistantMessage: (await Session.updateMessage({ id: Identifier.ascending("message"), @@ -838,15 +844,15 @@ export namespace SessionPrompt { system, messages: [ ...MessageV2.toModelMessages(msgs, model), - ...(reminder + ...(notice ? [ { role: "user" as const, - content: `${reminder}`, + content: `${notice}`, }, ] : []), - ...(isLastStep + ...(isLastStep && !contract ? [ { role: ProviderTransform.isGeminiTarget(model) ? ("user" as const) : ("assistant" as const), @@ -857,7 +863,7 @@ export namespace SessionPrompt { ], tools, model, - toolChoice: contract ? structuredToolChoice(model, reminder) : undefined, + toolChoice: contract ? structuredToolChoice(model, notice) : undefined, structured: contract ? { reject, diff --git a/packages/cli/test/session/output-schema.test.ts b/packages/cli/test/session/output-schema.test.ts index 8ae8ab3..bee9dae 100644 --- a/packages/cli/test/session/output-schema.test.ts +++ b/packages/cli/test/session/output-schema.test.ts @@ -520,6 +520,31 @@ test("step cap without a valid result has an explicit failure", async () => { expect(result.info.error).toMatchObject({ name: "StructuredOutputError", data: { retries: 0 } }) expect(result.outcomes).toEqual([{ status: "failed", reason: "step_limit", attempts: 1 }]) expect(result.requests).toHaveLength(1) + expect(result.requests[0].tool_choice).toEqual({ type: "function", function: { name: "StructuredOutput" } }) +}, 15_000) + +// #267: the text-only max-steps prompt forbade StructuredOutput, so a run at +// its step cap lost a result it had already written. +test("the last step forces StructuredOutput and returns the result already written", async () => { + const last = await run(["prose", '{"result":"written"}'], 1, { steps: 2 }) + expect(last.requests).toHaveLength(2) + expect(last.requests[1].tool_choice).toEqual({ type: "function", function: { name: "StructuredOutput" } }) + const request = JSON.stringify(last.requests[1]) + expect(request).toContain("The step limit is reached") + expect(request).not.toContain("MAXIMUM STEPS REACHED") + expect(last.info.error).toBeUndefined() + expect(last.info.structured).toEqual({ result: "written" }) + expect(last.outcomes).toEqual([{ status: "accepted", attempts: 2, value: { result: "written" } }]) +}, 15_000) + +test("a step-cap model without forced tool choice is still asked for StructuredOutput on its last step", async () => { + const result = await run(['{"result":"written"}'], 0, { steps: 1, anthropic: "claude-sonnet-5-5" }) + expect(result.requests.map((request) => request.tool_choice)).toEqual([{ type: "auto" }]) + const request = JSON.stringify(result.requests[0]) + expect(request).toContain("The step limit is reached") + expect(request).not.toContain("MAXIMUM STEPS REACHED") + expect(result.info.structured).toEqual({ result: "written" }) + expect(result.outcomes).toEqual([{ status: "accepted", attempts: 1, value: { result: "written" } }]) }, 15_000) for (const [name, options, reason] of [ From 072b5828beed2a9bf1b5c768a5436bd2391cd4fc Mon Sep 17 00:00:00 2001 From: Bulat Yapparov Date: Tue, 1 Dec 2026 14:37:09 +0000 Subject: [PATCH 2/3] chore(release): version 0.5.7, not 0.6.0 refs #267 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Br5cc1EsMnL19BcXrbDvUC --- CHANGELOG.md | 4 ++-- bun.lock | 2 +- packages/cli/package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 258ec46..6fe6d2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Changelog -## 0.6.0 (2026-12-01) +## 0.5.7 (2026-12-01) -### Behaviour changes +### 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) diff --git a/bun.lock b/bun.lock index 63921b3..3b89cf7 100644 --- a/bun.lock +++ b/bun.lock @@ -22,7 +22,7 @@ }, "packages/cli": { "name": "@aictrl/cli", - "version": "0.6.0", + "version": "0.5.7", "bin": { "aictrl": "./bin/aictrl", }, diff --git a/packages/cli/package.json b/packages/cli/package.json index 9eeeefd..6b2581b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "$schema": "https://json.schemastore.org/package.json", - "version": "0.6.0", + "version": "0.5.7", "name": "@aictrl/cli", "description": "The headless agent harness for CI, cron jobs, and container workflows", "type": "module", From 65cbb8ccc3a5f7d9a8167aac41a0605a402cd836 Mon Sep 17 00:00:00 2001 From: Bulat Yapparov Date: Tue, 1 Dec 2026 14:39:01 +0000 Subject: [PATCH 3/3] fix(session): offer only protocol tools on a schema run's last step 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 Claude-Session: https://claude.ai/code/session_01Br5cc1EsMnL19BcXrbDvUC --- CHANGELOG.md | 2 +- packages/cli/src/session/prompt.ts | 7 ++++++- packages/cli/test/session/output-schema.test.ts | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fe6d2a..b762d9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### 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) +- **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. That last step offers only `StructuredOutput`, so a model that cannot be forced to call it cannot run a work tool instead. Runs without `--output-schema` keep the text-only last step. (#268, refs #267) ## 0.5.6 (2026-11-30) diff --git a/packages/cli/src/session/prompt.ts b/packages/cli/src/session/prompt.ts index 175f512..3cc49f3 100644 --- a/packages/cli/src/session/prompt.ts +++ b/packages/cli/src/session/prompt.ts @@ -861,7 +861,12 @@ export namespace SessionPrompt { ] : []), ], - tools, + // The last step of a schema run offers only the protocol tools, so a + // model on auto tool choice cannot run a work tool either (#267). + tools: + isLastStep && contract + ? Object.fromEntries(Object.entries(tools).filter(([, definition]) => ToolGuidance.isProtocol(definition))) + : tools, model, toolChoice: contract ? structuredToolChoice(model, notice) : undefined, structured: contract diff --git a/packages/cli/test/session/output-schema.test.ts b/packages/cli/test/session/output-schema.test.ts index bee9dae..d8a13f7 100644 --- a/packages/cli/test/session/output-schema.test.ts +++ b/packages/cli/test/session/output-schema.test.ts @@ -529,6 +529,10 @@ test("the last step forces StructuredOutput and returns the result already writt const last = await run(["prose", '{"result":"written"}'], 1, { steps: 2 }) expect(last.requests).toHaveLength(2) expect(last.requests[1].tool_choice).toEqual({ type: "function", function: { name: "StructuredOutput" } }) + expect((last.requests[0].tools as unknown[]).length).toBeGreaterThan(1) + expect((last.requests[1].tools as { function: { name: string } }[]).map((tool) => tool.function.name)).toEqual([ + "StructuredOutput", + ]) const request = JSON.stringify(last.requests[1]) expect(request).toContain("The step limit is reached") expect(request).not.toContain("MAXIMUM STEPS REACHED") @@ -540,6 +544,8 @@ test("the last step forces StructuredOutput and returns the result already writt test("a step-cap model without forced tool choice is still asked for StructuredOutput on its last step", async () => { const result = await run(['{"result":"written"}'], 0, { steps: 1, anthropic: "claude-sonnet-5-5" }) expect(result.requests.map((request) => request.tool_choice)).toEqual([{ type: "auto" }]) + // Auto tool choice cannot enforce the call, so the last step offers no work tool. + expect((result.requests[0].tools as { name: string }[]).map((tool) => tool.name)).toEqual(["StructuredOutput"]) const request = JSON.stringify(result.requests[0]) expect(request).toContain("The step limit is reached") expect(request).not.toContain("MAXIMUM STEPS REACHED")