@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.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/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +93 -4
- package/skills/pi-delegates/SKILL.md +147 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
2
|
+
import type { SessionRuntime } from "../../runtime/session.ts";
|
|
3
|
+
import type { Timers } from "../../runtime/timers.ts";
|
|
4
|
+
import { observeSessionDoc, type Detach } from "../../runtime/observe.ts";
|
|
5
|
+
import { errorFields } from "../../runtime/log.ts";
|
|
6
|
+
import { GateIndexDoc, pendingGates } from "./gates.ts";
|
|
7
|
+
import { RequestIndexDoc } from "./requests.ts";
|
|
8
|
+
import { taskId as toTaskId } from "../ids.ts";
|
|
9
|
+
|
|
10
|
+
export type WaitOutcome = "settled" | "timeout" | "aborted" | "gate" | "request";
|
|
11
|
+
export type TaskWaitOutcome = Exclude<WaitOutcome, "gate" | "request">;
|
|
12
|
+
|
|
13
|
+
export interface WaitDeps {
|
|
14
|
+
readonly runtime: SessionRuntime;
|
|
15
|
+
readonly timers: Timers;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TaskWaitInput {
|
|
19
|
+
readonly taskId: number;
|
|
20
|
+
readonly waitMs: number;
|
|
21
|
+
readonly signal?: AbortSignal;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RunWaitInput extends TaskWaitInput {
|
|
25
|
+
readonly runId: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** One retry while an index is absent; a present index pushes its changes instead. */
|
|
29
|
+
export const ATTENTION_RETRY_MS = 1_000;
|
|
30
|
+
/** The backstop that guards a missed push, so a wait cannot reintroduce the long block. */
|
|
31
|
+
export const ATTENTION_BACKSTOP_MS = 5_000;
|
|
32
|
+
|
|
33
|
+
interface Racer<A> {
|
|
34
|
+
readonly promise: Promise<A>;
|
|
35
|
+
cancel(): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const deadline = (timers: Timers, waitMs: number): Racer<"timeout"> => {
|
|
39
|
+
let handle: unknown;
|
|
40
|
+
const promise = new Promise<"timeout">((resolve) => {
|
|
41
|
+
handle = timers.setTimeout(() => resolve("timeout"), waitMs);
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
promise,
|
|
45
|
+
cancel: () => {
|
|
46
|
+
if (handle !== undefined) timers.clearTimeout(handle);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const abortRacer = (signal: AbortSignal | undefined): Racer<"aborted"> => {
|
|
52
|
+
if (signal === undefined) {
|
|
53
|
+
return { promise: new Promise<"aborted">(() => {}), cancel: () => {} };
|
|
54
|
+
}
|
|
55
|
+
let listener: (() => void) | undefined;
|
|
56
|
+
const promise = new Promise<"aborted">((resolve) => {
|
|
57
|
+
if (signal.aborted) {
|
|
58
|
+
resolve("aborted");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
listener = () => resolve("aborted");
|
|
62
|
+
signal.addEventListener("abort", listener, { once: true });
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
promise,
|
|
66
|
+
cancel: () => {
|
|
67
|
+
if (listener !== undefined) signal.removeEventListener("abort", listener);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* One racer over both indexes. It attaches to the gate index and the request
|
|
74
|
+
* index; an advance only records, and one check outside the sink resolves when
|
|
75
|
+
* this run has a pending gate or request. A retry attaches again while an index
|
|
76
|
+
* is absent, and a backstop re-checks after a missed push. Only a found gate or
|
|
77
|
+
* request resolves the racer; `cancel` clears both timers and detaches both
|
|
78
|
+
* observations.
|
|
79
|
+
*/
|
|
80
|
+
const attention = (
|
|
81
|
+
runtime: SessionRuntime,
|
|
82
|
+
runId: string,
|
|
83
|
+
timers: Timers,
|
|
84
|
+
): Racer<"gate" | "request"> => {
|
|
85
|
+
let cancelled = false;
|
|
86
|
+
let settled = false;
|
|
87
|
+
let generation = 0;
|
|
88
|
+
let detachGates: Detach | undefined;
|
|
89
|
+
let detachRequests: Detach | undefined;
|
|
90
|
+
let retry: unknown;
|
|
91
|
+
let backstop: unknown;
|
|
92
|
+
let chain: Promise<void> = Promise.resolve();
|
|
93
|
+
let resolveOutcome: ((outcome: "gate" | "request") => void) | undefined;
|
|
94
|
+
const promise = new Promise<"gate" | "request">((resolve) => {
|
|
95
|
+
resolveOutcome = resolve;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const log = (error: unknown): void => {
|
|
99
|
+
runtime.log.write("warn", "wait.check_failed", errorFields(error));
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const settle = (outcome: "gate" | "request"): void => {
|
|
103
|
+
if (cancelled || settled) return;
|
|
104
|
+
settled = true;
|
|
105
|
+
resolveOutcome?.(outcome);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const check = (): Promise<void> => {
|
|
109
|
+
chain = chain
|
|
110
|
+
.then(async () => {
|
|
111
|
+
if (cancelled || settled) return;
|
|
112
|
+
const index = await runtime.snapshot(RequestIndexDoc);
|
|
113
|
+
if (cancelled || settled) return;
|
|
114
|
+
const now = runtime.now();
|
|
115
|
+
const waiting = (index?.rows ?? []).some(
|
|
116
|
+
(row) =>
|
|
117
|
+
row.runId === runId &&
|
|
118
|
+
row.status === "pending" &&
|
|
119
|
+
(row.deadline <= 0 || now < row.deadline),
|
|
120
|
+
);
|
|
121
|
+
const gates = await pendingGates(runtime, runId);
|
|
122
|
+
if (cancelled || settled) return;
|
|
123
|
+
// A pending gate keeps precedence over a request, so approve/deny ordering is unchanged.
|
|
124
|
+
if (gates.length > 0) settle("gate");
|
|
125
|
+
else if (waiting) settle("request");
|
|
126
|
+
})
|
|
127
|
+
.catch(log);
|
|
128
|
+
return chain;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const armBackstop = (): void => {
|
|
132
|
+
if (cancelled || settled || backstop !== undefined) return;
|
|
133
|
+
backstop = timers.setTimeout(() => {
|
|
134
|
+
backstop = undefined;
|
|
135
|
+
void check().then(armBackstop);
|
|
136
|
+
}, ATTENTION_BACKSTOP_MS);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const scheduleAttach = (): void => {
|
|
140
|
+
if (cancelled || settled || retry !== undefined) return;
|
|
141
|
+
retry = timers.setTimeout(() => {
|
|
142
|
+
retry = undefined;
|
|
143
|
+
void attach();
|
|
144
|
+
}, ATTENTION_RETRY_MS);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const dropAll = (): void => {
|
|
148
|
+
detachGates?.();
|
|
149
|
+
detachGates = undefined;
|
|
150
|
+
detachRequests?.();
|
|
151
|
+
detachRequests = undefined;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const sinkFor = () => ({
|
|
155
|
+
advance: () => {
|
|
156
|
+
void check();
|
|
157
|
+
},
|
|
158
|
+
end: () => {
|
|
159
|
+
if (cancelled) return;
|
|
160
|
+
generation += 1;
|
|
161
|
+
dropAll();
|
|
162
|
+
scheduleAttach();
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const attach = async (): Promise<void> => {
|
|
167
|
+
if (cancelled || settled) return;
|
|
168
|
+
const mine = generation;
|
|
169
|
+
let gates: Detach | undefined;
|
|
170
|
+
let requests: Detach | undefined;
|
|
171
|
+
try {
|
|
172
|
+
gates = await observeSessionDoc(runtime, GateIndexDoc, sinkFor());
|
|
173
|
+
requests = await observeSessionDoc(runtime, RequestIndexDoc, sinkFor());
|
|
174
|
+
} catch (error) {
|
|
175
|
+
log(error);
|
|
176
|
+
gates?.();
|
|
177
|
+
requests?.();
|
|
178
|
+
scheduleAttach();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (cancelled || settled || generation !== mine) {
|
|
182
|
+
gates?.();
|
|
183
|
+
requests?.();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (gates === undefined || requests === undefined) {
|
|
187
|
+
gates?.();
|
|
188
|
+
requests?.();
|
|
189
|
+
scheduleAttach();
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
detachGates = gates;
|
|
193
|
+
detachRequests = requests;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const cancel = (): void => {
|
|
197
|
+
cancelled = true;
|
|
198
|
+
generation += 1;
|
|
199
|
+
if (retry !== undefined) timers.clearTimeout(retry);
|
|
200
|
+
retry = undefined;
|
|
201
|
+
if (backstop !== undefined) timers.clearTimeout(backstop);
|
|
202
|
+
backstop = undefined;
|
|
203
|
+
dropAll();
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
void attach();
|
|
207
|
+
armBackstop();
|
|
208
|
+
return { promise, cancel };
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const race = async <A extends WaitOutcome = never>(
|
|
212
|
+
deps: WaitDeps,
|
|
213
|
+
input: TaskWaitInput,
|
|
214
|
+
extra?: Racer<A>,
|
|
215
|
+
): Promise<TaskWaitOutcome | A> => {
|
|
216
|
+
const controller = new AbortController();
|
|
217
|
+
const signal =
|
|
218
|
+
input.signal === undefined
|
|
219
|
+
? controller.signal
|
|
220
|
+
: AbortSignal.any([input.signal, controller.signal]);
|
|
221
|
+
let decided = false;
|
|
222
|
+
const harness = deps.runtime
|
|
223
|
+
.run(
|
|
224
|
+
(context: Context) => deps.runtime.harness.waitForTask(toTaskId(input.taskId), context),
|
|
225
|
+
signal,
|
|
226
|
+
)
|
|
227
|
+
.then(
|
|
228
|
+
(): "settled" => "settled",
|
|
229
|
+
(error: unknown) => {
|
|
230
|
+
// A cancelled wait is not a failure: the timeout, gate, request or abort racer decided.
|
|
231
|
+
if (!decided && input.signal?.aborted !== true) throw error;
|
|
232
|
+
return "settled" as const;
|
|
233
|
+
},
|
|
234
|
+
);
|
|
235
|
+
const timer = deadline(deps.timers, input.waitMs);
|
|
236
|
+
const abort = abortRacer(input.signal);
|
|
237
|
+
const racers: Promise<TaskWaitOutcome | A>[] = [harness, timer.promise, abort.promise];
|
|
238
|
+
if (extra !== undefined) racers.push(extra.promise);
|
|
239
|
+
try {
|
|
240
|
+
return await Promise.race(racers);
|
|
241
|
+
} finally {
|
|
242
|
+
decided = true;
|
|
243
|
+
timer.cancel();
|
|
244
|
+
abort.cancel();
|
|
245
|
+
extra?.cancel();
|
|
246
|
+
controller.abort();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
/** Waits for the task, the deadline or the caller's signal; a gate is not part of this wait. */
|
|
251
|
+
export const waitForTask = (deps: WaitDeps, input: TaskWaitInput): Promise<TaskWaitOutcome> =>
|
|
252
|
+
race(deps, input);
|
|
253
|
+
|
|
254
|
+
/** Waits for the task, the deadline, the caller's signal, a pending gate or a pending request. */
|
|
255
|
+
export const waitForRun = (deps: WaitDeps, input: RunWaitInput): Promise<WaitOutcome> =>
|
|
256
|
+
race(deps, input, attention(deps.runtime, input.runId, deps.timers));
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
3
|
+
import { rejects, throws } from "../../../test-support/assertions.ts";
|
|
4
|
+
import {
|
|
5
|
+
RefusingSandbox,
|
|
6
|
+
WORKFLOW_SECTION_KEY,
|
|
7
|
+
diagnosticSandbox,
|
|
8
|
+
refusingSandbox,
|
|
9
|
+
workflowSection,
|
|
10
|
+
} from "./workflow-section.ts";
|
|
11
|
+
|
|
12
|
+
const REFUSAL = /this runtime runs no script/;
|
|
13
|
+
|
|
14
|
+
it("the workflow section uses its published key", () => {
|
|
15
|
+
assert.strictEqual(workflowSection().key, WORKFLOW_SECTION_KEY);
|
|
16
|
+
assert.strictEqual(WORKFLOW_SECTION_KEY, "workflow-script");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("the workflow section tells the model to pass the script to codemode", async () => {
|
|
20
|
+
const text = await workflowSection().render({} as never, BACKGROUND_CONTEXT);
|
|
21
|
+
|
|
22
|
+
assert.match(text ?? "", /codemode/);
|
|
23
|
+
assert.match(text ?? "", /script/);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("the refusing sandbox declines a start with its own words", () => {
|
|
27
|
+
throws(() => refusingSandbox().start("1", {} as never), REFUSAL);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("the refusing sandbox declines an execute with the prefixed words", async () => {
|
|
31
|
+
await rejects(refusingSandbox().execute("1"), REFUSAL);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("the refusing sandbox holds no tools and closes without a worker", async () => {
|
|
35
|
+
const sandbox = new RefusingSandbox();
|
|
36
|
+
|
|
37
|
+
assert.deepStrictEqual(sandbox.tools, []);
|
|
38
|
+
assert.deepStrictEqual(sandbox.globals, []);
|
|
39
|
+
assert.strictEqual(sandbox.unregisterTool(), false);
|
|
40
|
+
sandbox.registerTool();
|
|
41
|
+
assert.deepStrictEqual(sandbox.tools, []);
|
|
42
|
+
await sandbox.close();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("the diagnostic sandbox names itself when a start fails", () => {
|
|
46
|
+
throws(
|
|
47
|
+
() => diagnosticSandbox(refusingSandbox()).start("1", {} as never),
|
|
48
|
+
/workflow sandbox: this runtime runs no script/,
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("the diagnostic sandbox delegates the table, the execute and the close", async () => {
|
|
53
|
+
const inner = refusingSandbox();
|
|
54
|
+
const sandbox = diagnosticSandbox(inner);
|
|
55
|
+
|
|
56
|
+
assert.deepStrictEqual(sandbox.tools, inner.tools);
|
|
57
|
+
assert.deepStrictEqual(sandbox.globals, inner.globals);
|
|
58
|
+
assert.strictEqual(sandbox.unregisterTool("read"), false);
|
|
59
|
+
sandbox.registerTool({ name: "read" } as never);
|
|
60
|
+
await rejects(sandbox.execute("1"), REFUSAL);
|
|
61
|
+
await sandbox.close();
|
|
62
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { CodemodeSandboxBase } from "@earendil-works/pi-codemode/portable";
|
|
2
|
+
import { section, type PromptSection } from "@earendil-works/pi-durable";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The instruction a codemode definition gets instead of an ordinary task: the
|
|
6
|
+
* user message is a script, and the model's one job is to pass it to `codemode`.
|
|
7
|
+
*/
|
|
8
|
+
export const WORKFLOW_SECTION_KEY = "workflow-script";
|
|
9
|
+
|
|
10
|
+
const WORKFLOW_INSTRUCTION = [
|
|
11
|
+
"This conversation runs a workflow. The user message is a JavaScript script.",
|
|
12
|
+
"Pass it verbatim to the codemode tool as its `code` argument, then answer with",
|
|
13
|
+
"the value the script returns. Do not rewrite the script and do not run it any",
|
|
14
|
+
"other way. The script calls the session's callable tools under the `runs`",
|
|
15
|
+
"namespace, for example `await tools.launch({ agent, task })`.",
|
|
16
|
+
].join(" ");
|
|
17
|
+
|
|
18
|
+
/** The `workflow-script` section: selected with the `runs` extension, so only a workflow reads it. */
|
|
19
|
+
export const workflowSection = (): PromptSection =>
|
|
20
|
+
section(WORKFLOW_SECTION_KEY, () => WORKFLOW_INSTRUCTION);
|
|
21
|
+
|
|
22
|
+
const REFUSAL = "this runtime runs no script.";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The read-only runtime's sandbox: it never starts a worker, and a start fails
|
|
26
|
+
* with a text the registry's diagnostic wrapper prefixes, so a viewer stays a viewer.
|
|
27
|
+
*/
|
|
28
|
+
export class RefusingSandbox {
|
|
29
|
+
readonly tools: readonly never[] = [];
|
|
30
|
+
readonly globals: readonly never[] = [];
|
|
31
|
+
registerTool(): void {}
|
|
32
|
+
unregisterTool(): boolean {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
async execute(): Promise<never> {
|
|
36
|
+
throw new Error(`workflow sandbox: ${REFUSAL}`);
|
|
37
|
+
}
|
|
38
|
+
start(): never {
|
|
39
|
+
throw new Error(REFUSAL);
|
|
40
|
+
}
|
|
41
|
+
async close(): Promise<void> {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The read-only sandbox, typed for the registry. */
|
|
45
|
+
export const refusingSandbox = (): CodemodeSandboxBase =>
|
|
46
|
+
new RefusingSandbox() as unknown as CodemodeSandboxBase;
|
|
47
|
+
|
|
48
|
+
const messageOf = (error: unknown): string =>
|
|
49
|
+
error instanceof Error ? error.message : String(error);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The sandbox the registry installs: it delegates everything, and a `start()`
|
|
53
|
+
* rejection carries the sandbox's name, so a failed run says where it failed.
|
|
54
|
+
*/
|
|
55
|
+
class DiagnosticSandbox {
|
|
56
|
+
constructor(readonly inner: CodemodeSandboxBase) {}
|
|
57
|
+
|
|
58
|
+
get tools() {
|
|
59
|
+
return this.inner.tools;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get globals() {
|
|
63
|
+
return this.inner.globals;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
registerTool(tool: Parameters<CodemodeSandboxBase["registerTool"]>[0]): void {
|
|
67
|
+
this.inner.registerTool(tool);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
unregisterTool(name: string): boolean {
|
|
71
|
+
return this.inner.unregisterTool(name);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
execute(
|
|
75
|
+
code: string,
|
|
76
|
+
options?: Parameters<CodemodeSandboxBase["execute"]>[1],
|
|
77
|
+
): ReturnType<CodemodeSandboxBase["execute"]> {
|
|
78
|
+
return this.inner.execute(code, options);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
start(
|
|
82
|
+
code: string,
|
|
83
|
+
options: Parameters<CodemodeSandboxBase["start"]>[1],
|
|
84
|
+
): ReturnType<CodemodeSandboxBase["start"]> {
|
|
85
|
+
try {
|
|
86
|
+
return this.inner.start(code, options);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw new Error(`workflow sandbox: ${messageOf(error)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
close(): Promise<void> {
|
|
93
|
+
return this.inner.close();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The one sandbox per runtime, with the diagnostic prefix. */
|
|
98
|
+
export const diagnosticSandbox = (inner: CodemodeSandboxBase): CodemodeSandboxBase =>
|
|
99
|
+
new DiagnosticSandbox(inner) as unknown as CodemodeSandboxBase;
|