@kodelyth/codex 2026.5.42 → 2026.6.1

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 (138) hide show
  1. package/package.json +16 -1
  2. package/doctor-contract-api.test.ts +0 -44
  3. package/doctor-contract-api.ts +0 -68
  4. package/harness.ts +0 -72
  5. package/index.test.ts +0 -230
  6. package/index.ts +0 -66
  7. package/media-understanding-provider.test.ts +0 -486
  8. package/media-understanding-provider.ts +0 -521
  9. package/prompt-overlay-runtime-contract.test.ts +0 -48
  10. package/prompt-overlay.ts +0 -21
  11. package/provider-catalog.ts +0 -83
  12. package/provider-discovery.ts +0 -45
  13. package/provider.test.ts +0 -384
  14. package/provider.ts +0 -243
  15. package/src/app-server/app-inventory-cache.test.ts +0 -176
  16. package/src/app-server/app-inventory-cache.ts +0 -324
  17. package/src/app-server/approval-bridge.test.ts +0 -1471
  18. package/src/app-server/approval-bridge.ts +0 -1211
  19. package/src/app-server/auth-bridge.test.ts +0 -1449
  20. package/src/app-server/auth-bridge.ts +0 -614
  21. package/src/app-server/auth-profile-runtime-contract.test.ts +0 -239
  22. package/src/app-server/capabilities.ts +0 -27
  23. package/src/app-server/client-factory.ts +0 -24
  24. package/src/app-server/client.test.ts +0 -563
  25. package/src/app-server/client.ts +0 -715
  26. package/src/app-server/compact.test.ts +0 -710
  27. package/src/app-server/compact.ts +0 -500
  28. package/src/app-server/computer-use.test.ts +0 -788
  29. package/src/app-server/computer-use.ts +0 -683
  30. package/src/app-server/config.test.ts +0 -879
  31. package/src/app-server/config.ts +0 -1038
  32. package/src/app-server/context-engine-projection.test.ts +0 -252
  33. package/src/app-server/context-engine-projection.ts +0 -403
  34. package/src/app-server/delivery-no-reply-runtime-contract.test.ts +0 -80
  35. package/src/app-server/dynamic-tool-diagnostics.ts +0 -73
  36. package/src/app-server/dynamic-tool-profile.ts +0 -69
  37. package/src/app-server/dynamic-tools.test.ts +0 -1302
  38. package/src/app-server/dynamic-tools.ts +0 -623
  39. package/src/app-server/elicitation-bridge.test.ts +0 -1056
  40. package/src/app-server/elicitation-bridge.ts +0 -783
  41. package/src/app-server/event-projector.test.ts +0 -2668
  42. package/src/app-server/event-projector.ts +0 -2057
  43. package/src/app-server/image-payload-sanitizer.test.ts +0 -49
  44. package/src/app-server/image-payload-sanitizer.ts +0 -167
  45. package/src/app-server/klaw-owned-tool-runtime-contract.test.ts +0 -456
  46. package/src/app-server/local-runtime-attribution.ts +0 -39
  47. package/src/app-server/managed-binary.test.ts +0 -139
  48. package/src/app-server/managed-binary.ts +0 -193
  49. package/src/app-server/models.test.ts +0 -246
  50. package/src/app-server/models.ts +0 -172
  51. package/src/app-server/native-hook-relay.test.ts +0 -271
  52. package/src/app-server/native-hook-relay.ts +0 -150
  53. package/src/app-server/native-subagent-task-mirror.test.ts +0 -573
  54. package/src/app-server/native-subagent-task-mirror.ts +0 -497
  55. package/src/app-server/outcome-fallback-runtime-contract.test.ts +0 -404
  56. package/src/app-server/plugin-activation.test.ts +0 -336
  57. package/src/app-server/plugin-activation.ts +0 -283
  58. package/src/app-server/plugin-app-cache-key.ts +0 -74
  59. package/src/app-server/plugin-approval-roundtrip.ts +0 -122
  60. package/src/app-server/plugin-inventory.test.ts +0 -355
  61. package/src/app-server/plugin-inventory.ts +0 -357
  62. package/src/app-server/plugin-thread-config.test.ts +0 -865
  63. package/src/app-server/plugin-thread-config.ts +0 -455
  64. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +0 -33
  65. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +0 -199
  66. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +0 -102
  67. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +0 -227
  68. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +0 -2630
  69. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +0 -2630
  70. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +0 -1659
  71. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +0 -1655
  72. package/src/app-server/protocol-validators.test.ts +0 -75
  73. package/src/app-server/protocol-validators.ts +0 -203
  74. package/src/app-server/protocol.ts +0 -520
  75. package/src/app-server/rate-limit-cache.ts +0 -48
  76. package/src/app-server/rate-limits.test.ts +0 -202
  77. package/src/app-server/rate-limits.ts +0 -583
  78. package/src/app-server/request.ts +0 -73
  79. package/src/app-server/run-attempt.context-engine.test.ts +0 -1004
  80. package/src/app-server/run-attempt.test.ts +0 -9477
  81. package/src/app-server/run-attempt.ts +0 -4683
  82. package/src/app-server/run-attempt.vision-tools.test.ts +0 -35
  83. package/src/app-server/schema-normalization-runtime-contract.test.ts +0 -206
  84. package/src/app-server/session-binding.test.ts +0 -303
  85. package/src/app-server/session-binding.ts +0 -398
  86. package/src/app-server/session-history.ts +0 -44
  87. package/src/app-server/shared-client.test.ts +0 -589
  88. package/src/app-server/shared-client.ts +0 -289
  89. package/src/app-server/side-question.test.ts +0 -1175
  90. package/src/app-server/side-question.ts +0 -1007
  91. package/src/app-server/test-support.ts +0 -48
  92. package/src/app-server/thread-lifecycle.test.ts +0 -447
  93. package/src/app-server/thread-lifecycle.ts +0 -939
  94. package/src/app-server/thread-lifecycle.user-mcp-servers.test.ts +0 -442
  95. package/src/app-server/timeout.ts +0 -9
  96. package/src/app-server/tool-progress-normalization.ts +0 -77
  97. package/src/app-server/trajectory.test.ts +0 -205
  98. package/src/app-server/trajectory.ts +0 -365
  99. package/src/app-server/transcript-mirror.test.ts +0 -524
  100. package/src/app-server/transcript-mirror.ts +0 -208
  101. package/src/app-server/transcript-repair-runtime-contract.test.ts +0 -44
  102. package/src/app-server/transport-stdio.test.ts +0 -171
  103. package/src/app-server/transport-stdio.ts +0 -107
  104. package/src/app-server/transport-websocket.test.ts +0 -69
  105. package/src/app-server/transport-websocket.ts +0 -90
  106. package/src/app-server/transport.ts +0 -117
  107. package/src/app-server/user-input-bridge.test.ts +0 -249
  108. package/src/app-server/user-input-bridge.ts +0 -316
  109. package/src/app-server/version.ts +0 -4
  110. package/src/app-server/vision-tools.ts +0 -12
  111. package/src/command-account.ts +0 -544
  112. package/src/command-formatters.ts +0 -425
  113. package/src/command-handlers.ts +0 -2004
  114. package/src/command-rpc.test.ts +0 -16
  115. package/src/command-rpc.ts +0 -142
  116. package/src/commands.test.ts +0 -3312
  117. package/src/commands.ts +0 -65
  118. package/src/conversation-binding-data.ts +0 -124
  119. package/src/conversation-binding.test.ts +0 -599
  120. package/src/conversation-binding.ts +0 -561
  121. package/src/conversation-control.test.ts +0 -126
  122. package/src/conversation-control.ts +0 -303
  123. package/src/conversation-turn-collector.test.ts +0 -191
  124. package/src/conversation-turn-collector.ts +0 -186
  125. package/src/conversation-turn-input.test.ts +0 -141
  126. package/src/conversation-turn-input.ts +0 -106
  127. package/src/manifest.test.ts +0 -20
  128. package/src/migration/apply.ts +0 -501
  129. package/src/migration/helpers.ts +0 -55
  130. package/src/migration/plan.ts +0 -461
  131. package/src/migration/provider.test.ts +0 -1741
  132. package/src/migration/provider.ts +0 -41
  133. package/src/migration/source.ts +0 -643
  134. package/src/migration/targets.ts +0 -25
  135. package/src/node-cli-sessions.test.ts +0 -180
  136. package/src/node-cli-sessions.ts +0 -711
  137. package/test-api.ts +0 -82
  138. package/tsconfig.json +0 -16
