@capybara-hq/kernel 0.1.0-rc.107 → 0.1.0-rc.109
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/bootstrap.json +13 -13
- package/dist/automations/acceptance.controller.d.ts +29 -0
- package/dist/automations/acceptance.controller.js +87 -0
- package/dist/automations/automations.module.js +4 -1
- package/dist/automations/automations.schemas.d.ts +46 -0
- package/dist/automations/automations.schemas.js +31 -0
- package/dist/automations/automations.service.js +10 -1
- package/dist/automations/errors.d.ts +5 -0
- package/dist/automations/errors.js +5 -0
- package/dist/automations/events/acceptance.d.ts +205 -0
- package/dist/automations/events/acceptance.js +822 -0
- package/dist/automations/events/deliver.d.ts +39 -0
- package/dist/automations/events/deliver.js +112 -0
- package/dist/automations/events/dispatcher.d.ts +26 -10
- package/dist/automations/events/dispatcher.js +98 -99
- package/dist/automations/events/sweep.job.d.ts +104 -18
- package/dist/automations/events/sweep.job.js +311 -45
- package/dist/automations/events/trigger-events.service.d.ts +31 -2
- package/dist/automations/events/trigger-events.service.js +113 -30
- package/dist/automations/store.d.ts +7 -3
- package/dist/automations/store.js +6 -0
- package/dist/automations/types.d.ts +5 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/config/schema.d.ts +3 -0
- package/dist/config/schema.js +14 -0
- package/dist/ctx/create-ctx.d.ts +24 -4
- package/dist/ctx/create-ctx.js +119 -6
- package/dist/db/kernel-schema.d.ts +89 -1
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +18 -0
- package/dist/extensions/hooks.d.ts +8 -5
- package/dist/extensions/hooks.js +2 -1
- package/dist/extensions/host.d.ts +12 -2
- package/dist/extensions/host.js +21 -1
- package/dist/extensions/lifecycle-hooks.d.ts +21 -7
- package/dist/extensions/lifecycle-hooks.js +51 -3
- package/dist/extensions/permission-set.d.ts +33 -1
- package/dist/extensions/permission-set.js +28 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +24 -1
- package/dist/extensions/run-lifecycle-owe.d.ts +67 -0
- package/dist/extensions/run-lifecycle-owe.js +84 -0
- package/dist/http/api.module.js +4 -0
- package/dist/inbox/inbox-item-actions.d.ts +37 -0
- package/dist/inbox/inbox-item-actions.js +69 -0
- package/dist/inbox/inbox-items.controller.d.ts +27 -0
- package/dist/inbox/inbox-items.controller.js +80 -0
- package/dist/inbox/inbox-items.service.d.ts +110 -0
- package/dist/inbox/inbox-items.service.js +324 -0
- package/dist/inbox/inbox.module.d.ts +2 -1
- package/dist/inbox/inbox.module.js +15 -4
- package/dist/inbox/inbox.schemas.d.ts +20 -1
- package/dist/inbox/inbox.schemas.js +16 -1
- package/dist/inbox/inbox.service.d.ts +29 -7
- package/dist/inbox/inbox.service.js +29 -8
- package/dist/inbox/token.d.ts +5 -0
- package/dist/inbox/token.js +5 -0
- package/dist/infra/config/kernel-config.service.js +2 -0
- package/dist/infra/outbox/registry.d.ts +17 -0
- package/dist/infra/outbox/registry.js +15 -0
- package/dist/kernel.module.js +6 -0
- package/dist/orgs/settings.service.d.ts +20 -0
- package/dist/orgs/settings.service.js +4 -0
- package/dist/platform/exclusions.js +15 -0
- package/dist/platform/operation.d.ts +8 -0
- package/dist/platform/ops/automations.ops.d.ts +3 -1
- package/dist/platform/ops/automations.ops.js +45 -3
- package/dist/platform/ops/inbox.ops.d.ts +3 -1
- package/dist/platform/ops/inbox.ops.js +69 -4
- package/dist/platform/ops/platform.ops.js +4 -3
- package/dist/plugin-reads/plugin-reads.service.d.ts +29 -0
- package/dist/plugin-reads/plugin-reads.service.js +210 -0
- package/dist/plugin-reads/token.d.ts +5 -0
- package/dist/plugin-reads/token.js +5 -0
- package/dist/push/jobs.d.ts +5 -0
- package/dist/push/jobs.js +2 -0
- package/dist/push/preferences.service.d.ts +2 -2
- package/dist/push/preferences.service.js +9 -3
- package/dist/push/push-owed.d.ts +2 -0
- package/dist/push/push-owed.js +10 -1
- package/dist/push/push.schemas.d.ts +5 -2
- package/dist/push/push.schemas.js +4 -3
- package/dist/push/recipients.d.ts +2 -0
- package/dist/push/recipients.js +44 -3
- package/dist/push/texts.d.ts +14 -5
- package/dist/push/texts.js +2 -1
- package/dist/push/triggers.d.ts +1 -1
- package/dist/runtime/run-finish.js +19 -1
- package/dist/runtime/run-jobs.d.ts +12 -2
- package/dist/runtime/run-jobs.js +41 -4
- package/dist/runtime/run-queue.js +10 -0
- package/dist/runtime/waits.js +24 -1
- package/dist/tools/builtins/platform.js +2 -0
- package/dist/tools/pipeline.d.ts +5 -0
- package/dist/tools/pipeline.js +13 -1
- package/dist/tools/registry.d.ts +6 -0
- package/package.json +3 -3
- package/src/migrations/0192_inbox_items.sql +50 -0
- package/src/migrations/0193_trigger_cause_handles.sql +49 -0
- package/src/migrations/0194_automation_acceptance.sql +83 -0
package/dist/runtime/run-jobs.js
CHANGED
|
@@ -11,6 +11,7 @@ import { getQueueToken } from "@nestjs/bullmq";
|
|
|
11
11
|
import { Inject, Injectable, Module } from "@nestjs/common";
|
|
12
12
|
import { sql } from "kysely";
|
|
13
13
|
import { BG_WAKE_JOB, bgWakeJobId } from "../background/wake-rules.js";
|
|
14
|
+
import { oweRunLifecycleFor } from "../extensions/run-lifecycle-owe.js";
|
|
14
15
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
15
16
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
16
17
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
@@ -139,6 +140,13 @@ let RunJobs = class RunJobs {
|
|
|
139
140
|
orgId,
|
|
140
141
|
});
|
|
141
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Owes the `started` notice of a new run to the plugin of its conversation's subject, in the
|
|
145
|
+
* caller's transaction; nothing when the conversation has none (no query then).
|
|
146
|
+
*/
|
|
147
|
+
oweRunStarted(db, facts) {
|
|
148
|
+
return oweRunLifecycleFor(db, this.outbox, facts, { phase: "started" });
|
|
149
|
+
}
|
|
142
150
|
/** {@link oweEnsureRun} in the caller's transaction. */
|
|
143
151
|
oweEnsureRun(conversationId, seq, orgId) {
|
|
144
152
|
return oweEnsureRun(this.outbox, conversationId, seq, orgId);
|
|
@@ -198,14 +206,17 @@ let RunResume = class RunResume {
|
|
|
198
206
|
* Locks the run row (`FOR UPDATE`); when it is `waiting` and waits on nothing else (no pending
|
|
199
207
|
* approval or input request, no active fork sub-agent), sets it `queued` (adding this wait's
|
|
200
208
|
* time to `waited_ms`, recording the resume job id) and owes the resume job
|
|
201
|
-
* {@link resumeJobId}`(conversation, waitId)` on its conversation kind's run queue
|
|
202
|
-
*
|
|
209
|
+
* {@link resumeJobId}`(conversation, waitId)` on its conversation kind's run queue, and, when
|
|
210
|
+
* the wait was on a person and the conversation has a subject, `run.lifecycle` `resumed` (not
|
|
211
|
+
* for compact runs). A `running` run is left alone: its suspend has not set `waiting` yet and
|
|
212
|
+
* re-checks the wait after.
|
|
203
213
|
*/
|
|
204
214
|
async resumeIn(runId, waitId) {
|
|
205
215
|
const t = this.kdb.db;
|
|
216
|
+
// the wait's expiry is set only for a wait on sub-agents: null here is a wait on a person
|
|
206
217
|
const run = await t
|
|
207
218
|
.selectFrom("runs")
|
|
208
|
-
.select("status")
|
|
219
|
+
.select(["status", "wait_expires_at", "compact", "agent_version", "trigger_account_id"])
|
|
209
220
|
.where("id", "=", runId)
|
|
210
221
|
.forUpdate()
|
|
211
222
|
.executeTakeFirst();
|
|
@@ -258,9 +269,22 @@ let RunResume = class RunResume {
|
|
|
258
269
|
// org-filter: the conversation of the run row just resumed above; reads its own org
|
|
259
270
|
const conv = await t
|
|
260
271
|
.selectFrom("conversations")
|
|
261
|
-
.select(["kind", "org_id"])
|
|
272
|
+
.select(["kind", "org_id", "agent_id", "subject"])
|
|
262
273
|
.where("id", "=", row.conversation_id)
|
|
263
274
|
.executeTakeFirstOrThrow();
|
|
275
|
+
// a person's wait cleared and the run continues: told to the subject's plugin, one notice per
|
|
276
|
+
// wait; the reads above carry what it needs, so a conversation without a subject costs no query
|
|
277
|
+
if (conv.subject !== null && run.wait_expires_at === null && !run.compact) {
|
|
278
|
+
await oweRunLifecycleFor(t, this.outbox, {
|
|
279
|
+
orgId: conv.org_id,
|
|
280
|
+
conversationId: row.conversation_id,
|
|
281
|
+
runId,
|
|
282
|
+
agentId: conv.agent_id,
|
|
283
|
+
agentVersion: run.agent_version,
|
|
284
|
+
accountId: run.trigger_account_id,
|
|
285
|
+
subject: conv.subject,
|
|
286
|
+
}, { phase: "resumed", waitingOn: await this.waitKind(conv.org_id, runId, waitId), waitId });
|
|
287
|
+
}
|
|
264
288
|
await this.outbox.add({
|
|
265
289
|
queue: runQueueNameFor(conv.kind),
|
|
266
290
|
name: RUN_JOB,
|
|
@@ -270,6 +294,19 @@ let RunResume = class RunResume {
|
|
|
270
294
|
});
|
|
271
295
|
return "queued";
|
|
272
296
|
}
|
|
297
|
+
/** What the cleared wait `waitId` of `runId` was: an approval (a permission one too), else input. */
|
|
298
|
+
async waitKind(orgId, runId, waitId) {
|
|
299
|
+
if (waitId.startsWith("permission-"))
|
|
300
|
+
return "approval";
|
|
301
|
+
const approval = await this.kdb.db
|
|
302
|
+
.selectFrom("approvals")
|
|
303
|
+
.select("id")
|
|
304
|
+
.where("org_id", "=", orgId)
|
|
305
|
+
.where("id", "=", waitId)
|
|
306
|
+
.where("run_id", "=", runId)
|
|
307
|
+
.executeTakeFirst();
|
|
308
|
+
return approval !== undefined ? "approval" : "input";
|
|
309
|
+
}
|
|
273
310
|
};
|
|
274
311
|
__decorate([
|
|
275
312
|
Transactional({ propagation: "mandatory" })
|
|
@@ -211,6 +211,7 @@ let RunQueue = class RunQueue {
|
|
|
211
211
|
"account_id",
|
|
212
212
|
"deleted_at",
|
|
213
213
|
"tree_stopped_at",
|
|
214
|
+
"subject",
|
|
214
215
|
])
|
|
215
216
|
.where("id", "=", conversationId)
|
|
216
217
|
.forUpdate()
|
|
@@ -358,6 +359,15 @@ let RunQueue = class RunQueue {
|
|
|
358
359
|
trigger_event_id: lineage.triggerEventId,
|
|
359
360
|
})
|
|
360
361
|
.execute();
|
|
362
|
+
await this.jobs.oweRunStarted(t, {
|
|
363
|
+
orgId: conv.org_id,
|
|
364
|
+
conversationId,
|
|
365
|
+
runId,
|
|
366
|
+
agentId: conv.agent_id,
|
|
367
|
+
agentVersion: agent.current_version,
|
|
368
|
+
accountId: trigger.accountId,
|
|
369
|
+
subject: conv.subject,
|
|
370
|
+
});
|
|
361
371
|
// the task run its message started follows this run from now on (spec §6)
|
|
362
372
|
if (taskRunId !== null)
|
|
363
373
|
await linkStartedRunIn(t, taskRunId, runId, conversationId);
|
package/dist/runtime/waits.js
CHANGED
|
@@ -13,6 +13,7 @@ import { sql } from "kysely";
|
|
|
13
13
|
import { BackgroundTaskStopper } from "../background/stop.js";
|
|
14
14
|
import { EventLog } from "../conversations/event-log.js";
|
|
15
15
|
import { conversationTree } from "../conversations/tree.js";
|
|
16
|
+
import { oweRunLifecycleFor } from "../extensions/run-lifecycle-owe.js";
|
|
16
17
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
18
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
18
19
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
@@ -169,11 +170,33 @@ let Waits = class Waits {
|
|
|
169
170
|
.whereRef("conversations.id", "=", "runs.conversation_id")
|
|
170
171
|
.where("runs.id", "=", runId)
|
|
171
172
|
.where("runs.status", "=", "running")
|
|
172
|
-
.returning(
|
|
173
|
+
.returning([
|
|
174
|
+
"conversations.org_id",
|
|
175
|
+
"conversations.id as conversation_id",
|
|
176
|
+
"conversations.agent_id",
|
|
177
|
+
"conversations.account_id",
|
|
178
|
+
"conversations.subject",
|
|
179
|
+
"runs.agent_version",
|
|
180
|
+
"runs.trigger_account_id",
|
|
181
|
+
"runs.compact",
|
|
182
|
+
])
|
|
173
183
|
.executeTakeFirst();
|
|
174
184
|
if (!row)
|
|
175
185
|
return false;
|
|
176
186
|
await this.oweTimeout(waitingFor, runId, row.org_id);
|
|
187
|
+
// a wait on a person (not on sub-agents) is told to the subject's plugin, one notice per wait;
|
|
188
|
+
// the row above carries what it needs, so a conversation without a subject costs no query
|
|
189
|
+
if (waitingFor.kind !== "subagents" && row.subject !== null && !row.compact) {
|
|
190
|
+
await oweRunLifecycleFor(this.db, this.outbox, {
|
|
191
|
+
orgId: row.org_id,
|
|
192
|
+
conversationId: row.conversation_id,
|
|
193
|
+
runId,
|
|
194
|
+
agentId: row.agent_id,
|
|
195
|
+
agentVersion: row.agent_version,
|
|
196
|
+
accountId: row.trigger_account_id,
|
|
197
|
+
subject: row.subject,
|
|
198
|
+
}, { phase: "waiting", waitingOn: waitingFor.kind, waitId: waitingFor.id });
|
|
199
|
+
}
|
|
177
200
|
return true;
|
|
178
201
|
}
|
|
179
202
|
/**
|
|
@@ -416,6 +416,8 @@ export function platformTool(deps) {
|
|
|
416
416
|
...(card !== null && { file: card }),
|
|
417
417
|
// its result is shown once to the run's person: only they may consent to it
|
|
418
418
|
...(op.revealsOnce !== undefined && { personOnly: true }),
|
|
419
|
+
// it changes what the person consents to: only they decide, never their profile
|
|
420
|
+
...(op.personOnly === true && { personOnly: true, personDecides: true }),
|
|
419
421
|
};
|
|
420
422
|
}
|
|
421
423
|
async function run(raw, ctx) {
|
package/dist/tools/pipeline.d.ts
CHANGED
|
@@ -112,6 +112,11 @@ export declare const PERSON_FIELDS_ROLE_TEXT = "This write needs the person you
|
|
|
112
112
|
* run's person) in an unattended run: nobody is there to read it.
|
|
113
113
|
*/
|
|
114
114
|
export declare const PERSON_ONLY_UNATTENDED_TEXT = "This write shows its result once, on the web, to the person you work for, and no one is there to read it; it cannot run unattended.";
|
|
115
|
+
/**
|
|
116
|
+
* A call that changes what the person consents to (`CallPolicy.personDecides`) in an unattended
|
|
117
|
+
* run: only that person decides it, and nobody is there.
|
|
118
|
+
*/
|
|
119
|
+
export declare const PERSON_DECIDES_UNATTENDED_TEXT = "This write changes what the person you work for consents to, so only they can confirm it, and no one is there; it cannot run unattended. Ask them to make the change on the web.";
|
|
115
120
|
/** A call only the person may confirm whose ask would go to a role instead of the person. */
|
|
116
121
|
export declare const PERSON_ONLY_ROLE_TEXT = "This write shows its result once to the person you work for, so it runs only for them, and they lack the role. Ask an admin to do it on the web.";
|
|
117
122
|
/**
|
package/dist/tools/pipeline.js
CHANGED
|
@@ -166,6 +166,11 @@ export const PERSON_FIELDS_ROLE_TEXT = "This write needs the person you work for
|
|
|
166
166
|
* run's person) in an unattended run: nobody is there to read it.
|
|
167
167
|
*/
|
|
168
168
|
export const PERSON_ONLY_UNATTENDED_TEXT = "This write shows its result once, on the web, to the person you work for, and no one is there to read it; it cannot run unattended.";
|
|
169
|
+
/**
|
|
170
|
+
* A call that changes what the person consents to (`CallPolicy.personDecides`) in an unattended
|
|
171
|
+
* run: only that person decides it, and nobody is there.
|
|
172
|
+
*/
|
|
173
|
+
export const PERSON_DECIDES_UNATTENDED_TEXT = "This write changes what the person you work for consents to, so only they can confirm it, and no one is there; it cannot run unattended. Ask them to make the change on the web.";
|
|
169
174
|
/** A call only the person may confirm whose ask would go to a role instead of the person. */
|
|
170
175
|
export const PERSON_ONLY_ROLE_TEXT = "This write shows its result once to the person you work for, so it runs only for them, and they lack the role. Ask an admin to do it on the web.";
|
|
171
176
|
/**
|
|
@@ -1030,7 +1035,11 @@ let ToolPipeline = class ToolPipeline {
|
|
|
1030
1035
|
// a call whose result is shown once to the run's person is refused there the same way
|
|
1031
1036
|
const personOnly = callPolicy?.personOnly === true;
|
|
1032
1037
|
if ((personFields !== undefined || personOnly) && unattendedRules(ctx.unattended)) {
|
|
1033
|
-
return fail(personFields !== undefined
|
|
1038
|
+
return fail(personFields !== undefined
|
|
1039
|
+
? PERSON_FIELDS_UNATTENDED_TEXT
|
|
1040
|
+
: callPolicy?.personDecides === true
|
|
1041
|
+
? PERSON_DECIDES_UNATTENDED_TEXT
|
|
1042
|
+
: PERSON_ONLY_UNATTENDED_TEXT, outKey);
|
|
1034
1043
|
}
|
|
1035
1044
|
let decision = await this.decide(tool, ctx.agent, {
|
|
1036
1045
|
orgId: ctx.orgId,
|
|
@@ -1578,6 +1587,9 @@ let ToolPipeline = class ToolPipeline {
|
|
|
1578
1587
|
return asks;
|
|
1579
1588
|
if (ctx.unattended != null)
|
|
1580
1589
|
return asks;
|
|
1590
|
+
// a change of what the person consents to: they answer it themself, never their profile
|
|
1591
|
+
if (callPolicy?.personDecides === true)
|
|
1592
|
+
return asks;
|
|
1581
1593
|
if (tool.spec.annotations?.requiresUserInteraction === true)
|
|
1582
1594
|
return asks;
|
|
1583
1595
|
// a decision already recorded for this call governs it whatever the profile reads now (R2: a
|
package/dist/tools/registry.d.ts
CHANGED
|
@@ -252,6 +252,12 @@ export interface CallPolicy {
|
|
|
252
252
|
* when the ask would go to a role instead of the person.
|
|
253
253
|
*/
|
|
254
254
|
personOnly?: true;
|
|
255
|
+
/**
|
|
256
|
+
* With `personOnly`: the conversation's profile never decides the ask either (no review model,
|
|
257
|
+
* no full access): the person answers it themself. For a call that changes what the person
|
|
258
|
+
* consents to (`PlatformOperation.personOnly`).
|
|
259
|
+
*/
|
|
260
|
+
personDecides?: true;
|
|
255
261
|
/**
|
|
256
262
|
* The file the call takes in place of an upload, as the card shows it (platform web parity spec
|
|
257
263
|
* §5): stored in the approval's `detail.file`, so the approval binds its sha256. Never the bytes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/kernel",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.109",
|
|
4
4
|
"description": "Capybara kernel: config, database, plugin host and HTTP API of the Capybara agent platform",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"yaml": "^2.9.1",
|
|
67
67
|
"yauzl": "3.4.0",
|
|
68
68
|
"zod": "^4.6.7",
|
|
69
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
69
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.87"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@langchain/langgraph-checkpoint-validation": "^1.2.0",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"@types/ws": "^8.18.2",
|
|
76
76
|
"@types/yauzl": "3.4.0",
|
|
77
77
|
"ws": "^8.22.0",
|
|
78
|
-
"@capybara-hq/testing": "0.1.0-rc.
|
|
78
|
+
"@capybara-hq/testing": "0.1.0-rc.49"
|
|
79
79
|
},
|
|
80
80
|
"scripts": {
|
|
81
81
|
"build": "tsc -b"
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
-- Personal inbox items (collaboration spec 2026-11-25 §4.9): an item for one person in one org,
|
|
2
|
+
-- posted by the kernel (a depth stop, §4.7; a declined offer, §4.4) or by an extension through
|
|
3
|
+
-- `ctx.inbox.post`. Only that person lists it, clicks its actions or dismisses it.
|
|
4
|
+
-- - `actions`: at most three `{ id, label: { zh, en }, style?, uiAction: { contribution,
|
|
5
|
+
-- actionId, payload? } }`; a click calls the plugin's UI action as the person who clicked;
|
|
6
|
+
-- - `dedupe_key`: while an item with the same key is open for the same person, source and plugin,
|
|
7
|
+
-- a post returns it instead of adding another (the partial unique index);
|
|
8
|
+
-- - `expires_at`: past it the item leaves the inbox (read as gone; no sweep needed to hide it);
|
|
9
|
+
-- - `detail`: a kernel item's structured facts the web renders in its own copy (a declined offer:
|
|
10
|
+
-- who declined, for which agent); null for a plugin item.
|
|
11
|
+
-- The push category `inbox` joins the CHECK of 0188 (dropped and added in this file's one
|
|
12
|
+
-- transaction). The table carries `org_id` and its `org_scope` policy (still dark: nothing enables
|
|
13
|
+
-- row level security before phase 5).
|
|
14
|
+
|
|
15
|
+
CREATE TABLE IF NOT EXISTS inbox_items (
|
|
16
|
+
id text PRIMARY KEY,
|
|
17
|
+
org_id text NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
|
|
18
|
+
account_id text NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
|
|
19
|
+
source text NOT NULL CHECK (source IN ('kernel', 'plugin')),
|
|
20
|
+
-- the posting extension; null for a kernel item
|
|
21
|
+
plugin text,
|
|
22
|
+
kind text NOT NULL,
|
|
23
|
+
title text NOT NULL,
|
|
24
|
+
body text NOT NULL DEFAULT '',
|
|
25
|
+
link text,
|
|
26
|
+
actions jsonb NOT NULL DEFAULT '[]'::jsonb,
|
|
27
|
+
dedupe_key text,
|
|
28
|
+
state text NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'resolved', 'expired')),
|
|
29
|
+
expires_at timestamptz NOT NULL,
|
|
30
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
31
|
+
resolved_at timestamptz,
|
|
32
|
+
detail jsonb,
|
|
33
|
+
CHECK ((source = 'plugin') = (plugin IS NOT NULL))
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
-- a person's open items (the inbox read)
|
|
37
|
+
CREATE INDEX IF NOT EXISTS inbox_items_open_idx
|
|
38
|
+
ON inbox_items (org_id, account_id) WHERE state = 'open';
|
|
39
|
+
|
|
40
|
+
-- one open item per (org, account, source, plugin, dedupe key)
|
|
41
|
+
CREATE UNIQUE INDEX IF NOT EXISTS inbox_items_dedupe_uq
|
|
42
|
+
ON inbox_items (org_id, account_id, source, coalesce(plugin, ''), dedupe_key)
|
|
43
|
+
WHERE state = 'open' AND dedupe_key IS NOT NULL;
|
|
44
|
+
|
|
45
|
+
DROP POLICY IF EXISTS org_scope ON inbox_items;
|
|
46
|
+
CREATE POLICY org_scope ON inbox_items USING (rls_admits(org_id)) WITH CHECK (rls_admits(org_id));
|
|
47
|
+
|
|
48
|
+
ALTER TABLE push_preferences DROP CONSTRAINT IF EXISTS push_preferences_category_check;
|
|
49
|
+
ALTER TABLE push_preferences ADD CONSTRAINT push_preferences_category_check
|
|
50
|
+
CHECK (category IN ('waiting', 'replies', 'tasks', 'automations', 'inbox'));
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
-- Serializable trigger causes (collaboration spec 2026-11-25 §4.2, §4.7) and retention indexes.
|
|
2
|
+
-- - `trigger_cause_handles`: what `ctx.triggers.causeOf(call)` returns, so an extension can emit
|
|
3
|
+
-- from a queue job with `cause: { handle }` and keep the chain depth and the self-trigger check
|
|
4
|
+
-- of the tool call that queued it. A handle is bound to the org, the extension that asked for it
|
|
5
|
+
-- and the call's run (verified from the pipeline's call scope when it was made), with the depth
|
|
6
|
+
-- and trigger account the run had then (so the retention sweep deleting the run's trigger event
|
|
7
|
+
-- within the handle's life cannot restart the chain); `emit` accepts it only for an event of that
|
|
8
|
+
-- org from that extension before `expires_at` (24 hours on). The
|
|
9
|
+
-- trigger event sweep deletes expired handles. The table carries `org_id` and its `org_scope`
|
|
10
|
+
-- policy (still dark: nothing enables row level security before phase 5).
|
|
11
|
+
-- - `trigger_events_settled_idx` (0189) is rebuilt with the predicate the retention sweep queries
|
|
12
|
+
-- (`state IN (...)`), so the planner can use it.
|
|
13
|
+
-- - `inbox_items` gains the indexes of its retention (the same sweep, `inbox.retentionDays`).
|
|
14
|
+
|
|
15
|
+
CREATE TABLE IF NOT EXISTS trigger_cause_handles (
|
|
16
|
+
id text PRIMARY KEY,
|
|
17
|
+
org_id text NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
|
|
18
|
+
-- the extension that asked for it; only it may emit with it
|
|
19
|
+
plugin text NOT NULL,
|
|
20
|
+
-- the run of the tool call it was made from (no foreign key, like trigger_events.cause_run_id)
|
|
21
|
+
run_id text NOT NULL,
|
|
22
|
+
-- the depth an event caused by the run gets, and the run's trigger account, at mint time
|
|
23
|
+
depth integer NOT NULL,
|
|
24
|
+
cause_account_id text,
|
|
25
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
26
|
+
expires_at timestamptz NOT NULL
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
-- the org's handles (an org's rows by org, as every org-scoped table)
|
|
30
|
+
CREATE INDEX IF NOT EXISTS trigger_cause_handles_org_idx
|
|
31
|
+
ON trigger_cause_handles (org_id);
|
|
32
|
+
|
|
33
|
+
-- the sweep: expired handles
|
|
34
|
+
CREATE INDEX IF NOT EXISTS trigger_cause_handles_expires_idx
|
|
35
|
+
ON trigger_cause_handles (expires_at);
|
|
36
|
+
|
|
37
|
+
DROP POLICY IF EXISTS org_scope ON trigger_cause_handles;
|
|
38
|
+
CREATE POLICY org_scope ON trigger_cause_handles
|
|
39
|
+
USING (rls_admits(org_id)) WITH CHECK (rls_admits(org_id));
|
|
40
|
+
|
|
41
|
+
DROP INDEX IF EXISTS trigger_events_settled_idx;
|
|
42
|
+
CREATE INDEX IF NOT EXISTS trigger_events_settled_idx
|
|
43
|
+
ON trigger_events (created_at) WHERE state IN ('dispatched', 'undelivered', 'depth_stopped');
|
|
44
|
+
|
|
45
|
+
-- the inbox retention: items resolved, or past their expiry, long enough ago
|
|
46
|
+
CREATE INDEX IF NOT EXISTS inbox_items_resolved_idx
|
|
47
|
+
ON inbox_items (resolved_at) WHERE resolved_at IS NOT NULL;
|
|
48
|
+
CREATE INDEX IF NOT EXISTS inbox_items_expires_idx
|
|
49
|
+
ON inbox_items (expires_at);
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- Directed events and acceptance (collaboration spec 2026-11-25 §4.4): what a person lets an
|
|
2
|
+
-- extension's directed events (an item assigned to them and their agent) do.
|
|
3
|
+
-- - `automation_acceptance`: a person's rule per trigger source in one org: `auto` (the kernel's
|
|
4
|
+
-- acceptance automation runs the work), `confirm` (an offer waits in their inbox) or `off`
|
|
5
|
+
-- (nothing runs). No row = the org's `automations.directedDefault` (default `auto`).
|
|
6
|
+
-- - `automation_offers`: work offered to a person under `confirm`, one per directed event: `open`
|
|
7
|
+
-- until the person accepts or declines it; one `open` past seven days counts as closed and is
|
|
8
|
+
-- set `expired` by the trigger event sweep (or when it is next read). `inbox_item_id` is the
|
|
9
|
+
-- push-carrying inbox item it resolves when decided; `automation_id` the acceptance automation
|
|
10
|
+
-- an accept delivered the event to.
|
|
11
|
+
-- - The acceptance automation of (org, person, source, agent) is one live row of `automations`
|
|
12
|
+
-- (`origin = 'acceptance'`): the partial unique index makes two directed events dispatched at
|
|
13
|
+
-- once create one.
|
|
14
|
+
-- Ids are text like every kernel id. Both tables carry `org_id` and their `org_scope` policies
|
|
15
|
+
-- (still dark: nothing enables row level security before phase 5).
|
|
16
|
+
|
|
17
|
+
CREATE TABLE IF NOT EXISTS automation_acceptance (
|
|
18
|
+
org_id text NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
|
|
19
|
+
account_id text NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
|
|
20
|
+
-- the trigger source (extension name)
|
|
21
|
+
source text NOT NULL,
|
|
22
|
+
mode text NOT NULL CHECK (mode IN ('auto', 'confirm', 'off')),
|
|
23
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
24
|
+
PRIMARY KEY (org_id, account_id, source)
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
DROP POLICY IF EXISTS org_scope ON automation_acceptance;
|
|
28
|
+
CREATE POLICY org_scope ON automation_acceptance
|
|
29
|
+
USING (rls_admits(org_id)) WITH CHECK (rls_admits(org_id));
|
|
30
|
+
|
|
31
|
+
CREATE TABLE IF NOT EXISTS automation_offers (
|
|
32
|
+
id text PRIMARY KEY,
|
|
33
|
+
org_id text NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
|
|
34
|
+
-- the person the work is offered to (the event's audience account)
|
|
35
|
+
account_id text NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
|
|
36
|
+
-- the directed event; the offer goes when retention deletes it
|
|
37
|
+
event_id text NOT NULL REFERENCES trigger_events(id) ON DELETE CASCADE,
|
|
38
|
+
-- the trigger source and the audience agent (no foreign key: an agent deleted since is
|
|
39
|
+
-- refused when the offer is accepted)
|
|
40
|
+
source text NOT NULL,
|
|
41
|
+
agent_id text NOT NULL,
|
|
42
|
+
-- the acceptance automation an accept delivered the event to
|
|
43
|
+
automation_id text,
|
|
44
|
+
inbox_item_id text,
|
|
45
|
+
state text NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'accepted', 'declined', 'expired')),
|
|
46
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
47
|
+
decided_at timestamptz
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
-- one offer per directed event (a retried dispatch finds it)
|
|
51
|
+
CREATE UNIQUE INDEX IF NOT EXISTS automation_offers_event_uq ON automation_offers (event_id);
|
|
52
|
+
|
|
53
|
+
-- a person's open offers (the inbox read)
|
|
54
|
+
CREATE INDEX IF NOT EXISTS automation_offers_open_idx
|
|
55
|
+
ON automation_offers (org_id, account_id) WHERE state = 'open';
|
|
56
|
+
|
|
57
|
+
DROP POLICY IF EXISTS org_scope ON automation_offers;
|
|
58
|
+
CREATE POLICY org_scope ON automation_offers
|
|
59
|
+
USING (rls_admits(org_id)) WITH CHECK (rls_admits(org_id));
|
|
60
|
+
|
|
61
|
+
-- one live acceptance automation per (org, person, source, agent)
|
|
62
|
+
CREATE UNIQUE INDEX IF NOT EXISTS automations_acceptance_uq
|
|
63
|
+
ON automations (org_id, account_id, agent_id, (trigger->>'source'))
|
|
64
|
+
WHERE origin = 'acceptance' AND deleted_at IS NULL;
|
|
65
|
+
|
|
66
|
+
-- the trigger event sweep's look for directed events still pending (a dispatch passed them over)
|
|
67
|
+
CREATE INDEX IF NOT EXISTS trigger_events_pending_directed_idx
|
|
68
|
+
ON trigger_events (created_at) WHERE state = 'pending' AND audience IS NOT NULL;
|
|
69
|
+
|
|
70
|
+
-- Directed events stored before this file were left `pending` by the dispatcher (it had no
|
|
71
|
+
-- acceptance routing yet), and their dispatch job is done: owe each its `trigger-dispatch` job
|
|
72
|
+
-- again through the outbox (the relay delivers it to the maintenance queue), with an id of its
|
|
73
|
+
-- own so a kept job of the first dispatch does not swallow it. The job routes them now. The
|
|
74
|
+
-- names are the kernel's constants: queue `MAINTENANCE_QUEUE_NAME`
|
|
75
|
+
-- (runtime/maintenance-queue.ts), job `TRIGGER_DISPATCH_JOB`, id `triggerDispatchJobId(id)` plus a
|
|
76
|
+
-- suffix and data `TriggerDispatchJobData` (automations/job-ids.ts). Events a worker from before
|
|
77
|
+
-- this release passes over during a rolling deploy are owed again by the trigger event sweep.
|
|
78
|
+
INSERT INTO outbox (queue, job_name, job_id, data, org_id)
|
|
79
|
+
SELECT 'cap-kernel-maintenance', 'trigger-dispatch', 'trigger-dispatch-' || id || '-acceptance',
|
|
80
|
+
jsonb_build_object('orgId', org_id, 'eventId', id), org_id
|
|
81
|
+
FROM trigger_events
|
|
82
|
+
WHERE state = 'pending' AND audience IS NOT NULL
|
|
83
|
+
ON CONFLICT DO NOTHING;
|