@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,480 @@
1
+ -- 003_verification.sql — the two record classes, and the ONE derived answer.
2
+ --
3
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.3 (agent_statement), §1.4
4
+ -- (verification), §1.5 (completion), AMENDED 2026-08-16 by founder rulings R1
5
+ -- (verification is non-author only), R2 (mandatory verify_by clock) and R4
6
+ -- (mandatory evidence, trace_ref, privacy_class, transfer_scope; the seven
7
+ -- done-ness mechanisms collapse to these two record classes).
8
+ -- Requires 001_core.sql.
9
+ --
10
+ -- Re-apply: idempotent (CREATE ... IF NOT EXISTS / OR REPLACE; the one ALTER is
11
+ -- guarded on pg_constraint).
12
+ -- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
13
+ --
14
+ -- WHY COMPLETION IS A VIEW AND NOT A COLUMN
15
+ --
16
+ -- On the Mac, "done" was reachable through whichever of seven overlapping
17
+ -- mechanisms a code path happened to consult, and a stored `closed` flag was one
18
+ -- of them — which is how the dependency system ended up releasing on the
19
+ -- upstream author's SELF-ASSERTED completion (contract §3.3, MEASURED). A stored
20
+ -- completion is a fact somebody can write; a derived one is a fact that follows
21
+ -- from the records, and the only way to change it is to change the records. The
22
+ -- tri-state {complete, asserted_unverified, none} is therefore computed here and
23
+ -- has no writable form anywhere in the cell.
24
+ --
25
+ -- Declared failure vocabulary added by this file (SQLSTATE class FC; this lane
26
+ -- reserves FC010-FC019):
27
+ -- FC010 VERIFICATION_AUTHOR_SELF a verifier who authored, or holds the claim
28
+ -- over, the statement being verified
29
+ -- FC011 SUPERSEDES_CROSSES_TASK a statement retracting another task's record
30
+ -- FC012 SUPERSEDES_WITHOUT_RESPONSIBILITY a supersession by a writer holding
31
+ -- no claim on the work being retracted
32
+ -- FC013 SUPERSEDES_WITHOUT_EVIDENCE a supersession with nothing addressable
33
+ -- behind it
34
+
35
+ -- ---------------------------------------------------------------------------
36
+ -- agent_statement — what an agent SAYS (contract §1.3)
37
+ --
38
+ -- "May be useful evidence. Never automatically authoritative truth" (L10). This
39
+ -- table is deliberately permissive about content and strict about provenance:
40
+ -- an author may say anything, and the substrate records exactly who said it,
41
+ -- under which claim, with what epistemic standing.
42
+ -- ---------------------------------------------------------------------------
43
+ CREATE TABLE IF NOT EXISTS cell.agent_statement (
44
+ statement_id uuid NOT NULL,
45
+ org_id text NOT NULL,
46
+ task_id text NOT NULL,
47
+ work_claim_id uuid NOT NULL,
48
+
49
+ -- Two identities, not one (contract §1.3, §2.1): statements from temporary
50
+ -- execution actors carry the ACTOR identity distinct from the claim-holding
51
+ -- principal. Collapsing them would make an in-process subagent's assertion
52
+ -- indistinguishable from its principal's, and the non-author rule below
53
+ -- would then be enforceable against only half of the authors there are.
54
+ execution_actor text NOT NULL,
55
+ claim_principal text NOT NULL,
56
+
57
+ statement_type text NOT NULL,
58
+ subject_ref text NOT NULL,
59
+ proposition text NOT NULL,
60
+
61
+ -- An array, possibly EMPTY: a statement with no evidence is a lawful
62
+ -- statement (that is what makes it a statement). The mandatory-evidence rule
63
+ -- of R4 binds `verification`, where the absence of evidence would be a claim
64
+ -- to have checked something without saying what was checked.
65
+ evidence_refs jsonb NOT NULL,
66
+
67
+ -- L13: observed | derived | inferred | unknown.
68
+ epistemic_status text NOT NULL,
69
+
70
+ -- NULL exactly when this statement supersedes nothing, which is the ordinary
71
+ -- case; a superseded completion assertion stops counting in v_task_completion.
72
+ supersedes_ref uuid,
73
+
74
+ asserted_at timestamptz NOT NULL,
75
+
76
+ CONSTRAINT agent_statement_pkey PRIMARY KEY (statement_id),
77
+
78
+ -- The handle `verification` references. Task identity travels WITH the
79
+ -- statement identity so a verification cannot name one task while evaluating
80
+ -- a statement made about another.
81
+ CONSTRAINT agent_statement_task_scoped_key UNIQUE (org_id, task_id, statement_id),
82
+
83
+ CONSTRAINT agent_statement_task_fkey
84
+ FOREIGN KEY (org_id, task_id) REFERENCES cell.task (org_id, task_id),
85
+
86
+ CONSTRAINT agent_statement_claim_fkey
87
+ FOREIGN KEY (org_id, work_claim_id)
88
+ REFERENCES cell.work_claim (org_id, work_claim_id),
89
+
90
+ CONSTRAINT agent_statement_supersedes_fkey
91
+ FOREIGN KEY (supersedes_ref) REFERENCES cell.agent_statement (statement_id),
92
+
93
+ CONSTRAINT agent_statement_statement_type_is_a_declared_type
94
+ CHECK (statement_type IN ('completion', 'progress', 'result', 'observation')),
95
+
96
+ CONSTRAINT agent_statement_epistemic_status_is_a_declared_status
97
+ CHECK (epistemic_status IN ('observed', 'derived', 'inferred', 'unknown')),
98
+
99
+ CONSTRAINT agent_statement_evidence_refs_is_an_array
100
+ CHECK (jsonb_typeof(evidence_refs) = 'array'),
101
+
102
+ CONSTRAINT agent_statement_supersedes_something_other_than_itself
103
+ CHECK (supersedes_ref IS DISTINCT FROM statement_id)
104
+ );
105
+
106
+ COMMENT ON TABLE cell.agent_statement IS
107
+ 'Contract §1.3. A completion assertion is a statement, not completion: '
108
+ 'author-run test output, gate-hook passes and self-reports all land here.';
109
+
110
+ CREATE INDEX IF NOT EXISTS agent_statement_by_task
111
+ ON cell.agent_statement (org_id, task_id, statement_type);
112
+
113
+ CREATE INDEX IF NOT EXISTS agent_statement_by_supersedes
114
+ ON cell.agent_statement (supersedes_ref) WHERE supersedes_ref IS NOT NULL;
115
+
116
+ -- ------------------------------------------ a supersession is scoped to the work
117
+ --
118
+ -- `agent_statement_supersedes_fkey` checks only that the superseded statement
119
+ -- EXISTS, and v_task_completion's NOT EXISTS clause below looks at nothing but
120
+ -- the ref — so, unscoped, ANY statement anywhere could retract ANY task's
121
+ -- completion assertion. That assertion is the input to the R4 dependency release
122
+ -- bar (§4), which makes an unscoped supersession a way to move the release bar
123
+ -- for work the writer has nothing to do with, leaving no evidence and no
124
+ -- verification record anywhere in the substrate.
125
+ --
126
+ -- Three scopes, and deliberately not a fourth:
127
+ --
128
+ -- 1. SAME TASK. A statement about other work cannot speak to this record.
129
+ -- 2. UNDER A CLAIM ON THIS TASK. A responsibility rule, not an author rule:
130
+ -- the successor claimant after a yield MUST be able to correct the previous
131
+ -- claimant's assertion (R8 — the reopened task's next claimant resumes on
132
+ -- the same work), so what is checked is that the writer holds an interval of
133
+ -- responsibility for the work whose done-ness they are retracting.
134
+ -- 3. EVIDENCE. An ordinary statement with no evidence is lawful — that is what
135
+ -- makes it a statement (see the column comment above). A statement that
136
+ -- retracts the release bar's input is a claim about the world, and L13 says
137
+ -- a claim about the world is addressable.
138
+ --
139
+ -- The fourth, NOT enforced: statement_type equality. A withdrawal is properly
140
+ -- recorded as a `progress` statement superseding a `completion` assertion, so
141
+ -- requiring the types to match would forbid the ordinary retraction.
142
+ CREATE OR REPLACE FUNCTION cell.enforce_supersession_is_scoped()
143
+ RETURNS trigger
144
+ LANGUAGE plpgsql
145
+ AS $supersede$
146
+ DECLARE
147
+ v_superseded_task text;
148
+ v_claim_task text;
149
+ BEGIN
150
+ IF NEW.supersedes_ref IS NULL THEN
151
+ RETURN NEW;
152
+ END IF;
153
+
154
+ SELECT task_id INTO v_superseded_task
155
+ FROM cell.agent_statement
156
+ WHERE statement_id = NEW.supersedes_ref;
157
+
158
+ IF NOT FOUND THEN
159
+ -- agent_statement_supersedes_fkey is the authority on whether the named
160
+ -- statement exists, and it refuses this row at the end of the statement
161
+ -- with the FK's own message. A second, competing refusal here would
162
+ -- report a scoping problem for what is a dangling reference.
163
+ RETURN NEW;
164
+ END IF;
165
+
166
+ IF v_superseded_task IS DISTINCT FROM NEW.task_id THEN
167
+ RAISE EXCEPTION
168
+ 'SUPERSEDES_CROSSES_TASK:% — this statement is about ''%'' and '
169
+ 'supersedes a statement about ''%''. A completion assertion is the R4 '
170
+ 'release bar''s input; letting another task''s writer retract it moves '
171
+ 'that bar with nothing recorded about the work it actually gates.',
172
+ NEW.statement_id, NEW.task_id, v_superseded_task
173
+ USING ERRCODE = 'FC011';
174
+ END IF;
175
+
176
+ SELECT task_id INTO v_claim_task
177
+ FROM cell.work_claim
178
+ WHERE org_id = NEW.org_id AND work_claim_id = NEW.work_claim_id;
179
+
180
+ IF v_claim_task IS DISTINCT FROM NEW.task_id THEN
181
+ RAISE EXCEPTION
182
+ 'SUPERSEDES_WITHOUT_RESPONSIBILITY:%:% — this supersession is written '
183
+ 'under a claim on ''%'', not on ''%''. Retracting what the company '
184
+ 'believes about work is an act of the principal responsible for it; a '
185
+ 'bystander retraction is an authority change nobody granted (L11). '
186
+ 'This is a responsibility rule and not an author rule: the NEXT '
187
+ 'claimant of the same work supersedes freely.',
188
+ NEW.statement_id, NEW.work_claim_id,
189
+ coalesce(v_claim_task, '<no such claim>'), NEW.task_id
190
+ USING ERRCODE = 'FC012';
191
+ END IF;
192
+
193
+ IF jsonb_array_length(NEW.evidence_refs) = 0 THEN
194
+ RAISE EXCEPTION
195
+ 'SUPERSEDES_WITHOUT_EVIDENCE:% — a supersession retracts the record '
196
+ 'the dependency lane releases on, so it carries at least one '
197
+ 'addressable evidence ref (L13). An ordinary statement may still carry '
198
+ 'none; retracting done-ness silently is what this refuses.',
199
+ NEW.statement_id
200
+ USING ERRCODE = 'FC013';
201
+ END IF;
202
+
203
+ RETURN NEW;
204
+ END;
205
+ $supersede$;
206
+
207
+ CREATE OR REPLACE TRIGGER agent_statement_supersession_is_scoped
208
+ BEFORE INSERT OR UPDATE ON cell.agent_statement
209
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_supersession_is_scoped();
210
+
211
+ -- ---------------------------------------------------------------------------
212
+ -- verification — a NON-AUTHOR evaluation against an explicit contract (§1.4)
213
+ --
214
+ -- Exactly three sources mint a row here, and nothing else does:
215
+ -- dep_verified_evaluator the one non-author evaluator running a registered
216
+ -- checker (contract §4)
217
+ -- acceptance_package an acceptance-package v1 verdict whose `ruled_by` is
218
+ -- a registered non-author seat
219
+ -- confirm_token_consume an operator CONFIRM, state_version-bound; a REJECT
220
+ -- consume is a verification with result='rejected'
221
+ --
222
+ -- Retracting a dependency edge mints NO row here (§4.4), and the auto-void
223
+ -- bypass class is empty by construction (§3.1) because the cell has no auto-void.
224
+ -- ---------------------------------------------------------------------------
225
+ CREATE TABLE IF NOT EXISTS cell.verification (
226
+ verification_id uuid NOT NULL,
227
+ org_id text NOT NULL,
228
+ task_id text NOT NULL,
229
+
230
+ -- NOT NULL on purpose: every verification in the cell evaluates a RECORDED
231
+ -- statement. It is what gives the non-author rule an author to compare
232
+ -- against, and a verification floating free of any statement is a verdict
233
+ -- about nothing anybody said.
234
+ statement_id uuid NOT NULL,
235
+
236
+ source text NOT NULL,
237
+ result text NOT NULL,
238
+ verifier_principal text NOT NULL,
239
+ verifier_type text NOT NULL,
240
+
241
+ -- Names the plan CLAUSE this record discharges. v_task_completion counts
242
+ -- clauses, so a verification naming no clause discharges nothing — which is
243
+ -- the honest outcome for a verdict that does not say what it verified.
244
+ verification_plan_ref text NOT NULL,
245
+
246
+ checks jsonb NOT NULL,
247
+
248
+ -- FOUNDER RULING R4: mandatory, and non-empty. The `--clears` lane's
249
+ -- optional-evidence default does not survive into the cell.
250
+ evidence_refs jsonb NOT NULL,
251
+
252
+ -- FOUNDER RULING R4: a durable trace_id held IN THE SUBSTRATE, so bad
253
+ -- decisions can seed DPO datasets from events. Langfuse is a replaceable
254
+ -- view over this column, never its home (L14).
255
+ trace_ref text NOT NULL,
256
+
257
+ -- H7 / PRD §16, from day one: what this record is, and how far it may travel.
258
+ privacy_class text NOT NULL,
259
+ transfer_scope text NOT NULL,
260
+
261
+ -- §1.4 source 3 is state_version-bound (the 0073 lesson). NOT NULL exactly
262
+ -- for a confirm-token consume; meaningless — and therefore forbidden — on an
263
+ -- evaluator verdict or an acceptance package.
264
+ state_version bigint,
265
+
266
+ recorded_at timestamptz NOT NULL,
267
+
268
+ CONSTRAINT verification_pkey PRIMARY KEY (verification_id),
269
+
270
+ CONSTRAINT verification_statement_fkey
271
+ FOREIGN KEY (org_id, task_id, statement_id)
272
+ REFERENCES cell.agent_statement (org_id, task_id, statement_id),
273
+
274
+ CONSTRAINT verification_source_is_a_declared_source
275
+ CHECK (source IN ('dep_verified_evaluator', 'acceptance_package',
276
+ 'confirm_token_consume')),
277
+
278
+ CONSTRAINT verification_result_is_a_declared_verdict
279
+ CHECK (result IN ('verified', 'rejected', 'indeterminate')),
280
+
281
+ CONSTRAINT verification_verifier_type_matches_its_source
282
+ CHECK ((source = 'dep_verified_evaluator' AND verifier_type = 'evaluator')
283
+ OR (source = 'acceptance_package' AND verifier_type = 'seat')
284
+ OR (source = 'confirm_token_consume' AND verifier_type = 'operator')),
285
+
286
+ CONSTRAINT verification_consume_is_state_version_bound
287
+ CHECK ((source = 'confirm_token_consume') = (state_version IS NOT NULL)),
288
+
289
+ CONSTRAINT verification_carries_addressable_evidence
290
+ CHECK (jsonb_typeof(evidence_refs) = 'array'
291
+ AND jsonb_array_length(evidence_refs) > 0),
292
+
293
+ CONSTRAINT verification_checks_is_an_array
294
+ CHECK (jsonb_typeof(checks) = 'array'),
295
+
296
+ -- NOT NULL permits the empty string; a blank trace_ref is an absent one
297
+ -- wearing a value's clothes, and the DPO lane would carry unresolvable refs.
298
+ CONSTRAINT verification_trace_ref_is_addressable
299
+ CHECK (length(btrim(trace_ref)) > 0),
300
+
301
+ CONSTRAINT verification_privacy_class_is_a_declared_class
302
+ CHECK (privacy_class IN ('public', 'org_internal', 'customer_derived')),
303
+
304
+ CONSTRAINT verification_transfer_scope_is_a_declared_scope
305
+ CHECK (transfer_scope IN ('cell_only', 'org_internal', 'exportable')),
306
+
307
+ -- H7: raw conversation and behavioural content stays in the cell. Stated as
308
+ -- a constraint rather than a review habit, because the review that catches
309
+ -- this is the one nobody schedules.
310
+ CONSTRAINT verification_customer_derived_content_stays_in_the_cell
311
+ CHECK (privacy_class <> 'customer_derived' OR transfer_scope = 'cell_only')
312
+ );
313
+
314
+ COMMENT ON TABLE cell.verification IS
315
+ 'Contract §1.4 as amended by R1/R4. Exactly three non-author sources mint a '
316
+ 'row; retraction mints none; the auto-void bypass class is empty because the '
317
+ 'cell has no auto-void.';
318
+
319
+ CREATE INDEX IF NOT EXISTS verification_by_task_clause
320
+ ON cell.verification (org_id, task_id, verification_plan_ref, result);
321
+
322
+ -- ------------------------------------------------ the non-author rule (R1)
323
+ --
324
+ -- A CHECK constraint cannot see another table, so this is the one rule in this
325
+ -- file that has to be a trigger. It is not application code: a caller that never
326
+ -- heard of the cell library still meets it, which is the whole lesson of the 23
327
+ -- historical `verified` settles that reached the bus past a CLI-only gate.
328
+ CREATE OR REPLACE FUNCTION cell.enforce_non_author_verification()
329
+ RETURNS trigger
330
+ LANGUAGE plpgsql
331
+ AS $non_author$
332
+ DECLARE
333
+ v_actor text;
334
+ v_principal text;
335
+ BEGIN
336
+ SELECT execution_actor, claim_principal INTO v_actor, v_principal
337
+ FROM cell.agent_statement
338
+ WHERE statement_id = NEW.statement_id;
339
+
340
+ IF NEW.verifier_principal IN (v_actor, v_principal) THEN
341
+ RAISE EXCEPTION
342
+ 'VERIFICATION_AUTHOR_SELF:% — % authored the statement under evaluation '
343
+ '(actor %, claim principal %), so this record is a self-report wearing '
344
+ 'a verification''s name. Verification is non-author only: author-run '
345
+ 'test output and gate-hook passes are agent_statements with degraded '
346
+ 'epistemic status — they inform, they never release.',
347
+ NEW.statement_id, NEW.verifier_principal, v_actor, v_principal
348
+ USING ERRCODE = 'FC010';
349
+ END IF;
350
+
351
+ RETURN NEW;
352
+ END;
353
+ $non_author$;
354
+
355
+ CREATE OR REPLACE TRIGGER verification_is_non_author
356
+ BEFORE INSERT OR UPDATE ON cell.verification
357
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_non_author_verification();
358
+
359
+ -- ------------------------------------- a declared plan names its clauses
360
+ --
361
+ -- Additive amendment to cell.task from 001_core.sql. Without it, a plan that
362
+ -- says `plan_status: declared` with an empty clause list has zero required
363
+ -- clauses, zero unsatisfied ones, and would therefore project COMPLETE off a
364
+ -- bare assertion — an undeclared plan being the easiest plan in the cell to
365
+ -- satisfy. `plan_status: unknown` remains lawful and clause-less: that is the
366
+ -- honest report of a task with no plan (§1.1, L13), and it never projects
367
+ -- complete either.
368
+ DO $declared_plan$
369
+ BEGIN
370
+ IF NOT EXISTS (
371
+ SELECT 1 FROM pg_constraint
372
+ WHERE conname = 'task_declared_plan_names_its_clauses'
373
+ AND conrelid = 'cell.task'::regclass
374
+ ) THEN
375
+ -- The `IS NOT NULL` is load-bearing, exactly as it is on 001_core.sql's
376
+ -- plan-status constraint: with no 'clauses' key at all the -> operator
377
+ -- yields SQL NULL, jsonb_typeof(NULL) is NULL, the AND chain is NULL, and
378
+ -- a CHECK PASSES on NULL — so the clause-less declared plan this
379
+ -- constraint exists to refuse would sail straight through.
380
+ ALTER TABLE cell.task ADD CONSTRAINT task_declared_plan_names_its_clauses
381
+ CHECK (verification_plan ->> 'plan_status' IS DISTINCT FROM 'declared'
382
+ OR (verification_plan -> 'clauses' IS NOT NULL
383
+ AND jsonb_typeof(verification_plan -> 'clauses') = 'array'
384
+ AND jsonb_array_length(verification_plan -> 'clauses') > 0));
385
+ END IF;
386
+ END
387
+ $declared_plan$;
388
+
389
+ -- ---------------------------------------------------------------------------
390
+ -- v_task_completion — the derived tri-state (contract §1.5)
391
+ --
392
+ -- none nobody has asserted completion
393
+ -- asserted_unverified asserted, but the plan's clauses are not all discharged
394
+ -- by a recorded verification — the F6 read guarantee
395
+ -- complete every declared clause carries a `verified` record
396
+ --
397
+ -- Completion is never the same event as settlement (§1.6): a completed-but-
398
+ -- unsettled task is a first-class, honestly-rendered condition, and this view
399
+ -- says nothing about settlement at all.
400
+ -- ---------------------------------------------------------------------------
401
+ CREATE OR REPLACE VIEW cell.v_task_completion AS
402
+ SELECT
403
+ t.org_id,
404
+ t.task_id,
405
+ t.department,
406
+ t.verify_by,
407
+ a.completion_assertions,
408
+ p.required_clauses,
409
+ p.satisfied_clauses,
410
+ CASE
411
+ WHEN a.completion_assertions = 0 THEN 'none'
412
+ -- An unknown plan can never be discharged, so it can never complete. The
413
+ -- ordering matters: this arm sits ABOVE the clause count so a plan with
414
+ -- no clauses cannot fall through to the equality below.
415
+ WHEN t.verification_plan ->> 'plan_status' = 'unknown' THEN 'asserted_unverified'
416
+ WHEN p.satisfied_clauses = p.required_clauses THEN 'complete'
417
+ ELSE 'asserted_unverified'
418
+ END AS completion
419
+ FROM cell.task t
420
+ CROSS JOIN LATERAL (
421
+ SELECT count(*)::int AS completion_assertions
422
+ FROM cell.agent_statement s
423
+ WHERE s.org_id = t.org_id
424
+ AND s.task_id = t.task_id
425
+ AND s.statement_type = 'completion'
426
+ AND NOT EXISTS (
427
+ SELECT 1 FROM cell.agent_statement later
428
+ WHERE later.supersedes_ref = s.statement_id)
429
+ ) a
430
+ CROSS JOIN LATERAL (
431
+ SELECT count(*)::int AS required_clauses,
432
+ count(*) FILTER (WHERE EXISTS (
433
+ SELECT 1 FROM cell.verification v
434
+ WHERE v.org_id = t.org_id
435
+ AND v.task_id = t.task_id
436
+ AND v.verification_plan_ref = clause
437
+ AND v.result = 'verified'))::int AS satisfied_clauses
438
+ FROM jsonb_array_elements_text(
439
+ coalesce(t.verification_plan -> 'clauses', '[]'::jsonb)) AS c(clause)
440
+ ) p;
441
+
442
+ COMMENT ON VIEW cell.v_task_completion IS
443
+ 'Contract §1.5. The tri-state is DERIVED and has no writable form: a stored '
444
+ 'completion is a fact somebody can write, which is how the Mac''s dependency '
445
+ 'system ended up releasing on the upstream author''s self-assertion.';
446
+
447
+ -- ---------------------------------------------------------------------------
448
+ -- unverified_past_verify_by — the R2 clock leg, as a query (contract §8, F2)
449
+ --
450
+ -- The instant is an EXPLICIT argument. A function reading now() cannot be tested
451
+ -- at a stated instant, and a falsifier leg that cannot state its instant is a
452
+ -- flake wearing a green tick. Callers pass the clock they are reasoning about;
453
+ -- the pager passes the current one.
454
+ --
455
+ -- Note what this deliberately does NOT report: work nobody has asserted
456
+ -- complete. The leg is `asserted_unverified past verify_by`, not `past
457
+ -- verify_by`; reporting open work as a verification failure would bury the real
458
+ -- ones, which is the widened-arm lesson of contract §3.4(1).
459
+ -- ---------------------------------------------------------------------------
460
+ CREATE OR REPLACE FUNCTION cell.unverified_past_verify_by(p_as_of timestamptz)
461
+ RETURNS TABLE (
462
+ org_id text,
463
+ task_id text,
464
+ department text,
465
+ completion text,
466
+ verify_by timestamptz
467
+ )
468
+ LANGUAGE sql
469
+ STABLE
470
+ AS $overdue$
471
+ SELECT c.org_id, c.task_id, c.department, c.completion, c.verify_by
472
+ FROM cell.v_task_completion c
473
+ WHERE c.completion = 'asserted_unverified'
474
+ AND c.verify_by < p_as_of
475
+ ORDER BY c.verify_by, c.task_id;
476
+ $overdue$;
477
+
478
+ COMMENT ON FUNCTION cell.unverified_past_verify_by IS
479
+ 'FOUNDER RULING R2 / F2 clock leg: every row this returns is RED and pages. '
480
+ 'The instant is an explicit argument so a leg can state the moment it means.';