Recover malformed model-stage results with a bounded, work-preserving repair

A model stage whose final result was malformed failed the whole run even when
the work and evidence had survived, because the only recovery was an isolated
formatter call on a fixed 30-second deadline that could time out on its own
(run 55, ui_reviewer of pipeline 9 v6).

Add an ordered, deterministic local repair pass (internal/stage/result_repair.go)
that runs before any model call: (a) append missing closing delimiters via the
existing closeJSONContainers; (b) remove one unambiguous trailing comma before a
closing } or ]; (c) insert exactly the single comma/colon the JSON parser reported
at its byte offset. Every candidate must pass losslessSyntaxRepair(original,
candidate) and decodeModelResult before it is accepted, so no field value is
invented, removed, reordered or rewritten. Ambiguous payloads (an unescaped quote
or colon inside a string value, or a stray delimiter with no single completion)
return not-ok and fall through to the bounded model re-emit rather than guessing.
stageResultText still strips surrounding prose and a single code fence first;
model_prompt.go now also strips prose around a single balanced-but-malformed
object span so the deterministic pass sees just the object.

Replace the fixed 30-second formatter deadline with repairDeadline(payloadBytes,
remaining), which grows monotonically with payload size, has a floor, and is
clamped to the attempt's remaining budget minus a safety margin, so the stage
context remains the outer bound and a large result is not guaranteed to time out.
The one-request-per-attempt, 32 KiB bound and the deny-all, retained, new-session
property of RepairFormat are unchanged for OpenCodeClient and PiClient.

Record an explicit recovery status on every terminal path: repaired, partial or
unrecoverable, plus a byte offset. A candidate that is valid JSON but still fails
envelope validation is marked partial and never returned as a completed result;
the unrecoverable path retains the bounded original payload, the precise parse
error (including its byte offset) and surfaces the status in the FINAL RESULT
VALIDATION diagnostic.

safeToReplay is intentionally unchanged. The deterministic local repair and the
fresh deny-all, no-tools, retained formatter session are independent of the
stage's tool set and never re-prompt or replay the admitted agent session, so a
shell-enabled review stage gains malformed-result recovery without any replay of
admitted work. Risk: no change to mutation safety; the only added cost is one
bounded formatter call, already budgeted. Replayability is not extended for shell
stages.

Docs and packaged skill references (docs/model-results.md,
internal/author/skill/references/{format,debugging,verification}.md) describe the
three-tier recovery, the status words and the derived deadline; the safeToReplay
decision and its risk are recorded there alongside the commit.

