feat(research): retain immutable strategy versions and review history

This commit is contained in:
Boki 2026-10-23 19:35:56 -04:00
parent b106c1ca85
commit 4a2e5ad362
36 changed files with 3236 additions and 148 deletions

View file

@ -0,0 +1,52 @@
-- Retain authored packages independently of execution eligibility and human review.
CREATE TABLE IF NOT EXISTS strategies (
id UUID PRIMARY KEY,
name TEXT NOT NULL CHECK (length(btrim(name)) > 0),
description TEXT NOT NULL DEFAULT '',
owner_principal TEXT NOT NULL CHECK (owner_principal IN ('operator', 'service')),
updated_by TEXT NOT NULL CHECK (updated_by IN ('operator', 'service')),
builtin_key TEXT UNIQUE,
archived_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS strategy_versions (
id UUID PRIMARY KEY,
strategy_id UUID NOT NULL REFERENCES strategies(id) ON DELETE RESTRICT,
ordinal INTEGER NOT NULL CHECK (ordinal > 0),
parent_version_id UUID REFERENCES strategy_versions(id) ON DELETE RESTRICT,
envelope JSONB NOT NULL CHECK (jsonb_typeof(envelope) = 'object'),
created_by TEXT NOT NULL CHECK (created_by IN ('operator', 'service')),
origin TEXT NOT NULL CHECK (origin IN ('candidate', 'builtin')),
submission_key TEXT NOT NULL CHECK (length(submission_key) > 0),
submission_fingerprint TEXT NOT NULL CHECK (submission_fingerprint ~ '^[a-f0-9]{64}$'),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (strategy_id, ordinal),
UNIQUE (created_by, submission_key)
);
CREATE TABLE IF NOT EXISTS strategy_review_events (
id UUID PRIMARY KEY,
version_id UUID NOT NULL REFERENCES strategy_versions(id) ON DELETE RESTRICT,
ordinal INTEGER NOT NULL CHECK (ordinal > 0),
decision TEXT NOT NULL CHECK (decision IN ('approve', 'revoke')),
actor TEXT NOT NULL CHECK (actor = 'operator'),
reason TEXT NOT NULL CHECK (length(btrim(reason)) > 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (version_id, ordinal)
);
CREATE OR REPLACE FUNCTION reject_strategy_history_mutation() RETURNS trigger
LANGUAGE plpgsql AS $$ BEGIN
RAISE EXCEPTION 'Strategy history is immutable; publish a new version or append a review';
END $$;
DROP TRIGGER IF EXISTS strategy_versions_immutable ON strategy_versions;
CREATE TRIGGER strategy_versions_immutable BEFORE UPDATE OR DELETE ON strategy_versions
FOR EACH ROW EXECUTE FUNCTION reject_strategy_history_mutation();
DROP TRIGGER IF EXISTS strategy_reviews_immutable ON strategy_review_events;
CREATE TRIGGER strategy_reviews_immutable BEFORE UPDATE OR DELETE ON strategy_review_events
FOR EACH ROW EXECUTE FUNCTION reject_strategy_history_mutation();
DROP TRIGGER IF EXISTS strategies_archive_only ON strategies;
CREATE TRIGGER strategies_archive_only BEFORE DELETE ON strategies
FOR EACH ROW EXECUTE FUNCTION reject_strategy_history_mutation();