@capybara-hq/kernel 0.1.0-rc.8 → 0.1.0-rc.9

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 (110) hide show
  1. package/dist/background/finish.js +2 -1
  2. package/dist/background/launch.d.ts +13 -7
  3. package/dist/background/launch.js +15 -8
  4. package/dist/background/poll.job.d.ts +1 -1
  5. package/dist/background/poll.job.js +8 -3
  6. package/dist/background/session.d.ts +2 -2
  7. package/dist/background/session.js +3 -2
  8. package/dist/background/status.d.ts +1 -1
  9. package/dist/background/status.js +1 -1
  10. package/dist/background/stop.js +4 -3
  11. package/dist/background/store.d.ts +6 -1
  12. package/dist/background/store.js +5 -1
  13. package/dist/background/tasks.service.d.ts +1 -1
  14. package/dist/background/tasks.service.js +4 -4
  15. package/dist/catalog/marketplaces.service.d.ts +1 -1
  16. package/dist/config/docs.js +2 -0
  17. package/dist/config/schema.d.ts +7 -0
  18. package/dist/config/schema.js +45 -0
  19. package/dist/conversations/event-log.d.ts +4 -3
  20. package/dist/conversations/event-log.js +4 -3
  21. package/dist/conversations/events.d.ts +9 -0
  22. package/dist/conversations/events.js +14 -0
  23. package/dist/db/kernel-schema.d.ts +26 -1
  24. package/dist/devices/device-auth.controller.d.ts +42 -0
  25. package/dist/devices/device-auth.controller.js +116 -0
  26. package/dist/devices/device-auth.d.ts +119 -0
  27. package/dist/devices/device-auth.js +403 -0
  28. package/dist/devices/device-auth.module.d.ts +7 -0
  29. package/dist/devices/device-auth.module.js +25 -0
  30. package/dist/devices/device-gateway.d.ts +128 -0
  31. package/dist/devices/device-gateway.js +530 -0
  32. package/dist/devices/device-provider.d.ts +89 -0
  33. package/dist/devices/device-provider.js +151 -0
  34. package/dist/devices/device-routes.d.ts +36 -0
  35. package/dist/devices/device-routes.js +90 -0
  36. package/dist/devices/devices.module.d.ts +16 -0
  37. package/dist/devices/devices.module.js +44 -0
  38. package/dist/devices/dto.d.ts +66 -0
  39. package/dist/devices/dto.js +44 -0
  40. package/dist/devices/keys.d.ts +25 -0
  41. package/dist/devices/keys.js +25 -0
  42. package/dist/devices/machines.d.ts +106 -0
  43. package/dist/devices/machines.js +301 -0
  44. package/dist/devices/relay.d.ts +100 -0
  45. package/dist/devices/relay.js +373 -0
  46. package/dist/devices/targets.d.ts +28 -0
  47. package/dist/devices/targets.js +73 -0
  48. package/dist/environments/access.d.ts +11 -6
  49. package/dist/environments/access.js +23 -8
  50. package/dist/environments/conversation-environments.d.ts +20 -6
  51. package/dist/environments/conversation-environments.js +70 -9
  52. package/dist/environments/environments.controller.d.ts +4 -1
  53. package/dist/environments/environments.controller.js +15 -1
  54. package/dist/environments/environments.module.js +3 -1
  55. package/dist/environments/environments.service.d.ts +54 -7
  56. package/dist/environments/environments.service.js +104 -20
  57. package/dist/environments/legacy-cleanup.job.d.ts +1 -1
  58. package/dist/environments/legacy-cleanup.job.js +4 -4
  59. package/dist/errors.d.ts +24 -0
  60. package/dist/errors.js +24 -0
  61. package/dist/evals/recorded-tools.d.ts +9 -0
  62. package/dist/evals/recorded-tools.js +27 -0
  63. package/dist/http/api.module.js +4 -0
  64. package/dist/http/gateway-routes.d.ts +7 -3
  65. package/dist/http/gateway-routes.js +6 -3
  66. package/dist/infra/config/kernel-config.service.js +2 -0
  67. package/dist/infra/throttler/named-throttlers.d.ts +5 -0
  68. package/dist/infra/throttler/named-throttlers.js +38 -0
  69. package/dist/kernel.module.js +3 -0
  70. package/dist/lifecycle/kernel-lifecycle.js +5 -0
  71. package/dist/platform/exclusions.js +33 -0
  72. package/dist/platform/ops/environments.ops.js +21 -3
  73. package/dist/roles/gateway-role.d.ts +14 -3
  74. package/dist/roles/gateway-role.js +25 -5
  75. package/dist/roles/gateway.module.d.ts +2 -1
  76. package/dist/roles/gateway.module.js +6 -3
  77. package/dist/runtime/context/build.d.ts +2 -0
  78. package/dist/runtime/context/build.js +14 -0
  79. package/dist/runtime/context/compaction.js +1 -0
  80. package/dist/runtime/context/environment-paths.d.ts +40 -0
  81. package/dist/runtime/context/environment-paths.js +48 -0
  82. package/dist/runtime/model-call.js +2 -0
  83. package/dist/sandbox/providers.d.ts +29 -7
  84. package/dist/sandbox/providers.js +43 -8
  85. package/dist/sandbox/sandbox.service.d.ts +70 -18
  86. package/dist/sandbox/sandbox.service.js +176 -59
  87. package/dist/tools/builtins/sandbox/bash.d.ts +2 -2
  88. package/dist/tools/builtins/sandbox/bash.js +39 -27
  89. package/dist/tools/builtins/sandbox/common.d.ts +60 -25
  90. package/dist/tools/builtins/sandbox/common.js +121 -30
  91. package/dist/tools/builtins/sandbox/deliver.js +9 -6
  92. package/dist/tools/builtins/sandbox/edit.js +40 -37
  93. package/dist/tools/builtins/sandbox/glob.js +13 -8
  94. package/dist/tools/builtins/sandbox/grep.js +10 -6
  95. package/dist/tools/builtins/sandbox/index.d.ts +6 -4
  96. package/dist/tools/builtins/sandbox/index.js +6 -4
  97. package/dist/tools/builtins/sandbox/read.js +9 -6
  98. package/dist/tools/builtins/sandbox/task-output.d.ts +7 -3
  99. package/dist/tools/builtins/sandbox/task-output.js +14 -10
  100. package/dist/tools/builtins/sandbox/task-stop.js +3 -3
  101. package/dist/tools/builtins/sandbox/write.js +33 -30
  102. package/dist/tools/builtins/skill-load.d.ts +8 -5
  103. package/dist/tools/builtins/skill-load.js +21 -15
  104. package/dist/tools/builtins/skill-write.js +2 -2
  105. package/dist/tools/pipeline.js +2 -0
  106. package/dist/tools/registry.d.ts +21 -4
  107. package/dist/tools/registry.js +10 -2
  108. package/package.json +5 -2
  109. package/src/migrations/0093_connected_environments.sql +37 -0
  110. package/src/migrations/0094_environment_paths_idx.sql +7 -0
