Skip to content

Human-In-The-Loop ​

Do not duplicate

  • Review session schema
  • Locking algorithm details
  • API endpoint details
  • Full UI walkthroughs

HITL routes low-confidence or review-required document results to humans. It is session-oriented: a reviewer works through a bounded review session for a document, records corrections, and then approves it, flags it for someone else, or skips it back to the queue.

Source Map ​

  • Architecture and lifecycle concepts live in docs-md/architecture/HITL_ARCHITECTURE.md.
  • Field-level review gating (the hitl.applyReviewCriteria graph activity — a prediction-only rule engine deciding which fields need review) is documented in docs-md/architecture/HITL_REVIEW_CRITERIA.md; it produces a review plan in workflow context but does not itself touch session/queue state.
  • Backend session, queue, lock, correction, and analytics behavior lives in apps/backend-services/src/hitl/.
  • Persisted model truth lives in apps/shared/prisma/schema.prisma.
  • Frontend review entrypoints are split between page-level routes and feature components under apps/frontend/src/.

Design Notes ​

  • HITL is per-document-instance state, not group-level reusable configuration.
  • The queue reads persisted ocr_results only — gated workflow templates persist OCR before the human gate (persistOcr node, since 2026-08-01); a post-gate store, in the templates that have one, writes the same extracted result again, and reviewer corrections stay in field_corrections. See the queue-entry note in HITL_ARCHITECTURE.md.
  • Reviewers edit inline on the document canvas (CanvasFieldOverlay under apps/frontend/src/features/annotation/hitl/), not only in the side panel.
  • A demo/reset seed exists for local review-queue demos without paid OCR: npm run demo:reset — see docs-md/extraction/HITL_DEMO_RESET.md.
  • It differs from Tables and extensions because it involves session lifecycle, locking, and human completion decisions.
  • Corrections are audit-like records of review actions, not a replacement for the original document record. Each records who made it, so a session that changed hands still credits every correction correctly.
  • A session ends in one of four states: approved, rejected (the document moves to the rejected status, and the session keeps the reason and comment), flagged (handed on for attention) or abandoned (skipped, or the lock expired). A skipped or lapsed session goes back to flagged instead while its document still has an open flag (transition rules in docs-md/architecture/HITL_ARCHITECTURE.md). An abandoned document returns to the Pending queue and the next reviewer gets a fresh session, which opens with the corrections already saved on the document; the history, and ground truth for datasets, span all of a document's sessions.
  • Flagging is a hand-off, not an ending: the flag carries an optional note for the next reviewer, the Flagged tab opens a document read-only without a lock, and Take in that view hands the same session to the reader under their own lock, corrections, note and all. A document can carry several flags, each resolved on its own; Approve and Reject resolve any still open. Editing always holds a lock.
  • Approving is final. It signals the workflow parked at the humanGate, which then runs every node after the gate, so there is nothing to return to and no undo. Rejecting is final too: the reject call marks the document rejected itself, and the workflow's failure hook leaves that status alone. A dataset labeling job is the exception: it drives nothing downstream and its original reviewer can reopen it until the dataset version is frozen.
  • Locks are reclaimed by LockExpiryService, a per-minute cron that abandons the session, deletes the lock row, and audits the expiry. A session whose document still has an open flag goes back to flagged instead, on the same rule as Skip, so an unresolved hand-off keeps its place.
  • Queue statistics are database counts over the whole queue, not a summary of the page in view.

Common Drift Risks ​

  • Queue behavior, locking TTLs, and frontend session handling need to remain aligned.
  • Workflow pause/resume expectations should be checked against current Temporal implementation before changing HITL behavior.
  • Feature docs may lag behind the implemented session model; prefer stable docs-md and code paths.
  • The five rejection reasons are defined twice: the Prisma RejectionReason enum and the frontend's RejectionReason with REJECTION_REASON_LABELS in apps/frontend/src/shared/types. Change both together.