@intentic/extension-api 1.431.0 → 1.432.0
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 +124 -4
- package/dist/api.d.ts +1 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/backend-reach.d.ts +23 -0
- package/dist/backend-reach.d.ts.map +1 -0
- package/dist/backend-reach.js +241 -0
- package/dist/backend-reach.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/runtime.d.ts +12 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +110 -132
- package/dist/runtime.js.map +1 -1
- package/dist/server-reach.d.ts +58 -0
- package/dist/server-reach.d.ts.map +1 -0
- package/dist/server-reach.js +2 -0
- package/dist/server-reach.js.map +1 -0
- package/dist/server.d.ts +186 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/sleep-fake.d.ts +15 -0
- package/dist/sleep-fake.d.ts.map +1 -0
- package/dist/sleep-fake.js +99 -0
- package/dist/sleep-fake.js.map +1 -0
- package/dist/sleep-reach.d.ts +7 -0
- package/dist/sleep-reach.d.ts.map +1 -0
- package/dist/sleep-reach.js +104 -0
- package/dist/sleep-reach.js.map +1 -0
- package/dist/testing.d.ts +64 -9
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +322 -13
- package/dist/testing.js.map +1 -1
- package/dist/turn-reach.d.ts +7 -0
- package/dist/turn-reach.d.ts.map +1 -0
- package/dist/turn-reach.js +140 -0
- package/dist/turn-reach.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +4 -4
- package/src/api.ts +3 -0
- package/src/backend-reach.integration.test.ts +154 -0
- package/src/backend-reach.ts +335 -0
- package/src/caller.integration.test.ts +108 -0
- package/src/index.ts +1 -0
- package/src/runtime.ts +184 -157
- package/src/server-reach.ts +104 -0
- package/src/server.ts +364 -3
- package/src/sleep-fake.ts +140 -0
- package/src/sleep-reach.integration.test.ts +119 -0
- package/src/sleep-reach.ts +147 -0
- package/src/surface.json +12741 -0
- package/src/testing.ts +506 -23
- package/src/turn-reach.integration.test.ts +166 -0
- package/src/turn-reach.ts +198 -0
- package/src/version.ts +110 -1
package/src/runtime.ts
CHANGED
|
@@ -1,44 +1,56 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { isAbsolute, relative, resolve } from "node:path";
|
|
4
|
-
import {
|
|
4
|
+
import { within } from "@intentic/base/async";
|
|
5
5
|
import { errorMessage, undefinedIfMissing } from "@intentic/base/errors";
|
|
6
6
|
import { FetchJsonError, fetchJson } from "@intentic/base/fetch-json";
|
|
7
7
|
import { queueOnFile, writeFileAtomic } from "@intentic/base/fs";
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
EXTENSION_FIRE_HANDLER,
|
|
10
|
+
type ExtensionDeclaredReach,
|
|
11
|
+
NO_DECLARED_REACH,
|
|
12
|
+
sandboxRouteAllowed,
|
|
13
|
+
} from "@intentic/extension-manifest/permissions";
|
|
10
14
|
import type * as SandboxContract from "@intentic/sandbox-contract";
|
|
11
15
|
import { readDocument } from "@intentic/sandbox-contract/documents";
|
|
12
16
|
import {
|
|
13
|
-
|
|
14
|
-
EXTENSION_EVENTS_HEARTBEAT_MS,
|
|
17
|
+
EXTENSION_CALLER_HEADER,
|
|
15
18
|
EXTENSION_PROCESS_ENV,
|
|
16
|
-
|
|
17
|
-
ExtensionEventSchema,
|
|
18
|
-
extensionEventsUrl,
|
|
19
|
+
ExtensionDeclaredReachSchema,
|
|
19
20
|
ExtensionHealthSchema,
|
|
20
21
|
ExtensionOwnSettingsSchema,
|
|
21
22
|
ExtensionPermissionsSchema,
|
|
22
23
|
extensionSettingsUrl,
|
|
24
|
+
readCallerClaim,
|
|
23
25
|
} from "@intentic/sandbox-contract/extension-protocol";
|
|
26
|
+
import { fenceAllows, foldPath } from "@intentic/sandbox-contract/policy/fence-paths";
|
|
24
27
|
import { EXTENSION_TOKEN_HEADER } from "@intentic/sandbox-contract/headers";
|
|
25
28
|
import type * as OrpcClient from "@orpc/client";
|
|
26
29
|
import type * as OpenApiFetch from "@orpc/openapi-client/fetch";
|
|
30
|
+
import { createBackendReach } from "./backend-reach.js";
|
|
31
|
+
import { createSleepReach } from "./sleep-reach.js";
|
|
32
|
+
import { createTurnReach } from "./turn-reach.js";
|
|
27
33
|
import type { Disposable } from "./disposable.js";
|
|
28
34
|
import type {
|
|
29
35
|
ApprovedActionHandler,
|
|
36
|
+
ApprovedPostHandler,
|
|
37
|
+
AutomationFireHandler,
|
|
30
38
|
BackendRouteHandler,
|
|
39
|
+
DaemonDoors,
|
|
40
|
+
ExtensionCaller,
|
|
31
41
|
ExtensionHealth,
|
|
32
42
|
ExtensionProcessApi,
|
|
33
43
|
ExtensionServerApi,
|
|
34
44
|
ExtensionServerContext,
|
|
35
45
|
ExtensionServerModule,
|
|
46
|
+
PublicRouteHandler,
|
|
36
47
|
ServerActivation,
|
|
37
48
|
ServerDocument,
|
|
38
49
|
ServerDocumentOptions,
|
|
39
50
|
StoredJson,
|
|
40
51
|
ToolCard,
|
|
41
52
|
ToolDefinition,
|
|
53
|
+
WatchCheckHandler,
|
|
42
54
|
} from "./server.js";
|
|
43
55
|
import { extensionApiVersion } from "./version.js";
|
|
44
56
|
|
|
@@ -48,7 +60,15 @@ export type {
|
|
|
48
60
|
ApprovedAction,
|
|
49
61
|
ApprovedActionHandler,
|
|
50
62
|
ApprovedActionOutcome,
|
|
63
|
+
ApprovedPost,
|
|
64
|
+
ApprovedPostHandler,
|
|
65
|
+
AutomationFire,
|
|
66
|
+
AutomationFireHandler,
|
|
51
67
|
BackendRouteHandler,
|
|
68
|
+
BrowserCall,
|
|
69
|
+
DaemonDoors,
|
|
70
|
+
ExtensionCaller,
|
|
71
|
+
ExtensionCallerWho,
|
|
52
72
|
ExtensionHealth,
|
|
53
73
|
ExtensionHealthState,
|
|
54
74
|
ExtensionProcessApi,
|
|
@@ -56,17 +76,36 @@ export type {
|
|
|
56
76
|
ExtensionServerContext,
|
|
57
77
|
ExtensionServerModule,
|
|
58
78
|
ExtensionSettingValues,
|
|
79
|
+
JobEnd,
|
|
80
|
+
JobSpec,
|
|
81
|
+
OwnerBrowserCall,
|
|
82
|
+
PublicRouteHandler,
|
|
83
|
+
PublicRouteVerification,
|
|
84
|
+
RunningJob,
|
|
59
85
|
ServerActivation,
|
|
60
86
|
ServerDocument,
|
|
61
87
|
ServerDocumentOptions,
|
|
88
|
+
SleepBoot,
|
|
89
|
+
SleepHandler,
|
|
90
|
+
SleepHeardMessage,
|
|
91
|
+
SleepNotice,
|
|
92
|
+
StartedJob,
|
|
62
93
|
StoredJson,
|
|
94
|
+
ToolArguments,
|
|
63
95
|
ToolCallContext,
|
|
96
|
+
ToolCallGrants,
|
|
64
97
|
ToolCard,
|
|
65
98
|
ToolContent,
|
|
66
99
|
ToolDefinition,
|
|
67
100
|
ToolEffect,
|
|
101
|
+
ToolGrantAccount,
|
|
102
|
+
TurnStanding,
|
|
103
|
+
WatchCheck,
|
|
104
|
+
WatchCheckAnswer,
|
|
105
|
+
WatchCheckHandler,
|
|
68
106
|
ToolResult,
|
|
69
107
|
} from "./server.js";
|
|
108
|
+
export type { ActivityRow, CiFeedEvent, ExecClass, ExecOptions, ExecResult, RunSettled, SandboxActivity } from "./server-reach.js";
|
|
70
109
|
|
|
71
110
|
// The backend api, built once for every place an extension's Node code runs: the daemon's backend host, which runs every
|
|
72
111
|
// `server` bundle in one process, and each declared process (`contributes.processes`), which connects itself from the
|
|
@@ -92,6 +131,8 @@ export interface DaemonApiOptions {
|
|
|
92
131
|
readonly permissions: readonly string[];
|
|
93
132
|
// Where requests go; the global `fetch` when absent, read at call time so a test can stand in for it.
|
|
94
133
|
readonly fetch?: Fetch | undefined;
|
|
134
|
+
// Set on every request beside the token: a caller's assertion, for `asCaller`.
|
|
135
|
+
readonly headers?: Readonly<Record<string, string>> | undefined;
|
|
95
136
|
}
|
|
96
137
|
|
|
97
138
|
// Loads a module synchronously, which Node 22+ and Bun both do for ESM; used only for what a process should not pay for
|
|
@@ -110,7 +151,7 @@ const typedClient = (options: DaemonApiOptions, send: Fetch): Rpc => {
|
|
|
110
151
|
return createORPCClient<Rpc>(
|
|
111
152
|
new OpenAPILink(contract.sandboxContract, {
|
|
112
153
|
url: options.url,
|
|
113
|
-
headers: { [EXTENSION_TOKEN_HEADER]: options.token },
|
|
154
|
+
headers: { ...options.headers, [EXTENSION_TOKEN_HEADER]: options.token },
|
|
114
155
|
fetch: (request, init) => send(request, init),
|
|
115
156
|
interceptors: [
|
|
116
157
|
// A procedure this build's contract does not declare is refused too: reaching one means a hand-built path.
|
|
@@ -145,6 +186,9 @@ export const createDaemonApi = (options: DaemonApiOptions): DaemonApi => {
|
|
|
145
186
|
const send: Fetch = (input, init) => (options.fetch ?? globalThis.fetch)(input, init);
|
|
146
187
|
const granted = (init?: RequestInit): Headers => {
|
|
147
188
|
const headers = new Headers(init?.headers);
|
|
189
|
+
for (const [name, value] of Object.entries(options.headers ?? {})) {
|
|
190
|
+
headers.set(name, value);
|
|
191
|
+
}
|
|
148
192
|
headers.set(EXTENSION_TOKEN_HEADER, options.token);
|
|
149
193
|
return headers;
|
|
150
194
|
};
|
|
@@ -154,6 +198,12 @@ export const createDaemonApi = (options: DaemonApiOptions): DaemonApi => {
|
|
|
154
198
|
client ??= typedClient(options, send);
|
|
155
199
|
return client;
|
|
156
200
|
},
|
|
201
|
+
// The same doors with the caller's assertion on every request; the daemon judges it, so a stale or foreign one is
|
|
202
|
+
// its refusal to say, not this side's.
|
|
203
|
+
asCaller: (caller: ExtensionCaller): DaemonDoors => {
|
|
204
|
+
const { asCaller: _nested, ...doors } = createDaemonApi({ ...options, headers: { ...options.headers, [EXTENSION_CALLER_HEADER]: caller.assertion } });
|
|
205
|
+
return doors;
|
|
206
|
+
},
|
|
157
207
|
request: (path, init) => send(`${options.url}${path}`, { ...init, headers: granted(init) }),
|
|
158
208
|
// Over base's transport, through the same `send`: a 30 s deadline (with the caller's signal), and a refusal's
|
|
159
209
|
// body cut to one line in the message instead of quoted whole.
|
|
@@ -188,144 +238,6 @@ export const createDaemonApi = (options: DaemonApiOptions): DaemonApi => {
|
|
|
188
238
|
};
|
|
189
239
|
};
|
|
190
240
|
|
|
191
|
-
type Listener = (values: readonly string[]) => void;
|
|
192
|
-
|
|
193
|
-
// How long a connection may go without a frame before it is taken for dead: three missed heartbeats.
|
|
194
|
-
const SILENCE_MS = EXTENSION_EVENTS_HEARTBEAT_MS * 3;
|
|
195
|
-
|
|
196
|
-
// Every complete line in `buffer`, and what is left after the last one.
|
|
197
|
-
const splitLines = (buffer: string) => {
|
|
198
|
-
const parts = buffer.split("\n");
|
|
199
|
-
const rest = parts.pop() ?? "";
|
|
200
|
-
return { lines: parts.map((line) => line.trim()).filter((line) => line !== ""), rest };
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
// The values a frame carries for its listeners, or undefined for a line this version cannot read (not JSON, or a kind a
|
|
204
|
-
// newer daemon sends), which is skipped rather than fatal.
|
|
205
|
-
const frameOf = (line: string): { readonly kind: ExtensionEventKind; readonly values: readonly string[] } | undefined => {
|
|
206
|
-
// A line that is not JSON is a frame this version cannot read, skipped like an unknown kind.
|
|
207
|
-
const event = parseJsonWith(line, ExtensionEventSchema);
|
|
208
|
-
if (event === undefined) {
|
|
209
|
-
return undefined;
|
|
210
|
-
}
|
|
211
|
-
switch (event.kind) {
|
|
212
|
-
case "heartbeat":
|
|
213
|
-
return undefined;
|
|
214
|
-
case "files":
|
|
215
|
-
return { kind: event.kind, values: event.paths };
|
|
216
|
-
case "settings":
|
|
217
|
-
return { kind: event.kind, values: event.keys };
|
|
218
|
-
default:
|
|
219
|
-
return { kind: event.kind, values: event.repos };
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
// The extension's event stream: opened by the first listener, closed with the last, reconnected with backoff while
|
|
224
|
-
// anything listens. A drop means frames were missed, so a reconnect tells every listener "anything may have changed" with
|
|
225
|
-
// an empty list, which is what a reader of an empty batch already does.
|
|
226
|
-
const createEventHub = (open: (signal: AbortSignal) => Promise<Response>, log: (message: string) => void) => {
|
|
227
|
-
const listeners = new Map<ExtensionEventKind, Set<Listener>>(EXTENSION_EVENT_KINDS.map((kind) => [kind, new Set<Listener>()]));
|
|
228
|
-
let running: AbortController | undefined;
|
|
229
|
-
const count = (): number => [...listeners.values()].reduce((sum, set) => sum + set.size, 0);
|
|
230
|
-
|
|
231
|
-
const deliver = (kind: ExtensionEventKind, values: readonly string[]): void => {
|
|
232
|
-
for (const listener of listeners.get(kind) ?? []) {
|
|
233
|
-
try {
|
|
234
|
-
listener(values);
|
|
235
|
-
} catch (error) {
|
|
236
|
-
// A throwing listener is its own bug; the stream and every other listener carry on.
|
|
237
|
-
log(`a ${kind} listener threw: ${errorMessage(error)}`);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
// One connection, read to its end; resolves when the daemon ends it or it falls silent.
|
|
243
|
-
const connect = async (outer: AbortSignal): Promise<void> => {
|
|
244
|
-
const connection = new AbortController();
|
|
245
|
-
const abort = (): void => connection.abort();
|
|
246
|
-
outer.addEventListener("abort", abort, { once: true });
|
|
247
|
-
let silence = setTimeout(abort, SILENCE_MS);
|
|
248
|
-
try {
|
|
249
|
-
const response = await open(connection.signal);
|
|
250
|
-
if (!response.ok || response.body === null) {
|
|
251
|
-
throw new Error(`the daemon answered ${response.status}`);
|
|
252
|
-
}
|
|
253
|
-
const reader = response.body.getReader();
|
|
254
|
-
const decoder = new TextDecoder();
|
|
255
|
-
let buffer = "";
|
|
256
|
-
for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
|
|
257
|
-
clearTimeout(silence);
|
|
258
|
-
silence = setTimeout(abort, SILENCE_MS);
|
|
259
|
-
const split = splitLines(buffer + decoder.decode(chunk.value, { stream: true }));
|
|
260
|
-
buffer = split.rest;
|
|
261
|
-
for (const line of split.lines) {
|
|
262
|
-
const frame = frameOf(line);
|
|
263
|
-
if (frame !== undefined) {
|
|
264
|
-
deliver(frame.kind, frame.values);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
} finally {
|
|
269
|
-
clearTimeout(silence);
|
|
270
|
-
outer.removeEventListener("abort", abort);
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
// Every connection after the first follows a gap, which a listener hears about as an empty batch.
|
|
275
|
-
const run = async (signal: AbortSignal): Promise<void> => {
|
|
276
|
-
let connections = 0;
|
|
277
|
-
const backoff = createBackoff({ floorMs: 1_000, capMs: 30_000, stableMs: SILENCE_MS });
|
|
278
|
-
while (!signal.aborted) {
|
|
279
|
-
if (connections > 0) {
|
|
280
|
-
for (const kind of EXTENSION_EVENT_KINDS) {
|
|
281
|
-
deliver(kind, []);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
connections += 1;
|
|
285
|
-
const opened = Date.now();
|
|
286
|
-
try {
|
|
287
|
-
await connect(signal);
|
|
288
|
-
} catch (error) {
|
|
289
|
-
if (!signal.aborted) {
|
|
290
|
-
log(`the event stream dropped (${errorMessage(error)}); reconnecting`);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
if (signal.aborted) {
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
await sleep(backoff.next(Date.now() - opened), { signal, unref: true });
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
const subscribe = (kind: ExtensionEventKind, listener: Listener): Disposable => {
|
|
301
|
-
listeners.get(kind)?.add(listener);
|
|
302
|
-
if (running === undefined) {
|
|
303
|
-
running = new AbortController();
|
|
304
|
-
void run(running.signal);
|
|
305
|
-
}
|
|
306
|
-
return {
|
|
307
|
-
dispose: () => {
|
|
308
|
-
listeners.get(kind)?.delete(listener);
|
|
309
|
-
if (count() === 0) {
|
|
310
|
-
running?.abort();
|
|
311
|
-
running = undefined;
|
|
312
|
-
}
|
|
313
|
-
},
|
|
314
|
-
};
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
return {
|
|
318
|
-
subscribe,
|
|
319
|
-
close: (): void => {
|
|
320
|
-
for (const set of listeners.values()) {
|
|
321
|
-
set.clear();
|
|
322
|
-
}
|
|
323
|
-
running?.abort();
|
|
324
|
-
running = undefined;
|
|
325
|
-
},
|
|
326
|
-
};
|
|
327
|
-
};
|
|
328
|
-
|
|
329
241
|
// A JSON file under the extension's own state directory, read through the contract's one document reading.
|
|
330
242
|
const openDocument = <T>(stateDir: string, path: string, options: ServerDocumentOptions<T>): ServerDocument<T> => {
|
|
331
243
|
const file = resolve(stateDir, path);
|
|
@@ -373,6 +285,11 @@ export interface ExtensionRuntimeOptions {
|
|
|
373
285
|
// Where a log line goes; stdout with the id as a prefix when absent, which is what reaches the daemon's log.
|
|
374
286
|
readonly log?: ((message: string) => void) | undefined;
|
|
375
287
|
readonly fetch?: Fetch | undefined;
|
|
288
|
+
// The rest of the manifest's `permissions` (events, work, exec, fires): what the runtime asks for on the event
|
|
289
|
+
// stream, and refuses before asking when undeclared. Nothing declared when absent.
|
|
290
|
+
readonly declared?: ExtensionDeclaredReach | undefined;
|
|
291
|
+
// The ids of the watch kinds its manifest declares (`contributes.watchKinds`): the checks its backend may serve.
|
|
292
|
+
readonly watchKinds?: readonly string[] | undefined;
|
|
376
293
|
}
|
|
377
294
|
|
|
378
295
|
export interface ExtensionRuntime {
|
|
@@ -385,7 +302,8 @@ export interface ExtensionRuntime {
|
|
|
385
302
|
export const createExtensionRuntime = (options: ExtensionRuntimeOptions): ExtensionRuntime => {
|
|
386
303
|
const log = options.log ?? ((message: string) => void process.stdout.write(`[${options.id}] ${message}\n`));
|
|
387
304
|
const daemon = createDaemonApi({ url: options.daemonUrl, token: options.token, id: options.id, permissions: options.permissions, fetch: options.fetch });
|
|
388
|
-
const
|
|
305
|
+
const reach = createBackendReach(options.id, daemon.request, options.declared ?? NO_DECLARED_REACH, log);
|
|
306
|
+
const { close: closeSleep, ...sleep } = createSleepReach(daemon.request, log);
|
|
389
307
|
return {
|
|
390
308
|
api: {
|
|
391
309
|
apiVersion: extensionApiVersion,
|
|
@@ -398,34 +316,112 @@ export const createExtensionRuntime = (options: ExtensionRuntimeOptions): Extens
|
|
|
398
316
|
document: (path, documentOptions) => openDocument(options.stateDir, path, documentOptions),
|
|
399
317
|
settings: {
|
|
400
318
|
get: async () => ExtensionOwnSettingsSchema.parse(await (await daemon.request(extensionSettingsUrl())).json()).settings,
|
|
401
|
-
onDidChange:
|
|
402
|
-
},
|
|
403
|
-
workspace: {
|
|
404
|
-
onDidChangeFiles: (listener) => events.subscribe("files", listener),
|
|
405
|
-
onDidChangeRefs: (listener) => events.subscribe("refs", listener),
|
|
406
|
-
onDidChangeRepos: (listener) => events.subscribe("repos", listener),
|
|
319
|
+
onDidChange: reach.onSettings,
|
|
407
320
|
},
|
|
321
|
+
workspace: reach.workspace,
|
|
322
|
+
events: reach.events,
|
|
323
|
+
work: reach.work,
|
|
324
|
+
exec: reach.exec,
|
|
325
|
+
activity: reach.activity,
|
|
326
|
+
sleep,
|
|
327
|
+
},
|
|
328
|
+
close: () => {
|
|
329
|
+
reach.close();
|
|
330
|
+
closeSleep();
|
|
408
331
|
},
|
|
409
|
-
close: events.close,
|
|
410
332
|
};
|
|
411
333
|
};
|
|
412
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Who a request into `/x/<id>` comes from, read off the assertion the daemon attached for extension `id`: undefined when
|
|
337
|
+
* there is none, it does not read, it names another extension, or its window has passed. Its signature is the daemon's
|
|
338
|
+
* to check (the host holds no key): what arrives here came through the daemon's proxy, which drops any the caller sent.
|
|
339
|
+
*/
|
|
340
|
+
export const callerOf = (request: Request, id: string, now = Date.now()): ExtensionCaller | undefined => {
|
|
341
|
+
const assertion = request.headers.get(EXTENSION_CALLER_HEADER) ?? "";
|
|
342
|
+
const claim = assertion === "" ? undefined : readCallerClaim(assertion);
|
|
343
|
+
if (claim === undefined || claim.extension !== id || now >= claim.expiresAt) {
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
// Only a person with areas is fenced; every other caller reads the workspace whole, as the daemon's routes answer it.
|
|
347
|
+
const { folders, ...who } = claim.caller.kind === "person" ? claim.caller : { ...claim.caller, folders: undefined };
|
|
348
|
+
// A path that climbs out of the workspace is inside nothing, whoever asks.
|
|
349
|
+
const reaches = (path: string): boolean => foldPath(path) !== undefined && fenceAllows(folders, path);
|
|
350
|
+
return { ...who, folders, reaches, expiresAt: claim.expiresAt, assertion };
|
|
351
|
+
};
|
|
352
|
+
|
|
413
353
|
// The backend api a `server` bundle is handed: the runtime plus the slots only the host serves, its route namespace, its
|
|
414
|
-
// tools
|
|
415
|
-
// api's contract.
|
|
354
|
+
// tools, the approved actions it carries out and the handlers automations fire. A second `mount`, `serve`, `perform` or
|
|
355
|
+
// `onFire` for one name replaces the first, per the api's contract.
|
|
416
356
|
export interface ServerApiSlots {
|
|
417
357
|
readonly api: ExtensionServerApi;
|
|
418
358
|
readonly handler: () => BackendRouteHandler | undefined;
|
|
359
|
+
// (2026-11-26) What answers the extension's public routes (`api.publicRoutes.serve`).
|
|
360
|
+
readonly publicHandler: () => PublicRouteHandler | undefined;
|
|
419
361
|
readonly tools: () => ToolSource | undefined;
|
|
420
362
|
readonly performer: () => ApprovedActionHandler | undefined;
|
|
363
|
+
// (2026-11-26) What publishes the approved posts its cards declare (`api.approvals.publish`).
|
|
364
|
+
readonly publisher: () => ApprovedPostHandler | undefined;
|
|
365
|
+
readonly fireHandler: (name: string) => AutomationFireHandler | undefined;
|
|
366
|
+
readonly checkHandler: (kind: string) => WatchCheckHandler | undefined;
|
|
421
367
|
close(): void;
|
|
422
368
|
}
|
|
423
369
|
|
|
370
|
+
// The handlers automations fire, by name: only a name the manifest declares (`permissions.fires`), and a handler
|
|
371
|
+
// disposed lets go of its name only while it is still the one registered.
|
|
372
|
+
const createFireHandlers = (id: string, declared: readonly string[]) => {
|
|
373
|
+
const handlers = new Map<string, AutomationFireHandler>();
|
|
374
|
+
return {
|
|
375
|
+
onFire: (name: string, listener: AutomationFireHandler): Disposable => {
|
|
376
|
+
if (!EXTENSION_FIRE_HANDLER.test(name) || !declared.includes(name)) {
|
|
377
|
+
throw new Error(`extension "${id}" registered the fire handler "${name}" without declaring it: declare it in permissions.fires in its manifest`);
|
|
378
|
+
}
|
|
379
|
+
handlers.set(name, listener);
|
|
380
|
+
return {
|
|
381
|
+
dispose: () => {
|
|
382
|
+
if (handlers.get(name) === listener) {
|
|
383
|
+
handlers.delete(name);
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
},
|
|
388
|
+
handler: (name: string): AutomationFireHandler | undefined => handlers.get(name),
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// The checks a backend serves, by kind: only a kind the manifest declares (`contributes.watchKinds`), and a handler
|
|
393
|
+
// disposed lets go of its kind only while it is still the one registered.
|
|
394
|
+
const createCheckHandlers = (id: string, declared: readonly string[]) => {
|
|
395
|
+
const handlers = new Map<string, WatchCheckHandler>();
|
|
396
|
+
return {
|
|
397
|
+
serve: (kind: string, handler: WatchCheckHandler): Disposable => {
|
|
398
|
+
if (!declared.includes(kind)) {
|
|
399
|
+
throw new Error(`extension "${id}" served the watch kind "${kind}" without declaring it: declare it in contributes.watchKinds in its manifest`);
|
|
400
|
+
}
|
|
401
|
+
handlers.set(kind, handler);
|
|
402
|
+
return {
|
|
403
|
+
dispose: () => {
|
|
404
|
+
if (handlers.get(kind) === handler) {
|
|
405
|
+
handlers.delete(kind);
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
};
|
|
409
|
+
},
|
|
410
|
+
handler: (kind: string): WatchCheckHandler | undefined => handlers.get(kind),
|
|
411
|
+
};
|
|
412
|
+
};
|
|
413
|
+
|
|
424
414
|
export const createServerApi = (options: ExtensionRuntimeOptions): ServerApiSlots => {
|
|
425
415
|
const runtime = createExtensionRuntime(options);
|
|
416
|
+
// (2026-11-26) What its tools do with the calling turn's grants (turn-reach.ts).
|
|
417
|
+
const turn = createTurnReach(options.id, runtime.api.daemon.request, (options.declared ?? NO_DECLARED_REACH).turn, runtime.api.log);
|
|
426
418
|
let mounted: BackendRouteHandler | undefined;
|
|
419
|
+
let publicHandler: PublicRouteHandler | undefined;
|
|
427
420
|
let tools: ToolSource | undefined;
|
|
428
421
|
let performer: ApprovedActionHandler | undefined;
|
|
422
|
+
let publisher: ApprovedPostHandler | undefined;
|
|
423
|
+
const fires = createFireHandlers(options.id, (options.declared ?? NO_DECLARED_REACH).fires);
|
|
424
|
+
const checks = createCheckHandlers(options.id, options.watchKinds ?? []);
|
|
429
425
|
return {
|
|
430
426
|
api: {
|
|
431
427
|
...runtime.api,
|
|
@@ -434,6 +430,12 @@ export const createServerApi = (options: ExtensionRuntimeOptions): ServerApiSlot
|
|
|
434
430
|
mounted = handler;
|
|
435
431
|
},
|
|
436
432
|
},
|
|
433
|
+
caller: (request) => callerOf(request, options.id),
|
|
434
|
+
publicRoutes: {
|
|
435
|
+
serve: (handler) => {
|
|
436
|
+
publicHandler = handler;
|
|
437
|
+
},
|
|
438
|
+
},
|
|
437
439
|
tools: {
|
|
438
440
|
serve: (source) => {
|
|
439
441
|
tools = source;
|
|
@@ -443,11 +445,23 @@ export const createServerApi = (options: ExtensionRuntimeOptions): ServerApiSlot
|
|
|
443
445
|
perform: (handler) => {
|
|
444
446
|
performer = handler;
|
|
445
447
|
},
|
|
448
|
+
publish: (handler) => {
|
|
449
|
+
publisher = handler;
|
|
450
|
+
},
|
|
446
451
|
},
|
|
452
|
+
automations: { onFire: fires.onFire },
|
|
453
|
+
checks: { serve: checks.serve },
|
|
454
|
+
browser: turn.browser,
|
|
455
|
+
ownerBrowser: turn.ownerBrowser,
|
|
456
|
+
jobs: turn.jobs,
|
|
447
457
|
},
|
|
448
458
|
handler: () => mounted,
|
|
459
|
+
publicHandler: () => publicHandler,
|
|
449
460
|
tools: () => tools,
|
|
450
461
|
performer: () => performer,
|
|
462
|
+
publisher: () => publisher,
|
|
463
|
+
fireHandler: fires.handler,
|
|
464
|
+
checkHandler: checks.handler,
|
|
451
465
|
close: runtime.close,
|
|
452
466
|
};
|
|
453
467
|
};
|
|
@@ -520,6 +534,18 @@ const required = (env: NodeJS.ProcessEnv, name: string): string => {
|
|
|
520
534
|
return value;
|
|
521
535
|
};
|
|
522
536
|
|
|
537
|
+
// The declared reach a process was started with; nothing declared when the variable is absent (a daemon from before it).
|
|
538
|
+
const declaredOf = (raw: string | undefined): ExtensionDeclaredReach => {
|
|
539
|
+
if (raw === undefined || raw === "") {
|
|
540
|
+
return NO_DECLARED_REACH;
|
|
541
|
+
}
|
|
542
|
+
const parsed = ExtensionDeclaredReachSchema.safeParse(JSON.parse(raw));
|
|
543
|
+
if (!parsed.success) {
|
|
544
|
+
throw new Error(`${EXTENSION_PROCESS_ENV.declared} is not the JSON of a declared reach`);
|
|
545
|
+
}
|
|
546
|
+
return parsed.data;
|
|
547
|
+
};
|
|
548
|
+
|
|
523
549
|
const permissionsOf = (raw: string | undefined): readonly string[] => {
|
|
524
550
|
if (raw === undefined || raw === "") {
|
|
525
551
|
return [];
|
|
@@ -544,4 +570,5 @@ export const connectExtensionProcess = (env: NodeJS.ProcessEnv = process.env, op
|
|
|
544
570
|
stateDir: required(env, EXTENSION_PROCESS_ENV.state),
|
|
545
571
|
cacheDir: required(env, EXTENSION_PROCESS_ENV.cache),
|
|
546
572
|
log: options.log,
|
|
573
|
+
declared: declaredOf(env[EXTENSION_PROCESS_ENV.declared]),
|
|
547
574
|
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { CiFeedEvent } from "@intentic/sandbox-contract/schemas/ci";
|
|
2
|
+
|
|
3
|
+
// What a backend (and a declared process) hears, holds, runs and writes beside its routes (`ExtensionServerApi`'s
|
|
4
|
+
// `events`, `exec`, `activity` and `sleep`, server.ts), split from server.ts to keep each file one reading.
|
|
5
|
+
|
|
6
|
+
// (2026-11-26) A run ending in a conversation this extension started (POST /agent with its own token), as
|
|
7
|
+
// `api.events.onRunSettled` hears it. `stopped`: a person's stop or a parent's cancel ended it, not the turn itself.
|
|
8
|
+
export interface RunSettled {
|
|
9
|
+
readonly conversationId: string;
|
|
10
|
+
// Which run of the conversation it was.
|
|
11
|
+
readonly run: string;
|
|
12
|
+
readonly outcome: "done" | "failed" | "stopped";
|
|
13
|
+
readonly error?: string | undefined;
|
|
14
|
+
// Its last top-level prose, empty when it said nothing (cut at 20 000 characters).
|
|
15
|
+
readonly closing: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Whether any agent turn is running in the sandbox, as `api.events.onActivity` hears it.
|
|
19
|
+
export interface SandboxActivity {
|
|
20
|
+
readonly live: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// (2026-11-26) What the forges said, as the core's CI ingestion heard it (`api.events.onCi`): a run that finished, or a
|
|
24
|
+
// job that ended, with what the forge said of a failed job's failure. The CI feed's own frame (GET /ci/feed).
|
|
25
|
+
export type { CiFeedEvent };
|
|
26
|
+
|
|
27
|
+
// (2026-11-26) A row in the sandbox's activity feed, written as this extension's own (`api.activity.append`): `type` is
|
|
28
|
+
// dotted like the sandbox's own rows (`ci.repair_started`), `content` the sentence a person reads.
|
|
29
|
+
export interface ActivityRow {
|
|
30
|
+
readonly type: string;
|
|
31
|
+
readonly content: string;
|
|
32
|
+
readonly outcome?: "ok" | "error" | undefined;
|
|
33
|
+
readonly conversationId?: string | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The workload classes `api.exec` runs under, each declared in `permissions.exec`: `command`, admitted and ranked as an
|
|
37
|
+
// agent's shell command; `toolchain`, a heavy tool, which also takes turns in the sandbox's heavy-command queue.
|
|
38
|
+
export type ExecClass = "command" | "toolchain";
|
|
39
|
+
|
|
40
|
+
export interface ExecOptions {
|
|
41
|
+
// `command` when absent.
|
|
42
|
+
readonly class?: ExecClass;
|
|
43
|
+
// Absolute, or relative to the workspace root; the workspace root when absent.
|
|
44
|
+
readonly cwd?: string;
|
|
45
|
+
// Laid over the daemon's environment.
|
|
46
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
47
|
+
readonly stdin?: string;
|
|
48
|
+
// How long it may take, waiting for room or a queue slot included: 10 minutes when absent, at most 2 hours.
|
|
49
|
+
readonly timeoutMs?: number;
|
|
50
|
+
// Bytes kept per stream, 1 MiB when absent, at most 64 MiB; past it the stream is still read and `truncated` says so.
|
|
51
|
+
readonly outputBytes?: number;
|
|
52
|
+
// Which end of an overflowing stream is kept: the head when absent.
|
|
53
|
+
readonly keep?: "head" | "tail";
|
|
54
|
+
// What the queue and the process list call it, after the extension's name.
|
|
55
|
+
readonly label?: string;
|
|
56
|
+
// Ends it, as its deadline does.
|
|
57
|
+
readonly signal?: AbortSignal;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ExecResult {
|
|
61
|
+
// Set when the sandbox did not start it: no room came free within its deadline. Nothing ran.
|
|
62
|
+
readonly refused?: string | undefined;
|
|
63
|
+
// How long it waited for room before it started; a queued `toolchain` run waits inside its own run instead.
|
|
64
|
+
readonly waitedMs: number;
|
|
65
|
+
// Absent when it did not exit by itself: ended (`ended`), never started (`spawnError`), or killed by a signal.
|
|
66
|
+
readonly exitCode?: number | undefined;
|
|
67
|
+
readonly signal?: string | undefined;
|
|
68
|
+
readonly stdout: string;
|
|
69
|
+
readonly stderr: string;
|
|
70
|
+
readonly truncated: boolean;
|
|
71
|
+
readonly ended?: "timeout" | "aborted" | undefined;
|
|
72
|
+
readonly spawnError?: string | undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// (2026-11-27) The hosted machine about to sleep, as `api.sleep.beforeSleep` hears it: the moment (ms) after which it
|
|
76
|
+
// stops whether or not every handler finished.
|
|
77
|
+
export interface SleepNotice {
|
|
78
|
+
readonly deadline: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type SleepHandler = (notice: SleepNotice) => Promise<void> | void;
|
|
82
|
+
|
|
83
|
+
// (2026-11-27) What a boot follows, as `api.sleep.boot()` answers it.
|
|
84
|
+
export interface SleepBoot {
|
|
85
|
+
// The daemon stopped last time because the hosted machine went to sleep: the before-sleep handlers ran.
|
|
86
|
+
readonly afterSleep: boolean;
|
|
87
|
+
// The platform started this machine for a request at one of its wake doors (a webhook's, or a `wakes` route's).
|
|
88
|
+
readonly requestWake: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// (2026-11-27) Where one message is that the platform's sleep watch heard for one of this extension's connections while
|
|
92
|
+
// the sandbox slept, as `api.sleep.heard()` answers it: ids only, never what it says.
|
|
93
|
+
export interface SleepHeardMessage {
|
|
94
|
+
// The service the platform held the connection on (`discord`).
|
|
95
|
+
readonly service: string;
|
|
96
|
+
// The card's id in this sandbox.
|
|
97
|
+
readonly connection: string;
|
|
98
|
+
readonly channelId: string;
|
|
99
|
+
readonly messageId: string;
|
|
100
|
+
// The server it was written in; absent for a direct message.
|
|
101
|
+
readonly guildId?: string | undefined;
|
|
102
|
+
// When the platform heard it (ms).
|
|
103
|
+
readonly at: number;
|
|
104
|
+
}
|