Products · Human Review V1

Human review

Review only the fields the system is unsure about, with the evidence beside each one.

Version 1 · A person checks flagged fieldsDesigned, not builtModule 5 of 15

DataSchemaSections

Schema Designed

The SQL for the Human Review service's tables, in the review schema. No other service can read them.

-- Human Review service, version 1. Schema: review.
-- IDs are UUIDv7 values generated by the service.
-- Checked with `npm run schema:check`: runs on PGlite, and every sample row passes every constraint.

create schema if not exists review;

create table review.tasks (
  id                uuid        primary key,
  owner_id          uuid        not null,
  reference         text        not null,
  status            text        not null default 'open',
  checks_overridden boolean     not null default false,
  created_at        timestamptz not null default now(),
  completed_at      timestamptz,
  completed_by      uuid,
  held_by           text,
  released_at       timestamptz,

  constraint tasks_reference_check check (length(reference) between 1 and 200),
  constraint tasks_held_by_check check (held_by ~ '^service:[a-z][a-z0-9-]*$'),
  constraint tasks_released_check check (released_at is null or held_by is not null),
  constraint tasks_status_check check (status in ('open', 'completed')),
  constraint tasks_completed_check check (
    (status = 'completed') = (completed_at is not null)
    and (completed_at is null) = (completed_by is null)
    and (completed_at is null or completed_at >= created_at)
  )
);

create index tasks_owner_id_idx on review.tasks (owner_id, created_at desc);

create table review.fields (
  id             uuid         primary key,
  task_id        uuid         not null,
  name           text         not null,
  line_index     smallint,
  type           text         not null,
  original_value jsonb,
  value          jsonb,
  confidence     numeric(3,2) not null,
  source_page    smallint,
  source_quote   text,
  flagged        boolean      not null,
  flag_reasons   text[]       not null default '{}',
  status         text         not null default 'pending',
  updated_by     uuid,
  updated_at     timestamptz,

  constraint fields_task_id_fkey foreign key (task_id)
    references review.tasks (id) on delete cascade,
  constraint fields_task_name_line_key unique nulls not distinct (task_id, name, line_index),
  constraint fields_name_check check (length(name) between 1 and 64 and (line_index is null or line_index >= 0)),
  constraint fields_type_check check (type in ('text', 'date', 'decimal', 'currency')),
  constraint fields_status_check check (status in ('pending', 'accepted', 'edited', 'not_present')),
  constraint fields_flagged_check check (flagged = (cardinality(flag_reasons) > 0)),
  constraint fields_decided_check check (
    (status <> 'pending') = (updated_by is not null)
    and (updated_by is null) = (updated_at is null)
  ),
  constraint fields_not_present_check check (
    (status <> 'not_present' or value is null)
    and (status <> 'accepted' or value is not distinct from original_value)
  ),
  constraint fields_confidence_check check (confidence between 0 and 1 and (source_page is null or source_page >= 1)),
  constraint fields_quote_check check (length(source_quote) between 1 and 500)
);

-- The queue: flagged first, then lowest confidence.
create index fields_queue_idx on review.fields (task_id, flagged desc, confidence);

create table review.field_changes (
  id         uuid        primary key,
  field_id   uuid        not null,
  action     text        not null,
  old_value  jsonb,
  new_value  jsonb,
  source     text        not null default 'review',
  changed_by uuid        not null,
  created_at timestamptz not null default now(),

  constraint field_changes_field_id_fkey foreign key (field_id)
    references review.fields (id) on delete cascade,
  constraint field_changes_action_check check (action in ('accept', 'edit', 'not_present')),
  constraint field_changes_source_check check (source in ('review'))
);

create index field_changes_field_id_idx on review.field_changes (field_id, created_at);

create table review.eval_candidates (
  id          uuid        primary key,
  task_id     uuid        not null,
  reference   text        not null,
  field_name  text        not null,
  line_index  smallint,
  model_value jsonb,
  human_value jsonb,
  status      text        not null default 'candidate',
  decided_by  uuid,
  decided_at  timestamptz,
  created_at  timestamptz not null default now(),

  constraint eval_candidates_reference_check check (length(reference) between 1 and 200 and length(field_name) between 1 and 64),
  constraint eval_candidates_task_id_fkey foreign key (task_id)
    references review.tasks (id) on delete cascade,
  constraint eval_candidates_status_check check (status in ('candidate', 'approved', 'rejected')),
  constraint eval_candidates_changed_check check (human_value is distinct from model_value),
  constraint eval_candidates_decided_check check (
    (status <> 'candidate') = (decided_at is not null)
    and (decided_at is null) = (decided_by is null)
  )
);

-- One candidate per corrected field of a task; the admin list by status.
create unique index eval_candidates_task_field_idx
  on review.eval_candidates (task_id, field_name, line_index) nulls not distinct;
create index eval_candidates_status_idx on review.eval_candidates (status, created_at);

create table review.inbound_events (
  event_id   uuid        primary key,
  type       text        not null,
  created_at timestamptz not null default now(),

  constraint inbound_events_type_check check (length(type) between 1 and 100)
);

-- The service's database login reaches only this schema. History is never edited.
grant usage on schema review to review_service;
grant select, insert, update, delete on review.tasks, review.fields to review_service;
grant select, insert, delete on review.field_changes to review_service;
grant select, insert, update, delete on review.eval_candidates to review_service;
grant select, insert on review.inbound_events to review_service;
grant select, insert on review.audit_events to review_service;