@@ -0,0 +1,373 @@
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 { randomUUID } from "node:crypto";
11
+ import { DeviceProtocolError, FileSystemGoneError, parseDeviceFrame, parseOpResult, SandboxGoneError, ToolError, } from "@capybara-hq/plugin-sdk";
12
+ import { Inject, Injectable } from "@nestjs/common";
13
+ import { ApiError } from "../errors.js";
14
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
15
+ import { LockService } from "../infra/lock/lock.service.js";
16
+ import { KERNEL_BUS, KERNEL_LOGGER, REDIS } from "../infra/tokens.js";
17
+ import { environmentNotFound } from "../sandbox/sandbox.service.js";
18
+ import { deviceLeaseKey, deviceReqKey, deviceReqTopic, deviceResKey, deviceResTopic, } from "./keys.js";
19
+ import { DeviceTargets } from "./targets.js";
20
+ /** The environment's connected machine is not connected (no replica holds its lease). */
21
+ export const environmentOffline = () => new ApiError("environment.offline", "The connected machine of this environment is offline. Start capybara-device on that machine, or pick another environment.");
22
+ /**
23
+ * The environment's machine was disconnected (revoked, signed out): 403 `environment.forbidden`
24
+ * with `details: [{ reason: "machine_revoked" }]`, so the tools tell the model what to ask for.
25
+ */
26
+ export const machineRevoked = () => new ApiError("environment.forbidden", "The connected machine of this environment was disconnected. Run capybara-device logout, then capybara-device login on it, or pick another environment.", [{ reason: "machine_revoked" }]);
27
+ /** The text a machine's `error` result reaches the model with (ruling R28). */
28
+ export const ON_MACHINE_PREFIX = "On the connected machine: ";
29
+ /** The longest one lease check of a waiting caller may take before it counts as "unknown". */
30
+ export const LEASE_CHECK_TIMEOUT_MS = 2000;
31
+ /** How often a waiting caller checks that a replica still holds the machine's connection. */
32
+ export const HOLDER_POLL_MS = 5000;
33
+ /**
34
+ * How much longer than the caller's wait a queued request lives: giving up must still find an
35
+ * untaken request on the list (an expired one would look taken and get a needless cancel).
36
+ */
37
+ export const LIST_TTL_MARGIN_MS = 30_000;
38
+ /**
39
+ * Appends one frame to the request list and makes sure the list lives at least `ARGV[2]` ms
40
+ * (never shortens the TTL a request with a longer deadline set). Atomic, so the list never exists
41
+ * without a TTL.
42
+ */
43
+ const PUSH_WITH_TTL = `
44
+ redis.call('RPUSH', KEYS[1], ARGV[1])
45
+ if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then
46
+ redis.call('PEXPIRE', KEYS[1], ARGV[2])
47
+ end
48
+ return 1
49
+ `;
50
+ /**
51
+ * Gives up on a request (ruling R15): removes the exact request frame `ARGV[1]` from the list; only
52
+ * when it is no longer queued (the machine already took it) appends the cancel `ARGV[2]`, with the
53
+ * TTL rule of {@link PUSH_WITH_TTL}. Returns 1 when a cancel was queued.
54
+ */
55
+ const GIVE_UP = `
56
+ if redis.call('LREM', KEYS[1], 1, ARGV[1]) > 0 then
57
+ return 0
58
+ end
59
+ redis.call('RPUSH', KEYS[1], ARGV[2])
60
+ if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[3]) then
61
+ redis.call('PEXPIRE', KEYS[1], ARGV[3])
62
+ end
63
+ return 1
64
+ `;
65
+ /** Why a call stopped waiting without a result. */
66
+ const ABORTED = Symbol("aborted");
67
+ const GRACE_EXPIRED = Symbol("grace expired");
68
+ const errMsg = (e) => (e instanceof Error ? e.message : String(e));
69
+ /**
70
+ * The caller side of the device relay (environments spec §13.4). A request goes onto the
71
+ * environment's Redis list (`capk:device-req:<environmentId>`) and is announced on
72
+ * `device.req.<environmentId>`; whichever gateway replica holds the machine's lease drains the
73
+ * list and forwards the frames to the machine.
74
+ *
75
+ * **The holder's half of the wire** (Task 6 follows it): items on the list are `KernelReq` or
76
+ * `KernelCancel` frames as JSON text, to be parsed with `parseDeviceFrame(item, "kernel")`; the
77
+ * nudge's payload is `{ id }`. For each `res` frame the machine sends, the holder writes the frame
78
+ * (the JSON text as validated by `parseDeviceFrame(…, "device")`) under
79
+ * `capk:device-res:<requestId>` with a TTL and publishes the same frame object on
80
+ * `device.res.<requestId>`.
81
+ *
82
+ * The caller subscribes to its result topic before pushing and reads the result key once after,
83
+ * and once more before giving up (the run-abort pattern; a publish lost while the subscriber
84
+ * reconnects is still found). It waits within its own call for at most `deadlineMs` plus
85
+ * `environments.device.reconnectGraceSeconds`; when the lease disappears meanwhile (checked every
86
+ * {@link HOLDER_POLL_MS}) the wait ends at most one grace later. A result produced while the
87
+ * machine was disconnected arrives after it reconnects. Giving up (grace, abort, a bad result)
88
+ * removes the request from the list, or queues a `cancel` when the machine already took it
89
+ * (ruling R15). Nothing is kept in process memory beyond the call.
90
+ */
91
+ let DeviceRelay = class DeviceRelay {
92
+ redis;
93
+ bus;
94
+ locks;
95
+ config;
96
+ log;
97
+ targets;
98
+ constructor(redis, bus, locks, config, log, targets) {
99
+ this.redis = redis;
100
+ this.bus = bus;
101
+ this.locks = locks;
102
+ this.config = config;
103
+ this.log = log;
104
+ this.targets = targets;
105
+ }
106
+ /** Whether a gateway replica holds the machine's connection now. */
107
+ async holderLive(environmentId) {
108
+ return (await this.locks.holderOf(deviceLeaseKey(environmentId))) !== null;
109
+ }
110
+ /**
111
+ * Queues `op` for the machine without waiting for an answer (ruling R14: `stop`). The caller
112
+ * checks {@link holderLive} first; the frame waits on the list for the grace like any request.
113
+ */
114
+ async send(environmentId, op, args, deadlineMs) {
115
+ const id = randomUUID();
116
+ const d = Math.max(1, Math.ceil(deadlineMs));
117
+ const req = { type: "req", id, op, args, deadlineMs: d };
118
+ await this.push(environmentId, JSON.stringify(req), this.graceMs() + d + LIST_TTL_MARGIN_MS);
119
+ await this.bus.publish(deviceReqTopic(environmentId), { id });
120
+ }
121
+ /**
122
+ * Runs `op` on the environment's machine and returns its validated result. Throws
123
+ * `environment.offline` when no replica holds the machine at send time or when the wait ran
124
+ * out, but `environment.forbidden` ({@link machineRevoked}) when the machine was revoked and
125
+ * `environment.not_found` when the environment was deleted (checked whenever the connection is
126
+ * missing, ruling R27); the machine's failures map to `SandboxGoneError` (`gone`),
127
+ * `FileSystemGoneError` (`fs_gone`) or a `ToolError` (`policy`, and `error` prefixed
128
+ * {@link ON_MACHINE_PREFIX}); an invalid answer throws a `DeviceProtocolError`. On abort the
129
+ * abort reason is thrown.
130
+ */
131
+ async call(environmentId, op, args, opts) {
132
+ const { signal } = opts;
133
+ signal.throwIfAborted();
134
+ if (!(await this.holderLive(environmentId))) {
135
+ throw this.goneError(await this.targetState(environmentId)) ?? environmentOffline();
136
+ }
137
+ signal.throwIfAborted();
138
+ const id = randomUUID();
139
+ const deadlineMs = Math.max(1, Math.ceil(opts.deadlineMs));
140
+ const graceMs = this.graceMs();
141
+ const listTtlMs = graceMs + deadlineMs + LIST_TTL_MARGIN_MS;
142
+ const reqJson = JSON.stringify({ type: "req", id, op, args, deadlineMs });
143
+ let settle;
144
+ const outcome = new Promise((resolve) => {
145
+ let done = false;
146
+ settle = (r) => {
147
+ if (done)
148
+ return;
149
+ done = true;
150
+ resolve(r);
151
+ };
152
+ });
153
+ const onStored = (raw) => {
154
+ if (raw !== null)
155
+ settle(this.parseRes(raw, id));
156
+ };
157
+ const onAbort = () => settle({ error: ABORTED });
158
+ const unsubscribe = await this.bus.subscribe(deviceResTopic(id), (payload) => {
159
+ onStored(JSON.stringify(payload));
160
+ });
161
+ let timer;
162
+ let stopped = false;
163
+ try {
164
+ signal.addEventListener("abort", onAbort, { once: true });
165
+ // aborted while subscribing: nothing was sent yet
166
+ signal.throwIfAborted();
167
+ await this.push(environmentId, reqJson, listTtlMs);
168
+ this.nudge(environmentId, id);
169
+ onStored(await this.redis.get(deviceResKey(id)));
170
+ // wait until the result, the abort, or `giveUpAt`. The lease is checked every poll: its loss
171
+ // caps `giveUpAt` at one grace from then (never extending it); a reconnect within that grace
172
+ // restores the full wait, so a long command whose machine blipped keeps its deadline
173
+ const fullGiveUpAt = Date.now() + deadlineMs + graceMs;
174
+ let giveUpAt = fullGiveUpAt;
175
+ let capped = false;
176
+ const tick = async () => {
177
+ if (stopped)
178
+ return;
179
+ try {
180
+ // a stalled Redis must not hold the give-up back: no answer in time means "unknown"
181
+ const live = await this.leaseCheck(environmentId);
182
+ if (live === true && capped) {
183
+ capped = false;
184
+ giveUpAt = fullGiveUpAt;
185
+ }
186
+ else if (live === false && !capped) {
187
+ capped = true;
188
+ giveUpAt = Math.min(giveUpAt, Date.now() + graceMs);
189
+ }
190
+ if (live === false) {
191
+ // a revoked machine or a deleted environment never comes back: fail at once
192
+ const gone = this.goneError(await this.targetState(environmentId));
193
+ if (gone) {
194
+ settle({ error: gone });
195
+ return;
196
+ }
197
+ }
198
+ }
199
+ catch (e) {
200
+ this.log.warn("device relay lease check failed", { environmentId, error: errMsg(e) });
201
+ }
202
+ if (stopped)
203
+ return;
204
+ if (Date.now() >= giveUpAt) {
205
+ // a last look at the key: the publish may have been lost while the subscriber reconnected
206
+ this.redis.get(deviceResKey(id)).then((raw) => (raw !== null ? onStored(raw) : settle({ error: GRACE_EXPIRED })), () => settle({ error: GRACE_EXPIRED }));
207
+ return;
208
+ }
209
+ schedule();
210
+ };
211
+ const schedule = () => {
212
+ if (stopped)
213
+ return;
214
+ const wait = Math.max(0, Math.min(HOLDER_POLL_MS, giveUpAt - Date.now()));
215
+ timer = setTimeout(() => void tick(), wait);
216
+ };
217
+ schedule();
218
+ const r = await outcome;
219
+ stopped = true;
220
+ clearTimeout(timer);
221
+ if ("res" in r) {
222
+ await this.quietly("delete the result", environmentId, () => this.redis.del(deviceResKey(id)));
223
+ return this.resultOf(op, r.res);
224
+ }
225
+ if (r.error instanceof Error) {
226
+ // a bad answer: drop it, give the request up, report the protocol error
227
+ await this.quietly("delete the result", environmentId, () => this.redis.del(deviceResKey(id)));
228
+ }
229
+ await this.giveUp(environmentId, id, reqJson, listTtlMs);
230
+ if (r.error === ABORTED)
231
+ throw signal.reason;
232
+ if (r.error === GRACE_EXPIRED)
233
+ throw environmentOffline();
234
+ throw r.error;
235
+ }
236
+ finally {
237
+ stopped = true;
238
+ clearTimeout(timer);
239
+ signal.removeEventListener("abort", onAbort);
240
+ await this.quietly("unsubscribe", environmentId, unsubscribe);
241
+ }
242
+ }
243
+ /** {@link holderLive}, or undefined when it takes longer than {@link LEASE_CHECK_TIMEOUT_MS}. */
244
+ async leaseCheck(environmentId) {
245
+ let timer;
246
+ const check = this.holderLive(environmentId);
247
+ check.catch(() => { }); // a late failure after the timeout is not unhandled
248
+ try {
249
+ return await Promise.race([
250
+ check,
251
+ new Promise((resolve) => {
252
+ timer = setTimeout(() => resolve(undefined), LEASE_CHECK_TIMEOUT_MS);
253
+ }),
254
+ ]);
255
+ }
256
+ finally {
257
+ clearTimeout(timer);
258
+ }
259
+ }
260
+ /**
261
+ * The environment's state ({@link DeviceTargets}), or undefined when it cannot be read within
262
+ * {@link LEASE_CHECK_TIMEOUT_MS} (a failure only logs: the caller keeps its other rules).
263
+ */
264
+ async targetState(environmentId) {
265
+ let timer;
266
+ const read = this.targets.state(environmentId);
267
+ read.catch(() => { });
268
+ try {
269
+ return await Promise.race([
270
+ read,
271
+ new Promise((resolve) => {
272
+ timer = setTimeout(() => resolve(undefined), LEASE_CHECK_TIMEOUT_MS);
273
+ }),
274
+ ]);
275
+ }
276
+ catch (e) {
277
+ this.log.warn("device relay could not read the environment state", {
278
+ environmentId,
279
+ error: errMsg(e),
280
+ });
281
+ return undefined;
282
+ }
283
+ finally {
284
+ clearTimeout(timer);
285
+ }
286
+ }
287
+ /** The error of a state that never comes back; undefined for `active` or unknown. */
288
+ goneError(state) {
289
+ if (state === "deleted")
290
+ return environmentNotFound();
291
+ if (state === "revoked")
292
+ return machineRevoked();
293
+ return undefined;
294
+ }
295
+ graceMs() {
296
+ return this.config.get().environments.device.reconnectGraceSeconds * 1000;
297
+ }
298
+ async push(environmentId, frame, ttlMs) {
299
+ await this.redis.eval(PUSH_WITH_TTL, 1, deviceReqKey(environmentId), frame, String(ttlMs));
300
+ }
301
+ /** Announces new frames to the holder; a lost nudge is picked up on the holder's next drain. */
302
+ nudge(environmentId, id) {
303
+ this.bus.publish(deviceReqTopic(environmentId), { id }).catch((e) => {
304
+ this.log.warn("device relay nudge failed", { environmentId, error: errMsg(e) });
305
+ });
306
+ }
307
+ /** Removes the request from the list, or queues a cancel when the machine took it (R15). */
308
+ async giveUp(environmentId, id, reqJson, ttlMs) {
309
+ const cancel = JSON.stringify({ type: "cancel", id });
310
+ await this.quietly("give the request up", environmentId, async () => {
311
+ const queued = await this.redis.eval(GIVE_UP, 1, deviceReqKey(environmentId), reqJson, cancel, String(ttlMs));
312
+ if (queued === 1)
313
+ this.nudge(environmentId, id);
314
+ });
315
+ }
316
+ /** Runs a side step whose failure must not replace the call's outcome: logged, swallowed. */
317
+ async quietly(what, environmentId, fn) {
318
+ try {
319
+ await fn();
320
+ }
321
+ catch (e) {
322
+ this.log.warn(`device relay could not ${what}`, { environmentId, error: errMsg(e) });
323
+ }
324
+ }
325
+ /** A stored / published `res` frame for request `id`, or the protocol error it carries. */
326
+ parseRes(raw, id) {
327
+ try {
328
+ const f = parseDeviceFrame(raw, "device");
329
+ if (f.type !== "res" || f.id !== id) {
330
+ return { error: new DeviceProtocolError("the connected machine sent an unexpected frame") };
331
+ }
332
+ return { res: f };
333
+ }
334
+ catch (e) {
335
+ return {
336
+ error: e instanceof DeviceProtocolError
337
+ ? e
338
+ : new DeviceProtocolError(`the connected machine sent an invalid frame: ${errMsg(e)}`),
339
+ };
340
+ }
341
+ }
342
+ resultOf(op, res) {
343
+ if (res.ok) {
344
+ try {
345
+ return parseOpResult(op, res.result);
346
+ }
347
+ catch (e) {
348
+ throw new DeviceProtocolError(`The connected machine sent an invalid ${op} result: ${errMsg(e)}`);
349
+ }
350
+ }
351
+ const { kind, message } = res.error;
352
+ switch (kind) {
353
+ case "gone":
354
+ throw new SandboxGoneError(message);
355
+ case "fs_gone":
356
+ throw new FileSystemGoneError(message);
357
+ case "policy":
358
+ throw new ToolError(`Refused by the machine's local policy: ${message}`);
359
+ default:
360
+ throw new ToolError(`${ON_MACHINE_PREFIX}${message}`);
361
+ }
362
+ }
363
+ };
364
+ DeviceRelay = __decorate([
365
+ Injectable(),
366
+ __param(0, Inject(REDIS)),
367
+ __param(1, Inject(KERNEL_BUS)),
368
+ __param(2, Inject(LockService)),
369
+ __param(3, Inject(KernelConfigService)),
370
+ __param(4, Inject(KERNEL_LOGGER)),
371
+ __param(5, Inject(DeviceTargets))
372
+ ], DeviceRelay);
373
+ export { DeviceRelay };
@@ -0,0 +1,28 @@
1
+ import type { SandboxRoots } from "@capybara-hq/plugin-sdk";
2
+ import { KernelDb } from "../infra/database/kernel-db.js";
3
+ /**
4
+ * Whether a device environment can still be served: `active` (live and an unrevoked machine),
5
+ * `revoked` (live, no active machine: disconnected or signed out) or `deleted` (missing or
6
+ * deleted, or its org deleted).
7
+ */
8
+ export type DeviceTargetState = "active" | "revoked" | "deleted";
9
+ /** Whether two sets of roots name the same three directories. */
10
+ export declare const sameRoots: (a: SandboxRoots, b: SandboxRoots) => boolean;
11
+ /**
12
+ * Reads a device environment and its machine from PG for the relay and the `@device` provider.
13
+ * The relay asks {@link state} when a machine has no connection, so a revoked machine or a deleted
14
+ * environment fails a waiting call at once instead of after the reconnect grace (ruling R27).
15
+ * Kept apart from `MachinesService` (which depends on the relay) so there is no dependency cycle.
16
+ */
17
+ export declare class DeviceTargets {
18
+ private readonly db;
19
+ constructor(db: KernelDb);
20
+ state(environmentId: string): Promise<DeviceTargetState>;
21
+ /** The org of a live environment, or null (missing or deleted). */
22
+ orgOf(environmentId: string): Promise<string | null>;
23
+ /**
24
+ * The roots the environment's active machine last reported in its hello frame, or null (no
25
+ * active machine, or none reported yet).
26
+ */
27
+ machineRoots(environmentId: string): Promise<SandboxRoots | null>;
28
+ }
@@ -0,0 +1,73 @@
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 { Inject, Injectable } from "@nestjs/common";
11
+ import { KernelDb } from "../infra/database/kernel-db.js";
12
+ const ROOT_KEYS = ["workspace", "skills", "tmp"];
13
+ /** Whether two sets of roots name the same three directories. */
14
+ export const sameRoots = (a, b) => ROOT_KEYS.every((k) => a[k] === b[k]);
15
+ /**
16
+ * Reads a device environment and its machine from PG for the relay and the `@device` provider.
17
+ * The relay asks {@link state} when a machine has no connection, so a revoked machine or a deleted
18
+ * environment fails a waiting call at once instead of after the reconnect grace (ruling R27).
19
+ * Kept apart from `MachinesService` (which depends on the relay) so there is no dependency cycle.
20
+ */
21
+ let DeviceTargets = class DeviceTargets {
22
+ db;
23
+ constructor(db) {
24
+ this.db = db;
25
+ }
26
+ async state(environmentId) {
27
+ const row = await this.db
28
+ .selectFrom("environments as e")
29
+ .innerJoin("orgs as o", "o.id", "e.org_id")
30
+ .leftJoin("environment_machines as m", (j) => j.onRef("m.environment_id", "=", "e.id").on("m.revoked_at", "is", null))
31
+ .select(["e.id", "m.id as machine_id"])
32
+ .where("e.id", "=", environmentId)
33
+ .where("e.deleted_at", "is", null)
34
+ .where("o.deleted_at", "is", null)
35
+ .executeTakeFirst();
36
+ if (!row)
37
+ return "deleted";
38
+ return row.machine_id === null ? "revoked" : "active";
39
+ }
40
+ /** The org of a live environment, or null (missing or deleted). */
41
+ async orgOf(environmentId) {
42
+ const row = await this.db
43
+ .selectFrom("environments")
44
+ .select("org_id")
45
+ .where("id", "=", environmentId)
46
+ .where("deleted_at", "is", null)
47
+ .executeTakeFirst();
48
+ return row?.org_id ?? null;
49
+ }
50
+ /**
51
+ * The roots the environment's active machine last reported in its hello frame, or null (no
52
+ * active machine, or none reported yet).
53
+ */
54
+ async machineRoots(environmentId) {
55
+ const row = await this.db
56
+ .selectFrom("environment_machines")
57
+ .select("roots")
58
+ .where("environment_id", "=", environmentId)
59
+ .where("revoked_at", "is", null)
60
+ .executeTakeFirst();
61
+ const r = row?.roots;
62
+ if (!r || typeof r !== "object")
63
+ return null;
64
+ if (!ROOT_KEYS.every((k) => typeof r[k] === "string"))
65
+ return null;
66
+ return r;
67
+ }
68
+ };
69
+ DeviceTargets = __decorate([
70
+ Injectable(),
71
+ __param(0, Inject(KernelDb))
72
+ ], DeviceTargets);
73
+ export { DeviceTargets };
@@ -13,12 +13,16 @@ export interface ProviderFacts {
13
13
  hasInstance: boolean;
14
14
  }
