@capybara-hq/plugin-sdk 0.1.0-rc.8 → 0.1.0-rc.81
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.
- package/README.md +1 -1
- package/agent-docs/agent-templates.md +14 -2
- package/agent-docs/concepts.md +14 -5
- package/agent-docs/connector-templates.md +11 -3
- package/agent-docs/extension-points/channel.md +1 -1
- package/agent-docs/extension-points/context.md +15 -0
- package/agent-docs/extension-points/device-provider.md +15 -0
- package/agent-docs/extension-points/file-preview.md +3 -1
- package/agent-docs/extension-points/identity-source.md +2 -0
- package/agent-docs/extension-points/model-provider.md +3 -1
- package/agent-docs/extension-points/object-store.md +2 -0
- package/agent-docs/extension-points/push-provider.md +17 -0
- package/agent-docs/extensions.md +77 -4
- package/agent-docs/plugin-format.md +45 -10
- package/agent-docs/setup.md +54 -0
- package/dist/agent-template.d.ts +15 -6
- package/dist/agent-template.js +32 -13
- package/dist/avatar-presets.d.ts +8 -0
- package/dist/avatar-presets.js +17 -0
- package/dist/bootstrap.d.ts +109 -1
- package/dist/bootstrap.js +185 -2
- package/dist/cards.d.ts +97 -13
- package/dist/cards.js +121 -19
- package/dist/channel.d.ts +133 -47
- package/dist/channel.js +10 -5
- package/dist/client-css.d.ts +36 -0
- package/dist/client-css.js +326 -0
- package/dist/client-section.d.ts +35 -0
- package/dist/client-section.js +133 -0
- package/dist/config-ui.d.ts +28 -6
- package/dist/config-ui.js +88 -25
- package/dist/config-url-fetch.d.ts +19 -0
- package/dist/config-url-fetch.js +43 -0
- package/dist/connector-auth.d.ts +13 -2
- package/dist/connector-auth.js +82 -8
- package/dist/connector.d.ts +74 -32
- package/dist/context.d.ts +40 -0
- package/dist/context.js +2 -0
- package/dist/ctx.d.ts +224 -30
- package/dist/ctx.js +9 -1
- package/dist/define-extension.d.ts +23 -13
- package/dist/define-extension.js +19 -5
- package/dist/device-denied-paths.d.ts +20 -0
- package/dist/device-denied-paths.js +118 -0
- package/dist/device-operation.d.ts +339 -0
- package/dist/device-operation.js +312 -0
- package/dist/device-protocol.d.ts +285 -50
- package/dist/device-protocol.js +755 -90
- package/dist/device-provider.d.ts +780 -0
- package/dist/device-provider.js +409 -0
- package/dist/device-service.d.ts +126 -0
- package/dist/device-service.js +211 -0
- package/dist/dial-in.d.ts +286 -0
- package/dist/dial-in.js +213 -0
- package/dist/directory.d.ts +15 -12
- package/dist/extension-point.d.ts +2 -0
- package/dist/extension-point.js +7 -3
- package/dist/field-meta.d.ts +19 -0
- package/dist/field-meta.js +54 -0
- package/dist/file-preview.d.ts +43 -10
- package/dist/git-hosts.d.ts +53 -0
- package/dist/git-hosts.js +98 -0
- package/dist/hooks.d.ts +125 -20
- package/dist/hooks.js +5 -0
- package/dist/http-access.d.ts +71 -0
- package/dist/http-access.js +12 -0
- package/dist/identity.d.ts +70 -9
- package/dist/identity.js +46 -1
- package/dist/index.d.ts +28 -4
- package/dist/index.js +28 -4
- package/dist/model-params.d.ts +41 -22
- package/dist/model-params.js +28 -15
- package/dist/model.d.ts +227 -75
- package/dist/model.js +152 -19
- package/dist/native-failure.d.ts +13 -0
- package/dist/native-failure.js +31 -0
- package/dist/net.d.ts +52 -0
- package/dist/net.js +34 -0
- package/dist/network-classes.d.ts +98 -0
- package/dist/network-classes.js +291 -0
- package/dist/permissions.d.ts +65 -22
- package/dist/permissions.js +139 -43
- package/dist/platform-only.d.ts +27 -0
- package/dist/platform-only.js +35 -0
- package/dist/plugin-manifest.d.ts +30 -6
- package/dist/plugin-manifest.js +147 -55
- package/dist/plugin-package.d.ts +82 -39
- package/dist/plugin-package.js +68 -41
- package/dist/push.d.ts +75 -0
- package/dist/push.js +87 -0
- package/dist/sandbox-linux-runtime-source.d.ts +1 -0
- package/dist/sandbox-linux-runtime-source.js +2343 -0
- package/dist/sandbox-native-runtime-source.d.ts +10 -0
- package/dist/sandbox-native-runtime-source.js +480 -0
- package/dist/sandbox-runtime-source.d.ts +47 -0
- package/dist/sandbox-runtime-source.js +73 -0
- package/dist/sandbox-write-denies.d.ts +139 -0
- package/dist/sandbox-write-denies.js +869 -0
- package/dist/secret.d.ts +8 -2
- package/dist/secret.js +11 -4
- package/dist/setup-check.d.ts +128 -0
- package/dist/setup-check.js +142 -0
- package/dist/setup-guide-parse.d.ts +121 -0
- package/dist/setup-guide-parse.js +356 -0
- package/dist/setup-guide-render.d.ts +45 -0
- package/dist/setup-guide-render.js +351 -0
- package/dist/setup-guide.d.ts +86 -72
- package/dist/setup-guide.js +340 -108
- package/dist/shell-capabilities.d.ts +211 -0
- package/dist/shell-capabilities.js +44 -0
- package/dist/shell-environment.d.ts +57 -0
- package/dist/shell-environment.js +21 -0
- package/dist/skill-invocation.d.ts +5 -0
- package/dist/skill-invocation.js +4 -3
- package/dist/skill.d.ts +27 -17
- package/dist/skill.js +40 -26
- package/dist/storage.d.ts +52 -25
- package/dist/svg-viewbox.d.ts +10 -0
- package/dist/svg-viewbox.js +24 -0
- package/dist/theme-assets.d.ts +25 -9
- package/dist/theme-assets.js +87 -47
- package/dist/theme-tokens.generated.d.ts +26 -2
- package/dist/theme-tokens.generated.js +17 -0
- package/dist/theme.d.ts +48 -10
- package/dist/theme.js +100 -21
- package/dist/tool.d.ts +196 -31
- package/dist/tool.js +133 -13
- package/dist/ui-parts.d.ts +23 -0
- package/dist/ui-parts.js +385 -0
- package/dist/ui-resolve.d.ts +35 -0
- package/dist/ui-resolve.js +42 -0
- package/dist/ui-slots.d.ts +65 -0
- package/dist/ui-slots.js +261 -0
- package/dist/ui.d.ts +245 -77
- package/dist/ui.js +104 -27
- package/dist/url.d.ts +4 -1
- package/dist/url.js +4 -1
- package/dist/web.d.ts +26 -20
- package/package.json +12 -1
- package/schemas/bootstrap-extra.v1.schema.json +186 -0
- package/schemas/bootstrap.v1.schema.json +20 -0
- package/schemas/connector-template.v1.schema.json +62 -4
- package/schemas/plugin.v1.schema.json +74 -143
- package/schemas/theme.v1.schema.json +77 -0
- package/agent-docs/extension-points/sandbox.md +0 -15
- package/dist/org-access.d.ts +0 -27
- package/dist/org-access.js +0 -35
- package/dist/sandbox.d.ts +0 -254
- package/dist/sandbox.js +0 -153
|
@@ -0,0 +1,409 @@
|
|
|
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 { createHash } from "node:crypto";
|
|
12
|
+
/** Device provider extension point ref. */
|
|
13
|
+
export const DEVICE_PROVIDER_EXTENSION_POINT = "device-provider@1";
|
|
14
|
+
/** The roots inside the sandbox the model may access. */
|
|
15
|
+
export const SANDBOX_ROOTS = ["/workspace", "/skills", "/tmp"];
|
|
16
|
+
const MAX_PATH_LENGTH = 4096;
|
|
17
|
+
/** The roots every container-like provider uses; the same paths as {@link SANDBOX_ROOTS}. */
|
|
18
|
+
export const DEFAULT_SANDBOX_ROOTS = Object.freeze({
|
|
19
|
+
workspace: "/workspace",
|
|
20
|
+
skills: "/skills",
|
|
21
|
+
tmp: "/tmp",
|
|
22
|
+
});
|
|
23
|
+
/** The roots of an instance, falling back to {@link DEFAULT_SANDBOX_ROOTS}. */
|
|
24
|
+
export function rootsOf(h) {
|
|
25
|
+
return h.roots ?? DEFAULT_SANDBOX_ROOTS;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The layer of a refusal by a device's sandbox proxy, read from the reason the proxy gives: the
|
|
29
|
+
* status phrase of its 403 answer to a CONNECT, or the reason in its violation line
|
|
30
|
+
* (`deny network-outbound <host>:<port> (<reason>)`).
|
|
31
|
+
*
|
|
32
|
+
* - `address`: the destination's address is closed (an IP literal in a closed class, or a name
|
|
33
|
+
* resolving to a private or local address: "address is …", "resolved to …"), or the host is
|
|
34
|
+
* malformed. No host grant reaches it, only a command run outside the sandbox.
|
|
35
|
+
* - `device`: the host is not on the operation's allowlist ("host is not on the allow list").
|
|
36
|
+
* - `unknown`: any other reason (a proxy that names none, a failed fake-IP lookup).
|
|
37
|
+
*/
|
|
38
|
+
export function sandboxProxyRefusalLayer(reason) {
|
|
39
|
+
const r = reason.trim().toLowerCase();
|
|
40
|
+
if (r.startsWith("address is ") || r.startsWith("resolved to ") || r === "malformed host")
|
|
41
|
+
return "address";
|
|
42
|
+
if (r === "host is not on the allow list")
|
|
43
|
+
return "device";
|
|
44
|
+
return "unknown";
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The instance is definitely gone on the provider's side (container removed, E2B sandbox expired).
|
|
48
|
+
* `exec` / `readFile` / `writeFile` / `stat` must throw it (not a plain Error) when they find the
|
|
49
|
+
* instance gone; the kernel then rebuilds the instance and retries once. A transient connection
|
|
50
|
+
* failure throws a plain error. In a managed operation (`operation()`), a gone instance found
|
|
51
|
+
* **before** the request reached the machine throws a `DeviceRequestNotSentError` with this error
|
|
52
|
+
* as its `cause`: the operation is then recorded as not started rather than as an unknown outcome,
|
|
53
|
+
* and the instance is still rebuilt, so the call can be retried on it.
|
|
54
|
+
*/
|
|
55
|
+
export class SandboxGoneError extends Error {
|
|
56
|
+
sandboxGone = true;
|
|
57
|
+
constructor(message, opts) {
|
|
58
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
59
|
+
this.name = "SandboxGoneError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
|
|
63
|
+
export function isSandboxGoneError(e) {
|
|
64
|
+
return (e instanceof Error &&
|
|
65
|
+
e.name === "SandboxGoneError" &&
|
|
66
|
+
e.sandboxGone === true);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Thrown by a lifecycle call whose {@link DeviceFence} token is lower than one the instance
|
|
70
|
+
* already applied: the caller is a stale holder, and nothing was changed. The kernel handles it as
|
|
71
|
+
* a lost lease (never as a reason to recreate). `current` is the token the instance holds, when the
|
|
72
|
+
* provider knows it.
|
|
73
|
+
*/
|
|
74
|
+
export class StaleFenceError extends Error {
|
|
75
|
+
staleFence = true;
|
|
76
|
+
current;
|
|
77
|
+
constructor(message, opts) {
|
|
78
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
79
|
+
this.name = "StaleFenceError";
|
|
80
|
+
this.current = opts?.current;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
|
|
84
|
+
export function isStaleFenceError(e) {
|
|
85
|
+
return (e instanceof Error &&
|
|
86
|
+
e.name === "StaleFenceError" &&
|
|
87
|
+
e.staleFence === true);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The file system is definitely gone on the provider's side (volume removed, E2B sandbox killed).
|
|
91
|
+
* `create` throws it instead of attaching the instance to a new, empty file system; the kernel then
|
|
92
|
+
* reports the device's files as lost rather than handing out a silently empty workspace.
|
|
93
|
+
*/
|
|
94
|
+
export class FileSystemGoneError extends Error {
|
|
95
|
+
fileSystemGone = true;
|
|
96
|
+
constructor(message, opts) {
|
|
97
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
98
|
+
this.name = "FileSystemGoneError";
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
|
|
102
|
+
export function isFileSystemGoneError(e) {
|
|
103
|
+
return (e instanceof Error &&
|
|
104
|
+
e.name === "FileSystemGoneError" &&
|
|
105
|
+
e.fileSystemGone === true);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* A holder tag (`exec` argument `tag`, {@link DeviceProvider.killTagged}): the kernel passes its
|
|
109
|
+
* lease tokens (UUIDs). Providers check tags against it before putting them in a command line or a
|
|
110
|
+
* kill pattern.
|
|
111
|
+
*/
|
|
112
|
+
export const SANDBOX_TAG_RE = /^[a-z0-9-]{1,64}$/;
|
|
113
|
+
/** Size limits {@link parseDeviceReport} enforces. */
|
|
114
|
+
export const DEVICE_REPORT_LIMITS = Object.freeze({
|
|
115
|
+
revisionChars: 128,
|
|
116
|
+
descriptionChars: 300,
|
|
117
|
+
limits: 8,
|
|
118
|
+
limitLabelChars: 60,
|
|
119
|
+
limitValueChars: 60,
|
|
120
|
+
homeChars: 4096,
|
|
121
|
+
changeHintChars: 400,
|
|
122
|
+
});
|
|
123
|
+
const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
124
|
+
function exactKeys(v, keys, what) {
|
|
125
|
+
for (const k of Object.keys(v)) {
|
|
126
|
+
if (!keys.includes(k))
|
|
127
|
+
throw new TypeError(`${what}: unknown key ${k}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function parseText(v, max, what) {
|
|
131
|
+
if (!isRecord(v))
|
|
132
|
+
throw new TypeError(`${what} must be an object`);
|
|
133
|
+
exactKeys(v, ["en", "zh"], what);
|
|
134
|
+
const { en, zh } = v;
|
|
135
|
+
if (typeof en !== "string" || en === "" || en.length > max) {
|
|
136
|
+
throw new TypeError(`${what}.en must be a non-empty string of at most ${max} characters`);
|
|
137
|
+
}
|
|
138
|
+
if (zh === undefined)
|
|
139
|
+
return { en };
|
|
140
|
+
if (typeof zh !== "string" || zh === "" || zh.length > max) {
|
|
141
|
+
throw new TypeError(`${what}.zh must be a non-empty string of at most ${max} characters`);
|
|
142
|
+
}
|
|
143
|
+
return { en, zh };
|
|
144
|
+
}
|
|
145
|
+
/** Checks a provider's report; throws `TypeError` on any other shape, returns a clean copy. */
|
|
146
|
+
export function parseDeviceReport(value) {
|
|
147
|
+
const L = DEVICE_REPORT_LIMITS;
|
|
148
|
+
if (!isRecord(value))
|
|
149
|
+
throw new TypeError("device report must be an object");
|
|
150
|
+
exactKeys(value, ["revision", "description", "home", "limits", "reset", "changeHint"], "device report");
|
|
151
|
+
const { revision, home, limits, reset } = value;
|
|
152
|
+
if (typeof revision !== "string" || revision === "" || revision.length > L.revisionChars) {
|
|
153
|
+
throw new TypeError(`device report revision must be a non-empty string of at most ${L.revisionChars} characters`);
|
|
154
|
+
}
|
|
155
|
+
const description = parseText(value.description, L.descriptionChars, "device report description");
|
|
156
|
+
if (home !== null) {
|
|
157
|
+
if (typeof home !== "string" || home.length > L.homeChars) {
|
|
158
|
+
throw new TypeError("device report home must be an absolute path or null");
|
|
159
|
+
}
|
|
160
|
+
const errors = validateSandboxPathShape(home);
|
|
161
|
+
if (errors.length > 0)
|
|
162
|
+
throw new TypeError(`device report home: ${errors.join("; ")}`);
|
|
163
|
+
}
|
|
164
|
+
if (!Array.isArray(limits) || limits.length > L.limits) {
|
|
165
|
+
throw new TypeError(`device report limits must be an array of at most ${L.limits} entries`);
|
|
166
|
+
}
|
|
167
|
+
const parsedLimits = limits.map((raw, i) => {
|
|
168
|
+
const what = `device report limits[${i}]`;
|
|
169
|
+
if (!isRecord(raw))
|
|
170
|
+
throw new TypeError(`${what} must be an object`);
|
|
171
|
+
exactKeys(raw, ["label", "value"], what);
|
|
172
|
+
return {
|
|
173
|
+
label: parseText(raw.label, L.limitLabelChars, `${what}.label`),
|
|
174
|
+
value: parseText(raw.value, L.limitValueChars, `${what}.value`),
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
if (typeof reset !== "boolean")
|
|
178
|
+
throw new TypeError("device report reset must be a boolean");
|
|
179
|
+
const changeHint = value.changeHint === undefined
|
|
180
|
+
? undefined
|
|
181
|
+
: parseText(value.changeHint, L.changeHintChars, "device report changeHint");
|
|
182
|
+
return {
|
|
183
|
+
revision,
|
|
184
|
+
description,
|
|
185
|
+
home,
|
|
186
|
+
limits: parsedLimits,
|
|
187
|
+
reset,
|
|
188
|
+
...(changeHint !== undefined && { changeHint }),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The provider refuses a project directory (`projects` operations: it lies where the provider
|
|
193
|
+
* will not serve, for example another connection's directory). `reason` is shown to the person.
|
|
194
|
+
* The kernel answers it as `device.project_refused` (422).
|
|
195
|
+
*/
|
|
196
|
+
export class ProjectPathRefusedError extends Error {
|
|
197
|
+
projectPathRefused = true;
|
|
198
|
+
reason;
|
|
199
|
+
constructor(reason, opts) {
|
|
200
|
+
super(reason.en, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
201
|
+
this.name = "ProjectPathRefusedError";
|
|
202
|
+
this.reason = reason;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Duck-typed check (not instanceof), so a plugin bundling another plugin-sdk copy still matches. */
|
|
206
|
+
export function isProjectPathRefusedError(e) {
|
|
207
|
+
return (e instanceof Error &&
|
|
208
|
+
e.name === "ProjectPathRefusedError" &&
|
|
209
|
+
e.projectPathRefused === true &&
|
|
210
|
+
isRecord(e.reason));
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* A device service name as an extension declares it (`permissions.deviceAccess.services`) and
|
|
214
|
+
* names it in `ctx.deviceAccess`: 1 to 32 characters, a lower-case letter then letters, digits
|
|
215
|
+
* and `-`.
|
|
216
|
+
*/
|
|
217
|
+
export const DEVICE_SERVICE_NAME_RE = /^[a-z][a-z0-9-]{0,31}$/;
|
|
218
|
+
/**
|
|
219
|
+
* A device service id: what a device provider receives as the service's name. The kernel
|
|
220
|
+
* namespaces every extension's service names by the extension (`<extension>.<name>`,
|
|
221
|
+
* {@link deviceServiceId}), so two extensions declaring the same name never start, stop or reach
|
|
222
|
+
* each other's process; the provider keys everything of a service (its state and code
|
|
223
|
+
* directories, its socket, its process) by the id. At most 74 characters, one path segment.
|
|
224
|
+
*/
|
|
225
|
+
export const DEVICE_SERVICE_ID_RE = /^[a-z][a-z0-9-]{1,40}\.[a-z][a-z0-9-]{0,31}$/;
|
|
226
|
+
/**
|
|
227
|
+
* The device service id of an extension's service (`<extension>.<name>`,
|
|
228
|
+
* {@link DEVICE_SERVICE_ID_RE}). Throws a `TypeError` for an extension name that is not one or a
|
|
229
|
+
* service name breaking {@link DEVICE_SERVICE_NAME_RE}.
|
|
230
|
+
*/
|
|
231
|
+
export function deviceServiceId(extension, name) {
|
|
232
|
+
const id = `${extension}.${name}`;
|
|
233
|
+
if (!DEVICE_SERVICE_NAME_RE.test(name) || !DEVICE_SERVICE_ID_RE.test(id))
|
|
234
|
+
throw new TypeError(`Invalid device service ${JSON.stringify(name)} of extension ${JSON.stringify(extension)}`);
|
|
235
|
+
return id;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The digest of a service's files: `sha256:` and the hex SHA-256 over each file in path order
|
|
239
|
+
* (path, NUL, byte length, NUL, bytes). Providers report it in {@link DeviceServiceStatus} and
|
|
240
|
+
* may send it to a machine instead of the files.
|
|
241
|
+
*/
|
|
242
|
+
export function deviceServiceFilesDigest(files) {
|
|
243
|
+
const cached = digests.get(files);
|
|
244
|
+
if (cached !== undefined)
|
|
245
|
+
return cached;
|
|
246
|
+
const digest = digestOf(files);
|
|
247
|
+
digests.set(files, digest);
|
|
248
|
+
return digest;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Digests already computed, by files object: a plugin passes the same `files` object on every
|
|
252
|
+
* call (its service files are read once), and a browser's are megabytes. Callers never mutate a
|
|
253
|
+
* spec's files after building it.
|
|
254
|
+
*/
|
|
255
|
+
const digests = new WeakMap();
|
|
256
|
+
/**
|
|
257
|
+
* A fixed input and its {@link deviceServiceFilesDigest}, for the tests of every implementation
|
|
258
|
+
* of the digest (the SDK's, capybara-device's, a provider's own): one that disagrees would restart
|
|
259
|
+
* every running service at every ensure.
|
|
260
|
+
*/
|
|
261
|
+
export const DEVICE_SERVICE_FILES_DIGEST_VECTOR = Object.freeze({
|
|
262
|
+
files: Object.freeze({
|
|
263
|
+
"main.js": "console.log(1)\n",
|
|
264
|
+
"lib/a.js": "export const a = 1;\n",
|
|
265
|
+
empty: "",
|
|
266
|
+
}),
|
|
267
|
+
digest: "sha256:7b3959ff2e10237b9f1a255bbecb5dacebd31422fe734b2867403e42913432a5",
|
|
268
|
+
});
|
|
269
|
+
function digestOf(files) {
|
|
270
|
+
const h = createHash("sha256");
|
|
271
|
+
for (const path of Object.keys(files).sort()) {
|
|
272
|
+
const data = files[path];
|
|
273
|
+
h.update(`${path}\0${data.byteLength}\0`);
|
|
274
|
+
h.update(data);
|
|
275
|
+
}
|
|
276
|
+
return `sha256:${h.digest("hex")}`;
|
|
277
|
+
}
|
|
278
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are exactly what must be blocked
|
|
279
|
+
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
280
|
+
/**
|
|
281
|
+
* The most entries a `list` may ask for and its result may carry ({@link DeviceProvider.list}):
|
|
282
|
+
* part of the provider contract, which the device protocol's `list` op also honors.
|
|
283
|
+
*/
|
|
284
|
+
export const MAX_DEVICE_LIST_ENTRIES = 1000;
|
|
285
|
+
/** The longest project name, in UTF-16 code units. */
|
|
286
|
+
export const MAX_DEVICE_PROJECT_NAME_LENGTH = 64;
|
|
287
|
+
/** The most candidates a `scan` answers ({@link DeviceProvider.scan}). */
|
|
288
|
+
export const MAX_DEVICE_SCAN_RESULTS = 50;
|
|
289
|
+
/**
|
|
290
|
+
* Directory names a `scan` never enters, at any depth ({@link DeviceProvider.scan}): dependency
|
|
291
|
+
* trees, not projects. Every provider skips exactly these.
|
|
292
|
+
*/
|
|
293
|
+
export const DEVICE_SCAN_SKIPPED_DIRS = Object.freeze(["node_modules"]);
|
|
294
|
+
/**
|
|
295
|
+
* Directory names a `scan` does not enter directly under its `base` ({@link DeviceProvider.scan}):
|
|
296
|
+
* the owner's macOS `Library` (app data, huge, partly privacy-protected) is not where projects
|
|
297
|
+
* live. Every provider skips exactly these.
|
|
298
|
+
*/
|
|
299
|
+
export const DEVICE_SCAN_SKIPPED_BASE_DIRS = Object.freeze(["Library"]);
|
|
300
|
+
/** How far below its base a `scan` looks: the base's children and their children. */
|
|
301
|
+
export const MAX_DEVICE_SCAN_DEPTH = 2;
|
|
302
|
+
/**
|
|
303
|
+
* The most directories one `scan` visits (reads the entries of), its `base` included
|
|
304
|
+
* ({@link DeviceProvider.scan}): breadth-first, each level in code-unit order of path.
|
|
305
|
+
*/
|
|
306
|
+
export const MAX_DEVICE_SCAN_DIRS = 500;
|
|
307
|
+
/**
|
|
308
|
+
* Whether `v` is a valid project name (a project's identity on its device, and a `scan`
|
|
309
|
+
* candidate's `suggestedName`): non-empty, at most {@link MAX_DEVICE_PROJECT_NAME_LENGTH} UTF-16
|
|
310
|
+
* code units, not `.` or `..`, no `/`, no control characters, and no leading or trailing
|
|
311
|
+
* whitespace.
|
|
312
|
+
*/
|
|
313
|
+
export function isDeviceProjectName(v) {
|
|
314
|
+
return (typeof v === "string" &&
|
|
315
|
+
v !== "" &&
|
|
316
|
+
v !== "." &&
|
|
317
|
+
v !== ".." &&
|
|
318
|
+
v.length <= MAX_DEVICE_PROJECT_NAME_LENGTH &&
|
|
319
|
+
!CONTROL.test(v) &&
|
|
320
|
+
!v.includes("/") &&
|
|
321
|
+
!/^\s|\s$/.test(v));
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The root-independent checks of a path inside an instance (non-empty, at most 4096 characters, no
|
|
325
|
+
* control characters or backslashes, absolute, no empty or `..` segments); returns the errors,
|
|
326
|
+
* empty when valid. {@link validateSandboxPath} adds root membership.
|
|
327
|
+
*/
|
|
328
|
+
export function validateSandboxPathShape(p) {
|
|
329
|
+
if (typeof p !== "string" || p.length === 0)
|
|
330
|
+
return ["path must be a non-empty string"];
|
|
331
|
+
if (p.length > MAX_PATH_LENGTH)
|
|
332
|
+
return [`path must be at most ${MAX_PATH_LENGTH} characters`];
|
|
333
|
+
if (CONTROL.test(p))
|
|
334
|
+
return ["path must not contain control characters"];
|
|
335
|
+
if (p.includes("\\"))
|
|
336
|
+
return ["path must not contain backslashes"];
|
|
337
|
+
if (!p.startsWith("/"))
|
|
338
|
+
return ["path must be absolute"];
|
|
339
|
+
if (p.includes("//"))
|
|
340
|
+
return ["path must not contain empty segments"];
|
|
341
|
+
for (const seg of p.split("/")) {
|
|
342
|
+
let decoded = seg;
|
|
343
|
+
try {
|
|
344
|
+
decoded = decodeURIComponent(seg);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
// invalid percent-encoding is taken literally
|
|
348
|
+
}
|
|
349
|
+
if (seg === ".." || decoded === ".." || decoded.includes("/") || decoded.includes("\\")) {
|
|
350
|
+
return ["path must not contain '..' segments"];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
/** A structural error after which {@link validateSandboxPath} stops (before the root check). */
|
|
356
|
+
const STOPS = new Set([
|
|
357
|
+
"path must be a non-empty string",
|
|
358
|
+
`path must be at most ${MAX_PATH_LENGTH} characters`,
|
|
359
|
+
"path must not contain control characters",
|
|
360
|
+
"path must not contain backslashes",
|
|
361
|
+
"path must be absolute",
|
|
362
|
+
"path must not contain empty segments",
|
|
363
|
+
]);
|
|
364
|
+
/**
|
|
365
|
+
* Checks a path inside an instance; returns the errors, empty when valid: the
|
|
366
|
+
* {@link validateSandboxPathShape} checks, then root membership. `roots`: the instance's roots
|
|
367
|
+
* ({@link rootsOf}); absent means {@link SANDBOX_ROOTS}. The path must be one of them or under one.
|
|
368
|
+
*/
|
|
369
|
+
export function validateSandboxPath(p, roots) {
|
|
370
|
+
const errors = validateSandboxPathShape(p);
|
|
371
|
+
if (errors.some((e) => STOPS.has(e)))
|
|
372
|
+
return errors;
|
|
373
|
+
const allowed = roots === undefined ? SANDBOX_ROOTS : [roots.workspace, roots.skills, roots.tmp];
|
|
374
|
+
if (!allowed.some((r) => p === r || p.startsWith(`${r}/`))) {
|
|
375
|
+
errors.push(`path must be under ${allowed.join(", ")}`);
|
|
376
|
+
}
|
|
377
|
+
return errors;
|
|
378
|
+
}
|
|
379
|
+
const LABEL = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
380
|
+
/**
|
|
381
|
+
* Validates an outbound allowlist host name: lowercase, no protocol or port, optional leading `*.`,
|
|
382
|
+
* IDN accepted only as punycode.
|
|
383
|
+
*/
|
|
384
|
+
export function validateAllowHost(h) {
|
|
385
|
+
if (typeof h !== "string" || h.length === 0)
|
|
386
|
+
return ["host must be a non-empty string"];
|
|
387
|
+
if (h.length > 253)
|
|
388
|
+
return ["host must be at most 253 characters"];
|
|
389
|
+
const name = h.startsWith("*.") ? h.slice(2) : h;
|
|
390
|
+
if (name.length === 0)
|
|
391
|
+
return ["wildcard must be followed by a domain"];
|
|
392
|
+
const labels = name.split(".");
|
|
393
|
+
if (labels.every((l) => LABEL.test(l))) {
|
|
394
|
+
const tld = labels[labels.length - 1];
|
|
395
|
+
if (labels.length < 2)
|
|
396
|
+
return ["host must have at least two labels (no single-label names)"];
|
|
397
|
+
// The last label must start with a letter: this blocks the decimal / octal / hexadecimal
|
|
398
|
+
// spellings of IPv4
|
|
399
|
+
if (!/^[a-z]/.test(tld))
|
|
400
|
+
return ["host must not be an IP address literal"];
|
|
401
|
+
if (["local", "internal", "localhost"].includes(tld)) {
|
|
402
|
+
return ["host must not be in an internal-only domain (.local, .internal, .localhost)"];
|
|
403
|
+
}
|
|
404
|
+
return [];
|
|
405
|
+
}
|
|
406
|
+
return [
|
|
407
|
+
"host must be a lowercase hostname without scheme, port or path (optional leading '*.'; IDN as punycode)",
|
|
408
|
+
];
|
|
409
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device service socket protocol: how a device provider (or the machine program serving a
|
|
3
|
+
* dial-in device) talks to a service it started with `DeviceProvider.ensureService`. Every
|
|
4
|
+
* provider and every service speaks it, so a service runs the same on every provider.
|
|
5
|
+
*
|
|
6
|
+
* The provider starts the service with {@link DEVICE_SERVICE_SOCKET_ENV} naming the unix socket
|
|
7
|
+
* the service listens on and {@link DEVICE_SERVICE_STATE_ENV} naming its state directory (the
|
|
8
|
+
* only place it may write besides the workspace downloads). The spec's files are written before
|
|
9
|
+
* start into the service's code directory: a directory the service can only read where the
|
|
10
|
+
* provider can separate code from state, else the state directory. On a connection,
|
|
11
|
+
* both sides exchange length-prefixed messages ({@link encodeServiceMessage}). The first message
|
|
12
|
+
* from the provider is JSON whose `kind` is one of {@link SERVICE_CONNECTION_KINDS}:
|
|
13
|
+
*
|
|
14
|
+
* - `{"kind":"request"}`: the next provider message is the request body; the service answers
|
|
15
|
+
* with exactly one message and closes the connection.
|
|
16
|
+
* - `{"kind":"stream"}`: both sides keep exchanging messages until either side closes.
|
|
17
|
+
*
|
|
18
|
+
* **Viewer streams.** On a stream the kernel opens for a viewer (a person's panel, through a
|
|
19
|
+
* viewer ticket), the first stream message after `{"kind":"stream"}` is written by the kernel, not
|
|
20
|
+
* the panel: the viewer context {@link DeviceServiceViewerContext},
|
|
21
|
+
* `{"kind":"viewer","conversationId":"<id>","rootConversationId":"<id>"}`, naming the ticket's
|
|
22
|
+
* conversation (checked at mint to be owned by the viewing person) and the root of its
|
|
23
|
+
* conversation tree (the conversation itself outside sub-agents). The panel's own messages
|
|
24
|
+
* follow, unchanged and in order. A service reads only that first message as context
|
|
25
|
+
* ({@link parseViewerContext}); a later message of the same shape comes from the panel and is
|
|
26
|
+
* never context. Providers and the device program pass the viewer context through like any
|
|
27
|
+
* stream message.
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* The request body the kernel sends a running service before restarting it on new files (an
|
|
31
|
+
* upgraded extension, {@link DeviceServiceRollCheck}). A service that answers
|
|
32
|
+
* `{"busy":true,"reason":"…"}` is kept running (a person is using it); the kernel asks again at a
|
|
33
|
+
* later ensure. Any other answer, an error or no answer within a few seconds lets the restart go
|
|
34
|
+
* ahead, so a service that does not know the check is restarted as before. Every service gets
|
|
35
|
+
* this body on its request connection, so a service must ignore a request body it does not know
|
|
36
|
+
* (answer an error, act on nothing), never treat an arbitrary body as a command.
|
|
37
|
+
*/
|
|
38
|
+
export declare const DEVICE_SERVICE_ROLL_CHECK = "{\"capybara\":\"roll-check\"}";
|
|
39
|
+
/** A service's answer to {@link DEVICE_SERVICE_ROLL_CHECK}. */
|
|
40
|
+
export interface DeviceServiceRollCheck {
|
|
41
|
+
/** True while restarting now would lose something a person is doing (a takeover, a handoff). */
|
|
42
|
+
busy: boolean;
|
|
43
|
+
/** What keeps it busy, for the kernel's log. */
|
|
44
|
+
reason?: string;
|
|
45
|
+
}
|
|
46
|
+
/** Whether `body` is the kernel's {@link DEVICE_SERVICE_ROLL_CHECK} request. */
|
|
47
|
+
export declare function isRollCheck(body: Uint8Array | string): boolean;
|
|
48
|
+
/** Reads a service's answer to the roll check: busy only for `{"busy":true}` (never throws). */
|
|
49
|
+
export declare function parseRollCheck(body: Uint8Array | string): DeviceServiceRollCheck;
|
|
50
|
+
/** Names the unix socket path the service listens on. */
|
|
51
|
+
export declare const DEVICE_SERVICE_SOCKET_ENV = "CAPYBARA_SERVICE_SOCKET";
|
|
52
|
+
/** Names the service's state directory (its own data, its write root; not where its code is). */
|
|
53
|
+
export declare const DEVICE_SERVICE_STATE_ENV = "CAPYBARA_SERVICE_STATE";
|
|
54
|
+
/**
|
|
55
|
+
* Names the device's workspace root. Every provider sets it for every service (a spec cannot set
|
|
56
|
+
* it); a service confines the files it takes in (uploads) and lets out (downloads) to it.
|
|
57
|
+
*/
|
|
58
|
+
export declare const DEVICE_SERVICE_WORKSPACE_ENV = "CAPYBARA_SERVICE_WORKSPACE";
|
|
59
|
+
/**
|
|
60
|
+
* Names the service's install directory, where the provider gives it one (device-local does;
|
|
61
|
+
* absent elsewhere): a place for what the service fetches once and then only runs, such as a
|
|
62
|
+
* browser, apart from its state. The service may write it until it creates the regular file
|
|
63
|
+
* {@link DEVICE_SERVICE_INSTALL_SEAL} in it; from its next start on the directory is read only
|
|
64
|
+
* for the service, so nothing the service later runs (a compromised browser included) can
|
|
65
|
+
* replace what was installed. The provider empties it at every start that does not find it
|
|
66
|
+
* sealed under the service's current files (unsealed, or a plugin upgrade since), so a service
|
|
67
|
+
* installs afresh while unsealed and never trusts what it finds there. The process that seals
|
|
68
|
+
* keeps its write access until it exits, so a service runs no untrusted input (no web page) while
|
|
69
|
+
* unsealed, seals only a complete install and then exits; the next start, reviewed again, runs it
|
|
70
|
+
* read only.
|
|
71
|
+
*/
|
|
72
|
+
export declare const DEVICE_SERVICE_INSTALL_ENV = "CAPYBARA_SERVICE_INSTALL";
|
|
73
|
+
/** The file that seals a service's install directory ({@link DEVICE_SERVICE_INSTALL_ENV}). */
|
|
74
|
+
export declare const DEVICE_SERVICE_INSTALL_SEAL = ".sealed";
|
|
75
|
+
/**
|
|
76
|
+
* Names the Mach service allowances the provider's sandbox gives the service (macOS: device-local
|
|
77
|
+
* sets it, a spec cannot). Its value {@link DEVICE_SERVICE_MACH_CHROMIUM} means the full Chromium
|
|
78
|
+
* build can start there (its `MachPortRendezvousServer` check-in is allowed); absent, a service
|
|
79
|
+
* must not count on it (an older capybara-device refuses it).
|
|
80
|
+
*/
|
|
81
|
+
export declare const DEVICE_SERVICE_MACH_ENV = "CAPYBARA_SERVICE_MACH";
|
|
82
|
+
/** {@link DEVICE_SERVICE_MACH_ENV}'s value for the full Chromium build's Mach services. */
|
|
83
|
+
export declare const DEVICE_SERVICE_MACH_CHROMIUM = "chromium";
|
|
84
|
+
/** The largest message body, in bytes, either side may send on a service connection. */
|
|
85
|
+
export declare const DEVICE_SERVICE_MAX_MESSAGE: number;
|
|
86
|
+
/** The kinds a connection's first message may name. */
|
|
87
|
+
export declare const SERVICE_CONNECTION_KINDS: readonly ["request", "stream"];
|
|
88
|
+
/** A service connection kind ({@link SERVICE_CONNECTION_KINDS}). */
|
|
89
|
+
export type ServiceConnectionKind = (typeof SERVICE_CONNECTION_KINDS)[number];
|
|
90
|
+
/**
|
|
91
|
+
* Frames one message: a 4-byte big-endian length followed by `body`. Throws a `RangeError` for a
|
|
92
|
+
* body above {@link DEVICE_SERVICE_MAX_MESSAGE}.
|
|
93
|
+
*/
|
|
94
|
+
export declare function encodeServiceMessage(body: Uint8Array): Uint8Array;
|
|
95
|
+
/**
|
|
96
|
+
* Splits a byte stream into the messages {@link encodeServiceMessage} framed, whatever the chunk
|
|
97
|
+
* boundaries. `push` returns the messages completed by `chunk` (copies, safe to keep). A declared
|
|
98
|
+
* length above {@link DEVICE_SERVICE_MAX_MESSAGE} throws a `RangeError` as soon as the header is
|
|
99
|
+
* read; the connection should then be closed (the decoder is not usable afterwards).
|
|
100
|
+
*/
|
|
101
|
+
export declare class ServiceMessageDecoder {
|
|
102
|
+
private chunks;
|
|
103
|
+
private first;
|
|
104
|
+
private pending;
|
|
105
|
+
private need;
|
|
106
|
+
push(chunk: Uint8Array): Uint8Array[];
|
|
107
|
+
/** Removes the first `n` (at most `pending`) bytes into one new array, copying each byte once. */
|
|
108
|
+
private take;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The first stream message of a viewer stream, written by the kernel (see the protocol above):
|
|
112
|
+
* the ticket's conversation and the root of its conversation tree.
|
|
113
|
+
*/
|
|
114
|
+
export interface DeviceServiceViewerContext {
|
|
115
|
+
kind: "viewer";
|
|
116
|
+
conversationId: string;
|
|
117
|
+
rootConversationId: string;
|
|
118
|
+
}
|
|
119
|
+
/** The body of the viewer context stream message (UTF-8 JSON; the provider frames it). */
|
|
120
|
+
export declare function encodeViewerContext(ctx: DeviceServiceViewerContext): Uint8Array;
|
|
121
|
+
/**
|
|
122
|
+
* Reads a stream message body as the viewer context: `kind` is `"viewer"` and both ids are
|
|
123
|
+
* non-empty strings; only those three fields are returned. Null for anything else (not UTF-8,
|
|
124
|
+
* not JSON, another shape).
|
|
125
|
+
*/
|
|
126
|
+
export declare function parseViewerContext(body: Uint8Array | string): DeviceServiceViewerContext | null;
|