chatccc 0.2.243 → 0.2.245

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +6 -6
  2. package/bin/cccagent.mjs +17 -17
  3. package/deepccc-agent/README.md +14 -8
  4. package/deepccc-agent/bin/deepccc.mjs +26 -26
  5. package/deepccc-agent/os-prompts/darwin.md +8 -0
  6. package/deepccc-agent/os-prompts/linux.md +8 -0
  7. package/deepccc-agent/os-prompts/win32.md +9 -0
  8. package/deepccc-agent/package-lock.json +2 -2
  9. package/deepccc-agent/package.json +63 -62
  10. package/deepccc-agent/src/__tests__/chat-session.test.ts +682 -578
  11. package/deepccc-agent/src/__tests__/cli-json.test.ts +49 -49
  12. package/deepccc-agent/src/__tests__/config.test.ts +26 -26
  13. package/deepccc-agent/src/__tests__/context.test.ts +319 -319
  14. package/deepccc-agent/src/__tests__/file-tools.test.ts +240 -240
  15. package/deepccc-agent/src/__tests__/permissions.test.ts +195 -195
  16. package/deepccc-agent/src/__tests__/privacy.test.ts +8 -5
  17. package/deepccc-agent/src/__tests__/progress-reducer.test.ts +121 -121
  18. package/deepccc-agent/src/__tests__/session-search.test.ts +262 -262
  19. package/deepccc-agent/src/__tests__/session-select.test.ts +116 -116
  20. package/deepccc-agent/src/__tests__/sigint.test.ts +56 -56
  21. package/deepccc-agent/src/__tests__/skills.test.ts +284 -284
  22. package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +247 -247
  23. package/deepccc-agent/src/__tests__/web-tools.test.ts +220 -220
  24. package/deepccc-agent/src/cli.ts +7 -6
  25. package/deepccc-agent/src/config.ts +88 -84
  26. package/deepccc-agent/src/context.ts +465 -465
  27. package/deepccc-agent/src/file-log.ts +38 -38
  28. package/deepccc-agent/src/index.ts +103 -36
  29. package/deepccc-agent/src/proc-tree-kill.ts +61 -61
  30. package/deepccc-agent/src/progress/cards-helpers.ts +76 -76
  31. package/deepccc-agent/src/progress/reducer.ts +113 -113
  32. package/deepccc-agent/src/progress/terminal-renderer.ts +294 -294
  33. package/deepccc-agent/src/progress/view.ts +77 -77
  34. package/deepccc-agent/src/raw-stream-log.ts +124 -124
  35. package/deepccc-agent/src/session-search.ts +370 -370
  36. package/deepccc-agent/src/session-select.ts +48 -48
  37. package/deepccc-agent/src/sigint.ts +50 -50
  38. package/deepccc-agent/src/skills.ts +205 -205
  39. package/deepccc-agent/src/web-tools.ts +313 -313
  40. package/deepccc-agent/tsconfig.build.json +13 -13
  41. package/deepccc-agent/tsconfig.json +13 -13
  42. package/deepccc-agent/vitest.config.ts +7 -7
  43. package/package.json +1 -1
  44. package/src/__tests__/builtin-chat-session.test.ts +522 -522
  45. package/src/__tests__/builtin-config.test.ts +26 -26
  46. package/src/__tests__/builtin-context.test.ts +319 -319
  47. package/src/__tests__/builtin-file-tools.test.ts +240 -240
  48. package/src/__tests__/builtin-permissions.test.ts +211 -211
  49. package/src/__tests__/builtin-session-search.test.ts +262 -262
  50. package/src/__tests__/builtin-session-select.test.ts +116 -116
  51. package/src/__tests__/builtin-sigint.test.ts +56 -56
  52. package/src/__tests__/builtin-skills.test.ts +284 -284
  53. package/src/__tests__/builtin-web-tools.test.ts +220 -220
  54. package/src/__tests__/ccc-adapter.test.ts +15 -0
  55. package/src/__tests__/config.test.ts +17 -17
  56. package/src/__tests__/progress-reducer.test.ts +121 -121
  57. package/src/__tests__/session-ccc-config.test.ts +45 -45
  58. package/src/__tests__/session.test.ts +369 -306
  59. package/src/adapters/adapter-interface.ts +8 -2
  60. package/src/adapters/ccc-adapter.ts +149 -145
  61. package/src/config-utils.ts +13 -13
  62. package/src/config.ts +13 -13
  63. package/src/progress/reducer.ts +113 -113
  64. package/src/session-chat-binding.ts +83 -83
  65. package/src/session.ts +323 -320