15
15
  /**
16
- * The provider an environment works with: the one keeping its files or instance once it has any
17
- * (they are never migrated, spec §4.2 item 13); before that, the current provider (its first use
18
- * creates them there), else the one recorded at creation. The same rule as `SandboxService`'s use.
16
+ * The provider an environment works with: a kernel-internal one (`@device`) as recorded; for a
17
+ * cloud environment the one keeping its files or instance once it has any (they are never
18
+ * migrated, spec §4.2 item 13); before that, the current cloud provider (its first use creates
19
+ * them there), else the one recorded at creation. The same rule as `SandboxService`'s use.
19
20
  */
20
21
  export declare function environmentProvider(f: ProviderFacts, current: string | undefined): string | null;
21
- /** The environment's provider is not the current one, or there is no provider (503). */
22
+ /**
23
+ * The environment's provider is not the current one, or there is no provider (503); for an
24
+ * internal provider (`@device`), this kernel does not serve it.
25
+ */
22
26
  export declare const environmentUnavailable: (provider: string | null) => ApiError;
23
27
  /**
24
28
  * Who may use an environment (spec §4.2 item 12): in phase 1 only the owner of a personal
@@ -28,11 +32,12 @@ export declare const environmentUnavailable: (provider: string | null) => ApiErr
28
32
  export declare class EnvironmentAccess {
29
33
  private readonly db;
30
34
  private readonly providers;
31
- constructor(db: KernelDb, providers: Pick<SandboxProviders, "current">);
35
+ constructor(db: KernelDb, providers: Pick<SandboxProviders, "cloud" | "forName">);
32
36
  /**
33
37
  * The environment if `accountId` may use it in `orgId` now: `environment.not_found` when it is
34
38
  * missing, deleted, in another org or someone else's; `environment.unavailable` when its
35
- * provider is not the current one (or there is none).
39
+ * provider is not the current one (or there is none). A `device` environment is usable while
40
+ * `@device` is registered: whether its machine is online is the relay's concern, not access.
36
41
  */
