@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
@@ -12,6 +12,7 @@ import { Inject, Injectable, Optional } from "@nestjs/common";
12
12
  import { EventLog } from "../conversations/event-log.js";
13
13
  import { COMMAND_ASK_MAX_CHARS, COMMAND_ASK_TEXT_MAX_CHARS, ToolViewSchema, } from "../conversations/events.js";
14
14
  import { ConversationDevices } from "../devices/conversation-devices.js";
15
+ import { pathAgainstWorkingRoot } from "../devices/working-root.js";
15
16
  import { ApiError } from "../errors.js";
16
17
  import { EffectiveConfigs, NEEDS_CONFIG_REASON } from "../extensions/effective-config.js";
17
18
  import { clipText } from "../extensions/hooks.js";
@@ -34,6 +35,7 @@ import { rootSurfaceOf } from "../runtime/subagents/child.js";
34
35
  import { SubagentMessages } from "../runtime/subagents/messages.js";
35
36
  import { pairStoppedForks } from "../runtime/subagents/skill-fork.js";
36
37
  import { SubagentsService } from "../runtime/subagents/subagents.service.js";
38
+ import { orgHostOutsideCeiling } from "../sandbox/modes.js";
37
39
  import { isSessionGrantableRule, } from "../sandbox/review/index.js";
38
40
  import { CommandReviewService } from "../sandbox/review/review.service.js";
39
41
  import { appendMissingGrant, connectionGrantKey, UNATTENDED_CONNECTION_ERROR, UNATTENDED_ELICITATION_NOTE, UNATTENDED_QUESTION_ERROR, unattendedFails, unattendedRunOf, } from "../schedules/unattended.js";
@@ -179,6 +181,14 @@ export const TOOL_PIPELINE_HOOKS = Symbol("TOOL_PIPELINE_HOOKS");
179
181
  const REVIEWED_TOOL_KEYS = new Set(["Bash", "Write", "Edit"]);
180
182
  /** Whether the review holds a shell command or a file write, for the texts and the ask's detail. */
181
183
  const reviewKindOf = (toolKey) => toolKey === "Bash" ? "command" : "write";
184
+ /**
185
+ * The working root a reviewed call carries to its tool: a project root only. In free mode the
186
+ * reviewed root is the recorded workspace, which may be stale; the tool then maps it to the held
187
+ * instance's own workspace as it always has.
188
+ */
189
+ const carriedRoot = (facts) => facts.workingRoot !== undefined && facts.workingRoot !== facts.workspace
190
+ ? facts.workingRoot
191
+ : undefined;
182
192
  /** The review record of an allowed call. */
183
193
  const allowRecord = (v) => ({
184
194
  decision: "allow",
@@ -688,6 +698,7 @@ let ToolPipeline = class ToolPipeline {
688
698
  return fail(reviewed.content, outKey);
689
699
  }
690
700
  review = reviewed.review;
701
+ const reviewedWorkingRoot = reviewed.workingRoot;
691
702
  if (reviewed.commandAsk !== undefined)
692
703
  authorization = approvalAuthorization(reviewed.commandAsk);
693
704
  // tool.preExecute after policy and approval, before tool_call_started (F10)
@@ -824,7 +835,7 @@ let ToolPipeline = class ToolPipeline {
824
835
  },
825
836
  ]);
826
837
  const t0 = Date.now();
827
- let outcome = await this.execute(tool, input, ctx, call.id, resume, resolved.tools);
838
+ let outcome = await this.execute(tool, input, ctx, call.id, resume, resolved.tools, reviewedWorkingRoot);
828
839
  const durationMs = Date.now() - t0;
829
840
  await this.deps.hooks?.afterExecute?.(ctx.runId, call.id);
830
841
  // scheduled tasks spec §7, `fail` mode: no one answers an input request or connects an app