Tests add mocked, offline coverage for the deterministic and partial paths, a
repairDeadline monotonicity/floor/clamp table, and a shell-stage regression
proving a recoverable malformed result does not fail the run or replay tool work.
This commit is contained in:
2026-10-16 13:02:53 +01:00
parent 7b3a56398d
commit 9f88e4ff71
11 changed files with 514 additions and 61 deletions
+13 -6
View File
@@ -31,12 +31,19 @@ Each playbook is *symptom → how to confirm → what to do*.
- **Confirm** Read the diagnostic and the retained final result: the stage's result
does not match its `output` contract. A retained transcript can be a valid bounded
preview envelope, not the complete history.
- **Do** A malformed result on an explicit read-only stage may be re-emitted up to
`retries` with its `format_repairs` evidence retained; a stage with `edit`/`shell`
or no tools block is never replayed. Never accept prose, flattened output or a
tool-call envelope as success. If one stage keeps producing invalid JSON, tighten
its `output`/`prompt` in the **pipeline definition** (`references/format.md`) — a
pipeline fix, not a project edit.
- **Do** Recovery is bounded and work-preserving. A deterministic local pass first
appends missing closers, drops one trailing comma, or inserts the single
comma/colon the parser expected; only if that is ambiguous does one deny-all
formatter request run, with a deadline derived from the payload size and the
attempt's remaining budget. Each path records a `repaired`, `partial` or
`unrecoverable` status in `format_repairs`; a partial or unrecoverable payload
is never accepted as complete. On an explicit read-only stage a malformed result
may be re-emitted up to `retries`; a stage with `edit`/`shell` or no tools block
is never replayed, though its malformed result can still be repaired by the
deterministic pass or the fresh deny-all formatter session. Never accept prose,
flattened output or a tool-call envelope as success. If one stage keeps
producing invalid JSON, tighten its `output`/`prompt` in the **pipeline
definition** (`references/format.md`) — a pipeline fix, not a project edit.
## A reviewer or visual reviewer that blocks, rejects repeatedly, or hangs
+17 -9
View File
@@ -105,15 +105,23 @@ Use this block for intentional file/command restrictions; prose is not a runtime
permission policy. Keep ordinary workflows language-neutral and unrestricted
within their run directory unless the user asks for a narrower scope.
Malformed final JSON has bounded, syntax-only recovery even with `retries=0`;
it never changes verdict/evidence. On an explicit read-only stage a rejected
malformed result may be re-emitted up to `retries` with its `format_repairs`
evidence retained; a stage with `edit`/`shell` or no tools block never replays
stage tools or re-emits the result. Compatible native stable
backends request schema output with schema retries disabled; a retained deny-all,
no-inference compatibility probe identifies known format incompatibility. Incompatible
native backends and V2 require strict local envelope validation. Do not promise
recovery of truncated strings, missing evidence or invalid outcomes.
Malformed final JSON has bounded recovery even with `retries=0`; it never changes
verdict/evidence. Recovery tries a deterministic local pass first (append missing
closing delimiters, drop one trailing comma, or insert the single comma/colon the
parser expected), then at most one deny-all formatter request whose deadline is
derived from the payload size and the attempt's remaining budget rather than a
fixed short constant. Every path records `repaired`, `partial` or `unrecoverable`
in `format_repairs`; a partial or unrecoverable result is never returned as
complete, and an ambiguous payload (an unescaped quote/colon inside a string
value) is never guessed at. On an explicit read-only stage a rejected malformed
result may be re-emitted up to `retries`; a stage with `edit`/`shell` or no tools
block never replays stage tools or re-emits the result, though the deterministic
pass and the fresh deny-all, tool-free formatter session still apply. Compatible
native stable backends request schema output with schema retries disabled; a
retained deny-all, no-inference compatibility probe identifies known format
incompatibility. Incompatible native backends and V2 require strict local envelope
validation. Do not promise recovery of truncated strings, missing evidence or
invalid outcomes.
Return the strict `{ "outcome": "declared-edge-key", "output": { ... } }`
envelope. Do not treat tool-call/DSML markup, plain prose or flattened output as
success, or rerun tool work to fix a response's formatting.
@@ -94,6 +94,24 @@ field is sent. A review-only fresh execution is a future recovery idea, **not
implemented**. Onboarding UI visual approval remains unresolved and must not be
reported complete.
## Bounded malformed-result recovery: 2026-10-16
Source-level, mocked `internal/stage` regression coverage (not live-provider
evidence and not a deployment): malformed final-result recovery now tries a
deterministic local pass first (append missing closers, drop one trailing comma,
or insert the single comma/colon the parser expected), then at most one deny-all
formatter request whose deadline is derived from the payload size and the
attempt's remaining budget rather than a fixed 30-second constant. Every path
records `repaired`, `partial` or `unrecoverable` in `format_repairs`; a `partial`
or `unrecoverable` payload is never returned as a completed result, and an
ambiguous payload (an unescaped quote/colon inside a string value) is never
guessed at. A shell-enabled stage's malformed but deterministically recoverable
result now completes without replaying admitted tool work; `safeToReplay` is
unchanged. The run-55 `ui_reviewer` payload was retrieved read-only with
`pipelines inspect 55 --json` and reproduced its exact byte offset 7837; it is the
ambiguous error class, so it is reported unrecoverable rather than repaired. This
does not update or restart any running server.
## Failure-handling source changes: 2026-10-15
Source-level, mocked `internal/stage` and `internal/server` regression coverage