@tangle-network/agent-app 0.44.16 → 0.44.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/work-product/index.d.ts +332 -127
- package/dist/work-product/index.js +262 -63
- package/dist/work-product/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/work-product/service.ts","../../src/work-product/provenance.ts","../../src/work-product/claim-support.ts","../../src/work-product/quote.ts","../../src/work-product/tools.ts","../../src/work-product/route.ts"],"sourcesContent":["/**\n * The guarded work-product status machine — the `/missions` service PATTERN\n * (load → validate against a transition table → CAS-write guarded on what was\n * read → audit event) over {@link WorkProductStorePort}. Deliberately NOT a\n * reuse of the mission service: missions' cursor/plan/budget machinery does\n * not apply here, and the six-status review machine is a different contract.\n *\n * Concurrency contract: a scope's draft is driven by a single serialized turn\n * owner, so real contention is rare. The service is the typed guard layer,\n * not a serializer — every mutation re-reads the record and CAS-writes\n * guarded on the `{status, version}` it read. A guard miss surfaces as\n * `{ succeeded: false, conflict: true }` (retryable: re-read and re-apply),\n * never a silent clobber.\n */\n\nimport {\n unresolvedBlockingExceptions,\n type EvidenceEntry,\n type ExceptionEntry,\n type QualityCheck,\n type WorkProductArtifact,\n type WorkProductAuditEvent,\n type WorkProductPatch,\n type WorkProductProvenance,\n type WorkProductRecord,\n type WorkProductStatus,\n type WorkProductStorePort,\n type WorkProductVersionEntry,\n} from './types'\n\n/** Discriminated outcome for guarded operations — `conflict` distinguishes a\n * lost guarded race (retryable) from a logic rejection (illegal edge,\n * missing row — deterministic, never retried). */\nexport type WorkProductOutcome<T> =\n | { succeeded: true; value: T }\n | { succeeded: false; error: string; conflict: boolean }\n\n// Legal status transitions. A target absent from a source's set is rejected\n// by the guarded helpers. `superseded` is terminal.\nconst WORK_PRODUCT_TRANSITIONS: Record<WorkProductStatus, ReadonlySet<WorkProductStatus>> = {\n draft: new Set<WorkProductStatus>(['blocked', 'ready']),\n blocked: new Set<WorkProductStatus>(['draft']),\n ready: new Set<WorkProductStatus>(['changes_requested', 'approved', 'superseded']),\n changes_requested: new Set<WorkProductStatus>(['draft', 'superseded']),\n approved: new Set<WorkProductStatus>(['superseded']),\n superseded: new Set<WorkProductStatus>(),\n}\n\n/** Whether a legal edge exists from `from` to `to` in the review machine. */\nexport function canTransitionWorkProduct(from: WorkProductStatus, to: WorkProductStatus): boolean {\n return WORK_PRODUCT_TRANSITIONS[from].has(to)\n}\n\n/** Statuses a work product can never leave. */\nexport function isWorkProductTerminal(status: WorkProductStatus): boolean {\n return WORK_PRODUCT_TRANSITIONS[status].size === 0\n}\n\n/** Define the input required to create a new draft work product row */\nexport interface CreateWorkProductInput {\n /** Explicit row id — omit to use the service's generator. The MODEL never\n * supplies ids; this is for deterministic server-side creation. */\n id?: string\n workspaceId: string\n threadId: string | null\n scopeKey: string\n /** Version this draft will become; default 1. The tool layer passes\n * `last reviewed version + 1` when a scope is re-engaged after approval. */\n version?: number\n provenance: WorkProductProvenance\n /** Opaque product-column values handed VERBATIM to the store's insert. */\n extras?: Record<string, unknown>\n}\n\n/** Payload for the draft→ready submit transition */\nexport interface SubmitWorkProductInput {\n artifact: WorkProductArtifact\n checks: QualityCheck[]\n provenance: WorkProductProvenance\n /** Frozen snapshot ref of this version's body for the history entry;\n * defaults to `artifact.path`. */\n artifactPath?: string\n}\n\n/** Reviewer verdict payload for the ready→approved / ready→changes_requested transition */\nexport interface WorkProductVerdictInput {\n verdict: 'approve' | 'request_changes'\n reviewedBy: string\n note?: string\n}\n\n/** Guarded mutation surface over the work-product store */\nexport interface WorkProductService {\n create(input: CreateWorkProductInput): Promise<WorkProductRecord>\n get(id: string): Promise<WorkProductRecord | null>\n /** The scope's open accumulator row (`draft`/`blocked`), or null. */\n openDraft(workspaceId: string, scopeKey: string): Promise<WorkProductRecord | null>\n /** The scope's `changes_requested` row awaiting its correction turn, or null. */\n awaitingCorrection(workspaceId: string, scopeKey: string): Promise<WorkProductRecord | null>\n /** The scope's `ready` row awaiting review, or null. */\n awaitingReview(workspaceId: string, scopeKey: string): Promise<WorkProductRecord | null>\n /** `max(version over the scope's approved/superseded rows) + 1` — the\n * version a fresh draft for the scope should carry. */\n nextVersion(workspaceId: string, scopeKey: string): Promise<number>\n /** Re-open a `changes_requested` row as the next draft: version bumps +1\n * and the correction turn accumulates into the same scope row. */\n reopen(id: string): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Merge evidence entries by id (same id replaces — the agent can correct\n * itself mid-turn). Legal only while `draft`/`blocked`. */\n upsertEvidence(id: string, entries: readonly EvidenceEntry[]): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Merge exception entries by id, then reconcile the blocked flag: any\n * unresolved blocking entry parks `draft`→`blocked`; resolving the last\n * one releases `blocked`→`draft`. */\n upsertExceptions(id: string, entries: readonly ExceptionEntry[]): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Persist a checks array without transitioning — how a failed platform\n * gate (e.g. evidence_coverage) stays visible on the still-draft row. */\n recordChecks(id: string, checks: readonly QualityCheck[]): Promise<WorkProductOutcome<WorkProductRecord>>\n /** The terminal agent call: CAS `draft`→`ready` with artifact + checks +\n * provenance, appending the version-history entry. Refuses while an\n * unresolved blocking exception exists. */\n submit(id: string, input: SubmitWorkProductInput): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Reviewer verdict: CAS `ready`→`approved`/`changes_requested` + history\n * entry. Approval also supersedes the scope's prior approved versions so\n * exactly one approved version is current per scope. */\n applyVerdict(id: string, input: WorkProductVerdictInput): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Explicit replacement: CAS to `superseded` (legal from ready /\n * changes_requested / approved). */\n supersede(id: string): Promise<WorkProductOutcome<WorkProductRecord>>\n}\n\n/** Configuration options for creating a work product service */\nexport interface WorkProductServiceOptions {\n store: WorkProductStorePort\n /** Injectable clock (epoch ms). Default `Date.now`. */\n now?: () => number\n /** Row-id generator. Default `crypto.randomUUID()`. */\n generateId?: () => string\n}\n\nfunction rejected<T>(error: string): WorkProductOutcome<T> {\n return { succeeded: false, error, conflict: false }\n}\n\nfunction lostRace<T>(id: string): WorkProductOutcome<T> {\n return { succeeded: false, error: `Work product ${id} changed concurrently`, conflict: true }\n}\n\n/** Merge-by-id upsert: existing order preserved, replaced in place, new\n * entries appended in call order. */\nfunction mergeById<T extends { id: string }>(existing: readonly T[], incoming: readonly T[]): T[] {\n const merged = existing.slice()\n const indexById = new Map(merged.map((entry, index) => [entry.id, index] as const))\n for (const entry of incoming) {\n const at = indexById.get(entry.id)\n if (at === undefined) {\n indexById.set(entry.id, merged.length)\n merged.push(entry)\n } else {\n merged[at] = entry\n }\n }\n return merged\n}\n\n/** Create the guarded work-product service over a store port */\nexport function createWorkProductService(options: WorkProductServiceOptions): WorkProductService {\n const { store } = options\n const now = options.now ?? (() => Date.now())\n const generateId = options.generateId ?? (() => crypto.randomUUID())\n\n async function appendEvent(\n record: WorkProductRecord,\n step: string,\n message: string,\n metadata: Record<string, unknown> = {},\n ): Promise<void> {\n await store.appendEvent({\n workProductId: record.id,\n workspaceId: record.workspaceId,\n step,\n message,\n metadata,\n at: now(),\n })\n }\n\n // Guarded status transition: load → validate the edge → CAS guarded on the\n // {status, version} read → audit event. The loser of a racing transition\n // gets a conflict instead of silently violating the machine.\n async function transition(\n id: string,\n to: WorkProductStatus,\n patch: Omit<WorkProductPatch, 'status'> = {},\n eventMeta: Record<string, unknown> = {},\n ): Promise<WorkProductOutcome<WorkProductRecord>> {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n const from = record.status\n if (isWorkProductTerminal(from)) {\n return rejected(`Work product ${id} is terminal (${from}); cannot transition to ${to}`)\n }\n if (!canTransitionWorkProduct(from, to)) {\n return rejected(`Illegal work-product transition ${from} -> ${to} for ${id}`)\n }\n const updated = await store.update(\n id,\n { status: from, version: record.version },\n { status: to, updatedAt: now(), ...patch },\n )\n if (!updated) return lostRace(id)\n await appendEvent(updated, `wp.${to}`, `Work product ${from} -> ${to}`, { from, to, ...eventMeta })\n return { succeeded: true, value: updated }\n }\n\n // Merge-style guarded write with no status change (evidence / exceptions /\n // checks accumulate on the open row).\n async function guardedMerge(\n id: string,\n legalStatuses: readonly WorkProductStatus[],\n build: (record: WorkProductRecord) => Omit<WorkProductPatch, 'status' | 'version'>,\n event: { step: string; message: (record: WorkProductRecord) => string; metadata?: (record: WorkProductRecord) => Record<string, unknown> },\n ): Promise<WorkProductOutcome<WorkProductRecord>> {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (!legalStatuses.includes(record.status)) {\n return rejected(`Work product ${id} is ${record.status}; expected ${legalStatuses.join('/')}`)\n }\n const updated = await store.update(\n id,\n { status: record.status, version: record.version },\n { updatedAt: now(), ...build(record) },\n )\n if (!updated) return lostRace(id)\n await appendEvent(updated, event.step, event.message(updated), event.metadata?.(updated) ?? {})\n return { succeeded: true, value: updated }\n }\n\n const create: WorkProductService['create'] = async (input) => {\n const at = now()\n const record = await store.insert(\n {\n id: input.id ?? generateId(),\n workspaceId: input.workspaceId,\n threadId: input.threadId,\n scopeKey: input.scopeKey,\n status: 'draft',\n version: input.version ?? 1,\n artifact: null,\n evidence: [],\n exceptions: [],\n checks: [],\n provenance: input.provenance,\n history: [],\n createdAt: at,\n updatedAt: at,\n },\n input.extras,\n )\n await appendEvent(record, 'wp.created', `Work product draft v${record.version} created for ${record.scopeKey}`, {\n scopeKey: record.scopeKey,\n version: record.version,\n threadId: record.threadId,\n })\n return record\n }\n\n async function findByScopeAndStatus(\n workspaceId: string,\n scopeKey: string,\n status: WorkProductStatus,\n ): Promise<WorkProductRecord | null> {\n const rows = await store.listByWorkspace(workspaceId, { status: [status] })\n return rows.find((row) => row.scopeKey === scopeKey) ?? null\n }\n\n const nextVersion: WorkProductService['nextVersion'] = async (workspaceId, scopeKey) => {\n const reviewed = await store.listByWorkspace(workspaceId, { status: ['approved', 'superseded'] })\n const versions = reviewed.filter((row) => row.scopeKey === scopeKey).map((row) => row.version)\n return versions.length === 0 ? 1 : Math.max(...versions) + 1\n }\n\n const reopen: WorkProductService['reopen'] = async (id) => {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (record.status !== 'changes_requested') {\n return rejected(`Work product ${id} is ${record.status}; only changes_requested reopens`)\n }\n const updated = await store.update(\n id,\n { status: 'changes_requested', version: record.version },\n { status: 'draft', version: record.version + 1, updatedAt: now() },\n )\n if (!updated) return lostRace(id)\n await appendEvent(updated, 'wp.reopened', `Correction draft v${updated.version} opened`, {\n from: record.version,\n to: updated.version,\n })\n return { succeeded: true, value: updated }\n }\n\n const upsertEvidence: WorkProductService['upsertEvidence'] = (id, entries) =>\n guardedMerge(\n id,\n ['draft', 'blocked'],\n (record) => ({ evidence: mergeById(record.evidence, entries) }),\n {\n step: 'wp.evidence',\n message: (record) => `Evidence upserted (${entries.length} entries, ${record.evidence.length} total)`,\n metadata: () => ({ upserted: entries.map((entry) => entry.id) }),\n },\n )\n\n const upsertExceptions: WorkProductService['upsertExceptions'] = async (id, entries) => {\n const merged = await guardedMerge(\n id,\n ['draft', 'blocked'],\n (record) => ({ exceptions: mergeById(record.exceptions, entries) }),\n {\n step: 'wp.exception',\n message: (record) =>\n `Exceptions upserted (${entries.length} entries, ${unresolvedBlockingExceptions(record.exceptions).length} blocking unresolved)`,\n metadata: () => ({ upserted: entries.map((entry) => entry.id) }),\n },\n )\n if (!merged.succeeded) return merged\n // Reconcile the blocked flag AFTER the merge commits: an unresolved\n // blocking entry parks the draft; resolving the last one releases it.\n const record = merged.value\n const blocking = unresolvedBlockingExceptions(record.exceptions).length\n if (record.status === 'draft' && blocking > 0) {\n return transition(id, 'blocked', {}, { blocking })\n }\n if (record.status === 'blocked' && blocking === 0) {\n return transition(id, 'draft', {}, { blocking })\n }\n return merged\n }\n\n const recordChecks: WorkProductService['recordChecks'] = (id, checks) =>\n guardedMerge(\n id,\n ['draft', 'blocked'],\n () => ({ checks: checks.slice() }),\n {\n step: 'wp.checks',\n message: () => `Checks recorded (${checks.length}, ${checks.filter((check) => !check.passed).length} failed)`,\n metadata: () => ({ failed: checks.filter((check) => !check.passed).map((check) => check.name) }),\n },\n )\n\n const submit: WorkProductService['submit'] = async (id, input) => {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (record.status !== 'draft') {\n return rejected(`Work product ${id} is ${record.status}; only a draft submits`)\n }\n const blocking = unresolvedBlockingExceptions(record.exceptions)\n if (blocking.length > 0) {\n return rejected(\n `Work product ${id} has ${blocking.length} unresolved blocking exception(s): ${blocking.map((entry) => entry.id).join(', ')}`,\n )\n }\n const artifactPath = input.artifactPath ?? input.artifact.path\n const entry: WorkProductVersionEntry = {\n version: record.version,\n status: 'ready',\n provenance: input.provenance,\n ...(artifactPath === undefined ? {} : { artifactPath }),\n at: now(),\n }\n return transition(\n id,\n 'ready',\n {\n artifact: input.artifact,\n checks: input.checks.slice(),\n provenance: input.provenance,\n history: [...record.history, entry],\n },\n { version: record.version, failedChecks: input.checks.filter((check) => !check.passed).length },\n )\n }\n\n const applyVerdict: WorkProductService['applyVerdict'] = async (id, input) => {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (record.status !== 'ready') {\n return rejected(`Work product ${id} is ${record.status}; a verdict applies only to ready`)\n }\n const to: WorkProductStatus = input.verdict === 'approve' ? 'approved' : 'changes_requested'\n const entry: WorkProductVersionEntry = {\n version: record.version,\n status: to,\n provenance: record.provenance,\n ...(record.artifact?.path === undefined ? {} : { artifactPath: record.artifact.path }),\n reviewedBy: input.reviewedBy,\n ...(input.note === undefined ? {} : { reviewNote: input.note }),\n at: now(),\n }\n const outcome = await transition(\n id,\n to,\n { history: [...record.history, entry] },\n { verdict: input.verdict, reviewedBy: input.reviewedBy },\n )\n if (!outcome.succeeded || to !== 'approved') return outcome\n // Exactly one approved version per scope: supersede prior approved rows.\n const priorApproved = await store.listByWorkspace(record.workspaceId, { status: ['approved'] })\n for (const prior of priorApproved) {\n if (prior.id === id || prior.scopeKey !== record.scopeKey) continue\n await transition(prior.id, 'superseded', {}, { supersededBy: id })\n }\n return outcome\n }\n\n return {\n create,\n get: (id) => store.load(id),\n openDraft: (workspaceId, scopeKey) => store.findDraft(workspaceId, scopeKey),\n awaitingCorrection: (workspaceId, scopeKey) => findByScopeAndStatus(workspaceId, scopeKey, 'changes_requested'),\n awaitingReview: (workspaceId, scopeKey) => findByScopeAndStatus(workspaceId, scopeKey, 'ready'),\n nextVersion,\n reopen,\n upsertEvidence,\n upsertExceptions,\n recordChecks,\n submit,\n applyVerdict,\n supersede: (id) => transition(id, 'superseded'),\n }\n}\n\n// ── in-memory store ──────────────────────────────────────────────────────────\n\n/** In-memory store surface with audit trail access and unguarded direct writes for tests */\nexport interface InMemoryWorkProductStore extends WorkProductStorePort {\n /** The full audit trail, append order. */\n events(): WorkProductAuditEvent[]\n /** Unguarded direct write — simulates a concurrent owner in tests. */\n put(record: WorkProductRecord): void\n}\n\n/**\n * In-memory {@link WorkProductStorePort} — the portable backend for tests and\n * reference assemblies. Records are deep-copied on every boundary so callers\n * can never mutate stored state around the guards.\n */\nexport function createInMemoryWorkProductStore(): InMemoryWorkProductStore {\n const rows = new Map<string, WorkProductRecord>()\n const events: WorkProductAuditEvent[] = []\n\n return {\n async load(id) {\n const record = rows.get(id)\n return record ? structuredClone(record) : null\n },\n async findDraft(workspaceId, scopeKey) {\n for (const record of rows.values()) {\n if (\n record.workspaceId === workspaceId &&\n record.scopeKey === scopeKey &&\n (record.status === 'draft' || record.status === 'blocked')\n ) {\n return structuredClone(record)\n }\n }\n return null\n },\n async listByWorkspace(workspaceId, opts) {\n const out: WorkProductRecord[] = []\n for (const record of rows.values()) {\n if (record.workspaceId !== workspaceId) continue\n if (opts?.status && !opts.status.includes(record.status)) continue\n out.push(structuredClone(record))\n }\n return out\n },\n async insert(record) {\n if (rows.has(record.id)) throw new Error(`Work product ${record.id} already exists`)\n rows.set(record.id, structuredClone(record))\n return structuredClone(record)\n },\n async update(id, guard, patch) {\n const current = rows.get(id)\n if (!current) return null\n if (guard.status !== undefined && current.status !== guard.status) return null\n if (guard.version !== undefined && current.version !== guard.version) return null\n const next: WorkProductRecord = { ...current }\n if (patch.status !== undefined) next.status = patch.status\n if (patch.version !== undefined) next.version = patch.version\n if (patch.artifact !== undefined) next.artifact = patch.artifact\n if (patch.evidence !== undefined) next.evidence = patch.evidence\n if (patch.exceptions !== undefined) next.exceptions = patch.exceptions\n if (patch.checks !== undefined) next.checks = patch.checks\n if (patch.provenance !== undefined) next.provenance = patch.provenance\n if (patch.history !== undefined) next.history = patch.history\n if (patch.updatedAt !== undefined) next.updatedAt = patch.updatedAt\n rows.set(id, structuredClone(next))\n return structuredClone(next)\n },\n async appendEvent(event) {\n events.push(structuredClone(event))\n },\n events() {\n return events.map((event) => structuredClone(event))\n },\n put(record) {\n rows.set(record.id, structuredClone(record))\n },\n }\n}\n","/**\n * Provenance stamping — the backtest spine. Every version of every work\n * product carries `profileHash + runId + servingModels`, stamped at the two\n * honest moments:\n *\n * 1. At dispatch, the product route composes the turn's profile and computes\n * agent-eval's `agentProfileHash(profile)` — the SAME hash that keys the\n * app's scorecard cells — and closes it (plus runId/sessionId) into the\n * `provenance` seam `buildWorkProductTools` receives, so the model can\n * neither omit nor forge it.\n * 2. At turn completion, the route's existing lifecycle seam calls\n * {@link finalizeWorkProductProvenance}: serving model and cost come from\n * the usage receipt, because only the completed turn knows what actually\n * served. `servingModels` is honestly EMPTY until then.\n *\n * The trust bridge ({@link workProductTrustInputs}) maps judge-sourced\n * quality verdicts into `/eval-campaign`'s `trustVerdicts()` input with\n * type-only imports — products call `trustVerdicts` from\n * `@tangle-network/agent-app/eval-campaign` (which they already import for\n * their ensemble loop). Deliberately NOT a value re-export here: it would put\n * agent-eval's runtime on `/work-product`'s import path and drag the eval\n * engine into every product worker bundle.\n */\n\nimport type { JudgeVerdict } from '@tangle-network/agent-eval'\nimport type { TrustItem } from '../eval-campaign/trust-gate'\nimport type {\n WorkProductProvenance,\n WorkProductRecord,\n WorkProductStorePort,\n} from './types'\n\nexport type { TrustItem }\n\n/** The dispatch-time provenance closure's output: everything the route knows\n * before the turn completes. */\nexport type WorkProductProvenanceBase = Omit<WorkProductProvenance, 'servingModels' | 'producedAt'>\n\n/** Stamp a full provenance from the dispatch-time base. `servingModels` starts\n * empty (honestly absent — never guessed) until the completion back-fill. */\nexport function stampProvenance(base: WorkProductProvenanceBase, now: () => number = Date.now): WorkProductProvenance {\n return { ...base, servingModels: [], producedAt: now() }\n}\n\n/** Completion receipt for one run, from the turn's usage/lifecycle seam. */\nexport interface FinalizeWorkProductProvenanceInput {\n workspaceId: string\n /** The chat turnId / mission-step run id the records were stamped with. */\n runId: string\n /** What actually served, from the usage receipt / serving-model header. */\n servingModels: readonly string[]\n costUsd?: number\n logger?: Pick<Console, 'warn'>\n}\n\n/**\n * Back-fill `servingModels`/`costUsd` onto every record (and its history\n * entries) stamped with `runId` — wire it into the chat route's existing\n * `lifecycle.onTurnComplete` seam; no new hook. Returns the updated records.\n * A CAS miss on one record is logged and skipped (the next completion or a\n * re-read re-applies); it never throws mid-fleet.\n */\nexport async function finalizeWorkProductProvenance(\n store: WorkProductStorePort,\n input: FinalizeWorkProductProvenanceInput,\n): Promise<WorkProductRecord[]> {\n const rows = await store.listByWorkspace(input.workspaceId)\n const updated: WorkProductRecord[] = []\n for (const record of rows) {\n const recordMatches = record.provenance.runId === input.runId\n const historyMatches = record.history.some((entry) => entry.provenance.runId === input.runId)\n if (!recordMatches && !historyMatches) continue\n const finalize = (provenance: WorkProductProvenance): WorkProductProvenance => ({\n ...provenance,\n servingModels: [...input.servingModels],\n ...(input.costUsd === undefined ? {} : { costUsd: input.costUsd }),\n })\n const next = await store.update(\n record.id,\n { status: record.status, version: record.version },\n {\n ...(recordMatches ? { provenance: finalize(record.provenance) } : {}),\n history: record.history.map((entry) =>\n entry.provenance.runId === input.runId ? { ...entry, provenance: finalize(entry.provenance) } : entry,\n ),\n },\n )\n if (!next) {\n input.logger?.warn(`[work-product] provenance back-fill lost a race on ${record.id}; skipped`)\n continue\n }\n await store.appendEvent({\n workProductId: record.id,\n workspaceId: record.workspaceId,\n step: 'wp.provenance',\n message: `Serving models back-filled for run ${input.runId}`,\n metadata: { runId: input.runId, servingModels: [...input.servingModels], costUsd: input.costUsd ?? null },\n at: Date.now(),\n })\n updated.push(next)\n }\n return updated\n}\n\n/**\n * Trust-gate bridge: one `TrustItem` per work product whose production\n * quality was scored by the product's eval-campaign ensemble. `verdictsFor`\n * returns the per-judge raw verdicts the product retained for a record (the\n * same verdicts `aggregateJudgeVerdicts` reduces); records without verdicts\n * are omitted. Feed the result to `/eval-campaign`'s `trustVerdicts()` —\n * an untrusted verdict renders as \"quality: unverified\", never a naked\n * number. Zero statistics code here: pure mapping.\n */\nexport function workProductTrustInputs<D extends string = string>(\n records: readonly WorkProductRecord[],\n verdictsFor: (record: WorkProductRecord) => readonly JudgeVerdict<D>[] | undefined,\n): TrustItem<D>[] {\n const items: TrustItem<D>[] = []\n for (const record of records) {\n const verdicts = verdictsFor(record)\n if (!verdicts || verdicts.length === 0) continue\n items.push({ itemId: record.id, verdicts })\n }\n return items\n}\n","/**\n * Claim support — does the anchored text actually say what the entry claims?\n *\n * The two gates before this one answer different questions. `sourceContainsQuote`\n * asks whether the quote is really in the document; `findSourceLine` /\n * `sliceSourceSpan` make the platform produce the quote so it cannot be typed\n * wrong. Both are about the TEXT's provenance. Neither one looks at `claim`.\n *\n * Production row `7256ef49` is what that gap costs. Four evidence entries, all\n * `quoteBasis:'span'`, every quote a genuine slice of the document it named —\n * and every one landing on the employer/payer line about 200 characters above\n * the figure:\n *\n * claim 128450.00 -> \"tics LLC EIN 84-2213907\\nEmployee: Dana\"\n * claim 812.44 -> \"nt Savings Bank TIN 22-5510983\\nRecip\"\n * claim 2204.18 -> \"ndex Fund Trust TIN 47-3320115\\nRec\"\n * claim 1955.02 -> \"pient: Dana R. Whitfield\\n------------\"\n *\n * That is strictly worse than a fabricated quote. A fabricated quote fails the\n * verbatim gate; this one passes every gate, is real text from the right\n * document, and reads to a reviewer as an authoritative citation while\n * supporting nothing. `locator.find` (the platform locating a value the model\n * names) prevents the model from ADDRESSING the wrong line, and it is the right\n * primary fix. This module is the independent check underneath it: whatever\n * anchoring form produced the text, the text has to contain the figure.\n *\n * That also settles what a raw `locator.span` is worth. Hand-computed offsets\n * stay accepted, but only when they independently verify — which is exactly\n * \"the slice contains the claimed value\".\n *\n * ── The rule, and why it is drawn here ──────────────────────────────────────\n *\n * Strict on figures, silent on everything else. An unsatisfiable gate does not\n * stop a bad submit, it SELECTS for one — that is the measured mechanism behind\n * the 38 fabricated quotes on row `a68b1943`, where a coverage gate demanded\n * document lineage for computed values and got thirteen invented citations\n * forty seconds later. So the rule fires only where an honest citation can\n * always satisfy it:\n *\n * - The claim IS a value (`\"128450.00\"`, `\"$30,000\"`, `\"3\"`) — the anchored\n * text MUST contain that value. No latitude. This is the shape every tax\n * figure takes and the shape row `7256ef49` failed.\n * - The claim is prose naming figures (`\"indemnity capped at $5,000,000\"`) —\n * at least one of its currency-shaped figures must occur. Not all of them:\n * a claim may legitimately narrate a computation over several lines while\n * anchoring to the one line under discussion, and refusing that would make\n * an honest citation unrepresentable for the sake of a stricter-sounding\n * rule. One is enough to keep the anchor tethered to the claim's subject.\n * - The claim carries no figure at all (`\"Married Filing Jointly\"`,\n * `\"Dana R. Whitfield\"`, `\"2025-04-15\"`) — nothing to check, and the entry\n * passes. A filing status has no number to find, and inventing a\n * word-overlap score here would re-open the hole the verbatim gate closes.\n * - The entry has no anchored text at all — nothing to check. A computed\n * value cites its computation, not a document.\n *\n * A bare year, a form number and a box number are deliberately NOT figures:\n * `2025`, `1040` and `Box 1` have no thousands separator, no cent pair and no\n * currency symbol, so prose mentioning them does not trip the rule.\n *\n * ── Matching is value-wise, never substring ─────────────────────────────────\n *\n * `text.includes(claim)` would be the obvious implementation and it is wrong in\n * the direction that matters: it passes `\"450.00\"` against `\"128,450.00\"`, so a\n * claim citing the wrong figure survives whenever its digits happen to be a tail\n * of a real one. Both sides are tokenized into numbers and compared as VALUES,\n * so `450` and `128450` are simply different.\n */\n\n/** Currency marks stripped before a token is read as a number. */\nconst CURRENCY = /[$€£¥₹]/gu\n\n/**\n * Numbers as they occur in document text. The grouped form is tried first so\n * `128,450.00` is read as one value rather than `128` followed by `450.00` —\n * the whole point of comparing values instead of substrings.\n *\n * A leading `[$€£¥₹]?\\s*` is absorbed so `$ 1,200` tokenizes once, and the\n * dot-leaders typical of a form line (`Box 1 Wages ..... 128,450.00`) are not\n * digits, so they never join a token.\n */\nconst NUMBER_IN_TEXT = /[$€£¥₹]?\\s*\\d{1,3}(?:,\\d{3})+(?:\\.\\d+)?|[$€£¥₹]?\\s*\\d+(?:\\.\\d+)?/gu\n\n/**\n * A figure inside prose: a currency symbol, OR thousands separators, OR a cent\n * pair. Each of the three is a positive signal that the writer meant a\n * quantity rather than an identifier, which is what keeps `2025`, `1040` and\n * `Box 1` out.\n */\nconst FIGURE_IN_PROSE =\n /[$€£¥₹]\\s*[-+]?\\d[\\d,]*(?:\\.\\d+)?|[-+]?\\d{1,3}(?:,\\d{3})+(?:\\.\\d+)?|[-+]?\\d+\\.\\d{2}(?!\\d)/gu\n\n/** The whole string is one value: optional currency, sign, digits with\n * optional grouping and decimals, optional percent. Anchored, so an EIN\n * (`84-2213907`), a date (`2025-04-15`) and a range (`10-20`) are NOT values. */\nconst WHOLE_VALUE = /^[$€£¥₹]?\\s*[-+]?\\s*(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d+)?\\s*%?$/u\n\n/**\n * Reduce a numeric token to the form both sides are compared in: no currency,\n * no grouping, no trailing zeros in the fraction, no leading zeros.\n *\n * Sign and accounting parentheses are stripped rather than preserved, so\n * `(1,234.00)`, `-1,234.00` and `1,234.00` all reduce to `1234`. A document\n * renders the same deduction all three ways depending on the form and the\n * extractor, and the sign is a property of the TARGET LINE's semantics, not of\n * the document's typography. Comparing magnitudes keeps those honest citations\n * working, and it concedes nothing to fabrication: the digits still have to be\n * the document's digits.\n *\n * Returns `null` for anything that is not a plain number.\n */\nexport function canonicalizeValue(token: string): string | null {\n let text = token.trim()\n if (text.length === 0) return null\n if (/^\\(.*\\)$/u.test(text)) text = text.slice(1, -1).trim()\n text = text.replace(CURRENCY, '').trim()\n text = text.replace(/^[-+]\\s*/u, '').trim()\n text = text.replace(/%$/u, '').trim()\n if (!/^(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d+)?$/u.test(text)) return null\n text = text.replace(/,/gu, '')\n if (text.includes('.')) text = text.replace(/0+$/u, '').replace(/\\.$/u, '')\n text = text.replace(/^0+(?=\\d)/u, '')\n return text.length === 0 ? null : text\n}\n\n/** Every distinct value appearing in `text`, canonicalized. */\nexport function valuesInText(text: string): string[] {\n const seen = new Set<string>()\n for (const match of text.matchAll(NUMBER_IN_TEXT)) {\n const canonical = canonicalizeValue(match[0])\n if (canonical !== null) seen.add(canonical)\n }\n return [...seen]\n}\n\n/**\n * The values a claim asserts, canonicalized — empty when the claim asserts no\n * figure, which is the \"nothing to check\" case.\n *\n * A claim that is ENTIRELY one value yields that value (strict path). Otherwise\n * only currency-shaped figures inside the prose count, so an assertion that\n * merely mentions a year or a form number yields nothing.\n */\nexport function claimValues(claim: string): string[] {\n const trimmed = claim.trim()\n if (WHOLE_VALUE.test(trimmed)) {\n const whole = canonicalizeValue(trimmed)\n if (whole !== null) return [whole]\n }\n const seen = new Set<string>()\n for (const match of trimmed.matchAll(FIGURE_IN_PROSE)) {\n const canonical = canonicalizeValue(match[0])\n if (canonical !== null) seen.add(canonical)\n }\n return [...seen]\n}\n\nexport type ClaimSupport =\n /** No figure to check: a non-numeric claim, or no anchored text. */\n | { status: 'not_applicable' }\n | { status: 'supported'; matched: string }\n | { status: 'unsupported'; claimed: string[]; present: string[] }\n\n/**\n * Does `quote` carry the figure `claim` asserts?\n *\n * `supported` requires ONE claimed value to occur, which is exact for the\n * single-value claim (there is only one) and deliberate latitude for prose (see\n * the rule note at the top of the file).\n */\nexport function verifyClaimSupport(quote: string, claim: string): ClaimSupport {\n if (quote.trim().length === 0) return { status: 'not_applicable' }\n const claimed = claimValues(claim)\n if (claimed.length === 0) return { status: 'not_applicable' }\n const present = valuesInText(quote)\n const matched = claimed.find((value) => present.includes(value))\n if (matched !== undefined) return { status: 'supported', matched }\n return { status: 'unsupported', claimed, present }\n}\n\n/** Short, quotable rendering of the anchored text for an error message. */\nfunction excerpt(quote: string, limit = 120): string {\n const flat = quote.replace(/\\s+/gu, ' ').trim()\n return flat.length <= limit ? flat : `${flat.slice(0, limit)}…`\n}\n\n/**\n * The sentence a model can act on without re-reading the document: what it\n * claimed, what the line it cited actually says, what figures that line does\n * carry, and the two ways out — cite the value, or drop the locator because the\n * figure was computed. Naming both keeps the gate satisfiable, which is the\n * property that stops it manufacturing the citation it screens for.\n */\nexport function claimSupportErrorDetail(\n failure: Extract<ClaimSupport, { status: 'unsupported' }>,\n quote: string,\n): string {\n const wanted = failure.claimed.length === 1 ? failure.claimed[0]! : `any of ${failure.claimed.join(', ')}`\n const carries =\n failure.present.length === 0\n ? 'that line carries no figure at all'\n : `the only figures on it are ${failure.present.join(', ')}`\n return `the cited text does not contain ${wanted}. It reads \"${excerpt(quote)}\", and ${carries}. Cite locator.find with the value exactly as it appears in the document and the platform will locate the right line for you. If this figure was COMPUTED rather than read from the document, omit the locator entirely and state the computation in claim.`\n}\n","/**\n * Verbatim quote verification — the lineage gate.\n *\n * An evidence `locator.quote` is the reviewer's click target: it must land on\n * text that is actually in the document the entry names. Nothing here is\n * fuzzy. The only latitude is representational — the same characters written\n * differently by a PDF extractor, a text transcription, or a model repeating\n * what it read: Unicode compatibility forms, curly quotes, the several dash\n * codepoints, non-breaking spaces, and run-length whitespace. A quote that\n * still does not occur after that is not a formatting difference; it is text\n * the document does not contain.\n *\n * Deliberately NOT tolerated, because each one re-opens the hole this closes:\n * case (a quote is a quote), token subsets, edit distance, word-overlap\n * scoring, and \"the numbers match\" heuristics. A fabricated quote and a real\n * one differ by exactly the thing a fuzzy matcher forgives.\n */\n\nimport { canonicalizeValue, valuesInText } from './claim-support'\n\n/** Whitespace folded to a single ASCII space: the Unicode space separators\n * (`\\p{Zs}`), the line/paragraph separators, and the zero-width characters a\n * PDF text layer leaves behind (ZWSP / ZWNJ / ZWJ / BOM). */\nconst WHITESPACE = /[\\s\\p{Zs}\\u2028\\u2029\\u200b-\\u200d\\ufeff]+/gu\n\n/** Dash-like codepoints that render alike but differ by byte: non-breaking\n * hyphen, figure / en / em / horizontal dash, minus sign, small and fullwidth\n * forms. */\nconst DASHES = /[\\u2010-\\u2015\\u2212\\ufe58\\ufe63\\uff0d]/gu\n\n/** Curly, prime and grave apostrophes folded to ASCII `'`. */\nconst SINGLE_QUOTES = /[\\u2018\\u2019\\u201a\\u201b\\u2032\\u2035\\u00b4`]/gu\n\n/** Curly, low, prime and guillemet double quotes folded to ASCII `\"`. */\nconst DOUBLE_QUOTES = /[\\u201c\\u201d\\u201e\\u201f\\u2033\\u2036\\u00ab\\u00bb]/gu\n\n/**\n * Fold a string to the form both sides of a quote comparison are measured in.\n * Composition and presentation only: NFKC, one dash, one apostrophe, one\n * double quote, runs of any whitespace to a single space, trimmed. Case is\n * PRESERVED — lowercasing would let \"Box 1\" match \"box 1\", and a citation\n * that cannot reproduce capitalization did not read the document.\n */\nexport function normalizeQuoteText(value: string): string {\n return value\n .normalize('NFKC')\n .replace(DASHES, '-')\n .replace(SINGLE_QUOTES, \"'\")\n .replace(DOUBLE_QUOTES, '\"')\n .replace(WHITESPACE, ' ')\n .trim()\n}\n\n/**\n * Does `quote` occur in `sourceText`? Exact substring first (the common case,\n * and free); the normalized comparison second, for the representational\n * differences above. Nothing else.\n *\n * An empty or whitespace-only quote is NOT a match — it would otherwise be a\n * substring of every document and pass the gate vacuously.\n */\nexport function sourceContainsQuote(sourceText: string, quote: string): boolean {\n const trimmed = quote.trim()\n if (trimmed.length === 0) return false\n if (sourceText.includes(trimmed)) return true\n const normalizedQuote = normalizeQuoteText(quote)\n if (normalizedQuote.length === 0) return false\n return normalizeQuoteText(sourceText).includes(normalizedQuote)\n}\n\n// ── spans: the citation form that cannot be wrong ────────────────────────────\n\n/**\n * Slice a citation out of the source text by character offset — the reason\n * this module exists in its stronger form.\n *\n * Verification above is a REJECTION gate: the model retypes a quote and the\n * shell refuses it when the characters do not occur. That gate is correct and\n * it works, but a model that reproduces a line character-for-character only\n * some of the time cannot USE it — every miss is a refusal, and the package\n * ends up with no lineage at all rather than false lineage. A measured 9 of 59\n * on the live tax surface is what \"some of the time\" meant in practice.\n *\n * A span inverts it. The model names two integers into text it just read; the\n * PLATFORM produces the quote from the bytes it already holds. There is no\n * retyping step to get wrong, so a fabricated quote is not rejected — it is\n * unrepresentable. `sourceContainsQuote(text, sliceSourceSpan(text, span))` is\n * true for every span this function returns, by construction.\n *\n * Offsets index the SAME string the product's document-reading tool pages\n * with an offset, which is the same string its `readSourceText` seam returns.\n * That is the one contract a product must keep; violate it and spans point at\n * the wrong characters (still real characters of that document — never\n * invented text, but the wrong line).\n *\n * Half-open `[start, end)`, matching `String.prototype.slice` and the\n * `offset`/`offset + text.length` window a paged read already reports.\n */\nexport type SourceSpanFailure =\n | { reason: 'not_integer'; field: 'start' | 'end' }\n | { reason: 'negative'; field: 'start' | 'end' }\n | { reason: 'inverted' }\n | { reason: 'out_of_range'; totalChars: number }\n | { reason: 'blank' }\n\nexport type SourceSpanResult =\n | { ok: true; quote: string }\n | { ok: false; failure: SourceSpanFailure }\n\n/**\n * Locate the line containing `value` and return it as a span — the citation\n * form for a model that cannot count characters.\n *\n * Measured on production (tax session 135b7cc3, gpt-4.1-mini): given the\n * document text and told exactly which line to cite, the model produced\n * offsets that landed on the WRONG line four times out of four, then missed\n * again on a second attempt after being shown the text its offsets had\n * selected. Character arithmetic is not something this model class does.\n *\n * What it DOES do reliably is read the value: all four `claim` fields in that\n * same run were correct to the cent. So the model names the value it read and\n * the PLATFORM finds it. Both failure modes close at once —\n *\n * - the quote cannot be invented, because the platform slices it;\n * - the span cannot be mis-addressed, because the platform computed it from\n * a needle it PROVED occurs in the text.\n *\n * A needle that is not in the document is refused, which is the same fail-loud\n * posture as a quote that does not occur — and correctly so: the model is\n * asserting the document says something it does not.\n *\n * The cited span is the whole LINE, not the needle: \"128,450.00\" alone is not\n * a click target a reviewer can judge, whereas the line it sits on says what\n * the number IS. Number-only needles are common and deliberately supported.\n */\nexport type SourceFindFailure =\n | { reason: 'blank_needle' }\n | { reason: 'not_found' }\n | { reason: 'occurrence_out_of_range'; found: number }\n | { reason: 'not_distinctive'; needle: string; found: number }\n\n/** A needle must be long enough to identify a place in the document. Two\n * characters is not a citation: measured on production work product\n * b9a37e44, nine evidence entries for lines the 1099-DIV does not state were\n * cited with the value \"0\", and the platform faithfully matched the \"0\"\n * inside \"Tax Year 2025\" in the header. Every one re-sliced byte-exactly and\n * every one was worthless as evidence. */\nconst MIN_NEEDLE_LENGTH = 3\n\n/** Above this many hits the needle names no particular place, so citing the\n * first is arbitrary rather than evidential. `findOccurrence` remains the way\n * to cite a genuinely repeated value on purpose. */\nconst MAX_AMBIGUOUS_MATCHES = 8\n\nexport type SourceFindResult =\n | { ok: true; span: { start: number; end: number }; quote: string; occurrences: number }\n | { ok: false; failure: SourceFindFailure }\n\n/** Line bounds containing `index`, trimmed of the newline terminators. */\nfunction lineAround(text: string, index: number): { start: number; end: number } {\n let start = text.lastIndexOf('\\n', index)\n start = start < 0 ? 0 : start + 1\n let end = text.indexOf('\\n', index)\n if (end < 0) end = text.length\n // A CRLF document leaves a trailing \\r inside the line; drop it so the\n // quote is the line a reader sees rather than the line plus a control char.\n if (end > start && text[end - 1] === '\\r') end -= 1\n return { start, end }\n}\n\nexport function findSourceLine(\n sourceText: string,\n needle: string,\n occurrence = 1,\n): SourceFindResult {\n const trimmed = needle.trim()\n if (trimmed.length === 0) return { ok: false, failure: { reason: 'blank_needle' } }\n if (trimmed.length < MIN_NEEDLE_LENGTH) {\n return { ok: false, failure: { reason: 'not_distinctive', needle: trimmed, found: 0 } }\n }\n\n /** Walk the document a line at a time. */\n const eachLine = (visit: (bound: { start: number; end: number }, line: string) => void): void => {\n let cursor = 0\n while (cursor <= sourceText.length) {\n const bound = lineAround(sourceText, cursor)\n visit(bound, sourceText.slice(bound.start, bound.end))\n if (bound.end >= sourceText.length) break\n cursor = bound.end + 1\n }\n }\n\n const positions: number[] = []\n\n // A needle that IS a figure is matched BY VALUE, per line — never as a\n // substring. Substring matching quietly answers the wrong question here: a\n // model citing a genuine \"450.00\" line in a document that also carries\n // \"128,450.00\" gets the WAGE line back, because \"450.00\" is a tail of it and\n // occurs earlier. The citation then fails claim support and the honest\n // anchor is unreachable at occurrence 1, which is the unsatisfiable-gate\n // failure this whole area exists to avoid. Matching values also subsumes the\n // representation cases the old second-chance pass handled — \"128450.00\"\n // finds \"128,450.00\" because both canonicalize to the same number.\n const wantedValue = canonicalizeValue(trimmed)\n if (wantedValue !== null) {\n eachLine((bound, line) => {\n if (valuesInText(line).includes(wantedValue)) positions.push(bound.start)\n })\n } else {\n // A PHRASE needle. Exact hits first (free, and the common case), then the\n // normalized comparison ONLY to locate a position — the returned quote is\n // always sliced from the ORIGINAL string, so representation folding never\n // leaks into a stored citation.\n for (let at = sourceText.indexOf(trimmed); at >= 0; at = sourceText.indexOf(trimmed, at + 1)) {\n positions.push(at)\n }\n if (positions.length === 0) {\n const wanted = normalizeQuoteText(trimmed)\n if (wanted.length > 0) {\n eachLine((bound, line) => {\n if (normalizeQuoteText(line).includes(wanted)) positions.push(bound.start)\n })\n }\n }\n }\n if (positions.length === 0) return { ok: false, failure: { reason: 'not_found' } }\n // An explicit `findOccurrence` is the caller saying \"yes, it repeats, I mean\n // that one\" — so ambiguity is only a failure when they did NOT say which.\n if (occurrence === 1 && positions.length > MAX_AMBIGUOUS_MATCHES) {\n return { ok: false, failure: { reason: 'not_distinctive', needle: trimmed, found: positions.length } }\n }\n if (occurrence < 1 || occurrence > positions.length) {\n return { ok: false, failure: { reason: 'occurrence_out_of_range', found: positions.length } }\n }\n\n const bound = lineAround(sourceText, positions[occurrence - 1]!)\n const quote = sourceText.slice(bound.start, bound.end)\n if (quote.trim().length === 0) return { ok: false, failure: { reason: 'not_found' } }\n return { ok: true, span: bound, quote, occurrences: positions.length }\n}\n\n/** Resolve `[start, end)` against `sourceText`. Every rejection is a caller\n * mistake the model can correct from the paged read it already has, so each\n * carries the discriminator a tool layer turns into a specific message. */\nexport function sliceSourceSpan(\n sourceText: string,\n span: { start: number; end: number },\n): SourceSpanResult {\n for (const field of ['start', 'end'] as const) {\n const value = span[field]\n if (!Number.isInteger(value)) return { ok: false, failure: { reason: 'not_integer', field } }\n if (value < 0) return { ok: false, failure: { reason: 'negative', field } }\n }\n if (span.end <= span.start) return { ok: false, failure: { reason: 'inverted' } }\n if (span.end > sourceText.length) {\n return { ok: false, failure: { reason: 'out_of_range', totalChars: sourceText.length } }\n }\n const quote = sourceText.slice(span.start, span.end)\n // A whitespace-only slice is a real slice of the document and would pass\n // `sourceContainsQuote` on any text — the same vacuous pass an empty quote\n // gets there, refused for the same reason: it is not a click target.\n if (quote.trim().length === 0) return { ok: false, failure: { reason: 'blank' } }\n return { ok: true, quote }\n}\n","/**\n * The agent-facing work-product side channel — three registry `customTools`\n * built on `/tools`' `defineAppTool`, dispatched through `dispatchAppTool`'s\n * single validation/outcome path (a thrown `ToolInputError` → correctable 4xx\n * back to the model; any other throw → internal error; a call never silently\n * succeeds without its effect). Deliberately NOT new `/tools` built-ins:\n * extending `AppToolHandlers` would break every existing consumer, and the\n * registry seam exists precisely for a product tool family like this.\n *\n * Partial-emission contract across a long turn: the draft ROW is the\n * accumulator. Evidence and exceptions stream in as found via small batched\n * calls; the artifact arrives once at the end. The agent addresses everything\n * by `scopeKey` — the server mints row ids, the model never invents them, and\n * identity (userId/workspaceId/threadId) rides the trusted `AppToolContext`\n * from headers, never model args. A turn that dies mid-emission leaves a\n * consistent draft the next turn resumes by the same scopeKey.\n */\n\nimport { ToolInputError } from '../tools/errors'\nimport { defineAppTool, type AppToolDefinition } from '../tools/registry'\nimport type { AppToolContext } from '../tools/types'\nimport { createWorkProductService, type WorkProductOutcome, type WorkProductService } from './service'\nimport { stampProvenance, type WorkProductProvenanceBase } from './provenance'\nimport { findSourceLine, sliceSourceSpan, sourceContainsQuote, type SourceFindFailure, type SourceSpanFailure } from './quote'\nimport { claimSupportErrorDetail, verifyClaimSupport } from './claim-support'\nimport {\n parseAgentCheckInput,\n parseArtifactInput,\n parseEvidenceInput,\n parseExceptionInput,\n unresolvedBlockingExceptions,\n type EvidenceEntry,\n type ExceptionEntry,\n type QualityCheck,\n type WorkProductArtifact,\n type WorkProductRecord,\n type WorkProductStorePort,\n} from './types'\n\n/** Max entries per `upsert_evidence`/`flag_exception` call — keeps each call a\n * small batch the model can correct precisely on a named-index failure. */\nexport const MAX_WORK_PRODUCT_BATCH = 50\n\n/** Platform check: every material target has ≥1 evidence row. Recorded as\n * `QualityCheck{source:'platform'}`. */\nexport const EVIDENCE_COVERAGE_CHECK = 'evidence_coverage'\n\n/** Platform check: how many evidence entries carry a quote the shell PROVED\n * occurs in the source it names. Recorded when `readSourceText` is wired, so\n * a reviewer reads the strength of the lineage off the row itself rather than\n * trusting that a quote was checked. */\nexport const QUOTE_VERIFICATION_CHECK = 'quote_verification'\n\n/** Platform check: how many quoted evidence entries anchor to text that\n * actually CARRIES the figure the entry claims. Distinct from\n * `quote_verification`, which only proves the text came from the document —\n * production row `7256ef49` passed that one on all four entries while\n * supporting none of them. */\nexport const CLAIM_SUPPORT_CHECK = 'claim_support'\n\n/** Domain seams for the three work-product tools — every domain word is a\n * parameter; the shell bakes none. */\nexport interface WorkProductToolConfig {\n store: WorkProductStorePort\n /** PARAMETER — accepted `artifact.kind` values, validated on submit. */\n artifactKinds: readonly string[]\n /** PARAMETER — accepted exception `kind` values. */\n exceptionKinds: readonly string[]\n /** Fail-loud source check: resolve an evidence `sourceRef` to existence\n * (vault stat / attachment lookup). A dangling ref is a `ToolInputError`\n * naming the entry index — lineage can never point at nothing. */\n resolveSourceRef: (ref: string, ctx: AppToolContext) => Promise<boolean>\n /** Fail-loud QUOTE verification: return the source document's TEXT for a\n * ref so the shell can prove each `locator.quote` occurs in it verbatim.\n * Wiring this turns the gate ON — a quote that does not occur is a\n * `ToolInputError` naming the entry index, so the model re-extracts from\n * the document instead of persisting invented lineage.\n *\n * Return `null` ONLY when the ref genuinely has no extractable text (an\n * image scan, an opaque blob). The gate is fail-CLOSED on `null`: a quote\n * that cannot be checked is refused, because \"unverifiable\" and \"verified\"\n * must never look the same to a reviewer. Such an entry is still recordable\n * without `locator.quote` — `claim` carries the assertion.\n *\n * Omit the seam entirely and no quote is checked. */\n readSourceText?: (ref: string, ctx: AppToolContext) => Promise<string | null>\n /** The material targets the platform coverage check requires evidence for.\n * Product-owned vocabulary; omit to skip the coverage gate.\n *\n * Return ONLY targets a source document can actually evidence. A gate that\n * demands document lineage for a value the session COMPUTED is not\n * satisfiable by any honest answer, and an unsatisfiable gate does not stop\n * a submit — it selects for an invented one. Computed values belong to a\n * product's own computation check, where \"matches what we already\n * calculated\" is satisfiable by construction. */\n materialTargets?: (artifact: WorkProductArtifact) => string[]\n /** Require every material target to carry a SOURCE ANCHOR — a span-sliced or\n * verified quote — not merely an evidence row. Off by default (a bare\n * `claim` is legitimate lineage for products with no readable sources);\n * turn it ON once `readSourceText` is wired and `materialTargets` names only\n * document-derived targets, and coverage stops being satisfiable by\n * assertion. */\n requireAnchoredEvidence?: boolean\n /** Verify that each anchored quote CARRIES the figure its entry claims.\n * ON by default — an anchor that does not support its claim is the one\n * failure mode every other gate here passes, and it reads to a reviewer as\n * the most authoritative citation on the row.\n *\n * Only claims that assert a figure are checked, so a filing status, a name\n * or a date is unaffected; see `./claim-support` for exactly where the line\n * is drawn and why it is drawn to stay satisfiable. Set `false` only for a\n * product whose claims are figures the source states in a form no numeric\n * comparison can reach. */\n verifyClaimSupport?: boolean\n /** Per-turn provenance closure the ROUTE supplies (profileHash + runId are\n * known at dispatch; trusted, never read from model args). */\n provenance: (ctx: AppToolContext) => WorkProductProvenanceBase\n /** Called on the draft→ready commit so the route persists the transcript\n * anchor part and the queue projection updates. */\n onReady?: (record: WorkProductRecord, ctx: AppToolContext) => void | Promise<void>\n /** Injectable clock / id generator (tests, deterministic ids). */\n now?: () => number\n generateId?: () => string\n}\n\n/** Unwrap a guarded outcome, retrying ONCE on a lost race (the single\n * serialized turn owner makes real contention rare; one re-read-and-retry\n * absorbs the benign case). A deterministic rejection maps to a correctable\n * `ToolInputError` so the model learns exactly why. */\nasync function unwrap<T>(\n run: () => Promise<WorkProductOutcome<T>>,\n code: string,\n): Promise<T> {\n let outcome = await run()\n if (!outcome.succeeded && outcome.conflict) outcome = await run()\n if (!outcome.succeeded) throw new ToolInputError(code, outcome.error, outcome.conflict ? 409 : 400)\n return outcome.value\n}\n\nfunction requireScopeKey(args: Record<string, unknown>): string {\n const scopeKey = typeof args.scopeKey === 'string' ? args.scopeKey.trim() : ''\n if (!scopeKey) throw new ToolInputError('missing_scope_key', 'scopeKey is required — the engagement key this work product belongs to.')\n return scopeKey\n}\n\nfunction requireBatch(args: Record<string, unknown>, field: string): unknown[] {\n const raw = args[field]\n if (!Array.isArray(raw) || raw.length === 0) {\n throw new ToolInputError('missing_entries', `${field} must be a non-empty array.`)\n }\n if (raw.length > MAX_WORK_PRODUCT_BATCH) {\n throw new ToolInputError('batch_too_large', `${field} accepts at most ${MAX_WORK_PRODUCT_BATCH} entries per call — send smaller batches.`)\n }\n return raw\n}\n\n/**\n * Resolve the scope's open draft row, creating or reopening as needed:\n * an open `draft`/`blocked` row resumes; a `changes_requested` row reopens as\n * the correction draft (version +1); a `ready` row refuses — the package is\n * awaiting review and must receive a verdict first; otherwise a fresh draft\n * is created at `last reviewed version + 1` with dispatch-stamped provenance.\n */\nasync function resolveDraft(\n service: WorkProductService,\n config: WorkProductToolConfig,\n scopeKey: string,\n ctx: AppToolContext,\n): Promise<WorkProductRecord> {\n const open = await service.openDraft(ctx.workspaceId, scopeKey)\n if (open) return open\n const awaitingReview = await service.awaitingReview(ctx.workspaceId, scopeKey)\n if (awaitingReview) {\n throw new ToolInputError(\n 'awaiting_review',\n `Work product for ${scopeKey} (v${awaitingReview.version}) is awaiting review — no further emission until a reviewer verdict.`,\n 409,\n )\n }\n const awaitingCorrection = await service.awaitingCorrection(ctx.workspaceId, scopeKey)\n if (awaitingCorrection) {\n return unwrap(() => service.reopen(awaitingCorrection.id), 'reopen_failed')\n }\n return service.create({\n workspaceId: ctx.workspaceId,\n threadId: ctx.threadId,\n scopeKey,\n version: await service.nextVersion(ctx.workspaceId, scopeKey),\n provenance: stampProvenance(config.provenance(ctx), config.now),\n })\n}\n\n/** Turn a span rejection into the sentence the model can act on WITHOUT\n * re-reading the document — every message names the number that was wrong and\n * the number it must be under. */\nfunction spanErrorDetail(failure: SourceSpanFailure): string {\n switch (failure.reason) {\n case 'not_integer':\n return `${failure.field} must be a whole character offset.`\n case 'negative':\n return `${failure.field} must not be negative.`\n case 'inverted':\n return 'end must be greater than start — the range is half-open [start, end).'\n case 'out_of_range':\n return `end is past the end of the document, which is ${failure.totalChars} characters. Offsets are absolute in the whole document: when you read with an offset, add that offset to the position within the returned text.`\n case 'blank':\n return 'that range is only whitespace. Widen it to the characters that carry the value.'\n }\n}\n\n/** Turn a locate failure into a sentence naming what was searched for. */\nfunction findErrorDetail(failure: SourceFindFailure, needle: string): string {\n switch (failure.reason) {\n case 'blank_needle':\n return 'the value to locate is empty.'\n case 'not_found':\n return `${JSON.stringify(needle)} does not occur in that document. Read it again and cite a value it actually contains, or — if this figure is COMPUTED rather than read — omit the locator and state the computation in claim.`\n case 'occurrence_out_of_range':\n return `that value occurs ${failure.found} time(s) in the document; findOccurrence is out of range.`\n case 'not_distinctive':\n return failure.found === 0\n ? `${JSON.stringify(failure.needle)} is too short to identify a place in the document — a digit or two matches somewhere in almost any text. Cite the labelled line instead (for example \"Box 1 Wages, tips, other compensation ......... 128,450.00\"). If the document does not state this value at all, omit the locator and say so in claim rather than pointing at an unrelated line.`\n : `${JSON.stringify(failure.needle)} occurs ${failure.found} times, so it names no particular place. Cite a longer stretch of the supporting line, or pass findOccurrence to say which one you mean.`\n }\n}\n\n/**\n * Resolve every entry's quote against the document it names — the one place\n * lineage text is decided.\n *\n * Two citation forms, and the asymmetry between them is the point:\n *\n * - `locator.span` — the model names `[start, end)` into text it just read\n * and the PLATFORM slices the quote out of the source bytes. There is no\n * retyping step, so a fabricated quote is not rejected, it is impossible.\n * The slice REPLACES any quote the model also sent (the document is the\n * authority, not the model's transcription of it) and the resolved text\n * goes back in the tool result, so the model sees what was stored.\n * - `locator.quote` alone — the legacy free-text form, still verified\n * character-for-character. Kept because evidence written before spans\n * existed, and sources reached through paths that cannot offer offsets,\n * must stay expressible; a product does not lose lineage by upgrading.\n *\n * Entries are MUTATED in place with the resolved quote and its server-set\n * `quoteBasis`, then persisted — so what a reviewer reads is what the platform\n * proved, not what the model typed.\n *\n * Source texts are read once per distinct `sourceRef` in the batch — a\n * 50-entry batch citing three documents is three reads, not fifty.\n *\n * Without `readSourceText` the product has given the shell no way to see its\n * documents: free-text quotes go unchecked (as before — inventing a weaker\n * check would report unverified lineage as verified), and a span is a LOUD\n * refusal rather than a silently dropped locator, because a span the platform\n * cannot slice is a wiring bug in the product, not a model mistake.\n */\nasync function resolveEvidenceQuotes(\n config: WorkProductToolConfig,\n entries: readonly EvidenceEntry[],\n ctx: AppToolContext,\n): Promise<void> {\n const readSourceText = config.readSourceText\n const texts = new Map<string, string | null>()\n const readText = async (ref: string): Promise<string | null> => {\n if (!texts.has(ref)) texts.set(ref, await readSourceText!(ref, ctx))\n return texts.get(ref) ?? null\n }\n\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n // `quoteBasis` arrives unset — `parseEvidenceInput` drops a model-supplied\n // one — so this function only ever WRITES it, and only on a path that\n // established it. Every `continue` below therefore leaves the entry\n // unlabelled, which is the honest state for a quote nothing proved.\n const find = entry.locator.find\n const span = entry.locator.span\n const quote = entry.locator.quote\n\n if (find !== undefined) {\n if (!readSourceText) {\n throw new ToolInputError(\n 'span_unsupported',\n `entries[${index}].locator.find: this deployment cannot read source text, so a value cannot be located. Record the entry without locator.find.`,\n 500,\n )\n }\n const text = await readText(entry.sourceRef)\n if (text === null) {\n throw new ToolInputError(\n 'unverifiable_quote',\n `entries[${index}].locator.find: \"${entry.sourceRef}\" has no readable text, so the value cannot be located. Record the entry without a locator and state the basis in claim.`,\n )\n }\n const located = findSourceLine(text, find, entry.locator.findOccurrence ?? 1)\n if (!located.ok) {\n throw new ToolInputError(\n 'value_not_found',\n `entries[${index}].locator.find into \"${entry.sourceRef}\": ${findErrorDetail(located.failure, find)}`,\n )\n }\n // The platform owns BOTH halves now: it found the position and it cut\n // the text. The model contributed a value it read, which is the one\n // thing it does reliably.\n entry.locator.span = located.span\n entry.locator.quote = located.quote\n entry.locator.quoteBasis = 'span'\n continue\n }\n\n if (span) {\n if (!readSourceText) {\n throw new ToolInputError(\n 'span_unsupported',\n `entries[${index}].locator.span: this deployment cannot read source text, so a span cannot be resolved into a quote. Record the entry without locator.span.`,\n 500,\n )\n }\n const text = await readText(entry.sourceRef)\n if (text === null) {\n throw new ToolInputError(\n 'unverifiable_quote',\n `entries[${index}].locator.span: \"${entry.sourceRef}\" has no readable text, so the span cannot be resolved. Record the entry without locator.span or locator.quote and state the basis in claim.`,\n )\n }\n const sliced = sliceSourceSpan(text, span)\n if (!sliced.ok) {\n throw new ToolInputError(\n 'invalid_span',\n `entries[${index}].locator.span [${span.start}, ${span.end}) into \"${entry.sourceRef}\": ${spanErrorDetail(sliced.failure)}`,\n )\n }\n entry.locator.quote = sliced.quote\n entry.locator.quoteBasis = 'span'\n continue\n }\n\n if (quote === undefined || quote.trim().length === 0) continue\n if (!readSourceText) continue\n const text = await readText(entry.sourceRef)\n if (text === null) {\n throw new ToolInputError(\n 'unverifiable_quote',\n `entries[${index}].locator.quote: \"${entry.sourceRef}\" has no readable text, so the quote cannot be verified. Record the entry without locator.quote and state the basis in claim.`,\n )\n }\n if (!sourceContainsQuote(text, quote)) {\n throw new ToolInputError(\n 'quote_not_found',\n `entries[${index}].locator.quote: ${JSON.stringify(quote)} does not occur in \"${entry.sourceRef}\". Cite locator.find instead — the value as it appears in the document — and this platform locates it and writes the quote itself, so the text is right by construction. If the value is COMPUTED rather than read, omit both and state the computation in claim.`,\n )\n }\n entry.locator.quoteBasis = 'model'\n }\n}\n\n/**\n * Refuse any entry whose anchored text does not carry the figure it claims.\n *\n * This runs AFTER `resolveEvidenceQuotes`, on the text the platform decided,\n * and it is deliberately independent of how that text was anchored. `find`\n * makes a mis-addressed citation unlikely — the platform locates the line — but\n * `span` still accepts offsets a caller computed, and this is what \"accepted\n * only when they independently verify\" means concretely: the slice has to\n * contain the value.\n *\n * Row `7256ef49` is the case. Four span citations, every quote a real slice of\n * the right document, every one ~200 characters above the figure it claimed.\n * `resolveEvidenceQuotes` passed all four; nothing else here would have caught\n * them; a reviewer would have read four authoritative-looking citations\n * supporting nothing.\n *\n * Rejection is a `ToolInputError` so it folds back to the model mid-turn, the\n * same posture a non-occurring quote already gets, and the message names both\n * ways out — cite the value, or drop the locator because the figure was\n * computed. An unsatisfiable gate does not stop a bad submit, it selects for\n * one, so naming the second exit is load-bearing rather than politeness.\n */\nfunction assertClaimsSupported(\n config: WorkProductToolConfig,\n entries: readonly EvidenceEntry[],\n): void {\n if (config.verifyClaimSupport === false) return\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n const quote = entry.locator.quote\n if (quote === undefined) continue\n const support = verifyClaimSupport(quote, entry.claim)\n if (support.status !== 'unsupported') continue\n throw new ToolInputError(\n 'claim_not_supported',\n `entries[${index}].claim ${JSON.stringify(entry.claim)} is not supported by the text it cites in \"${entry.sourceRef}\": ${claimSupportErrorDetail(support, quote)}`,\n )\n }\n}\n\n/** Re-verify every persisted quote at submit time. The upsert gate stops new\n * fabrication; this stops a package whose evidence was written BEFORE the\n * gate existed (or under a since-corrected document) from reaching a\n * reviewer. Entries with no quote are neither verified nor failures — they\n * are lineage with no click target, counted separately so the recorded check\n * states how much of the package is quote-backed.\n *\n * A span-anchored entry is re-checked by RE-SLICING the current document and\n * comparing to the stored text, not by substring search. The difference\n * matters exactly when a source has been replaced since the citation was\n * written: a substring search would still pass if the sentence survived\n * anywhere in the new file, while the reviewer's click target has silently\n * moved. Re-slicing says the offsets still select the same characters. */\nasync function summarizeQuoteVerification(\n config: WorkProductToolConfig,\n evidence: readonly EvidenceEntry[],\n ctx: AppToolContext,\n): Promise<{ verified: number; spanAnchored: number; withoutQuote: number; failed: string[] } | undefined> {\n const readSourceText = config.readSourceText\n if (!readSourceText) return undefined\n const texts = new Map<string, string | null>()\n let verified = 0\n let spanAnchored = 0\n let withoutQuote = 0\n const failed: string[] = []\n for (const entry of evidence) {\n const quote = entry.locator.quote\n if (quote === undefined || quote.trim().length === 0) {\n withoutQuote += 1\n continue\n }\n if (!texts.has(entry.sourceRef)) texts.set(entry.sourceRef, await readSourceText(entry.sourceRef, ctx))\n const text = texts.get(entry.sourceRef)\n if (typeof text !== 'string') {\n failed.push(entry.id)\n continue\n }\n const span = entry.locator.span\n if (span) {\n const sliced = sliceSourceSpan(text, span)\n if (sliced.ok && sliced.quote === quote) {\n verified += 1\n spanAnchored += 1\n } else failed.push(entry.id)\n continue\n }\n if (sourceContainsQuote(text, quote)) verified += 1\n else failed.push(entry.id)\n }\n return { verified, spanAnchored, withoutQuote, failed }\n}\n\n/**\n * Re-run claim support over every PERSISTED entry at submit time.\n *\n * Unlike `summarizeQuoteVerification` this needs no `readSourceText`: the\n * question is whether the stored quote carries the stored claim, and both are\n * on the row. So it runs for every product, including one that cannot read its\n * sources back — and it is the check that catches a package whose evidence was\n * written before this gate existed. Row `7256ef49` is exactly that package.\n */\nfunction summarizeClaimSupport(evidence: readonly EvidenceEntry[]): {\n supported: number\n checkable: number\n unsupported: string[]\n} {\n let supported = 0\n let checkable = 0\n const unsupported: string[] = []\n for (const entry of evidence) {\n const quote = entry.locator.quote\n if (quote === undefined) continue\n const support = verifyClaimSupport(quote, entry.claim)\n if (support.status === 'not_applicable') continue\n checkable += 1\n if (support.status === 'supported') supported += 1\n else unsupported.push(entry.id)\n }\n return { supported, checkable, unsupported }\n}\n\n/** Build the three work-product tools for `customTools` registration on the\n * MCP server / HTTP handler / runtime executor. */\nexport function buildWorkProductTools(config: WorkProductToolConfig): AppToolDefinition[] {\n const service = createWorkProductService({\n store: config.store,\n ...(config.now ? { now: config.now } : {}),\n ...(config.generateId ? { generateId: config.generateId } : {}),\n })\n\n const upsertEvidence = defineAppTool({\n name: 'upsert_evidence',\n description:\n 'Record source→field lineage for the current work product, incrementally as you find it. Each entry links a source document (sourceRef + locator) to one artifact target and states the claim it supports. Cite by locator.find — the value exactly as it appears in the document — and the platform locates it and writes the supporting quote for you. Re-emitting an entry id replaces that entry. Address the work product by scopeKey; the first call creates the draft.',\n parameters: {\n type: 'object',\n properties: {\n scopeKey: { type: 'string', description: 'Engagement key for this work product.' },\n entries: {\n type: 'array',\n minItems: 1,\n maxItems: MAX_WORK_PRODUCT_BATCH,\n items: {\n type: 'object',\n properties: {\n id: { type: 'string', description: 'Stable entry id — re-emit to replace.' },\n sourceRef: { type: 'string', description: 'Vault path / attachment id of the SOURCE document.' },\n locator: {\n type: 'object',\n properties: {\n page: { type: 'number' },\n range: { type: 'string', description: \"Free-form location: 'L120-L134' | 'B7' | '¶4'.\" },\n find: {\n type: 'string',\n description:\n 'PREFERRED — use this whenever the value was READ from a document. The value exactly as it appears in the source (for example \"128,450.00\"), or a short distinctive phrase from the supporting line. The platform LOCATES it, cites the whole line it sits on, and returns that line to you. You do not retype the quote and you do not compute character offsets — so the citation can neither be invented nor land on the wrong line. If the value is not in the document, the entry is refused.',\n },\n findOccurrence: {\n type: 'integer',\n minimum: 1,\n description: 'Which occurrence of `find` to cite when the value appears more than once. Defaults to the first.',\n },\n span: {\n type: 'object',\n description:\n 'Only when you have exact character offsets from a tool that computed them. Absolute offsets into the whole document: start is the first character, end is one past the last. Prefer `find` — offsets computed by hand land on the wrong line.',\n properties: {\n start: { type: 'integer', minimum: 0 },\n end: { type: 'integer', minimum: 1 },\n },\n required: ['start', 'end'],\n },\n quote: {\n type: 'string',\n description:\n 'Fallback for sources that cannot give you character offsets: the supporting text copied character-for-character. The platform checks it occurs in the document and REFUSES the entry if it does not. Prefer span. For a value you COMPUTED rather than read, omit both and state the computation in claim.',\n },\n },\n },\n target: { type: 'string', description: 'Artifact field/claim this evidence supports.' },\n claim: { type: 'string', description: 'The value/assertion at the target.' },\n confidence: { type: 'number', minimum: 0, maximum: 1 },\n },\n required: ['id', 'sourceRef', 'target', 'claim'],\n },\n },\n },\n required: ['scopeKey', 'entries'],\n },\n async execute(args: Record<string, unknown>, ctx: AppToolContext) {\n const scopeKey = requireScopeKey(args)\n const raw = requireBatch(args, 'entries')\n const entries: EvidenceEntry[] = []\n for (let index = 0; index < raw.length; index += 1) {\n const parsed = parseEvidenceInput(raw[index], `entries[${index}]`)\n if (!parsed.ok) throw new ToolInputError('invalid_evidence', `${parsed.field}: ${parsed.error}`)\n entries.push(parsed.value)\n }\n // Fail-loud source resolution: lineage can never point at nothing.\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n if (!(await config.resolveSourceRef(entry.sourceRef, ctx))) {\n throw new ToolInputError(\n 'unknown_source_ref',\n `entries[${index}].sourceRef: \"${entry.sourceRef}\" does not resolve to an existing source document.`,\n )\n }\n }\n // Fail-loud quote verification: a quote must occur in the document the\n // entry names. Rejected BEFORE the draft is resolved, so a batch with a\n // fabricated quote persists nothing — the model corrects and re-sends\n // rather than leaving a half-written row behind.\n await resolveEvidenceQuotes(config, entries, ctx)\n // ...and the text that resolution produced must carry the figure the\n // entry claims. Same batch, same fail-before-persist discipline: an\n // entry citing the payer line for a wage figure never reaches the draft.\n assertClaimsSupported(config, entries)\n const draft = await resolveDraft(service, config, scopeKey, ctx)\n const record = await unwrap(() => service.upsertEvidence(draft.id, entries), 'evidence_rejected')\n return {\n workProductId: record.id,\n version: record.version,\n evidenceCount: record.evidence.length,\n // Echo what was actually STORED for the entries in this call. A span\n // citation's quote is produced here, not sent here, so the model must\n // be able to see the text its offsets selected — that is how it\n // notices an off-by-a-line span without a second read.\n entries: entries.map((entry) => ({\n id: entry.id,\n target: entry.target,\n ...(entry.locator.quote === undefined ? {} : { quote: entry.locator.quote }),\n ...(entry.locator.quoteBasis === undefined ? {} : { quoteBasis: entry.locator.quoteBasis }),\n })),\n }\n },\n })\n\n const flagException = defineAppTool({\n name: 'flag_exception',\n description:\n 'Flag problems with the current work product (missing documents, inconsistent sources, …). An unresolved blocking exception parks the work product until it is resolved; re-emit the same id with resolved:true to release it. Address by scopeKey.',\n parameters: {\n type: 'object',\n properties: {\n scopeKey: { type: 'string', description: 'Engagement key for this work product.' },\n exceptions: {\n type: 'array',\n minItems: 1,\n maxItems: MAX_WORK_PRODUCT_BATCH,\n items: {\n type: 'object',\n properties: {\n id: { type: 'string', description: 'Stable entry id — re-emit to replace.' },\n severity: { type: 'string', enum: ['blocking', 'material', 'advisory'] },\n kind: { type: 'string', description: 'Exception kind from the product vocabulary.' },\n message: { type: 'string' },\n targets: { type: 'array', items: { type: 'string' } },\n resolved: { type: 'boolean' },\n resolutionNote: { type: 'string' },\n },\n required: ['id', 'severity', 'kind', 'message'],\n },\n },\n },\n required: ['scopeKey', 'exceptions'],\n },\n async execute(args: Record<string, unknown>, ctx: AppToolContext) {\n const scopeKey = requireScopeKey(args)\n const raw = requireBatch(args, 'exceptions')\n const entries: ExceptionEntry[] = []\n for (let index = 0; index < raw.length; index += 1) {\n const parsed = parseExceptionInput(raw[index], `exceptions[${index}]`)\n if (!parsed.ok) throw new ToolInputError('invalid_exception', `${parsed.field}: ${parsed.error}`)\n if (!config.exceptionKinds.includes(parsed.value.kind)) {\n throw new ToolInputError(\n 'invalid_exception',\n `exceptions[${index}].kind: must be one of: ${config.exceptionKinds.join(', ')}`,\n )\n }\n // An agent resolving its own exception is tagged as such; the reviewer\n // tag is reserved for the verdict route.\n if (parsed.value.resolved && parsed.value.resolvedBy === undefined) parsed.value.resolvedBy = 'agent'\n entries.push(parsed.value)\n }\n const draft = await resolveDraft(service, config, scopeKey, ctx)\n const record = await unwrap(() => service.upsertExceptions(draft.id, entries), 'exception_rejected')\n return {\n workProductId: record.id,\n version: record.version,\n status: record.status,\n unresolvedBlocking: unresolvedBlockingExceptions(record.exceptions).length,\n }\n },\n })\n\n const submitWorkProduct = defineAppTool({\n name: 'submit_work_product',\n description:\n 'Submit the finished work product for professional review — the terminal call after evidence and exceptions are recorded. Refused while a blocking exception is unresolved, or when a material target lacks evidence. Include your own quality checks in `checks`.',\n parameters: {\n type: 'object',\n properties: {\n scopeKey: { type: 'string', description: 'Engagement key for this work product.' },\n artifact: {\n type: 'object',\n properties: {\n kind: { type: 'string', description: 'Artifact kind from the product vocabulary.' },\n title: { type: 'string' },\n path: { type: 'string', description: 'Vault/object-store ref of the rendered document.' },\n content: { type: 'string', description: 'Inline body when small (markdown/JSON).' },\n mediaType: { type: 'string' },\n baseline: {\n type: 'object',\n properties: { path: { type: 'string' }, content: { type: 'string' } },\n description: 'For diff-first artifacts: the source document being redlined.',\n },\n fields: { type: 'object', description: 'Structured field map lineage targets anchor to.' },\n },\n required: ['kind', 'title'],\n },\n checks: {\n type: 'array',\n items: {\n type: 'object',\n properties: {\n id: { type: 'string' },\n name: { type: 'string' },\n passed: { type: 'boolean' },\n detail: { type: 'string' },\n },\n required: ['id', 'name', 'passed'],\n },\n description: 'Your own quality self-checks (recorded as agent-sourced).',\n },\n },\n required: ['scopeKey', 'artifact'],\n },\n async execute(args: Record<string, unknown>, ctx: AppToolContext) {\n const scopeKey = requireScopeKey(args)\n const parsedArtifact = parseArtifactInput(args.artifact)\n if (!parsedArtifact.ok) throw new ToolInputError('invalid_artifact', `${parsedArtifact.field}: ${parsedArtifact.error}`)\n const artifact = parsedArtifact.value\n if (!config.artifactKinds.includes(artifact.kind)) {\n throw new ToolInputError('invalid_artifact', `artifact.kind: must be one of: ${config.artifactKinds.join(', ')}`)\n }\n const agentChecks: QualityCheck[] = []\n if (args.checks !== undefined) {\n if (!Array.isArray(args.checks)) throw new ToolInputError('invalid_checks', 'checks must be an array when present.')\n for (let index = 0; index < args.checks.length; index += 1) {\n const parsed = parseAgentCheckInput(args.checks[index], `checks[${index}]`)\n if (!parsed.ok) throw new ToolInputError('invalid_checks', `${parsed.field}: ${parsed.error}`)\n agentChecks.push({ ...parsed.value, source: 'agent' })\n }\n }\n\n const draft = await resolveDraft(service, config, scopeKey, ctx)\n const blocking = unresolvedBlockingExceptions(draft.exceptions)\n if (blocking.length > 0) {\n throw new ToolInputError(\n 'blocking_exceptions_unresolved',\n `Cannot submit: ${blocking.length} unresolved blocking exception(s) (${blocking.map((entry) => entry.id).join(', ')}). Resolve each via flag_exception with resolved:true, or downgrade its severity with a resolutionNote justifying why it does not block.`,\n 409,\n )\n }\n\n // Platform check one: every persisted quote still occurs in the source\n // it names. The upsert gate stops NEW fabrication; this stops a package\n // whose evidence predates the gate from reaching a reviewer. Recorded on\n // the row AND rejected fail-loud — the same discipline as coverage.\n const checks: QualityCheck[] = [...agentChecks]\n const quotes = await summarizeQuoteVerification(config, draft.evidence, ctx)\n if (quotes) {\n const quoted = quotes.verified + quotes.failed.length\n checks.unshift({\n id: QUOTE_VERIFICATION_CHECK,\n name: QUOTE_VERIFICATION_CHECK,\n passed: quotes.failed.length === 0,\n detail:\n quotes.failed.length === 0\n ? `${quotes.verified}/${quoted} quoted evidence entries verified against their source (${quotes.spanAnchored} platform-sliced from a source span, ${quotes.verified - quotes.spanAnchored} model-quoted and proved to occur); ${quotes.withoutQuote} recorded without a quote`\n : `Unverifiable quotes on: ${quotes.failed.join(', ')}`,\n source: 'platform',\n })\n if (quotes.failed.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'quote_verification_failed',\n `Cannot submit: ${quotes.failed.length} evidence entr${quotes.failed.length === 1 ? 'y quotes' : 'ies quote'} text that does not occur in the source named (${quotes.failed.join(', ')}). Re-emit each with a quote copied character-for-character from that document, or without locator.quote if the value is computed.`,\n )\n }\n }\n\n // Platform check two: every citation's text carries the figure it\n // claims. Recorded AND rejected, like the other two — a citation that\n // points at real text supporting nothing is the failure a reviewer is\n // least able to catch by eye, because it looks exactly like a good one.\n if (config.verifyClaimSupport !== false) {\n const support = summarizeClaimSupport(draft.evidence)\n checks.unshift({\n id: CLAIM_SUPPORT_CHECK,\n name: CLAIM_SUPPORT_CHECK,\n passed: support.unsupported.length === 0,\n detail:\n support.unsupported.length > 0\n ? `Cited text does not contain the claimed figure on: ${support.unsupported.join(', ')}`\n : support.checkable === 0\n ? // Honest about a vacuous pass: no entry paired a quote with a\n // figure, so nothing was checked. Reporting \"0/0 verified\"\n // would read to a reviewer as assurance that was never earned.\n 'No citation pairs a quote with a claimed figure — nothing to check'\n : `${support.supported}/${support.checkable} value-bearing citations anchor to text containing the claimed figure`,\n source: 'platform',\n })\n if (support.unsupported.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'claim_not_supported',\n `Cannot submit: ${support.unsupported.length} evidence entr${support.unsupported.length === 1 ? 'y cites' : 'ies cite'} text that does not contain the figure claimed (${support.unsupported.join(', ')}). Re-emit each with locator.find set to the value exactly as it appears in the document, or without a locator if the figure was computed rather than read.`,\n )\n }\n }\n\n // Platform check three: every material target has ≥1 evidence row. A\n // failing coverage check is BOTH recorded on the row (visible to the\n // queue) and rejected fail-loud — a lineage-free package can never reach\n // a reviewer.\n if (config.materialTargets) {\n const targets = config.materialTargets(artifact)\n const covered = new Set(draft.evidence.map((entry) => entry.target))\n const missing = targets.filter((target) => !covered.has(target))\n // How each covered target is BACKED, not merely that a row exists.\n // A coverage count that cannot distinguish a platform-sliced citation\n // from a bare assertion is the number that let 13 invented quotes read\n // as \"13/13 evidenced\" on the row a reviewer then opened.\n const anchoredTargets = new Set(\n draft.evidence.filter((entry) => entry.locator.quoteBasis !== undefined).map((entry) => entry.target),\n )\n const spanTargets = new Set(\n draft.evidence.filter((entry) => entry.locator.quoteBasis === 'span').map((entry) => entry.target),\n )\n const present = targets.filter((target) => covered.has(target))\n const unanchored = present.filter((target) => !anchoredTargets.has(target))\n const spanCount = present.filter((target) => spanTargets.has(target)).length\n const breakdown = `${spanCount} span-anchored, ${present.length - spanCount - unanchored.length} quote-verified, ${unanchored.length} claim-only`\n const coverage: QualityCheck = {\n id: EVIDENCE_COVERAGE_CHECK,\n name: EVIDENCE_COVERAGE_CHECK,\n passed: missing.length === 0 && !(config.requireAnchoredEvidence && unanchored.length > 0),\n detail:\n missing.length > 0\n ? `Missing evidence for: ${missing.join(', ')}`\n : config.requireAnchoredEvidence && unanchored.length > 0\n ? `No source anchor for: ${unanchored.join(', ')}`\n : `${targets.length}/${targets.length} material targets evidenced (${breakdown})`,\n source: 'platform',\n }\n checks.unshift(coverage)\n if (missing.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'evidence_coverage_failed',\n `Cannot submit: material targets lack evidence: ${missing.join(', ')}. Add upsert_evidence entries targeting each, then resubmit.`,\n )\n }\n if (config.requireAnchoredEvidence && unanchored.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'evidence_not_anchored',\n `Cannot submit: these material targets have an evidence row but no source anchor: ${unanchored.join(', ')}. Re-emit each with locator.find — the value exactly as it appears in the document. The platform locates it and writes the quote itself, so you never retype source text or count characters.`,\n )\n }\n }\n\n const provenance = stampProvenance(config.provenance(ctx), config.now)\n const record = await unwrap(\n () => service.submit(draft.id, { artifact, checks, provenance }),\n 'submit_rejected',\n )\n await config.onReady?.(record, ctx)\n return {\n workProductId: record.id,\n version: record.version,\n status: record.status,\n checks: record.checks.map((check) => ({ name: check.name, passed: check.passed, source: check.source })),\n }\n },\n })\n\n return [upsertEvidence, flagException, submitWorkProduct]\n}\n","/**\n * Framework-neutral work-product review endpoints — the\n * `createInteractionAnswerRoute` factory pattern: web-standard\n * `Request`/`Response`, ONE product-supplied `authorize` seam (session auth,\n * workspace access, reviewer identity, rate limits), everything behind it\n * mechanism. Server-only and subpath-only: never reachable from a client\n * bundle (enforced by the browser-safety test).\n *\n * The verdict endpoint is deliberately NOT a second approval broker: agent\n * asks stay on `/interactions`; this is the REVIEWER's plain authorized\n * verdict on a ready package — one human-in-the-loop channel per direction.\n * A `request_changes` note re-enters chat as the correction turn via the\n * `onVerdict` seam; chat remains the driver surface.\n */\n\nimport {\n createWorkProductService,\n type WorkProductService,\n} from './service'\nimport {\n workProductToPersistedPart,\n type WorkProductPersistedPart,\n type WorkProductRecord,\n type WorkProductStorePort,\n} from './types'\n\n/** Reviewer verdict wire body for the POST endpoint. */\nexport type WorkProductVerdictBody =\n | { ok: true; id: string; verdict: 'approve'; note?: string }\n | { ok: true; id: string; verdict: 'request_changes'; note: string }\n | { ok: false; error: string }\n\n/** Validate the verdict POST body: `{ id, verdict, note? }`; a\n * `request_changes` verdict REQUIRES a non-empty note — the note IS the\n * correction turn the agent works from. */\nexport function validateWorkProductVerdictBody(body: Record<string, unknown>): WorkProductVerdictBody {\n const id = typeof body.id === 'string' && body.id.trim() ? body.id.trim() : null\n if (!id) return { ok: false, error: 'Missing work product id' }\n const verdict = body.verdict\n if (verdict !== 'approve' && verdict !== 'request_changes') {\n return { ok: false, error: 'Invalid verdict: expected approve or request_changes' }\n }\n const note = body.note === undefined ? undefined : typeof body.note === 'string' ? body.note.trim() : null\n if (note === null) return { ok: false, error: 'Invalid note: expected a string' }\n if (verdict === 'request_changes') {\n if (!note) return { ok: false, error: 'request_changes requires a note — it becomes the correction instruction in chat' }\n return { ok: true, id, verdict, note }\n }\n return note ? { ok: true, id, verdict, note } : { ok: true, id, verdict }\n}\n\n/** The product seam's verdict for one request: authenticated reviewer +\n * workspace, or a product-authored short-circuit Response (401/403/429…). */\nexport type WorkProductRouteAuthorization =\n | { ok: true; workspaceId: string; reviewedBy: string }\n | { ok: false; response: Response }\n\n/** Auth seam arguments carrying the request, the endpoint intent, and the parsed verdict body */\nexport interface WorkProductAuthorizeArgs {\n request: Request\n intent: 'list' | 'detail' | 'verdict'\n /** The parsed, validated POST body (verdict intent only). */\n body?: Record<string, unknown>\n}\n\n/** Configuration options assembling the review endpoints from the store and product seams */\nexport interface WorkProductRoutesOptions {\n store: WorkProductStorePort\n /** Authenticate + authorize the caller; the ONLY product access step. */\n authorize: (args: WorkProductAuthorizeArgs) => Promise<WorkProductRouteAuthorization>\n /** Post-verdict product seam: post the `request_changes` note into the\n * driving chat thread as the correction turn, notify, etc. Runs after the\n * transition commits; a throw is logged, never unwinds the verdict. */\n onVerdict?: (args: {\n record: WorkProductRecord\n verdict: 'approve' | 'request_changes'\n note?: string\n reviewedBy: string\n }) => void | Promise<void>\n /** Persist/update the transcript anchor part reflecting the new status, so\n * the chat card flips with the verdict. */\n persistAnchorPart?: (part: WorkProductPersistedPart, record: WorkProductRecord) => void | Promise<void>\n /** Future integration point fired on approval (push to a DMS/CRM/export\n * pipeline). A stub seam by design — export itself stays the product's\n * signed object-store download. */\n onExport?: (record: WorkProductRecord) => void | Promise<void>\n logger?: Pick<Console, 'warn' | 'error'>\n now?: () => number\n}\n\n/** Assembled review endpoints returning web-standard Responses */\nexport interface WorkProductRoutes {\n /** GET — the workspace's records for the queue projection. Optional\n * `?status=a,b` filter. */\n list: (request: Request) => Promise<Response>\n /** GET — one record by id (404 when absent or outside the workspace). */\n detail: (request: Request, id: string) => Promise<Response>\n /** POST `{ id, verdict, note? }` — the reviewer verdict: CAS transition +\n * history entry + product seams. 409 when the record is no longer ready. */\n verdict: (request: Request) => Promise<Response>\n}\n\n/** Create the work-product review endpoints over the store port and product seams */\nexport function createWorkProductRoutes(options: WorkProductRoutesOptions): WorkProductRoutes {\n const logger = options.logger ?? console\n const service: WorkProductService = createWorkProductService({\n store: options.store,\n ...(options.now ? { now: options.now } : {}),\n })\n\n async function list(request: Request): Promise<Response> {\n const auth = await options.authorize({ request, intent: 'list' })\n if (!auth.ok) return auth.response\n const url = new URL(request.url)\n const statusParam = url.searchParams.get('status')\n const statuses = statusParam\n ? statusParam.split(',').map((value) => value.trim()).filter(Boolean)\n : null\n const workProducts = await options.store.listByWorkspace(\n auth.workspaceId,\n statuses ? { status: statuses as WorkProductRecord['status'][] } : undefined,\n )\n return Response.json({ workProducts })\n }\n\n async function detail(request: Request, id: string): Promise<Response> {\n const auth = await options.authorize({ request, intent: 'detail' })\n if (!auth.ok) return auth.response\n const record = await options.store.load(id)\n if (!record || record.workspaceId !== auth.workspaceId) {\n return Response.json({ error: 'Work product not found' }, { status: 404 })\n }\n return Response.json({ workProduct: record })\n }\n\n async function verdict(request: Request): Promise<Response> {\n const body = (await request.json().catch(() => null)) as Record<string, unknown> | null\n if (!body || typeof body !== 'object' || Array.isArray(body)) {\n return Response.json({ error: 'Invalid JSON body' }, { status: 400 })\n }\n const validation = validateWorkProductVerdictBody(body)\n if (!validation.ok) return Response.json({ error: validation.error }, { status: 400 })\n\n const auth = await options.authorize({ request, intent: 'verdict', body })\n if (!auth.ok) return auth.response\n\n const existing = await options.store.load(validation.id)\n if (!existing || existing.workspaceId !== auth.workspaceId) {\n return Response.json({ error: 'Work product not found' }, { status: 404 })\n }\n\n const outcome = await service.applyVerdict(validation.id, {\n verdict: validation.verdict,\n reviewedBy: auth.reviewedBy,\n ...(validation.note === undefined ? {} : { note: validation.note }),\n })\n if (!outcome.succeeded) {\n // A lost race and an illegal edge both mean \"the row is no longer the\n // ready version you looked at\" — 409 so the client re-reads.\n return Response.json({ code: 'VERDICT_CONFLICT', error: outcome.error }, { status: 409 })\n }\n const record = outcome.value\n\n // Product seams run AFTER the committed transition; their failures are\n // logged, never unwound — the verdict is durable truth at this point.\n try {\n await options.persistAnchorPart?.(workProductToPersistedPart(record), record)\n } catch (error) {\n logger.error('[work-product] persistAnchorPart failed:', error)\n }\n try {\n await options.onVerdict?.({\n record,\n verdict: validation.verdict,\n ...(validation.note === undefined ? {} : { note: validation.note }),\n reviewedBy: auth.reviewedBy,\n })\n } catch (error) {\n logger.error('[work-product] onVerdict failed:', error)\n }\n if (validation.verdict === 'approve') {\n try {\n await options.onExport?.(record)\n } catch (error) {\n logger.error('[work-product] onExport failed:', error)\n }\n }\n\n return Response.json({ ok: true, workProduct: record })\n }\n\n return { list, detail, verdict }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAuCA,IAAM,2BAAsF;AAAA,EAC1F,OAAO,oBAAI,IAAuB,CAAC,WAAW,OAAO,CAAC;AAAA,EACtD,SAAS,oBAAI,IAAuB,CAAC,OAAO,CAAC;AAAA,EAC7C,OAAO,oBAAI,IAAuB,CAAC,qBAAqB,YAAY,YAAY,CAAC;AAAA,EACjF,mBAAmB,oBAAI,IAAuB,CAAC,SAAS,YAAY,CAAC;AAAA,EACrE,UAAU,oBAAI,IAAuB,CAAC,YAAY,CAAC;AAAA,EACnD,YAAY,oBAAI,IAAuB;AACzC;AAGO,SAAS,yBAAyB,MAAyB,IAAgC;AAChG,SAAO,yBAAyB,IAAI,EAAE,IAAI,EAAE;AAC9C;AAGO,SAAS,sBAAsB,QAAoC;AACxE,SAAO,yBAAyB,MAAM,EAAE,SAAS;AACnD;AAmFA,SAAS,SAAY,OAAsC;AACzD,SAAO,EAAE,WAAW,OAAO,OAAO,UAAU,MAAM;AACpD;AAEA,SAAS,SAAY,IAAmC;AACtD,SAAO,EAAE,WAAW,OAAO,OAAO,gBAAgB,EAAE,yBAAyB,UAAU,KAAK;AAC9F;AAIA,SAAS,UAAoC,UAAwB,UAA6B;AAChG,QAAM,SAAS,SAAS,MAAM;AAC9B,QAAM,YAAY,IAAI,IAAI,OAAO,IAAI,CAAC,OAAO,UAAU,CAAC,MAAM,IAAI,KAAK,CAAU,CAAC;AAClF,aAAW,SAAS,UAAU;AA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AQ,QAAQ;AACjC;","names":["record","entry","bound","text"]}
|
|
1
|
+
{"version":3,"sources":["../../src/work-product/claim-support.ts","../../src/work-product/service.ts","../../src/work-product/provenance.ts","../../src/work-product/quote.ts","../../src/work-product/tools.ts","../../src/work-product/route.ts"],"sourcesContent":["/**\n * Claim support and target correctness — is this citation attached to the RIGHT\n * place, and does the text it names actually say what the entry claims?\n *\n * Three checks live here, each answering a different half of \"is this evidence\n * row honest\": {@link verifyClaimSupport} (claim ↔ cited text),\n * {@link verifyTargetLabel} (target ↔ cited line), and\n * {@link verifyArtifactAgreement} (claim ↔ the artifact field it decorates).\n * They share the numeric canonicalization below, which is why they share a\n * file — a second copy of \"what counts as the same figure\" is how two gates\n * drift into disagreeing about the same row.\n *\n * ── Check one: claim support ────────────────────────────────────────────────\n *\n * The two gates before this one answer different questions. `sourceContainsQuote`\n * asks whether the quote is really in the document; `findSourceLine` /\n * `sliceSourceSpan` make the platform produce the quote so it cannot be typed\n * wrong. Both are about the TEXT's provenance. Neither one looks at `claim`.\n *\n * Production row `7256ef49` is what that gap costs. Four evidence entries, all\n * `quoteBasis:'span'`, every quote a genuine slice of the document it named —\n * and every one landing on the employer/payer line about 200 characters above\n * the figure:\n *\n * claim 128450.00 -> \"tics LLC EIN 84-2213907\\nEmployee: Dana\"\n * claim 812.44 -> \"nt Savings Bank TIN 22-5510983\\nRecip\"\n * claim 2204.18 -> \"ndex Fund Trust TIN 47-3320115\\nRec\"\n * claim 1955.02 -> \"pient: Dana R. Whitfield\\n------------\"\n *\n * That is strictly worse than a fabricated quote. A fabricated quote fails the\n * verbatim gate; this one passes every gate, is real text from the right\n * document, and reads to a reviewer as an authoritative citation while\n * supporting nothing. `locator.find` (the platform locating a value the model\n * names) prevents the model from ADDRESSING the wrong line, and it is the right\n * primary fix. This module is the independent check underneath it: whatever\n * anchoring form produced the text, the text has to contain the figure.\n *\n * That also settles what a raw `locator.span` is worth. Hand-computed offsets\n * stay accepted, but only when they independently verify — which is exactly\n * \"the slice contains the claimed value\".\n *\n * ── The rule, and why it is drawn here ──────────────────────────────────────\n *\n * Strict on figures, silent on everything else. An unsatisfiable gate does not\n * stop a bad submit, it SELECTS for one — that is the measured mechanism behind\n * the 38 fabricated quotes on row `a68b1943`, where a coverage gate demanded\n * document lineage for computed values and got thirteen invented citations\n * forty seconds later. So the rule fires only where an honest citation can\n * always satisfy it:\n *\n * - The claim IS a value (`\"128450.00\"`, `\"$30,000\"`, `\"3\"`) — the anchored\n * text MUST contain that value. No latitude. This is the shape every tax\n * figure takes and the shape row `7256ef49` failed.\n * - The claim is prose naming figures (`\"indemnity capped at $5,000,000\"`) —\n * at least one of its currency-shaped figures must occur. Not all of them:\n * a claim may legitimately narrate a computation over several lines while\n * anchoring to the one line under discussion, and refusing that would make\n * an honest citation unrepresentable for the sake of a stricter-sounding\n * rule. One is enough to keep the anchor tethered to the claim's subject.\n * - The claim carries no figure at all (`\"Married Filing Jointly\"`,\n * `\"Dana R. Whitfield\"`, `\"2025-04-15\"`) — nothing to check, and the entry\n * passes. A filing status has no number to find, and inventing a\n * word-overlap score here would re-open the hole the verbatim gate closes.\n * - The entry has no anchored text at all — nothing to check. A computed\n * value cites its computation, not a document.\n *\n * A bare year, a form number and a box number are deliberately NOT figures:\n * `2025`, `1040` and `Box 1` have no thousands separator, no cent pair and no\n * currency symbol, so prose mentioning them does not trip the rule.\n *\n * ── Matching is value-wise, never substring ─────────────────────────────────\n *\n * `text.includes(claim)` would be the obvious implementation and it is wrong in\n * the direction that matters: it passes `\"450.00\"` against `\"128,450.00\"`, so a\n * claim citing the wrong figure survives whenever its digits happen to be a tail\n * of a real one. Both sides are tokenized into numbers and compared as VALUES,\n * so `450` and `128450` are simply different.\n */\n\n/** Currency marks stripped before a token is read as a number. */\nconst CURRENCY = /[$€£¥₹]/gu\n\n/**\n * Numbers as they occur in document text. The grouped form is tried first so\n * `128,450.00` is read as one value rather than `128` followed by `450.00` —\n * the whole point of comparing values instead of substrings.\n *\n * A leading `[$€£¥₹]?\\s*` is absorbed so `$ 1,200` tokenizes once, and the\n * dot-leaders typical of a form line (`Box 1 Wages ..... 128,450.00`) are not\n * digits, so they never join a token.\n */\nconst NUMBER_IN_TEXT = /[$€£¥₹]?\\s*\\d{1,3}(?:,\\d{3})+(?:\\.\\d+)?|[$€£¥₹]?\\s*\\d+(?:\\.\\d+)?/gu\n\n/**\n * A figure inside prose: a currency symbol, OR thousands separators, OR a cent\n * pair. Each of the three is a positive signal that the writer meant a\n * quantity rather than an identifier, which is what keeps `2025`, `1040` and\n * `Box 1` out.\n */\nconst FIGURE_IN_PROSE =\n /[$€£¥₹]\\s*[-+]?\\d[\\d,]*(?:\\.\\d+)?|[-+]?\\d{1,3}(?:,\\d{3})+(?:\\.\\d+)?|[-+]?\\d+\\.\\d{2}(?!\\d)/gu\n\n/** The whole string is one value: optional currency, sign, digits with\n * optional grouping and decimals, optional percent. Anchored, so an EIN\n * (`84-2213907`), a date (`2025-04-15`) and a range (`10-20`) are NOT values. */\nconst WHOLE_VALUE = /^[$€£¥₹]?\\s*[-+]?\\s*(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d+)?\\s*%?$/u\n\n/**\n * Reduce a numeric token to the form both sides are compared in: no currency,\n * no grouping, no trailing zeros in the fraction, no leading zeros.\n *\n * Sign and accounting parentheses are stripped rather than preserved, so\n * `(1,234.00)`, `-1,234.00` and `1,234.00` all reduce to `1234`. A document\n * renders the same deduction all three ways depending on the form and the\n * extractor, and the sign is a property of the TARGET LINE's semantics, not of\n * the document's typography. Comparing magnitudes keeps those honest citations\n * working, and it concedes nothing to fabrication: the digits still have to be\n * the document's digits.\n *\n * Returns `null` for anything that is not a plain number.\n */\nexport function canonicalizeValue(token: string): string | null {\n let text = token.trim()\n if (text.length === 0) return null\n if (/^\\(.*\\)$/u.test(text)) text = text.slice(1, -1).trim()\n text = text.replace(CURRENCY, '').trim()\n text = text.replace(/^[-+]\\s*/u, '').trim()\n text = text.replace(/%$/u, '').trim()\n if (!/^(?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d+)?$/u.test(text)) return null\n text = text.replace(/,/gu, '')\n if (text.includes('.')) text = text.replace(/0+$/u, '').replace(/\\.$/u, '')\n text = text.replace(/^0+(?=\\d)/u, '')\n return text.length === 0 ? null : text\n}\n\n/** Every distinct value appearing in `text`, canonicalized. */\nexport function valuesInText(text: string): string[] {\n const seen = new Set<string>()\n for (const match of text.matchAll(NUMBER_IN_TEXT)) {\n const canonical = canonicalizeValue(match[0])\n if (canonical !== null) seen.add(canonical)\n }\n return [...seen]\n}\n\n/**\n * The values a claim asserts, canonicalized — empty when the claim asserts no\n * figure, which is the \"nothing to check\" case.\n *\n * A claim that is ENTIRELY one value yields that value (strict path). Otherwise\n * only currency-shaped figures inside the prose count, so an assertion that\n * merely mentions a year or a form number yields nothing.\n */\nexport function claimValues(claim: string): string[] {\n const trimmed = claim.trim()\n if (WHOLE_VALUE.test(trimmed)) {\n const whole = canonicalizeValue(trimmed)\n if (whole !== null) return [whole]\n }\n const seen = new Set<string>()\n for (const match of trimmed.matchAll(FIGURE_IN_PROSE)) {\n const canonical = canonicalizeValue(match[0])\n if (canonical !== null) seen.add(canonical)\n }\n return [...seen]\n}\n\nexport type ClaimSupport =\n /** No figure to check: a non-numeric claim, or no anchored text. */\n | { status: 'not_applicable' }\n | { status: 'supported'; matched: string }\n | { status: 'unsupported'; claimed: string[]; present: string[] }\n\n/**\n * Does `quote` carry the figure `claim` asserts?\n *\n * `supported` requires ONE claimed value to occur, which is exact for the\n * single-value claim (there is only one) and deliberate latitude for prose (see\n * the rule note at the top of the file).\n */\nexport function verifyClaimSupport(quote: string, claim: string): ClaimSupport {\n if (quote.trim().length === 0) return { status: 'not_applicable' }\n const claimed = claimValues(claim)\n if (claimed.length === 0) return { status: 'not_applicable' }\n const present = valuesInText(quote)\n const matched = claimed.find((value) => present.includes(value))\n if (matched !== undefined) return { status: 'supported', matched }\n return { status: 'unsupported', claimed, present }\n}\n\n/** Short, quotable rendering of the anchored text for an error message. */\nfunction excerpt(quote: string, limit = 120): string {\n const flat = quote.replace(/\\s+/gu, ' ').trim()\n return flat.length <= limit ? flat : `${flat.slice(0, limit)}…`\n}\n\n/**\n * The sentence a model can act on without re-reading the document: what it\n * claimed, what the line it cited actually says, what figures that line does\n * carry, and the two ways out — cite the value, or drop the locator because the\n * figure was computed. Naming both keeps the gate satisfiable, which is the\n * property that stops it manufacturing the citation it screens for.\n */\nexport function claimSupportErrorDetail(\n failure: Extract<ClaimSupport, { status: 'unsupported' }>,\n quote: string,\n): string {\n const wanted = failure.claimed.length === 1 ? failure.claimed[0]! : `any of ${failure.claimed.join(', ')}`\n const carries =\n failure.present.length === 0\n ? 'that line carries no figure at all'\n : `the only figures on it are ${failure.present.join(', ')}`\n return `the cited text does not contain ${wanted}. It reads \"${excerpt(quote)}\", and ${carries}. Cite locator.find with the value exactly as it appears in the document and the platform will locate the right line for you. If this figure was COMPUTED rather than read from the document, omit the locator entirely and state the computation in claim.`\n}\n\n// ── check two: target ↔ cited line ───────────────────────────────────────────\n\n/**\n * Does the cited line belong to the target the entry attaches it to?\n *\n * `verifyClaimSupport` asks whether the anchored text carries the claimed\n * figure. It is blind, BY CONSTRUCTION, to whether that figure belongs on that\n * line. Production row `95105c8a` is what the blindness costs — two entries,\n * both anchored to a real line of the right document, both carrying the figure\n * they claim, and both attached to the wrong form line:\n *\n * f1040.line_3b claim 1955.02 -> \"Box 1b Qualified dividends ..... 1,955.02\"\n * f1040.line_3a claim 2204.18 -> \"Box 1a Total ordinary dividends . 2,204.18\"\n *\n * Form 1040 line 3a is QUALIFIED dividends and line 3b is ORDINARY, so each\n * citation points a reviewer at the other one's line. `quote_verification`,\n * `claim_support` and `evidence_coverage` were all green on that row. A\n * citation that points at the wrong line is a wrong citation even when every\n * character of it is real, and it is the failure a reviewer is least able to\n * catch by eye, because it looks exactly like a good one.\n *\n * ── Why the domain supplies LABELS and the shell supplies the COMPARISON ────\n *\n * The shell cannot know that line 3a means qualified dividends; that is tax\n * vocabulary and baking it here would violate the one rule this package is\n * built on. So the product declares which targets are CONFUSABLE with each\n * other and what each one's line looks like in a source document — a\n * {@link ConfusableTargetGroup} — and the shell does the comparing.\n *\n * ── Why a group of labels, and not a per-target expectation ─────────────────\n *\n * The obvious shape is a per-target expectation read POSITIVELY: \"a citation\n * for line_3a must contain 'Qualified'\". It is stricter, it is easier to\n * explain, and it is the wrong shape, because it refuses honest work. A\n * consolidated broker statement writing \"Qual. div. income\", a payer writing\n * \"Dividends that are qualified\", an OCR layer dropping a word — each one turns\n * a correct citation into a refusal. And this codebase has already measured\n * what refusing honest work does: a coverage gate that demanded document\n * lineage for computed values produced 38 fabricated quotes on row `a68b1943`,\n * because a gate a model cannot satisfy honestly is a gate it satisfies\n * dishonestly. An unsatisfiable rule does not stop bad work; it SELECTS for\n * invented work.\n *\n * So the same labels are read NEGATIVELY and COMPARATIVELY. An entry is\n * refused only when the cited line positively identifies a SIBLING target and\n * says nothing that identifies its own:\n *\n * - the line carries one of the target's own labels → `identified`, pass\n * - the line carries no label from the group at all → `not_applicable`, pass\n * - the line carries a sibling's label and none of its own → `crossed`, refuse\n *\n * Silence therefore always passes. An unusually-labelled document costs\n * nothing, a target the product never grouped costs nothing, and the only way\n * to fail is for the document itself to say the line belongs to a different\n * target — which is not a phrasing accident, it is the crossed pair. The rule\n * is satisfiable by the honest citation in every case, and unsatisfiable only\n * by the wrong one.\n *\n * Matching is punctuation- and case-insensitive: a label is a semantic marker\n * for which LINE this is, not a quotation. (`sourceContainsQuote` stays exact —\n * different question, different tolerance.)\n */\n\n/** One set of targets whose source lines are mistakable for each other, with\n * the phrases that tell them apart. Every string is product vocabulary; the\n * shell reads none of them as meaning anything. */\nexport interface ConfusableTargetGroup {\n /** Named in the refusal so the model is told which distinction it missed\n * (\"Form 1099-DIV boxes 1a/1b\"). */\n note?: string\n /** Target → phrases that identify THAT target's line in a source document.\n * Targets must be spelled the way evidence targets are spelled after the\n * product's `normalizeTarget`, so one canonical name is written once. */\n labels: Record<string, readonly string[]>\n}\n\nexport type TargetLabelVerdict =\n /** No group covers this target, or the line says nothing either way. */\n | { status: 'not_applicable' }\n /** The cited line carries one of this target's own labels. */\n | { status: 'identified'; label: string }\n /** The cited line identifies a DIFFERENT target in the same group. */\n | { status: 'crossed'; rival: string; rivalLabel: string; expected: readonly string[]; note?: string }\n\n/** Fold for label matching: case- and punctuation-insensitive, whitespace\n * collapsed. `\"Box 1b Qualified dividends .....\"` and `\"qualified dividend\"`\n * meet here; nothing about the numbers on the line is touched. */\nfunction foldLabel(value: string): string {\n return value\n .normalize('NFKC')\n .toLowerCase()\n .replace(/[^\\p{L}\\p{N}]+/gu, ' ')\n .trim()\n}\n\nexport function verifyTargetLabel(\n quote: string,\n target: string,\n groups: readonly ConfusableTargetGroup[],\n): TargetLabelVerdict {\n if (quote.trim().length === 0) return { status: 'not_applicable' }\n const group = groups.find((candidate) => Object.hasOwn(candidate.labels, target))\n if (!group) return { status: 'not_applicable' }\n const line = ` ${foldLabel(quote)} `\n\n const own = group.labels[target] ?? []\n for (const label of own) {\n const folded = foldLabel(label)\n if (folded.length > 0 && line.includes(folded)) return { status: 'identified', label }\n }\n for (const [rival, labels] of Object.entries(group.labels)) {\n if (rival === target) continue\n for (const label of labels) {\n const folded = foldLabel(label)\n if (folded.length === 0 || !line.includes(folded)) continue\n return {\n status: 'crossed',\n rival,\n rivalLabel: label,\n expected: own,\n ...(group.note === undefined ? {} : { note: group.note }),\n }\n }\n }\n return { status: 'not_applicable' }\n}\n\n/** The sentence a model can act on: which line it actually cited, which target\n * that line belongs to, and what its own target's line looks like. */\nexport function targetLabelErrorDetail(\n failure: Extract<TargetLabelVerdict, { status: 'crossed' }>,\n target: string,\n quote: string,\n): string {\n const ownLooks =\n failure.expected.length === 0\n ? ''\n : ` A citation for ${target} should land on the line naming ${failure.expected.map((label) => JSON.stringify(label)).join(' or ')}.`\n const note = failure.note === undefined ? '' : ` (${failure.note})`\n return `the line it cites reads \"${excerpt(quote)}\", which is the line for ${failure.rival} — it names ${JSON.stringify(failure.rivalLabel)}${note}.${ownLooks} Cite the line that belongs to this target, or attach this citation to ${failure.rival} instead. The figure is real; it is on the wrong line.`\n}\n\n// ── check three: claim ↔ the artifact field it decorates ─────────────────────\n\n/**\n * Does the entry agree with the artifact it is evidence FOR?\n *\n * The two checks above compare evidence to the SOURCE. This one compares it to\n * the DELIVERABLE, and it needs no product vocabulary at all: when the artifact\n * states a value for a target and an evidence entry attached to that target\n * asserts a different one, the work product contradicts itself, and a reviewer\n * clicking that line is shown a figure the package does not report there.\n *\n * ── The rule, and the honest citation it must not refuse ────────────────────\n *\n * \"Claim must equal the field\" is the tempting rule and it is unsatisfiable for\n * every aggregated or derived line. Measured on production row `a68b1943`:\n * line 1a is 189,750.00 and its two evidence rows cite $128,450.00 and\n * $61,300.00 — one per W-2, which is exactly the lineage a reviewer wants.\n * Line 8 is a Schedule C net profit of 11,056.56 evidenced by gross receipts of\n * 14,750.00 and four expense lines. Refusing those would delete correct work\n * and, on this codebase's measured history, buy invented citations in its place.\n *\n * So the refusal is narrower and it is a CONTRADICTION rather than an\n * inequality: the claim asserts a figure that the artifact itself assigns to a\n * DIFFERENT target. On row `95105c8a` that is precisely the crossed pair —\n * `line_3a` is 1955.02 in the artifact and its evidence claims 2204.18, which\n * the same artifact reports on `line_3b`. There is no honest reading of that:\n * the package cannot simultaneously say the number belongs on 3b and offer it\n * as the support for 3a.\n *\n * A component that appears nowhere else in the field map (128,450.00 of a\n * 189,750.00 total) is not a contradiction and is not refused. A claim\n * narrating a computation passes as soon as one of its figures is the target's\n * own value, which is what a narration of line 9 does by definition.\n */\n\n/** Canonical target → canonical value, for the artifact fields that state a\n * figure. Non-numeric fields are dropped: a filing status or a name has no\n * numeric contradiction to detect, and inventing a string comparison here\n * would re-open the fuzzy-matching hole the quote gate closes. */\nexport function indexArtifactValues(\n fields: Readonly<Record<string, unknown>> | undefined,\n normalizeTarget?: (target: string) => string,\n): Map<string, string> {\n const index = new Map<string, string>()\n for (const [key, raw] of Object.entries(fields ?? {})) {\n const value =\n typeof raw === 'number' && Number.isFinite(raw)\n ? canonicalizeValue(String(raw))\n : typeof raw === 'string'\n ? canonicalizeValue(raw)\n : null\n if (value === null) continue\n index.set(normalizeTarget ? normalizeTarget(key) : key, value)\n }\n return index\n}\n\nexport type ArtifactAgreement =\n /** The artifact states no figure for this target, or the claim asserts none. */\n | { status: 'not_applicable' }\n | { status: 'agrees'; value: string }\n /** The claim asserts a figure the artifact reports on a different target. */\n | { status: 'contradicts'; claimed: string; expected: string; belongsTo: string }\n\nexport function verifyArtifactAgreement(\n target: string,\n claim: string,\n fieldValues: ReadonlyMap<string, string>,\n): ArtifactAgreement {\n const expected = fieldValues.get(target)\n if (expected === undefined) return { status: 'not_applicable' }\n const claimed = claimValues(claim)\n if (claimed.length === 0) return { status: 'not_applicable' }\n if (claimed.includes(expected)) return { status: 'agrees', value: expected }\n for (const value of claimed) {\n for (const [other, otherValue] of fieldValues) {\n if (other === target || otherValue !== value) continue\n return { status: 'contradicts', claimed: value, expected, belongsTo: other }\n }\n }\n return { status: 'not_applicable' }\n}\n\n/** The sentence a model can act on: both numbers, both lines, and the two ways\n * out — move the citation, or correct the artifact. */\nexport function artifactAgreementErrorDetail(\n failure: Extract<ArtifactAgreement, { status: 'contradicts' }>,\n target: string,\n): string {\n return `the artifact reports ${failure.expected} on ${target} and ${failure.claimed} on ${failure.belongsTo}, so a citation claiming ${failure.claimed} does not support ${target} — it supports ${failure.belongsTo}. Attach this citation to ${failure.belongsTo}, or correct the artifact if ${target} really is ${failure.claimed}. The package cannot state both.`\n}\n","/**\n * The guarded work-product status machine — the `/missions` service PATTERN\n * (load → validate against a transition table → CAS-write guarded on what was\n * read → audit event) over {@link WorkProductStorePort}. Deliberately NOT a\n * reuse of the mission service: missions' cursor/plan/budget machinery does\n * not apply here, and the six-status review machine is a different contract.\n *\n * Concurrency contract: a scope's draft is driven by a single serialized turn\n * owner, so real contention is rare. The service is the typed guard layer,\n * not a serializer — every mutation re-reads the record and CAS-writes\n * guarded on the `{status, version}` it read. A guard miss surfaces as\n * `{ succeeded: false, conflict: true }` (retryable: re-read and re-apply),\n * never a silent clobber.\n */\n\nimport { canonicalizeValue } from './claim-support'\nimport {\n unresolvedBlockingExceptions,\n type EvidenceEntry,\n type ExceptionEntry,\n type QualityCheck,\n type WorkProductArtifact,\n type WorkProductAuditEvent,\n type WorkProductPatch,\n type WorkProductProvenance,\n type WorkProductRecord,\n type WorkProductStatus,\n type WorkProductStorePort,\n type WorkProductVersionEntry,\n} from './types'\n\n/** Discriminated outcome for guarded operations — `conflict` distinguishes a\n * lost guarded race (retryable) from a logic rejection (illegal edge,\n * missing row — deterministic, never retried). */\nexport type WorkProductOutcome<T> =\n | { succeeded: true; value: T }\n | { succeeded: false; error: string; conflict: boolean }\n\n// Legal status transitions. A target absent from a source's set is rejected\n// by the guarded helpers. `superseded` is terminal.\nconst WORK_PRODUCT_TRANSITIONS: Record<WorkProductStatus, ReadonlySet<WorkProductStatus>> = {\n draft: new Set<WorkProductStatus>(['blocked', 'ready']),\n blocked: new Set<WorkProductStatus>(['draft']),\n ready: new Set<WorkProductStatus>(['changes_requested', 'approved', 'superseded']),\n changes_requested: new Set<WorkProductStatus>(['draft', 'superseded']),\n approved: new Set<WorkProductStatus>(['superseded']),\n superseded: new Set<WorkProductStatus>(),\n}\n\n/** Whether a legal edge exists from `from` to `to` in the review machine. */\nexport function canTransitionWorkProduct(from: WorkProductStatus, to: WorkProductStatus): boolean {\n return WORK_PRODUCT_TRANSITIONS[from].has(to)\n}\n\n/** Statuses a work product can never leave. */\nexport function isWorkProductTerminal(status: WorkProductStatus): boolean {\n return WORK_PRODUCT_TRANSITIONS[status].size === 0\n}\n\n/** Define the input required to create a new draft work product row */\nexport interface CreateWorkProductInput {\n /** Explicit row id — omit to use the service's generator. The MODEL never\n * supplies ids; this is for deterministic server-side creation. */\n id?: string\n workspaceId: string\n threadId: string | null\n scopeKey: string\n /** Version this draft will become; default 1. The tool layer passes\n * `last reviewed version + 1` when a scope is re-engaged after approval. */\n version?: number\n provenance: WorkProductProvenance\n /** Opaque product-column values handed VERBATIM to the store's insert. */\n extras?: Record<string, unknown>\n}\n\n/** Payload for the draft→ready submit transition */\nexport interface SubmitWorkProductInput {\n artifact: WorkProductArtifact\n checks: QualityCheck[]\n provenance: WorkProductProvenance\n /** Frozen snapshot ref of this version's body for the history entry;\n * defaults to `artifact.path`. */\n artifactPath?: string\n}\n\n/** Reviewer verdict payload for the ready→approved / ready→changes_requested transition */\nexport interface WorkProductVerdictInput {\n verdict: 'approve' | 'request_changes'\n reviewedBy: string\n note?: string\n}\n\n/** Guarded mutation surface over the work-product store */\nexport interface WorkProductService {\n create(input: CreateWorkProductInput): Promise<WorkProductRecord>\n get(id: string): Promise<WorkProductRecord | null>\n /** The scope's open accumulator row (`draft`/`blocked`), or null. */\n openDraft(workspaceId: string, scopeKey: string): Promise<WorkProductRecord | null>\n /** The scope's `changes_requested` row awaiting its correction turn, or null. */\n awaitingCorrection(workspaceId: string, scopeKey: string): Promise<WorkProductRecord | null>\n /** The scope's `ready` row awaiting review, or null. */\n awaitingReview(workspaceId: string, scopeKey: string): Promise<WorkProductRecord | null>\n /** `max(version over the scope's approved/superseded rows) + 1` — the\n * version a fresh draft for the scope should carry. */\n nextVersion(workspaceId: string, scopeKey: string): Promise<number>\n /** Re-open a `changes_requested` row as the next draft: version bumps +1\n * and the correction turn accumulates into the same scope row. */\n reopen(id: string): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Merge evidence entries by id AND by what they assert (target + source +\n * claim), so re-stating a fact already recorded replaces it under whatever\n * id this batch minted rather than appending a near-copy. Legal only while\n * `draft`/`blocked`. */\n upsertEvidence(id: string, entries: readonly EvidenceEntry[]): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Merge exception entries by id, then reconcile the blocked flag: any\n * unresolved blocking entry parks `draft`→`blocked`; resolving the last\n * one releases `blocked`→`draft`. */\n upsertExceptions(id: string, entries: readonly ExceptionEntry[]): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Persist a checks array without transitioning — how a failed platform\n * gate (e.g. evidence_coverage) stays visible on the still-draft row. */\n recordChecks(id: string, checks: readonly QualityCheck[]): Promise<WorkProductOutcome<WorkProductRecord>>\n /** The terminal agent call: CAS `draft`→`ready` with artifact + checks +\n * provenance, appending the version-history entry. Refuses while an\n * unresolved blocking exception exists. */\n submit(id: string, input: SubmitWorkProductInput): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Reviewer verdict: CAS `ready`→`approved`/`changes_requested` + history\n * entry. Approval also supersedes the scope's prior approved versions so\n * exactly one approved version is current per scope. */\n applyVerdict(id: string, input: WorkProductVerdictInput): Promise<WorkProductOutcome<WorkProductRecord>>\n /** Explicit replacement: CAS to `superseded` (legal from ready /\n * changes_requested / approved). */\n supersede(id: string): Promise<WorkProductOutcome<WorkProductRecord>>\n}\n\n/** Configuration options for creating a work product service */\nexport interface WorkProductServiceOptions {\n store: WorkProductStorePort\n /** Injectable clock (epoch ms). Default `Date.now`. */\n now?: () => number\n /** Row-id generator. Default `crypto.randomUUID()`. */\n generateId?: () => string\n}\n\nfunction rejected<T>(error: string): WorkProductOutcome<T> {\n return { succeeded: false, error, conflict: false }\n}\n\nfunction lostRace<T>(id: string): WorkProductOutcome<T> {\n return { succeeded: false, error: `Work product ${id} changed concurrently`, conflict: true }\n}\n\n/** Merge-by-id upsert: existing order preserved, replaced in place, new\n * entries appended in call order. */\nfunction mergeById<T extends { id: string }>(existing: readonly T[], incoming: readonly T[]): T[] {\n const merged = existing.slice()\n const indexById = new Map(merged.map((entry, index) => [entry.id, index] as const))\n for (const entry of incoming) {\n const at = indexById.get(entry.id)\n if (at === undefined) {\n indexById.set(entry.id, merged.length)\n merged.push(entry)\n } else {\n merged[at] = entry\n }\n }\n return merged\n}\n\n/**\n * What makes two evidence rows THE SAME ROW, independent of the id the model\n * happened to mint for them.\n *\n * Merging on the model-supplied id alone is idempotent only if the model\n * re-uses ids, and it does not. Production row `95105c8a` carries 21 entries\n * for 7 targets: the same seven facts emitted three times across a turn under\n * `wages-line1a`, then `w2-wages`, then `wages` — every batch a fresh set of\n * ids, so every batch appended instead of replacing. The tool contract says\n * \"re-emit an id to replace\"; nothing made re-stating a fact you already\n * recorded a replace, and re-stating facts is what an agent does when it\n * revisits its work.\n *\n * So identity is what the row ASSERTS: this target, from this source, with\n * this value. Re-emit any of the three under any id and it replaces.\n *\n * The claim is part of the key on purpose, and it is the part that keeps\n * honest lineage alive: Form 1040 line 2b legitimately carries two rows from\n * one 1099-INT (box 1 and box 3), and line 1a two rows from two W-2s. Keying\n * on target+source alone would silently delete the second one — a merge rule\n * that destroys evidence is worse than the duplication it fixes. A claim that\n * IS a value keys on the canonical NUMBER, so `128450.00` and `128,450.00`\n * are one fact; any other claim keys on its folded text, which is\n * conservative: two different sentences stay two rows.\n */\nfunction evidenceIdentity(entry: EvidenceEntry): string {\n const whole = canonicalizeValue(entry.claim)\n const claimKey = whole ?? entry.claim.normalize('NFKC').toLowerCase().replace(/\\s+/gu, ' ').trim()\n return `${entry.target}\u0000${entry.sourceRef}\u0000${claimKey}`\n}\n\n/**\n * Upsert evidence by BOTH keys: the explicit id (the documented \"re-emit to\n * replace\") and the assertion identity above. An incoming entry replaces every\n * existing row it matches on either key, collapsing them to one at the position\n * of the earliest — so a re-stated fact patches the row it duplicates instead of\n * appending a near-copy, and a batch that re-states a fact twice lands once.\n */\nfunction mergeEvidence(existing: readonly EvidenceEntry[], incoming: readonly EvidenceEntry[]): EvidenceEntry[] {\n const merged = existing.slice()\n for (const entry of incoming) {\n const identity = evidenceIdentity(entry)\n const hits: number[] = []\n for (let index = 0; index < merged.length; index += 1) {\n const candidate = merged[index]!\n if (candidate.id === entry.id || evidenceIdentity(candidate) === identity) hits.push(index)\n }\n if (hits.length === 0) {\n merged.push(entry)\n continue\n }\n merged[hits[0]!] = entry\n for (const index of hits.slice(1).reverse()) merged.splice(index, 1)\n }\n return merged\n}\n\n/** Create the guarded work-product service over a store port */\nexport function createWorkProductService(options: WorkProductServiceOptions): WorkProductService {\n const { store } = options\n const now = options.now ?? (() => Date.now())\n const generateId = options.generateId ?? (() => crypto.randomUUID())\n\n async function appendEvent(\n record: WorkProductRecord,\n step: string,\n message: string,\n metadata: Record<string, unknown> = {},\n ): Promise<void> {\n await store.appendEvent({\n workProductId: record.id,\n workspaceId: record.workspaceId,\n step,\n message,\n metadata,\n at: now(),\n })\n }\n\n // Guarded status transition: load → validate the edge → CAS guarded on the\n // {status, version} read → audit event. The loser of a racing transition\n // gets a conflict instead of silently violating the machine.\n async function transition(\n id: string,\n to: WorkProductStatus,\n patch: Omit<WorkProductPatch, 'status'> = {},\n eventMeta: Record<string, unknown> = {},\n ): Promise<WorkProductOutcome<WorkProductRecord>> {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n const from = record.status\n if (isWorkProductTerminal(from)) {\n return rejected(`Work product ${id} is terminal (${from}); cannot transition to ${to}`)\n }\n if (!canTransitionWorkProduct(from, to)) {\n return rejected(`Illegal work-product transition ${from} -> ${to} for ${id}`)\n }\n const updated = await store.update(\n id,\n { status: from, version: record.version },\n { status: to, updatedAt: now(), ...patch },\n )\n if (!updated) return lostRace(id)\n await appendEvent(updated, `wp.${to}`, `Work product ${from} -> ${to}`, { from, to, ...eventMeta })\n return { succeeded: true, value: updated }\n }\n\n // Merge-style guarded write with no status change (evidence / exceptions /\n // checks accumulate on the open row).\n async function guardedMerge(\n id: string,\n legalStatuses: readonly WorkProductStatus[],\n build: (record: WorkProductRecord) => Omit<WorkProductPatch, 'status' | 'version'>,\n event: { step: string; message: (record: WorkProductRecord) => string; metadata?: (record: WorkProductRecord) => Record<string, unknown> },\n ): Promise<WorkProductOutcome<WorkProductRecord>> {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (!legalStatuses.includes(record.status)) {\n return rejected(`Work product ${id} is ${record.status}; expected ${legalStatuses.join('/')}`)\n }\n const updated = await store.update(\n id,\n { status: record.status, version: record.version },\n { updatedAt: now(), ...build(record) },\n )\n if (!updated) return lostRace(id)\n await appendEvent(updated, event.step, event.message(updated), event.metadata?.(updated) ?? {})\n return { succeeded: true, value: updated }\n }\n\n const create: WorkProductService['create'] = async (input) => {\n const at = now()\n const record = await store.insert(\n {\n id: input.id ?? generateId(),\n workspaceId: input.workspaceId,\n threadId: input.threadId,\n scopeKey: input.scopeKey,\n status: 'draft',\n version: input.version ?? 1,\n artifact: null,\n evidence: [],\n exceptions: [],\n checks: [],\n provenance: input.provenance,\n history: [],\n createdAt: at,\n updatedAt: at,\n },\n input.extras,\n )\n await appendEvent(record, 'wp.created', `Work product draft v${record.version} created for ${record.scopeKey}`, {\n scopeKey: record.scopeKey,\n version: record.version,\n threadId: record.threadId,\n })\n return record\n }\n\n async function findByScopeAndStatus(\n workspaceId: string,\n scopeKey: string,\n status: WorkProductStatus,\n ): Promise<WorkProductRecord | null> {\n const rows = await store.listByWorkspace(workspaceId, { status: [status] })\n return rows.find((row) => row.scopeKey === scopeKey) ?? null\n }\n\n const nextVersion: WorkProductService['nextVersion'] = async (workspaceId, scopeKey) => {\n const reviewed = await store.listByWorkspace(workspaceId, { status: ['approved', 'superseded'] })\n const versions = reviewed.filter((row) => row.scopeKey === scopeKey).map((row) => row.version)\n return versions.length === 0 ? 1 : Math.max(...versions) + 1\n }\n\n const reopen: WorkProductService['reopen'] = async (id) => {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (record.status !== 'changes_requested') {\n return rejected(`Work product ${id} is ${record.status}; only changes_requested reopens`)\n }\n const updated = await store.update(\n id,\n { status: 'changes_requested', version: record.version },\n { status: 'draft', version: record.version + 1, updatedAt: now() },\n )\n if (!updated) return lostRace(id)\n await appendEvent(updated, 'wp.reopened', `Correction draft v${updated.version} opened`, {\n from: record.version,\n to: updated.version,\n })\n return { succeeded: true, value: updated }\n }\n\n const upsertEvidence: WorkProductService['upsertEvidence'] = (id, entries) =>\n guardedMerge(\n id,\n ['draft', 'blocked'],\n (record) => ({ evidence: mergeEvidence(record.evidence, entries) }),\n {\n step: 'wp.evidence',\n message: (record) => `Evidence upserted (${entries.length} entries, ${record.evidence.length} total)`,\n metadata: () => ({ upserted: entries.map((entry) => entry.id) }),\n },\n )\n\n const upsertExceptions: WorkProductService['upsertExceptions'] = async (id, entries) => {\n const merged = await guardedMerge(\n id,\n ['draft', 'blocked'],\n (record) => ({ exceptions: mergeById(record.exceptions, entries) }),\n {\n step: 'wp.exception',\n message: (record) =>\n `Exceptions upserted (${entries.length} entries, ${unresolvedBlockingExceptions(record.exceptions).length} blocking unresolved)`,\n metadata: () => ({ upserted: entries.map((entry) => entry.id) }),\n },\n )\n if (!merged.succeeded) return merged\n // Reconcile the blocked flag AFTER the merge commits: an unresolved\n // blocking entry parks the draft; resolving the last one releases it.\n const record = merged.value\n const blocking = unresolvedBlockingExceptions(record.exceptions).length\n if (record.status === 'draft' && blocking > 0) {\n return transition(id, 'blocked', {}, { blocking })\n }\n if (record.status === 'blocked' && blocking === 0) {\n return transition(id, 'draft', {}, { blocking })\n }\n return merged\n }\n\n const recordChecks: WorkProductService['recordChecks'] = (id, checks) =>\n guardedMerge(\n id,\n ['draft', 'blocked'],\n () => ({ checks: checks.slice() }),\n {\n step: 'wp.checks',\n message: () => `Checks recorded (${checks.length}, ${checks.filter((check) => !check.passed).length} failed)`,\n metadata: () => ({ failed: checks.filter((check) => !check.passed).map((check) => check.name) }),\n },\n )\n\n const submit: WorkProductService['submit'] = async (id, input) => {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (record.status !== 'draft') {\n return rejected(`Work product ${id} is ${record.status}; only a draft submits`)\n }\n const blocking = unresolvedBlockingExceptions(record.exceptions)\n if (blocking.length > 0) {\n return rejected(\n `Work product ${id} has ${blocking.length} unresolved blocking exception(s): ${blocking.map((entry) => entry.id).join(', ')}`,\n )\n }\n const artifactPath = input.artifactPath ?? input.artifact.path\n const entry: WorkProductVersionEntry = {\n version: record.version,\n status: 'ready',\n provenance: input.provenance,\n ...(artifactPath === undefined ? {} : { artifactPath }),\n at: now(),\n }\n return transition(\n id,\n 'ready',\n {\n artifact: input.artifact,\n checks: input.checks.slice(),\n provenance: input.provenance,\n history: [...record.history, entry],\n },\n { version: record.version, failedChecks: input.checks.filter((check) => !check.passed).length },\n )\n }\n\n const applyVerdict: WorkProductService['applyVerdict'] = async (id, input) => {\n const record = await store.load(id)\n if (!record) return rejected(`Work product ${id} not found`)\n if (record.status !== 'ready') {\n return rejected(`Work product ${id} is ${record.status}; a verdict applies only to ready`)\n }\n const to: WorkProductStatus = input.verdict === 'approve' ? 'approved' : 'changes_requested'\n const entry: WorkProductVersionEntry = {\n version: record.version,\n status: to,\n provenance: record.provenance,\n ...(record.artifact?.path === undefined ? {} : { artifactPath: record.artifact.path }),\n reviewedBy: input.reviewedBy,\n ...(input.note === undefined ? {} : { reviewNote: input.note }),\n at: now(),\n }\n const outcome = await transition(\n id,\n to,\n { history: [...record.history, entry] },\n { verdict: input.verdict, reviewedBy: input.reviewedBy },\n )\n if (!outcome.succeeded || to !== 'approved') return outcome\n // Exactly one approved version per scope: supersede prior approved rows.\n const priorApproved = await store.listByWorkspace(record.workspaceId, { status: ['approved'] })\n for (const prior of priorApproved) {\n if (prior.id === id || prior.scopeKey !== record.scopeKey) continue\n await transition(prior.id, 'superseded', {}, { supersededBy: id })\n }\n return outcome\n }\n\n return {\n create,\n get: (id) => store.load(id),\n openDraft: (workspaceId, scopeKey) => store.findDraft(workspaceId, scopeKey),\n awaitingCorrection: (workspaceId, scopeKey) => findByScopeAndStatus(workspaceId, scopeKey, 'changes_requested'),\n awaitingReview: (workspaceId, scopeKey) => findByScopeAndStatus(workspaceId, scopeKey, 'ready'),\n nextVersion,\n reopen,\n upsertEvidence,\n upsertExceptions,\n recordChecks,\n submit,\n applyVerdict,\n supersede: (id) => transition(id, 'superseded'),\n }\n}\n\n// ── in-memory store ──────────────────────────────────────────────────────────\n\n/** In-memory store surface with audit trail access and unguarded direct writes for tests */\nexport interface InMemoryWorkProductStore extends WorkProductStorePort {\n /** The full audit trail, append order. */\n events(): WorkProductAuditEvent[]\n /** Unguarded direct write — simulates a concurrent owner in tests. */\n put(record: WorkProductRecord): void\n}\n\n/**\n * In-memory {@link WorkProductStorePort} — the portable backend for tests and\n * reference assemblies. Records are deep-copied on every boundary so callers\n * can never mutate stored state around the guards.\n */\nexport function createInMemoryWorkProductStore(): InMemoryWorkProductStore {\n const rows = new Map<string, WorkProductRecord>()\n const events: WorkProductAuditEvent[] = []\n\n return {\n async load(id) {\n const record = rows.get(id)\n return record ? structuredClone(record) : null\n },\n async findDraft(workspaceId, scopeKey) {\n for (const record of rows.values()) {\n if (\n record.workspaceId === workspaceId &&\n record.scopeKey === scopeKey &&\n (record.status === 'draft' || record.status === 'blocked')\n ) {\n return structuredClone(record)\n }\n }\n return null\n },\n async listByWorkspace(workspaceId, opts) {\n const out: WorkProductRecord[] = []\n for (const record of rows.values()) {\n if (record.workspaceId !== workspaceId) continue\n if (opts?.status && !opts.status.includes(record.status)) continue\n out.push(structuredClone(record))\n }\n return out\n },\n async insert(record) {\n if (rows.has(record.id)) throw new Error(`Work product ${record.id} already exists`)\n rows.set(record.id, structuredClone(record))\n return structuredClone(record)\n },\n async update(id, guard, patch) {\n const current = rows.get(id)\n if (!current) return null\n if (guard.status !== undefined && current.status !== guard.status) return null\n if (guard.version !== undefined && current.version !== guard.version) return null\n const next: WorkProductRecord = { ...current }\n if (patch.status !== undefined) next.status = patch.status\n if (patch.version !== undefined) next.version = patch.version\n if (patch.artifact !== undefined) next.artifact = patch.artifact\n if (patch.evidence !== undefined) next.evidence = patch.evidence\n if (patch.exceptions !== undefined) next.exceptions = patch.exceptions\n if (patch.checks !== undefined) next.checks = patch.checks\n if (patch.provenance !== undefined) next.provenance = patch.provenance\n if (patch.history !== undefined) next.history = patch.history\n if (patch.updatedAt !== undefined) next.updatedAt = patch.updatedAt\n rows.set(id, structuredClone(next))\n return structuredClone(next)\n },\n async appendEvent(event) {\n events.push(structuredClone(event))\n },\n events() {\n return events.map((event) => structuredClone(event))\n },\n put(record) {\n rows.set(record.id, structuredClone(record))\n },\n }\n}\n","/**\n * Provenance stamping — the backtest spine. Every version of every work\n * product carries `profileHash + runId + servingModels`, stamped at the two\n * honest moments:\n *\n * 1. At dispatch, the product route composes the turn's profile and computes\n * agent-eval's `agentProfileHash(profile)` — the SAME hash that keys the\n * app's scorecard cells — and closes it (plus runId/sessionId) into the\n * `provenance` seam `buildWorkProductTools` receives, so the model can\n * neither omit nor forge it.\n * 2. At turn completion, the route's existing lifecycle seam calls\n * {@link finalizeWorkProductProvenance}: serving model and cost come from\n * the usage receipt, because only the completed turn knows what actually\n * served. `servingModels` is honestly EMPTY until then.\n *\n * The trust bridge ({@link workProductTrustInputs}) maps judge-sourced\n * quality verdicts into `/eval-campaign`'s `trustVerdicts()` input with\n * type-only imports — products call `trustVerdicts` from\n * `@tangle-network/agent-app/eval-campaign` (which they already import for\n * their ensemble loop). Deliberately NOT a value re-export here: it would put\n * agent-eval's runtime on `/work-product`'s import path and drag the eval\n * engine into every product worker bundle.\n */\n\nimport type { JudgeVerdict } from '@tangle-network/agent-eval'\nimport type { TrustItem } from '../eval-campaign/trust-gate'\nimport type {\n WorkProductProvenance,\n WorkProductRecord,\n WorkProductStorePort,\n} from './types'\n\nexport type { TrustItem }\n\n/** The dispatch-time provenance closure's output: everything the route knows\n * before the turn completes. */\nexport type WorkProductProvenanceBase = Omit<WorkProductProvenance, 'servingModels' | 'producedAt'>\n\n/** Stamp a full provenance from the dispatch-time base. `servingModels` starts\n * empty (honestly absent — never guessed) until the completion back-fill. */\nexport function stampProvenance(base: WorkProductProvenanceBase, now: () => number = Date.now): WorkProductProvenance {\n return { ...base, servingModels: [], producedAt: now() }\n}\n\n/** Completion receipt for one run, from the turn's usage/lifecycle seam. */\nexport interface FinalizeWorkProductProvenanceInput {\n workspaceId: string\n /** The chat turnId / mission-step run id the records were stamped with. */\n runId: string\n /** What actually served, from the usage receipt / serving-model header. */\n servingModels: readonly string[]\n costUsd?: number\n logger?: Pick<Console, 'warn'>\n}\n\n/**\n * Back-fill `servingModels`/`costUsd` onto every record (and its history\n * entries) stamped with `runId` — wire it into the chat route's existing\n * `lifecycle.onTurnComplete` seam; no new hook. Returns the updated records.\n * A CAS miss on one record is logged and skipped (the next completion or a\n * re-read re-applies); it never throws mid-fleet.\n */\nexport async function finalizeWorkProductProvenance(\n store: WorkProductStorePort,\n input: FinalizeWorkProductProvenanceInput,\n): Promise<WorkProductRecord[]> {\n const rows = await store.listByWorkspace(input.workspaceId)\n const updated: WorkProductRecord[] = []\n for (const record of rows) {\n const recordMatches = record.provenance.runId === input.runId\n const historyMatches = record.history.some((entry) => entry.provenance.runId === input.runId)\n if (!recordMatches && !historyMatches) continue\n const finalize = (provenance: WorkProductProvenance): WorkProductProvenance => ({\n ...provenance,\n servingModels: [...input.servingModels],\n ...(input.costUsd === undefined ? {} : { costUsd: input.costUsd }),\n })\n const next = await store.update(\n record.id,\n { status: record.status, version: record.version },\n {\n ...(recordMatches ? { provenance: finalize(record.provenance) } : {}),\n history: record.history.map((entry) =>\n entry.provenance.runId === input.runId ? { ...entry, provenance: finalize(entry.provenance) } : entry,\n ),\n },\n )\n if (!next) {\n input.logger?.warn(`[work-product] provenance back-fill lost a race on ${record.id}; skipped`)\n continue\n }\n await store.appendEvent({\n workProductId: record.id,\n workspaceId: record.workspaceId,\n step: 'wp.provenance',\n message: `Serving models back-filled for run ${input.runId}`,\n metadata: { runId: input.runId, servingModels: [...input.servingModels], costUsd: input.costUsd ?? null },\n at: Date.now(),\n })\n updated.push(next)\n }\n return updated\n}\n\n/**\n * Trust-gate bridge: one `TrustItem` per work product whose production\n * quality was scored by the product's eval-campaign ensemble. `verdictsFor`\n * returns the per-judge raw verdicts the product retained for a record (the\n * same verdicts `aggregateJudgeVerdicts` reduces); records without verdicts\n * are omitted. Feed the result to `/eval-campaign`'s `trustVerdicts()` —\n * an untrusted verdict renders as \"quality: unverified\", never a naked\n * number. Zero statistics code here: pure mapping.\n */\nexport function workProductTrustInputs<D extends string = string>(\n records: readonly WorkProductRecord[],\n verdictsFor: (record: WorkProductRecord) => readonly JudgeVerdict<D>[] | undefined,\n): TrustItem<D>[] {\n const items: TrustItem<D>[] = []\n for (const record of records) {\n const verdicts = verdictsFor(record)\n if (!verdicts || verdicts.length === 0) continue\n items.push({ itemId: record.id, verdicts })\n }\n return items\n}\n","/**\n * Verbatim quote verification — the lineage gate.\n *\n * An evidence `locator.quote` is the reviewer's click target: it must land on\n * text that is actually in the document the entry names. Nothing here is\n * fuzzy. The only latitude is representational — the same characters written\n * differently by a PDF extractor, a text transcription, or a model repeating\n * what it read: Unicode compatibility forms, curly quotes, the several dash\n * codepoints, non-breaking spaces, and run-length whitespace. A quote that\n * still does not occur after that is not a formatting difference; it is text\n * the document does not contain.\n *\n * Deliberately NOT tolerated, because each one re-opens the hole this closes:\n * case (a quote is a quote), token subsets, edit distance, word-overlap\n * scoring, and \"the numbers match\" heuristics. A fabricated quote and a real\n * one differ by exactly the thing a fuzzy matcher forgives.\n */\n\nimport { canonicalizeValue, valuesInText } from './claim-support'\n\n/** Whitespace folded to a single ASCII space: the Unicode space separators\n * (`\\p{Zs}`), the line/paragraph separators, and the zero-width characters a\n * PDF text layer leaves behind (ZWSP / ZWNJ / ZWJ / BOM). */\nconst WHITESPACE = /[\\s\\p{Zs}\\u2028\\u2029\\u200b-\\u200d\\ufeff]+/gu\n\n/** Dash-like codepoints that render alike but differ by byte: non-breaking\n * hyphen, figure / en / em / horizontal dash, minus sign, small and fullwidth\n * forms. */\nconst DASHES = /[\\u2010-\\u2015\\u2212\\ufe58\\ufe63\\uff0d]/gu\n\n/** Curly, prime and grave apostrophes folded to ASCII `'`. */\nconst SINGLE_QUOTES = /[\\u2018\\u2019\\u201a\\u201b\\u2032\\u2035\\u00b4`]/gu\n\n/** Curly, low, prime and guillemet double quotes folded to ASCII `\"`. */\nconst DOUBLE_QUOTES = /[\\u201c\\u201d\\u201e\\u201f\\u2033\\u2036\\u00ab\\u00bb]/gu\n\n/**\n * Fold a string to the form both sides of a quote comparison are measured in.\n * Composition and presentation only: NFKC, one dash, one apostrophe, one\n * double quote, runs of any whitespace to a single space, trimmed. Case is\n * PRESERVED — lowercasing would let \"Box 1\" match \"box 1\", and a citation\n * that cannot reproduce capitalization did not read the document.\n */\nexport function normalizeQuoteText(value: string): string {\n return value\n .normalize('NFKC')\n .replace(DASHES, '-')\n .replace(SINGLE_QUOTES, \"'\")\n .replace(DOUBLE_QUOTES, '\"')\n .replace(WHITESPACE, ' ')\n .trim()\n}\n\n/**\n * Does `quote` occur in `sourceText`? Exact substring first (the common case,\n * and free); the normalized comparison second, for the representational\n * differences above. Nothing else.\n *\n * An empty or whitespace-only quote is NOT a match — it would otherwise be a\n * substring of every document and pass the gate vacuously.\n */\nexport function sourceContainsQuote(sourceText: string, quote: string): boolean {\n const trimmed = quote.trim()\n if (trimmed.length === 0) return false\n if (sourceText.includes(trimmed)) return true\n const normalizedQuote = normalizeQuoteText(quote)\n if (normalizedQuote.length === 0) return false\n return normalizeQuoteText(sourceText).includes(normalizedQuote)\n}\n\n// ── spans: the citation form that cannot be wrong ────────────────────────────\n\n/**\n * Slice a citation out of the source text by character offset — the reason\n * this module exists in its stronger form.\n *\n * Verification above is a REJECTION gate: the model retypes a quote and the\n * shell refuses it when the characters do not occur. That gate is correct and\n * it works, but a model that reproduces a line character-for-character only\n * some of the time cannot USE it — every miss is a refusal, and the package\n * ends up with no lineage at all rather than false lineage. A measured 9 of 59\n * on the live tax surface is what \"some of the time\" meant in practice.\n *\n * A span inverts it. The model names two integers into text it just read; the\n * PLATFORM produces the quote from the bytes it already holds. There is no\n * retyping step to get wrong, so a fabricated quote is not rejected — it is\n * unrepresentable. `sourceContainsQuote(text, sliceSourceSpan(text, span))` is\n * true for every span this function returns, by construction.\n *\n * Offsets index the SAME string the product's document-reading tool pages\n * with an offset, which is the same string its `readSourceText` seam returns.\n * That is the one contract a product must keep; violate it and spans point at\n * the wrong characters (still real characters of that document — never\n * invented text, but the wrong line).\n *\n * Half-open `[start, end)`, matching `String.prototype.slice` and the\n * `offset`/`offset + text.length` window a paged read already reports.\n */\nexport type SourceSpanFailure =\n | { reason: 'not_integer'; field: 'start' | 'end' }\n | { reason: 'negative'; field: 'start' | 'end' }\n | { reason: 'inverted' }\n | { reason: 'out_of_range'; totalChars: number }\n | { reason: 'blank' }\n\nexport type SourceSpanResult =\n | { ok: true; quote: string }\n | { ok: false; failure: SourceSpanFailure }\n\n/**\n * Locate the line containing `value` and return it as a span — the citation\n * form for a model that cannot count characters.\n *\n * Measured on production (tax session 135b7cc3, gpt-4.1-mini): given the\n * document text and told exactly which line to cite, the model produced\n * offsets that landed on the WRONG line four times out of four, then missed\n * again on a second attempt after being shown the text its offsets had\n * selected. Character arithmetic is not something this model class does.\n *\n * What it DOES do reliably is read the value: all four `claim` fields in that\n * same run were correct to the cent. So the model names the value it read and\n * the PLATFORM finds it. Both failure modes close at once —\n *\n * - the quote cannot be invented, because the platform slices it;\n * - the span cannot be mis-addressed, because the platform computed it from\n * a needle it PROVED occurs in the text.\n *\n * A needle that is not in the document is refused, which is the same fail-loud\n * posture as a quote that does not occur — and correctly so: the model is\n * asserting the document says something it does not.\n *\n * The cited span is the whole LINE, not the needle: \"128,450.00\" alone is not\n * a click target a reviewer can judge, whereas the line it sits on says what\n * the number IS. Number-only needles are common and deliberately supported.\n */\nexport type SourceFindFailure =\n | { reason: 'blank_needle' }\n | { reason: 'not_found' }\n | { reason: 'occurrence_out_of_range'; found: number }\n | { reason: 'not_distinctive'; needle: string; found: number }\n\n/** A needle must be long enough to identify a place in the document. Two\n * characters is not a citation: measured on production work product\n * b9a37e44, nine evidence entries for lines the 1099-DIV does not state were\n * cited with the value \"0\", and the platform faithfully matched the \"0\"\n * inside \"Tax Year 2025\" in the header. Every one re-sliced byte-exactly and\n * every one was worthless as evidence. */\nconst MIN_NEEDLE_LENGTH = 3\n\n/** Above this many hits the needle names no particular place, so citing the\n * first is arbitrary rather than evidential. `findOccurrence` remains the way\n * to cite a genuinely repeated value on purpose. */\nconst MAX_AMBIGUOUS_MATCHES = 8\n\nexport type SourceFindResult =\n | { ok: true; span: { start: number; end: number }; quote: string; occurrences: number }\n | { ok: false; failure: SourceFindFailure }\n\n/** Line bounds containing `index`, trimmed of the newline terminators. */\nfunction lineAround(text: string, index: number): { start: number; end: number } {\n let start = text.lastIndexOf('\\n', index)\n start = start < 0 ? 0 : start + 1\n let end = text.indexOf('\\n', index)\n if (end < 0) end = text.length\n // A CRLF document leaves a trailing \\r inside the line; drop it so the\n // quote is the line a reader sees rather than the line plus a control char.\n if (end > start && text[end - 1] === '\\r') end -= 1\n return { start, end }\n}\n\nexport function findSourceLine(\n sourceText: string,\n needle: string,\n occurrence = 1,\n): SourceFindResult {\n const trimmed = needle.trim()\n if (trimmed.length === 0) return { ok: false, failure: { reason: 'blank_needle' } }\n if (trimmed.length < MIN_NEEDLE_LENGTH) {\n return { ok: false, failure: { reason: 'not_distinctive', needle: trimmed, found: 0 } }\n }\n\n /** Walk the document a line at a time. */\n const eachLine = (visit: (bound: { start: number; end: number }, line: string) => void): void => {\n let cursor = 0\n while (cursor <= sourceText.length) {\n const bound = lineAround(sourceText, cursor)\n visit(bound, sourceText.slice(bound.start, bound.end))\n if (bound.end >= sourceText.length) break\n cursor = bound.end + 1\n }\n }\n\n const positions: number[] = []\n\n // A needle that IS a figure is matched BY VALUE, per line — never as a\n // substring. Substring matching quietly answers the wrong question here: a\n // model citing a genuine \"450.00\" line in a document that also carries\n // \"128,450.00\" gets the WAGE line back, because \"450.00\" is a tail of it and\n // occurs earlier. The citation then fails claim support and the honest\n // anchor is unreachable at occurrence 1, which is the unsatisfiable-gate\n // failure this whole area exists to avoid. Matching values also subsumes the\n // representation cases the old second-chance pass handled — \"128450.00\"\n // finds \"128,450.00\" because both canonicalize to the same number.\n const wantedValue = canonicalizeValue(trimmed)\n if (wantedValue !== null) {\n eachLine((bound, line) => {\n if (valuesInText(line).includes(wantedValue)) positions.push(bound.start)\n })\n } else {\n // A PHRASE needle. Exact hits first (free, and the common case), then the\n // normalized comparison ONLY to locate a position — the returned quote is\n // always sliced from the ORIGINAL string, so representation folding never\n // leaks into a stored citation.\n for (let at = sourceText.indexOf(trimmed); at >= 0; at = sourceText.indexOf(trimmed, at + 1)) {\n positions.push(at)\n }\n if (positions.length === 0) {\n const wanted = normalizeQuoteText(trimmed)\n if (wanted.length > 0) {\n eachLine((bound, line) => {\n if (normalizeQuoteText(line).includes(wanted)) positions.push(bound.start)\n })\n }\n }\n }\n if (positions.length === 0) return { ok: false, failure: { reason: 'not_found' } }\n // An explicit `findOccurrence` is the caller saying \"yes, it repeats, I mean\n // that one\" — so ambiguity is only a failure when they did NOT say which.\n if (occurrence === 1 && positions.length > MAX_AMBIGUOUS_MATCHES) {\n return { ok: false, failure: { reason: 'not_distinctive', needle: trimmed, found: positions.length } }\n }\n if (occurrence < 1 || occurrence > positions.length) {\n return { ok: false, failure: { reason: 'occurrence_out_of_range', found: positions.length } }\n }\n\n const bound = lineAround(sourceText, positions[occurrence - 1]!)\n const quote = sourceText.slice(bound.start, bound.end)\n if (quote.trim().length === 0) return { ok: false, failure: { reason: 'not_found' } }\n return { ok: true, span: bound, quote, occurrences: positions.length }\n}\n\n/** Resolve `[start, end)` against `sourceText`. Every rejection is a caller\n * mistake the model can correct from the paged read it already has, so each\n * carries the discriminator a tool layer turns into a specific message. */\nexport function sliceSourceSpan(\n sourceText: string,\n span: { start: number; end: number },\n): SourceSpanResult {\n for (const field of ['start', 'end'] as const) {\n const value = span[field]\n if (!Number.isInteger(value)) return { ok: false, failure: { reason: 'not_integer', field } }\n if (value < 0) return { ok: false, failure: { reason: 'negative', field } }\n }\n if (span.end <= span.start) return { ok: false, failure: { reason: 'inverted' } }\n if (span.end > sourceText.length) {\n return { ok: false, failure: { reason: 'out_of_range', totalChars: sourceText.length } }\n }\n const quote = sourceText.slice(span.start, span.end)\n // A whitespace-only slice is a real slice of the document and would pass\n // `sourceContainsQuote` on any text — the same vacuous pass an empty quote\n // gets there, refused for the same reason: it is not a click target.\n if (quote.trim().length === 0) return { ok: false, failure: { reason: 'blank' } }\n return { ok: true, quote }\n}\n","/**\n * The agent-facing work-product side channel — three registry `customTools`\n * built on `/tools`' `defineAppTool`, dispatched through `dispatchAppTool`'s\n * single validation/outcome path (a thrown `ToolInputError` → correctable 4xx\n * back to the model; any other throw → internal error; a call never silently\n * succeeds without its effect). Deliberately NOT new `/tools` built-ins:\n * extending `AppToolHandlers` would break every existing consumer, and the\n * registry seam exists precisely for a product tool family like this.\n *\n * Partial-emission contract across a long turn: the draft ROW is the\n * accumulator. Evidence and exceptions stream in as found via small batched\n * calls; the artifact arrives once at the end. The agent addresses everything\n * by `scopeKey` — the server mints row ids, the model never invents them, and\n * identity (userId/workspaceId/threadId) rides the trusted `AppToolContext`\n * from headers, never model args. A turn that dies mid-emission leaves a\n * consistent draft the next turn resumes by the same scopeKey.\n */\n\nimport { ToolInputError } from '../tools/errors'\nimport { defineAppTool, type AppToolDefinition } from '../tools/registry'\nimport type { AppToolContext } from '../tools/types'\nimport { createWorkProductService, type WorkProductOutcome, type WorkProductService } from './service'\nimport { stampProvenance, type WorkProductProvenanceBase } from './provenance'\nimport { findSourceLine, sliceSourceSpan, sourceContainsQuote, type SourceFindFailure, type SourceSpanFailure } from './quote'\nimport {\n artifactAgreementErrorDetail,\n claimSupportErrorDetail,\n indexArtifactValues,\n targetLabelErrorDetail,\n verifyArtifactAgreement,\n verifyClaimSupport,\n verifyTargetLabel,\n type ConfusableTargetGroup,\n} from './claim-support'\nimport {\n parseAgentCheckInput,\n parseArtifactInput,\n parseEvidenceInput,\n parseExceptionInput,\n unresolvedBlockingExceptions,\n type EvidenceEntry,\n type ExceptionEntry,\n type QualityCheck,\n type WorkProductArtifact,\n type WorkProductRecord,\n type WorkProductStorePort,\n} from './types'\n\n/** Max entries per `upsert_evidence`/`flag_exception` call — keeps each call a\n * small batch the model can correct precisely on a named-index failure. */\nexport const MAX_WORK_PRODUCT_BATCH = 50\n\n/** Platform check: every material target has ≥1 evidence row. Recorded as\n * `QualityCheck{source:'platform'}`. */\nexport const EVIDENCE_COVERAGE_CHECK = 'evidence_coverage'\n\n/** Platform check: how many evidence entries carry a quote the shell PROVED\n * occurs in the source it names. Recorded when `readSourceText` is wired, so\n * a reviewer reads the strength of the lineage off the row itself rather than\n * trusting that a quote was checked. */\nexport const QUOTE_VERIFICATION_CHECK = 'quote_verification'\n\n/** Platform check: how many quoted evidence entries anchor to text that\n * actually CARRIES the figure the entry claims. Distinct from\n * `quote_verification`, which only proves the text came from the document —\n * production row `7256ef49` passed that one on all four entries while\n * supporting none of them. */\nexport const CLAIM_SUPPORT_CHECK = 'claim_support'\n\n/** Platform check: how many citations anchor to a line that belongs to the\n * TARGET they are attached to, rather than to a sibling target's line.\n * Recorded when the product declares `confusableTargets`; `claim_support`\n * passes a crossed pair by construction, because the figure really is on the\n * line — the wrong one. */\nexport const TARGET_CORRECTNESS_CHECK = 'target_correctness'\n\n/** Platform check: how many evidence claims agree with the artifact field they\n * decorate. A package whose evidence reports one figure on a line and whose\n * artifact reports another contradicts itself; every other gate on this row\n * reads only one of the two halves. */\nexport const ARTIFACT_AGREEMENT_CHECK = 'artifact_agreement'\n\n/** Domain seams for the three work-product tools — every domain word is a\n * parameter; the shell bakes none. */\nexport interface WorkProductToolConfig {\n store: WorkProductStorePort\n /** PARAMETER — accepted `artifact.kind` values, validated on submit. */\n artifactKinds: readonly string[]\n /** PARAMETER — accepted exception `kind` values. */\n exceptionKinds: readonly string[]\n /** Fail-loud source check: resolve an evidence `sourceRef` to existence\n * (vault stat / attachment lookup). A dangling ref is a `ToolInputError`\n * naming the entry index — lineage can never point at nothing. */\n resolveSourceRef: (ref: string, ctx: AppToolContext) => Promise<boolean>\n /** Fail-loud QUOTE verification: return the source document's TEXT for a\n * ref so the shell can prove each `locator.quote` occurs in it verbatim.\n * Wiring this turns the gate ON — a quote that does not occur is a\n * `ToolInputError` naming the entry index, so the model re-extracts from\n * the document instead of persisting invented lineage.\n *\n * Return `null` ONLY when the ref genuinely has no extractable text (an\n * image scan, an opaque blob). The gate is fail-CLOSED on `null`: a quote\n * that cannot be checked is refused, because \"unverifiable\" and \"verified\"\n * must never look the same to a reviewer. Such an entry is still recordable\n * without `locator.quote` — `claim` carries the assertion.\n *\n * Omit the seam entirely and no quote is checked. */\n readSourceText?: (ref: string, ctx: AppToolContext) => Promise<string | null>\n /** The material targets the platform coverage check requires evidence for.\n * Product-owned vocabulary; omit to skip the coverage gate.\n *\n * Return ONLY targets a source document can actually evidence. A gate that\n * demands document lineage for a value the session COMPUTED is not\n * satisfiable by any honest answer, and an unsatisfiable gate does not stop\n * a submit — it selects for an invented one. Computed values belong to a\n * product's own computation check, where \"matches what we already\n * calculated\" is satisfiable by construction. */\n materialTargets?: (artifact: WorkProductArtifact) => string[]\n /** Require every material target to carry a SOURCE ANCHOR — a span-sliced or\n * verified quote — not merely an evidence row. Off by default (a bare\n * `claim` is legitimate lineage for products with no readable sources);\n * turn it ON once `readSourceText` is wired and `materialTargets` names only\n * document-derived targets, and coverage stops being satisfiable by\n * assertion. */\n requireAnchoredEvidence?: boolean\n /** Verify that each anchored quote CARRIES the figure its entry claims.\n * ON by default — an anchor that does not support its claim is the one\n * failure mode every other gate here passes, and it reads to a reviewer as\n * the most authoritative citation on the row.\n *\n * Only claims that assert a figure are checked, so a filing status, a name\n * or a date is unaffected; see `./claim-support` for exactly where the line\n * is drawn and why it is drawn to stay satisfiable. Set `false` only for a\n * product whose claims are figures the source states in a form no numeric\n * comparison can reach. */\n verifyClaimSupport?: boolean\n /** Fold an evidence `target` (and every `materialTargets` name) to the\n * product's ONE canonical spelling.\n *\n * Without it a target namespace forks and every check that joins evidence\n * to the artifact by name silently half-works. Measured on production row\n * `95105c8a`: the same seven form lines arrived as `line_3a` and\n * `f1040.line_3a` in one turn, so coverage, deduplication and artifact\n * agreement each saw two unrelated targets where the return has one line.\n *\n * Pure and total — it runs on every ingested entry and on the coverage\n * target list, so a target it does not recognize must come back unchanged\n * rather than throw. */\n normalizeTarget?: (target: string) => string\n /** Targets whose SOURCE LINES are mistakable for each other, with the\n * phrases that tell them apart — the product's vocabulary, compared by the\n * shell. Omit and no target-correctness check runs.\n *\n * Read negatively: an entry is refused only when the line it cites carries\n * a sibling target's label and none of its own, so an unusually-labelled\n * document never costs an honest citation. See `./claim-support` for why\n * the positive form (\"the line must say X\") is the wrong shape. */\n confusableTargets?: readonly ConfusableTargetGroup[]\n /** Refuse an evidence claim that asserts a figure the artifact reports on a\n * DIFFERENT target. ON by default and domain-free — it compares the package\n * to itself. Set `false` only for a product whose evidence claims are not\n * the artifact's own figures. */\n verifyArtifactAgreement?: boolean\n /** Per-turn provenance closure the ROUTE supplies (profileHash + runId are\n * known at dispatch; trusted, never read from model args). */\n provenance: (ctx: AppToolContext) => WorkProductProvenanceBase\n /** Called on the draft→ready commit so the route persists the transcript\n * anchor part and the queue projection updates. */\n onReady?: (record: WorkProductRecord, ctx: AppToolContext) => void | Promise<void>\n /** Injectable clock / id generator (tests, deterministic ids). */\n now?: () => number\n generateId?: () => string\n}\n\n/** Unwrap a guarded outcome, retrying ONCE on a lost race (the single\n * serialized turn owner makes real contention rare; one re-read-and-retry\n * absorbs the benign case). A deterministic rejection maps to a correctable\n * `ToolInputError` so the model learns exactly why. */\nasync function unwrap<T>(\n run: () => Promise<WorkProductOutcome<T>>,\n code: string,\n): Promise<T> {\n let outcome = await run()\n if (!outcome.succeeded && outcome.conflict) outcome = await run()\n if (!outcome.succeeded) throw new ToolInputError(code, outcome.error, outcome.conflict ? 409 : 400)\n return outcome.value\n}\n\nfunction requireScopeKey(args: Record<string, unknown>): string {\n const scopeKey = typeof args.scopeKey === 'string' ? args.scopeKey.trim() : ''\n if (!scopeKey) throw new ToolInputError('missing_scope_key', 'scopeKey is required — the engagement key this work product belongs to.')\n return scopeKey\n}\n\nfunction requireBatch(args: Record<string, unknown>, field: string): unknown[] {\n const raw = args[field]\n if (!Array.isArray(raw) || raw.length === 0) {\n throw new ToolInputError('missing_entries', `${field} must be a non-empty array.`)\n }\n if (raw.length > MAX_WORK_PRODUCT_BATCH) {\n throw new ToolInputError('batch_too_large', `${field} accepts at most ${MAX_WORK_PRODUCT_BATCH} entries per call — send smaller batches.`)\n }\n return raw\n}\n\n/**\n * Resolve the scope's open draft row, creating or reopening as needed:\n * an open `draft`/`blocked` row resumes; a `changes_requested` row reopens as\n * the correction draft (version +1); a `ready` row refuses — the package is\n * awaiting review and must receive a verdict first; otherwise a fresh draft\n * is created at `last reviewed version + 1` with dispatch-stamped provenance.\n */\nasync function resolveDraft(\n service: WorkProductService,\n config: WorkProductToolConfig,\n scopeKey: string,\n ctx: AppToolContext,\n): Promise<WorkProductRecord> {\n const open = await service.openDraft(ctx.workspaceId, scopeKey)\n if (open) return open\n const awaitingReview = await service.awaitingReview(ctx.workspaceId, scopeKey)\n if (awaitingReview) {\n throw new ToolInputError(\n 'awaiting_review',\n `Work product for ${scopeKey} (v${awaitingReview.version}) is awaiting review — no further emission until a reviewer verdict.`,\n 409,\n )\n }\n const awaitingCorrection = await service.awaitingCorrection(ctx.workspaceId, scopeKey)\n if (awaitingCorrection) {\n return unwrap(() => service.reopen(awaitingCorrection.id), 'reopen_failed')\n }\n return service.create({\n workspaceId: ctx.workspaceId,\n threadId: ctx.threadId,\n scopeKey,\n version: await service.nextVersion(ctx.workspaceId, scopeKey),\n provenance: stampProvenance(config.provenance(ctx), config.now),\n })\n}\n\n/** Turn a span rejection into the sentence the model can act on WITHOUT\n * re-reading the document — every message names the number that was wrong and\n * the number it must be under. */\nfunction spanErrorDetail(failure: SourceSpanFailure): string {\n switch (failure.reason) {\n case 'not_integer':\n return `${failure.field} must be a whole character offset.`\n case 'negative':\n return `${failure.field} must not be negative.`\n case 'inverted':\n return 'end must be greater than start — the range is half-open [start, end).'\n case 'out_of_range':\n return `end is past the end of the document, which is ${failure.totalChars} characters. Offsets are absolute in the whole document: when you read with an offset, add that offset to the position within the returned text.`\n case 'blank':\n return 'that range is only whitespace. Widen it to the characters that carry the value.'\n }\n}\n\n/** Turn a locate failure into a sentence naming what was searched for. */\nfunction findErrorDetail(failure: SourceFindFailure, needle: string): string {\n switch (failure.reason) {\n case 'blank_needle':\n return 'the value to locate is empty.'\n case 'not_found':\n return `${JSON.stringify(needle)} does not occur in that document. Read it again and cite a value it actually contains, or — if this figure is COMPUTED rather than read — omit the locator and state the computation in claim.`\n case 'occurrence_out_of_range':\n return `that value occurs ${failure.found} time(s) in the document; findOccurrence is out of range.`\n case 'not_distinctive':\n return failure.found === 0\n ? `${JSON.stringify(failure.needle)} is too short to identify a place in the document — a digit or two matches somewhere in almost any text. Cite the labelled line instead (for example \"Box 1 Wages, tips, other compensation ......... 128,450.00\"). If the document does not state this value at all, omit the locator and say so in claim rather than pointing at an unrelated line.`\n : `${JSON.stringify(failure.needle)} occurs ${failure.found} times, so it names no particular place. Cite a longer stretch of the supporting line, or pass findOccurrence to say which one you mean.`\n }\n}\n\n/**\n * Resolve every entry's quote against the document it names — the one place\n * lineage text is decided.\n *\n * Two citation forms, and the asymmetry between them is the point:\n *\n * - `locator.span` — the model names `[start, end)` into text it just read\n * and the PLATFORM slices the quote out of the source bytes. There is no\n * retyping step, so a fabricated quote is not rejected, it is impossible.\n * The slice REPLACES any quote the model also sent (the document is the\n * authority, not the model's transcription of it) and the resolved text\n * goes back in the tool result, so the model sees what was stored.\n * - `locator.quote` alone — the legacy free-text form, still verified\n * character-for-character. Kept because evidence written before spans\n * existed, and sources reached through paths that cannot offer offsets,\n * must stay expressible; a product does not lose lineage by upgrading.\n *\n * Entries are MUTATED in place with the resolved quote and its server-set\n * `quoteBasis`, then persisted — so what a reviewer reads is what the platform\n * proved, not what the model typed.\n *\n * Source texts are read once per distinct `sourceRef` in the batch — a\n * 50-entry batch citing three documents is three reads, not fifty.\n *\n * Without `readSourceText` the product has given the shell no way to see its\n * documents: free-text quotes go unchecked (as before — inventing a weaker\n * check would report unverified lineage as verified), and a span is a LOUD\n * refusal rather than a silently dropped locator, because a span the platform\n * cannot slice is a wiring bug in the product, not a model mistake.\n */\nasync function resolveEvidenceQuotes(\n config: WorkProductToolConfig,\n entries: readonly EvidenceEntry[],\n ctx: AppToolContext,\n): Promise<void> {\n const readSourceText = config.readSourceText\n const texts = new Map<string, string | null>()\n const readText = async (ref: string): Promise<string | null> => {\n if (!texts.has(ref)) texts.set(ref, await readSourceText!(ref, ctx))\n return texts.get(ref) ?? null\n }\n\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n // `quoteBasis` arrives unset — `parseEvidenceInput` drops a model-supplied\n // one — so this function only ever WRITES it, and only on a path that\n // established it. Every `continue` below therefore leaves the entry\n // unlabelled, which is the honest state for a quote nothing proved.\n const find = entry.locator.find\n const span = entry.locator.span\n const quote = entry.locator.quote\n\n if (find !== undefined) {\n if (!readSourceText) {\n throw new ToolInputError(\n 'span_unsupported',\n `entries[${index}].locator.find: this deployment cannot read source text, so a value cannot be located. Record the entry without locator.find.`,\n 500,\n )\n }\n const text = await readText(entry.sourceRef)\n if (text === null) {\n throw new ToolInputError(\n 'unverifiable_quote',\n `entries[${index}].locator.find: \"${entry.sourceRef}\" has no readable text, so the value cannot be located. Record the entry without a locator and state the basis in claim.`,\n )\n }\n const located = findSourceLine(text, find, entry.locator.findOccurrence ?? 1)\n if (!located.ok) {\n throw new ToolInputError(\n 'value_not_found',\n `entries[${index}].locator.find into \"${entry.sourceRef}\": ${findErrorDetail(located.failure, find)}`,\n )\n }\n // The platform owns BOTH halves now: it found the position and it cut\n // the text. The model contributed a value it read, which is the one\n // thing it does reliably.\n entry.locator.span = located.span\n entry.locator.quote = located.quote\n entry.locator.quoteBasis = 'span'\n continue\n }\n\n if (span) {\n if (!readSourceText) {\n throw new ToolInputError(\n 'span_unsupported',\n `entries[${index}].locator.span: this deployment cannot read source text, so a span cannot be resolved into a quote. Record the entry without locator.span.`,\n 500,\n )\n }\n const text = await readText(entry.sourceRef)\n if (text === null) {\n throw new ToolInputError(\n 'unverifiable_quote',\n `entries[${index}].locator.span: \"${entry.sourceRef}\" has no readable text, so the span cannot be resolved. Record the entry without locator.span or locator.quote and state the basis in claim.`,\n )\n }\n const sliced = sliceSourceSpan(text, span)\n if (!sliced.ok) {\n throw new ToolInputError(\n 'invalid_span',\n `entries[${index}].locator.span [${span.start}, ${span.end}) into \"${entry.sourceRef}\": ${spanErrorDetail(sliced.failure)}`,\n )\n }\n entry.locator.quote = sliced.quote\n entry.locator.quoteBasis = 'span'\n continue\n }\n\n if (quote === undefined || quote.trim().length === 0) continue\n if (!readSourceText) continue\n const text = await readText(entry.sourceRef)\n if (text === null) {\n throw new ToolInputError(\n 'unverifiable_quote',\n `entries[${index}].locator.quote: \"${entry.sourceRef}\" has no readable text, so the quote cannot be verified. Record the entry without locator.quote and state the basis in claim.`,\n )\n }\n if (!sourceContainsQuote(text, quote)) {\n throw new ToolInputError(\n 'quote_not_found',\n `entries[${index}].locator.quote: ${JSON.stringify(quote)} does not occur in \"${entry.sourceRef}\". Cite locator.find instead — the value as it appears in the document — and this platform locates it and writes the quote itself, so the text is right by construction. If the value is COMPUTED rather than read, omit both and state the computation in claim.`,\n )\n }\n entry.locator.quoteBasis = 'model'\n }\n}\n\n/**\n * Refuse any entry whose anchored text does not carry the figure it claims.\n *\n * This runs AFTER `resolveEvidenceQuotes`, on the text the platform decided,\n * and it is deliberately independent of how that text was anchored. `find`\n * makes a mis-addressed citation unlikely — the platform locates the line — but\n * `span` still accepts offsets a caller computed, and this is what \"accepted\n * only when they independently verify\" means concretely: the slice has to\n * contain the value.\n *\n * Row `7256ef49` is the case. Four span citations, every quote a real slice of\n * the right document, every one ~200 characters above the figure it claimed.\n * `resolveEvidenceQuotes` passed all four; nothing else here would have caught\n * them; a reviewer would have read four authoritative-looking citations\n * supporting nothing.\n *\n * Rejection is a `ToolInputError` so it folds back to the model mid-turn, the\n * same posture a non-occurring quote already gets, and the message names both\n * ways out — cite the value, or drop the locator because the figure was\n * computed. An unsatisfiable gate does not stop a bad submit, it selects for\n * one, so naming the second exit is load-bearing rather than politeness.\n */\nfunction assertClaimsSupported(\n config: WorkProductToolConfig,\n entries: readonly EvidenceEntry[],\n): void {\n if (config.verifyClaimSupport === false) return\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n const quote = entry.locator.quote\n if (quote === undefined) continue\n const support = verifyClaimSupport(quote, entry.claim)\n if (support.status !== 'unsupported') continue\n throw new ToolInputError(\n 'claim_not_supported',\n `entries[${index}].claim ${JSON.stringify(entry.claim)} is not supported by the text it cites in \"${entry.sourceRef}\": ${claimSupportErrorDetail(support, quote)}`,\n )\n }\n}\n\n/**\n * Refuse any entry whose cited line belongs to a sibling target.\n *\n * The fifth degree of freedom, and the one the four gates before it pass by\n * construction: `quote_verification` proves the text is in the document,\n * `claim_support` proves the text carries the claimed figure, and neither\n * asks whether the figure belongs on THIS line. Row `95105c8a` shipped\n * `evidence_coverage 6/6`, `claim_support 19/19`, `quote_verification 19/19`\n * with `line_3a` (qualified dividends) pointing at the ordinary-dividends line\n * and `line_3b` pointing at the qualified one.\n *\n * The product supplies which targets are confusable and what their lines say;\n * the shell only compares. Refusal requires the document to positively name a\n * sibling, so silence passes — see `./claim-support` for why that asymmetry is\n * load-bearing rather than lenient.\n */\nfunction assertTargetsNotCrossed(config: WorkProductToolConfig, entries: readonly EvidenceEntry[]): void {\n const groups = config.confusableTargets\n if (!groups || groups.length === 0) return\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n const quote = entry.locator.quote\n if (quote === undefined) continue\n const verdict = verifyTargetLabel(quote, entry.target, groups)\n if (verdict.status !== 'crossed') continue\n throw new ToolInputError(\n 'target_crossed',\n `entries[${index}] is attached to ${entry.target} but ${targetLabelErrorDetail(verdict, entry.target, quote)}`,\n )\n }\n}\n\n/**\n * Refuse any entry whose claim contradicts the artifact field it decorates.\n *\n * Runs on every upsert against the draft's CURRENT artifact (usually absent —\n * evidence streams in first) and again over the whole row at submit, which is\n * where a package like `95105c8a` is actually caught: its artifact was right\n * and its evidence crossed, and the two were never compared.\n *\n * Only a real contradiction is refused — the claim asserts a figure the same\n * artifact reports on another target. A component of an aggregate is not one\n * (row `a68b1943` evidences a 189,750.00 wage line with two W-2s of 128,450.00\n * and 61,300.00, and must keep doing so).\n */\nfunction assertEvidenceAgreesWithArtifact(\n config: WorkProductToolConfig,\n entries: readonly EvidenceEntry[],\n artifact: WorkProductArtifact | null,\n): void {\n if (config.verifyArtifactAgreement === false) return\n const fieldValues = indexArtifactValues(artifact?.fields, config.normalizeTarget)\n if (fieldValues.size === 0) return\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n const agreement = verifyArtifactAgreement(entry.target, entry.claim, fieldValues)\n if (agreement.status !== 'contradicts') continue\n throw new ToolInputError(\n 'contradicts_artifact',\n `entries[${index}].claim ${JSON.stringify(entry.claim)} contradicts this work product's own artifact: ${artifactAgreementErrorDetail(agreement, entry.target)}`,\n )\n }\n}\n\n/** Re-verify every persisted quote at submit time. The upsert gate stops new\n * fabrication; this stops a package whose evidence was written BEFORE the\n * gate existed (or under a since-corrected document) from reaching a\n * reviewer. Entries with no quote are neither verified nor failures — they\n * are lineage with no click target, counted separately so the recorded check\n * states how much of the package is quote-backed.\n *\n * A span-anchored entry is re-checked by RE-SLICING the current document and\n * comparing to the stored text, not by substring search. The difference\n * matters exactly when a source has been replaced since the citation was\n * written: a substring search would still pass if the sentence survived\n * anywhere in the new file, while the reviewer's click target has silently\n * moved. Re-slicing says the offsets still select the same characters. */\nasync function summarizeQuoteVerification(\n config: WorkProductToolConfig,\n evidence: readonly EvidenceEntry[],\n ctx: AppToolContext,\n): Promise<{ verified: number; spanAnchored: number; withoutQuote: number; failed: string[] } | undefined> {\n const readSourceText = config.readSourceText\n if (!readSourceText) return undefined\n const texts = new Map<string, string | null>()\n let verified = 0\n let spanAnchored = 0\n let withoutQuote = 0\n const failed: string[] = []\n for (const entry of evidence) {\n const quote = entry.locator.quote\n if (quote === undefined || quote.trim().length === 0) {\n withoutQuote += 1\n continue\n }\n if (!texts.has(entry.sourceRef)) texts.set(entry.sourceRef, await readSourceText(entry.sourceRef, ctx))\n const text = texts.get(entry.sourceRef)\n if (typeof text !== 'string') {\n failed.push(entry.id)\n continue\n }\n const span = entry.locator.span\n if (span) {\n const sliced = sliceSourceSpan(text, span)\n if (sliced.ok && sliced.quote === quote) {\n verified += 1\n spanAnchored += 1\n } else failed.push(entry.id)\n continue\n }\n if (sourceContainsQuote(text, quote)) verified += 1\n else failed.push(entry.id)\n }\n return { verified, spanAnchored, withoutQuote, failed }\n}\n\n/**\n * Re-run claim support over every PERSISTED entry at submit time.\n *\n * Unlike `summarizeQuoteVerification` this needs no `readSourceText`: the\n * question is whether the stored quote carries the stored claim, and both are\n * on the row. So it runs for every product, including one that cannot read its\n * sources back — and it is the check that catches a package whose evidence was\n * written before this gate existed. Row `7256ef49` is exactly that package.\n */\nfunction summarizeClaimSupport(evidence: readonly EvidenceEntry[]): {\n supported: number\n checkable: number\n unsupported: string[]\n} {\n let supported = 0\n let checkable = 0\n const unsupported: string[] = []\n for (const entry of evidence) {\n const quote = entry.locator.quote\n if (quote === undefined) continue\n const support = verifyClaimSupport(quote, entry.claim)\n if (support.status === 'not_applicable') continue\n checkable += 1\n if (support.status === 'supported') supported += 1\n else unsupported.push(entry.id)\n }\n return { supported, checkable, unsupported }\n}\n\n/**\n * Re-run target correctness over every PERSISTED entry at submit time — the\n * check that catches a package whose evidence was written before this gate\n * existed. Like `summarizeClaimSupport` it reads only the row (the stored quote\n * and the stored target), so it runs for every product.\n */\nfunction summarizeTargetCorrectness(\n evidence: readonly EvidenceEntry[],\n groups: readonly ConfusableTargetGroup[],\n targetOf: (entry: EvidenceEntry) => string,\n): { correct: number; checkable: number; crossed: { id: string; detail: string }[] } {\n let correct = 0\n let checkable = 0\n const crossed: { id: string; detail: string }[] = []\n for (const entry of evidence) {\n const quote = entry.locator.quote\n if (quote === undefined) continue\n const target = targetOf(entry)\n const verdict = verifyTargetLabel(quote, target, groups)\n if (verdict.status === 'not_applicable') continue\n checkable += 1\n if (verdict.status === 'identified') correct += 1\n else crossed.push({ id: entry.id, detail: `${entry.id} (${target} cites the ${verdict.rival} line)` })\n }\n return { correct, checkable, crossed }\n}\n\n/** Re-run artifact agreement over every PERSISTED entry against the artifact\n * being submitted. This is the pass that catches row `95105c8a`: the crossed\n * entries were written turns before the artifact arrived, so the upsert-time\n * comparison had nothing to compare them to. */\nfunction summarizeArtifactAgreement(\n evidence: readonly EvidenceEntry[],\n fieldValues: ReadonlyMap<string, string>,\n targetOf: (entry: EvidenceEntry) => string,\n): { agreeing: number; checkable: number; contradicting: { id: string; detail: string }[] } {\n let agreeing = 0\n let checkable = 0\n const contradicting: { id: string; detail: string }[] = []\n for (const entry of evidence) {\n const target = targetOf(entry)\n const agreement = verifyArtifactAgreement(target, entry.claim, fieldValues)\n if (agreement.status === 'not_applicable') continue\n checkable += 1\n if (agreement.status === 'agrees') agreeing += 1\n else {\n contradicting.push({\n id: entry.id,\n detail: `${entry.id} (${target} claims ${agreement.claimed}, which the artifact reports on ${agreement.belongsTo}; ${target} is ${agreement.expected})`,\n })\n }\n }\n return { agreeing, checkable, contradicting }\n}\n\n/** Build the three work-product tools for `customTools` registration on the\n * MCP server / HTTP handler / runtime executor. */\nexport function buildWorkProductTools(config: WorkProductToolConfig): AppToolDefinition[] {\n const service = createWorkProductService({\n store: config.store,\n ...(config.now ? { now: config.now } : {}),\n ...(config.generateId ? { generateId: config.generateId } : {}),\n })\n\n const upsertEvidence = defineAppTool({\n name: 'upsert_evidence',\n description:\n 'Record source→field lineage for the current work product, incrementally as you find it. Each entry links a source document (sourceRef + locator) to one artifact target and states the claim it supports. Cite by locator.find — the value exactly as it appears in the document — and the platform locates it and writes the supporting quote for you. Re-emitting an entry id replaces that entry. Address the work product by scopeKey; the first call creates the draft.',\n parameters: {\n type: 'object',\n properties: {\n scopeKey: { type: 'string', description: 'Engagement key for this work product.' },\n entries: {\n type: 'array',\n minItems: 1,\n maxItems: MAX_WORK_PRODUCT_BATCH,\n items: {\n type: 'object',\n properties: {\n id: { type: 'string', description: 'Stable entry id — re-emit to replace.' },\n sourceRef: { type: 'string', description: 'Vault path / attachment id of the SOURCE document.' },\n locator: {\n type: 'object',\n properties: {\n page: { type: 'number' },\n range: { type: 'string', description: \"Free-form location: 'L120-L134' | 'B7' | '¶4'.\" },\n find: {\n type: 'string',\n description:\n 'PREFERRED — use this whenever the value was READ from a document. The value exactly as it appears in the source (for example \"128,450.00\"), or a short distinctive phrase from the supporting line. The platform LOCATES it, cites the whole line it sits on, and returns that line to you. You do not retype the quote and you do not compute character offsets — so the citation can neither be invented nor land on the wrong line. If the value is not in the document, the entry is refused.',\n },\n findOccurrence: {\n type: 'integer',\n minimum: 1,\n description: 'Which occurrence of `find` to cite when the value appears more than once. Defaults to the first.',\n },\n span: {\n type: 'object',\n description:\n 'Only when you have exact character offsets from a tool that computed them. Absolute offsets into the whole document: start is the first character, end is one past the last. Prefer `find` — offsets computed by hand land on the wrong line.',\n properties: {\n start: { type: 'integer', minimum: 0 },\n end: { type: 'integer', minimum: 1 },\n },\n required: ['start', 'end'],\n },\n quote: {\n type: 'string',\n description:\n 'Fallback for sources that cannot give you character offsets: the supporting text copied character-for-character. The platform checks it occurs in the document and REFUSES the entry if it does not. Prefer span. For a value you COMPUTED rather than read, omit both and state the computation in claim.',\n },\n },\n },\n target: { type: 'string', description: 'Artifact field/claim this evidence supports.' },\n claim: { type: 'string', description: 'The value/assertion at the target.' },\n confidence: { type: 'number', minimum: 0, maximum: 1 },\n },\n required: ['id', 'sourceRef', 'target', 'claim'],\n },\n },\n },\n required: ['scopeKey', 'entries'],\n },\n async execute(args: Record<string, unknown>, ctx: AppToolContext) {\n const scopeKey = requireScopeKey(args)\n const raw = requireBatch(args, 'entries')\n const entries: EvidenceEntry[] = []\n for (let index = 0; index < raw.length; index += 1) {\n const parsed = parseEvidenceInput(raw[index], `entries[${index}]`)\n if (!parsed.ok) throw new ToolInputError('invalid_evidence', `${parsed.field}: ${parsed.error}`)\n // Fold to the product's canonical target spelling at INGEST, so the\n // row never carries two names for one line. Every downstream check —\n // coverage, deduplication, artifact agreement, the confusable-target\n // groups — joins on this string.\n if (config.normalizeTarget) parsed.value.target = config.normalizeTarget(parsed.value.target)\n entries.push(parsed.value)\n }\n // Fail-loud source resolution: lineage can never point at nothing.\n for (let index = 0; index < entries.length; index += 1) {\n const entry = entries[index]!\n if (!(await config.resolveSourceRef(entry.sourceRef, ctx))) {\n throw new ToolInputError(\n 'unknown_source_ref',\n `entries[${index}].sourceRef: \"${entry.sourceRef}\" does not resolve to an existing source document.`,\n )\n }\n }\n // Fail-loud quote verification: a quote must occur in the document the\n // entry names. Rejected BEFORE the draft is resolved, so a batch with a\n // fabricated quote persists nothing — the model corrects and re-sends\n // rather than leaving a half-written row behind.\n await resolveEvidenceQuotes(config, entries, ctx)\n // ...and the text that resolution produced must carry the figure the\n // entry claims. Same batch, same fail-before-persist discipline: an\n // entry citing the payer line for a wage figure never reaches the draft.\n assertClaimsSupported(config, entries)\n // ...and that line must belong to the target it is attached to. A\n // citation carrying the right figure on a sibling's line passes every\n // gate above it and misleads a reviewer more effectively than a\n // fabricated one, because it survives being clicked.\n assertTargetsNotCrossed(config, entries)\n const draft = await resolveDraft(service, config, scopeKey, ctx)\n // Nothing from this batch has been written yet, so a contradiction with\n // an artifact the draft already carries still fails before persisting.\n // On the usual path the artifact arrives last and this is a no-op; the\n // submit-time pass is where a full row is compared.\n assertEvidenceAgreesWithArtifact(config, entries, draft.artifact)\n const record = await unwrap(() => service.upsertEvidence(draft.id, entries), 'evidence_rejected')\n return {\n workProductId: record.id,\n version: record.version,\n evidenceCount: record.evidence.length,\n // Echo what was actually STORED for the entries in this call. A span\n // citation's quote is produced here, not sent here, so the model must\n // be able to see the text its offsets selected — that is how it\n // notices an off-by-a-line span without a second read.\n entries: entries.map((entry) => ({\n id: entry.id,\n target: entry.target,\n ...(entry.locator.quote === undefined ? {} : { quote: entry.locator.quote }),\n ...(entry.locator.quoteBasis === undefined ? {} : { quoteBasis: entry.locator.quoteBasis }),\n })),\n }\n },\n })\n\n const flagException = defineAppTool({\n name: 'flag_exception',\n description:\n 'Flag problems with the current work product (missing documents, inconsistent sources, …). An unresolved blocking exception parks the work product until it is resolved; re-emit the same id with resolved:true to release it. Address by scopeKey.',\n parameters: {\n type: 'object',\n properties: {\n scopeKey: { type: 'string', description: 'Engagement key for this work product.' },\n exceptions: {\n type: 'array',\n minItems: 1,\n maxItems: MAX_WORK_PRODUCT_BATCH,\n items: {\n type: 'object',\n properties: {\n id: { type: 'string', description: 'Stable entry id — re-emit to replace.' },\n severity: { type: 'string', enum: ['blocking', 'material', 'advisory'] },\n kind: { type: 'string', description: 'Exception kind from the product vocabulary.' },\n message: { type: 'string' },\n targets: { type: 'array', items: { type: 'string' } },\n resolved: { type: 'boolean' },\n resolutionNote: { type: 'string' },\n },\n required: ['id', 'severity', 'kind', 'message'],\n },\n },\n },\n required: ['scopeKey', 'exceptions'],\n },\n async execute(args: Record<string, unknown>, ctx: AppToolContext) {\n const scopeKey = requireScopeKey(args)\n const raw = requireBatch(args, 'exceptions')\n const entries: ExceptionEntry[] = []\n for (let index = 0; index < raw.length; index += 1) {\n const parsed = parseExceptionInput(raw[index], `exceptions[${index}]`)\n if (!parsed.ok) throw new ToolInputError('invalid_exception', `${parsed.field}: ${parsed.error}`)\n if (!config.exceptionKinds.includes(parsed.value.kind)) {\n throw new ToolInputError(\n 'invalid_exception',\n `exceptions[${index}].kind: must be one of: ${config.exceptionKinds.join(', ')}`,\n )\n }\n // An agent resolving its own exception is tagged as such; the reviewer\n // tag is reserved for the verdict route.\n if (parsed.value.resolved && parsed.value.resolvedBy === undefined) parsed.value.resolvedBy = 'agent'\n entries.push(parsed.value)\n }\n const draft = await resolveDraft(service, config, scopeKey, ctx)\n const record = await unwrap(() => service.upsertExceptions(draft.id, entries), 'exception_rejected')\n return {\n workProductId: record.id,\n version: record.version,\n status: record.status,\n unresolvedBlocking: unresolvedBlockingExceptions(record.exceptions).length,\n }\n },\n })\n\n const submitWorkProduct = defineAppTool({\n name: 'submit_work_product',\n description:\n 'Submit the finished work product for professional review — the terminal call after evidence and exceptions are recorded. Refused while a blocking exception is unresolved, or when a material target lacks evidence. Include your own quality checks in `checks`.',\n parameters: {\n type: 'object',\n properties: {\n scopeKey: { type: 'string', description: 'Engagement key for this work product.' },\n artifact: {\n type: 'object',\n properties: {\n kind: { type: 'string', description: 'Artifact kind from the product vocabulary.' },\n title: { type: 'string' },\n path: { type: 'string', description: 'Vault/object-store ref of the rendered document.' },\n content: { type: 'string', description: 'Inline body when small (markdown/JSON).' },\n mediaType: { type: 'string' },\n baseline: {\n type: 'object',\n properties: { path: { type: 'string' }, content: { type: 'string' } },\n description: 'For diff-first artifacts: the source document being redlined.',\n },\n fields: { type: 'object', description: 'Structured field map lineage targets anchor to.' },\n },\n required: ['kind', 'title'],\n },\n checks: {\n type: 'array',\n items: {\n type: 'object',\n properties: {\n id: { type: 'string' },\n name: { type: 'string' },\n passed: { type: 'boolean' },\n detail: { type: 'string' },\n },\n required: ['id', 'name', 'passed'],\n },\n description: 'Your own quality self-checks (recorded as agent-sourced).',\n },\n },\n required: ['scopeKey', 'artifact'],\n },\n async execute(args: Record<string, unknown>, ctx: AppToolContext) {\n const scopeKey = requireScopeKey(args)\n const parsedArtifact = parseArtifactInput(args.artifact)\n if (!parsedArtifact.ok) throw new ToolInputError('invalid_artifact', `${parsedArtifact.field}: ${parsedArtifact.error}`)\n const artifact = parsedArtifact.value\n if (!config.artifactKinds.includes(artifact.kind)) {\n throw new ToolInputError('invalid_artifact', `artifact.kind: must be one of: ${config.artifactKinds.join(', ')}`)\n }\n const agentChecks: QualityCheck[] = []\n if (args.checks !== undefined) {\n if (!Array.isArray(args.checks)) throw new ToolInputError('invalid_checks', 'checks must be an array when present.')\n for (let index = 0; index < args.checks.length; index += 1) {\n const parsed = parseAgentCheckInput(args.checks[index], `checks[${index}]`)\n if (!parsed.ok) throw new ToolInputError('invalid_checks', `${parsed.field}: ${parsed.error}`)\n agentChecks.push({ ...parsed.value, source: 'agent' })\n }\n }\n\n const draft = await resolveDraft(service, config, scopeKey, ctx)\n const blocking = unresolvedBlockingExceptions(draft.exceptions)\n if (blocking.length > 0) {\n throw new ToolInputError(\n 'blocking_exceptions_unresolved',\n `Cannot submit: ${blocking.length} unresolved blocking exception(s) (${blocking.map((entry) => entry.id).join(', ')}). Resolve each via flag_exception with resolved:true, or downgrade its severity with a resolutionNote justifying why it does not block.`,\n 409,\n )\n }\n\n // Platform check one: every persisted quote still occurs in the source\n // it names. The upsert gate stops NEW fabrication; this stops a package\n // whose evidence predates the gate from reaching a reviewer. Recorded on\n // the row AND rejected fail-loud — the same discipline as coverage.\n const checks: QualityCheck[] = [...agentChecks]\n const quotes = await summarizeQuoteVerification(config, draft.evidence, ctx)\n if (quotes) {\n const quoted = quotes.verified + quotes.failed.length\n checks.unshift({\n id: QUOTE_VERIFICATION_CHECK,\n name: QUOTE_VERIFICATION_CHECK,\n passed: quotes.failed.length === 0,\n detail:\n quotes.failed.length === 0\n ? `${quotes.verified}/${quoted} quoted evidence entries verified against their source (${quotes.spanAnchored} platform-sliced from a source span, ${quotes.verified - quotes.spanAnchored} model-quoted and proved to occur); ${quotes.withoutQuote} recorded without a quote`\n : `Unverifiable quotes on: ${quotes.failed.join(', ')}`,\n source: 'platform',\n })\n if (quotes.failed.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'quote_verification_failed',\n `Cannot submit: ${quotes.failed.length} evidence entr${quotes.failed.length === 1 ? 'y quotes' : 'ies quote'} text that does not occur in the source named (${quotes.failed.join(', ')}). Re-emit each with a quote copied character-for-character from that document, or without locator.quote if the value is computed.`,\n )\n }\n }\n\n // Platform check two: every citation's text carries the figure it\n // claims. Recorded AND rejected, like the other two — a citation that\n // points at real text supporting nothing is the failure a reviewer is\n // least able to catch by eye, because it looks exactly like a good one.\n if (config.verifyClaimSupport !== false) {\n const support = summarizeClaimSupport(draft.evidence)\n checks.unshift({\n id: CLAIM_SUPPORT_CHECK,\n name: CLAIM_SUPPORT_CHECK,\n passed: support.unsupported.length === 0,\n detail:\n support.unsupported.length > 0\n ? `Cited text does not contain the claimed figure on: ${support.unsupported.join(', ')}`\n : support.checkable === 0\n ? // Honest about a vacuous pass: no entry paired a quote with a\n // figure, so nothing was checked. Reporting \"0/0 verified\"\n // would read to a reviewer as assurance that was never earned.\n 'No citation pairs a quote with a claimed figure — nothing to check'\n : `${support.supported}/${support.checkable} value-bearing citations anchor to text containing the claimed figure`,\n source: 'platform',\n })\n if (support.unsupported.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'claim_not_supported',\n `Cannot submit: ${support.unsupported.length} evidence entr${support.unsupported.length === 1 ? 'y cites' : 'ies cite'} text that does not contain the figure claimed (${support.unsupported.join(', ')}). Re-emit each with locator.find set to the value exactly as it appears in the document, or without a locator if the figure was computed rather than read.`,\n )\n }\n }\n\n // Legacy rows carry whatever target spelling was current when they were\n // written, so every submit-time check folds on READ as well as ingest.\n const targetOf = (entry: EvidenceEntry): string =>\n config.normalizeTarget ? config.normalizeTarget(entry.target) : entry.target\n\n // Platform check three: every citation's line belongs to the target it\n // is attached to. Recorded AND rejected — a crossed citation is real\n // text carrying the real figure, so it is invisible to every gate above.\n if (config.confusableTargets && config.confusableTargets.length > 0) {\n const crossing = summarizeTargetCorrectness(draft.evidence, config.confusableTargets, targetOf)\n checks.unshift({\n id: TARGET_CORRECTNESS_CHECK,\n name: TARGET_CORRECTNESS_CHECK,\n passed: crossing.crossed.length === 0,\n detail:\n crossing.crossed.length > 0\n ? `Citations attached to the wrong target: ${crossing.crossed.map((item) => item.detail).join('; ')}`\n : crossing.checkable === 0\n ? 'No citation lands on a line this product can tell apart from a sibling target — nothing to check'\n : `${crossing.correct}/${crossing.checkable} citations land on a line belonging to their own target`,\n source: 'platform',\n })\n if (crossing.crossed.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'target_crossed',\n `Cannot submit: ${crossing.crossed.length} evidence entr${crossing.crossed.length === 1 ? 'y cites' : 'ies cite'} a line belonging to a different target — ${crossing.crossed.map((item) => item.detail).join('; ')}. Re-emit each against the target whose line it actually cites, or cite the line that belongs to the target it is attached to.`,\n )\n }\n }\n\n // Platform check four: the evidence and the artifact must agree about\n // what each line holds. This is the pass that catches a package whose\n // crossed evidence predates the artifact it decorates — every entry was\n // recorded turns before there was an artifact to compare it to.\n const artifactValues = indexArtifactValues(artifact.fields, config.normalizeTarget)\n if (config.verifyArtifactAgreement !== false && artifactValues.size > 0) {\n const agreement = summarizeArtifactAgreement(draft.evidence, artifactValues, targetOf)\n checks.unshift({\n id: ARTIFACT_AGREEMENT_CHECK,\n name: ARTIFACT_AGREEMENT_CHECK,\n passed: agreement.contradicting.length === 0,\n detail:\n agreement.contradicting.length > 0\n ? `Evidence contradicts the artifact on: ${agreement.contradicting.map((item) => item.detail).join('; ')}`\n : agreement.checkable === 0\n ? 'No evidence claim states a figure the artifact also states — nothing to check'\n : `${agreement.agreeing}/${agreement.checkable} evidence claims agree with the artifact field they support`,\n source: 'platform',\n })\n if (agreement.contradicting.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'contradicts_artifact',\n `Cannot submit: ${agreement.contradicting.length} evidence entr${agreement.contradicting.length === 1 ? 'y contradicts' : 'ies contradict'} the artifact they support — ${agreement.contradicting.map((item) => item.detail).join('; ')}. Move each citation to the target it actually supports, or correct the artifact. The package cannot state both.`,\n )\n }\n }\n\n // Platform check five: every material target has ≥1 evidence row. A\n // failing coverage check is BOTH recorded on the row (visible to the\n // queue) and rejected fail-loud — a lineage-free package can never reach\n // a reviewer.\n if (config.materialTargets) {\n // Both sides of the join fold to the canonical spelling: an artifact\n // field written `line_3a` and an evidence row written `f1040.line_3a`\n // are one line, and a coverage check that read them as two would\n // report a miss on a target that IS evidenced.\n const targets = [\n ...new Set(\n config.materialTargets(artifact).map((target) => (config.normalizeTarget ? config.normalizeTarget(target) : target)),\n ),\n ]\n const covered = new Set(draft.evidence.map(targetOf))\n const missing = targets.filter((target) => !covered.has(target))\n // How each covered target is BACKED, not merely that a row exists.\n // A coverage count that cannot distinguish a platform-sliced citation\n // from a bare assertion is the number that let 13 invented quotes read\n // as \"13/13 evidenced\" on the row a reviewer then opened.\n const anchoredTargets = new Set(\n draft.evidence.filter((entry) => entry.locator.quoteBasis !== undefined).map(targetOf),\n )\n const spanTargets = new Set(\n draft.evidence.filter((entry) => entry.locator.quoteBasis === 'span').map(targetOf),\n )\n const present = targets.filter((target) => covered.has(target))\n const unanchored = present.filter((target) => !anchoredTargets.has(target))\n const spanCount = present.filter((target) => spanTargets.has(target)).length\n const breakdown = `${spanCount} span-anchored, ${present.length - spanCount - unanchored.length} quote-verified, ${unanchored.length} claim-only`\n const coverage: QualityCheck = {\n id: EVIDENCE_COVERAGE_CHECK,\n name: EVIDENCE_COVERAGE_CHECK,\n passed: missing.length === 0 && !(config.requireAnchoredEvidence && unanchored.length > 0),\n detail:\n missing.length > 0\n ? `Missing evidence for: ${missing.join(', ')}`\n : config.requireAnchoredEvidence && unanchored.length > 0\n ? `No source anchor for: ${unanchored.join(', ')}`\n : `${targets.length}/${targets.length} material targets evidenced (${breakdown})`,\n source: 'platform',\n }\n checks.unshift(coverage)\n if (missing.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'evidence_coverage_failed',\n `Cannot submit: material targets lack evidence: ${missing.join(', ')}. Add upsert_evidence entries targeting each, then resubmit.`,\n )\n }\n if (config.requireAnchoredEvidence && unanchored.length > 0) {\n await unwrap(() => service.recordChecks(draft.id, checks), 'checks_rejected')\n throw new ToolInputError(\n 'evidence_not_anchored',\n `Cannot submit: these material targets have an evidence row but no source anchor: ${unanchored.join(', ')}. Re-emit each with locator.find — the value exactly as it appears in the document. The platform locates it and writes the quote itself, so you never retype source text or count characters.`,\n )\n }\n }\n\n const provenance = stampProvenance(config.provenance(ctx), config.now)\n const record = await unwrap(\n () => service.submit(draft.id, { artifact, checks, provenance }),\n 'submit_rejected',\n )\n await config.onReady?.(record, ctx)\n return {\n workProductId: record.id,\n version: record.version,\n status: record.status,\n checks: record.checks.map((check) => ({ name: check.name, passed: check.passed, source: check.source })),\n }\n },\n })\n\n return [upsertEvidence, flagException, submitWorkProduct]\n}\n","/**\n * Framework-neutral work-product review endpoints — the\n * `createInteractionAnswerRoute` factory pattern: web-standard\n * `Request`/`Response`, ONE product-supplied `authorize` seam (session auth,\n * workspace access, reviewer identity, rate limits), everything behind it\n * mechanism. Server-only and subpath-only: never reachable from a client\n * bundle (enforced by the browser-safety test).\n *\n * The verdict endpoint is deliberately NOT a second approval broker: agent\n * asks stay on `/interactions`; this is the REVIEWER's plain authorized\n * verdict on a ready package — one human-in-the-loop channel per direction.\n * A `request_changes` note re-enters chat as the correction turn via the\n * `onVerdict` seam; chat remains the driver surface.\n */\n\nimport {\n createWorkProductService,\n type WorkProductService,\n} from './service'\nimport {\n workProductToPersistedPart,\n type WorkProductPersistedPart,\n type WorkProductRecord,\n type WorkProductStorePort,\n} from './types'\n\n/** Reviewer verdict wire body for the POST endpoint. */\nexport type WorkProductVerdictBody =\n | { ok: true; id: string; verdict: 'approve'; note?: string }\n | { ok: true; id: string; verdict: 'request_changes'; note: string }\n | { ok: false; error: string }\n\n/** Validate the verdict POST body: `{ id, verdict, note? }`; a\n * `request_changes` verdict REQUIRES a non-empty note — the note IS the\n * correction turn the agent works from. */\nexport function validateWorkProductVerdictBody(body: Record<string, unknown>): WorkProductVerdictBody {\n const id = typeof body.id === 'string' && body.id.trim() ? body.id.trim() : null\n if (!id) return { ok: false, error: 'Missing work product id' }\n const verdict = body.verdict\n if (verdict !== 'approve' && verdict !== 'request_changes') {\n return { ok: false, error: 'Invalid verdict: expected approve or request_changes' }\n }\n const note = body.note === undefined ? undefined : typeof body.note === 'string' ? body.note.trim() : null\n if (note === null) return { ok: false, error: 'Invalid note: expected a string' }\n if (verdict === 'request_changes') {\n if (!note) return { ok: false, error: 'request_changes requires a note — it becomes the correction instruction in chat' }\n return { ok: true, id, verdict, note }\n }\n return note ? { ok: true, id, verdict, note } : { ok: true, id, verdict }\n}\n\n/** The product seam's verdict for one request: authenticated reviewer +\n * workspace, or a product-authored short-circuit Response (401/403/429…). */\nexport type WorkProductRouteAuthorization =\n | { ok: true; workspaceId: string; reviewedBy: string }\n | { ok: false; response: Response }\n\n/** Auth seam arguments carrying the request, the endpoint intent, and the parsed verdict body */\nexport interface WorkProductAuthorizeArgs {\n request: Request\n intent: 'list' | 'detail' | 'verdict'\n /** The parsed, validated POST body (verdict intent only). */\n body?: Record<string, unknown>\n}\n\n/** Configuration options assembling the review endpoints from the store and product seams */\nexport interface WorkProductRoutesOptions {\n store: WorkProductStorePort\n /** Authenticate + authorize the caller; the ONLY product access step. */\n authorize: (args: WorkProductAuthorizeArgs) => Promise<WorkProductRouteAuthorization>\n /** Post-verdict product seam: post the `request_changes` note into the\n * driving chat thread as the correction turn, notify, etc. Runs after the\n * transition commits; a throw is logged, never unwinds the verdict. */\n onVerdict?: (args: {\n record: WorkProductRecord\n verdict: 'approve' | 'request_changes'\n note?: string\n reviewedBy: string\n }) => void | Promise<void>\n /** Persist/update the transcript anchor part reflecting the new status, so\n * the chat card flips with the verdict. */\n persistAnchorPart?: (part: WorkProductPersistedPart, record: WorkProductRecord) => void | Promise<void>\n /** Future integration point fired on approval (push to a DMS/CRM/export\n * pipeline). A stub seam by design — export itself stays the product's\n * signed object-store download. */\n onExport?: (record: WorkProductRecord) => void | Promise<void>\n logger?: Pick<Console, 'warn' | 'error'>\n now?: () => number\n}\n\n/** Assembled review endpoints returning web-standard Responses */\nexport interface WorkProductRoutes {\n /** GET — the workspace's records for the queue projection. Optional\n * `?status=a,b` filter. */\n list: (request: Request) => Promise<Response>\n /** GET — one record by id (404 when absent or outside the workspace). */\n detail: (request: Request, id: string) => Promise<Response>\n /** POST `{ id, verdict, note? }` — the reviewer verdict: CAS transition +\n * history entry + product seams. 409 when the record is no longer ready. */\n verdict: (request: Request) => Promise<Response>\n}\n\n/** Create the work-product review endpoints over the store port and product seams */\nexport function createWorkProductRoutes(options: WorkProductRoutesOptions): WorkProductRoutes {\n const logger = options.logger ?? console\n const service: WorkProductService = createWorkProductService({\n store: options.store,\n ...(options.now ? { now: options.now } : {}),\n })\n\n async function list(request: Request): Promise<Response> {\n const auth = await options.authorize({ request, intent: 'list' })\n if (!auth.ok) return auth.response\n const url = new URL(request.url)\n const statusParam = url.searchParams.get('status')\n const statuses = statusParam\n ? statusParam.split(',').map((value) => value.trim()).filter(Boolean)\n : null\n const workProducts = await options.store.listByWorkspace(\n auth.workspaceId,\n statuses ? { status: statuses as WorkProductRecord['status'][] } : undefined,\n )\n return Response.json({ workProducts })\n }\n\n async function detail(request: Request, id: string): Promise<Response> {\n const auth = await options.authorize({ request, intent: 'detail' })\n if (!auth.ok) return auth.response\n const record = await options.store.load(id)\n if (!record || record.workspaceId !== auth.workspaceId) {\n return Response.json({ error: 'Work product not found' }, { status: 404 })\n }\n return Response.json({ workProduct: record })\n }\n\n async function verdict(request: Request): Promise<Response> {\n const body = (await request.json().catch(() => null)) as Record<string, unknown> | null\n if (!body || typeof body !== 'object' || Array.isArray(body)) {\n return Response.json({ error: 'Invalid JSON body' }, { status: 400 })\n }\n const validation = validateWorkProductVerdictBody(body)\n if (!validation.ok) return Response.json({ error: validation.error }, { status: 400 })\n\n const auth = await options.authorize({ request, intent: 'verdict', body })\n if (!auth.ok) return auth.response\n\n const existing = await options.store.load(validation.id)\n if (!existing || existing.workspaceId !== auth.workspaceId) {\n return Response.json({ error: 'Work product not found' }, { status: 404 })\n }\n\n const outcome = await service.applyVerdict(validation.id, {\n verdict: validation.verdict,\n reviewedBy: auth.reviewedBy,\n ...(validation.note === undefined ? {} : { note: validation.note }),\n })\n if (!outcome.succeeded) {\n // A lost race and an illegal edge both mean \"the row is no longer the\n // ready version you looked at\" — 409 so the client re-reads.\n return Response.json({ code: 'VERDICT_CONFLICT', error: outcome.error }, { status: 409 })\n }\n const record = outcome.value\n\n // Product seams run AFTER the committed transition; their failures are\n // logged, never unwound — the verdict is durable truth at this point.\n try {\n await options.persistAnchorPart?.(workProductToPersistedPart(record), record)\n } catch (error) {\n logger.error('[work-product] persistAnchorPart failed:', error)\n }\n try {\n await options.onVerdict?.({\n record,\n verdict: validation.verdict,\n ...(validation.note === undefined ? {} : { note: validation.note }),\n reviewedBy: auth.reviewedBy,\n })\n } catch (error) {\n logger.error('[work-product] onVerdict failed:', error)\n }\n if (validation.verdict === 'approve') {\n try {\n await options.onExport?.(record)\n } catch (error) {\n logger.error('[work-product] onExport failed:', error)\n }\n }\n\n return Response.json({ ok: true, workProduct: record })\n }\n\n return { list, detail, verdict }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAgFA,IAAM,WAAW;AAWjB,IAAM,iBAAiB;AAQvB,IAAM,kBACJ;AAKF,IAAM,cAAc;AAgBb,SAAS,kBAAkB,OAA8B;AAC9D,MAAI,OAAO,MAAM,KAAK;AACtB,MAAI,KAAK,WAAW,EAAG,QAAO;AAC9B,MAAI,YAAY,KAAK,IAAI,EAAG,QAAO,KAAK,MAAM,GAAG,EAAE,EAAE,KAAK;AAC1D,SAAO,KAAK,QAAQ,UAAU,EAAE,EAAE,KAAK;AACvC,SAAO,KAAK,QAAQ,aAAa,EAAE,EAAE,KAAK;AAC1C,SAAO,KAAK,QAAQ,OAAO,EAAE,EAAE,KAAK;AACpC,MAAI,CAAC,0CAA0C,KAAK,IAAI,EAAG,QAAO;AAClE,SAAO,KAAK,QAAQ,OAAO,EAAE;AAC7B,MAA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