Schema level: enforced by the database, whatever the code does

  • A field appears once per task and line item (nulls not distinct).
  • A decided field always records who and when; an accepted field keeps its original value; a not-present field has none.
  • A completed task always records who completed it and when.
  • A candidate always differs from what the model said, and records who decided on it.
  • One candidate per corrected field of a task.
  • History rows are inserted and read, never updated. Review's database login can reach only the review schema.

Constraints and indexes by table

review.tasks
  • tasks_pkey: primary key (id)
  • tasks_status_check: open or completed
  • tasks_completed_check: completed_at and completed_by are set exactly when completed
  • tasks_reference_check: reference 1 to 200 characters
  • tasks_held_by_check: held_by is like service:<name>
  • tasks_released_check: released_at only when held_by is set
  • tasks_owner_id_idx: index on (owner_id, created_at desc), for a user's tasks
review.fields
  • fields_pkey: primary key (id)
  • fields_task_id_fkey: foreign key (task_id) references review.tasks on delete cascade
  • fields_task_name_line_key: unique (task_id, name, line_index) nulls not distinct
  • fields_type_check, fields_status_check: only the listed values
  • fields_flagged_check: flagged exactly when flag_reasons is not empty
  • fields_decided_check: updated_by and updated_at are set exactly when status is not pending
  • fields_not_present_check: a not_present field has no value; an accepted one keeps original_value
  • fields_confidence_check, fields_quote_check: confidence 0 to 1; quote 1 to 500 characters
  • fields_queue_idx: index on (task_id, flagged desc, confidence), for the queue order
review.field_changes
  • field_changes_pkey: primary key (id)
  • field_changes_field_id_fkey: foreign key (field_id) references review.fields on delete cascade
  • field_changes_action_check, field_changes_source_check: only the listed values
  • field_changes_field_id_idx: index on (field_id, created_at), for a field's history in order
review.eval_candidates
  • eval_candidates_pkey: primary key (id)
  • eval_candidates_status_check: only the listed values
  • eval_candidates_changed_check: human_value is different from model_value
  • eval_candidates_decided_check: decided_by and decided_at are set exactly when not candidate
  • eval_candidates_task_id_fkey: foreign key to review.tasks, delete cascades
  • eval_candidates_task_field_idx: unique (task_id, field_name, line_index) nulls not distinct, one candidate per corrected field
  • eval_candidates_status_idx: index on (status, created_at), for the admin list
review.inbound_events
  • inbound_events_pkey: primary key (event_id)
  • inbound_events_type_check: 1 to 100 characters

Sample data

review.tasks

idowner_idreferencestatuschecks_overriddencreated_atcompleted_atcompleted_by
0193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a2b0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3edocflow:0193b0c4-91aa-7b33-8c44-5d6e7f8a9b0ccompletedfalse2026-11-17 09:14:43+002026-11-17 09:16:02+000193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e

review.fields

idtask_idnameline_indextypeoriginal_valuevalueconfidencesource_pagesource_quoteflaggedflag_reasonsstatusupdated_byupdated_at
0193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a300193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a2btotal(empty)decimal"1240.00""1230.00"0.302TOTAL DUE $1,240.00true{"Total ≠ subtotal + tax (1,150.00 + 80.00 = 1,230.00)"}edited0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e2026-11-17 09:15:30+00
0193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a310193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a2bvendor_tax_id(empty)text(empty)(empty)0.62(empty)(empty)true{"Low confidence (0.62)"}not_present0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e2026-11-17 09:15:41+00
0193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a320193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a2bvendor_name(empty)text"Acme Supplies Ltd""Acme Supplies Ltd"0.971Acme Supplies Ltdfalse{}pending(empty)(empty)

review.field_changes

idfield_idactionold_valuenew_valuesourcechanged_bycreated_at
0193b0f9-0000-7a00-8b00-0c00d00e00f10193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a30edit"1240.00""1230.00"review0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e2026-11-17 09:15:30+00

review.eval_candidates

idtask_idreferencefield_nameline_indexmodel_valuehuman_valuestatusdecided_bydecided_atcreated_at
0193b101-aa00-7b01-8c02-0d03e04f05a60193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a2bdocflow:0193b0c4-91aa-7b33-8c44-5d6e7f8a9b0ctotal(empty)"1240.00""1230.00"candidate(empty)(empty)2026-11-17 09:15:30+00

review.inbound_events

event_idtypecreated_at
0193b2aa-6f01-7c2d-9e3f-4a5b6c7d8e9fidentity.user.deleted2026-11-17 10:00:01+00

Common queries

The review queue (`GET /tasks/{taskId}`)

select f.*
from review.fields f
join review.tasks t on t.id = f.task_id
where f.task_id = $1 and t.owner_id = $2
  and (f.flagged or $3)          -- $3: all=true
order by f.flagged desc, f.confidence;

Can the task be completed?

select count(*) filter (where flagged and status = 'pending') as unresolved
from review.fields
where task_id = $1;

Record a correction as a candidate (in the same transaction as the edit)

insert into review.eval_candidates (id, task_id, reference, field_name, line_index, model_value, human_value)
values ($1, $2, $3, $4, $5, $6, $7)
on conflict (task_id, field_name, line_index)
do update set human_value = excluded.human_value;