# Write Diagnosis Finalization

<!-- auto-oncall-optimization-record
{
  "schemaVersion": "auto-oncall-optimization-record-v1",
  "optimizationId": "write-diagnosis-finalization-2026-07-27",
  "problems": [
    {
      "problemId": "write-diagnosis-evidence-complete-but-final-json-times-out",
      "summary": "A write-path diagnosis can complete the required provider chain and pass workload identity and transaction shape gates, then spend the remaining initial-turn budget on finalization without returning JSON.",
      "status": "candidate",
      "issueKeys": ["APID-29194"],
      "sourceRefs": [
        "https://tidb.atlassian.net/browse/APID-29194#problem:diagnostic_regression:evidence-complete-finalization-timeout"
      ],
      "signalTextIncludesAny": [
        "evidence complete but final json timed out",
        "diagnostic finalization continues after workload gates pass",
        "long replay context prevents final response",
        "tool chain succeeds but diagnostic initial aborts"
      ],
      "observedThrough": "2026-07-27",
      "effectiveAt": null,
      "verifiedAt": null,
      "attemptedAction": "When required workload identity and transaction shape gates pass and every selected scenario has returned a finding or bounded failure, stop initial-turn tools immediately. Finalize directly when no load-bearing current-read-only gap can change the outcome; otherwise return that gap for the backend evidence turn. Keep safety handoff out of diagnosticFollowup and bound the result to five replay-context sections, six evidence bullets, and a 1200-character customer draft.",
      "evidenceUrl": null
    }
  ]
}
-->

- Optimization ID: `write-diagnosis-finalization-2026-07-27`
- Owner: auto-oncall-services
- Source reference: `APID-29194`
- Implementation status: local candidate; not deployed
- Effective status: `candidate`

## Bounded problem

The diagnostic query sequence can finish successfully while the same Codex
turn continues reasoning, enumerating routes, or composing a long internal
analysis until the initial deadline aborts. This problem begins only after the
required workload identity and transaction shape evidence is already present.
It does not own provider query selection, model choice, or timeout values.

## Regression evidence

Historical-cutoff replay `63a5df5e-b424-4bc5-bae0-b78e3199d161` started at
`2026-07-27T06:02:44.713Z`. Its read-only chain reached the required evidence
in about three minutes:

- one readiness query;
- one Commit/Prewrite scheduler mechanism query;
- one selected-instance TopSQL query with `window: "10s"`;
- one business-table Slow Query ordered by `write_keys`.

The Slow Query returned COMMIT rows whose `prev_stmt` identified
`UPDATE dh_active_receive_logs ... WHERE id IN (...)`. Each transaction had
68,000 write keys, about 6.21 MB of writes, and about 0.32–0.58 seconds total
query time. Retry, lock, and backoff fields were zero. This was sufficient for
the required workload identity and transaction shape gates.

The initial call did not return a result. It aborted at
`2026-07-27T06:16:25.106Z` after 820,394 ms with zero response items and no
final JSON. The query chain is successful evidence for the preceding provider
and candidate-priority changes, but the run is a failure for this finalization
problem.

## Candidate change

- Treat both required workload gates passing, after every selected scenario has
  returned a finding or bounded failure, as an immediate stop for initial-turn
  tool use and exploration.
- Finalize directly when no load-bearing current-read-only gap can change the
  leading cause, severity, safety decision, or recommendation.
- If one eligible high-information gap remains, return it without querying it;
  only the backend gap turn may execute it.
- Treat recurrence alone as non-load-bearing when the first window already
  establishes the same cause, transaction shape, safety decision, and
  recommendation.
- Keep safety handoff, human operations, customer clarification, and
  unavailable-provider gaps out of `diagnosticFollowup`; preserve them in the
  safety decision and Handoff.
- Return schema-valid JSON in the next response.
- Limit `internalHandoffMarkdown` to Root Cause, Key Evidence, Alternatives or
  Missing, Recommendations, and Handoff, at most 6,000 characters.
- Limit Key Evidence to six bullets and `customerReplyMarkdown` to 1,200
  characters.
- Preserve the established safety decision and exact unresolved boundary.

## Branch-local acceptance evidence

Read-only historical replay `f79d1126-2a24-46ea-b1d8-f0a0a949a735`
returned HTTP 200 in 353.271 seconds. The diagnostic initial stage completed
in 302,715 ms and normalization completed in 14,981 ms; neither a diagnostic
gap turn nor synthesis ran.

After readiness, scheduler command P99, latch P99, single-instance TopSQL Top
10, and the table-text Slow Query established the workload and transaction
shape, the run finalized without more provider exploration. Its structured
result had `diagnosticFollowup.required=false`, a 1,308-character replay
context with the five allowed sections and six evidence bullets, and a
465-character comment draft. It retained the `human_required` safety boundary
while identifying the large-batch `UPDATE dh_active_receive_logs` transaction
as the leading cause. The final evaluation rated the result `ai_better`.

This is passing branch-local evidence for the finalization stop condition and
bounded output. Because the candidate is not deployed or effective, the
machine-readable problem status, `effectiveAt`, and `verifiedAt` remain
`candidate`, null, and null.

## Unresolved gaps

- It is not effective in staging or production.
- The limits are behavioral contracts, not backend truncation or schema
  `maxLength` enforcement.
- No Jira internal comment has been refreshed.

## Next validation action

After staging publication, repeat the replay through the rendered pod and
confirm the same stop and output bounds in the live trace. Separately validate
a case with a genuinely outcome-changing current-read-only gap and confirm it
is returned for the backend turn rather than queried in the initial turn.
