PutResource request-id dedup ignores kind: a reused request ID returns the wrong kind's resource instead of writing #1315

Closed
opened 2026-10-27 13:39:53 +00:00 by sam · 0 comments
Owner

store.PutResource (internal/store/resources.go:91) consults resourceByRequest BEFORE writing, and resourceByRequest (resources.go:117) looks resource_history up by request_id alone — no kind filter. Request IDs are meant to make one write idempotent per kind, but the dedup key is global across all kinds: if any caller ever mints one request ID for two different kinds (a future multi-kind batch apply, or a client bug), the second kind's replace silently returns the FIRST kind's history row (wrong kind, wrong version, wrong value) and never writes, with no error. Every current production caller mints unique IDs ('import:'+kind, 'import:legacy-schedules', 'recovery-rollback--', fresh webAudit IDs), so nothing misbehaves today — this is a latent contract hazard, filed because the store API is per-kind and the dedup is not. Minimal fix: add 'AND kind=?' to the resourceByRequest query (history is only consulted to dedup the same kind's write), or document the request ID as globally unique and assert it at the write path.

Notes

Corroborated live 2026-09-21 by session 01a0c2af (control-plane backlog): the step-vocabulary agreement test reused ONE RequestId across admits, so every admit after the first was a silent idempotent replay — a reviewer flagged it as a test weakness and the fixture was fixed to per-step IDs (internal/app/controlplane/step_vocabulary_agreement_test.go:282, RequestId: "admit:"+stepType). That fixture artefact is exactly this bead's store-side shape: the dedup key ignores kind, so the replay returned the first write instead of writing. The test fix papers over the symptom; the store defect (resourceByRequest has no kind filter) is still live for any future caller that reuses an ID across kinds.
Already resolved before this lane ran. 62fe55e4 (feat(store): key control-plane resources by (org, kind), an ancestor of 7d34b57f) filters the lookup by org_id AND kind AND request_id, and migration 0021 adds UNIQUE INDEX idx_resource_history_org_kind_request ON resource_history (org_id, kind, request_id) while dropping the kind-only index -- so a cross-kind reuse is now a constraint violation, not a silent wrong-kind replay. Coverage already existed: postgres statestore_test.go TestResourceRequestIDIsIdempotentPerKind writes two kinds under one request ID and asserts the second lands as its own revision. The lane re-proved the test can fail by mutating the kind predicate out of the generated query (exact symptom from this bead) and reverting, tree clean. No code change needed or made.
CORRECTION to the close note above, flagged by the doc-clauses lane and confirmed against the schema: a cross-kind or cross-org reuse of one request ID is NOT a constraint violation. With UNIQUE (org_id, kind, request_id) and the kind-filtered lookup, reuse under a different kind or org is a DISTINCT key that writes freely -- which is the fix working as intended, each kind and org getting its own revision. The unique index only refuses a same-key replay, which is the idempotent case it exists for.

Close reason

Closed


Imported from beads archie-core-fcvd · GitHub #1179 · type bug · priority P3 · created 2026-09-21 · closed 2026-09-29

store.PutResource (internal/store/resources.go:91) consults resourceByRequest BEFORE writing, and resourceByRequest (resources.go:117) looks resource_history up by request_id alone — no kind filter. Request IDs are meant to make one write idempotent per kind, but the dedup key is global across all kinds: if any caller ever mints one request ID for two different kinds (a future multi-kind batch apply, or a client bug), the second kind's replace silently returns the FIRST kind's history row (wrong kind, wrong version, wrong value) and never writes, with no error. Every current production caller mints unique IDs ('import:'+kind, 'import:legacy-schedules', 'recovery-rollback-<kind>-<nano>', fresh webAudit IDs), so nothing misbehaves today — this is a latent contract hazard, filed because the store API is per-kind and the dedup is not. Minimal fix: add 'AND kind=?' to the resourceByRequest query (history is only consulted to dedup the same kind's write), or document the request ID as globally unique and assert it at the write path. ### Notes Corroborated live 2026-09-21 by session 01a0c2af (control-plane backlog): the step-vocabulary agreement test reused ONE RequestId across admits, so every admit after the first was a silent idempotent replay — a reviewer flagged it as a test weakness and the fixture was fixed to per-step IDs (internal/app/controlplane/step_vocabulary_agreement_test.go:282, RequestId: "admit:"+stepType). That fixture artefact is exactly this bead's store-side shape: the dedup key ignores kind, so the replay returned the first write instead of writing. The test fix papers over the symptom; the store defect (resourceByRequest has no kind filter) is still live for any future caller that reuses an ID across kinds. Already resolved before this lane ran. 62fe55e4 (feat(store): key control-plane resources by (org, kind), an ancestor of 7d34b57f) filters the lookup by org_id AND kind AND request_id, and migration 0021 adds UNIQUE INDEX idx_resource_history_org_kind_request ON resource_history (org_id, kind, request_id) while dropping the kind-only index -- so a cross-kind reuse is now a constraint violation, not a silent wrong-kind replay. Coverage already existed: postgres statestore_test.go TestResourceRequestIDIsIdempotentPerKind writes two kinds under one request ID and asserts the second lands as its own revision. The lane re-proved the test can fail by mutating the kind predicate out of the generated query (exact symptom from this bead) and reverting, tree clean. No code change needed or made. CORRECTION to the close note above, flagged by the doc-clauses lane and confirmed against the schema: a cross-kind or cross-org reuse of one request ID is NOT a constraint violation. With UNIQUE (org_id, kind, request_id) and the kind-filtered lookup, reuse under a different kind or org is a DISTINCT key that writes freely -- which is the fix working as intended, each kind and org getting its own revision. The unique index only refuses a same-key replay, which is the idempotent case it exists for. ### Close reason Closed --- _Imported from beads `archie-core-fcvd` · GitHub #1179 · type bug · priority P3 · created 2026-09-21 · closed 2026-09-29_
sam added the lowbug labels 2026-10-27 13:39:53 +00:00
sam self-assigned this 2026-10-27 13:39:53 +00:00
sam closed this issue 2026-10-27 13:39:54 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Reference: sam/archie#1315