@capybara-hq/kernel 0.1.0-rc.32 → 0.1.0-rc.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/bootstrap.json +3 -3
  2. package/dist/catalog/marketplaces.service.d.ts +1 -1
  3. package/dist/catalog/requests.service.d.ts +8 -6
  4. package/dist/catalog/requests.service.js +52 -29
  5. package/dist/channels/inbound.d.ts +0 -25
  6. package/dist/channels/inbound.js +26 -1
  7. package/dist/channels/phrases.d.ts +28 -0
  8. package/dist/channels/phrases.js +28 -0
  9. package/dist/config/schema.d.ts +4 -0
  10. package/dist/config/schema.js +7 -1
  11. package/dist/conversations/conversations.controller.d.ts +4 -1
  12. package/dist/conversations/conversations.controller.js +14 -2
  13. package/dist/conversations/conversations.service.d.ts +43 -3
  14. package/dist/conversations/conversations.service.js +93 -19
  15. package/dist/conversations/event-log.d.ts +3 -3
  16. package/dist/conversations/event-log.js +3 -3
  17. package/dist/conversations/events.d.ts +24 -0
  18. package/dist/conversations/events.js +69 -0
  19. package/dist/db/kernel-schema.d.ts +20 -0
  20. package/dist/devices/conversation-devices.d.ts +57 -3
  21. package/dist/devices/conversation-devices.js +63 -11
  22. package/dist/devices/device-projects.service.d.ts +162 -0
  23. package/dist/devices/device-projects.service.js +503 -0
  24. package/dist/devices/devices.controller.d.ts +15 -2
  25. package/dist/devices/devices.controller.js +60 -3
  26. package/dist/devices/devices.module.js +15 -2
  27. package/dist/devices/devices.schemas.js +9 -1
  28. package/dist/devices/devices.service.js +8 -4
  29. package/dist/devices/dto.d.ts +31 -0
  30. package/dist/devices/dto.js +38 -1
  31. package/dist/devices/working-root.d.ts +41 -0
  32. package/dist/devices/working-root.js +50 -0
  33. package/dist/errors.d.ts +39 -0
  34. package/dist/errors.js +39 -0
  35. package/dist/evals/run-replay.d.ts +9 -1
  36. package/dist/evals/run-replay.js +15 -1
  37. package/dist/local-devices/local-device-provider.d.ts +37 -1
  38. package/dist/local-devices/local-device-provider.js +33 -0
  39. package/dist/local-devices/machines.d.ts +2 -1
  40. package/dist/local-devices/machines.js +4 -1
  41. package/dist/local-devices/network-grants.d.ts +1 -1
  42. package/dist/local-devices/network-grants.js +2 -3
  43. package/dist/local-devices/policy-summary.js +1 -0
  44. package/dist/local-devices/relay.d.ts +2 -0
  45. package/dist/local-devices/relay.js +3 -1
  46. package/dist/local-devices/targets.d.ts +6 -1
  47. package/dist/local-devices/targets.js +14 -0
  48. package/dist/orgs/settings.controller.d.ts +3 -1
  49. package/dist/orgs/settings.controller.js +7 -3
  50. package/dist/orgs/settings.service.d.ts +78 -31
  51. package/dist/orgs/settings.service.js +109 -52
  52. package/dist/overrides.d.ts +4 -0
  53. package/dist/overrides.js +2 -0
  54. package/dist/platform/ops/agent-skills.ops.js +2 -1
  55. package/dist/platform/ops/devices.ops.d.ts +3 -1
  56. package/dist/platform/ops/devices.ops.js +136 -3
  57. package/dist/platform/ops/org-settings.ops.js +20 -10
  58. package/dist/runtime/context/build.d.ts +3 -0
  59. package/dist/runtime/context/build.js +14 -0
  60. package/dist/runtime/context/compaction.js +2 -0
  61. package/dist/runtime/context/project-root.d.ts +43 -0
  62. package/dist/runtime/context/project-root.js +85 -0
  63. package/dist/runtime/model-call.js +25 -1
  64. package/dist/runtime/project/knowledge.d.ts +129 -0
  65. package/dist/runtime/project/knowledge.js +248 -0
  66. package/dist/runtime/project/knowledge.service.d.ts +85 -0
  67. package/dist/runtime/project/knowledge.service.js +288 -0
  68. package/dist/runtime/run-worker.d.ts +5 -0
  69. package/dist/runtime/run-worker.js +27 -6
  70. package/dist/runtime/runtime.module.js +7 -0
  71. package/dist/sandbox/modes.d.ts +31 -19
  72. package/dist/sandbox/modes.js +43 -24
  73. package/dist/sandbox/review/review.service.d.ts +14 -2
  74. package/dist/sandbox/review/review.service.js +2 -0
  75. package/dist/sandbox/review/rules.d.ts +6 -4
  76. package/dist/sandbox/review/rules.js +185 -62
  77. package/dist/sandbox/review/types.d.ts +14 -0
  78. package/dist/sandbox/sandbox.service.d.ts +20 -2
  79. package/dist/sandbox/sandbox.service.js +86 -3
  80. package/dist/schedules/grant-rules.d.ts +2 -0
  81. package/dist/schedules/grant-rules.js +3 -1
  82. package/dist/schedules/schedule-math.js +21 -2
  83. package/dist/schedules/scheduled-tasks.service.js +3 -1
  84. package/dist/skills/catalog.d.ts +6 -1
  85. package/dist/skills/catalog.js +18 -4
  86. package/dist/skills/invoke.d.ts +64 -2
  87. package/dist/skills/invoke.js +214 -6
  88. package/dist/skills/skills.module.js +5 -0
  89. package/dist/tools/approvals.service.d.ts +6 -0
  90. package/dist/tools/builtins/sandbox/bash.js +19 -4
  91. package/dist/tools/builtins/sandbox/common.d.ts +36 -6
  92. package/dist/tools/builtins/sandbox/common.js +69 -8
  93. package/dist/tools/builtins/sandbox/edit.js +2 -2
  94. package/dist/tools/builtins/sandbox/glob.js +3 -2
  95. package/dist/tools/builtins/sandbox/grep.js +2 -1
  96. package/dist/tools/builtins/sandbox/read.js +3 -3
  97. package/dist/tools/builtins/sandbox/write.js +2 -2
  98. package/dist/tools/builtins/skill-load.d.ts +47 -2
  99. package/dist/tools/builtins/skill-load.js +160 -2
  100. package/dist/tools/network-asks.d.ts +7 -0
  101. package/dist/tools/network-asks.js +11 -1
  102. package/dist/tools/pipeline.d.ts +1 -1
  103. package/dist/tools/pipeline.js +60 -9
  104. package/dist/tools/registry.d.ts +6 -0
  105. package/package.json +2 -2
  106. package/src/migrations/0133_project_root.sql +30 -0
  107. package/src/migrations/0134_org_cloud_network_delta.sql +38 -0
