@capybara-hq/plugin-sdk 0.1.0-rc.9 → 0.1.0-rc.90

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 (159) hide show
  1. package/README.md +3 -1
  2. package/agent-docs/agent-templates.md +14 -2
  3. package/agent-docs/concepts.md +14 -5
  4. package/agent-docs/connector-templates.md +11 -3
  5. package/agent-docs/extension-points/channel.md +1 -1
  6. package/agent-docs/extension-points/context.md +15 -0
  7. package/agent-docs/extension-points/device-provider.md +15 -0
  8. package/agent-docs/extension-points/file-preview.md +3 -1
  9. package/agent-docs/extension-points/identity-source.md +2 -0
  10. package/agent-docs/extension-points/model-provider.md +5 -1
  11. package/agent-docs/extension-points/object-store.md +2 -0
  12. package/agent-docs/extension-points/push-provider.md +17 -0
  13. package/agent-docs/extension-points/trigger-source.md +19 -0
  14. package/agent-docs/extensions.md +98 -4
  15. package/agent-docs/plugin-format.md +45 -10
  16. package/agent-docs/setup.md +54 -0
  17. package/api/plugin-sdk.api.md +5983 -0
  18. package/dist/agent-template.d.ts +15 -6
  19. package/dist/agent-template.js +32 -13
  20. package/dist/agents-ctx.d.ts +116 -0
  21. package/dist/agents-ctx.js +22 -0
  22. package/dist/avatar-presets.d.ts +8 -0
  23. package/dist/avatar-presets.js +17 -0
  24. package/dist/bootstrap.d.ts +109 -1
  25. package/dist/bootstrap.js +185 -2
  26. package/dist/cards.d.ts +121 -13
  27. package/dist/cards.js +161 -19
  28. package/dist/channel.d.ts +133 -47
  29. package/dist/channel.js +10 -5
  30. package/dist/client-css.d.ts +36 -0
  31. package/dist/client-css.js +326 -0
  32. package/dist/client-section.d.ts +35 -0
  33. package/dist/client-section.js +133 -0
  34. package/dist/config-ui.d.ts +28 -6
  35. package/dist/config-ui.js +88 -25
  36. package/dist/config-url-fetch.d.ts +19 -0
  37. package/dist/config-url-fetch.js +43 -0
  38. package/dist/connector-auth.d.ts +13 -2
  39. package/dist/connector-auth.js +82 -8
  40. package/dist/connector.d.ts +74 -32
  41. package/dist/context.d.ts +40 -0
  42. package/dist/context.js +2 -0
  43. package/dist/ctx.d.ts +344 -31
  44. package/dist/ctx.js +9 -1
  45. package/dist/define-extension.d.ts +18 -12
  46. package/dist/define-extension.js +18 -5
  47. package/dist/device-denied-paths.d.ts +20 -0
  48. package/dist/device-denied-paths.js +118 -0
  49. package/dist/device-operation.d.ts +348 -0
  50. package/dist/device-operation.js +312 -0
  51. package/dist/device-protocol.d.ts +286 -49
  52. package/dist/device-protocol.js +766 -89
  53. package/dist/device-provider.d.ts +820 -0
  54. package/dist/device-provider.js +432 -0
  55. package/dist/device-service.d.ts +204 -0
  56. package/dist/device-service.js +291 -0
  57. package/dist/dial-in.d.ts +286 -0
  58. package/dist/dial-in.js +213 -0
  59. package/dist/directory.d.ts +15 -12
  60. package/dist/extension-point.d.ts +2 -0
  61. package/dist/extension-point.js +8 -3
  62. package/dist/field-meta.d.ts +19 -0
  63. package/dist/field-meta.js +54 -0
  64. package/dist/file-preview.d.ts +46 -9
  65. package/dist/git-hosts.d.ts +53 -0
  66. package/dist/git-hosts.js +98 -0
  67. package/dist/hooks.d.ts +185 -22
  68. package/dist/hooks.js +6 -0
  69. package/dist/http-access.d.ts +71 -0
  70. package/dist/http-access.js +12 -0
  71. package/dist/identity.d.ts +70 -9
  72. package/dist/identity.js +46 -1
  73. package/dist/inbox.d.ts +103 -0
  74. package/dist/inbox.js +173 -0
  75. package/dist/index.d.ts +30 -4
  76. package/dist/index.js +30 -4
  77. package/dist/model-params.d.ts +41 -22
  78. package/dist/model-params.js +28 -15
  79. package/dist/model.d.ts +295 -77
  80. package/dist/model.js +249 -21
  81. package/dist/native-failure.d.ts +13 -0
  82. package/dist/native-failure.js +31 -0
  83. package/dist/net.d.ts +52 -0
  84. package/dist/net.js +34 -0
  85. package/dist/network-classes.d.ts +98 -0
  86. package/dist/network-classes.js +291 -0
  87. package/dist/permissions.d.ts +95 -25
  88. package/dist/permissions.js +175 -54
  89. package/dist/platform-only.d.ts +27 -0
  90. package/dist/platform-only.js +35 -0
  91. package/dist/plugin-manifest.d.ts +30 -6
  92. package/dist/plugin-manifest.js +147 -55
  93. package/dist/plugin-package.d.ts +82 -39
  94. package/dist/plugin-package.js +68 -41
  95. package/dist/push.d.ts +75 -0
  96. package/dist/push.js +87 -0
  97. package/dist/sandbox-linux-runtime-source.d.ts +1 -0
  98. package/dist/sandbox-linux-runtime-source.js +2343 -0
  99. package/dist/sandbox-native-runtime-source.d.ts +10 -0
  100. package/dist/sandbox-native-runtime-source.js +480 -0
  101. package/dist/sandbox-runtime-source.d.ts +47 -0
  102. package/dist/sandbox-runtime-source.js +73 -0
  103. package/dist/sandbox-write-denies.d.ts +139 -0
  104. package/dist/sandbox-write-denies.js +869 -0
  105. package/dist/secret.d.ts +8 -2
  106. package/dist/secret.js +11 -4
  107. package/dist/setup-check.d.ts +128 -0
  108. package/dist/setup-check.js +142 -0
  109. package/dist/setup-guide-parse.d.ts +121 -0
  110. package/dist/setup-guide-parse.js +356 -0
  111. package/dist/setup-guide-render.d.ts +45 -0
  112. package/dist/setup-guide-render.js +351 -0
  113. package/dist/setup-guide.d.ts +86 -72
  114. package/dist/setup-guide.js +340 -108
  115. package/dist/shell-capabilities.d.ts +216 -0
  116. package/dist/shell-capabilities.js +44 -0
  117. package/dist/shell-environment.d.ts +57 -0
  118. package/dist/shell-environment.js +21 -0
  119. package/dist/skill-invocation.d.ts +5 -0
  120. package/dist/skill-invocation.js +4 -3
  121. package/dist/skill.d.ts +27 -17
  122. package/dist/skill.js +40 -26
  123. package/dist/storage.d.ts +52 -25
  124. package/dist/svg-viewbox.d.ts +10 -0
  125. package/dist/svg-viewbox.js +24 -0
  126. package/dist/theme-assets.d.ts +25 -9
  127. package/dist/theme-assets.js +87 -47
  128. package/dist/theme-tokens.generated.d.ts +40 -12
  129. package/dist/theme-tokens.generated.js +36 -8
  130. package/dist/theme.d.ts +56 -10
  131. package/dist/theme.js +117 -23
  132. package/dist/tool.d.ts +242 -28
  133. package/dist/tool.js +135 -13
  134. package/dist/trigger-source.d.ts +35 -0
  135. package/dist/trigger-source.js +2 -0
  136. package/dist/triggers.d.ts +88 -0
  137. package/dist/triggers.js +71 -0
  138. package/dist/ui-parts.d.ts +23 -0
  139. package/dist/ui-parts.js +374 -0
  140. package/dist/ui-resolve.d.ts +2 -7
  141. package/dist/ui-resolve.js +7 -19
  142. package/dist/ui-slots.d.ts +27 -5
  143. package/dist/ui-slots.js +112 -19
  144. package/dist/ui.d.ts +203 -64
  145. package/dist/ui.js +117 -23
  146. package/dist/url.d.ts +4 -1
  147. package/dist/url.js +4 -1
  148. package/dist/web.d.ts +26 -20
  149. package/package.json +13 -1
  150. package/schemas/bootstrap-extra.v1.schema.json +186 -0
  151. package/schemas/bootstrap.v1.schema.json +20 -0
  152. package/schemas/connector-template.v1.schema.json +62 -4
  153. package/schemas/plugin.v1.schema.json +74 -143
  154. package/schemas/theme.v1.schema.json +77 -0
  155. package/agent-docs/extension-points/sandbox.md +0 -15
  156. package/dist/org-access.d.ts +0 -27
  157. package/dist/org-access.js +0 -35
  158. package/dist/sandbox.d.ts +0 -254
  159. package/dist/sandbox.js +0 -153
