@capybara-hq/kernel 0.1.0-rc.31 → 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 (104) 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/ctx/create-ctx.d.ts +26 -4
  15. package/dist/ctx/create-ctx.js +54 -34
  16. package/dist/db/kernel-schema.d.ts +20 -0
  17. package/dist/devices/conversation-devices.d.ts +57 -3
  18. package/dist/devices/conversation-devices.js +63 -11
  19. package/dist/devices/device-projects.service.d.ts +162 -0
  20. package/dist/devices/device-projects.service.js +503 -0
  21. package/dist/devices/devices.controller.d.ts +15 -2
  22. package/dist/devices/devices.controller.js +60 -3
  23. package/dist/devices/devices.module.js +15 -2
  24. package/dist/devices/devices.schemas.js +9 -1
  25. package/dist/devices/dto.d.ts +31 -0
  26. package/dist/devices/dto.js +38 -1
  27. package/dist/devices/working-root.d.ts +41 -0
  28. package/dist/devices/working-root.js +50 -0
  29. package/dist/errors.d.ts +39 -0
  30. package/dist/errors.js +39 -0
  31. package/dist/evals/run-replay.d.ts +9 -1
  32. package/dist/evals/run-replay.js +15 -1
  33. package/dist/extensions/permission-set.d.ts +7 -6
  34. package/dist/extensions/permission-set.js +9 -6
  35. package/dist/extensions/permission-view.d.ts +4 -4
  36. package/dist/extensions/permission-view.js +10 -5
  37. package/dist/http/app.d.ts +8 -2
  38. package/dist/http/app.js +12 -2
  39. package/dist/http/plugin-dispatch.d.ts +55 -18
  40. package/dist/http/plugin-dispatch.js +124 -29
  41. package/dist/http/plugin-route-access.d.ts +56 -0
  42. package/dist/http/plugin-route-access.js +192 -0
  43. package/dist/identity/auth.guard.d.ts +26 -12
  44. package/dist/identity/auth.guard.js +62 -33
  45. package/dist/infra/throttler/named-throttlers.d.ts +3 -0
  46. package/dist/infra/throttler/named-throttlers.js +16 -0
  47. package/dist/infra/throttler/throttler.guard.d.ts +2 -1
  48. package/dist/infra/throttler/throttler.guard.js +2 -1
  49. package/dist/kernel.module.js +3 -0
  50. package/dist/local-devices/local-device-provider.d.ts +37 -1
  51. package/dist/local-devices/local-device-provider.js +33 -0
  52. package/dist/local-devices/machines.d.ts +2 -1
  53. package/dist/local-devices/machines.js +4 -1
  54. package/dist/local-devices/policy-summary.js +1 -0
  55. package/dist/local-devices/relay.d.ts +2 -0
  56. package/dist/local-devices/relay.js +3 -1
  57. package/dist/local-devices/targets.d.ts +6 -1
  58. package/dist/local-devices/targets.js +14 -0
  59. package/dist/overrides.d.ts +7 -0
  60. package/dist/overrides.js +4 -0
  61. package/dist/platform/ops/agent-skills.ops.js +2 -1
  62. package/dist/platform/ops/devices.ops.d.ts +3 -1
  63. package/dist/platform/ops/devices.ops.js +136 -3
  64. package/dist/runtime/context/build.d.ts +3 -0
  65. package/dist/runtime/context/build.js +14 -0
  66. package/dist/runtime/context/compaction.js +2 -0
  67. package/dist/runtime/context/project-root.d.ts +43 -0
  68. package/dist/runtime/context/project-root.js +85 -0
  69. package/dist/runtime/model-call.js +25 -1
  70. package/dist/runtime/project/knowledge.d.ts +129 -0
  71. package/dist/runtime/project/knowledge.js +248 -0
  72. package/dist/runtime/project/knowledge.service.d.ts +85 -0
  73. package/dist/runtime/project/knowledge.service.js +288 -0
  74. package/dist/runtime/run-worker.d.ts +5 -0
  75. package/dist/runtime/run-worker.js +27 -6
  76. package/dist/runtime/runtime.module.js +7 -0
  77. package/dist/sandbox/review/review.service.d.ts +14 -2
  78. package/dist/sandbox/review/review.service.js +2 -0
  79. package/dist/sandbox/review/rules.d.ts +6 -4
  80. package/dist/sandbox/review/rules.js +185 -62
  81. package/dist/sandbox/review/types.d.ts +14 -0
  82. package/dist/sandbox/sandbox.service.d.ts +16 -1
  83. package/dist/sandbox/sandbox.service.js +75 -3
  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/pipeline.d.ts +1 -1
  101. package/dist/tools/pipeline.js +53 -8
  102. package/dist/tools/registry.d.ts +6 -0
  103. package/package.json +3 -3
  104. package/src/migrations/0133_project_root.sql +30 -0
