@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,279 @@
1
+ -- 013_claim_terminal.sql — THE COMPLETION TERMINAL: the sanctioned verb that
2
+ -- ends an interval of responsibility, and the call-stack guard that makes it the
3
+ -- only way in.
4
+ --
5
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.2 (claim terminals; terminals
6
+ -- are identified by event MEANING, never by producing code path), §1.3 (a
7
+ -- completion statement is an ASSERTION and not completion), §1.6 (completion and
8
+ -- settlement are separate facts), FOUNDER RULING R1 (a claim is responsibility,
9
+ -- an attempt is runtime) and R2 (the uniform assertion terminal).
10
+ -- Requires 001_core.sql, 002_claim.sql (cell.written_by, the producer registry)
11
+ -- and 003_verification.sql (cell.agent_statement).
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
+ -- 002_claim.sql registered ONE producer — `work_claim.claimed` — and named the
19
+ -- rest of contract §1.2's terminal verbs as an L8 dated bridge that had not
20
+ -- landed. The bake-off measured what that bridge's absence costs. `C3-K6` is the
21
+ -- `lifecycle_silent_complete` analog (78 of 120 measured orphans on the Mac were
22
+ -- work that had finished and never said so), and its check asks for exactly one
23
+ -- row: the claim, at its completion terminal, with `completed_at` stamped. Both
24
+ -- candidates were red on it in every repetition, and neither red was theirs: THE
25
+ -- CELL HAD NO VERB THAT WRITES ONE. The only road to that row was a direct
26
+ -- `UPDATE cell.work_claim` from executor code — a settlement-class write by the
27
+ -- one-writer census's own list (A2.4-F0), forbidden absolutely to every
28
+ -- candidate. So the leg sat between a missing domain verb and a boundary, and no
29
+ -- executor closed it by trying harder.
30
+ --
31
+ -- This file lands the bridge's first span. It is deliberately NOT "the executor
32
+ -- may now write claim terminals": the census is unchanged and `cell.work_claim`
33
+ -- remains a table no candidate's source may write. What a candidate may do is
34
+ -- CAUSE the write it may not PERFORM — the same shape as `cell.claim_effect_
35
+ -- receipt` and `cell.confirm_effect_receipt`, and the shape the evaluator seat's
36
+ -- three verbs already have.
37
+ --
38
+ -- WHY THIS IS A COMPLETION TERMINAL AND NOT A SETTLEMENT
39
+ --
40
+ -- Said plainly because the two are one word apart in English and two facts apart
41
+ -- in the contract. §1.6: settlement is a SEPARATE fact from completion, and
42
+ -- `settled_at` is a different column with a different owner — the shared
43
+ -- evaluator seat, which transcribes verdicts that already exist. This verb
44
+ -- writes `state`, `completed_at` and `ownership_ended_at` and never touches
45
+ -- `settled_at`. An author asserting completion is L10's `agent_statement`: it
46
+ -- ends the author's RESPONSIBILITY and settles nothing at all. The
47
+ -- `work_claim_settlement_follows_a_claim_terminal` constraint in 001 is what
48
+ -- keeps the two orders honest afterwards.
49
+ --
50
+ -- WHY IT IS A TRANSCRIPTION AND TAKES NO INSTANT
51
+ --
52
+ -- The verb's two arguments are identifiers. Everything it writes is READ off the
53
+ -- `cell.agent_statement` row the caller names: the org, the task, the claim and
54
+ -- the instant. There is nowhere in the statement below to put a value the caller
55
+ -- invented, which is the same discipline `cell.record_confirm_verdict` is built
56
+ -- to and the reason a lane writing a settlement-adjacent fact is lawful at all.
57
+ -- In particular there is no `p_completed_at`: a runtime that could choose the
58
+ -- instant its principal's responsibility ended could end it before the assertion
59
+ -- that is supposed to be its basis.
60
+ --
61
+ -- Declared failure vocabulary (SQLSTATE class FC; this lane reserves
62
+ -- FC100-FC109):
63
+ -- FC100 CLAIM_TERMINAL_WRITTEN_OUTSIDE_THE_SANCTIONED_VERB a completion
64
+ -- terminal moved onto a claim row by
65
+ -- something that is not this verb
66
+ -- FC101 CLAIM_TERMINAL_STATEMENT_UNKNOWN a terminal transcribed from a
67
+ -- statement the cell has no record of
68
+ -- FC102 CLAIM_TERMINAL_STATEMENT_IS_NOT_A_COMPLETION a terminal transcribed
69
+ -- from a progress/result/observation
70
+ -- FC103 CLAIM_TERMINAL_STATEMENT_SUPERSEDED a terminal transcribed from an
71
+ -- assertion its own author withdrew
72
+ -- FC104 CLAIM_TERMINAL_CLAIM_ALREADY_ENDED a completion terminal onto a claim
73
+ -- that yielded, aborted, was taken over
74
+ -- or expired
75
+
76
+ -- ------------------------------------------------------------- the writer guard
77
+ --
78
+ -- The 009/012 lesson, restated for the claim row: a rule that lives only in
79
+ -- whoever happens to be writing is a rule with a bypass, and the Mac's 23
80
+ -- direct-publish `verified` settles are what that bypass cost. The signal is
81
+ -- `cell.written_by` — the PL/pgSQL CALL STACK, which a caller cannot put a frame
82
+ -- on that is not there — rather than a column it could type or a setting it
83
+ -- could set for itself.
84
+ --
85
+ -- ONLY THE TRANSITION IS GUARDED, and only on UPDATE. Two deliberate scopes:
86
+ --
87
+ -- * a row that is ALREADY `completion_asserted` may be updated for reasons
88
+ -- that have nothing to do with the terminal (the evaluator seat's later
89
+ -- `settled_at`, a correction to `ownership_valid_until`), and re-checking the
90
+ -- terminal there would make an unrelated write fail for a reason that is not
91
+ -- about it — the same scoping `cell.enforce_task_edge_release_is_the_
92
+ -- evaluator_lanes` uses;
93
+ -- * an INSERT of a row already at a terminal is a SCENE declaring history, not
94
+ -- a writer performing one. `cell.claim_work` is the only path that mints a
95
+ -- claim and it always mints `active`, so every terminal a live cell reaches
96
+ -- is reached by UPDATE. 012's reopen guard is `BEFORE UPDATE` for exactly
97
+ -- this reason.
98
+ CREATE OR REPLACE FUNCTION cell.enforce_completion_terminal_is_the_sanctioned_verbs()
99
+ RETURNS trigger
100
+ LANGUAGE plpgsql
101
+ AS $completion_writer$
102
+ BEGIN
103
+ IF NEW.state IS DISTINCT FROM 'completion_asserted'
104
+ OR OLD.state = 'completion_asserted' THEN
105
+ RETURN NEW;
106
+ END IF;
107
+
108
+ IF NOT cell.written_by('cell.assert_claim_completion') THEN
109
+ RAISE EXCEPTION
110
+ 'CLAIM_TERMINAL_WRITTEN_OUTSIDE_THE_SANCTIONED_VERB:% — a claim '
111
+ 'reaches its completion terminal through cell.assert_claim_completion '
112
+ 'and through nothing else. Contract §1.2: claim terminals are '
113
+ 'identified by MEANING and the cell has exactly one producer per '
114
+ 'meaning; §1.3 and L10 make the author''s completion statement an '
115
+ 'assertion, so the terminal is a TRANSCRIPTION of a recorded '
116
+ 'statement rather than a runtime''s opinion about itself. The writer '
117
+ 'signal is the PL/pgSQL call stack, which a direct writer cannot '
118
+ 'fabricate, rather than a column it could type: 23 historical '
119
+ '`verified` settles reached the bus by direct publish naming exactly '
120
+ 'the right evaluator.',
121
+ NEW.work_claim_id
122
+ USING ERRCODE = 'FC100';
123
+ END IF;
124
+
125
+ RETURN NEW;
126
+ END;
127
+ $completion_writer$;
128
+
129
+ CREATE OR REPLACE TRIGGER work_claim_completion_terminal_is_the_sanctioned_verbs
130
+ BEFORE UPDATE ON cell.work_claim
131
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_completion_terminal_is_the_sanctioned_verbs();
132
+
133
+ -- ------------------------------------------------------ the producer registry
134
+ --
135
+ -- The other half of "one producer per meaning", and the half 002 could not seed
136
+ -- until this verb existed. With this row present, `cell.enforce_one_producer_
137
+ -- per_meaning` refuses a hand-written `work_claim.completion_asserted` event
138
+ -- from any session — the same refusal `work_claim.claimed` has had since 002.
139
+ --
140
+ -- L8 (dated bridge), narrowed by one span: the registry is complete when the
141
+ -- remaining terminal verbs (yield, abort, takeover, expiry) ship. Retirement
142
+ -- condition unchanged — all of contract §1.2's claim-terminal verbs present in
143
+ -- this table; deletion owner: the A1 work-spine lane.
144
+ INSERT INTO cell.claim_event_producer (topic, producer)
145
+ VALUES ('work_claim.completion_asserted', 'cell.assert_claim_completion')
146
+ ON CONFLICT (topic) DO UPDATE SET producer = EXCLUDED.producer;
147
+
148
+ -- --------------------------------------------------------------------- the verb
149
+ --
150
+ -- Both arguments are required and both are identifiers. There is no default here
151
+ -- and there will not be one: a server-generated event id would make the caller
152
+ -- unable to address the event it caused, and a defaulted instant would be the
153
+ -- substrate choosing when a principal stopped being responsible.
154
+ --
155
+ -- It ANSWERS WHETHER THIS CALL MOVED ANYTHING, like every verb in 012: "the
156
+ -- terminal was recorded" and "the terminal was already recorded" are different
157
+ -- facts to an executor that has to write down what it did, and a recovery that
158
+ -- transcribed nothing must be distinguishable in evidence from one that never
159
+ -- looked.
160
+ CREATE OR REPLACE FUNCTION cell.assert_claim_completion(
161
+ p_statement_id uuid,
162
+ p_event_id uuid
163
+ )
164
+ RETURNS boolean
165
+ LANGUAGE plpgsql
166
+ AS $assert_completion$
167
+ DECLARE
168
+ v_org text;
169
+ v_task text;
170
+ v_claim uuid;
171
+ v_type text;
172
+ v_asserted_at timestamptz;
173
+ v_superseded_by uuid;
174
+ v_state text;
175
+ v_moved integer;
176
+ BEGIN
177
+ SELECT s.org_id, s.task_id, s.work_claim_id, s.statement_type, s.asserted_at,
178
+ (SELECT later.statement_id FROM cell.agent_statement later
179
+ WHERE later.supersedes_ref = s.statement_id LIMIT 1)
180
+ INTO v_org, v_task, v_claim, v_type, v_asserted_at, v_superseded_by
181
+ FROM cell.agent_statement s
182
+ WHERE s.statement_id = p_statement_id;
183
+
184
+ IF NOT FOUND THEN
185
+ RAISE EXCEPTION
186
+ 'CLAIM_TERMINAL_STATEMENT_UNKNOWN:% — a completion terminal is a '
187
+ 'TRANSCRIPTION of a recorded assertion (§1.3), and there is no such '
188
+ 'statement. Writing anyway would end a principal''s responsibility on '
189
+ 'the strength of something nobody said.',
190
+ p_statement_id
191
+ USING ERRCODE = 'FC101';
192
+ END IF;
193
+
194
+ IF v_type IS DISTINCT FROM 'completion' THEN
195
+ RAISE EXCEPTION
196
+ 'CLAIM_TERMINAL_STATEMENT_IS_NOT_A_COMPLETION:%:% — only a '
197
+ '`completion` statement asserts that the work described by the '
198
+ 'objective is done. A progress, result or observation statement is an '
199
+ 'author saying something ELSE about work still in hand, and a terminal '
200
+ 'transcribed from one would end responsibility nobody offered to end.',
201
+ p_statement_id, coalesce(v_type, '<null>')
202
+ USING ERRCODE = 'FC102';
203
+ END IF;
204
+
205
+ IF v_superseded_by IS NOT NULL THEN
206
+ RAISE EXCEPTION
207
+ 'CLAIM_TERMINAL_STATEMENT_SUPERSEDED:%:% — this assertion was '
208
+ 'withdrawn by a later one, and a superseded completion assertion '
209
+ 'stops counting in v_task_completion for exactly that reason. The '
210
+ 'terminal follows the LIVE assertion or it follows nothing.',
211
+ p_statement_id, v_superseded_by
212
+ USING ERRCODE = 'FC103';
213
+ END IF;
214
+
215
+ -- Lock the claim before reading its state: two transcriptions of the same
216
+ -- assertion on the same tick serialize here, and the loser re-reads AFTER
217
+ -- the winner has committed rather than both seeing `active`.
218
+ SELECT state INTO v_state
219
+ FROM cell.work_claim
220
+ WHERE work_claim_id = v_claim
221
+ FOR UPDATE;
222
+
223
+ -- Idempotency is the PREDICATE, not a set this verb remembers: a restart
224
+ -- takes a remembered set with it, which is the whole reason L2 puts durable
225
+ -- state below the agent. A recovery that finds the terminal already recorded
226
+ -- has nothing to do and says so.
227
+ IF v_state = 'completion_asserted' THEN
228
+ RETURN false;
229
+ END IF;
230
+
231
+ IF v_state IS DISTINCT FROM 'active' THEN
232
+ RAISE EXCEPTION
233
+ 'CLAIM_TERMINAL_CLAIM_ALREADY_ENDED:%:% — responsibility for this work '
234
+ 'already ended by another terminal, and claim terminals are not '
235
+ 'reversible by a second one arriving. Contract §1.2: responsibility '
236
+ 'passes by yield, abort, takeover, reassignment or organizational '
237
+ 'expiry, and a completion terminal written over one of those would '
238
+ 'record that the principal who was relieved of the work finished it.',
239
+ v_claim, coalesce(v_state, '<no such claim>')
240
+ USING ERRCODE = 'FC104';
241
+ END IF;
242
+
243
+ -- The terminal. Every value is the statement's own: `completed_at` is R2's
244
+ -- assertion stamp and `ownership_ended_at` is the same instant, because the
245
+ -- assertion IS the end of the author's responsibility (§1.2 as amended by
246
+ -- R2). `settled_at` is untouched and belongs to the evaluator seat (§1.6).
247
+ --
248
+ -- Nothing is set to authorize this write: THIS frame, on the call stack, is
249
+ -- what the guard above reads, and it stops existing when the function
250
+ -- returns — so nothing later in the transaction inherits the authority to
251
+ -- end a claim, without anybody having to remember to clear a setting.
252
+ UPDATE cell.work_claim
253
+ SET state = 'completion_asserted',
254
+ completed_at = v_asserted_at,
255
+ ownership_ended_at = v_asserted_at
256
+ WHERE work_claim_id = v_claim
257
+ AND state = 'active';
258
+
259
+ GET DIAGNOSTICS v_moved = ROW_COUNT;
260
+
261
+ -- The meaning event, in the SAME transaction as the row. A terminal visible
262
+ -- as a row but not as an event (or the reverse) is the split-brain 002's
263
+ -- claim transaction exists to make impossible, and this verb inherits the
264
+ -- rule rather than restating it: the producer trigger reads the same call
265
+ -- stack, so this INSERT is lawful here and nowhere else.
266
+ INSERT INTO cell.work_claim_event (event_id, org_id, work_claim_id, topic, ts,
267
+ producer)
268
+ VALUES (p_event_id, v_org, v_claim, 'work_claim.completion_asserted',
269
+ v_asserted_at, 'cell.assert_claim_completion');
270
+
271
+ RETURN v_moved = 1;
272
+ END;
273
+ $assert_completion$;
274
+
275
+ COMMENT ON FUNCTION cell.assert_claim_completion IS
276
+ 'Contract §1.2/R2: the one writer of a claim''s completion terminal. A '
277
+ 'TRANSCRIPTION of a recorded completion statement — org, task, claim and '
278
+ 'instant are all read off it — and never a settlement: `settled_at` is the '
279
+ 'evaluator seat''s (§1.6).';
@@ -0,0 +1,327 @@
1
+ -- 014_claim_settlement.sql — THE SETTLEMENT STAMP: the shared evaluator seat's
2
+ -- verb for the claim row, and the call-stack guard that makes it the only way in.
3
+ --
4
+ -- Contract: docs/a1-cell-work-spine-contract.md §1.6 (completion and settlement
5
+ -- are SEPARATE facts, with different owners), §1.3 and L10 (a completion
6
+ -- statement is an assertion, never truth), §1.4 (a verification is a non-author
7
+ -- verdict about a recorded statement), FOUNDER RULING R4 (release is performed
8
+ -- by the evaluator lane) and R8 (a rejection reopens work; it never settles it).
9
+ -- Requires 001_core.sql, 002_claim.sql (cell.written_by), 003_verification.sql
10
+ -- and 013_claim_terminal.sql (the terminal this settlement follows).
11
+ --
12
+ -- Re-apply: idempotent (CREATE OR REPLACE).
13
+ -- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
14
+ --
15
+ -- WHY THIS FILE EXISTS, MEASURED
16
+ --
17
+ -- 013 landed the COMPLETION half of §1.6 and said in its own comment that the
18
+ -- other half was somebody else's: "`settled_at` is untouched and belongs to the
19
+ -- evaluator seat (§1.6)". The seat did not have it. the cell schema
20
+ -- evaluator`'s fifth standing property was "No settlement fact on the claim —
21
+ -- nothing here touches `cell.work_claim`", and what that cost was measured
22
+ -- across a whole bake-off family: `SETTLE_LOST`, case 3's standing assertion
23
+ -- that a settled claim carries a `verified` verification recorded at or before
24
+ -- its `settled_at`, was red on ALL EIGHT legs against BOTH entrants and was
25
+ -- UNREACHABLE rather than unmet. `cell.work_claim` is on the one-writer census's
26
+ -- settlement list absolutely (A2.4-F0), so no candidate could write it; and no
27
+ -- verb existed for the seat, so waking the seat could not cause it either. The
28
+ -- red was the CELL's.
29
+ --
30
+ -- This file closes it in the shape 013 and 012 already established: a sanctioned
31
+ -- verb under a call-stack guard. A candidate may CAUSE this write by waking the
32
+ -- seat and may never PERFORM it, and `cell.settle_work_claim` joins the census's
33
+ -- SANCTIONED_SETTLEMENT_VERBS — the list of names no candidate's source may even
34
+ -- reach for.
35
+ --
36
+ -- WHY THIS IS NOT A CLAIM TERMINAL
37
+ --
38
+ -- Responsibility has already ended when this runs; §1.2's terminals are the
39
+ -- author's or the organization's acts and this is neither. So the verb writes
40
+ -- `settled_at` and NOTHING else — not `state`, not `completed_at`, not
41
+ -- `ownership_ended_at` — and it writes no `cell.work_claim_event`: 001_core.sql
42
+ -- declares six claim MEANINGS and settlement is not among them. A settlement
43
+ -- event would make settlement look like a seventh terminal to every reader of
44
+ -- that table.
45
+ --
46
+ -- WHY THERE IS NO SIGNER ARGUMENT
47
+ --
48
+ -- Because there is no signer column, and adding one would be the drift `cell.
49
+ -- v_dep_edge_release` exists to prevent: `cell.verification.verifier_principal`
50
+ -- already holds the name of whoever signed, and a copy of it on the claim row
51
+ -- would be a second place for "who settled this" to be answered differently. The
52
+ -- verdict this settlement rests on is addressable from the claim's own org and
53
+ -- task, which is what the refusals below read.
54
+ --
55
+ -- WHY THE INSTANT IS AN ARGUMENT AND THE VERDICT IS NOT
56
+ --
57
+ -- `p_settled_at` is the SEAT's, exactly as `cell.settle_task_edge`'s is: a lane
58
+ -- that transcribes says when it did so, and a server-chosen `now()` would be the
59
+ -- substrate deciding an instant that belongs in the caller's evidence. Every
60
+ -- other precondition is READ: the claim's terminal, and a standing `verified`
61
+ -- verification over the claim's own task. There is nowhere in the UPDATE below
62
+ -- to put a value a caller invented except that instant, and both orderings it
63
+ -- could get wrong are refused by name rather than by a bare CHECK violation.
64
+ --
65
+ -- Declared failure vocabulary (SQLSTATE class FC; this lane reserves
66
+ -- FC110-FC119):
67
+ -- FC110 CLAIM_SETTLEMENT_WRITTEN_OUTSIDE_THE_SANCTIONED_VERB a settlement
68
+ -- stamp moved onto a claim row by
69
+ -- something that is not this verb
70
+ -- FC111 CLAIM_SETTLEMENT_CLAIM_UNKNOWN a settlement of a claim the company
71
+ -- has no record of
72
+ -- FC112 CLAIM_SETTLEMENT_WITHOUT_A_CLAIM_TERMINAL a settlement of work whose
73
+ -- principal still holds it
74
+ -- FC113 CLAIM_SETTLEMENT_UNVERIFIED a settlement with no `verified`
75
+ -- verification standing over the
76
+ -- claim's own task at or before it
77
+ -- FC114 CLAIM_SETTLEMENT_PRECEDES_ITS_OWN_TERMINAL a settlement instant
78
+ -- earlier than the end of the
79
+ -- responsibility it settles
80
+ -- FC115 CLAIM_SETTLEMENT_SUPERSEDED_BY_A_SUCCESSOR a settlement of an interval
81
+ -- that handed the work on to a later one
82
+ -- FC116 CLAIM_SETTLEMENT_VERIFICATION_NOT_BOUND_TO_THIS_INTERVAL a successor
83
+ -- settled on a verdict about an earlier
84
+ -- interval's statement
85
+ --
86
+ -- The two codes above were ADDED 2026-08-25 — F3, the ownership-return §5.6,
87
+ -- FOUNDER CHECKPOINT **FC-C ruled S2** (R-A35-25 §5): settlement is stamped on
88
+ -- the interval that carried the work to DONE, and a predecessor's `settled_at`
89
+ -- stays NULL forever. They amend this file IN PLACE and take codes from its own
90
+ -- FC110-FC119 block rather than arriving from 027, because re-creating
91
+ -- cell.settle_work_claim out of another migration would split one verb's
92
+ -- definition across two files (§5.6.1's stated reason; 027's FC187 is released).
93
+ --
94
+ -- WHY A RETURN NEEDS TWO NEW REFUSALS AND NOT ONE
95
+ --
96
+ -- After R-A35-23's ownership return, ONE TASK CAN CARRY TWO INTERVALS: the
97
+ -- predecessor that handed the work back and the successor that resumed it. Both
98
+ -- of the checks below were correct while a task had one interval and are holes
99
+ -- the moment it has two.
100
+ --
101
+ -- FC115 — FC113's message says settlement exists to prevent "work marked done
102
+ -- by the thing that did it". A predecessor settling off a SUCCESSOR's
103
+ -- verification is the adjacent defect: work marked done FOR an interval that
104
+ -- did not do it and did not verify it, whose own recorded terminal says it
105
+ -- handed the work away. Nothing breaks under S2 — a claim with NULL
106
+ -- `settled_at` is exactly what an `aborted` or `taken_over` interval looks
107
+ -- like, and work_claim_settlement_follows_a_claim_terminal
108
+ -- (001_core.sql:182-184) permits the absence unconditionally.
109
+ --
110
+ -- FC116 — FC113 reads its verdict over the claim's own ORG AND TASK, which is
111
+ -- the right grain for a task with one interval and too WIDE for a task with
112
+ -- two: the predecessor's historical `verified` verification satisfies a
113
+ -- task-grain lookup and would settle a successor that has not yet said
114
+ -- anything. The narrowed predicate follows the verdict back to its author —
115
+ -- `verification.statement_id` → `agent_statement.work_claim_id` — which 003
116
+ -- makes NOT NULL precisely so a verdict always has a recorded statement to be
117
+ -- about. It applies to claims that HAVE a predecessor and to no others: an
118
+ -- ordinary claim's settlement rule is unchanged, because for it the two grains
119
+ -- select the same rows.
120
+
121
+ -- ------------------------------------------------------------- the writer guard
122
+ --
123
+ -- ONLY THE TRANSITION into a settled claim is guarded, and only on UPDATE — the
124
+ -- same two scopes 013's terminal guard carries, for the same two reasons. A row
125
+ -- that is ALREADY settled may be written for reasons that are not about
126
+ -- settlement, and an INSERT of a settled row is a SCENE declaring history rather
127
+ -- than a writer performing one (`cell.claim_work` always mints `active`, so
128
+ -- every settlement a live cell reaches is reached by UPDATE).
129
+ CREATE OR REPLACE FUNCTION cell.enforce_claim_settlement_is_the_evaluator_seats()
130
+ RETURNS trigger
131
+ LANGUAGE plpgsql
132
+ AS $settlement_writer$
133
+ BEGIN
134
+ IF NEW.settled_at IS NULL OR OLD.settled_at IS NOT NULL THEN
135
+ RETURN NEW;
136
+ END IF;
137
+
138
+ IF NOT cell.written_by('cell.settle_work_claim') THEN
139
+ RAISE EXCEPTION
140
+ 'CLAIM_SETTLEMENT_WRITTEN_OUTSIDE_THE_SANCTIONED_VERB:% — a claim is '
141
+ 'settled through cell.settle_work_claim and through nothing else. '
142
+ 'FOUNDER RULING R4 with contract §1.2: settlement is performed by the '
143
+ 'evaluator lane, never by the completing author''s runtime; §1.6 keeps '
144
+ 'it a separate fact from the completion terminal beside it. The writer '
145
+ 'signal is the PL/pgSQL call stack, which a direct writer cannot '
146
+ 'fabricate, rather than a column it could type: 23 historical '
147
+ '`verified` settles reached the bus by direct publish naming exactly '
148
+ 'the right evaluator.',
149
+ NEW.work_claim_id
150
+ USING ERRCODE = 'FC110';
151
+ END IF;
152
+
153
+ RETURN NEW;
154
+ END;
155
+ $settlement_writer$;
156
+
157
+ CREATE OR REPLACE TRIGGER work_claim_settlement_is_the_evaluator_seats
158
+ BEFORE UPDATE ON cell.work_claim
159
+ FOR EACH ROW EXECUTE FUNCTION cell.enforce_claim_settlement_is_the_evaluator_seats();
160
+
161
+ -- --------------------------------------------------------------------- the verb
162
+ --
163
+ -- Both arguments are required and neither has a default. It ANSWERS WHETHER THIS
164
+ -- CALL MOVED ANYTHING, like every verb in 012 and 013: "settled" and "already
165
+ -- settled" are different facts to a lane that has to write down what its wake
166
+ -- did, and a wake that settled nothing must be distinguishable in evidence from
167
+ -- one that never looked.
168
+ CREATE OR REPLACE FUNCTION cell.settle_work_claim(
169
+ p_work_claim_id uuid,
170
+ p_settled_at timestamptz
171
+ )
172
+ RETURNS boolean
173
+ LANGUAGE plpgsql
174
+ AS $settle_claim$
175
+ DECLARE
176
+ v_org text;
177
+ v_task text;
178
+ v_state text;
179
+ v_ended_at timestamptz;
180
+ v_settled timestamptz;
181
+ v_verified timestamptz;
182
+ v_predecessor uuid;
183
+ v_moved integer;
184
+ BEGIN
185
+ -- Lock the claim before reading it: two wakes on the same tick serialize
186
+ -- here, and the loser re-reads AFTER the winner has committed rather than
187
+ -- both seeing an unsettled row.
188
+ SELECT org_id, task_id, state, ownership_ended_at, settled_at,
189
+ predecessor_work_claim_id
190
+ INTO v_org, v_task, v_state, v_ended_at, v_settled, v_predecessor
191
+ FROM cell.work_claim
192
+ WHERE work_claim_id = p_work_claim_id
193
+ FOR UPDATE;
194
+
195
+ IF NOT FOUND THEN
196
+ RAISE EXCEPTION
197
+ 'CLAIM_SETTLEMENT_CLAIM_UNKNOWN:% — a settlement is an act on work the '
198
+ 'company has a record of. An UPDATE that matched nothing fires no '
199
+ 'trigger at all, which is how a settlement path comes to report '
200
+ 'success for a row it never touched.',
201
+ p_work_claim_id
202
+ USING ERRCODE = 'FC111';
203
+ END IF;
204
+
205
+ -- Idempotency is the PREDICATE, not a set this lane remembers: a restart
206
+ -- takes a remembered set with it, which is the whole of what L2 puts below
207
+ -- the agent.
208
+ IF v_settled IS NOT NULL THEN
209
+ RETURN false;
210
+ END IF;
211
+
212
+ -- F3, first half. Read BEFORE the terminal check, because "this interval
213
+ -- handed the work on" is a stronger and more specific fact than "this
214
+ -- interval is live", and an operator reading the log should be told which of
215
+ -- the two is true of the row they asked about.
216
+ IF EXISTS (SELECT 1 FROM cell.work_claim succ
217
+ WHERE succ.org_id = v_org
218
+ AND succ.predecessor_work_claim_id = p_work_claim_id) THEN
219
+ RAISE EXCEPTION
220
+ 'CLAIM_SETTLEMENT_SUPERSEDED_BY_A_SUCCESSOR:%/%:% — this interval was '
221
+ 'RESUMED: a later interval on the same work carries it, and the '
222
+ 'settlement stamp belongs to whichever interval carried the work to '
223
+ 'done (§5.6, FC-C ruled S2). Stamping it here would assert that this '
224
+ 'interval''s work was accepted, about an interval whose own recorded '
225
+ 'terminal says it handed the work away — the adjacent defect to the '
226
+ 'one FC113 exists to refuse.',
227
+ v_org, v_task, p_work_claim_id
228
+ USING ERRCODE = 'FC115';
229
+ END IF;
230
+
231
+ IF v_state = 'active' THEN
232
+ RAISE EXCEPTION
233
+ 'CLAIM_SETTLEMENT_WITHOUT_A_CLAIM_TERMINAL:% — settlement follows the '
234
+ 'end of responsibility (§1.6, and 001_core.sql''s '
235
+ 'work_claim_settlement_follows_a_claim_terminal). A live claim settled '
236
+ 'would record work as finished while its principal still holds it, and '
237
+ 'the refusal is named here rather than left to a CHECK whose message '
238
+ 'says nothing about who was asking.',
239
+ p_work_claim_id
240
+ USING ERRCODE = 'FC112';
241
+ END IF;
242
+
243
+ -- THE VERDICT, read over the claim's OWN org and task, at or before the
244
+ -- instant being stamped. `result = 'verified'` and not merely "a verification
245
+ -- exists": R8 makes a rejection reopen work rather than settle it, and a
246
+ -- lane that settled on any verdict would settle rejected work.
247
+ SELECT min(v.recorded_at) INTO v_verified
248
+ FROM cell.verification v
249
+ WHERE v.org_id = v_org
250
+ AND v.task_id = v_task
251
+ AND v.result = 'verified'
252
+ AND v.recorded_at <= p_settled_at;
253
+
254
+ IF v_verified IS NULL THEN
255
+ RAISE EXCEPTION
256
+ 'CLAIM_SETTLEMENT_UNVERIFIED:%/%:% — a settled claim carries a '
257
+ '`verified` verification recorded at or before its settled_at. §1.3 '
258
+ 'and L10 make the author''s completion statement an ASSERTION, so a '
259
+ 'claim settled on the strength of it alone is the Mac''s '
260
+ 'queue_task_state.closed — work marked done by the thing that did it — '
261
+ 'which is the single defect cell.verification exists to make '
262
+ 'impossible.',
263
+ v_org, v_task, p_settled_at
264
+ USING ERRCODE = 'FC113';
265
+ END IF;
266
+
267
+ -- F3, second half: a SUCCESSOR settles only on a verdict about ITS OWN
268
+ -- completion statement. Read after FC113 so a successor with no verdict at
269
+ -- all still gets the unverified refusal — "nobody verified this work" and
270
+ -- "the verdict is about an earlier interval" are different facts and a
271
+ -- caller keying on the code must be able to tell them apart.
272
+ IF v_predecessor IS NOT NULL
273
+ AND NOT EXISTS (SELECT 1
274
+ FROM cell.verification v
275
+ JOIN cell.agent_statement s
276
+ ON s.statement_id = v.statement_id
277
+ WHERE v.org_id = v_org
278
+ AND v.task_id = v_task
279
+ AND v.result = 'verified'
280
+ AND v.recorded_at <= p_settled_at
281
+ AND s.work_claim_id = p_work_claim_id) THEN
282
+ RAISE EXCEPTION
283
+ 'CLAIM_SETTLEMENT_VERIFICATION_NOT_BOUND_TO_THIS_INTERVAL:%/%:% — '
284
+ 'this interval RESUMED % and the only `verified` verdicts standing '
285
+ 'over this task are about an earlier interval''s statement. After a '
286
+ 'return one task carries two intervals, so a task-grain lookup can no '
287
+ 'longer tell whose work was accepted: the verdict that settles an '
288
+ 'interval is the one recorded against a statement that interval made.',
289
+ v_org, v_task, p_work_claim_id, v_predecessor
290
+ USING ERRCODE = 'FC116';
291
+ END IF;
292
+
293
+ IF p_settled_at < v_ended_at THEN
294
+ RAISE EXCEPTION
295
+ 'CLAIM_SETTLEMENT_PRECEDES_ITS_OWN_TERMINAL:%:% — the instant a claim '
296
+ 'is settled is at or after the instant responsibility for it ended '
297
+ '(§1.6). Stamping earlier would settle work somebody still held, and '
298
+ 'the constraint that would otherwise catch it says nothing about which '
299
+ 'of the two orderings was wrong.',
300
+ p_work_claim_id, p_settled_at
301
+ USING ERRCODE = 'FC114';
302
+ END IF;
303
+
304
+ -- Nothing is set to authorize this write: THIS frame, on the call stack, is
305
+ -- what the guard above reads, and it stops existing when the function
306
+ -- returns — so nothing later in the transaction inherits the authority to
307
+ -- settle a claim, without anybody having to remember to clear a setting.
308
+ UPDATE cell.work_claim
309
+ SET settled_at = p_settled_at
310
+ WHERE work_claim_id = p_work_claim_id
311
+ AND settled_at IS NULL;
312
+
313
+ GET DIAGNOSTICS v_moved = ROW_COUNT;
314
+ RETURN v_moved = 1;
315
+ END;
316
+ $settle_claim$;
317
+
318
+ COMMENT ON FUNCTION cell.settle_work_claim IS
319
+ 'Contract §1.6 / FOUNDER RULING R4: the one writer of a claim''s settlement '
320
+ 'stamp, held by the shared evaluator seat. Never a claim terminal — `state`, '
321
+ '`completed_at` and `ownership_ended_at` are 013''s and are untouched here — '
322
+ 'and never written without a `verified` verification standing over the '
323
+ 'claim''s own task at or before the instant stamped. After R-A35-23''s '
324
+ 'ownership return a task may carry two intervals, so it also refuses to '
325
+ 'settle an interval that was RESUMED (FC115, FC-C ruled S2) and refuses to '
326
+ 'settle a SUCCESSOR on a verdict about an earlier interval''s statement '
327
+ '(FC116).';