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

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 (85) hide show
  1. package/dist/catalog/marketplaces.service.d.ts +1 -1
  2. package/dist/channels/inbound.d.ts +0 -25
  3. package/dist/channels/inbound.js +26 -1
  4. package/dist/channels/phrases.d.ts +28 -0
  5. package/dist/channels/phrases.js +28 -0
  6. package/dist/conversations/conversations.controller.d.ts +4 -1
  7. package/dist/conversations/conversations.controller.js +14 -2
  8. package/dist/conversations/conversations.service.d.ts +43 -3
  9. package/dist/conversations/conversations.service.js +93 -19
  10. package/dist/conversations/event-log.d.ts +3 -3
  11. package/dist/conversations/event-log.js +3 -3
  12. package/dist/conversations/events.d.ts +24 -0
  13. package/dist/conversations/events.js +69 -0
  14. package/dist/db/kernel-schema.d.ts +20 -0
  15. package/dist/devices/conversation-devices.d.ts +57 -3
  16. package/dist/devices/conversation-devices.js +63 -11
  17. package/dist/devices/device-projects.service.d.ts +162 -0
  18. package/dist/devices/device-projects.service.js +503 -0
  19. package/dist/devices/devices.controller.d.ts +15 -2
  20. package/dist/devices/devices.controller.js +60 -3
  21. package/dist/devices/devices.module.js +15 -2
  22. package/dist/devices/devices.schemas.js +9 -1
  23. package/dist/devices/dto.d.ts +31 -0
  24. package/dist/devices/dto.js +38 -1
  25. package/dist/devices/working-root.d.ts +41 -0
  26. package/dist/devices/working-root.js +50 -0
  27. package/dist/errors.d.ts +39 -0
  28. package/dist/errors.js +39 -0
  29. package/dist/evals/run-replay.d.ts +9 -1
  30. package/dist/evals/run-replay.js +15 -1
  31. package/dist/local-devices/local-device-provider.d.ts +37 -1
  32. package/dist/local-devices/local-device-provider.js +33 -0
  33. package/dist/local-devices/machines.d.ts +2 -1
  34. package/dist/local-devices/machines.js +4 -1
  35. package/dist/local-devices/policy-summary.js +1 -0
  36. package/dist/local-devices/relay.d.ts +2 -0
  37. package/dist/local-devices/relay.js +3 -1
  38. package/dist/local-devices/targets.d.ts +6 -1
  39. package/dist/local-devices/targets.js +14 -0
  40. package/dist/overrides.d.ts +4 -0
  41. package/dist/overrides.js +2 -0
  42. package/dist/platform/ops/agent-skills.ops.js +2 -1
  43. package/dist/platform/ops/devices.ops.d.ts +3 -1
  44. package/dist/platform/ops/devices.ops.js +136 -3
  45. package/dist/runtime/context/build.d.ts +3 -0
  46. package/dist/runtime/context/build.js +14 -0
  47. package/dist/runtime/context/compaction.js +2 -0
  48. package/dist/runtime/context/project-root.d.ts +43 -0
  49. package/dist/runtime/context/project-root.js +85 -0
  50. package/dist/runtime/model-call.js +25 -1
  51. package/dist/runtime/project/knowledge.d.ts +129 -0
  52. package/dist/runtime/project/knowledge.js +248 -0
  53. package/dist/runtime/project/knowledge.service.d.ts +85 -0
  54. package/dist/runtime/project/knowledge.service.js +288 -0
  55. package/dist/runtime/run-worker.d.ts +5 -0
  56. package/dist/runtime/run-worker.js +27 -6
  57. package/dist/runtime/runtime.module.js +7 -0
  58. package/dist/sandbox/review/review.service.d.ts +14 -2
  59. package/dist/sandbox/review/review.service.js +2 -0
  60. package/dist/sandbox/review/rules.d.ts +6 -4
  61. package/dist/sandbox/review/rules.js +185 -62
  62. package/dist/sandbox/review/types.d.ts +14 -0
  63. package/dist/sandbox/sandbox.service.d.ts +16 -1
  64. package/dist/sandbox/sandbox.service.js +75 -3
  65. package/dist/skills/catalog.d.ts +6 -1
  66. package/dist/skills/catalog.js +18 -4
  67. package/dist/skills/invoke.d.ts +64 -2
  68. package/dist/skills/invoke.js +214 -6
  69. package/dist/skills/skills.module.js +5 -0
  70. package/dist/tools/approvals.service.d.ts +6 -0
  71. package/dist/tools/builtins/sandbox/bash.js +19 -4
  72. package/dist/tools/builtins/sandbox/common.d.ts +36 -6
  73. package/dist/tools/builtins/sandbox/common.js +69 -8
  74. package/dist/tools/builtins/sandbox/edit.js +2 -2
  75. package/dist/tools/builtins/sandbox/glob.js +3 -2
  76. package/dist/tools/builtins/sandbox/grep.js +2 -1
  77. package/dist/tools/builtins/sandbox/read.js +3 -3
  78. package/dist/tools/builtins/sandbox/write.js +2 -2
  79. package/dist/tools/builtins/skill-load.d.ts +47 -2
  80. package/dist/tools/builtins/skill-load.js +160 -2
  81. package/dist/tools/pipeline.d.ts +1 -1
  82. package/dist/tools/pipeline.js +53 -8
  83. package/dist/tools/registry.d.ts +6 -0
  84. package/package.json +2 -2
  85. package/src/migrations/0133_project_root.sql +30 -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";
