返回源码地图

packages/coding-agent/test/session-manager/build-context.test.ts

v1.3.0 · 71023aec1546 · 07:127–232及292–末尾,context编辑与compaction投影,未本机执行上游suite

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1import { describe, expect, it } from "vitest";
2import {
3 type BranchSummaryEntry,
4 buildContextEntries,
5 buildSessionContext,
6 type CompactionEntry,
7 type CustomEntry,
8 type ModelChangeEntry,
9 type SessionEntry,
10 type SessionMessageEntry,
11 type ThinkingLevelChangeEntry,
12} from "../../src/core/session-manager.ts";
13
14function msg(id: string, parentId: string | null, role: "user" | "assistant", text: string): SessionMessageEntry {
15 const base = { type: "message" as const, id, parentId, timestamp: "2025-01-01T00:00:00Z" };
16 if (role === "user") {
17 return { ...base, message: { role, content: text, timestamp: 1 } };
18 }
19 return {
20 ...base,
21 message: {
22 role,
23 content: [{ type: "text", text }],
24 api: "anthropic-messages",
25 provider: "anthropic",
26 model: "claude-test",
27 usage: {
28 input: 1,
29 output: 1,
30 cacheRead: 0,
31 cacheWrite: 0,
32 totalTokens: 2,
33 cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
34 },
35 stopReason: "stop",
36 timestamp: 1,
37 },
38 };
39}
40
41function compaction(id: string, parentId: string | null, summary: string, firstKeptEntryId: string): CompactionEntry {
42 return {
43 type: "compaction",
44 id,
45 parentId,
46 timestamp: "2025-01-01T00:00:00Z",
47 summary,
48 firstKeptEntryId,
49 tokensBefore: 1000,
50 };
51}
52
53function branchSummary(id: string, parentId: string | null, summary: string, fromId: string): BranchSummaryEntry {
54 return { type: "branch_summary", id, parentId, timestamp: "2025-01-01T00:00:00Z", summary, fromId };
55}
56
57function custom(id: string, parentId: string | null, customType: string, data?: unknown): CustomEntry {
58 return { type: "custom", id, parentId, timestamp: "2025-01-01T00:00:00Z", customType, data };
59}
60
61function thinkingLevel(id: string, parentId: string | null, level: string): ThinkingLevelChangeEntry {
62 return { type: "thinking_level_change", id, parentId, timestamp: "2025-01-01T00:00:00Z", thinkingLevel: level };
63}
64
65function modelChange(id: string, parentId: string | null, provider: string, modelId: string): ModelChangeEntry {
66 return { type: "model_change", id, parentId, timestamp: "2025-01-01T00:00:00Z", provider, modelId };
67}
68
69describe("buildSessionContext", () => {
70 describe("trivial cases", () => {
71 it("empty entries returns empty context", () => {
72 const ctx = buildSessionContext([]);
73 expect(ctx.messages).toEqual([]);
74 expect(ctx.thinkingLevel).toBe("off");
75 expect(ctx.model).toBeNull();
76 });
77
78 it("single user message", () => {
79 const entries: SessionEntry[] = [msg("1", null, "user", "hello")];
80 const ctx = buildSessionContext(entries);
81 expect(ctx.messages).toHaveLength(1);
82 expect(ctx.messages[0].role).toBe("user");
83 });
84
85 it("simple conversation", () => {
86 const entries: SessionEntry[] = [
87 msg("1", null, "user", "hello"),
88 msg("2", "1", "assistant", "hi there"),
89 msg("3", "2", "user", "how are you"),
90 msg("4", "3", "assistant", "great"),
91 ];
92 const ctx = buildSessionContext(entries);
93 expect(ctx.messages).toHaveLength(4);
94 expect(ctx.messages.map((m) => m.role)).toEqual(["user", "assistant", "user", "assistant"]);
95 });
96
97 it("tracks thinking level changes", () => {
98 const entries: SessionEntry[] = [
99 msg("1", null, "user", "hello"),
100 thinkingLevel("2", "1", "high"),
101 msg("3", "2", "assistant", "thinking hard"),
102 ];
103 const ctx = buildSessionContext(entries);
104 expect(ctx.thinkingLevel).toBe("high");
105 expect(ctx.messages).toHaveLength(2);
106 });
107
108 it("tracks model from assistant message", () => {
109 const entries: SessionEntry[] = [msg("1", null, "user", "hello"), msg("2", "1", "assistant", "hi")];
110 const ctx = buildSessionContext(entries);
111 expect(ctx.model).toEqual({ provider: "anthropic", modelId: "claude-test" });
112 });
113
114 it("tracks model from model change entry", () => {
115 const entries: SessionEntry[] = [
116 msg("1", null, "user", "hello"),
117 modelChange("2", "1", "openai", "gpt-4"),
118 msg("3", "2", "assistant", "hi"),
119 ];
120 const ctx = buildSessionContext(entries);
121 // Assistant message overwrites model change
122 expect(ctx.model).toEqual({ provider: "anthropic", modelId: "claude-test" });
123 });
124 });
125
126 describe("with compaction", () => {
127 it("includes summary before kept messages", () => {
128 const entries: SessionEntry[] = [
129 msg("1", null, "user", "first"),
130 msg("2", "1", "assistant", "response1"),
131 msg("3", "2", "user", "second"),
132 msg("4", "3", "assistant", "response2"),
133 compaction("5", "4", "Summary of first two turns", "3"),
134 msg("6", "5", "user", "third"),
135 msg("7", "6", "assistant", "response3"),
136 ];
137 const ctx = buildSessionContext(entries);
138
139 // Should have: summary + kept (3,4) + after (6,7) = 5 messages
140 expect(ctx.messages).toHaveLength(5);
141 expect((ctx.messages[0] as any).summary).toContain("Summary of first two turns");
142 expect((ctx.messages[1] as any).content).toBe("second");
143 expect((ctx.messages[2] as any).content[0].text).toBe("response2");
144 expect((ctx.messages[3] as any).content).toBe("third");
145 expect((ctx.messages[4] as any).content[0].text).toBe("response3");
146 });
147
148 it("handles compaction keeping from first message", () => {
149 const entries: SessionEntry[] = [
150 msg("1", null, "user", "first"),
151 msg("2", "1", "assistant", "response"),
152 compaction("3", "2", "Empty summary", "1"),
153 msg("4", "3", "user", "second"),
154 ];
155 const ctx = buildSessionContext(entries);
156
157 // Summary + all messages (1,2,4)
158 expect(ctx.messages).toHaveLength(4);
159 expect((ctx.messages[0] as any).summary).toContain("Empty summary");
160 });
161
162 it("multiple compactions uses latest", () => {
163 const entries: SessionEntry[] = [
164 msg("1", null, "user", "a"),
165 msg("2", "1", "assistant", "b"),
166 compaction("3", "2", "First summary", "1"),
167 msg("4", "3", "user", "c"),
168 msg("5", "4", "assistant", "d"),
169 compaction("6", "5", "Second summary", "4"),
170 msg("7", "6", "user", "e"),
171 ];
172 const ctx = buildSessionContext(entries);
173
174 // Should use second summary, keep from 4
175 expect(ctx.messages).toHaveLength(4);
176 expect((ctx.messages[0] as any).summary).toContain("Second summary");
177 });
178
179 it("buildContextEntries returns compaction-aware entries including custom entries", () => {
180 const entries: SessionEntry[] = [
181 msg("1", null, "user", "first"),
182 custom("2", "1", "old-state", { hidden: true }),
183 msg("3", "2", "assistant", "response1"),
184 custom("4", "3", "kept-card", { title: "Kept" }),
185 msg("5", "4", "user", "second"),
186 compaction("6", "5", "Summary", "4"),
187 custom("7", "6", "after-card", { title: "After" }),
188 msg("8", "7", "assistant", "response2"),
189 ];
190
191 expect(buildContextEntries(entries).map((entry) => entry.id)).toEqual(["6", "4", "5", "7", "8"]);
192 const ctx = buildSessionContext(entries);
193 expect(ctx.messages.map((message) => message.role)).toEqual(["compactionSummary", "user", "assistant"]);
194 });
195
196 it("keeps settings from the full path after compaction", () => {
197 const entries: SessionEntry[] = [
198 msg("1", null, "user", "first"),
199 thinkingLevel("2", "1", "high"),
200 msg("3", "2", "assistant", "response1"),
201 msg("4", "3", "user", "second"),
202 compaction("5", "4", "Summary", "4"),
203 ];
204
205 const ctx = buildSessionContext(entries);
206 expect(ctx.thinkingLevel).toBe("high");
207 expect(ctx.messages.map((message) => message.role)).toEqual(["compactionSummary", "user"]);
208 });
209 });
210
211 describe("with branches", () => {
212 it("follows path to specified leaf", () => {
213 // Tree:
214 // 1 -> 2 -> 3 (branch A)
215 // \-> 4 (branch B)
216 const entries: SessionEntry[] = [
217 msg("1", null, "user", "start"),
218 msg("2", "1", "assistant", "response"),
219 msg("3", "2", "user", "branch A"),
220 msg("4", "2", "user", "branch B"),
221 ];
222
223 const ctxA = buildSessionContext(entries, "3");
224 expect(ctxA.messages).toHaveLength(3);
225 expect((ctxA.messages[2] as any).content).toBe("branch A");
226
227 const ctxB = buildSessionContext(entries, "4");
228 expect(ctxB.messages).toHaveLength(3);
229 expect((ctxB.messages[2] as any).content).toBe("branch B");
230 });
231
232 it("includes branch summary in path", () => {
233 const entries: SessionEntry[] = [
234 msg("1", null, "user", "start"),
235 msg("2", "1", "assistant", "response"),
236 msg("3", "2", "user", "abandoned path"),
237 branchSummary("4", "2", "Summary of abandoned work", "3"),
238 msg("5", "4", "user", "new direction"),
239 ];
240 const ctx = buildSessionContext(entries, "5");
241
242 expect(ctx.messages).toHaveLength(4);
243 expect((ctx.messages[2] as any).summary).toContain("Summary of abandoned work");
244 expect((ctx.messages[3] as any).content).toBe("new direction");
245 });
246
247 it("complex tree with multiple branches and compaction", () => {
248 // Tree:
249 // 1 -> 2 -> 3 -> 4 -> compaction(5) -> 6 -> 7 (main path)
250 // \-> 8 -> 9 (abandoned branch)
251 // \-> branchSummary(10) -> 11 (resumed from 3)
252 const entries: SessionEntry[] = [
253 msg("1", null, "user", "start"),
254 msg("2", "1", "assistant", "r1"),
255 msg("3", "2", "user", "q2"),
256 msg("4", "3", "assistant", "r2"),
257 compaction("5", "4", "Compacted history", "3"),
258 msg("6", "5", "user", "q3"),
259 msg("7", "6", "assistant", "r3"),
260 // Abandoned branch from 3
261 msg("8", "3", "user", "wrong path"),
262 msg("9", "8", "assistant", "wrong response"),
263 // Branch summary resuming from 3
264 branchSummary("10", "3", "Tried wrong approach", "9"),
265 msg("11", "10", "user", "better approach"),
266 ];
267
268 // Main path to 7: summary + kept(3,4) + after(6,7)
269 const ctxMain = buildSessionContext(entries, "7");
270 expect(ctxMain.messages).toHaveLength(5);
271 expect((ctxMain.messages[0] as any).summary).toContain("Compacted history");
272 expect((ctxMain.messages[1] as any).content).toBe("q2");
273 expect((ctxMain.messages[2] as any).content[0].text).toBe("r2");
274 expect((ctxMain.messages[3] as any).content).toBe("q3");
275 expect((ctxMain.messages[4] as any).content[0].text).toBe("r3");
276
277 // Branch path to 11: 1,2,3 + branch_summary + 11
278 const ctxBranch = buildSessionContext(entries, "11");
279 expect(ctxBranch.messages).toHaveLength(5);
280 expect((ctxBranch.messages[0] as any).content).toBe("start");
281 expect((ctxBranch.messages[1] as any).content[0].text).toBe("r1");
282 expect((ctxBranch.messages[2] as any).content).toBe("q2");
283 expect((ctxBranch.messages[3] as any).summary).toContain("Tried wrong approach");
284 expect((ctxBranch.messages[4] as any).content).toBe("better approach");
285 });
286 });
287
288 describe("edge cases", () => {
289 it("uses last entry when leafId not found", () => {
290 const entries: SessionEntry[] = [msg("1", null, "user", "hello"), msg("2", "1", "assistant", "hi")];
291 const ctx = buildSessionContext(entries, "nonexistent");
292 expect(ctx.messages).toHaveLength(2);
293 });
294
295 it("handles orphaned entries gracefully", () => {
296 const entries: SessionEntry[] = [
297 msg("1", null, "user", "hello"),
298 msg("2", "missing", "assistant", "orphan"), // parent doesn't exist
299 ];
300 const ctx = buildSessionContext(entries, "2");
301 // Should only get the orphan since parent chain is broken
302 expect(ctx.messages).toHaveLength(1);
303 });
304 });
305});