chatccc 0.2.231 → 0.2.232

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 (127) hide show
  1. package/.agents/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  2. package/.claude/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  3. package/.cursor/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  4. package/README.md +90 -90
  5. package/agent-prompts/claude_specific.md +45 -45
  6. package/agent-prompts/codex_specific.md +2 -2
  7. package/agent-prompts/cursor_specific.md +13 -13
  8. package/bin/cccagent.mjs +17 -17
  9. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  10. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  11. package/im-skills/feishu-skill/skill.md +3 -3
  12. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  13. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  14. package/im-skills/wechat-file-skill/skill.md +10 -10
  15. package/im-skills/wechat-image-skill/skill.md +10 -10
  16. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  17. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  18. package/im-skills/wechat-video-skill/skill.md +10 -10
  19. package/package.json +1 -1
  20. package/scripts/postinstall-sharp-check.mjs +58 -58
  21. package/src/__tests__/agent-activity.test.ts +86 -86
  22. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  23. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -99
  24. package/src/__tests__/builtin-chat-session.test.ts +449 -393
  25. package/src/__tests__/builtin-cli-json.test.ts +39 -39
  26. package/src/__tests__/builtin-config.test.ts +26 -26
  27. package/src/__tests__/builtin-context.test.ts +319 -202
  28. package/src/__tests__/builtin-file-tools.test.ts +240 -240
  29. package/src/__tests__/builtin-permissions.test.ts +211 -211
  30. package/src/__tests__/builtin-session-search.test.ts +228 -0
  31. package/src/__tests__/builtin-session-select.test.ts +116 -116
  32. package/src/__tests__/builtin-sigint.test.ts +56 -56
  33. package/src/__tests__/builtin-skills.test.ts +284 -284
  34. package/src/__tests__/builtin-web-tools.test.ts +220 -220
  35. package/src/__tests__/card-action-routing.test.ts +18 -18
  36. package/src/__tests__/cardkit.test.ts +60 -60
  37. package/src/__tests__/ccc-adapter.test.ts +170 -170
  38. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  39. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  40. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  41. package/src/__tests__/claude-adapter.test.ts +614 -614
  42. package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
  43. package/src/__tests__/codex-adapter.test.ts +58 -58
  44. package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
  45. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  46. package/src/__tests__/cursor-adapter.test.ts +268 -268
  47. package/src/__tests__/feishu-api.test.ts +60 -60
  48. package/src/__tests__/feishu-avatar.test.ts +164 -164
  49. package/src/__tests__/feishu-message-ingress.test.ts +138 -138
  50. package/src/__tests__/feishu-platform.test.ts +22 -22
  51. package/src/__tests__/format-message.test.ts +47 -47
  52. package/src/__tests__/jsonl-stream.test.ts +79 -79
  53. package/src/__tests__/package-files.test.ts +24 -24
  54. package/src/__tests__/privacy.test.ts +198 -198
  55. package/src/__tests__/progress-reducer.test.ts +121 -121
  56. package/src/__tests__/raw-stream-log.test.ts +106 -106
  57. package/src/__tests__/response-stall.test.ts +49 -49
  58. package/src/__tests__/restart.test.ts +132 -132
  59. package/src/__tests__/session.test.ts +199 -199
  60. package/src/__tests__/shared-prefix.test.ts +36 -36
  61. package/src/__tests__/sim-platform.test.ts +16 -16
  62. package/src/__tests__/startup-lifecycle.test.ts +231 -231
  63. package/src/__tests__/stop-session.test.ts +34 -34
  64. package/src/__tests__/stream-state.test.ts +42 -42
  65. package/src/__tests__/terminal-error.test.ts +54 -54
  66. package/src/__tests__/terminal-renderer.test.ts +247 -247
  67. package/src/__tests__/update-command-guard.test.ts +144 -144
  68. package/src/__tests__/web-ui.test.ts +326 -326
  69. package/src/adapters/adapter-interface.ts +33 -33
  70. package/src/adapters/ccc-adapter.ts +141 -141
  71. package/src/adapters/claude-adapter.ts +620 -620
  72. package/src/adapters/claude-session-meta-store.ts +120 -120
  73. package/src/adapters/codex-adapter.ts +426 -426
  74. package/src/adapters/cursor-adapter.ts +681 -681
  75. package/src/adapters/jsonl-stream.ts +157 -157
  76. package/src/adapters/raw-stream-log.ts +124 -124
  77. package/src/adapters/resource-monitor.ts +140 -140
  78. package/src/agent-activity.ts +175 -175
  79. package/src/agent-delegate-task-rpc.ts +153 -153
  80. package/src/agent-delegate-task.ts +91 -91
  81. package/src/agent-reload-config-rpc.ts +34 -34
  82. package/src/agent-stop-stuck.ts +129 -129
  83. package/src/builtin/cli.ts +6 -0
  84. package/src/builtin/config.ts +84 -84
  85. package/src/builtin/context.ts +465 -391
  86. package/src/builtin/file-log.ts +38 -38
  87. package/src/builtin/file-tools.ts +34 -0
  88. package/src/builtin/index.ts +24 -23
  89. package/src/builtin/proc-tree-kill.ts +61 -61
  90. package/src/builtin/progress/cards-helpers.ts +76 -76
  91. package/src/builtin/progress/reducer.ts +113 -113
  92. package/src/builtin/progress/terminal-renderer.ts +294 -294
  93. package/src/builtin/progress/view.ts +77 -77
  94. package/src/builtin/raw-stream-log.ts +124 -124
  95. package/src/builtin/session-search.ts +363 -0
  96. package/src/builtin/session-select.ts +48 -48
  97. package/src/builtin/sigint.ts +50 -50
  98. package/src/builtin/skills.ts +205 -205
  99. package/src/builtin/web-tools.ts +313 -313
  100. package/src/card-action-routing.ts +14 -14
  101. package/src/chatgpt-subscription-rpc.ts +27 -27
  102. package/src/chatgpt-subscription.ts +299 -299
  103. package/src/chrome-devtools-guard.ts +318 -318
  104. package/src/codex-reset-actions.ts +184 -184
  105. package/src/feishu-api.ts +193 -193
  106. package/src/feishu-message-ingress.ts +195 -195
  107. package/src/feishu-platform.ts +20 -20
  108. package/src/format-message.ts +293 -293
  109. package/src/litellm-proxy.ts +374 -374
  110. package/src/orchestrator.ts +2494 -2494
  111. package/src/platform-adapter.ts +6 -6
  112. package/src/privacy.ts +118 -118
  113. package/src/progress/reducer.ts +113 -113
  114. package/src/progress/terminal-renderer.ts +294 -294
  115. package/src/progress/view.ts +77 -77
  116. package/src/response-stall.ts +28 -28
  117. package/src/runtime-reload.ts +34 -34
  118. package/src/session-chat-binding.ts +82 -82
  119. package/src/session-name.ts +8 -8
  120. package/src/session.ts +201 -201
  121. package/src/shared-prefix.ts +29 -29
  122. package/src/sim-platform.ts +20 -20
  123. package/src/startup-lifecycle.ts +250 -250
  124. package/src/stream-state.ts +21 -21
  125. package/src/terminal-error.ts +129 -129
  126. package/src/turn-cards.ts +117 -117
  127. package/src/update-command-guard.ts +165 -165