@@ -0,0 +1,288 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { DeviceProtocolError } from "@capybara-hq/plugin-sdk";
11
+ import { Inject, Injectable, Optional } from "@nestjs/common";
12
+ import { EventLog } from "../../conversations/event-log.js";
13
+ import { ConversationDevices } from "../../devices/conversation-devices.js";
14
+ import { DeviceProjectsService, withinMode } from "../../devices/device-projects.service.js";
15
+ import { aboveHome } from "../../devices/working-root.js";
16
+ import { ApiError } from "../../errors.js";
17
+ import { KernelDb } from "../../infra/database/kernel-db.js";
18
+ import { TransactionHost } from "../../infra/database/transaction-host.js";
19
+ import { Transactional } from "../../infra/database/transactional.decorator.js";
20
+ import { KERNEL_LOGGER } from "../../infra/tokens.js";
21
+ import { SandboxLockLostError, SandboxPathBusyError } from "../../sandbox/leases.js";
22
+ import { homeOf } from "../../sandbox/review/rules.js";
23
+ import { SandboxService } from "../../sandbox/sandbox.service.js";
24
+ import { providerConnectionFailure } from "../../sandbox/unreachable.js";
25
+ import { runEndedIn } from "../run-status.js";
26
+ import { projectRootIdemKey, projectUnavailable, readProjectKnowledge, sameProjectRoot, unavailableProject, } from "./knowledge.js";
27
+ /**
28
+ * The longest a run start waits for its project's knowledge (every device call of the read under
29
+ * one bound): past it the run goes on without a new snapshot, as when the device is unreachable.
30
+ */
31
+ export const PROJECT_KNOWLEDGE_TIMEOUT_MS = 30_000;
32
+ /**
33
+ * Test seam (`KernelSeams`): the read's time limit in ms, asked at every read; default
34
+ * {@link PROJECT_KNOWLEDGE_TIMEOUT_MS}.
35
+ */
36
+ export const PROJECT_KNOWLEDGE_TIMEOUT = Symbol("PROJECT_KNOWLEDGE_TIMEOUT");
37
+ /**
38
+ * An error that says the device could not be asked, not that the project is unreadable: the
39
+ * provider unreachable, a kernel refusal (the device offline, gone or busy, a relay given up), a
40
+ * malformed device answer, a lost lease.
41
+ */
42
+ function isDeviceFailure(e) {
43
+ return (providerConnectionFailure(e) !== undefined ||
44
+ e instanceof ApiError ||
45
+ e instanceof DeviceProtocolError ||
46
+ e instanceof SandboxLockLostError ||
47
+ e instanceof SandboxPathBusyError);
48
+ }
49
+ /**
50
+ * Run start's project knowledge (project root spec §4.4, §4.7): for a conversation bound to a
51
+ * project (`conversations.project_name` / `project_path`), reads the project's `AGENTS.md` and
52
+ * skills on its device before the run's first model call ({@link readProjectKnowledge}), outside
53
+ * any transaction, and appends a `project_root` event when the snapshot differs from the
54
+ * conversation's latest one. A free conversation reads nothing, so its device still starts lazily
55
+ * at its first sandbox call; an eval replay or a sub-agent never reads a device here. A sub-agent's
56
+ * run start copies its **root** conversation's latest `project_root` into its own log instead (it
57
+ * works in the same project), under its own run's idempotency key, and again only when the root's
58
+ * latest snapshot differs from the child's latest; the renderer then shows it to the child as is.
59
+ *
60
+ * Reads in the device's effective mode (`DeviceProjectsService.scopeFor`, the same scope the
61
+ * binding was checked against) and through `SandboxService.withSandbox`, as a tool call does, so
62
+ * a bound conversation starts its device eagerly (the accepted cost, spec §4.4). A project the
63
+ * run cannot use is recorded as `unavailable` with its reason: a project the device no longer
64
+ * keeps under the bound name and path (`removed`, not asked), a root that is gone, out of the mode
65
+ * (or above the owner's home) or unreadable, or a machine that does not announce projects
66
+ * (`unsupported`, not asked). The
67
+ * device not being reachable (offline, the provider down, the time limit) records nothing, and the
68
+ * run goes on as it does today when its device is away. Nothing is cached in the process.
69
+ */
70
+ let ProjectKnowledgeService = class ProjectKnowledgeService {
71
+ db;
72
+ eventLog;
73
+ txHost;
74
+ devices;
75
+ projects;
76
+ sandbox;
77
+ log;
78
+ timeoutMs;
79
+ constructor(db, eventLog, txHost, devices, projects, sandbox, log, timeoutMs = () => PROJECT_KNOWLEDGE_TIMEOUT_MS) {
80
+ this.db = db;
81
+ this.eventLog = eventLog;
82
+ this.txHost = txHost;
83
+ this.devices = devices;
84
+ this.projects = projects;
85
+ this.sandbox = sandbox;
86
+ this.log = log;
87
+ this.timeoutMs = timeoutMs;
88
+ }
89
+ /**
90
+ * Reads and records (see the class); notifies after the commit. False: the run had ended under
91
+ * the locks (a stop landed while the device was read), so nothing was appended and the run must
92
+ * not go on (as `PluginContextService.record`).
93
+ */
94
+ async record(input) {
95
+ if (input.kind === "subagent")
96
+ return this.copyFromRoot(input);
97
+ if (input.kind !== "chat")
98
+ return true;
99
+ const conv = await this.db.db
100
+ .selectFrom("conversations")
101
+ .select(["account_id", "project_name", "project_path"])
102
+ .where("id", "=", input.conversationId)
103
+ .where("org_id", "=", input.orgId)
104
+ .executeTakeFirst();
105
+ if (!conv || conv.project_name === null || conv.project_path === null)
106
+ return true;
107
+ const payload = await this.read(input, {
108
+ accountId: conv.account_id,
109
+ project: conv.project_name,
110
+ root: conv.project_path,
111
+ });
112
+ if (payload === null)
113
+ return true;
114
+ const seq = await this.append(input, payload);
115
+ if (seq === "ended")
116
+ return false;
117
+ if (seq !== null)
118
+ await this.eventLog.notify(input.conversationId, seq);
119
+ return true;
120
+ }
121
+ /**
122
+ * A sub-agent's run start: the root conversation's latest snapshot appended to the child's log
123
+ * when it differs from the child's latest ({@link append}); nothing when the root has none. No
124
+ * device call, so a replay of the child (which copies the child's own event) never needs one.
125
+ */
126
+ async copyFromRoot(input) {
127
+ const rootId = input.rootConversationId;
128
+ if (rootId === undefined || rootId === input.conversationId)
129
+ return true;
130
+ const latest = await this.db.db
131
+ .selectFrom("conversation_events")
132
+ .select("payload")
133
+ .where("conversation_id", "=", rootId)
134
+ .where("type", "=", "project_root")
135
+ .orderBy("seq", "desc")
136
+ .limit(1)
137
+ .executeTakeFirst();
138
+ if (latest === undefined)
139
+ return true;
140
+ // the stored payload passed the event schema when the root's run start appended it
141
+ const seq = await this.append(input, latest.payload);
142
+ if (seq === "ended")
143
+ return false;
144
+ if (seq !== null)
145
+ await this.eventLog.notify(input.conversationId, seq);
146
+ return true;
147
+ }
148
+ /** The snapshot read on the device, or null when the device could not be asked (logged). */
149
+ async read(input, bound) {
150
+ const where = {
151
+ orgId: input.orgId,
152
+ conversationId: input.conversationId,
153
+ runId: input.runId,
154
+ project: bound.project,
155
+ };
156
+ const signal = AbortSignal.any([input.signal, AbortSignal.timeout(this.timeoutMs())]);
157
+ try {
158
+ // the device a tool call of this conversation would enter, checked for its owner now
159
+ const deviceId = await this.devices.forTool(input.orgId, input.conversationId);
160
+ const scope = await this.projects.scopeFor(input.orgId, bound.accountId, deviceId);
161
+ // the binding is the name and the path: a project removed from the device (or re-added
162
+ // under its name with another path) no longer serves the conversation, which goes on free
163
+ if (!(await this.projects.keeps(deviceId, bound.project, bound.root))) {
164
+ return unavailableProject(bound.project, bound.root, projectUnavailable.removed(bound.project));
165
+ }
166
+ // a device that cannot serve list / scan (a machine without `projects`, a provider without
167
+ // them) is not asked: the project is recorded as unavailable, so an older snapshot stops
168
+ // applying
169
+ if (!scope.supported) {
170
+ this.log.warn("project knowledge not read: the device offers no projects", {
171
+ ...where,
172
+ deviceId,
173
+ });
174
+ return unavailableProject(bound.project, bound.root, projectUnavailable.unsupported());
175
+ }
176
+ // stat / readFile carry no ceiling: a machine running a wider mode than the org allows is
177
+ // not read unasked (exec refuses it too)
178
+ if (scope.widerThanAllowed) {
179
+ this.log.warn("project knowledge not read: the device runs a wider mode than allowed", {
180
+ ...where,
181
+ deviceId,
182
+ });
183
+ return unavailableProject(bound.project, bound.root, projectUnavailable.widerMode(bound.root));
184
+ }
185
+ return await this.sandbox.withSandbox({
186
+ orgId: input.orgId,
187
+ deviceId,
188
+ holderConversationId: input.conversationId,
189
+ // the read tells the model nothing: its first tool call still learns that the device
190
+ // started, restarted or had its files reset
191
+ silent: true,
192
+ actor: {
193
+ accountId: bound.accountId,
194
+ conversationId: input.conversationId,
195
+ runId: input.runId,
196
+ },
197
+ }, async (h, p, info, sig) => {
198
+ const target = { project: bound.project, root: bound.root, mode: scope.mode };
199
+ // the kernel's own check first: a root the mode cannot read is never asked about
200
+ // a root above the owner's home is refused as the working root's rule refuses it
201
+ // (`workingRootOf`, the same home: the reported one, else the workspace's prefix)
202
+ const reported = scope.home;
203
+ const home = reported?.startsWith("/") ? reported : homeOf(info.roots.workspace);
204
+ if (!withinMode(scope, bound.root, info.roots.workspace) || aboveHome(bound.root, home)) {
205
+ return unavailableProject(bound.project, bound.root, projectUnavailable.outsideMode(bound.root, scope.mode));
206
+ }
207
+ const list = p.list;
208
+ const files = {
209
+ stat: (path) => p.stat(h, path, sig),
210
+ readFile: (path, maxBytes) => p.readFile(h, path, { maxBytes, signal: sig }),
211
+ ...(list !== undefined && {
212
+ list: (path, limit) => list.call(p, h, path, { limit, limits: { mode: scope.mode }, signal: sig }),
213
+ }),
214
+ };
215
+ return readProjectKnowledge(files, target, {
216
+ log: (msg, detail) => this.log.info(msg, { ...where, ...detail }),
217
+ isDeviceFailure: (e) => sig.aborted || isDeviceFailure(e),
218
+ });
219
+ }, signal);
220
+ }
221
+ catch (err) {
222
+ this.log.warn("project knowledge not read: the device could not be asked; the run goes on", {
223
+ ...where,
224
+ timedOut: signal.aborted && !input.signal.aborted,
225
+ err,
226
+ });
227
+ return null;
228
+ }
229
+ }
230
+ /**
231
+ * One transaction under the conversation row lock (as `PluginContextService`): compares the
232
+ * snapshot with the conversation's latest `project_root` and appends it when it differs, with
233
+ * the run's idempotency key, so a retried run start finds its event (or nothing changed).
234
+ * Returns the seq written, null for none, `ended` when the run is terminal under the locks.
235
+ */
236
+ async append(input, payload) {
237
+ const t = this.db.db;
238
+ await t
239
+ .selectFrom("conversations")
240
+ .select("id")
241
+ .where("id", "=", input.conversationId)
242
+ .forUpdate()
243
+ .executeTakeFirst();
244
+ if (await runEndedIn(t, input.runId))
245
+ return "ended";
246
+ const latest = await t
247
+ .selectFrom("conversation_events")
248
+ .select(["seq", "payload"])
249
+ .where("conversation_id", "=", input.conversationId)
250
+ .where("type", "=", "project_root")
251
+ .orderBy("seq", "desc")
252
+ .limit(1)
253
+ .executeTakeFirst();
254
+ // the stored payload passed the event schema when it was appended (`skills` defaulted)
255
+ if (latest !== undefined && sameProjectRoot(latest.payload, payload)) {
256
+ return null;
257
+ }
258
+ const [written] = await this.eventLog.appendIn(input.conversationId, [
259
+ {
260
+ type: "project_root",
261
+ runId: input.runId,
262
+ idemKey: projectRootIdemKey(input.runId),
263
+ payload,
264
+ },
265
+ ]);
266
+ // an earlier attempt of this run start already wrote its snapshot: nothing new to tell
267
+ if (written === undefined || (latest !== undefined && written.seq <= Number(latest.seq))) {
268
+ return null;
269
+ }
270
+ return written.seq;
271
+ }
272
+ };
273
+ __decorate([
274
+ Transactional()
275
+ ], ProjectKnowledgeService.prototype, "append", null);
276
+ ProjectKnowledgeService = __decorate([
277
+ Injectable(),
278
+ __param(0, Inject(KernelDb)),
279
+ __param(1, Inject(EventLog)),
280
+ __param(2, Inject(TransactionHost)),
281
+ __param(3, Inject(ConversationDevices)),
282
+ __param(4, Inject(DeviceProjectsService)),
283
+ __param(5, Inject(SandboxService)),
284
+ __param(6, Inject(KERNEL_LOGGER)),
285
+ __param(7, Optional()),
286
+ __param(7, Inject(PROJECT_KNOWLEDGE_TIMEOUT))
287
+ ], ProjectKnowledgeService);
288
+ export { ProjectKnowledgeService };
@@ -14,6 +14,7 @@ import { KyselyCheckpointSaver } from "./checkpointer.js";
14
14
  import { CompactionService } from "./compaction.service.js";