@@ -1,1175 +0,0 @@
1
- import { nativeHookRelayTesting } from "klaw/plugin-sdk/agent-harness-runtime";
2
- import {
3
- onInternalDiagnosticEvent,
4
- resetDiagnosticEventsForTest,
5
- type DiagnosticEventPayload,
6
- } from "klaw/plugin-sdk/diagnostic-runtime";
7
- import { initializeGlobalHookRunner, resetGlobalHookRunner } from "klaw/plugin-sdk/hook-runtime";
8
- import { createMockPluginRegistry } from "klaw/plugin-sdk/plugin-test-runtime";
9
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10
- import type { CodexServerNotification, RpcRequest } from "./protocol.js";
11
-
12
- const readCodexAppServerBindingMock = vi.fn();
13
- const isCodexAppServerNativeAuthProfileMock = vi.fn();
14
- const getSharedCodexAppServerClientMock = vi.fn();
15
- const refreshCodexAppServerAuthTokensMock = vi.fn();
16
- const createKlawCodingToolsMock = vi.fn();
17
- const toolExecuteMock = vi.fn();
18
- const handleCodexAppServerApprovalRequestMock = vi.fn();
19
-
20
- vi.mock("./session-binding.js", () => ({
21
- clearCodexAppServerBinding: vi.fn(),
22
- isCodexAppServerNativeAuthProfile: (...args: unknown[]) =>
23
- isCodexAppServerNativeAuthProfileMock(...args),
24
- readCodexAppServerBinding: (...args: unknown[]) => readCodexAppServerBindingMock(...args),
25
- writeCodexAppServerBinding: vi.fn(),
26
- }));
27
-
28
- vi.mock("./shared-client.js", () => ({
29
- getSharedCodexAppServerClient: (...args: unknown[]) => getSharedCodexAppServerClientMock(...args),
30
- }));
31
-
32
- vi.mock("./auth-bridge.js", () => ({
33
- refreshCodexAppServerAuthTokens: (...args: unknown[]) =>
34
- refreshCodexAppServerAuthTokensMock(...args),
35
- }));
36
-
37
- vi.mock("./approval-bridge.js", () => ({
38
- handleCodexAppServerApprovalRequest: (...args: unknown[]) =>
39
- handleCodexAppServerApprovalRequestMock(...args),
40
- }));
41
-
42
- vi.mock("klaw/plugin-sdk/agent-harness", () => ({
43
- createKlawCodingTools: (...args: unknown[]) => createKlawCodingToolsMock(...args),
44
- createKlawCodingTools: (...args: unknown[]) => createKlawCodingToolsMock(...args),
45
- }));
46
-
47
- const { testing, runCodexAppServerSideQuestion } = await import("./side-question.js");
48
-
49
- type ServerRequest = Required<Pick<RpcRequest, "id" | "method">> & {
50
- params?: RpcRequest["params"];
51
- };
52
-
53
- type FakeClient = {
54
- request: ReturnType<typeof vi.fn>;
55
- addNotificationHandler: ReturnType<typeof vi.fn>;
56
- addRequestHandler: ReturnType<typeof vi.fn>;
57
- notifications: Array<(notification: CodexServerNotification) => void>;
58
- requests: Array<(request: ServerRequest) => unknown>;
59
- emit: (notification: CodexServerNotification) => void;
60
- handleRequest: (request: ServerRequest) => Promise<unknown>;
61
- };
62
-
63
- function createFakeClient(): FakeClient {
64
- const notifications: FakeClient["notifications"] = [];
65
- const requests: FakeClient["requests"] = [];
66
- const client: FakeClient = {
67
- notifications,
68
- requests,
69
- request: vi.fn(),
70
- addNotificationHandler: vi.fn((handler: (notification: CodexServerNotification) => void) => {
71
- notifications.push(handler);
72
- return () => {
73
- const index = notifications.indexOf(handler);
74
- if (index >= 0) {
75
- notifications.splice(index, 1);
76
- }
77
- };
78
- }),
79
- addRequestHandler: vi.fn((handler: FakeClient["requests"][number]) => {
80
- requests.push(handler);
81
- return () => {
82
- const index = requests.indexOf(handler);
83
- if (index >= 0) {
84
- requests.splice(index, 1);
85
- }
86
- };
87
- }),
88
- emit: (notification) => {
89
- for (const handler of notifications) {
90
- handler(notification);
91
- }
92
- },
93
- handleRequest: async (request) => {
94
- for (const handler of requests) {
95
- const result = await handler(request);
96
- if (result !== undefined) {
97
- return result;
98
- }
99
- }
100
- return undefined;
101
- },
102
- };
103
- client.request.mockImplementation(async (method: string) => {
104
- if (method === "thread/fork") {
105
- return threadResult("side-thread");
106
- }
107
- if (method === "thread/inject_items") {
108
- return {};
109
- }
110
- if (method === "turn/start") {
111
- queueMicrotask(() => {
112
- client.emit(agentDelta("side-thread", "turn-1", "Side answer."));
113
- client.emit(turnCompleted("side-thread", "turn-1", "Side answer."));
114
- });
115
- return turnStartResult("turn-1");
116
- }
117
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
118
- return {};
119
- }
120
- throw new Error(`unexpected request: ${method}`);
121
- });
122
- return client;
123
- }
124
-
125
- function mockCall(mock: ReturnType<typeof vi.fn>, index = 0): unknown[] {
126
- const call = mock.mock.calls.at(index);
127
- if (!call) {
128
- throw new Error(`Expected mock call ${index}`);
129
- }
130
- return call;
131
- }
132
-
133
- function flushDiagnosticEvents() {
134
- return new Promise<void>((resolve) => setImmediate(resolve));
135
- }
136
-
137
- function activeDiagnosticToolKeys(events: DiagnosticEventPayload[]): Set<string> {
138
- const active = new Set<string>();
139
- for (const event of events) {
140
- if (event.type === "tool.execution.started") {
141
- active.add(
142
- `${event.runId ?? event.sessionId ?? event.sessionKey ?? "unknown"}:${event.toolCallId ?? event.toolName}`,
143
- );
144
- } else if (
145
- event.type === "tool.execution.completed" ||
146
- event.type === "tool.execution.error" ||
147
- event.type === "tool.execution.blocked"
148
- ) {
149
- active.delete(
150
- `${event.runId ?? event.sessionId ?? event.sessionKey ?? "unknown"}:${event.toolCallId ?? event.toolName}`,
151
- );
152
- }
153
- }
154
- return active;
155
- }
156
-
157
- function extractRelayIdFromThreadConfig(config: unknown): string {
158
- const record = config as Record<string, unknown> | undefined;
159
- let command: string | undefined;
160
- for (const key of [
161
- "hooks.PreToolUse",
162
- "hooks.PostToolUse",
163
- "hooks.PermissionRequest",
164
- "hooks.Stop",
165
- ]) {
166
- const entries = record?.[key];
167
- if (!Array.isArray(entries)) {
168
- continue;
169
- }
170
- for (const entry of entries as Array<{ hooks?: Array<{ command?: string }> }>) {
171
- command = entry.hooks?.find((hook) => typeof hook.command === "string")?.command;
172
- if (command) {
173
- break;
174
- }
175
- }
176
- if (command) {
177
- break;
178
- }
179
- }
180
- const match = command?.match(/--relay-id ([^ ]+)/);
181
- if (!match?.[1]) {
182
- throw new Error(`relay id missing from command: ${command}`);
183
- }
184
- return match[1];
185
- }
186
-
187
- function codexHookCommand(config: unknown, key: string) {
188
- const entries = (config as Record<string, unknown> | undefined)?.[key];
189
- if (!Array.isArray(entries)) {
190
- return undefined;
191
- }
192
- return (
193
- entries as Array<{ hooks?: Array<{ command?: string; timeout?: number; type?: string }> }>
194
- )
195
- .at(0)
196
- ?.hooks?.at(0);
197
- }
198
-
199
- function codexHookStateForEvent(
200
- hookState: Record<string, { enabled?: unknown; trusted_hash?: unknown }> | undefined,
201
- event: string,
202
- ) {
203
- return Object.entries(hookState ?? {}).find(([key]) => key.endsWith(`:${event}:0:0`))?.[1];
204
- }
205
-
206
- function threadResult(threadId: string) {
207
- return {
208
- thread: {
209
- id: threadId,
210
- sessionId: threadId,
211
- forkedFromId: null,
212
- preview: "",
213
- ephemeral: true,
214
- modelProvider: "openai",
215
- createdAt: 1,
216
- updatedAt: 1,
217
- status: { type: "idle" },
218
- path: null,
219
- cwd: "/tmp/workspace",
220
- cliVersion: "0.125.0",
221
- source: "unknown",
222
- agentNickname: null,
223
- agentRole: null,
224
- gitInfo: null,
225
- name: null,
226
- turns: [],
227
- },
228
- model: "gpt-5.5",
229
- modelProvider: "openai",
230
- cwd: "/tmp/workspace",
231
- approvalPolicy: "on-request",
232
- approvalsReviewer: "user",
233
- sandbox: { type: "dangerFullAccess" },
234
- };
235
- }
236
-
237
- function turnStartResult(turnId: string) {
238
- return {
239
- turn: {
240
- id: turnId,
241
- threadId: "side-thread",
242
- status: "inProgress",
243
- items: [],
244
- error: null,
245
- startedAt: null,
246
- completedAt: null,
247
- durationMs: null,
248
- },
249
- };
250
- }
251
-
252
- function agentDelta(threadId: string, turnId: string, delta: string): CodexServerNotification {
253
- return {
254
- method: "item/agentMessage/delta",
255
- params: { threadId, turnId, itemId: "agent-1", delta },
256
- };
257
- }
258
-
259
- function turnCompleted(threadId: string, turnId: string, text: string): CodexServerNotification {
260
- return {
261
- method: "turn/completed",
262
- params: {
263
- threadId,
264
- turn: {
265
- id: turnId,
266
- threadId,
267
- status: "completed",
268
- items: [{ id: "agent-1", type: "agentMessage", text }],
269
- error: null,
270
- startedAt: null,
271
- completedAt: null,
272
- durationMs: null,
273
- },
274
- },
275
- };
276
- }
277
-
278
- function sideParams(overrides: Partial<Parameters<typeof runCodexAppServerSideQuestion>[0]> = {}) {
279
- return {
280
- cfg: {} as never,
281
- agentDir: "/tmp/agent",
282
- provider: "openai",
283
- model: "gpt-5.5",
284
- question: "What changed?",
285
- sessionEntry: {
286
- sessionId: "session-1",
287
- sessionFile: "/tmp/session-1.jsonl",
288
- updatedAt: 1,
289
- },
290
- resolvedReasoningLevel: "off",
291
- opts: {},
292
- isNewSession: false,
293
- sessionId: "session-1",
294
- sessionFile: "/tmp/session-1.jsonl",
295
- workspaceDir: "/tmp/workspace",
296
- authProfileId: "openai-codex:work",
297
- authProfileIdSource: "user",
298
- ...overrides,
299
- } satisfies Parameters<typeof runCodexAppServerSideQuestion>[0];
300
- }
301
-
302
- describe("runCodexAppServerSideQuestion", () => {
303
- beforeEach(() => {
304
- nativeHookRelayTesting.clearNativeHookRelaysForTests();
305
- readCodexAppServerBindingMock.mockReset();
306
- isCodexAppServerNativeAuthProfileMock.mockReset();
307
- getSharedCodexAppServerClientMock.mockReset();
308
- refreshCodexAppServerAuthTokensMock.mockReset();
309
- createKlawCodingToolsMock.mockReset();
310
- toolExecuteMock.mockReset();
311
- handleCodexAppServerApprovalRequestMock.mockReset();
312
-
313
- toolExecuteMock.mockResolvedValue({
314
- content: [{ type: "text", text: "tool output" }],
315
- });
316
- createKlawCodingToolsMock.mockReturnValue([
317
- {
318
- name: "wiki_status",
319
- description: "Check wiki status",
320
- parameters: { type: "object", properties: {} },
321
- execute: toolExecuteMock,
322
- },
323
- ]);
324
-
325
- readCodexAppServerBindingMock.mockResolvedValue({
326
- schemaVersion: 1,
327
- threadId: "parent-thread",
328
- sessionFile: "/tmp/session-1.jsonl",
329
- cwd: "/tmp/workspace",
330
- authProfileId: "openai-codex:work",
331
- model: "gpt-5.5",
332
- approvalPolicy: "on-request",
333
- sandbox: "workspace-write",
334
- createdAt: new Date(0).toISOString(),
335
- updatedAt: new Date(0).toISOString(),
336
- });
337
- isCodexAppServerNativeAuthProfileMock.mockReturnValue(true);
338
- getSharedCodexAppServerClientMock.mockResolvedValue(createFakeClient());
339
- refreshCodexAppServerAuthTokensMock.mockResolvedValue({
340
- accessToken: "access-token",
341
- chatgptAccountId: "account-1",
342
- chatgptPlanType: "plus",
343
- });
344
- });
345
-
346
- afterEach(() => {
347
- nativeHookRelayTesting.clearNativeHookRelaysForTests();
348
- resetDiagnosticEventsForTest();
349
- resetGlobalHookRunner();
350
- });
351
-
352
- it("forks an ephemeral side thread and returns the completed assistant text", async () => {
353
- const client = createFakeClient();
354
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
355
-
356
- const result = await runCodexAppServerSideQuestion(
357
- sideParams({
358
- messageChannel: "discord",
359
- messageProvider: "discord-voice",
360
- currentChannelId: "voice-room",
361
- }),
362
- );
363
-
364
- expect(result).toEqual({ text: "Side answer." });
365
- const forkCall = mockCall(client.request);
366
- expect(forkCall?.[0]).toBe("thread/fork");
367
- const forkParams = forkCall?.[1] as Record<string, unknown> | undefined;
368
- expect(Object.keys(forkParams ?? {}).toSorted()).toEqual([
369
- "approvalPolicy",
370
- "approvalsReviewer",
371
- "config",
372
- "cwd",
373
- "developerInstructions",
374
- "ephemeral",
375
- "model",
376
- "sandbox",
377
- "threadId",
378
- "threadSource",
379
- ]);
380
- expect(forkParams?.threadId).toBe("parent-thread");
381
- expect(forkParams?.model).toBe("gpt-5.5");
382
- expect(forkParams?.approvalPolicy).toBe("on-request");
383
- expect(forkParams?.sandbox).toBe("workspace-write");
384
- expect(forkParams?.ephemeral).toBe(true);
385
- expect(forkParams?.threadSource).toBe("user");
386
- expect(forkParams?.approvalsReviewer).toBe("user");
387
- expect(forkParams?.cwd).toBe("/tmp/workspace");
388
- expect(forkParams?.config).toEqual({
389
- "features.code_mode": true,
390
- "features.code_mode_only": false,
391
- });
392
- expect(forkParams?.developerInstructions).toContain("You are in a side conversation");
393
- expect(forkParams?.developerInstructions).toContain(
394
- "Only instructions submitted after the side-conversation boundary are active.",
395
- );
396
- expect(forkCall?.[2]).toEqual({ timeoutMs: 60_000, signal: undefined });
397
-
398
- const injectCall = mockCall(client.request, 1);
399
- expect(injectCall?.[0]).toBe("thread/inject_items");
400
- const injectParams = injectCall?.[1] as
401
- | { threadId?: string; items?: Array<{ type?: string; role?: string; content?: unknown }> }
402
- | undefined;
403
- expect(injectParams?.threadId).toBe("side-thread");
404
- expect(injectParams?.items).toHaveLength(1);
405
- expect(injectParams?.items?.[0]?.type).toBe("message");
406
- expect(injectParams?.items?.[0]?.role).toBe("user");
407
- expect(injectCall?.[2]).toEqual({ timeoutMs: 60_000, signal: undefined });
408
- const injectedItem = injectParams?.items?.[0] as
409
- | { content?: Array<{ text?: string }> }
410
- | undefined;
411
- const injectedText = injectedItem?.content?.[0]?.text;
412
- expect(injectedText).toContain(
413
- "External tools may be available according to this thread's current permissions",
414
- );
415
- expect(injectedText).toContain(
416
- "unless the user explicitly asks for that mutation after this boundary",
417
- );
418
- const turnStartCall = client.request.mock.calls.find(([method]) => method === "turn/start");
419
- expect(turnStartCall).toEqual([
420
- "turn/start",
421
- {
422
- threadId: "side-thread",
423
- input: [{ type: "text", text: "What changed?", text_elements: [] }],
424
- cwd: "/tmp/workspace",
425
- model: "gpt-5.5",
426
- effort: null,
427
- collaborationMode: {
428
- mode: "default",
429
- settings: {
430
- model: "gpt-5.5",
431
- reasoning_effort: null,
432
- developer_instructions: null,
433
- },
434
- },
435
- },
436
- { timeoutMs: 60_000, signal: undefined },
437
- ]);
438
- const turnStartParams = turnStartCall?.[1] as Record<string, unknown> | undefined;
439
- expect(turnStartParams).not.toHaveProperty("approvalPolicy");
440
- expect(turnStartParams).not.toHaveProperty("sandboxPolicy");
441
- expect(client.request.mock.calls.at(-1)).toEqual([
442
- "thread/unsubscribe",
443
- { threadId: "side-thread" },
444
- { timeoutMs: 60_000 },
445
- ]);
446
- expect(client.request.mock.calls.some(([method]) => method === "turn/interrupt")).toBe(false);
447
-
448
- const [toolOptions] = mockCall(createKlawCodingToolsMock);
449
- expect(toolOptions).toHaveProperty("agentDir", "/tmp/agent");
450
- expect(toolOptions).toHaveProperty("workspaceDir", "/tmp/workspace");
451
- expect(toolOptions).toHaveProperty("sessionId", "session-1");
452
- expect(toolOptions).toHaveProperty("modelProvider", "openai");
453
- expect(toolOptions).toHaveProperty("modelId", "gpt-5.5");
454
- expect(toolOptions).toHaveProperty("messageProvider", "discord-voice");
455
- expect(toolOptions).toHaveProperty("currentChannelId", "voice-room");
456
- expect(toolOptions).toHaveProperty("requireExplicitMessageTarget", true);
457
- });
458
-
459
- it("installs native hook relay config for opted-in side threads", async () => {
460
- const client = createFakeClient();
461
- let relayIdDuringFork: string | undefined;
462
- client.request.mockImplementation(async (method: string, requestParams: unknown) => {
463
- if (method === "thread/fork") {
464
- const config = (requestParams as { config?: Record<string, unknown> }).config;
465
- relayIdDuringFork = extractRelayIdFromThreadConfig(config);
466
- expect(
467
- nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayIdDuringFork),
468
- ).toMatchObject({
469
- agentId: "main",
470
- sessionId: "session-1",
471
- sessionKey: "agent:main:session-1",
472
- runId: "run-side-1",
473
- channelId: "voice-room",
474
- allowedEvents: ["pre_tool_use", "post_tool_use", "before_agent_finalize"],
475
- });
476
- return threadResult("side-thread");
477
- }
478
- if (method === "thread/inject_items") {
479
- return {};
480
- }
481
- if (method === "turn/start") {
482
- queueMicrotask(() => {
483
- client.emit(agentDelta("side-thread", "turn-1", "Side answer."));
484
- client.emit(turnCompleted("side-thread", "turn-1", "Side answer."));
485
- });
486
- return turnStartResult("turn-1");
487
- }
488
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
489
- return {};
490
- }
491
- throw new Error(`unexpected request: ${method}`);
492
- });
493
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
494
-
495
- await expect(
496
- runCodexAppServerSideQuestion(
497
- sideParams({
498
- sessionKey: "agent:main:session-1",
499
- messageChannel: "discord",
500
- messageProvider: "discord-voice",
501
- currentChannelId: "discord:voice-room",
502
- opts: { runId: "run-side-1" },
503
- }),
504
- { nativeHookRelay: { enabled: true, hookTimeoutSec: 9 } },
505
- ),
506
- ).resolves.toEqual({ text: "Side answer." });
507
-
508
- const forkParams = mockCall(client.request)[1] as Record<string, unknown> | undefined;
509
- const config = forkParams?.config as Record<string, unknown> | undefined;
510
- expect(config?.["features.hooks"]).toBe(true);
511
- expect(config?.["features.code_mode"]).toBe(true);
512
- expect(config?.["features.code_mode_only"]).toBe(false);
513
- expect(config?.["hooks.PermissionRequest"]).toEqual([]);
514
- const preToolUseHooks = config?.["hooks.PreToolUse"] as
515
- | Array<{ hooks?: Array<{ command?: string; timeout?: number; type?: string }> }>
516
- | undefined;
517
- const preToolUseCommand = preToolUseHooks?.[0]?.hooks?.[0];
518
- expect(preToolUseCommand?.type).toBe("command");
519
- expect(preToolUseCommand?.timeout).toBe(9);
520
- expect(preToolUseCommand?.command).toContain("--event pre_tool_use");
521
- const hookState = config?.["hooks.state"] as
522
- | Record<string, { enabled?: unknown; trusted_hash?: unknown }>
523
- | undefined;
524
- const preToolUseState = codexHookStateForEvent(hookState, "pre_tool_use");
525
- expect(preToolUseState?.enabled).toBe(true);
526
- expect(preToolUseState?.trusted_hash).toMatch(/^sha256:[a-f0-9]{64}$/);
527
- const permissionRequestState = codexHookStateForEvent(hookState, "permission_request");
528
- expect(permissionRequestState).toEqual({ enabled: false });
529
- const turnStartCall = client.request.mock.calls.find(([method]) => method === "turn/start");
530
- expect(turnStartCall?.[1]).not.toHaveProperty("config");
531
- expect(relayIdDuringFork).toBeDefined();
532
- expect(
533
- nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayIdDuringFork!),
534
- ).toBeUndefined();
535
- });
536
-
537
- it("forwards side-thread command approvals through the active native hook relay", async () => {
538
- const client = createFakeClient();
539
- let relayIdDuringFork: string | undefined;
540
- let approvalResponse: unknown;
541
- handleCodexAppServerApprovalRequestMock.mockResolvedValueOnce({ decision: "decline" });
542
- client.request.mockImplementation(async (method: string, requestParams: unknown) => {
543
- if (method === "thread/fork") {
544
- const config = (requestParams as { config?: Record<string, unknown> }).config;
545
- relayIdDuringFork = extractRelayIdFromThreadConfig(config);
546
- return threadResult("side-thread");
547
- }
548
- if (method === "thread/inject_items") {
549
- return {};
550
- }
551
- if (method === "turn/start") {
552
- setTimeout(async () => {
553
- approvalResponse = await client.handleRequest({
554
- id: 42,
555
- method: "item/commandExecution/requestApproval",
556
- params: {
557
- threadId: "side-thread",
558
- turnId: "turn-1",
559
- itemId: "cmd-side",
560
- command: "/bin/bash -lc 'node -v'",
561
- cwd: "/tmp/workspace",
562
- },
563
- });
564
- client.emit(turnCompleted("side-thread", "turn-1", "Side answer."));
565
- }, 0);
566
- return turnStartResult("turn-1");
567
- }
568
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
569
- return {};
570
- }
571
- throw new Error(`unexpected request: ${method}`);
572
- });
573
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
574
-
575
- await expect(
576
- runCodexAppServerSideQuestion(
577
- sideParams({
578
- sessionKey: "agent:main:session-1",
579
- messageChannel: "discord",
580
- messageProvider: "discord-voice",
581
- opts: { runId: "run-side-approval" },
582
- }),
583
- { nativeHookRelay: { enabled: true } },
584
- ),
585
- ).resolves.toEqual({ text: "Side answer." });
586
-
587
- expect(approvalResponse).toEqual({ decision: "decline" });
588
- expect(handleCodexAppServerApprovalRequestMock).toHaveBeenCalledTimes(1);
589
- const approvalArgs = handleCodexAppServerApprovalRequestMock.mock.calls[0]?.[0] as
590
- | {
591
- method?: string;
592
- requestParams?: Record<string, unknown>;
593
- threadId?: string;
594
- turnId?: string;
595
- paramsForRun?: { messageChannel?: string; messageProvider?: string };
596
- nativeHookRelay?: { relayId?: string; allowedEvents?: readonly string[] };
597
- }
598
- | undefined;
599
- expect(approvalArgs).toMatchObject({
600
- method: "item/commandExecution/requestApproval",
601
- requestParams: {
602
- threadId: "side-thread",
603
- turnId: "turn-1",
604
- itemId: "cmd-side",
605
- command: "/bin/bash -lc 'node -v'",
606
- cwd: "/tmp/workspace",
607
- },
608
- threadId: "side-thread",
609
- turnId: "turn-1",
610
- paramsForRun: {
611
- messageChannel: "discord",
612
- messageProvider: "discord-voice",
613
- },
614
- });
615
- expect(approvalArgs?.nativeHookRelay).toMatchObject({
616
- relayId: relayIdDuringFork,
617
- allowedEvents: expect.arrayContaining(["pre_tool_use"]),
618
- });
619
- expect(
620
- nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayIdDuringFork!),
621
- ).toBeUndefined();
622
- });
623
-
624
- it("unregisters the native hook relay when side thread fork fails", async () => {
625
- const client = createFakeClient();
626
- let relayIdDuringFork: string | undefined;
627
- client.request.mockImplementation(async (method: string, requestParams: unknown) => {
628
- if (method === "thread/fork") {
629
- relayIdDuringFork = extractRelayIdFromThreadConfig(
630
- (requestParams as { config?: Record<string, unknown> }).config,
631
- );
632
- expect(
633
- nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayIdDuringFork),
634
- ).toBeDefined();
635
- throw new Error("fork failed");
636
- }
637
- throw new Error(`unexpected request: ${method}`);
638
- });
639
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
640
-
641
- await expect(
642
- runCodexAppServerSideQuestion(sideParams(), { nativeHookRelay: { enabled: true } }),
643
- ).rejects.toThrow("fork failed");
644
-
645
- expect(relayIdDuringFork).toBeDefined();
646
- expect(
647
- nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayIdDuringFork!),
648
- ).toBeUndefined();
649
- });
650
-
651
- it("includes permission request native hooks for side threads with yolo approval policy", async () => {
652
- readCodexAppServerBindingMock.mockResolvedValue({
653
- schemaVersion: 1,
654
- threadId: "parent-thread",
655
- sessionFile: "/tmp/session-1.jsonl",
656
- cwd: "/tmp/workspace",
657
- authProfileId: "openai-codex:work",
658
- model: "gpt-5.5",
659
- approvalPolicy: "never",
660
- sandbox: "workspace-write",
661
- createdAt: new Date(0).toISOString(),
662
- updatedAt: new Date(0).toISOString(),
663
- });
664
- const client = createFakeClient();
665
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
666
-
667
- await expect(
668
- runCodexAppServerSideQuestion(sideParams(), { nativeHookRelay: { enabled: true } }),
669
- ).resolves.toEqual({ text: "Side answer." });
670
-
671
- const forkParams = mockCall(client.request)[1] as Record<string, unknown> | undefined;
672
- const config = forkParams?.config as Record<string, unknown> | undefined;
673
- expect(forkParams?.approvalPolicy).toBe("never");
674
- expect(codexHookCommand(config, "hooks.PermissionRequest")?.command).toContain(
675
- "--event permission_request",
676
- );
677
- expect(codexHookCommand(config, "hooks.PreToolUse")?.command).toContain("--event pre_tool_use");
678
- });
679
-
680
- it("preserves explicitly configured side-thread native hook events", async () => {
681
- const client = createFakeClient();
682
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
683
-
684
- await expect(
685
- runCodexAppServerSideQuestion(sideParams(), {
686
- nativeHookRelay: { enabled: true, events: ["permission_request"] },
687
- }),
688
- ).resolves.toEqual({ text: "Side answer." });
689
-
690
- const forkParams = mockCall(client.request)[1] as Record<string, unknown> | undefined;
691
- const config = forkParams?.config as Record<string, unknown> | undefined;
692
- expect(codexHookCommand(config, "hooks.PermissionRequest")?.command).toContain(
693
- "--event permission_request",
694
- );
695
- expect(config?.["hooks.PreToolUse"]).toEqual([]);
696
- expect(config?.["hooks.PostToolUse"]).toEqual([]);
697
- expect(config?.["hooks.Stop"]).toEqual([]);
698
- const hookState = config?.["hooks.state"] as
699
- | Record<string, { enabled?: unknown; trusted_hash?: unknown }>
700
- | undefined;
701
- expect(codexHookStateForEvent(hookState, "permission_request")?.enabled).toBe(true);
702
- expect(codexHookStateForEvent(hookState, "pre_tool_use")).toEqual({ enabled: false });
703
- expect(codexHookStateForEvent(hookState, "post_tool_use")).toEqual({ enabled: false });
704
- expect(codexHookStateForEvent(hookState, "stop")).toEqual({ enabled: false });
705
- });
706
-
707
- it("sends clearing native hook config when side-thread relay is disabled", async () => {
708
- const client = createFakeClient();
709
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
710
-
711
- await expect(
712
- runCodexAppServerSideQuestion(sideParams(), { nativeHookRelay: { enabled: false } }),
713
- ).resolves.toEqual({ text: "Side answer." });
714
-
715
- const forkParams = mockCall(client.request)[1] as Record<string, unknown> | undefined;
716
- const config = forkParams?.config as Record<string, unknown> | undefined;
717
- expect(config).toMatchObject({
718
- "features.hooks": false,
719
- "features.code_mode": true,
720
- "features.code_mode_only": false,
721
- "hooks.PreToolUse": [],
722
- "hooks.PostToolUse": [],
723
- "hooks.PermissionRequest": [],
724
- "hooks.Stop": [],
725
- });
726
- expect(config).not.toHaveProperty("hooks.state");
727
- });
728
-
729
- it("passes Codex code-mode-only opt-in to side-thread forks", async () => {
730
- const client = createFakeClient();
731
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
732
-
733
- await expect(
734
- runCodexAppServerSideQuestion(sideParams(), {
735
- pluginConfig: { appServer: { codeModeOnly: true } },
736
- }),
737
- ).resolves.toEqual({ text: "Side answer." });
738
-
739
- const forkParams = mockCall(client.request)[1] as Record<string, unknown> | undefined;
740
- const config = forkParams?.config as Record<string, unknown> | undefined;
741
- expect(config?.["features.code_mode"]).toBe(true);
742
- expect(config?.["features.code_mode_only"]).toBe(true);
743
- });
744
-
745
- it("keeps native hook relays alive across side-thread startup and completion timeouts", async () => {
746
- const client = createFakeClient();
747
- const requestTimeoutMs = 400_000;
748
- const completionTimeoutMs = 700_000;
749
- const expectedRelayTtlMs = requestTimeoutMs * 3 + completionTimeoutMs + 5 * 60_000;
750
- let relayIdDuringFork: string | undefined;
751
- let startedAtMs = 0;
752
- client.request.mockImplementation(async (method: string, requestParams: unknown) => {
753
- if (method === "thread/fork") {
754
- relayIdDuringFork = extractRelayIdFromThreadConfig(
755
- (requestParams as { config?: Record<string, unknown> }).config,
756
- );
757
- const registration =
758
- nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayIdDuringFork);
759
- if (!registration) {
760
- throw new Error("Expected native hook relay registration");
761
- }
762
- expect(registration.expiresAtMs - startedAtMs).toBeGreaterThanOrEqual(expectedRelayTtlMs);
763
- expect(registration.expiresAtMs - startedAtMs).toBeLessThan(expectedRelayTtlMs + 10_000);
764
- return threadResult("side-thread");
765
- }
766
- if (method === "thread/inject_items") {
767
- return {};
768
- }
769
- if (method === "turn/start") {
770
- queueMicrotask(() => {
771
- client.emit(agentDelta("side-thread", "turn-1", "Side answer."));
772
- client.emit(turnCompleted("side-thread", "turn-1", "Side answer."));
773
- });
774
- return turnStartResult("turn-1");
775
- }
776
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
777
- return {};
778
- }
779
- throw new Error(`unexpected request: ${method}`);
780
- });
781
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
782
-
783
- startedAtMs = Date.now();
784
- await expect(
785
- runCodexAppServerSideQuestion(sideParams(), {
786
- pluginConfig: {
787
- appServer: {
788
- requestTimeoutMs,
789
- turnCompletionIdleTimeoutMs: completionTimeoutMs,
790
- },
791
- },
792
- nativeHookRelay: { enabled: true },
793
- }),
794
- ).resolves.toEqual({ text: "Side answer." });
795
-
796
- expect(relayIdDuringFork).toBeDefined();
797
- const registration = nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(
798
- relayIdDuringFork!,
799
- );
800
- expect(registration).toBeUndefined();
801
- const forkCall = mockCall(client.request);
802
- const forkOptions = forkCall[2] as { timeoutMs?: number } | undefined;
803
- expect(forkOptions?.timeoutMs).toBe(requestTimeoutMs);
804
- const config = (forkCall[1] as { config?: Record<string, unknown> }).config;
805
- const relayId = extractRelayIdFromThreadConfig(config);
806
- expect(relayId).toBe(relayIdDuringFork);
807
- });
808
-
809
- it("bridges side-thread dynamic tool requests to Klaw tools", async () => {
810
- const client = createFakeClient();
811
- let toolResponse: unknown;
812
- client.request.mockImplementation(async (method: string) => {
813
- if (method === "thread/fork") {
814
- return threadResult("side-thread");
815
- }
816
- if (method === "thread/inject_items") {
817
- return {};
818
- }
819
- if (method === "turn/start") {
820
- setTimeout(async () => {
821
- toolResponse = await client.handleRequest({
822
- id: 42,
823
- method: "item/tool/call",
824
- params: {
825
- threadId: "side-thread",
826
- turnId: "turn-1",
827
- callId: "tool-1",
828
- tool: "wiki_status",
829
- arguments: { topic: "AGENTS.md" },
830
- },
831
- });
832
- client.emit(agentDelta("side-thread", "turn-1", "Tool answer."));
833
- client.emit(turnCompleted("side-thread", "turn-1", "Tool answer."));
834
- }, 0);
835
- return turnStartResult("turn-1");
836
- }
837
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
838
- return {};
839
- }
840
- throw new Error(`unexpected request: ${method}`);
841
- });
842
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
843
-
844
- const result = await runCodexAppServerSideQuestion(sideParams());
845
-
846
- expect(result).toEqual({ text: "Tool answer." });
847
- const [toolCallId, toolArguments, toolSignal, toolOptions] = mockCall(toolExecuteMock);
848
- expect(toolExecuteMock).toHaveBeenCalledTimes(1);
849
- expect(toolCallId).toBe("tool-1");
850
- expect(toolArguments).toEqual({ topic: "AGENTS.md" });
851
- expect(toolSignal).toBeInstanceOf(AbortSignal);
852
- expect(toolOptions).toBeUndefined();
853
- expect(toolResponse).toEqual({
854
- success: true,
855
- contentItems: [{ type: "inputText", text: "tool output" }],
856
- });
857
- });
858
-
859
- it("clears side-thread dynamic tool diagnostics at the app-server request boundary", async () => {
860
- const client = createFakeClient();
861
- const diagnosticEvents: DiagnosticEventPayload[] = [];
862
- const unsubscribeDiagnostics = onInternalDiagnosticEvent((event) =>
863
- diagnosticEvents.push(event),
864
- );
865
- client.request.mockImplementation(async (method: string) => {
866
- if (method === "thread/fork") {
867
- return threadResult("side-thread");
868
- }
869
- if (method === "thread/inject_items") {
870
- return {};
871
- }
872
- if (method === "turn/start") {
873
- setTimeout(async () => {
874
- await client.handleRequest({
875
- id: 42,
876
- method: "item/tool/call",
877
- params: {
878
- threadId: "side-thread",
879
- turnId: "turn-1",
880
- callId: "tool-1",
881
- tool: "wiki_status",
882
- arguments: { topic: "AGENTS.md" },
883
- },
884
- });
885
- client.emit(agentDelta("side-thread", "turn-1", "Tool answer."));
886
- client.emit(turnCompleted("side-thread", "turn-1", "Tool answer."));
887
- }, 0);
888
- return turnStartResult("turn-1");
889
- }
890
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
891
- return {};
892
- }
893
- throw new Error(`unexpected request: ${method}`);
894
- });
895
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
896
-
897
- await runCodexAppServerSideQuestion(
898
- sideParams({
899
- opts: { runId: "run-side-diagnostics" },
900
- }),
901
- );
902
- await flushDiagnosticEvents();
903
- unsubscribeDiagnostics();
904
-
905
- const toolDiagnosticEvents = diagnosticEvents.filter(
906
- (
907
- event,
908
- ): event is Extract<
909
- DiagnosticEventPayload,
910
- { type: "tool.execution.started" | "tool.execution.completed" | "tool.execution.error" }
911
- > => event.type.startsWith("tool.execution."),
912
- );
913
- expect(
914
- toolDiagnosticEvents.map((event) => ({
915
- type: event.type,
916
- toolName: event.toolName,
917
- toolCallId: event.toolCallId,
918
- })),
919
- ).toEqual([
920
- {
921
- type: "tool.execution.started",
922
- toolName: "wiki_status",
923
- toolCallId: "tool-1",
924
- },
925
- {
926
- type: "tool.execution.completed",
927
- toolName: "wiki_status",
928
- toolCallId: "tool-1",
929
- },
930
- ]);
931
- expect(activeDiagnosticToolKeys(diagnosticEvents)).toEqual(new Set());
932
- });
933
-
934
- it("normalizes hook channel ids for side-thread dynamic tool requests", async () => {
935
- const beforeToolCall = vi.fn((...args: unknown[]) => {
936
- const context = args[1] as { channelId?: string };
937
- expect(context.channelId).toBe("voice-room");
938
- return undefined;
939
- });
940
- initializeGlobalHookRunner(
941
- createMockPluginRegistry([{ hookName: "before_tool_call", handler: beforeToolCall }]),
942
- );
943
- const client = createFakeClient();
944
- client.request.mockImplementation(async (method: string) => {
945
- if (method === "thread/fork") {
946
- return threadResult("side-thread");
947
- }
948
- if (method === "thread/inject_items") {
949
- return {};
950
- }
951
- if (method === "turn/start") {
952
- setTimeout(async () => {
953
- await client.handleRequest({
954
- id: 42,
955
- method: "item/tool/call",
956
- params: {
957
- threadId: "side-thread",
958
- turnId: "turn-1",
959
- callId: "tool-1",
960
- tool: "wiki_status",
961
- arguments: { topic: "AGENTS.md" },
962
- },
963
- });
964
- client.emit(agentDelta("side-thread", "turn-1", "Tool answer."));
965
- client.emit(turnCompleted("side-thread", "turn-1", "Tool answer."));
966
- }, 0);
967
- return turnStartResult("turn-1");
968
- }
969
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
970
- return {};
971
- }
972
- throw new Error(`unexpected request: ${method}`);
973
- });
974
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
975
-
976
- await expect(
977
- runCodexAppServerSideQuestion(
978
- sideParams({
979
- messageChannel: "discord",
980
- messageProvider: "discord-voice",
981
- currentChannelId: "discord:voice-room",
982
- }),
983
- ),
984
- ).resolves.toEqual({ text: "Tool answer." });
985
-
986
- expect(beforeToolCall).toHaveBeenCalledTimes(1);
987
- expect(createKlawCodingToolsMock).toHaveBeenCalledWith(
988
- expect.objectContaining({ hookChannelId: "voice-room" }),
989
- );
990
- expect(toolExecuteMock).toHaveBeenCalledTimes(1);
991
- });
992
-
993
- it("returns an empty response for side-thread user input requests", async () => {
994
- const client = createFakeClient();
995
- let unrelatedUserInputResponse: unknown;
996
- let userInputResponse: unknown;
997
- client.request.mockImplementation(async (method: string) => {
998
- if (method === "thread/fork") {
999
- return threadResult("side-thread");
1000
- }
1001
- if (method === "thread/inject_items") {
1002
- return {};
1003
- }
1004
- if (method === "turn/start") {
1005
- setTimeout(async () => {
1006
- unrelatedUserInputResponse = await client.handleRequest({
1007
- id: 42,
1008
- method: "item/tool/requestUserInput",
1009
- params: {
1010
- threadId: "parent-thread",
1011
- turnId: "parent-turn",
1012
- itemId: "input-parent",
1013
- questions: [],
1014
- },
1015
- });
1016
- userInputResponse = await client.handleRequest({
1017
- id: 43,
1018
- method: "item/tool/requestUserInput",
1019
- params: {
1020
- threadId: "side-thread",
1021
- turnId: "turn-1",
1022
- itemId: "input-1",
1023
- questions: [
1024
- {
1025
- id: "choice",
1026
- header: "Choice",
1027
- question: "Pick one",
1028
- options: [{ label: "A", description: "" }],
1029
- },
1030
- ],
1031
- },
1032
- });
1033
- client.emit(turnCompleted("side-thread", "turn-1", "No input needed."));
1034
- }, 0);
1035
- return turnStartResult("turn-1");
1036
- }
1037
- if (method === "thread/unsubscribe" || method === "turn/interrupt") {
1038
- return {};
1039
- }
1040
- throw new Error(`unexpected request: ${method}`);
1041
- });
1042
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
1043
-
1044
- const result = await runCodexAppServerSideQuestion(sideParams());
1045
-
1046
- expect(result).toEqual({ text: "No input needed." });
1047
- expect(unrelatedUserInputResponse).toBeUndefined();
1048
- expect(userInputResponse).toEqual({ answers: {} });
1049
- });
1050
-
1051
- it("uses configured image generation timeout for side-thread image_generate calls", () => {
1052
- const timeoutMs = testing.resolveSideDynamicToolCallTimeoutMs({
1053
- call: {
1054
- threadId: "side-thread",
1055
- turnId: "turn-1",
1056
- callId: "tool-1",
1057
- tool: "image_generate",
1058
- },
1059
- config: {
1060
- agents: {
1061
- defaults: {
1062
- imageGenerationModel: {
1063
- timeoutMs: 123_456,
1064
- },
1065
- },
1066
- },
1067
- } as never,
1068
- });
1069
-
1070
- expect(timeoutMs).toBe(123_456);
1071
- });
1072
-
1073
- it("cleans up notification handlers when side tool setup fails", async () => {
1074
- const client = createFakeClient();
1075
- createKlawCodingToolsMock.mockImplementation(() => {
1076
- throw new Error("tool setup failed");
1077
- });
1078
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
1079
-
1080
- await expect(runCodexAppServerSideQuestion(sideParams())).rejects.toThrow("tool setup failed");
1081
-
1082
- expect(client.notifications).toHaveLength(0);
1083
- expect(client.requests).toHaveLength(0);
1084
- });
1085
-
1086
- it("uses the app-server auth refresh request handler while the side thread is active", async () => {
1087
- const client = createFakeClient();
1088
- client.request.mockImplementation(async (method: string) => {
1089
- if (method === "thread/fork") {
1090
- await client.requests[0]?.({
1091
- id: 1,
1092
- method: "account/chatgptAuthTokens/refresh",
1093
- });
1094
- return threadResult("side-thread");
1095
- }
1096
- if (method === "thread/inject_items") {
1097
- return {};
1098
- }
1099
- if (method === "turn/start") {
1100
- queueMicrotask(() => client.emit(turnCompleted("side-thread", "turn-1", "Done.")));
1101
- return turnStartResult("turn-1");
1102
- }
1103
- return {};
1104
- });
1105
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
1106
-
1107
- await runCodexAppServerSideQuestion(sideParams());
1108
-
1109
- expect(refreshCodexAppServerAuthTokensMock).toHaveBeenCalledWith({
1110
- agentDir: "/tmp/agent",
1111
- authProfileId: "openai-codex:work",
1112
- config: {},
1113
- });
1114
- });
1115
-
1116
- it("returns a clear setup error when there is no Codex parent thread", async () => {
1117
- readCodexAppServerBindingMock.mockResolvedValue(undefined);
1118
-
1119
- await expect(runCodexAppServerSideQuestion(sideParams())).rejects.toThrow(
1120
- "Codex /btw needs an active Codex thread. Send a normal message first, then try /btw again.",
1121
- );
1122
- expect(getSharedCodexAppServerClientMock).not.toHaveBeenCalled();
1123
- });
1124
-
1125
- it("returns the same setup error when the persisted parent binding is stale", async () => {
1126
- const client = createFakeClient();
1127
- client.request.mockImplementation(async (method: string) => {
1128
- if (method === "thread/fork") {
1129
- throw new Error("thread/fork failed: no rollout found for thread id parent-thread");
1130
- }
1131
- return {};
1132
- });
1133
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
1134
-
1135
- await expect(runCodexAppServerSideQuestion(sideParams())).rejects.toThrow(
1136
- "Codex /btw needs an active Codex thread. Send a normal message first, then try /btw again.",
1137
- );
1138
- });
1139
-
1140
- it("interrupts and unsubscribes the ephemeral thread on abort", async () => {
1141
- const controller = new AbortController();
1142
- const client = createFakeClient();
1143
- client.request.mockImplementation(async (method: string) => {
1144
- if (method === "thread/fork") {
1145
- return threadResult("side-thread");
1146
- }
1147
- if (method === "thread/inject_items") {
1148
- return {};
1149
- }
1150
- if (method === "turn/start") {
1151
- queueMicrotask(() => controller.abort());
1152
- return turnStartResult("turn-1");
1153
- }
1154
- if (method === "turn/interrupt" || method === "thread/unsubscribe") {
1155
- return {};
1156
- }
1157
- throw new Error(`unexpected request: ${method}`);
1158
- });
1159
- getSharedCodexAppServerClientMock.mockResolvedValue(client);
1160
-
1161
- await expect(
1162
- runCodexAppServerSideQuestion(
1163
- sideParams({
1164
- opts: { abortSignal: controller.signal },
1165
- }),
1166
- ),
1167
- ).rejects.toThrow("Codex /btw was aborted.");
1168
- expect(client.request.mock.calls.filter(([method]) => method === "turn/interrupt")).toEqual([
1169
- ["turn/interrupt", { threadId: "side-thread", turnId: "turn-1" }, { timeoutMs: 60_000 }],
1170
- ]);
1171
- expect(client.request.mock.calls.filter(([method]) => method === "thread/unsubscribe")).toEqual(
1172
- [["thread/unsubscribe", { threadId: "side-thread" }, { timeoutMs: 60_000 }]],
1173
- );
1174
- });
1175
- });