@oathe/oathe 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -21
- package/NOTICE +7 -0
- package/README.md +99 -12
- package/assets/oathe-hero.svg +92 -0
- package/bin/oathe.mjs +376 -0
- package/docs/PRIVACY.md +45 -0
- package/package.json +32 -23
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/.mcp.json +13 -0
- package/plugin/hooks/frame-note.mjs +24 -0
- package/plugin/hooks/heartbeat.mjs +49 -0
- package/plugin/hooks/hooks.json +39 -0
- package/plugin/hooks/lib.mjs +83 -0
- package/plugin/hooks/render-board.mjs +11 -0
- package/plugin/skills/oathe-work/SKILL.md +33 -0
- package/src/atif.mjs +633 -0
- package/src/blocks.mjs +192 -0
- package/src/board-render.mjs +106 -0
- package/src/config.mjs +169 -0
- package/src/context.mjs +35 -0
- package/src/doctor.mjs +108 -0
- package/src/harness.mjs +271 -0
- package/src/init.mjs +92 -0
- package/src/launch-env.mjs +25 -0
- package/src/launch.mjs +259 -0
- package/src/manifest.mjs +89 -0
- package/src/mcp/oathe-tools.mjs +517 -0
- package/src/paths.mjs +49 -0
- package/src/plans.mjs +50 -0
- package/src/runtime/discharge.mjs +57 -0
- package/src/runtime/provider.mjs +154 -0
- package/src/runtime/simple-cage.mjs +67 -0
- package/src/runtime/sql-acceptance-lane.mjs +89 -0
- package/src/session-host.mjs +81 -0
- package/src/substrate.mjs +337 -0
- package/src/successor.mjs +31 -0
- package/src/traces.mjs +242 -0
- package/src/uninstall.mjs +45 -0
- package/src/verifier.mjs +322 -0
- package/src/workspace.mjs +47 -0
- package/vendor/ddl/001_core.sql +312 -0
- package/vendor/ddl/002_claim.sql +438 -0
- package/vendor/ddl/003_verification.sql +480 -0
- package/vendor/ddl/004_delegation.sql +397 -0
- package/vendor/ddl/005_escalation.sql +303 -0
- package/vendor/ddl/006_dependency.sql +384 -0
- package/vendor/ddl/007_assignment.sql +502 -0
- package/vendor/ddl/008_effect_receipt.sql +290 -0
- package/vendor/ddl/009_verified_edge.sql +452 -0
- package/vendor/ddl/010_parent_child_verification.sql +320 -0
- package/vendor/ddl/011_confirm.sql +288 -0
- package/vendor/ddl/012_evaluator_lane.sql +444 -0
- package/vendor/ddl/013_claim_terminal.sql +279 -0
- package/vendor/ddl/014_claim_settlement.sql +327 -0
- package/vendor/ddl/015_claim_yield.sql +213 -0
- package/vendor/ddl/016_reopened_reclaim.sql +285 -0
- package/vendor/ddl/017_claim_yield_causes.sql +357 -0
- package/vendor/ddl/018_receiptless_unknown.sql +249 -0
- package/vendor/ddl/019_verification_contract_freeze.sql +335 -0
- package/vendor/ddl/020_context_compilation.sql +105 -0
- package/vendor/ddl/021_execution_attempt_closure.sql +95 -0
- package/vendor/ddl/022_verification_clause.sql +106 -0
- package/vendor/ddl/023_executor_role.sql +66 -0
- package/vendor/ddl/024_executor_role_hardening.sql +439 -0
- package/vendor/ddl/025_running_attempt_uniqueness.sql +102 -0
- package/vendor/ddl/026_acceptance_authority.sql +230 -0
- package/vendor/ddl/027_ownership_return.sql +836 -0
- package/vendor/ddl/028_attempt_bind_ownership.sql +173 -0
- package/vendor/ddl/manifest.json +183 -0
- package/index.js +0 -33
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
-- 004_delegation.sql — the delegation lane: principals, grants, the enqueue
|
|
2
|
+
-- transaction, and the audit topic that names the TARGET department.
|
|
3
|
+
--
|
|
4
|
+
-- Contract: docs/a1-cell-work-spine-contract.md §2.3 as amended, FOUNDER RULING
|
|
5
|
+
-- R5, PRD §13.4 as amended — "the delegator's authority to create work in the
|
|
6
|
+
-- target scope is checked in the SAME TRANSACTION as the task/dependency
|
|
7
|
+
-- creation, against a grants table with a substrate-level check (the estate's
|
|
8
|
+
-- proven pattern — STF-1, 0047, 0073 triggers). No new authorization service is
|
|
9
|
+
-- introduced." Requires 001_core.sql.
|
|
10
|
+
--
|
|
11
|
+
-- Re-apply: idempotent (CREATE ... IF NOT EXISTS / OR REPLACE, ON CONFLICT seed).
|
|
12
|
+
-- Revert: DROP SCHEMA cell CASCADE; (this file installs nothing outside it)
|
|
13
|
+
--
|
|
14
|
+
-- TWO MEASURED DEFECTS THIS FILE CLOSES — in the cell design; the Mac is
|
|
15
|
+
-- untouched (L3):
|
|
16
|
+
--
|
|
17
|
+
-- 1. **cross-dept enqueue is unguarded** — the department is a free parameter
|
|
18
|
+
-- on the dispatch path, so any principal could create work in any scope.
|
|
19
|
+
-- The answer here is a grants table plus a BEFORE-INSERT trigger on the
|
|
20
|
+
-- delegation record, so the check is met by EVERY writer, including one
|
|
21
|
+
-- that never calls cell.enqueue_delegated_work. A rule with a CLI gate and
|
|
22
|
+
-- no substrate trigger is precisely the measured direct-publish bypass
|
|
23
|
+
-- (23 historical `verified` settles went around exactly such a gate).
|
|
24
|
+
-- 2. **cross-dept audit topics are hardcoded** to one department's dispatch
|
|
25
|
+
-- prefix, so work delegated INTO another scope was audited into the
|
|
26
|
+
-- delegator's — invisible to the department that has to answer for it.
|
|
27
|
+
-- Here the topic is a GENERATED column over the row's department, and the
|
|
28
|
+
-- department is checked against the task's own. A hardcoded department is
|
|
29
|
+
-- REFUSED rather than silently rewritten: a rewrite would hide that a
|
|
30
|
+
-- caller still believes every dispatch belongs to one department.
|
|
31
|
+
--
|
|
32
|
+
-- Declared failure vocabulary added by this file (SQLSTATE class FC — outside
|
|
33
|
+
-- every class Postgres assigns; message prefix and SQLSTATE carry the SAME
|
|
34
|
+
-- invariant name, so a caller may key on either and an operator reading the log
|
|
35
|
+
-- sees the rule). This lane reserves FC020-FC039.
|
|
36
|
+
--
|
|
37
|
+
-- The FC020-FC039 block is ONE range shared by the delegation, escalation and
|
|
38
|
+
-- assignment lanes on purpose: those three verbs raise each other's invariants
|
|
39
|
+
-- (UNKNOWN_TASK and DELEGATION_NOT_GRANTED are genuinely the same rule seen from
|
|
40
|
+
-- three verbs), and splitting the block would force one invariant to carry two
|
|
41
|
+
-- codes. Codes RE-RAISED from a neighbouring file are listed below with their
|
|
42
|
+
-- owning file named, so this header is the whole vocabulary this file can emit —
|
|
43
|
+
-- tests/test_failure_vocabulary.py holds the substrate to that.
|
|
44
|
+
-- FC020 DELEGATION_NOT_GRANTED work created in a scope not granted
|
|
45
|
+
-- FC021 UNKNOWN_TASK a lane record about work that does not exist
|
|
46
|
+
-- FC022 DELEGATION_SCOPE_MISMATCH a delegation naming a scope the task is not in
|
|
47
|
+
-- FC023 AUDIT_DEPARTMENT_MISATTRIBUTED an audit row claiming another department
|
|
48
|
+
|
|
49
|
+
-- ---------------------------------------------------------------------------
|
|
50
|
+
-- principal — the org chart, as rows
|
|
51
|
+
--
|
|
52
|
+
-- Needed here rather than later because BOTH halves of this lane read it: a
|
|
53
|
+
-- grant is held BY a principal, and the escalation chain (005) walks
|
|
54
|
+
-- `assigner_principal_id` from the escalating principal to the CEO, who ALONE
|
|
55
|
+
-- decides human contact (FOUNDER RULING R6). PRD §4.2 v1 keeps one standing
|
|
56
|
+
-- lead per department; the shape here is multi-principal-ready (R5), because a
|
|
57
|
+
-- department holding two principals must not require re-architecture.
|
|
58
|
+
-- ---------------------------------------------------------------------------
|
|
59
|
+
CREATE TABLE IF NOT EXISTS cell.principal (
|
|
60
|
+
org_id text NOT NULL,
|
|
61
|
+
principal_id text NOT NULL,
|
|
62
|
+
department text NOT NULL,
|
|
63
|
+
role text NOT NULL,
|
|
64
|
+
|
|
65
|
+
-- Who this principal escalates to. NULL EXACTLY for the CEO principal —
|
|
66
|
+
-- the one seat with nobody above it — and the CHECK below is what makes
|
|
67
|
+
-- that the only lawful absence. A lead with no assigner is a chain that
|
|
68
|
+
-- runs out, and an escalation that runs out is the abolished modal block
|
|
69
|
+
-- wearing a different name.
|
|
70
|
+
assigner_principal_id text,
|
|
71
|
+
|
|
72
|
+
CONSTRAINT principal_pkey PRIMARY KEY (org_id, principal_id),
|
|
73
|
+
|
|
74
|
+
CONSTRAINT principal_role_is_a_declared_seat
|
|
75
|
+
CHECK (role IN ('ceo', 'lead')),
|
|
76
|
+
|
|
77
|
+
CONSTRAINT principal_assigner_fkey
|
|
78
|
+
FOREIGN KEY (org_id, assigner_principal_id)
|
|
79
|
+
REFERENCES cell.principal (org_id, principal_id),
|
|
80
|
+
|
|
81
|
+
CONSTRAINT principal_only_the_ceo_answers_to_nobody
|
|
82
|
+
CHECK ((role = 'ceo') = (assigner_principal_id IS NULL)),
|
|
83
|
+
|
|
84
|
+
-- The smallest possible cycle, and the one a chain walk cannot tell apart
|
|
85
|
+
-- from progress. The longer cycles are caught by the walk itself (005).
|
|
86
|
+
CONSTRAINT principal_answers_to_someone_else
|
|
87
|
+
CHECK (assigner_principal_id IS DISTINCT FROM principal_id)
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
-- One root per org. Two CEO principals is a chain that FORKS, and an escalation
|
|
91
|
+
-- that forks is an escalation two seats can each believe the other is handling.
|
|
92
|
+
CREATE UNIQUE INDEX IF NOT EXISTS principal_one_ceo_per_org
|
|
93
|
+
ON cell.principal (org_id) WHERE role = 'ceo';
|
|
94
|
+
|
|
95
|
+
COMMENT ON TABLE cell.principal IS
|
|
96
|
+
'PRD §4.2 / FOUNDER RULING R5-R6. The org chart the grants table and the '
|
|
97
|
+
'escalation chain both read. Multi-principal-ready; v1 seats one lead per '
|
|
98
|
+
'department.';
|
|
99
|
+
|
|
100
|
+
-- ---------------------------------------------------------------------------
|
|
101
|
+
-- delegation_grant — the grants table PRD §13.4 names
|
|
102
|
+
--
|
|
103
|
+
-- "A delegation is authorized before it exists." This is the whole of that
|
|
104
|
+
-- authority: which principal may create work in which department. It is a
|
|
105
|
+
-- table and not a service because a durable store can own the semantic
|
|
106
|
+
-- (C.1: no new service when a store can own it), and because a table is what a
|
|
107
|
+
-- trigger can read inside the delegator's own transaction.
|
|
108
|
+
-- ---------------------------------------------------------------------------
|
|
109
|
+
CREATE TABLE IF NOT EXISTS cell.delegation_grant (
|
|
110
|
+
org_id text NOT NULL,
|
|
111
|
+
from_principal text NOT NULL,
|
|
112
|
+
to_department text NOT NULL,
|
|
113
|
+
state text NOT NULL,
|
|
114
|
+
granted_by text NOT NULL,
|
|
115
|
+
granted_at timestamptz NOT NULL,
|
|
116
|
+
|
|
117
|
+
-- NULL exactly while the grant is live. Paired with `state` by the CHECK
|
|
118
|
+
-- below: a revoked grant with no stamp is an authority change nobody can
|
|
119
|
+
-- date, and dating authority changes is the entire point of holding them
|
|
120
|
+
-- as rows rather than as configuration.
|
|
121
|
+
revoked_at timestamptz,
|
|
122
|
+
|
|
123
|
+
CONSTRAINT delegation_grant_pkey
|
|
124
|
+
PRIMARY KEY (org_id, from_principal, to_department),
|
|
125
|
+
|
|
126
|
+
CONSTRAINT delegation_grant_holder_fkey
|
|
127
|
+
FOREIGN KEY (org_id, from_principal)
|
|
128
|
+
REFERENCES cell.principal (org_id, principal_id),
|
|
129
|
+
|
|
130
|
+
CONSTRAINT delegation_grant_granted_by_fkey
|
|
131
|
+
FOREIGN KEY (org_id, granted_by)
|
|
132
|
+
REFERENCES cell.principal (org_id, principal_id),
|
|
133
|
+
|
|
134
|
+
CONSTRAINT delegation_grant_state_is_declared
|
|
135
|
+
CHECK (state IN ('live', 'revoked')),
|
|
136
|
+
|
|
137
|
+
CONSTRAINT delegation_grant_revoked_grant_carries_its_stamp
|
|
138
|
+
CHECK ((state = 'revoked') = (revoked_at IS NOT NULL))
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
-- ---------------------------------------------------------------------------
|
|
142
|
+
-- task_delegation — the durable record that this task was created BY a
|
|
143
|
+
-- delegator INTO a scope, carrying PRD §13.4's minimal delegation contract
|
|
144
|
+
-- fields that are not already on the task itself.
|
|
145
|
+
--
|
|
146
|
+
-- It exists as its own row rather than as columns on `task` for one reason:
|
|
147
|
+
-- it is the row the grant trigger fires on. A task inserted with no delegation
|
|
148
|
+
-- record is not a delegation — it is work a scope created for itself — and the
|
|
149
|
+
-- distinction is what keeps the authority check aimed at the act that needs it.
|
|
150
|
+
-- ---------------------------------------------------------------------------
|
|
151
|
+
CREATE TABLE IF NOT EXISTS cell.task_delegation (
|
|
152
|
+
org_id text NOT NULL,
|
|
153
|
+
task_id text NOT NULL,
|
|
154
|
+
from_principal text NOT NULL,
|
|
155
|
+
to_department text NOT NULL,
|
|
156
|
+
contract_ref text NOT NULL,
|
|
157
|
+
created_at timestamptz NOT NULL,
|
|
158
|
+
|
|
159
|
+
CONSTRAINT task_delegation_pkey PRIMARY KEY (org_id, task_id),
|
|
160
|
+
|
|
161
|
+
CONSTRAINT task_delegation_task_fkey
|
|
162
|
+
FOREIGN KEY (org_id, task_id) REFERENCES cell.task (org_id, task_id),
|
|
163
|
+
|
|
164
|
+
CONSTRAINT task_delegation_delegator_fkey
|
|
165
|
+
FOREIGN KEY (org_id, from_principal)
|
|
166
|
+
REFERENCES cell.principal (org_id, principal_id)
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
-- ---------------------------------------------------------------------------
|
|
170
|
+
-- audit_event — the dispatch audit lane, with the department it belongs to
|
|
171
|
+
--
|
|
172
|
+
-- `topic` is GENERATED, not supplied. A topic a caller can set is a topic a
|
|
173
|
+
-- caller can hardcode, and hardcoding it is the measured defect. The vocabulary
|
|
174
|
+
-- of `kind` is closed here for the whole lane; the verbs land across this file
|
|
175
|
+
-- (task_created) and 005/006 (escalated, task_assigned, task_declined), each
|
|
176
|
+
-- registering itself in the producer CHECK below.
|
|
177
|
+
-- ---------------------------------------------------------------------------
|
|
178
|
+
CREATE TABLE IF NOT EXISTS cell.audit_event (
|
|
179
|
+
audit_event_id uuid NOT NULL,
|
|
180
|
+
org_id text NOT NULL,
|
|
181
|
+
|
|
182
|
+
-- The TARGET department — the scope that must answer for the work — never
|
|
183
|
+
-- the actor's. Checked against the task's own department by the trigger
|
|
184
|
+
-- below, so this column cannot drift from the work it describes.
|
|
185
|
+
department text NOT NULL,
|
|
186
|
+
kind text NOT NULL,
|
|
187
|
+
topic text GENERATED ALWAYS AS
|
|
188
|
+
(department || '.dispatch.' || kind) STORED,
|
|
189
|
+
task_id text NOT NULL,
|
|
190
|
+
|
|
191
|
+
-- WHO acted is still recorded — the fix is not to lose the delegator, it is
|
|
192
|
+
-- to stop filing the event under the delegator's department.
|
|
193
|
+
actor_principal text NOT NULL,
|
|
194
|
+
ts timestamptz NOT NULL,
|
|
195
|
+
producer text NOT NULL,
|
|
196
|
+
|
|
197
|
+
CONSTRAINT audit_event_pkey PRIMARY KEY (audit_event_id),
|
|
198
|
+
|
|
199
|
+
CONSTRAINT audit_event_task_fkey
|
|
200
|
+
FOREIGN KEY (org_id, task_id) REFERENCES cell.task (org_id, task_id),
|
|
201
|
+
|
|
202
|
+
CONSTRAINT audit_event_actor_fkey
|
|
203
|
+
FOREIGN KEY (org_id, actor_principal)
|
|
204
|
+
REFERENCES cell.principal (org_id, principal_id),
|
|
205
|
+
|
|
206
|
+
CONSTRAINT audit_event_kind_is_a_declared_meaning
|
|
207
|
+
CHECK (kind IN ('task_created', 'task_assigned', 'task_declined', 'escalated')),
|
|
208
|
+
|
|
209
|
+
CONSTRAINT audit_event_producer_is_a_declared_verb
|
|
210
|
+
CHECK (producer IN ('cell.enqueue_delegated_work', 'cell.escalate',
|
|
211
|
+
'cell.assign_task', 'cell.decline_task'))
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
CREATE INDEX IF NOT EXISTS audit_event_by_department
|
|
215
|
+
ON cell.audit_event (org_id, department, ts);
|
|
216
|
+
|
|
217
|
+
COMMENT ON COLUMN cell.audit_event.topic IS
|
|
218
|
+
'GENERATED from the TARGET department. The measured Mac defect was a '
|
|
219
|
+
'hardcoded dispatch prefix, which filed cross-department delegations under '
|
|
220
|
+
'the delegator''s department; a generated topic has no hand to hardcode.';
|
|
221
|
+
|
|
222
|
+
-- ------------------------------------------------------- the audit dept guard
|
|
223
|
+
CREATE OR REPLACE FUNCTION cell.enforce_audit_department_is_the_tasks_own()
|
|
224
|
+
RETURNS trigger
|
|
225
|
+
LANGUAGE plpgsql
|
|
226
|
+
AS $audit_dept$
|
|
227
|
+
DECLARE
|
|
228
|
+
v_task_department text;
|
|
229
|
+
BEGIN
|
|
230
|
+
SELECT department INTO v_task_department
|
|
231
|
+
FROM cell.task
|
|
232
|
+
WHERE org_id = NEW.org_id AND task_id = NEW.task_id;
|
|
233
|
+
|
|
234
|
+
IF NOT FOUND THEN
|
|
235
|
+
RAISE EXCEPTION
|
|
236
|
+
'UNKNOWN_TASK:%/% — an audit event describes work, and no work by '
|
|
237
|
+
'that identity exists in this org, so there is no department it '
|
|
238
|
+
'could belong to.',
|
|
239
|
+
NEW.org_id, NEW.task_id
|
|
240
|
+
USING ERRCODE = 'FC021';
|
|
241
|
+
END IF;
|
|
242
|
+
|
|
243
|
+
IF NEW.department IS DISTINCT FROM v_task_department THEN
|
|
244
|
+
RAISE EXCEPTION
|
|
245
|
+
'AUDIT_DEPARTMENT_MISATTRIBUTED:%:% — this audit event claims '
|
|
246
|
+
'department ''%'' for work that is in ''%''. The topic is generated '
|
|
247
|
+
'from this column, so a wrong department files the event where the '
|
|
248
|
+
'scope answering for the work will never read it — the measured '
|
|
249
|
+
'hardcoded-dispatch-topic defect. Refused rather than rewritten: a '
|
|
250
|
+
'rewrite would hide that the caller still believes otherwise.',
|
|
251
|
+
NEW.org_id, NEW.task_id, NEW.department, v_task_department
|
|
252
|
+
USING ERRCODE = 'FC023';
|
|
253
|
+
END IF;
|
|
254
|
+
|
|
255
|
+
RETURN NEW;
|
|
256
|
+
END;
|
|
257
|
+
$audit_dept$;
|
|
258
|
+
|
|
259
|
+
CREATE OR REPLACE TRIGGER audit_event_department_is_the_tasks_own
|
|
260
|
+
BEFORE INSERT OR UPDATE ON cell.audit_event
|
|
261
|
+
FOR EACH ROW EXECUTE FUNCTION cell.enforce_audit_department_is_the_tasks_own();
|
|
262
|
+
|
|
263
|
+
-- ------------------------------------------------------- the grant guard
|
|
264
|
+
--
|
|
265
|
+
-- This is PRD §13.4's "substrate-level check", and it is a trigger rather than
|
|
266
|
+
-- a clause inside the enqueue function so that the check is unavoidable: the
|
|
267
|
+
-- delegation record cannot exist without it, whoever writes the row, through
|
|
268
|
+
-- whatever path. The transaction the check runs in is the caller's own — there
|
|
269
|
+
-- is no second round trip in which authority could have changed.
|
|
270
|
+
CREATE OR REPLACE FUNCTION cell.enforce_delegation_grant()
|
|
271
|
+
RETURNS trigger
|
|
272
|
+
LANGUAGE plpgsql
|
|
273
|
+
AS $grant_guard$
|
|
274
|
+
DECLARE
|
|
275
|
+
v_task_department text;
|
|
276
|
+
v_grant_state text;
|
|
277
|
+
BEGIN
|
|
278
|
+
SELECT department INTO v_task_department
|
|
279
|
+
FROM cell.task
|
|
280
|
+
WHERE org_id = NEW.org_id AND task_id = NEW.task_id;
|
|
281
|
+
|
|
282
|
+
IF NOT FOUND THEN
|
|
283
|
+
RAISE EXCEPTION
|
|
284
|
+
'UNKNOWN_TASK:%/% — a delegation records that work was created in a '
|
|
285
|
+
'scope, and no work by that identity exists in this org.',
|
|
286
|
+
NEW.org_id, NEW.task_id
|
|
287
|
+
USING ERRCODE = 'FC021';
|
|
288
|
+
END IF;
|
|
289
|
+
|
|
290
|
+
-- Checked BEFORE the grant: a delegation naming a department the task is
|
|
291
|
+
-- not in would otherwise satisfy a grant for a scope the work never
|
|
292
|
+
-- entered — authority checked against fiction.
|
|
293
|
+
IF NEW.to_department IS DISTINCT FROM v_task_department THEN
|
|
294
|
+
RAISE EXCEPTION
|
|
295
|
+
'DELEGATION_SCOPE_MISMATCH:%/%:% — this delegation records scope '
|
|
296
|
+
'''%'' for work that is in ''%''. The grant is checked against the '
|
|
297
|
+
'recorded scope, so a scope that is not the work''s own would '
|
|
298
|
+
'authorize a delegation that never happened.',
|
|
299
|
+
NEW.org_id, NEW.task_id, NEW.from_principal,
|
|
300
|
+
NEW.to_department, v_task_department
|
|
301
|
+
USING ERRCODE = 'FC022';
|
|
302
|
+
END IF;
|
|
303
|
+
|
|
304
|
+
SELECT state INTO v_grant_state
|
|
305
|
+
FROM cell.delegation_grant
|
|
306
|
+
WHERE org_id = NEW.org_id
|
|
307
|
+
AND from_principal = NEW.from_principal
|
|
308
|
+
AND to_department = NEW.to_department;
|
|
309
|
+
|
|
310
|
+
IF NOT FOUND OR v_grant_state IS DISTINCT FROM 'live' THEN
|
|
311
|
+
RAISE EXCEPTION
|
|
312
|
+
'DELEGATION_NOT_GRANTED:%:% — principal % holds no live grant to '
|
|
313
|
+
'create work in ''%'' (grant state: %). A delegation is authorized '
|
|
314
|
+
'BEFORE it exists; authority is a row in cell.delegation_grant, and '
|
|
315
|
+
'the check runs in the same transaction as the work it authorizes.',
|
|
316
|
+
NEW.from_principal, NEW.to_department, NEW.from_principal,
|
|
317
|
+
NEW.to_department, coalesce(v_grant_state, '<no grant>')
|
|
318
|
+
USING ERRCODE = 'FC020';
|
|
319
|
+
END IF;
|
|
320
|
+
|
|
321
|
+
RETURN NEW;
|
|
322
|
+
END;
|
|
323
|
+
$grant_guard$;
|
|
324
|
+
|
|
325
|
+
CREATE OR REPLACE TRIGGER task_delegation_requires_a_live_grant
|
|
326
|
+
BEFORE INSERT OR UPDATE ON cell.task_delegation
|
|
327
|
+
FOR EACH ROW EXECUTE FUNCTION cell.enforce_delegation_grant();
|
|
328
|
+
|
|
329
|
+
-- --------------------------------------------------- the enqueue transaction
|
|
330
|
+
--
|
|
331
|
+
-- Offer-based delegation (contract §2.3): the delegator CREATEs the work in the
|
|
332
|
+
-- target scope and the target principal CLAIMs it. This verb therefore leaves
|
|
333
|
+
-- `to_principal` and `assignment_deadline` NULL — direct assignment is its own
|
|
334
|
+
-- verb (006), because an enqueue that quietly assigned would widen authority
|
|
335
|
+
-- without anyone asking for it (L11).
|
|
336
|
+
--
|
|
337
|
+
-- Every argument is required and none has a default, for the reason stated in
|
|
338
|
+
-- 002_claim.sql: a default here would be a policy decision made in the
|
|
339
|
+
-- substrate, invisible at the call site and unreachable by the one config
|
|
340
|
+
-- object that is supposed to hold every such decision.
|
|
341
|
+
CREATE OR REPLACE FUNCTION cell.enqueue_delegated_work(
|
|
342
|
+
p_org_id text,
|
|
343
|
+
p_task_id text,
|
|
344
|
+
p_target_department text,
|
|
345
|
+
p_from_principal text,
|
|
346
|
+
p_origin text,
|
|
347
|
+
p_objective text,
|
|
348
|
+
p_verification_plan jsonb,
|
|
349
|
+
p_verify_by timestamptz,
|
|
350
|
+
p_claim_mode text,
|
|
351
|
+
p_contract_ref text,
|
|
352
|
+
p_created_at timestamptz,
|
|
353
|
+
p_audit_event_id uuid
|
|
354
|
+
)
|
|
355
|
+
RETURNS text
|
|
356
|
+
LANGUAGE plpgsql
|
|
357
|
+
AS $enqueue$
|
|
358
|
+
BEGIN
|
|
359
|
+
-- 1. the work itself, in the TARGET scope. Unassigned: this is the offer.
|
|
360
|
+
INSERT INTO cell.task (
|
|
361
|
+
org_id, task_id, department, origin, objective, verification_plan,
|
|
362
|
+
verify_by, claim_mode, to_principal, assignment_deadline, created_at
|
|
363
|
+
) VALUES (
|
|
364
|
+
p_org_id, p_task_id, p_target_department, p_origin, p_objective,
|
|
365
|
+
p_verification_plan, p_verify_by, p_claim_mode, NULL, NULL, p_created_at
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
-- 2. the delegation record — and with it, IN THIS SAME TRANSACTION, the
|
|
369
|
+
-- grant check (the trigger above). A refusal here takes the task with
|
|
370
|
+
-- it: PRD §13.4's same-transaction clause is not a sequencing
|
|
371
|
+
-- preference, it is what stops unauthorized work existing for the width
|
|
372
|
+
-- of a round trip.
|
|
373
|
+
INSERT INTO cell.task_delegation (
|
|
374
|
+
org_id, task_id, from_principal, to_department, contract_ref, created_at
|
|
375
|
+
) VALUES (
|
|
376
|
+
p_org_id, p_task_id, p_from_principal, p_target_department,
|
|
377
|
+
p_contract_ref, p_created_at
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
-- 3. the audit event, filed under the TARGET department.
|
|
381
|
+
INSERT INTO cell.audit_event (
|
|
382
|
+
audit_event_id, org_id, department, kind, task_id, actor_principal,
|
|
383
|
+
ts, producer
|
|
384
|
+
) VALUES (
|
|
385
|
+
p_audit_event_id, p_org_id, p_target_department, 'task_created',
|
|
386
|
+
p_task_id, p_from_principal, p_created_at, 'cell.enqueue_delegated_work'
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
RETURN p_task_id;
|
|
390
|
+
END;
|
|
391
|
+
$enqueue$;
|
|
392
|
+
|
|
393
|
+
COMMENT ON FUNCTION cell.enqueue_delegated_work IS
|
|
394
|
+
'Offer-based delegation (contract §2.3): creates work in the target scope, '
|
|
395
|
+
'records the delegation — whose trigger checks the grant in this same '
|
|
396
|
+
'transaction — and files the audit event under the TARGET department. '
|
|
397
|
+
'Leaves the task unassigned; direct assignment is cell.assign_task.';
|