triflux 7.1.3 → 7.2.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 (73) hide show
  1. package/.claude-plugin/marketplace.json +31 -31
  2. package/.claude-plugin/plugin.json +22 -23
  3. package/bin/triflux.mjs +18 -5
  4. package/hooks/keyword-rules.json +393 -361
  5. package/hub/bridge.mjs +799 -786
  6. package/hub/delegator/contracts.mjs +37 -38
  7. package/hub/delegator/schema/delegator-tools.schema.json +250 -250
  8. package/hub/delegator/service.mjs +307 -302
  9. package/hub/intent.mjs +108 -11
  10. package/hub/lib/process-utils.mjs +20 -0
  11. package/hub/pipe.mjs +589 -589
  12. package/hub/pipeline/gates/confidence.mjs +1 -1
  13. package/hub/pipeline/gates/selfcheck.mjs +2 -4
  14. package/hub/pipeline/state.mjs +191 -187
  15. package/hub/pipeline/transitions.mjs +124 -120
  16. package/hub/public/dashboard.html +355 -349
  17. package/hub/quality/deslop.mjs +5 -3
  18. package/hub/reflexion.mjs +5 -1
  19. package/hub/research.mjs +6 -1
  20. package/hub/router.mjs +791 -782
  21. package/hub/server.mjs +893 -822
  22. package/hub/store.mjs +807 -778
  23. package/hub/team/agent-map.json +10 -0
  24. package/hub/team/ansi.mjs +3 -4
  25. package/hub/team/cli/commands/control.mjs +43 -43
  26. package/hub/team/cli/commands/interrupt.mjs +36 -36
  27. package/hub/team/cli/commands/kill.mjs +3 -3
  28. package/hub/team/cli/commands/send.mjs +37 -37
  29. package/hub/team/cli/commands/start/index.mjs +18 -8
  30. package/hub/team/cli/commands/start/parse-args.mjs +3 -1
  31. package/hub/team/cli/commands/start/start-headless.mjs +4 -1
  32. package/hub/team/cli/commands/status.mjs +87 -87
  33. package/hub/team/cli/commands/stop.mjs +1 -1
  34. package/hub/team/cli/commands/task.mjs +1 -1
  35. package/hub/team/cli/index.mjs +41 -39
  36. package/hub/team/cli/manifest.mjs +29 -28
  37. package/hub/team/cli/services/hub-client.mjs +37 -0
  38. package/hub/team/cli/services/state-store.mjs +26 -12
  39. package/hub/team/dashboard.mjs +11 -4
  40. package/hub/team/handoff.mjs +12 -0
  41. package/hub/team/headless.mjs +202 -200
  42. package/hub/team/native-supervisor.mjs +386 -346
  43. package/hub/team/nativeProxy.mjs +680 -692
  44. package/hub/team/staleState.mjs +361 -369
  45. package/hub/team/tui-viewer.mjs +27 -3
  46. package/hub/team/tui.mjs +1 -0
  47. package/hub/token-mode.mjs +114 -24
  48. package/hub/workers/delegator-mcp.mjs +1059 -1057
  49. package/hud/colors.mjs +88 -0
  50. package/hud/constants.mjs +78 -0
  51. package/hud/hud-qos-status.mjs +206 -1872
  52. package/hud/providers/claude.mjs +309 -0
  53. package/hud/providers/codex.mjs +151 -0
  54. package/hud/providers/gemini.mjs +320 -0
  55. package/hud/renderers.mjs +424 -0
  56. package/hud/terminal.mjs +140 -0
  57. package/hud/utils.mjs +271 -0
  58. package/package.json +1 -2
  59. package/scripts/__tests__/keyword-detector.test.mjs +234 -234
  60. package/scripts/headless-guard-fast.sh +21 -0
  61. package/scripts/headless-guard.mjs +26 -6
  62. package/scripts/lib/keyword-rules.mjs +166 -168
  63. package/scripts/setup.mjs +720 -690
  64. package/scripts/tfx-route-post.mjs +424 -424
  65. package/scripts/tfx-route.sh +1663 -1650
  66. package/scripts/tmp-cleanup.mjs +74 -0
  67. package/skills/tfx-auto/SKILL.md +279 -278
  68. package/skills/tfx-auto-codex/SKILL.md +98 -77
  69. package/skills/tfx-codex/SKILL.md +65 -65
  70. package/skills/tfx-gemini/SKILL.md +83 -82
  71. package/skills/tfx-hub/SKILL.md +205 -136
  72. package/skills/tfx-multi/SKILL.md +11 -5
  73. package/.mcp.json +0 -8