@@ -979,9 +990,23 @@ let ToolPipeline = class ToolPipeline {
979
990
  if (!REVIEWED_TOOL_KEYS.has(tool.key))
980
991
  return { kind: "continue" };
981
992
  const asked = await this.deps.approvals.commandForCall(ctx.runId, call.id);
982
- if (asked !== null)
983
- return this.settledCommandAsk(asked, tool.key);
984
993
  const rootConversationId = ctx.parent?.rootConversationId ?? ctx.conversationId;
994
+ if (asked !== null) {
995
+ const settled = this.settledCommandAsk(asked, tool.key);
996
+ // an approved call runs in the root it was reviewed against only while the device still runs
997
+ // in the mode recorded with the ask; otherwise the tool decides its working root afresh. A
998
+ // judged root that is the recorded workspace (free mode) is not carried either
999
+ // ({@link carriedRoot}): the tool maps it to the held instance's workspace as always
1000
+ if (settled.kind === "continue" && settled.workingRoot !== undefined) {
1001
+ const now = await devices.reviewFacts(ctx.orgId, rootConversationId, ctx.conversationId);
1002
+ if (now === null ||
1003
+ now.mode !== asked.command?.mode ||
1004
+ carriedRoot({ workspace: now.workspace, workingRoot: settled.workingRoot }) === undefined) {
1005
+ return { ...settled, workingRoot: undefined };
1006
+ }
1007
+ }
1008
+ return settled;
1009
+ }
985
1010
  const facts = await devices.reviewFacts(ctx.orgId, rootConversationId, ctx.conversationId);
986
1011
  if (facts === null)
987
1012
  return { kind: "continue" }; // no device: the tool fails on its own
@@ -1007,7 +1032,12 @@ let ToolPipeline = class ToolPipeline {
1007
1032
  toolKey: tool.key,
1008
1033
  input,
1009
1034
  mode: facts.mode,
1010
- workspace: facts.workspace,
1035
+ // the working root: a bound project's root, where commands start and writes need no ask
1036
+ workspace: facts.workingRoot,
1037
+ // the owner's home, never the project root: the home-level rules stay where they were
1038
+ home: facts.home,
1039
+ // and the device workspace's home, so a project outside every home still protects it
1040
+ deviceHome: facts.deviceHome,
1011
1041
  attended,
1012
1042
  step: ctx.step,
1013
1043
  callId: call.id,
@@ -1026,7 +1056,7 @@ let ToolPipeline = class ToolPipeline {
1026
1056
  ...(ctx.conversationKind !== undefined && { conversationKind: ctx.conversationKind }),
1027
1057
  });
1028
1058
  if (verdict.decision === "allow") {
1029
- return { kind: "continue", review: allowRecord(verdict) };
1059
+ return { kind: "continue", review: allowRecord(verdict), workingRoot: carriedRoot(facts) };
1030
1060
  }
1031
1061
  if (verdict.decision === "deny") {
1032
1062
  return {
@@ -1052,9 +1082,17 @@ let ToolPipeline = class ToolPipeline {
1052
1082
  kind,
1053
1083
  // the whole command the approval covers (the event keeps a clamped copy for display)
1054
1084
  command,
1055
- ...(kind === "write" && { path: clipText(textField(input, "file_path"), 4096) }),
1085
+ // a relative path as it will be written: resolved against the working root
1086
+ ...(kind === "write" && {
1087
+ path: clipText(facts.workingRoot === undefined
1088
+ ? textField(input, "file_path")
1089
+ : pathAgainstWorkingRoot(textField(input, "file_path"), facts.workingRoot), 4096),
1090
+ }),
1056
1091
  rule: verdict.rule,
1057
1092
  reason: clipText(verdict.reason, COMMAND_ASK_TEXT_MAX_CHARS),
1093
+ // the root the review judged, always (the workspace in free mode): the approved call runs
1094
+ // against what the person approved, not a later snapshot
1095
+ ...(facts.workingRoot !== undefined && { workingRoot: facts.workingRoot }),
1058
1096
  };
1059
1097
  let ask;
1060
1098
  try {
@@ -1093,7 +1131,12 @@ let ToolPipeline = class ToolPipeline {
1093
1131
  if (ask.status === "pending")
1094
1132
  return { kind: "wait", wait: approvalWait(ask) };
1095
1133
  if (ask.status === "approved") {
1096
- return { kind: "continue", commandAsk: ask, review: this.personAllowRecord(ask) };
1134
+ return {
1135
+ kind: "continue",
1136
+ commandAsk: ask,
1137
+ review: this.personAllowRecord(ask),
1138
+ workingRoot: ask.command?.workingRoot,
1139
+ };
1097
1140
  }
1098
1141
  if (ask.status === "denied") {
1099
1142
  const reason = ask.denyReason;
@@ -1149,7 +1192,12 @@ let ToolPipeline = class ToolPipeline {
1149
1192
  const run = hosts.some((h) => h.layer === "device")
1150
1193
  ? await network.grants.runState(ctx.runId, report.deviceId)
1151
1194
  : null;
1152
- const plan = planNetworkAsk(hosts, run, { device, unattended: ctx.unattended });
1195
+ const limits = this.config.get().sandbox.limits;
1196
+ const plan = planNetworkAsk(hosts, run, {
1197
+ device,
1198
+ unattended: ctx.unattended,
1199
+ outsideCeiling: (h) => orgHostOutsideCeiling(limits, h),
1200
+ });
1153
1201
  if (ctx.unattended != null) {
1154
1202
  for (const key of plan.missingGrants) {
1155
1203
  await appendMissingGrant(this.deps.db, ctx.unattended.taskRunId, key);
@@ -1315,7 +1363,7 @@ let ToolPipeline = class ToolPipeline {
1315
1363
  * 带 `tools.timeoutMs` 超时与 run 的中止信号(合并后传给处理函数 / 连接器,不响应信号的也按时结束)。
1316
1364
  * 熔断只作用于连接器工具(键为组织 × 连接器)。
1317
1365
  */
1318
- async execute(tool, input, ctx, callId, resume, stepTools) {
1366
+ async execute(tool, input, ctx, callId, resume, stepTools, reviewedWorkingRoot) {
1319
1367
  if (tool.source.kind === "connector") {
1320
1368
  return this.executeConnector(tool, tool.source, input, ctx, resume);
1321
1369
  }
@@ -1338,6 +1386,9 @@ let ToolPipeline = class ToolPipeline {
1338
1386
  ...(tool.source.kind === "builtin" && { runId: ctx.runId }),
1339
1387
  ...(tool.source.kind === "builtin" &&
1340
1388
  ctx.deadline !== undefined && { runDeadline: ctx.deadline }),
1389
+ // a reviewed call runs in the working root its review judged (project root spec §4.3)
1390
+ ...(tool.source.kind === "builtin" &&
1391
+ reviewedWorkingRoot !== undefined && { reviewedWorkingRoot }),
1341
1392
  // a sub-agent works in its root conversation's sandbox (spec §10.1, at every depth)
1342
1393
  ...(tool.source.kind === "builtin" &&
1343
1394
  ctx.parent !== undefined && { sandboxConversationId: ctx.parent.rootConversationId }),
@@ -50,6 +50,12 @@ export interface BuiltinCallContext extends ToolCallContext {
50
50
  * default when absent).
51
51
  */
52
52
  sandboxConversationId?: string;
53
+ /**
54
+ * The working root the command review judged this call against (project root spec §4.3): a
55
+ * sandbox tool runs there instead of reading the working root again, so it executes exactly what
56
+ * was reviewed. Absent for a call the review does not cover.
57
+ */
58
+ reviewedWorkingRoot?: string;
53
59
  /** The parent run of a sub-agent's run (absent for a top-level run). */
54
60
  parentRunId?: string;
55
61
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/kernel",
3
- "version": "0.1.0-rc.32",
3
+ "version": "0.1.0-rc.34",
4
4
  "description": "Capybara kernel: config, database, plugin host and HTTP API of the Capybara agent platform",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -65,7 +65,7 @@
65
65
  "yaml": "^2.9.1",
66
66
  "yauzl": "3.4.0",
67
67
  "zod": "^4.6.7",
68
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.23"
68
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.24"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@langchain/langgraph-checkpoint-validation": "^1.2.0",
@@ -0,0 +1,30 @@
1
+ -- Project root (project root design §4.1, §4.3, §12): a device's projects live in PostgreSQL, keyed
2
+ -- by the device's id, the same for a local machine and a cloud device. The kernel is the only
3
+ -- writer; the device program keeps no project list of its own. A project's identity on its device
4
+ -- is its name; its path is absolute on the device. Deleted with its device row.
5
+ CREATE TABLE IF NOT EXISTS device_projects (
6
+ device_id text NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
7
+ name text NOT NULL,
8
+ path text NOT NULL,
9
+ created_by text NOT NULL,
10
+ created_at timestamptz NOT NULL DEFAULT now(),
11
+ PRIMARY KEY (device_id, name)
12
+ );
13
+
14
+ -- The owner's home directory a local machine reports in its hello (`DeviceHello.home`): the base
15
+ -- of an auto-mode project's relative path. NULL: not reported (cloud devices have none).
16
+ ALTER TABLE device_machines ADD COLUMN IF NOT EXISTS home text;
17
+
18
+ -- A conversation may be bound, at creation, to one project of its device (design §4.3). The name
19
+ -- and the path it resolved to are both stored, so re-pointing the name later does not move an
20
+ -- existing conversation. NULL: free mode.
21
+ ALTER TABLE conversations ADD COLUMN IF NOT EXISTS project_name text;
22
+ ALTER TABLE conversations ADD COLUMN IF NOT EXISTS project_path text;
23
+
24
+ -- Every sandbox tool call and every command review of a bound conversation reads the root
25
+ -- conversation's latest `project_root` event (its working root, design §4.3); the event is not
26
+ -- rewritten while nothing changes, so without this partial index the lookup scans the conversation's
27
+ -- events backwards. Not CONCURRENTLY, like the other index migrations (0025, 0094).
28
+ CREATE INDEX IF NOT EXISTS conversation_events_project_root_idx
29
+ ON conversation_events (conversation_id, seq)
30
+ WHERE type = 'project_root';
@@ -0,0 +1,38 @@
1
+ -- Org egress ceiling (spec 2026-11-15-org-egress-ceiling-design.md §D5): an org's cloudNetwork
2
+ -- stops replacing the platform's allowlist with a copy (`allowHosts`) and stores a delta over it
3
+ -- (`base`, `extraHosts`, `excludedHosts`). A migration cannot read the deployment's config, so a
4
+ -- stored list is kept exactly: `base: none` (only the org's own hosts) with the list as its extras,
5
+ -- never wider than before. An org admin re-attaches to the platform by following it again.
6
+ --
7
+ -- Only an object cloudNetwork whose allowHosts is an array of distinct strings is converted, in its
8
+ -- order. Anything else (a non-array allowHosts copied in by 0130, duplicates, non-string elements)
9
+ -- is left alone: the old reader already read it as invalid, the empty allowlist, and so does the
10
+ -- new one; converting it would widen it. Idempotent: a converted row has no allowHosts left.
11
+ UPDATE orgs
12
+ SET settings = jsonb_set(
13
+ settings,
14
+ '{cloudNetwork}',
15
+ ((settings->'cloudNetwork') - 'allowHosts')
16
+ || '{"base":"none"}'::jsonb
17
+ || COALESCE(
18
+ (
19
+ SELECT jsonb_build_object('extraHosts', jsonb_agg(h ORDER BY first_pos))
20
+ FROM (
21
+ SELECT e.value AS h, min(e.pos) AS first_pos
22
+ FROM jsonb_array_elements(settings->'cloudNetwork'->'allowHosts')
23
+ WITH ORDINALITY AS e(value, pos)
24
+ WHERE jsonb_typeof(e.value) = 'string'
25
+ GROUP BY e.value
26
+ ) AS uniq
27
+ HAVING count(*) > 0
28
+ ),
29
+ '{}'::jsonb
30
+ )
31
+ )
32
+ WHERE jsonb_typeof(settings->'cloudNetwork') = 'object'
33
+ AND jsonb_typeof(settings->'cloudNetwork'->'allowHosts') = 'array'
34
+ AND jsonb_array_length(settings->'cloudNetwork'->'allowHosts') = (
35
+ SELECT count(DISTINCT e.value)
36
+ FROM jsonb_array_elements(settings->'cloudNetwork'->'allowHosts') AS e(value)
37
+ WHERE jsonb_typeof(e.value) = 'string'
38
+ );