trusted-user-agent 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/dist/acp/agent.js +601 -0
- package/dist/acp/backend.js +2 -0
- package/dist/acp/config-options.js +26 -0
- package/dist/acp/mock-backend.js +277 -0
- package/dist/acp/sdk-backend.js +495 -0
- package/dist/browser/mcp-acp-client.js +81 -0
- package/dist/browser/provider.js +1045 -0
- package/dist/browser/schemas.js +221 -0
- package/dist/capability-registry.js +92 -0
- package/dist/client-heartbeat.js +118 -0
- package/dist/compose/schemas.js +90 -0
- package/dist/contacts/schemas.js +26 -0
- package/dist/installer/cli.js +70 -0
- package/dist/installer/common.js +93 -0
- package/dist/installer/index.js +341 -0
- package/dist/installer/mcp-clients.js +555 -0
- package/dist/installer/mcp.js +166 -0
- package/dist/installer/platforms.js +277 -0
- package/dist/logger.js +54 -0
- package/dist/mail/schemas.js +98 -0
- package/dist/mutation/schemas.js +31 -0
- package/dist/native-host/broker-ipc.js +344 -0
- package/dist/native-host/framing.js +76 -0
- package/dist/native-host/main.js +520 -0
- package/dist/native-host/mcp-server.js +322 -0
- package/dist/native-host/mcp-stdio-bridge.js +179 -0
- package/dist/native-host/mcp-stdio-main.js +13 -0
- package/dist/native-host/relay.js +84 -0
- package/dist/native-host/skills-startup.js +167 -0
- package/dist/native-host/transport.js +248 -0
- package/dist/permission-store.js +207 -0
- package/dist/repl/fs-sandbox.js +187 -0
- package/dist/repl/provider.js +252 -0
- package/dist/repl/runtime.js +436 -0
- package/dist/repl/types.js +10 -0
- package/dist/repl/worker.js +731 -0
- package/dist/skills/extension.js +99 -0
- package/dist/skills/grants-file.js +145 -0
- package/dist/skills/isolated-runner.js +259 -0
- package/dist/skills/mock-session-factory.js +49 -0
- package/dist/skills/package-roots.js +25 -0
- package/dist/skills/pi-bridge.js +8 -0
- package/dist/skills/pi-integration.js +110 -0
- package/dist/skills/planner.js +128 -0
- package/dist/skills/registry.js +104 -0
- package/dist/skills/skills-protocol.js +253 -0
- package/dist/skills/skills-service.js +195 -0
- package/dist/tool-bridge.js +8 -0
- package/dist/workspace.js +70 -0
- package/extensions/pi-browser-tools.ts +65 -0
- package/extensions/pi-browser.ts +60 -0
- package/extensions/pi-skills.ts +14 -0
- package/native/pi-agent-host +2 -0
- package/native/pi-browser-host +5 -0
- package/native/pi-browser-host.cmd +3 -0
- package/node_modules/@pi-browser/protocol/.bundled-by-trusted-user-agent +0 -0
- package/node_modules/@pi-browser/protocol/dist/acp.d.ts +10 -0
- package/node_modules/@pi-browser/protocol/dist/acp.js +9 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.js +478 -0
- package/node_modules/@pi-browser/protocol/dist/callers.d.ts +108 -0
- package/node_modules/@pi-browser/protocol/dist/callers.js +56 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.d.ts +30 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.js +175 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.d.ts +23 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.js +63 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.d.ts +32 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.js +201 -0
- package/node_modules/@pi-browser/protocol/dist/errors.d.ts +63 -0
- package/node_modules/@pi-browser/protocol/dist/errors.js +109 -0
- package/node_modules/@pi-browser/protocol/dist/index.d.ts +16 -0
- package/node_modules/@pi-browser/protocol/dist/index.js +16 -0
- package/node_modules/@pi-browser/protocol/dist/integration.d.ts +409 -0
- package/node_modules/@pi-browser/protocol/dist/integration.js +260 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.js +31 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.d.ts +27 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.js +88 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.d.ts +133 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.js +245 -0
- package/node_modules/@pi-browser/protocol/dist/permission.d.ts +113 -0
- package/node_modules/@pi-browser/protocol/dist/permission.js +257 -0
- package/node_modules/@pi-browser/protocol/dist/skills.d.ts +304 -0
- package/node_modules/@pi-browser/protocol/dist/skills.js +473 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.d.ts +18 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.js +52 -0
- package/node_modules/@pi-browser/protocol/dist/transport.d.ts +17 -0
- package/node_modules/@pi-browser/protocol/dist/transport.js +17 -0
- package/node_modules/@pi-browser/protocol/package.json +14 -0
- package/package.json +71 -6
- package/skills/browser-walk/SKILL.md +98 -0
- package/README.md +0 -3
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ReplRuntime — host-side owner of one session's ReplWorker child
|
|
3
|
+
* (BROWSER-USE-REPL-PLAN.md, Phase 0).
|
|
4
|
+
*
|
|
5
|
+
* One worker, one active cell. Termination is the cancellation boundary:
|
|
6
|
+
* a cell timeout or cancellation SIGKILLs the child, which resets all
|
|
7
|
+
* JavaScript state (the same contract as the SDK worker a timeout can kill
|
|
8
|
+
* a synchronous infinite loop; in-process execution cannot provide that
|
|
9
|
+
* guarantee). The child is a dumb JS sandbox: every browser primitive is a
|
|
10
|
+
* tool request answered by the host through the session's existing
|
|
11
|
+
* browser-tool transport (permissions and binding rules apply unchanged).
|
|
12
|
+
*
|
|
13
|
+
* Ported and adapted from browser-use-pi `src/runtime.ts` (MIT).
|
|
14
|
+
*/
|
|
15
|
+
import { fork } from "node:child_process";
|
|
16
|
+
import { mkdir, open, writeFile } from "node:fs/promises";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { randomUUID } from "node:crypto";
|
|
19
|
+
/** A cell failed (code error, timeout, or worker death). Partial output included. */
|
|
20
|
+
export class ReplCellError extends Error {
|
|
21
|
+
result;
|
|
22
|
+
stateReset;
|
|
23
|
+
constructor(message, result,
|
|
24
|
+
/** True when JavaScript state was reset (timeout/crash), not preserved (code error). */
|
|
25
|
+
stateReset) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.result = result;
|
|
28
|
+
this.stateReset = stateReset;
|
|
29
|
+
this.name = "ReplCellError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
33
|
+
const MAX_TIMEOUT_MS = 120_000;
|
|
34
|
+
const READY_TIMEOUT_MS = 20_000;
|
|
35
|
+
const CLOSE_TIMEOUT_MS = 2_000;
|
|
36
|
+
/**
|
|
37
|
+
* Cap on time a cell may spend paused inside permission prompts (WS2/T2.1 of
|
|
38
|
+
* BROWSER-USE-SUPPORT-PLAN.md): while a cell's tool waits on the user's
|
|
39
|
+
* permission overlay the cell deadline is paused, so an "Allow once" click
|
|
40
|
+
* cannot cost the cell its remaining budget. The cap (enforced by a watchdog
|
|
41
|
+
* while blocked, cumulative across prompts in the cell) stops an ignored
|
|
42
|
+
* overlay from hanging a cell forever; exceeding it aborts the cell (state
|
|
43
|
+
* reset).
|
|
44
|
+
*/
|
|
45
|
+
const PERMISSION_BLOCK_CAP_MS = 120_000;
|
|
46
|
+
/** Only these tools may block on the user's permission overlay (WS2/T2.1). */
|
|
47
|
+
const DEFAULT_PERMISSION_TOOLS = ["browser_screenshot", "browser_evaluate"];
|
|
48
|
+
export class ReplRuntime {
|
|
49
|
+
opts;
|
|
50
|
+
child;
|
|
51
|
+
busy = false;
|
|
52
|
+
workerLoss;
|
|
53
|
+
closed = false;
|
|
54
|
+
starting;
|
|
55
|
+
pending;
|
|
56
|
+
nextToolId = 0;
|
|
57
|
+
toolPending = new Map();
|
|
58
|
+
notes = [];
|
|
59
|
+
/** The active cell's permission-pause ledger (set by receive(), no-op once the cell ends). */
|
|
60
|
+
pauseLedger;
|
|
61
|
+
blockCapMs;
|
|
62
|
+
permissionTools;
|
|
63
|
+
constructor(opts) {
|
|
64
|
+
this.opts = opts;
|
|
65
|
+
this.blockCapMs = opts.permissionBlockCapMs ?? PERMISSION_BLOCK_CAP_MS;
|
|
66
|
+
this.permissionTools = new Set(opts.permissionTools ?? DEFAULT_PERMISSION_TOOLS);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Run one cell. Rejects with ReplCellError for code errors, timeouts and
|
|
70
|
+
* worker death (partial output in `error.result`); resolves with the
|
|
71
|
+
* result otherwise.
|
|
72
|
+
*/
|
|
73
|
+
async call(code, options = {}) {
|
|
74
|
+
if (this.closed)
|
|
75
|
+
throw new ReplCellError("Repl runtime is closed.", { text: "", images: [] }, true);
|
|
76
|
+
if (this.busy)
|
|
77
|
+
throw new Error("A Repl cell is already running. Await it before starting another.");
|
|
78
|
+
const timeoutMs = this.normalizeTimeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
79
|
+
const signal = options.signal;
|
|
80
|
+
if (signal?.aborted)
|
|
81
|
+
throw new ReplCellError("Execution cancelled.", { text: "", images: [] }, true);
|
|
82
|
+
if (!code.trim())
|
|
83
|
+
throw new Error("Provide JavaScript code to execute.");
|
|
84
|
+
if (this.workerLoss) {
|
|
85
|
+
const error = this.workerLoss;
|
|
86
|
+
this.workerLoss = undefined;
|
|
87
|
+
// No new cell ran. Expose the same reset contract as a crash during execution.
|
|
88
|
+
throw new ReplCellError(error.message, { text: "", images: [] }, true);
|
|
89
|
+
}
|
|
90
|
+
this.busy = true;
|
|
91
|
+
try {
|
|
92
|
+
const child = await this.start(signal);
|
|
93
|
+
if (signal?.aborted)
|
|
94
|
+
throw new ReplCellError("Execution cancelled.", { text: "", images: [] }, true);
|
|
95
|
+
const directory = join(this.opts.workspace, ".repl", "cells");
|
|
96
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
97
|
+
const outputFile = join(directory, `${randomUUID()}.txt`);
|
|
98
|
+
await writeFile(outputFile, "", { flag: "wx", mode: 0o600 });
|
|
99
|
+
const response = this.receive(child, timeoutMs, signal);
|
|
100
|
+
const notes = this.notes;
|
|
101
|
+
this.notes = [];
|
|
102
|
+
const sendError = child.send({ type: "execute", code, outputFile, timeoutMs, ...(notes.length ? { notes } : {}) }, (error) => {
|
|
103
|
+
if (error)
|
|
104
|
+
this.pending?.(error);
|
|
105
|
+
});
|
|
106
|
+
if (!sendError)
|
|
107
|
+
this.pending?.(new Error("Repl IPC send failed."));
|
|
108
|
+
let message;
|
|
109
|
+
try {
|
|
110
|
+
message = await response;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
await this.terminate();
|
|
114
|
+
const text = await readFileSafe(outputFile).catch(() => "");
|
|
115
|
+
throw new ReplCellError(String(error instanceof Error ? error.message : error), { text, images: [], outputFile }, true);
|
|
116
|
+
}
|
|
117
|
+
if (message.type !== "result")
|
|
118
|
+
throw new ReplCellError("Unexpected Repl worker response.", { text: "", images: [] }, true);
|
|
119
|
+
return { ...message.result, ...(message.error !== undefined ? { error: message.error } : {}) };
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
this.busy = false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Queue a note (e.g. "binding changed") to prepend to the next cell's output. */
|
|
126
|
+
invalidate(note) {
|
|
127
|
+
this.notes.push(note);
|
|
128
|
+
}
|
|
129
|
+
/** The worker child's pid (undefined when not running); for supervision/tests. */
|
|
130
|
+
get childPid() {
|
|
131
|
+
return this.child?.pid;
|
|
132
|
+
}
|
|
133
|
+
/** Idempotent. Cancels execution and reaps the child. */
|
|
134
|
+
async dispose() {
|
|
135
|
+
if (this.closed)
|
|
136
|
+
return;
|
|
137
|
+
this.closed = true;
|
|
138
|
+
if (this.busy) {
|
|
139
|
+
this.pending?.(new Error("Repl runtime closed during execution."));
|
|
140
|
+
await this.terminate();
|
|
141
|
+
}
|
|
142
|
+
const child = this.child;
|
|
143
|
+
this.child = undefined;
|
|
144
|
+
if (child && child.exitCode === null && child.signalCode === null) {
|
|
145
|
+
const exited = new Promise((resolve) => child.once("exit", () => resolve()));
|
|
146
|
+
const sendError = child.send({ type: "close" }, (err) => {
|
|
147
|
+
if (err)
|
|
148
|
+
this.log(`repl close send failed: ${err.message}`);
|
|
149
|
+
});
|
|
150
|
+
if (!sendError)
|
|
151
|
+
this.pending?.(new Error("Repl IPC send failed."));
|
|
152
|
+
const timer = setTimeout(() => {
|
|
153
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
154
|
+
child.kill("SIGKILL");
|
|
155
|
+
}, CLOSE_TIMEOUT_MS);
|
|
156
|
+
await exited;
|
|
157
|
+
clearTimeout(timer);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// ------------------------------------------------------------------
|
|
161
|
+
// Internals
|
|
162
|
+
// ------------------------------------------------------------------
|
|
163
|
+
normalizeTimeout(timeoutMs) {
|
|
164
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > MAX_TIMEOUT_MS)
|
|
165
|
+
throw new Error(`timeoutMs must be an integer from 1 to ${MAX_TIMEOUT_MS} (got ${String(timeoutMs)}).`);
|
|
166
|
+
return timeoutMs;
|
|
167
|
+
}
|
|
168
|
+
log(line) {
|
|
169
|
+
this.opts.log?.(`[repl] ${line}`);
|
|
170
|
+
}
|
|
171
|
+
start(signal) {
|
|
172
|
+
if (this.child)
|
|
173
|
+
return Promise.resolve(this.child);
|
|
174
|
+
this.starting ??= this.doStart(signal);
|
|
175
|
+
return this.starting;
|
|
176
|
+
}
|
|
177
|
+
async doStart(signal) {
|
|
178
|
+
const worker = fork(new URL("./worker.js", import.meta.url), [], {
|
|
179
|
+
execArgv: ["--max-old-space-size=256"], // Never inherit host loaders/preload flags.
|
|
180
|
+
env: {}, // Provider keys stay in the host process.
|
|
181
|
+
stdio: ["ignore", "ignore", "ignore", "ipc"],
|
|
182
|
+
serialization: "json",
|
|
183
|
+
});
|
|
184
|
+
this.child = worker;
|
|
185
|
+
// Only tool calls are handled here; ready/result/closed are consumed by
|
|
186
|
+
// the receive() waiter registered before any message can arrive.
|
|
187
|
+
worker.on("message", (message) => {
|
|
188
|
+
if (message.type === "tool")
|
|
189
|
+
void this.handleToolCall(worker, message);
|
|
190
|
+
});
|
|
191
|
+
worker.on("error", (error) => this.pending?.(error));
|
|
192
|
+
worker.on("exit", (code, sig) => {
|
|
193
|
+
if (this.child !== worker)
|
|
194
|
+
return;
|
|
195
|
+
this.child = undefined;
|
|
196
|
+
const error = new Error(`Repl worker exited (${sig ?? code}). JavaScript state was reset; inspect the page before retrying an action.`);
|
|
197
|
+
for (const [, pending] of [...this.toolPending])
|
|
198
|
+
pending.reject(error);
|
|
199
|
+
this.toolPending.clear();
|
|
200
|
+
if (this.pending)
|
|
201
|
+
this.pending(error);
|
|
202
|
+
else
|
|
203
|
+
this.workerLoss = error;
|
|
204
|
+
});
|
|
205
|
+
try {
|
|
206
|
+
const response = this.receive(worker, READY_TIMEOUT_MS, signal);
|
|
207
|
+
const sendError = worker.send({
|
|
208
|
+
workspace: this.opts.workspace,
|
|
209
|
+
redact: this.opts.redact ?? [],
|
|
210
|
+
maxOutputChars: this.opts.maxOutputChars ?? 12_000,
|
|
211
|
+
...(this.opts.preamble !== undefined ? { preamble: this.opts.preamble } : {}),
|
|
212
|
+
}, (error) => {
|
|
213
|
+
if (error)
|
|
214
|
+
this.pending?.(error);
|
|
215
|
+
});
|
|
216
|
+
if (!sendError)
|
|
217
|
+
this.pending?.(new Error("Repl IPC send failed."));
|
|
218
|
+
const ready = await response;
|
|
219
|
+
if (ready.type !== "ready")
|
|
220
|
+
throw new Error("Repl worker did not initialize.");
|
|
221
|
+
this.starting = undefined;
|
|
222
|
+
return worker;
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
this.starting = undefined;
|
|
226
|
+
await this.terminate();
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async handleToolCall(worker, message) {
|
|
231
|
+
if (message.tool === "javascript") {
|
|
232
|
+
this.replyTool(worker, message.id, undefined, "the javascript tool cannot be called from inside a cell");
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
// The cell deadline is paused ONLY while a tool can block on the user's
|
|
236
|
+
// permission overlay (screenshot, by default): an "Allow once" click must
|
|
237
|
+
// not cost the cell its budget (WS2/T2.1), and ordinary browser work is
|
|
238
|
+
// bounded by the deadline as usual. A denied tool rejects the cell's
|
|
239
|
+
// primitive with a structured `<CODE>: <message>` error (state
|
|
240
|
+
// preserved); only an IGNORED overlay past the cumulative cap aborts the
|
|
241
|
+
// cell (watchdog, state reset). The ledger is cell-scoped: a late
|
|
242
|
+
// completion after the cell ended is a no-op.
|
|
243
|
+
const ledger = this.pauseLedger;
|
|
244
|
+
const pausable = this.permissionTools.has(message.tool);
|
|
245
|
+
if (pausable)
|
|
246
|
+
ledger?.enter();
|
|
247
|
+
try {
|
|
248
|
+
const result = await this.opts.toolExecutor(message.tool, message.args);
|
|
249
|
+
this.replyTool(worker, message.id, result, undefined);
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
// Preserve structured codes (BROWSER_NOT_BOUND, BROWSER_PERMISSION_DENIED, …)
|
|
253
|
+
// in the message the cell sees: `<CODE>: <message>`.
|
|
254
|
+
const code = error?.code;
|
|
255
|
+
const message2 = error instanceof Error ? error.message : String(error);
|
|
256
|
+
this.replyTool(worker, message.id, undefined, typeof code === "string" && code ? `${code}: ${message2}` : message2);
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
if (pausable)
|
|
260
|
+
ledger?.exit();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
replyTool(worker, id, result, error) {
|
|
264
|
+
// Tool results must be JSON-serializable for the JSON IPC channel.
|
|
265
|
+
let payload;
|
|
266
|
+
if (error !== undefined) {
|
|
267
|
+
payload = { error };
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
try {
|
|
271
|
+
JSON.stringify(result);
|
|
272
|
+
payload = { result };
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
payload = { result: String(result) };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const ok = worker.send({ type: "tool-result", id, ...payload }, (err) => {
|
|
279
|
+
if (err) {
|
|
280
|
+
const pending = this.toolPending.get(id);
|
|
281
|
+
if (pending) {
|
|
282
|
+
this.toolPending.delete(id);
|
|
283
|
+
pending.reject(err);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
if (!ok) {
|
|
288
|
+
const pending = this.toolPending.get(id);
|
|
289
|
+
if (pending) {
|
|
290
|
+
this.toolPending.delete(id);
|
|
291
|
+
pending.reject(new Error("Repl IPC closed."));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
receive(worker, timeoutMs, signal) {
|
|
296
|
+
return new Promise((resolve, reject) => {
|
|
297
|
+
let finished = false;
|
|
298
|
+
let running = false;
|
|
299
|
+
let timer;
|
|
300
|
+
const finish = (value) => {
|
|
301
|
+
if (finished)
|
|
302
|
+
return;
|
|
303
|
+
finished = true;
|
|
304
|
+
running = false;
|
|
305
|
+
clearTimeout(timer);
|
|
306
|
+
// Kill the pause ledger with the cell: late completions of an in-flight
|
|
307
|
+
// permission tool (the user answers after the cell was killed/capped)
|
|
308
|
+
// must be no-ops, never touching this or the next cell's state.
|
|
309
|
+
ledger.live = false;
|
|
310
|
+
clearTimeout(ledger.watchdog);
|
|
311
|
+
ledger.watchdog = undefined;
|
|
312
|
+
signal?.removeEventListener("abort", abort);
|
|
313
|
+
worker.off("message", onMessage);
|
|
314
|
+
this.pending = undefined;
|
|
315
|
+
if (value instanceof Error)
|
|
316
|
+
reject(value);
|
|
317
|
+
else
|
|
318
|
+
resolve(value);
|
|
319
|
+
};
|
|
320
|
+
const onMessage = (message) => {
|
|
321
|
+
if (message.type === "ready" || message.type === "result" || message.type === "closed")
|
|
322
|
+
finish(message);
|
|
323
|
+
};
|
|
324
|
+
const abort = () => finish(new Error("Execution cancelled. JavaScript state was reset; browser actions may already have happened."));
|
|
325
|
+
// Pausable deadline: pause()/resume() around tool calls (a permission
|
|
326
|
+
// prompt must not consume the cell's remaining budget, WS2/T2.1).
|
|
327
|
+
// `remaining` counts only unpaused time, so the message stays truthful.
|
|
328
|
+
let remaining = timeoutMs;
|
|
329
|
+
let startedAt = 0;
|
|
330
|
+
const startTimer = () => {
|
|
331
|
+
if (finished || running)
|
|
332
|
+
return;
|
|
333
|
+
running = true;
|
|
334
|
+
startedAt = Date.now();
|
|
335
|
+
timer = setTimeout(() => finish(new Error(`Cell exceeded ${timeoutMs} ms. JavaScript state was reset; inspect the page before retrying actions.`)), remaining);
|
|
336
|
+
};
|
|
337
|
+
const cellTimer = {
|
|
338
|
+
pause: () => {
|
|
339
|
+
if (finished || !running)
|
|
340
|
+
return;
|
|
341
|
+
running = false;
|
|
342
|
+
clearTimeout(timer);
|
|
343
|
+
remaining = Math.max(0, remaining - (Date.now() - startedAt));
|
|
344
|
+
},
|
|
345
|
+
resume: () => startTimer(),
|
|
346
|
+
};
|
|
347
|
+
// Permission-pause ledger for THIS cell (WS2/T2.1): pauses only while
|
|
348
|
+
// outstanding permission tool calls are in flight, measures the UNION
|
|
349
|
+
// of their durations (concurrent calls neither double-count nor resume
|
|
350
|
+
// early), and enforces the cumulative cap with a watchdog that fires
|
|
351
|
+
// WHILE blocked — an ignored overlay cannot hang the cell.
|
|
352
|
+
const ledger = {
|
|
353
|
+
live: true,
|
|
354
|
+
outstanding: 0,
|
|
355
|
+
pausedSince: undefined,
|
|
356
|
+
blockedMs: 0,
|
|
357
|
+
watchdog: undefined,
|
|
358
|
+
enter: () => undefined,
|
|
359
|
+
exit: () => undefined,
|
|
360
|
+
};
|
|
361
|
+
const fireCap = () => {
|
|
362
|
+
if (!ledger.live)
|
|
363
|
+
return;
|
|
364
|
+
const effective = ledger.blockedMs + (ledger.pausedSince !== undefined ? Date.now() - ledger.pausedSince : 0);
|
|
365
|
+
finish(new Error(`Cell paused ${Math.round(effective / 1000)}s inside permission prompts (cap ${Math.round(this.blockCapMs / 1000)}s). ` +
|
|
366
|
+
"JavaScript state was reset; inspect the page before retrying actions."));
|
|
367
|
+
};
|
|
368
|
+
ledger.enter = () => {
|
|
369
|
+
if (!ledger.live)
|
|
370
|
+
return;
|
|
371
|
+
const first = ledger.outstanding === 0;
|
|
372
|
+
ledger.outstanding += 1;
|
|
373
|
+
if (!first)
|
|
374
|
+
return;
|
|
375
|
+
cellTimer.pause();
|
|
376
|
+
ledger.pausedSince = Date.now();
|
|
377
|
+
const grace = this.blockCapMs - ledger.blockedMs;
|
|
378
|
+
if (grace > 0) {
|
|
379
|
+
ledger.watchdog = setTimeout(fireCap, grace);
|
|
380
|
+
ledger.watchdog.unref?.();
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
fireCap();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
ledger.exit = () => {
|
|
387
|
+
if (!ledger.live || ledger.outstanding <= 0)
|
|
388
|
+
return;
|
|
389
|
+
ledger.outstanding -= 1;
|
|
390
|
+
if (ledger.outstanding > 0 || ledger.pausedSince === undefined)
|
|
391
|
+
return;
|
|
392
|
+
ledger.blockedMs += Date.now() - ledger.pausedSince;
|
|
393
|
+
ledger.pausedSince = undefined;
|
|
394
|
+
clearTimeout(ledger.watchdog);
|
|
395
|
+
ledger.watchdog = undefined;
|
|
396
|
+
if (ledger.blockedMs > this.blockCapMs) {
|
|
397
|
+
fireCap();
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
cellTimer.resume();
|
|
401
|
+
};
|
|
402
|
+
this.pauseLedger = ledger;
|
|
403
|
+
startTimer();
|
|
404
|
+
this.pending = (error) => finish(error);
|
|
405
|
+
worker.on("message", onMessage);
|
|
406
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
407
|
+
if (signal?.aborted)
|
|
408
|
+
abort();
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
async terminate() {
|
|
412
|
+
const worker = this.child;
|
|
413
|
+
this.child = undefined;
|
|
414
|
+
for (const [, pending] of [...this.toolPending])
|
|
415
|
+
pending.reject(new Error("Repl worker terminated."));
|
|
416
|
+
this.toolPending.clear();
|
|
417
|
+
if (worker && worker.exitCode === null && worker.signalCode === null) {
|
|
418
|
+
const exited = new Promise((resolve) => worker.once("exit", () => resolve()));
|
|
419
|
+
worker.kill("SIGKILL");
|
|
420
|
+
await exited;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
async function readFileSafe(path) {
|
|
425
|
+
const handle = await open(path, "r");
|
|
426
|
+
try {
|
|
427
|
+
const stat = await handle.stat();
|
|
428
|
+
const buffer = Buffer.alloc(Math.min(stat.size, 64 * 1024));
|
|
429
|
+
await handle.read(buffer, 0, buffer.length, 0);
|
|
430
|
+
return buffer.toString("utf8");
|
|
431
|
+
}
|
|
432
|
+
finally {
|
|
433
|
+
await handle.close();
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repl IPC types (BROWSER-USE-REPL-PLAN.md, Phase 0).
|
|
3
|
+
*
|
|
4
|
+
* The ReplWorker child speaks this small JSON protocol with the host
|
|
5
|
+
* ReplRuntime over Node IPC. The worker never touches the network protocol;
|
|
6
|
+
* every browser primitive becomes a tool request the host executes through
|
|
7
|
+
* the session's existing browser-tool transport.
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=types.js.map
|