Skip to content

refactor(reliability): read-only idempotency lookup — one deterministic answer to 'which execution is mine?', retiring the per-route timeout heuristic #2671

Open

Description

@vybe

Summary

Three MCP dispatch routes need to answer the same question after a gateway timeout — "which execution is mine?" — and each one currently re-derives its own answer:

Route Today
/chat (#914) executions scan: trigger set + key id + message + recency window
sync /task (#2661) the same scan, plus reading the execution_id out of an in-flight 409
fan_out (#2670) nothing — no receipt at all

The scan is a heuristic. It works, but it is identification-by-elimination: it needs a recency window, a trigger allowlist, a key filter and a message match, and it must return nothing when more than one candidate survives (#2661) because it cannot prove which row is the caller's.

Meanwhile the exact answer already exists. Idempotency-Key is minted per call at every one of these boundaries (Invariant #18 mandates it), and idempotency_keys(scope, key) already stores execution_id plus the completed response snapshot (db/schema.py:1652). The identity is already being recorded and simply never read back.

Proposal

A read-only lookup — GET /api/agents/{name}/idempotency/{key} (or equivalent) — returning {execution_id, status, snapshot?} without dispatching anything.

  • db/idempotency.py gains a peek() beside claim() — roughly a dozen lines, the same SELECT with the same TTL cutoff, minus the delete/insert.
  • The MCP client calls it on abort instead of scanning executions.

Properties the heuristic cannot have: exact attribution, no recency window, no trigger allowlist, no concurrency ambiguity — and it returns the real result when the task completed between the abort and the lookup. One implementation covers all three routes plus webhooks.

What this must NOT be

Do not implement it as "re-POST the dispatch with the same key." idempotency_service.begin() fails open on any DB error (idempotency_service.py:399-401), so on that path the "probe" dispatches a second execution — precisely the bug this family exists to prevent. #2661 rejected that approach for this reason. The lookup must be genuinely read-only.

Security requirement (do not skip)

The key is caller-supplied and the scope is agent:{name}, so the endpoint must verify the resolved execution belongs to the caller before returning anything. Keys do bind caller identity today — deriveMcpIdempotencyKey hashes [caller, agent, routeToken, model, sync|async, message] — but that is the client's convention, not an enforced property, and userId is a guessable integer. Returning execution_id + snapshot for an arbitrary (agent, key) would otherwise be a cross-user disclosure: exactly the hazard make_inline_auth_scope's docstring already documents ("MCP clients derive deterministic keys from call args, so two users asking the same agent the same question collide by design").

Ownership check on the resolved row, and enumeration-safe responses per Invariant #8.

Acceptance Criteria

Follow-up of #2661 (which chose the client-side fix under P1 incident pressure and deliberately left this as the mechanism that retires it). Related: #2670, #914, RELIABILITY-006 (#525).

Activity

  1. added
    priority-p2
    type-refactor
    theme-reliability
    complexity-medium
    status-ready
    on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-mediumpriority-p2status-readytheme-reliabilitytype-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions