Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 82 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"packages/telemetry",
"packages/examples",
"packages/cloudflare-runtime",
"packages/gcp-runtime",
"packages/webhook-runtime"
],
"scripts": {
Expand Down
13 changes: 13 additions & 0 deletions packages/cloudflare-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@
"@agent-assistant/surfaces": "^0.3.0",
"@agent-assistant/webhook-runtime": "^0.2.5"
},
"peerDependencies": {
"@agent-assistant/proactive": "^0.4.0",
"@agent-assistant/sessions": "^0.4.0",
"@agent-assistant/vfs": "^0.4.0"
},
"peerDependenciesMeta": {
"@agent-assistant/proactive": { "optional": true },
"@agent-assistant/sessions": { "optional": true },
"@agent-assistant/vfs": { "optional": true }
},
"devDependencies": {
"@agent-assistant/proactive": "^0.4.35",
"@agent-assistant/sessions": "^0.4.35",
"@agent-assistant/vfs": "^0.4.35",
"@cloudflare/workers-types": "^4.20260417.1",
"@types/node": "^24.6.0",
"typescript": "^5.9.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { describe, expect, it, vi } from 'vitest';
import type { ContinuationHarnessAdapter, ContinuationResumedTurnInput } from '@agent-assistant/continuation';
import type { HarnessResult } from '@agent-assistant/harness';
import { CfFiberTurnExecutor, type FiberContextLike, type RunFiberFn } from './cf-fiber-turn-executor.js';
import type { DurableObjectStorageLike } from './cf-continuation-store.js';

function makeStorage(): DurableObjectStorageLike {
const map = new Map<string, unknown>();
return {
async get<T>(key: string) { return map.get(key) as T | undefined; },
async put<T>(key: string, value: T) { map.set(key, value); },
async delete(key: string) { map.delete(key); return true; },
async list<T>() { return new Map<string, T>(); },
};
}

function makeRunFiber(): RunFiberFn {
return async <T>(_storage: DurableObjectStorageLike, fn: (ctx: FiberContextLike) => Promise<T>): Promise<T> => {
const stash = new Map<string, unknown>();
const ctx: FiberContextLike = {
stash<V>(key: string, value: V) { stash.set(key, value); },
stashed<V>(key: string): V | undefined { return stash.get(key) as V | undefined; },
};
return fn(ctx);
};
}

const successResult: HarnessResult = {
outcome: 'complete',
stopReason: 'end_turn',
outputMessages: [],
} as unknown as HarnessResult;

function makeInput(resumedTurnId = 'turn-abc'): ContinuationResumedTurnInput {
return {
resumedTurnId,
continuation: {} as never,
trigger: { type: 'user_reply', message: {} as never, receivedAt: new Date().toISOString() },
};
}

describe('CfFiberTurnExecutor', () => {
it('delegates to the inner harness adapter', async () => {
const inner: ContinuationHarnessAdapter = {
runResumedTurn: vi.fn().mockResolvedValue(successResult),
};
const executor = new CfFiberTurnExecutor({
inner,
storage: makeStorage(),
runFiber: makeRunFiber(),
});
const result = await executor.runResumedTurn(makeInput());
expect(result).toBe(successResult);
expect(inner.runResumedTurn).toHaveBeenCalledOnce();
});

it('returns stashed result without calling inner on recovery', async () => {
const inner: ContinuationHarnessAdapter = {
runResumedTurn: vi.fn().mockResolvedValue(successResult),
};

const stash = new Map<string, unknown>();
// Simulate a fiber that already has a stashed result (recovery scenario)
const recoveryRunFiber: RunFiberFn = async <T>(
_storage: DurableObjectStorageLike,
fn: (ctx: FiberContextLike) => Promise<T>,
): Promise<T> => {
const ctx: FiberContextLike = {
stash<V>(key: string, value: V) { stash.set(key, value); },
stashed<V>(key: string): V | undefined { return stash.get(key) as V | undefined; },
};
return fn(ctx);
};

// Pre-populate the stash with the result for this turn
stash.set('turn:turn-abc', successResult);

const executor = new CfFiberTurnExecutor({
inner,
storage: makeStorage(),
runFiber: recoveryRunFiber,
});

const result = await executor.runResumedTurn(makeInput('turn-abc'));
expect(result).toBe(successResult);
expect(inner.runResumedTurn).not.toHaveBeenCalled();
});

it('stashes result after a successful turn', async () => {
const stash = new Map<string, unknown>();
const inner: ContinuationHarnessAdapter = {
runResumedTurn: vi.fn().mockResolvedValue(successResult),
};

const trackingRunFiber: RunFiberFn = async <T>(
_storage: DurableObjectStorageLike,
fn: (ctx: FiberContextLike) => Promise<T>,
): Promise<T> => {
const ctx: FiberContextLike = {
stash<V>(key: string, value: V) { stash.set(key, value); },
stashed<V>(key: string): V | undefined { return stash.get(key) as V | undefined; },
};
return fn(ctx);
};

const executor = new CfFiberTurnExecutor({
inner,
storage: makeStorage(),
runFiber: trackingRunFiber,
});

await executor.runResumedTurn(makeInput('turn-xyz'));
expect(stash.has('turn:turn-xyz')).toBe(true);
expect(stash.get('turn:turn-xyz')).toBe(successResult);
});

it('propagates errors from the inner harness', async () => {
const inner: ContinuationHarnessAdapter = {
runResumedTurn: vi.fn().mockRejectedValue(new Error('harness failed')),
};
const executor = new CfFiberTurnExecutor({
inner,
storage: makeStorage(),
runFiber: makeRunFiber(),
});
await expect(executor.runResumedTurn(makeInput())).rejects.toThrow('harness failed');
});
});
72 changes: 72 additions & 0 deletions packages/cloudflare-runtime/src/adapters/cf-fiber-turn-executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type {
ContinuationHarnessAdapter,
ContinuationResumedTurnInput,
} from '@agent-assistant/continuation';
import type { HarnessResult } from '@agent-assistant/harness';

import type { DurableObjectStorageLike } from './cf-continuation-store.js';

/**
* Minimal interface for the Cloudflare Flue SDK fiber context.
* Provided by `runFiber()` — each key maps to a SQLite-backed checkpoint slot.
*/
export interface FiberContextLike {
stash<T>(key: string, value: T): void;
stashed<T>(key: string): T | undefined;
}

/**
* Signature for Cloudflare's `runFiber()` from the Flue SDK.
* Wraps an async function in a durable fiber that checkpoints to SQLite via
* the provided Durable Object storage. If the Worker process is interrupted,
* `runFiber` re-invokes `fn` with the same FiberContext — stashed values are
* restored from SQLite so completed work is not repeated.
*/
export type RunFiberFn = <T>(
storage: DurableObjectStorageLike,
fn: (ctx: FiberContextLike) => Promise<T>,
) => Promise<T>;

export interface CfFiberTurnExecutorOptions {
/** The underlying harness adapter that actually executes resumed turns. */
inner: ContinuationHarnessAdapter;
/** Durable Object storage used by runFiber for checkpointing. */
storage: DurableObjectStorageLike;
/**
* The Cloudflare Flue `runFiber` function.
* Pass `runFiber` imported from `@cloudflare/agents` (or equivalent).
*/
runFiber: RunFiberFn;
}

/**
* ContinuationHarnessAdapter that wraps turn execution in a Cloudflare fiber.
*
* Once a resumed turn completes its result is stashed to SQLite. If the
* Worker crashes after the turn finishes but before the result is persisted to
* the continuation store, the next invocation recovers the stashed result
* immediately — no model or tool calls are re-issued.
*/
export class CfFiberTurnExecutor implements ContinuationHarnessAdapter {
private readonly inner: ContinuationHarnessAdapter;
private readonly storage: DurableObjectStorageLike;
private readonly runFiber: RunFiberFn;

constructor(options: CfFiberTurnExecutorOptions) {
this.inner = options.inner;
this.storage = options.storage;
this.runFiber = options.runFiber;
}

async runResumedTurn(input: ContinuationResumedTurnInput): Promise<HarnessResult> {
return this.runFiber(this.storage, async (ctx) => {
const key = `turn:${input.resumedTurnId}`;
const stashed = ctx.stashed<HarnessResult>(key);
if (stashed !== undefined) return stashed;

const result = await this.inner.runResumedTurn(input);
ctx.stash(key, result);
return result;
});
}
}
Loading
Loading