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

ContextHandoffService.test.ts

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