@@ -1,262 +1,262 @@
1
- import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
- import { gzipSync } from "node:zlib";
5
-
6
- import { describe, expect, it } from "vitest";
7
-
8
- import { searchBuiltinSessions } from "../../deepccc-agent/src/session-search.ts";
9
-
10
- interface TestMessage {
11
- role: "user" | "assistant";
12
- content: string;
13
- toolCalls?: Array<{ name: string; input?: string; output?: string; is_error?: boolean }>;
14
- }
15
-
16
- async function writeContextSession(
17
- contextDir: string,
18
- sessionId: string,
19
- state: { summary?: string; messages?: TestMessage[] },
20
- ): Promise<void> {
21
- const dir = join(contextDir, sessionId);
22
- await mkdir(dir, { recursive: true });
23
- await writeFile(
24
- join(dir, "context.json"),
25
- JSON.stringify({
26
- version: 1,
27
- createdAt: 1,
28
- updatedAt: 2,
29
- sessionId,
30
- summary: state.summary ?? "",
31
- messages: state.messages ?? [],
32
- totalMessages: (state.messages ?? []).length,
33
- compactedMessages: 0,
34
- }),
35
- "utf8",
36
- );
37
- }
38
-
39
- describe("searchBuiltinSessions (context.json)", () => {
40
- it("finds matching messages and summary across sessions", async () => {
41
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-basic-"));
42
- await writeContextSession(contextDir, "session-a", {
43
- summary: "## 当前任务\n- 修复飞书卡片渲染",
44
- messages: [{ role: "user", content: "你好" }],
45
- });
46
- await writeContextSession(contextDir, "session-b", {
47
- messages: [
48
- { role: "assistant", content: "我修改了飞书卡片组件并运行了测试" },
49
- ],
50
- });
51
-
52
- const output = await searchBuiltinSessions("飞书", { contextDir });
53
-
54
- expect(output.terms).toEqual(["飞书"]);
55
- expect(output.scannedSessions).toBe(2);
56
- expect(output.matches).toHaveLength(2);
57
- const sources = output.matches.map((m) => [m.sessionId, m.source]);
58
- expect(sources).toContainEqual(["session-a", "summary"]);
59
- expect(sources).toContainEqual(["session-b", "context"]);
60
- expect(output.matches[1]).toMatchObject({
61
- sessionId: "session-b",
62
- source: "context",
63
- role: "assistant",
64
- messageIndex: 0,
65
- });
66
- });
67
-
68
- it("matches all terms with AND semantics, case-insensitively", async () => {
69
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-and-"));
70
- await writeContextSession(contextDir, "both", {
71
- messages: [{ role: "assistant", content: "DeepCCC deploy 到生产环境成功" }],
72
- });
73
- await writeContextSession(contextDir, "one-term", {
74
- messages: [{ role: "assistant", content: "只提到 deploy" }],
75
- });
76
-
77
- const output = await searchBuiltinSessions("deepccc deploy", { contextDir });
78
-
79
- expect(output.matches.map((m) => m.sessionId)).toEqual(["both"]);
80
- });
81
-
82
- it("searches structured tool calls and reports tool indexes", async () => {
83
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-tools-"));
84
- await writeContextSession(contextDir, "tool-session", {
85
- messages: [
86
- {
87
- role: "assistant",
88
- content: "回复正文",
89
- toolCalls: [
90
- { name: "run_command", input: "{\"command\":\"npm test\"}", output: "{\"exitCode\":0}" },
91
- { name: "read_file", input: "{\"path\":\"secret-key.txt\"}" },
92
- ],
93
- },
94
- ],
95
- });
96
-
97
- const output = await searchBuiltinSessions("secret-key", { contextDir });
98
-
99
- expect(output.matches).toHaveLength(1);
100
- expect(output.matches[0]).toMatchObject({
101
- sessionId: "tool-session",
102
- source: "context",
103
- messageIndex: 0,
104
- toolCallIndex: 1,
105
- toolCallName: "read_file",
106
- });
107
- expect(output.matches[0].snippet).toContain("secret-key");
108
- });
109
-
110
- it("restricts search to an explicit session id", async () => {
111
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-restrict-"));
112
- await writeContextSession(contextDir, "keep-me", {
113
- messages: [{ role: "user", content: "目标关键词 token-xyz" }],
114
- });
115
- await writeContextSession(contextDir, "skip-me", {
116
- messages: [{ role: "user", content: "目标关键词 token-xyz" }],
117
- });
118
-
119
- const output = await searchBuiltinSessions("token-xyz", {
120
- contextDir,
121
- sessionId: "keep-me",
122
- });
123
-
124
- expect(output.matches.map((m) => m.sessionId)).toEqual(["keep-me"]);
125
- expect(output.scannedSessions).toBe(1);
126
- });
127
-
128
- it("caps results and reports truncation", async () => {
129
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-cap-"));
130
- for (let index = 0; index < 5; index += 1) {
131
- await writeContextSession(contextDir, `session-${index}`, {
132
- messages: [{ role: "user", content: "共同关键词 shared-term" }],
133
- });
134
- }
135
-
136
- const output = await searchBuiltinSessions("shared-term", { contextDir, maxResults: 3 });
137
-
138
- expect(output.matches).toHaveLength(3);
139
- expect(output.truncated).toBe(true);
140
- });
141
-
142
- it("returns an empty result for empty query or missing directory", async () => {
143
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-empty-"));
144
- await writeContextSession(contextDir, "some", {
145
- messages: [{ role: "user", content: "内容" }],
146
- });
147
-
148
- const empty = await searchBuiltinSessions(" ", { contextDir });
149
- expect(empty.matches).toEqual([]);
150
- expect(empty.truncated).toBe(false);
151
-
152
- const missing = await searchBuiltinSessions("内容", {
153
- contextDir: join(contextDir, "does-not-exist"),
154
- });
155
- expect(missing.matches).toEqual([]);
156
- expect(missing.scannedSessions).toBe(0);
157
- });
158
-
159
- it("skips corrupted context files without failing", async () => {
160
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-corrupt-"));
161
- await writeContextSession(contextDir, "good", {
162
- messages: [{ role: "user", content: "可搜索内容 needle-42" }],
163
- });
164
- const corruptDir = join(contextDir, "corrupt");
165
- await mkdir(corruptDir);
166
- await writeFile(join(corruptDir, "context.json"), "not json{{{", "utf8");
167
-
168
- const output = await searchBuiltinSessions("needle-42", { contextDir });
169
-
170
- expect(output.matches.map((m) => m.sessionId)).toEqual(["good"]);
171
- expect(output.scannedSessions).toBe(1);
172
- });
173
- });
174
-
175
- describe("searchBuiltinSessions (raw stream logs)", () => {
176
- it("searches gzipped jsonl logs only when enabled", async () => {
177
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-context-"));
178
- const rawLogsDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-logs-"));
179
- await writeContextSession(contextDir, "raw-session", {
180
- messages: [{ role: "user", content: "无关键词" }],
181
- });
182
-
183
- const sessionDir = join(rawLogsDir, "deepccc", "raw-session");
184
- await mkdir(sessionDir, { recursive: true });
185
- const lines = [
186
- JSON.stringify({ type: "text-delta", text: "普通输出" }),
187
- JSON.stringify({ type: "tool-call", toolCallId: "c1", toolName: "run_command", input: { command: "git push --force origin" } }),
188
- JSON.stringify({ type: "tool-result", toolCallId: "c1", toolName: "run_command", output: { exitCode: 0, stdout: "done" } }),
189
- ];
190
- await writeFile(join(sessionDir, "2026-08-04T00-00-00-000Z-prompt.jsonl.gz"), gzipSync(lines.join("\n")), "utf8");
191
-
192
- const disabled = await searchBuiltinSessions("git push", {
193
- contextDir,
194
- rawLogsDir,
195
- includeRawLogs: false,
196
- });
197
- expect(disabled.matches).toHaveLength(0);
198
-
199
- const enabled = await searchBuiltinSessions("git push", {
200
- contextDir,
201
- rawLogsDir,
202
- includeRawLogs: true,
203
- });
204
- expect(enabled.matches).toHaveLength(1);
205
- expect(enabled.matches[0]).toMatchObject({
206
- sessionId: "raw-session",
207
- source: "raw-log",
208
- });
209
- expect(enabled.matches[0].snippet).toContain("git push");
210
- expect(enabled.scannedRawLogFiles).toBe(1);
211
- });
212
-
213
- it("skips truncated/corrupt gzip files without crashing (unexpected end of file)", async () => {
214
- // 护栏:线上事故——截断的 gzip 解压报 "unexpected end of file",若 error 事件
215
- // 无人监听会升级为 uncaughtException 杀死整个服务。必须跳过并继续。
216
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-trunc-"));
217
- const rawLogsDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-trunc-logs-"));
218
- await writeContextSession(contextDir, "trunc", {
219
- messages: [{ role: "user", content: "kw-trunc" }],
220
- });
221
-
222
- const sessionDir = join(rawLogsDir, "deepccc", "trunc");
223
- await mkdir(sessionDir, { recursive: true });
224
- const good = gzipSync(JSON.stringify({ type: "text-delta", text: "关键词 kw-good" }));
225
- await writeFile(join(sessionDir, "2026-08-04T00-00-00-000Z-a.jsonl.gz"), good, "utf8");
226
- // 截断一半:必然触发 zlib unexpected end of file
227
- const truncated = gzipSync(JSON.stringify({ type: "text-delta", text: "关键词 kw-trunc-in-gzip" }));
228
- await writeFile(join(sessionDir, "2026-08-04T00-00-00-000Z-b.jsonl.gz"), truncated.subarray(0, Math.floor(truncated.length / 2)), "utf8");
229
-
230
- let output;
231
- try {
232
- output = await searchBuiltinSessions("kw-good", {
233
- contextDir,
234
- rawLogsDir,
235
- includeRawLogs: true,
236
- });
237
- } catch (err) {
238
- // 若损坏 gzip 导致 reject,这里直接断言失败并暴露错误
239
- expect.fail(`searchBuiltinSessions 不应 reject: ${String(err)}`);
240
- }
241
-
242
- // 好文件正常命中;截断文件被跳过但不崩溃
243
- expect(output!.matches.map((m) => m.snippet).join("\n")).toContain("kw-good");
244
- expect(output!.scannedRawLogFiles).toBe(2);
245
- });
246
-
247
- it("ignores missing raw log roots", async () => {
248
- const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-missing-"));
249
- await writeContextSession(contextDir, "s", {
250
- messages: [{ role: "user", content: "关键词 kw-77" }],
251
- });
252
-
253
- const output = await searchBuiltinSessions("kw-77", {
254
- contextDir,
255
- rawLogsDir: join(contextDir, "no-raw-logs"),
256
- includeRawLogs: true,
257
- });
258
-
259
- expect(output.matches.map((m) => m.source)).toEqual(["context"]);
260
- expect(output.scannedRawLogFiles).toBe(0);
261
- });
262
- });
1
+ import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { gzipSync } from "node:zlib";
5
+
6
+ import { describe, expect, it } from "vitest";
7
+
8
+ import { searchBuiltinSessions } from "../../deepccc-agent/src/session-search.ts";
9
+
10
+ interface TestMessage {
11
+ role: "user" | "assistant";
12
+ content: string;
13
+ toolCalls?: Array<{ name: string; input?: string; output?: string; is_error?: boolean }>;
14
+ }
15
+
16
+ async function writeContextSession(
17
+ contextDir: string,
18
+ sessionId: string,
19
+ state: { summary?: string; messages?: TestMessage[] },
20
+ ): Promise<void> {
21
+ const dir = join(contextDir, sessionId);
22
+ await mkdir(dir, { recursive: true });
23
+ await writeFile(
24
+ join(dir, "context.json"),
25
+ JSON.stringify({
26
+ version: 1,
27
+ createdAt: 1,
28
+ updatedAt: 2,
29
+ sessionId,
30
+ summary: state.summary ?? "",
31
+ messages: state.messages ?? [],
32
+ totalMessages: (state.messages ?? []).length,
33
+ compactedMessages: 0,
34
+ }),
35
+ "utf8",
36
+ );
37
+ }
38
+
39
+ describe("searchBuiltinSessions (context.json)", () => {
40
+ it("finds matching messages and summary across sessions", async () => {
41
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-basic-"));
42
+ await writeContextSession(contextDir, "session-a", {
43
+ summary: "## 当前任务\n- 修复飞书卡片渲染",
44
+ messages: [{ role: "user", content: "你好" }],
45
+ });
46
+ await writeContextSession(contextDir, "session-b", {
47
+ messages: [
48
+ { role: "assistant", content: "我修改了飞书卡片组件并运行了测试" },
49
+ ],
50
+ });
51
+
52
+ const output = await searchBuiltinSessions("飞书", { contextDir });
53
+
54
+ expect(output.terms).toEqual(["飞书"]);
55
+ expect(output.scannedSessions).toBe(2);
56
+ expect(output.matches).toHaveLength(2);
57
+ const sources = output.matches.map((m) => [m.sessionId, m.source]);
58
+ expect(sources).toContainEqual(["session-a", "summary"]);
59
+ expect(sources).toContainEqual(["session-b", "context"]);
60
+ expect(output.matches[1]).toMatchObject({
61
+ sessionId: "session-b",
62
+ source: "context",
63
+ role: "assistant",
64
+ messageIndex: 0,
65
+ });
66
+ });
67
+
68
+ it("matches all terms with AND semantics, case-insensitively", async () => {
69
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-and-"));
70
+ await writeContextSession(contextDir, "both", {
71
+ messages: [{ role: "assistant", content: "DeepCCC deploy 到生产环境成功" }],
72
+ });
73
+ await writeContextSession(contextDir, "one-term", {
74
+ messages: [{ role: "assistant", content: "只提到 deploy" }],
75
+ });
76
+
77
+ const output = await searchBuiltinSessions("deepccc deploy", { contextDir });
78
+
79
+ expect(output.matches.map((m) => m.sessionId)).toEqual(["both"]);
80
+ });
81
+
82
+ it("searches structured tool calls and reports tool indexes", async () => {
83
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-tools-"));
84
+ await writeContextSession(contextDir, "tool-session", {
85
+ messages: [
86
+ {
87
+ role: "assistant",
88
+ content: "回复正文",
89
+ toolCalls: [
90
+ { name: "run_command", input: "{\"command\":\"npm test\"}", output: "{\"exitCode\":0}" },
91
+ { name: "read_file", input: "{\"path\":\"secret-key.txt\"}" },
92
+ ],
93
+ },
94
+ ],
95
+ });
96
+
97
+ const output = await searchBuiltinSessions("secret-key", { contextDir });
98
+
99
+ expect(output.matches).toHaveLength(1);
100
+ expect(output.matches[0]).toMatchObject({
101
+ sessionId: "tool-session",
102
+ source: "context",
103
+ messageIndex: 0,
104
+ toolCallIndex: 1,
105
+ toolCallName: "read_file",
106
+ });
107
+ expect(output.matches[0].snippet).toContain("secret-key");
108
+ });
109
+
110
+ it("restricts search to an explicit session id", async () => {
111
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-restrict-"));
112
+ await writeContextSession(contextDir, "keep-me", {
113
+ messages: [{ role: "user", content: "目标关键词 token-xyz" }],
114
+ });
115
+ await writeContextSession(contextDir, "skip-me", {
116
+ messages: [{ role: "user", content: "目标关键词 token-xyz" }],
117
+ });
118
+
119
+ const output = await searchBuiltinSessions("token-xyz", {
120
+ contextDir,
121
+ sessionId: "keep-me",
122
+ });
123
+
124
+ expect(output.matches.map((m) => m.sessionId)).toEqual(["keep-me"]);
125
+ expect(output.scannedSessions).toBe(1);
126
+ });
127
+
128
+ it("caps results and reports truncation", async () => {
129
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-cap-"));
130
+ for (let index = 0; index < 5; index += 1) {
131
+ await writeContextSession(contextDir, `session-${index}`, {
132
+ messages: [{ role: "user", content: "共同关键词 shared-term" }],
133
+ });
134
+ }
135
+
136
+ const output = await searchBuiltinSessions("shared-term", { contextDir, maxResults: 3 });
137
+
138
+ expect(output.matches).toHaveLength(3);
139
+ expect(output.truncated).toBe(true);
140
+ });
141
+
142
+ it("returns an empty result for empty query or missing directory", async () => {
143
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-empty-"));
144
+ await writeContextSession(contextDir, "some", {
145
+ messages: [{ role: "user", content: "内容" }],
146
+ });
147
+
148
+ const empty = await searchBuiltinSessions(" ", { contextDir });
149
+ expect(empty.matches).toEqual([]);
150
+ expect(empty.truncated).toBe(false);
151
+
152
+ const missing = await searchBuiltinSessions("内容", {
153
+ contextDir: join(contextDir, "does-not-exist"),
154
+ });
155
+ expect(missing.matches).toEqual([]);
156
+ expect(missing.scannedSessions).toBe(0);
157
+ });
158
+
159
+ it("skips corrupted context files without failing", async () => {
160
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-corrupt-"));
161
+ await writeContextSession(contextDir, "good", {
162
+ messages: [{ role: "user", content: "可搜索内容 needle-42" }],
163
+ });
164
+ const corruptDir = join(contextDir, "corrupt");
165
+ await mkdir(corruptDir);
166
+ await writeFile(join(corruptDir, "context.json"), "not json{{{", "utf8");
167
+
168
+ const output = await searchBuiltinSessions("needle-42", { contextDir });
169
+
170
+ expect(output.matches.map((m) => m.sessionId)).toEqual(["good"]);
171
+ expect(output.scannedSessions).toBe(1);
172
+ });
173
+ });
174
+
175
+ describe("searchBuiltinSessions (raw stream logs)", () => {
176
+ it("searches gzipped jsonl logs only when enabled", async () => {
177
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-context-"));
178
+ const rawLogsDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-logs-"));
179
+ await writeContextSession(contextDir, "raw-session", {
180
+ messages: [{ role: "user", content: "无关键词" }],
181
+ });
182
+
183
+ const sessionDir = join(rawLogsDir, "deepccc", "raw-session");
184
+ await mkdir(sessionDir, { recursive: true });
185
+ const lines = [
186
+ JSON.stringify({ type: "text-delta", text: "普通输出" }),
187
+ JSON.stringify({ type: "tool-call", toolCallId: "c1", toolName: "run_command", input: { command: "git push --force origin" } }),
188
+ JSON.stringify({ type: "tool-result", toolCallId: "c1", toolName: "run_command", output: { exitCode: 0, stdout: "done" } }),
189
+ ];
190
+ await writeFile(join(sessionDir, "2026-08-04T00-00-00-000Z-prompt.jsonl.gz"), gzipSync(lines.join("\n")), "utf8");
191
+
192
+ const disabled = await searchBuiltinSessions("git push", {
193
+ contextDir,
194
+ rawLogsDir,
195
+ includeRawLogs: false,
196
+ });
197
+ expect(disabled.matches).toHaveLength(0);
198
+
199
+ const enabled = await searchBuiltinSessions("git push", {
200
+ contextDir,
201
+ rawLogsDir,
202
+ includeRawLogs: true,
203
+ });
204
+ expect(enabled.matches).toHaveLength(1);
205
+ expect(enabled.matches[0]).toMatchObject({
206
+ sessionId: "raw-session",
207
+ source: "raw-log",
208
+ });
209
+ expect(enabled.matches[0].snippet).toContain("git push");
210
+ expect(enabled.scannedRawLogFiles).toBe(1);
211
+ });
212
+
213
+ it("skips truncated/corrupt gzip files without crashing (unexpected end of file)", async () => {
214
+ // 护栏:线上事故——截断的 gzip 解压报 "unexpected end of file",若 error 事件
215
+ // 无人监听会升级为 uncaughtException 杀死整个服务。必须跳过并继续。
216
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-trunc-"));
217
+ const rawLogsDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-trunc-logs-"));
218
+ await writeContextSession(contextDir, "trunc", {
219
+ messages: [{ role: "user", content: "kw-trunc" }],
220
+ });
221
+
222
+ const sessionDir = join(rawLogsDir, "deepccc", "trunc");
223
+ await mkdir(sessionDir, { recursive: true });
224
+ const good = gzipSync(JSON.stringify({ type: "text-delta", text: "关键词 kw-good" }));
225
+ await writeFile(join(sessionDir, "2026-08-04T00-00-00-000Z-a.jsonl.gz"), good, "utf8");
226
+ // 截断一半:必然触发 zlib unexpected end of file
227
+ const truncated = gzipSync(JSON.stringify({ type: "text-delta", text: "关键词 kw-trunc-in-gzip" }));
228
+ await writeFile(join(sessionDir, "2026-08-04T00-00-00-000Z-b.jsonl.gz"), truncated.subarray(0, Math.floor(truncated.length / 2)), "utf8");
229
+
230
+ let output;
231
+ try {
232
+ output = await searchBuiltinSessions("kw-good", {
233
+ contextDir,
234
+ rawLogsDir,
235
+ includeRawLogs: true,
236
+ });
237
+ } catch (err) {
238
+ // 若损坏 gzip 导致 reject,这里直接断言失败并暴露错误
239
+ expect.fail(`searchBuiltinSessions 不应 reject: ${String(err)}`);
240
+ }
241
+
242
+ // 好文件正常命中;截断文件被跳过但不崩溃
243
+ expect(output!.matches.map((m) => m.snippet).join("\n")).toContain("kw-good");
244
+ expect(output!.scannedRawLogFiles).toBe(2);
245
+ });
246
+
247
+ it("ignores missing raw log roots", async () => {
248
+ const contextDir = await mkdtemp(join(tmpdir(), "deepccc-search-raw-missing-"));
249
+ await writeContextSession(contextDir, "s", {
250
+ messages: [{ role: "user", content: "关键词 kw-77" }],
251
+ });
252
+
253
+ const output = await searchBuiltinSessions("kw-77", {
254
+ contextDir,
255
+ rawLogsDir: join(contextDir, "no-raw-logs"),
256
+ includeRawLogs: true,
257
+ });
258
+
259
+ expect(output.matches.map((m) => m.source)).toEqual(["context"]);
260
+ expect(output.scannedRawLogFiles).toBe(0);
261
+ });
262
+ });