@@ -1,4 +1,4 @@
1
- import type { SandboxRoots } from "@capybara-hq/plugin-sdk";
1
+ import type { DeviceFeature, SandboxRoots } from "@capybara-hq/plugin-sdk";
2
2
  import { KernelDb } from "../infra/database/kernel-db.js";
3
3
  /**
4
4
  * Whether a local device can still be served: `active` (live and an unrevoked machine),
@@ -25,4 +25,9 @@ export declare class DeviceTargets {
25
25
  * active machine, or none reported yet).
26
26
  */
27
27
  machineRoots(deviceId: string): Promise<SandboxRoots | null>;
28
+ /**
29
+ * The features the device's active machine announced in its last hello (known ones only);
30
+ * empty without an active machine.
31
+ */
32
+ machineFeatures(deviceId: string): Promise<DeviceFeature[]>;
28
33
  }
@@ -9,6 +9,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
9
  };
10
10
  import { Inject, Injectable } from "@nestjs/common";
11
11
  import { KernelDb } from "../infra/database/kernel-db.js";
12
+ import { deviceFeaturesOf } from "./policy-summary.js";
12
13
  const ROOT_KEYS = ["workspace", "skills", "tmp"];
13
14
  /** Whether two sets of roots name the same three directories. */
14
15
  export const sameRoots = (a, b) => ROOT_KEYS.every((k) => a[k] === b[k]);
@@ -65,6 +66,19 @@ let DeviceTargets = class DeviceTargets {
65
66
  return null;
66
67
  return r;
67
68
  }
69
+ /**
70
+ * The features the device's active machine announced in its last hello (known ones only);
71
+ * empty without an active machine.
72
+ */
73
+ async machineFeatures(deviceId) {
74
+ const row = await this.db
75
+ .selectFrom("device_machines")
76
+ .select("features")
77
+ .where("device_id", "=", deviceId)
78
+ .where("revoked_at", "is", null)
79
+ .executeTakeFirst();
80
+ return deviceFeaturesOf(row?.features);
81
+ }
68
82
  };
