@triflux/remote 10.0.0-alpha.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 (68) hide show
  1. package/hub/pipe.mjs +579 -0
  2. package/hub/public/dashboard.html +355 -0
  3. package/hub/public/tray-icon.ico +0 -0
  4. package/hub/public/tray-icon.png +0 -0
  5. package/hub/server.mjs +1124 -0
  6. package/hub/store-adapter.mjs +851 -0
  7. package/hub/store.mjs +897 -0
  8. package/hub/team/agent-map.json +11 -0
  9. package/hub/team/ansi.mjs +379 -0
  10. package/hub/team/backend.mjs +90 -0
  11. package/hub/team/cli/commands/attach.mjs +37 -0
  12. package/hub/team/cli/commands/control.mjs +43 -0
  13. package/hub/team/cli/commands/debug.mjs +74 -0
  14. package/hub/team/cli/commands/focus.mjs +53 -0
  15. package/hub/team/cli/commands/interrupt.mjs +36 -0
  16. package/hub/team/cli/commands/kill.mjs +37 -0
  17. package/hub/team/cli/commands/list.mjs +24 -0
  18. package/hub/team/cli/commands/send.mjs +37 -0
  19. package/hub/team/cli/commands/start/index.mjs +106 -0
  20. package/hub/team/cli/commands/start/parse-args.mjs +130 -0
  21. package/hub/team/cli/commands/start/start-headless.mjs +109 -0
  22. package/hub/team/cli/commands/start/start-in-process.mjs +40 -0
  23. package/hub/team/cli/commands/start/start-mux.mjs +73 -0
  24. package/hub/team/cli/commands/start/start-wt.mjs +69 -0
  25. package/hub/team/cli/commands/status.mjs +87 -0
  26. package/hub/team/cli/commands/stop.mjs +31 -0
  27. package/hub/team/cli/commands/task.mjs +30 -0
  28. package/hub/team/cli/commands/tasks.mjs +13 -0
  29. package/hub/team/cli/help.mjs +42 -0
  30. package/hub/team/cli/index.mjs +41 -0
  31. package/hub/team/cli/manifest.mjs +29 -0
  32. package/hub/team/cli/render.mjs +30 -0
  33. package/hub/team/cli/services/attach-fallback.mjs +54 -0
  34. package/hub/team/cli/services/hub-client.mjs +208 -0
  35. package/hub/team/cli/services/member-selector.mjs +30 -0
  36. package/hub/team/cli/services/native-control.mjs +117 -0
  37. package/hub/team/cli/services/runtime-mode.mjs +62 -0
  38. package/hub/team/cli/services/state-store.mjs +48 -0
  39. package/hub/team/cli/services/task-model.mjs +30 -0
  40. package/hub/team/dashboard-anchor.mjs +14 -0
  41. package/hub/team/dashboard-layout.mjs +33 -0
  42. package/hub/team/dashboard-open.mjs +153 -0
  43. package/hub/team/dashboard.mjs +274 -0
  44. package/hub/team/handoff.mjs +303 -0
  45. package/hub/team/headless.mjs +1149 -0
  46. package/hub/team/native-supervisor.mjs +392 -0
  47. package/hub/team/native.mjs +649 -0
  48. package/hub/team/nativeProxy.mjs +681 -0
  49. package/hub/team/orchestrator.mjs +161 -0
  50. package/hub/team/pane.mjs +153 -0
  51. package/hub/team/psmux.mjs +1354 -0
  52. package/hub/team/routing.mjs +223 -0
  53. package/hub/team/session.mjs +611 -0
  54. package/hub/team/shared.mjs +13 -0
  55. package/hub/team/staleState.mjs +361 -0
  56. package/hub/team/tui-lite.mjs +380 -0
  57. package/hub/team/tui-viewer.mjs +463 -0
  58. package/hub/team/tui.mjs +1245 -0
  59. package/hub/tools.mjs +554 -0
  60. package/hub/tray.mjs +376 -0
  61. package/hub/workers/claude-worker.mjs +475 -0
  62. package/hub/workers/codex-mcp.mjs +504 -0
  63. package/hub/workers/delegator-mcp.mjs +1076 -0
  64. package/hub/workers/factory.mjs +21 -0
  65. package/hub/workers/gemini-worker.mjs +373 -0
  66. package/hub/workers/interface.mjs +52 -0
  67. package/hub/workers/worker-utils.mjs +104 -0
  68. package/package.json +31 -0
