Files
lincodex/backend/db/migrations/00028_create_assessment_signals.sql
T

187 lines
7.8 KiB
PL/PgSQL

-- +goose Up
-- Signal storage is a separate, bounded side channel. It contains only
-- client-reported timing and byte counts, never answer or clipboard content.
-- Writes take the generic idempotency scope, C3 consent row, then attempt row;
-- the event trigger's parent lock is reentrant inside that same transaction.
ALTER TABLE assessment_attempt_items
ADD CONSTRAINT assessment_attempt_items_attempt_id_id_key UNIQUE (attempt_id, id);
CREATE TABLE assessment_signal_batches (
id uuid NOT NULL,
attempt_id uuid NOT NULL REFERENCES assessment_attempts (id) ON DELETE CASCADE,
received_at timestamptz NOT NULL,
event_count integer NOT NULL,
CONSTRAINT assessment_signal_batches_pkey PRIMARY KEY (id),
CONSTRAINT assessment_signal_batches_attempt_id_id_key UNIQUE (attempt_id, id),
CONSTRAINT assessment_signal_batches_event_count CHECK (event_count BETWEEN 1 AND 50)
);
CREATE INDEX assessment_signal_batches_attempt_received_idx
ON assessment_signal_batches (attempt_id, received_at, id);
-- Direct edits and deletes would invalidate accepted receipt counts. The
-- nested delete issued by the attempt foreign-key cascade is the only delete
-- path while a batch exists.
-- +goose StatementBegin
CREATE FUNCTION assessment_signal_batch_immutable() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP = 'DELETE' AND pg_trigger_depth() > 1 THEN
RETURN OLD;
END IF;
RAISE EXCEPTION USING
ERRCODE = '23000', CONSTRAINT = 'assessment_signal_batches_immutable',
MESSAGE = 'assessment signal batches are immutable';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_signal_batch_immutable
BEFORE UPDATE OR DELETE ON assessment_signal_batches
FOR EACH ROW EXECUTE FUNCTION assessment_signal_batch_immutable();
CREATE TABLE assessment_signal_events (
batch_id uuid NOT NULL,
attempt_id uuid NOT NULL,
ordinal integer NOT NULL,
item_instance_id uuid NOT NULL,
kind text NOT NULL,
occurred_at timestamptz NOT NULL,
duration_ms integer,
size_bytes integer,
CONSTRAINT assessment_signal_events_pkey PRIMARY KEY (batch_id, ordinal),
CONSTRAINT assessment_signal_events_batch_attempt_fk FOREIGN KEY (attempt_id, batch_id)
REFERENCES assessment_signal_batches (attempt_id, id) ON DELETE CASCADE,
CONSTRAINT assessment_signal_events_item_attempt_fk FOREIGN KEY (attempt_id, item_instance_id)
REFERENCES assessment_attempt_items (attempt_id, id) ON DELETE CASCADE,
CONSTRAINT assessment_signal_events_ordinal CHECK (ordinal BETWEEN 1 AND 50),
CONSTRAINT assessment_signal_events_kind CHECK (
kind IN ('focus_loss', 'copy', 'paste', 'cut', 'fullscreen_exit')
),
CONSTRAINT assessment_signal_events_measurement CHECK (
(kind = 'focus_loss' AND duration_ms IS NOT NULL AND duration_ms BETWEEN 1 AND 259200000 AND size_bytes IS NULL)
OR (kind IN ('copy', 'paste', 'cut') AND duration_ms IS NULL AND size_bytes IS NOT NULL AND size_bytes BETWEEN 0 AND 2147483647)
OR (kind = 'fullscreen_exit' AND duration_ms IS NULL AND size_bytes IS NULL)
)
);
CREATE INDEX assessment_signal_events_attempt_idx
ON assessment_signal_events (attempt_id, batch_id, ordinal);
-- Serialize capacity and lifecycle checks on the same attempt row used by the
-- usecase. The application stores a whole-batch saturated receipt before this
-- trigger is reached; the trigger is a backstop for other database writers.
-- +goose StatementBegin
CREATE FUNCTION assessment_signal_event_guard() RETURNS trigger
LANGUAGE plpgsql AS $$
DECLARE
attempt_kind text;
attempt_state text;
attempt_expires_at timestamptz;
existing_count bigint;
BEGIN
SELECT kind, state, expires_at
INTO attempt_kind, attempt_state, attempt_expires_at
FROM assessment_attempts
WHERE id = NEW.attempt_id
FOR UPDATE;
IF attempt_kind NOT IN ('root', 'corrective')
OR attempt_state NOT IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize')
OR attempt_expires_at <= clock_timestamp() THEN
RAISE EXCEPTION USING
ERRCODE = '23514', CONSTRAINT = 'assessment_signal_attempt_active',
MESSAGE = 'assessment attempt cannot accept signal events';
END IF;
SELECT count(*) INTO existing_count
FROM assessment_signal_events
WHERE attempt_id = NEW.attempt_id;
IF existing_count >= 10000 THEN
RAISE EXCEPTION USING
ERRCODE = '23514', CONSTRAINT = 'assessment_signal_attempt_capacity',
MESSAGE = 'assessment attempt signal capacity is full';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_signal_event_guard
BEFORE INSERT ON assessment_signal_events
FOR EACH ROW EXECUTE FUNCTION assessment_signal_event_guard();
-- Events are immutable. Only nested foreign-key cascades may delete them.
-- +goose StatementBegin
CREATE FUNCTION assessment_signal_event_immutable() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP = 'DELETE' AND pg_trigger_depth() > 1 THEN
RETURN OLD;
END IF;
RAISE EXCEPTION USING
ERRCODE = '23000', CONSTRAINT = 'assessment_signal_events_immutable',
MESSAGE = 'assessment signal events are immutable';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_signal_event_immutable
BEFORE UPDATE OR DELETE ON assessment_signal_events
FOR EACH ROW EXECUTE FUNCTION assessment_signal_event_immutable();
CREATE TABLE assessment_signal_idempotency_outcomes (
scope_hash bytea NOT NULL REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
attempt_id uuid NOT NULL REFERENCES assessment_attempts (id) ON DELETE CASCADE,
status text NOT NULL,
accepted_events integer NOT NULL,
received_at timestamptz NOT NULL,
CONSTRAINT assessment_signal_idempotency_outcomes_pkey PRIMARY KEY (scope_hash),
CONSTRAINT assessment_signal_idempotency_outcomes_scope_hash CHECK (octet_length(scope_hash) = 32),
CONSTRAINT assessment_signal_idempotency_outcomes_status CHECK (
(status = 'accepted' AND accepted_events BETWEEN 1 AND 50)
OR (status = 'saturated' AND accepted_events = 0)
)
);
CREATE INDEX assessment_signal_idempotency_outcomes_attempt_received_idx
ON assessment_signal_idempotency_outcomes (attempt_id, received_at, scope_hash);
-- Expired scope replacement and idempotency cleanup may delete an outcome,
-- but a live response snapshot cannot be rewritten.
-- +goose StatementBegin
CREATE FUNCTION assessment_signal_idempotency_outcome_immutable() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION USING
ERRCODE = '23000', CONSTRAINT = 'assessment_signal_idempotency_outcomes_immutable',
MESSAGE = 'assessment signal outcomes are immutable';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_signal_idempotency_outcome_immutable
BEFORE UPDATE ON assessment_signal_idempotency_outcomes
FOR EACH ROW EXECUTE FUNCTION assessment_signal_idempotency_outcome_immutable();
-- +goose Down
DROP TRIGGER assessment_signal_idempotency_outcome_immutable ON assessment_signal_idempotency_outcomes;
DROP FUNCTION assessment_signal_idempotency_outcome_immutable();
DROP INDEX assessment_signal_idempotency_outcomes_attempt_received_idx;
DROP TABLE assessment_signal_idempotency_outcomes;
DROP TRIGGER assessment_signal_event_immutable ON assessment_signal_events;
DROP FUNCTION assessment_signal_event_immutable();
DROP TRIGGER assessment_signal_event_guard ON assessment_signal_events;
DROP FUNCTION assessment_signal_event_guard();
DROP INDEX assessment_signal_events_attempt_idx;
DROP TABLE assessment_signal_events;
DROP TRIGGER assessment_signal_batch_immutable ON assessment_signal_batches;
DROP FUNCTION assessment_signal_batch_immutable();
DROP INDEX assessment_signal_batches_attempt_received_idx;
DROP TABLE assessment_signal_batches;
ALTER TABLE assessment_attempt_items
DROP CONSTRAINT assessment_attempt_items_attempt_id_id_key;