15
15
  import { ConversationLock } from "./conversation-lock.js";
16
16
  import { PluginContextService } from "./plugin-context.service.js";
17
+ import { ProjectKnowledgeService } from "./project/knowledge.service.js";
17
18
  import { ProviderRegistry } from "./providers.js";
18
19
  import { type OrgQuota } from "./quota.js";
19
20
  import { RunContextService } from "./run-context.service.js";
@@ -77,6 +78,8 @@ export declare class RunWorker {
77
78
  private readonly runContext;
78
79
  /** Run start's plugin context (memory plugin spec §6.1): the `context@1` providers' text. */
79
80
  private readonly pluginContext;
81
+ /** Run start's project knowledge (project root spec §4.4): a bound project's AGENTS.md, skills. */
82
+ private readonly projectKnowledge;
80
83
  /** The run's resolved tools, for the plugin context's `tools` (chat runs with providers). */
81
84
  private readonly tools;
82
85
  private readonly finisher;
@@ -110,6 +113,8 @@ export declare class RunWorker {
110
113
  runContext: Pick<RunContextService, "record">,
111
114
  /** Run start's plugin context (memory plugin spec §6.1): the `context@1` providers' text. */
112
115
  pluginContext: Pick<PluginContextService, "record">,
116
+ /** Run start's project knowledge (project root spec §4.4): a bound project's AGENTS.md, skills. */
117
+ projectKnowledge: Pick<ProjectKnowledgeService, "record">,
113
118
  /** The run's resolved tools, for the plugin context's `tools` (chat runs with providers). */
114
119
  tools: Pick<ToolRegistry, "forAgent">, finisher: Pick<RunFinish, "finish">, txHost: TransactionHost,
115
120
  /** The steps' live drafts this process writes for the runs it executes. */
@@ -33,6 +33,7 @@ import { ConversationLock } from "./conversation-lock.js";
33
33
  import { runDeadline, tightestLimit } from "./limits.js";
34
34
  import { RunError } from "./model-call.js";
35
35
  import { PluginContextService } from "./plugin-context.service.js";
36
+ import { ProjectKnowledgeService } from "./project/knowledge.service.js";
36
37
  import { ProviderListError, ProviderRegistry } from "./providers.js";
37
38
  import { ORG_QUOTA_LEASE_MS, orgRunLimit, RUN_QUOTA } from "./quota.js";
38
39
  import { RunContextService } from "./run-context.service.js";
@@ -101,6 +102,7 @@ let RunWorker = class RunWorker {
101
102
  skillsCatalog;
102
103
  runContext;
103
104
  pluginContext;
105
+ projectKnowledge;
104
106
  tools;
105
107
  finisher;
106
108
  txHost;
@@ -132,6 +134,8 @@ let RunWorker = class RunWorker {
132
134
  runContext,
133
135
  /** Run start's plugin context (memory plugin spec §6.1): the `context@1` providers' text. */
134
136
  pluginContext,
137
+ /** Run start's project knowledge (project root spec §4.4): a bound project's AGENTS.md, skills. */
138
+ projectKnowledge,
135
139
  /** The run's resolved tools, for the plugin context's `tools` (chat runs with providers). */
136
140
  tools, finisher, txHost,
137
141
  /** The steps' live drafts this process writes for the runs it executes. */
@@ -155,6 +159,7 @@ let RunWorker = class RunWorker {
155
159
  this.skillsCatalog = skillsCatalog;
156
160
  this.runContext = runContext;
157
161
  this.pluginContext = pluginContext;
162
+ this.projectKnowledge = projectKnowledge;
158
163
  this.tools = tools;
159
164
  this.finisher = finisher;
160
165
  this.txHost = txHost;
@@ -695,6 +700,21 @@ let RunWorker = class RunWorker {
695
700
  });
696
701
  if (!pluginContexted)
697
702
  return "finished";
703
+ // project knowledge (project root spec §4.4): a conversation bound to a project reads its
704
+ // AGENTS.md and skills on the device before the first model call, outside any transaction;
705
+ // a free one reads nothing, a sub-agent copies its root's latest snapshot. Its append
706
+ // re-reads the run under the locks, as above.
707
+ const projectRead = await this.projectKnowledge.record({
708
+ orgId: conv.org_id,
709
+ conversationId,
710
+ runId,
711
+ kind: conv.kind,
712
+ // a sub-agent copies its root's snapshot: it works in the root's project
713
+ ...(parent !== undefined && { rootConversationId: parent.rootConversationId }),
714
+ signal: state.shutdown.signal,
715
+ });
716
+ if (!projectRead)
717
+ return "finished";
698
718
  await this.hooks?.beforeRunning?.(runId, conversationId);
699
719
  // never over a terminal status: a run finished meanwhile stays finished
700
720
  const updated = await this.db
@@ -1199,11 +1219,12 @@ RunWorker = __decorate([
1199
1219
  __param(18, Inject(SkillsCatalogService)),
1200
1220
  __param(19, Inject(RunContextService)),
1201
1221
  __param(20, Inject(PluginContextService)),
1202
- __param(21, Inject(ToolRegistry)),
1203
- __param(22, Inject(RunFinish)),
1204
- __param(23, Inject(TransactionHost)),
1205
- __param(24, Inject(RunDrafts)),
1206
- __param(25, Optional()),
1207
- __param(25, Inject(RUN_HOOKS))
1222
+ __param(21, Inject(ProjectKnowledgeService)),
1223
+ __param(22, Inject(ToolRegistry)),
1224
+ __param(23, Inject(RunFinish)),
1225
+ __param(24, Inject(TransactionHost)),
1226
+ __param(25, Inject(RunDrafts)),
1227
+ __param(26, Optional()),
1228
+ __param(26, Inject(RUN_HOOKS))
1208
1229
  ], RunWorker);
1209
1230
  export { RunWorker };
@@ -11,12 +11,14 @@ import { ConversationAccessModule } from "../conversations/access.js";
11
11
  import { AttachmentsModule } from "../conversations/attachments.module.js";
12
12
  import { EventLogModule } from "../conversations/event-log.js";
13
13
  import { TitlesModule } from "../conversations/titles.js";
14
+ import { DevicesModule } from "../devices/devices.module.js";
14
15
  import { ExtensionsModule } from "../extensions/extensions.module.js";
15
16
  import { GitModule } from "../git/git.module.js";
16
17
  import { IdentityModule } from "../identity/identity.module.js";
17
18
  import { LockService } from "../infra/lock/lock.service.js";
18
19
  import { QueueModule } from "../infra/queue/queue.module.js";
19
20
  import { REDIS } from "../infra/tokens.js";
21
+ import { SandboxModule } from "../sandbox/sandbox.module.js";
20
22
  import { ScheduledTaskTrackerModule } from "../schedules/run-tracker.js";
21
23
  import { SkillsModule } from "../skills/skills.module.js";
22
24
  import { ToolsModule } from "../tools/tools.module.js";
@@ -28,6 +30,7 @@ import { CompactionSettingsService } from "./compaction-settings.service.js";
28
30
  import { ConversationLock } from "./conversation-lock.js";
29
31
  import { MAINTENANCE_QUEUE_NAME } from "./maintenance-queue.js";
30
32
  import { PluginContextService } from "./plugin-context.service.js";
33
+ import { ProjectKnowledgeService } from "./project/knowledge.service.js";
31
34
  import { ProvidersModule } from "./providers.js";
32
35
  import { EVAL_QUOTA, EVAL_QUOTA_PREFIX, OrgQuota, RUN_QUOTA, RUN_QUOTA_PREFIX } from "./quota.js";
33
36
  import { RunContextService } from "./run-context.service.js";
@@ -62,6 +65,9 @@ RuntimeModule = __decorate([
62
65
  IdentityModule,
63
66
  SkillsModule,
64
67
  ToolsModule,
68
+ // a bound conversation's device and project scope (project root spec §4.4)
69
+ DevicesModule,
70
+ SandboxModule,
65
71
  GitModule,
66
72
  ProvidersModule,
67
73
  RunJobsModule,
@@ -90,6 +96,7 @@ RuntimeModule = __decorate([
90
96
  RunSteps,
91
97
  RunContextService,
92
98
  PluginContextService,
99
+ ProjectKnowledgeService,
93
100
  RunWorker,
94
101
  // the org's effective compaction settings, one read per model step
95
102
  CompactionSettingsService,
@@ -42,10 +42,22 @@ export interface ReviewInput {
42
42
  input: unknown;
43
43
  mode: "auto" | "sandbox";
44
44
  /**
45
- * The device's workspace root. Absent: no device is bound, so the tool fails on its own and
46
- * there is nothing to review.
45
+ * The conversation's working root: the device's workspace root, or the bound project's root
46
+ * (project root spec §4.3). Commands start here and a write inside it needs no ask. Absent: no
47
+ * device is bound, so the tool fails on its own and there is nothing to review.
47
48
  */
48
49
  workspace?: string | undefined;
50
+ /**
51
+ * The owner's home directory (the machine's, else the device workspace's prefix), never derived
52
+ * from a project root: `~` and the home-level sensitive paths are judged against it. Absent or
53
+ * null: the rules derive it from `workspace`.
54
+ */
55
+ home?: string | null | undefined;
56
+ /**
57
+ * The home prefix of the device's own workspace, judged beside `home` (a project outside every
58
+ * home still protects it). Absent or null: none.
59
+ */
60
+ deviceHome?: string | null | undefined;
49
61
  /** Whether a person is present to answer an `ask` (rules may read it; the pipeline decides). */
50
62
  attended: boolean;
51
63
  /** Hook payload facts (optional: without enough of them no model hook fires). */
@@ -116,6 +116,8 @@ let CommandReviewService = class CommandReviewService {
116
116
  const facts = {
117
117
  workspace,
118
118
  cwd: workspace,
119
+ home: input.home ?? null,
120
+ deviceHome: input.deviceHome ?? null,
119
121
  mode: input.mode,
120
122
  attended: input.attended,
121
123
  };
@@ -63,8 +63,9 @@
63
63
  */
64
64
  import type { ParsedCommand, ReviewFacts, ReviewResult } from "./types.js";
65
65
  export type { ParsedCommand, Redirect, ReviewFacts, ReviewResult, RuleHit, RuleOutcome, SimpleCommand, } from "./types.js";
66
- /** The public parser: the parsed command, or null when the tokenizer is not confident. */
67
66
  export declare function parseCommand(command: string): ParsedCommand | null;
67
+ /** The home directory a path lies in by its `/home/<u>`, `/Users/<u>` or `/root` prefix, or null. */
68
+ export declare function homeOf(workspace: string): string | null;
68
69
  /**
69
70
  * The absolute paths a command line writes, for the review's record of what a conversation wrote
70
71
  * (hardening spec R1: running one of them later is not a development command). An unparseable line
@@ -78,8 +79,9 @@ export declare function writtenPaths(command: string, facts: ReviewFacts): strin
78
79
  */
79
80
  export declare function reviewRules(command: string, facts: ReviewFacts): ReviewResult;
80
81
  /**
81
- * The same table for a file-tool write: workspace and tmp writes pass; the device's own files are
82
- * denied; git hooks, shell rc files and secret files ask; anything else outside asks to write
83
- * outside.
82
+ * The same table for a file-tool write: the device's own files are denied; git hooks and
83
+ * home-level sensitive paths (shell rc files, SSH, launch agents, credentials) ask wherever they
84
+ * lie, inside the workspace too; other workspace and tmp writes pass; a `.env*` file outside the
85
+ * workspace asks; anything else outside asks to write outside.
84
86
  */
85
87
  export declare function reviewWrite(path: string, facts: ReviewFacts): ReviewResult;