@@ -179,6 +180,14 @@ export const TOOL_PIPELINE_HOOKS = Symbol("TOOL_PIPELINE_HOOKS");
179
180
  const REVIEWED_TOOL_KEYS = new Set(["Bash", "Write", "Edit"]);
180
181
  /** Whether the review holds a shell command or a file write, for the texts and the ask's detail. */
181
182
  const reviewKindOf = (toolKey) => toolKey === "Bash" ? "command" : "write";
183
+ /**
184
+ * The working root a reviewed call carries to its tool: a project root only. In free mode the
185
+ * reviewed root is the recorded workspace, which may be stale; the tool then maps it to the held
186
+ * instance's own workspace as it always has.
187
+ */
188
+ const carriedRoot = (facts) => facts.workingRoot !== undefined && facts.workingRoot !== facts.workspace
189
+ ? facts.workingRoot
190
+ : undefined;
182
191
  /** The review record of an allowed call. */
183
192
  const allowRecord = (v) => ({
184
193
  decision: "allow",
@@ -688,6 +697,7 @@ let ToolPipeline = class ToolPipeline {
688
697
  return fail(reviewed.content, outKey);
689
698
  }
690
699
  review = reviewed.review;
700
+ const reviewedWorkingRoot = reviewed.workingRoot;
691
701
  if (reviewed.commandAsk !== undefined)
692
702
  authorization = approvalAuthorization(reviewed.commandAsk);
693
703
  // tool.preExecute after policy and approval, before tool_call_started (F10)
@@ -824,7 +834,7 @@ let ToolPipeline = class ToolPipeline {
824
834
  },
825
835
  ]);
826
836
  const t0 = Date.now();
827
- let outcome = await this.execute(tool, input, ctx, call.id, resume, resolved.tools);
837
+ let outcome = await this.execute(tool, input, ctx, call.id, resume, resolved.tools, reviewedWorkingRoot);
828
838
  const durationMs = Date.now() - t0;
829
839
  await this.deps.hooks?.afterExecute?.(ctx.runId, call.id);
830
840
  // scheduled tasks spec §7, `fail` mode: no one answers an input request or connects an app