@@ -0,0 +1,851 @@
1
+ import { recalcConfidence } from './reflexion.mjs';
2
+ import { createStore, importBetterSqlite3, uuidv7 } from './store.mjs';
3
+
4
+ function clone(value) {
5
+ return value == null ? value : JSON.parse(JSON.stringify(value));
6
+ }
7
+
8
+ function clampMaxMessages(value, fallback = 20) {
9
+ const num = Number(value);
10
+ if (!Number.isFinite(num)) return fallback;
11
+ return Math.max(1, Math.min(Math.trunc(num), 100));
12
+ }
13
+
14
+ function clampPriority(value, fallback = 5) {
15
+ const num = Number(value);
16
+ if (!Number.isFinite(num)) return fallback;
17
+ return Math.max(1, Math.min(Math.trunc(num), 9));
18
+ }
19
+
20
+ function clampDuration(value, fallback = 600000, min = 1000, max = 86400000) {
21
+ const num = Number(value);
22
+ if (!Number.isFinite(num)) return fallback;
23
+ return Math.max(min, Math.min(Math.trunc(num), max));
24
+ }
25
+
26
+ function clampConfidence(value, fallback = 0.5) {
27
+ const num = Number(value);
28
+ if (!Number.isFinite(num)) return fallback;
29
+ return Math.max(0, Math.min(num, 1));
30
+ }
31
+
32
+ function clampHitCount(value, fallback = 1) {
33
+ const num = Number(value);
34
+ if (!Number.isFinite(num)) return fallback;
35
+ return Math.max(1, Math.trunc(num));
36
+ }
37
+
38
+ function clampHitIncrement(value, fallback = 1) {
39
+ const num = Number(value);
40
+ if (!Number.isFinite(num)) return fallback;
41
+ return Math.max(0, Math.trunc(num));
42
+ }
43
+
44
+ function coerceTimestamp(value, fallback = Date.now()) {
45
+ const num = Number(value);
46
+ if (!Number.isFinite(num)) return fallback;
47
+ return Math.trunc(num);
48
+ }
49
+
50
+ function clampRetentionMs(value, fallback = 30 * 24 * 3600 * 1000) {
51
+ const num = Number(value);
52
+ if (!Number.isFinite(num)) return fallback;
53
+ return Math.max(1, Math.trunc(num));
54
+ }
55
+
56
+ function normalizeAdaptiveRuleIdentity(projectSlug, pattern) {
57
+ const normalizedProject = String(projectSlug || '');
58
+ const normalizedPattern = String(pattern || '');
59
+ if (!normalizedProject || !normalizedPattern) return null;
60
+ return { project_slug: normalizedProject, pattern: normalizedPattern };
61
+ }
62
+
63
+ function buildAdaptiveRuleRow({
64
+ project_slug,
65
+ pattern,
66
+ confidence = 0.5,
67
+ hit_count = 1,
68
+ last_seen_ms,
69
+ created_ms,
70
+ } = {}) {
71
+ const identity = normalizeAdaptiveRuleIdentity(project_slug, pattern);
72
+ if (!identity) return null;
73
+ const createdAt = coerceTimestamp(created_ms);
74
+ const lastSeenAt = Math.max(createdAt, coerceTimestamp(last_seen_ms, createdAt));
75
+ return {
76
+ ...identity,
77
+ confidence: clampConfidence(confidence, 0.5),
78
+ hit_count: clampHitCount(hit_count, 1),
79
+ last_seen_ms: lastSeenAt,
80
+ created_ms: createdAt,
81
+ };
82
+ }
83
+
84
+ function buildAssignCallbackEvent(row) {
85
+ return {
86
+ job_id: row.job_id,
87
+ status: row.status,
88
+ result: row.result ?? row.error ?? null,
89
+ timestamp: new Date(row.updated_at_ms || Date.now()).toISOString(),
90
+ };
91
+ }
92
+
93
+ /**
94
+ * 인메모리 방식의 데이터 저장소를 생성합니다.
95
+ * SQLite를 사용할 수 없는 환경에서 폴백(fallback)으로 사용됩니다.
96
+ *
97
+ * @returns {object} 인메모리 스토어 객체
98
+ */
99
+ export function createMemoryStore() {
100
+ const agents = new Map();
101
+ const messages = new Map();
102
+ const humanRequests = new Map();
103
+ const deadLetters = new Map();
104
+ const assignJobs = new Map();
105
+ const reflexionEntries = new Map();
106
+ const adaptiveRules = new Map();
107
+ const assignStatusListeners = new Set();
108
+
109
+ function getAdaptiveRuleKey(projectSlug, pattern) {
110
+ return `${projectSlug}\u0000${pattern}`;
111
+ }
112
+
113
+ function notifyAssignStatusListeners(row) {
114
+ const event = buildAssignCallbackEvent(row);
115
+ for (const listener of Array.from(assignStatusListeners)) {
116
+ try { listener(event, clone(row)); } catch {}
117
+ }
118
+ }
119
+
120
+ function getRecentMessages() {
121
+ return Array.from(messages.values()).sort((left, right) => right.created_at_ms - left.created_at_ms);
122
+ }
123
+
124
+ function upsertMessage(message) {
125
+ messages.set(message.id, clone(message));
126
+ return clone(message);
127
+ }
128
+
129
+ const store = {
130
+ type: 'memory',
131
+ db: null,
132
+ uuidv7,
133
+
134
+ close() {},
135
+
136
+ registerAgent({ agent_id, cli, pid, capabilities = [], topics = [], heartbeat_ttl_ms = 30000, metadata = {} }) {
137
+ const now = Date.now();
138
+ const current = agents.get(agent_id) || {};
139
+ const next = {
140
+ ...current,
141
+ agent_id,
142
+ cli,
143
+ pid: pid ?? null,
144
+ capabilities: clone(capabilities),
145
+ topics: clone(topics),
146
+ last_seen_ms: now,
147
+ lease_expires_ms: now + heartbeat_ttl_ms,
148
+ status: 'online',
149
+ metadata: clone(metadata),
150
+ };
151
+ agents.set(agent_id, next);
152
+ return { agent_id, lease_id: uuidv7(), lease_expires_ms: next.lease_expires_ms, server_time_ms: now };
153
+ },
154
+
155
+ getAgent(id) {
156
+ return clone(agents.get(id) || null);
157
+ },
158
+
159
+ refreshLease(agentId, ttlMs = 30000) {
160
+ const current = agents.get(agentId);
161
+ if (!current) return { agent_id: agentId, lease_expires_ms: Date.now() + ttlMs, server_time_ms: Date.now() };
162
+ const now = Date.now();
163
+ current.last_seen_ms = now;
164
+ current.lease_expires_ms = now + ttlMs;
165
+ current.status = 'online';
166
+ return { agent_id: agentId, lease_expires_ms: current.lease_expires_ms, server_time_ms: now };
167
+ },
168
+
169
+ updateAgentTopics(agentId, topics = []) {
170
+ const current = agents.get(agentId);
171
+ if (!current) return false;
172
+ current.topics = clone(topics);
173
+ current.last_seen_ms = Date.now();
174
+ return true;
175
+ },
176
+
177
+ listOnlineAgents() {
178
+ return Array.from(agents.values())
179
+ .filter((agent) => agent.status !== 'offline')
180
+ .map((agent) => clone(agent));
181
+ },
182
+
183
+ listAllAgents() {
184
+ return Array.from(agents.values()).map((agent) => clone(agent));
185
+ },
186
+
187
+ getAgentsByTopic(topic) {
188
+ return Array.from(agents.values())
189
+ .filter((agent) => agent.status !== 'offline' && Array.isArray(agent.topics) && agent.topics.includes(topic))
190
+ .map((agent) => clone(agent));
191
+ },
192
+
193
+ sweepStaleAgents() {
194
+ const now = Date.now();
195
+ let stale = 0;
196
+ let offline = 0;
197
+ for (const agent of agents.values()) {
198
+ if (agent.status === 'online' && agent.lease_expires_ms < now) {
199
+ agent.status = 'stale';
200
+ stale += 1;
201
+ } else if (agent.status === 'stale' && agent.lease_expires_ms < now - 300000) {
202
+ agent.status = 'offline';
203
+ offline += 1;
204
+ }
205
+ }
206
+ return { stale, offline };
207
+ },
208
+
209
+ updateAgentStatus(agentId, status) {
210
+ const current = agents.get(agentId);
211
+ if (!current) return false;
212
+ current.status = status;
213
+ return true;
214
+ },
215
+
216
+ auditLog({ type, from, to, topic, priority = 5, ttl_ms = 300000, payload = {}, trace_id, correlation_id, status = 'queued' }) {
217
+ const now = Date.now();
218
+ const row = {
219
+ id: uuidv7(),
220
+ type,
221
+ from_agent: from,
222
+ to_agent: to,
223
+ topic,
224
+ priority,
225
+ ttl_ms,
226
+ created_at_ms: now,
227
+ expires_at_ms: now + ttl_ms,
228
+ correlation_id: correlation_id || uuidv7(),
229
+ trace_id: trace_id || uuidv7(),
230
+ payload: clone(payload || {}),
231
+ status,
232
+ };
233
+ return upsertMessage(row);
234
+ },
235
+
236
+ enqueueMessage(args) {
237
+ return store.auditLog(args);
238
+ },
239
+
240
+ getMessage(id) {
241
+ return clone(messages.get(id) || null);
242
+ },
243
+
244
+ getResponseByCorrelation(cid) {
245
+ return getRecentMessages().find((message) => message.correlation_id === cid && message.type === 'response') || null;
246
+ },
247
+
248
+ getMessagesByTrace(tid) {
249
+ return Array.from(messages.values())
250
+ .filter((message) => message.trace_id === tid)
251
+ .sort((left, right) => left.created_at_ms - right.created_at_ms)
252
+ .map((message) => clone(message));
253
+ },
254
+
255
+ updateMessageStatus(id, status) {
256
+ const current = messages.get(id);
257
+ if (!current) return false;
258
+ current.status = status;
259
+ return true;
260
+ },
261
+
262
+ getAuditMessagesForAgent(agentId, { max_messages = 20, include_topics = null } = {}) {
263
+ const limit = clampMaxMessages(max_messages);
264
+ const topics = Array.isArray(include_topics) && include_topics.length
265
+ ? include_topics
266
+ : (agents.get(agentId)?.topics || []);
267
+ const topicSet = new Set(topics);
268
+ return getRecentMessages()
269
+ .filter((message) => (
270
+ message.to_agent === agentId
271
+ || (String(message.to_agent || '').startsWith('topic:') && topicSet.has(message.topic))
272
+ ))
273
+ .slice(0, limit)
274
+ .map((message) => clone(message));
275
+ },
276
+
277
+ deliverToAgent(messageId, agentId) {
278
+ return Boolean(messages.get(messageId) && agentId);
279
+ },
280
+
281
+ deliverToTopic(messageId, topic) {
282
+ void messageId;
283
+ return store.getAgentsByTopic(topic).length;
284
+ },
285
+
286
+ pollForAgent(agentId, { max_messages = 20, include_topics = null } = {}) {
287
+ return store.getAuditMessagesForAgent(agentId, { max_messages, include_topics });
288
+ },
289
+
290
+ ackMessages() {
291
+ return 0;
292
+ },
293
+
294
+ insertHumanRequest({ requester_agent, kind, prompt, requested_schema = {}, deadline_ms, default_action, correlation_id, trace_id }) {
295
+ const requestId = uuidv7();
296
+ const now = Date.now();
297
+ const row = {
298
+ request_id: requestId,
299
+ requester_agent,
300
+ kind,
301
+ prompt,
302
+ schema: clone(requested_schema),
303
+ state: 'pending',
304
+ deadline_ms: now + deadline_ms,
305
+ default_action,
306
+ correlation_id: correlation_id || uuidv7(),
307
+ trace_id: trace_id || uuidv7(),
308
+ response: null,
309
+ };
310
+ humanRequests.set(requestId, row);
311
+ return { request_id: requestId, state: 'pending', deadline_ms: row.deadline_ms };
312
+ },
313
+
314
+ getHumanRequest(id) {
315
+ return clone(humanRequests.get(id) || null);
316
+ },
317
+
318
+ updateHumanRequest(id, state, resp = null) {
319
+ const current = humanRequests.get(id);
320
+ if (!current) return false;
321
+ current.state = state;
322
+ current.response = resp == null ? null : clone(resp);
323
+ return true;
324
+ },
325
+
326
+ getPendingHumanRequests() {
327
+ return Array.from(humanRequests.values())
328
+ .filter((request) => request.state === 'pending')
329
+ .map((request) => clone(request));
330
+ },
331
+
332
+ expireHumanRequests() {
333
+ let changed = 0;
334
+ const now = Date.now();
335
+ for (const request of humanRequests.values()) {
336
+ if (request.state === 'pending' && request.deadline_ms < now) {
337
+ request.state = 'timed_out';
338
+ changed += 1;
339
+ }
340
+ }
341
+ return changed;
342
+ },
343
+
344
+ moveToDeadLetter(messageId, reason, lastError = null) {
345
+ const current = messages.get(messageId);
346
+ if (current) current.status = 'dead_letter';
347
+ deadLetters.set(messageId, {
348
+ message_id: messageId,
349
+ reason,
350
+ failed_at_ms: Date.now(),
351
+ last_error: lastError,
352
+ });
353
+ return true;
354
+ },
355
+
356
+ getDeadLetters(limit = 50) {
357
+ return Array.from(deadLetters.values())
358
+ .sort((left, right) => right.failed_at_ms - left.failed_at_ms)
359
+ .slice(0, limit)
360
+ .map((entry) => clone(entry));
361
+ },
362
+
363
+ createAssign({
364
+ job_id,
365
+ supervisor_agent,
366
+ worker_agent,
367
+ topic = 'assign.job',
368
+ task = '',
369
+ payload = {},
370
+ status = 'queued',
371
+ attempt = 1,
372
+ retry_count = 0,
373
+ max_retries = 0,
374
+ priority = 5,
375
+ ttl_ms = 600000,
376
+ timeout_ms = 600000,
377
+ deadline_ms,
378
+ trace_id,
379
+ correlation_id,
380
+ last_message_id = null,
381
+ result = null,
382
+ error = null,
383
+ }) {
384
+ const now = Date.now();
385
+ const normalizedTimeout = clampDuration(timeout_ms, 600000);
386
+ const row = {
387
+ job_id: job_id || uuidv7(),
388
+ supervisor_agent,
389
+ worker_agent,
390
+ topic: String(topic || 'assign.job'),
391
+ task: String(task || ''),
392
+ payload: clone(payload || {}),
393
+ status,
394
+ attempt: Math.max(1, Number(attempt) || 1),
395
+ retry_count: Math.max(0, Number(retry_count) || 0),
396
+ max_retries: Math.max(0, Number(max_retries) || 0),
397
+ priority: clampPriority(priority, 5),
398
+ ttl_ms: clampDuration(ttl_ms, normalizedTimeout),
399
+ timeout_ms: normalizedTimeout,
400
+ deadline_ms: Number.isFinite(Number(deadline_ms))
401
+ ? Math.trunc(Number(deadline_ms))
402
+ : now + normalizedTimeout,
403
+ trace_id: trace_id || uuidv7(),
404
+ correlation_id: correlation_id || uuidv7(),
405
+ last_message_id,
406
+ result: result == null ? null : clone(result),
407
+ error: error == null ? null : clone(error),
408
+ created_at_ms: now,
409
+ updated_at_ms: now,
410
+ started_at_ms: status === 'running' ? now : null,
411
+ completed_at_ms: ['succeeded', 'failed', 'timed_out'].includes(status) ? now : null,
412
+ last_retry_at_ms: retry_count > 0 ? now : null,
413
+ };
414
+ assignJobs.set(row.job_id, row);
415
+ notifyAssignStatusListeners(row);
416
+ return clone(row);
417
+ },
418
+
419
+ getAssign(jobId) {
420
+ return clone(assignJobs.get(jobId) || null);
421
+ },
422
+
423
+ updateAssignStatus(jobId, status, patch = {}) {
424
+ const current = assignJobs.get(jobId);
425
+ if (!current) return null;
426
+
427
+ const snapshot = clone(current);
428
+ const now = Date.now();
429
+ const nextStatus = status || current.status;
430
+ const isTerminal = ['succeeded', 'failed', 'timed_out'].includes(nextStatus);
431
+ const nextTimeout = clampDuration(patch.timeout_ms ?? current.timeout_ms, current.timeout_ms);
432
+ const nextRow = {
433
+ ...current,
434
+ supervisor_agent: patch.supervisor_agent ?? current.supervisor_agent,
435
+ worker_agent: patch.worker_agent ?? current.worker_agent,
436
+ topic: patch.topic ?? current.topic,
437
+ task: patch.task ?? current.task,
438
+ payload: clone(patch.payload ?? current.payload ?? {}),
439
+ status: nextStatus,
440
+ attempt: Math.max(1, Number(patch.attempt ?? current.attempt) || current.attempt || 1),
441
+ retry_count: Math.max(0, Number(patch.retry_count ?? current.retry_count) || 0),
442
+ max_retries: Math.max(0, Number(patch.max_retries ?? current.max_retries) || 0),
443
+ priority: clampPriority(patch.priority ?? current.priority, current.priority || 5),
444
+ ttl_ms: clampDuration(patch.ttl_ms ?? current.ttl_ms, current.ttl_ms || nextTimeout),
445
+ timeout_ms: nextTimeout,
446
+ deadline_ms: (() => {
447
+ if (Object.hasOwn(patch, 'deadline_ms')) {
448
+ return patch.deadline_ms == null ? null : Math.trunc(Number(patch.deadline_ms));
449
+ }
450
+ if (isTerminal) return null;
451
+ if (nextStatus === 'running' && !current.deadline_ms) return now + nextTimeout;
452
+ return current.deadline_ms;
453
+ })(),
454
+ trace_id: patch.trace_id ?? current.trace_id,
455
+ correlation_id: patch.correlation_id ?? current.correlation_id,
456
+ last_message_id: Object.hasOwn(patch, 'last_message_id')
457
+ ? patch.last_message_id
458
+ : current.last_message_id,
459
+ result: Object.hasOwn(patch, 'result')
460
+ ? (patch.result == null ? null : clone(patch.result))
461
+ : current.result,
462
+ error: Object.hasOwn(patch, 'error')
463
+ ? (patch.error == null ? null : clone(patch.error))
464
+ : current.error,
465
+ updated_at_ms: now,
466
+ started_at_ms: Object.hasOwn(patch, 'started_at_ms')
467
+ ? patch.started_at_ms
468
+ : (nextStatus === 'running' ? (current.started_at_ms || now) : current.started_at_ms),
469
+ completed_at_ms: Object.hasOwn(patch, 'completed_at_ms')
470
+ ? patch.completed_at_ms
471
+ : (isTerminal ? (current.completed_at_ms || now) : current.completed_at_ms),
472
+ last_retry_at_ms: Object.hasOwn(patch, 'last_retry_at_ms')
473
+ ? patch.last_retry_at_ms
474
+ : current.last_retry_at_ms,
475
+ };
476
+ assignJobs.set(jobId, nextRow);
477
+ if (snapshot.status !== nextRow.status) {
478
+ notifyAssignStatusListeners(nextRow);
479
+ }
480
+ return clone(nextRow);
481
+ },
482
+
483
+ listAssigns({
484
+ supervisor_agent,
485
+ worker_agent,
486
+ status,
487
+ statuses,
488
+ trace_id,
489
+ correlation_id,
490
+ active_before_ms,
491
+ limit = 50,
492
+ } = {}) {
493
+ const statusList = Array.isArray(statuses) && statuses.length
494
+ ? statuses
495
+ : (status ? [status] : []);
496
+ return Array.from(assignJobs.values())
497
+ .filter((job) => !supervisor_agent || job.supervisor_agent === supervisor_agent)
498
+ .filter((job) => !worker_agent || job.worker_agent === worker_agent)
499
+ .filter((job) => !trace_id || job.trace_id === trace_id)
500
+ .filter((job) => !correlation_id || job.correlation_id === correlation_id)
501
+ .filter((job) => !statusList.length || statusList.includes(job.status))
502
+ .filter((job) => !Number.isFinite(Number(active_before_ms))
503
+ || (job.deadline_ms != null && job.deadline_ms <= Number(active_before_ms)))
504
+ .sort((left, right) => right.updated_at_ms - left.updated_at_ms)
505
+ .slice(0, clampMaxMessages(limit, 50))
506
+ .map((job) => clone(job));
507
+ },
508
+
509
+ retryAssign(jobId, patch = {}) {
510
+ const current = assignJobs.get(jobId);
511
+ if (!current) return null;
512
+ const nextRetryCount = Math.max(0, Number(patch.retry_count ?? current.retry_count + 1) || 0);
513
+ const nextAttempt = Math.max(current.attempt + 1, Number(patch.attempt ?? current.attempt + 1) || 1);
514
+ const nextTimeout = clampDuration(patch.timeout_ms ?? current.timeout_ms, current.timeout_ms);
515
+ return store.updateAssignStatus(jobId, 'queued', {
516
+ retry_count: nextRetryCount,
517
+ attempt: nextAttempt,
518
+ timeout_ms: nextTimeout,
519
+ ttl_ms: patch.ttl_ms ?? current.ttl_ms,
520
+ deadline_ms: Date.now() + nextTimeout,
521
+ completed_at_ms: null,
522
+ started_at_ms: null,
523
+ last_retry_at_ms: Date.now(),
524
+ result: patch.result ?? null,
525
+ error: Object.hasOwn(patch, 'error') ? patch.error : current.error,
526
+ last_message_id: null,
527
+ });
528
+ },
529
+
530
+ sweepExpired() {
531
+ const now = Date.now();
532
+ let expiredMessages = 0;
533
+ for (const message of messages.values()) {
534
+ if (message.status === 'queued' && message.expires_at_ms < now) {
535
+ message.status = 'dead_letter';
536
+ deadLetters.set(message.id, {
537
+ message_id: message.id,
538
+ reason: 'ttl_expired',
539
+ failed_at_ms: now,
540
+ last_error: null,
541
+ });
542
+ expiredMessages += 1;
543
+ }
544
+ }
545
+ const humanRequestsExpired = store.expireHumanRequests();
546
+ return { messages: expiredMessages, human_requests: humanRequestsExpired };
547
+ },
548
+
549
+ getQueueDepths() {
550
+ const depths = { urgent: 0, normal: 0, dlq: deadLetters.size };
551
+ const now = Date.now();
552
+ for (const message of messages.values()) {
553
+ if (message.status !== 'queued' || message.expires_at_ms < now) continue;
554
+ if (message.priority >= 7) depths.urgent += 1;
555
+ else depths.normal += 1;
556
+ }
557
+ return depths;
558
+ },
559
+
560
+ onAssignStatusChange(listener) {
561
+ if (typeof listener !== 'function') {
562
+ return () => {};
563
+ }
564
+ assignStatusListeners.add(listener);
565
+ return () => assignStatusListeners.delete(listener);
566
+ },
567
+
568
+ getDeliveryStats() {
569
+ const cutoff = Date.now() - 300000;
570
+ const totalDeliveries = Array.from(messages.values())
571
+ .filter((message) => message.status === 'acked' && message.created_at_ms > cutoff)
572
+ .length;
573
+ return { total_deliveries: totalDeliveries, avg_delivery_ms: 0 };
574
+ },
575
+
576
+ getHubStats() {
577
+ return {
578
+ online_agents: Array.from(agents.values()).filter((agent) => agent.status === 'online').length,
579
+ total_messages: messages.size,
580
+ active_assign_jobs: Array.from(assignJobs.values()).filter((job) => ['queued', 'running'].includes(job.status)).length,
581
+ ...store.getQueueDepths(),
582
+ };
583
+ },
584
+
585
+ getAuditStats() {
586
+ const countByStatus = (targetStatus) => Array.from(assignJobs.values()).filter((job) => job.status === targetStatus).length;
587
+ return {
588
+ online_agents: Array.from(agents.values()).filter((agent) => agent.status === 'online').length,
589
+ total_messages: messages.size,
590
+ dlq: deadLetters.size,
591
+ assign_queued: countByStatus('queued'),
592
+ assign_running: countByStatus('running'),
593
+ assign_failed: countByStatus('failed'),
594
+ assign_timed_out: countByStatus('timed_out'),
595
+ };
596
+ },
597
+
598
+ addReflexion({
599
+ type = 'reflexion',
600
+ error_pattern,
601
+ error_message,
602
+ context = {},
603
+ solution,
604
+ solution_code = null,
605
+ adaptive_state = {},
606
+ confidence = 0.5,
607
+ hit_count = 1,
608
+ success_count = 0,
609
+ last_hit_ms,
610
+ created_at_ms,
611
+ updated_at_ms,
612
+ }) {
613
+ const now = Date.now();
614
+ const entry = {
615
+ id: uuidv7(),
616
+ type,
617
+ error_pattern,
618
+ error_message,
619
+ context: clone(context),
620
+ solution,
621
+ solution_code,
622
+ adaptive_state: clone(adaptive_state),
623
+ confidence,
624
+ hit_count,
625
+ success_count,
626
+ last_hit_ms: last_hit_ms ?? now,
627
+ created_at_ms: created_at_ms ?? now,
628
+ updated_at_ms: updated_at_ms ?? now,
629
+ };
630
+ reflexionEntries.set(entry.id, entry);
631
+ return clone(entry);
632
+ },
633
+
634
+ getReflexion(id) {
635
+ return clone(reflexionEntries.get(id) || null);
636
+ },
637
+
638
+ findReflexion(errorPattern, context = {}) {
639
+ const entries = Array.from(reflexionEntries.values())
640
+ .filter((entry) => entry.error_pattern === errorPattern || entry.error_pattern.includes(errorPattern) || errorPattern.includes(entry.error_pattern))
641
+ .filter((entry) => Object.entries(context).every(([key, value]) => value == null || entry.context?.[key] === value))
642
+ .sort((left, right) => right.confidence - left.confidence);
643
+ return entries.map((entry) => clone(entry));
644
+ },
645
+
646
+ updateReflexionHit(id, success = false) {
647
+ const current = reflexionEntries.get(id);
648
+ if (!current) return null;
649
+ const now = Date.now();
650
+ current.hit_count += 1;
651
+ if (success) current.success_count += 1;
652
+ current.last_hit_ms = now;
653
+ current.updated_at_ms = now;
654
+ current.confidence = Math.max(0, Math.min(1, recalcConfidence(current)));
655
+ return clone(current);
656
+ },
657
+
658
+ listReflexion(filters = {}) {
659
+ const { type, minConfidence = Number.NEGATIVE_INFINITY, projectSlug } = filters;
660
+ return Array.from(reflexionEntries.values())
661
+ .filter((entry) => !type || entry.type === type)
662
+ .filter((entry) => entry.confidence >= minConfidence)
663
+ .filter((entry) => !projectSlug || entry.adaptive_state?.project_slug === projectSlug)
664
+ .sort((left, right) => right.confidence - left.confidence)
665
+ .map((entry) => clone(entry));
666
+ },
667
+
668
+ patchReflexion(id, patch = {}) {
669
+ const current = reflexionEntries.get(id);
670
+ if (!current) return null;
671
+ const next = {
672
+ ...current,
673
+ ...clone(patch),
674
+ context: patch.context ? clone(patch.context) : current.context,
675
+ adaptive_state: patch.adaptive_state ? clone(patch.adaptive_state) : current.adaptive_state,
676
+ updated_at_ms: patch.updated_at_ms ?? Date.now(),
677
+ };
678
+ reflexionEntries.set(id, next);
679
+ return clone(next);
680
+ },
681
+
682
+ deleteReflexion(id) {
683
+ return reflexionEntries.delete(id);
684
+ },
685
+
686
+ pruneReflexion(maxAge_ms = 30 * 24 * 3600 * 1000, minConfidence = 0.2) {
687
+ const cutoff = Date.now() - maxAge_ms;
688
+ let removed = 0;
689
+ for (const [id, entry] of Array.from(reflexionEntries.entries())) {
690
+ if (entry.updated_at_ms < cutoff && entry.confidence < minConfidence) {
691
+ reflexionEntries.delete(id);
692
+ removed += 1;
693
+ }
694
+ }
695
+ return removed;
696
+ },
697
+
698
+ addAdaptiveRule(rule) {
699
+ const next = buildAdaptiveRuleRow(rule);
700
+ if (!next) return null;
701
+ const key = getAdaptiveRuleKey(next.project_slug, next.pattern);
702
+ const current = adaptiveRules.get(key);
703
+ adaptiveRules.set(key, {
704
+ ...next,
705
+ created_ms: current ? Math.min(current.created_ms, next.created_ms) : next.created_ms,
706
+ });
707
+ return clone(adaptiveRules.get(key));
708
+ },
709
+
710
+ findAdaptiveRule(projectSlug, pattern) {
711
+ const identity = normalizeAdaptiveRuleIdentity(projectSlug, pattern);
712
+ if (!identity) return null;
713
+ return clone(adaptiveRules.get(getAdaptiveRuleKey(identity.project_slug, identity.pattern)) || null);
714
+ },
715
+
716
+ updateRuleConfidence(projectSlug, pattern, confidence, options = {}) {
717
+ const current = store.findAdaptiveRule(projectSlug, pattern);
718
+ if (!current) return null;
719
+ const next = {
720
+ ...current,
721
+ confidence: clampConfidence(confidence, current.confidence),
722
+ hit_count: current.hit_count + clampHitIncrement(options.hit_count_increment, 1),
723
+ last_seen_ms: Math.max(
724
+ current.last_seen_ms,
725
+ coerceTimestamp(options.last_seen_ms, Date.now()),
726
+ ),
727
+ };
728
+ adaptiveRules.set(getAdaptiveRuleKey(current.project_slug, current.pattern), next);
729
+ return clone(next);
730
+ },
731
+
732
+ pruneStaleRules(maxAge_ms = 30 * 24 * 3600 * 1000, minConfidence = 0.2) {
733
+ const cutoff = Date.now() - clampRetentionMs(maxAge_ms, 30 * 24 * 3600 * 1000);
734
+ let removed = 0;
735
+ for (const [key, rule] of Array.from(adaptiveRules.entries())) {
736
+ if (rule.last_seen_ms < cutoff && rule.confidence < minConfidence) {
737
+ adaptiveRules.delete(key);
738
+ removed += 1;
739
+ }
740
+ }
741
+ return removed;
742
+ },
743
+ };
744
+
745
+ return store;
746
+ }
747
+
748
+ function ensureAdaptiveRulesSchema(db) {
749
+ const schemaKey = 'adaptive_rules_schema_version';
750
+ const currentVersion = db.prepare('SELECT value FROM _meta WHERE key = ?').pluck().get(schemaKey);
751
+ if (currentVersion === '1') return;
752
+ db.exec(`
753
+ CREATE TABLE IF NOT EXISTS adaptive_rules (
754
+ project_slug TEXT NOT NULL,
755
+ pattern TEXT NOT NULL,
756
+ confidence REAL NOT NULL DEFAULT 0.5,
757
+ hit_count INTEGER NOT NULL DEFAULT 1,
758
+ last_seen_ms INTEGER NOT NULL,
759
+ created_ms INTEGER NOT NULL,
760
+ PRIMARY KEY (project_slug, pattern)
761
+ );
762
+ CREATE INDEX IF NOT EXISTS idx_adaptive_rules_last_seen
763
+ ON adaptive_rules(project_slug, last_seen_ms DESC);
764
+ CREATE INDEX IF NOT EXISTS idx_adaptive_rules_confidence
765
+ ON adaptive_rules(project_slug, confidence DESC);
766
+ `);
767
+ db.prepare('INSERT OR REPLACE INTO _meta (key, value) VALUES (?, ?)').run(schemaKey, '1');
768
+ }
769
+
770
+ function attachAdaptiveRuleStore(store) {
771
+ if (store.type !== 'sqlite' || !store.db) return store;
772
+ ensureAdaptiveRulesSchema(store.db);
773
+ const statements = {
774
+ upsertRule: store.db.prepare(`
775
+ INSERT INTO adaptive_rules (
776
+ project_slug, pattern, confidence, hit_count, last_seen_ms, created_ms
777
+ ) VALUES (
778
+ @project_slug, @pattern, @confidence, @hit_count, @last_seen_ms, @created_ms
779
+ )
780
+ ON CONFLICT(project_slug, pattern) DO UPDATE SET
781
+ confidence = excluded.confidence,
782
+ hit_count = excluded.hit_count,
783
+ last_seen_ms = excluded.last_seen_ms,
784
+ created_ms = MIN(adaptive_rules.created_ms, excluded.created_ms)`),
785
+ getRule: store.db.prepare('SELECT * FROM adaptive_rules WHERE project_slug = ? AND pattern = ?'),
786
+ updateRule: store.db.prepare(`
787
+ UPDATE adaptive_rules SET
788
+ confidence = ?,
789
+ hit_count = hit_count + ?,
790
+ last_seen_ms = ?
791
+ WHERE project_slug = ? AND pattern = ?`),
792
+ pruneRules: store.db.prepare(`
793
+ DELETE FROM adaptive_rules
794
+ WHERE last_seen_ms < ? AND confidence < ?`),
795
+ };
796
+
797
+ store.addAdaptiveRule = function addAdaptiveRule(rule) {
798
+ const next = buildAdaptiveRuleRow(rule);
799
+ if (!next) return null;
800
+ statements.upsertRule.run(next);
801
+ return store.findAdaptiveRule(next.project_slug, next.pattern);
802
+ };
803
+
804
+ store.findAdaptiveRule = function findAdaptiveRule(projectSlug, pattern) {
805
+ const identity = normalizeAdaptiveRuleIdentity(projectSlug, pattern);
806
+ if (!identity) return null;
807
+ return clone(statements.getRule.get(identity.project_slug, identity.pattern) || null);
808
+ };
809
+
810
+ store.updateRuleConfidence = function updateRuleConfidence(projectSlug, pattern, confidence, options = {}) {
811
+ const current = store.findAdaptiveRule(projectSlug, pattern);
812
+ if (!current) return null;
813
+ const updatedAt = Math.max(current.last_seen_ms, coerceTimestamp(options.last_seen_ms, Date.now()));
814
+ statements.updateRule.run(
815
+ clampConfidence(confidence, current.confidence),
816
+ clampHitIncrement(options.hit_count_increment, 1),
817
+ updatedAt,
818
+ current.project_slug,
819
+ current.pattern,
820
+ );
821
+ return store.findAdaptiveRule(current.project_slug, current.pattern);
822
+ };
823
+
824
+ store.pruneStaleRules = function pruneStaleRules(maxAge_ms = 30 * 24 * 3600 * 1000, minConfidence = 0.2) {
825
+ const cutoff = Date.now() - clampRetentionMs(maxAge_ms, 30 * 24 * 3600 * 1000);
826
+ return statements.pruneRules.run(cutoff, minConfidence).changes;
827
+ };
828
+
829
+ return store;
830
+ }
831
+
832
+ /**
833
+ * 환경에 따라 적절한 스토어 어댑터(SQLite 또는 인메모리)를 생성합니다.
834
+ *
835
+ * @param {string} dbPath - SQLite 데이터베이스 파일 경로
836
+ * @param {object} [options] - 옵션
837
+ * @param {Function} [options.loadDatabase] - 데이터베이스 로더 함수
838
+ * @returns {Promise<object>} 생성된 스토어 어댑터
839
+ */
840
+ export async function createStoreAdapter(dbPath, options = {}) {
841
+ const loadDatabase = options.loadDatabase || importBetterSqlite3;
842
+ try {
843
+ const DatabaseCtor = await loadDatabase();
844
+ const store = createStore(dbPath, { DatabaseCtor });
845
+ store.type = 'sqlite';
846
+ return attachAdaptiveRuleStore(store);
847
+ } catch (error) {
848
+ console.warn(`[store] SQLite unavailable (${error.message}), using in-memory fallback`);
849
+ return createMemoryStore();
850
+ }
851
+ }