feat: add audited async task reconciliation and frozen bindings

This commit is contained in:
xpp committed 2026-10-21 20:08:38 +08:00
1 parent 5be06a97c1
commit c716ec7478
28 files changed
+1312 -76

No files matched your search

@@ -0,0 +1,23 @@
-- Freeze route identity without copying credentials or mutable provider configuration.
ALTER TABLE model_tasks ADD COLUMN provider_id_snapshot varchar(64) REFERENCES model_providers(id);
ALTER TABLE model_tasks ADD COLUMN upstream_model_snapshot varchar(160);
ALTER TABLE model_tasks ADD CONSTRAINT model_tasks_execution_binding_check CHECK (
(provider_id_snapshot IS NULL AND upstream_model_snapshot IS NULL)
OR (provider_id_snapshot IS NOT NULL AND upstream_model_snapshot IS NOT NULL
AND length(trim(provider_id_snapshot)) > 0 AND length(trim(upstream_model_snapshot)) > 0)
);
-- Never guess the original route of an already-submitted legacy task.
-- Unsubmitted queued work can safely freeze its route at migration time.
UPDATE model_tasks t SET provider_id_snapshot=m.provider_id, upstream_model_snapshot=m.upstream_model
FROM model_catalog m
WHERE t.model_id=m.id AND t.status='queued' AND t.provider_task_id IS NULL;
COMMENT ON COLUMN model_tasks.provider_id_snapshot IS 'Provider identity at admission; NULL for ambiguous legacy accepted tasks';
-- New proven bindings cannot associate the same provider/account task with two local wallets.
-- Legacy accepted rows are deliberately not guessed/backfilled, so this index does not rewrite their history.
CREATE UNIQUE INDEX model_tasks_proven_upstream_identity_idx
ON model_tasks(provider_id_snapshot,provider_key_fingerprint,provider_task_id)
WHERE provider_id_snapshot IS NOT NULL AND provider_key_fingerprint IS NOT NULL
AND provider_task_id IS NOT NULL AND provider_task_id <> 'mock';
@@ -0,0 +1,20 @@
-- Append-only operation receipts; no prompts, generated media or raw credentials.
CREATE TABLE task_reconciliations (
request_id uuid PRIMARY KEY,
task_id varchar(160) NOT NULL REFERENCES model_tasks(id),
actor_user_id uuid NOT NULL REFERENCES users(id),
action varchar(24) NOT NULL CHECK (action IN ('resume_polling','confirm_failed','confirm_succeeded')),
request_hash varchar(64) NOT NULL CHECK (request_hash ~ '^[0-9a-f]{64}$'),
evidence_reference varchar(500) NOT NULL CHECK (length(trim(evidence_reference)) BETWEEN 3 AND 500),
note varchar(2000) NOT NULL CHECK (length(trim(note)) BETWEEN 10 AND 2000),
before_state jsonb NOT NULL,
after_state jsonb NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX task_reconciliations_task_idx ON task_reconciliations(task_id,created_at DESC);
CREATE FUNCTION protect_task_reconciliation_history() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'Task reconciliation history is append-only';
END $$;
CREATE TRIGGER task_reconciliation_history_immutable BEFORE UPDATE OR DELETE ON task_reconciliations
FOR EACH ROW EXECUTE FUNCTION protect_task_reconciliation_history();
@@ -0,0 +1,14 @@
-- A resumed task renews its own lease instead of consuming a second slot.
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
local existing = redis.call('ZSCORE', KEYS[1], ARGV[3])
if not existing and redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then
return 0
end
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then
redis.call('PEXPIRE', KEYS[1], ARGV[2])
end
if existing then return 2 end
return 1