@@ -979,9 +989,23 @@ let ToolPipeline = class ToolPipeline {
979
989
  if (!REVIEWED_TOOL_KEYS.has(tool.key))
980
990
  return { kind: "continue" };
981
991
  const asked = await this.deps.approvals.commandForCall(ctx.runId, call.id);
982
- if (asked !== null)
983
- return this.settledCommandAsk(asked, tool.key);
984
992
  const rootConversationId = ctx.parent?.rootConversationId ?? ctx.conversationId;
993
+ if (asked !== null) {
994
+ const settled = this.settledCommandAsk(asked, tool.key);
995
+ // an approved call runs in the root it was reviewed against only while the device still runs
996
+ // in the mode recorded with the ask; otherwise the tool decides its working root afresh. A
997
+ // judged root that is the recorded workspace (free mode) is not carried either
998
+ // ({@link carriedRoot}): the tool maps it to the held instance's workspace as always
999
+ if (settled.kind === "continue" && settled.workingRoot !== undefined) {
1000
+ const now = await devices.reviewFacts(ctx.orgId, rootConversationId, ctx.conversationId);
1001
+ if (now === null ||
1002
+ now.mode !== asked.command?.mode ||
1003
+ carriedRoot({ workspace: now.workspace, workingRoot: settled.workingRoot }) === undefined) {
1004
+ return { ...settled, workingRoot: undefined };
1005
+ }
1006
+ }
1007
+ return settled;
1008
+ }
985
1009
  const facts = await devices.reviewFacts(ctx.orgId, rootConversationId, ctx.conversationId);
986
1010
  if (facts === null)
987
1011
  return { kind: "continue" }; // no device: the tool fails on its own
@@ -1007,7 +1031,12 @@ let ToolPipeline = class ToolPipeline {
1007
1031
  toolKey: tool.key,
1008
1032
  input,
1009
1033
  mode: facts.mode,
1010
- workspace: facts.workspace,
1034
+ // the working root: a bound project's root, where commands start and writes need no ask
1035
+ workspace: facts.workingRoot,
1036
+ // the owner's home, never the project root: the home-level rules stay where they were
1037
+ home: facts.home,
1038
+ // and the device workspace's home, so a project outside every home still protects it
1039
+ deviceHome: facts.deviceHome,
1011
1040
  attended,
1012
1041
  step: ctx.step,
1013
1042
  callId: call.id,
@@ -1026,7 +1055,7 @@ let ToolPipeline = class ToolPipeline {
1026
1055
  ...(ctx.conversationKind !== undefined && { conversationKind: ctx.conversationKind }),
1027
1056
  });
1028
1057
  if (verdict.decision === "allow") {
1029
- return { kind: "continue", review: allowRecord(verdict) };
1058
+ return { kind: "continue", review: allowRecord(verdict), workingRoot: carriedRoot(facts) };
1030
1059
  }
1031
1060
  if (verdict.decision === "deny") {
1032
1061
  return {
@@ -1052,9 +1081,17 @@ let ToolPipeline = class ToolPipeline {
1052
1081
  kind,
1053
1082
  // the whole command the approval covers (the event keeps a clamped copy for display)
1054
1083
  command,
1055
- ...(kind === "write" && { path: clipText(textField(input, "file_path"), 4096) }),
1084
+ // a relative path as it will be written: resolved against the working root
1085
+ ...(kind === "write" && {
1086
+ path: clipText(facts.workingRoot === undefined
1087
+ ? textField(input, "file_path")
1088
+ : pathAgainstWorkingRoot(textField(input, "file_path"), facts.workingRoot), 4096),
1089
+ }),
1056
1090
  rule: verdict.rule,
1057
1091
  reason: clipText(verdict.reason, COMMAND_ASK_TEXT_MAX_CHARS),
1092
+ // the root the review judged, always (the workspace in free mode): the approved call runs
1093
+ // against what the person approved, not a later snapshot
1094
+ ...(facts.workingRoot !== undefined && { workingRoot: facts.workingRoot }),
1058
1095
  };
1059
1096
  let ask;
1060
1097
  try {
@@ -1093,7 +1130,12 @@ let ToolPipeline = class ToolPipeline {
1093
1130
  if (ask.status === "pending")
1094
1131
  return { kind: "wait", wait: approvalWait(ask) };
1095
1132
  if (ask.status === "approved") {
1096
- return { kind: "continue", commandAsk: ask, review: this.personAllowRecord(ask) };
1133
+ return {
1134
+ kind: "continue",
1135
+ commandAsk: ask,
1136
+ review: this.personAllowRecord(ask),
1137
+ workingRoot: ask.command?.workingRoot,
1138
+ };
1097
1139
  }
1098
1140
  if (ask.status === "denied") {
1099
1141
  const reason = ask.denyReason;
@@ -1315,7 +1357,7 @@ let ToolPipeline = class ToolPipeline {
1315
1357
  * 带 `tools.timeoutMs` 超时与 run 的中止信号(合并后传给处理函数 / 连接器,不响应信号的也按时结束)。
1316
1358
  * 熔断只作用于连接器工具(键为组织 × 连接器)。
1317
1359
  */
1318
- async execute(tool, input, ctx, callId, resume, stepTools) {
1360
+ async execute(tool, input, ctx, callId, resume, stepTools, reviewedWorkingRoot) {
1319
1361
  if (tool.source.kind === "connector") {
1320
1362
  return this.executeConnector(tool, tool.source, input, ctx, resume);
1321
1363
  }
@@ -1338,6 +1380,9 @@ let ToolPipeline = class ToolPipeline {
1338
1380
  ...(tool.source.kind === "builtin" && { runId: ctx.runId }),
1339
1381
  ...(tool.source.kind === "builtin" &&
1340
1382
  ctx.deadline !== undefined && { runDeadline: ctx.deadline }),
1383
+ // a reviewed call runs in the working root its review judged (project root spec §4.3)
1384
+ ...(tool.source.kind === "builtin" &&
1385
+ reviewedWorkingRoot !== undefined && { reviewedWorkingRoot }),
1341
1386
  // a sub-agent works in its root conversation's sandbox (spec §10.1, at every depth)
1342
1387
  ...(tool.source.kind === "builtin" &&
1343
1388
  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.33",
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';