-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisolate.ts
More file actions
318 lines (266 loc) · 11.8 KB
/
Copy pathisolate.ts
File metadata and controls
318 lines (266 loc) · 11.8 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
/// <reference lib="deno.worker" />
/**
* V8 Isolate Worker entry point.
*
* Each agent runs inside its own `new Worker()` isolate. Shared singletons
* (`MessageBus`, `RateLimitCoordinator`) live in the main thread and are
* accessed via postMessage proxies injected before `runAgent()` starts.
*
* Wire protocol (Main → Worker):
* { type: "start", agentConfig, initialPrompt, model, providerConfig, resumeFrom? }
* { type: "cancel" }
* { type: "bus_drain_response", id, messages: BusMessage[] }
* { type: "rate_limit_acquired", id }
*
* Wire protocol (Worker → Main):
* { type: "event", agentName, event: AgentEvent }
* { type: "bus_publish", channel, content, from }
* { type: "bus_drain", id, agentName, channel? }
* { type: "rate_limit_acquire", id, agentName }
* { type: "rate_limit_report", agentName, retryAfterMs }
* { type: "done", agentName, state: string }
* { type: "error", agentName, message }
*/
import type { BusMessage } from "./src/runtime/bus.ts";
import { setBus } from "./src/runtime/bus.ts";
import type { MessageBus } from "./src/runtime/bus.ts";
import { setCoordinator } from "./src/runtime/rate_limiter.ts";
import type { RateLimitCoordinator } from "./src/runtime/rate_limiter.ts";
import { createProvider } from "./src/providers/mod.ts";
import type { ProviderConfig } from "./src/providers/mod.ts";
import {
runAgent,
serializeState,
deserializeState,
type AgentEvent,
type CancelSignal,
} from "./src/runtime/agent.ts";
import type { AgentConfig } from "./src/core/config.ts";
// ---------------------------------------------------------------------------
// RPC layer — request-response over postMessage using callback IDs
// ---------------------------------------------------------------------------
const pending = new Map<number, (value: unknown) => void>();
let nextId = 0;
function rpc(msg: Record<string, unknown>): Promise<unknown> {
const id = nextId++;
return new Promise((resolve) => {
pending.set(id, resolve);
self.postMessage({ ...msg, id });
});
}
// ---------------------------------------------------------------------------
// GraphStoreProxy — proxies memory_write / memory_query to the main thread
// ---------------------------------------------------------------------------
class GraphStoreProxy {
addTriple(subject: string, predicate: string, object: string, graph?: string): void {
self.postMessage({ type: "graph_add_triple", subject, predicate, object, graph });
}
addLiteral(subject: string, predicate: string, value: string | number | boolean, graph?: string): void {
self.postMessage({ type: "graph_add_literal", subject, predicate, value, graph });
}
async query(sparql: string): Promise<Record<string, string>[]> {
const result = await rpc({ type: "graph_query", sparql });
return (result as { rows: Record<string, string>[] }).rows ?? [];
}
// Stubs for methods the tools don't use from isolates
validate(): { conforms: boolean; violations: never[] } { return { conforms: true, violations: [] }; }
describe(): Record<string, string | string[]> { return {}; }
load(): void {}
dump(): string { return ""; }
update(): void {}
}
// ---------------------------------------------------------------------------
// VectorStoreProxy — proxies embedding + vector ops to the main thread
// ---------------------------------------------------------------------------
class VectorStoreProxy {
async ensureCollection(): Promise<void> { /* main thread handles this */ }
async upsert(collection: string, points: Array<{ id: string; vector: number[]; payload: Record<string, unknown> }>): Promise<void> {
self.postMessage({ type: "vector_upsert", collection, points });
}
async search(collection: string, vector: number[], filter?: Record<string, unknown>, limit?: number): Promise<Array<{ id: string; score: number; payload: Record<string, unknown> }>> {
const result = await rpc({ type: "vector_search", collection, vector, filter, limit });
return (result as { points: Array<{ id: string; score: number; payload: Record<string, unknown> }> }).points ?? [];
}
}
class EmbedderProxy {
readonly dimensions = 0;
readonly name = "proxy";
async embed(texts: string[]): Promise<number[][]> {
const result = await rpc({ type: "embed_text", texts });
return (result as { vectors: number[][] }).vectors ?? [];
}
}
// ---------------------------------------------------------------------------
// BusProxy — implements the MessageBus interface over postMessage
// ---------------------------------------------------------------------------
class BusProxy {
async publish(
channel: string,
content: string,
from: string = "system",
): Promise<void> {
// Fire-and-forget: no response needed
self.postMessage({ type: "bus_publish", channel, content, from });
await Promise.resolve();
}
async drain(
subscriberId?: string,
channel?: string,
): Promise<BusMessage[]> {
const result = await rpc({
type: "bus_drain",
agentName: subscriberId,
channel,
});
return (result as { messages: BusMessage[] }).messages ?? [];
}
subscribe(_subscriberId: string, _channels: string[]): void {
// No-op — main thread owns all subscriptions
}
unsubscribe(_subscriberId: string): void {
// No-op — main thread owns subscriptions
}
addRelay(_relay: unknown): void {
// No-op — relays are main-thread only
}
removeRelay(_relay: unknown): void {
// No-op — relays are main-thread only
}
pending(_subscriberId: string): number {
// Cannot check synchronously across the isolate boundary
return 0;
}
}
// ---------------------------------------------------------------------------
// CoordinatorProxy — implements the RateLimitCoordinator interface over postMessage
// ---------------------------------------------------------------------------
class CoordinatorProxy {
async acquire(agentName: string, _cancel?: CancelSignal): Promise<void> {
await rpc({ type: "rate_limit_acquire", agentName });
}
reportRateLimit(agentName: string, retryAfterMs: number): void {
self.postMessage({ type: "rate_limit_report", agentName, retryAfterMs });
}
// Stub methods not called from within isolates
applyRemoteState(_ms: number): void {}
getState(): { cooldownRemainingMs: number } {
return { cooldownRemainingMs: 0 };
}
onCooldown: null = null;
}
// ---------------------------------------------------------------------------
// Singleton proxy instances
// ---------------------------------------------------------------------------
const busProxy = new BusProxy();
const coordinatorProxy = new CoordinatorProxy();
// ---------------------------------------------------------------------------
// Cancellation signal (controlled by "cancel" messages from main thread)
// ---------------------------------------------------------------------------
const cancelSignal: CancelSignal = { cancelled: false };
// ---------------------------------------------------------------------------
// Main message handler
// ---------------------------------------------------------------------------
self.onmessage = async (evt: MessageEvent) => {
const data = evt.data as Record<string, unknown>;
// Handle RPC responses (bus_drain_response, rate_limit_acquired)
if (data.id !== undefined && pending.has(data.id as number)) {
pending.get(data.id as number)!(data);
pending.delete(data.id as number);
return;
}
switch (data.type) {
case "start": {
// Install proxy singletons so getBus() / getCoordinator() inside
// the agent loop return these proxies instead of real singletons.
setBus(busProxy as unknown as MessageBus);
setCoordinator(coordinatorProxy as unknown as RateLimitCoordinator);
// Create the ModelProvider from the serialized ProviderConfig
const providerConfig = data.providerConfig as ProviderConfig;
const provider = createProvider(providerConfig);
// Deserialize resume state if provided
const resumeState = data.resumeFrom
? deserializeState(data.resumeFrom as string)
: undefined;
// Output handler — relay AgentEvent messages to the main thread
const onOutput = (name: string, event: AgentEvent): void => {
self.postMessage({ type: "event", agentName: name, event });
};
// Heartbeat is implicit: the main thread beats on every worker message
const onHeartbeat = (): void => {};
const agentConfig = data.agentConfig as AgentConfig;
const teamRoster = data.teamRoster as Array<{ name: string; role: string }> | undefined;
// Install graph store proxy so memory_write/memory_query work in isolates
const { setGraphStore } = await import("./src/graph/store.ts");
setGraphStore(new GraphStoreProxy() as unknown as import("./src/graph/store.ts").GraphStore);
// Install vector store proxy if main thread has vector store enabled
if (data.vectorEnabled) {
const { setVectorStore, setEmbedder } = await import("./src/vector/mod.ts");
setVectorStore(new VectorStoreProxy() as unknown as import("./src/vector/mod.ts").VectorStore);
setEmbedder(new EmbedderProxy() as unknown as import("./src/vector/mod.ts").EmbeddingProvider);
}
// Inject session-level environment variables into tools
const sessionEnv = data.sessionEnv as Record<string, string> | undefined;
if (sessionEnv && Object.keys(sessionEnv).length > 0) {
const { setSessionEnv: setBashEnv } = await import("./src/tools/bash.ts");
const { setSessionEnv: setGitEnv } = await import("./src/tools/git.ts");
setBashEnv(sessionEnv);
setGitEnv(sessionEnv);
}
// Initialize sandbox handle if orchestrator passed container info
if (data.sandboxContainerName) {
const { ContainerSandboxHandle } = await import("./src/sandbox/mod.ts");
const handle = new ContainerSandboxHandle(
data.sandboxRuntime as import("./src/sandbox/mod.ts").ContainerRuntime,
data.sandboxContainerName as string,
data.sandboxWorkingDir as string,
);
const { setSandboxExecutor: setBashSandbox } = await import("./src/tools/bash.ts");
const { setSandboxExecutor: setGitSandbox } = await import("./src/tools/git.ts");
setBashSandbox(handle);
setGitSandbox(handle);
// Set working dir for file tool path validation in the isolate
const { setWorkingDir: setReadWd } = await import("./src/tools/read_file.ts");
const { setWorkingDir: setWriteWd } = await import("./src/tools/write_file.ts");
const { setWorkingDir: setEditWd } = await import("./src/tools/edit_file.ts");
const { setWorkingDir: setGlobWd } = await import("./src/tools/glob.ts");
const { setWorkingDir: setGrepWd } = await import("./src/tools/grep.ts");
const { setWorkingDir: setListWd } = await import("./src/tools/list_dir.ts");
const wd = data.sandboxWorkingDir as string;
setReadWd(wd);
setWriteWd(wd);
setEditWd(wd);
setGlobWd(wd);
setGrepWd(wd);
setListWd(wd);
}
try {
const finalState = await runAgent(
provider,
agentConfig,
data.initialPrompt as string,
onOutput,
onHeartbeat,
resumeState,
cancelSignal,
undefined,
teamRoster,
);
self.postMessage({
type: "done",
agentName: agentConfig.name,
state: serializeState(finalState),
});
} catch (err) {
self.postMessage({
type: "error",
agentName: agentConfig.name,
message: (err as Error).message,
});
}
break;
}
case "cancel":
cancelSignal.cancelled = true;
break;
}
};