Make agent-stage retries safe and retain timeout failure evidence

Fix four stage failure-handling and observability defects (A2, A3, A6, A9).

- A2: add safeToReplay(def) as the single definition of retryable post-admission
  work - only an explicit tools block that selects neither edit nor shell. The
  agent/pi WorkStarted guards now break only when the stage is not safe to
  replay, so read-only stages genuinely retry a post-admission timeout while
  mutating and legacy-bundle stages still never replay admitted work.
- A6: re-emit a malformed final result on an explicit read-only stage up to the
  retry budget, preserving format_repairs evidence; mutating stages keep the
  immediate return and their session.
- A3: timed-out stages retain usable evidence. waitForSessionIdle records one
  bounded sanitized OperationEvidence on deadline; the agent deferred cleanup
  does a bounded best-effort latest-assistant readback and settles knowable V2
  usage; RetainSession adds a retained_session pointer to failed step
  transcripts so the session is visible beneath the BoundTranscript threshold.
- A9: a stage outcome routed to #failure now yields a non-empty run diagnostic
  ("stage <name> returned outcome <outcome>") while preferring an existing step
  diagnostic.

Adds focused stage and server tests for retry safety, malformed-result
re-emission, timeout evidence retention and run-level diagnostics; updates the
packaged skill references and docs/cli.md, docs/model-results.md.
This commit is contained in:
2026-10-15 18:27:22 +01:00
parent 0f5a188c6a
commit f57457aec9
13 changed files with 564 additions and 35 deletions
+22
View File
@@ -93,6 +93,7 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
defer cancel()
if err != nil {
c.captureTimedOutEvidence(ctx, err, config, watch)
if stopErr := c.Interrupt(cleanup, c.SessionID); stopErr != nil {
err = fmt.Errorf("agent interrupt failed; retained session %s needs inspection: %w", c.SessionID, errors.Join(err, stopErr, ErrStageCleanup))
return
@@ -185,6 +186,27 @@ func (c OpenCodeClient) settleV2Usage(ctx context.Context, budget *Budget, id st
_ = budget.readUsage(id, data, false)
}
// captureTimedOutEvidence retains one bounded, redacted best-effort readback of
// the latest assistant message after a post-admission deadline or transport
// failure, and settles knowable V2 usage for an unbudgeted stage. A failed
// readback is ignored and never changes the returned error.
func (c *AgentClient) captureTimedOutEvidence(ctx context.Context, err error, config ModelConfig, watch *budgetWatch) {
if !c.WorkStarted || c.SessionID == "" || !(errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrHarnessTransport)) {
return
}
readCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 3*time.Second)
data, readErr := c.call(readCtx, "get", "/api/session/"+c.SessionID+finalAssistantReadback, nil)
cancel()
if readErr == nil && len(data) > 0 {
c.Transcript = RedactEvidence(string(data))
}
// A budgeted stage already settles through watch.finish; only an unbudgeted
// V2 stage needs this bounded aggregate read.
if watch == nil && c.Backend == "v2" {
c.settleV2Usage(ctx, config.Budget, c.SessionID)
}
}
// finalAssistantReadback is the single bounded request used to select a V2
// final result: only the latest assistant message. Fetching the full assistant
// history also pulls large prior tool-read contents and can exceed the response