@capybara-hq/kernel 0.1.0-rc.121 → 0.1.0-rc.123
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 +3 -3
- package/dist/background/finish.d.ts +5 -1
- package/dist/background/finish.js +29 -18
- package/dist/background/launch.d.ts +0 -5
- package/dist/background/launch.js +1 -6
- package/dist/background/managed.js +1 -4
- package/dist/background/poll.job.d.ts +3 -25
- package/dist/background/poll.job.js +20 -112
- package/dist/background/render.d.ts +2 -8
- package/dist/background/render.js +3 -17
- package/dist/background/stop.d.ts +15 -16
- package/dist/background/stop.js +39 -76
- package/dist/background/store.d.ts +17 -45
- package/dist/background/store.js +60 -94
- package/dist/background/tasks.service.d.ts +2 -5
- package/dist/background/tasks.service.js +9 -20
- package/dist/background/wake.js +3 -2
- package/dist/catalog/admin-catalog.controller.d.ts +2 -18
- package/dist/catalog/admin-catalog.controller.js +2 -28
- package/dist/catalog/catalog-content.d.ts +1 -5
- package/dist/catalog/catalog-items.d.ts +2 -6
- package/dist/catalog/catalog-items.js +5 -26
- package/dist/catalog/catalog-scope.service.d.ts +1 -121
- package/dist/catalog/catalog-scope.service.js +12 -552
- package/dist/catalog/catalog.service.d.ts +4 -15
- package/dist/catalog/catalog.service.js +7 -35
- package/dist/catalog/job-ids.d.ts +0 -18
- package/dist/catalog/job-ids.js +0 -20
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/org-catalog.service.js +6 -20
- package/dist/catalog/org-plugin-uploads.controller.d.ts +4 -19
- package/dist/catalog/org-plugin-uploads.controller.js +6 -34
- package/dist/catalog/org-plugin-uploads.d.ts +3 -10
- package/dist/catalog/org-plugin-uploads.js +4 -18
- package/dist/catalog/org-plugin-uploads.schemas.js +0 -1
- package/dist/catalog/owned-items.d.ts +6 -11
- package/dist/catalog/owned-items.js +10 -20
- package/dist/catalog/ownership.d.ts +3 -29
- package/dist/catalog/ownership.js +4 -54
- package/dist/catalog/platform-components.d.ts +5 -11
- package/dist/catalog/platform-components.js +8 -15
- package/dist/catalog/plugin-import.d.ts +3 -42
- package/dist/catalog/plugin-import.js +26 -195
- package/dist/catalog/plugin-install.js +1 -6
- package/dist/catalog/plugin-package.d.ts +1 -0
- package/dist/catalog/plugin-package.js +5 -0
- package/dist/catalog/plugin-upgrade.d.ts +3 -26
- package/dist/catalog/plugin-upgrade.js +20 -103
- package/dist/catalog/request-rules.js +4 -6
- package/dist/catalog/scope-sync.js +2 -4
- package/dist/catalog/scope.d.ts +2 -5
- package/dist/channels/jobs.d.ts +3 -3
- package/dist/channels/jobs.js +4 -3
- package/dist/channels/outbound.d.ts +1 -0
- package/dist/channels/outbound.js +32 -8
- package/dist/config/load.d.ts +0 -25
- package/dist/config/load.js +0 -57
- package/dist/config/schema.js +2 -3
- package/dist/conversations/attachments.service.js +3 -5
- package/dist/conversations/conversations.service.d.ts +3 -3
- package/dist/conversations/conversations.service.js +3 -3
- package/dist/conversations/job-org.d.ts +3 -0
- package/dist/conversations/job-org.js +11 -0
- package/dist/conversations/prompt-suggestions.d.ts +2 -7
- package/dist/conversations/prompt-suggestions.js +29 -27
- package/dist/conversations/titles.d.ts +3 -4
- package/dist/conversations/titles.js +19 -30
- package/dist/db/kernel-schema.d.ts +6 -16
- package/dist/db/pool.js +7 -2
- package/dist/devices/devices.controller.d.ts +0 -5
- package/dist/devices/devices.controller.js +2 -20
- package/dist/devices/devices.service.d.ts +1 -17
- package/dist/devices/devices.service.js +0 -22
- package/dist/errors.d.ts +1 -5
- package/dist/errors.js +1 -5
- package/dist/evals/judge.d.ts +3 -0
- package/dist/evals/judge.js +1 -1
- package/dist/evals/run-replay.d.ts +1 -1
- package/dist/evals/run-replay.js +26 -29
- package/dist/evals/step-replay.d.ts +3 -1
- package/dist/evals/step-replay.js +10 -1
- package/dist/evals/worker.d.ts +3 -1
- package/dist/evals/worker.js +53 -21
- package/dist/extensions/deployment-config-store.js +18 -14
- package/dist/extensions/effective-config.d.ts +1 -5
- package/dist/extensions/effective-config.js +1 -5
- package/dist/extensions/hooks.d.ts +3 -1
- package/dist/extensions/hooks.js +7 -2
- package/dist/extensions/installs.d.ts +2 -14
- package/dist/extensions/installs.js +10 -32
- package/dist/extensions/org-config.d.ts +3 -4
- package/dist/extensions/org-config.js +3 -4
- package/dist/extensions/plugin-guides.d.ts +0 -1
- package/dist/extensions/plugin-guides.js +0 -1
- package/dist/extensions/plugin-icons.d.ts +0 -1
- package/dist/extensions/plugin-icons.js +0 -3
- package/dist/files/blob-sweep.job.d.ts +2 -11
- package/dist/files/blob-sweep.job.js +7 -21
- package/dist/files/hash-job.d.ts +5 -15
- package/dist/files/hash-job.js +14 -33
- package/dist/files/library-jobs.module.d.ts +1 -6
- package/dist/files/library-jobs.module.js +3 -9
- package/dist/identity/api-tokens.js +54 -40
- package/dist/identity/auth.guard.js +2 -2
- package/dist/identity/sessions.js +7 -3
- package/dist/identity/settings.js +5 -21
- package/dist/inbox/inbox.service.d.ts +2 -3
- package/dist/inbox/org-notices.d.ts +0 -14
- package/dist/inbox/platform-inbox.service.d.ts +1 -8
- package/dist/inbox/platform-inbox.service.js +2 -6
- package/dist/inbox/platform-notices.d.ts +3 -3
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -3
- package/dist/infra/database/database.module.js +2 -1
- package/dist/infra/database/org-scope.d.ts +5 -3
- package/dist/infra/database/org-scope.js +7 -1
- package/dist/infra/database/scoped-dialect.d.ts +3 -1
- package/dist/infra/database/scoped-dialect.js +17 -5
- package/dist/infra/events/kernel-events.d.ts +2 -0
- package/dist/infra/outbox/outbox.service.d.ts +2 -0
- package/dist/infra/outbox/outbox.service.js +10 -0
- package/dist/infra/outbox/registry.d.ts +0 -41
- package/dist/infra/outbox/registry.js +0 -37
- package/dist/models/embeddings.d.ts +0 -5
- package/dist/models/embeddings.js +28 -35
- package/dist/models/images.d.ts +3 -8
- package/dist/models/images.js +19 -37
- package/dist/models/purposes.d.ts +7 -2
- package/dist/models/purposes.js +16 -3
- package/dist/orgs/device-network-setting.d.ts +0 -44
- package/dist/orgs/device-network-setting.js +0 -83
- package/dist/orgs/invitations.service.js +34 -30
- package/dist/orgs/offboarding.d.ts +2 -2
- package/dist/orgs/offboarding.js +2 -2
- package/dist/orgs/orgs.service.d.ts +1 -1
- package/dist/orgs/orgs.service.js +19 -15
- package/dist/orgs/settings.service.d.ts +19 -14
- package/dist/orgs/settings.service.js +12 -37
- package/dist/platform/exclusions.js +0 -11
- package/dist/platform/ops/catalog-admin.ops.js +0 -8
- package/dist/platform/ops/devices.ops.js +0 -28
- package/dist/platform/ops/extensions-admin.ops.js +3 -3
- package/dist/platform/ops/inbox.ops.js +5 -22
- package/dist/platform/ops/org-plugins.ops.d.ts +1 -3
- package/dist/platform/ops/org-plugins.ops.js +11 -67
- package/dist/roles/worker.module.js +0 -9
- package/dist/runtime/hook-calls.d.ts +13 -6
- package/dist/runtime/hook-calls.js +26 -4
- package/dist/runtime/maintenance.d.ts +3 -12
- package/dist/runtime/maintenance.js +25 -55
- package/dist/runtime/model-call.d.ts +4 -2
- package/dist/runtime/model-call.js +59 -58
- package/dist/runtime/providers.d.ts +27 -1
- package/dist/runtime/providers.js +57 -0
- package/dist/runtime/run-jobs.d.ts +1 -1
- package/dist/runtime/run-processors.d.ts +3 -9
- package/dist/runtime/run-worker.d.ts +2 -0
- package/dist/runtime/run-worker.js +23 -8
- package/dist/runtime/subagents/deliveries.js +6 -1
- package/dist/runtime/subagents/dispatch.d.ts +3 -3
- package/dist/runtime/write-base.service.d.ts +2 -3
- package/dist/runtime/write-base.service.js +3 -5
- package/dist/sandbox/device-sandbox-settings.d.ts +38 -92
- package/dist/sandbox/device-sandbox-settings.js +19 -325
- package/dist/sandbox/jobs.d.ts +3 -14
- package/dist/sandbox/jobs.js +2 -20
- package/dist/sandbox/modes.d.ts +5 -45
- package/dist/sandbox/modes.js +3 -60
- package/dist/sandbox/operation-authority.js +0 -11
- package/dist/sandbox/operation-intents.d.ts +32 -9
- package/dist/sandbox/operation-intents.js +241 -32
- package/dist/sandbox/operation-intents.module.js +2 -1
- package/dist/sandbox/operation-reconcile.job.d.ts +9 -11
- package/dist/sandbox/operation-reconcile.job.js +119 -33
- package/dist/sandbox/operation-revoke.d.ts +20 -0
- package/dist/sandbox/operation-revoke.js +3 -0
- package/dist/sandbox/permission-ceiling-writes.d.ts +1 -2
- package/dist/sandbox/permission-ceiling-writes.js +3 -6
- package/dist/sandbox/permission-config-publication.d.ts +1 -28
- package/dist/sandbox/permission-config-publication.js +14 -211
- package/dist/sandbox/permission-facts.d.ts +4 -15
- package/dist/sandbox/permission-facts.js +7 -29
- package/dist/sandbox/permission-instance-writes.d.ts +1 -4
- package/dist/sandbox/permission-instance-writes.js +1 -55
- package/dist/sandbox/providers.d.ts +1 -6
- package/dist/sandbox/providers.js +1 -15
- package/dist/sandbox/review/classifier.js +12 -13
- package/dist/sandbox/sandbox.module.js +3 -6
- package/dist/sandbox/sandbox.service.d.ts +17 -123
- package/dist/sandbox/sandbox.service.js +239 -998
- package/dist/shares/liveness.d.ts +1 -2
- package/dist/shares/liveness.js +1 -2
- package/dist/shares/shares.module.d.ts +1 -2
- package/dist/shares/shares.module.js +2 -5
- package/dist/shares/shares.service.js +1 -2
- package/dist/tools/builtins/image/generate.js +7 -15
- package/dist/tools/builtins/sandbox/common.d.ts +0 -2
- package/dist/tools/builtins/sandbox/common.js +1 -27
- package/dist/tools/builtins/sandbox/task-list.js +10 -10
- package/dist/tools/builtins/sandbox/task-output.d.ts +3 -10
- package/dist/tools/builtins/sandbox/task-output.js +13 -83
- package/dist/tools/builtins/sandbox/task-stop.js +18 -22
- package/dist/tools/builtins/vision/read-image.js +9 -10
- package/dist/tools/builtins/web/extract.js +9 -10
- package/package.json +3 -3
- package/src/migrations/0206_retire_org_extension_uploads.sql +125 -0
- package/src/migrations/0207_confirm_device_instance_destruction.sql +25 -0
- package/src/migrations/0208_convert_stored_permission_settings.sql +189 -0
- package/dist/background/retire-legacy-tasks.job.d.ts +0 -57
- package/dist/background/retire-legacy-tasks.job.js +0 -149
- package/dist/catalog/owned-plugin-cleanup.d.ts +0 -47
- package/dist/catalog/owned-plugin-cleanup.js +0 -140
- package/dist/config/permission-migration.d.ts +0 -34
- package/dist/config/permission-migration.js +0 -115
- package/dist/devices/legacy-cleanup.job.d.ts +0 -34
- package/dist/devices/legacy-cleanup.job.js +0 -147
- package/dist/files/library-backfill.d.ts +0 -63
- package/dist/files/library-backfill.js +0 -143
- package/dist/infra/cli/permission-cutover.d.ts +0 -59
- package/dist/infra/cli/permission-cutover.js +0 -107
- package/dist/sandbox/cloud-permission-snapshot.d.ts +0 -159
- package/dist/sandbox/cloud-permission-snapshot.js +0 -430
- package/dist/sandbox/cloud-permission-snapshot.module.d.ts +0 -8
- package/dist/sandbox/cloud-permission-snapshot.module.js +0 -34
- package/dist/sandbox/legacy-retirement.d.ts +0 -284
- package/dist/sandbox/legacy-retirement.js +0 -1165
- package/dist/sandbox/legacy-retirement.module.d.ts +0 -8
- package/dist/sandbox/legacy-retirement.module.js +0 -25
- package/dist/shares/share-objects.d.ts +0 -47
- package/dist/shares/share-objects.js +0 -93
|
@@ -13,6 +13,7 @@ import { sql } from "kysely";
|
|
|
13
13
|
import { z } from "zod";
|
|
14
14
|
import { ApiError } from "../errors.js";
|
|
15
15
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
16
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
16
17
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
17
18
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
18
19
|
import { OrgLock } from "../orgs/org-lock.js";
|
|
@@ -61,14 +62,15 @@ let ApiTokenService = class ApiTokenService {
|
|
|
61
62
|
}
|
|
62
63
|
async createPersonal(accountId, input) {
|
|
63
64
|
validate(input);
|
|
64
|
-
|
|
65
|
+
// org-filter: deployment-wide account token write with a null org_id.
|
|
66
|
+
return asPlatform("Create the account's personal token without an org", () => this.insertToken("personal", {
|
|
65
67
|
account_id: accountId,
|
|
66
68
|
org_id: null,
|
|
67
69
|
role: null,
|
|
68
70
|
name: input.name,
|
|
69
71
|
created_by: accountId,
|
|
70
72
|
expires_at: input.expiresAt ?? null,
|
|
71
|
-
});
|
|
73
|
+
}));
|
|
72
74
|
}
|
|
73
75
|
async createService(orgId, input) {
|
|
74
76
|
validate(input);
|
|
@@ -131,54 +133,64 @@ let ApiTokenService = class ApiTokenService {
|
|
|
131
133
|
: undefined;
|
|
132
134
|
if (!kind)
|
|
133
135
|
return null;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
136
|
+
return asPlatform("Authenticate the token before an org is known", async () => {
|
|
137
|
+
// org-filter: deployment-wide lookup by token hash before any org is known; the record carries
|
|
138
|
+
// the token's org (or none for a personal token)
|
|
139
|
+
const row = await this.db
|
|
140
|
+
.selectFrom("api_tokens")
|
|
141
|
+
.selectAll()
|
|
142
|
+
.where("token_hash", "=", sha256Hex(token))
|
|
143
|
+
.where("kind", "=", kind)
|
|
144
|
+
.where("revoked_at", "is", null)
|
|
145
|
+
.where((eb) => eb.or([eb("expires_at", "is", null), eb("expires_at", ">", sql `now()`)]))
|
|
146
|
+
.executeTakeFirst();
|
|
147
|
+
if (!row)
|
|
148
|
+
return null;
|
|
149
|
+
// Touch the token at most once every five minutes.
|
|
150
|
+
// org-filter: deployment-wide update by primary key of the token authenticated above.
|
|
151
|
+
const touched = await this.db
|
|
152
|
+
.updateTable("api_tokens")
|
|
153
|
+
.set({ last_used_at: sql `now()` })
|
|
154
|
+
.where("id", "=", row.id)
|
|
155
|
+
.where((eb) => eb.or([
|
|
156
|
+
eb("last_used_at", "is", null),
|
|
157
|
+
eb("last_used_at", "<", sql `now() - interval '5 minutes'`),
|
|
158
|
+
]))
|
|
159
|
+
.returning("last_used_at")
|
|
160
|
+
.executeTakeFirst();
|
|
161
|
+
const record = toRecord(row);
|
|
162
|
+
if (touched)
|
|
163
|
+
record.lastUsedAt = touched.last_used_at;
|
|
164
|
+
return record;
|
|
165
|
+
});
|
|
162
166
|
}
|
|
163
167
|
/** 列出该 scope 下未吊销的令牌(含已过期),按创建时间升序。 */
|
|
164
168
|
// org-filter: scoped() filters a service token by org_id and a personal one (org_id null) by
|
|
165
169
|
// account_id
|
|
166
170
|
async list(scope) {
|
|
167
|
-
const
|
|
171
|
+
const read = () => this.scoped(this.db.selectFrom("api_tokens").selectAll(), scope)
|
|
168
172
|
.where("revoked_at", "is", null)
|
|
169
173
|
.orderBy("created_at")
|
|
170
174
|
.orderBy("id")
|
|
171
175
|
.execute();
|
|
176
|
+
// org-filter: deployment-wide account token lookup for personal tokens.
|
|
177
|
+
const rows = "accountId" in scope
|
|
178
|
+
? await asPlatform("List an account's personal tokens", read)
|
|
179
|
+
: await read();
|
|
172
180
|
return rows.map(toRecord);
|
|
173
181
|
}
|
|
174
182
|
/** Revokes a token (in the context's transaction when there is one); missing, revoked or out of scope → `token.not_found`. */
|
|
175
183
|
// org-filter: scoped() filters a service token by org_id and a personal one (org_id null) by
|
|
176
184
|
// account_id
|
|
177
185
|
async revoke(id, scope) {
|
|
178
|
-
const
|
|
186
|
+
const write = () => this.scoped(this.db.updateTable("api_tokens").set({ revoked_at: sql `now()` }), scope)
|
|
179
187
|
.where("id", "=", id)
|
|
180
188
|
.where("revoked_at", "is", null)
|
|
181
189
|
.executeTakeFirst();
|
|
190
|
+
// org-filter: deployment-wide account token revocation for personal tokens.
|
|
191
|
+
const r = "accountId" in scope
|
|
192
|
+
? await asPlatform("Revoke an account's personal token", write)
|
|
193
|
+
: await write();
|
|
182
194
|
if (r.numUpdatedRows === 0n)
|
|
183
195
|
throw new ApiError("token.not_found", "Token not found");
|
|
184
196
|
}
|
|
@@ -192,15 +204,17 @@ let ApiTokenService = class ApiTokenService {
|
|
|
192
204
|
.where("revoked_at", "is", null)
|
|
193
205
|
.execute();
|
|
194
206
|
}
|
|
195
|
-
// org-filter: personal tokens belong to the account, not an org (org_id null)
|
|
196
207
|
async revokeAllPersonal(accountId) {
|
|
197
|
-
await
|
|
198
|
-
|
|
199
|
-
.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
208
|
+
await asPlatform("Revoke all personal tokens for an account", async () => {
|
|
209
|
+
// org-filter: deployment-wide account token revocation (personal tokens have a null org_id).
|
|
210
|
+
await this.db
|
|
211
|
+
.updateTable("api_tokens")
|
|
212
|
+
.set({ revoked_at: sql `now()` })
|
|
213
|
+
.where("kind", "=", "personal")
|
|
214
|
+
.where("account_id", "=", accountId)
|
|
215
|
+
.where("revoked_at", "is", null)
|
|
216
|
+
.execute();
|
|
217
|
+
});
|
|
204
218
|
}
|
|
205
219
|
scoped(q, scope) {
|
|
206
220
|
return "accountId" in scope
|
|
@@ -10,7 +10,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
10
10
|
import { Inject, Injectable } from "@nestjs/common";
|
|
11
11
|
import { Reflector } from "@nestjs/core";
|
|
12
12
|
import { ApiError } from "../errors.js";
|
|
13
|
-
import {
|
|
13
|
+
import { bindRequestScope } from "../infra/database/org-scope.js";
|
|
14
14
|
import { ORG_ACTION_KEY } from "../orgs/org.guard.js";
|
|
15
15
|
import { AccountsService } from "./accounts.js";
|
|
16
16
|
import { ApiTokenService } from "./api-tokens.js";
|
|
@@ -37,7 +37,7 @@ async function resolveBearer(req, s) {
|
|
|
37
37
|
if (bearer === undefined)
|
|
38
38
|
return undefined;
|
|
39
39
|
// looked up by token hash before any org is known: personal tokens have no org (RLS spec §9.4)
|
|
40
|
-
const rec = await
|
|
40
|
+
const rec = await s.tokens.authenticate(bearer);
|
|
41
41
|
if (!rec)
|
|
42
42
|
throw new ApiError("auth.required", "Authentication required");
|
|
43
43
|
if (rec.kind === "service") {
|
|
@@ -15,6 +15,7 @@ import { ACCESS_TOKEN_TTL_MAX_SECONDS } from "../config/schema.js";
|
|
|
15
15
|
import { ApiError } from "../errors.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, withOrgScope } 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, REDIS } from "../infra/tokens.js";
|
|
@@ -85,14 +86,15 @@ let SessionService = class SessionService {
|
|
|
85
86
|
* app then asks to choose or create one).
|
|
86
87
|
*/
|
|
87
88
|
async startingOrg(accountId) {
|
|
88
|
-
|
|
89
|
+
// org-filter: deployment-wide selection from the account's memberships across organizations.
|
|
90
|
+
const orgs = await asPlatform("Choose the account's initial org before an org is known", () => this.db
|
|
89
91
|
.selectFrom("memberships as m")
|
|
90
92
|
.innerJoin("orgs as o", "o.id", "m.org_id")
|
|
91
93
|
.innerJoin("accounts as a", "a.id", "m.account_id")
|
|
92
94
|
.select(["m.org_id as orgId", "a.last_org_id as lastOrgId"])
|
|
93
95
|
.where("m.account_id", "=", accountId)
|
|
94
96
|
.where("o.deleted_at", "is", null)
|
|
95
|
-
.execute();
|
|
97
|
+
.execute());
|
|
96
98
|
const last = orgs.find((o) => o.orgId === o.lastOrgId);
|
|
97
99
|
if (last)
|
|
98
100
|
return last.orgId;
|
|
@@ -291,7 +293,9 @@ let SessionService = class SessionService {
|
|
|
291
293
|
async switchCurrentOrg(sessionId, accountId, orgId) {
|
|
292
294
|
if (orgId === null)
|
|
293
295
|
return this.setCurrentOrg(sessionId, null);
|
|
294
|
-
|
|
296
|
+
if (orgId === "")
|
|
297
|
+
throw new ApiError("org.not_found", "Organization not found");
|
|
298
|
+
await withOrgScope(orgId, () => this.switchIn(sessionId, accountId, orgId));
|
|
295
299
|
}
|
|
296
300
|
async switchIn(sessionId, accountId, orgId) {
|
|
297
301
|
// 加锁顺序与移出成员一致:先组织行、再成员关系行(sessions.current_org_id 的外键检查也会锁组织行,
|
|
@@ -19,7 +19,7 @@ import { KernelDb } from "../infra/database/kernel-db.js";
|
|
|
19
19
|
import { TransactionHost, withOrderedLocks } from "../infra/database/transaction-host.js";
|
|
20
20
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
21
21
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
22
|
-
import { applyDevicesNetworkPatch, applyDevicesOutsideSandboxPatch, configuredDeviceSandbox, DEVICES_NETWORK_PATCH, DEVICES_NETWORK_SETTING, DEVICES_OUTSIDE_SANDBOX_PATCH, DEVICES_OUTSIDE_SANDBOX_SETTING,
|
|
22
|
+
import { applyDevicesNetworkPatch, applyDevicesOutsideSandboxPatch, configuredDeviceSandbox, DEVICES_NETWORK_PATCH, DEVICES_NETWORK_SETTING, DEVICES_OUTSIDE_SANDBOX_PATCH, DEVICES_OUTSIDE_SANDBOX_SETTING, orgDeviceSandboxOf, readDeviceSandboxSettingsView, readStoredDeviceSandbox, resolveDeviceSandboxSettings, } from "../sandbox/device-sandbox-settings.js";
|
|
23
23
|
import { devicesFollowingDefault, needsFullCeiling, needsOpenNetwork, PermissionCeilingWrites, } from "../sandbox/permission-ceiling-writes.js";
|
|
24
24
|
import { EXTERNAL_SHARING_SETTING, ExternalSharingValue, HTML_CDN_ALLOWLIST_DEFAULT, HtmlCdnAllowlistSchema, readExternalSharing, readHtmlCdnAllowlist, readShareMaxExpiry, SHARE_MAX_EXPIRY_DEFAULT, SHARE_MAX_EXPIRY_SETTING, ShareMaxExpiryDays, } from "../shares/settings.js";
|
|
25
25
|
import { assertPlatformDefaultEligible, lockThemeShared } from "../themes/theme-state.js";
|
|
@@ -635,8 +635,7 @@ let SettingsService = class SettingsService {
|
|
|
635
635
|
if (fixed.length > 0) {
|
|
636
636
|
throw new ApiError("deployment.setting_readonly", `These device sandbox settings are set in the startup config (file, Nacos or environment) and are read-only here: ${fixed.map(([path]) => path).join(", ")}`, fixed.map(([path, , source]) => ({ path, source })));
|
|
637
637
|
}
|
|
638
|
-
const
|
|
639
|
-
const stored = await readStoredDeviceSandbox(this.db.db, this.log, configuredCeiling);
|
|
638
|
+
const stored = await readStoredDeviceSandbox(this.db.db, this.log);
|
|
640
639
|
const next = {
|
|
641
640
|
outsideSandbox: patch.devicesOutsideSandbox === undefined
|
|
642
641
|
? stored.outsideSandbox
|
|
@@ -911,25 +910,10 @@ let SettingsService = class SettingsService {
|
|
|
911
910
|
if (patch.devicesOutsideSandbox !== undefined || patch.devicesNetwork !== undefined) {
|
|
912
911
|
const r = await this.checkDeviceSandbox(patch, opts);
|
|
913
912
|
deviceSandboxChange = { before: r.before, after: r.after };
|
|
914
|
-
|
|
915
|
-
// old keys the publication conversion has not reached yet (a mismatch window before
|
|
916
|
-
// activation): the new keys are stored as every reader sees them, and each old key is
|
|
917
|
-
// rewritten in step for replicas of the previous version (Full allowed only when running
|
|
918
|
-
// outside the sandbox is allowed and the effective network ceiling is open)
|
|
919
|
-
if (patch.devicesOutsideSandbox !== undefined || legacy.length > 0)
|
|
913
|
+
if (patch.devicesOutsideSandbox !== undefined)
|
|
920
914
|
writes.push([DEVICES_OUTSIDE_SANDBOX_SETTING, r.next.outsideSandbox]);
|
|
921
|
-
if (patch.devicesNetwork !== undefined
|
|
922
|
-
|
|
923
|
-
await this.db
|
|
924
|
-
.deleteFrom("deployment_settings")
|
|
925
|
-
.where("key", "=", DEVICES_NETWORK_SETTING)
|
|
926
|
-
.execute();
|
|
927
|
-
else
|
|
928
|
-
writes.push([DEVICES_NETWORK_SETTING, r.next.network]);
|
|
929
|
-
}
|
|
930
|
-
const inStep = legacyPermissionFor(r.next.outsideSandbox.permission, r.after.network.ceiling);
|
|
931
|
-
for (const key of legacy)
|
|
932
|
-
writes.push([key, legacyPlatformValue(key, inStep)]);
|
|
915
|
+
if (patch.devicesNetwork !== undefined)
|
|
916
|
+
writes.push([DEVICES_NETWORK_SETTING, r.next.network]);
|
|
933
917
|
const outsideOff = r.before.outsideSandbox.permission === "allowed" &&
|
|
934
918
|
r.after.outsideSandbox.permission === "off";
|
|
935
919
|
const outsideChanged = r.before.outsideSandbox.permission !== r.after.outsideSandbox.permission;
|
|
@@ -10,7 +10,7 @@ import type { OrgNoticeDetails } from "./org-notices.js";
|
|
|
10
10
|
export declare const ORG_NOTICES_LIST_MAX = 100;
|
|
11
11
|
/**
|
|
12
12
|
* A notice for the org's admins (`org_notices`), shown until one of them dismisses it:
|
|
13
|
-
* `item_shared
|
|
13
|
+
* `item_shared` or `shared_item_updated`, with `detail` in the
|
|
14
14
|
* shape of {@link OrgNoticeDetails} (plugin scope phase 3, P3-22).
|
|
15
15
|
*/
|
|
16
16
|
export type OrgNoticeView = {
|
|
@@ -40,8 +40,7 @@ export declare const INBOX_MAX_ITEMS = 100;
|
|
|
40
40
|
* - `automation_attention`: the current user's own automation that needs them (spec §8): paused
|
|
41
41
|
* for any reason but their own pause, its latest run failed, or its latest run was refused tools
|
|
42
42
|
* or connections; derived live, gone once the task is resumed or runs clean;
|
|
43
|
-
* - the org notices (`item_shared
|
|
44
|
-
* `upload_rejected`; `type` = the notice's kind): for org admins, until one dismisses it
|
|
43
|
+
* - the org notices (`item_shared` and `shared_item_updated`): for org admins, until one dismisses it
|
|
45
44
|
* (`POST inbox/notices/:id/dismiss`);
|
|
46
45
|
* - `item`: the current user's own open personal items (`inbox_items`, collaboration spec §4.9),
|
|
47
46
|
* posted by the kernel or an extension, until they click an action or dismiss it
|
|
@@ -23,20 +23,6 @@ export interface OrgNoticeDetails {
|
|
|
23
23
|
to: string;
|
|
24
24
|
owner: Owner;
|
|
25
25
|
};
|
|
26
|
-
/** `displayName`: the plugin's display name, which the inbox names it by (null without one). */
|
|
27
|
-
upload_confirmed: {
|
|
28
|
-
itemId: string;
|
|
29
|
-
name: string;
|
|
30
|
-
displayName: LocalizedName;
|
|
31
|
-
version: string;
|
|
32
|
-
};
|
|
33
|
-
upload_rejected: {
|
|
34
|
-
itemId: string;
|
|
35
|
-
name: string;
|
|
36
|
-
displayName: LocalizedName;
|
|
37
|
-
version: string;
|
|
38
|
-
reason: string | null;
|
|
39
|
-
};
|
|
40
26
|
}
|
|
41
27
|
type LocalizedName = {
|
|
42
28
|
zh: string;
|
|
@@ -57,8 +57,6 @@ export interface ExtensionDisabledView {
|
|
|
57
57
|
* it; `import`: bring in a marketplace entry), as `GET /admin/catalog/requests` lists them;
|
|
58
58
|
* - `permission_confirmation`: a platform plugin whose extension permissions wait for a super
|
|
59
59
|
* admin's confirmation (`GET /admin/catalog/items?pending=true`), `id` = the item's;
|
|
60
|
-
* - `org_upload_pending`: an org-owned plugin whose uploaded version (with an extension) waits for
|
|
61
|
-
* the super admin in 待确认 (ruling P3-22; confirmed or rejected there), `id` = the item's;
|
|
62
60
|
* - `scope_sync_failed`: an open scope sync failure, `id` = `<itemId>/<orgId>`;
|
|
63
61
|
* - `extension_disabled`: a plugin its scope turns on whose extension is disabled on the deployment
|
|
64
62
|
* ({@link ExtensionDisabledView}), `id` = the item's; gone once the extension is enabled (or the
|
|
@@ -78,11 +76,6 @@ export type PlatformInboxItem = {
|
|
|
78
76
|
id: string;
|
|
79
77
|
createdAt: string;
|
|
80
78
|
item: CatalogItemView;
|
|
81
|
-
} | {
|
|
82
|
-
type: "org_upload_pending";
|
|
83
|
-
id: string;
|
|
84
|
-
createdAt: string;
|
|
85
|
-
item: CatalogItemView;
|
|
86
79
|
} | {
|
|
87
80
|
type: "scope_sync_failed";
|
|
88
81
|
id: string;
|
|
@@ -105,7 +98,7 @@ export type PlatformInboxItem = {
|
|
|
105
98
|
notice: PlatformNoticeView<"push_auth_failed">;
|
|
106
99
|
};
|
|
107
100
|
/** Every {@link PlatformInboxItem} type, in the order the admin overview lists them. */
|
|
108
|
-
export declare const PLATFORM_INBOX_TYPES: readonly ["catalog_request", "permission_confirmation", "
|
|
101
|
+
export declare const PLATFORM_INBOX_TYPES: readonly ["catalog_request", "permission_confirmation", "scope_sync_failed", "extension_disabled", "ownership_transferred", "push_auth_failed"];
|
|
109
102
|
/** How many items of each type wait: every type, zeros included. */
|
|
110
103
|
export type PlatformInboxCounts = Record<PlatformInboxItem["type"], number>;
|
|
111
104
|
/** An open platform notice (`platform_notices`) of one kind. */
|
|
@@ -22,7 +22,6 @@ export const SCOPE_SYNC_FAILURES_LIST_MAX = 200;
|
|
|
22
22
|
export const PLATFORM_INBOX_TYPES = [
|
|
23
23
|
"catalog_request",
|
|
24
24
|
"permission_confirmation",
|
|
25
|
-
"org_upload_pending",
|
|
26
25
|
"scope_sync_failed",
|
|
27
26
|
"extension_disabled",
|
|
28
27
|
"ownership_transferred",
|
|
@@ -63,7 +62,7 @@ let PlatformInboxService = class PlatformInboxService {
|
|
|
63
62
|
request: r,
|
|
64
63
|
})),
|
|
65
64
|
...pending.map((i) => ({
|
|
66
|
-
type:
|
|
65
|
+
type: "permission_confirmation",
|
|
67
66
|
id: i.id,
|
|
68
67
|
createdAt: i.updatedAt,
|
|
69
68
|
item: i,
|
|
@@ -97,11 +96,9 @@ let PlatformInboxService = class PlatformInboxService {
|
|
|
97
96
|
: a.id > b.id
|
|
98
97
|
? 1
|
|
99
98
|
: 0);
|
|
100
|
-
const orgUploads = pending.filter((i) => i.owner.kind === "org").length;
|
|
101
99
|
const byType = {
|
|
102
100
|
catalog_request: requestCount,
|
|
103
|
-
permission_confirmation: pending.length
|
|
104
|
-
org_upload_pending: orgUploads,
|
|
101
|
+
permission_confirmation: pending.length,
|
|
105
102
|
scope_sync_failed: failureCount,
|
|
106
103
|
extension_disabled: disabledCount,
|
|
107
104
|
ownership_transferred: noticeCount.ownership_transferred,
|
|
@@ -286,7 +283,6 @@ function disabledExtensionRows(kdb) {
|
|
|
286
283
|
.where("i.deleted_at", "is", null)
|
|
287
284
|
.where("i.builtin", "=", false)
|
|
288
285
|
.where("i.disabled", "=", false)
|
|
289
|
-
.where("i.awaiting_confirmation", "=", false)
|
|
290
286
|
.where("i.scope", "<>", "optional")
|
|
291
287
|
.where("e.desired_state", "=", "disabled")
|
|
292
288
|
.where((eb) => eb.exists(eb
|
|
@@ -3,7 +3,7 @@ import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
|
3
3
|
export type PlatformNoticeKind = "ownership_transferred" | "push_auth_failed";
|
|
4
4
|
/** The `detail` of each platform notice kind. */
|
|
5
5
|
export interface PlatformNoticeDetails {
|
|
6
|
-
/**
|
|
6
|
+
/** An org's item was transferred to the platform. */
|
|
7
7
|
ownership_transferred: {
|
|
8
8
|
ref: OwnedRef;
|
|
9
9
|
name: string;
|
|
@@ -11,8 +11,8 @@ export interface PlatformNoticeDetails {
|
|
|
11
11
|
id: string;
|
|
12
12
|
name: string;
|
|
13
13
|
};
|
|
14
|
-
reason: "owner_deleted" | "org_deleted";
|
|
15
|
-
/** How many
|
|
14
|
+
reason: "owner_deleted" | "org_deleted" | "extension_upload_retired";
|
|
15
|
+
/** How many orgs still use it. */
|
|
16
16
|
usedBy: number;
|
|
17
17
|
};
|
|
18
18
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,6 @@ export { type ConfigField, changedPaths, configFields, secretValues } from "./co
|
|
|
11
11
|
export { fileSource } from "./config/file-source.js";
|
|
12
12
|
export { ConfigError, type ConfigSource, loadConfig, loadConfigWithSources, resolveConfig, selectConfigSource, } from "./config/load.js";
|
|
13
13
|
export { createNacosSource, type NacosSourceOptions } from "./config/nacos-source.js";
|
|
14
|
-
export { legacyPermissionEnvRenames, migratePermissionConfigText, type PermissionConfigMigration, type PermissionEnvRename, } from "./config/permission-migration.js";
|
|
15
14
|
export { type FieldMeta, type KernelConfig, KernelConfigSchema, MARKETPLACE_OFFICIAL_URL_DEFAULT, meta, PACKAGES_MAX_BYTES_DEFAULT, } from "./config/schema.js";
|
|
16
15
|
export { type ConfigSourceKind, configSourceKind, extensionConfigSources, } from "./config/sources.js";
|
|
17
16
|
export type { ConversationEvent } from "./conversations/events.js";
|
|
@@ -60,7 +59,6 @@ export { type IssuedSession, REFRESH_RACE_GRACE_MS, SessionService, } from "./id
|
|
|
60
59
|
export { DEPLOYMENT_SETTINGS, type DeploymentSettings, SettingsService, } from "./identity/settings.js";
|
|
61
60
|
export { type KernelCliOptions, openKernelCli, openKernelCliOverPool, } from "./infra/cli/kernel-cli.module.js";
|
|
62
61
|
export { rewrapMasterKeys } from "./infra/cli/master-key-rewrap.js";
|
|
63
|
-
export { type CutoverReplica, type PermissionCutoverReport, permissionCutoverFreeze, permissionCutoverReport, permissionCutoverSnapshot, } from "./infra/cli/permission-cutover.js";
|
|
64
62
|
export { asPlatform, currentScope, type OrgScope, setUnscopedQueryCheck, type UnscopedQueryCheck, withOrgScope, } from "./infra/database/org-scope.js";
|
|
65
63
|
export type { RlsMode } from "./infra/database/scoped-dialect.js";
|
|
66
64
|
export { onProcessFault } from "./infra/health/process-fault.js";
|
|
@@ -83,9 +81,7 @@ export type { AgentLoop, LoopContext, LoopOutcome } from "./runtime/loop.js";
|
|
|
83
81
|
export { MAINTENANCE_QUEUE_NAME } from "./runtime/maintenance-queue.js";
|
|
84
82
|
export { type OrphanCandidate, queuedRunsWithoutJob } from "./runtime/run-orphan-check.job.js";
|
|
85
83
|
export { runJobId } from "./runtime/run-queue.js";
|
|
86
|
-
export type { CloudSnapshotProgress, CloudSnapshotResult, CloudSnapshotRun, } from "./sandbox/cloud-permission-snapshot.js";
|
|
87
84
|
export { SANDBOX_DESTROY_JOB, SANDBOX_SUSPEND_JOB, } from "./sandbox/jobs.js";
|
|
88
|
-
export { CutoverBarrierError, type LegacyFreezeResult, LegacyFreezeUnsettledError, type LegacyRetirementReport, } from "./sandbox/legacy-retirement.js";
|
|
89
85
|
export { PermissionAuthorityModule } from "./sandbox/permission-authority.module.js";
|
|
90
86
|
export { PermissionConfigPublication } from "./sandbox/permission-config-publication.js";
|
|
91
87
|
export { SANDBOX_LOCK_TTL_MS, SandboxService, type SandboxUseInfo, } from "./sandbox/sandbox.service.js";
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,6 @@ export { changedPaths, configFields, secretValues } from "./config/fields.js";
|
|
|
11
11
|
export { fileSource } from "./config/file-source.js";
|
|
12
12
|
export { ConfigError, loadConfig, loadConfigWithSources, resolveConfig, selectConfigSource, } from "./config/load.js";
|
|
13
13
|
export { createNacosSource } from "./config/nacos-source.js";
|
|
14
|
-
export { legacyPermissionEnvRenames, migratePermissionConfigText, } from "./config/permission-migration.js";
|
|
15
14
|
export { KernelConfigSchema, MARKETPLACE_OFFICIAL_URL_DEFAULT, meta, PACKAGES_MAX_BYTES_DEFAULT, } from "./config/schema.js";
|
|
16
15
|
export { configSourceKind, extensionConfigSources, } from "./config/sources.js";
|
|
17
16
|
export { createBus, createBusSubscriber } from "./ctx/bus.js";
|
|
@@ -58,7 +57,6 @@ export { REFRESH_RACE_GRACE_MS, SessionService, } from "./identity/sessions.js";
|
|
|
58
57
|
export { DEPLOYMENT_SETTINGS, SettingsService, } from "./identity/settings.js";
|
|
59
58
|
export { openKernelCli, openKernelCliOverPool, } from "./infra/cli/kernel-cli.module.js";
|
|
60
59
|
export { rewrapMasterKeys } from "./infra/cli/master-key-rewrap.js";
|
|
61
|
-
export { permissionCutoverFreeze, permissionCutoverReport, permissionCutoverSnapshot, } from "./infra/cli/permission-cutover.js";
|
|
62
60
|
export { asPlatform, currentScope, setUnscopedQueryCheck, withOrgScope, } from "./infra/database/org-scope.js";
|
|
63
61
|
export { onProcessFault } from "./infra/health/process-fault.js";
|
|
64
62
|
export { GUARD_CIRCUIT_LIMIT, GUARD_CIRCUIT_WINDOW_MS, installProcessGuards, } from "./infra/lifecycle/process-guards.js";
|
|
@@ -82,7 +80,6 @@ export { MAINTENANCE_QUEUE_NAME } from "./runtime/maintenance-queue.js";
|
|
|
82
80
|
export { queuedRunsWithoutJob } from "./runtime/run-orphan-check.job.js";
|
|
83
81
|
export { runJobId } from "./runtime/run-queue.js";
|
|
84
82
|
export { SANDBOX_DESTROY_JOB, SANDBOX_SUSPEND_JOB, } from "./sandbox/jobs.js";
|
|
85
|
-
export { CutoverBarrierError, LegacyFreezeUnsettledError, } from "./sandbox/legacy-retirement.js";
|
|
86
83
|
export { PermissionAuthorityModule } from "./sandbox/permission-authority.module.js";
|
|
87
84
|
export { PermissionConfigPublication } from "./sandbox/permission-config-publication.js";
|
|
88
85
|
export { SANDBOX_LOCK_TTL_MS, SandboxService, } from "./sandbox/sandbox.service.js";
|
|
@@ -15,6 +15,7 @@ import { GracefulShutdownModule } from "../lifecycle/graceful-shutdown.module.js
|
|
|
15
15
|
import { GracefulShutdown } from "../lifecycle/graceful-shutdown.service.js";
|
|
16
16
|
import { KERNEL_LOGGER, KERNEL_POOL } from "../tokens.js";
|
|
17
17
|
import { KernelDb } from "./kernel-db.js";
|
|
18
|
+
import { unscopedQueryCheck } from "./org-scope.js";
|
|
18
19
|
import { TransactionHost } from "./transaction-host.js";
|
|
19
20
|
import { XactLock } from "./xact-lock.js";
|
|
20
21
|
/**
|
|
@@ -68,7 +69,7 @@ let DatabaseModule = class DatabaseModule {
|
|
|
68
69
|
connectTimeoutMs: db.connectTimeoutMs,
|
|
69
70
|
idleTimeoutMs: db.idleTimeoutMs,
|
|
70
71
|
onClientError: (err) => log.warn("pg client error", { err }),
|
|
71
|
-
rls: db.rls,
|
|
72
|
+
rls: db.rls === "off" && unscopedQueryCheck() ? "scoped" : db.rls,
|
|
72
73
|
});
|
|
73
74
|
},
|
|
74
75
|
},
|
|
@@ -49,11 +49,13 @@ export declare function bindRequestScope(scope: OrgScope): void;
|
|
|
49
49
|
export declare function scopeKey(scope: OrgScope | undefined): string;
|
|
50
50
|
/**
|
|
51
51
|
* Test seam for RLS phase 4 (spec §7): called by the scoped pool for each query that runs with no
|
|
52
|
-
* scope and names an org-scoped table, with that table
|
|
53
|
-
*
|
|
52
|
+
* scope and names an org-scoped table, with that table, the SQL and the call site when available.
|
|
53
|
+
* The integration setup records unscoped paths; undefined (the default) checks nothing. Only the scoped pool
|
|
54
54
|
* (`db.rls` `scoped` or `enforce`) calls it.
|
|
55
55
|
*/
|
|
56
|
-
export type UnscopedQueryCheck = (table: string, sql: string) => void;
|
|
56
|
+
export type UnscopedQueryCheck = (table: string, sql: string, origin?: Error) => void;
|
|
57
57
|
export declare function setUnscopedQueryCheck(check: UnscopedQueryCheck | undefined): void;
|
|
58
|
+
/** Lets a pool test exercise an intentional unscoped query without changing another caller's check. */
|
|
59
|
+
export declare function withUnscopedQueryCheck<T>(check: UnscopedQueryCheck | undefined, fn: () => T): T;
|
|
58
60
|
/** @internal the installed check (read by the scoped pool). */
|
|
59
61
|
export declare function unscopedQueryCheck(): UnscopedQueryCheck | undefined;
|
|
@@ -59,10 +59,16 @@ export function scopeKey(scope) {
|
|
|
59
59
|
return scope.kind === "org" ? `org:${scope.orgId}` : "platform";
|
|
60
60
|
}
|
|
61
61
|
let unscopedCheck;
|
|
62
|
+
const localQueryCheck = new AsyncLocalStorage();
|
|
62
63
|
export function setUnscopedQueryCheck(check) {
|
|
63
64
|
unscopedCheck = check;
|
|
64
65
|
}
|
|
66
|
+
/** Lets a pool test exercise an intentional unscoped query without changing another caller's check. */
|
|
67
|
+
export function withUnscopedQueryCheck(check, fn) {
|
|
68
|
+
return localQueryCheck.run({ check }, fn);
|
|
69
|
+
}
|
|
65
70
|
/** @internal the installed check (read by the scoped pool). */
|
|
66
71
|
export function unscopedQueryCheck() {
|
|
67
|
-
|
|
72
|
+
const local = localQueryCheck.getStore();
|
|
73
|
+
return local ? local.check : unscopedCheck;
|
|
68
74
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Driver, PostgresDialect, type PostgresDialectConfig } from "kysely";
|
|
1
|
+
import { type Driver, type KyselyPlugin, PostgresDialect, type PostgresDialectConfig } from "kysely";
|
|
2
2
|
/**
|
|
3
3
|
* `db.rls`: how the kernel pool carries the org scope to Postgres (RLS spec §5.2, §7).
|
|
4
4
|
* - `off`: nothing is sent; the pool is a plain Kysely pool (no extra round trip).
|
|
@@ -7,6 +7,8 @@ import { type Driver, PostgresDialect, type PostgresDialectConfig } from "kysely
|
|
|
7
7
|
* - `enforce`: the same with `capybara.rls = enforce`: the policies filter by the scope.
|
|
8
8
|
*/
|
|
9
9
|
export type RlsMode = "off" | "scoped" | "enforce";
|
|
10
|
+
/** Installed only for the test check. Capture the call site before pool checkout loses it. */
|
|
11
|
+
export declare const captureUnscopedQueryOrigins: KyselyPlugin;
|
|
10
12
|
/**
|
|
11
13
|
* The kernel pool's dialect: Kysely's Postgres dialect, with {@link ScopedDriver} unless `rls` is
|
|
12
14
|
* `off` (then nothing changes, not even a round trip).
|
|
@@ -11,6 +11,18 @@ function setScopeQuery(scope, rls, local) {
|
|
|
11
11
|
/** Tables of the `capybara` schema with an `org_id` column (for the unscoped-query check). */
|
|
12
12
|
const ORG_TABLES_SQL = "select table_name from information_schema.columns " +
|
|
13
13
|
"where table_schema = 'capybara' and column_name = 'org_id'";
|
|
14
|
+
const queryOrigins = new WeakMap();
|
|
15
|
+
/** Installed only for the test check. Capture the call site before pool checkout loses it. */
|
|
16
|
+
export const captureUnscopedQueryOrigins = {
|
|
17
|
+
transformQuery({ node, queryId }) {
|
|
18
|
+
if (!currentScope())
|
|
19
|
+
queryOrigins.set(queryId, new Error());
|
|
20
|
+
return node;
|
|
21
|
+
},
|
|
22
|
+
async transformResult({ result }) {
|
|
23
|
+
return result;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
14
26
|
const TABLE_REF = /"capybara"\."([a-z0-9_]+)"|\bcapybara\.([a-z0-9_]+)/g;
|
|
15
27
|
/**
|
|
16
28
|
* One checkout of a pool connection. Before a query runs, the scope of the caller's async context
|
|
@@ -57,21 +69,21 @@ class ScopedConnection {
|
|
|
57
69
|
await this.inner.executeQuery(setScopeQuery(scope, this.rls, this.inTransaction));
|
|
58
70
|
this.applied = key;
|
|
59
71
|
}
|
|
60
|
-
async check(scope,
|
|
72
|
+
async check(scope, query) {
|
|
61
73
|
const check = unscopedQueryCheck();
|
|
62
74
|
if (!check || scope)
|
|
63
75
|
return;
|
|
64
76
|
const tables = await this.orgTables(this.inner);
|
|
65
|
-
for (const m of sql.matchAll(TABLE_REF)) {
|
|
77
|
+
for (const m of query.sql.matchAll(TABLE_REF)) {
|
|
66
78
|
const table = m[1] ?? m[2];
|
|
67
79
|
if (table && tables.has(table))
|
|
68
|
-
check(table, sql);
|
|
80
|
+
check(table, query.sql, queryOrigins.get(query.queryId));
|
|
69
81
|
}
|
|
70
82
|
}
|
|
71
83
|
executeQuery(query) {
|
|
72
84
|
const scope = currentScope();
|
|
73
85
|
return this.serial(async () => {
|
|
74
|
-
await this.check(scope, query
|
|
86
|
+
await this.check(scope, query);
|
|
75
87
|
await this.ensure(scope);
|
|
76
88
|
return this.inner.executeQuery(query);
|
|
77
89
|
});
|
|
@@ -92,7 +104,7 @@ class ScopedConnection {
|
|
|
92
104
|
});
|
|
93
105
|
void this.serial(async () => {
|
|
94
106
|
try {
|
|
95
|
-
await this.check(scope, query
|
|
107
|
+
await this.check(scope, query);
|
|
96
108
|
await this.ensure(scope);
|
|
97
109
|
}
|
|
98
110
|
catch (error) {
|
|
@@ -74,6 +74,7 @@ export interface KernelEventMap {
|
|
|
74
74
|
* next `ensureRun` (a message, the end of a run, a page opening).
|
|
75
75
|
*/
|
|
76
76
|
"subagent.delivered": {
|
|
77
|
+
orgId: string;
|
|
77
78
|
conversationId: string;
|
|
78
79
|
};
|
|
79
80
|
/**
|
|
@@ -82,6 +83,7 @@ export interface KernelEventMap {
|
|
|
82
83
|
* event is re-derived by the next `ensureRun` (a message, the end of a run, a page opening).
|
|
83
84
|
*/
|
|
84
85
|
"background_task.finished": {
|
|
86
|
+
orgId: string;
|
|
85
87
|
conversationId: string;
|
|
86
88
|
};
|
|
87
89
|
}
|
|
@@ -49,6 +49,8 @@ export declare class OutboxService {
|
|
|
49
49
|
* {@link OutboxConflictError}, so the caller's transaction fails. Undefined data is stored as JSON null.
|
|
50
50
|
*/
|
|
51
51
|
add<T>(job: OutboxJob<T>): Promise<boolean>;
|
|
52
|
+
/** Parks an exhausted operation directly in the admin dead-letter queue with its retry payload. */
|
|
53
|
+
addDead<T>(job: OutboxJob<T>, attempts: number, reason: string): Promise<void>;
|
|
52
54
|
/**
|
|
53
55
|
* {@link add} for many jobs in one multi-row insert per {@link ADD_MANY_CHUNK} jobs (an eval
|
|
54
56
|
* run's cases). A job whose (queue, jobId) is already owed is skipped without comparing its data.
|
|
@@ -112,6 +112,13 @@ let OutboxService = class OutboxService {
|
|
|
112
112
|
return false;
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
|
+
/** Parks an exhausted operation directly in the admin dead-letter queue with its retry payload. */
|
|
116
|
+
async addDead(job, attempts, reason) {
|
|
117
|
+
await asPlatform("the outbox is a deployment-wide table", () => this.db
|
|
118
|
+
.insertInto("outbox")
|
|
119
|
+
.values({ ...rowOf(job), dead_at: new Date(), attempts, last_error: reason })
|
|
120
|
+
.execute());
|
|
121
|
+
}
|
|
115
122
|
/**
|
|
116
123
|
* {@link add} for many jobs in one multi-row insert per {@link ADD_MANY_CHUNK} jobs (an eval
|
|
117
124
|
* run's cases). A job whose (queue, jobId) is already owed is skipped without comparing its data.
|
|
@@ -154,6 +161,9 @@ let OutboxService = class OutboxService {
|
|
|
154
161
|
__decorate([
|
|
155
162
|
Transactional({ propagation: "mandatory" })
|
|
156
163
|
], OutboxService.prototype, "add", null);
|
|
164
|
+
__decorate([
|
|
165
|
+
Transactional({ propagation: "mandatory" })
|
|
166
|
+
], OutboxService.prototype, "addDead", null);
|
|
157
167
|
__decorate([
|
|
158
168
|
Transactional({ propagation: "mandatory" })
|
|
159
169
|
], OutboxService.prototype, "addMany", null);
|