@@ -1,231 +1,231 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- import {
4
- INTERNAL_RESTART_ENV_VAR,
5
- buildWebUiUrl,
6
- createServiceLifecycleGuard,
7
- createInternalRestartEnv,
8
- openWebUiInDefaultBrowser,
9
- shouldAutoOpenWebUi,
10
- } from "../startup-lifecycle.ts";
11
-
12
- describe("ChatCCC startup lifecycle", () => {
13
- it("opens the Web UI by default for a direct start", () => {
14
- expect(shouldAutoOpenWebUi({ env: {} })).toBe(true);
15
- expect(shouldAutoOpenWebUi({ env: {}, openOnStart: true })).toBe(true);
16
- });
17
-
18
- it("respects the persisted openOnStart preference", () => {
19
- expect(shouldAutoOpenWebUi({ env: {}, openOnStart: false })).toBe(false);
20
- });
21
-
22
- it("never opens the Web UI for an internal restart", () => {
23
- expect(shouldAutoOpenWebUi({
24
- env: { [INTERNAL_RESTART_ENV_VAR]: "1" },
25
- openOnStart: true,
26
- })).toBe(false);
27
- });
28
-
29
- it("marks internal restart children without losing the inherited environment", () => {
30
- expect(createInternalRestartEnv({ PATH: "test-path", CUSTOM: "value" })).toEqual({
31
- PATH: "test-path",
32
- CUSTOM: "value",
33
- [INTERNAL_RESTART_ENV_VAR]: "1",
34
- });
35
- });
36
-
37
- it("uses localhost and the configured port for the Web UI URL", () => {
38
- expect(buildWebUiUrl(18080)).toBe("http://localhost:18080/");
39
- expect(buildWebUiUrl(18081)).toBe("http://localhost:18081/");
40
- });
41
-
42
- it("opens the configured URL in the Windows default browser", () => {
43
- const child = {
44
- on: vi.fn().mockReturnThis(),
45
- unref: vi.fn(),
46
- };
47
- const spawnImpl = vi.fn(() => child as never);
48
-
49
- expect(openWebUiInDefaultBrowser(18081, {
50
- platform: "win32",
51
- spawnImpl,
52
- })).toBe(true);
53
- expect(spawnImpl).toHaveBeenCalledWith(
54
- "cmd.exe",
55
- ["/c", "start", "", "http://localhost:18081/"],
56
- {
57
- detached: true,
58
- stdio: "ignore",
59
- windowsHide: true,
60
- },
61
- );
62
- expect(child.on).toHaveBeenCalledWith("error", expect.any(Function));
63
- expect(child.unref).toHaveBeenCalledOnce();
64
- });
65
-
66
- it("opens the configured URL with xdg-open on a Linux desktop", () => {
67
- const child = {
68
- on: vi.fn().mockReturnThis(),
69
- unref: vi.fn(),
70
- };
71
- const spawnImpl = vi.fn(() => child as never);
72
-
73
- expect(openWebUiInDefaultBrowser(18080, {
74
- platform: "linux",
75
- env: { DISPLAY: ":0" },
76
- spawnImpl,
77
- })).toBe(true);
78
- expect(spawnImpl).toHaveBeenCalledWith(
79
- "xdg-open",
80
- ["http://localhost:18080/"],
81
- { detached: true, stdio: "ignore" },
82
- );
83
- });
84
-
85
- it("skips xdg-open and prints an SSH tunnel hint on headless Linux", () => {
86
- const spawnImpl = vi.fn();
87
- const onInfo = vi.fn();
88
-
89
- expect(openWebUiInDefaultBrowser(18080, {
90
- platform: "linux",
91
- env: {},
92
- spawnImpl,
93
- onInfo,
94
- })).toBe(false);
95
- expect(spawnImpl).not.toHaveBeenCalled();
96
- expect(onInfo).toHaveBeenCalledWith(expect.stringContaining("ssh -L 18080:127.0.0.1:18080"));
97
- expect(onInfo).toHaveBeenCalledWith(expect.stringContaining("http://localhost:18080/"));
98
- });
99
- });
100
-
101
- describe("ChatCCC service lifecycle guard", () => {
102
- function createHarness() {
103
- let intervalCallback: (() => void) | undefined;
104
- const timer = { ref: vi.fn() };
105
- const setIntervalImpl = vi.fn((callback: () => void) => {
106
- intervalCallback = callback;
107
- return timer;
108
- });
109
- const clearIntervalImpl = vi.fn();
110
- const tracer = vi.fn();
111
- const server = {
112
- listening: true,
113
- address: vi.fn(() => ({ address: "127.0.0.1", port: 18080 })),
114
- ref: vi.fn(),
115
- };
116
- const recoverServer = vi.fn(async () => {
117
- server.listening = true;
118
- });
119
- const guard = createServiceLifecycleGuard({
120
- intervalMs: 10_000,
121
- setIntervalImpl,
122
- clearIntervalImpl,
123
- tracer,
124
- getActiveResourcesInfo: () => ["TCPServerWrap", "Timeout"],
125
- });
126
-
127
- return {
128
- clearIntervalImpl,
129
- getIntervalCallback: () => intervalCallback,
130
- guard,
131
- recoverServer,
132
- server,
133
- setIntervalImpl,
134
- timer,
135
- tracer,
136
- };
137
- }
138
-
139
- it("starts exactly one referenced keep-alive timer", () => {
140
- const h = createHarness();
141
-
142
- h.guard.start();
143
- h.guard.start();
144
-
145
- expect(h.setIntervalImpl).toHaveBeenCalledOnce();
146
- expect(h.setIntervalImpl).toHaveBeenCalledWith(expect.any(Function), 10_000);
147
- expect(h.timer.ref).toHaveBeenCalledOnce();
148
- });
149
-
150
- it("keeps a listening HTTP server referenced", async () => {
151
- const h = createHarness();
152
- h.guard.attachServer(h.server, h.recoverServer);
153
- h.guard.start();
154
-
155
- await h.guard.checkNow();
156
-
157
- expect(h.server.ref).toHaveBeenCalled();
158
- expect(h.recoverServer).not.toHaveBeenCalled();
159
- });
160
-
161
- it("coalesces concurrent recovery when the HTTP server is not listening", async () => {
162
- const h = createHarness();
163
- h.server.listening = false;
164
- let finishRecovery: (() => void) | undefined;
165
- h.recoverServer.mockImplementation(() => new Promise<void>((resolve) => {
166
- finishRecovery = () => {
167
- h.server.listening = true;
168
- resolve();
169
- };
170
- }));
171
- h.guard.attachServer(h.server, h.recoverServer);
172
- h.guard.start();
173
-
174
- const first = h.guard.checkNow();
175
- const second = h.guard.checkNow();
176
- await Promise.resolve();
177
- expect(h.recoverServer).toHaveBeenCalledOnce();
178
-
179
- finishRecovery?.();
180
- await Promise.all([first, second]);
181
- expect(h.tracer).toHaveBeenCalledWith(
182
- "service-lifecycle: HTTP server recovered",
183
- expect.objectContaining({ serverListening: true }),
184
- );
185
- });
186
-
187
- it("re-arms on unexpected beforeExit and records public diagnostics", async () => {
188
- const h = createHarness();
189
- h.guard.attachServer(h.server, h.recoverServer);
190
-
191
- h.guard.handleBeforeExit(0);
192
- await h.guard.checkNow();
193
-
194
- expect(h.setIntervalImpl).toHaveBeenCalledOnce();
195
- expect(h.server.ref).toHaveBeenCalled();
196
- expect(h.tracer).toHaveBeenCalledWith(
197
- "service-lifecycle: unexpected beforeExit",
198
- expect.objectContaining({
199
- code: 0,
200
- activeResources: ["TCPServerWrap", "Timeout"],
201
- serverListening: true,
202
- }),
203
- );
204
- });
205
-
206
- it("stays stopped after an intentional shutdown", () => {
207
- const h = createHarness();
208
- h.guard.start();
209
-
210
- h.guard.beginShutdown("SIGTERM");
211
- h.guard.handleBeforeExit(0);
212
-
213
- expect(h.clearIntervalImpl).toHaveBeenCalledWith(h.timer);
214
- expect(h.setIntervalImpl).toHaveBeenCalledOnce();
215
- expect(h.tracer).toHaveBeenCalledWith(
216
- "service-lifecycle: shutdown requested",
217
- { reason: "SIGTERM" },
218
- );
219
- });
220
-
221
- it("the timer callback runs a health check", async () => {
222
- const h = createHarness();
223
- h.guard.attachServer(h.server, h.recoverServer);
224
- h.guard.start();
225
-
226
- h.getIntervalCallback()?.();
227
- await Promise.resolve();
228
-
229
- expect(h.server.ref).toHaveBeenCalled();
230
- });
231
- });
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ import {
4
+ INTERNAL_RESTART_ENV_VAR,
5
+ buildWebUiUrl,
6
+ createServiceLifecycleGuard,
7
+ createInternalRestartEnv,
8
+ openWebUiInDefaultBrowser,
9
+ shouldAutoOpenWebUi,
10
+ } from "../startup-lifecycle.ts";
11
+
12
+ describe("ChatCCC startup lifecycle", () => {
13
+ it("opens the Web UI by default for a direct start", () => {
14
+ expect(shouldAutoOpenWebUi({ env: {} })).toBe(true);
15
+ expect(shouldAutoOpenWebUi({ env: {}, openOnStart: true })).toBe(true);
16
+ });
17
+
18
+ it("respects the persisted openOnStart preference", () => {
19
+ expect(shouldAutoOpenWebUi({ env: {}, openOnStart: false })).toBe(false);
20
+ });
21
+
22
+ it("never opens the Web UI for an internal restart", () => {
23
+ expect(shouldAutoOpenWebUi({
24
+ env: { [INTERNAL_RESTART_ENV_VAR]: "1" },
25
+ openOnStart: true,
26
+ })).toBe(false);
27
+ });
28
+
29
+ it("marks internal restart children without losing the inherited environment", () => {
30
+ expect(createInternalRestartEnv({ PATH: "test-path", CUSTOM: "value" })).toEqual({
31
+ PATH: "test-path",
32
+ CUSTOM: "value",
33
+ [INTERNAL_RESTART_ENV_VAR]: "1",
34
+ });
35
+ });
36
+
37
+ it("uses localhost and the configured port for the Web UI URL", () => {
38
+ expect(buildWebUiUrl(18080)).toBe("http://localhost:18080/");
39
+ expect(buildWebUiUrl(18081)).toBe("http://localhost:18081/");
40
+ });
41
+
42
+ it("opens the configured URL in the Windows default browser", () => {
43
+ const child = {
44
+ on: vi.fn().mockReturnThis(),
45
+ unref: vi.fn(),
46
+ };
47
+ const spawnImpl = vi.fn(() => child as never);
48
+
49
+ expect(openWebUiInDefaultBrowser(18081, {
50
+ platform: "win32",
51
+ spawnImpl,
52
+ })).toBe(true);
53
+ expect(spawnImpl).toHaveBeenCalledWith(
54
+ "cmd.exe",
55
+ ["/c", "start", "", "http://localhost:18081/"],
56
+ {
57
+ detached: true,
58
+ stdio: "ignore",
59
+ windowsHide: true,
60
+ },
61
+ );
62
+ expect(child.on).toHaveBeenCalledWith("error", expect.any(Function));
63
+ expect(child.unref).toHaveBeenCalledOnce();
64
+ });
65
+
66
+ it("opens the configured URL with xdg-open on a Linux desktop", () => {
67
+ const child = {
68
+ on: vi.fn().mockReturnThis(),
69
+ unref: vi.fn(),
70
+ };
71
+ const spawnImpl = vi.fn(() => child as never);
72
+
73
+ expect(openWebUiInDefaultBrowser(18080, {
74
+ platform: "linux",
75
+ env: { DISPLAY: ":0" },
76
+ spawnImpl,
77
+ })).toBe(true);
78
+ expect(spawnImpl).toHaveBeenCalledWith(
79
+ "xdg-open",
80
+ ["http://localhost:18080/"],
81
+ { detached: true, stdio: "ignore" },
82
+ );
83
+ });
84
+
85
+ it("skips xdg-open and prints an SSH tunnel hint on headless Linux", () => {
86
+ const spawnImpl = vi.fn();
87
+ const onInfo = vi.fn();
88
+
89
+ expect(openWebUiInDefaultBrowser(18080, {
90
+ platform: "linux",
91
+ env: {},
92
+ spawnImpl,
93
+ onInfo,
94
+ })).toBe(false);
95
+ expect(spawnImpl).not.toHaveBeenCalled();
96
+ expect(onInfo).toHaveBeenCalledWith(expect.stringContaining("ssh -L 18080:127.0.0.1:18080"));
97
+ expect(onInfo).toHaveBeenCalledWith(expect.stringContaining("http://localhost:18080/"));
98
+ });
99
+ });
100
+
101
+ describe("ChatCCC service lifecycle guard", () => {
102
+ function createHarness() {
103
+ let intervalCallback: (() => void) | undefined;
104
+ const timer = { ref: vi.fn() };
105
+ const setIntervalImpl = vi.fn((callback: () => void) => {
106
+ intervalCallback = callback;
107
+ return timer;
108
+ });
109
+ const clearIntervalImpl = vi.fn();
110
+ const tracer = vi.fn();
111
+ const server = {
112
+ listening: true,
113
+ address: vi.fn(() => ({ address: "127.0.0.1", port: 18080 })),
114
+ ref: vi.fn(),
115
+ };
116
+ const recoverServer = vi.fn(async () => {
117
+ server.listening = true;
118
+ });
119
+ const guard = createServiceLifecycleGuard({
120
+ intervalMs: 10_000,
121
+ setIntervalImpl,
122
+ clearIntervalImpl,
123
+ tracer,
124
+ getActiveResourcesInfo: () => ["TCPServerWrap", "Timeout"],
125
+ });
126
+
127
+ return {
128
+ clearIntervalImpl,
129
+ getIntervalCallback: () => intervalCallback,
130
+ guard,
131
+ recoverServer,
132
+ server,
133
+ setIntervalImpl,
134
+ timer,
135
+ tracer,
136
+ };
137
+ }
138
+
139
+ it("starts exactly one referenced keep-alive timer", () => {
140
+ const h = createHarness();
141
+
142
+ h.guard.start();
143
+ h.guard.start();
144
+
145
+ expect(h.setIntervalImpl).toHaveBeenCalledOnce();
146
+ expect(h.setIntervalImpl).toHaveBeenCalledWith(expect.any(Function), 10_000);
147
+ expect(h.timer.ref).toHaveBeenCalledOnce();
148
+ });
149
+
150
+ it("keeps a listening HTTP server referenced", async () => {
151
+ const h = createHarness();
152
+ h.guard.attachServer(h.server, h.recoverServer);
153
+ h.guard.start();
154
+
155
+ await h.guard.checkNow();
156
+
157
+ expect(h.server.ref).toHaveBeenCalled();
158
+ expect(h.recoverServer).not.toHaveBeenCalled();
159
+ });
160
+
161
+ it("coalesces concurrent recovery when the HTTP server is not listening", async () => {
162
+ const h = createHarness();
163
+ h.server.listening = false;
164
+ let finishRecovery: (() => void) | undefined;
165
+ h.recoverServer.mockImplementation(() => new Promise<void>((resolve) => {
166
+ finishRecovery = () => {
167
+ h.server.listening = true;
168
+ resolve();
169
+ };
170
+ }));
171
+ h.guard.attachServer(h.server, h.recoverServer);
172
+ h.guard.start();
173
+
174
+ const first = h.guard.checkNow();
175
+ const second = h.guard.checkNow();
176
+ await Promise.resolve();
177
+ expect(h.recoverServer).toHaveBeenCalledOnce();
178
+
179
+ finishRecovery?.();
180
+ await Promise.all([first, second]);
181
+ expect(h.tracer).toHaveBeenCalledWith(
182
+ "service-lifecycle: HTTP server recovered",
183
+ expect.objectContaining({ serverListening: true }),
184
+ );
185
+ });
186
+
187
+ it("re-arms on unexpected beforeExit and records public diagnostics", async () => {
188
+ const h = createHarness();
189
+ h.guard.attachServer(h.server, h.recoverServer);
190
+
191
+ h.guard.handleBeforeExit(0);
192
+ await h.guard.checkNow();
193
+
194
+ expect(h.setIntervalImpl).toHaveBeenCalledOnce();
195
+ expect(h.server.ref).toHaveBeenCalled();
196
+ expect(h.tracer).toHaveBeenCalledWith(
197
+ "service-lifecycle: unexpected beforeExit",
198
+ expect.objectContaining({
199
+ code: 0,
200
+ activeResources: ["TCPServerWrap", "Timeout"],
201
+ serverListening: true,
202
+ }),
203
+ );
204
+ });
205
+
206
+ it("stays stopped after an intentional shutdown", () => {
207
+ const h = createHarness();
208
+ h.guard.start();
209
+
210
+ h.guard.beginShutdown("SIGTERM");
211
+ h.guard.handleBeforeExit(0);
212
+
213
+ expect(h.clearIntervalImpl).toHaveBeenCalledWith(h.timer);
214
+ expect(h.setIntervalImpl).toHaveBeenCalledOnce();
215
+ expect(h.tracer).toHaveBeenCalledWith(
216
+ "service-lifecycle: shutdown requested",
217
+ { reason: "SIGTERM" },
218
+ );
219
+ });
220
+
221
+ it("the timer callback runs a health check", async () => {
222
+ const h = createHarness();
223
+ h.guard.attachServer(h.server, h.recoverServer);
224
+ h.guard.start();
225
+
226
+ h.getIntervalCallback()?.();
227
+ await Promise.resolve();
228
+
229
+ expect(h.server.ref).toHaveBeenCalled();
230
+ });
231
+ });
@@ -11,15 +11,15 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
11
11
  import type { StreamState } from "../stream-state.ts";