@@ -0,0 +1,820 @@
1
+ /**
2
+ * Device provider contract `device-provider@1`: a multi, brokered, org-scoped extension point
3
+ * (extensions may not `requires` it; the kernel's `SandboxService` consumes it). The provider implements only the
4
+ * primitives below; the kernel builds `glob` / `grep` / `edit` from `exec` plus `readFile` /
5
+ * `writeFile`.
6
+ *
7
+ * A device separates its **file system** from its **instance**: the file system
8
+ * (`createFileSystem`) lives until the device is deleted (`destroyFileSystem`); instances
9
+ * (`create`) attach to it and come and go (`destroy` drops the instance and keeps the files).
10
+ */
11
+ import type { SandboxBackground, SandboxBrowse, SandboxBrowseScope, SandboxOperation, SandboxOperationRef, SandboxOperationScope, SandboxPermissionSupport, SandboxPreparation, SandboxPreparationScope } from "./device-operation.js";
12
+ import type { DialInHandler } from "./dial-in.js";
13
+ import type { AddressClass } from "./network-classes.js";
14
+ /** Device provider extension point ref. */
15
+ export declare const DEVICE_PROVIDER_EXTENSION_POINT = "device-provider@1";
16
+ /** The roots inside the sandbox the model may access. */
17
+ export declare const SANDBOX_ROOTS: readonly ["/workspace", "/skills", "/tmp"];
18
+ /**
19
+ * Sandbox resource and network limits: what lifecycle create and update carry (resources and the
20
+ * outer network only; each operation's scope says how it runs). A provider that ignores `network`
21
+ * still runs: it then behaves as an allowlisted sandbox (the safe direction).
22
+ */
23
+ export interface SandboxLimits {
24
+ cpu: number;
25
+ memoryMb: number;
26
+ diskMb: number;
27
+ /**
28
+ * The network the instance (cloud) must enforce, or the allowlist a local device's sandbox
29
+ * intersects with its own list (`open-public` is for cloud instances only). Each entry of
30
+ * `hosts` passes {@link validateAllowHost}.
31
+ */
32
+ network: {
33
+ kind: "open-public" | "allowlist";
34
+ hosts: string[];
35
+ };
36
+ }
37
+ /** The absolute roots inside one instance (a device may place them anywhere on its host). */
38
+ export interface SandboxRoots {
39
+ workspace: string;
40
+ skills: string;
41
+ tmp: string;
42
+ }
43
+ /** The roots every container-like provider uses; the same paths as {@link SANDBOX_ROOTS}. */
44
+ export declare const DEFAULT_SANDBOX_ROOTS: Readonly<SandboxRoots>;
45
+ /** The provider's instance handle. */
46
+ export interface SandboxHandle {
47
+ /** The provider's instance id. */
48
+ id: string;
49
+ /** The instance's roots; absent means {@link DEFAULT_SANDBOX_ROOTS}. */
50
+ roots?: SandboxRoots;
51
+ }
52
+ /** The roots of an instance, falling back to {@link DEFAULT_SANDBOX_ROOTS}. */
53
+ export declare function rootsOf(h: SandboxHandle): SandboxRoots;
54
+ /** The provider's file system handle (a device's files, independent of any instance). */
55
+ export interface FileSystemHandle {
56
+ /** The provider's file system id. */
57
+ id: string;
58
+ }
59
+ /** Who runs an `exec` (informational in phase 1: audit and attribution). */
60
+ export interface SandboxActor {
61
+ accountId: string;
62
+ conversationId: string;
63
+ /**
64
+ * The run the command serves. The kernel uses it to route run grants (network hosts allowed
65
+ * for the rest of a run) to a connected device; informational for other providers.
66
+ */
67
+ runId?: string;
68
+ }
69
+ /**
70
+ * The layer that refused a network host: `device` (the org allows it, the device's local policy
71
+ * does not), `org` (not in the org's effective egress), `address` (an IP literal or a name that
72
+ * points to a private or local address; never grantable), `unknown`.
73
+ */
74
+ export type NetworkBlockLayer = "device" | "org" | "address" | "unknown";
75
+ /**
76
+ * A host a device service saw refused, with the layer that refused it
77
+ * (`ctx.deviceAccess.reportBlockedHosts`), the refused port and, for a closed address, its class.
78
+ * A `localhost` or loopback literal refused at the `address` layer is what a loopback port ask
79
+ * starts from.
80
+ */
81
+ export interface ServiceBlockedHost {
82
+ host: string;
83
+ layer: NetworkBlockLayer;
84
+ /** The refused port; absent: the HTTPS port (443). */
85
+ port?: number;
86
+ /**
87
+ * The closed address class when `layer` is `address` and known (`classifyAddress`, or
88
+ * `loopback` for `localhost`).
89
+ */
90
+ class?: AddressClass;
91
+ }
92
+ /**
93
+ * The only hosts a loopback port grant reaches (`ctx.deviceAccess.grantLoopbackPort`): the IPv4
94
+ * and IPv6 loopback literals and the name `localhost`, which the device dials without DNS. Every
95
+ * other loopback literal (`127.0.0.2`) and every private, link-local or metadata address stays
96
+ * closed.
97
+ */
98
+ export declare const LOOPBACK_GRANT_HOSTS: readonly ["127.0.0.1", "::1", "localhost"];
99
+ /**
100
+ * Whether `host` is one of {@link LOOPBACK_GRANT_HOSTS}: `::1` may also be written `[::1]`, and
101
+ * `localhost` matches case-insensitively. A trailing dot, a subdomain of `localhost` or an
102
+ * IPv4-mapped address is not one.
103
+ */
104
+ export declare function isLoopbackGrantHost(host: string): boolean;
105
+ /**
106
+ * Well-known database ports (PostgreSQL, MySQL, Redis, MongoDB): a grant to one is allowed but
107
+ * flagged (`LoopbackGrantView.database`) and named in the approval and review texts.
108
+ */
109
+ export declare const LOOPBACK_DATABASE_PORTS: readonly number[];
110
+ /** The most live loopback port grants one device service holds at once. */
111
+ export declare const LOOPBACK_MAX_PORTS_PER_SERVICE = 32;
112
+ /**
113
+ * The layer of a refusal by a device's sandbox proxy, read from the reason the proxy gives: the
114
+ * status phrase of its 403 answer to a CONNECT, or the reason in its violation line
115
+ * (`deny network-outbound <host>:<port> (<reason>)`).
116
+ *
117
+ * - `address`: the destination's address is closed (an IP literal in a closed class, or a name
118
+ * resolving to a private or local address: "address is …", "resolved to …"), or the host is
119
+ * malformed. No host grant reaches it, only a command run outside the sandbox.
120
+ * - `device`: the host is not on the operation's allowlist ("host is not on the allow list").
121
+ * - `unknown`: any other reason (a proxy that names none, a failed fake-IP lookup).
122
+ */
123
+ export declare function sandboxProxyRefusalLayer(reason: string): NetworkBlockLayer;
124
+ /** A host a command was refused, as the provider reports it. */
125
+ export interface NetworkBlockedHost {
126
+ /** The host as the proxy saw it (IPv6 literals bracketed). */
127
+ host: string;
128
+ port: number;
129
+ layer: NetworkBlockLayer;
130
+ }
131
+ /** Result of a command. */
132
+ export interface ExecResult {
133
+ /** null on timeout or when aborted while running (`timedOut` is then true / false). */
134
+ exitCode: number | null;
135
+ stdout: string;
136
+ stderr: string;
137
+ truncated: boolean;
138
+ timedOut: boolean;
139
+ /**
140
+ * Network refusals during the command (optional for every provider): at most
141
+ * `MAX_NETWORK_BLOCKED_HOSTS` hosts, deduped by host. The kernel recomputes the `org` layer
142
+ * itself and never trusts a provider's word that the org allows a host.
143
+ */
144
+ network?: {
145
+ blocked: NetworkBlockedHost[];
146
+ };
147
+ }
148
+ /**
149
+ * The instance is definitely gone on the provider's side (container removed, E2B sandbox expired).
150
+ * `exec` / `readFile` / `writeFile` / `stat` must throw it (not a plain Error) when they find the
151
+ * instance gone; the kernel then rebuilds the instance and retries once. A transient connection
152
+ * failure throws a plain error. In a managed operation (`operation()`), a gone instance found
153
+ * **before** the request reached the machine throws a `DeviceRequestNotSentError` with this error
154
+ * as its `cause`: the operation is then recorded as not started rather than as an unknown outcome,
155
+ * and the instance is still rebuilt, so the call can be retried on it.
156
+ */
157
+ export declare class SandboxGoneError extends Error {
158
+ readonly sandboxGone: true;
159
+ constructor(message: string, opts?: {
160
+ cause?: unknown;
161
+ });
162
+ }
163
+ /** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
164
+ export declare function isSandboxGoneError(e: unknown): e is SandboxGoneError;
165
+ /**
166
+ * A lifecycle call's fence (device update fencing design §3): the kernel passes it to `update`,
167
+ * `create`, `destroy` and `destroyByName` made under the device's exclusive lease, so a provider
168
+ * can refuse a call from a holder whose lease ended while the call was in flight.
169
+ */
170
+ export interface DeviceFence {
171
+ /**
172
+ * The device's fencing generation as the calling holder last wrote it: a non-negative safe
173
+ * integer, strictly higher for every later exclusive holder of the device, never lower for later
174
+ * calls of the same holder.
175
+ */
176
+ readonly token: number;
177
+ }
178
+ /**
179
+ * Thrown by a lifecycle call whose {@link DeviceFence} token is lower than one the instance
180
+ * already applied: the caller is a stale holder, and nothing was changed. The kernel handles it as
181
+ * a lost lease (never as a reason to recreate). `current` is the token the instance holds, when the
182
+ * provider knows it.
183
+ */
184
+ export declare class StaleFenceError extends Error {
185
+ readonly staleFence: true;
186
+ readonly current: number | undefined;
187
+ constructor(message: string, opts?: {
188
+ cause?: unknown;
189
+ current?: number;
190
+ });
191
+ }
192
+ /** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
193
+ export declare function isStaleFenceError(e: unknown): e is StaleFenceError;
194
+ /**
195
+ * The file system is definitely gone on the provider's side (volume removed, E2B sandbox killed).
196
+ * `create` throws it instead of attaching the instance to a new, empty file system; the kernel then
197
+ * reports the device's files as lost rather than handing out a silently empty workspace.
198
+ */
199
+ export declare class FileSystemGoneError extends Error {
200
+ readonly fileSystemGone: true;
201
+ constructor(message: string, opts?: {
202
+ cause?: unknown;
203
+ });
204
+ }
205
+ /** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
206
+ export declare function isFileSystemGoneError(e: unknown): e is FileSystemGoneError;
207
+ /**
208
+ * A holder tag (`exec` argument `tag`, {@link DeviceProvider.killTagged}): the kernel passes its
209
+ * lease tokens (UUIDs). Providers check tags against it before putting them in a command line or a
210
+ * kill pattern.
211
+ */
212
+ export declare const SANDBOX_TAG_RE: RegExp;
213
+ /** Text in English with an optional Chinese translation (what a person reads). */
214
+ export interface DeviceText {
215
+ en: string;
216
+ zh?: string;
217
+ }
218
+ /** The device a provider answers about: the kernel's device id, its org and the instance, if any. */
219
+ export interface DeviceRef {
220
+ deviceId: string;
221
+ orgId: string;
222
+ /** The current instance, or null when the device has none. */
223
+ handle: SandboxHandle | null;
224
+ }
225
+ /**
226
+ * One limit line of a {@link DeviceReport} (shown as is; the provider enforces it). `value` is
227
+ * text too: words in it get a Chinese form; pure data (a path, a size) may carry `en` only.
228
+ */
229
+ export interface DeviceReportLimit {
230
+ label: DeviceText;
231
+ value: DeviceText;
232
+ }
233
+ /**
234
+ * What a provider tells about a device ({@link DeviceProvider.report}): shown to people, never
235
+ * used for enforcement. `revision` must change whenever anything that affects what was approved
236
+ * against the report changes (how operations are confined, what the device may reach): a new
237
+ * revision invalidates the approvals, grants and pending admissions bound to the old one.
238
+ * Display-only facts (for example the shared project roots shown as a limit line) may change
239
+ * without a revision change: announce such a change with `ctx.devices.reportChanged`, and the
240
+ * kernel stores the new text without invalidating anything. `home` is the owner's home directory
241
+ * (absolute) or null; `reset` says whether the provider can reset the device's files.
242
+ *
243
+ * `changeHint` (optional) says in the provider's own words who can change the device's limits and
244
+ * how (for a local connection: the machine's owner, with a command on that machine; for a
245
+ * container: the super admin's deployment config of the extension). The device page shows it
246
+ * under the limits, and the kernel appends it to a refusal that cites the report (an escalated
247
+ * call the provider does not allow now), so a person and the model learn the way out. State the
248
+ * mechanism, not advice for one situation.
249
+ */
250
+ export interface DeviceReport {
251
+ revision: string;
252
+ description: DeviceText;
253
+ home: string | null;
254
+ limits: DeviceReportLimit[];
255
+ reset: boolean;
256
+ changeHint?: DeviceText;
257
+ }
258
+ /** Size limits {@link parseDeviceReport} enforces. */
259
+ export declare const DEVICE_REPORT_LIMITS: Readonly<{
260
+ revisionChars: 128;
261
+ descriptionChars: 300;
262
+ limits: 8;
263
+ limitLabelChars: 60;
264
+ limitValueChars: 60;
265
+ homeChars: 4096;
266
+ changeHintChars: 400;
267
+ }>;
268
+ /** Checks a provider's report; throws `TypeError` on any other shape, returns a clean copy. */
269
+ export declare function parseDeviceReport(value: unknown): DeviceReport;
270
+ /**
271
+ * The provider refuses a project directory (`projects` operations: it lies where the provider
272
+ * will not serve, for example another connection's directory). `reason` is shown to the person.
273
+ * The kernel answers it as `device.project_refused` (422).
274
+ */
275
+ export declare class ProjectPathRefusedError extends Error {
276
+ readonly projectPathRefused: true;
277
+ readonly reason: DeviceText;
278
+ constructor(reason: DeviceText, opts?: {
279
+ cause?: unknown;
280
+ });
281
+ }
282
+ /** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
283
+ export declare function isProjectPathRefusedError(e: unknown): e is ProjectPathRefusedError;
284
+ /**
285
+ * A device service name as an extension declares it (`permissions.deviceAccess.services`) and
286
+ * names it in `ctx.deviceAccess`: 1 to 32 characters, a lower-case letter then letters, digits
287
+ * and `-`.
288
+ */
289
+ export declare const DEVICE_SERVICE_NAME_RE: RegExp;
290
+ /**
291
+ * A device service id: what a device provider receives as the service's name. The kernel
292
+ * namespaces every extension's service names by the extension (`<extension>.<name>`,
293
+ * {@link deviceServiceId}), so two extensions declaring the same name never start, stop or reach
294
+ * each other's process; the provider keys everything of a service (its state and code
295
+ * directories, its socket, its process) by the id. At most 74 characters, one path segment.
296
+ */
297
+ export declare const DEVICE_SERVICE_ID_RE: RegExp;
298
+ /**
299
+ * The device service id of an extension's service (`<extension>.<name>`,
300
+ * {@link DEVICE_SERVICE_ID_RE}). Throws a `TypeError` for an extension name that is not one or a
301
+ * service name breaking {@link DEVICE_SERVICE_NAME_RE}.
302
+ */
303
+ export declare function deviceServiceId(extension: string, name: string): string;
304
+ /**
305
+ * A long-lived service process in a device ({@link DeviceProvider.ensureService}), started on an
306
+ * extension's behalf. It runs under the device's own sandbox (not under any one conversation's
307
+ * operation scope), writes only in its own state directory (outside every project root; `files`
308
+ * are written before start into its code directory, read only for the service where the provider
309
+ * can separate code from state, else the state directory), reaches the network through the
310
+ * device's network setting, and is killed with the instance on suspend and destroy. It speaks the service socket protocol
311
+ * (`device-service.ts`).
312
+ */
313
+ export interface DeviceServiceSpec {
314
+ /**
315
+ * One service per name per device. In `ctx.deviceAccess.ensureService` the extension's service
316
+ * name ({@link DEVICE_SERVICE_NAME_RE}); a provider receives the kernel's service id in its place
317
+ * (`<extension>.<name>`, {@link DEVICE_SERVICE_ID_RE}).
318
+ */
319
+ name: string;
320
+ /** The provider supplies Node 22 or later. */
321
+ runtime: "node";
322
+ /**
323
+ * The script to run: one of the `files` keys. The process runs
324
+ * `node <codeDir ?? stateDir>/<entry>` (the code directory where the provider keeps code apart,
325
+ * else the state directory) with the state directory as its working directory; that exact command passes the command review.
326
+ */
327
+ entry: string;
328
+ args: string[];
329
+ /**
330
+ * Written into the service's code directory before start (read only for the service where the
331
+ * provider separates code from state, else the state directory), keyed by relative path: no
332
+ * absolute path, no `..` or `.` segment, no empty segment, no backslash, no NUL (the kernel
333
+ * refuses a spec that breaks this before any provider call).
334
+ */
335
+ files: Record<string, Uint8Array>;
336
+ /**
337
+ * Extra environment of the process (shown to the command review as `env K=V …`). The kernel
338
+ * refuses `NODE_*`, `PATH`, `SSL_CERT_FILE`, `SSL_CERT_DIR`, `OPENSSL_CONF`, `OPENSSL_ENGINES`,
339
+ * `LD_*`, `DYLD_*`, `CAPYBARA_*` and the proxy variables (`HTTP_PROXY`, `HTTPS_PROXY`,
340
+ * `NO_PROXY`, `ALL_PROXY`, `FTP_PROXY`), any case: they change what runs or what it trusts, or
341
+ * the provider sets them itself.
342
+ */
343
+ env?: Record<string, string>;
344
+ }
345
+ /** A device service's state ({@link DeviceProvider.ensureService}). */
346
+ export interface DeviceServiceStatus {
347
+ /**
348
+ * The name the status was asked for: the service id from a provider, the extension's service
349
+ * name from `ctx.deviceAccess`.
350
+ */
351
+ name: string;
352
+ state: "running" | "stopped";
353
+ /** ISO 8601 time the running process started; null when stopped. */
354
+ startedAt: string | null;
355
+ /**
356
+ * The service's state directory in the device (absolute), also when it is stopped: its working
357
+ * directory and write root (its `files` too, unless the provider keeps code apart). The kernel
358
+ * reviews the launch against it.
359
+ */
360
+ stateDir: string;
361
+ /**
362
+ * The directory the entry runs from (absolute), when the provider keeps the service's code
363
+ * apart from its state (read only for the service); absent: the state directory. Known before
364
+ * the first start like `stateDir`. The kernel's launch review shows
365
+ * `node <codeDir ?? stateDir>/<entry>`, so a provider runs exactly that.
366
+ */
367
+ codeDir?: string;
368
+ /**
369
+ * The {@link deviceServiceFilesDigest} of the files the running process was started from, when
370
+ * the provider knows it (absent when stopped). The kernel compares it with the spec's files: a
371
+ * running service on other files (an upgraded extension) gets a reviewed start, which restarts
372
+ * it on the new files.
373
+ */
374
+ filesDigest?: string;
375
+ }
376
+ /**
377
+ * The digest of a service's files: `sha256:` and the hex SHA-256 over each file in path order
378
+ * (path, NUL, byte length, NUL, bytes). Providers report it in {@link DeviceServiceStatus} and
379
+ * may send it to a machine instead of the files.
380
+ */
381
+ export declare function deviceServiceFilesDigest(files: Record<string, Uint8Array>): string;
382
+ /**
383
+ * A fixed input and its {@link deviceServiceFilesDigest}, for the tests of every implementation
384
+ * of the digest (the SDK's, capybara-device's, a provider's own): one that disagrees would restart
385
+ * every running service at every ensure.
386
+ */
387
+ export declare const DEVICE_SERVICE_FILES_DIGEST_VECTOR: {
388
+ readonly files: Readonly<Record<string, string>>;
389
+ readonly digest: string;
390
+ };
391
+ /**
392
+ * A message stream to a device service ({@link DeviceProvider.openStream},
393
+ * `DialInChannel.openStream`): whole messages both ways, flow-controlled by credit.
394
+ */
395
+ export interface DeviceStream {
396
+ /** Sends one message; resolves when the peer has credit for it (backpressure). */
397
+ send(message: Uint8Array): Promise<void>;
398
+ /** The peer's messages, in order; ends when the stream closes. */
399
+ readonly messages: AsyncIterable<Uint8Array>;
400
+ /** Closes the stream (idempotent); `reason` reaches the peer. */
401
+ close(reason?: string): void;
402
+ /** Resolves once the stream is closed, by either side, with the reason. */
403
+ readonly closed: Promise<{
404
+ reason: string;
405
+ }>;
406
+ }
407
+ /**
408
+ * The `device-provider@1` implementation (provided by a sandbox plugin such as Docker or E2B).
409
+ *
410
+ * **File systems and instances.** `createFileSystem` makes a device's files; they live until
411
+ * `destroyFileSystem` (the device is deleted). `create` makes an instance attached to an
412
+ * existing file system; `destroy` / `destroyByName` drop the instance only and keep the files, and
413
+ * `suspend` keeps them too. When the file system is gone, `create` must throw
414
+ * {@link FileSystemGoneError} and never create an empty one in its place (a Docker provider checks
415
+ * its volumes exist before mounting them, because Docker would otherwise create empty volumes).
416
+ *
417
+ * **Gone instances.** When the instance is definitely gone: `resume` returns `gone`; `exec` /
418
+ * `readFile` / `writeFile` / `stat` / `killAll` / `killTagged` throw {@link SandboxGoneError};
419
+ * `suspend` / `destroy` succeed as usual.
420
+ *
421
+ * **Auto-start**: when the instance still exists but is stopped or paused (for a reason outside the
422
+ * kernel: host or daemon restart, a process in the sandbox stopping its init, an idle sweep that
423
+ * stopped the instance before recording `suspended`...), `exec` / `readFile` / `writeFile` /
424
+ * `stat` / `killAll` / `killTagged` must start it transparently first (like `resume`, files kept)
425
+ * and then run, never fail: the kernel trusts only the `running` it recorded itself. Auto-start
426
+ * must be idempotent under concurrent calls: two calls that both find the instance stopped both
427
+ * start it without failing.
428
+ *
429
+ * **Time limits apply inside the sandbox too**: besides stopping a command after `timeoutMs` on the
430
+ * provider's side, `exec` must make the command in the sandbox kill itself shortly (a few seconds)
431
+ * after `timeoutMs`. When a kernel replica crashes, the provider-side timer goes with it, and a
432
+ * dropped connection does not end the command; the in-sandbox limit keeps it from running
433
+ * indefinitely next to the next holder's commands.
434
+ *
435
+ * **Abort**: every method must respond to `signal` promptly (fail at once when already aborted, end
436
+ * with the abort reason as soon as possible when aborted mid-way): the kernel relies on it to stop
437
+ * on lock loss, run abort or timeout, and ignoring it may let two holders operate on one instance.
438
+ * An aborted `exec` must kill its running command (and the processes it spawned), not just stop
439
+ * waiting for its output; an `exec` aborted after its command started resolves normally with
440
+ * `exitCode: null`, `timedOut: false` (and the output collected so far), it does not reject. An
441
+ * `exec` called with an already aborted signal rejects at once.
442
+ *
443
+ * **Fencing** (device update fencing design §4): `update`, `create`, `destroy` and
444
+ * `destroyByName` get a {@link DeviceFence} from the kernel. A provider that keeps per-instance
445
+ * state it can update conditionally must store the highest token it applied next to that state,
446
+ * compare and store in one atomic step, and throw {@link StaleFenceError} for a lower token (an
447
+ * equal token is the same holder and is accepted); `create` seeds the new instance's token and
448
+ * should refuse when the instance it would replace carries a higher one; `destroy` /
449
+ * `destroyByName` should refuse an instance carrying a higher token. A provider that cannot (no
450
+ * conditional write in its backend) ignores the token and states the residual race in its README;
451
+ * it should implement {@link DeviceProvider.appliedNetwork}, so the kernel narrows a late stale
452
+ * `update` again at the next lifecycle step.
453
+ * The fence is `undefined` only from an older kernel that sends none: freshness is then unproven,
454
+ * so a provider answers `recreate` to an in-place change it would otherwise fence, seeds 0 at
455
+ * `create` and destroys without a check. `null` (destroy only) means the kernel has no fence for
456
+ * the call (no device record, or the one-off legacy cleanup): destroy without a check.
457
+ *
458
+ * **Concurrent use** (required in `@2`): the sessions of one device share its instance, so the
459
+ * kernel calls `exec`, `readFile`, `writeFile` and `stat` concurrently on one instance. Those four
460
+ * methods must be safe to call concurrently. Each `exec` labels its command with its `tag`, and an
461
+ * aborted `exec` kills only its own command.
462
+ */
463
+ export interface DeviceProvider {
464
+ /** Runtime permission support for this instance; does not grant consent or provision an instance. */
465
+ permissionSupport?(h: SandboxHandle, signal: AbortSignal): Promise<SandboxPermissionSupport>;
466
+ /**
467
+ * Fresh immutable grant for one actual method invocation. Lightweight until a method is called;
468
+ * never cache grants on the handle or fall back to raw methods when unsupported.
469
+ */
470
+ operation?(h: SandboxHandle, scope: SandboxOperationScope, signal: AbortSignal): Promise<SandboxOperation>;
471
+ /** Trusted skill/attachment staging only; never exposed to user operations. */
472
+ prepare?(h: SandboxHandle, scope: SandboxPreparationScope, signal: AbortSignal): Promise<SandboxPreparation>;
473
+ /** Owner-authenticated metadata browsing without conversation/profile consent. */
474
+ browse?(h: SandboxHandle, scope: SandboxBrowseScope, signal: AbortSignal): Promise<SandboxBrowse>;
475
+ /**
476
+ * Required for permission support: idempotently persist an operation-ID tombstone before
477
+ * cancelling exact owned work. A delayed launch checks it before effects; retain it through
478
+ * admitBeforeMs and any owned execution's lifetime, including across kernel restarts.
479
+ */
480
+ revokeOperation?(h: SandboxHandle, ref: SandboxOperationRef, signal: AbortSignal): Promise<void>;
481
+ /** Exact registered background ownership; missing support must not fall back to shell launch. */
482
+ background?: SandboxBackground;
483
+ /** `exec` / `readFile` / `writeFile` / `stat` may run concurrently on one instance (required). */
484
+ readonly concurrentExec: true;
485
+ /**
486
+ * Declares that the provider enforces `limits.network` allowlists (required, always `true`):
487
+ * a provider that cannot restrict outbound hosts does not implement `device-provider@1`.
488
+ */
489
+ readonly networkAllowlist: true;
490
+ /**
491
+ * What the provider tells about the device: a description, the owner's home, limits and whether
492
+ * files can be reset ({@link DeviceReport}). Shown to people only; the answer passes
493
+ * {@link parseDeviceReport}. Must respond to `signal`.
494
+ */
495
+ report(device: DeviceRef, signal: AbortSignal): Promise<DeviceReport>;
496
+ /**
497
+ * The dial-in side of the provider, for an extension declaring `permissions.dialIn`: the kernel
498
+ * hands it the hellos and unsolicited frames of the machines serving its devices.
499
+ */
500
+ readonly dialIn?: DialInHandler;
501
+ /**
502
+ * Whether the provider isolates the sessions of one device from each other inside the
503
+ * instance (a declared capability; absent means false).
504
+ */
505
+ readonly sessionIsolation?: boolean;
506
+ /**
507
+ * Where `create`-time limits are fixed: `instance` (absent; Docker: a new instance applies new
508
+ * limits) or `fileSystem` (E2B: the instance is the file system, so only a file reset applies
509
+ * them without {@link DeviceProvider.update}).
510
+ */
511
+ readonly limitsApplyAt?: "instance" | "fileSystem";
512
+ /**
513
+ * Applies new limits to a running instance without recreating it (optional). `recreate` asks
514
+ * the kernel to destroy the instance and create one on the same files instead.
515
+ *
516
+ * `updated` means the instance now enforces `limits.network`, and no later rebuild of the
517
+ * instance (resume, auto-start, a lost egress component) can enforce a wider network than this
518
+ * one. For a narrowing (a host removed, or a kind that allows less), answer `updated` only once
519
+ * no connection to a removed host survives and no new one can open; otherwise answer `recreate`
520
+ * or throw. A thrown `update` is the kernel's to handle (recreate, or quarantine and retry on the
521
+ * next use), so a repeated call with the same limits must be safe.
522
+ *
523
+ * `fence`: a provider that can must refuse a token lower than the one the instance applied last
524
+ * with {@link StaleFenceError}, checked and stored in the same atomic step as the new limits (see
525
+ * "Fencing" above); an equal or higher token with limits already in force raises the stored
526
+ * token and answers `updated`. Any answer to a call whose token is not lower (`updated` or
527
+ * `recreate`) leaves the stored token at least that high, so an older call arriving later is
528
+ * still refused.
529
+ */
530
+ update?(h: SandboxHandle, limits: SandboxLimits, signal: AbortSignal, fence: DeviceFence): Promise<"updated" | "recreate">;
531
+ /**
532
+ * The network the instance `h` enforces now, as the provider's backend reports it, or null when
533
+ * the provider cannot tell (optional; read only with {@link DeviceProvider.update}). A read
534
+ * only: it must not resume, start or change the instance, and must respond to `signal`.
535
+ *
536
+ * For a provider whose `update` cannot honor the fence (no conditional write in its backend), a
537
+ * stale holder's late `update` can leave the instance wider than the kernel's record. At a
538
+ * lifecycle step under the exclusive lease (a resumed instance, a running one the use holds
539
+ * exclusively, an instance created on kept files whose limits are fixed at the file system)
540
+ * the kernel compares this answer with the limits it recorded and, when it allows more (the
541
+ * open public network over an allowlist, or a host the record does not cover), is null or
542
+ * fails, applies the desired limits again through `update` with its own fence (a provider bound
543
+ * to its file system that cannot apply them is stopped until it can). Equal or narrower changes
544
+ * nothing. Since null forces that `update` at every such step, answer null only for a state the
545
+ * provider really cannot read, never as a routine answer. Shared uses of a running instance read
546
+ * nothing, so a late call that lands while the instance keeps running is narrowed at its next
547
+ * resume or rebuild (at the latest after the kernel's maximum running time).
548
+ */
549
+ appliedNetwork?(h: SandboxHandle, signal: AbortSignal): Promise<SandboxLimits["network"] | null>;
550
+ /**
551
+ * Creates a device's file system. `name` looks like `cap-device-<deviceId>`. If a file
552
+ * system with this name already exists, return it.
553
+ */
554
+ createFileSystem(args: {
555
+ name: string;
556
+ limits: SandboxLimits;
557
+ signal: AbortSignal;
558
+ }): Promise<FileSystemHandle>;
559
+ /** Deletes the file system and its files. Succeeds when it does not exist. */
560
+ destroyFileSystem(fs: FileSystemHandle, signal: AbortSignal): Promise<void>;
561
+ /**
562
+ * Deletes the file system made under `name` by `createFileSystem`; succeeds when there is none.
563
+ * The kernel uses it when it has no handle (`createFileSystem` finished on the provider's side
564
+ * but the kernel stopped before recording the handle), and in its one-off cleanup of
565
+ * per-conversation sandboxes with `cap-<conversationId>` names that a `sandbox@1` provider's
566
+ * `create` made together with their files (Docker: the `cap-<conversationId>-workspace` and
567
+ * `-skills` volumes); a provider removes those under the same call.
568
+ */
569
+ destroyFileSystemByName(name: string, signal: AbortSignal): Promise<void>;
570
+ /**
571
+ * Creates an instance attached to `fileSystem`. Without `image` the provider uses its own
572
+ * published image. Throws {@link FileSystemGoneError} when the file system is gone.
573
+ *
574
+ * `fence`: the new instance starts with this token stored (so a stale `update` is refused
575
+ * against it); a provider should throw {@link StaleFenceError}, leaving everything as it is,
576
+ * when an instance it would replace under `name` carries a higher token.
577
+ */
578
+ create(args: {
579
+ name: string;
580
+ image?: string;
581
+ limits: SandboxLimits;
582
+ fileSystem: FileSystemHandle;
583
+ signal: AbortSignal;
584
+ fence: DeviceFence;
585
+ }): Promise<SandboxHandle>;
586
+ /** `gone` means the provider no longer has this instance. */
587
+ resume(h: SandboxHandle, signal: AbortSignal): Promise<"resumed" | "gone">;
588
+ /** Files must be kept. Succeeds when the instance does not exist. */
589
+ suspend(h: SandboxHandle, signal: AbortSignal): Promise<void>;
590
+ /**
591
+ * Drops the instance only; the file system and its files are kept. Succeeds when it does not
592
+ * exist. `fence`: a provider should throw {@link StaleFenceError} when the instance carries a
593
+ * higher token; `null` when the kernel has no fence for the call (nothing to compare).
594
+ */
595
+ destroy(h: SandboxHandle, signal: AbortSignal, fence: DeviceFence | null): Promise<void>;
596
+ /**
597
+ * Drops the instance made under `name` by `create` (instance only, files kept); succeeds when
598
+ * there is none. The kernel uses it when it has no handle (`create` finished on the provider's
599
+ * side but the kernel stopped before recording the handle), and in its one-off cleanup of
600
+ * per-conversation sandboxes with `cap-<conversationId>` names made by a `sandbox@1` provider's
601
+ * `create`. `fence` as for {@link DeviceProvider.destroy} (`null` in that one-off cleanup,
602
+ * which has no device record).
603
+ */
604
+ destroyByName(name: string, signal: AbortSignal, fence: DeviceFence | null): Promise<void>;
605
+ /**
606
+ * Kills every running command started through `exec` in this instance (and the processes they
607
+ * spawned that did not start their own session). The kernel calls it only when no other holder
608
+ * can be running commands (an instance just resumed from `suspended`, or under the exclusive
609
+ * lease), never while another session may be; to clean up after one crashed holder it uses
610
+ * {@link DeviceProvider.killTagged}. Processes a command put in the background in their own
611
+ * session are not guaranteed to be killed.
612
+ */
613
+ killAll(h: SandboxHandle, signal: AbortSignal): Promise<void>;
614
+ /**
615
+ * Kills every running `exec` command labelled with one of `tags` (and its process tree), leaving
616
+ * other commands running. The kernel calls it for the tags of holders that crashed while live
617
+ * holders keep running commands. Tags match {@link SANDBOX_TAG_RE}. Match whole tags: one tag
618
+ * may be a prefix of another (`ab` of `ab-cd`), and killing `ab` must not touch `ab-cd`'s
619
+ * commands.
620
+ */
621
+ killTagged(h: SandboxHandle, tags: string[], signal: AbortSignal): Promise<void>;
622
+ /**
623
+ * Runs a command. `maxOutputBytes` caps stdout and stderr **combined**; the rest is dropped and
624
+ * `truncated` is true.
625
+ */
626
+ exec(h: SandboxHandle, args: {
627
+ command: string;
628
+ cwd?: string;
629
+ env?: Record<string, string>;
630
+ timeoutMs: number;
631
+ maxOutputBytes: number;
632
+ signal: AbortSignal;
633
+ /**
634
+ * The holder's tag ({@link SANDBOX_TAG_RE}); label the command with it so
635
+ * {@link DeviceProvider.killTagged} can find it.
636
+ */
637
+ tag: string;
638
+ /** Who runs the command; informational in phase 1 (record it, do not act on it). */
639
+ actor: SandboxActor;
640
+ }): Promise<ExecResult>;
641
+ /**
642
+ * Reads a file (at most `maxBytes`; longer files are truncated and `truncated` is true). Returns
643
+ * null when it does not exist or is a directory.
644
+ */
645
+ readFile(h: SandboxHandle, path: string, opts: {
646
+ maxBytes: number;
647
+ signal: AbortSignal;
648
+ }): Promise<{
649
+ data: Uint8Array;
650
+ truncated: boolean;
651
+ } | null>;
652
+ /** Parent directories are created. */
653
+ writeFile(h: SandboxHandle, path: string, data: Uint8Array, signal: AbortSignal): Promise<void>;
654
+ stat(h: SandboxHandle, path: string, signal: AbortSignal): Promise<{
655
+ size: number;
656
+ isDir: boolean;
657
+ } | null>;
658
+ /**
659
+ * Lists one directory's entries — each entry's `name` and whether it `isDir` — sorted by code
660
+ * unit (locale-independent), at most `limit` of them; `truncated` is true when the directory has
661
+ * more. Entries only: never file contents, never a recursive walk. A `name` is never `.` or `..`;
662
+ * `isDir` follows symbolic links (stat semantics: a link to a directory is `true`, a dangling link
663
+ * `false`, and so is a link whose target the device cannot reach). Returns null only when the
664
+ * path does not exist and the device could reach it; a path it cannot reach throws (a policy
665
+ * refusal), as does a path that is a file or a directory that cannot be read.
666
+ *
667
+ * Optional: a provider without it (or without {@link DeviceProvider.scan}) offers no projects,
668
+ * and its conversations stay in free mode.
669
+ */
670
+ list?(h: SandboxHandle, path: string, opts: {
671
+ limit: number;
672
+ signal: AbortSignal;
673
+ }): Promise<{
674
+ entries: {
675
+ name: string;
676
+ isDir: boolean;
677
+ }[];
678
+ truncated: boolean;
679
+ } | null>;
680
+ /**
681
+ * Looks for project-like directories, read as for {@link DeviceProvider.list}. Every provider
682
+ * traverses the same way:
683
+ *
684
+ * - Candidates are `base` itself and the directories at most {@link MAX_DEVICE_SCAN_DEPTH}
685
+ * levels below it that hold a `.git` entry (directory or file) or an `AGENTS.md` file.
686
+ * - The walk does not follow symbolic links and does not enter hidden directories (names
687
+ * starting with `.`); a candidate does not stop the descent (a monorepo's sub-projects are
688
+ * candidates too). It never enters a directory named in {@link DEVICE_SCAN_SKIPPED_DIRS}, at
689
+ * any depth, nor one named in {@link DEVICE_SCAN_SKIPPED_BASE_DIRS} directly under `base`;
690
+ * every provider skips exactly these. Paths under `base` the device cannot reach are skipped,
691
+ * never an error.
692
+ * - A directory whose basename does not satisfy {@link isDeviceProjectName} is not returned as
693
+ * a candidate (never with a changed name), but it is still traversed.
694
+ * - Directories are visited breadth-first, each level in code-unit order of `path`, `base`
695
+ * counting as the first visit (visiting a directory reads its entries; one that cannot be read
696
+ * still counts as a visit); the walk stops after {@link MAX_DEVICE_SCAN_DIRS} visits, so a
697
+ * huge tree answers what was found by then.
698
+ * - Each candidate is its absolute `path` and its basename as `suggestedName`; the candidates
699
+ * found are sorted by `path` in code-unit order and cut to the first
700
+ * {@link MAX_DEVICE_SCAN_RESULTS}.
701
+ *
702
+ * Directory entries only, never file contents. A `base` that does not exist answers `[]`; a
703
+ * `base` that is a file, cannot be read, or lies outside what the device reaches throws.
704
+ * Optional, like `list`.
705
+ */
706
+ scan?(h: SandboxHandle, base: string, opts: {
707
+ signal: AbortSignal;
708
+ }): Promise<{
709
+ path: string;
710
+ suggestedName: string;
711
+ }[]>;
712
+ /**
713
+ * Starts the named service in the instance, or finds it running (optional, with
714
+ * {@link DeviceProvider.stopService} and {@link DeviceProvider.serviceRequest}). Every service
715
+ * method names the service by its id (`spec.name`, `name`, `target.service`:
716
+ * `<extension>.<name>`, {@link DEVICE_SERVICE_ID_RE}), never by the extension's bare name.
717
+ * It writes `spec.files` into the service's code directory, then runs `spec.entry` with Node, the
718
+ * service socket and state directory named in its environment (`DEVICE_SERVICE_SOCKET_ENV`,
719
+ * `DEVICE_SERVICE_STATE_ENV`). A service already running under the name is kept as is, unless
720
+ * it runs other files than the spec's: then it is stopped and started on them. The kernel has
721
+ * passed the launch through the command review before it calls this.
722
+ *
723
+ * With `opts.ifRunning` it never starts anything and reads only `spec.name` (the kernel sends
724
+ * `{ name, runtime: "node", entry: "", args: [], files: {} }`, which must not be refused): a
725
+ * running service is kept and the device's current network limits are applied to it again,
726
+ * a restart for that runs the spec it was launched with and its stored code (the kernel calls
727
+ * this after the device's allowlist changed, and before reviewing a launch); a stopped one
728
+ * answers its `stopped` status (with `stateDir`, and `codeDir` where kept apart) and stays
729
+ * stopped. A running one answers its `filesDigest` when known, so the kernel reviews a start on
730
+ * new files before calling this without `ifRunning`.
731
+ *
732
+ * `opts.loopbackPorts` is the whole sorted set of the service's live loopback port grants
733
+ * (`DeviceServiceNetwork.loopbackPorts`, at most {@link LOOPBACK_MAX_PORTS_PER_SERVICE}): the
734
+ * service may reach {@link LOOPBACK_GRANT_HOSTS} on exactly these ports and no other loopback
735
+ * port. The kernel sends only a non-empty set, while the device's permission support reports
736
+ * `network.loopbackPorts` or is not known (none, or one naming an `unavailableReason`), and its
737
+ * ceilings allow running outside the sandbox; on a known refusal the key is absent. A provider forwards it only to a machine that
738
+ * announced the feature. A changed set applies like changed network limits (the running service
739
+ * is restarted on it), and an absent or empty set closes every loopback port again.
740
+ */
741
+ ensureService?(h: SandboxHandle, spec: DeviceServiceSpec, signal: AbortSignal, opts?: {
742
+ ifRunning?: boolean;
743
+ loopbackPorts?: readonly number[];
744
+ }): Promise<DeviceServiceStatus>;
745
+ /** Stops the named service; succeeds when it is not running. */
746
+ stopService?(h: SandboxHandle, name: string, signal: AbortSignal): Promise<void>;
747
+ /**
748
+ * The named service's state in the instance, without starting it (optional; the kernel's
749
+ * status reads). Whether a launch is reviewed is decided by `ensureService` with
750
+ * `ifRunning`, which is race-free.
751
+ */
752
+ serviceStatus?(h: SandboxHandle, name: string, signal: AbortSignal): Promise<DeviceServiceStatus>;
753
+ /**
754
+ * One `request` connection to the named service's socket: sends `body`, resolves the service's
755
+ * one answer. Throws when the service is not running, on timeout and on abort.
756
+ */
757
+ serviceRequest?(h: SandboxHandle, name: string, body: Uint8Array, opts: {
758
+ timeoutMs: number;
759
+ signal: AbortSignal;
760
+ }): Promise<Uint8Array>;
761
+ /**
762
+ * Opens a `stream` connection to the named service's socket (optional; the browser's live view
763
+ * needs it). The target is always a service of the device, never a general port forward.
764
+ * `signal` bounds the opening only; the stream lives until either side closes it.
765
+ */
766
+ openStream?(h: SandboxHandle, target: {
767
+ service: string;
768
+ }, signal: AbortSignal): Promise<DeviceStream>;
769
+ }
770
+ /**
771
+ * The most entries a `list` may ask for and its result may carry ({@link DeviceProvider.list}):
772
+ * part of the provider contract, which the device protocol's `list` op also honors.
773
+ */
774
+ export declare const MAX_DEVICE_LIST_ENTRIES = 1000;
775
+ /** The longest project name, in UTF-16 code units. */
776
+ export declare const MAX_DEVICE_PROJECT_NAME_LENGTH = 64;
777
+ /** The most candidates a `scan` answers ({@link DeviceProvider.scan}). */
778
+ export declare const MAX_DEVICE_SCAN_RESULTS = 50;
779
+ /**
780
+ * Directory names a `scan` never enters, at any depth ({@link DeviceProvider.scan}): dependency
781
+ * trees, not projects. Every provider skips exactly these.
782
+ */
783
+ export declare const DEVICE_SCAN_SKIPPED_DIRS: readonly string[];
784
+ /**
785
+ * Directory names a `scan` does not enter directly under its `base` ({@link DeviceProvider.scan}):
786
+ * the owner's macOS `Library` (app data, huge, partly privacy-protected) is not where projects
787
+ * live. Every provider skips exactly these.
788
+ */
789
+ export declare const DEVICE_SCAN_SKIPPED_BASE_DIRS: readonly string[];
790
+ /** How far below its base a `scan` looks: the base's children and their children. */
791
+ export declare const MAX_DEVICE_SCAN_DEPTH = 2;
792
+ /**
793
+ * The most directories one `scan` visits (reads the entries of), its `base` included
794
+ * ({@link DeviceProvider.scan}): breadth-first, each level in code-unit order of path.
795
+ */
796
+ export declare const MAX_DEVICE_SCAN_DIRS = 500;
797
+ /**
798
+ * Whether `v` is a valid project name (a project's identity on its device, and a `scan`
799
+ * candidate's `suggestedName`): non-empty, at most {@link MAX_DEVICE_PROJECT_NAME_LENGTH} UTF-16
800
+ * code units, not `.` or `..`, no `/`, no control characters, and no leading or trailing
801
+ * whitespace.
802
+ */
803
+ export declare function isDeviceProjectName(v: unknown): v is string;
804
+ /**
805
+ * The root-independent checks of a path inside an instance (non-empty, at most 4096 characters, no
806
+ * control characters or backslashes, absolute, no empty or `..` segments); returns the errors,
807
+ * empty when valid. {@link validateSandboxPath} adds root membership.
808
+ */
809
+ export declare function validateSandboxPathShape(p: string): string[];
810
+ /**
811
+ * Checks a path inside an instance; returns the errors, empty when valid: the
812
+ * {@link validateSandboxPathShape} checks, then root membership. `roots`: the instance's roots
813
+ * ({@link rootsOf}); absent means {@link SANDBOX_ROOTS}. The path must be one of them or under one.
814
+ */
815
+ export declare function validateSandboxPath(p: string, roots?: SandboxRoots): string[];
816
+ /**
817
+ * Validates an outbound allowlist host name: lowercase, no protocol or port, optional leading `*.`,
818
+ * IDN accepted only as punycode.
819
+ */
820
+ export declare function validateAllowHost(h: string): string[];