@triflux/remote 10.1.0 → 10.25.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.
- package/hub/pipe.mjs +239 -121
- package/hub/public/dashboard.html +61 -0
- package/hub/server.mjs +2165 -708
- package/hub/store-adapter.mjs +59 -26
- package/hub/store.mjs +356 -153
- package/hub/team/agent-map.json +26 -7
- package/hub/team/ansi.mjs +245 -146
- package/hub/team/backend.mjs +84 -17
- package/hub/team/build-worker-prompt.mjs +42 -0
- package/hub/team/check-mcp-hub.mjs +49 -0
- package/hub/team/claude-daemon-control.mjs +219 -0
- package/hub/team/claude-native-bridge.mjs +784 -0
- package/hub/team/claude-session-projection.mjs +65 -0
- package/hub/team/cli/commands/attach.mjs +57 -37
- package/hub/team/cli/commands/control.mjs +29 -8
- package/hub/team/cli/commands/debug.mjs +95 -74
- package/hub/team/cli/commands/focus.mjs +80 -53
- package/hub/team/cli/commands/interrupt.mjs +18 -6
- package/hub/team/cli/commands/kill.mjs +16 -5
- package/hub/team/cli/commands/list.mjs +31 -24
- package/hub/team/cli/commands/send.mjs +19 -6
- package/hub/team/cli/commands/start/index.mjs +154 -33
- package/hub/team/cli/commands/start/parse-args.mjs +89 -12
- package/hub/team/cli/commands/start/start-headless.mjs +125 -37
- package/hub/team/cli/commands/start/start-in-process.mjs +50 -40
- package/hub/team/cli/commands/start/start-mux.mjs +122 -73
- package/hub/team/cli/commands/start/start-wt.mjs +86 -69
- package/hub/team/cli/commands/status.mjs +43 -14
- package/hub/team/cli/commands/stop.mjs +11 -4
- package/hub/team/cli/commands/task.mjs +8 -3
- package/hub/team/cli/commands/tasks.mjs +13 -13
- package/hub/team/cli/index.mjs +2 -2
- package/hub/team/cli/manifest.mjs +38 -8
- package/hub/team/cli/render.mjs +52 -30
- package/hub/team/cli/services/attach-fallback.mjs +74 -54
- package/hub/team/cli/services/hub-client.mjs +44 -16
- package/hub/team/cli/services/member-selector.mjs +37 -30
- package/hub/team/cli/services/native-control.mjs +144 -116
- package/hub/team/cli/services/runtime-mode.mjs +55 -1
- package/hub/team/cli/services/state-store.mjs +66 -9
- package/hub/team/cli/services/task-model.mjs +40 -30
- package/hub/team/codex-review.mjs +521 -0
- package/hub/team/conductor-mesh-bridge.mjs +25 -24
- package/hub/team/conductor-registry.mjs +121 -0
- package/hub/team/conductor.mjs +1379 -671
- package/hub/team/consensus-meta.mjs +176 -0
- package/hub/team/dashboard-anchor.mjs +4 -5
- package/hub/team/dashboard-layout.mjs +3 -1
- package/hub/team/dashboard-open.mjs +20 -118
- package/hub/team/dashboard.mjs +76 -28
- package/hub/team/event-log.mjs +18 -10
- package/hub/team/execution-mode.mjs +241 -0
- package/hub/team/extract-completion-payload.mjs +110 -0
- package/hub/team/git-preflight.mjs +286 -0
- package/hub/team/handoff.mjs +49 -18
- package/hub/team/headless-bridge-session.mjs +17 -0
- package/hub/team/headless.mjs +2065 -1156
- package/hub/team/health-probe.mjs +232 -69
- package/hub/team/launcher-template.mjs +23 -13
- package/hub/team/lead-control.mjs +91 -4
- package/hub/team/mcp-selector.mjs +145 -0
- package/hub/team/native-supervisor.mjs +90 -26
- package/hub/team/native.mjs +74 -35
- package/hub/team/nativeProxy.mjs +184 -113
- package/hub/team/notify.mjs +147 -78
- package/hub/team/orchestrator.mjs +173 -161
- package/hub/team/pane.mjs +43 -15
- package/hub/team/process-cleanup.mjs +69 -25
- package/hub/team/psmux.mjs +1020 -279
- package/hub/team/recovery-store.mjs +149 -0
- package/hub/team/remote-probe.mjs +68 -52
- package/hub/team/remote-session.mjs +131 -59
- package/hub/team/remote-watcher.mjs +61 -33
- package/hub/team/retry-state-machine.mjs +267 -0
- package/hub/team/routing.mjs +51 -25
- package/hub/team/runtime-strategy.mjs +200 -0
- package/hub/team/schemas/worker-completion.json +39 -0
- package/hub/team/sentinel-capture.mjs +127 -0
- package/hub/team/session-sync.mjs +153 -6
- package/hub/team/session.mjs +605 -611
- package/hub/team/shared.mjs +13 -13
- package/hub/team/staleState.mjs +72 -30
- package/hub/team/swarm-cli.mjs +390 -0
- package/hub/team/swarm-hypervisor.mjs +1522 -193
- package/hub/team/swarm-intent.mjs +216 -0
- package/hub/team/swarm-locks.mjs +140 -25
- package/hub/team/swarm-planner.mjs +121 -21
- package/hub/team/swarm-preflight.mjs +317 -0
- package/hub/team/swarm-reconciler.mjs +128 -23
- package/hub/team/synapse-cli.mjs +458 -0
- package/hub/team/synapse-http.mjs +60 -0
- package/hub/team/synapse-registry.mjs +329 -0
- package/hub/team/terminal-opener.mjs +216 -0
- package/hub/team/tui-core.mjs +288 -0
- package/hub/team/tui-lite.mjs +234 -146
- package/hub/team/tui-remote-adapter.mjs +18 -34
- package/hub/team/tui-synapse.mjs +223 -0
- package/hub/team/tui-viewer.mjs +104 -44
- package/hub/team/tui-widgets.mjs +262 -0
- package/hub/team/tui.mjs +927 -437
- package/hub/team/worker-completion-validator.mjs +84 -0
- package/hub/team/worker-sandbox.mjs +93 -0
- package/hub/team/worker-signal.mjs +263 -0
- package/hub/team/worker-signal.types.d.ts +52 -0
- package/hub/team/worktree-lifecycle.mjs +517 -63
- package/hub/team/wt-manager.mjs +297 -26
- package/hub/tools.mjs +474 -252
- package/hub/tray.mjs +411 -368
- package/hub/workers/claude-worker.mjs +162 -118
- package/hub/workers/codex-app-server-worker.mjs +1015 -0
- package/hub/workers/codex-mcp.mjs +724 -507
- package/hub/workers/delegator-mcp.mjs +662 -335
- package/hub/workers/factory.mjs +100 -21
- package/hub/workers/gemini-worker.mjs +179 -86
- package/hub/workers/interface.mjs +23 -2
- package/hub/workers/lib/jsonrpc-stdio.mjs +463 -0
- package/hub/workers/worker-utils.mjs +22 -16
- package/package.json +4 -3
- package/scripts/lib/agent-json.mjs +27 -0
- package/scripts/lib/async.mjs +174 -0
- package/scripts/lib/claudemd-scanner.mjs +223 -0
- package/scripts/lib/cli-agy.mjs +62 -0
- package/scripts/lib/cli-claude.mjs +78 -0
- package/scripts/lib/cli-codex.mjs +199 -0
- package/scripts/lib/cli-gemini.mjs +67 -0
- package/scripts/lib/codex-recovery.sh +50 -0
- package/scripts/lib/context.mjs +67 -0
- package/scripts/lib/cross-review-utils.mjs +59 -0
- package/scripts/lib/doctor-env-checks.mjs +121 -0
- package/scripts/lib/dynamic-route-cli.mjs +107 -0
- package/scripts/lib/env-probe.mjs +269 -0
- package/scripts/lib/env.mjs +14 -0
- package/scripts/lib/gemini-profiles.mjs +119 -0
- package/scripts/lib/handoff.mjs +187 -0
- package/scripts/lib/hook-utils.mjs +16 -0
- package/scripts/lib/hub.mjs +48 -0
- package/scripts/lib/keyword-rules.mjs +195 -0
- package/scripts/lib/logger.mjs +105 -0
- package/scripts/lib/mcp-filter.mjs +883 -0
- package/scripts/lib/mcp-gateway-servers.mjs +70 -0
- package/scripts/lib/mcp-guard-engine.mjs +2120 -0
- package/scripts/lib/mcp-health.mjs +773 -0
- package/scripts/lib/mcp-manifest.mjs +89 -0
- package/scripts/lib/mcp-server-catalog.mjs +355 -0
- package/scripts/lib/pid.mjs +63 -0
- package/scripts/lib/process-utils.mjs +26 -0
- package/scripts/lib/psmux-info.mjs +179 -0
- package/scripts/lib/quota.mjs +47 -0
- package/scripts/lib/remote-spawn-transfer.mjs +226 -0
- package/scripts/lib/skill-state.mjs +220 -0
- package/scripts/lib/skill-template.mjs +375 -0
- package/scripts/lib/team.mjs +75 -0
- package/scripts/lib/timeout.mjs +66 -0
- package/scripts/lib/tmp.mjs +26 -0
- package/scripts/lib/toml.mjs +113 -0
|
@@ -1,507 +1,724 @@
|
|
|
1
|
-
// hub/workers/codex-mcp.mjs — Codex MCP 서버 래퍼
|
|
2
|
-
import process from
|
|
3
|
-
import { fileURLToPath } from
|
|
4
|
-
|
|
5
|
-
import { Client } from
|
|
6
|
-
import { StdioClientTransport } from
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
10
|
-
CODEX_MCP_TRANSPORT_EXIT_CODE,
|
|
11
|
-
} from
|
|
12
|
-
import { withRetry } from
|
|
13
|
-
|
|
14
|
-
const REQUIRED_TOOLS = [
|
|
15
|
-
|
|
16
|
-
export { CODEX_MCP_EXECUTION_EXIT_CODE, CODEX_MCP_TRANSPORT_EXIT_CODE };
|
|
17
|
-
export const DEFAULT_CODEX_MCP_TIMEOUT_MS = 10 * 60 * 1000;
|
|
18
|
-
export const DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
* @param {
|
|
27
|
-
* @param {
|
|
28
|
-
* @param {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this.
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
transport
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
1
|
+
// hub/workers/codex-mcp.mjs — Codex MCP 서버 래퍼
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
6
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
10
|
+
CODEX_MCP_TRANSPORT_EXIT_CODE,
|
|
11
|
+
} from "@triflux/core/hub/cli-adapter-base.mjs";
|
|
12
|
+
import { withRetry } from "./worker-utils.mjs";
|
|
13
|
+
|
|
14
|
+
const REQUIRED_TOOLS = ["codex", "codex-reply"];
|
|
15
|
+
|
|
16
|
+
export { CODEX_MCP_EXECUTION_EXIT_CODE, CODEX_MCP_TRANSPORT_EXIT_CODE };
|
|
17
|
+
export const DEFAULT_CODEX_MCP_TIMEOUT_MS = 10 * 60 * 1000;
|
|
18
|
+
export const DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS = 120 * 1000;
|
|
19
|
+
export const DEFAULT_CODEX_MCP_SHUTDOWN_TIMEOUT_MS = 2_000;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Codex MCP transport/bootstrap 계층 오류
|
|
23
|
+
*/
|
|
24
|
+
export class CodexMcpTransportError extends Error {
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} message
|
|
27
|
+
* @param {object} [options]
|
|
28
|
+
* @param {unknown} [options.cause]
|
|
29
|
+
* @param {string} [options.stderr]
|
|
30
|
+
*/
|
|
31
|
+
constructor(message, options = {}) {
|
|
32
|
+
super(message, { cause: options.cause });
|
|
33
|
+
this.name = "CodexMcpTransportError";
|
|
34
|
+
this.stderr = options.stderr || "";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function cloneEnv(env = process.env) {
|
|
39
|
+
return Object.fromEntries(
|
|
40
|
+
Object.entries(env).filter(([, value]) => typeof value === "string"),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function closeWithin(label, closeFn, timeoutMs) {
|
|
45
|
+
let timer = null;
|
|
46
|
+
try {
|
|
47
|
+
const timeout = new Promise((resolve) => {
|
|
48
|
+
timer = setTimeout(() => {
|
|
49
|
+
resolve({ timedOut: true });
|
|
50
|
+
}, timeoutMs);
|
|
51
|
+
});
|
|
52
|
+
const closed = Promise.resolve()
|
|
53
|
+
.then(closeFn)
|
|
54
|
+
.then(() => ({ timedOut: false }));
|
|
55
|
+
const result = await Promise.race([closed, timeout]);
|
|
56
|
+
if (result.timedOut) {
|
|
57
|
+
console.error(
|
|
58
|
+
`[codex-mcp] WARNING: ${label} did not close within ${timeoutMs}ms; continuing shutdown.`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error(
|
|
64
|
+
`[codex-mcp] WARNING: ${label} close failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
65
|
+
);
|
|
66
|
+
return { timedOut: false };
|
|
67
|
+
} finally {
|
|
68
|
+
if (timer) clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function collectTextContent(content = []) {
|
|
73
|
+
return content
|
|
74
|
+
.filter((item) => item?.type === "text" && typeof item.text === "string")
|
|
75
|
+
.map((item) => item.text)
|
|
76
|
+
.join("\n")
|
|
77
|
+
.trim();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function normalizeStructuredContent(structuredContent, fallbackText = "") {
|
|
81
|
+
if (!structuredContent || typeof structuredContent !== "object") {
|
|
82
|
+
return { threadId: null, content: fallbackText };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const threadId =
|
|
86
|
+
typeof structuredContent.threadId === "string"
|
|
87
|
+
? structuredContent.threadId
|
|
88
|
+
: null;
|
|
89
|
+
const content =
|
|
90
|
+
typeof structuredContent.content === "string"
|
|
91
|
+
? structuredContent.content
|
|
92
|
+
: fallbackText;
|
|
93
|
+
|
|
94
|
+
return { threadId, content };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function buildCodexArguments(prompt, opts = {}) {
|
|
98
|
+
const args = { prompt };
|
|
99
|
+
|
|
100
|
+
if (typeof opts.cwd === "string" && opts.cwd) args.cwd = opts.cwd;
|
|
101
|
+
if (typeof opts.model === "string" && opts.model) args.model = opts.model;
|
|
102
|
+
if (typeof opts.profile === "string" && opts.profile)
|
|
103
|
+
args.profile = opts.profile;
|
|
104
|
+
if (typeof opts.approvalPolicy === "string" && opts.approvalPolicy) {
|
|
105
|
+
args["approval-policy"] = opts.approvalPolicy;
|
|
106
|
+
}
|
|
107
|
+
if (typeof opts.sandbox === "string" && opts.sandbox)
|
|
108
|
+
args.sandbox = opts.sandbox;
|
|
109
|
+
if (opts.config && typeof opts.config === "object") args.config = opts.config;
|
|
110
|
+
if (typeof opts.baseInstructions === "string" && opts.baseInstructions) {
|
|
111
|
+
args["base-instructions"] = opts.baseInstructions;
|
|
112
|
+
}
|
|
113
|
+
if (
|
|
114
|
+
typeof opts.developerInstructions === "string" &&
|
|
115
|
+
opts.developerInstructions
|
|
116
|
+
) {
|
|
117
|
+
args["developer-instructions"] = opts.developerInstructions;
|
|
118
|
+
}
|
|
119
|
+
if (typeof opts.compactPrompt === "string" && opts.compactPrompt) {
|
|
120
|
+
args["compact-prompt"] = opts.compactPrompt;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return args;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function pickToolName(threadId) {
|
|
127
|
+
return threadId ? "codex-reply" : "codex";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function withTimeout(promise, timeoutMs, message) {
|
|
131
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
132
|
+
return promise;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let timer;
|
|
136
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
137
|
+
timer = setTimeout(() => {
|
|
138
|
+
reject(new Error(message));
|
|
139
|
+
}, timeoutMs);
|
|
140
|
+
timer.unref?.();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return Promise.race([promise, timeoutPromise]).finally(() => {
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Known noise-only output patterns from codex MCP that indicate transport failure
|
|
149
|
+
// despite exitCode=0. Each entry is matched after trim() against the full output
|
|
150
|
+
// string. Length cap is applied separately to avoid masking real responses that
|
|
151
|
+
// happen to contain a noise substring as a header line.
|
|
152
|
+
const NOISE_ONLY_OUTPUT_PATTERNS = Object.freeze([
|
|
153
|
+
/^MCP issues detected\.\s*Run \/mcp list for status\.?$/i,
|
|
154
|
+
]);
|
|
155
|
+
const NOISE_ONLY_MAX_BYTES = 256;
|
|
156
|
+
|
|
157
|
+
function isNoiseOnlyOutput(text) {
|
|
158
|
+
if (typeof text !== "string") return false;
|
|
159
|
+
const trimmed = text.trim();
|
|
160
|
+
if (!trimmed || trimmed.length > NOISE_ONLY_MAX_BYTES) return false;
|
|
161
|
+
return NOISE_ONLY_OUTPUT_PATTERNS.some((pattern) => pattern.test(trimmed));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function watchBootstrapClose(transport) {
|
|
165
|
+
let active = true;
|
|
166
|
+
const promise = new Promise((_, reject) => {
|
|
167
|
+
const priorClose = transport.onclose;
|
|
168
|
+
transport.onclose = () => {
|
|
169
|
+
priorClose?.();
|
|
170
|
+
if (active) {
|
|
171
|
+
reject(new Error("Codex MCP transport closed during bootstrap"));
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const priorError = transport.onerror;
|
|
176
|
+
transport.onerror = (error) => {
|
|
177
|
+
priorError?.(error);
|
|
178
|
+
if (active) {
|
|
179
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
promise,
|
|
186
|
+
stop() {
|
|
187
|
+
active = false;
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function watchBootstrapProcessExit(transport) {
|
|
193
|
+
let active = true;
|
|
194
|
+
let timer = null;
|
|
195
|
+
let child = null;
|
|
196
|
+
|
|
197
|
+
const promise = new Promise((_, reject) => {
|
|
198
|
+
const rejectClosed = (code, signal) => {
|
|
199
|
+
if (!active) return;
|
|
200
|
+
const suffix = signal ? `signal ${signal}` : `exit ${code}`;
|
|
201
|
+
reject(
|
|
202
|
+
new Error(`Codex MCP transport closed during bootstrap (${suffix})`),
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
timer = setInterval(() => {
|
|
207
|
+
child = transport._process;
|
|
208
|
+
if (!child) return;
|
|
209
|
+
clearInterval(timer);
|
|
210
|
+
timer = null;
|
|
211
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
212
|
+
rejectClosed(child.exitCode, child.signalCode);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
child.once("close", rejectClosed);
|
|
216
|
+
}, 10);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
promise,
|
|
221
|
+
stop() {
|
|
222
|
+
active = false;
|
|
223
|
+
if (timer) clearInterval(timer);
|
|
224
|
+
child?.off?.("close", () => {});
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function normalizeRetryOptions(retryOptions) {
|
|
230
|
+
if (!retryOptions || typeof retryOptions !== "object") {
|
|
231
|
+
return Object.freeze({});
|
|
232
|
+
}
|
|
233
|
+
return Object.freeze({ ...retryOptions });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function isCodexRetryable(error) {
|
|
237
|
+
return (
|
|
238
|
+
error instanceof CodexMcpTransportError ||
|
|
239
|
+
error?.code === "ETIMEDOUT" ||
|
|
240
|
+
error?.cause?.code === "ETIMEDOUT"
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function detectWorkerCategory(error, fallbackCategory = "transient") {
|
|
245
|
+
const combined =
|
|
246
|
+
`${error?.message || ""}\n${error?.stderr || ""}`.toLowerCase();
|
|
247
|
+
|
|
248
|
+
if (error?.code === "INVALID_INPUT") return "input";
|
|
249
|
+
if (
|
|
250
|
+
/(unauthorized|forbidden|auth|login|token|credential|apikey|api key)/i.test(
|
|
251
|
+
combined,
|
|
252
|
+
)
|
|
253
|
+
) {
|
|
254
|
+
return "auth";
|
|
255
|
+
}
|
|
256
|
+
if (
|
|
257
|
+
/(config|unknown option|invalid option|missing|필수 mcp 도구 누락)/i.test(
|
|
258
|
+
combined,
|
|
259
|
+
)
|
|
260
|
+
) {
|
|
261
|
+
return "config";
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return fallbackCategory;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function buildCodexErrorInfo(error, attempts) {
|
|
268
|
+
const retryable = isCodexRetryable(error);
|
|
269
|
+
const code =
|
|
270
|
+
error instanceof CodexMcpTransportError
|
|
271
|
+
? "CODEX_TRANSPORT_ERROR"
|
|
272
|
+
: error?.code || "CODEX_EXECUTION_ERROR";
|
|
273
|
+
const category = detectWorkerCategory(
|
|
274
|
+
error,
|
|
275
|
+
retryable ? "transient" : "config",
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
let recovery =
|
|
279
|
+
"Review the Codex worker error output and retry after correcting the issue.";
|
|
280
|
+
if (code === "INVALID_INPUT") {
|
|
281
|
+
recovery = "Provide a non-empty prompt before invoking the Codex worker.";
|
|
282
|
+
} else if (retryable) {
|
|
283
|
+
recovery = "Retry after reconnecting the Codex MCP transport.";
|
|
284
|
+
} else if (category === "auth") {
|
|
285
|
+
recovery = "Refresh the Codex authentication state and retry.";
|
|
286
|
+
} else if (category === "config") {
|
|
287
|
+
recovery = "Check the Codex MCP configuration and available tools.";
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return Object.freeze({
|
|
291
|
+
code,
|
|
292
|
+
retryable,
|
|
293
|
+
attempts,
|
|
294
|
+
category,
|
|
295
|
+
recovery,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Codex MCP 워커
|
|
301
|
+
*/
|
|
302
|
+
export class CodexMcpWorker {
|
|
303
|
+
type = "codex";
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* @param {object} [options]
|
|
307
|
+
* @param {string} [options.command]
|
|
308
|
+
* @param {string[]} [options.args]
|
|
309
|
+
* @param {string} [options.cwd]
|
|
310
|
+
* @param {Record<string, string>} [options.env]
|
|
311
|
+
* @param {{ name: string, version: string }} [options.clientInfo]
|
|
312
|
+
* @param {number} [options.bootstrapTimeoutMs]
|
|
313
|
+
*/
|
|
314
|
+
constructor(options = {}) {
|
|
315
|
+
this.command = options.command || process.env.CODEX_BIN || "codex";
|
|
316
|
+
this.args =
|
|
317
|
+
Array.isArray(options.args) && options.args.length
|
|
318
|
+
? [...options.args]
|
|
319
|
+
: ["mcp-server"];
|
|
320
|
+
this.cwd = options.cwd || process.cwd();
|
|
321
|
+
this.env = cloneEnv({ ...cloneEnv(process.env), ...cloneEnv(options.env) });
|
|
322
|
+
this.clientInfo = options.clientInfo || {
|
|
323
|
+
name: "triflux-codex-mcp",
|
|
324
|
+
version: "1.0.0",
|
|
325
|
+
};
|
|
326
|
+
this.bootstrapTimeoutMs = Number.isFinite(options.bootstrapTimeoutMs)
|
|
327
|
+
? options.bootstrapTimeoutMs
|
|
328
|
+
: DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS;
|
|
329
|
+
this.retryOptions = normalizeRetryOptions(options.retryOptions);
|
|
330
|
+
|
|
331
|
+
this.client = null;
|
|
332
|
+
this.transport = null;
|
|
333
|
+
this.ready = false;
|
|
334
|
+
this.availableTools = new Set();
|
|
335
|
+
this.threadIds = new Map();
|
|
336
|
+
this.serverStderr = "";
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
isReady() {
|
|
340
|
+
return this.ready;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
getThreadId(sessionKey) {
|
|
344
|
+
return this.threadIds.get(sessionKey) || null;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
setThreadId(sessionKey, threadId) {
|
|
348
|
+
if (!sessionKey || !threadId) return;
|
|
349
|
+
this.threadIds.set(sessionKey, threadId);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
clearThread(sessionKey) {
|
|
353
|
+
if (!sessionKey) return;
|
|
354
|
+
this.threadIds.delete(sessionKey);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async start() {
|
|
358
|
+
if (this.ready && this.client && this.transport) return;
|
|
359
|
+
|
|
360
|
+
await this.stop();
|
|
361
|
+
|
|
362
|
+
const transport = new StdioClientTransport({
|
|
363
|
+
command: this.command,
|
|
364
|
+
args: this.args,
|
|
365
|
+
cwd: this.cwd,
|
|
366
|
+
env: this.env,
|
|
367
|
+
stderr: "pipe",
|
|
368
|
+
});
|
|
369
|
+
const client = new Client(this.clientInfo, { capabilities: {} });
|
|
370
|
+
|
|
371
|
+
this.serverStderr = "";
|
|
372
|
+
transport.stderr?.on("data", (chunk) => {
|
|
373
|
+
this.serverStderr += String(chunk);
|
|
374
|
+
if (this.serverStderr.length > 16000) {
|
|
375
|
+
this.serverStderr = this.serverStderr.slice(-16000);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
try {
|
|
380
|
+
const bootstrapClose = watchBootstrapClose(transport);
|
|
381
|
+
const bootstrapProcessExit = watchBootstrapProcessExit(transport);
|
|
382
|
+
await withTimeout(
|
|
383
|
+
Promise.race([
|
|
384
|
+
(async () => {
|
|
385
|
+
await client.connect(transport);
|
|
386
|
+
const tools = await client.listTools(undefined, {
|
|
387
|
+
timeout: this.bootstrapTimeoutMs,
|
|
388
|
+
});
|
|
389
|
+
this.availableTools = new Set(tools.tools.map((tool) => tool.name));
|
|
390
|
+
|
|
391
|
+
for (const requiredTool of REQUIRED_TOOLS) {
|
|
392
|
+
if (!this.availableTools.has(requiredTool)) {
|
|
393
|
+
throw new Error(`필수 MCP 도구 누락: ${requiredTool}`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
})(),
|
|
397
|
+
bootstrapClose.promise,
|
|
398
|
+
bootstrapProcessExit.promise,
|
|
399
|
+
]).finally(() => {
|
|
400
|
+
bootstrapClose.stop();
|
|
401
|
+
bootstrapProcessExit.stop();
|
|
402
|
+
}),
|
|
403
|
+
this.bootstrapTimeoutMs,
|
|
404
|
+
`Codex MCP bootstrap timeout (${this.bootstrapTimeoutMs}ms)`,
|
|
405
|
+
);
|
|
406
|
+
} catch (error) {
|
|
407
|
+
await client.close().catch(() => {});
|
|
408
|
+
transport.stderr?.destroy?.();
|
|
409
|
+
throw new CodexMcpTransportError(
|
|
410
|
+
`Codex MCP 연결 실패: ${error instanceof Error ? error.message : String(error)}`,
|
|
411
|
+
{ cause: error, stderr: this.serverStderr.trim() },
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
this.client = client;
|
|
416
|
+
this.transport = transport;
|
|
417
|
+
this.ready = true;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
async stop(options = {}) {
|
|
421
|
+
this.ready = false;
|
|
422
|
+
this.availableTools.clear();
|
|
423
|
+
|
|
424
|
+
const client = this.client;
|
|
425
|
+
const transport = this.transport;
|
|
426
|
+
this.transport = null;
|
|
427
|
+
this.client = null;
|
|
428
|
+
|
|
429
|
+
const shutdownTimeoutMs = Number.isFinite(options.shutdownTimeoutMs)
|
|
430
|
+
? options.shutdownTimeoutMs
|
|
431
|
+
: DEFAULT_CODEX_MCP_SHUTDOWN_TIMEOUT_MS;
|
|
432
|
+
|
|
433
|
+
let clientCloseTimedOut = false;
|
|
434
|
+
if (client) {
|
|
435
|
+
const result = await closeWithin(
|
|
436
|
+
"client.close",
|
|
437
|
+
() => client.close(),
|
|
438
|
+
shutdownTimeoutMs,
|
|
439
|
+
);
|
|
440
|
+
clientCloseTimedOut = Boolean(result.timedOut);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (transport && (!client || clientCloseTimedOut)) {
|
|
444
|
+
await closeWithin(
|
|
445
|
+
"transport.close",
|
|
446
|
+
() => transport.close(),
|
|
447
|
+
shutdownTimeoutMs,
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
transport?.stderr?.destroy?.();
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* @param {string} prompt
|
|
456
|
+
* @param {import('./interface.mjs').WorkerExecuteOptions} [opts]
|
|
457
|
+
* @returns {Promise<import('./interface.mjs').WorkerResult>}
|
|
458
|
+
*/
|
|
459
|
+
async execute(prompt, opts = {}) {
|
|
460
|
+
if (typeof prompt !== "string" || !prompt.trim()) {
|
|
461
|
+
return {
|
|
462
|
+
output: "prompt는 비어 있을 수 없습니다.",
|
|
463
|
+
exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
464
|
+
threadId: null,
|
|
465
|
+
sessionKey: opts.sessionKey || null,
|
|
466
|
+
error: buildCodexErrorInfo(
|
|
467
|
+
{ code: "INVALID_INPUT", message: "prompt는 비어 있을 수 없습니다." },
|
|
468
|
+
0,
|
|
469
|
+
),
|
|
470
|
+
raw: null,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const sessionKey =
|
|
475
|
+
typeof opts.sessionKey === "string" && opts.sessionKey
|
|
476
|
+
? opts.sessionKey
|
|
477
|
+
: null;
|
|
478
|
+
|
|
479
|
+
if (opts.resetSession && sessionKey) {
|
|
480
|
+
this.clearThread(sessionKey);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const threadId =
|
|
484
|
+
typeof opts.threadId === "string" && opts.threadId
|
|
485
|
+
? opts.threadId
|
|
486
|
+
: sessionKey
|
|
487
|
+
? this.getThreadId(sessionKey)
|
|
488
|
+
: null;
|
|
489
|
+
const timeoutMs = Number.isFinite(opts.timeoutMs)
|
|
490
|
+
? opts.timeoutMs
|
|
491
|
+
: DEFAULT_CODEX_MCP_TIMEOUT_MS;
|
|
492
|
+
let attempts = 0;
|
|
493
|
+
let activeThreadId = threadId;
|
|
494
|
+
|
|
495
|
+
try {
|
|
496
|
+
const { rawResult, normalized } = await withRetry(
|
|
497
|
+
async () => {
|
|
498
|
+
attempts += 1;
|
|
499
|
+
if (attempts === 1) {
|
|
500
|
+
await this.start();
|
|
501
|
+
} else {
|
|
502
|
+
await this.stop();
|
|
503
|
+
await this.start();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const toolName = pickToolName(activeThreadId);
|
|
507
|
+
const toolArguments =
|
|
508
|
+
toolName === "codex-reply"
|
|
509
|
+
? { prompt, threadId: activeThreadId }
|
|
510
|
+
: buildCodexArguments(prompt, opts);
|
|
511
|
+
|
|
512
|
+
const nextRawResult = await this.client.callTool(
|
|
513
|
+
{ name: toolName, arguments: toolArguments },
|
|
514
|
+
undefined,
|
|
515
|
+
{ timeout: timeoutMs },
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
const textContent = collectTextContent(nextRawResult.content);
|
|
519
|
+
const nextNormalized = normalizeStructuredContent(
|
|
520
|
+
nextRawResult.structuredContent,
|
|
521
|
+
textContent,
|
|
522
|
+
);
|
|
523
|
+
activeThreadId = nextNormalized.threadId || activeThreadId;
|
|
524
|
+
|
|
525
|
+
return { rawResult: nextRawResult, normalized: nextNormalized };
|
|
526
|
+
},
|
|
527
|
+
{
|
|
528
|
+
...this.retryOptions,
|
|
529
|
+
shouldRetry: (error) => isCodexRetryable(error),
|
|
530
|
+
},
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
if (sessionKey && normalized.threadId) {
|
|
534
|
+
this.setThreadId(sessionKey, normalized.threadId);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (rawResult.isError) {
|
|
538
|
+
return {
|
|
539
|
+
output: normalized.content,
|
|
540
|
+
exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
541
|
+
threadId: normalized.threadId,
|
|
542
|
+
sessionKey,
|
|
543
|
+
error: buildCodexErrorInfo(
|
|
544
|
+
{ code: "CODEX_TOOL_ERROR", message: normalized.content },
|
|
545
|
+
attempts,
|
|
546
|
+
),
|
|
547
|
+
raw: rawResult,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return {
|
|
552
|
+
output: normalized.content,
|
|
553
|
+
exitCode: 0,
|
|
554
|
+
threadId: normalized.threadId,
|
|
555
|
+
sessionKey,
|
|
556
|
+
raw: rawResult,
|
|
557
|
+
};
|
|
558
|
+
} catch (error) {
|
|
559
|
+
await this.stop().catch(() => {});
|
|
560
|
+
return {
|
|
561
|
+
output: error instanceof Error ? error.message : String(error),
|
|
562
|
+
exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
563
|
+
threadId: activeThreadId,
|
|
564
|
+
sessionKey,
|
|
565
|
+
error: buildCodexErrorInfo(error, attempts || 1),
|
|
566
|
+
raw: null,
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
export function createCodexMcpWorker(options = {}) {
|
|
573
|
+
return new CodexMcpWorker(options);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function parseCliArgs(argv) {
|
|
577
|
+
const options = {
|
|
578
|
+
command: process.env.CODEX_BIN || "codex",
|
|
579
|
+
cwd: process.cwd(),
|
|
580
|
+
timeoutMs: DEFAULT_CODEX_MCP_TIMEOUT_MS,
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
584
|
+
const token = argv[i];
|
|
585
|
+
const next = () => {
|
|
586
|
+
const value = argv[i + 1];
|
|
587
|
+
if (value === undefined) {
|
|
588
|
+
throw new Error(`${token} 값이 필요합니다.`);
|
|
589
|
+
}
|
|
590
|
+
i += 1;
|
|
591
|
+
return value;
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
switch (token) {
|
|
595
|
+
case "--prompt":
|
|
596
|
+
options.prompt = next();
|
|
597
|
+
break;
|
|
598
|
+
case "--thread-id":
|
|
599
|
+
options.threadId = next();
|
|
600
|
+
break;
|
|
601
|
+
case "--session-key":
|
|
602
|
+
options.sessionKey = next();
|
|
603
|
+
break;
|
|
604
|
+
case "--cwd":
|
|
605
|
+
options.cwd = next();
|
|
606
|
+
break;
|
|
607
|
+
case "--profile":
|
|
608
|
+
options.profile = next();
|
|
609
|
+
break;
|
|
610
|
+
case "--model":
|
|
611
|
+
options.model = next();
|
|
612
|
+
break;
|
|
613
|
+
case "--approval-policy":
|
|
614
|
+
options.approvalPolicy = next();
|
|
615
|
+
break;
|
|
616
|
+
case "--sandbox":
|
|
617
|
+
options.sandbox = next();
|
|
618
|
+
break;
|
|
619
|
+
case "--base-instructions":
|
|
620
|
+
options.baseInstructions = next();
|
|
621
|
+
break;
|
|
622
|
+
case "--developer-instructions":
|
|
623
|
+
options.developerInstructions = next();
|
|
624
|
+
break;
|
|
625
|
+
case "--compact-prompt":
|
|
626
|
+
options.compactPrompt = next();
|
|
627
|
+
break;
|
|
628
|
+
case "--timeout-ms":
|
|
629
|
+
options.timeoutMs = Number.parseInt(next(), 10);
|
|
630
|
+
break;
|
|
631
|
+
case "--config-json":
|
|
632
|
+
options.config = JSON.parse(next());
|
|
633
|
+
break;
|
|
634
|
+
case "--codex-command":
|
|
635
|
+
options.command = next();
|
|
636
|
+
break;
|
|
637
|
+
case "--reset-session":
|
|
638
|
+
options.resetSession = true;
|
|
639
|
+
break;
|
|
640
|
+
default:
|
|
641
|
+
throw new Error(`알 수 없는 옵션: ${token}`);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (typeof options.prompt !== "string" || !options.prompt) {
|
|
646
|
+
throw new Error("--prompt는 필수입니다.");
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return options;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
export async function runCodexMcpCli(argv = process.argv.slice(2)) {
|
|
653
|
+
let options;
|
|
654
|
+
try {
|
|
655
|
+
options = parseCliArgs(argv);
|
|
656
|
+
} catch (error) {
|
|
657
|
+
console.error(
|
|
658
|
+
`[codex-mcp] ${error instanceof Error ? error.message : String(error)}`,
|
|
659
|
+
);
|
|
660
|
+
process.exitCode = 64;
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const worker = new CodexMcpWorker({
|
|
665
|
+
command: options.command,
|
|
666
|
+
cwd: options.cwd,
|
|
667
|
+
retryOptions: { maxAttempts: 1 },
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
try {
|
|
671
|
+
const result = await worker.execute(options.prompt, options);
|
|
672
|
+
const hasOutput =
|
|
673
|
+
typeof result.output === "string" && result.output.trim().length > 0;
|
|
674
|
+
const noiseOnly = hasOutput && isNoiseOnlyOutput(result.output);
|
|
675
|
+
if (hasOutput) {
|
|
676
|
+
process.stdout.write(result.output);
|
|
677
|
+
if (!result.output.endsWith("\n")) {
|
|
678
|
+
process.stdout.write("\n");
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
// Silent-success regression guard (codex 0.124.0+):
|
|
682
|
+
// codex MCP can return exitCode=0 with empty/whitespace `output` when the
|
|
683
|
+
// result-extraction path fails to read the assistant content from the MCP
|
|
684
|
+
// response. Without this guard, tfx-route.sh sees STDOUT_LOG=0 bytes and
|
|
685
|
+
// treats it as success — caller gets nothing back.
|
|
686
|
+
// Promote to a transport failure so the wrapper falls back to `codex exec`.
|
|
687
|
+
//
|
|
688
|
+
// Noise-only guard (codex 0.125.0+, P2 swarm signature):
|
|
689
|
+
// codex MCP can also return exitCode=0 with a short noise-only message like
|
|
690
|
+
// "MCP issues detected. Run /mcp list for status." (~48 bytes) instead of
|
|
691
|
+
// an actual response payload. trim().length > 0 passes the empty-output
|
|
692
|
+
// check, so the previous guard misses this case. Pattern-match known noise
|
|
693
|
+
// strings under a small length cap and promote to transport failure too.
|
|
694
|
+
if (result.error?.code === "CODEX_TRANSPORT_ERROR") {
|
|
695
|
+
process.exitCode = CODEX_MCP_TRANSPORT_EXIT_CODE;
|
|
696
|
+
} else if (!hasOutput && result.exitCode === 0) {
|
|
697
|
+
console.error(
|
|
698
|
+
"[codex-mcp] WARNING: empty output with exit 0 — treating as transport failure (codex 0.124.0 silent-flush regression). Wrapper will fall back to exec path.",
|
|
699
|
+
);
|
|
700
|
+
process.exitCode = CODEX_MCP_TRANSPORT_EXIT_CODE;
|
|
701
|
+
} else if (noiseOnly && result.exitCode === 0) {
|
|
702
|
+
console.error(
|
|
703
|
+
"[codex-mcp] WARNING: noise-only output (codex MCP error string without payload) with exit 0 — treating as transport failure. Wrapper will fall back to exec path.",
|
|
704
|
+
);
|
|
705
|
+
process.exitCode = CODEX_MCP_TRANSPORT_EXIT_CODE;
|
|
706
|
+
} else {
|
|
707
|
+
process.exitCode = result.exitCode;
|
|
708
|
+
}
|
|
709
|
+
} catch (error) {
|
|
710
|
+
const lines = [error instanceof Error ? error.message : String(error)];
|
|
711
|
+
if (error instanceof CodexMcpTransportError && error.stderr) {
|
|
712
|
+
lines.push(error.stderr);
|
|
713
|
+
}
|
|
714
|
+
console.error(`[codex-mcp] ${lines.join("\n")}`);
|
|
715
|
+
process.exitCode = CODEX_MCP_TRANSPORT_EXIT_CODE;
|
|
716
|
+
} finally {
|
|
717
|
+
await worker.stop();
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
722
|
+
await runCodexMcpCli();
|
|
723
|
+
process.exit(process.exitCode ?? 0);
|
|
724
|
+
}
|