37
42
  usable(orgId: string, accountId: string, environmentId: string): Promise<{
38
43
  id: string;
@@ -10,23 +10,31 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
10
10
  import { Inject, Injectable } from "@nestjs/common";
11
11
  import { ApiError } from "../errors.js";
12
12
  import { KernelDb } from "../infra/database/kernel-db.js";
13
- import { SandboxProviders } from "../sandbox/providers.js";
13
+ import { isInternalProvider, SandboxProviders } from "../sandbox/providers.js";
14
14
  import { environmentNotFound } from "../sandbox/sandbox.service.js";
15
15
  /**
16
- * The provider an environment works with: the one keeping its files or instance once it has any
17
- * (they are never migrated, spec §4.2 item 13); before that, the current provider (its first use
18
- * creates them there), else the one recorded at creation. The same rule as `SandboxService`'s use.
16
+ * The provider an environment works with: a kernel-internal one (`@device`) as recorded; for a
17
+ * cloud environment the one keeping its files or instance once it has any (they are never
18
+ * migrated, spec §4.2 item 13); before that, the current cloud provider (its first use creates
19
+ * them there), else the one recorded at creation. The same rule as `SandboxService`'s use.
19
20
  */
20
21
  export function environmentProvider(f, current) {
22
+ if (f.provider !== null && isInternalProvider(f.provider))
23
+ return f.provider;
21
24
  const holdsResources = f.fsStatus !== null && (f.fsStatus !== "none" || f.hasInstance);
22
25
  if (holdsResources)
23
26
  return f.instanceProvider;
24
27
  return current ?? f.provider;
25
28
  }
26
- /** The environment's provider is not the current one, or there is no provider (503). */
29
+ /**
30
+ * The environment's provider is not the current one, or there is no provider (503); for an
31
+ * internal provider (`@device`), this kernel does not serve it.
32
+ */
27
33
  export const environmentUnavailable = (provider) => new ApiError("environment.unavailable", provider === null
28
34
  ? "No cloud sandbox is available in this deployment; ask the super admin to enable a sandbox plugin"
29
- : `This environment is kept by the sandbox provider ${provider}, which is not the current provider; it can be used again once ${provider} is the deployment's sandbox provider`);
35
+ : isInternalProvider(provider)
36
+ ? `This environment runs on a connected machine (${provider}), which this deployment does not serve`
37
+ : `This environment is kept by the sandbox provider ${provider}, which is not the current provider; it can be used again once ${provider} is the deployment's sandbox provider`);
30
38
  /**
31
39
  * Who may use an environment (spec §4.2 item 12): in phase 1 only the owner of a personal
32
40
  * environment. Anyone else gets `environment.not_found`, never `forbidden`, so ids cannot be
@@ -42,7 +50,8 @@ let EnvironmentAccess = class EnvironmentAccess {
42
50
  /**
43
51
  * The environment if `accountId` may use it in `orgId` now: `environment.not_found` when it is
44
52
  * missing, deleted, in another org or someone else's; `environment.unavailable` when its
45
- * provider is not the current one (or there is none).
53
+ * provider is not the current one (or there is none). A `device` environment is usable while
54
+ * `@device` is registered: whether its machine is online is the relay's concern, not access.
46
55
  */
