Skip to content
Draft
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
8 changes: 8 additions & 0 deletions .changeset/configurable-stream-flush-interval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@workflow/world": patch
"@workflow/core": patch
"@workflow/world-local": patch
"@workflow/world-postgres": patch
---

Add `streamFlushIntervalMs` option to `Streamer` interface, allowing non-Vercel world implementations to configure the stream write buffer flush interval. Defaults to 10ms (unchanged behavior). Set to 0 for immediate flushing on backends with sub-millisecond writes.
5 changes: 2 additions & 3 deletions packages/core/src/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export class WorkflowServerWritableStream extends WritableStream<Uint8Array> {
for (const w of currentWaiters) w.reject(err);
}
);
}, STREAM_FLUSH_INTERVAL_MS);
}, world.streamFlushIntervalMs ?? STREAM_FLUSH_INTERVAL_MS);
};

super({
Expand Down Expand Up @@ -570,8 +570,7 @@ export class WorkflowServerWritableStream extends WritableStream<Uint8Array> {
// unsettled promise because the cleared timer will never fire.
const waiters = flushWaiters;
flushWaiters = [];
const abortError =
reason ?? new Error("Stream aborted");
const abortError = reason ?? new Error('Stream aborted');
for (const w of waiters) w.reject(abortError);
},
});
Expand Down
49 changes: 49 additions & 0 deletions packages/core/src/writable-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('WorkflowServerWritableStream', () => {
writeToStream: ReturnType<typeof vi.fn>;
writeToStreamMulti: ReturnType<typeof vi.fn>;
closeStream: ReturnType<typeof vi.fn>;
streamFlushIntervalMs?: number;
};

beforeEach(async () => {
Expand Down Expand Up @@ -248,4 +249,52 @@ describe('WorkflowServerWritableStream', () => {
);
});
});

describe('streamFlushIntervalMs', () => {
it('should use world.streamFlushIntervalMs when set to 0 (immediate flush)', async () => {
mockWorld.streamFlushIntervalMs = 0;

const stream = new WorkflowServerWritableStream('s', 'run-1');
const writer = stream.getWriter();

// With interval=0, the flush fires on the next microtask tick via setTimeout(fn, 0)
await writer.write(new Uint8Array([1]));
expect(mockWorld.writeToStream).toHaveBeenCalledTimes(1);

await writer.close();
});

it('should fall back to default interval when streamFlushIntervalMs is undefined', async () => {
// mockWorld has no streamFlushIntervalMs set — uses default 10ms
delete mockWorld.streamFlushIntervalMs;

const stream = new WorkflowServerWritableStream('s', 'run-1');
const writer = stream.getWriter();

await writer.write(new Uint8Array([1]));
expect(mockWorld.writeToStream).toHaveBeenCalledTimes(1);

await writer.close();
});

it('should respect a custom non-zero flush interval', async () => {
mockWorld.streamFlushIntervalMs = 50;

const stream = new WorkflowServerWritableStream('s', 'run-1');
const writer = stream.getWriter();

// Start a write — the flush is scheduled 50ms from now
const writePromise = writer.write(new Uint8Array([1]));

// After 10ms (the old default), data should NOT have flushed yet
await new Promise((r) => setTimeout(r, 10));
expect(mockWorld.writeToStream).not.toHaveBeenCalled();

// Wait for the write to complete (will resolve after the 50ms timer fires)
await writePromise;
expect(mockWorld.writeToStream).toHaveBeenCalledTimes(1);

await writer.close();
});
});
});
5 changes: 5 additions & 0 deletions packages/world-local/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export type Config = {
* `.workflow-data` directory.
*/
tag?: string;
/**
* Override the flush interval (in ms) for buffered stream writes.
* Default is 10ms. Set to 0 for immediate flushing.
*/
streamFlushIntervalMs?: number;
};

export const config = once<Config>(() => {
Expand Down
10 changes: 6 additions & 4 deletions packages/world-local/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export function createLocalWorld(args?: Partial<Config>): LocalWorld {
return {
...queue,
...createStorage(mergedConfig.dataDir, tag),
...instrumentObject(
'world.streams',
createStreamer(mergedConfig.dataDir, tag)
),
...instrumentObject('world.streams', {
...createStreamer(mergedConfig.dataDir, tag),
...(mergedConfig.streamFlushIntervalMs !== undefined && {
streamFlushIntervalMs: mergedConfig.streamFlushIntervalMs,
}),
}),
async start() {
await initDataDir(mergedConfig.dataDir);
},
Expand Down
5 changes: 5 additions & 0 deletions packages/world-postgres/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ type PgConnectionConfig =
export type PostgresWorldConfig = PgConnectionConfig & {
jobPrefix?: string;
queueConcurrency?: number;
/**
* Override the flush interval (in ms) for buffered stream writes.
* Default is 10ms. Set to 0 for immediate flushing.
*/
streamFlushIntervalMs?: number;
};
3 changes: 3 additions & 0 deletions packages/world-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export function createWorld(
...storage,
...streamer,
...queue,
...(config.streamFlushIntervalMs !== undefined && {
streamFlushIntervalMs: config.streamFlushIntervalMs,
}),
async start() {
await queue.start();
},
Expand Down
13 changes: 13 additions & 0 deletions packages/world/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ import type {
} from './steps.js';

export interface Streamer {
/**
* Override the default flush interval (in milliseconds) for buffered stream writes.
* Chunks are accumulated in a buffer and flushed together on this interval.
*
* The default is 10ms, which is appropriate for HTTP-based backends like world-vercel
* where each flush is a network round-trip. For backends with sub-millisecond writes
* (e.g., Redis, local filesystem), a lower value (or 0 for immediate flushing) reduces
* end-to-end stream latency.
*
* Not supported by world-vercel.
*/
streamFlushIntervalMs?: number;

writeToStream(
name: string,
runId: string,
Expand Down
Loading