# Agent9 Managed Tool Query Repair Parity

<!-- auto-oncall-optimization-record
{
  "schemaVersion": "auto-oncall-optimization-record-v1",
  "optimizationId": "agent9-managed-tool-query-repair-2026-08-25",
  "problems": [
    {
      "problemId": "agent9-managed-tool-rejection-bypasses-same-turn-repair",
      "summary": "Agent9 Managed Clinic reduced a rejected request to an error code and materialized every failed attempt independently, so a locally invalid TopSQL instance was not corrected inside the Evidence Turn and could leave acquisition partial.",
      "status": "optimized_verified",
      "applicability": "Agent9-backed Clinic evidence acquisition in which a local argument validation or HTTP 4xx rejects one caller-authored bounded request.",
      "issueKeys": ["TCID-13071"],
      "sourceRefs": ["https://tidb.atlassian.net/browse/TCID-13071#problem:agent9-managed-tool-rejection-bypasses-same-turn-repair"],
      "signalTextIncludesAny": [
        "topsql_instance_invalid",
        "managed tool query repair",
        "Clinic HTTP 4xx",
        "TCID-13071"
      ],
      "observedThrough": "2026-08-25",
      "effectiveAt": "2026-08-25T12:24:00+08:00",
      "verifiedAt": "2026-08-25T12:35:47+08:00",
      "attemptedAction": "Return a stable queryId and safe field-level or HTTP 4xx correction details from Managed Clinic, require up to four changed corrections in the same Evidence Turn, and use only the final outcome for that queryId in both evidence materialization and final execution status while retaining the actual terminal error code.",
      "evidenceUrl": "https://internal.us-west-2.agent.mem9.ai/api/sessions/sess_f5d2f36c6c8c4ffdb164fb437fdf7407"
    },
    {
      "problemId": "agent9-managed-slow-query-prefix-parity",
      "summary": "Agent9 Managed Clinic advertised compact Slow Query Digest-prefix lookup but rejected the six-character Digest references emitted by its own compact results, unlike the Kubernetes Provider Executor.",
      "status": "optimized_verified",
      "applicability": "Agent9-backed Clinic Slow Query refinement using a safe 6-to-63-character SQL Digest prefix from a prior compact result.",
      "issueKeys": ["TCID-13071"],
      "sourceRefs": ["https://tidb.atlassian.net/browse/TCID-13071#problem:agent9-managed-slow-query-prefix-parity"],
      "signalTextIncludesAny": [
        "slow_query_digest_invalid",
        "compact Digest prefix",
        "TCID-13071"
      ],
      "observedThrough": "2026-08-25",
      "effectiveAt": "2026-08-25T12:24:00+08:00",
      "verifiedAt": "2026-08-25T12:35:47+08:00",
      "attemptedAction": "Accept the same 6-to-64-character Slow Query Digest selector as the Kubernetes Provider Executor; use bounded prefix filtering for dedicated Data Proxy SQL and bounded local filtering of the NGM candidate response without exposing a full Digest.",
      "evidenceUrl": "https://internal.us-west-2.agent.mem9.ai/api/sessions/sess_2891edd7ffaa4cca8434433fb6c5c666"
    },
    {
      "problemId": "agent9-managed-tool-runtime-error-obscured",
      "summary": "Agent9 retained a Managed Tool runtime_error and safe message, but the AOS adapter and final execution summary reduced that ordinary program failure to provider_tool_call_failed; harmless overlong semantic query IDs were also rejected before useful Provider work.",
      "status": "optimized_verified",
      "applicability": "Agent9-backed Managed Provider execution with a Tool runtime failure, or a semantic query identifier that needs bounded canonicalization rather than Provider-request rejection.",
      "issueKeys": ["TCID-13070", "TCID-13071"],
      "sourceRefs": [
        "https://tidb.atlassian.net/browse/TCID-13070#problem:agent9-managed-tool-runtime-error-obscured",
        "https://tidb.atlassian.net/browse/TCID-13071#problem:agent9-managed-tool-runtime-error-obscured"
      ],
      "signalTextIncludesAny": [
        "provider_tool_call_failed",
        "runtime_error",
        "query_id_invalid",
        "TCID-13070",
        "TCID-13071"
      ],
      "observedThrough": "2026-08-25",
      "effectiveAt": "2026-08-25T12:24:00+08:00",
      "verifiedAt": "2026-08-25T12:35:47+08:00",
      "attemptedAction": "Project Agent9 Managed Tool execution failures into the same provider-result-v1 shape with the actual safe runtime code and message, make the final execution summary preserve that code, and canonicalize harmless queryId length or separator differences inside the ordinary Tool program.",
      "evidenceUrl": "https://internal.us-west-2.agent.mem9.ai/api/sessions/sess_fff6384e376848419952d5fdcf98ce0d"
    }
  ]
}
-->

