@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,836 @@
1
+ -- 027_ownership_return.sql — R-A35-23's OWNERSHIP RETURN: the second interval a
2
+ -- handed-back obligation gets when the work it was waiting on comes back, the
3
+ -- succession lineage that records which interval it resumes, and the call-stack
4
+ -- guard that makes this verb the only way to either.
5
+ --
6
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.2 (a claim is an interval of
7
+ -- RESPONSIBILITY; responsibility passes by a terminal, never by a second
8
+ -- claimant arriving), §1.6 (settlement is a separate fact), §4 (the dependency
9
+ -- lane whose release is the returned fact), PRD 3.0 §5.2 (the ownership
10
+ -- constitution) and design laws L1 (one authoritative owner per lifecycle), L2
11
+ -- (durable state below the agent), L11 (learning never widens authority) and L12
12
+ -- (durable judgment does not decay). FOUNDER RULINGS R-A35-25 §5 (FC-A: the
13
+ -- evaluator seat mints the deterministic SAME-PRINCIPAL successor through this
14
+ -- verb; FC-B: No, a return re-seats the predecessor) and R-A35-26/27.
15
+ -- Design of record: the ownership-return design of record §5.1-§5.6.
16
+ --
17
+ -- Requires 001_core.sql (the claim table and its unique active ownership),
18
+ -- 002_claim.sql (cell.claim_work and cell.written_by, the frame-stack signal
19
+ -- every guard here reads), 006_dependency.sql (cell.v_task_open_deps and
20
+ -- cell.dep_edge, whose release is the fact this lane turns on),
21
+ -- 012_evaluator_lane.sql (cell.settle_task_edge, the release verb this file's
22
+ -- stimulus trigger fires inside) and 017_claim_yield_causes.sql (the handback
23
+ -- cause and the basis prefix G2 discriminates on).
24
+ --
25
+ -- Re-apply: idempotent (ADD COLUMN IF NOT EXISTS, DROP CONSTRAINT IF EXISTS
26
+ -- before ADD, CREATE OR REPLACE / IF NOT EXISTS throughout, and a
27
+ -- DROP FUNCTION IF EXISTS that is a no-op on a fresh install).
28
+ -- Revert: DROP TRIGGER work_claim_return_resumption_is_a_declared_cause
29
+ -- ON cell.work_claim;
30
+ -- DROP TRIGGER dep_edge_release_records_a_return_stimulus ON cell.dep_edge;
31
+ -- DROP TRIGGER return_stimulus_is_written_by_the_release ON cell.return_stimulus;
32
+ -- DROP FUNCTION cell.reclaim_returned_work(text,text,uuid,timestamptz,uuid);
33
+ -- DROP FUNCTION cell.enforce_returned_resumption_is_a_declared_cause();
34
+ -- DROP FUNCTION cell.record_return_stimulus();
35
+ -- DROP FUNCTION cell.enforce_return_stimulus_is_the_releases();
36
+ -- DROP VIEW cell.v_returned_work;
37
+ -- DROP TABLE cell.return_stimulus;
38
+ -- DROP INDEX cell.work_claim_one_successor_per_predecessor;
39
+ -- ALTER TABLE cell.work_claim
40
+ -- DROP CONSTRAINT work_claim_predecessor_is_not_itself,
41
+ -- DROP CONSTRAINT work_claim_predecessor_fkey,
42
+ -- DROP COLUMN predecessor_work_claim_id;
43
+ -- (a reverted cell has the hole back: any writer may claim a second
44
+ -- interval on a yielded task and nothing refuses it. That is the state
45
+ -- this file exists to end, not a safe resting place. Reverting also
46
+ -- requires restoring 002's eleven-argument cell.claim_work.)
47
+ --
48
+ -- WHY THIS FILE EXISTS, MEASURED
49
+ --
50
+ -- 017 gave a parent with delegated work outstanding a lawful way to hand
51
+ -- responsibility back. Nothing gave the obligation a way to come back. When the
52
+ -- delegated work returns — the child completes, a non-author verifies it, and
53
+ -- cell.settle_task_edge releases the edge — the work sits at a terminal with
54
+ -- nobody answerable for it, and the only road to a second interval is a bare
55
+ -- cell.claim_work from whatever process happened to notice. MEASURED at
56
+ -- f45e9e64: `cell.work_claim` carried four triggers and NONE of them looked at a
57
+ -- prior yield, so that road was open to every writer in the cell, including one
58
+ -- that had no business naming who answers for work next.
59
+ --
60
+ -- 016 is the sibling and the shape: R8's reopen-after-rejection has a verb, a
61
+ -- call-stack guard and a transcription discipline. This is the OTHER resumption
62
+ -- cause, and it gets its own guard rather than a shared registry — see below.
63
+ --
64
+ -- WHY TWO INDEPENDENT GUARDS AND NOT A RESUMPTION-CAUSE REGISTRY (L8, dated)
65
+ --
66
+ -- 017's cause registry exists because ONE meaning (`work_claim.yielded`) had
67
+ -- several causes each wanting to write the row. That is not this situation: the
68
+ -- meaning here is `work_claim.claimed`, whose producer is already singular and
69
+ -- registered (`cell.claim_work`, 002:65-67), and this verb PERFORMs through it.
70
+ -- So there is no new topic, no ALTER of either topic CHECK, and no new
71
+ -- producer-registry row. What there are two of is RESUMPTION CAUSES — 016's
72
+ -- reopen and this one's return — and two is not a registry. Moving 016's FC130
73
+ -- into a shared reader would break the vocabulary test that requires a file to
74
+ -- raise what it declares, for no gain.
75
+ -- RETIREMENT CONDITION: the first verb that writes `aborted`, `taken_over` or
76
+ -- `expired` onto a claim — a THIRD resumption cause is what makes a registry
77
+ -- cheaper than three guards. DELETION OWNER: whoever lands that verb, in the
78
+ -- same change. That is the same trigger `delegation.mjs:170-175` already names
79
+ -- as its own retirement condition, and it is W4's FC-E grid.
80
+ --
81
+ -- WHY THE ELIGIBILITY SURFACE IS A VIEW
82
+ --
83
+ -- Every fact it needs is already durable and already owned by a verb: the yield
84
+ -- terminal by cell.record_claim_yield, the dependency release by
85
+ -- cell.settle_task_edge. A table would need a writer, and a writer of
86
+ -- eligibility is a SECOND AUTHORITY over the work lifecycle (L1) — which is
87
+ -- exactly what 005_escalation.sql refuses when it says "nothing keys a row to an
88
+ -- escalation and no trigger turns one into a consequence". The view answers
89
+ -- "what is returnable" and never "who may resume it".
90
+ --
91
+ -- WHY THE STIMULUS IS NOT THAT WRITER
92
+ --
93
+ -- `cell.return_stimulus` is EVIDENCE, not eligibility. It records that a release
94
+ -- happened, what verdict it rested on, when, and which interval later consumed
95
+ -- it — so the fact survives every process that might have noticed it (L2) and so
96
+ -- "who resumed on this release" has exactly one answer. NOTHING READS IT AS A
97
+ -- PRECONDITION: the verb's guards read the view and the claim rows, so a missing
98
+ -- stimulus cannot make a lawful return unlawful and a present one cannot make an
99
+ -- unlawful return lawful. That is the difference between a durable record and a
100
+ -- second authority, and it is why this table may exist where an eligibility
101
+ -- table may not.
102
+ --
103
+ -- WHAT THE STIMULUS DOES NOT COVER, STATED RATHER THAN LEFT TO BE DISCOVERED
104
+ --
105
+ -- It is recorded only where a `verified` verification stands behind the release,
106
+ -- which is the task and acceptance-package lanes. The other five dependency
107
+ -- families release on an observation this substrate does not hold (an operator's
108
+ -- consume, a clock, free capacity, an external event) and there is no verdict to
109
+ -- name, so no stimulus is written — the obligation is still RETURNABLE and
110
+ -- `cell.v_returned_work` still says so, because the view reads the release and
111
+ -- not the evidence of it. OWNER: W4's residual-owner map, beside FC-D. PHASE: W4.
112
+ --
113
+ -- Declared failure vocabulary (FC180-FC189 reserved; 8 codes used):
114
+ -- FC180 RETURNED_WORK_RESUMED_OUTSIDE_A_DECLARED_CAUSE a second interval on a
115
+ -- task whose last holder handed the work
116
+ -- back, claimed by something that is not
117
+ -- this file's verb
118
+ -- FC181 RETURN_RECLAIM_TASK_UNKNOWN a return of work the company has no
119
+ -- record of
120
+ -- FC182 RETURN_RECLAIM_NO_YIELDED_PREDECESSOR a return of work whose last
121
+ -- interval did not end by handback
122
+ -- FC183 RETURN_RECLAIM_YIELD_WAS_NOT_A_HANDBACK a return of a yield whose
123
+ -- cause was an expired ask — an
124
+ -- escalation, not work coming back
125
+ -- FC184 RETURN_RECLAIM_DEPENDENCY_STILL_OPEN a return of work whose declared
126
+ -- dependencies have not been released
127
+ -- FC185 RETURN_RECLAIM_RESUMES_BEFORE_THE_INTERVAL_IT_FOLLOWS a resumption
128
+ -- stamped inside the previous holder's
129
+ -- own window
130
+ -- FC186 RETURN_RECLAIM_PREDECESSOR_ALREADY_RESUMED a SECOND interval, under a
131
+ -- different id, claiming to be the
132
+ -- return of one yield
133
+ -- FC188 RETURN_STIMULUS_RECORDED_OUTSIDE_THE_RELEASE a return stimulus written
134
+ -- by something that is not a dependency
135
+ -- release
136
+ -- FC189 RETURN_STIMULUS_CONSUMED_OUTSIDE_THE_RETURN_VERB a stimulus marked
137
+ -- consumed by something that is not this
138
+ -- file's verb, or consumed twice
139
+ --
140
+ -- FC187 (CLAIM_SETTLEMENT_SUPERSEDED_BY_A_SUCCESSOR) was reserved in this block
141
+ -- while FOUNDER CHECKPOINT FC-C was open and is RELEASED: FC-C is ruled S2
142
+ -- (R-A35-25 §5), so the settlement guard amends 014_claim_settlement.sql in place
143
+ -- and takes FC115 from that file's own block rather than splitting one verb's
144
+ -- definition across two migrations. FC187 is unallocated; it is named here so a
145
+ -- reader who greps the design's earlier table finds the disposition.
146
+
147
+ -- =========================================================================
148
+ -- 1. SUCCESSION PROVENANCE — a column of its own, and not the parent link
149
+ -- =========================================================================
150
+ --
151
+ -- MEASURED consequence of overloading `parent_work_claim_id` instead: the
152
+ -- successor would become an ACTIVE DELEGATED CHILD of the predecessor, and
153
+ -- cell.yield_claim_with_delegated_work_outstanding derives its recorded fact
154
+ -- from exactly that (`child.parent_work_claim_id = p_work_claim_id AND
155
+ -- child.state = 'active'`, 017:310-315). FC144's "nothing outstanding" refusal
156
+ -- would stop firing and a claim could yield on the strength of its own
157
+ -- successor. That is a reachable data corruption with a named verb and a named
158
+ -- refusal that stops working, not a style objection.
159
+ --
160
+ -- 001_core.sql's CREATE TABLE carries the same column and constraints inline, so
161
+ -- a fresh install and an ALTERed one agree. The ALTERs below are written
162
+ -- idempotently because 001 uses CREATE TABLE IF NOT EXISTS and therefore does
163
+ -- not reach a database that already exists.
164
+
165
+ ALTER TABLE cell.work_claim
166
+ ADD COLUMN IF NOT EXISTS predecessor_work_claim_id uuid;
167
+
168
+ COMMENT ON COLUMN cell.work_claim.predecessor_work_claim_id IS
169
+ 'R-A35-23: the interval this one RESUMES. SUCCESSION lineage — same work, '
170
+ 'same principal, a later interval — and deliberately NOT '
171
+ 'parent_work_claim_id, which is DELEGATION lineage (§2.1: a child claim '
172
+ 'owned by a DIFFERENT principal). Overloading the parent column would make '
173
+ 'a successor look like an outstanding delegated child to '
174
+ 'cell.yield_claim_with_delegated_work_outstanding (017), letting a claim '
175
+ 'yield on the strength of its own successor. NULL for every interval that '
176
+ 'resumed nothing, which is the majority case.';
177
+
178
+ ALTER TABLE cell.work_claim
179
+ DROP CONSTRAINT IF EXISTS work_claim_predecessor_fkey;
180
+ ALTER TABLE cell.work_claim
181
+ ADD CONSTRAINT work_claim_predecessor_fkey
182
+ FOREIGN KEY (org_id, predecessor_work_claim_id)
183
+ REFERENCES cell.work_claim (org_id, work_claim_id);
184
+
185
+ ALTER TABLE cell.work_claim
186
+ DROP CONSTRAINT IF EXISTS work_claim_predecessor_is_not_itself;
187
+ ALTER TABLE cell.work_claim
188
+ ADD CONSTRAINT work_claim_predecessor_is_not_itself
189
+ CHECK (predecessor_work_claim_id IS DISTINCT FROM work_claim_id);
190
+
191
+ -- EXACTLY ONE successor per interval, unstorable to violate, for EVERY writer —
192
+ -- the same argument 001_core.sql:187-193 makes for unique active ownership. It
193
+ -- is the layer that holds when the predicate cannot: the unique-ACTIVE index is
194
+ -- partial and stops refusing the moment a successor reaches its own terminal,
195
+ -- which is precisely the quiet window between an assertion and its settlement.
196
+ CREATE UNIQUE INDEX IF NOT EXISTS work_claim_one_successor_per_predecessor
197
+ ON cell.work_claim (org_id, predecessor_work_claim_id)
198
+ WHERE predecessor_work_claim_id IS NOT NULL;
199
+
200
+ -- ---------------------------------------------------- the OLD claim signature
201
+ --
202
+ -- 002 now declares cell.claim_work with twelve arguments. CREATE OR REPLACE
203
+ -- FUNCTION with a different argument list installs a SECOND function rather than
204
+ -- replacing the first, so the eleven-argument form is dropped here — leaving
205
+ -- both would be two claim paths, which is the one thing 002 exists not to have,
206
+ -- and a caller that kept the old shape would write successor claims with no
207
+ -- provenance at all. L5: the obsolete mechanism goes in the same change.
208
+ --
209
+ -- A no-op on a fresh install, where 002 created only the twelve-argument form.
210
+ DROP FUNCTION IF EXISTS cell.claim_work(
211
+ text, text, uuid, uuid, text, text, text, timestamptz, text, timestamptz, uuid);
212
+
213
+ -- =========================================================================
214
+ -- 1-bis. ONE SPELLING OF "IS THIS A HANDBACK"
215
+ -- =========================================================================
216
+ --
217
+ -- Three places in this file ask it — the view's WHERE, the writer guard's
218
+ -- narrowing, and the verb's G2 — and three spellings of one predicate is how a
219
+ -- fourth cause comes to be recognised by two of them and not the third. It is a
220
+ -- FUNCTION and not a repeated expression for the same reason `cell.written_by`
221
+ -- is: two answers to a question the whole guard family rests on is two answers.
222
+ --
223
+ -- It resolves through the REGISTRY (`cell.claim_yield_cause`, 017:122-125)
224
+ -- rather than matching the prefix as a literal, so a third cause reaches every
225
+ -- one of the three readers by landing its row.
226
+ CREATE OR REPLACE FUNCTION cell.terminal_basis_is_a_handback(p_basis text)
227
+ RETURNS boolean
228
+ LANGUAGE sql
229
+ STABLE
230
+ AS $is_handback$
231
+ SELECT EXISTS (
232
+ SELECT 1 FROM cell.claim_yield_cause c
233
+ WHERE c.basis_prefix = split_part(p_basis, ':', 1)
234
+ AND c.cause = 'cell.yield_claim_with_delegated_work_outstanding')
235
+ $is_handback$;
236
+
237
+ COMMENT ON FUNCTION cell.terminal_basis_is_a_handback IS
238
+ 'Does this terminal_basis name the delegated-work-outstanding yield cause — '
239
+ 'the ONE yield that has a resumption verb. Read from cell.claim_yield_cause '
240
+ 'rather than matched as a literal, and stated once because the view, the '
241
+ 'writer guard and the verb''s G2 all ask it.';
242
+
243
+ -- =========================================================================
244
+ -- 2. THE ELIGIBILITY SURFACE — a view, holding no authority
245
+ -- =========================================================================
246
+
247
+ CREATE OR REPLACE VIEW cell.v_returned_work AS
248
+ SELECT pred.org_id,
249
+ pred.task_id,
250
+ pred.work_claim_id AS predecessor_work_claim_id,
251
+ pred.principal_id AS resuming_principal,
252
+ pred.department,
253
+ pred.terminal_basis,
254
+ pred.ownership_ended_at AS returned_after,
255
+ d.total_deps, d.satisfied_deps
256
+ FROM cell.work_claim pred
257
+ JOIN cell.v_task_open_deps d
258
+ ON d.org_id = pred.org_id AND d.task_id = pred.task_id
259
+ WHERE pred.state = 'yielded'
260
+ AND cell.terminal_basis_is_a_handback(pred.terminal_basis)
261
+ -- `total_deps > 0` is load-bearing: all_deps_satisfied is
262
+ -- (open_deps = 0 AND retracted_deps = 0), which is trivially TRUE for a task
263
+ -- carrying no edges at all — and a yield that never waited on anything is not
264
+ -- work that came back.
265
+ AND d.total_deps > 0
266
+ AND d.all_deps_satisfied
267
+ AND NOT EXISTS (SELECT 1 FROM cell.work_claim live
268
+ WHERE live.org_id = pred.org_id AND live.task_id = pred.task_id
269
+ AND live.state = 'active')
270
+ AND NOT EXISTS (SELECT 1 FROM cell.work_claim succ
271
+ WHERE succ.org_id = pred.org_id
272
+ AND succ.predecessor_work_claim_id = pred.work_claim_id);
273
+
274
+ COMMENT ON VIEW cell.v_returned_work IS
275
+ 'What is RETURNABLE: an obligation handed back with delegated work '
276
+ 'outstanding, whose declared dependencies have since been released, with no '
277
+ 'live owner and no successor yet. A view and not a table because every fact '
278
+ 'in it is already owned by a verb, and a writer of eligibility would be a '
279
+ 'second authority over the work lifecycle (L1). It never answers who may '
280
+ 'resume — that is cell.reclaim_returned_work''s, under FC-A.';
281
+
282
+ -- =========================================================================
283
+ -- 3. THE DURABLE, CONSUMED-ONCE RETURN STIMULUS
284
+ -- =========================================================================
285
+
286
+ CREATE TABLE IF NOT EXISTS cell.return_stimulus (
287
+ org_id text NOT NULL,
288
+ task_id text NOT NULL,
289
+
290
+ -- The interval that handed the work back. It is also the IDENTITY of this
291
+ -- row: a stable idempotency key that needs no derivation, because at most
292
+ -- one interval is ever the predecessor of at most one successor
293
+ -- (work_claim_one_successor_per_predecessor, above).
294
+ predecessor_work_claim_id uuid NOT NULL,
295
+
296
+ -- What the release rested on. NOT NULL: a stimulus that could not name the
297
+ -- verdict behind it would be a release nobody can audit, which is the exact
298
+ -- shape of the Mac's void events (zero consumers, because they could not
299
+ -- answer "who released this, on what basis, when").
300
+ release_verification_id uuid NOT NULL,
301
+ released_at timestamptz NOT NULL,
302
+
303
+ -- WHICH POLICY ROUTED THE RETURN. Today the cell has exactly one and it is
304
+ -- a ruling rather than a row: FC-B is ruled No, so a return re-seats the
305
+ -- predecessor's principal and cross-principal movement is a different verb.
306
+ -- L8 (dated bridge): when PRD §8.4's policy-driven stimulus routing lands,
307
+ -- this column names the policy ROW instead of the ruling. RETIREMENT
308
+ -- CONDITION: a policy registry exists that this column can reference.
309
+ -- DELETION OWNER: the A4 admission/gateway lane (the post-extraction
310
+ -- supervisor task, R-A35-25 §4). PHASE: A4.
311
+ routing_policy_ref text NOT NULL,
312
+
313
+ recorded_at timestamptz NOT NULL,
314
+
315
+ -- CONSUMED-ONCE. NULL exactly while no interval has resumed on this release;
316
+ -- the pair is written together by cell.reclaim_returned_work and by nothing
317
+ -- else, and the guard below refuses a second transition.
318
+ consumed_at timestamptz,
319
+ consumed_by_work_claim_id uuid,
320
+
321
+ CONSTRAINT return_stimulus_pkey PRIMARY KEY (org_id, predecessor_work_claim_id),
322
+
323
+ CONSTRAINT return_stimulus_predecessor_fkey
324
+ FOREIGN KEY (org_id, predecessor_work_claim_id)
325
+ REFERENCES cell.work_claim (org_id, work_claim_id),
326
+
327
+ CONSTRAINT return_stimulus_task_fkey
328
+ FOREIGN KEY (org_id, task_id) REFERENCES cell.task (org_id, task_id),
329
+
330
+ CONSTRAINT return_stimulus_verification_fkey
331
+ FOREIGN KEY (release_verification_id)
332
+ REFERENCES cell.verification (verification_id),
333
+
334
+ CONSTRAINT return_stimulus_consumer_fkey
335
+ FOREIGN KEY (org_id, consumed_by_work_claim_id)
336
+ REFERENCES cell.work_claim (org_id, work_claim_id),
337
+
338
+ -- A consumption is an instant AND a consumer, or it is not a consumption.
339
+ -- One without the other is a row that says a wake happened and cannot say
340
+ -- whose, which is the hole L13's provenance requirement exists to close.
341
+ CONSTRAINT return_stimulus_consumption_names_its_consumer
342
+ CHECK ((consumed_at IS NULL) = (consumed_by_work_claim_id IS NULL)),
343
+
344
+ CONSTRAINT return_stimulus_consumption_follows_the_release
345
+ CHECK (consumed_at IS NULL OR consumed_at >= released_at),
346
+
347
+ CONSTRAINT return_stimulus_routing_policy_is_named
348
+ CHECK (length(btrim(routing_policy_ref)) > 0)
349
+ );
350
+
351
+ COMMENT ON TABLE cell.return_stimulus IS
352
+ 'R-A35-24 §3: an accepted release of delegated work records ONE durable, '
353
+ 'consumed-once return stimulus. EVIDENCE, never eligibility — nothing reads '
354
+ 'it as a precondition, so a lost stimulus cannot make a lawful return '
355
+ 'unlawful. It is what makes "the release happened and exactly one interval '
356
+ 'resumed on it" answerable after every process that saw it has died (L2).';
357
+
358
+ -- The stimulus is written by the RELEASE, and consumed by the RETURN. Both are
359
+ -- the PL/pgSQL call stack rather than a column a writer could type: a
360
+ -- `recorded_by` column alone is authorised by typing the right string into it,
361
+ -- and MEASURED, 23 historical `verified` settles reached the Mac's bus by direct
362
+ -- publish naming exactly the right evaluator.
363
+ CREATE OR REPLACE FUNCTION cell.enforce_return_stimulus_is_the_releases()
364
+ RETURNS trigger
365
+ LANGUAGE plpgsql
366
+ AS $stimulus_writer$
367
+ BEGIN
368
+ IF TG_OP = 'INSERT' THEN
369
+ IF NOT cell.written_by('cell.record_return_stimulus') THEN
370
+ RAISE EXCEPTION
371
+ 'RETURN_STIMULUS_RECORDED_OUTSIDE_THE_RELEASE:%/% — a return '
372
+ 'stimulus records what a DEPENDENCY RELEASE did, and it is '
373
+ 'written by the trigger that observes the release and by nothing '
374
+ 'else. A writer that could type one would be declaring that work '
375
+ 'came back when no verdict released it — the eligibility view '
376
+ 'would still refuse the return, and the evidence record would '
377
+ 'still be a lie.',
378
+ NEW.org_id, NEW.task_id
379
+ USING ERRCODE = 'FC188';
380
+ END IF;
381
+ RETURN NEW;
382
+ END IF;
383
+
384
+ -- UPDATE. Only the transition INTO a consumed stimulus is guarded, and only
385
+ -- once: a row that is already consumed carries a settled fact, and a second
386
+ -- transition would give "who resumed on this release" two answers.
387
+ IF NEW.consumed_at IS NOT DISTINCT FROM OLD.consumed_at
388
+ AND NEW.consumed_by_work_claim_id IS NOT DISTINCT FROM OLD.consumed_by_work_claim_id THEN
389
+ RETURN NEW;
390
+ END IF;
391
+
392
+ IF OLD.consumed_at IS NOT NULL
393
+ OR NOT cell.written_by('cell.reclaim_returned_work') THEN
394
+ RAISE EXCEPTION
395
+ 'RETURN_STIMULUS_CONSUMED_OUTSIDE_THE_RETURN_VERB:%/%:% — a stimulus '
396
+ 'is consumed exactly once, by cell.reclaim_returned_work, in the same '
397
+ 'transaction as the interval that resumed on it. This row was '
398
+ 'consumed at % already, or by something that is not that verb.',
399
+ NEW.org_id, NEW.task_id, NEW.predecessor_work_claim_id,
400
+ coalesce(OLD.consumed_at::text, '<not yet>')
401
+ USING ERRCODE = 'FC189';
402
+ END IF;
403
+
404
+ RETURN NEW;
405
+ END;
406
+ $stimulus_writer$;
407
+
408
+ CREATE OR REPLACE TRIGGER return_stimulus_is_written_by_the_release
409
+ BEFORE INSERT OR UPDATE ON cell.return_stimulus
410
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_return_stimulus_is_the_releases();
411
+
412
+ -- The observer. AFTER UPDATE on cell.dep_edge, narrowed to the transition INTO
413
+ -- `satisfied`, and firing only when that release was the LAST one this
414
+ -- obligation was waiting on — a stimulus per edge would say "something moved",
415
+ -- and what a wake needs to know is "this obligation is returnable now".
416
+ --
417
+ -- The instant is the EDGE's own `settled_at`, never now(): the release said when
418
+ -- it happened and this row transcribes it (011:263-287 / 015:162-171).
419
+ CREATE OR REPLACE FUNCTION cell.record_return_stimulus()
420
+ RETURNS trigger
421
+ LANGUAGE plpgsql
422
+ AS $record_stimulus$
423
+ DECLARE
424
+ v_pred cell.work_claim%ROWTYPE;
425
+ v_verification uuid;
426
+ BEGIN
427
+ IF NEW.state IS DISTINCT FROM 'satisfied' OR OLD.state = 'satisfied' THEN
428
+ RETURN NULL;
429
+ END IF;
430
+
431
+ -- The interval that handed this work back, if there is one. The predicate is
432
+ -- the view's own, spelled here rather than SELECTed from it: a trigger that
433
+ -- read the view would fire on a row the view has already excluded for a
434
+ -- reason (an active claim, an existing successor) that has nothing to do
435
+ -- with whether a release happened.
436
+ SELECT * INTO v_pred
437
+ FROM cell.work_claim pred
438
+ WHERE pred.org_id = NEW.org_id AND pred.task_id = NEW.task_id
439
+ AND pred.state = 'yielded'
440
+ AND cell.terminal_basis_is_a_handback(pred.terminal_basis)
441
+ ORDER BY pred.ownership_ended_at DESC, pred.claimed_at DESC, pred.work_claim_id
442
+ LIMIT 1;
443
+
444
+ IF NOT FOUND THEN
445
+ RETURN NULL; -- an ordinary release; nothing came back
446
+ END IF;
447
+
448
+ -- Not the last edge: the obligation is still blocked, and a stimulus here
449
+ -- would name a return that has not happened.
450
+ IF NOT EXISTS (SELECT 1 FROM cell.v_task_open_deps d
451
+ WHERE d.org_id = NEW.org_id AND d.task_id = NEW.task_id
452
+ AND d.total_deps > 0 AND d.all_deps_satisfied) THEN
453
+ RETURN NULL;
454
+ END IF;
455
+
456
+ -- Already recorded. The predicate is the readable path and the primary key
457
+ -- is the guarantee (002's own words): two concurrent releases racing here
458
+ -- both pass this check and one meets return_stimulus_pkey, which is a loud
459
+ -- rollback rather than two answers.
460
+ IF EXISTS (SELECT 1 FROM cell.return_stimulus s
461
+ WHERE s.org_id = NEW.org_id
462
+ AND s.predecessor_work_claim_id = v_pred.work_claim_id) THEN
463
+ RETURN NULL;
464
+ END IF;
465
+
466
+ -- The verdict the release rested on, read over the GATING task and at or
467
+ -- before the release instant. Absent for the five families that release on
468
+ -- an observation this substrate does not hold, and the row is then not
469
+ -- written at all rather than written with a hole (see the header).
470
+ SELECT v.verification_id INTO v_verification
471
+ FROM cell.verification v
472
+ WHERE v.org_id = NEW.org_id
473
+ AND v.task_id = NEW.gating_task_id
474
+ AND v.result = 'verified'
475
+ AND v.recorded_at <= NEW.settled_at
476
+ ORDER BY v.recorded_at, v.verification_id
477
+ LIMIT 1;
478
+
479
+ IF v_verification IS NULL THEN
480
+ RETURN NULL;
481
+ END IF;
482
+
483
+ INSERT INTO cell.return_stimulus (
484
+ org_id, task_id, predecessor_work_claim_id, release_verification_id,
485
+ released_at, routing_policy_ref, recorded_at, consumed_at,
486
+ consumed_by_work_claim_id)
487
+ VALUES (
488
+ NEW.org_id, NEW.task_id, v_pred.work_claim_id, v_verification,
489
+ NEW.settled_at,
490
+ -- FOUNDER RULING R-A35-25 §5 (FC-B, "No"): the return re-seats the
491
+ -- predecessor's principal, and that is the whole of the routing policy
492
+ -- the cell has. Named as the RULING because there is no policy row to
493
+ -- point at; the L8 terms for replacing it are on the column.
494
+ 'R-A35-25:same_principal_return',
495
+ NEW.settled_at, NULL, NULL);
496
+
497
+ RETURN NULL;
498
+ END;
499
+ $record_stimulus$;
500
+
501
+ CREATE OR REPLACE TRIGGER dep_edge_release_records_a_return_stimulus
502
+ AFTER UPDATE ON cell.dep_edge
503
+ FOR EACH ROW EXECUTE FUNCTION cell.record_return_stimulus();
504
+
505
+ -- =========================================================================
506
+ -- 4. THE WRITER GUARD — what closes today's hole
507
+ -- =========================================================================
508
+ --
509
+ -- BEFORE INSERT, because a resumption is a NEW claim row and never an update of
510
+ -- the interval it follows (§1.2: the ended interval stays in the record exactly
511
+ -- as it ended, L12). Narrowed to the one intersection that matters: a SECOND
512
+ -- interval on a task whose holder handed the work back. A first claim on work
513
+ -- nobody yielded is an ordinary claim and stays one.
514
+ CREATE OR REPLACE FUNCTION cell.enforce_returned_resumption_is_a_declared_cause()
515
+ RETURNS trigger
516
+ LANGUAGE plpgsql
517
+ AS $returned_writer$
518
+ DECLARE
519
+ v_last cell.work_claim%ROWTYPE;
520
+ BEGIN
521
+ -- The interval this row would follow: the LAST one to END on this task. Not
522
+ -- "any yielded interval anywhere in the history" — a task whose second
523
+ -- episode ended by assertion is not waiting on anything, and a guard that
524
+ -- read the whole history would refuse every later claim on any task that ever
525
+ -- had a handback in it.
526
+ SELECT * INTO v_last
527
+ FROM cell.work_claim prior
528
+ WHERE prior.org_id = NEW.org_id
529
+ AND prior.task_id = NEW.task_id
530
+ AND prior.work_claim_id <> NEW.work_claim_id
531
+ AND prior.ownership_ended_at IS NOT NULL
532
+ ORDER BY prior.ownership_ended_at DESC, prior.claimed_at DESC, prior.work_claim_id
533
+ LIMIT 1;
534
+
535
+ -- Not a return-resumption. Three ways, each deliberate:
536
+ --
537
+ -- (i) no ended interval at all, or the last one ended some other way. The
538
+ -- claim path's own refusals (FC001/FC002/FC003) are what apply.
539
+ --
540
+ -- (ii) the last interval yielded for the OTHER declared cause. 017 declares
541
+ -- two, and only the handback has a resumption verb: a
542
+ -- `confirm_ask_expired` yield is a question nobody answered, and what
543
+ -- may follow it is FOUNDER CHECKPOINT FC-D, which is open and is W4's.
544
+ -- A guard that refused every writer there would settle FC-D by side
545
+ -- effect — this file may refuse the road it owns and may not close one
546
+ -- nobody has designed yet.
547
+ --
548
+ -- (iii) a basis no registered cause could have derived. `cell.claim_yield_cause`
549
+ -- (017:122-125) is what makes "why did responsibility pass" resolvable,
550
+ -- and a prefix outside it names no cause at all. Read from the registry
551
+ -- rather than spelled here, so a third cause reaches this guard by
552
+ -- landing its row and not by remembering to edit this file.
553
+ IF NOT FOUND
554
+ OR v_last.state IS DISTINCT FROM 'yielded'
555
+ OR NOT cell.terminal_basis_is_a_handback(v_last.terminal_basis)
556
+ THEN
557
+ RETURN NEW;
558
+ END IF;
559
+
560
+ -- The OTHER declared resumption cause steps through here untouched. 016's own
561
+ -- guard (FC130) is what governs a reopen — it requires
562
+ -- cell.reclaim_reopened_task on the stack for a second interval on a task the
563
+ -- seat reopened — and two guards demanding two different verbs of one INSERT
564
+ -- would make a task that was both reopened AND handed back unclaimable by
565
+ -- anything at all. The more specific, already-shipped cause wins, and D1's
566
+ -- retirement condition covers the case where a THIRD cause makes naming them
567
+ -- here worse than a registry.
568
+ IF NOT cell.written_by('cell.reclaim_returned_work')
569
+ AND NOT cell.written_by('cell.reclaim_reopened_task') THEN
570
+ RAISE EXCEPTION
571
+ 'RETURNED_WORK_RESUMED_OUTSIDE_A_DECLARED_CAUSE:%/% — the next '
572
+ 'claimant of work that was handed back is seated by '
573
+ 'cell.reclaim_returned_work and by nothing else. A runtime that '
574
+ 'seated this claim would be naming the principal who answers for '
575
+ 'work next, on the strength of noticing that a dependency closed — '
576
+ 'the authority L11 says a capability may never widen for itself. The '
577
+ 'writer signal is the PL/pgSQL call stack, which a direct writer '
578
+ 'cannot fabricate, rather than a column it could type: 23 historical '
579
+ '`verified` settles reached the bus by direct publish naming exactly '
580
+ 'the right evaluator.',
581
+ NEW.org_id, NEW.task_id
582
+ USING ERRCODE = 'FC180';
583
+ END IF;
584
+
585
+ RETURN NEW;
586
+ END;
587
+ $returned_writer$;
588
+
589
+ CREATE OR REPLACE TRIGGER work_claim_return_resumption_is_a_declared_cause
590
+ BEFORE INSERT ON cell.work_claim
591
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_returned_resumption_is_a_declared_cause();
592
+
593
+ -- =========================================================================
594
+ -- 5. THE VERB
595
+ -- =========================================================================
596
+ --
597
+ -- Every argument is required and none may grow a default. The three the caller
598
+ -- supplies are an IDENTITY, an INSTANT and an EVENT ID — the same three 013, 015
599
+ -- and 016 take — and everything the row MEANS is read off the substrate. There
600
+ -- is nowhere in this signature to put a principal a caller picked, which is
601
+ -- FC-B's ruling expressed as a shape rather than as a check.
602
+ --
603
+ -- It ANSWERS WHETHER THIS CALL MOVED ANYTHING, like every verb in 012, 013, 015
604
+ -- and 016: an eligibility wake may fire arbitrarily often, and "the work was
605
+ -- resumed" and "somebody already resumed it" are different facts to a seat that
606
+ -- has to write down what its wake did. A wake that resumed nothing must be
607
+ -- distinguishable in evidence from one that never looked.
608
+ CREATE OR REPLACE FUNCTION cell.reclaim_returned_work(
609
+ p_org_id text,
610
+ p_task_id text,
611
+ p_work_claim_id uuid,
612
+ p_claimed_at timestamptz,
613
+ p_event_id uuid
614
+ )
615
+ RETURNS boolean
616
+ LANGUAGE plpgsql
617
+ AS $reclaim_returned$
618
+ DECLARE
619
+ v_verify_by timestamptz;
620
+ v_pred cell.work_claim%ROWTYPE;
621
+ v_live uuid;
622
+ v_successor uuid;
623
+ v_deps cell.v_task_open_deps%ROWTYPE;
624
+ BEGIN
625
+ -- G0. The task's policy row, locked before anything is read off it: two
626
+ -- wakes resuming the same work on the same tick serialize HERE, and the
627
+ -- loser re-reads after the winner has committed rather than both seeing an
628
+ -- unowned obligation.
629
+ SELECT verify_by INTO v_verify_by
630
+ FROM cell.task
631
+ WHERE org_id = p_org_id AND task_id = p_task_id
632
+ FOR UPDATE;
633
+
634
+ IF NOT FOUND THEN
635
+ RAISE EXCEPTION
636
+ 'RETURN_RECLAIM_TASK_UNKNOWN:%/% — resuming is an act on work the '
637
+ 'company has a record of. A return that matched nothing would report '
638
+ 'a claimant for a task nobody can later account for.',
639
+ p_org_id, p_task_id
640
+ USING ERRCODE = 'FC181';
641
+ END IF;
642
+
643
+ -- G1. The interval being resumed: the LAST one to END, which is the one
644
+ -- whose holder was answerable for this work most recently. Everything the
645
+ -- fresh claim says about WHO is read from this row.
646
+ SELECT * INTO v_pred
647
+ FROM cell.work_claim
648
+ WHERE org_id = p_org_id AND task_id = p_task_id
649
+ AND ownership_ended_at IS NOT NULL
650
+ ORDER BY ownership_ended_at DESC, claimed_at DESC, work_claim_id
651
+ LIMIT 1;
652
+
653
+ IF NOT FOUND OR v_pred.state IS DISTINCT FROM 'yielded' THEN
654
+ -- Before refusing: a handback on this task that has ALREADY been resumed,
655
+ -- whose successor has since reached its own terminal, is why the last
656
+ -- ENDED interval is not the yield any more. That is a REPLAY of a wake
657
+ -- this verb already answered, not work with no handback behind it, and
658
+ -- L1-2's discipline is that a wake which resumed nothing says `false`
659
+ -- rather than raising. It is also §7.1 leg 18's shape from the other
660
+ -- side: an older yield under a later interval is not returnable, and the
661
+ -- honest report of that is "nothing to do" rather than an error.
662
+ SELECT succ.work_claim_id INTO v_successor
663
+ FROM cell.work_claim pred
664
+ JOIN cell.work_claim succ
665
+ ON succ.org_id = pred.org_id
666
+ AND succ.predecessor_work_claim_id = pred.work_claim_id
667
+ WHERE pred.org_id = p_org_id AND pred.task_id = p_task_id
668
+ AND pred.state = 'yielded'
669
+ ORDER BY pred.ownership_ended_at DESC, pred.work_claim_id
670
+ LIMIT 1;
671
+
672
+ IF FOUND THEN
673
+ -- G5's rule, reached early. Replay and duplicate are different facts
674
+ -- here for the same reason they are there: the same id resumed
675
+ -- nothing and says so; a different id over a handback that has
676
+ -- already been returned is a second interval claiming to be the
677
+ -- return of one yield, and it is refused loudly whether or not its
678
+ -- predecessor is still the last interval to have ended.
679
+ IF v_successor = p_work_claim_id THEN
680
+ RETURN false;
681
+ END IF;
682
+ RAISE EXCEPTION
683
+ 'RETURN_RECLAIM_PREDECESSOR_ALREADY_RESUMED:%:%:% — this work''s '
684
+ 'handback was already resumed by %, and this call names a '
685
+ 'different id. One yield has one return: two intervals both '
686
+ 'claiming to be it would make "which one answers for this work" '
687
+ 'unanswerable from the record.',
688
+ p_org_id, p_task_id, p_work_claim_id, v_successor
689
+ USING ERRCODE = 'FC186';
690
+ END IF;
691
+
692
+ RAISE EXCEPTION
693
+ 'RETURN_RECLAIM_NO_YIELDED_PREDECESSOR:%/%:% — a return resumes a '
694
+ 'HANDBACK. This work''s last interval ended some other way — by '
695
+ 'assertion, abort, takeover or expiry — or it has no ended interval '
696
+ 'at all, and a resumption of it (if there should be one) belongs to a '
697
+ 'different cause with a different verb.',
698
+ p_org_id, p_task_id,
699
+ coalesce(v_pred.state, '<no ended interval>')
700
+ USING ERRCODE = 'FC182';
701
+ END IF;
702
+
703
+ -- G2. 017 declares TWO yield causes, and only one of them is work coming
704
+ -- back. A `confirm_ask_expired` yield is a question nobody answered — an
705
+ -- escalation to the assigner — and resuming it would restart a principal on
706
+ -- work whose blocking decision still does not exist. The basis PREFIX is the
707
+ -- registry-resolvable discriminator (cell.claim_yield_cause, 017:122-125).
708
+ IF NOT cell.terminal_basis_is_a_handback(v_pred.terminal_basis) THEN
709
+ RAISE EXCEPTION
710
+ 'RETURN_RECLAIM_YIELD_WAS_NOT_A_HANDBACK:%/%:% — this interval ended '
711
+ 'by a yield whose recorded cause is not delegated work outstanding. '
712
+ 'Two causes write one meaning (017); only the handback is work coming '
713
+ 'back, and the other is an escalation somebody still owes an answer '
714
+ 'to.',
715
+ p_org_id, p_task_id, coalesce(v_pred.terminal_basis, '<none>')
716
+ USING ERRCODE = 'FC183';
717
+ END IF;
718
+
719
+ -- G3. THE RETURNED FACT ITSELF. `total_deps > 0` is load-bearing for the
720
+ -- same reason it is in the view: all_deps_satisfied is trivially true for a
721
+ -- task with no edges, and a yield that never waited on anything did not have
722
+ -- anything come back.
723
+ SELECT * INTO v_deps
724
+ FROM cell.v_task_open_deps
725
+ WHERE org_id = p_org_id AND task_id = p_task_id;
726
+
727
+ IF NOT FOUND OR NOT v_deps.total_deps > 0 OR NOT v_deps.all_deps_satisfied THEN
728
+ RAISE EXCEPTION
729
+ 'RETURN_RECLAIM_DEPENDENCY_STILL_OPEN:%/%:% — the work this '
730
+ 'obligation was waiting on has not been released. The dependency '
731
+ 'release IS the returned fact: without it this verb would seat a '
732
+ 'principal on work that is still blocked, and the handback it '
733
+ 'reverses would have reversed nothing.',
734
+ p_org_id, p_task_id,
735
+ coalesce(v_deps.open_deps::text, '<no declared dependency>')
736
+ USING ERRCODE = 'FC184';
737
+ END IF;
738
+
739
+ -- G4. A LIVE OWNER ends this call, and it is the idempotency PREDICATE
740
+ -- rather than a set this verb remembers: a restart takes a remembered set
741
+ -- with it, which is the whole reason L2 puts durable state below the agent.
742
+ -- It is also the honest answer for work somebody else has already picked up.
743
+ SELECT work_claim_id INTO v_live
744
+ FROM cell.work_claim
745
+ WHERE org_id = p_org_id AND task_id = p_task_id AND state = 'active';
746
+
747
+ IF FOUND THEN
748
+ RETURN false;
749
+ END IF;
750
+
751
+ -- G5. Replay and duplicate are DIFFERENT FACTS. A replay of the same derived
752
+ -- id resumed nothing and says so; a different id over the same predecessor is
753
+ -- a second interval claiming to be the return of one yield, and it is refused
754
+ -- loudly. Beneath both, work_claim_pkey and
755
+ -- work_claim_one_successor_per_predecessor hold against a writer that never
756
+ -- called this verb.
757
+ SELECT work_claim_id INTO v_successor
758
+ FROM cell.work_claim
759
+ WHERE org_id = p_org_id
760
+ AND predecessor_work_claim_id = v_pred.work_claim_id;
761
+
762
+ IF FOUND THEN
763
+ IF v_successor = p_work_claim_id THEN
764
+ RETURN false;
765
+ END IF;
766
+ RAISE EXCEPTION
767
+ 'RETURN_RECLAIM_PREDECESSOR_ALREADY_RESUMED:%:%:% — interval % was '
768
+ 'already resumed by %, and this call names a different id. One yield '
769
+ 'has one return: two intervals both claiming to be it would make '
770
+ '"which one answers for this work" unanswerable from the record.',
771
+ p_org_id, p_task_id, p_work_claim_id,
772
+ v_pred.work_claim_id, v_successor
773
+ USING ERRCODE = 'FC186';
774
+ END IF;
775
+
776
+ -- G6. Two intervals of responsibility over one task may not overlap in the
777
+ -- record: a history saying two principals were answerable at once is the
778
+ -- exact state unique active ownership exists to make impossible, and a
779
+ -- runtime that could pick the instant could write one by choosing an earlier
780
+ -- clock (016:234-245's reasoning, verbatim).
781
+ IF p_claimed_at < v_pred.ownership_ended_at THEN
782
+ RAISE EXCEPTION
783
+ 'RETURN_RECLAIM_RESUMES_BEFORE_THE_INTERVAL_IT_FOLLOWS:%:% — the '
784
+ 'interval this return follows ended at %, and this claim is stamped '
785
+ '%.',
786
+ p_task_id, p_work_claim_id, v_pred.ownership_ended_at, p_claimed_at
787
+ USING ERRCODE = 'FC185';
788
+ END IF;
789
+
790
+ -- THE ONE CLAIM PATH, called rather than re-spelled. The lock, the mode
791
+ -- check (R7), the unique-active-ownership refusal and the claim-meaning event
792
+ -- are the same ones every other claimant meets. Nothing is set to authorize
793
+ -- the write: THIS frame, on the call stack, is what the guard above reads,
794
+ -- and it stops existing when the function returns.
795
+ --
796
+ -- Every value is READ. `p_principal_id` is the predecessor's — FC-B, ruled
797
+ -- No — the delegation lineage is PROPAGATED, and the ownership window is the
798
+ -- TASK's own verify_by, the one recorded answer to "by when is this due".
799
+ PERFORM cell.claim_work(
800
+ p_org_id => p_org_id,
801
+ p_task_id => p_task_id,
802
+ p_work_claim_id => p_work_claim_id,
803
+ p_parent_work_claim_id => v_pred.parent_work_claim_id,
804
+ p_predecessor_work_claim_id => v_pred.work_claim_id,
805
+ p_principal_id => v_pred.principal_id,
806
+ p_department => v_pred.department,
807
+ p_claim_mode => v_pred.claim_mode,
808
+ p_ownership_valid_until => v_verify_by,
809
+ p_contract_ref => v_pred.contract_ref,
810
+ p_claimed_at => p_claimed_at,
811
+ p_event_id => p_event_id);
812
+
813
+ -- The stimulus, consumed in the SAME transaction as the interval that
814
+ -- resumed on it. `WHERE consumed_at IS NULL` and not a re-read: the row lock
815
+ -- the UPDATE takes is what settles two wakes, and a row that is already
816
+ -- consumed is simply not matched. A stimulus that is absent — the five
817
+ -- dependency families with no verdict behind them — leaves this a no-op,
818
+ -- because the stimulus is evidence and never a precondition.
819
+ UPDATE cell.return_stimulus
820
+ SET consumed_at = p_claimed_at,
821
+ consumed_by_work_claim_id = p_work_claim_id
822
+ WHERE org_id = p_org_id
823
+ AND predecessor_work_claim_id = v_pred.work_claim_id
824
+ AND consumed_at IS NULL;
825
+
826
+ RETURN true;
827
+ END;
828
+ $reclaim_returned$;
829
+
830
+ COMMENT ON FUNCTION cell.reclaim_returned_work IS
831
+ 'R-A35-23 / FOUNDER RULING R-A35-25 §5: the obligation handed back with '
832
+ 'delegated work outstanding gets a SECOND interval when that work returns, '
833
+ 'and the same principal answers for it. A TRANSCRIPTION — the principal, '
834
+ 'department, mode, contract ref and delegation lineage are read off the '
835
+ 'interval being resumed and the ownership window off the task''s own '
836
+ 'verify_by — and it goes through cell.claim_work rather than around it.';