@capybara-hq/kernel 0.1.0-rc.80 → 0.1.0-rc.81
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/dist/admin/outbox-dead.service.js +133 -120
- package/dist/background/store.js +78 -67
- package/dist/background/tasks.service.d.ts +6 -0
- package/dist/background/tasks.service.js +3 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/plugin-import.js +45 -40
- package/dist/catalog/scope-sync-workers.js +3 -0
- package/dist/channels/inbound.d.ts +6 -1
- package/dist/channels/inbound.js +18 -1
- package/dist/client/client-reports.service.js +9 -6
- package/dist/config/schema.d.ts +5 -0
- package/dist/config/schema.js +6 -0
- package/dist/conversations/attachments.service.js +101 -96
- package/dist/conversations/events.d.ts +7 -0
- package/dist/conversations/events.js +9 -0
- package/dist/ctx/bus.js +5 -3
- package/dist/ctx/queue.js +4 -2
- package/dist/db/pool.d.ts +6 -0
- package/dist/db/pool.js +3 -2
- package/dist/devices/legacy-cleanup.job.js +73 -70
- package/dist/dial-in/gateway.d.ts +8 -0
- package/dist/dial-in/gateway.js +22 -2
- package/dist/extensions/admin-ops.js +22 -19
- package/dist/extensions/lifecycle-hooks.js +3 -1
- package/dist/http/app.js +3 -1
- package/dist/http/plugin-dispatch.js +8 -0
- package/dist/identity/auth.guard.js +13 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/infra/database/database.module.js +1 -0
- package/dist/infra/database/org-scope.d.ts +59 -0
- package/dist/infra/database/org-scope.js +68 -0
- package/dist/infra/database/rls-role.d.ts +11 -0
- package/dist/infra/database/rls-role.js +33 -0
- package/dist/infra/database/scoped-dialect.d.ts +18 -0
- package/dist/infra/database/scoped-dialect.js +245 -0
- package/dist/infra/outbox/outbox.dispatcher.js +98 -90
- package/dist/infra/outbox/outbox.service.js +29 -26
- package/dist/infra/queue/run-job.d.ts +8 -2
- package/dist/infra/queue/run-job.js +16 -3
- package/dist/infra/schedule/locked-tick.js +12 -4
- package/dist/infra/schedule/scheduled-runs.js +3 -1
- package/dist/lifecycle/kernel-lifecycle.d.ts +1 -0
- package/dist/lifecycle/kernel-lifecycle.js +16 -1
- package/dist/orgs/org.guard.js +4 -0
- package/dist/platform/sealed-reveals.js +9 -6
- package/dist/runtime/maintenance.js +6 -1
- package/dist/runtime/run-execution.d.ts +1 -0
- package/dist/runtime/run-execution.js +5 -0
- package/dist/runtime/run-orphan-check.job.js +20 -15
- package/dist/sandbox/legacy-retirement.js +438 -397
- package/dist/sandbox/modes.d.ts +10 -0
- package/dist/sandbox/modes.js +23 -0
- package/dist/sandbox/operation-reconcile.job.d.ts +14 -1
- package/dist/sandbox/operation-reconcile.job.js +35 -3
- package/dist/sandbox/permission-config-publication.js +83 -77
- package/dist/sandbox/sandbox.service.d.ts +24 -0
- package/dist/sandbox/sandbox.service.js +159 -62
- package/dist/schedules/store.js +17 -14
- package/dist/tools/builtins/sandbox/bash.js +16 -1
- package/dist/tools/drift.js +31 -24
- package/dist/tools/outputs.js +20 -17
- package/package.json +2 -2
- package/src/migrations/0182_rls_policies.sql +202 -0
|
@@ -34,6 +34,8 @@ import { insecurePublicUrlWarning } from "../identity/cookies.js";
|
|
|
34
34
|
import { waitWithin } from "../infra/close-within.js";
|
|
35
35
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
36
36
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
37
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
38
|
+
import { rlsEnforceProblem } from "../infra/database/rls-role.js";
|
|
37
39
|
import { GracefulShutdown } from "../infra/lifecycle/graceful-shutdown.service.js";
|
|
38
40
|
import { ReplicaPresence } from "../infra/presence/replica-presence.js";
|
|
39
41
|
import { KERNEL_BUS, KERNEL_LOGGER, KERNEL_POOL, REDIS } from "../infra/tokens.js";
|
|
@@ -199,7 +201,13 @@ let KernelLifecycle = class KernelLifecycle {
|
|
|
199
201
|
if (this.shutdown.isShuttingDown())
|
|
200
202
|
throw new Error("kernel stopped during start");
|
|
201
203
|
}
|
|
202
|
-
|
|
204
|
+
onApplicationBootstrap() {
|
|
205
|
+
// the start sequence (migrations, bootstrap, extension load, the gateway's connections) is
|
|
206
|
+
// deployment-wide work (RLS spec §5.1); what it starts keeps this scope until an entry point
|
|
207
|
+
// (a request, a job, a run, an event) enters its own
|
|
208
|
+
return asPlatform("kernel start sequence", () => this.startSequence());
|
|
209
|
+
}
|
|
210
|
+
async startSequence() {
|
|
203
211
|
if (this.shutdown.isShuttingDown())
|
|
204
212
|
throw new Error("kernel is stopped");
|
|
205
213
|
const { config, options: opts } = this;
|
|
@@ -215,6 +223,13 @@ let KernelLifecycle = class KernelLifecycle {
|
|
|
215
223
|
this.checkpoint();
|
|
216
224
|
await sql `select 1`.execute(this.pool);
|
|
217
225
|
this.checkpoint();
|
|
226
|
+
if (cfg().db.rls === "enforce") {
|
|
227
|
+
// never fails the start: enforcement is a backstop, and the operator decides
|
|
228
|
+
const problem = await rlsEnforceProblem(this.pool);
|
|
229
|
+
if (problem !== undefined) {
|
|
230
|
+
this.log.error(`db.rls is enforce, but ${problem}: PostgreSQL filters nothing by the org scope there`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
218
233
|
// In flight for the stop: aborted when the shutdown begins, which then waits for its session to end
|
|
219
234
|
await this.shutdown.trackInFlight(migrate(config.db.url, { schema: KERNEL_SCHEMA, dir: KERNEL_MIGRATIONS_DIR }, { connectTimeoutMs: config.db.connectTimeoutMs, signal: this.shutdown.signal }));
|
|
220
235
|
this.checkpoint();
|
package/dist/orgs/org.guard.js
CHANGED
|
@@ -12,6 +12,7 @@ import { Reflector } from "@nestjs/core";
|
|
|
12
12
|
import { ApiError } from "../errors.js";
|
|
13
13
|
import { AllowService } from "../identity/principal.js";
|
|
14
14
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
15
|
+
import { bindRequestScope } from "../infra/database/org-scope.js";
|
|
15
16
|
import { can, HUMAN_ONLY } from "./roles.js";
|
|
16
17
|
export const ORG_ACTION_KEY = "capybara:org:action";
|
|
17
18
|
/**
|
|
@@ -93,6 +94,9 @@ let OrgGuard = class OrgGuard {
|
|
|
93
94
|
const orgId = req.params.orgId;
|
|
94
95
|
if (!orgId)
|
|
95
96
|
throw new Error("OrgGuard: route has no :orgId parameter");
|
|
97
|
+
// the request works in this org from here on (RLS spec §5.1), the role check included: it
|
|
98
|
+
// reads only this org's membership; a refused check ends the request
|
|
99
|
+
bindRequestScope({ kind: "org", orgId });
|
|
96
100
|
const role = await authorizeOrgAction(this.db.db, p, orgId, action);
|
|
97
101
|
req.org = { orgId, role };
|
|
98
102
|
return true;
|
|
@@ -15,6 +15,7 @@ import { ApiError } from "../errors.js";
|
|
|
15
15
|
import { audit } from "../identity/audit.js";
|
|
16
16
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
17
17
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
18
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
18
19
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
19
20
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
21
|
import { KEYRING } from "../infra/tokens.js";
|
|
@@ -143,10 +144,12 @@ SealedReveals = __decorate([
|
|
|
143
144
|
export { SealedReveals };
|
|
144
145
|
/** Deletes the sealed reveals past `expires_at`; returns how many. */
|
|
145
146
|
export async function deleteExpiredSealedReveals(db) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
147
|
+
return asPlatform("deployment-wide expiry sweep over every org's sealed reveals", async () => {
|
|
148
|
+
// org-filter: deployment-wide expiry sweep over every org's sealed reveals (maintenance job)
|
|
149
|
+
const r = await db
|
|
150
|
+
.deleteFrom("platform_sealed_reveals")
|
|
151
|
+
.where("expires_at", "<=", sql `now()`)
|
|
152
|
+
.executeTakeFirst();
|
|
153
|
+
return Number(r.numDeletedRows);
|
|
154
|
+
});
|
|
152
155
|
}
|
|
@@ -13,6 +13,7 @@ import { BackgroundModule } from "../background/background.module.js";
|
|
|
13
13
|
import { BG_WAKE_JOB } from "../background/wake-rules.js";
|
|
14
14
|
import { OWNED_PLUGIN_CLEANUP_JOB } from "../catalog/job-ids.js";
|
|
15
15
|
import { OwnedPluginCleanup, OwnedPluginCleanupModule } from "../catalog/owned-plugin-cleanup.js";
|
|
16
|
+
import { EventLogModule } from "../conversations/event-log.js";
|
|
16
17
|
import { configuredProcessor } from "../infra/queue/configured-processor.js";
|
|
17
18
|
import { QueueModule } from "../infra/queue/queue.module.js";
|
|
18
19
|
import { runJob } from "../infra/queue/run-job.js";
|
|
@@ -97,7 +98,9 @@ let MaintenanceProcessor = class MaintenanceProcessor extends WorkerHost {
|
|
|
97
98
|
this.snapshots = snapshots;
|
|
98
99
|
}
|
|
99
100
|
process(job, token) {
|
|
100
|
-
return runJob(MAINTENANCE_QUEUE_NAME, job, () => this.dispatch(job, token)
|
|
101
|
+
return runJob(MAINTENANCE_QUEUE_NAME, job, () => this.dispatch(job, token), {
|
|
102
|
+
platform: "maintenance job (deployment-wide sweeps)",
|
|
103
|
+
});
|
|
101
104
|
}
|
|
102
105
|
/** Starts consuming (worker role, after the extensions loaded; built with `autorun: false`). */
|
|
103
106
|
start() {
|
|
@@ -253,6 +256,8 @@ MaintenanceModule = __decorate([
|
|
|
253
256
|
KernelScheduleModule,
|
|
254
257
|
// the exact stop a revoked background start owes (SandboxOperationReconcileJob)
|
|
255
258
|
BackgroundModule,
|
|
259
|
+
// the background list hint after reconciling a start (SandboxOperationReconcileJob)
|
|
260
|
+
EventLogModule,
|
|
256
261
|
RuntimeModule,
|
|
257
262
|
SandboxModule,
|
|
258
263
|
ToolsModule,
|
|
@@ -68,6 +68,7 @@ export declare class RunExecution {
|
|
|
68
68
|
constructor(db: KernelDb, xactLock: XactLock, txHost: TransactionHost);
|
|
69
69
|
claim(conversationId: string, runId: string, executionId: string, live?: () => boolean): Promise<ExecutionClaim | null>;
|
|
70
70
|
withClaim<T>(claim: ExecutionClaim, live: () => boolean, fn: () => Promise<T>): Promise<T>;
|
|
71
|
+
private claimed;
|
|
71
72
|
/**
|
|
72
73
|
* The admission (see the class), then the root and the current conversation; NO KEY UPDATE
|
|
73
74
|
* allows foreign key references while excluding deletion and other writers. The root stays
|
|
@@ -10,6 +10,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
10
10
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
11
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
12
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
13
|
+
import { withOrgScope } from "../infra/database/org-scope.js";
|
|
13
14
|
import { TransactionHost, withTransactionFence } from "../infra/database/transaction-host.js";
|
|
14
15
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
15
16
|
import { XactLock } from "../infra/database/xact-lock.js";
|
|
@@ -113,6 +114,10 @@ let RunExecution = class RunExecution {
|
|
|
113
114
|
return run ? claim : null;
|
|
114
115
|
}
|
|
115
116
|
withClaim(claim, live, fn) {
|
|
117
|
+
// the run works in its org (RLS spec §5.1): every query of the claimed run carries that scope
|
|
118
|
+
return withOrgScope(claim.orgId, () => this.claimed(claim, live, fn));
|
|
119
|
+
}
|
|
120
|
+
claimed(claim, live, fn) {
|
|
116
121
|
return invocationClaim.run(Object.freeze({ ...claim }), () => withTransactionFence(async ({ shared, prelude }) => {
|
|
117
122
|
const admitShared = shared && !exclusiveScope.getStore();
|
|
118
123
|
if (prelude && admitShared)
|
|
@@ -12,6 +12,7 @@ import { sql } from "kysely";
|
|
|
12
12
|
import { EventLog } from "../conversations/event-log.js";
|
|
13
13
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
14
14
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
15
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
15
16
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
16
17
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
17
18
|
import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
@@ -95,14 +96,16 @@ export async function orphanCandidates(db, statuses, minAgeMs, limit, after) {
|
|
|
95
96
|
// org-filter: outbox rows looked up by queue and deterministic kernel job id (unique across the
|
|
96
97
|
// deployment) in the deployment-wide orphan check; outbox.org_id is nullable
|
|
97
98
|
export async function jobInOutbox(db, queue, jobId) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
99
|
+
return asPlatform("orphan check: outbox rows by kernel job id", async () => {
|
|
100
|
+
const row = await db
|
|
101
|
+
.selectFrom("outbox")
|
|
102
|
+
.select("id")
|
|
103
|
+
.where("queue", "=", queue)
|
|
104
|
+
.where("job_id", "=", jobId)
|
|
105
|
+
.where("dead_at", "is", null)
|
|
106
|
+
.executeTakeFirst();
|
|
107
|
+
return row !== undefined;
|
|
108
|
+
});
|
|
106
109
|
}
|
|
107
110
|
/** Every page of {@link orphanCandidates}, at most {@link RUN_ORPHAN_CHECK_MAX_PAGES}. */
|
|
108
111
|
async function* candidatePages(db, statuses, minAgeMs, after) {
|
|
@@ -271,13 +274,15 @@ let RunOrphanCheck = class RunOrphanCheck {
|
|
|
271
274
|
// org-filter: outbox rows looked up by queue and deterministic kernel job id (unique across the
|
|
272
275
|
// deployment) in the deployment-wide orphan check; outbox.org_id is nullable
|
|
273
276
|
async hasDeadDelivery(c) {
|
|
274
|
-
return ((
|
|
275
|
-
.
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
277
|
+
return asPlatform("orphan check: outbox rows by kernel job id", async () => {
|
|
278
|
+
return ((await this.db
|
|
279
|
+
.selectFrom("outbox")
|
|
280
|
+
.select("id")
|
|
281
|
+
.where("queue", "=", c.queue)
|
|
282
|
+
.where("job_id", "=", c.jobId)
|
|
283
|
+
.where("dead_at", ">=", sql `${c.since}::timestamptz`)
|
|
284
|
+
.executeTakeFirst()) !== undefined);
|
|
285
|
+
});
|
|
281
286
|
}
|
|
282
287
|
/**
|
|
283
288
|
* Neither a conversation lock holder nor the run's job in BullMQ (nor, queued, in the outbox: an
|