feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -0,0 +1,449 @@
|
||||
-- Finds candidate-requested selections across currently opened categories.
|
||||
-- The adapter validates policy and manifest freshness, locks the category,
|
||||
-- then runs this query again before it returns any pinned families.
|
||||
-- name: ResolveCurrentAssessmentSelection :many
|
||||
WITH current_openings AS (
|
||||
SELECT DISTINCT ON (category)
|
||||
id,
|
||||
category,
|
||||
policy_version_id,
|
||||
required_families
|
||||
FROM assessment_category_openings
|
||||
ORDER BY category, opened_at DESC, id DESC
|
||||
), current_policy AS (
|
||||
SELECT id, version
|
||||
FROM policy_versions
|
||||
WHERE policy_key = 'product' AND effective_at <= statement_timestamp()
|
||||
ORDER BY version DESC
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT
|
||||
opening.id AS opening_id,
|
||||
opening.category,
|
||||
opening.policy_version_id,
|
||||
opening.required_families,
|
||||
COALESCE((
|
||||
SELECT count(DISTINCT family.family_key)::integer
|
||||
FROM assessment_families AS family
|
||||
WHERE family.category = opening.category AND family.lifecycle = 'published'
|
||||
), 0)::integer AS published_count,
|
||||
policy.id AS effective_policy_id,
|
||||
policy.version AS effective_policy_version,
|
||||
binding.specialization,
|
||||
binding.grade,
|
||||
binding.task_languages,
|
||||
binding.published_manifest_sha256,
|
||||
COALESCE((
|
||||
SELECT jsonb_agg(
|
||||
jsonb_build_object(
|
||||
'family_key', family.family_key,
|
||||
'version', family.version,
|
||||
'content_sha256', encode(family.content_sha256, 'hex')
|
||||
) ORDER BY family.family_key, family.version
|
||||
)
|
||||
FROM assessment_families AS family
|
||||
WHERE family.category = opening.category AND family.lifecycle = 'published'
|
||||
), '[]'::jsonb)::text AS published_manifest
|
||||
FROM current_openings AS opening
|
||||
JOIN assessment_category_catalog_bindings AS binding
|
||||
ON binding.opening_id = opening.id
|
||||
LEFT JOIN current_policy AS policy ON true
|
||||
WHERE binding.specialization = sqlc.arg(specialization)
|
||||
AND binding.grade = sqlc.arg(grade)
|
||||
AND (
|
||||
(sqlc.narg(task_language)::text IS NULL AND cardinality(binding.task_languages) = 0)
|
||||
OR sqlc.narg(task_language)::text = ANY(binding.task_languages)
|
||||
)
|
||||
ORDER BY opening.category;
|
||||
|
||||
-- Serializes root quota checks for one candidate before taking its optional
|
||||
-- specialization or training lock. All callers acquire these locks in order.
|
||||
-- name: LockAssessmentAttemptScope :exec
|
||||
WITH candidate_lock AS MATERIALIZED (
|
||||
SELECT pg_advisory_xact_lock(hashtextextended(
|
||||
'lincodex.assessment.attempt:user:' || sqlc.arg(user_id)::text,
|
||||
0
|
||||
))
|
||||
)
|
||||
SELECT pg_advisory_xact_lock(hashtextextended(
|
||||
'lincodex.assessment.attempt:track:' || sqlc.arg(user_id)::text || ':' ||
|
||||
COALESCE(sqlc.narg(specialization)::text, 'training'),
|
||||
0
|
||||
))
|
||||
FROM candidate_lock;
|
||||
|
||||
-- Locks the active real attempt after LockAssessmentAttemptScope.
|
||||
-- name: GetActiveAssessmentAttemptForSpecialization :one
|
||||
SELECT
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at
|
||||
FROM assessment_attempts
|
||||
WHERE user_id = sqlc.arg(user_id)
|
||||
AND specialization = sqlc.arg(specialization)
|
||||
AND kind IN ('root', 'corrective')
|
||||
AND state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize')
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1
|
||||
FOR UPDATE;
|
||||
|
||||
-- Locks the one active express attempt after LockAssessmentAttemptScope.
|
||||
-- name: GetActiveTrainingAssessmentAttempt :one
|
||||
SELECT
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at
|
||||
FROM assessment_attempts
|
||||
WHERE user_id = sqlc.arg(user_id)
|
||||
AND kind = 'demo_express'
|
||||
AND state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize')
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1
|
||||
FOR UPDATE;
|
||||
|
||||
-- Reads the latest root in a specialization, optionally restricted to grade.
|
||||
-- name: GetLatestRootAssessmentAttempt :one
|
||||
SELECT
|
||||
attempt.id, attempt.user_id, attempt.kind, attempt.mode, attempt.state, attempt.outcome,
|
||||
attempt.bank_category, attempt.specialization, attempt.target_grade, attempt.task_language,
|
||||
attempt.root_attempt_id, attempt.product_policy_version_id, attempt.product_policy_version,
|
||||
attempt.grading_policy_version_id, attempt.opening_id, attempt.opening_manifest_sha256,
|
||||
attempt.training_binding_id, attempt.training_manifest_sha256, attempt.bank_version,
|
||||
attempt.algorithm_version, attempt.created_at, attempt.updated_at, attempt.expires_at,
|
||||
attempt.chain_deadline, attempt.idle_timeout_seconds, attempt.max_sessions, attempt.sessions_used,
|
||||
attempt.session_started_at, attempt.last_activity_at, attempt.version, attempt.ended_at
|
||||
FROM assessment_attempts AS attempt
|
||||
LEFT JOIN assessment_attempt_results AS result
|
||||
ON result.attempt_id = attempt.id AND result.user_id = attempt.user_id
|
||||
WHERE attempt.user_id = sqlc.arg(user_id)
|
||||
AND attempt.specialization = sqlc.arg(specialization)
|
||||
AND attempt.kind = 'root'
|
||||
AND (sqlc.narg(target_grade)::text IS NULL OR attempt.target_grade = sqlc.narg(target_grade)::text)
|
||||
AND attempt.state <> 'insufficient_bank'
|
||||
AND COALESCE(result.result_snapshot->>'not_determined_reason', '')
|
||||
NOT IN ('insufficient_bank', 'sandbox_unavailable')
|
||||
ORDER BY attempt.created_at DESC, attempt.id DESC
|
||||
LIMIT 1
|
||||
FOR SHARE OF attempt;
|
||||
|
||||
-- Counts root attempts only; the caller applies the effective policy's
|
||||
-- configured window and the product's approved accounting scope.
|
||||
-- name: CountRootAssessmentAttemptsSince :one
|
||||
SELECT count(*)::bigint
|
||||
FROM assessment_attempts AS attempt
|
||||
LEFT JOIN assessment_attempt_results AS result
|
||||
ON result.attempt_id = attempt.id AND result.user_id = attempt.user_id
|
||||
WHERE attempt.user_id = sqlc.arg(user_id)
|
||||
AND attempt.kind = 'root'
|
||||
AND attempt.created_at >= sqlc.arg(since)::timestamptz
|
||||
AND (sqlc.narg(specialization)::text IS NULL OR attempt.specialization = sqlc.narg(specialization)::text)
|
||||
AND attempt.state <> 'insufficient_bank'
|
||||
AND COALESCE(result.result_snapshot->>'not_determined_reason', '')
|
||||
NOT IN ('insufficient_bank', 'sandbox_unavailable');
|
||||
|
||||
-- Lists explicit immutable training bindings for the effective product
|
||||
-- policy. Callers reject more than one matching active binding as ambiguous.
|
||||
-- name: ListCurrentAssessmentTrainingBindings :many
|
||||
SELECT
|
||||
binding.id,
|
||||
binding.category,
|
||||
binding.product_policy_version_id,
|
||||
binding.item_count,
|
||||
binding.manifest_sha256,
|
||||
binding.family_manifest,
|
||||
policy.version AS product_policy_version
|
||||
FROM assessment_training_bank_bindings AS binding
|
||||
JOIN policy_versions AS policy ON policy.id = binding.product_policy_version_id
|
||||
WHERE policy.policy_key = 'product'
|
||||
AND policy.effective_at <= statement_timestamp()
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM policy_versions AS newer
|
||||
WHERE newer.policy_key = 'product'
|
||||
AND newer.effective_at <= statement_timestamp()
|
||||
AND newer.version > policy.version
|
||||
)
|
||||
ORDER BY binding.category, binding.created_at DESC, binding.id;
|
||||
|
||||
-- Reads the exact family membership pinned by a training binding.
|
||||
-- name: ListAssessmentTrainingBindingFamilies :many
|
||||
SELECT
|
||||
family_id,
|
||||
ordinal,
|
||||
family_key,
|
||||
family_version,
|
||||
content_sha256
|
||||
FROM assessment_training_bank_binding_families
|
||||
WHERE binding_id = sqlc.arg(binding_id)
|
||||
ORDER BY ordinal;
|
||||
|
||||
-- Inserts the attempt header. The database guard rechecks the current policy,
|
||||
-- opening or disjoint training binding, and immutable pins.
|
||||
-- name: InsertAssessmentAttempt :exec
|
||||
INSERT INTO assessment_attempts (
|
||||
id,
|
||||
user_id,
|
||||
kind,
|
||||
mode,
|
||||
state,
|
||||
outcome,
|
||||
bank_category,
|
||||
specialization,
|
||||
target_grade,
|
||||
task_language,
|
||||
root_attempt_id,
|
||||
product_policy_version_id,
|
||||
product_policy_version,
|
||||
grading_policy_version_id,
|
||||
opening_id,
|
||||
opening_manifest_sha256,
|
||||
training_binding_id,
|
||||
training_manifest_sha256,
|
||||
bank_version,
|
||||
algorithm_version,
|
||||
created_at,
|
||||
updated_at,
|
||||
expires_at,
|
||||
chain_deadline,
|
||||
idle_timeout_seconds,
|
||||
max_sessions,
|
||||
sessions_used,
|
||||
session_started_at,
|
||||
last_activity_at,
|
||||
version,
|
||||
ended_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(kind),
|
||||
sqlc.arg(mode),
|
||||
sqlc.arg(state),
|
||||
sqlc.narg(outcome),
|
||||
sqlc.arg(bank_category),
|
||||
sqlc.narg(specialization),
|
||||
sqlc.narg(target_grade),
|
||||
sqlc.narg(task_language),
|
||||
sqlc.arg(root_attempt_id),
|
||||
sqlc.arg(product_policy_version_id),
|
||||
sqlc.arg(product_policy_version),
|
||||
sqlc.narg(grading_policy_version_id),
|
||||
sqlc.narg(opening_id),
|
||||
sqlc.narg(opening_manifest_sha256),
|
||||
sqlc.narg(training_binding_id),
|
||||
sqlc.narg(training_manifest_sha256),
|
||||
sqlc.arg(bank_version),
|
||||
sqlc.arg(algorithm_version),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(updated_at),
|
||||
sqlc.arg(expires_at),
|
||||
sqlc.narg(chain_deadline),
|
||||
sqlc.arg(idle_timeout_seconds),
|
||||
sqlc.arg(max_sessions),
|
||||
sqlc.arg(sessions_used),
|
||||
sqlc.arg(session_started_at),
|
||||
sqlc.arg(last_activity_at),
|
||||
sqlc.arg(version),
|
||||
sqlc.narg(ended_at)
|
||||
);
|
||||
|
||||
-- Stores the immutable generated variant and empty answer slot.
|
||||
-- name: InsertAssessmentAttemptItem :exec
|
||||
INSERT INTO assessment_attempt_items (
|
||||
id,
|
||||
attempt_id,
|
||||
ordinal,
|
||||
family_id,
|
||||
family_key,
|
||||
family_version,
|
||||
family_content_sha256,
|
||||
variant,
|
||||
answer,
|
||||
answered_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(attempt_id),
|
||||
sqlc.arg(ordinal),
|
||||
sqlc.arg(family_id),
|
||||
sqlc.arg(family_key),
|
||||
sqlc.arg(family_version),
|
||||
sqlc.arg(family_content_sha256),
|
||||
sqlc.arg(variant),
|
||||
sqlc.narg(answer),
|
||||
sqlc.narg(answered_at)
|
||||
);
|
||||
|
||||
-- Returns a full private attempt only to its candidate.
|
||||
-- name: GetAssessmentAttemptForCandidate :one
|
||||
SELECT
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at
|
||||
FROM assessment_attempts
|
||||
WHERE id = sqlc.arg(id) AND user_id = sqlc.arg(user_id)
|
||||
FOR SHARE;
|
||||
|
||||
-- Reads the immutable attempt kind without locking the row. Session commands
|
||||
-- use this only to decide whether C3 applies before acquiring the row lock.
|
||||
-- name: GetAssessmentAttemptKindForCandidate :one
|
||||
SELECT kind
|
||||
FROM assessment_attempts
|
||||
WHERE id = sqlc.arg(id) AND user_id = sqlc.arg(user_id);
|
||||
|
||||
-- Locks one owner-scoped attempt while a session command performs its CAS.
|
||||
-- name: LockAssessmentAttemptForCandidate :one
|
||||
SELECT
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at
|
||||
FROM assessment_attempts
|
||||
WHERE id = sqlc.arg(id) AND user_id = sqlc.arg(user_id)
|
||||
FOR UPDATE;
|
||||
|
||||
-- Reads chain headers in creation order. The use case verifies root eligibility
|
||||
-- and descending target grades in the domain before it creates a correction.
|
||||
-- name: ListAssessmentAttemptChain :many
|
||||
SELECT
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at
|
||||
FROM assessment_attempts
|
||||
WHERE user_id = sqlc.arg(user_id)
|
||||
AND specialization = sqlc.arg(specialization)
|
||||
AND (id = sqlc.arg(root_attempt_id) OR root_attempt_id = sqlc.arg(root_attempt_id))
|
||||
ORDER BY created_at, id
|
||||
FOR SHARE;
|
||||
|
||||
-- Reads private frozen variants and candidate answer bytes in stable order.
|
||||
-- name: ListAssessmentAttemptItems :many
|
||||
SELECT
|
||||
id, attempt_id, ordinal, family_id, family_key, family_version,
|
||||
family_content_sha256, variant::text AS variant, answer, answered_at
|
||||
FROM assessment_attempt_items
|
||||
WHERE attempt_id = sqlc.arg(attempt_id)
|
||||
ORDER BY ordinal;
|
||||
|
||||
-- Applies a versioned session transition after the attempt row is locked.
|
||||
-- name: UpdateAssessmentAttemptSession :one
|
||||
UPDATE assessment_attempts
|
||||
SET
|
||||
state = sqlc.arg(state),
|
||||
outcome = sqlc.narg(outcome),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
sessions_used = sqlc.arg(sessions_used),
|
||||
session_started_at = sqlc.arg(session_started_at),
|
||||
last_activity_at = sqlc.arg(last_activity_at),
|
||||
version = sqlc.arg(version),
|
||||
ended_at = sqlc.narg(ended_at)
|
||||
WHERE id = sqlc.arg(id)
|
||||
AND user_id = sqlc.arg(user_id)
|
||||
AND version = sqlc.arg(expected_version)
|
||||
RETURNING
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at;
|
||||
|
||||
-- Stores one final answer before advancing the enclosing attempt version.
|
||||
-- The caller holds the owner-scoped attempt row lock for the surrounding unit.
|
||||
-- name: UpdateAssessmentAttemptItemAnswer :one
|
||||
UPDATE assessment_attempt_items
|
||||
SET answer = sqlc.arg(answer), answered_at = sqlc.arg(answered_at)
|
||||
WHERE attempt_id = sqlc.arg(attempt_id)
|
||||
AND id = sqlc.arg(item_id)
|
||||
AND answer IS NULL
|
||||
RETURNING id;
|
||||
|
||||
-- Applies an answer, expiry, or finalization transition after the attempt row
|
||||
-- is locked. Session limits and all immutable pins remain unchanged.
|
||||
-- name: UpdateAssessmentAttemptTransition :one
|
||||
UPDATE assessment_attempts
|
||||
SET
|
||||
state = sqlc.arg(state),
|
||||
outcome = sqlc.narg(outcome),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
last_activity_at = sqlc.arg(last_activity_at),
|
||||
version = sqlc.arg(version),
|
||||
ended_at = sqlc.narg(ended_at)
|
||||
WHERE id = sqlc.arg(id)
|
||||
AND user_id = sqlc.arg(user_id)
|
||||
AND version = sqlc.arg(expected_version)
|
||||
RETURNING
|
||||
id, user_id, kind, mode, state, outcome, bank_category, specialization,
|
||||
target_grade, task_language, root_attempt_id, product_policy_version_id,
|
||||
product_policy_version, grading_policy_version_id, opening_id,
|
||||
opening_manifest_sha256, training_binding_id, training_manifest_sha256,
|
||||
bank_version, algorithm_version, created_at, updated_at, expires_at,
|
||||
chain_deadline, idle_timeout_seconds, max_sessions, sessions_used,
|
||||
session_started_at, last_activity_at, version, ended_at;
|
||||
|
||||
-- A newly reserved generic idempotency window can replace an expired typed
|
||||
-- outcome before the new immutable response is inserted.
|
||||
-- name: DeleteAssessmentAttemptIdempotencyOutcome :exec
|
||||
DELETE FROM assessment_attempt_idempotency_outcomes
|
||||
WHERE scope_hash = sqlc.arg(scope_hash);
|
||||
|
||||
-- name: InsertAssessmentAttemptIdempotencyOutcome :exec
|
||||
INSERT INTO assessment_attempt_idempotency_outcomes (
|
||||
scope_hash, operation, status_code, problem_code, response_snapshot,
|
||||
response_schema_version, pending_item_ids, created_at
|
||||
) VALUES (
|
||||
sqlc.arg(scope_hash), sqlc.arg(operation), sqlc.arg(status_code),
|
||||
sqlc.narg(problem_code), sqlc.narg(response_snapshot), 1,
|
||||
sqlc.arg(pending_item_ids)::uuid[], transaction_timestamp()
|
||||
);
|
||||
|
||||
-- name: GetAssessmentAttemptIdempotencyOutcome :one
|
||||
SELECT operation, status_code, problem_code,
|
||||
CAST(COALESCE(response_snapshot::text, ''::text) AS text) AS response_snapshot,
|
||||
pending_item_ids
|
||||
FROM assessment_attempt_idempotency_outcomes
|
||||
WHERE scope_hash = sqlc.arg(scope_hash);
|
||||
|
||||
-- Stores one immutable candidate-visible result with the decision rationale
|
||||
-- and source versions used by its finalized attempt.
|
||||
-- name: InsertAssessmentAttemptResult :exec
|
||||
INSERT INTO assessment_attempt_results (
|
||||
attempt_id, user_id, result_snapshot, schema_version, kind, mode, outcome,
|
||||
product_policy_version_id, product_policy_version,
|
||||
grading_policy_version_id, opening_id, opening_manifest_sha256,
|
||||
training_binding_id, training_manifest_sha256, bank_version, algorithm_version,
|
||||
rationale_rule, pass_mark_basis_points, scored_items, raw_score, pass_points, decided_at
|
||||
)
|
||||
VALUES (
|
||||
sqlc.arg(attempt_id), sqlc.arg(user_id), sqlc.arg(result_snapshot)::jsonb, 1,
|
||||
sqlc.arg(kind), sqlc.arg(mode), sqlc.arg(outcome),
|
||||
sqlc.arg(product_policy_version_id), sqlc.arg(product_policy_version),
|
||||
sqlc.narg(grading_policy_version_id), sqlc.narg(opening_id), sqlc.narg(opening_manifest_sha256),
|
||||
sqlc.narg(training_binding_id), sqlc.narg(training_manifest_sha256),
|
||||
sqlc.arg(bank_version), sqlc.arg(algorithm_version), sqlc.arg(rationale_rule),
|
||||
sqlc.narg(pass_mark_basis_points), sqlc.narg(scored_items), sqlc.narg(raw_score),
|
||||
sqlc.narg(pass_points), sqlc.arg(decided_at)
|
||||
);
|
||||
|
||||
-- Reads a result only for the owning candidate.
|
||||
-- name: GetAssessmentAttemptResult :one
|
||||
SELECT result_snapshot::text AS result_snapshot
|
||||
FROM assessment_attempt_results
|
||||
WHERE user_id = sqlc.arg(user_id) AND attempt_id = sqlc.arg(attempt_id);
|
||||
Reference in new issue
Block a user