feat(assessment): find the attempt behind a leaked task text

This commit is contained in:
shira committed 2026-10-11 13:19:38 +03:00
1 parent bb917bf62e
commit 5a731c25b7
26 files changed
+2500

No files matched your search

@@ -0,0 +1,85 @@
-- +goose Up
-- The audit of the operator search for the attempt behind a leaked task text.
-- Every search writes one row, whatever it finds, before a result is shown.
-- A row holds no leaked text: only a keyed digest of the normalized text and its
-- length, the outcome, and, for a found search, the attempt. The attempt is not
-- a foreign key, so the row outlives an account deletion. Nothing reads these
-- rows for a candidate, an employer, a result, a category, or a quota.
CREATE TABLE assessment_leak_traces (
id uuid NOT NULL,
actor text NOT NULL,
searched_at timestamptz NOT NULL,
text_digest bytea NOT NULL,
key_version smallint NOT NULL,
text_runes integer NOT NULL,
status text NOT NULL,
method text,
attempt_id uuid,
candidates integer NOT NULL,
evidence_bits integer NOT NULL,
CONSTRAINT assessment_leak_traces_pkey PRIMARY KEY (id),
CONSTRAINT assessment_leak_traces_actor CHECK (
char_length(actor) <= 128 AND actor ~ '^operator:[a-z0-9][a-z0-9._-]*$'
),
CONSTRAINT assessment_leak_traces_digest CHECK (octet_length(text_digest) = 32),
CONSTRAINT assessment_leak_traces_key_version CHECK (key_version > 0),
CONSTRAINT assessment_leak_traces_counts CHECK (text_runes >= 0 AND candidates >= 0 AND evidence_bits >= 0),
CONSTRAINT assessment_leak_traces_status CHECK (status IN ('found', 'ambiguous', 'not_found')),
CONSTRAINT assessment_leak_traces_method CHECK (
method IS NULL OR method IN ('invisible_mark', 'visible_code', 'wording', 'variant')
),
CONSTRAINT assessment_leak_traces_found CHECK (
(status = 'found' AND attempt_id IS NOT NULL AND method IS NOT NULL)
OR (status <> 'found' AND attempt_id IS NULL)
)
);
CREATE INDEX assessment_leak_traces_attempt_idx
ON assessment_leak_traces (attempt_id, searched_at)
WHERE attempt_id IS NOT NULL;
CREATE INDEX assessment_leak_traces_searched_idx
ON assessment_leak_traces (searched_at, id);
-- +goose StatementBegin
CREATE FUNCTION assessment_leak_traces_immutable() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION USING
ERRCODE = '23000', CONSTRAINT = 'assessment_leak_traces_immutable',
MESSAGE = 'assessment leak trace audit records are immutable';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_leak_traces_immutable
BEFORE UPDATE OR DELETE ON assessment_leak_traces
FOR EACH ROW EXECUTE FUNCTION assessment_leak_traces_immutable();
CREATE TRIGGER assessment_leak_traces_no_truncate
BEFORE TRUNCATE ON assessment_leak_traces
FOR EACH STATEMENT EXECUTE FUNCTION assessment_leak_traces_immutable();
-- +goose Down
-- The audit of operator searches is durable evidence. Rolling the schema back
-- must not remove it as a side effect: an operator exports or resolves it first.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM assessment_leak_traces) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'assessment_leak_traces_retained',
TABLE = 'assessment_leak_traces',
MESSAGE = 'assessment leak trace audit must be retained before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
DROP TRIGGER assessment_leak_traces_no_truncate ON assessment_leak_traces;
DROP TRIGGER assessment_leak_traces_immutable ON assessment_leak_traces;
DROP FUNCTION assessment_leak_traces_immutable();
DROP INDEX assessment_leak_traces_searched_idx;
DROP INDEX assessment_leak_traces_attempt_idx;
DROP TABLE assessment_leak_traces;
@@ -0,0 +1,51 @@
-- Real attempts in ascending identifier order, so that a caller can walk all of
-- them in pages. Only the identifier and the time span are read.
-- name: ListAssessmentTraceAttempts :many
SELECT id, created_at, expires_at
FROM assessment_attempts
WHERE kind IN ('root', 'corrective') AND id > sqlc.arg(after_id)
ORDER BY id
LIMIT sqlc.arg(row_limit);
-- The unmarked statements of real attempts whose text, with runs of white space
-- collapsed, contains the fragment. The fragment is matched literally: position
-- is not a pattern. The scan reads the stored variant of every item, which is
-- fine for an operator command and not for a request path.
-- name: FindAssessmentTraceStatements :many
SELECT
item.attempt_id,
(item.variant ->> 'Statement')::text AS statement
FROM assessment_attempt_items AS item
INNER JOIN assessment_attempts AS attempt ON item.attempt_id = attempt.id
WHERE
attempt.kind IN ('root', 'corrective')
AND position(sqlc.arg(fragment)::text IN regexp_replace((item.variant ->> 'Statement')::text, '\s+', ' ', 'g')) > 0
ORDER BY item.attempt_id, item.ordinal
LIMIT sqlc.arg(row_limit);
-- name: InsertAssessmentLeakTrace :exec
INSERT INTO assessment_leak_traces (
id,
actor,
searched_at,
text_digest,
key_version,
text_runes,
status,
method,
attempt_id,
candidates,
evidence_bits
) VALUES (
sqlc.arg(id),
sqlc.arg(actor),
sqlc.arg(searched_at),
sqlc.arg(text_digest),
sqlc.arg(key_version),
sqlc.arg(text_runes),
sqlc.arg(status),
sqlc.narg(method),
sqlc.narg(attempt_id),
sqlc.arg(candidates),
sqlc.arg(evidence_bits)
);