@oathe/oathe 0.0.1 → 0.2.1

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.
Files changed (71) hide show
  1. package/.claude-plugin/marketplace.json +14 -0
  2. package/LICENSE +202 -21
  3. package/NOTICE +7 -0
  4. package/README.md +104 -12
  5. package/assets/oathe-hero.svg +92 -0
  6. package/bin/oathe.mjs +376 -0
  7. package/docs/PRIVACY.md +45 -0
  8. package/package.json +33 -23
  9. package/plugin/.claude-plugin/plugin.json +10 -0
  10. package/plugin/.mcp.json +13 -0
  11. package/plugin/hooks/frame-note.mjs +24 -0
  12. package/plugin/hooks/heartbeat.mjs +49 -0
  13. package/plugin/hooks/hooks.json +39 -0
  14. package/plugin/hooks/lib.mjs +83 -0
  15. package/plugin/hooks/render-board.mjs +11 -0
  16. package/plugin/skills/oathe-work/SKILL.md +33 -0
  17. package/src/atif.mjs +633 -0
  18. package/src/blocks.mjs +192 -0
  19. package/src/board-render.mjs +106 -0
  20. package/src/config.mjs +169 -0
  21. package/src/context.mjs +35 -0
  22. package/src/doctor.mjs +108 -0
  23. package/src/harness.mjs +271 -0
  24. package/src/init.mjs +92 -0
  25. package/src/launch-env.mjs +25 -0
  26. package/src/launch.mjs +259 -0
  27. package/src/manifest.mjs +89 -0
  28. package/src/mcp/oathe-tools.mjs +517 -0
  29. package/src/paths.mjs +49 -0
  30. package/src/plans.mjs +50 -0
  31. package/src/runtime/discharge.mjs +57 -0
  32. package/src/runtime/provider.mjs +154 -0
  33. package/src/runtime/simple-cage.mjs +67 -0
  34. package/src/runtime/sql-acceptance-lane.mjs +89 -0
  35. package/src/session-host.mjs +81 -0
  36. package/src/substrate.mjs +337 -0
  37. package/src/successor.mjs +31 -0
  38. package/src/traces.mjs +242 -0
  39. package/src/uninstall.mjs +45 -0
  40. package/src/verifier.mjs +322 -0
  41. package/src/workspace.mjs +47 -0
  42. package/vendor/ddl/001_core.sql +312 -0
  43. package/vendor/ddl/002_claim.sql +438 -0
  44. package/vendor/ddl/003_verification.sql +480 -0
  45. package/vendor/ddl/004_delegation.sql +397 -0
  46. package/vendor/ddl/005_escalation.sql +303 -0
  47. package/vendor/ddl/006_dependency.sql +384 -0
  48. package/vendor/ddl/007_assignment.sql +502 -0
  49. package/vendor/ddl/008_effect_receipt.sql +290 -0
  50. package/vendor/ddl/009_verified_edge.sql +452 -0
  51. package/vendor/ddl/010_parent_child_verification.sql +320 -0
  52. package/vendor/ddl/011_confirm.sql +288 -0
  53. package/vendor/ddl/012_evaluator_lane.sql +444 -0
  54. package/vendor/ddl/013_claim_terminal.sql +279 -0
  55. package/vendor/ddl/014_claim_settlement.sql +327 -0
  56. package/vendor/ddl/015_claim_yield.sql +213 -0
  57. package/vendor/ddl/016_reopened_reclaim.sql +285 -0
  58. package/vendor/ddl/017_claim_yield_causes.sql +357 -0
  59. package/vendor/ddl/018_receiptless_unknown.sql +249 -0
  60. package/vendor/ddl/019_verification_contract_freeze.sql +335 -0
  61. package/vendor/ddl/020_context_compilation.sql +105 -0
  62. package/vendor/ddl/021_execution_attempt_closure.sql +95 -0
  63. package/vendor/ddl/022_verification_clause.sql +106 -0
  64. package/vendor/ddl/023_executor_role.sql +66 -0
  65. package/vendor/ddl/024_executor_role_hardening.sql +439 -0
  66. package/vendor/ddl/025_running_attempt_uniqueness.sql +102 -0
  67. package/vendor/ddl/026_acceptance_authority.sql +230 -0
  68. package/vendor/ddl/027_ownership_return.sql +836 -0
  69. package/vendor/ddl/028_attempt_bind_ownership.sql +173 -0
  70. package/vendor/ddl/manifest.json +183 -0
  71. package/index.js +0 -33
