-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathagent-coordinator.ts
More file actions
646 lines (605 loc) · 21.6 KB
/
Copy pathagent-coordinator.ts
File metadata and controls
646 lines (605 loc) · 21.6 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import type {
AgentExecutionStore,
AgentSubmission,
AgentSubmissionStore,
} from '../agent-execution-store.ts';
import type { FlueContextInternal } from '../client.ts';
import {
createAgentSubmissionObserverRegistry,
type createAgentSubmissionSessionHandler,
createDirectAgentSubmissionInput,
processSubmission,
reconcileInterruptedSubmission,
submissionSyntheticRequest,
} from '../runtime/agent-submissions.ts';
import { agentStreamPath } from '../runtime/event-stream-store.ts';
import { assertAgentDispatchAdmissionInput, handleAgentRequest } from '../runtime/handle-agent.ts';
import { handleStreamHead, handleStreamRead } from '../runtime/handle-stream-routes.ts';
import { isStreamExcludedEvent } from '../runtime/run-store.ts';
import { deleteSessionTree } from '../session.ts';
import type { AttachedAgentEvent, DirectAgentPayload } from '../types.ts';
import { createSqlAgentExecutionStore } from './agent-execution-store.ts';
export const CLOUDFLARE_AGENT_INTERNAL_DISPATCH_PATH = '/__flue/internal/dispatch';
const FLUE_AGENT_SUBMISSION_WAKE_CALLBACK = '__flueWakeAgentSubmissions';
const FLUE_AGENT_SUBMISSION_WAKE_SECONDS = 30;
const FLUE_AGENT_SUBMISSION_ATTEMPT_STALE_MS = 15 * 60 * 1000;
const FLUE_AGENT_SUBMISSION_ATTEMPT_FIBER = 'flue:submission-attempt';
import type { SqlStorage } from '../sql-storage.ts';
interface CloudflareAgentStorage {
sql?: SqlStorage;
transactionSync?<T>(closure: () => T): T;
}
interface CloudflareAgentInstance {
readonly name: string;
readonly env: Record<string, unknown>;
readonly ctx: {
readonly id: { toString(): string };
readonly storage: CloudflareAgentStorage;
};
__unsafe_ensureInitialized(): Promise<void>;
schedule(
delaySeconds: number,
callback: string,
payload: undefined,
options: { idempotent: boolean },
): Promise<unknown>;
runFiber(
name: string,
callback: (ctx: { stash(snapshot: unknown): void }) => Promise<void>,
): Promise<void>;
}
interface CloudflareAgentRecoveredFiberContext {
readonly name?: string;
readonly snapshot?: Record<string, unknown>;
}
interface CloudflareAgentPreparedCoordinator {
readonly agentName: string;
readonly executionStore: AgentExecutionStore;
}
interface CloudflareAgentRuntimeOptions {
readonly createdAgents: Record<string, Parameters<typeof createAgentSubmissionSessionHandler>[0]>;
readonly createContext: (options: {
readonly executionStore: AgentExecutionStore;
readonly instance: CloudflareAgentInstance;
readonly payload: unknown;
readonly request: Request;
readonly initialEventIndex?: number;
readonly dispatchId?: string;
}) => FlueContextInternal;
readonly runWithInstanceContext: <T>(
instance: CloudflareAgentInstance,
agentName: string,
callback: () => T,
) => T;
readonly createEventStreamStore: (
instance: CloudflareAgentInstance,
) => import('../runtime/event-stream-store.ts').EventStreamStore;
}
export interface CloudflareAgentRuntime {
prepare(options: {
readonly storage: CloudflareAgentStorage;
readonly className: string;
readonly agentName: string;
}): CloudflareAgentPreparedCoordinator;
attach(instance: CloudflareAgentInstance, prepared: CloudflareAgentPreparedCoordinator): void;
onStart(
instance: CloudflareAgentInstance,
inherited: () => Promise<unknown> | unknown,
): Promise<void>;
wakeSubmissions(instance: CloudflareAgentInstance): Promise<void>;
onRequest(instance: CloudflareAgentInstance, request: Request): Promise<Response | null>;
onFiberRecovered(
instance: CloudflareAgentInstance,
ctx: CloudflareAgentRecoveredFiberContext,
inherited: () => Promise<unknown> | unknown,
): Promise<unknown>;
}
export function createCloudflareAgentRuntime(
options: CloudflareAgentRuntimeOptions,
): CloudflareAgentRuntime {
const coordinators = new WeakMap<CloudflareAgentInstance, CloudflareAgentCoordinator>();
const observers = createAgentSubmissionObserverRegistry();
const activeAttempts = new Set<string>();
const getCoordinator = (instance: CloudflareAgentInstance): CloudflareAgentCoordinator => {
const coordinator = coordinators.get(instance);
if (!coordinator) {
throw new Error('[flue] Generated Cloudflare agent coordinator was not initialized.');
}
return coordinator;
};
return {
prepare({ storage, className, agentName }) {
return {
agentName,
executionStore: createSqlAgentExecutionStore(storage, className),
};
},
attach(instance, prepared) {
coordinators.set(
instance,
new CloudflareAgentCoordinator(
instance,
prepared,
options,
options.createEventStreamStore(instance),
observers,
activeAttempts,
),
);
},
onStart(instance, inherited) {
return getCoordinator(instance).onStart(inherited);
},
wakeSubmissions(instance) {
return getCoordinator(instance).wakeSubmissions();
},
onRequest(instance, request) {
return getCoordinator(instance).onRequest(request);
},
onFiberRecovered(instance, ctx, inherited) {
return getCoordinator(instance).onFiberRecovered(ctx, inherited);
},
};
}
class CloudflareAgentCoordinator {
constructor(
private readonly instance: CloudflareAgentInstance,
private readonly prepared: CloudflareAgentPreparedCoordinator,
private readonly options: CloudflareAgentRuntimeOptions,
private readonly eventStreamStore: import('../runtime/event-stream-store.ts').EventStreamStore,
private readonly observers: ReturnType<typeof createAgentSubmissionObserverRegistry>,
private readonly activeAttempts: Set<string>,
) {}
async onStart(inherited: () => Promise<unknown> | unknown): Promise<void> {
await this.restoreSubmissionWake();
await inherited();
await this.resumePendingSessionDeletions();
await this.reconcileSubmissions({ driverAlreadyArmed: true });
}
/**
* Resume session deletions interrupted by an eviction or crash. A durable
* deletion marker written before the interruption blocks every admission
* for that session until deletion completes, so re-run the (idempotent)
* deletion to clear it. Failures are logged and left for the next start;
* the marker keeps the session safely blocked meanwhile.
*/
private async resumePendingSessionDeletions(): Promise<void> {
for (const sessionKey of await this.submissions.listPendingSessionDeletions()) {
try {
await this.submissions.deleteSession(sessionKey, () =>
deleteSessionTree(this.executionStore.sessions, sessionKey),
);
} catch (error) {
console.error(
'[flue:session-deletion]',
{
agentName: this.agentName,
instanceId: this.instance.name,
sessionKey,
operation: 'resume_session_deletion',
outcome: 'failed',
},
error,
);
}
}
}
async wakeSubmissions(): Promise<void> {
if (!(await this.submissions.hasUnsettledSubmissions())) return;
await this.armSubmissionWake({ idempotent: false });
await this.reconcileSubmissions({ driverAlreadyArmed: true });
}
async onRequest(request: Request): Promise<Response | null> {
if (isInternalDispatchRequest(request)) return this.admitDispatch(request);
// DS stream read (GET/HEAD) — served from the event stream store.
const method = request.method;
if (method === 'GET' || method === 'HEAD') {
const store = this.eventStreamStore;
const streamPath = agentStreamPath(this.agentName, this.instance.name);
if (method === 'HEAD') return await handleStreamHead(store, streamPath);
return handleStreamRead({ store, path: streamPath, request });
}
return this.runWithInstanceContext(() =>
handleAgentRequest({
request,
id: this.instance.name,
agentName: this.agentName,
eventStreamStore: this.eventStreamStore,
admitAttachedSubmission: (payload, onEvent, waitForResult) =>
this.admitAttachedSubmission(payload, onEvent, waitForResult),
}),
);
}
async onFiberRecovered(
ctx: CloudflareAgentRecoveredFiberContext,
inherited: () => Promise<unknown> | unknown,
): Promise<unknown> {
if (ctx.name !== FLUE_AGENT_SUBMISSION_ATTEMPT_FIBER) return inherited();
const submissionId = ctx.snapshot?.submissionId;
const attemptId = ctx.snapshot?.attemptId;
if (typeof submissionId !== 'string' || typeof attemptId !== 'string') return inherited();
await this.restoreSubmissionWake();
await this.submissions.requestSubmissionRecovery({ submissionId, attemptId });
await this.reconcileSubmissions({ driverAlreadyArmed: true });
}
private get agentName(): string {
return this.prepared.agentName;
}
private get executionStore(): AgentExecutionStore {
return this.prepared.executionStore;
}
private get submissions(): AgentSubmissionStore {
return this.executionStore.submissions;
}
private runWithInstanceContext<T>(callback: () => T): T {
return this.options.runWithInstanceContext(this.instance, this.agentName, callback);
}
private createContext(
payload: unknown,
request: Request,
initialEventIndex?: number,
dispatchId?: string,
): FlueContextInternal {
return this.options.createContext({
executionStore: this.executionStore,
instance: this.instance,
payload,
request,
initialEventIndex,
dispatchId,
});
}
/**
* Create a context whose events are appended to the agent's durable event
* stream. Used for both submission processing and reconciliation so events
* emitted on either path (including reconciliation-driven settlement)
* reach detached stream readers.
*/
private createDurableContext(
payload: unknown,
request: Request,
dispatchId?: string,
): FlueContextInternal {
const ctx = this.createContext(payload, request, undefined, dispatchId);
const streamPath = agentStreamPath(this.agentName, this.instance.name);
ctx.subscribeEvent((event) => {
if (isStreamExcludedEvent(event)) return;
this.eventStreamStore.appendEvent(streamPath, event).catch((error) => {
console.error('[flue:event-stream] appendEvent failed:', error);
});
});
return ctx;
}
private assertAgentsDurabilityApi(method: 'runFiber' | 'schedule'): void {
if (typeof this.instance[method] !== 'function') {
throw new Error(
`[flue] The installed "agents" package does not provide the required Cloudflare Agents SDK method "${method}". Install or upgrade the "agents" package in your project.`,
);
}
}
private armSubmissionWake(
options: { delaySeconds?: number; idempotent?: boolean } = {},
): Promise<unknown> {
this.assertAgentsDurabilityApi('schedule');
return this.instance.schedule(
options.delaySeconds ?? FLUE_AGENT_SUBMISSION_WAKE_SECONDS,
FLUE_AGENT_SUBMISSION_WAKE_CALLBACK,
undefined,
{ idempotent: options.idempotent ?? true },
);
}
private async restoreSubmissionWake(): Promise<boolean> {
if (!(await this.submissions.hasUnsettledSubmissions())) return false;
await this.armSubmissionWake();
return true;
}
private async reconcileSubmissions(
options: { driverAlreadyArmed?: boolean } = {},
): Promise<boolean> {
if (!(await this.submissions.hasUnsettledSubmissions())) return false;
if (!options.driverAlreadyArmed) await this.restoreSubmissionWake();
try {
// The marker scan is advisory: a fresh marker suppresses
// re-reconciling an attempt that may still be running. If the scan
// itself fails, degrade to an empty marker set instead of aborting —
// a hard failure here would permanently block claiming and hang
// attached callers, while double-processing is bounded by the claim
// CAS and attempt-id ownership checks.
let attemptMarkers: ReadonlySet<string>;
try {
attemptMarkers = await this.listActiveAttemptMarkers();
} catch (error) {
attemptMarkers = new Set();
console.error(
'[flue:submission-reconciliation]',
{
agentName: this.agentName,
instanceId: this.instance.name,
operation: 'list_attempt_markers',
outcome: 'degraded_to_empty_marker_set',
},
error,
);
}
for (const submission of await this.submissions.listRunningSubmissions()) {
if (this.activeAttempts.has(this.submissionAttemptLocalKey(submission))) continue;
if (
attemptMarkers.has(submissionAttemptMarkerKey(submission)) &&
submission.recoveryRequestedAt === undefined
)
continue;
try {
await this.reconcileInterruptedSubmission(submission);
} catch (error) {
this.logSubmissionReconciliationFailure(submission, 'reconcile_submission', error);
}
}
for (const submission of await this.submissions.listRunnableSubmissions()) {
// Cloudflare DOs are single-threaded per instance — leases are
// advisory-only. Set to 0 so reconciliation never misidentifies
// an active submission as expired. The Node coordinator uses real
// lease expiry with heartbeat renewal for multi-process safety.
const claimed = await this.submissions.claimSubmission({
submissionId: submission.submissionId,
attemptId: crypto.randomUUID(),
ownerId: this.instance.ctx.id.toString(),
leaseExpiresAt: 0,
});
if (!claimed) continue;
try {
await this.startSubmissionAttempt(claimed);
} catch (error) {
this.logSubmissionReconciliationFailure(claimed, 'start_submission', error);
}
}
} catch (error) {
console.error(
'[flue:submission-reconciliation]',
{
agentName: this.agentName,
instanceId: this.instance.name,
operation: 'reconcile',
outcome: 'deferred_to_scheduled_wake',
},
error,
);
return true;
}
return await this.submissions.hasUnsettledSubmissions();
}
private logSubmissionReconciliationFailure(
submission: AgentSubmission,
operation: 'reconcile_submission' | 'start_submission',
error: unknown,
): void {
console.error(
'[flue:submission-reconciliation]',
{
agentName: this.agentName,
instanceId: this.instance.name,
submissionId: submission.submissionId,
sessionKey: submission.sessionKey,
attemptId: submission.attemptId,
operation,
outcome: 'deferred_to_scheduled_wake',
},
error,
);
}
private async reconcileInterruptedSubmission(submission: AgentSubmission): Promise<void> {
const agent = this.options.createdAgents[this.agentName];
if (!agent) throw new Error('[flue] Agent target unavailable during durable reconciliation.');
// Ensure the agent event stream exists (idempotent, normally created
// at first accepted processing) so a settlement event emitted during
// reconciliation lands durably even when the previous incarnation
// died before creating it. Best-effort: settlement must never depend
// on event-stream plumbing.
await Promise.resolve(
this.eventStreamStore.createStream(agentStreamPath(this.agentName, this.instance.name)),
).catch((error) => {
console.error('[flue:event-stream] createStream failed:', error);
});
const reconciled = await this.runWithInstanceContext(() =>
reconcileInterruptedSubmission(
this.submissions,
submission,
agent,
(payload, dispatchId) =>
this.createDurableContext(
payload,
submissionSyntheticRequest(submission.input),
dispatchId,
),
{ ownerId: this.instance.ctx.id.toString(), leaseExpiresAt: 0 },
),
);
if (reconciled.disposition === 'replacement') {
await this.startSubmissionAttempt(reconciled.submission);
} else if (submission.kind === 'direct') {
// Observer resolution is best-effort and per-process: only a
// waiting caller attached in this process can be resolved here.
// Detached observers see the durable settlement event.
if (reconciled.disposition === 'completed') {
this.observers.complete(submission.submissionId, reconciled.result);
} else if (reconciled.disposition === 'failed') {
this.observers.fail(submission.submissionId, reconciled.error);
}
}
}
private async startSubmissionAttempt(submission: AgentSubmission): Promise<void> {
if (submission.status !== 'running' || !submission.attemptId) return;
const attempt = { submissionId: submission.submissionId, attemptId: submission.attemptId };
const attemptKey = this.submissionAttemptLocalKey(submission);
if (this.activeAttempts.has(attemptKey)) return;
this.assertAgentsDurabilityApi('runFiber');
this.activeAttempts.add(attemptKey);
let running: Promise<void>;
try {
// Flue's own durable evidence that this attempt started; deleted at
// settlement. The fiber stash below stays for the SDK's crash replay
// (onFiberRecovered); the marker is what reconciliation reads.
await this.submissions.insertAttemptMarker(attempt);
running = this.instance.runFiber(FLUE_AGENT_SUBMISSION_ATTEMPT_FIBER, async (fiberCtx) => {
fiberCtx.stash({ submissionId: submission.submissionId, attemptId: submission.attemptId });
await this.processSubmissionEntry(submission);
});
} catch (error) {
this.activeAttempts.delete(attemptKey);
await this.deleteAttemptMarkerSafely(attempt);
throw error;
}
void running
.catch((error) => {
console.error(
'[flue:submission-processing]',
{
agentName: this.agentName,
instanceId: this.instance.name,
submissionId: submission.submissionId,
operation: 'process',
outcome: 'failed',
},
error,
);
})
.finally(() => {
this.activeAttempts.delete(attemptKey);
void this.deleteAttemptMarkerSafely(attempt);
});
}
/**
* Delete the attempt marker at settlement. Deletion failures are logged
* rather than thrown: a leftover marker only delays reconciliation of
* this attempt until the staleness cutoff expires.
*/
private async deleteAttemptMarkerSafely(attempt: {
submissionId: string;
attemptId: string;
}): Promise<void> {
try {
await this.submissions.deleteAttemptMarker(attempt);
} catch (error) {
console.error(
'[flue:submission-reconciliation]',
{
agentName: this.agentName,
instanceId: this.instance.name,
submissionId: attempt.submissionId,
attemptId: attempt.attemptId,
operation: 'delete_attempt_marker',
outcome: 'marker_left_until_stale',
},
error,
);
}
}
private submissionAttemptLocalKey(submission: AgentSubmission): string {
return `${this.instance.ctx.id.toString()}:${submission.attemptId}`;
}
private async listActiveAttemptMarkers(): Promise<Set<string>> {
const keys = new Set<string>();
for (const marker of await this.submissions.listAttemptMarkers()) {
if (Date.now() - marker.createdAt > FLUE_AGENT_SUBMISSION_ATTEMPT_STALE_MS) continue;
keys.add(`${marker.submissionId}:${marker.attemptId}`);
}
return keys;
}
private async processSubmissionEntry(submission: AgentSubmission): Promise<void> {
// Ensure the agent event stream exists before processing. createStream
// is idempotent — safe to call on every submission.
await this.eventStreamStore.createStream(agentStreamPath(this.agentName, this.instance.name));
await processSubmission({
submissions: this.submissions,
submission,
resolveAgent: (name) => {
const agent = this.options.createdAgents[name];
if (!agent) throw new Error('[flue] Agent target unavailable during durable processing.');
return agent;
},
createContext: (payload, dispatchId) =>
this.createDurableContext(
payload,
submissionSyntheticRequest(submission.input),
dispatchId,
),
observers: this.observers,
wrapExecution: (fn) => this.runWithInstanceContext(fn),
onSettled: () => {
void this.reconcileSubmissions().catch((error) => {
console.error(
'[flue:submission-reconciliation]',
{
agentName: this.agentName,
instanceId: this.instance.name,
operation: 'settlement',
outcome: 'reconcile_failed',
},
error,
);
});
},
});
}
private async admitAttachedSubmission(
payload: DirectAgentPayload,
onEvent?: (event: AttachedAgentEvent) => Promise<void> | void,
waitForResult = true,
) {
const input = createDirectAgentSubmissionInput({
agent: this.agentName,
id: this.instance.name,
payload,
});
const attachment = this.observers.attach(input.submissionId, { onEvent });
try {
await this.armSubmissionWake();
await this.submissions.admitDirect(input);
await this.reconcileSubmissions({ driverAlreadyArmed: true });
if (!waitForResult) return { submissionId: input.submissionId };
return { submissionId: input.submissionId, result: await attachment.completion };
} catch (error) {
// If admission or reconciliation fails before the claim loop
// could pick up this submission, fail the observer so the
// caller's completion promise rejects instead of hanging.
this.observers.fail(input.submissionId, error);
throw error;
} finally {
attachment.detach();
}
}
private async admitDispatch(request: Request): Promise<Response> {
const input: unknown = await request.json();
assertAgentDispatchAdmissionInput(input);
if (input.agent !== this.agentName || input.id !== this.instance.name) {
return new Response('Invalid internal dispatch target.', { status: 400 });
}
if (!this.options.createdAgents[this.agentName]) {
return new Response('Dispatch target unavailable.', { status: 404 });
}
await this.armSubmissionWake();
const admission = await this.submissions.admitDispatch(input);
if (admission.kind === 'retained_receipt') {
return Response.json({
dispatchId: admission.receipt.submissionId,
acceptedAt: new Date(admission.receipt.acceptedAt).toISOString(),
});
}
if (admission.kind === 'conflict') {
return new Response('Conflicting internal dispatch replay.', { status: 409 });
}
await this.reconcileSubmissions({ driverAlreadyArmed: true });
return Response.json({
dispatchId: admission.submission.submissionId,
acceptedAt: input.acceptedAt,
});
}
}
function submissionAttemptMarkerKey(submission: AgentSubmission): string {
return `${submission.submissionId}:${submission.attemptId}`;
}
function isInternalDispatchRequest(request: Request): boolean {
return (
request.method === 'POST' &&
new URL(request.url).pathname === CLOUDFLARE_AGENT_INTERNAL_DISPATCH_PATH
);
}