@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,213 @@
1
+ -- 015_claim_yield.sql — THE YIELD TERMINAL FOR AN ASK NOBODY ANSWERED: the
2
+ -- sanctioned verb that hands responsibility back when the decision a claim was
3
+ -- waiting on can never arrive.
4
+ --
5
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.2 (claim terminals;
6
+ -- responsibility passes by yield, and a terminal is identified by event MEANING
7
+ -- rather than by producing code path), §5 case 4 (the confirm wait), FOUNDER
8
+ -- RULING R1 (a claim is responsibility, an attempt is runtime — a runtime dying
9
+ -- costs a binding and never an ownership, and so does a question dying).
10
+ -- Requires 001_core.sql, 002_claim.sql (cell.claim_event_producer) and
11
+ -- 011_confirm.sql (the ask this terminal is transcribed from).
12
+ --
13
+ -- Re-apply: idempotent (CREATE OR REPLACE, ON CONFLICT registry seed).
14
+ -- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
15
+ --
16
+ -- WHY THIS FILE EXISTS, MEASURED
17
+ --
18
+ -- `C4-TOKEN-EXPIRES` asks for one row: the claim whose operator ask died
19
+ -- unanswered, at a yield terminal, carrying a typed basis. Both bake-off
20
+ -- entrants were red on it in every repetition of the 20260818T1200Z pair and
21
+ -- NEITHER RED WAS THEIRS. `cell.consume_confirm_token`'s own FC062 message names
22
+ -- the outcome — "the honest outcome is a typed yield and an escalation to the
23
+ -- assigner" — and the cell had no verb that writes one: 002_claim.sql registered
24
+ -- `work_claim.claimed`, 013_claim_terminal.sql added `work_claim.
25
+ -- completion_asserted`, and the remaining four terminals were an L8 dated bridge
26
+ -- that had not landed. The only road to the row was a direct
27
+ -- `UPDATE cell.work_claim` from executor code, which the one-writer census
28
+ -- (A2.4-F0) forbids to every candidate absolutely. So the leg sat between a
29
+ -- missing domain verb and a boundary, and no executor closed it by trying harder.
30
+ --
31
+ -- This file lands the bridge's second span, for the ONE yield cause case 4
32
+ -- measures. The census is unchanged and `cell.work_claim` remains a table no
33
+ -- candidate's source may write; what a candidate may do is CAUSE the write it
34
+ -- may not PERFORM, exactly as with `cell.claim_effect_receipt`,
35
+ -- `cell.confirm_effect_receipt` and `cell.assert_claim_completion`.
36
+ --
37
+ -- WHY THE VERB NAMES THE ASK RATHER THAN THE CLAIM
38
+ --
39
+ -- Because it is a TRANSCRIPTION and not a decision, which is the only shape in
40
+ -- which a lane may write a claim terminal at all (013's argument, restated for
41
+ -- this cause). Everything written below is read off the ask: the org and task are
42
+ -- the token's, the claim is the one holding that task, and the basis is DERIVED
43
+ -- from the ask's own canonical key — there is nowhere in the statement to put a
44
+ -- basis a caller invented, so no runtime can end its principal's responsibility
45
+ -- and describe why in words of its own.
46
+ --
47
+ -- The INSTANT is the caller's and is required, and that is 011's lesson 3 rather
48
+ -- than an exception to 013's no-instant rule: expiry is derived at an explicit
49
+ -- instant, never stored, so a caller that could not state the instant it read the
50
+ -- world at could not be tested at a stated one. It cannot be used to end
51
+ -- responsibility early: the verb REFUSES any instant at which the ask was still
52
+ -- answerable, so the earliest yield this verb can write is the ask's own death.
53
+ --
54
+ -- WHAT THIS FILE OWNS, AND WHAT MOVED OUT OF IT
55
+ --
56
+ -- It owns the CAUSE and nothing else: which recorded fact ends this claim (an
57
+ -- ask that died unanswered), at which instants that fact is already true, and
58
+ -- what basis it derives. The TERMINAL itself — the claim lock, the idempotency
59
+ -- predicate, the terminals-are-not-reversible rule, the row and the meaning
60
+ -- event — moved to `cell.record_claim_yield` in 017_claim_yield_causes.sql, and
61
+ -- the inline copy that used to be here is deleted in that same change (L5).
62
+ --
63
+ -- It moved because a SECOND CAUSE arrived. §1.2 identifies a terminal by its
64
+ -- MEANING and 002's registry holds one producer per meaning, so a verb per cause
65
+ -- each writing its own row and its own event would be two producers behind one
66
+ -- meaning — the state that guard exists to refuse. One writer, several declared
67
+ -- causes, and the causes are a registry (`cell.claim_yield_cause`) rather than a
68
+ -- comment: this verb is registered there against the `confirm_ask_expired`
69
+ -- basis, and may write no other.
70
+ --
71
+ -- The apply order is a table-and-constraint order, not a call-graph one. PL/pgSQL
72
+ -- resolves the call below when it RUNS, so this file may hand its terminal to a
73
+ -- verb the next file installs; `tests/test_apply.py` applies the whole declared
74
+ -- order and `tests/test_claim_yield.py` drives this verb against it, so a 017
75
+ -- that stopped installing the writer fails there rather than in a cell.
76
+ --
77
+ -- FC123 (CLAIM_YIELD_CLAIM_ALREADY_ENDED) was raised here and is not any more: the
78
+ -- rule is every cause's, so it belongs to the shared writer (017, FC142).
79
+ --
80
+ -- WHY THERE IS STILL NO ROW TRIGGER ON THIS TRANSITION, SAID PLAINLY
81
+ --
82
+ -- 013 guards the transition into `completion_asserted` with `cell.written_by`,
83
+ -- and the symmetric guard here would be wrong TODAY rather than merely
84
+ -- unfinished. A yield has several lawful causes — a holder handing back
85
+ -- unfinished work, a parent yielding with delegated work outstanding, a
86
+ -- reassignment — and a guard naming ONE verb refuses every yield the other causes
87
+ -- perform, including the ones the matrix's own scenes declare as history. A
88
+ -- guard that has to be true of a meaning cannot be installed while only one of
89
+ -- that meaning's causes has a verb.
90
+ --
91
+ -- What IS installed is the half that can be true now: the EVENT registry, seeded
92
+ -- in 017 against the shared writer. `cell.enforce_one_producer_per_meaning`
93
+ -- refuses a hand-written `work_claim.yielded` event from any session — so a
94
+ -- forged terminal can still reach the ROW but can never reach the MEANING, and a
95
+ -- terminal with no event beside it is visible to anybody who looks for one.
96
+ -- Contract §1.2 makes the event the identity of a terminal, so that is the half
97
+ -- that decides.
98
+ --
99
+ -- L8, dated, narrowed by one span in 017: the row-level guard lands in the same
100
+ -- change as the remaining yield causes' verbs (the plain handback, the
101
+ -- reassignment). Retirement condition unchanged — all of contract §1.2's yield
102
+ -- causes reachable through a verb in this schema. Deletion owner: the A1
103
+ -- work-spine lane.
104
+ --
105
+ -- Declared failure vocabulary (SQLSTATE class FC; this lane reserves
106
+ -- FC120-FC129):
107
+ -- FC120 CLAIM_YIELD_ASK_UNKNOWN a yield transcribed from an ask the
108
+ -- cell has no record of
109
+ -- FC121 CLAIM_YIELD_ASK_WAS_ANSWERED a yield transcribed from an ask an
110
+ -- operator answered
111
+ -- FC122 CLAIM_YIELD_ASK_STILL_ANSWERABLE a yield at an instant the ask was
112
+ -- still alive at
113
+
114
+ -- --------------------------------------------------------------------- the verb
115
+ --
116
+ -- Every argument is required and none may grow a default. It ANSWERS WHETHER
117
+ -- THIS CALL MOVED ANYTHING, like every verb in 012 and 013: "the claim yielded"
118
+ -- and "the claim had already ended" are different facts to an executor that has
119
+ -- to write down what it did, and a wake that transcribed nothing must be
120
+ -- distinguishable in evidence from one that never looked.
121
+ CREATE OR REPLACE FUNCTION cell.yield_claim_on_expired_ask(
122
+ p_token_id uuid,
123
+ p_at timestamptz,
124
+ p_event_id uuid
125
+ )
126
+ RETURNS boolean
127
+ LANGUAGE plpgsql
128
+ AS $yield_on_expiry$
129
+ DECLARE
130
+ v_org text;
131
+ v_task text;
132
+ v_ask_key text;
133
+ v_state text;
134
+ v_expires_at timestamptz;
135
+ v_claim uuid;
136
+ BEGIN
137
+ SELECT t.org_id, t.task_id, t.ask_key, t.state, t.expires_at
138
+ INTO v_org, v_task, v_ask_key, v_state, v_expires_at
139
+ FROM cell.confirm_token t
140
+ WHERE t.token_id = p_token_id;
141
+
142
+ IF NOT FOUND THEN
143
+ RAISE EXCEPTION
144
+ 'CLAIM_YIELD_ASK_UNKNOWN:% — a yield on an expiry is a TRANSCRIPTION '
145
+ 'of an ask that died, and there is no such ask. Writing anyway would '
146
+ 'end a principal''s responsibility on the strength of a question '
147
+ 'nobody ever put.',
148
+ p_token_id
149
+ USING ERRCODE = 'FC120';
150
+ END IF;
151
+
152
+ IF v_state <> 'issued' THEN
153
+ RAISE EXCEPTION
154
+ 'CLAIM_YIELD_ASK_WAS_ANSWERED:%:% — an operator answered this ask, so '
155
+ 'the decision the claim was waiting on EXISTS. The verdict it becomes '
156
+ 'is the evaluator seat''s to transcribe (011_confirm.sql); yielding '
157
+ 'here would hand back work whose answer had already arrived.',
158
+ p_token_id, v_state
159
+ USING ERRCODE = 'FC121';
160
+ END IF;
161
+
162
+ IF p_at < v_expires_at THEN
163
+ RAISE EXCEPTION
164
+ 'CLAIM_YIELD_ASK_STILL_ANSWERABLE:% — the ask lives until % and this '
165
+ 'yield is stamped %. 011_confirm.sql lesson 2: a waiter that gives up '
166
+ 'while its ask is still answerable has not timed out, it has dropped a '
167
+ 'live decision — and a runtime that could pick the instant would be '
168
+ 'able to drop one by choosing a later clock.',
169
+ p_token_id, v_expires_at, p_at
170
+ USING ERRCODE = 'FC122';
171
+ END IF;
172
+
173
+ -- The claim that HOLDS this work. The lock, the state read and the
174
+ -- not-reversible rule are `cell.record_claim_yield`'s (017): they are true of
175
+ -- every yield whatever caused it, and a rule that lives in one cause is a
176
+ -- rule the next cause has to remember. What is read HERE is the identity —
177
+ -- which claim this ask's work belongs to — because that is the cause's own
178
+ -- question and nobody else's.
179
+ SELECT wc.work_claim_id INTO v_claim
180
+ FROM cell.work_claim wc
181
+ WHERE wc.org_id = v_org AND wc.task_id = v_task
182
+ ORDER BY wc.claimed_at DESC, wc.work_claim_id
183
+ LIMIT 1;
184
+
185
+ -- An ask over work nobody ever claimed transcribes nothing and says so. It
186
+ -- is not a refusal: a token can outlive the claim it was minted under, and a
187
+ -- verb that raised here would take down a recovery sweep over the ordinary
188
+ -- shape of the world.
189
+ IF v_claim IS NULL THEN
190
+ RETURN false;
191
+ END IF;
192
+
193
+ -- The terminal, through the one producer of the yield MEANING. The basis is
194
+ -- DERIVED from the ask's own canonical key, so "why did responsibility pass"
195
+ -- resolves to the question that died rather than to a sentence a runtime
196
+ -- composed about itself — and the registry binds this verb to the
197
+ -- `confirm_ask_expired` prefix, so it is the only basis this cause can write.
198
+ -- `settled_at` is untouched and belongs to the evaluator seat (§1.6): a
199
+ -- question nobody answered settles nothing.
200
+ RETURN cell.record_claim_yield(
201
+ p_work_claim_id => v_claim,
202
+ p_basis => 'confirm_ask_expired:' || v_ask_key,
203
+ p_at => p_at,
204
+ p_event_id => p_event_id);
205
+ END;
206
+ $yield_on_expiry$;
207
+
208
+ COMMENT ON FUNCTION cell.yield_claim_on_expired_ask IS
209
+ 'Contract §1.2 / R1 and 011_confirm.sql FC062: the FIRST declared cause of '
210
+ 'a work_claim.yielded meaning — the operator ask that died unanswered. A '
211
+ 'TRANSCRIPTION: org, task, claim and basis are all read off the ask, and the '
212
+ 'instant is refused unless the ask was already dead at it. The terminal '
213
+ 'itself is written by cell.record_claim_yield (017).';
@@ -0,0 +1,285 @@
1
+ -- 016_reopened_reclaim.sql — R8's SECOND HALF: the fresh interval of
2
+ -- responsibility a reopened task gets, and the call-stack guard that makes the
3
+ -- evaluator seat the only way to it.
4
+ --
5
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.2 (a claim is an interval of
6
+ -- RESPONSIBILITY; responsibility passes by a terminal, never by a second
7
+ -- claimant arriving), §2.1 shape (a) (the delegated child claim), FOUNDER
8
+ -- RULING R8 ("a rejection reopens the work — the reopened task's next claimant
9
+ -- resumes on the SAME work"), and R1 (a claim is responsibility, an attempt is
10
+ -- runtime). Requires 001_core.sql, 002_claim.sql (cell.claim_work,
11
+ -- cell.written_by), 003_verification.sql and 012_evaluator_lane.sql
12
+ -- (cell.reopen_rejected_task, whose act this one follows and never performs).
13
+ --
14
+ -- Re-apply: idempotent (CREATE OR REPLACE throughout).
15
+ -- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
16
+ --
17
+ -- WHY THIS FILE EXISTS, MEASURED
18
+ --
19
+ -- `C1-CHILD-REJECTED` asks for one row: the rejected delegated child carrying an
20
+ -- ACTIVE claim again, while the parent goes on waiting and nothing anywhere is
21
+ -- settled. Both bake-off entrants were red on it in every repetition of the
22
+ -- 20260818T2100Z pair and NEITHER RED WAS THEIRS. 012 landed half of R8 —
23
+ -- `cell.reopen_rejected_task` moves the task's origin to `reopened`, which says
24
+ -- the work is not done — and the other half of the same sentence, the next
25
+ -- claimant resuming, had no verb in the cell at all. The only road to the row
26
+ -- was a `cell.claim_work` issued from executor code on behalf of a principal
27
+ -- that executor does not represent: in the leg's own scene, an engineering-scope
28
+ -- runtime claiming as the RESEARCH lead. The one-writer census (A2.4-F0) forbids
29
+ -- exactly that, so the leg sat between a missing domain verb and a boundary and
30
+ -- no executor closed it by trying harder.
31
+ --
32
+ -- WHY A SEAT MAY MINT A CLAIM AT ALL
33
+ --
34
+ -- Because it invents nothing, which is the same reason 013's completion terminal
35
+ -- and 012's confirm transcription are lawful. Every value in the fresh claim is
36
+ -- READ: the principal, the department, the mode, the contract ref and the parent
37
+ -- claim come off the interval being resumed, and the ownership window is the
38
+ -- TASK's own `verify_by` — the one recorded answer to "by when is this work
39
+ -- due". There is nowhere in the signature below to put a principal a caller
40
+ -- picked. A verb that took one would be a runtime deciding who answers for work
41
+ -- next, which is the authority L11 says a capability may never widen for itself.
42
+ --
43
+ -- WHY THE REOPEN IS NOT PERFORMED HERE
44
+ --
45
+ -- Two facts, two verbs. `cell.reopen_rejected_task` says the work is not done;
46
+ -- this one says who resumes it. A single verb doing both would let a resumption
47
+ -- reopen work nobody rejected — the seat handing live work to a second interval
48
+ -- on its own say-so — and the refusal below (FC132) is what keeps the order
49
+ -- honest: this verb acts only on a task the reopen has already reached.
50
+ --
51
+ -- WHY THE GUARD READS THE ORIGIN AND NOT THE COUNT
52
+ --
53
+ -- A first claim on reopened work is an ORDINARY claim and stays one: a task
54
+ -- reopened before anybody ever held it must be claimable by whoever claims it,
55
+ -- or the reopen would be a state no principal can take responsibility for. A
56
+ -- second claim on work nobody reopened is a takeover, a reassignment or a fresh
57
+ -- interval after a yield, and none of those is this file's business either. What
58
+ -- is guarded is the intersection — a SECOND interval on a task the seat
59
+ -- reopened — because that is precisely the write an executor would have to
60
+ -- perform to close its own leg. The signal is `cell.written_by`, the PL/pgSQL
61
+ -- call stack, rather than a column a writer could type or a setting it could set
62
+ -- for itself: MEASURED, 23 historical `verified` settles reached the bus by
63
+ -- direct publish naming exactly the right evaluator.
64
+ --
65
+ -- Declared failure vocabulary (SQLSTATE class FC; this lane reserves
66
+ -- FC130-FC139):
67
+ -- FC130 REOPENED_WORK_RESUMED_OUTSIDE_THE_EVALUATOR_LANE a second interval on
68
+ -- a reopened task, claimed by something
69
+ -- that is not this seat's verb
70
+ -- FC131 REOPENED_RECLAIM_TASK_UNKNOWN a resumption of work the company has
71
+ -- no record of
72
+ -- FC132 REOPENED_RECLAIM_TASK_NOT_REOPENED a resumption of work nobody
73
+ -- reopened
74
+ -- FC133 REOPENED_RECLAIM_NO_PRIOR_CLAIM a resumption of work nobody ever held,
75
+ -- so there is no claimant to read
76
+ -- FC134 REOPENED_RECLAIM_RESUMES_BEFORE_THE_INTERVAL_IT_FOLLOWS a resumption
77
+ -- stamped inside the previous holder's
78
+ -- own window
79
+
80
+ -- ------------------------------------------------------------- the writer guard
81
+ --
82
+ -- BEFORE INSERT, because a resumption is a NEW claim row and never an update of
83
+ -- the interval it follows (§1.2: responsibility passes by a terminal; the ended
84
+ -- interval stays in the record exactly as it ended, L12). The two narrowing
85
+ -- clauses are the whole subject of the header above: the task's origin, and
86
+ -- whether this row is the first interval or a later one.
87
+ CREATE OR REPLACE FUNCTION cell.enforce_reopened_resumption_is_the_evaluator_lanes()
88
+ RETURNS trigger
89
+ LANGUAGE plpgsql
90
+ AS $resumption_writer$
91
+ DECLARE
92
+ v_origin text;
93
+ BEGIN
94
+ SELECT origin INTO v_origin
95
+ FROM cell.task
96
+ WHERE org_id = NEW.org_id AND task_id = NEW.task_id;
97
+
98
+ -- Not a resumption: an ordinary claim on work nobody reopened. The claim
99
+ -- path's own refusals (FC001/FC002/FC003) are the ones that apply.
100
+ IF v_origin IS DISTINCT FROM 'reopened' THEN
101
+ RETURN NEW;
102
+ END IF;
103
+
104
+ -- Not a resumption either: the FIRST interval on a task that happens to
105
+ -- carry the reopened origin. R8 is about the next claimant, and where there
106
+ -- was no previous one there is nothing to be next to.
107
+ IF NOT EXISTS (SELECT 1 FROM cell.work_claim prior
108
+ WHERE prior.org_id = NEW.org_id
109
+ AND prior.task_id = NEW.task_id
110
+ AND prior.work_claim_id <> NEW.work_claim_id) THEN
111
+ RETURN NEW;
112
+ END IF;
113
+
114
+ IF NOT cell.written_by('cell.reclaim_reopened_task') THEN
115
+ RAISE EXCEPTION
116
+ 'REOPENED_WORK_RESUMED_OUTSIDE_THE_EVALUATOR_LANE:%/% — the next '
117
+ 'claimant of reopened work is seated by cell.reclaim_reopened_task '
118
+ 'and by nothing else. FOUNDER RULING R8 makes the resumption the '
119
+ 'other half of the reopen, and the reopen is already the evaluator '
120
+ 'seat''s (012_evaluator_lane.sql): an executor that seated this '
121
+ 'claim would be naming the principal who answers for work next, on '
122
+ 'the strength of a verdict about work it ran itself. The writer '
123
+ 'signal is the PL/pgSQL call stack, which a direct writer cannot '
124
+ 'fabricate, rather than a column it could type: 23 historical '
125
+ '`verified` settles reached the bus by direct publish naming '
126
+ 'exactly the right evaluator.',
127
+ NEW.org_id, NEW.task_id
128
+ USING ERRCODE = 'FC130';
129
+ END IF;
130
+
131
+ RETURN NEW;
132
+ END;
133
+ $resumption_writer$;
134
+
135
+ CREATE OR REPLACE TRIGGER work_claim_resumption_is_the_evaluator_lanes
136
+ BEFORE INSERT ON cell.work_claim
137
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_reopened_resumption_is_the_evaluator_lanes();
138
+
139
+ -- --------------------------------------------------------------------- the verb
140
+ --
141
+ -- Every argument is required and none may grow a default. The three the caller
142
+ -- supplies are an IDENTITY, an INSTANT and an EVENT ID — the same three 013 and
143
+ -- 015 take — and everything the row MEANS is read off the substrate. A defaulted
144
+ -- identifier would make the caller unable to address the claim it caused; a
145
+ -- defaulted instant would be the substrate deciding when a second interval of
146
+ -- responsibility began.
147
+ --
148
+ -- It ANSWERS WHETHER THIS CALL MOVED ANYTHING, like every verb in 012, 013 and
149
+ -- 015: "the work was resumed" and "the work already has a holder" are different
150
+ -- facts to a seat that has to write down what its wake did, and a wake that
151
+ -- resumed nothing must be distinguishable in evidence from one that never
152
+ -- looked.
153
+ CREATE OR REPLACE FUNCTION cell.reclaim_reopened_task(
154
+ p_org_id text,
155
+ p_task_id text,
156
+ p_work_claim_id uuid,
157
+ p_claimed_at timestamptz,
158
+ p_event_id uuid
159
+ )
160
+ RETURNS boolean
161
+ LANGUAGE plpgsql
162
+ AS $reclaim_reopened$
163
+ DECLARE
164
+ v_origin text;
165
+ v_verify_by timestamptz;
166
+ v_prior cell.work_claim%ROWTYPE;
167
+ v_live uuid;
168
+ BEGIN
169
+ -- The task's policy row, locked before anything is read off it: two wakes
170
+ -- resuming the same work on the same tick serialize here, exactly as two
171
+ -- claimants of one task serialize inside `cell.claim_work`.
172
+ SELECT origin, verify_by INTO v_origin, v_verify_by
173
+ FROM cell.task
174
+ WHERE org_id = p_org_id AND task_id = p_task_id
175
+ FOR UPDATE;
176
+
177
+ IF NOT FOUND THEN
178
+ RAISE EXCEPTION
179
+ 'REOPENED_RECLAIM_TASK_UNKNOWN:%/% — resuming is an act on work the '
180
+ 'company has a record of. A resumption that matched nothing would '
181
+ 'report a claimant for a task nobody can later account for.',
182
+ p_org_id, p_task_id
183
+ USING ERRCODE = 'FC131';
184
+ END IF;
185
+
186
+ IF v_origin IS DISTINCT FROM 'reopened' THEN
187
+ RAISE EXCEPTION
188
+ 'REOPENED_RECLAIM_TASK_NOT_REOPENED:%/%:% — R8''s two halves are two '
189
+ 'verbs. `cell.reopen_rejected_task` says the work is not done; this '
190
+ 'one says who resumes it, and it acts only on a task the reopen has '
191
+ 'already reached. A verb that did both would let a resumption reopen '
192
+ 'work nobody rejected — a seat handing live work to a second '
193
+ 'interval on its own say-so.',
194
+ p_org_id, p_task_id, coalesce(v_origin, '<null>')
195
+ USING ERRCODE = 'FC132';
196
+ END IF;
197
+
198
+ -- A LIVE OWNER ends this call, and it is the idempotency predicate rather
199
+ -- than a set this verb remembers: a restart takes a remembered set with it,
200
+ -- which is the whole reason L2 puts durable state below the agent. It is
201
+ -- also the answer for a task reopened while its holder is still working —
202
+ -- reopening does not evict anybody, and a seat that took the work off a live
203
+ -- principal would be performing the takeover terminal nobody asked it for.
204
+ SELECT work_claim_id INTO v_live
205
+ FROM cell.work_claim
206
+ WHERE org_id = p_org_id AND task_id = p_task_id AND state = 'active';
207
+
208
+ IF FOUND THEN
209
+ RETURN false;
210
+ END IF;
211
+
212
+ -- The interval being resumed: the LAST one to end, which is the one whose
213
+ -- holder was answerable for this work most recently. Everything the fresh
214
+ -- claim says about WHO is read from this row.
215
+ SELECT * INTO v_prior
216
+ FROM cell.work_claim
217
+ WHERE org_id = p_org_id AND task_id = p_task_id
218
+ AND ownership_ended_at IS NOT NULL
219
+ ORDER BY ownership_ended_at DESC, claimed_at DESC, work_claim_id
220
+ LIMIT 1;
221
+
222
+ IF NOT FOUND THEN
223
+ RAISE EXCEPTION
224
+ 'REOPENED_RECLAIM_NO_PRIOR_CLAIM:%/% — there is no ended interval of '
225
+ 'responsibility to read the claimant off, and this verb does not '
226
+ 'choose one. R8 is about the NEXT claimant; a task reopened without '
227
+ 'ever having been claimed is a mis-declared task, and seating a '
228
+ 'principal on it would be the substrate deciding who answers for '
229
+ 'work nobody has answered for yet (L11).',
230
+ p_org_id, p_task_id
231
+ USING ERRCODE = 'FC133';
232
+ END IF;
233
+
234
+ IF p_claimed_at < v_prior.ownership_ended_at THEN
235
+ RAISE EXCEPTION
236
+ 'REOPENED_RECLAIM_RESUMES_BEFORE_THE_INTERVAL_IT_FOLLOWS:%:% — the '
237
+ 'interval this resumption follows ended at %, and this claim is '
238
+ 'stamped %. Two intervals of responsibility over one task may not '
239
+ 'overlap in the record: a history saying two principals were '
240
+ 'answerable at once is the exact state unique active ownership '
241
+ 'exists to make impossible, and a runtime that could pick the '
242
+ 'instant could write one by choosing an earlier clock.',
243
+ p_task_id, p_work_claim_id, v_prior.ownership_ended_at, p_claimed_at
244
+ USING ERRCODE = 'FC134';
245
+ END IF;
246
+
247
+ -- THE ONE CLAIM PATH, called rather than re-spelled. The lock, the mode
248
+ -- check (R7), the unique-active-ownership refusal and the claim-meaning
249
+ -- event are the same ones every other claimant meets — a second INSERT here
250
+ -- would be a second claim path, which is the thing 002 exists to not have.
251
+ --
252
+ -- Nothing is set to authorize the write: THIS frame, on the call stack, is
253
+ -- what the guard above reads, and it stops existing when the function
254
+ -- returns.
255
+ PERFORM cell.claim_work(
256
+ p_org_id => p_org_id,
257
+ p_task_id => p_task_id,
258
+ p_work_claim_id => p_work_claim_id,
259
+ p_parent_work_claim_id => v_prior.parent_work_claim_id,
260
+ -- NULL, and it is a statement rather than an omission (027 gave
261
+ -- cell.claim_work this argument and no default). A reopen is not a
262
+ -- succession: R8's next claimant resumes work a REJECTION reopened, and
263
+ -- the prior interval it reads from may be any terminal at all. Naming it
264
+ -- as a predecessor here would put a reopen inside
265
+ -- work_claim_one_successor_per_predecessor's uniqueness and make a task
266
+ -- reopenable exactly once.
267
+ p_predecessor_work_claim_id => NULL,
268
+ p_principal_id => v_prior.principal_id,
269
+ p_department => v_prior.department,
270
+ p_claim_mode => v_prior.claim_mode,
271
+ p_ownership_valid_until => v_verify_by,
272
+ p_contract_ref => v_prior.contract_ref,
273
+ p_claimed_at => p_claimed_at,
274
+ p_event_id => p_event_id);
275
+
276
+ RETURN true;
277
+ END;
278
+ $reclaim_reopened$;
279
+
280
+ COMMENT ON FUNCTION cell.reclaim_reopened_task IS
281
+ 'FOUNDER RULING R8, second half: the reopened task''s next claimant resumes '
282
+ 'on the SAME work. A TRANSCRIPTION — the principal, department, mode, '
283
+ 'contract ref and parent claim are read off the interval being resumed and '
284
+ 'the ownership window off the task''s own verify_by — and it goes through '
285
+ 'cell.claim_work rather than around it.';