@@ -0,0 +1,444 @@
1
+ -- 012_evaluator_lane.sql — THE EVALUATOR SEAT: the three verbs that own
2
+ -- settlement facts, and the writer guards that make them the only way in.
3
+ --
4
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.2 as amended (the executor
5
+ -- never owns settlement facts), §1.4 source 3 (the confirm consume is a
6
+ -- state_version-bound verification), §3.3 (FOUNDER RULING R4 — a task-type edge
7
+ -- releases on the upstream task's recorded verification, period), §4.3's signer
8
+ -- discipline generalised to the task lane, R8 (a rejection reopens the work).
9
+ -- Requires 001_core.sql, 002_claim.sql (cell.written_by), 003_verification.sql,
10
+ -- 006_dependency.sql, 011_confirm.sql.
11
+ --
12
+ -- Re-apply: idempotent (CREATE OR REPLACE throughout).
13
+ -- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
14
+ --
15
+ -- WHY THIS FILE EXISTS, MEASURED
16
+ --
17
+ -- A2.4's gate-1 writer census failed against BOTH bake-off candidates, for the
18
+ -- same reason and independently: each had grown an EVALUATOR LANE inside itself
19
+ -- — an `evaluate.mjs` in one, an evaluator lane and a confirm lane in the other
20
+ -- — issuing `UPDATE cell.dep_edge SET state='satisfied'` and
21
+ -- `INSERT INTO cell.verification` from executor code. Finding
22
+ -- A2.4-F0-GATE-1-WRITER-CENSUS-FAILS states the consequence: a settlement fact
23
+ -- written by the thing that ran the work makes the executor a SECOND AUTHORITY
24
+ -- over settlement (L1). FOUNDER RULING R4 and the amended contract §1.2 rule the
25
+ -- other way — release is performed by the evaluator lane, never by the
26
+ -- completing author's runtime.
27
+ --
28
+ -- The finding's own flip condition named the shape of the fix: the lane moves
29
+ -- OUT of the candidates, deleted with its wiring in the same change (L5), and a
30
+ -- separate evaluator lane owns the release. This file is the substrate half of
31
+ -- that move, and it is deliberately more than a place to put the code:
32
+ --
33
+ -- **A seat that lives only in whoever happens to be writing is a seat with a
34
+ -- bypass.** 009_verified_edge.sql learned that at a cost — 23 historical
35
+ -- `verified` settles reached the bus by direct publish past a CLI-only gate,
36
+ -- every one of them naming the right evaluator in the row. So the task lane gets
37
+ -- the same treatment the `verified` lane already has: the guard reads the
38
+ -- PL/pgSQL CALL STACK (cell.written_by, 002_claim.sql) rather than a column or a
39
+ -- settable GUC, because a caller cannot put a frame on the stack that is not
40
+ -- there. A candidate that learns the table name still cannot release an edge.
41
+ --
42
+ -- WHAT IS NOT HERE, AND WHY
43
+ --
44
+ -- No policy. The release bar is `cell.v_dep_edge_release` and its
45
+ -- `dep_edge_release_meets_the_acceptance_bar` trigger, both untouched: this
46
+ -- file's verb meets that bar like every other writer, and a verb that could
47
+ -- waive the bar by being the sanctioned writer would be the bar moving into
48
+ -- whoever holds the seat. Six other dependency families each name a different
49
+ -- settler and none of them is reached from here.
50
+ --
51
+ -- Declared failure vocabulary added by this file (SQLSTATE class FC; this lane
52
+ -- reserves FC090-FC099):
53
+ -- FC090 TASK_EDGE_RELEASED_OUTSIDE_THE_EVALUATOR_LANE a task-lane release
54
+ -- written by something that is not the
55
+ -- seat's verb
56
+ -- FC091 TASK_REOPENED_OUTSIDE_THE_EVALUATOR_LANE the same, for R8's reopen
57
+ -- FC092 EVALUATOR_EDGE_IS_NOT_THE_TASK_LANES the seat handed an edge belonging
58
+ -- to one of the six other families
59
+ -- FC093 EVALUATOR_EDGE_UNKNOWN a release against an edge that is not
60
+ -- there
61
+ -- FC094 EVALUATOR_RELEASE_UNSIGNED a release carrying no signer
62
+ -- FC095 EVALUATOR_TASK_UNKNOWN a reopen against a task the company
63
+ -- has no record of
64
+ -- FC096 CONFIRM_VERDICT_NOT_ANSWERABLE a transcription of an ask that is not
65
+ -- answered, not planned for, or has no
66
+ -- recorded statement to be about
67
+ -- FC097 CONFIRM_VERDICT_UNADDRESSABLE_TRACE R4's mandatory trace_ref, blank
68
+
69
+ -- ---------------------------------------------------------------------------
70
+ -- cell.v_evaluator_confirm_ask — what one ask IS, read in one place
71
+ --
72
+ -- The lane's read surface for case 4. Four things are JOINED rather than
73
+ -- assumed, and each of them is a way a transcription could otherwise write a row
74
+ -- meaning something nobody checked:
75
+ --
76
+ -- * `cell.v_confirm_verdict` for the VERDICT — the view is the one place that
77
+ -- decides what a consumed-with-no-verdict token projects (MEASURED: 217 rows
78
+ -- of exactly that shape). A CASE anywhere else would be a second opinion.
79
+ -- * `cell.agent_statement` for the SUBJECT — §1.4 makes every verification a
80
+ -- verdict about a RECORDED statement, and the one taken is the task's LIVE
81
+ -- completion assertion (not superseded), which is the same predicate
82
+ -- v_task_completion counts by.
83
+ -- * `cell.work_claim` for the CLAIM — read OFF the statement rather than
84
+ -- joined on the task, so the claim in this row is the claim whose author
85
+ -- said the thing being judged, even where a task has been claimed more than
86
+ -- once in its life.
87
+ -- * `cell.task` for the CLAUSE — reported as a column rather than filtered
88
+ -- out, so "this claim has no ask" and "this ask discharges a clause the plan
89
+ -- never declared" stay different answers to a lane that has to log which one
90
+ -- it met.
91
+ -- ---------------------------------------------------------------------------
92
+ CREATE OR REPLACE VIEW cell.v_evaluator_confirm_ask AS
93
+ SELECT
94
+ v.token_id,
95
+ v.org_id,
96
+ v.task_id,
97
+ v.ask_key,
98
+ v.state,
99
+ v.projects,
100
+ v.consumed_by,
101
+ v.consumed_at,
102
+ v.state_version,
103
+ s.statement_id,
104
+ s.work_claim_id,
105
+ (t.verification_plan -> 'clauses' @> to_jsonb('operator_approval'::text))
106
+ AS clause_declared
107
+ FROM cell.v_confirm_verdict v
108
+ JOIN cell.task t
109
+ ON t.org_id = v.org_id AND t.task_id = v.task_id
110
+ LEFT JOIN LATERAL (
111
+ SELECT st.statement_id, st.work_claim_id
112
+ FROM cell.agent_statement st
113
+ WHERE st.org_id = v.org_id
114
+ AND st.task_id = v.task_id
115
+ AND st.statement_type = 'completion'
116
+ AND NOT EXISTS (SELECT 1 FROM cell.agent_statement later
117
+ WHERE later.supersedes_ref = st.statement_id)
118
+ ORDER BY st.asserted_at DESC, st.statement_id
119
+ LIMIT 1
120
+ ) s ON true;
121
+
122
+ COMMENT ON VIEW cell.v_evaluator_confirm_ask IS
123
+ 'The evaluator seat''s read surface for case 4: one row per canonical ask, '
124
+ 'carrying the projection, the live statement it would be a verdict about, '
125
+ 'the claim that authored it, and whether the plan declares the clause.';
126
+
127
+ -- ------------------------------------------- the task lane's writer guard (R4)
128
+ --
129
+ -- The `verified` family has had this clause since 009. The task family did not,
130
+ -- and that absence is exactly what let two executor candidates hold the
131
+ -- evaluator seat with a `UPDATE cell.dep_edge` of their own. Only a TRANSITION
132
+ -- into satisfied is checked: re-stating an already-settled edge is idempotent,
133
+ -- and re-checking it would make an unrelated correction on the row fail for a
134
+ -- reason that has nothing to do with it.
135
+ --
136
+ -- The trigger is named to sort AFTER `dep_edge_release_meets_the_acceptance_bar`
137
+ -- so a release that fails BOTH rules reports the bar it missed rather than the
138
+ -- seat it lacked: R4's substance is the older and more informative refusal.
139
+ CREATE OR REPLACE FUNCTION cell.enforce_task_edge_release_is_the_evaluator_lanes()
140
+ RETURNS trigger
141
+ LANGUAGE plpgsql
142
+ AS $task_lane_writer$
143
+ BEGIN
144
+ IF NEW.dep_type <> 'task' OR NEW.state IS DISTINCT FROM 'satisfied' THEN
145
+ RETURN NEW;
146
+ END IF;
147
+ IF TG_OP = 'UPDATE' AND OLD.state = 'satisfied' THEN
148
+ RETURN NEW;
149
+ END IF;
150
+
151
+ IF NOT cell.written_by('cell.settle_task_edge') THEN
152
+ RAISE EXCEPTION
153
+ 'TASK_EDGE_RELEASED_OUTSIDE_THE_EVALUATOR_LANE:% — a task-lane '
154
+ 'dependency releases through cell.settle_task_edge and through '
155
+ 'nothing else. FOUNDER RULING R4 and contract §1.2: release is '
156
+ 'performed by the evaluator lane, never by the completing author''s '
157
+ 'runtime — an executor that wrote this row would be a second '
158
+ 'authority over settlement (L1). The writer signal is the PL/pgSQL '
159
+ 'call stack, which a direct writer cannot fabricate, rather than a '
160
+ 'column it could type or a setting it could set for itself: 23 '
161
+ 'historical `verified` settles reached the bus by direct publish '
162
+ 'naming exactly the right evaluator.',
163
+ NEW.edge_id
164
+ USING ERRCODE = 'FC090';
165
+ END IF;
166
+
167
+ RETURN NEW;
168
+ END;
169
+ $task_lane_writer$;
170
+
171
+ CREATE OR REPLACE TRIGGER dep_edge_task_release_is_the_evaluator_lanes
172
+ BEFORE INSERT OR UPDATE ON cell.dep_edge
173
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_task_edge_release_is_the_evaluator_lanes();
174
+
175
+ -- ------------------------------------------------- the reopen's writer guard
176
+ --
177
+ -- R8's reopen is a settlement-adjacent fact: it says the work is NOT done and
178
+ -- the next claimant resumes on it. Two lanes needed it (a rejected gate and a
179
+ -- rejected operator answer) and both candidates had spelled it themselves, so it
180
+ -- gets one verb and the same guard. Only the TRANSITION into `reopened` is
181
+ -- guarded — a task's objective, its assignment and its plan are nobody's
182
+ -- settlement fact, and a guard over the whole row would make the assignment
183
+ -- lane's writes fail for a reason that has nothing to do with them.
184
+ CREATE OR REPLACE FUNCTION cell.enforce_task_reopen_is_the_evaluator_lanes()
185
+ RETURNS trigger
186
+ LANGUAGE plpgsql
187
+ AS $reopen_writer$
188
+ BEGIN
189
+ IF NEW.origin IS DISTINCT FROM 'reopened' OR OLD.origin = 'reopened' THEN
190
+ RETURN NEW;
191
+ END IF;
192
+
193
+ IF NOT cell.written_by('cell.reopen_rejected_task') THEN
194
+ RAISE EXCEPTION
195
+ 'TASK_REOPENED_OUTSIDE_THE_EVALUATOR_LANE:%/% — reopening rejected '
196
+ 'work is the evaluator seat''s act (R8: the reopened task''s next '
197
+ 'claimant resumes on the same work), and it happens through '
198
+ 'cell.reopen_rejected_task. An executor that reopened its own work '
199
+ 'would be deciding that its verdict had not stood.',
200
+ NEW.org_id, NEW.task_id
201
+ USING ERRCODE = 'FC091';
202
+ END IF;
203
+
204
+ RETURN NEW;
205
+ END;
206
+ $reopen_writer$;
207
+
208
+ CREATE OR REPLACE TRIGGER task_reopen_is_the_evaluator_lanes
209
+ BEFORE UPDATE ON cell.task
210
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_task_reopen_is_the_evaluator_lanes();
211
+
212
+ -- ----------------------------------------------------------------- the verbs
213
+ --
214
+ -- Every argument is required; no signature below carries a default and none may
215
+ -- grow one. A default signer, a default instant or a server-chosen trace ref
216
+ -- would each be a governance decision made in the substrate, invisible at the
217
+ -- call site and unreachable by the config object that holds every such decision.
218
+ --
219
+ -- Each verb ANSWERS WHETHER THIS CALL MOVED ANYTHING. That is not a convenience:
220
+ -- "released" and "already released" are different facts to a lane that has to
221
+ -- record what it did, and a wake that released nothing must be distinguishable
222
+ -- in evidence from a wake that never looked.
223
+
224
+ CREATE OR REPLACE FUNCTION cell.settle_task_edge(
225
+ p_edge_id uuid,
226
+ p_settled_by text,
227
+ p_settled_at timestamptz
228
+ )
229
+ RETURNS boolean
230
+ LANGUAGE plpgsql
231
+ AS $settle_task$
232
+ DECLARE
233
+ v_dep_type text;
234
+ v_state text;
235
+ v_moved integer;
236
+ BEGIN
237
+ IF length(btrim(coalesce(p_settled_by, ''))) = 0 THEN
238
+ RAISE EXCEPTION
239
+ 'EVALUATOR_RELEASE_UNSIGNED:% — a satisfied edge carries its signer, '
240
+ 'its kind and its stamp (dep_edge_satisfied_edge_carries_its_signer_'
241
+ 'kind_and_stamp). This seat READS the signer off the verdict that met '
242
+ 'the bar and never chooses one, so a blank signer here means the '
243
+ 'selector found an edge no verification stands behind. "Who released '
244
+ 'this, on what basis, when" is the question the Mac''s void events '
245
+ 'could not answer, which is why zero consumers ever read one.',
246
+ p_edge_id
247
+ USING ERRCODE = 'FC094';
248
+ END IF;
249
+
250
+ SELECT dep_type, state INTO v_dep_type, v_state
251
+ FROM cell.dep_edge WHERE edge_id = p_edge_id;
252
+
253
+ IF NOT FOUND THEN
254
+ RAISE EXCEPTION
255
+ 'EVALUATOR_EDGE_UNKNOWN:% — there is no such edge, so nothing was '
256
+ 'released and there is no wait this call could be evidence of. An '
257
+ 'UPDATE that matched nothing fires no trigger at all, which is how a '
258
+ 'release path comes to report success for a row it never touched.',
259
+ p_edge_id
260
+ USING ERRCODE = 'FC093';
261
+ END IF;
262
+
263
+ IF v_dep_type <> 'task' THEN
264
+ RAISE EXCEPTION
265
+ 'EVALUATOR_EDGE_IS_NOT_THE_TASK_LANES:%:% — cell.v_dep_edge_release '
266
+ 'names this family''s settler, and it is not `evaluator_lane`. Six '
267
+ 'other families each name a different seat — an acceptance seat, a '
268
+ 'registered checker, an operator''s consume, the reconciler''s '
269
+ 'mootness, an external-event evaluator — and a lane that released '
270
+ 'whatever edge it was handed would be silently holding five seats '
271
+ 'nobody gave it.',
272
+ p_edge_id, v_dep_type
273
+ USING ERRCODE = 'FC092';
274
+ END IF;
275
+
276
+ -- Idempotency is the PREDICATE, not a set this lane remembers: a restart
277
+ -- takes a remembered set with it, which is the whole reason L2 puts durable
278
+ -- state below the agent. C5-DUP-SATISFY measures it from the other side —
279
+ -- the duplicate verdict is a second verification ROW (history is durable,
280
+ -- L12) and the EDGE is what the predicate names.
281
+ --
282
+ -- Nothing is set to authorize this write: THIS frame, on the call stack, is
283
+ -- what the guard above reads, and it stops existing when the function returns.
284
+ UPDATE cell.dep_edge
285
+ SET state = 'satisfied',
286
+ settled_by = p_settled_by,
287
+ settle_kind = 'evaluator_verdict',
288
+ settled_at = p_settled_at
289
+ WHERE edge_id = p_edge_id
290
+ AND state = 'declared';
291
+
292
+ GET DIAGNOSTICS v_moved = ROW_COUNT;
293
+ RETURN v_moved = 1;
294
+ END;
295
+ $settle_task$;
296
+
297
+ COMMENT ON FUNCTION cell.settle_task_edge IS
298
+ 'FOUNDER RULING R4 / contract §1.2: the one writer of a task-lane release. '
299
+ 'The acceptance bar is enforced by its own trigger, not here — a check that '
300
+ 'lives only in this function is a check a direct UPDATE skips.';
301
+
302
+
303
+ CREATE OR REPLACE FUNCTION cell.reopen_rejected_task(
304
+ p_org_id text,
305
+ p_task_id text
306
+ )
307
+ RETURNS boolean
308
+ LANGUAGE plpgsql
309
+ AS $reopen$
310
+ DECLARE
311
+ v_moved integer;
312
+ BEGIN
313
+ IF NOT EXISTS (SELECT 1 FROM cell.task
314
+ WHERE org_id = p_org_id AND task_id = p_task_id) THEN
315
+ RAISE EXCEPTION
316
+ 'EVALUATOR_TASK_UNKNOWN:%/% — reopening is an act on work the company '
317
+ 'has a record of. A reopen that matched nothing would report success '
318
+ 'for a task nobody can later account for.',
319
+ p_org_id, p_task_id
320
+ USING ERRCODE = 'FC095';
321
+ END IF;
322
+
323
+ -- THE SAME TASK, never a fresh one (R8): the next claimant resumes on the
324
+ -- same work, and minting a new task id would leave the rejected work with two
325
+ -- records and the waiting edge gating the abandoned one.
326
+ UPDATE cell.task
327
+ SET origin = 'reopened'
328
+ WHERE org_id = p_org_id
329
+ AND task_id = p_task_id
330
+ AND origin <> 'reopened';
331
+
332
+ GET DIAGNOSTICS v_moved = ROW_COUNT;
333
+ RETURN v_moved = 1;
334
+ END;
335
+ $reopen$;
336
+
337
+ COMMENT ON FUNCTION cell.reopen_rejected_task IS
338
+ 'FOUNDER RULING R8. One verb for both rejections — a gate''s and an '
339
+ 'operator''s — because they mean the same thing: this work is not done and '
340
+ 'the next claimant resumes on it.';
341
+
342
+
343
+ -- The TRANSCRIPTION, and the reason it is lawful for a lane to write a
344
+ -- verification at all: it invents nothing. The operator's name is in
345
+ -- `consumed_by`, the instant in `consumed_at`, the binding in `state_version`,
346
+ -- and what the answer MEANS is `cell.v_confirm_verdict.projects`. Every column
347
+ -- below is read off a row or is a constant this file declares; there is nowhere
348
+ -- in the statement to put a value the caller supplied except the identifier it
349
+ -- derived and the trace ref R4 makes mandatory.
350
+ CREATE OR REPLACE FUNCTION cell.record_confirm_verdict(
351
+ p_token_id uuid,
352
+ p_verification_id uuid,
353
+ p_trace_ref text,
354
+ p_recorded_at timestamptz
355
+ )
356
+ RETURNS boolean
357
+ LANGUAGE plpgsql
358
+ AS $transcribe$
359
+ DECLARE
360
+ v_ask cell.v_evaluator_confirm_ask%ROWTYPE;
361
+ v_written integer;
362
+ BEGIN
363
+ IF length(btrim(coalesce(p_trace_ref, ''))) = 0 THEN
364
+ RAISE EXCEPTION
365
+ 'CONFIRM_VERDICT_UNADDRESSABLE_TRACE:% — FOUNDER RULING R4 puts a '
366
+ 'durable trace_id on every verification so bad decisions can seed DPO '
367
+ 'datasets from events (Langfuse is a replaceable view, L14). A blank '
368
+ 'ref is an absent one wearing a value''s clothes.',
369
+ p_token_id
370
+ USING ERRCODE = 'FC097';
371
+ END IF;
372
+
373
+ SELECT * INTO v_ask FROM cell.v_evaluator_confirm_ask
374
+ WHERE token_id = p_token_id;
375
+
376
+ IF NOT FOUND
377
+ OR v_ask.projects = 'unconsumed'
378
+ OR NOT v_ask.clause_declared
379
+ OR v_ask.statement_id IS NULL THEN
380
+ RAISE EXCEPTION
381
+ 'CONFIRM_VERDICT_NOT_ANSWERABLE:% — a transcription needs an ask that '
382
+ 'exists, an operator who ANSWERED it, a plan that declares the clause '
383
+ 'the answer discharges, and a recorded statement for the answer to be '
384
+ 'a verdict about (§1.4). This one has: found=%, projects=%, '
385
+ 'clause_declared=%, statement=%. Writing anyway would mint a verdict '
386
+ 'about nothing anybody said, or one discharging a clause no reader of '
387
+ 'the plan can account for.',
388
+ p_token_id, FOUND, coalesce(v_ask.projects, '<no ask>'),
389
+ coalesce(v_ask.clause_declared::text, '<no ask>'),
390
+ coalesce(v_ask.statement_id::text, '<none>')
391
+ USING ERRCODE = 'FC096';
392
+ END IF;
393
+
394
+ -- Exactly-once is the PRIMARY KEY on a DERIVED identifier, not a NOT EXISTS
395
+ -- and not a set in a process: two racers on the same tick address the SAME
396
+ -- row and the index settles it under the row lock Postgres already takes.
397
+ -- `recorded_at` is the instant of the TRANSCRIPTION; the DECISION's instant
398
+ -- is `consumed_at`, durable in the token and carried into `checks` so a
399
+ -- reader holding only this row can never confuse the two.
400
+ INSERT INTO cell.verification (
401
+ verification_id, org_id, task_id, statement_id, source, result,
402
+ verifier_principal, verifier_type, verification_plan_ref, checks,
403
+ evidence_refs, trace_ref, privacy_class, transfer_scope, state_version,
404
+ recorded_at)
405
+ VALUES (
406
+ p_verification_id,
407
+ v_ask.org_id,
408
+ v_ask.task_id,
409
+ v_ask.statement_id,
410
+ 'confirm_token_consume',
411
+ v_ask.projects,
412
+ v_ask.consumed_by,
413
+ 'operator',
414
+ 'operator_approval',
415
+ jsonb_build_array(jsonb_build_object(
416
+ 'check', 'confirm_token_consume',
417
+ 'ask_key', v_ask.ask_key,
418
+ 'token_id', v_ask.token_id,
419
+ 'projects', v_ask.projects,
420
+ 'consumed_by', v_ask.consumed_by,
421
+ 'consumed_at', v_ask.consumed_at)),
422
+ jsonb_build_array('cell.confirm_token:' || v_ask.token_id::text,
423
+ 'cell.agent_statement:' || v_ask.statement_id::text),
424
+ p_trace_ref,
425
+ -- H7 / PRD §16, chosen rather than defaulted: an operator's verdict on
426
+ -- this org's own work is org_internal content, and `cell_only` is the
427
+ -- tighter of the two transfer scopes that class permits. A record naming
428
+ -- a person, a decision and the work it was about has no reason to leave
429
+ -- the cell.
430
+ 'org_internal',
431
+ 'cell_only',
432
+ v_ask.state_version,
433
+ p_recorded_at)
434
+ ON CONFLICT (verification_id) DO NOTHING;
435
+
436
+ GET DIAGNOSTICS v_written = ROW_COUNT;
437
+ RETURN v_written = 1;
438
+ END;
439
+ $transcribe$;
440
+
441
+ COMMENT ON FUNCTION cell.record_confirm_verdict IS
442
+ 'Contract §1.4 source 3. A TRANSCRIPTION, not a decision: every column is '
443
+ 'read off the ask, the statement and the plan, and the projection of a '
444
+ 'NULL-verdict consume is v_confirm_verdict''s (MEASURED: 217 rows).';