@oathe/oathe 0.0.1 → 0.2.0

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 (70) hide show
  1. package/LICENSE +202 -21
  2. package/NOTICE +7 -0
  3. package/README.md +99 -12
  4. package/assets/oathe-hero.svg +92 -0
  5. package/bin/oathe.mjs +376 -0
  6. package/docs/PRIVACY.md +45 -0
  7. package/package.json +32 -23
  8. package/plugin/.claude-plugin/plugin.json +10 -0
  9. package/plugin/.mcp.json +13 -0
  10. package/plugin/hooks/frame-note.mjs +24 -0
  11. package/plugin/hooks/heartbeat.mjs +49 -0
  12. package/plugin/hooks/hooks.json +39 -0
  13. package/plugin/hooks/lib.mjs +83 -0
  14. package/plugin/hooks/render-board.mjs +11 -0
  15. package/plugin/skills/oathe-work/SKILL.md +33 -0
  16. package/src/atif.mjs +633 -0
  17. package/src/blocks.mjs +192 -0
  18. package/src/board-render.mjs +106 -0
  19. package/src/config.mjs +169 -0
  20. package/src/context.mjs +35 -0
  21. package/src/doctor.mjs +108 -0
  22. package/src/harness.mjs +271 -0
  23. package/src/init.mjs +92 -0
  24. package/src/launch-env.mjs +25 -0
  25. package/src/launch.mjs +259 -0
  26. package/src/manifest.mjs +89 -0
  27. package/src/mcp/oathe-tools.mjs +517 -0
  28. package/src/paths.mjs +49 -0
  29. package/src/plans.mjs +50 -0
  30. package/src/runtime/discharge.mjs +57 -0
  31. package/src/runtime/provider.mjs +154 -0
  32. package/src/runtime/simple-cage.mjs +67 -0
  33. package/src/runtime/sql-acceptance-lane.mjs +89 -0
  34. package/src/session-host.mjs +81 -0
  35. package/src/substrate.mjs +337 -0
  36. package/src/successor.mjs +31 -0
  37. package/src/traces.mjs +242 -0
  38. package/src/uninstall.mjs +45 -0
  39. package/src/verifier.mjs +322 -0
  40. package/src/workspace.mjs +47 -0
  41. package/vendor/ddl/001_core.sql +312 -0
  42. package/vendor/ddl/002_claim.sql +438 -0
  43. package/vendor/ddl/003_verification.sql +480 -0
  44. package/vendor/ddl/004_delegation.sql +397 -0
  45. package/vendor/ddl/005_escalation.sql +303 -0
  46. package/vendor/ddl/006_dependency.sql +384 -0
  47. package/vendor/ddl/007_assignment.sql +502 -0
  48. package/vendor/ddl/008_effect_receipt.sql +290 -0
  49. package/vendor/ddl/009_verified_edge.sql +452 -0
  50. package/vendor/ddl/010_parent_child_verification.sql +320 -0
  51. package/vendor/ddl/011_confirm.sql +288 -0
  52. package/vendor/ddl/012_evaluator_lane.sql +444 -0
  53. package/vendor/ddl/013_claim_terminal.sql +279 -0
  54. package/vendor/ddl/014_claim_settlement.sql +327 -0
  55. package/vendor/ddl/015_claim_yield.sql +213 -0
  56. package/vendor/ddl/016_reopened_reclaim.sql +285 -0
  57. package/vendor/ddl/017_claim_yield_causes.sql +357 -0
  58. package/vendor/ddl/018_receiptless_unknown.sql +249 -0
  59. package/vendor/ddl/019_verification_contract_freeze.sql +335 -0
  60. package/vendor/ddl/020_context_compilation.sql +105 -0
  61. package/vendor/ddl/021_execution_attempt_closure.sql +95 -0
  62. package/vendor/ddl/022_verification_clause.sql +106 -0
  63. package/vendor/ddl/023_executor_role.sql +66 -0
  64. package/vendor/ddl/024_executor_role_hardening.sql +439 -0
  65. package/vendor/ddl/025_running_attempt_uniqueness.sql +102 -0
  66. package/vendor/ddl/026_acceptance_authority.sql +230 -0
  67. package/vendor/ddl/027_ownership_return.sql +836 -0
  68. package/vendor/ddl/028_attempt_bind_ownership.sql +173 -0
  69. package/vendor/ddl/manifest.json +183 -0
  70. package/index.js +0 -33