69
83
  DeviceTargets = __decorate([
70
84
  Injectable(),
@@ -1,6 +1,6 @@
1
1
  import { type Principal } from "../identity/principal.js";
2
2
  import { type OrgContext } from "./org.guard.js";
3
- import { type OrgSandboxLimits, type OrgSettings, type OrgSettingsDeployment, type OrgSettingsPatch, OrgSettingsService } from "./settings.service.js";
3
+ import { type OrgEffectiveNetwork, type OrgSandboxLimits, type OrgSettings, type OrgSettingsDeployment, type OrgSettingsPatch, OrgSettingsService } from "./settings.service.js";
4
4
  /**
5
5
  * 组织设置(`/api/v1/orgs/:orgId/settings`):成员可读,修改需 `org.update`(admin)。
6
6
  * 响应带 `deployment`(部署默认值,如沙箱出站),设置页据此展示「跟随部署」时的实际取值;
@@ -9,6 +9,8 @@ import { type OrgSandboxLimits, type OrgSettings, type OrgSettingsDeployment, ty
9
9
  type SettingsResponse = {
10
10
  settings: OrgSettings;
11
11
  deployment: OrgSettingsDeployment;
12
+ /** The org's effective sandbox-mode network (org egress ceiling spec §D4). */
13
+ effectiveNetwork: OrgEffectiveNetwork;
12
14
  sandboxLimits: OrgSandboxLimits;
13
15
  };
14
16
  export declare class OrgSettingsController {
@@ -21,18 +21,22 @@ let OrgSettingsController = class OrgSettingsController {
21
21
  this.settings = settings;
22
22
  }
23
23
  async get(org) {
24
+ const settings = await this.settings.get(org.orgId);
24
25
  return {
25
- settings: await this.settings.get(org.orgId),
26
+ settings,
26
27
  deployment: await this.settings.deployment(),
28
+ effectiveNetwork: this.settings.effectiveNetwork(settings),
27
29
  sandboxLimits: await this.settings.sandboxLimits(org.orgId),
28
30
  };
29
31
  }
30
32
  async update(principal, org, body) {
31
33
  const p = requireUser(principal);
34
+ // an API token may narrow auto mode and the cloud network, never widen them (auto mode spec §4.3)
35
+ const settings = await this.settings.update(org.orgId, body, p.account.id, patTokenId(p), undefined, { onlyNarrows: patTokenId(p) !== undefined });
32
36
  return {
33
- // an API token may narrow auto mode and the cloud network, never widen them (auto mode spec §4.3)
34
- settings: await this.settings.update(org.orgId, body, p.account.id, patTokenId(p), undefined, { onlyNarrows: patTokenId(p) !== undefined }),
37
+ settings,
35
38
  deployment: await this.settings.deployment(),
39
+ effectiveNetwork: this.settings.effectiveNetwork(settings),
36
40
  sandboxLimits: await this.settings.sandboxLimits(org.orgId),
37
41
  };
38
42
  }
@@ -20,9 +20,9 @@ import { OrgLock } from "./org-lock.js";
20
20
  * - `autoMode`: whether devices of the org may run in auto mode (auto mode spec §4.3); absent
21
21
  * follows the platform's `devices.autoMode`, and an invalid stored value reads as `off`
22
22
  * (narrower, never wider). Setting `allowed` while the platform is `off` is refused.
23
- * - `cloudNetwork`: the org's network for cloud devices (auto mode spec §4.3): `open` (any public
24
- * host; the default) or `allowlist` with `allowHosts` (the deployment's list when omitted). An
25
- * invalid stored value reads as `{ mode: "allowlist", allowHosts: [] }` (narrower, never wider).
23
+ * - `cloudNetwork`: the org's sandbox-mode network as a delta over the platform's allowlist (org
24
+ * egress ceiling spec §D1, {@link CloudNetworkSettingSchema}). An invalid stored value reads as
25
+ * `{ mode: "allowlist", base: "none" }`, the empty allowlist (narrower, never wider).
26
26
  * - `platformTool`: the `Platform` builtin for the whole org (Platform tool spec §3): `on`,
27
27
  * `read-only` (writes listed as unavailable) or `off` (default `on`).
28
28
  * - `timeZone`: the org's default time zone (IANA; absent = UTC) for senders without one of their
@@ -37,19 +37,31 @@ import { OrgLock } from "./org-lock.js";
37
37
  /** The org cloud-network allowlist's host limit. */
38
38
  export declare const ORG_SANDBOX_ALLOW_HOSTS_MAX = 200;
39
39
  /**
40
- * The org's network for cloud devices (auto mode spec §4.3): `open` (any public host; loopback,
41
- * private and cloud metadata addresses are blocked outside the container) or `allowlist` with
42
- * `allowHosts`. `allowHosts` given **replaces** the deployment's list (the org may drop the presets
43
- * and add its own hosts); omitted, the deployment's `sandbox.limits.allowHosts` applies.
44
- * The host list is also the ceiling a local device in sandbox mode reaches (the device intersects
45
- * it with its own list), so narrowing it limits local sandbox devices too.
40
+ * The org's sandbox-mode network (org egress ceiling spec §D1), stored as a delta over the
41
+ * platform's `sandbox.limits.allowHosts`, never a copy of it, so platform changes keep reaching the
42
+ * org:
43
+ * - `mode`: the cloud network of a cloud device in sandbox mode, `open` (any public host; loopback,
44
+ * private and cloud metadata addresses are blocked outside the container) or `allowlist`; absent
45
+ * follows the platform's default (open). `sandbox.limits.orgHosts: within` makes it the allowlist.
46
+ * - `base`: `platform` (the default: start from the platform's list) or `none` (only the org's own
47
+ * hosts).
48
+ * - `extraHosts`: hosts the org adds (ignored outside the platform list under `within`).
49
+ * - `excludedHosts`: platform entries the org drops (exact entries; any host is accepted, since the
50
+ * platform may drop an entry later).
51
+ * The effective hosts ({@link orgEffectiveHosts} in `sandbox/modes.ts`) are also the ceiling a local
52
+ * device in sandbox mode reaches (the device intersects them with its own list).
46
53
  */
47
54
  export declare const CloudNetworkSettingSchema: z.ZodObject<{
48
- mode: z.ZodEnum<{
55
+ mode: z.ZodOptional<z.ZodEnum<{
49
56
  allowlist: "allowlist";
50
57
  open: "open";
51
- }>;
52
- allowHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
58
+ }>>;
59
+ base: z.ZodOptional<z.ZodEnum<{
60
+ none: "none";
61
+ platform: "platform";
62
+ }>>;
63
+ extraHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
64
+ excludedHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
53
65
  }, z.core.$strict>;
54
66
  export type CloudNetworkSetting = z.infer<typeof CloudNetworkSettingSchema>;
55
67
  /** What an invalid stored `cloudNetwork` reads as: the allowlist with no host (never wider). */
@@ -154,11 +166,16 @@ export declare const OrgSettingsSchema: z.ZodObject<{
154
166
  off: "off";
155
167
  }>>>;
156
168
  cloudNetwork: z.ZodCatch<z.ZodOptional<z.ZodObject<{
157
- mode: z.ZodEnum<{
169
+ mode: z.ZodOptional<z.ZodEnum<{
158
170
  allowlist: "allowlist";
159
171
  open: "open";
160
- }>;
161
- allowHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
172
+ }>>;
173
+ base: z.ZodOptional<z.ZodEnum<{
174
+ none: "none";
175
+ platform: "platform";
176
+ }>>;
177
+ extraHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
178
+ excludedHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
162
179
  }, z.core.$strict>>>;
163
180
  wakeOnBackgroundTask: z.ZodCatch<z.ZodOptional<z.ZodBoolean>>;
164
181
  timeZone: z.ZodCatch<z.ZodOptional<z.ZodString>>;
@@ -223,11 +240,16 @@ export declare const OrgSettingsPatchSchema: z.ZodObject<{
223
240
  off: "off";
224
241
  }>>>;
225
242
  cloudNetwork: z.ZodOptional<z.ZodNullable<z.ZodObject<{
226
- mode: z.ZodEnum<{
243
+ mode: z.ZodOptional<z.ZodEnum<{
227
244
  allowlist: "allowlist";
228
245
  open: "open";
229
- }>;
230
- allowHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
246
+ }>>;
247
+ base: z.ZodOptional<z.ZodEnum<{
248
+ none: "none";
249
+ platform: "platform";
250
+ }>>;
251
+ extraHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
252
+ excludedHosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
231
253
  }, z.core.$strict>>>;
232
254
  timeZone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
233
255
  theme: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -279,17 +301,24 @@ export interface OrgSandboxLimits {
279
301
  memoryMb: number;
280
302
  source: "deployment" | "override";
281
303
  }
304
+ /** The org's effective sandbox-mode network (`effectiveNetwork` of the settings responses). */
305
+ export interface OrgEffectiveNetwork {
306
+ mode: "open" | "allowlist";
307
+ hosts: string[];
308
+ }
282
309
  /** The deployment defaults the settings page shows for "follow the platform" (`GET` returns them). */
283
310
  export interface OrgSettingsDeployment {
284
311
  /** The platform's auto-mode switch and where it comes from. */
285
312
  autoMode: DevicesAutoModeView;
286
313
  /**
287
- * The deployment's default cloud network: `open` (the normal default; `allowlist` when
288
- * `sandbox.limits.egress` is `none`, the kill switch) and the host list an allowlist starts from.
314
+ * The platform's network: its allowlist (what an org's delta applies to), whether orgs may go
315
+ * beyond it (`sandbox.limits.orgHosts`) and the kill switch (`off`: `sandbox.limits.egress` is
316
+ * `none`).
289
317
  */
290
318
  cloudNetwork: {
291
- mode: "open" | "allowlist";
292
319
  allowHosts: string[];
320
+ orgHosts: "extend" | "within";
321
+ off: boolean;
293
322
  };
294
323
  /** The platform's effective attribution defaults for agent-created commits and pull requests. */
295
324
  attribution: PlatformAttribution;
@@ -309,6 +338,14 @@ export declare function readOrgSettings(db: KernelSchemaDb, orgId: string): Prom
309
338
  export declare function invalidStoredAutoMode(raw: unknown): boolean;
310
339
  /** Whether a stored settings object holds a `cloudNetwork` this version reads as an empty allowlist. */
311
340
  export declare function invalidStoredCloudNetwork(raw: unknown): boolean;
341
+ /**
342
+ * Whether `next` (absent: follow the platform) widens `prev` (spec §D3), both normalized (mode
343
+ * open, base platform, empty lists): the open mode where it was the allowlist; base none to
344
+ * platform; with base platform, dropping one of the stored exclusions; an extra host no stored
345
+ * extra covers. Over-strict on purpose: moving to base none with a platform entry as an extra is a
346
+ * narrowing these rules refuse (a token cannot pin a platform host the platform may drop later).
347
+ */
348
+ export declare function cloudNetworkWidens(prev: CloudNetworkSetting | undefined, next: CloudNetworkSetting | undefined): boolean;
312
349
  /** 组织设置服务:读取与修改(修改需 `org.update`,锁内实时复核角色,写审计)。 */
313
350
  export declare class OrgSettingsService {
314
351
  private readonly db;
@@ -331,11 +368,27 @@ export declare class OrgSettingsService {
331
368
  private parseStored;
332
369
  /** 部署默认值(设置页展示「跟随部署」时的实际取值)。 */
333
370
  deployment(): Promise<OrgSettingsDeployment>;
371
+ /**
372
+ * The org's effective sandbox-mode network (spec §D3): the cloud network's mode and the hosts,
373
+ * from the platform's `sandbox.limits` and the org's stored delta.
374
+ */
375
+ effectiveNetwork(settings: Pick<OrgSettings, "cloudNetwork">): OrgEffectiveNetwork;
334
376
  /** 组织实际生效的沙箱 cpu / 内存(只读;由超级管理员在部署设置里覆盖)。 */
335
377
  sandboxLimits(orgId: string): Promise<OrgSandboxLimits>;
336
378
  get(orgId: string): Promise<OrgSettings>;
337
- /** The refusals of {@link update} that need no read of the org (the Platform tool's precheck). */
379
+ /**
380
+ * The refusals of {@link update} that need no read of the org (the Platform tool's precheck):
381
+ * auto mode where the platform turned it off.
382
+ */
338
383
  check(patch: OrgSettingsPatch): Promise<void>;
384
+ /**
385
+ * Under `sandbox.limits.orgHosts: within` (spec §D2), refuses what would widen past the platform
386
+ * ceiling compared with the stored value `before`: the open cloud network where the org did not
387
+ * store it, or an extra host outside the platform's allowlist that the org did not store. Stored
388
+ * values are sent back as they are (the PATCH replaces `cloudNetwork` whole) and have no effect
389
+ * under the ceiling (they are filtered on read), so they never block a write.
390
+ */
391
+ assertWithinCeiling(before: OrgSettings, patch: OrgSettingsPatch): void;
339
392
  update(orgId: string, patch: OrgSettingsPatch, actorAccountId: string, actorTokenId?: string,
340
393
  /** Merged into the audit detail (the Platform tool's `via: "agent"`). */
341
394
  auditDetail?: Record<string, unknown>,
@@ -351,15 +404,9 @@ export declare class OrgSettingsService {
351
404
  * never the Platform tool or an API token):
352
405
  * - `autoMode`: `allowed` where the organization runs `off` (an absent value follows the
353
406
  * platform); clearing it follows the platform too, never wider than the platform allows;
354
- * - `cloudNetwork`: `open` where the network is the allowlist, or a host the current allowlist
355
- * does not have (the deployment's `sandbox.limits.allowHosts` when the org gives none).
356
- * `cloudNetwork.allowHosts` is also the local sandbox-mode ceiling (`orgAllowedDomains` in
357
- * `sandbox/modes.ts` becomes `deviceLimits.localAllowedDomains`, which the device intersects
358
- * with its own list): an org with no `cloudNetwork` still caps its local devices at the
359
- * deployment's list, and an org whose network is `open` still caps them at its own `allowHosts`.
360
- * The host comparison therefore applies to both shapes, whatever the target mode: a patch may
361
- * only keep or lower the host set. Switching from `open` is a widening by itself, and the host
362
- * set must not grow even when the target stays `open` (spec §4.2, §4.3).
407
+ * - `cloudNetwork`: compared as stored deltas ({@link cloudNetworkWidens}), never as effective
408
+ * values: under the kill switch or the platform ceiling the effective values hide a widening
409
+ * that takes effect when the platform relaxes (org egress ceiling spec §D3).
363
410
  */
364
411
  assertOnlyNarrows(before: OrgSettings, patch: OrgSettingsPatch): Promise<void>;
365
412
  private updateIn;
@@ -19,6 +19,7 @@ import { KernelDb } from "../infra/database/kernel-db.js";
19
19
  import { TransactionHost } 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 { coversHost, orgCloudMode, orgEffectiveHosts, orgHostOutsideCeiling, } from "../sandbox/modes.js";
22
23
  import { readDevicesAutoMode, } from "../sandbox/platform-auto-mode.js";
23
24
  import { assertThemeOnIn } from "../themes/theme-state.js";
24
25
  import { IanaTimeZone } from "../time-zone.js";
@@ -35,9 +36,9 @@ import { OrgLock, OrgLockModule } from "./org-lock.js";
35
36
  * - `autoMode`: whether devices of the org may run in auto mode (auto mode spec §4.3); absent
36
37
  * follows the platform's `devices.autoMode`, and an invalid stored value reads as `off`
37
38
  * (narrower, never wider). Setting `allowed` while the platform is `off` is refused.
38
- * - `cloudNetwork`: the org's network for cloud devices (auto mode spec §4.3): `open` (any public
39
- * host; the default) or `allowlist` with `allowHosts` (the deployment's list when omitted). An
40
- * invalid stored value reads as `{ mode: "allowlist", allowHosts: [] }` (narrower, never wider).
39
+ * - `cloudNetwork`: the org's sandbox-mode network as a delta over the platform's allowlist (org
40
+ * egress ceiling spec §D1, {@link CloudNetworkSettingSchema}). An invalid stored value reads as
41
+ * `{ mode: "allowlist", base: "none" }`, the empty allowlist (narrower, never wider).
41
42
  * - `platformTool`: the `Platform` builtin for the whole org (Platform tool spec §3): `on`,
42
43
  * `read-only` (writes listed as unavailable) or `off` (default `on`).
43
44
  * - `timeZone`: the org's default time zone (IANA; absent = UTC) for senders without one of their
@@ -51,37 +52,48 @@ import { OrgLock, OrgLockModule } from "./org-lock.js";
51
52
  */
52
53
  /** The org cloud-network allowlist's host limit. */
53
54
  export const ORG_SANDBOX_ALLOW_HOSTS_MAX = 200;
55
+ /** One host list of the org's cloud network: at most {@link ORG_SANDBOX_ALLOW_HOSTS_MAX} valid, unique hosts. */
56
+ const orgHostList = (name) => z
57
+ .array(z.string())
58
+ .max(ORG_SANDBOX_ALLOW_HOSTS_MAX)
59
+ .superRefine((hosts, ctx) => {
60
+ hosts.forEach((h, i) => {
61
+ for (const message of validateAllowHost(h)) {
62
+ ctx.addIssue({ code: "custom", path: [i], message: `${h}: ${message}` });
63
+ }
64
+ });
65
+ if (new Set(hosts).size !== hosts.length) {
66
+ ctx.addIssue({ code: "custom", message: `${name} must not contain duplicates` });
67
+ }
68
+ })
69
+ .optional();
54
70
  /**
55
- * The org's network for cloud devices (auto mode spec §4.3): `open` (any public host; loopback,
56
- * private and cloud metadata addresses are blocked outside the container) or `allowlist` with
57
- * `allowHosts`. `allowHosts` given **replaces** the deployment's list (the org may drop the presets
58
- * and add its own hosts); omitted, the deployment's `sandbox.limits.allowHosts` applies.
59
- * The host list is also the ceiling a local device in sandbox mode reaches (the device intersects
60
- * it with its own list), so narrowing it limits local sandbox devices too.
71
+ * The org's sandbox-mode network (org egress ceiling spec §D1), stored as a delta over the
72
+ * platform's `sandbox.limits.allowHosts`, never a copy of it, so platform changes keep reaching the
73
+ * org:
74
+ * - `mode`: the cloud network of a cloud device in sandbox mode, `open` (any public host; loopback,
75
+ * private and cloud metadata addresses are blocked outside the container) or `allowlist`; absent
76
+ * follows the platform's default (open). `sandbox.limits.orgHosts: within` makes it the allowlist.
77
+ * - `base`: `platform` (the default: start from the platform's list) or `none` (only the org's own
78
+ * hosts).
79
+ * - `extraHosts`: hosts the org adds (ignored outside the platform list under `within`).
80
+ * - `excludedHosts`: platform entries the org drops (exact entries; any host is accepted, since the
81
+ * platform may drop an entry later).
82
+ * The effective hosts ({@link orgEffectiveHosts} in `sandbox/modes.ts`) are also the ceiling a local
83
+ * device in sandbox mode reaches (the device intersects them with its own list).
61
84
  */
62
85
  export const CloudNetworkSettingSchema = z
63
86
  .object({
64
- mode: z.enum(["open", "allowlist"]),
65
- allowHosts: z
66
- .array(z.string())
67
- .max(ORG_SANDBOX_ALLOW_HOSTS_MAX)
68
- .superRefine((hosts, ctx) => {
69
- hosts.forEach((h, i) => {
70
- for (const message of validateAllowHost(h)) {
71
- ctx.addIssue({ code: "custom", path: [i], message: `${h}: ${message}` });
72
- }
73
- });
74
- if (new Set(hosts).size !== hosts.length) {
75
- ctx.addIssue({ code: "custom", message: "allowHosts must not contain duplicates" });
76
- }
77
- })
78
- .optional(),
87
+ mode: z.enum(["open", "allowlist"]).optional(),
88
+ base: z.enum(["platform", "none"]).optional(),
89
+ extraHosts: orgHostList("extraHosts"),
90
+ excludedHosts: orgHostList("excludedHosts"),
79
91
  })
80
92
  .strict();
81
93
  /** What an invalid stored `cloudNetwork` reads as: the allowlist with no host (never wider). */
82
94
  export const invalidCloudNetwork = () => ({
83
95
  mode: "allowlist",
84
- allowHosts: [],
96
+ base: "none",
85
97
  });
86
98
  /**
87
99
  * The org's auto-mode switch (auto mode spec §4.3): `allowed` or `off`; absent follows the
@@ -268,6 +280,28 @@ export function invalidStoredCloudNetwork(raw) {
268
280
  const v = asObject(raw).cloudNetwork;
269
281
  return v !== undefined && !CloudNetworkSettingSchema.safeParse(v).success;
270
282
  }
283
+ /**
284
+ * Whether `next` (absent: follow the platform) widens `prev` (spec §D3), both normalized (mode
285
+ * open, base platform, empty lists): the open mode where it was the allowlist; base none to
286
+ * platform; with base platform, dropping one of the stored exclusions; an extra host no stored
287
+ * extra covers. Over-strict on purpose: moving to base none with a platform entry as an extra is a
288
+ * narrowing these rules refuse (a token cannot pin a platform host the platform may drop later).
289
+ */
290
+ export function cloudNetworkWidens(prev, next) {
291
+ const mode = (v) => v?.mode ?? "open";
292
+ const base = (v) => v?.base ?? "platform";
293
+ if (mode(next) === "open" && mode(prev) !== "open")
294
+ return true;
295
+ if (base(next) === "platform" && base(prev) === "none")
296
+ return true;
297
+ if (base(next) === "platform") {
298
+ const kept = new Set(next?.excludedHosts ?? []);
299
+ if ((prev?.excludedHosts ?? []).some((h) => !kept.has(h)))
300
+ return true;
301
+ }
302
+ const prevExtra = (prev?.extraHosts ?? []).map((h) => h.toLowerCase());
303
+ return (next?.extraHosts ?? []).some((h) => !prevExtra.some((p) => coversHost(p, h.toLowerCase())));
304
+ }
271
305
  /** 组织设置服务:读取与修改(修改需 `org.update`,锁内实时复核角色,写审计)。 */
272
306
  let OrgSettingsService = class OrgSettingsService {
273
307
  db;
@@ -310,14 +344,27 @@ let OrgSettingsService = class OrgSettingsService {
310
344
  ]);
311
345
  return {
312
346
  autoMode,
313
- cloudNetwork: lim.egress === "none"
314
- ? { mode: "allowlist", allowHosts: [] }
315
- : { mode: "open", allowHosts: [...lim.allowHosts] },
347
+ cloudNetwork: {
348
+ allowHosts: [...lim.allowHosts],
349
+ orgHosts: lim.orgHosts,
350
+ off: lim.egress === "none",
351
+ },
316
352
  attribution,
317
353
  publicUrl: this.config.get().http.publicUrl !== undefined,
318
354
  compaction,
319
355
  };
320
356
  }
357
+ /**
358
+ * The org's effective sandbox-mode network (spec §D3): the cloud network's mode and the hosts,
359
+ * from the platform's `sandbox.limits` and the org's stored delta.
360
+ */
361
+ effectiveNetwork(settings) {
362
+ const lim = this.config.get().sandbox.limits;
363
+ return {
364
+ mode: orgCloudMode(lim, settings.cloudNetwork),
365
+ hosts: orgEffectiveHosts(lim, settings.cloudNetwork),
366
+ };
367
+ }
321
368
  /** 组织实际生效的沙箱 cpu / 内存(只读;由超级管理员在部署设置里覆盖)。 */
322
369
  async sandboxLimits(orgId) {
323
370
  const lim = this.config.get().sandbox.limits;
@@ -339,7 +386,10 @@ let OrgSettingsService = class OrgSettingsService {
339
386
  throw new ApiError("org.not_found", "Organization not found");
340
387
  return this.parseStored(orgId, row.settings);
341
388
  }
342
- /** The refusals of {@link update} that need no read of the org (the Platform tool's precheck). */
389
+ /**
390
+ * The refusals of {@link update} that need no read of the org (the Platform tool's precheck):
391
+ * auto mode where the platform turned it off.
392
+ */
343
393
  async check(patch) {
344
394
  // turning auto mode on where the platform turned it off is wider than the platform allows
345
395
  if (patch.autoMode === "allowed") {
@@ -349,6 +399,29 @@ let OrgSettingsService = class OrgSettingsService {
349
399
  }
350
400
  }
351
401
  }
402
+ /**
403
+ * Under `sandbox.limits.orgHosts: within` (spec §D2), refuses what would widen past the platform
404
+ * ceiling compared with the stored value `before`: the open cloud network where the org did not
405
+ * store it, or an extra host outside the platform's allowlist that the org did not store. Stored
406
+ * values are sent back as they are (the PATCH replaces `cloudNetwork` whole) and have no effect
407
+ * under the ceiling (they are filtered on read), so they never block a write.
408
+ */
409
+ assertWithinCeiling(before, patch) {
410
+ const cn = patch.cloudNetwork;
411
+ if (cn == null)
412
+ return;
413
+ const lim = this.config.get().sandbox.limits;
414
+ if (lim.orgHosts !== "within")
415
+ return;
416
+ if (cn.mode === "open" && before.cloudNetwork?.mode !== "open") {
417
+ throw new ApiError("org.sandbox_egress_not_allowed", "The platform limits sandbox networks to its allowlist; the open cloud network is not available", [{ reason: "open_not_allowed" }]);
418
+ }
419
+ const stored = new Set(before.cloudNetwork?.extraHosts ?? []);
420
+ const outside = (cn.extraHosts ?? []).filter((h) => !stored.has(h) && orgHostOutsideCeiling(lim, h));
421
+ if (outside.length > 0) {
422
+ throw new ApiError("org.sandbox_egress_not_allowed", `The platform limits sandbox hosts to its allowlist; ask a super admin to add ${outside.join(", ")} to sandbox.limits.allowHosts`, [{ reason: "outside_ceiling", hosts: outside }]);
423
+ }
424
+ }
352
425
  async update(orgId, patch, actorAccountId, actorTokenId,
353
426
  /** Merged into the audit detail (the Platform tool's `via: "agent"`). */
354
427
  auditDetail,
@@ -365,15 +438,9 @@ let OrgSettingsService = class OrgSettingsService {
365
438
  * never the Platform tool or an API token):
366
439
  * - `autoMode`: `allowed` where the organization runs `off` (an absent value follows the
367
440
  * platform); clearing it follows the platform too, never wider than the platform allows;
368
- * - `cloudNetwork`: `open` where the network is the allowlist, or a host the current allowlist
369
- * does not have (the deployment's `sandbox.limits.allowHosts` when the org gives none).
370
- * `cloudNetwork.allowHosts` is also the local sandbox-mode ceiling (`orgAllowedDomains` in
371
- * `sandbox/modes.ts` becomes `deviceLimits.localAllowedDomains`, which the device intersects
372
- * with its own list): an org with no `cloudNetwork` still caps its local devices at the
373
- * deployment's list, and an org whose network is `open` still caps them at its own `allowHosts`.
374
- * The host comparison therefore applies to both shapes, whatever the target mode: a patch may
375
- * only keep or lower the host set. Switching from `open` is a widening by itself, and the host
376
- * set must not grow even when the target stays `open` (spec §4.2, §4.3).
441
+ * - `cloudNetwork`: compared as stored deltas ({@link cloudNetworkWidens}), never as effective
442
+ * values: under the kill switch or the platform ceiling the effective values hide a widening
443
+ * that takes effect when the platform relaxes (org egress ceiling spec §D3).
377
444
  */
378
445
  async assertOnlyNarrows(before, patch) {
379
446
  if (patch.autoMode !== undefined) {
@@ -383,20 +450,9 @@ let OrgSettingsService = class OrgSettingsService {
383
450
  throw autoModeNotAllowed("Turning auto mode on needs a person in the organization settings.");
384
451
  }
385
452
  }
386
- if (patch.cloudNetwork !== undefined) {
387
- const lim = this.config.get().sandbox.limits;
388
- const defaultMode = lim.egress === "none" ? "allowlist" : "open";
389
- const defaultHosts = lim.egress === "none" ? [] : [...lim.allowHosts];
390
- const prev = before.cloudNetwork;
391
- const next = patch.cloudNetwork ?? undefined;
392
- const modeOf = (v) => v?.mode ?? defaultMode;
393
- const hostsOf = (v) => v?.allowHosts ?? defaultHosts;
394
- const nextMode = next?.mode ?? defaultMode;
395
- const widens = (nextMode === "open" && modeOf(prev) !== "open") ||
396
- !hostsOf(next).every((h) => hostsOf(prev).includes(h));
397
- if (widens) {
398
- throw new ApiError("org.sandbox_egress_not_allowed", "Widening the cloud network needs a person in the organization settings.");
399
- }
453
+ if (patch.cloudNetwork !== undefined &&
454
+ cloudNetworkWidens(before.cloudNetwork, patch.cloudNetwork ?? undefined)) {
455
+ throw new ApiError("org.sandbox_egress_not_allowed", "Widening the cloud network needs a person in the organization settings.");
400
456
  }
401
457
  }
402
458
  async updateIn(orgId, patch, actorAccountId, actorTokenId, auditDetail, opts) {
@@ -411,6 +467,7 @@ let OrgSettingsService = class OrgSettingsService {
411
467
  // again under the org lock: a hot config reload may have lowered the platform since
412
468
  await this.check(patch);
413
469
  // under the org lock, so the comparison is with the value the write replaces
470
+ this.assertWithinCeiling(before, patch);
414
471
  if (opts?.onlyNarrows)
415
472
  await this.assertOnlyNarrows(before, patch);
416
473
  const changes = Object.fromEntries(Object.entries(patch)
@@ -17,6 +17,7 @@ import { OUTBOX_STRICT_CONFLICTS } from "./infra/outbox/outbox.service.js";
17
17
  import { PRESENCE_INTERVAL_MS } from "./infra/presence/replica-presence.js";
18
18
  import type { KernelQueueName } from "./infra/queue/queue-names.js";
19
19
  import { DEVICE_POLICY_AFTER_CALL } from "./local-devices/device-policy.service.js";
20
+ import { PROJECT_KNOWLEDGE_TIMEOUT } from "./runtime/project/knowledge.service.js";
20
21
  import { LIST_MODELS_TIMEOUT } from "./runtime/providers.js";
21
22
  import { RUN_CONTEXT_CLOCK } from "./runtime/run-context.service.js";
22
23
  import { ENSURE_RUN_FAILS } from "./runtime/run-queue.js";
@@ -55,6 +56,8 @@ import { EMBED_TOKEN_CLOCK } from "./ui/embed-token.js";
55
56
  * direct call after a commit skipped by a crash);
56
57
  * - `DEVICE_POLICY_AFTER_CALL`: called after a device policy op's call, before its outcome is
57
58
  * recorded (throwing stands for a crash between the call and the resolve transaction);
59
+ * - `PROJECT_KNOWLEDGE_TIMEOUT`: the run-start read of a bound project's time limit, read at every
60
+ * read (a test shortens it for one run);
58
61
  * - `PLUGIN_PUBLIC_WRITE_LIMIT`: a lower limit for unsafe methods on `public` extension routes;
59
62
  * - `SCOPE_BACKSTOP_DELAY`: the delay of a confirmation's scope sync backstop (ruling R24);
60
63
  * - `APP_INTERCEPTOR`: one more global interceptor (a probe of the matched routes).
@@ -83,6 +86,7 @@ interface KernelSeams {
83
86
  [OUTBOX_FAST_PATH_SETTLED]: (pass: Promise<void>) => void;
84
87
  [ENSURE_RUN_FAILS]: () => boolean;
85
88
  [SCOPE_BACKSTOP_DELAY]: number;
89
+ [PROJECT_KNOWLEDGE_TIMEOUT]: () => number;
86
90
  [PLUGIN_PUBLIC_WRITE_LIMIT]: number;
87
91
  [DEVICE_POLICY_AFTER_CALL]: (opId: string) => void;
88
92
  [APP_INTERCEPTOR]: NestInterceptor;
package/dist/overrides.js CHANGED
@@ -14,6 +14,7 @@ import { OUTBOX_FAST_PATH_SETTLED } from "./infra/outbox/outbox.module.js";
14
14
  import { OUTBOX_STRICT_CONFLICTS } from "./infra/outbox/outbox.service.js";
15
15
  import { PRESENCE_INTERVAL_MS } from "./infra/presence/replica-presence.js";
16
16
  import { DEVICE_POLICY_AFTER_CALL } from "./local-devices/device-policy.service.js";
17
+ import { PROJECT_KNOWLEDGE_TIMEOUT } from "./runtime/project/knowledge.service.js";
17
18
  import { LIST_MODELS_TIMEOUT } from "./runtime/providers.js";
18
19
  import { RUN_CONTEXT_CLOCK } from "./runtime/run-context.service.js";
19
20
  import { ENSURE_RUN_FAILS } from "./runtime/run-queue.js";
@@ -48,6 +49,7 @@ const SEAM_TOKENS = [
48
49
  OUTBOX_FAST_PATH_SETTLED,
49
50
  ENSURE_RUN_FAILS,
50
51
  SCOPE_BACKSTOP_DELAY,
52
+ PROJECT_KNOWLEDGE_TIMEOUT,
51
53
  PLUGIN_PUBLIC_WRITE_LIMIT,
52
54
  DEVICE_POLICY_AFTER_CALL,
53
55
  APP_INTERCEPTOR,
@@ -185,7 +185,7 @@ let AgentSkillsOperations = class AgentSkillsOperations {
185
185
  }),
186
186
  defineOperation({
187
187
  id: "agents.invocable-skills",
188
- description: "List the skills a person can invoke with / on an agent (its effective skills: org, platform and its own), by name: id, name, state (manual for manual-only), description, version and the plugin. Optionally pass a conversation of yours with that agent to include its trial drafts.",
188
+ description: "List the skills a person can invoke with / on an agent (its effective skills: org, platform and its own), by name: id, name, source (platform, plugin, agent or project), state (manual for manual-only), description, version and the plugin. Optionally pass a conversation of yours with that agent to include its trial drafts and, for a conversation bound to a project, the project's skills as project:<name>.",
189
189
  input: z
190
190
  .object({
191
191
  agentId: AgentId,
@@ -210,6 +210,7 @@ let AgentSkillsOperations = class AgentSkillsOperations {
210
210
  items: (await this.invoker.listFor(ctx.orgId, input.agentId, ctx.actorAccountId, input.conversationId)).map((s) => ({
211
211
  id: s.id,
212
212
  name: s.name,
213
+ source: s.source,
213
214
  state: s.manual ? "manual" : "active",
214
215
  description: clip(s.description),
215
216
  version: s.version,
@@ -1,4 +1,5 @@
1
1
  import { type OnModuleInit } from "@nestjs/common";
2
+ import { DeviceProjectsService } from "../../devices/device-projects.service.js";
2
3
  import { DevicesService } from "../../devices/devices.service.js";
3
4
  import { KernelDb } from "../../infra/database/kernel-db.js";
4
5
  import { PlatformOperations } from "../registry.js";
@@ -11,8 +12,9 @@ import { PlatformOperations } from "../registry.js";
11
12
  export declare class DevicesOperations implements OnModuleInit {
12
13
  private readonly registry;
13
14
  private readonly devices;
15
+ private readonly projects;
14
16
  private readonly kdb;
15
- constructor(registry: PlatformOperations, devices: DevicesService, kdb: KernelDb);
17
+ constructor(registry: PlatformOperations, devices: DevicesService, projects: DeviceProjectsService, kdb: KernelDb);
16
18
  /**
17
19
  * Whether `id` is the device the run works in: the one its root conversation is bound to
18
20
  * (sub-agents use the root's). Deleting or wiping it would pull the sandbox from under the run.