Files
t3code/apps/server/src/orchestration-v2/WireProjection.test.ts
T

404 lines
15 KiB
TypeScript

import {
MessageId,
ContextHandoffId,
ProviderThreadId,
NodeId,
EventId,
ProjectId,
ProviderDriverKind,
ProviderInstanceId,
ThreadId,
TurnItemId,
type OrchestrationV2ThreadProjection,
OrchestrationV2TurnItem,
OrchestrationV2TurnItemJson,
} from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as DateTime from "effect/DateTime";
import * as Schema from "effect/Schema";
import {
projectTurnItemForWire,
projectTurnItemForDetail,
projectDomainEventForWire,
} from "./WireProjection.ts";
import { threadShellFromProjection } from "./ProjectionStore.ts";
import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages } from "@t3tools/shared/toolOutput";
const decodeTurnItem = Schema.decodeUnknownSync(OrchestrationV2TurnItem);
const encodeTurnItemJson = Schema.encodeSync(OrchestrationV2TurnItemJson);
const decodeTurnItemJson = Schema.decodeUnknownSync(OrchestrationV2TurnItemJson);
const base = {
id: TurnItemId.make("tool-1"),
type: "dynamic_tool" as const,
threadId: ThreadId.make("thread-1"),
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
status: "completed" as const,
title: "MCP tool",
toolName: "mcp__github__fetch_pr",
input: { pr: 42 },
startedAt: DateTime.makeUnsafe("2026-08-13T00:00:00.000Z"),
completedAt: DateTime.makeUnsafe("2026-08-13T00:00:01.000Z"),
updatedAt: DateTime.makeUnsafe("2026-08-13T00:00:01.000Z"),
};
describe("orchestration V2 wire projection", () => {
it("keeps copied handoff transcripts out of activity items and live events", () => {
const item = {
...base,
type: "handoff" as const,
contextHandoffId: ContextHandoffId.make("handoff:wire"),
fromProviderThreadIds: [],
toProviderThreadId: ProviderThreadId.make("target"),
fromProviderInstanceIds: [],
toProviderInstanceId: ProviderInstanceId.make("codex"),
strategy: "full_thread_summary" as const,
summary: "PRIVATE_HANDOFF_TRANSCRIPT",
};
const projected = projectTurnItemForWire(item);
expect(projected).not.toHaveProperty("summary");
expect(decodeTurnItem(projected)).toMatchObject({ contextHandoffId: item.contextHandoffId });
const event = {
id: EventId.make("handoff:wire"),
type: "turn-item.updated" as const,
threadId: base.threadId,
occurredAt: base.updatedAt,
payload: item,
};
expect(JSON.stringify(projectDomainEventForWire(event))).not.toContain(item.summary);
expect(item.summary).toBe("PRIVATE_HANDOFF_TRANSCRIPT");
});
it("preserves image metadata through wire and JSON contracts while redacting output", () => {
const item = {
...base,
toolName: "Read",
input: { file_path: "/workspace/reference.png" },
viewedImagePath: "/workspace/reference.png",
output: { data: "private-image-data" },
} satisfies OrchestrationV2TurnItem;
const projected = projectTurnItemForWire(item);
const live = decodeTurnItem(projected);
const encoded = encodeTurnItemJson(live);
const json = decodeTurnItemJson(encoded);
const decoded = decodeTurnItem(json);
expect(decoded).toMatchObject({ viewedImagePath: "/workspace/reference.png" });
expect(decoded).not.toHaveProperty("output");
expect(item.output.data).toBe("private-image-data");
});
it("preserves provider notices in bounded items and live events", () => {
const item = {
...base,
type: "system_notice" as const,
message: "Safeguards flagged this message. Switched to Opus 4.8.",
};
expect(projectTurnItemForWire(item)).toEqual(item);
const event = {
id: EventId.make("notice-wire-event"),
type: "turn-item.updated" as const,
threadId: item.threadId,
occurredAt: item.updatedAt,
payload: item,
};
expect(projectDomainEventForWire(event)).toEqual(event);
});
it("keeps transcript bodies out of shell rows", () => {
const now = DateTime.makeUnsafe("2026-08-13T00:00:00.000Z");
const threadId = ThreadId.make("thread-shell-budget");
const projection = {
thread: {
createdBy: "user",
creationSource: "web",
id: threadId,
projectId: ProjectId.make("project-shell-budget"),
title: "Shell payload budget",
providerInstanceId: ProviderInstanceId.make("codex"),
modelSelection: null,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
lineage: {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: threadId,
},
forkedFrom: null,
activeProviderThreadId: null,
createdAt: now,
updatedAt: now,
archivedAt: null,
settledOverride: null,
settledAt: null,
deletedAt: null,
},
runs: [],
attempts: [],
runtimeRequests: [],
messages: [
{
id: MessageId.make("message-shell-budget"),
role: "assistant",
text: "x".repeat(1_000_000),
updatedAt: now,
},
],
providerSessions: [],
providerThreads: [],
turnItems: [],
visibleTurnItems: [],
plans: [],
updatedAt: now,
} as unknown as OrchestrationV2ThreadProjection;
const shell = threadShellFromProjection(projection);
expect(shell.latestVisibleMessage).toBeNull();
expect(JSON.stringify(shell).length).toBeLessThan(2_000);
});
it("omits oversized dynamic tool results without mutating persistence data", () => {
const output = { content: [{ type: "text", text: "first line\n" + "x".repeat(100_000) }] };
const item = { ...base, output } satisfies OrchestrationV2TurnItem;
const projected = projectTurnItemForWire(item);
expect(item.output).toBe(output);
expect(projected).not.toBe(item);
expect(JSON.stringify(projected).length).toBeLessThan(2_000);
expect(projected).not.toHaveProperty("output");
});
it("omits even small dynamic tool results while retaining input", () => {
const item = { ...base, output: { ok: true } } satisfies OrchestrationV2TurnItem;
expect(projectTurnItemForWire(item)).toEqual({ ...base, outputOmitted: true });
expect(item.output).toEqual({ ok: true });
});
it("keeps undefined dynamic input intact", () => {
const item = { ...base, input: undefined } satisfies OrchestrationV2TurnItem;
expect(projectTurnItemForWire(item)).toEqual(item);
});
it.each([
[" \t\r\n\n first\t line \r\n" + "x\n".repeat(10_000), "first line"],
[" \n".repeat(10_000), "Large tool output"],
["a".repeat(160) + "\n" + "x".repeat(20_000), "a".repeat(160)],
["a".repeat(161) + "\n" + "x".repeat(20_000), "a".repeat(159) + "…"],
["a".repeat(159) + "\t b" + "x".repeat(20_000), "a".repeat(159) + "…"],
[" café\u00a0\u2003😀 \t\r\n" + "x".repeat(20_000), "café 😀"],
])("preserves bounded input summary normalization for case %#", (input, summary) => {
const projected = projectTurnItemForWire({ ...base, input });
expect(projected.type === "dynamic_tool" ? projected.input : null).toEqual({
summary,
truncated: true,
});
});
it("uses encoded JSON bytes for strings near the dynamic-value limit", () => {
const small = '"'.repeat(8_191);
const large = '"'.repeat(8_192);
const smallItem = { ...base, input: small };
expect(projectTurnItemForWire(smallItem)).toEqual(smallItem);
const projected = projectTurnItemForWire({ ...base, input: large });
expect(projected.type === "dynamic_tool" ? projected.input : null).toEqual({
summary: '"'.repeat(159) + "…",
truncated: true,
});
});
it("serializes an oversized structured input only once for its summary", () => {
let serializations = 0;
const input = {
toJSON() {
serializations += 1;
return { text: "x".repeat(100_000) };
},
};
const projected = projectTurnItemForWire({ ...base, input });
expect(serializations).toBe(1);
expect(projected.type === "dynamic_tool" ? projected.input : null).toMatchObject({
truncated: true,
});
});
it("truncates detail at a UTF-8 boundary without changing the source", () => {
const progress = "a".repeat(32_767) + "😀" + "x".repeat(100_000);
const item = {
...base,
type: "subagent" as const,
subagentId: NodeId.make("child-agent"),
origin: "app_owned" as const,
driver: ProviderDriverKind.make("codex"),
providerInstanceId: ProviderInstanceId.make("codex"),
childThreadId: null,
prompt: "Inspect code",
result: null,
progress,
};
const projected = projectTurnItemForWire(item);
expect(projected.type === "subagent" ? projected.progress : null).toBe(
"a".repeat(32_767) + "\n… output truncated for transport",
);
expect(item.progress).toBe(progress);
});
it.each(["", "small output", "x".repeat(1_048_576)])(
"omits command output of every size, case %#",
(output) => {
const item = { ...base, type: "command_execution" as const, input: "test", output };
const projected = projectTurnItemForWire(item);
expect(projected).not.toHaveProperty("output");
expect(projected).toMatchObject({ input: "test", status: "completed" });
// Clients fetch withheld output on demand, so they need to know it exists.
expect(projected.type === "command_execution" ? projected.outputOmitted : null).toBe(
output ? true : undefined,
);
expect(item.output).toBe(output);
},
);
it.each([
["echo ok", "echo ok"],
["a".repeat(262_143) + "😀", "a".repeat(262_143) + "\n… output truncated for transport"],
])("bounds fetched command input without changing persistence, case %#", (input, expected) => {
const item = { ...base, type: "command_execution" as const, input, output: "ok" };
const projected = projectTurnItemForDetail(item);
expect(projected).toMatchObject({ input: expected, output: "ok" });
expect(item.input).toBe(input);
});
it("leaves a screenshot's bytes out of the fetched tool output", () => {
const data = "A".repeat(600_000);
const item = {
...base,
type: "dynamic_tool" as const,
toolName: "mcp__t3-code__device_screenshot",
input: { deviceId: "phone" },
output: {
content: [{ type: "image", source: { type: "base64", media_type: "image/png", data } }],
},
};
expect(projectTurnItemForDetail(item)).toMatchObject({
output: { content: [{ type: "image", mimeType: "image/png" }] },
});
expect(item.output.content[0]?.source.data).toBe(data);
});
it("keeps image markers when the rest of a tool output is too large to send", () => {
const image = {
type: "image",
source: { type: "base64", media_type: "image/png", data: "AAAA" },
};
const item = {
...base,
type: "dynamic_tool" as const,
output: { content: [{ type: "text", text: "x".repeat(300_000) }, image] },
};
const projected = projectTurnItemForDetail(item);
const output = projected.type === "dynamic_tool" ? projected.output : null;
expect(Array.isArray(output) ? output.slice(1) : null).toEqual([
{ type: "image", mimeType: "image/png" },
]);
expect(JSON.stringify(output).length).toBeLessThan(270_000);
});
it("bounds a tool output made of many images", () => {
const image = { type: "image", data: "AAAA", mimeType: "image/png" };
const item = {
...base,
type: "dynamic_tool" as const,
output: Array.from({ length: 10_000 }, () => image),
};
const projected = projectTurnItemForDetail(item);
const output = projected.type === "dynamic_tool" ? projected.output : null;
// The truncated text block plus a fixed number of markers, however many images there are.
expect(Array.isArray(output) ? output.length : null).toBe(1 + MAX_TOOL_OUTPUT_IMAGES);
expect(Array.isArray(output) ? String(output[0]?.text).length : null).toBeLessThan(263_000);
expect(toolOutputImages(output)).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
});
it("keeps failure evidence without retaining command output", () => {
const item = {
...base,
type: "command_execution" as const,
input: "cat missing-file",
output: "cat: missing-file: No such file or directory",
};
const projected = projectTurnItemForWire(item);
expect(projected).not.toHaveProperty("output");
expect(projected).toMatchObject({ outputIndicatesFailure: true });
expect(projectTurnItemForWire(projected)).toEqual(projected);
expect(projectTurnItemForWire({ ...item, output: "", exitCode: 2 })).toMatchObject({
outputIndicatesFailure: true,
exitCode: 2,
});
});
it("omits inline file bodies but preserves file identity and change counts", () => {
const item = {
...base,
type: "file_change" as const,
fileName: "src/main.ts",
additions: 3,
deletions: 1,
diffStr: "+new code",
oldStr: "old code",
newStr: "new code",
};
const projected = projectTurnItemForWire(item);
expect(projected).not.toHaveProperty("diffStr");
expect(projected).not.toHaveProperty("oldStr");
expect(projected).not.toHaveProperty("newStr");
expect(projected).toMatchObject({ fileName: "src/main.ts", additions: 3, deletions: 1 });
expect(item.diffStr).toBe("+new code");
// A failed edit keeps the provider's error so expanding the row can show it.
const failed = projectTurnItemForWire({
...item,
status: "failed",
diffStr: "String to replace not found",
});
expect(failed).toMatchObject({ diffStr: "String to replace not found" });
expect(failed).not.toHaveProperty("newStr");
});
it("retains only result identities and failure metadata in live tool events", () => {
const output = {
isError: true,
structuredContent: { threadId: "child-thread", messageId: "message", text: "PRIVATE_BODY" },
content: [{ type: "text", text: "PRIVATE_BODY" }],
};
const event = {
id: EventId.make("result-event"),
type: "turn-item.updated" as const,
threadId: base.threadId,
occurredAt: base.updatedAt,
payload: { ...base, output },
};
const projected = projectDomainEventForWire(event);
expect(projected.payload).toMatchObject({
output: { isError: true, threadId: "child-thread", messageId: "message" },
});
expect(JSON.stringify(projected)).not.toContain("PRIVATE_BODY");
expect(event.payload.output).toBe(output);
expect(projectDomainEventForWire(projected)).toEqual(projected);
});
it("does not serialize raw dynamic output to discard it", () => {
const output = {
toJSON: () => {
throw new Error("Raw output must not be serialized");
},
};
expect(projectTurnItemForWire({ ...base, output })).not.toHaveProperty("output");
});
});