fix(store): scope the resource request-ID idempotency key to its kind

PutResource replayed a prior write by request_id alone: the dedup
lookup and the schema's own UNIQUE constraint were both global, while
the resource API -- and every production caller -- is per kind. A
request ID reused across kinds therefore either replayed the first
kind's resource with the second kind's name attached to the call, or,
once the lookup was fixed, failed the insert on
UNIQUE(resource_history.request_id). The step-vocabulary agreement test
hit exactly this shape before its fixture was fixed to per-step IDs,
which is what elevated the hazard from latent to demonstrated
(archie-core-fcvd).

The dedup key is now (kind, request ID) in both places: resourceByRequest
filters on the kind, and schema v4 rebuilds resource_history with a
UNIQUE(kind, request_id) index instead of the column constraint.
Existing data satisfies the new shape -- a global constraint is strictly
stricter than the per-kind one -- so the rebuild copies every row and
drops nothing. Detection is the auto-index the old column constraint
created, so the step is a no-op on a database that already has the v4
shape and on one created fresh.
This commit is contained in:
sam committed 2026-09-22 01:30:46 +10:00
1 parent b2ac69ace3
commit 9cd6a2aa44
4 files changed
+161 -7

No files matched your search

+14 -4
View File
@@ -42,10 +42,15 @@ CREATE TABLE IF NOT EXISTS resources (
CREATE TABLE IF NOT EXISTS resource_history (
id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, value BLOB NOT NULL,
version INTEGER NOT NULL, actor TEXT NOT NULL, source TEXT NOT NULL,
request_id TEXT NOT NULL UNIQUE, expected_version INTEGER NOT NULL,
request_id TEXT NOT NULL, expected_version INTEGER NOT NULL,
current_version INTEGER NOT NULL, at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_resource_history_kind_version ON resource_history(kind, version);
-- The request-ID idempotency key is per kind, not global: a resource write
-- replays only the same kind's prior write (resourceByRequest). A database
-- written before v4 carries the old global UNIQUE as a column constraint;
-- migrateResourceHistoryToPerKindRequestIDs rebuilds it to this shape.
CREATE UNIQUE INDEX IF NOT EXISTS idx_resource_history_kind_request ON resource_history(kind, request_id);
`
func (s *Store) Resource(ctx context.Context, kind string) (Resource, error) {
@@ -88,7 +93,7 @@ func (s *Store) PutResource(ctx context.Context, w ResourceWrite) (_ Resource, r
}
defer func() { _ = tx.Rollback() }()
if existing, ok, err := resourceByRequest(ctx, tx, w.RequestID); err != nil {
if existing, ok, err := resourceByRequest(ctx, tx, w.Kind, w.RequestID); err != nil {
return Resource{}, err
} else if ok {
return existing, nil
@@ -118,9 +123,14 @@ func (s *Store) PutResource(ctx context.Context, w ResourceWrite) (_ Resource, r
return Resource{Kind: w.Kind, Value: append([]byte(nil), w.Value...), Version: next, Actor: w.Actor, Source: w.Source, RequestID: w.RequestID, ExpectedVersion: w.ExpectedVersion, CurrentVersion: current, At: w.At.UTC()}, nil
}
func resourceByRequest(ctx context.Context, tx *sql.Tx, requestID string) (Resource, bool, error) {
// resourceByRequest replays one prior write by its request ID, so a retried
// replace is idempotent instead of a second revision. The key is (kind,
// request ID), not the request ID alone: the API is per kind, so an ID reused
// across kinds must write the new kind rather than silently replay the first
// kind's resource (archie-core-fcvd).
func resourceByRequest(ctx context.Context, tx *sql.Tx, kind, requestID string) (Resource, bool, error) {
var r Resource
err := tx.QueryRowContext(ctx, `SELECT kind,value,version,actor,source,request_id,expected_version,current_version,at FROM resource_history WHERE request_id=?`, requestID).
err := tx.QueryRowContext(ctx, `SELECT kind,value,version,actor,source,request_id,expected_version,current_version,at FROM resource_history WHERE kind=? AND request_id=?`, kind, requestID).
Scan(&r.Kind, &r.Value, &r.Version, &r.Actor, &r.Source, &r.RequestID, &r.ExpectedVersion, &r.CurrentVersion, sqliteTime{&r.At})
if errors.Is(err, sql.ErrNoRows) {
return Resource{}, false, nil
+20
View File
@@ -39,6 +39,26 @@ func TestPutResourceDuplicateRequestIsIdempotent(t *testing.T) {
}
}
// TestPutResourceRequestIDIsScopedToTheKind: the request ID makes one write
// idempotent for the kind it wrote: the dedup key is (kind, request ID), so a
// request ID reused across two kinds writes the second instead of silently
// replaying the first kind's resource (archie-core-fcvd).
func TestPutResourceRequestIDIsScopedToTheKind(t *testing.T) {
s := OpenTest(t)
defer s.Close()
ctx := t.Context()
if _, err := s.PutResource(ctx, ResourceWrite{Kind: "settings", Value: []byte(`{"v":1}`), Actor: "a", Source: "test", RequestID: "shared", ExpectedVersion: 0, At: time.Now()}); err != nil {
t.Fatal(err)
}
second, err := s.PutResource(ctx, ResourceWrite{Kind: "other", Value: []byte(`{"v":9}`), Actor: "a", Source: "test", RequestID: "shared", ExpectedVersion: 0, At: time.Now()})
if err != nil {
t.Fatalf("PutResource with a request ID another kind already used: %v", err)
}
if second.Kind != "other" || second.Version != 1 {
t.Fatalf("second write = (%s, v%d), want the %q write itself, not a replay of the first kind's resource", second.Kind, second.Version, "other")
}
}
func TestResourceHistoryReturnsEveryRevisionNewestFirst(t *testing.T) {
s := OpenTest(t)
defer s.Close()
+46 -2
View File
@@ -24,7 +24,10 @@ type Store struct {
bindingsCipher BindingCipher
}
const taskSchemaVersion = 3
// taskSchemaVersion is the newest schema this binary opens. ValidateFile
// refuses a database whose version is newer, so a binary and the file it
// opens move together.
const taskSchemaVersion = 4
// OpenOption configures the store at open time.
type OpenOption func(*openOptions)
@@ -139,16 +142,57 @@ func migrateTasks(ctx context.Context, db *sql.DB) error {
return err
}
}
if err := migrateResourceHistoryToPerKindRequestIDs(ctx, tx); err != nil {
return err
}
return finishTaskMigration(ctx, tx, columns)
}
// migrateResourceHistoryToPerKindRequestIDs is the v4 step: the history
// table's global request_id UNIQUE becomes per-kind uniqueness, the key the
// resource write path actually dedups on (resourceByRequest). SQLite cannot
// drop a column constraint, so the table is rebuilt in place; a global
// constraint is strictly stricter than the per-kind one, so no existing row
// can fail the new shape. Detection is the auto-index the old column
// constraint created -- sqlite_master carries it with a NULL sql -- which the
// rebuilt table leaves no copy of, so the step is a no-op on a database that
// already has the v4 shape (including one resourcesSchema created fresh).
func migrateResourceHistoryToPerKindRequestIDs(ctx context.Context, tx *sql.Tx) error {
var autoIndexes int
if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type='index' AND tbl_name='resource_history' AND sql IS NULL`).Scan(&autoIndexes); err != nil {
return err
}
if autoIndexes == 0 {
return nil
}
for _, statement := range []string{
`CREATE TABLE resource_history_per_kind_request (
id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, value BLOB NOT NULL,
version INTEGER NOT NULL, actor TEXT NOT NULL, source TEXT NOT NULL,
request_id TEXT NOT NULL, expected_version INTEGER NOT NULL,
current_version INTEGER NOT NULL, at TEXT NOT NULL
)`,
`INSERT INTO resource_history_per_kind_request (id,kind,value,version,actor,source,request_id,expected_version,current_version,at)
SELECT id,kind,value,version,actor,source,request_id,expected_version,current_version,at FROM resource_history`,
`DROP TABLE resource_history`,
`ALTER TABLE resource_history_per_kind_request RENAME TO resource_history`,
`CREATE INDEX IF NOT EXISTS idx_resource_history_kind_version ON resource_history(kind, version)`,
`CREATE UNIQUE INDEX IF NOT EXISTS idx_resource_history_kind_request ON resource_history(kind, request_id)`,
} {
if _, err := tx.ExecContext(ctx, statement); err != nil {
return fmt.Errorf("rebuild resource_history for per-kind request IDs: %w", err)
}
}
return nil
}
func finishTaskMigration(ctx context.Context, tx *sql.Tx, columns map[string]bool) error {
if columns["owner"] && columns["repo"] && columns["pr_number"] {
if _, err := tx.ExecContext(ctx, `CREATE INDEX IF NOT EXISTS idx_tasks_pr ON tasks(owner, repo, pr_number)`); err != nil {
return err
}
}
if _, err := tx.ExecContext(ctx, `PRAGMA user_version = 3`); err != nil {
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`PRAGMA user_version = %d`, taskSchemaVersion)); err != nil {
return err
}
return tx.Commit()
+81 -1
View File
@@ -4,10 +4,12 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"unicode/utf8"
"github.com/samcharles93/archie-core/internal/domain/workflow"
@@ -190,13 +192,91 @@ func TestOpenMigratesLegacyEventsAttemptColumn(t *testing.T) {
}
}
// TestOpenMigratesResourceHistoryToPerKindRequestIDs covers the v4 step: the
// history table's global request_id UNIQUE becomes per-kind uniqueness. A
// global constraint is strictly stricter than the per-kind one, so no legacy
// row can fail the rebuild -- but the legacy table must not leave a request
// ID another kind already used refusing the write (archie-core-fcvd).
func TestOpenMigratesResourceHistoryToPerKindRequestIDs(t *testing.T) {
path := filepath.Join(t.TempDir(), "legacy.db")
raw, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
_, err = raw.ExecContext(t.Context(), `
CREATE TABLE resources (
kind TEXT PRIMARY KEY, value BLOB NOT NULL, version INTEGER NOT NULL, updated_at TEXT NOT NULL
);
CREATE TABLE resource_history (
id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, value BLOB NOT NULL,
version INTEGER NOT NULL, actor TEXT NOT NULL, source TEXT NOT NULL,
request_id TEXT NOT NULL UNIQUE, expected_version INTEGER NOT NULL,
current_version INTEGER NOT NULL, at TEXT NOT NULL
);
INSERT INTO resources (kind,value,version,updated_at)
VALUES ('settings','{"v":1}',1,'2026-01-01T00:00:00Z');
INSERT INTO resource_history (kind,value,version,actor,source,request_id,expected_version,current_version,at)
VALUES ('settings','{"v":1}',1,'a','test','shared',0,0,'2026-01-01T00:00:00Z');
PRAGMA user_version = 3`)
if err != nil {
t.Fatal(err)
}
if err := raw.Close(); err != nil {
t.Fatal(err)
}
s, err := Open(t.Context(), path)
if err != nil {
t.Fatalf("Open: %v", err)
}
t.Cleanup(func() { _ = s.Close() })
// The row written before the migration survives.
resource, err := s.Resource(t.Context(), "settings")
if err != nil {
t.Fatalf("Resource after migration: %v", err)
}
if string(resource.Value) != `{"v":1}` {
t.Fatalf("legacy history row = %s, want the preserved value", resource.Value)
}
// The column constraint is gone -- no auto-index (an index sqlite_master
// carries with a NULL sql) is left on the table -- and per-kind
// uniqueness is its replacement.
var autoIndexes int
if err := s.db.QueryRowContext(t.Context(), `SELECT count(*) FROM sqlite_master WHERE type='index' AND tbl_name='resource_history' AND sql IS NULL`).Scan(&autoIndexes); err != nil {
t.Fatal(err)
}
if autoIndexes != 0 {
t.Fatalf("resource_history still carries %d column-constraint index(es), want none", autoIndexes)
}
var perKind int
if err := s.db.QueryRowContext(t.Context(), `SELECT count(*) FROM sqlite_master WHERE type='index' AND name='idx_resource_history_kind_request'`).Scan(&perKind); err != nil {
t.Fatal(err)
}
if perKind != 1 {
t.Fatalf("idx_resource_history_kind_request exists %d times, want 1", perKind)
}
// The dedup key itself: a request ID another kind already used writes the
// new kind instead of failing the global constraint or replaying the old
// resource.
second, err := s.PutResource(t.Context(), ResourceWrite{Kind: "other", Value: []byte(`{"v":9}`), Actor: "a", Source: "test", RequestID: "shared", ExpectedVersion: 0, At: time.Now()})
if err != nil {
t.Fatalf("PutResource with a request ID another kind already used: %v", err)
}
if second.Kind != "other" || second.Version != 1 {
t.Fatalf("second write = (%s, v%d), want the %q write itself", second.Kind, second.Version, "other")
}
}
func TestOpenRejectsNewerSchemaVersion(t *testing.T) {
path := filepath.Join(t.TempDir(), "tasks.db")
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(t.Context(), `PRAGMA user_version = 4`); err != nil {
if _, err := db.ExecContext(t.Context(), fmt.Sprintf(`PRAGMA user_version = %d`, taskSchemaVersion+1)); err != nil {
t.Fatal(err)
}
if err := db.Close(); err != nil {