Files
t3code/apps/server/src/orchestration-v2/ThreadStream.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

265 lines
8.1 KiB
TypeScript

import {
EventId,
ThreadId,
TurnItemId,
type OrchestrationV2ProjectedTurnItem,
type OrchestrationV2ThreadProjection,
type OrchestrationV2TurnItem,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import * as DateTime from "effect/DateTime";
import {
buildBoundedThreadStreamSnapshot,
decideThreadResume,
isThreadReplayRawPayloadSafe,
threadReplayEncodedBytes,
THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES,
THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES,
THREAD_RESUME_MAX_REPLAY_EVENTS,
} from "./ThreadStream.ts";
import { projectDomainEventForWire } from "./WireProjection.ts";
const NOW = DateTime.makeUnsafe("2026-09-08T00:00:00.000Z");
const THREAD_ID = ThreadId.make("thread-stream-test");
const LARGE_PROJECTABLE_OUTPUT_BYTES = 10 * 1_048_576;
const LARGE_PROJECTABLE_RAW_PAYLOAD_BYTES = 10_486_214;
// The first projected replay measured 821 bytes; leave room for small envelope additions.
const MAX_PROJECTED_DYNAMIC_REPLAY_BYTES = 2_048;
function timelineProjection(itemCount: number): OrchestrationV2ThreadProjection {
const visibleTurnItems: OrchestrationV2ProjectedTurnItem[] = Array.from(
{ length: itemCount },
(_, index) => {
const id = TurnItemId.make(`item-${index}`);
const item = {
id,
type: "command_execution",
threadId: THREAD_ID,
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: index + 1,
status: "completed",
title: `Command ${index}`,
input: `command-${index}`,
output: `output-${index}`,
exitCode: 0,
startedAt: NOW,
completedAt: NOW,
updatedAt: NOW,
} satisfies OrchestrationV2TurnItem;
return {
position: index,
visibility: "local",
sourceThreadId: THREAD_ID,
sourceItemId: id,
item,
};
},
);
return {
thread: {
id: THREAD_ID,
projectId: "project-stream-test",
title: "Thread stream test",
providerInstanceId: "codex",
modelSelection: null,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
activeProviderThreadId: null,
lineage: {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: THREAD_ID,
},
forkedFrom: null,
createdBy: "user",
creationSource: "web",
createdAt: NOW,
updatedAt: NOW,
archivedAt: null,
settledOverride: null,
settledAt: null,
deletedAt: null,
},
runs: [],
attempts: [],
nodes: [],
subagents: [],
providerSessions: [],
providerThreads: [],
providerTurns: [],
runtimeRequests: [],
messages: [],
plans: [],
turnItems: visibleTurnItems.map((row) => row.item),
checkpointScopes: [],
checkpoints: [],
contextHandoffs: [],
contextTransfers: [],
visibleTurnItems,
updatedAt: NOW,
} as unknown as OrchestrationV2ThreadProjection;
}
describe("decideThreadResume", () => {
it("replays when the gap is zero", () => {
expect(
decideThreadResume({
afterSequence: 10,
highWater: 10,
replayEventCount: 0,
replayEncodedBytes: 0,
}),
).toEqual({ mode: "replay", afterSequence: 10, throughSequence: 10 });
});
it("replays when the event count is within the bound", () => {
expect(
decideThreadResume({
afterSequence: 10,
highWater: 20_000,
replayEventCount: THREAD_RESUME_MAX_REPLAY_EVENTS,
replayEncodedBytes: THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES,
}),
).toEqual({
mode: "replay",
afterSequence: 10,
throughSequence: 20_000,
});
});
it("falls back to a snapshot when the event count exceeds the bound", () => {
expect(
decideThreadResume({
afterSequence: 10,
highWater: 20_000,
replayEventCount: THREAD_RESUME_MAX_REPLAY_EVENTS + 1,
replayEncodedBytes: 1,
}),
).toEqual({ mode: "snapshot" });
});
it("falls back to a snapshot when encoded replay bytes exceed the bound", () => {
expect(
decideThreadResume({
afterSequence: 10,
highWater: 11,
replayEventCount: 1,
replayEncodedBytes: THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES + 1,
}),
).toEqual({ mode: "snapshot" });
});
it("rejects pathological raw payloads before decoding them", () => {
expect(isThreadReplayRawPayloadSafe(THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES)).toBe(true);
expect(isThreadReplayRawPayloadSafe(THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES + 1)).toBe(false);
// Raw safety and projected transport size are deliberately separate. A
// tiny projected replay is transport-safe even when its raw source is not.
expect(
decideThreadResume({
afterSequence: 9,
highWater: 10,
replayEventCount: 1,
replayEncodedBytes: 1,
}),
).toEqual({ mode: "replay", afterSequence: 9, throughSequence: 10 });
});
it("falls back to a snapshot when the client cursor is ahead of the store", () => {
expect(
decideThreadResume({
afterSequence: 50,
highWater: 40,
replayEventCount: 0,
replayEncodedBytes: 0,
}),
).toEqual({ mode: "snapshot" });
});
it("counts UTF-8 bytes across projected stream items", () => {
expect(threadReplayEncodedBytes([{ value: "a" }, { value: "🦊" }])).toBe(
Buffer.byteLength('{"value":"a"}', "utf8") + Buffer.byteLength('{"value":"🦊"}', "utf8"),
);
});
it("builds socket fallback snapshots with a bounded timeline and history cursor", () => {
const snapshot = buildBoundedThreadStreamSnapshot({
snapshotSequence: 44,
projection: timelineProjection(80),
});
expect(snapshot.kind).toBe("snapshot");
expect(snapshot.snapshotSequence).toBe(44);
expect(snapshot.projection.visibleTurnItems).toHaveLength(75);
expect(snapshot.projection.turnItems).toHaveLength(75);
expect(snapshot.historyCursor).not.toBeNull();
expect(snapshot.hasMoreHistory).toBe(true);
expect(snapshot.latestLocalTurnOrdinal).toBe(80);
expect(snapshot.payloadBudgetExceeded).toBe(false);
});
it("rejects a 10 MiB raw replay even when its projected form fits the wire budget", () => {
const item = {
id: TurnItemId.make("large-dynamic-tool"),
type: "dynamic_tool",
threadId: THREAD_ID,
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
status: "completed",
title: "Large result",
toolName: "mcp__test__large_result",
input: { query: "small" },
output: { text: "x".repeat(LARGE_PROJECTABLE_OUTPUT_BYTES) },
startedAt: NOW,
completedAt: NOW,
updatedAt: NOW,
} satisfies OrchestrationV2TurnItem;
const event = {
id: EventId.make("large-dynamic-tool-event"),
type: "turn-item.updated" as const,
threadId: THREAD_ID,
occurredAt: NOW,
payload: item,
};
const rawReplay = [{ kind: "event" as const, sequence: 10, event }];
const projectedReplay = [
{
kind: "event" as const,
sequence: 10,
event: projectDomainEventForWire(event),
},
];
const rawPayloadBytes = Buffer.byteLength(JSON.stringify(event.payload), "utf8");
expect(rawPayloadBytes).toBe(LARGE_PROJECTABLE_RAW_PAYLOAD_BYTES);
expect(rawPayloadBytes).toBeGreaterThan(THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES);
expect(isThreadReplayRawPayloadSafe(rawPayloadBytes)).toBe(false);
expect(threadReplayEncodedBytes(rawReplay)).toBeGreaterThan(
THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES,
);
const projectedBytes = threadReplayEncodedBytes(projectedReplay);
expect(projectedBytes).toBeLessThanOrEqual(MAX_PROJECTED_DYNAMIC_REPLAY_BYTES);
expect(
decideThreadResume({
afterSequence: 9,
highWater: 10,
replayEventCount: projectedReplay.length,
replayEncodedBytes: projectedBytes,
}),
).toEqual({ mode: "replay", afterSequence: 9, throughSequence: 10 });
});
});