Repository navigation
Expand file tree
/
Copy pathcontrol.ts
More file actions
366 lines (363 loc) · 11.9 KB
/
Copy pathcontrol.ts
File metadata and controls
366 lines (363 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import type {
ComputerControlState,
HandoffRequest,
HandoffSource,
} from "../../shared/computer-control";
import { createActionBarrier } from "./action-barrier";
import type { ControlStore, StoredControl } from "./control-store";
export type ControlState = ComputerControlState;
export class ControlError extends Error {
constructor(message: string) {
super(message);
this.name = "ControlError";
}
}
export class ControlRequestError extends Error {
constructor(
message: string,
readonly status: 400 | 404 | 409 = 400,
) {
super(message);
this.name = "ControlRequestError";
}
}
export class SnapshotRequiredError extends Error {
constructor() {
super("Take a fresh browser snapshot after handback before acting.");
this.name = "SnapshotRequiredError";
}
}
export const NO_SECRET_PENDING = "Nothing is waiting for a secret.";
export const HELP_REQUEST_TTL_MS = 10 * 60 * 1000;
export const SECRET_REQUEST_TTL_MS = HELP_REQUEST_TTL_MS;
export const HUMAN_HAS_CONTROL =
"A person has control or has been asked to take control. Wait for them to hand it back before acting.";
export const TAKE_CONTROL_FIRST =
"Take control before driving the computer yourself.";
type Options = {
store?: ControlStore;
invalidateSnapshot?: () => void;
drainTimeoutMs?: number;
};
/** Request identity is durable; secret values never enter this state machine. */
export function createControl(
now: () => string = () => new Date().toISOString(),
options: Options = {},
) {
const barrier = createActionBarrier();
const restored = options.store?.load();
const data: StoredControl = restored ?? {
version: 1,
holder: "bot",
since: now(),
resumeSnapshotRequired: false,
recoveryRequired: false,
requests: [],
aliases: {},
};
let secret:
| { label: string; ref: string; snapshotId?: number; requestedAt: string }
| undefined;
let storageError: Error | undefined;
const current = () =>
data.requests.find((r) => r.id === data.currentRequestId);
const active = (request: HandoffRequest | undefined) =>
request?.status === "waiting" || request?.status === "taken";
function persist() {
// Keep the current identity and the latest 32 terminal records. Their aliases expire together.
const cutoff = Date.parse(now()) - 30 * 24 * 60 * 60 * 1000;
const keep = data.requests
.filter(
(r) =>
r.id === data.currentRequestId || Date.parse(r.updatedAt) >= cutoff,
)
.slice(-33);
const ids = new Set(keep.map((r) => r.id));
data.requests = keep;
data.aliases = Object.fromEntries(
Object.entries(data.aliases)
.filter(([, id]) => ids.has(id))
.slice(-256),
);
if (storageError) throw storageError;
try {
options.store?.save(structuredClone(data));
} catch (error) {
storageError = new Error(
"The handoff state could not be saved. Computer actions are disabled until storage is repaired.",
{ cause: error },
);
barrier.close();
throw storageError;
}
}
function finish(
request: HandoffRequest,
status: "completed" | "cancelled" | "expired" | "interrupted",
interruption?: string,
) {
request.status = status;
request.updatedAt = now();
request.finishedAt = request.updatedAt;
delete request.expiresAt;
if (interruption) request.interruption = interruption;
}
function syncAdmission() {
if (active(current()) || data.holder === "human" || data.recoveryRequired)
barrier.close();
else barrier.open();
}
function expire() {
if (storageError) throw storageError;
const request = current();
if (
request?.status === "waiting" &&
request.expiresAt &&
Date.parse(now()) > Date.parse(request.expiresAt)
) {
finish(request, "expired");
persist();
syncAdmission();
}
if (
secret &&
Date.parse(now()) - Date.parse(secret.requestedAt) > SECRET_REQUEST_TTL_MS
)
secret = undefined;
}
function get(requestId?: string): ControlState {
expire();
const request = requestId
? data.requests.find((r) => r.id === requestId)
: current();
if (requestId && !request)
throw new ControlRequestError("That handoff request was not found.", 404);
const live = current();
return {
holder: data.holder,
since: data.since,
requested: live?.status === "waiting",
reason: active(live) ? live?.reason : undefined,
request: request ? { ...request } : undefined,
transitioning:
(active(live) || data.holder === "human" || data.recoveryRequired) &&
barrier.pending() > 0,
resumeSnapshotRequired: data.resumeSnapshotRequired,
...(secret
? {
secretWanted: secret.label,
secretRef: secret.ref,
secretSnapshotId: secret.snapshotId,
}
: {}),
};
}
function intended(id: string): HandoffRequest {
expire();
const request = current();
if (!request || request.id !== id)
throw new ControlRequestError(
"That request is no longer the current handoff.",
409,
);
return request;
}
function interrupt(reason: string): ControlState {
const request = current();
// Recovery is for a page a person was driving: what they left it as is unknown until someone
// looks. A request nobody took left the page exactly as the Bot had it, and holding the Bot
// until a person "checks" a page no person touched stranded it asking for control it had.
const personHadIt = data.holder === "human" || request?.status === "taken";
if (request && active(request)) finish(request, "interrupted", reason);
if (personHadIt) data.recoveryRequired = true;
data.holder = "bot";
data.since = now();
data.resumeSnapshotRequired = true;
secret = undefined;
options.invalidateSnapshot?.();
persist();
syncAdmission();
return get();
}
// A persisted active request names a page in a previous process. Never call that completion.
if (restored && (active(current()) || data.holder === "human"))
interrupt(
"The computer process restarted; the previous page was interrupted. Request help again to continue.",
);
if (restored && !active(current()) && data.holder === "bot") {
data.resumeSnapshotRequired = true;
persist();
}
syncAdmission();
function rememberAlias(toolCallId: string, requestId: string) {
Object.defineProperty(data.aliases, toolCallId, {
value: requestId,
enumerable: true,
configurable: true,
writable: true,
});
}
return {
get,
requestHelp(
reason: unknown,
toolCallId?: string,
source: HandoffSource = "model",
): ControlState {
expire();
if (
toolCallId !== undefined &&
(!toolCallId.trim() || toolCallId.length > 200)
)
throw new ControlRequestError(
"The tool call id must be a nonempty string of at most 200 characters.",
);
const previous =
toolCallId && Object.hasOwn(data.aliases, toolCallId)
? data.aliases[toolCallId]
: undefined;
if (previous) return get(previous);
const existing = current();
if (active(existing) || data.holder === "human") {
if (toolCallId && existing) {
rememberAlias(toolCallId, existing.id);
persist();
}
return get();
}
barrier.close(); // Before any persistence or awaited browser operation.
const at = now();
const request: HandoffRequest = {
id: crypto.randomUUID(),
toolCallId,
reason:
typeof reason === "string" && reason.trim()
? reason.trim().slice(0, 500)
: "The assistant needs a person to continue.",
source,
status: "waiting",
createdAt: at,
updatedAt: at,
expiresAt: new Date(Date.parse(at) + HELP_REQUEST_TTL_MS).toISOString(),
};
data.currentRequestId = request.id;
data.requests.push(request);
if (toolCallId) rememberAlias(toolCallId, request.id);
persist();
return get();
},
async take(requestId: string): Promise<ControlState> {
const request = intended(requestId);
if (request.status === "taken" && data.holder === "human") return get();
if (request.status !== "waiting")
throw new ControlRequestError(
"That request is no longer waiting for control.",
409,
);
barrier.close();
// Never await here from requestHelp: a navigation detector still owns its action lease.
await barrier.drain(options.drainTimeoutMs);
const after = intended(requestId);
if (after.status === "taken" && data.holder === "human") return get();
if (after.status !== "waiting")
throw new ControlRequestError(
"That request is no longer waiting for control.",
409,
);
after.status = "taken";
after.updatedAt = now();
delete after.expiresAt;
data.holder = "human";
data.since = now();
secret = undefined;
persist();
return get();
},
release(requestId: string): ControlState {
const request = intended(requestId);
if (request.status === "completed" && data.holder === "bot") return get();
if (
data.holder !== "human" ||
(request.status !== "taken" && request.status !== "cancelled")
)
throw new ControlRequestError(
"That request is no longer held by a person.",
409,
);
if (request.status === "taken") finish(request, "completed");
data.holder = "bot";
data.since = now();
data.resumeSnapshotRequired = true;
data.recoveryRequired = false;
secret = undefined;
options.invalidateSnapshot?.();
persist();
syncAdmission();
return get();
},
cancel(requestId: string): ControlState {
const request = intended(requestId);
if (request.status === "cancelled") return get();
if (!active(request))
throw new ControlRequestError("That request is no longer active.", 409);
finish(request, "cancelled");
persist();
syncAdmission();
return get();
},
interrupt,
snapshotTaken() {
if (
data.holder === "bot" &&
data.resumeSnapshotRequired &&
!active(current())
) {
data.resumeSnapshotRequired = false;
persist();
}
},
assertBotMayAct(browserMutation = false) {
expire();
if (data.holder === "human" || active(current()) || data.recoveryRequired)
throw new ControlError(HUMAN_HAS_CONTROL);
if (browserMutation && data.resumeSnapshotRequired)
throw new SnapshotRequiredError();
},
admitBotAction(browserMutation = false): () => void {
this.assertBotMayAct(browserMutation);
return barrier.enter();
},
humanMayDrive(): boolean {
return data.holder === "human" && barrier.pending() === 0;
},
requestSecret(input: {
label?: unknown;
ref?: unknown;
snapshotId?: unknown;
}): ControlState {
if (typeof input.ref !== "string" || !input.ref.trim())
throw new ControlRequestError(
"Say which field the value goes in, using a ref from your snapshot.",
);
secret = {
label:
typeof input.label === "string" && input.label.trim()
? input.label.trim().slice(0, 500)
: "the value this page is asking for",
ref: input.ref.trim(),
snapshotId:
typeof input.snapshotId === "number" ? input.snapshotId : undefined,
requestedAt: now(),
};
return get();
},
pendingSecret(): { ref: string; snapshotId?: number } | null {
expire();
return secret ? { ref: secret.ref, snapshotId: secret.snapshotId } : null;
},
secretSupplied() {
secret = undefined;
},
};
}
export type Control = ReturnType<typeof createControl>;