package/hub/pipe.mjs CHANGED
@@ -1,589 +1,589 @@
1
- // hub/pipe.mjs — Named Pipe/Unix socket 제어 채널
2
- // NDJSON 프로토콜로 에이전트 실시간 제어/이벤트 푸시를 처리한다.
3
-
4
- import net from 'node:net';
5
- import { existsSync, unlinkSync } from 'node:fs';
6
- import { join } from 'node:path';
7
- import { randomUUID } from 'node:crypto';
8
- import {
9
- teamInfo,
10
- teamTaskList,
11
- teamTaskUpdate,
12
- teamSendMessage,
13
- } from './team/nativeProxy.mjs';
14
- import { createPipeline } from './pipeline/index.mjs';
15
- import {
16
- ensurePipelineTable,
17
- initPipelineState,
18
- listPipelineStates,
19
- readPipelineState,
20
- } from './pipeline/state.mjs';
21
-
22
- const DEFAULT_HEARTBEAT_TTL_MS = 60000;
23
-
24
- /** 플랫폼별 pipe 경로 계산 */
25
- export function getPipePath(sessionId = process.pid) {
26
- if (process.platform === 'win32') {
27
- return `\\\\.\\pipe\\triflux-${sessionId}`;
28
- }
29
- return join('/tmp', `triflux-${sessionId}.sock`);
30
- }
31
-
32
- function safeJsonParse(line) {
33
- try {
34
- return JSON.parse(line);
35
- } catch {
36
- return null;
37
- }
38
- }
39
-
40
- function normalizeTopics(topics) {
41
- if (!Array.isArray(topics)) return [];
42
- return topics
43
- .map((topic) => String(topic || '').trim())
44
- .filter(Boolean);
45
- }
46
-
47
- /**
48
- * Named Pipe 서버 생성
49
- * @param {object} opts
50
- * @param {object} opts.router
51
- * @param {object} [opts.store]
52
- * @param {string|number} [opts.sessionId]
53
- * @param {number} [opts.heartbeatTtlMs]
54
- */
55
- export function createPipeServer({
56
- router,
57
- store = null,
58
- sessionId = process.pid,
59
- heartbeatTtlMs = DEFAULT_HEARTBEAT_TTL_MS,
60
- delegatorService = null,
61
- } = {}) {
62
- if (!router) {
63
- throw new Error('router is required');
64
- }
65
-
66
- const pipePath = getPipePath(sessionId);
67
- const clients = new Map();
68
- let server = null;
69
- let heartbeatTimer = null;
70
-
71
- function sendFrame(client, frame) {
72
- if (!client || client.closed || !client.socket.writable) return false;
73
- try {
74
- client.socket.write(`${JSON.stringify(frame)}\n`);
75
- return true;
76
- } catch {
77
- return false;
78
- }
79
- }
80
-
81
- function sendResponse(client, requestId, result) {
82
- return sendFrame(client, { type: 'response', request_id: requestId, ...result });
83
- }
84
-
85
- function closeClient(client) {
86
- if (!client || client.closed) return;
87
- client.closed = true;
88
- clients.delete(client.id);
89
- try { client.socket.destroy(); } catch {}
90
- }
91
-
92
- function touchClient(client) {
93
- client.lastHeartbeatMs = Date.now();
94
- }
95
-
96
- function resolveAgentId(client, payload) {
97
- const agentId = payload?.agent_id || client?.agentId;
98
- if (!agentId) {
99
- throw new Error('agent_id required');
100
- }
101
- return agentId;
102
- }
103
-
104
- function pushEvent(agentId, message) {
105
- let delivered = false;
106
- for (const client of clients.values()) {
107
- if (client.agentId !== agentId) continue;
108
- if (sendFrame(client, {
109
- type: 'event',
110
- event: 'message',
111
- payload: { agent_id: agentId, message },
112
- })) {
113
- delivered = true;
114
- }
115
- }
116
- return delivered;
117
- }
118
-
119
- function pushPendingMessages(agentId) {
120
- if (!agentId) return 0;
121
- const pending = router.getPendingMessages(agentId, { max_messages: 100 });
122
- let pushed = 0;
123
- for (const message of pending) {
124
- if (router.markMessagePushed(agentId, message.id)) {
125
- pushed += pushEvent(agentId, message) ? 1 : 0;
126
- } else if (pushEvent(agentId, message)) {
127
- pushed += 1;
128
- }
129
- }
130
- return pushed;
131
- }
132
-
133
- async function processCommand(client, action, payload = {}) {
134
- switch (action) {
135
- case 'register': {
136
- const result = router.registerAgent(payload);
137
- if (client) {
138
- client.agentId = payload.agent_id;
139
- client.subscriptions = new Set(router.getSubscribedTopics(client.agentId));
140
- touchClient(client);
141
- pushPendingMessages(client.agentId);
142
- }
143
- return { ok: true, data: { ...result, pipe_path: pipePath } };
144
- }
145
-
146
- case 'subscribe': {
147
- const agentId = resolveAgentId(client, payload);
148
- const topics = normalizeTopics(payload.topics);
149
- const result = router.subscribeAgent(agentId, topics, {
150
- replace: Boolean(payload.replace),
151
- });
152
- if (client) {
153
- client.agentId = agentId;
154
- client.subscriptions = new Set(result.topics);
155
- touchClient(client);
156
- }
157
- const replayed = pushPendingMessages(agentId);
158
- return {
159
- ok: true,
160
- data: { ...result, replayed_messages: replayed },
161
- };
162
- }
163
-
164
- case 'ack': {
165
- const agentId = resolveAgentId(client, payload);
166
- const acked = router.ackMessages(payload.message_ids || payload.ack_ids || [], agentId);
167
- if (client) touchClient(client);
168
- return { ok: true, data: { agent_id: agentId, acked_count: acked } };
169
- }
170
-
171
- case 'heartbeat': {
172
- const agentId = resolveAgentId(client, payload);
173
- const result = router.refreshAgentLease(agentId, payload.heartbeat_ttl_ms || heartbeatTtlMs);
174
- if (client) touchClient(client);
175
- return { ok: true, data: result };
176
- }
177
-
178
- case 'publish': {
179
- const result = router.handlePublish(payload);
180
- if (client) touchClient(client);
181
- return result;
182
- }
183
-
184
- case 'handoff': {
185
- const result = router.handleHandoff(payload);
186
- if (client) touchClient(client);
187
- return result;
188
- }
189
-
190
- case 'assign': {
191
- const result = router.assignAsync(payload);
192
- if (client) touchClient(client);
193
- return result;
194
- }
195
-
196
- case 'assign_result': {
197
- const result = router.reportAssignResult(payload);
198
- if (client) touchClient(client);
199
- return result;
200
- }
201
-
202
- case 'assign_retry': {
203
- const result = router.retryAssign(payload.job_id, payload);
204
- if (client) touchClient(client);
205
- return result;
206
- }
207
-
208
- case 'result': {
209
- const result = router.handlePublish({
210
- from: payload.agent_id,
211
- to: `topic:${payload.topic || 'task.result'}`,
212
- topic: payload.topic || 'task.result',
213
- payload: payload.payload || {},
214
- priority: 5,
215
- ttl_ms: 3600000,
216
- trace_id: payload.trace_id,
217
- correlation_id: payload.correlation_id,
218
- });
219
- if (client) touchClient(client);
220
- return result;
221
- }
222
-
223
- case 'control': {
224
- const result = router.handlePublish({
225
- from: payload.from_agent || 'lead',
226
- to: payload.to_agent,
227
- topic: 'lead.control',
228
- payload: {
229
- command: payload.command,
230
- reason: payload.reason || '',
231
- ...(payload.payload || {}),
232
- issued_at: Date.now(),
233
- },
234
- priority: 8,
235
- ttl_ms: Math.max(1000, Math.min(Number(payload.ttl_ms) || 3600000, 86400000)),
236
- trace_id: payload.trace_id,
237
- correlation_id: payload.correlation_id,
238
- });
239
- if (client) touchClient(client);
240
- return result;
241
- }
242
-
243
- case 'deregister': {
244
- const agentId = resolveAgentId(client, payload);
245
- router.updateAgentStatus(agentId, 'offline');
246
- if (client) touchClient(client);
247
- return {
248
- ok: true,
249
- data: { agent_id: agentId, status: 'offline' },
250
- };
251
- }
252
-
253
- case 'team_task_update': {
254
- const result = await teamTaskUpdate(payload);
255
- if (client) touchClient(client);
256
- return result;
257
- }
258
-
259
- case 'team_send_message': {
260
- const result = await teamSendMessage(payload);
261
- if (client) touchClient(client);
262
- return result;
263
- }
264
-
265
- case 'pipeline_advance': {
266
- if (client) touchClient(client);
267
- if (!store?.db) {
268
- return { ok: false, error: 'hub_db_not_found' };
269
- }
270
- ensurePipelineTable(store.db);
271
- const pipeline = createPipeline(store.db, payload.team_name);
272
- return pipeline.advance(payload.phase);
273
- }
274
-
275
- case 'pipeline_init': {
276
- if (client) touchClient(client);
277
- if (!store?.db) {
278
- return { ok: false, error: 'hub_db_not_found' };
279
- }
280
- ensurePipelineTable(store.db);
281
- const state = initPipelineState(store.db, payload.team_name, {
282
- fix_max: payload.fix_max,
283
- ralph_max: payload.ralph_max,
284
- });
285
- return { ok: true, data: state };
286
- }
287
-
288
- case 'delegator_delegate': {
289
- if (!delegatorService) {
290
- return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
291
- }
292
- if (client) touchClient(client);
293
- const result = await delegatorService.delegate(payload);
294
- return { ok: result?.ok !== false, data: result };
295
- }
296
-
297
- case 'delegator_reply': {
298
- if (!delegatorService) {
299
- return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
300
- }
301
- if (client) touchClient(client);
302
- const result = await delegatorService.reply(payload);
303
- return { ok: result?.ok !== false, data: result };
304
- }
305
-
306
- default:
307
- return {
308
- ok: false,
309
- error: { code: 'UNKNOWN_PIPE_COMMAND', message: `지원하지 않는 command: ${action}` },
310
- };
311
- }
312
- }
313
-
314
- function buildReplayMessages(agentId, payload = {}) {
315
- const maxMessages = Math.max(1, Math.min(Number(payload.max_messages) || 20, 100));
316
- const pending = router.getPendingMessages(agentId, {
317
- max_messages: maxMessages,
318
- include_topics: payload.topics,
319
- });
320
- if (!store?.getAuditMessagesForAgent) {
321
- return pending.slice(0, maxMessages);
322
- }
323
-
324
- const audit = store.getAuditMessagesForAgent(agentId, {
325
- max_messages: maxMessages,
326
- include_topics: payload.topics,
327
- });
328
- const byId = new Map();
329
- for (const message of [...pending, ...audit]) {
330
- if (!message?.id || byId.has(message.id)) continue;
331
- byId.set(message.id, message);
332
- }
333
- return Array.from(byId.values())
334
- .sort((left, right) => right.created_at_ms - left.created_at_ms)
335
- .slice(0, maxMessages);
336
- }
337
-
338
- async function processQuery(client, action, payload = {}) {
339
- switch (action) {
340
- case 'drain': {
341
- const agentId = resolveAgentId(client, payload);
342
- const messages = router.drainAgent(agentId, {
343
- max_messages: payload.max_messages,
344
- include_topics: payload.topics,
345
- auto_ack: payload.auto_ack,
346
- });
347
- if (client) touchClient(client);
348
- return {
349
- ok: true,
350
- data: { messages, count: messages.length, server_time_ms: Date.now() },
351
- };
352
- }
353
-
354
- case 'context': {
355
- const agentId = resolveAgentId(client, payload);
356
- const messages = buildReplayMessages(agentId, payload);
357
- if (client) touchClient(client);
358
- return {
359
- ok: true,
360
- data: { messages, count: messages.length, server_time_ms: Date.now() },
361
- };
362
- }
363
-
364
- case 'status': {
365
- const scope = payload.scope || 'hub';
366
- if (client) touchClient(client);
367
- return router.getStatus(scope, payload);
368
- }
369
-
370
- case 'assign_status': {
371
- if (client) touchClient(client);
372
- return router.getAssignStatus(payload);
373
- }
374
-
375
- case 'team_info': {
376
- const result = await teamInfo(payload);
377
- if (client) touchClient(client);
378
- return result;
379
- }
380
-
381
- case 'team_task_list': {
382
- const result = await teamTaskList(payload);
383
- if (client) touchClient(client);
384
- return result;
385
- }
386
-
387
- case 'pipeline_state': {
388
- if (client) touchClient(client);
389
- if (!store?.db) {
390
- return { ok: false, error: 'hub_db_not_found' };
391
- }
392
- ensurePipelineTable(store.db);
393
- const state = readPipelineState(store.db, payload.team_name);
394
- return state
395
- ? { ok: true, data: state }
396
- : { ok: false, error: 'pipeline_not_found' };
397
- }
398
-
399
- case 'pipeline_list': {
400
- if (client) touchClient(client);
401
- if (!store?.db) {
402
- return { ok: false, error: 'hub_db_not_found' };
403
- }
404
- ensurePipelineTable(store.db);
405
- return { ok: true, data: listPipelineStates(store.db) };
406
- }
407
-
408
- case 'delegator_status': {
409
- if (!delegatorService) {
410
- return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
411
- }
412
- if (client) touchClient(client);
413
- const result = await delegatorService.status(payload);
414
- return { ok: result?.ok !== false, data: result };
415
- }
416
-
417
- default:
418
- return {
419
- ok: false,
420
- error: { code: 'UNKNOWN_PIPE_QUERY', message: `지원하지 않는 query: ${action}` },
421
- };
422
- }
423
- }
424
-
425
- function onMessage(agentId, message) {
426
- if (!agentId || !message) return;
427
- if (router.markMessagePushed(agentId, message.id)) {
428
- pushEvent(agentId, message);
429
- return;
430
- }
431
- pushEvent(agentId, message);
432
- }
433
-
434
- async function handleFrame(client, frame) {
435
- if (!frame || typeof frame !== 'object') {
436
- return sendResponse(client, null, {
437
- ok: false,
438
- error: { code: 'INVALID_FRAME', message: 'JSON object frame required' },
439
- });
440
- }
441
-
442
- if (!frame.type) {
443
- return sendResponse(client, frame.request_id || null, {
444
- ok: false,
445
- error: { code: 'INVALID_FRAME', message: 'type required' },
446
- });
447
- }
448
-
449
- touchClient(client);
450
-
451
- try {
452
- if (frame.type === 'command') {
453
- const action = frame.payload?.action || frame.payload?.command;
454
- const result = await processCommand(client, action, frame.payload || {});
455
- return sendResponse(client, frame.payload?.request_id || frame.request_id || null, result);
456
- }
457
- if (frame.type === 'query') {
458
- const action = frame.payload?.action || frame.payload?.query;
459
- const result = await processQuery(client, action, frame.payload || {});
460
- return sendResponse(client, frame.payload?.request_id || frame.request_id || null, result);
461
- }
462
- return sendResponse(client, frame.request_id || null, {
463
- ok: false,
464
- error: { code: 'INVALID_FRAME_TYPE', message: `지원하지 않는 type: ${frame.type}` },
465
- });
466
- } catch (error) {
467
- return sendResponse(client, frame.request_id || null, {
468
- ok: false,
469
- error: { code: 'PIPE_REQUEST_FAILED', message: error.message },
470
- });
471
- }
472
- }
473
-
474
- function attachSocket(socket) {
475
- const client = {
476
- id: randomUUID(),
477
- socket,
478
- buffer: '',
479
- agentId: null,
480
- subscriptions: new Set(),
481
- lastHeartbeatMs: Date.now(),
482
- closed: false,
483
- };
484
- clients.set(client.id, client);
485
-
486
- socket.setEncoding('utf8');
487
- socket.on('data', async (chunk) => {
488
- client.buffer += chunk;
489
- let newlineIndex = client.buffer.indexOf('\n');
490
- while (newlineIndex >= 0) {
491
- const line = client.buffer.slice(0, newlineIndex).trim();
492
- client.buffer = client.buffer.slice(newlineIndex + 1);
493
- if (line) {
494
- const frame = safeJsonParse(line);
495
- await handleFrame(client, frame);
496
- }
497
- newlineIndex = client.buffer.indexOf('\n');
498
- }
499
- });
500
-
501
- socket.on('close', () => closeClient(client));
502
- socket.on('error', () => closeClient(client));
503
- }
504
-
505
- function startHeartbeatMonitor() {
506
- heartbeatTimer = setInterval(() => {
507
- const now = Date.now();
508
- for (const client of clients.values()) {
509
- if (now - client.lastHeartbeatMs <= heartbeatTtlMs) continue;
510
- sendFrame(client, {
511
- type: 'event',
512
- event: 'disconnect',
513
- payload: { reason: 'heartbeat_timeout' },
514
- });
515
- closeClient(client);
516
- }
517
- }, Math.max(1000, Math.floor(heartbeatTtlMs / 2)));
518
- heartbeatTimer.unref();
519
- }
520
-
521
- return {
522
- path: pipePath,
523
-
524
- async start() {
525
- if (server) return { path: pipePath };
526
-
527
- if (process.platform !== 'win32' && existsSync(pipePath)) {
528
- try { unlinkSync(pipePath); } catch {}
529
- }
530
-
531
- server = net.createServer(attachSocket);
532
- router.deliveryEmitter.on('message', onMessage);
533
-
534
- await new Promise((resolve, reject) => {
535
- server.once('error', reject);
536
- server.listen(pipePath, () => {
537
- server.off('error', reject);
538
- resolve();
539
- });
540
- });
541
-
542
- startHeartbeatMonitor();
543
- return { path: pipePath };
544
- },
545
-
546
- async stop() {
547
- if (heartbeatTimer) {
548
- clearInterval(heartbeatTimer);
549
- heartbeatTimer = null;
550
- }
551
-
552
- router.deliveryEmitter.off('message', onMessage);
553
-
554
- for (const client of clients.values()) {
555
- closeClient(client);
556
- }
557
-
558
- if (server) {
559
- const current = server;
560
- server = null;
561
- await new Promise((resolve) => current.close(resolve));
562
- }
563
-
564
- if (process.platform !== 'win32' && existsSync(pipePath)) {
565
- try { unlinkSync(pipePath); } catch {}
566
- }
567
- },
568
-
569
- getStatus() {
570
- return {
571
- path: pipePath,
572
- protocol: 'ndjson',
573
- clients: clients.size,
574
- pending_messages: Array.from(clients.values()).reduce((sum, client) => {
575
- if (!client.agentId) return sum;
576
- return sum + router.getPendingMessages(client.agentId, { max_messages: 1000 }).length;
577
- }, 0),
578
- };
579
- },
580
-
581
- async executeCommand(action, payload) {
582
- return await processCommand(null, action, payload);
583
- },
584
-
585
- async executeQuery(action, payload) {
586
- return await processQuery(null, action, payload);
587
- },
588
- };
589
- }
1
+ // hub/pipe.mjs — Named Pipe/Unix socket 제어 채널
2
+ // NDJSON 프로토콜로 에이전트 실시간 제어/이벤트 푸시를 처리한다.
3
+
4
+ import net from 'node:net';
5
+ import { existsSync, unlinkSync } from 'node:fs';
6
+ import { join } from 'node:path';
7
+ import { randomUUID } from 'node:crypto';
8
+ import {
9
+ teamInfo,
10
+ teamTaskList,
11
+ teamTaskUpdate,
12
+ teamSendMessage,
13
+ } from './team/nativeProxy.mjs';
14
+ import { createPipeline } from './pipeline/index.mjs';
15
+ import {
16
+ ensurePipelineTable,
17
+ initPipelineState,
18
+ listPipelineStates,
19
+ readPipelineState,
20
+ } from './pipeline/state.mjs';
21
+
22
+ const DEFAULT_HEARTBEAT_TTL_MS = 60000;
23
+
24
+ /** 플랫폼별 pipe 경로 계산 */
25
+ export function getPipePath(sessionId = process.pid) {
26
+ if (process.platform === 'win32') {
27
+ return `\\\\.\\pipe\\triflux-${sessionId}`;
28
+ }
29
+ return join('/tmp', `triflux-${sessionId}.sock`);
30
+ }
31
+
32
+ function safeJsonParse(line) {
33
+ try {
34
+ return JSON.parse(line);
35
+ } catch {
36
+ return null;
37
+ }
38
+ }
39
+
40
+ function normalizeTopics(topics) {
41
+ if (!Array.isArray(topics)) return [];
42
+ return topics
43
+ .map((topic) => String(topic || '').trim())
44
+ .filter(Boolean);
45
+ }
46
+
47
+ /**
48
+ * Named Pipe 서버 생성
49
+ * @param {object} opts
50
+ * @param {object} opts.router
51
+ * @param {object} [opts.store]
52
+ * @param {string|number} [opts.sessionId]
53
+ * @param {number} [opts.heartbeatTtlMs]
54
+ */
55
+ export function createPipeServer({
56
+ router,
57
+ store = null,
58
+ sessionId = process.pid,
59
+ heartbeatTtlMs = DEFAULT_HEARTBEAT_TTL_MS,
60
+ delegatorService = null,
61
+ } = {}) {
62
+ if (!router) {
63
+ throw new Error('router is required');
64
+ }
65
+
66
+ const pipePath = getPipePath(sessionId);
67
+ const clients = new Map();
68
+ let server = null;
69
+ let heartbeatTimer = null;
70
+
71
+ function sendFrame(client, frame) {
72
+ if (!client || client.closed || !client.socket.writable) return false;
73
+ try {
74
+ client.socket.write(`${JSON.stringify(frame)}\n`);
75
+ return true;
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
80
+
81
+ function sendResponse(client, requestId, result) {
82
+ return sendFrame(client, { type: 'response', request_id: requestId, ...result });
83
+ }
84
+
85
+ function closeClient(client) {
86
+ if (!client || client.closed) return;
87
+ client.closed = true;
88
+ clients.delete(client.id);
89
+ try { client.socket.destroy(); } catch {}
90
+ }
91
+
92
+ function touchClient(client) {
93
+ client.lastHeartbeatMs = Date.now();
94
+ }
95
+
96
+ function resolveAgentId(client, payload) {
97
+ const agentId = payload?.agent_id || client?.agentId;
98
+ if (!agentId) {
99
+ throw new Error('agent_id required');
100
+ }
101
+ return agentId;
102
+ }
103
+
104
+ function pushEvent(agentId, message) {
105
+ let delivered = false;
106
+ for (const client of clients.values()) {
107
+ if (client.agentId !== agentId) continue;
108
+ if (sendFrame(client, {
109
+ type: 'event',
110
+ event: 'message',
111
+ payload: { agent_id: agentId, message },
112
+ })) {
113
+ delivered = true;
114
+ }
115
+ }
116
+ return delivered;
117
+ }
118
+
119
+ function pushPendingMessages(agentId) {
120
+ if (!agentId) return 0;
121
+ const pending = router.getPendingMessages(agentId, { max_messages: 100 });
122
+ let pushed = 0;
123
+ for (const message of pending) {
124
+ if (router.markMessagePushed(agentId, message.id)) {
125
+ pushed += pushEvent(agentId, message) ? 1 : 0;
126
+ } else if (pushEvent(agentId, message)) {
127
+ pushed += 1;
128
+ }
129
+ }
130
+ return pushed;
131
+ }
132
+
133
+ async function processCommand(client, action, payload = {}) {
134
+ switch (action) {
135
+ case 'register': {
136
+ const result = router.registerAgent(payload);
137
+ if (client) {
138
+ client.agentId = payload.agent_id;
139
+ client.subscriptions = new Set(router.getSubscribedTopics(client.agentId));
140
+ touchClient(client);
141
+ pushPendingMessages(client.agentId);
142
+ }
143
+ return { ok: true, data: { ...result, pipe_path: pipePath } };
144
+ }
145
+
146
+ case 'subscribe': {
147
+ const agentId = resolveAgentId(client, payload);
148
+ const topics = normalizeTopics(payload.topics);
149
+ const result = router.subscribeAgent(agentId, topics, {
150
+ replace: Boolean(payload.replace),
151
+ });
152
+ if (client) {
153
+ client.agentId = agentId;
154
+ client.subscriptions = new Set(result.topics);
155
+ touchClient(client);
156
+ }
157
+ const replayed = pushPendingMessages(agentId);
158
+ return {
159
+ ok: true,
160
+ data: { ...result, replayed_messages: replayed },
161
+ };
162
+ }
163
+
164
+ case 'ack': {
165
+ const agentId = resolveAgentId(client, payload);
166
+ const acked = router.ackMessages(payload.message_ids || payload.ack_ids || [], agentId);
167
+ if (client) touchClient(client);
168
+ return { ok: true, data: { agent_id: agentId, acked_count: acked } };
169
+ }
170
+
171
+ case 'heartbeat': {
172
+ const agentId = resolveAgentId(client, payload);
173
+ const result = router.refreshAgentLease(agentId, payload.heartbeat_ttl_ms || heartbeatTtlMs);
174
+ if (client) touchClient(client);
175
+ return { ok: true, data: result };
176
+ }
177
+
178
+ case 'publish': {
179
+ const result = router.handlePublish(payload);
180
+ if (client) touchClient(client);
181
+ return result;
182
+ }
183
+
184
+ case 'handoff': {
185
+ const result = router.handleHandoff(payload);
186
+ if (client) touchClient(client);
187
+ return result;
188
+ }
189
+
190
+ case 'assign': {
191
+ const result = router.assignAsync(payload);
192
+ if (client) touchClient(client);
193
+ return result;
194
+ }
195
+
196
+ case 'assign_result': {
197
+ const result = router.reportAssignResult(payload);
198
+ if (client) touchClient(client);
199
+ return result;
200
+ }
201
+
202
+ case 'assign_retry': {
203
+ const result = router.retryAssign(payload.job_id, payload);
204
+ if (client) touchClient(client);
205
+ return result;
206
+ }
207
+
208
+ case 'result': {
209
+ const result = router.handlePublish({
210
+ from: payload.agent_id,
211
+ to: `topic:${payload.topic || 'task.result'}`,
212
+ topic: payload.topic || 'task.result',
213
+ payload: payload.payload || {},
214
+ priority: 5,
215
+ ttl_ms: 3600000,
216
+ trace_id: payload.trace_id,
217
+ correlation_id: payload.correlation_id,
218
+ });
219
+ if (client) touchClient(client);
220
+ return result;
221
+ }
222
+
223
+ case 'control': {
224
+ const result = router.handlePublish({
225
+ from: payload.from_agent || 'lead',
226
+ to: payload.to_agent,
227
+ topic: 'lead.control',
228
+ payload: {
229
+ command: payload.command,
230
+ reason: payload.reason || '',
231
+ ...(payload.payload || {}),
232
+ issued_at: Date.now(),
233
+ },
234
+ priority: 8,
235
+ ttl_ms: Math.max(1000, Math.min(Number(payload.ttl_ms) || 3600000, 86400000)),
236
+ trace_id: payload.trace_id,
237
+ correlation_id: payload.correlation_id,
238
+ });
239
+ if (client) touchClient(client);
240
+ return result;
241
+ }
242
+
243
+ case 'deregister': {
244
+ const agentId = resolveAgentId(client, payload);
245
+ router.updateAgentStatus(agentId, 'offline');
246
+ if (client) touchClient(client);
247
+ return {
248
+ ok: true,
249
+ data: { agent_id: agentId, status: 'offline' },
250
+ };
251
+ }
252
+
253
+ case 'team_task_update': {
254
+ const result = await teamTaskUpdate(payload);
255
+ if (client) touchClient(client);
256
+ return result;
257
+ }
258
+
259
+ case 'team_send_message': {
260
+ const result = await teamSendMessage(payload);
261
+ if (client) touchClient(client);
262
+ return result;
263
+ }
264
+
265
+ case 'pipeline_advance': {
266
+ if (client) touchClient(client);
267
+ if (!store?.db) {
268
+ return { ok: false, error: 'hub_db_not_found' };
269
+ }
270
+ ensurePipelineTable(store.db);
271
+ const pipeline = createPipeline(store.db, payload.team_name);
272
+ return pipeline.advance(payload.phase);
273
+ }
274
+
275
+ case 'pipeline_init': {
276
+ if (client) touchClient(client);
277
+ if (!store?.db) {
278
+ return { ok: false, error: 'hub_db_not_found' };
279
+ }
280
+ ensurePipelineTable(store.db);
281
+ const state = initPipelineState(store.db, payload.team_name, {
282
+ fix_max: payload.fix_max,
283
+ ralph_max: payload.ralph_max,
284
+ });
285
+ return { ok: true, data: state };
286
+ }
287
+
288
+ case 'delegator_delegate': {
289
+ if (!delegatorService) {
290
+ return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
291
+ }
292
+ if (client) touchClient(client);
293
+ const result = await delegatorService.delegate(payload);
294
+ return { ok: result?.ok !== false, data: result };
295
+ }
296
+
297
+ case 'delegator_reply': {
298
+ if (!delegatorService) {
299
+ return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
300
+ }
301
+ if (client) touchClient(client);
302
+ const result = await delegatorService.reply(payload);
303
+ return { ok: result?.ok !== false, data: result };
304
+ }
305
+
306
+ default:
307
+ return {
308
+ ok: false,
309
+ error: { code: 'UNKNOWN_PIPE_COMMAND', message: `지원하지 않는 command: ${action}` },
310
+ };
311
+ }
312
+ }
313
+
314
+ function buildReplayMessages(agentId, payload = {}) {
315
+ const maxMessages = Math.max(1, Math.min(Number(payload.max_messages) || 20, 100));
316
+ const pending = router.getPendingMessages(agentId, {
317
+ max_messages: maxMessages,
318
+ include_topics: payload.topics,
319
+ });
320
+ if (!store?.getAuditMessagesForAgent) {
321
+ return pending.slice(0, maxMessages);
322
+ }
323
+
324
+ const audit = store.getAuditMessagesForAgent(agentId, {
325
+ max_messages: maxMessages,
326
+ include_topics: payload.topics,
327
+ });
328
+ const byId = new Map();
329
+ for (const message of [...pending, ...audit]) {
330
+ if (!message?.id || byId.has(message.id)) continue;
331
+ byId.set(message.id, message);
332
+ }
333
+ return Array.from(byId.values())
334
+ .sort((left, right) => right.created_at_ms - left.created_at_ms)
335
+ .slice(0, maxMessages);
336
+ }
337
+
338
+ async function processQuery(client, action, payload = {}) {
339
+ switch (action) {
340
+ case 'drain': {
341
+ const agentId = resolveAgentId(client, payload);
342
+ const messages = router.drainAgent(agentId, {
343
+ max_messages: payload.max_messages,
344
+ include_topics: payload.topics,
345
+ auto_ack: payload.auto_ack,
346
+ });
347
+ if (client) touchClient(client);
348
+ return {
349
+ ok: true,
350
+ data: { messages, count: messages.length, server_time_ms: Date.now() },
351
+ };
352
+ }
353
+
354
+ case 'context': {
355
+ const agentId = resolveAgentId(client, payload);
356
+ const messages = buildReplayMessages(agentId, payload);
357
+ if (client) touchClient(client);
358
+ return {
359
+ ok: true,
360
+ data: { messages, count: messages.length, server_time_ms: Date.now() },
361
+ };
362
+ }
363
+
364
+ case 'status': {
365
+ const scope = payload.scope || 'hub';
366
+ if (client) touchClient(client);
367
+ return router.getStatus(scope, payload);
368
+ }
369
+
370
+ case 'assign_status': {
371
+ if (client) touchClient(client);
372
+ return router.getAssignStatus(payload);
373
+ }
374
+
375
+ case 'team_info': {
376
+ const result = await teamInfo(payload);
377
+ if (client) touchClient(client);
378
+ return result;
379
+ }
380
+
381
+ case 'team_task_list': {
382
+ const result = await teamTaskList(payload);
383
+ if (client) touchClient(client);
384
+ return result;
385
+ }
386
+
387
+ case 'pipeline_state': {
388
+ if (client) touchClient(client);
389
+ if (!store?.db) {
390
+ return { ok: false, error: 'hub_db_not_found' };
391
+ }
392
+ ensurePipelineTable(store.db);
393
+ const state = readPipelineState(store.db, payload.team_name);
394
+ return state
395
+ ? { ok: true, data: state }
396
+ : { ok: false, error: 'pipeline_not_found' };
397
+ }
398
+
399
+ case 'pipeline_list': {
400
+ if (client) touchClient(client);
401
+ if (!store?.db) {
402
+ return { ok: false, error: 'hub_db_not_found' };
403
+ }
404
+ ensurePipelineTable(store.db);
405
+ return { ok: true, data: listPipelineStates(store.db) };
406
+ }
407
+
408
+ case 'delegator_status': {
409
+ if (!delegatorService) {
410
+ return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
411
+ }
412
+ if (client) touchClient(client);
413
+ const result = await delegatorService.status(payload);
414
+ return { ok: result?.ok !== false, data: result };
415
+ }
416
+
417
+ default:
418
+ return {
419
+ ok: false,
420
+ error: { code: 'UNKNOWN_PIPE_QUERY', message: `지원하지 않는 query: ${action}` },
421
+ };
422
+ }
423
+ }
424
+
425
+ function onMessage(agentId, message) {
426
+ if (!agentId || !message) return;
427
+ if (router.markMessagePushed(agentId, message.id)) {
428
+ pushEvent(agentId, message);
429
+ return;
430
+ }
431
+ pushEvent(agentId, message);
432
+ }
433
+
434
+ async function handleFrame(client, frame) {
435
+ if (!frame || typeof frame !== 'object') {
436
+ return sendResponse(client, null, {
437
+ ok: false,
438
+ error: { code: 'INVALID_FRAME', message: 'JSON object frame required' },
439
+ });
440
+ }
441
+
442
+ if (!frame.type) {
443
+ return sendResponse(client, frame.request_id || null, {
444
+ ok: false,
445
+ error: { code: 'INVALID_FRAME', message: 'type required' },
446
+ });
447
+ }
448
+
449
+ touchClient(client);
450
+
451
+ try {
452
+ if (frame.type === 'command') {
453
+ const action = frame.payload?.action || frame.payload?.command;
454
+ const result = await processCommand(client, action, frame.payload || {});
455
+ return sendResponse(client, frame.payload?.request_id || frame.request_id || null, result);
456
+ }
457
+ if (frame.type === 'query') {
458
+ const action = frame.payload?.action || frame.payload?.query;
459
+ const result = await processQuery(client, action, frame.payload || {});
460
+ return sendResponse(client, frame.payload?.request_id || frame.request_id || null, result);
461
+ }
462
+ return sendResponse(client, frame.request_id || null, {
463
+ ok: false,
464
+ error: { code: 'INVALID_FRAME_TYPE', message: `지원하지 않는 type: ${frame.type}` },
465
+ });
466
+ } catch (error) {
467
+ return sendResponse(client, frame.request_id || null, {
468
+ ok: false,
469
+ error: { code: 'PIPE_REQUEST_FAILED', message: error.message },
470
+ });
471
+ }
472
+ }
473
+
474
+ function attachSocket(socket) {
475
+ const client = {
476
+ id: randomUUID(),
477
+ socket,
478
+ buffer: '',
479
+ agentId: null,
480
+ subscriptions: new Set(),
481
+ lastHeartbeatMs: Date.now(),
482
+ closed: false,
483
+ };
484
+ clients.set(client.id, client);
485
+
486
+ socket.setEncoding('utf8');
487
+ socket.on('data', async (chunk) => {
488
+ client.buffer += chunk;
489
+ let newlineIndex = client.buffer.indexOf('\n');
490
+ while (newlineIndex >= 0) {
491
+ const line = client.buffer.slice(0, newlineIndex).trim();
492
+ client.buffer = client.buffer.slice(newlineIndex + 1);
493
+ if (line) {
494
+ const frame = safeJsonParse(line);
495
+ await handleFrame(client, frame);
496
+ }
497
+ newlineIndex = client.buffer.indexOf('\n');
498
+ }
499
+ });
500
+
501
+ socket.on('close', () => closeClient(client));
502
+ socket.on('error', () => closeClient(client));
503
+ }
504
+
505
+ function startHeartbeatMonitor() {
506
+ heartbeatTimer = setInterval(() => {
507
+ const now = Date.now();
508
+ for (const client of clients.values()) {
509
+ if (now - client.lastHeartbeatMs <= heartbeatTtlMs) continue;
510
+ sendFrame(client, {
511
+ type: 'event',
512
+ event: 'disconnect',
513
+ payload: { reason: 'heartbeat_timeout' },
514
+ });
515
+ closeClient(client);
516
+ }
517
+ }, Math.max(1000, Math.floor(heartbeatTtlMs / 2)));
518
+ heartbeatTimer.unref();
519
+ }
520
+
521
+ return {
522
+ path: pipePath,
523
+
524
+ async start() {
525
+ if (server) return { path: pipePath };
526
+
527
+ if (process.platform !== 'win32' && existsSync(pipePath)) {
528
+ try { unlinkSync(pipePath); } catch {}
529
+ }
530
+
531
+ server = net.createServer(attachSocket);
532
+ router.deliveryEmitter.on('message', onMessage);
533
+
534
+ await new Promise((resolve, reject) => {
535
+ server.once('error', reject);
536
+ server.listen(pipePath, () => {
537
+ server.off('error', reject);
538
+ resolve();
539
+ });
540
+ });
541
+
542
+ startHeartbeatMonitor();
543
+ return { path: pipePath };
544
+ },
545
+
546
+ async stop() {
547
+ if (heartbeatTimer) {
548
+ clearInterval(heartbeatTimer);
549
+ heartbeatTimer = null;
550
+ }
551
+
552
+ router.deliveryEmitter.off('message', onMessage);
553
+
554
+ for (const client of clients.values()) {
555
+ closeClient(client);
556
+ }
557
+
558
+ if (server) {
559
+ const current = server;
560
+ server = null;
561
+ await new Promise((resolve) => current.close(resolve));
562
+ }
563
+
564
+ if (process.platform !== 'win32' && existsSync(pipePath)) {
565
+ try { unlinkSync(pipePath); } catch {}
566
+ }
567
+ },
568
+
569
+ getStatus() {
570
+ return {
571
+ path: pipePath,
572
+ protocol: 'ndjson',
573
+ clients: clients.size,
574
+ pending_messages: Array.from(clients.values()).reduce((sum, client) => {
575
+ if (!client.agentId) return sum;
576
+ return sum + router.countPendingMessages(client.agentId);
577
+ }, 0),
578
+ };
579
+ },
580
+
581
+ async executeCommand(action, payload) {
582
+ return await processCommand(null, action, payload);
583
+ },
584
+
585
+ async executeQuery(action, payload) {
586
+ return await processQuery(null, action, payload);
587
+ },
588
+ };
589
+ }