@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,66 @@
1
+ -- 023_executor_role.sql — the executor database role: the A3 ENTRY GATE.
2
+ --
3
+ -- Contract: docs/a3-entry.md "Entry gate (inherited, RULED)" (signed assumption
4
+ -- 6) + a3-verification-strategy.md §3.2 row RT-ROLE-DENIED. A2 proved the
5
+ -- executor's SOURCE does not write settlement (a source-half proof). This gate
6
+ -- converts that into "the executor CANNOT" — a substrate-enforced privilege
7
+ -- boundary. Applied LAST so every settlement table it revokes already exists.
8
+ --
9
+ -- Re-apply: idempotent (DO-guarded CREATE ROLE; GRANT/REVOKE are declarative).
10
+ -- Revert: DROP OWNED BY cell_executor; DROP ROLE cell_executor;
11
+ --
12
+ -- THE SETTLEMENT SURFACE, AND THE ANTI-VACUITY HALF
13
+ --
14
+ -- The settlement surface is where a completion or a verdict becomes the company's
15
+ -- truth: cell.verification (the non-author verdict) and cell.work_claim (the
16
+ -- claim terminal and its settlement stamp). The executor role gets NO write there.
17
+ --
18
+ -- But "cannot write settlement" proves nothing if the role cannot write ANYTHING
19
+ -- — a role denied everything denies settlement vacuously. So the role is granted
20
+ -- exactly the writes the executor legitimately makes — an execution_attempt is
21
+ -- its own disposable runtime binding (R1), and an agent_statement is what an
22
+ -- agent SAYS (never truth, L10) — and those INSERTs SUCCEED. The gate's falsifier
23
+ -- runs a settlement write under this role expecting DENIED, and a permitted write
24
+ -- under the same role expecting ACCEPTED, so the denial is proven to be a real
25
+ -- privilege boundary and not the role being inert.
26
+
27
+ -- The guard is BOTH a look-before and a catch-after, deliberately. Roles are
28
+ -- CLUSTER-global while this file is applied per-database, so the check and the
29
+ -- CREATE are not atomic against a concurrent applier: two scratch cells loading
30
+ -- the DDL at the same instant — which is the normal case, the suites run nine
31
+ -- appliers in parallel — can both see the role absent and both try to create it.
32
+ -- The loser gets `duplicate_object`, and an idempotent migration must not fail on
33
+ -- having already got what it wanted. Only that one condition is swallowed.
34
+ DO $executor_role$
35
+ BEGIN
36
+ IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'cell_executor') THEN
37
+ -- NOLOGIN: a privilege template a connection SETs to, not a login of its
38
+ -- own. The deploy binds a login role to it; the cell owns the boundary.
39
+ CREATE ROLE cell_executor NOLOGIN;
40
+ END IF;
41
+ EXCEPTION
42
+ WHEN duplicate_object THEN NULL; -- another applier won the race; that is the outcome we wanted
43
+ END
44
+ $executor_role$;
45
+
46
+ -- Reach the schema and read all of it — the executor compiles context and reads
47
+ -- the spine constantly; reading is never the boundary.
48
+ GRANT USAGE ON SCHEMA cell TO cell_executor;
49
+ GRANT SELECT ON ALL TABLES IN SCHEMA cell TO cell_executor;
50
+
51
+ -- The writes the executor legitimately makes (the anti-vacuity half): its own
52
+ -- runtime binding, and what it SAYS. Neither is a settlement fact.
53
+ GRANT INSERT ON cell.execution_attempt TO cell_executor;
54
+ GRANT INSERT ON cell.agent_statement TO cell_executor;
55
+
56
+ -- The boundary. No write on the settlement surface, for this role — stated as an
57
+ -- explicit REVOKE as well as an absent GRANT, so a future GRANT ON ALL TABLES
58
+ -- cannot silently re-open it and the boundary reads as a deliberate act.
59
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.verification FROM cell_executor;
60
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.work_claim FROM cell_executor;
61
+
62
+ COMMENT ON ROLE cell_executor IS
63
+ 'A3 entry gate (signed assumption 6): the executor runs work but CANNOT write '
64
+ 'the settlement surface (cell.verification, cell.work_claim). It may write its '
65
+ 'own execution_attempt and agent_statement — the anti-vacuity half that proves '
66
+ 'the settlement denial is a real boundary (RT-ROLE-DENIED).';
@@ -0,0 +1,439 @@
1
+ -- 024_executor_role_hardening.sql — the executor's boundary stated in full, and
2
+ -- the ONE door left open through it.
3
+ --
4
+ -- Contract: docs/a3-entry.md "Entry gate (inherited, RULED)" (signed assumption
5
+ -- 6) + a3-verification-strategy.md §3.2 row RT-ROLE-DENIED, carried forward to
6
+ -- the A3.4 capability surface (R-A3.4-2: INTENT / IDENTITY / RECONCILIATION are
7
+ -- the three things A3.4 makes the runtime responsible for, and all three are
8
+ -- readable-not-writable for the role that runs the work).
9
+ --
10
+ -- AMENDS, NEVER REWRITES. 023 (the gate) and 008 (the R3 effect verbs) are
11
+ -- shipped. This file changes their effect with GRANT / REVOKE / ALTER FUNCTION
12
+ -- rather than editing them, because a migration that rewrites its predecessors
13
+ -- leaves a history nobody can read: the reviewer of 008 would be reading a file
14
+ -- that never applied in the shape it now claims. ALTER FUNCTION rather than
15
+ -- CREATE OR REPLACE for the same reason one level down — restating those bodies
16
+ -- here would create a second copy of the R3 rule to drift against the first.
17
+ -- Requires 008_effect_receipt.sql and 023_executor_role.sql. Applied LAST.
18
+ --
19
+ -- Re-apply: idempotent (CREATE OR REPLACE; GRANT / REVOKE / ALTER FUNCTION are
20
+ -- all declarative).
21
+ -- Revert: re-apply 008_effect_receipt.sql (it CREATE OR REPLACEs the unfenced,
22
+ -- invoker-rights cell.claim_effect_receipt over this file's);
23
+ -- ALTER FUNCTION cell.confirm_effect_receipt(text,text,text,text,timestamptz)
24
+ -- SECURITY INVOKER RESET search_path;
25
+ -- GRANT EXECUTE ON FUNCTION cell.claim_effect_receipt(text,text,uuid,uuid,text,text,text,text,timestamptz),
26
+ -- cell.confirm_effect_receipt(text,text,text,text,timestamptz) TO PUBLIC;
27
+ -- (the REVOKEs below need no revert — 023 never granted what they revoke)
28
+ --
29
+ -- Declared failure vocabulary. This file re-owns cell.claim_effect_receipt (see
30
+ -- THE FENCE MOVES INTO THE VERB, below), so it carries the WHOLE of that verb's
31
+ -- vocabulary — the two codes it adds and the two it inherits from 008 verbatim.
32
+ -- It stays inside the RECEIPT lane's own block, FC080-FC089, which 008 opened,
33
+ -- rather than taking a block of its own: every one of these is a refusal by 008's
34
+ -- claim verb about 008's receipt, and a consumer keying on the code reads ONE
35
+ -- lane, not two.
36
+ -- FC080 RECEIPT_KEY_PAYLOAD_CONFLICT a reused key with a different payload (from 008)
37
+ -- FC081 RECEIPT_KEY_ADDRESS_CONFLICT a reused key aimed somewhere else (from 008)
38
+ -- FC084 RECEIPT_ATTEMPT_NOT_CURRENT the presented attempt is not the claim's running one
39
+ -- FC085 RECEIPT_MULTIPLE_RUNNING_ATTEMPTS the claim holds more than one running attempt
40
+ --
41
+ -- THE DUPLICATION THAT CREATES, NAMED. After this file applies, 008's copy of the
42
+ -- claim body NEVER RUNS — this one replaces it. 008 keeps its copy because it is
43
+ -- the revert path (re-applying 008 restores the unfenced invoker-rights verb) and
44
+ -- because a migration that guts its predecessor leaves no history to read. The
45
+ -- live risk is an edit to FC080/FC081 landing in 008 and being silently
46
+ -- overwritten here. RETIREMENT CONDITION: when the receipt lane is next opened for
47
+ -- a semantic change, 008 and 024 collapse into one file and this note goes with
48
+ -- the second copy. DELETION OWNER: whoever makes that change.
49
+ --
50
+ --
51
+ -- THE CAPABILITY SURFACE, AND WHY THESE FIVE
52
+ --
53
+ -- 023 closed the SETTLEMENT surface: cell.verification (the non-author verdict)
54
+ -- and cell.work_claim (the terminal and its settlement stamp). A3.4 gives the
55
+ -- runtime real work to do, and that work runs alongside five more tables whose
56
+ -- rows are not settlement but are still not the executor's to write:
57
+ --
58
+ -- cell.dep_edge what work waits on what. An executor that could
59
+ -- declare or settle an edge could release its own
60
+ -- gate — §4.1's lifecycle would be advisory.
61
+ -- cell.delegation_grant who may hand work across a scope boundary
62
+ -- (PRD §13.4). Writing it is granting yourself
63
+ -- reach, which is L11 exactly: improving HOW a
64
+ -- capability works never widens WHAT it may do.
65
+ -- cell.task_delegation the delegated-work record R8's parent gate reads.
66
+ -- Same authority, one table over.
67
+ -- cell.verification_clause the acceptance clause a verdict is measured
68
+ -- against (022). Editing the bar is passing the
69
+ -- test by moving it.
70
+ -- cell.effect_receipt the durable pre-image of an irreversible external
71
+ -- effect (008, FOUNDER RULING R3). A direct writer
72
+ -- here can mint a receipt for an effect that never
73
+ -- fired, or erase the one record a reconciler had.
74
+ --
75
+ -- WHY EXPLICIT, WHEN THE GRANT WAS NEVER THERE — AND WHAT THAT DOES NOT PROVE
76
+ --
77
+ -- 023 (line 47) states the reason for its own explicit REVOKEs: so a future
78
+ -- `GRANT ... ON ALL TABLES` cannot silently re-open the boundary, and so the
79
+ -- boundary reads as a deliberate act rather than an omission somebody might
80
+ -- "fix". The same reasoning covers these five, and the same honesty is owed
81
+ -- about its limit: PostgreSQL records no NEGATIVE grant. Revoking a privilege a
82
+ -- role never held writes nothing at all to pg_class.relacl, so no catalog query
83
+ -- can distinguish "revoked" from "never granted", and any test claiming to read
84
+ -- the revoke off the catalog would be reading 023's GRANT SELECT instead.
85
+ --
86
+ -- What IS falsifiable is the scenario the statement exists for, and the suite
87
+ -- runs exactly that: widen every table at once with `GRANT ALL ON ALL TABLES IN
88
+ -- SCHEMA cell TO cell_executor`, observe the surface open, re-apply this file,
89
+ -- observe it closed — inside a rolled-back transaction, so the widening leaves
90
+ -- no residue. That is the difference between belt-and-suspenders and prose.
91
+
92
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.dep_edge FROM cell_executor;
93
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.delegation_grant FROM cell_executor;
94
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.task_delegation FROM cell_executor;
95
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.verification_clause FROM cell_executor;
96
+ REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON cell.effect_receipt FROM cell_executor;
97
+
98
+ -- ------------------------------------------------------- the one door, and its lock
99
+ --
100
+ -- THE ANTI-VACUITY HALF, at the surface A3.4 actually runs on. A role that
101
+ -- cannot record that an effect was about to fire does not have a boundary; it
102
+ -- has a dead lane — and worse, R3's `unknown` becomes permanent by construction
103
+ -- for every effect the executor performs, which is the precise failure 008 was
104
+ -- written to prevent. So the SAME role that cannot touch cell.effect_receipt
105
+ -- must still be able to claim and confirm a receipt through the committed verbs.
106
+ --
107
+ -- MEASURED, before this file (scratch cell, DDL 001..023, role cell_executor):
108
+ -- prosecdef(claim_effect_receipt) = false
109
+ -- prosecdef(confirm_effect_receipt) = false
110
+ -- SELECT cell.claim_effect_receipt(...) -> 42501 permission denied for
111
+ -- table effect_receipt
112
+ -- No function anywhere in this DDL tree was definer-rights. These two are the
113
+ -- first, and they are definer-rights for a reason that is the opposite of
114
+ -- convenience: it is what makes "the EXECUTOR writes receipts ONLY through these
115
+ -- verbs" a substrate fact rather than a calling convention. Said precisely: the
116
+ -- door is role-scoped. It is the only door for a caller with no direct DML on
117
+ -- cell.effect_receipt — which is every caller inside the executor boundary — and
118
+ -- it is one door among the owner's several, because the owner has the table.
119
+ --
120
+ -- The verbs own the idempotency key, the payload/address conflict refusals
121
+ -- (FC080/FC081), the no-claim-behind-it refusal (FC082) and the contradiction
122
+ -- refusal (FC083); a caller with direct INSERT rights owns none of them.
123
+ --
124
+ -- DEPLOY REQUIREMENT — LEDGERED FOR THE A5 DEPLOY CHECKLIST, and stated here so
125
+ -- it travels with the DDL rather than with a report: definer rights inherit the
126
+ -- OWNER's privileges, so cell.claim_effect_receipt and cell.confirm_effect_receipt
127
+ -- MUST be owned by the cell's schema owner and NEVER by a superuser in a real
128
+ -- cell — a superuser-owned definer function hands the executor superuser reach
129
+ -- for the duration of the call. Nothing in this file can enforce that (the owner
130
+ -- is whoever applies it), and the scratch databases these suites run in are
131
+ -- superuser-owned, which is why the test asserts the search_path pin instead.
132
+ --
133
+ -- search_path is pinned because an unpinned definer function is the classic
134
+ -- escalation shape: a caller sets search_path, and an unqualified reference
135
+ -- inside the body resolves to an object the caller planted. Both bodies
136
+ -- schema-qualify everything they touch, so pg_catalog alone would run them —
137
+ -- `cell` is listed anyway so a later edit that forgets to qualify resolves to
138
+ -- the cell's own object rather than silently to whatever `public` holds, and
139
+ -- `public` is deliberately absent. pg_temp is last, never first.
140
+ --
141
+ --
142
+ -- THE FENCE MOVES INTO THE VERB (ORCHESTRATOR RULING, A3.4 Task 3 fix round 1)
143
+ --
144
+ -- A3.4 Task 1 closed the stale-attempt TOCTOU on the CALLER side: performEffect
145
+ -- opened a transaction, took `FOR SHARE` on the claim's running attempt row, and
146
+ -- claimed the receipt before committing. An earlier draft of THIS file recorded
147
+ -- the decision NOT to restate that check verb-side, on L1 grounds — one authority
148
+ -- over one question.
149
+ --
150
+ -- THAT DECISION IS REVERSED HERE, and the caller-side lock path is DELETED rather
151
+ -- than duplicated, because the fact it rested on turned out to be false. MEASURED
152
+ -- (scratch cell, 001..023, role cell_executor):
153
+ --
154
+ -- SELECT ... FROM cell.execution_attempt ... FOR SHARE
155
+ -- -> 42501 permission denied for table execution_attempt
156
+ -- FOR KEY SHARE -> 42501 plain SELECT (no lock) -> OK
157
+ -- FOR SHARE + GRANT UPDATE -> OK (column-level UPDATE is enough too)
158
+ --
159
+ -- PostgreSQL checks EVERY row-locking clause against UPDATE privilege on the
160
+ -- table (ACL_SELECT_FOR_UPDATE is ACL_UPDATE). cell_executor holds SELECT and
161
+ -- INSERT on cell.execution_attempt and nothing more, so the caller-side fence
162
+ -- could not run at all under the role that runs the work — the lane was blocked
163
+ -- one step EARLIER than the receipt denial this file was written to repair. The
164
+ -- only caller-side fix is `GRANT UPDATE ON cell.execution_attempt TO
165
+ -- cell_executor`, which hands the executor its own attempt's state,
166
+ -- outcome_status and reconcile_by. That is the trade this whole file refuses.
167
+ --
168
+ -- So the fence goes where the state it guards is owned: INSIDE the definer-rights
169
+ -- claim verb, which runs as the OWNER and therefore may take the lock. L1 is
170
+ -- satisfied by DELETION, not by duplication — there is now exactly ONE fence, and
171
+ -- it is this one. It is also STRONGER than what it replaces: the lock, the
172
+ -- currency test and the INSERT are one statement, so no window exists between
173
+ -- them at all, and no caller can forget to open the transaction that made the
174
+ -- old one work.
175
+ --
176
+ -- The refusals are typed in 008's own vocabulary (FC080-FC089), because they are
177
+ -- refusals of 008's claim about 008's receipt:
178
+ -- FC084 the presented attempt is not the claim's single running attempt — it
179
+ -- ended, it belongs to another claim, or the claim has none running
180
+ -- FC085 the claim holds MORE THAN ONE running attempt. Currency is
181
+ -- unanswerable, so the verb fails closed rather than picking a winner;
182
+ -- a coin toss dressed as a fact is the one answer worse than a refusal.
183
+ --
184
+ -- THE FENCE IS ROLE-SCOPED, AND THE COVERAGE STATEMENT THAT OWES (L9)
185
+ --
186
+ -- ENFORCED ON 100% OF EXECUTOR-ROLE CLAIMS. ENFORCED ON 0% OF OWNER-LANE CLAIMS,
187
+ -- BY DESIGN. That is the whole coverage number, stated up front, because a
188
+ -- blocking rule that does not publish what it is actually enforced on is L9's
189
+ -- unpublishable coverage — and this one is deliberately not universal.
190
+ --
191
+ -- WHY. The fence's threat model is a superseded SCOPE still firing effects: an
192
+ -- agent process that outlived its ExecutionAttempt, holding a connection whose
193
+ -- ONLY path to cell.effect_receipt is this verb. A caller that already holds
194
+ -- direct INSERT on the table is not fenced by anything this verb does — it can
195
+ -- write the row itself — so applying the check to it buys no security whatsoever.
196
+ --
197
+ -- And it costs something real. MEASURED: the SUPERVISOR lane (the page, journal
198
+ -- and reconciler workflows that run beside the executor, not inside it) connects
199
+ -- as the OWNER with no SET ROLE, and its page lane claims a receipt for an
200
+ -- escalation AFTER the attempt has reached its terminal state — post-terminal page
201
+ -- attribution, which is A2 PRODUCTION-PROVEN behaviour and exactly what the
202
+ -- harness matrix's C6-SIGSTOP leg builds. An unconditional fence refused that
203
+ -- seed outright:
204
+ --
205
+ -- LEG_SEED_FAILED:C6-SIGSTOP — ERROR: RECEIPT_ATTEMPT_NOT_CURRENT:... attempt
206
+ -- 253be835 (recorded state interrupted) is not the running attempt of work
207
+ -- claim 99de565e (running: <none>)
208
+ --
209
+ -- So the fence applies to the callers it is FOR. Attribution is unchanged for
210
+ -- both lanes — the receipt still names the attempt the caller passed — and no
211
+ -- authority is widened: the owner lane could always write this table, and the
212
+ -- executor lane still cannot.
213
+ --
214
+ -- AND THAT COVERAGE IS SELF-ASSERTED, NOT ASSUMED. Because the scope of this
215
+ -- fence is a property of GRANTS, a later `GRANT INSERT ON cell.effect_receipt`
216
+ -- to the effect sidecar's login — or a COLUMN-level grant on one column, which
217
+ -- has_table_privilege cannot see — would take that sidecar outside the fence
218
+ -- while every suite stayed green. So the governed-effect server evaluates this
219
+ -- same predicate FOR ITSELF at connect time (has_table_privilege OR
220
+ -- has_any_column_privilege) and refuses to serve when the answer is "unfenced",
221
+ -- with the frozen typed code EFFECT_PRINCIPAL_UNFENCED, recording the resolved
222
+ -- principal in its startup line. The coverage number below is therefore checked
223
+ -- against the REAL caller on every run rather than asserted by intent.
224
+ --
225
+ -- HOW THE CALLER IS CLASSIFIED, AND A DEVIATION STATED. The ruling named
226
+ -- `pg_has_role(session_user, 'cell_executor', 'MEMBER')`. MEASURED, that
227
+ -- predicate cannot do the job in the environment the cell is tested in:
228
+ --
229
+ -- pg_has_role(session_user, cell_executor, MEMBER) = true
230
+ -- pg_has_role(current_user, cell_executor, MEMBER) = true
231
+ --
232
+ -- pg_has_role is TRUE for a superuser against EVERY role, and every scratch cell
233
+ -- these suites run in is superuser-owned — so a role-name predicate fences the
234
+ -- owner lane too, the C6-SIGSTOP leg stays red, and the check becomes untestable
235
+ -- in the only environment that can test it.
236
+ --
237
+ -- What is used instead is the ruling's own RATIONALE, stated as the fact it rests
238
+ -- on rather than as a proxy for it: fence exactly those callers for whom this
239
+ -- verb is the ONLY door — `NOT has_table_privilege(<caller>, 'cell.effect_receipt',
240
+ -- 'INSERT')`. It classifies the executor correctly (no INSERT -> fenced), the
241
+ -- owner correctly (INSERT -> not fenced) whether or not the owner is a superuser,
242
+ -- and a future read-only lane correctly without having to be told its name. See
243
+ -- WHO IS CALLING in the body for how `<caller>` is recovered, which is the part
244
+ -- that is genuinely subtle.
245
+
246
+ -- Everything else about the verb — the ON CONFLICT DO NOTHING, the boolean that
247
+ -- tells "I claimed it" from "somebody already did", FC080 and FC081 — is 008's,
248
+ -- restated verbatim (see THE DUPLICATION THAT CREATES, in the header). This file
249
+ -- changes WHEN the insert is allowed to happen, not what it means.
250
+
251
+ CREATE OR REPLACE FUNCTION cell.claim_effect_receipt(
252
+ p_org_id text,
253
+ p_receipt_key text,
254
+ p_work_claim_id uuid,
255
+ p_attempt_id uuid,
256
+ p_bus_event_ref text,
257
+ p_connector text,
258
+ p_address text,
259
+ p_payload_hash text,
260
+ p_claimed_at timestamptz
261
+ )
262
+ RETURNS boolean
263
+ LANGUAGE plpgsql
264
+ SECURITY DEFINER
265
+ SET search_path = pg_catalog, cell, pg_temp
266
+ AS $claim_receipt$
267
+ DECLARE
268
+ v_claimed boolean := false;
269
+ v_payload_hash text;
270
+ v_address text;
271
+ v_connector text;
272
+ v_running uuid[];
273
+ v_presented_state text;
274
+ v_caller name;
275
+ BEGIN
276
+ -- WHO IS CALLING. Harder than it looks inside a SECURITY DEFINER function,
277
+ -- and got wrong once here, so the measurement is written down:
278
+ --
279
+ -- current_user session_user current_setting('role')
280
+ -- owner lane <owner> <owner> none
281
+ -- after SET ROLE cell_executor <owner> <owner> cell_executor
282
+ --
283
+ -- `current_user` is the function OWNER for the whole body — that is what
284
+ -- definer rights MEAN — so it cannot see the caller at all. `session_user` is
285
+ -- the LOGIN, which SET ROLE does not change, so in any cell whose owner logs
286
+ -- in as a superuser (every scratch cell these suites use) it cannot tell the
287
+ -- two lanes apart either. The `role` GUC survives the definer switch and is
288
+ -- the one thing that does. A deploy where the executor has its OWN login and
289
+ -- never issues SET ROLE reads 'none' here, and falls back to session_user,
290
+ -- which is then correct: that login inherits cell_executor and holds no
291
+ -- INSERT on the receipt table.
292
+ v_caller := coalesce(nullif(current_setting('role', true), 'none'), session_user);
293
+
294
+ -- THE FENCE, SCOPED TO THE CALLERS IT IS FOR. See THE FENCE IS ROLE-SCOPED,
295
+ -- above: a caller that may INSERT into cell.effect_receipt directly is not
296
+ -- fenced by anything this verb does, and fencing it would only break the
297
+ -- supervisor lane's post-terminal writes for no security at all.
298
+ IF NOT has_table_privilege(v_caller, 'cell.effect_receipt', 'INSERT') THEN
299
+ -- FOR SHARE on the claim's running attempt row(s), held until this
300
+ -- statement's transaction ends — which, for a bare SELECT of this
301
+ -- function, is the statement itself. FOR SHARE and not FOR UPDATE: the
302
+ -- fence is a reader, and must serialise only against the writer that
303
+ -- would END the attempt. org_id leads the predicate so it can use
304
+ -- execution_attempt's (org_id, ...) index prefix rather than scanning.
305
+ SELECT array_agg(a.attempt_id)
306
+ INTO v_running
307
+ FROM (SELECT ea.attempt_id
308
+ FROM cell.execution_attempt ea
309
+ WHERE ea.org_id = p_org_id
310
+ AND ea.work_claim_id = p_work_claim_id
311
+ AND ea.state = 'running'
312
+ ORDER BY ea.attempt_id
313
+ FOR SHARE) a;
314
+
315
+ IF v_running IS NOT NULL AND array_length(v_running, 1) > 1 THEN
316
+ RAISE EXCEPTION
317
+ 'RECEIPT_MULTIPLE_RUNNING_ATTEMPTS:% — work claim % holds % running '
318
+ 'attempts (%), so which one owns this effect is unanswerable. The '
319
+ 'fence fails closed rather than choosing: an irreversible effect '
320
+ 'attributed to a guessed attempt is a receipt no reconciler can trust.',
321
+ p_receipt_key, p_work_claim_id, array_length(v_running, 1), v_running
322
+ USING ERRCODE = 'FC085';
323
+ END IF;
324
+
325
+ IF v_running IS NULL OR v_running[1] IS DISTINCT FROM p_attempt_id THEN
326
+ SELECT ea.state INTO v_presented_state
327
+ FROM cell.execution_attempt ea
328
+ WHERE ea.org_id = p_org_id
329
+ AND ea.work_claim_id = p_work_claim_id
330
+ AND ea.attempt_id = p_attempt_id;
331
+ RAISE EXCEPTION
332
+ 'RECEIPT_ATTEMPT_NOT_CURRENT:% — attempt % (recorded state %) is not '
333
+ 'the running attempt of work claim % (running: %). A survivor of a '
334
+ 'superseded attempt must not claim a receipt: it would leave a durable '
335
+ 'pre-image for an effect nobody authorised, attributed to a binding '
336
+ 'that no longer owns the work.',
337
+ p_receipt_key, p_attempt_id, coalesce(v_presented_state, '<not recorded>'),
338
+ p_work_claim_id, coalesce(v_running::text, '<none>')
339
+ USING ERRCODE = 'FC084';
340
+ END IF;
341
+ END IF;
342
+
343
+ -- 008's claim, unchanged from here down.
344
+ INSERT INTO cell.effect_receipt (
345
+ org_id, receipt_key, work_claim_id, attempt_id, bus_event_ref,
346
+ connector, address, payload_hash, claimed_at, confirmed_at, outcome,
347
+ remote_ref
348
+ ) VALUES (
349
+ p_org_id, p_receipt_key, p_work_claim_id, p_attempt_id, p_bus_event_ref,
350
+ p_connector, p_address, p_payload_hash, p_claimed_at, NULL, 'claimed',
351
+ NULL
352
+ )
353
+ ON CONFLICT (org_id, receipt_key) DO NOTHING;
354
+
355
+ GET DIAGNOSTICS v_claimed = ROW_COUNT;
356
+ IF v_claimed THEN
357
+ RETURN true;
358
+ END IF;
359
+
360
+ SELECT payload_hash, address, connector
361
+ INTO v_payload_hash, v_address, v_connector
362
+ FROM cell.effect_receipt
363
+ WHERE org_id = p_org_id AND receipt_key = p_receipt_key;
364
+
365
+ IF v_payload_hash IS DISTINCT FROM p_payload_hash THEN
366
+ RAISE EXCEPTION
367
+ 'RECEIPT_KEY_PAYLOAD_CONFLICT:% — this key already holds a receipt for '
368
+ 'a DIFFERENT payload (recorded %, offered %). Overwriting it would '
369
+ 'leave the reconciler reading a receipt for an effect that was never '
370
+ 'fired with those contents; a key that can be re-pointed at another '
371
+ 'payload is not an idempotency key.',
372
+ p_receipt_key, v_payload_hash, p_payload_hash
373
+ USING ERRCODE = 'FC080';
374
+ END IF;
375
+
376
+ IF v_address IS DISTINCT FROM p_address OR v_connector IS DISTINCT FROM p_connector THEN
377
+ RAISE EXCEPTION
378
+ 'RECEIPT_KEY_ADDRESS_CONFLICT:% — this key already holds a receipt '
379
+ 'aimed at %/%, and this call aims at %/%. Same reasoning as the '
380
+ 'payload clause, one field over.',
381
+ p_receipt_key, v_connector, v_address, p_connector, p_address
382
+ USING ERRCODE = 'FC081';
383
+ END IF;
384
+
385
+ -- An identical re-claim: the effect was already claimed by this or an
386
+ -- earlier attempt, and the caller must NOT fire again.
387
+ RETURN false;
388
+ END;
389
+ $claim_receipt$;
390
+
391
+ ALTER FUNCTION cell.confirm_effect_receipt(text, text, text, text, timestamptz)
392
+ SECURITY DEFINER
393
+ SET search_path = pg_catalog, cell, pg_temp;
394
+
395
+ -- Definer rights that PUBLIC may execute are definer rights handed to every role
396
+ -- in the cluster — the door would be open, just differently. EXECUTE goes to the
397
+ -- one role that needs it, and to nobody else.
398
+ REVOKE EXECUTE ON FUNCTION
399
+ cell.claim_effect_receipt(text, text, uuid, uuid, text, text, text, text, timestamptz),
400
+ cell.confirm_effect_receipt(text, text, text, text, timestamptz)
401
+ FROM PUBLIC;
402
+
403
+ GRANT EXECUTE ON FUNCTION
404
+ cell.claim_effect_receipt(text, text, uuid, uuid, text, text, text, text, timestamptz),
405
+ cell.confirm_effect_receipt(text, text, text, text, timestamptz)
406
+ TO cell_executor;
407
+
408
+ COMMENT ON FUNCTION cell.claim_effect_receipt(
409
+ text, text, uuid, uuid, text, text, text, text, timestamptz) IS
410
+ 'FOUNDER RULING R3, claim-before-fire. SECURITY DEFINER (024): the executor '
411
+ 'role has NO direct DML on cell.effect_receipt, so this verb and its confirm '
412
+ 'are the only path an executor has to a receipt — and the FC080/FC081 '
413
+ 'conflict refusals therefore cannot be walked around. The attempt fence '
414
+ '(FC084/FC085) is enforced on 100%% of claims by callers without direct '
415
+ 'INSERT on cell.effect_receipt and on 0%% of owner-lane claims, by design '
416
+ '(L9 coverage): a caller that can write the row is not fenced by this verb. '
417
+ 'The effect sidecar checks that predicate against ITSELF at connect and '
418
+ 'refuses to serve unfenced (EFFECT_PRINCIPAL_UNFENCED), so the coverage is '
419
+ 'self-asserted against the real caller rather than assumed.';
420
+
421
+ -- ------------------------------------------- what this file deliberately does NOT do
422
+ --
423
+ -- cell.unresolved_receipts STAYS INVOKER-RIGHTS. It is a reader, and 023's first
424
+ -- ruling is that reading is never the boundary: cell_executor already holds
425
+ -- SELECT on every table in this schema, so R6's MID-EFFECT-HOLD read and R3's
426
+ -- reconciler set both work as the invoker. Making it definer would buy nothing
427
+ -- and cost the property that matters — a future revocation of the executor's
428
+ -- SELECT would then be invisible through this function, and an enforcement change
429
+ -- nobody can observe is L9's unpublishable coverage in miniature. The suite
430
+ -- asserts prosecdef = false so the decision cannot drift into place unnoticed.
431
+ --
432
+ -- And it does NOT grant UPDATE on cell.execution_attempt. That would make the
433
+ -- OLD caller-side fence runnable again, and it is the one thing the fence-in-the-
434
+ -- verb design exists to avoid buying: the executor would hold write authority
435
+ -- over its own attempt's state, outcome_status and reconcile_by — R3's whole
436
+ -- reconciliation surface — in exchange for a lock it no longer needs. The runtime
437
+ -- suite asserts has_table_privilege(cell_executor, cell.execution_attempt,
438
+ -- UPDATE) = false in the same leg that proves the lane works, so the two facts
439
+ -- can never drift apart: the lane runs, and it runs without that grant.
@@ -0,0 +1,102 @@
1
+ -- 025_running_attempt_uniqueness.sql — one work claim, at most one RUNNING
2
+ -- execution attempt, as a property of what the substrate can STORE.
3
+ --
4
+ -- Contract: contract §1.2 (the attempt is the disposable runtime binding beneath
5
+ -- a claim; the claim is the interval of responsibility) + FOUNDER RULING R1 (the
6
+ -- runtime binding is the executor's, the judgment is not) + a3-verification-
7
+ -- strategy.md §3.2 row RT-CLOSURE's premise, which is that an attempt row
8
+ -- identifies ONE execution. Requires 001_core.sql (declares cell.execution_attempt
9
+ -- and its state vocabulary) and 021_execution_attempt_closure.sql (the columns a
10
+ -- runtime-written attempt carries). Nothing else in the substrate is touched.
11
+ --
12
+ -- Re-apply: idempotent (CREATE UNIQUE INDEX IF NOT EXISTS).
13
+ -- Revert: DROP INDEX cell.execution_attempt_one_running_per_claim;
14
+ -- (FC085 in 024_executor_role_hardening.sql keeps refusing the receipt,
15
+ -- so a reverted cell is back to the read-time fence alone, not to no
16
+ -- enforcement at all)
17
+ --
18
+ -- Declared failure vocabulary: NONE. This file raises no FC code. Its refusal is
19
+ -- PostgreSQL's own 23505 unique_violation naming the index, deliberately: the
20
+ -- rule is "this row may not exist", which is the exact thing a unique index says
21
+ -- and a consumer already knows how to read. Inventing an FC code for it would put
22
+ -- a domain-vocabulary spelling on a refusal the substrate raises without any
23
+ -- plpgsql of ours running, and a caller keying on FC codes would then have two
24
+ -- ways to learn one fact.
25
+ --
26
+ --
27
+ -- WHAT THIS CHANGES, AND WHAT WAS ALREADY THERE
28
+ --
29
+ -- The read-time fence remains: FC085 (RECEIPT_MULTIPLE_RUNNING_ATTEMPTS) in
30
+ -- 024_executor_role_hardening.sql:315-322. The name is parenthesised on every
31
+ -- mention below, per the convention 015 established: this file declares no
32
+ -- vocabulary of its own, and a bare `FCnnn NAME` line reads as a declaration.
33
+ -- The fence sits inside cell.claim_effect_receipt: when the
34
+ -- claim beneath a receipt holds more than one running attempt, currency is
35
+ -- unanswerable and the verb fails closed rather than attributing an irreversible
36
+ -- effect to a guessed binding. This index does not replace it and does not weaken
37
+ -- it — it removes its cause. FC085 is checked at the worst moment there is (a
38
+ -- runtime that has already done the work, at the instant it records the effect)
39
+ -- and only for callers that come through that one verb; the index refuses the
40
+ -- SECOND running row at the moment somebody tries to write it, for every writer,
41
+ -- including one that bypasses the verbs entirely. Two enforcement points, one
42
+ -- invariant, in the order that costs least: unstorable at write time, and still
43
+ -- named at read time for a substrate that carries the state already.
44
+ --
45
+ -- THAT FENCE IS NOT NOW DEAD CODE, and this is the reason it is not deleted in the
46
+ -- same change (L5 asks for the obsolete mechanism, not for a still-reachable one):
47
+ -- * a cell UPGRADED with two running attempts already in place keeps them —
48
+ -- CREATE UNIQUE INDEX fails loudly on such a cell rather than deleting
49
+ -- evidence (L12), and the operator resolving it needs the fence standing
50
+ -- while they do;
51
+ -- * an index can be dropped, by an operator or a migration, and a fence that
52
+ -- only exists as an index is a fence whose absence is silent;
53
+ -- * FC085's own suite (tests/test_executor_role.py) seeds the state with the
54
+ -- index dropped and asserts the verb still refuses — so the fence keeps its
55
+ -- falsifier and cannot rot behind the index.
56
+ --
57
+ -- WHY (org_id, work_claim_id) AND NOT THE ATTEMPT'S OTHER COLUMNS. The question
58
+ -- the invariant answers is "which attempt is this claim's current binding", and
59
+ -- that question is asked per claim. org_id leads so the index shares the
60
+ -- (org_id, ...) prefix shape every other execution_attempt index uses and answers
61
+ -- an org-scoped lookup as well as the exclusion.
62
+ --
63
+ -- WHY PARTIAL. Attempt rows are durable execution evidence and only accumulate
64
+ -- (L12): a claim retried three times holds three attempt rows, and a total unique
65
+ -- index on (org_id, work_claim_id) would make the second retry unstorable — it
66
+ -- would forbid history rather than ambiguity. Only 'running' is exclusive, and
67
+ -- only while it is running: 001_core's execution_attempt_running_attempt_has_no_end
68
+ -- ties that state to a NULL ended_at, so leaving the predicate is the same event
69
+ -- as the attempt ending.
70
+ --
71
+ -- THE OTHER INDEX ON THIS PREDICATE, AND WHY IT IS NOT DELETED HERE. The A2
72
+ -- dispatcher candidate declares `execution_attempt_running` on
73
+ -- (org_id, work_claim_id, started_at) WHERE state = 'running' from its own schema
74
+ -- file (the dispatcher's own DDL (not vendored here)), to bound its 4-Hz hot
75
+ -- loop. Its L8 retirement condition is specific — "deleted the moment the same
76
+ -- index is adopted beside execution_attempt_by_claim in ddl/001_core.sql", with
77
+ -- the name identical so adoption is a no-op for a live cell — and this file is
78
+ -- not that: different name, different file, and a stricter shape that enforces
79
+ -- rather than only accelerates. The bridge and its deletion owner are unchanged.
80
+ --
81
+ -- WHAT IT DOES NOT DO. It does not decide WHICH attempt wins a race — the loser's
82
+ -- INSERT is refused with 23505 and the caller sees that its binding was not
83
+ -- established, which is the answer it needed. It grants nothing, revokes nothing
84
+ -- and changes no query's result; a reader that saw one running attempt before sees
85
+ -- one now.
86
+ --
87
+ -- BUILT NON-CONCURRENTLY, DELIBERATELY. This file is applied as part of the
88
+ -- declared order, in one transaction per file, on a cell being built or upgraded —
89
+ -- CREATE INDEX CONCURRENTLY cannot run inside a transaction block and would leave
90
+ -- an INVALID index behind on failure, which is the one outcome worse than a lock:
91
+ -- an invalid unique index enforces nothing while looking installed.
92
+
93
+ CREATE UNIQUE INDEX IF NOT EXISTS execution_attempt_one_running_per_claim
94
+ ON cell.execution_attempt (org_id, work_claim_id)
95
+ WHERE state = 'running';
96
+
97
+ COMMENT ON INDEX cell.execution_attempt_one_running_per_claim IS
98
+ 'contract §1.2: a work claim has at most one RUNNING execution attempt. The '
99
+ 'write-time half of the invariant FC085 (024_executor_role_hardening.sql) '
100
+ 'fences at receipt-claim time; this index makes the double-running state '
101
+ 'unstorable for every writer. PARTIAL because attempt history accumulates '
102
+ '(L12) — only the running binding is exclusive.';