@capybara-hq/plugin-sdk 0.1.0-rc.23 → 0.1.0-rc.25
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/dist/device-protocol.d.ts +81 -7
- package/dist/device-protocol.js +95 -4
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/sandbox-native-runtime-source.d.ts +8 -0
- package/dist/sandbox-native-runtime-source.js +307 -0
- package/dist/sandbox-operation.d.ts +248 -0
- package/dist/sandbox-operation.js +245 -0
- package/dist/sandbox-runtime-source.d.ts +13 -0
- package/dist/sandbox-runtime-source.js +16 -0
- package/dist/sandbox.d.ts +109 -0
- package/dist/sandbox.js +38 -0
- package/package.json +1 -1
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { type ExecResult, type SandboxActor, type SandboxHandle, type SandboxProvider } from "./sandbox.js";
|
|
2
|
+
/** The permission profile selected for a conversation. */
|
|
3
|
+
export type PermissionProfile = "request-approval" | "approve-for-me" | "full-access";
|
|
4
|
+
/** Accepts only the supported permission profile names. */
|
|
5
|
+
export declare function isPermissionProfile(value: unknown): value is PermissionProfile;
|
|
6
|
+
/** The provider cannot enforce the requested permission profile. */
|
|
7
|
+
export declare class SandboxPermissionUnsupportedError extends Error {
|
|
8
|
+
readonly sandboxPermissionUnsupported: true;
|
|
9
|
+
constructor(message: string, opts?: ErrorOptions);
|
|
10
|
+
}
|
|
11
|
+
/** Recognizes the name and tag across SDK copies without requiring this class's identity. */
|
|
12
|
+
export declare function isSandboxPermissionUnsupportedError(e: unknown): e is SandboxPermissionUnsupportedError;
|
|
13
|
+
/** The requested operation is outside the permitted scope. */
|
|
14
|
+
export declare class SandboxScopeDeniedError extends Error {
|
|
15
|
+
readonly sandboxScopeDenied: true;
|
|
16
|
+
constructor(message: string, opts?: ErrorOptions);
|
|
17
|
+
}
|
|
18
|
+
/** Recognizes the name and tag across SDK copies without requiring this class's identity. */
|
|
19
|
+
export declare function isSandboxScopeDeniedError(e: unknown): e is SandboxScopeDeniedError;
|
|
20
|
+
/** A scope or execution reference is malformed; validate it before performing any effects. */
|
|
21
|
+
export declare class SandboxScopeValidationError extends Error {
|
|
22
|
+
readonly sandboxScopeValidation: true;
|
|
23
|
+
constructor(message: string, opts?: ErrorOptions);
|
|
24
|
+
}
|
|
25
|
+
/** Recognizes validation errors across SDK copies. */
|
|
26
|
+
export declare function isSandboxScopeValidationError(e: unknown): e is SandboxScopeValidationError;
|
|
27
|
+
/** An operation was consumed but its outcome is unknown. Never automatically retry with a new ID. */
|
|
28
|
+
export declare class SandboxOperationOutcomeUnknownError extends Error {
|
|
29
|
+
readonly sandboxOperationOutcomeUnknown: true;
|
|
30
|
+
constructor(message: string, opts?: ErrorOptions);
|
|
31
|
+
}
|
|
32
|
+
/** Recognizes unknown outcomes across SDK copies; an unknown outcome is not a gone instance. */
|
|
33
|
+
export declare function isSandboxOperationOutcomeUnknownError(e: unknown): e is SandboxOperationOutcomeUnknownError;
|
|
34
|
+
/** Writable roots are also readable. Device scope uses the device's OS authority, never its host's. */
|
|
35
|
+
export type SandboxFileScope = {
|
|
36
|
+
readonly kind: "scoped";
|
|
37
|
+
readonly readRoots: readonly string[];
|
|
38
|
+
readonly writeRoots: readonly string[];
|
|
39
|
+
} | {
|
|
40
|
+
readonly kind: "device";
|
|
41
|
+
};
|
|
42
|
+
/** Environment removes only the agent-specific filter; owner/org/provider ceilings still apply. */
|
|
43
|
+
export type SandboxNetworkScope = {
|
|
44
|
+
readonly kind: "none";
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: "allowlist";
|
|
47
|
+
readonly hosts: readonly string[];
|
|
48
|
+
} | {
|
|
49
|
+
readonly kind: "environment";
|
|
50
|
+
};
|
|
51
|
+
/** An operation locator for idempotent admission and revocation, never a credential. */
|
|
52
|
+
export interface SandboxOperationRef {
|
|
53
|
+
readonly operationId: string;
|
|
54
|
+
/** Absolute launch-admission deadline; does not shorten an already started execution's timeout. */
|
|
55
|
+
readonly admitBeforeMs: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A fresh grant for one method invocation. Providers must copy and freeze the input before use.
|
|
59
|
+
* Full access normally uses device/environment; explicit bounded grants may expand other profiles.
|
|
60
|
+
* Validation does not resolve authority or infer a grant from the profile name.
|
|
61
|
+
*/
|
|
62
|
+
export interface SandboxOperationScope extends SandboxOperationRef {
|
|
63
|
+
readonly deviceId: string;
|
|
64
|
+
readonly rootConversationId: string;
|
|
65
|
+
readonly profileRevision: number;
|
|
66
|
+
readonly ceilingRevision: string;
|
|
67
|
+
readonly profile: PermissionProfile;
|
|
68
|
+
readonly actor: Readonly<SandboxActor>;
|
|
69
|
+
readonly tag: string;
|
|
70
|
+
readonly filesystem: SandboxFileScope;
|
|
71
|
+
readonly network: SandboxNetworkScope;
|
|
72
|
+
}
|
|
73
|
+
/** Runtime support, not consent. Restricted support requires all four claims together. */
|
|
74
|
+
export interface SandboxPermissionSupport {
|
|
75
|
+
readonly version: 1;
|
|
76
|
+
readonly restricted: {
|
|
77
|
+
readonly filesystem: true;
|
|
78
|
+
readonly network: true;
|
|
79
|
+
readonly native: true;
|
|
80
|
+
readonly concurrentScopes: true;
|
|
81
|
+
} | null;
|
|
82
|
+
readonly fullAccess: boolean;
|
|
83
|
+
readonly background: boolean;
|
|
84
|
+
readonly unavailableReason?: string;
|
|
85
|
+
}
|
|
86
|
+
/** Actor and tag come only from the immutable operation scope. */
|
|
87
|
+
export type SandboxExecOptions = Omit<Parameters<SandboxProvider["exec"]>[1], "tag" | "actor">;
|
|
88
|
+
/**
|
|
89
|
+
* A lightweight factory result: no worker opens until a method is called. Factory and method
|
|
90
|
+
* signals jointly end the operation. Every method consumes its operation ID before effects;
|
|
91
|
+
* a matching retry returns the recorded result or SandboxOperationOutcomeUnknownError, never
|
|
92
|
+
* repeats effects. Missing operation support has no fallback to the provider's raw methods.
|
|
93
|
+
* Native results and exec abort/timeout/output semantics match the existing provider contract.
|
|
94
|
+
*/
|
|
95
|
+
export interface SandboxOperation {
|
|
96
|
+
exec(args: SandboxExecOptions): Promise<ExecResult>;
|
|
97
|
+
readFile(path: string, opts: {
|
|
98
|
+
maxBytes: number;
|
|
99
|
+
signal: AbortSignal;
|
|
100
|
+
}): Promise<{
|
|
101
|
+
data: Uint8Array;
|
|
102
|
+
truncated: boolean;
|
|
103
|
+
} | null>;
|
|
104
|
+
writeFile(path: string, data: Uint8Array, signal: AbortSignal): Promise<void>;
|
|
105
|
+
stat(path: string, signal: AbortSignal): Promise<{
|
|
106
|
+
size: number;
|
|
107
|
+
isDir: boolean;
|
|
108
|
+
} | null>;
|
|
109
|
+
/** List and scan are optional together; without both, projects are unavailable. */
|
|
110
|
+
list?(path: string, opts: {
|
|
111
|
+
limit: number;
|
|
112
|
+
signal: AbortSignal;
|
|
113
|
+
}): Promise<{
|
|
114
|
+
entries: {
|
|
115
|
+
name: string;
|
|
116
|
+
isDir: boolean;
|
|
117
|
+
}[];
|
|
118
|
+
truncated: boolean;
|
|
119
|
+
} | null>;
|
|
120
|
+
scan?(base: string, opts: {
|
|
121
|
+
signal: AbortSignal;
|
|
122
|
+
}): Promise<{
|
|
123
|
+
path: string;
|
|
124
|
+
suggestedName: string;
|
|
125
|
+
}[]>;
|
|
126
|
+
}
|
|
127
|
+
/** Trusted staging only: providers validate purpose-specific roots; user tools never supply this grant. */
|
|
128
|
+
export interface SandboxPreparationScope {
|
|
129
|
+
readonly operationId: string;
|
|
130
|
+
readonly deviceId: string;
|
|
131
|
+
readonly purpose: "skills" | "attachments";
|
|
132
|
+
readonly writeRoots: readonly string[];
|
|
133
|
+
readonly tag: string;
|
|
134
|
+
}
|
|
135
|
+
/** Marker reads and writes inside trusted preparation roots, with no exec or network capability. */
|
|
136
|
+
export type SandboxPreparation = Pick<SandboxOperation, "readFile" | "writeFile" | "stat">;
|
|
137
|
+
/** Owner-authenticated browsing, independent of a conversation or its profile/consent. */
|
|
138
|
+
export interface SandboxBrowseScope extends SandboxOperationRef {
|
|
139
|
+
readonly deviceId: string;
|
|
140
|
+
readonly accountId: string;
|
|
141
|
+
readonly ceilingRevision: string;
|
|
142
|
+
readonly readRoots: readonly string[];
|
|
143
|
+
readonly tag: string;
|
|
144
|
+
}
|
|
145
|
+
/** Browsing exposes metadata only: no file contents, writes, preparation or execution. */
|
|
146
|
+
export type SandboxBrowse = Pick<SandboxOperation, "stat"> & Required<Pick<SandboxOperation, "list" | "scan">>;
|
|
147
|
+
/** An opaque locator over authenticated transport; validate generation before status, tail or stop. */
|
|
148
|
+
export interface SandboxExecutionRef {
|
|
149
|
+
readonly executionId: string;
|
|
150
|
+
readonly operationId: string;
|
|
151
|
+
readonly instanceId: string;
|
|
152
|
+
readonly instanceGeneration: string;
|
|
153
|
+
}
|
|
154
|
+
/** A lost supervisor never binds this reference to a reused PID. */
|
|
155
|
+
export type SandboxExecutionStatus = {
|
|
156
|
+
state: "running";
|
|
157
|
+
} | {
|
|
158
|
+
state: "exited";
|
|
159
|
+
exitCode: number | null;
|
|
160
|
+
timedOut: boolean;
|
|
161
|
+
} | {
|
|
162
|
+
state: "lost";
|
|
163
|
+
};
|
|
164
|
+
/** Atomically claimed terminal outcome; retries preserve it, including a prior natural exit. */
|
|
165
|
+
export type SandboxStopResult = {
|
|
166
|
+
outcome: "already-exited";
|
|
167
|
+
exitCode: number | null;
|
|
168
|
+
timedOut: boolean;
|
|
169
|
+
} | {
|
|
170
|
+
outcome: "stopped";
|
|
171
|
+
} | {
|
|
172
|
+
outcome: "cancel-pending";
|
|
173
|
+
} | {
|
|
174
|
+
outcome: "lost";
|
|
175
|
+
};
|
|
176
|
+
/** Trusted exact-execution supervision. Status, tail and stop never provision or resume an instance. */
|
|
177
|
+
export interface SandboxBackground {
|
|
178
|
+
/**
|
|
179
|
+
* Persist exact ownership before acknowledging; retries of the same generation/operation/input
|
|
180
|
+
* return the same reference. Retain starting grants for the lifetime of the owned execution.
|
|
181
|
+
* Validate outputPath inside the scope. retainUntilMs bounds status/output record retention.
|
|
182
|
+
*/
|
|
183
|
+
start(h: SandboxHandle, scope: SandboxOperationScope, args: SandboxExecOptions & {
|
|
184
|
+
taskId: string;
|
|
185
|
+
outputPath: string;
|
|
186
|
+
retainUntilMs: number;
|
|
187
|
+
}): Promise<SandboxExecutionRef>;
|
|
188
|
+
status(h: SandboxHandle, ref: SandboxExecutionRef, signal: AbortSignal): Promise<SandboxExecutionStatus>;
|
|
189
|
+
/** At most 65,536 bytes, preserving UTF-8 boundaries and existing final-line limits; no caller path. */
|
|
190
|
+
tail(h: SandboxHandle, ref: SandboxExecutionRef, opts: {
|
|
191
|
+
maxBytes: number;
|
|
192
|
+
signal: AbortSignal;
|
|
193
|
+
}): Promise<{
|
|
194
|
+
data: Uint8Array;
|
|
195
|
+
truncated: boolean;
|
|
196
|
+
} | {
|
|
197
|
+
unavailable: "lost" | "expired";
|
|
198
|
+
}>;
|
|
199
|
+
/**
|
|
200
|
+
* Read retained raw merged-output bytes by exact reference, without a caller path, new scope,
|
|
201
|
+
* exec, provisioning or resume. Validate before I/O: offset is a nonnegative safe integer;
|
|
202
|
+
* maxBytes is an integer in [0, 65_536], including zero for a metadata-only probe. Invalid
|
|
203
|
+
* bounds throw SandboxScopeDeniedError. The byte budget includes any caller-requested overlap.
|
|
204
|
+
*
|
|
205
|
+
* totalBytes is one committed append-only prefix snapshot pinned at read admission. Return
|
|
206
|
+
* exactly min(maxBytes, max(totalBytes - offset, 0)) bytes starting at the requested offset,
|
|
207
|
+
* without UTF-8 trimming or decoding; retry short reads or report lost. nextOffset equals
|
|
208
|
+
* offset + data.byteLength. At/past the snapshot end, return empty data with unchanged offset.
|
|
209
|
+
* Reads are non-destructive and repeatable; concurrent appends cannot change returned bytes
|
|
210
|
+
* or this snapshot. Safe-integer position overflow fails collection rather than rounding.
|
|
211
|
+
*
|
|
212
|
+
* Missing/untrustworthy retained storage returns lost; a known expired record returns expired.
|
|
213
|
+
* Exact-ref/device/generation mismatches remain scope-denied errors; aborts propagate.
|
|
214
|
+
* Unlike tail, terminal reads retain every line. Current task access checks still apply.
|
|
215
|
+
*/
|
|
216
|
+
readOutput(h: SandboxHandle, ref: SandboxExecutionRef, opts: {
|
|
217
|
+
offset: number;
|
|
218
|
+
maxBytes: number;
|
|
219
|
+
signal: AbortSignal;
|
|
220
|
+
}): Promise<{
|
|
221
|
+
data: Uint8Array;
|
|
222
|
+
offset: number;
|
|
223
|
+
nextOffset: number;
|
|
224
|
+
totalBytes: number;
|
|
225
|
+
} | {
|
|
226
|
+
unavailable: "lost" | "expired";
|
|
227
|
+
}>;
|
|
228
|
+
/** Persist pending cancellation while unreachable; apply it before an explicit resume can run work. */
|
|
229
|
+
stop(h: SandboxHandle, ref: SandboxExecutionRef, opts: {
|
|
230
|
+
graceMs: number;
|
|
231
|
+
signal: AbortSignal;
|
|
232
|
+
}): Promise<SandboxStopResult>;
|
|
233
|
+
}
|
|
234
|
+
/** Pure structural validation and a frozen copy; no clock lookup or authority resolution. */
|
|
235
|
+
export declare function parseSandboxOperationRef(value: unknown): SandboxOperationRef;
|
|
236
|
+
/**
|
|
237
|
+
* Validates a scope and returns a deeply frozen copy, throwing SandboxScopeValidationError.
|
|
238
|
+
* IDs and ceiling revisions are bounded to 128 characters; roots to 64 paths of at most 4096 characters;
|
|
239
|
+
* hosts to 100 entries passing validateAllowHost. Empty grants stay empty, including deny-all.
|
|
240
|
+
* Performs no I/O, authority lookup or deadline refresh. Unknown fields are rejected at every level.
|
|
241
|
+
*/
|
|
242
|
+
export declare function parseSandboxOperationScope(value: unknown): SandboxOperationScope;
|
|
243
|
+
/** Validates an owner browsing grant and returns a deeply frozen copy; no conversation is required. */
|
|
244
|
+
export declare function parseSandboxBrowseScope(value: unknown): SandboxBrowseScope;
|
|
245
|
+
/** Validates trusted staging structure and returns a deeply frozen copy; does not authorize its roots. */
|
|
246
|
+
export declare function parseSandboxPreparationScope(value: unknown): SandboxPreparationScope;
|
|
247
|
+
/** Validates all four bounded locator fields and returns a frozen copy; does not authenticate the caller. */
|
|
248
|
+
export declare function parseSandboxExecutionRef(value: unknown): SandboxExecutionRef;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { SANDBOX_TAG_RE, validateAllowHost, validateSandboxPathShape, } from "./sandbox.js";
|
|
2
|
+
/** Accepts only the supported permission profile names. */
|
|
3
|
+
export function isPermissionProfile(value) {
|
|
4
|
+
return value === "request-approval" || value === "approve-for-me" || value === "full-access";
|
|
5
|
+
}
|
|
6
|
+
/** The provider cannot enforce the requested permission profile. */
|
|
7
|
+
export class SandboxPermissionUnsupportedError extends Error {
|
|
8
|
+
sandboxPermissionUnsupported = true;
|
|
9
|
+
constructor(message, opts) {
|
|
10
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
11
|
+
this.name = "SandboxPermissionUnsupportedError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Recognizes the name and tag across SDK copies without requiring this class's identity. */
|
|
15
|
+
export function isSandboxPermissionUnsupportedError(e) {
|
|
16
|
+
return (e instanceof Error &&
|
|
17
|
+
e.name === "SandboxPermissionUnsupportedError" &&
|
|
18
|
+
e.sandboxPermissionUnsupported === true);
|
|
19
|
+
}
|
|
20
|
+
/** The requested operation is outside the permitted scope. */
|
|
21
|
+
export class SandboxScopeDeniedError extends Error {
|
|
22
|
+
sandboxScopeDenied = true;
|
|
23
|
+
constructor(message, opts) {
|
|
24
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
25
|
+
this.name = "SandboxScopeDeniedError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Recognizes the name and tag across SDK copies without requiring this class's identity. */
|
|
29
|
+
export function isSandboxScopeDeniedError(e) {
|
|
30
|
+
return (e instanceof Error &&
|
|
31
|
+
e.name === "SandboxScopeDeniedError" &&
|
|
32
|
+
e.sandboxScopeDenied === true);
|
|
33
|
+
}
|
|
34
|
+
/** A scope or execution reference is malformed; validate it before performing any effects. */
|
|
35
|
+
export class SandboxScopeValidationError extends Error {
|
|
36
|
+
sandboxScopeValidation = true;
|
|
37
|
+
constructor(message, opts) {
|
|
38
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
39
|
+
this.name = "SandboxScopeValidationError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Recognizes validation errors across SDK copies. */
|
|
43
|
+
export function isSandboxScopeValidationError(e) {
|
|
44
|
+
return (e instanceof Error &&
|
|
45
|
+
e.name === "SandboxScopeValidationError" &&
|
|
46
|
+
e.sandboxScopeValidation === true);
|
|
47
|
+
}
|
|
48
|
+
/** An operation was consumed but its outcome is unknown. Never automatically retry with a new ID. */
|
|
49
|
+
export class SandboxOperationOutcomeUnknownError extends Error {
|
|
50
|
+
sandboxOperationOutcomeUnknown = true;
|
|
51
|
+
constructor(message, opts) {
|
|
52
|
+
super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
|
|
53
|
+
this.name = "SandboxOperationOutcomeUnknownError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Recognizes unknown outcomes across SDK copies; an unknown outcome is not a gone instance. */
|
|
57
|
+
export function isSandboxOperationOutcomeUnknownError(e) {
|
|
58
|
+
return (e instanceof Error &&
|
|
59
|
+
e.name === "SandboxOperationOutcomeUnknownError" &&
|
|
60
|
+
e.sandboxOperationOutcomeUnknown === true);
|
|
61
|
+
}
|
|
62
|
+
function invalid(field, requirement) {
|
|
63
|
+
throw new SandboxScopeValidationError(`Invalid ${field}: ${requirement}. Supply a valid scope or reference before retrying.`);
|
|
64
|
+
}
|
|
65
|
+
function record(value, keys, field) {
|
|
66
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
67
|
+
return invalid(field, "expected an object");
|
|
68
|
+
}
|
|
69
|
+
const prototype = Object.getPrototypeOf(value);
|
|
70
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
71
|
+
return invalid(field, "expected a plain object");
|
|
72
|
+
const result = Object.create(null);
|
|
73
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
74
|
+
if (typeof key !== "string" || !keys.includes(key))
|
|
75
|
+
return invalid(field, "unknown fields are not allowed");
|
|
76
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
77
|
+
if (!descriptor || !("value" in descriptor))
|
|
78
|
+
return invalid(field, "accessor fields are not allowed");
|
|
79
|
+
result[key] = descriptor.value;
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
function id(value, field) {
|
|
84
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 128) {
|
|
85
|
+
return invalid(field, "expected a nonempty string of at most 128 characters");
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
function integer(value, minimum, field) {
|
|
90
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum) {
|
|
91
|
+
return invalid(field, `expected a safe integer greater than or equal to ${minimum}`);
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
function tag(value) {
|
|
96
|
+
if (typeof value !== "string" || !SANDBOX_TAG_RE.test(value))
|
|
97
|
+
return invalid("tag", "expected 1–64 lowercase letters, digits or hyphens");
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
function strings(value, maximum, check, field) {
|
|
101
|
+
if (!Array.isArray(value) || value.length > maximum)
|
|
102
|
+
return invalid(field, `expected an array of at most ${maximum} entries`);
|
|
103
|
+
if (Reflect.ownKeys(value).length !== value.length + 1)
|
|
104
|
+
return invalid(field, "sparse arrays and extra fields are not allowed");
|
|
105
|
+
const result = [];
|
|
106
|
+
for (let i = 0; i < value.length; i++) {
|
|
107
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, i);
|
|
108
|
+
if (!descriptor || !("value" in descriptor))
|
|
109
|
+
return invalid(field, "expected array data entries");
|
|
110
|
+
const item = descriptor.value;
|
|
111
|
+
if (typeof item !== "string")
|
|
112
|
+
return invalid(field, "expected string entries");
|
|
113
|
+
const errors = check(item);
|
|
114
|
+
if (errors.length > 0)
|
|
115
|
+
return invalid(field, errors.join("; "));
|
|
116
|
+
result.push(item);
|
|
117
|
+
}
|
|
118
|
+
return Object.freeze(result);
|
|
119
|
+
}
|
|
120
|
+
function roots(value, field) {
|
|
121
|
+
return strings(value, 64, validateSandboxPathShape, field);
|
|
122
|
+
}
|
|
123
|
+
function fileScope(value) {
|
|
124
|
+
const v = record(value, ["kind", "readRoots", "writeRoots"], "filesystem");
|
|
125
|
+
if (v.kind === "device") {
|
|
126
|
+
record(value, ["kind"], "filesystem");
|
|
127
|
+
return Object.freeze({ kind: "device" });
|
|
128
|
+
}
|
|
129
|
+
if (v.kind !== "scoped")
|
|
130
|
+
return invalid("filesystem.kind", "expected scoped or device");
|
|
131
|
+
return Object.freeze({
|
|
132
|
+
kind: "scoped",
|
|
133
|
+
readRoots: roots(v.readRoots, "filesystem.readRoots"),
|
|
134
|
+
writeRoots: roots(v.writeRoots, "filesystem.writeRoots"),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function networkScope(value) {
|
|
138
|
+
const v = record(value, ["kind", "hosts"], "network");
|
|
139
|
+
if (v.kind === "none" || v.kind === "environment") {
|
|
140
|
+
record(value, ["kind"], "network");
|
|
141
|
+
return Object.freeze({ kind: v.kind });
|
|
142
|
+
}
|
|
143
|
+
if (v.kind !== "allowlist")
|
|
144
|
+
return invalid("network.kind", "expected none, allowlist or environment");
|
|
145
|
+
return Object.freeze({
|
|
146
|
+
kind: "allowlist",
|
|
147
|
+
hosts: strings(v.hosts, 100, validateAllowHost, "network.hosts"),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function actor(value) {
|
|
151
|
+
const v = record(value, ["accountId", "conversationId", "runId"], "actor");
|
|
152
|
+
return Object.freeze({
|
|
153
|
+
accountId: id(v.accountId, "actor.accountId"),
|
|
154
|
+
conversationId: id(v.conversationId, "actor.conversationId"),
|
|
155
|
+
...(Object.hasOwn(v, "runId") ? { runId: id(v.runId, "actor.runId") } : {}),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function operationRef(v) {
|
|
159
|
+
return {
|
|
160
|
+
operationId: id(v.operationId, "operationId"),
|
|
161
|
+
admitBeforeMs: integer(v.admitBeforeMs, 1, "admitBeforeMs"),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/** Pure structural validation and a frozen copy; no clock lookup or authority resolution. */
|
|
165
|
+
export function parseSandboxOperationRef(value) {
|
|
166
|
+
return Object.freeze(operationRef(record(value, ["operationId", "admitBeforeMs"], "operation reference")));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Validates a scope and returns a deeply frozen copy, throwing SandboxScopeValidationError.
|
|
170
|
+
* IDs and ceiling revisions are bounded to 128 characters; roots to 64 paths of at most 4096 characters;
|
|
171
|
+
* hosts to 100 entries passing validateAllowHost. Empty grants stay empty, including deny-all.
|
|
172
|
+
* Performs no I/O, authority lookup or deadline refresh. Unknown fields are rejected at every level.
|
|
173
|
+
*/
|
|
174
|
+
export function parseSandboxOperationScope(value) {
|
|
175
|
+
const v = record(value, [
|
|
176
|
+
"operationId",
|
|
177
|
+
"admitBeforeMs",
|
|
178
|
+
"deviceId",
|
|
179
|
+
"rootConversationId",
|
|
180
|
+
"profileRevision",
|
|
181
|
+
"ceilingRevision",
|
|
182
|
+
"profile",
|
|
183
|
+
"actor",
|
|
184
|
+
"tag",
|
|
185
|
+
"filesystem",
|
|
186
|
+
"network",
|
|
187
|
+
], "operation scope");
|
|
188
|
+
if (!isPermissionProfile(v.profile))
|
|
189
|
+
return invalid("profile", "expected request-approval, approve-for-me or full-access");
|
|
190
|
+
return Object.freeze({
|
|
191
|
+
...operationRef(v),
|
|
192
|
+
deviceId: id(v.deviceId, "deviceId"),
|
|
193
|
+
rootConversationId: id(v.rootConversationId, "rootConversationId"),
|
|
194
|
+
profileRevision: integer(v.profileRevision, 0, "profileRevision"),
|
|
195
|
+
ceilingRevision: id(v.ceilingRevision, "ceilingRevision"),
|
|
196
|
+
profile: v.profile,
|
|
197
|
+
actor: actor(v.actor),
|
|
198
|
+
tag: tag(v.tag),
|
|
199
|
+
filesystem: fileScope(v.filesystem),
|
|
200
|
+
network: networkScope(v.network),
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/** Validates an owner browsing grant and returns a deeply frozen copy; no conversation is required. */
|
|
204
|
+
export function parseSandboxBrowseScope(value) {
|
|
205
|
+
const v = record(value, [
|
|
206
|
+
"operationId",
|
|
207
|
+
"admitBeforeMs",
|
|
208
|
+
"deviceId",
|
|
209
|
+
"accountId",
|
|
210
|
+
"ceilingRevision",
|
|
211
|
+
"readRoots",
|
|
212
|
+
"tag",
|
|
213
|
+
], "browse scope");
|
|
214
|
+
return Object.freeze({
|
|
215
|
+
...operationRef(v),
|
|
216
|
+
deviceId: id(v.deviceId, "deviceId"),
|
|
217
|
+
accountId: id(v.accountId, "accountId"),
|
|
218
|
+
ceilingRevision: id(v.ceilingRevision, "ceilingRevision"),
|
|
219
|
+
readRoots: roots(v.readRoots, "readRoots"),
|
|
220
|
+
tag: tag(v.tag),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/** Validates trusted staging structure and returns a deeply frozen copy; does not authorize its roots. */
|
|
224
|
+
export function parseSandboxPreparationScope(value) {
|
|
225
|
+
const v = record(value, ["operationId", "deviceId", "purpose", "writeRoots", "tag"], "preparation scope");
|
|
226
|
+
if (v.purpose !== "skills" && v.purpose !== "attachments")
|
|
227
|
+
return invalid("purpose", "expected skills or attachments");
|
|
228
|
+
return Object.freeze({
|
|
229
|
+
operationId: id(v.operationId, "operationId"),
|
|
230
|
+
deviceId: id(v.deviceId, "deviceId"),
|
|
231
|
+
purpose: v.purpose,
|
|
232
|
+
writeRoots: roots(v.writeRoots, "writeRoots"),
|
|
233
|
+
tag: tag(v.tag),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/** Validates all four bounded locator fields and returns a frozen copy; does not authenticate the caller. */
|
|
237
|
+
export function parseSandboxExecutionRef(value) {
|
|
238
|
+
const v = record(value, ["executionId", "operationId", "instanceId", "instanceGeneration"], "execution reference");
|
|
239
|
+
return Object.freeze({
|
|
240
|
+
executionId: id(v.executionId, "executionId"),
|
|
241
|
+
operationId: id(v.operationId, "operationId"),
|
|
242
|
+
instanceId: id(v.instanceId, "instanceId"),
|
|
243
|
+
instanceGeneration: id(v.instanceGeneration, "instanceGeneration"),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable portable native helper bytes for trusted consumer staging (requires Python 3).
|
|
3
|
+
* Consumers verify hashes before use and supply pinned, already-authorized root descriptors.
|
|
4
|
+
* This bundle declares no restricted-runtime support; enforcement and launch belong to providers.
|
|
5
|
+
*/
|
|
6
|
+
export declare const SANDBOX_RUNTIME_SOURCE: {
|
|
7
|
+
readonly version: number;
|
|
8
|
+
readonly files: readonly {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly source: string;
|
|
11
|
+
readonly sha256: string;
|
|
12
|
+
}[];
|
|
13
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { NATIVE_FILES_SOURCE } from "./sandbox-native-runtime-source.js";
|
|
2
|
+
/**
|
|
3
|
+
* Immutable portable native helper bytes for trusted consumer staging (requires Python 3).
|
|
4
|
+
* Consumers verify hashes before use and supply pinned, already-authorized root descriptors.
|
|
5
|
+
* This bundle declares no restricted-runtime support; enforcement and launch belong to providers.
|
|
6
|
+
*/
|
|
7
|
+
export const SANDBOX_RUNTIME_SOURCE = Object.freeze({
|
|
8
|
+
version: 1,
|
|
9
|
+
files: Object.freeze([
|
|
10
|
+
Object.freeze({
|
|
11
|
+
name: "native-files.py",
|
|
12
|
+
source: NATIVE_FILES_SOURCE,
|
|
13
|
+
sha256: "601f51c1c46bedac5ab56f7ca38d7c15fdea8481efc6476d5a5b3243318ef79b",
|
|
14
|
+
}),
|
|
15
|
+
]),
|
|
16
|
+
});
|
package/dist/sandbox.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* (`createFileSystem`) lives until the device is deleted (`destroyFileSystem`); instances
|
|
9
9
|
* (`create`) attach to it and come and go (`destroy` drops the instance and keeps the files).
|
|
10
10
|
*/
|
|
11
|
+
import type { SandboxBackground, SandboxBrowse, SandboxBrowseScope, SandboxOperation, SandboxOperationRef, SandboxOperationScope, SandboxPermissionSupport, SandboxPreparation, SandboxPreparationScope } from "./sandbox-operation.js";
|
|
11
12
|
/** Sandbox extension point ref. */
|
|
12
13
|
export declare const SANDBOX_EXTENSION_POINT = "sandbox@2";
|
|
13
14
|
/** The roots inside the sandbox the model may access. */
|
|
@@ -23,6 +24,10 @@ export interface SandboxLimits {
|
|
|
23
24
|
/**
|
|
24
25
|
* `auto`: no OS sandbox (local: the owner; cloud: the container is the boundary);
|
|
25
26
|
* `sandbox`: the OS sandbox with the network limited by {@link SandboxLimits.network}.
|
|
27
|
+
* For operation-capable providers this encodes the independent device ceiling: auto allows
|
|
28
|
+
* Full access when separately authorized, sandbox permits restricted operations only.
|
|
29
|
+
* It never selects a conversation profile or changes per operation. Legacy consumers retain
|
|
30
|
+
* the current mode behavior until coordinated protocol activation.
|
|
26
31
|
*/
|
|
27
32
|
mode: "auto" | "sandbox";
|
|
28
33
|
/**
|
|
@@ -172,6 +177,25 @@ export declare const SANDBOX_TAG_RE: RegExp;
|
|
|
172
177
|
* aborted `exec` kills only its own command.
|
|
173
178
|
*/
|
|
174
179
|
export interface SandboxProvider {
|
|
180
|
+
/** Runtime permission support for this instance; does not grant consent or provision an instance. */
|
|
181
|
+
permissionSupport?(h: SandboxHandle, signal: AbortSignal): Promise<SandboxPermissionSupport>;
|
|
182
|
+
/**
|
|
183
|
+
* Fresh immutable grant for one actual method invocation. Lightweight until a method is called;
|
|
184
|
+
* never cache grants on the handle or fall back to raw methods when unsupported.
|
|
185
|
+
*/
|
|
186
|
+
operation?(h: SandboxHandle, scope: SandboxOperationScope, signal: AbortSignal): Promise<SandboxOperation>;
|
|
187
|
+
/** Trusted skill/attachment staging only; never exposed to user operations. */
|
|
188
|
+
prepare?(h: SandboxHandle, scope: SandboxPreparationScope, signal: AbortSignal): Promise<SandboxPreparation>;
|
|
189
|
+
/** Owner-authenticated metadata browsing without conversation/profile consent. */
|
|
190
|
+
browse?(h: SandboxHandle, scope: SandboxBrowseScope, signal: AbortSignal): Promise<SandboxBrowse>;
|
|
191
|
+
/**
|
|
192
|
+
* Required for permission support: idempotently persist an operation-ID tombstone before
|
|
193
|
+
* cancelling exact owned work. A delayed launch checks it before effects; retain it through
|
|
194
|
+
* admitBeforeMs and any owned execution's lifetime, including across kernel restarts.
|
|
195
|
+
*/
|
|
196
|
+
revokeOperation?(h: SandboxHandle, ref: SandboxOperationRef, signal: AbortSignal): Promise<void>;
|
|
197
|
+
/** Exact registered background ownership; missing support must not fall back to shell launch. */
|
|
198
|
+
background?: SandboxBackground;
|
|
175
199
|
/** `exec` / `readFile` / `writeFile` / `stat` may run concurrently on one instance (required). */
|
|
176
200
|
readonly concurrentExec: true;
|
|
177
201
|
/**
|
|
@@ -288,7 +312,92 @@ export interface SandboxProvider {
|
|
|
288
312
|
size: number;
|
|
289
313
|
isDir: boolean;
|
|
290
314
|
} | null>;
|
|
315
|
+
/**
|
|
316
|
+
* Lists one directory's entries — each entry's `name` and whether it `isDir` — sorted by code
|
|
317
|
+
* unit (locale-independent), at most `limit` of them; `truncated` is true when the directory has
|
|
318
|
+
* more. Entries only: never file contents, never a recursive walk. A `name` is never `.` or `..`;
|
|
319
|
+
* `isDir` follows symbolic links (stat semantics: a link to a directory is `true`, a dangling link
|
|
320
|
+
* `false`, and so is a link whose target the request's mode cannot reach). `limits` is the kernel's current ceiling: read in the mode it gives, never in one an
|
|
321
|
+
* earlier command saw. Returns null only when the path does not exist and that mode could reach
|
|
322
|
+
* it; a path the mode cannot reach throws (a policy refusal), as does a path that is a file or a
|
|
323
|
+
* directory that cannot be read.
|
|
324
|
+
*
|
|
325
|
+
* Optional: a provider without it (or without {@link SandboxProvider.scan}) offers no projects,
|
|
326
|
+
* and its conversations stay in free mode.
|
|
327
|
+
*/
|
|
328
|
+
list?(h: SandboxHandle, path: string, opts: {
|
|
329
|
+
limit: number;
|
|
330
|
+
limits: Pick<SandboxLimits, "mode">;
|
|
331
|
+
signal: AbortSignal;
|
|
332
|
+
}): Promise<{
|
|
333
|
+
entries: {
|
|
334
|
+
name: string;
|
|
335
|
+
isDir: boolean;
|
|
336
|
+
}[];
|
|
337
|
+
truncated: boolean;
|
|
338
|
+
} | null>;
|
|
339
|
+
/**
|
|
340
|
+
* Looks for project-like directories, read in the mode `limits` gives (as for
|
|
341
|
+
* {@link SandboxProvider.list}). Every provider traverses the same way:
|
|
342
|
+
*
|
|
343
|
+
* - Candidates are `base` itself and the directories at most {@link MAX_DEVICE_SCAN_DEPTH}
|
|
344
|
+
* levels below it that hold a `.git` entry (directory or file) or an `AGENTS.md` file.
|
|
345
|
+
* - The walk does not follow symbolic links and does not enter hidden directories (names
|
|
346
|
+
* starting with `.`); a candidate does not stop the descent (a monorepo's sub-projects are
|
|
347
|
+
* candidates too). It never enters a directory named in {@link DEVICE_SCAN_SKIPPED_DIRS}, at
|
|
348
|
+
* any depth, nor one named in {@link DEVICE_SCAN_SKIPPED_BASE_DIRS} directly under `base`;
|
|
349
|
+
* every provider skips exactly these. Paths under `base` the mode cannot reach are skipped,
|
|
350
|
+
* never an error.
|
|
351
|
+
* - A directory whose basename does not satisfy {@link isDeviceProjectName} is not returned as
|
|
352
|
+
* a candidate (never with a changed name), but it is still traversed.
|
|
353
|
+
* - Directories are visited breadth-first, each level in code-unit order of `path`, `base`
|
|
354
|
+
* counting as the first visit (visiting a directory reads its entries; one that cannot be read
|
|
355
|
+
* still counts as a visit); the walk stops after {@link MAX_DEVICE_SCAN_DIRS} visits, so a
|
|
356
|
+
* huge tree answers what was found by then.
|
|
357
|
+
* - Each candidate is its absolute `path` and its basename as `suggestedName`; the candidates
|
|
358
|
+
* found are sorted by `path` in code-unit order and cut to the first
|
|
359
|
+
* {@link MAX_DEVICE_SCAN_RESULTS}.
|
|
360
|
+
*
|
|
361
|
+
* Directory entries only, never file contents. A `base` that does not exist answers `[]`; a
|
|
362
|
+
* `base` that is a file, cannot be read, or lies outside the mode throws. Optional, like `list`.
|
|
363
|
+
*/
|
|
364
|
+
scan?(h: SandboxHandle, base: string, opts: {
|
|
365
|
+
limits: Pick<SandboxLimits, "mode">;
|
|
366
|
+
signal: AbortSignal;
|
|
367
|
+
}): Promise<{
|
|
368
|
+
path: string;
|
|
369
|
+
suggestedName: string;
|
|
370
|
+
}[]>;
|
|
291
371
|
}
|
|
372
|
+
/** The longest project name, in UTF-16 code units. */
|
|
373
|
+
export declare const MAX_DEVICE_PROJECT_NAME_LENGTH = 64;
|
|
374
|
+
/** The most candidates a `scan` answers ({@link SandboxProvider.scan}). */
|
|
375
|
+
export declare const MAX_DEVICE_SCAN_RESULTS = 50;
|
|
376
|
+
/**
|
|
377
|
+
* Directory names a `scan` never enters, at any depth ({@link SandboxProvider.scan}): dependency
|
|
378
|
+
* trees, not projects. Every provider skips exactly these.
|
|
379
|
+
*/
|
|
380
|
+
export declare const DEVICE_SCAN_SKIPPED_DIRS: readonly string[];
|
|
381
|
+
/**
|
|
382
|
+
* Directory names a `scan` does not enter directly under its `base` ({@link SandboxProvider.scan}):
|
|
383
|
+
* the owner's macOS `Library` (app data, huge, partly privacy-protected) is not where projects
|
|
384
|
+
* live. Every provider skips exactly these.
|
|
385
|
+
*/
|
|
386
|
+
export declare const DEVICE_SCAN_SKIPPED_BASE_DIRS: readonly string[];
|
|
387
|
+
/** How far below its base a `scan` looks: the base's children and their children. */
|
|
388
|
+
export declare const MAX_DEVICE_SCAN_DEPTH = 2;
|
|
389
|
+
/**
|
|
390
|
+
* The most directories one `scan` visits (reads the entries of), its `base` included
|
|
391
|
+
* ({@link SandboxProvider.scan}): breadth-first, each level in code-unit order of path.
|
|
392
|
+
*/
|
|
393
|
+
export declare const MAX_DEVICE_SCAN_DIRS = 500;
|
|
394
|
+
/**
|
|
395
|
+
* Whether `v` is a valid project name (a project's identity on its device, and a `scan`
|
|
396
|
+
* candidate's `suggestedName`): non-empty, at most {@link MAX_DEVICE_PROJECT_NAME_LENGTH} UTF-16
|
|
397
|
+
* code units, not `.` or `..`, no `/`, no control characters, and no leading or trailing
|
|
398
|
+
* whitespace.
|
|
399
|
+
*/
|
|
400
|
+
export declare function isDeviceProjectName(v: unknown): v is string;
|
|
292
401
|
/**
|
|
293
402
|
* The root-independent checks of a path inside an instance (non-empty, at most 4096 characters, no
|
|
294
403
|
* control characters or backslashes, absolute, no empty or `..` segments); returns the errors,
|