12
12
 
13
13
  // mock stream-state,使用模块内可观测的 Map 记录读写
14
- const stateStore = new Map<string, StreamState>();
15
- const writeCalls: StreamState[] = [];
16
- const killProcessTreeMock = vi.hoisted(() => vi.fn(async (_pid?: number) => {}));
17
-
18
- vi.mock("../adapters/proc-tree-kill.ts", () => ({
19
- killProcessTree: killProcessTreeMock,
20
- }));
21
-
22
- vi.mock("../stream-state.ts", () => ({
14
+ const stateStore = new Map<string, StreamState>();
15
+ const writeCalls: StreamState[] = [];
16
+ const killProcessTreeMock = vi.hoisted(() => vi.fn(async (_pid?: number) => {}));
17
+
18
+ vi.mock("../adapters/proc-tree-kill.ts", () => ({
19
+ killProcessTree: killProcessTreeMock,
20
+ }));
21
+
22
+ vi.mock("../stream-state.ts", () => ({
23
23
  readStreamState: async (sid: string): Promise<StreamState | null> => {
24
24
  return stateStore.get(sid) ?? null;
25
25
  },
@@ -77,10 +77,10 @@ async function flush(): Promise<void> {
77
77
  }
78
78
 
79
79
  beforeEach(() => {
80
- activePrompts.clear();
81
- stateStore.clear();
82
- writeCalls.length = 0;
83
- killProcessTreeMock.mockClear();
80
+ activePrompts.clear();
81
+ stateStore.clear();
82
+ writeCalls.length = 0;
83
+ killProcessTreeMock.mockClear();
84
84
  });
85
85
 
86
86
  describe("stopSession 行为护栏", () => {
@@ -134,29 +134,29 @@ describe("stopSession 行为护栏", () => {
134
134
  expect(activePrompts.get("sid-C")?.stopped).toBe(true);
135
135
  });
136
136
 
137
- it("调用 adapter 提供的 closeSession 以主动关闭底层 SDK session", () => {
137
+ it("调用 adapter 提供的 closeSession 以主动关闭底层 SDK session", () => {
138
138
  const closeSession = vi.fn();
139
139
  seedRunningSession("sid-D", "partial output", closeSession);
140
140
 
141
141
  const ok = stopSession("sid-D");
142
142
 
143
- expect(ok).toBe(true);
144
- expect(closeSession).toHaveBeenCalledTimes(1);
145
- });
146
-
147
- it("按完整进程树停止 CLI,不先杀壳进程造成后代进程逃逸", async () => {
148
- seedRunningSession("sid-tree");
149
- activePrompts.get("sid-tree")!.processPid = 4242;
150
- const processKillSpy = vi.spyOn(process, "kill").mockImplementation(() => true);
151
-
152
- try {
153
- expect(stopSession("sid-tree")).toBe(true);
154
- await flush();
155
-
156
- expect(killProcessTreeMock).toHaveBeenCalledWith(4242);
157
- expect(processKillSpy).not.toHaveBeenCalled();
158
- } finally {
159
- processKillSpy.mockRestore();
160
- }
161
- });
162
- });
143
+ expect(ok).toBe(true);
144
+ expect(closeSession).toHaveBeenCalledTimes(1);
145
+ });
146
+
147
+ it("按完整进程树停止 CLI,不先杀壳进程造成后代进程逃逸", async () => {
148
+ seedRunningSession("sid-tree");
149
+ activePrompts.get("sid-tree")!.processPid = 4242;
150
+ const processKillSpy = vi.spyOn(process, "kill").mockImplementation(() => true);
151
+
152
+ try {
153
+ expect(stopSession("sid-tree")).toBe(true);
154
+ await flush();
155
+
156
+ expect(killProcessTreeMock).toHaveBeenCalledWith(4242);
157
+ expect(processKillSpy).not.toHaveBeenCalled();
158
+ } finally {
159
+ processKillSpy.mockRestore();
160
+ }
161
+ });
162
+ });
@@ -15,12 +15,12 @@ vi.mock("../config.ts", async () => {
15
15
  });
16
16
 
17
17
  const {
18
- writeStreamState,
19
- readStreamState,
20
- createEmptyStreamState,
21
- isFinalReplySentForTurn,
22
- markFinalReplySent,
23
- STREAMS_DIR,
18
+ writeStreamState,
19
+ readStreamState,
20
+ createEmptyStreamState,
21
+ isFinalReplySentForTurn,
22
+ markFinalReplySent,
23
+ STREAMS_DIR,
24
24
  _setRenameForTest,
25
25
  _resetRenameForTest,
26
26
  } = await import("../stream-state.ts");
@@ -119,36 +119,36 @@ describe("writeStreamState — atomic rename", () => {
119
119
  const got = await readStreamState("never-existed");
120
120
  expect(got).toBeNull();
121
121
  });
122
- it("marks final reply delivery for the matching terminal turn only", async () => {
123
- const state = createEmptyStreamState("sid-delivered", "/tmp", "claude", 2);
124
- state.status = "done";
125
- state.finalReply = "done";
126
- await writeStreamState(state);
127
-
128
- await markFinalReplySent("sid-delivered", 2, 12345);
129
-
130
- const got = await readStreamState("sid-delivered");
131
- expect(got).not.toBeNull();
132
- expect(got!.finalReplySentTurn).toBe(2);
133
- expect(got!.finalReplySentAt).toBe(12345);
134
- expect(isFinalReplySentForTurn(got!)).toBe(true);
135
- });
136
-
137
- it("does not mark a running state or a different turn as delivered", async () => {
138
- const state = createEmptyStreamState("sid-running", "/tmp", "claude", 3);
139
- await writeStreamState(state);
140
-
141
- await markFinalReplySent("sid-running", 2, 12345);
142
- await markFinalReplySent("sid-running", 3, 12345);
143
-
144
- const got = await readStreamState("sid-running");
145
- expect(got).not.toBeNull();
146
- expect(got!.finalReplySentTurn).toBeUndefined();
147
- expect(isFinalReplySentForTurn(got!)).toBe(false);
148
- });
149
- });
150
-
151
- describe("createEmptyStreamState", () => {
122
+ it("marks final reply delivery for the matching terminal turn only", async () => {
123
+ const state = createEmptyStreamState("sid-delivered", "/tmp", "claude", 2);
124
+ state.status = "done";
125
+ state.finalReply = "done";
126
+ await writeStreamState(state);
127
+
128
+ await markFinalReplySent("sid-delivered", 2, 12345);
129
+
130
+ const got = await readStreamState("sid-delivered");
131
+ expect(got).not.toBeNull();
132
+ expect(got!.finalReplySentTurn).toBe(2);
133
+ expect(got!.finalReplySentAt).toBe(12345);
134
+ expect(isFinalReplySentForTurn(got!)).toBe(true);
135
+ });
136
+
137
+ it("does not mark a running state or a different turn as delivered", async () => {
138
+ const state = createEmptyStreamState("sid-running", "/tmp", "claude", 3);
139
+ await writeStreamState(state);
140
+
141
+ await markFinalReplySent("sid-running", 2, 12345);
142
+ await markFinalReplySent("sid-running", 3, 12345);
143
+
144
+ const got = await readStreamState("sid-running");
145
+ expect(got).not.toBeNull();
146
+ expect(got!.finalReplySentTurn).toBeUndefined();
147
+ expect(isFinalReplySentForTurn(got!)).toBe(false);
148
+ });
149
+ });
150
+
151
+ describe("createEmptyStreamState", () => {
152
152
  it("生成的 state 字段正确且 status=running", () => {
153
153
  const s = createEmptyStreamState("sid-X", "/cwd", "cursor", 5);
154
154
  expect(s.sessionId).toBe("sid-X");
@@ -156,9 +156,9 @@ describe("createEmptyStreamState", () => {
156
156
  expect(s.tool).toBe("cursor");
157
157
  expect(s.turnCount).toBe(5);
158
158
  expect(s.status).toBe("running");
159
- expect(s.accumulatedContent).toBe("");
160
- expect(s.finalReply).toBe("");
161
- expect(s.finalReplySentTurn).toBeUndefined();
162
- expect(s.chunkCount).toBe(0);
163
- });
164
- });
159
+ expect(s.accumulatedContent).toBe("");
160
+ expect(s.finalReply).toBe("");
161
+ expect(s.finalReplySentTurn).toBeUndefined();
162
+ expect(s.chunkCount).toBe(0);
163
+ });
164
+ });