Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ContextHandoffService.test.ts

158 lines5.2 KB

1import { assert, it } from "@effect/vitest";
2import {
3 MessageId,
4 ProviderInstanceId,
5 ProviderThreadId,
6 RunId,
7 ThreadId,
8 TurnItemId,
9 type OrchestrationV2TurnItem,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import * as Effect from "effect/Effect";
13import * as Layer from "effect/Layer";
14​
15import * as ContextHandoffService from "./ContextHandoffService.ts";
16import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
17​
18const layerTest = ContextHandoffService.layer.pipe(Layer.provide(IdAllocator.layer));
19​
20function importedItem(
21 input:
22 | {
23 readonly role: "user";
24 readonly id: string;
25 readonly text: string;
26 readonly ordinal: number;
27 }
28 | {
29 readonly role: "assistant";
30 readonly id: string;
31 readonly text: string;
32 readonly ordinal: number;
33 },
34): OrchestrationV2TurnItem {
35 const now = DateTime.makeUnsafe("2026-01-01T00:00:00.000Z");
36 const base = {
37 id: TurnItemId.make(`turn-item:${input.id}`),
38 threadId: ThreadId.make("thread:legacy-context"),
39 runId: null,
40 nodeId: null,
41 providerThreadId: null,
42 providerTurnId: null,
43 nativeItemRef: null,
44 parentItemId: null,
45 ordinal: input.ordinal,
46 status: "completed" as const,
47 title: null,
48 startedAt: now,
49 completedAt: now,
50 updatedAt: now,
51 messageId: MessageId.make(`message:${input.id}`),
52 text: input.text,
53 };
54 return input.role === "user"
55 ? {
56 ...base,
57 createdBy: "user",
58 creationSource: "server",
59 type: "user_message",
60 inputIntent: "turn_start",
61 attachments: [],
62 }
63 : {
64 ...base,
65 type: "assistant_message",
66 streaming: false,
67 };
69​
70it.layer(layerTest)("ContextHandoffService legacy import", (it) => {
71 it.effect("prepares imported history for the first native v2 turn", () =>
72 Effect.gen(function* () {
73 const service = yield* ContextHandoffService.ContextHandoffServiceV2;
74 const handoff = yield* service.prepareLegacyImport({
75 threadId: ThreadId.make("thread:legacy-context"),
76 targetRunId: RunId.make("run:first-v2"),
77 toProviderThreadId: ProviderThreadId.make("provider-thread:first-v2"),
78 toProviderInstanceId: ProviderInstanceId.make("codex"),
79 items: [
80 importedItem({ role: "user", id: "one", text: "What did we decide?", ordinal: 1 }),
81 importedItem({
82 role: "assistant",
83 id: "two",
84 text: "We decided to keep the migration lightweight.",
85 ordinal: 2,
86 }),
87 ],
88 createdAt: DateTime.makeUnsafe("2026-01-02T00:00:00.000Z"),
89 });
90​
91 assert.equal(handoff.strategy, "manual_context");
92 assert.deepStrictEqual(handoff.fromProviderThreadIds, []);
93 assert.include(handoff.summaryText, "What did we decide?");
94 assert.include(handoff.summaryText, "keep the migration lightweight");
95 }),
96 );
97​
98 it.effect("preserves role attribution when truncating imported history", () =>
99 Effect.gen(function* () {
100 const service = yield* ContextHandoffService.ContextHandoffServiceV2;
101 const handoff = yield* service.prepareLegacyImport({
102 threadId: ThreadId.make("thread:legacy-context"),
103 targetRunId: RunId.make("run:first-v2"),
104 toProviderThreadId: ProviderThreadId.make("provider-thread:first-v2"),
105 toProviderInstanceId: ProviderInstanceId.make("codex"),
106 items: [
107 importedItem({
108 role: "user",
109 id: "long",
110 text: `${"x".repeat(35_000)} retained final words`,
111 ordinal: 1,
112 }),
113 ],
114 createdAt: DateTime.makeUnsafe("2026-01-02T00:00:00.000Z"),
115 });
116​
117 assert.isAtMost(handoff.summaryText.length, 32_000);
118 assert.include(handoff.summaryText, "User:\n... retained final words");
119 assert.notMatch(handoff.summaryText, /\n+x+ retained final words/);
120 }),
121 );
122​
123 it.effect("retains the newest oversized import even when it has no whitespace", () =>
124 Effect.gen(function* () {
125 const service = yield* ContextHandoffService.ContextHandoffServiceV2;
126 const handoff = yield* service.prepareLegacyImport({
127 threadId: ThreadId.make("thread:legacy-context"),
128 targetRunId: RunId.make("run:first-v2"),
129 toProviderThreadId: ProviderThreadId.make("provider-thread:first-v2"),
130 toProviderInstanceId: ProviderInstanceId.make("codex"),
131 items: [
132 importedItem({
133 role: "assistant",
134 id: "older",
135 text: "older message",
136 ordinal: 1,
137 }),
138 importedItem({
139 role: "user",
140 id: "long-single-token",
141 text: `${"🧪".repeat(20_000)}LATEST_SINGLE_TOKEN`,
142 ordinal: 2,
143 }),
144 ],
145 createdAt: DateTime.makeUnsafe("2026-01-02T00:00:00.000Z"),
146 });
147​
148 assert.include(
149 handoff.history?.omittedItemIds ?? [],
150 TurnItemId.make("turn-item:long-single-token"),
151 );
152 assert.isAtMost(handoff.summaryText.length, 32_000);
153 assert.include(handoff.summaryText, "User:\n... ");
154 assert.include(handoff.summaryText, "LATEST_SINGLE_TOKEN");
155 assert.notInclude(handoff.summaryText, "\ufffd");
156 }),
157 );
158});