Repository navigation
fix(session): run-mode nudge without a clarification stop, a blocked exit, and the completion contract for run-mode agents #1641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ff90f06
2db5740
537087f
77a452d
720eeaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ import { MessageID } from "../../session/schema" | |
| // altimate_change end | ||
| // altimate_change end | ||
| // altimate_change start — run implies run mode (fork-only module) | ||
| import { applyRunModeDefault } from "./run/run-mode" | ||
| import { applyRunModeDefault, isLocalRunMode, RUN_BLOCKED_MESSAGE } from "./run/run-mode" | ||
| // altimate_change end | ||
| // altimate_change start — a failed provider list is reported as itself, not "Model not found" (fork-only module) | ||
| import { providerListOrThrow } from "./run/provider-list" | ||
|
|
@@ -651,8 +651,12 @@ You are speaking to a non-technical business executive. Follow these rules stric | |
| - "Query Duration" not "total_elapsed_time" — "Data Processed" not "bytes_scanned" — "Monthly Cost" not "credits_used * 3.00"` | ||
|
|
||
| async function execute(sdk: OpencodeClient) { | ||
| // altimate_change start — collect completed assistant text for run output | ||
| const outputParts = new OutputCollector() | ||
| // altimate_change start — collect completed assistant text for run output; in run mode the | ||
| // completion token is stripped at every output boundary. Local run mode only: with | ||
| // ALTIMATE_RUN_MODE=0 or --attach the text is shown as written and BLOCKED: is not an exit. | ||
| const localRunMode = isLocalRunMode({ runMode: Flag.ALTIMATE_RUN_MODE, attach: Boolean(args.attach) }) | ||
| const stripDone = localRunMode | ||
| const outputParts = new OutputCollector({ stripCompletionToken: stripDone }) | ||
| // altimate_change end | ||
| // altimate_change start — a turn must end with text (#1334). Track whether | ||
| // the assistant said anything at all, and the last tool failure, so a | ||
|
|
@@ -749,7 +753,8 @@ You are speaking to a non-technical business executive. Follow these rules stric | |
| // altimate_change end | ||
| // altimate_change start — turn accounting + dual-attribution | ||
| // termination state for this run (see run-accounting.ts). | ||
| const accounting = RunAccounting.create() | ||
| // Blocked endings are classified only in local run mode. | ||
| const accounting = RunAccounting.create({ detectBlocked: localRunMode }) | ||
| const requestOwnership = RunAccounting.createRequestOwnership() | ||
| // A queued request must not revoke the in-flight echo owned by an earlier request. | ||
| const committedRequests = new Set<string>() | ||
|
|
@@ -1224,15 +1229,18 @@ You are speaking to a non-technical business executive. Follow these rules stric | |
| } | ||
| // altimate_change end | ||
| // altimate_change start — upstream_fix: --output collects text in JSON mode too | ||
| const text = part.text.trim() | ||
| if (args.output && text) { | ||
| outputParts.text(text, { | ||
| if (args.output && part.text.trim()) { | ||
| outputParts.text(part.text, { | ||
| partID: part.id, | ||
| synthetic: part.synthetic === true, | ||
| compaction: accounting.isCompactionStep(part.messageID), | ||
| }) | ||
| } | ||
| if (emit("text", { part })) continue | ||
| // Run mode: stdout and JSON text events carry the answer without the | ||
| // completion token; done_reason still records it. | ||
| const shown = stripDone ? SessionTermination.stripCompletionToken(part.text) : part.text | ||
| if (emit("text", { part: shown === part.text ? part : { ...part, text: shown } })) continue | ||
|
Comment on lines
+1241
to
+1242
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const text = shown.trim() | ||
| if (!text) continue | ||
| // altimate_change end | ||
| if (!process.stdout.isTTY) { | ||
|
|
@@ -2006,7 +2014,10 @@ You are speaking to a non-technical business executive. Follow these rules stric | |
| // branch with a normal text-only stop. That has already returned from | ||
| // SessionPrompt.loop, so enqueue one explicit continuation turn instead | ||
| // of silently finalizing the run at rc 0 with done_reason=none. | ||
| if (!accounting.fatal && !challengeConfirmed) { | ||
| // altimate_change start — a blocked reply to the challenge is a final report, not a decline | ||
| const challengeBlocked = accounting.termination().done_reason === "blocked" | ||
| if (!accounting.fatal && !challengeConfirmed && !challengeBlocked) { | ||
| // altimate_change end | ||
| NudgeArbiter.register(sessionID, { | ||
| source: "termination_challenge", | ||
| kind: "continue_after_decline", | ||
|
|
@@ -2202,7 +2213,12 @@ You are speaking to a non-technical business executive. Follow these rules stric | |
| Tracer.setActive(null) | ||
| // altimate_change start — persist only overflow errors that never reached a recovery event | ||
| // altimate_change start — warnings become a fatal trace outcome only if no final answer recovered the budget | ||
| const traceError = [error, ...recoverableOverflowTraceErrors.values(), ...(accounting.fatal ? budgetWarnings : [])] | ||
| const traceError = [ | ||
| error, | ||
| ...recoverableOverflowTraceErrors.values(), | ||
| ...(accounting.fatal ? budgetWarnings : []), | ||
| ...(!accounting.fatal && termination.done_reason === "blocked" ? [RUN_BLOCKED_MESSAGE] : []), | ||
| ] | ||
| .filter(Boolean).join(EOL) || undefined | ||
| // altimate_change end | ||
| traceAbort.abort() | ||
|
|
@@ -2232,6 +2248,15 @@ You are speaking to a non-technical business executive. Follow these rules stric | |
| // (not process.exit) so pending stdout/trace writes still flush. | ||
| if (accounting.fatal) process.exitCode = 1 | ||
| // altimate_change end | ||
| // altimate_change start — a blocked run did not do its task: exit nonzero and say why | ||
| // (done_reason=blocked is also in the termination record/JSON event). | ||
| if (!accounting.fatal && termination.done_reason === "blocked") { | ||
|
RaghwendraSingh marked this conversation as resolved.
Resolved
|
||
| process.exitCode = 1 | ||
|
RaghwendraSingh marked this conversation as resolved.
Resolved
|
||
| if (args.format !== "json") { | ||
| process.stderr.write(`\n! ${RUN_BLOCKED_MESSAGE}\n`) | ||
| } | ||
| } | ||
| // altimate_change end | ||
| } | ||
|
|
||
| if (args.attach) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ async function interruptedToolPart(part: MessageV2.ToolPart, capTokens: number): | |
| const output = await interruptedTaskOutput( | ||
| state.metadata.sessionId as SessionID, | ||
| state.metadata.messageID as MessageID, | ||
| { runMode: Flag.ALTIMATE_RUN_MODE }, | ||
| ) | ||
| if (output) return { | ||
| ...part, | ||
|
|
@@ -129,7 +130,8 @@ export namespace SessionProcessor { | |
|
|
||
| export function resolveFinishOutcome(state: { | ||
| needsCompaction: boolean | ||
| /** errorless turn that finished with "stop" AND asserted completion; never the summarizer. */ | ||
| /** errorless turn that finished with "stop" AND asserted completion (or, in run mode, ended on a | ||
| * `BLOCKED:` report); never the summarizer. */ | ||
| explicitDone: boolean | ||
| blocked: boolean | ||
| error: boolean | ||
|
|
@@ -1466,18 +1468,22 @@ export namespace SessionProcessor { | |
| // mode — prompt.ts's pre-dispatch overflow check compacts before the | ||
| // next request. Never bare finishReason "stop" (that ends nearly every | ||
| // ordinary text turn), and never for the compaction summarizer itself. | ||
| const finalStopInput = { | ||
| finish: input.assistantMessage.finish, | ||
| hasError: input.assistantMessage.error !== undefined, | ||
| parts: p, | ||
| } | ||
| const explicitDone = | ||
| !input.assistantMessage.summary && | ||
| SessionTermination.explicitDoneStop({ | ||
| finish: input.assistantMessage.finish, | ||
| hasError: input.assistantMessage.error !== undefined, | ||
| parts: p, | ||
| }) | ||
| if (needsCompaction && explicitDone) { | ||
| log.info("explicit DONE with pending compaction — terminating instead of compacting", { | ||
| sessionID: input.sessionID, | ||
| messageID: input.assistantMessage.id, | ||
| }) | ||
| !input.assistantMessage.summary && SessionTermination.explicitDoneStop(finalStopInput) | ||
| // Run mode: a final BLOCKED: line is a report, terminal like DONE, so a pending | ||
| // compaction does not prompt the agent again. | ||
| const blockedEnding = | ||
| runMode && !input.assistantMessage.summary && SessionTermination.blockedStop(finalStopInput) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: the run-mode I replaced Not blocking: test-only. A processor-level case next to the explicit- |
||
| if (needsCompaction && (explicitDone || blockedEnding)) { | ||
| log.info( | ||
| `${explicitDone ? "explicit DONE" : "BLOCKED ending"} with pending compaction — terminating instead of compacting`, | ||
| { sessionID: input.sessionID, messageID: input.assistantMessage.id }, | ||
| ) | ||
| } | ||
| // altimate_change end | ||
| // altimate_change start — the ordering itself lives in the exported | ||
|
|
@@ -1486,7 +1492,7 @@ export namespace SessionProcessor { | |
| if (recoveredCommittedAnswer) return "stop" as const | ||
| return resolveFinishOutcome({ | ||
| needsCompaction, | ||
| explicitDone, | ||
| explicitDone: explicitDone || blockedEnding, | ||
| blocked, | ||
| error: input.assistantMessage.error !== undefined, | ||
| starvationStop, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: stripping works per text part, so a code fence split across parts is mishandled.
Take one assistant message with the text parts
"Example:\n```text","DONE"and"```\nAnswer.\nDONE".stripCompletionTokendeletes the exampleDONEinside the fence. The closing fence in the third part is then read as an opening fence, so the real trailingDONEis left in place. The same happens on stdout, in JSON events and inOutputCollector. I reproduced this at this head.Not blocking: a fence must open in one text part and close in a later one, which is an edge input. The detectors on main have the same per-part limit, and you already listed it as a follow-up. Raised by Codex; we agreed on minor.