- Owner: Managed Clinic Tool, Evidence Agent prompt, and deterministic Agent9 Provider materializer
- Source: TCID-13071 replay `7be96988-68d9-43d0-b3bf-e75b1d93f944`; v12 verification replays TCID-13070 `93e36c66-f453-4360-8378-6a9626b2a20b` and TCID-13071 `03c9f361-8ca9-44cd-af99-d848c849cf2d`; v13 verification replays TCID-13070 `ef8432e2-922d-415c-8439-f0b7612befe0` and TCID-13071 `9f4efb52-7cd8-4077-a752-62f91959781b`
- Implementation status: deployed in AOS `7f30f3b13c87b2b3e0691ae51be80ba389216ae3` with Agent9 Managed Clinic v13
- Effective status: `optimized_verified`

## Bounded problem

Managed Clinic returned only `topsql_instance_invalid` for a local TopSQL
argument rejection. The Evidence Agent did not receive the invalid field and
accepted shape, and the final execution summary treated the failed call as a
permanent independent objective even when a later call with the same queryId
succeeded. The Managed Tool also rejected six-character Slow Query Digest
references that the Kubernetes Provider Executor and the published Agent9 Tool
schema both support. The v12 verification also showed that an overlong semantic
query identifier was rejected even though it is only a correlation key, and
that a real Agent9 Tool `runtime_error: fetch failed` was persisted but reduced
to `provider_tool_call_failed` by the AOS adapter.

## Intended change

Managed Clinic behaves like the ordinary Kubernetes Provider program: it
returns a stable semantic `queryId`, operation, safe error classification,
field and accepted shape for local validation, and bounded upstream status and
detail for every HTTP 4xx. The same Evidence Agent may make at most four changed
corrections in its current Turn. Calls sharing the exact `queryId` are one
objective, and only their final outcome determines evidence completion and the
final execution status. An unrepaired request retains its actual terminal error
code instead of becoming `provider_tool_call_failed`.

Managed Slow Query now accepts an exact 64-character Digest or a 6-to-63
character prefix. Dedicated uses a bounded `LIKE '<prefix>%'` predicate;
Starter/Essential retrieves the bounded NGM candidate list and filters it
inside the ordinary Tool program, preserving the full Digest inside the Tool
Sandbox while returning only the compact reference.

Managed Clinic canonicalizes harmless query-id length or separator differences
to one deterministic bounded artifact key instead of rejecting Provider work.
When Agent9 itself reports a Tool execution failure, the adapter now projects
the actual safe runtime code and message into `provider-result-v1`, and final
execution retains that code rather than inventing a generic Provider failure.

## Verification

The v13 read-only replays ran concurrently after both AOS and Agent9 internal
production deployment. TCID-13071 completed evidence acquisition with
`providerCapabilityVerified=true`, no failure code, and no
`query_id_invalid` or `slow_query_digest_invalid`; its final evaluation was
`ai_better`. This verifies that harmless semantic query IDs are canonicalized,
compact Slow Query Digest-prefix refinement executes, and corrected caller
requests do not leave a false terminal failure.

TCID-13070 completed response generation and its final evaluation was
`ai_better`. One real Agent9 Tool Sandbox transport failure remained, and the
final execution summary retained the actual `runtime_error` emitted by Agent9
instead of replacing it with `provider_tool_call_failed`. This is verification
of the bounded error-projection fix, not a claim that the independent transient
Agent9-to-Tool transport failure has been eliminated.

Focused contract tests cover local validation and every supported upstream HTTP
4xx class: 400, 401, 403, 404, 409, 422, and 429. Each returns the same ordinary
structured Provider result with its canonical query ID, safe status/detail,
and correction context where applicable; 422 has no special status.

## Next validation action

Track a recurrence of Agent9 Tool Sandbox `runtime_error: fetch failed` as a
separate operational reliability signal. Do not reopen this Provider-result
contract optimization unless a future replay again obscures the actual code or
marks a corrected caller request as terminal.