47
56
  async usable(orgId, accountId, environmentId) {
48
57
  const row = await this.db
@@ -57,7 +66,13 @@ let EnvironmentAccess = class EnvironmentAccess {
57
66
  .executeTakeFirst();
58
67
  if (!row)
59
68
  throw environmentNotFound();
60
- const current = this.providers.current()?.plugin;
69
+ if (row.provider !== null && isInternalProvider(row.provider)) {
70
+ if (this.providers.forName(row.provider) === undefined) {
71
+ throw environmentUnavailable(row.provider);
72
+ }
73
+ return { id: row.id, provider: row.provider };
74
+ }
75
+ const current = this.providers.cloud()?.plugin;
61
76
  const provider = environmentProvider({
62
77
  provider: row.provider,
63
78
  instanceProvider: row.instance_provider,
@@ -29,12 +29,13 @@ export declare class ConversationEnvironments {
29
29
  private readonly providers;
30
30
  private readonly access;
31
31
  private readonly environments;
32
- constructor(db: KernelDb, txHost: TransactionHost, providers: Pick<SandboxProviders, "current">, access: Pick<EnvironmentAccess, "usable">, environments: Pick<EnvironmentsService, "ensureDefault">);
32
+ constructor(db: KernelDb, txHost: TransactionHost, providers: Pick<SandboxProviders, "cloud"> & Partial<Pick<SandboxProviders, "forName">>, access: Pick<EnvironmentAccess, "usable">, environments: Pick<EnvironmentsService, "ensureDefault">);
33
33
  /**
34
34
  * The environment a new conversation of `accountId` is bound to: the chosen one when it is
35
35
  * usable by the person (else `environment.not_found` / `environment.unavailable`, and nothing
36
36
  * is created); without a choice the IM default (IM only) or the person's default environment.
37
- * Null (unbound) without a sandbox provider and for anonymous IM senders.
37
+ * Null (unbound) for anonymous IM senders, and without a cloud sandbox provider when no usable
38
+ * IM default (a connected machine's environment, say) applies.
38
39
  */
39
40
  forNew(orgId: string, accountId: string, pick?: NewConversationEnvironment): Promise<string | null>;
40
41
  /**
@@ -42,18 +43,31 @@ export declare class ConversationEnvironments {
42
43
  * enters, checked for its owner now. A conversation still unbound (created before
43
44
  * environments, or while no provider existed) is bound first, to the owner's IM default when it
44
45
  * is an IM thread, else to the owner's default environment; a racing call binds it once.
45
- * Throws `sandbox.unavailable` without a provider or for an anonymous IM conversation, and
46
- * `environment.not_found` / `environment.unavailable` when the owner may not use it now.
46
+ * Throws `sandbox.unavailable` for a cloud environment (or none to bind) without a cloud
47
+ * provider and for an anonymous IM conversation, and `environment.not_found` /
48
+ * `environment.unavailable` when the owner may not use it now. A conversation bound to a
49
+ * connected machine's environment (`@device`) does not need a cloud provider.
47
50
  */
48
51
  forTool(orgId: string, rootConversationId: string): Promise<string>;
52
+ /**
53
+ * Whether a sandbox tool call of the tree rooted at `rootConversationId` has a provider to run
54
+ * on, for tool listings (ruling R8; no binding, no access check, never throws): a cloud
55
+ * provider is loaded, or the environment the conversation is bound to (an unbound IM thread:
56
+ * its owner's IM default) runs on a registered kernel provider (`@device`). A forbidden or gone
57
+ * environment still counts: the call then says why.
58
+ */
59
+ usableNow(orgId: string, rootConversationId: string): Promise<boolean>;
49
60
  /**
50
61
  * The person's IM default when `im` and it is still theirs, live, in this org and usable (the
51
- * table has no such guard), else their default environment (created when missing). The owner
52
- * having left the org reads as `environment.not_found`.
62
+ * table has no such guard), else their default environment (created when missing); null when
63
+ * that is needed and there is no cloud provider to make or use it. The owner having left the org
64
+ * reads as `environment.not_found`.
53
65
  */
54
66
  private defaultFor;
55
67
  /** Binds an unbound conversation to `environmentId`; returns what it is bound to now. */
56
68
  private bind;
69
+ /** The environment runs on a kernel-internal provider (`@device`). */
70
+ private isInternal;
57
71
  /** The account is the org's anonymous IM guest (no person behind it). */
58
72
  private isAnonymous;
59
73
  /** The conversation is (or was) an IM thread's. */