@@ -0,0 +1,502 @@
1
+ -- 007_assignment.sql — direct assignment: claim-or-refuse, the decline
2
+ -- tombstone, and the deadline lapse that escalates to the assigner.
3
+ --
4
+ -- Contract: docs/a1-cell-work-spine-contract.md §2.3 as amended, FOUNDER RULING
5
+ -- R5 — "Direct assignment is built now rather than deferred: `to_principal` is a
6
+ -- real routing field; a claim-or-refuse contract with an explicit deadline; a
7
+ -- typed `task_declined` event — the bar whitelist comes from the never-defer
8
+ -- rules, extended with `not-my-capability` and `wrong-principal`; declines
9
+ -- TOMBSTONE, closing the measured re-dispatch-forever defect; deadline lapse
10
+ -- with neither claim nor decline → escalation to the assigner."
11
+ -- Requires 001_core.sql, 004_delegation.sql and 005_escalation.sql.
12
+ --
13
+ -- Re-apply: idempotent (CREATE ... IF NOT EXISTS / OR REPLACE, ON CONFLICT seed).
14
+ -- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
15
+ --
16
+ -- THE DEFECT THE TOMBSTONE CLOSES, stated precisely because it decides the
17
+ -- shape of the rule: declined work kept being re-dispatched, so the same
18
+ -- refusal was collected forever and nobody was ever told. A tombstone is
19
+ -- therefore a REFUSAL of any subsequent assignment — not merely the absence of
20
+ -- one — and it holds for a re-offer to a DIFFERENT principal too, because
21
+ -- re-dispatch-forever never required the same addressee.
22
+ --
23
+ -- WHY ASSIGNING NEEDS THE SAME GRANT AS CREATING
24
+ --
25
+ -- Routing existing work into a department decides who that department answers
26
+ -- for; treating it as a lesser act would leave an ambient authority the grants
27
+ -- table exists to remove. So the assignment record meets the SAME
28
+ -- cell.enforce_delegation_grant question — may this principal put work in this
29
+ -- department — through its own substrate trigger. A deployment seats each
30
+ -- lead's own department grant explicitly; nothing is implied by membership.
31
+ --
32
+ -- WHY THE LAPSE IS A QUERY AND NOT A DAEMON
33
+ --
34
+ -- "Deadline lapse with neither claim nor decline" is a CONDITION over rows, so
35
+ -- it is a function over rows, evaluated at an instant the caller supplies. A
36
+ -- condition that reads the wall clock cannot be asserted by a falsifier, and an
37
+ -- operator asking "what had lapsed at 14:30" is asking something the substrate
38
+ -- should be able to answer. The escalation it yields is a separate, explicit
39
+ -- verb that RE-CHECKS the condition, because an escalation minted from a
40
+ -- condition that no longer holds is a page nobody can act on.
41
+ --
42
+ -- Declared failure vocabulary added by this file (SQLSTATE class FC; this lane
43
+ -- reserves FC020-FC039):
44
+ -- FC020 DELEGATION_NOT_GRANTED work put in a scope not granted
45
+ -- (re-raised; owned by 004)
46
+ -- FC021 UNKNOWN_TASK an assignment routing work that does not
47
+ -- exist (re-raised; owned by 004)
48
+ -- FC027 ASSIGNMENT_TOMBSTONED a re-offer of work that was declined
49
+ -- FC028 DECLINE_BAR_NOT_WHITELISTED a bar outside the ruled vocabulary
50
+ -- FC029 DECLINE_BY_NON_ASSIGNEE a decline by someone it was not offered to
51
+ -- FC030 TASK_ALREADY_DECLINED a second decline of the same work
52
+ -- FC031 ASSIGNMENT_NOT_LAPSED escalating a lapse that has not happened
53
+
54
+ -- ---------------------------------------------------------------------------
55
+ -- task_decline_bar — the whitelist R5 names
56
+ --
57
+ -- A table AND a CHECK, deliberately. The table is the readable registry a lead
58
+ -- can query to see which refusals are answerable; the CHECK is the governance —
59
+ -- without it, "which refusals are answerable" would be widenable by anyone who
60
+ -- could write a row, and the whitelist would stop being a ruling. Widening it
61
+ -- takes a migration, which is what a ruling looks like in a substrate.
62
+ -- ---------------------------------------------------------------------------
63
+ CREATE TABLE IF NOT EXISTS cell.task_decline_bar (
64
+ bar text NOT NULL,
65
+ source text NOT NULL,
66
+
67
+ CONSTRAINT task_decline_bar_pkey PRIMARY KEY (bar),
68
+
69
+ CONSTRAINT task_decline_bar_source_is_declared
70
+ CHECK (source IN ('never_defer_rules', 'r5_extension')),
71
+
72
+ CONSTRAINT task_decline_bar_is_a_ruled_bar
73
+ CHECK (bar IN ('operator-decision-needed', 'dependency-unmet',
74
+ 'lead-busy-on-claim', 'not-my-capability',
75
+ 'wrong-principal'))
76
+ );
77
+
78
+ INSERT INTO cell.task_decline_bar (bar, source) VALUES
79
+ ('operator-decision-needed', 'never_defer_rules'),
80
+ ('dependency-unmet', 'never_defer_rules'),
81
+ ('lead-busy-on-claim', 'never_defer_rules'),
82
+ ('not-my-capability', 'r5_extension'),
83
+ ('wrong-principal', 'r5_extension')
84
+ ON CONFLICT (bar) DO UPDATE SET source = EXCLUDED.source;
85
+
86
+ COMMENT ON TABLE cell.task_decline_bar IS
87
+ 'FOUNDER RULING R5: the bar whitelist comes from the never-defer rules, '
88
+ 'extended with not-my-capability and wrong-principal. An invented bar is a '
89
+ 'refusal nobody can act on — "because" is not an answerable reason.';
90
+
91
+ -- ---------------------------------------------------------------------------
92
+ -- task_declined — the typed decline event, and the tombstone itself
93
+ --
94
+ -- One row per task, by the unique constraint: the tombstone IS this row, so
95
+ -- "has this work been declined" is a question with one answer and no state
96
+ -- column anywhere that could disagree with it.
97
+ -- ---------------------------------------------------------------------------
98
+ CREATE TABLE IF NOT EXISTS cell.task_declined (
99
+ decline_id uuid NOT NULL,
100
+ org_id text NOT NULL,
101
+ task_id text NOT NULL,
102
+ declined_by text NOT NULL,
103
+ bar text NOT NULL,
104
+
105
+ -- The bar says WHICH answerable reason; the basis says why it applies here.
106
+ -- NOT NULL: a decline whose basis is absent is a refusal the assigner can
107
+ -- only re-litigate by asking, which is the conversation the typed event
108
+ -- exists to replace.
109
+ basis text NOT NULL,
110
+ declined_at timestamptz NOT NULL,
111
+
112
+ CONSTRAINT task_declined_pkey PRIMARY KEY (decline_id),
113
+
114
+ -- The tombstone. One decline per task, forever.
115
+ CONSTRAINT task_declined_one_per_task UNIQUE (org_id, task_id),
116
+
117
+ CONSTRAINT task_declined_task_fkey
118
+ FOREIGN KEY (org_id, task_id) REFERENCES cell.task (org_id, task_id),
119
+
120
+ CONSTRAINT task_declined_principal_fkey
121
+ FOREIGN KEY (org_id, declined_by)
122
+ REFERENCES cell.principal (org_id, principal_id),
123
+
124
+ CONSTRAINT task_declined_bar_fkey
125
+ FOREIGN KEY (bar) REFERENCES cell.task_decline_bar (bar)
126
+ );
127
+
128
+ -- ---------------------------------------------------------------------------
129
+ -- task_assignment — the durable assignment record
130
+ --
131
+ -- `cell.task.to_principal` is the routing PROJECTION the claim path and the
132
+ -- board read; this is the history. Multi-principal-ready (R5): the same work
133
+ -- may be assigned to different principals over time, and each attempt is a row.
134
+ -- ---------------------------------------------------------------------------
135
+ CREATE TABLE IF NOT EXISTS cell.task_assignment (
136
+ assignment_id uuid NOT NULL,
137
+ org_id text NOT NULL,
138
+ task_id text NOT NULL,
139
+
140
+ -- the assigner — `dispatched_by` in escalation terms, and the seat a lapse
141
+ -- escalates back to
142
+ assigned_by text NOT NULL,
143
+ to_principal text NOT NULL,
144
+
145
+ -- The explicit claim-or-refuse deadline. NOT NULL is the whole contract: an
146
+ -- assignment with no deadline is an offer with no lapse condition, which is
147
+ -- the re-dispatch-forever defect in its other form.
148
+ deadline timestamptz NOT NULL,
149
+ assigned_at timestamptz NOT NULL,
150
+
151
+ CONSTRAINT task_assignment_pkey PRIMARY KEY (assignment_id),
152
+
153
+ CONSTRAINT task_assignment_one_per_task_and_assignee
154
+ UNIQUE (org_id, task_id, to_principal),
155
+
156
+ CONSTRAINT task_assignment_task_fkey
157
+ FOREIGN KEY (org_id, task_id) REFERENCES cell.task (org_id, task_id),
158
+
159
+ CONSTRAINT task_assignment_assigner_fkey
160
+ FOREIGN KEY (org_id, assigned_by)
161
+ REFERENCES cell.principal (org_id, principal_id),
162
+
163
+ CONSTRAINT task_assignment_assignee_fkey
164
+ FOREIGN KEY (org_id, to_principal)
165
+ REFERENCES cell.principal (org_id, principal_id)
166
+ );
167
+
168
+ CREATE INDEX IF NOT EXISTS task_assignment_by_deadline
169
+ ON cell.task_assignment (org_id, deadline);
170
+
171
+ -- ------------------------------------------------- the assignment guard
172
+ --
173
+ -- A trigger, not a clause inside the verb: a rule enforced only by the verb is
174
+ -- a rule bypassed by direct insert (the measured 0073 lesson). It asks the two
175
+ -- questions an assignment has to pass — may this seat put work in this
176
+ -- department, and is this work still offerable at all.
177
+ CREATE OR REPLACE FUNCTION cell.enforce_assignment_is_offerable()
178
+ RETURNS trigger
179
+ LANGUAGE plpgsql
180
+ AS $assignable$
181
+ DECLARE
182
+ v_task_department text;
183
+ v_grant_state text;
184
+ v_declined_by text;
185
+ v_bar text;
186
+ BEGIN
187
+ SELECT t.department INTO v_task_department
188
+ FROM cell.task t
189
+ WHERE t.org_id = NEW.org_id AND t.task_id = NEW.task_id;
190
+
191
+ IF NOT FOUND THEN
192
+ RAISE EXCEPTION
193
+ 'UNKNOWN_TASK:%/% — an assignment routes work, and no work by that '
194
+ 'identity exists in this org.',
195
+ NEW.org_id, NEW.task_id
196
+ USING ERRCODE = 'FC021';
197
+ END IF;
198
+
199
+ -- The tombstone is checked BEFORE authority: a granted seat re-offering
200
+ -- declined work is exactly the measured defect, and reporting it as an
201
+ -- authority problem would send an operator looking for the wrong fix.
202
+ SELECT d.declined_by, d.bar INTO v_declined_by, v_bar
203
+ FROM cell.task_declined d
204
+ WHERE d.org_id = NEW.org_id AND d.task_id = NEW.task_id;
205
+
206
+ IF FOUND THEN
207
+ RAISE EXCEPTION
208
+ 'ASSIGNMENT_TOMBSTONED:%/%:% — % already declined this work with the '
209
+ 'bar ''%''. A decline tombstones the task: there are no re-offers, to '
210
+ 'this principal or any other. Re-dispatching declined work collects '
211
+ 'the same refusal forever and tells nobody; the escalation lane is '
212
+ 'how a decline gets answered.',
213
+ NEW.org_id, NEW.task_id, NEW.to_principal, v_declined_by, v_bar
214
+ USING ERRCODE = 'FC027';
215
+ END IF;
216
+
217
+ SELECT g.state INTO v_grant_state
218
+ FROM cell.delegation_grant g
219
+ WHERE g.org_id = NEW.org_id
220
+ AND g.from_principal = NEW.assigned_by
221
+ AND g.to_department = v_task_department;
222
+
223
+ IF NOT FOUND OR v_grant_state IS DISTINCT FROM 'live' THEN
224
+ RAISE EXCEPTION
225
+ 'DELEGATION_NOT_GRANTED:%:% — principal % holds no live grant to put '
226
+ 'work in ''%'' (grant state: %). Routing work into a department '
227
+ 'decides who answers for it, so it is the same authority question as '
228
+ 'creating work there, checked against the same grants table.',
229
+ NEW.assigned_by, v_task_department, NEW.assigned_by,
230
+ v_task_department, coalesce(v_grant_state, '<no grant>')
231
+ USING ERRCODE = 'FC020';
232
+ END IF;
233
+
234
+ RETURN NEW;
235
+ END;
236
+ $assignable$;
237
+
238
+ CREATE OR REPLACE TRIGGER task_assignment_is_granted_and_untombstoned
239
+ BEFORE INSERT OR UPDATE ON cell.task_assignment
240
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_assignment_is_offerable();
241
+
242
+ -- ------------------------------------------------------------ the assign verb
243
+ CREATE OR REPLACE FUNCTION cell.assign_task(
244
+ p_org_id text,
245
+ p_task_id text,
246
+ p_assignment_id uuid,
247
+ p_assigned_by text,
248
+ p_to_principal text,
249
+ p_deadline timestamptz,
250
+ p_assigned_at timestamptz,
251
+ p_audit_event_id uuid
252
+ )
253
+ RETURNS uuid
254
+ LANGUAGE plpgsql
255
+ AS $assign$
256
+ DECLARE
257
+ v_task_department text;
258
+ BEGIN
259
+ -- Lock the task's row: the routing field and the assignment record must not
260
+ -- be decided by two assigners interleaving.
261
+ SELECT t.department INTO v_task_department
262
+ FROM cell.task t
263
+ WHERE t.org_id = p_org_id AND t.task_id = p_task_id
264
+ FOR UPDATE;
265
+
266
+ IF NOT FOUND THEN
267
+ RAISE EXCEPTION
268
+ 'UNKNOWN_TASK:%/% — there is no work by that identity in this org to '
269
+ 'route.',
270
+ p_org_id, p_task_id
271
+ USING ERRCODE = 'FC021';
272
+ END IF;
273
+
274
+ -- The record first, so the trigger's refusals (tombstone, grant) take the
275
+ -- routing update with them. The check and the write are one transaction.
276
+ INSERT INTO cell.task_assignment (
277
+ assignment_id, org_id, task_id, assigned_by, to_principal, deadline,
278
+ assigned_at
279
+ ) VALUES (
280
+ p_assignment_id, p_org_id, p_task_id, p_assigned_by, p_to_principal,
281
+ p_deadline, p_assigned_at
282
+ );
283
+
284
+ -- to_principal is a real routing field (R5), and 001_core's pairing
285
+ -- constraint means it is never written without its deadline.
286
+ UPDATE cell.task
287
+ SET to_principal = p_to_principal,
288
+ assignment_deadline = p_deadline
289
+ WHERE org_id = p_org_id AND task_id = p_task_id;
290
+
291
+ INSERT INTO cell.audit_event (
292
+ audit_event_id, org_id, department, kind, task_id, actor_principal,
293
+ ts, producer
294
+ ) VALUES (
295
+ p_audit_event_id, p_org_id, v_task_department, 'task_assigned', p_task_id,
296
+ p_assigned_by, p_assigned_at, 'cell.assign_task'
297
+ );
298
+
299
+ RETURN p_assignment_id;
300
+ END;
301
+ $assign$;
302
+
303
+ -- ----------------------------------------------------------- the decline verb
304
+ CREATE OR REPLACE FUNCTION cell.decline_task(
305
+ p_org_id text,
306
+ p_task_id text,
307
+ p_decline_id uuid,
308
+ p_declined_by text,
309
+ p_bar text,
310
+ p_basis text,
311
+ p_declined_at timestamptz,
312
+ p_audit_event_id uuid
313
+ )
314
+ RETURNS uuid
315
+ LANGUAGE plpgsql
316
+ AS $decline$
317
+ DECLARE
318
+ v_task_department text;
319
+ v_assigned boolean;
320
+ v_already text;
321
+ BEGIN
322
+ SELECT t.department INTO v_task_department
323
+ FROM cell.task t
324
+ WHERE t.org_id = p_org_id AND t.task_id = p_task_id
325
+ FOR UPDATE;
326
+
327
+ IF NOT FOUND THEN
328
+ RAISE EXCEPTION
329
+ 'UNKNOWN_TASK:%/% — there is no work by that identity in this org to '
330
+ 'decline.',
331
+ p_org_id, p_task_id
332
+ USING ERRCODE = 'FC021';
333
+ END IF;
334
+
335
+ -- The bar, before anything is written. The FK to the whitelist would refuse
336
+ -- this too, but with an untyped constraint violation; the caller needs to
337
+ -- key on the RULE, and an operator reading the log needs to see which
338
+ -- vocabulary was missed.
339
+ PERFORM 1 FROM cell.task_decline_bar b WHERE b.bar = p_bar;
340
+ IF NOT FOUND THEN
341
+ RAISE EXCEPTION
342
+ 'DECLINE_BAR_NOT_WHITELISTED:% — a decline cites one of the ruled '
343
+ 'bars (see cell.task_decline_bar). An invented bar is a refusal '
344
+ 'nobody can act on, which is how work stops moving without anyone '
345
+ 'being able to say why.',
346
+ coalesce(p_bar, '<null>')
347
+ USING ERRCODE = 'FC028';
348
+ END IF;
349
+
350
+ SELECT EXISTS (
351
+ SELECT 1 FROM cell.task_assignment a
352
+ WHERE a.org_id = p_org_id AND a.task_id = p_task_id
353
+ AND a.to_principal = p_declined_by
354
+ ) INTO v_assigned;
355
+
356
+ IF NOT v_assigned THEN
357
+ RAISE EXCEPTION
358
+ 'DECLINE_BY_NON_ASSIGNEE:%/%:% — this work was never offered to that '
359
+ 'principal. `wrong-principal` is a bar the ASSIGNEE may cite, not a '
360
+ 'licence for a third party to decline on their behalf: a decline by '
361
+ 'anyone else tombstones work its assignee never saw.',
362
+ p_org_id, p_task_id, p_declined_by
363
+ USING ERRCODE = 'FC029';
364
+ END IF;
365
+
366
+ SELECT d.declined_by INTO v_already
367
+ FROM cell.task_declined d
368
+ WHERE d.org_id = p_org_id AND d.task_id = p_task_id;
369
+
370
+ IF FOUND THEN
371
+ RAISE EXCEPTION
372
+ 'TASK_ALREADY_DECLINED:%/%:% — this work already carries a decline '
373
+ '(by %). The tombstone is one row and one answer; a second decline '
374
+ 'would be a second answer to a question that is already settled.',
375
+ p_org_id, p_task_id, p_declined_by, v_already
376
+ USING ERRCODE = 'FC030';
377
+ END IF;
378
+
379
+ INSERT INTO cell.task_declined (
380
+ decline_id, org_id, task_id, declined_by, bar, basis, declined_at
381
+ ) VALUES (
382
+ p_decline_id, p_org_id, p_task_id, p_declined_by, p_bar, p_basis,
383
+ p_declined_at
384
+ );
385
+
386
+ INSERT INTO cell.audit_event (
387
+ audit_event_id, org_id, department, kind, task_id, actor_principal,
388
+ ts, producer
389
+ ) VALUES (
390
+ p_audit_event_id, p_org_id, v_task_department, 'task_declined', p_task_id,
391
+ p_declined_by, p_declined_at, 'cell.decline_task'
392
+ );
393
+
394
+ RETURN p_decline_id;
395
+ END;
396
+ $decline$;
397
+
398
+ COMMENT ON FUNCTION cell.decline_task IS
399
+ 'FOUNDER RULING R5. The second branch of claim-or-refuse. Writes the typed '
400
+ 'decline event, which IS the tombstone: no re-offer of this work is possible '
401
+ 'afterwards, to this principal or any other.';
402
+
403
+ -- ------------------------------------------------------- the lapse condition
404
+ --
405
+ -- "Deadline lapse with NEITHER claim NOR decline" — both branches of the
406
+ -- claim-or-refuse contract close the condition, and both are stated here, so a
407
+ -- query that only knew about claims could not answer this question.
408
+ --
409
+ -- The instant is an argument, never now(): a condition that reads the wall clock
410
+ -- cannot be asserted by a falsifier.
411
+ CREATE OR REPLACE FUNCTION cell.assignments_lapsed(
412
+ p_org_id text,
413
+ p_as_of timestamptz
414
+ )
415
+ RETURNS TABLE (
416
+ lapsed_task_id text,
417
+ lapsed_assignee text,
418
+ lapsed_assigner text,
419
+ lapsed_deadline timestamptz
420
+ )
421
+ LANGUAGE sql
422
+ STABLE
423
+ AS $lapsed$
424
+ SELECT a.task_id, a.to_principal, a.assigned_by, a.deadline
425
+ FROM cell.task_assignment a
426
+ WHERE a.org_id = p_org_id
427
+ AND a.deadline < p_as_of
428
+ AND NOT EXISTS (
429
+ SELECT 1 FROM cell.work_claim w
430
+ WHERE w.org_id = a.org_id AND w.task_id = a.task_id
431
+ AND w.principal_id = a.to_principal
432
+ -- C4 (R-A35-24 §2, 2026-08-25): a LIVE claim, not any claim ever.
433
+ -- A claim row is durable (L12), so without this clause an assignee
434
+ -- who claimed and then handed the work back suppressed the lapse
435
+ -- condition FOREVER: the assigner was told nothing, and
436
+ -- cell.escalate_lapsed_assignment — which re-checks through this
437
+ -- same function — raised FC031 ASSIGNMENT_NOT_LAPSED instead of a
438
+ -- page. Written as the NAMED state and not
439
+ -- `w.ownership_ended_at IS NULL` (its biconditional twin,
440
+ -- 001_core.sql:183-184) so a seventh claim state is a visible edit
441
+ -- here rather than a silent reclassification — the argument
442
+ -- delegation.mjs:154-158 makes for CHILD_CLAIM_TERMINALS.
443
+ AND w.state = 'active')
444
+ AND NOT EXISTS (
445
+ SELECT 1 FROM cell.task_declined d
446
+ WHERE d.org_id = a.org_id AND d.task_id = a.task_id)
447
+ $lapsed$;
448
+
449
+ COMMENT ON FUNCTION cell.assignments_lapsed IS
450
+ 'FOUNDER RULING R5: deadline lapse with neither claim nor decline, as a '
451
+ 'queryable condition evaluated at an explicit instant.';
452
+
453
+ -- ------------------------------------------- the lapse escalation
454
+ --
455
+ -- A separate, explicit verb that RE-CHECKS the condition. Trusting a caller's
456
+ -- claim that something lapsed would mint pages for conditions that no longer
457
+ -- hold — and a page nobody can act on is how an escalation lane goes unread.
458
+ CREATE OR REPLACE FUNCTION cell.escalate_lapsed_assignment(
459
+ p_org_id text,
460
+ p_task_id text,
461
+ p_escalation_id uuid,
462
+ p_as_of timestamptz,
463
+ p_created_at timestamptz,
464
+ p_audit_event_id uuid
465
+ )
466
+ RETURNS text
467
+ LANGUAGE plpgsql
468
+ AS $lapse_escalation$
469
+ DECLARE
470
+ v_assignee text;
471
+ v_assigner text;
472
+ v_dispatched_by text;
473
+ BEGIN
474
+ SELECT l.lapsed_assignee, l.lapsed_assigner INTO v_assignee, v_assigner
475
+ FROM cell.assignments_lapsed(p_org_id, p_as_of) l
476
+ WHERE l.lapsed_task_id = p_task_id;
477
+
478
+ IF NOT FOUND THEN
479
+ RAISE EXCEPTION
480
+ 'ASSIGNMENT_NOT_LAPSED:%/%:% — at that instant this work carried no '
481
+ 'assignment that had lapsed: it was claimed, declined, or its '
482
+ 'deadline had not passed. An escalation minted from a condition that '
483
+ 'does not hold is a page nobody can act on.',
484
+ p_org_id, p_task_id, p_as_of
485
+ USING ERRCODE = 'FC031';
486
+ END IF;
487
+
488
+ -- The escalation goes to the ASSIGNER (R5) — dispatched_by, not a walk up
489
+ -- the assignee's organizational parents, which for a cross-department
490
+ -- assignment is a different seat entirely. The one exception is a seat that
491
+ -- assigned to itself: there is no dispatcher above it to answer, so the
492
+ -- chain resolves through its organizational parent instead. Stated here
493
+ -- rather than left to the chain, because cell.escalation refuses to address
494
+ -- an escalation to its own author.
495
+ v_dispatched_by := CASE WHEN v_assigner = v_assignee THEN NULL ELSE v_assigner END;
496
+
497
+ RETURN cell.escalate(
498
+ p_org_id, p_escalation_id, v_assignee, v_dispatched_by,
499
+ 'assignment_deadline_lapsed', p_task_id, NULL, p_created_at,
500
+ p_audit_event_id);
501
+ END;
502
+ $lapse_escalation$;