@triflux/core 10.51.0 → 10.53.0

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 (61) hide show
  1. package/hub/bridge.mjs +69 -1197
  2. package/hub/cli-adapter-base.mjs +4 -104
  3. package/hub/codex-adapter.mjs +7 -116
  4. package/hub/index.mjs +3 -20
  5. package/hub/lib/process-utils.mjs +1 -33
  6. package/hub/team/claude-daemon-control.mjs +1 -2
  7. package/hub/team/claude-session-projection.mjs +5 -73
  8. package/hub/team/claude-transcript.mjs +19 -0
  9. package/hub/team/nativeProxy.mjs +759 -0
  10. package/hub/team/session-context.mjs +1 -1
  11. package/hud/constants.mjs +9 -22
  12. package/hud/hud-qos-status.mjs +16 -19
  13. package/hud/providers/codex-probe.mjs +2 -31
  14. package/hud/providers/codex.mjs +16 -58
  15. package/hud/providers/gemini.mjs +20 -0
  16. package/hud/renderers.mjs +101 -77
  17. package/hud/utils.mjs +9 -13
  18. package/package.json +3 -6
  19. package/scripts/lib/agent-route-policy.mjs +0 -67
  20. package/scripts/lib/cli-claude.mjs +0 -12
  21. package/scripts/lib/env-probe.mjs +0 -70
  22. package/scripts/lib/legacy-mcp-cleanup.mjs +283 -0
  23. package/scripts/lib/mcp-guard-engine.mjs +42 -101
  24. package/scripts/lib/session-presence.mjs +1 -99
  25. package/hub/account-broker.mjs +0 -1221
  26. package/hub/adaptive-diagnostic.mjs +0 -350
  27. package/hub/adaptive-inject.mjs +0 -224
  28. package/hub/adaptive-memory.mjs +0 -277
  29. package/hub/adaptive.mjs +0 -172
  30. package/hub/assign-callbacks.mjs +0 -170
  31. package/hub/delegator/contracts.mjs +0 -38
  32. package/hub/delegator/index.mjs +0 -14
  33. package/hub/delegator/schema/delegator-tools.schema.json +0 -250
  34. package/hub/delegator/service.mjs +0 -336
  35. package/hub/delegator/tool-definitions.mjs +0 -35
  36. package/hub/hitl.mjs +0 -202
  37. package/hub/lib/known-errors.json +0 -72
  38. package/hub/lib/memory-store.mjs +0 -1001
  39. package/hub/lib/phase-manager.mjs +0 -493
  40. package/hub/lib/state-snapshot.mjs +0 -293
  41. package/hub/lib/trace-recorder.mjs +0 -153
  42. package/hub/lib/uuidv7.mjs +0 -45
  43. package/hub/memory-doctor.mjs +0 -640
  44. package/hub/middleware/quota-middleware.mjs +0 -70
  45. package/hub/middleware/request-logger.mjs +0 -191
  46. package/hub/paths.mjs +0 -24
  47. package/hub/pipeline/index.mjs +0 -163
  48. package/hub/pipeline/state.mjs +0 -191
  49. package/hub/pipeline/transitions.mjs +0 -131
  50. package/hub/reflexion.mjs +0 -290
  51. package/hub/role-contract.mjs +0 -312
  52. package/hub/router.mjs +0 -2026
  53. package/hub/session-fingerprint.mjs +0 -418
  54. package/hub/state.mjs +0 -300
  55. package/hub/team/synapse-http.mjs +0 -311
  56. package/hub/team-bridge.mjs +0 -27
  57. package/scripts/lib/claudemd-scanner.mjs +0 -26
  58. package/scripts/lib/context.mjs +0 -51
  59. package/scripts/lib/handoff.mjs +0 -187
  60. package/scripts/lib/logger.mjs +0 -122
  61. package/scripts/lib/stealth-fetch.mjs +0 -176
@@ -1,493 +0,0 @@
1
- import { existsSync } from "node:fs";
2
- import {
3
- mkdir,
4
- readdir,
5
- readFile,
6
- rename,
7
- stat,
8
- unlink,
9
- writeFile,
10
- } from "node:fs/promises";
11
- import os from "node:os";
12
- import { dirname, join } from "node:path";
13
-
14
- /**
15
- * @typedef {"Research" | "Strategy" | "Execution" | "Validation"} WorkPhase
16
- */
17
-
18
- /**
19
- * @typedef {"active" | "complete" | "failed"} PhaseStatus
20
- */
21
-
22
- /**
23
- * @typedef {WorkPhase | "complete"} LegacyPhase
24
- */
25
-
26
- /**
27
- * @typedef {object} PhaseRecord
28
- * @property {WorkPhase | "complete"} phase
29
- * @property {PhaseStatus} phaseStatus
30
- * @property {WorkPhase | null} lastPhase
31
- */
32
-
33
- export const PHASE_ENUM = Object.freeze([
34
- "Research",
35
- "Strategy",
36
- "Execution",
37
- "Validation",
38
- ]);
39
-
40
- export const PHASE_STATUS = Object.freeze(["active", "complete", "failed"]);
41
-
42
- const PHASE_SET = new Set(PHASE_ENUM);
43
- const PHASE_STATUS_SET = new Set(PHASE_STATUS);
44
-
45
- /**
46
- * @returns {string}
47
- */
48
- function getHomeDir() {
49
- return process.env.HOME || process.env.USERPROFILE || os.homedir();
50
- }
51
-
52
- /**
53
- * @param {unknown} value
54
- * @returns {value is WorkPhase}
55
- */
56
- function isWorkPhase(value) {
57
- return PHASE_SET.has(String(value));
58
- }
59
-
60
- /**
61
- * @param {unknown} value
62
- * @returns {value is PhaseStatus}
63
- */
64
- function isPhaseStatus(value) {
65
- return PHASE_STATUS_SET.has(String(value));
66
- }
67
-
68
- /**
69
- * @param {unknown} value
70
- * @returns {WorkPhase | null}
71
- */
72
- function coerceWorkPhase(value) {
73
- const phase = coerceLegacyPhase(value);
74
- return phase === "complete" ? null : phase;
75
- }
76
-
77
- /**
78
- * @param {string} runId
79
- * @returns {{fullcycleStatePath: string, phaseStatePath: string}}
80
- */
81
- function getRunPaths(runId) {
82
- return {
83
- fullcycleStatePath: join(
84
- process.cwd(),
85
- ".tfx",
86
- "fullcycle",
87
- runId,
88
- "state.json",
89
- ),
90
- phaseStatePath: join(process.cwd(), ".tfx", "phases", `${runId}.json`),
91
- };
92
- }
93
-
94
- /**
95
- * @param {string} runId
96
- * @returns {string | null}
97
- */
98
- function resolveReadPath(runId) {
99
- const { fullcycleStatePath, phaseStatePath } = getRunPaths(runId);
100
- if (existsSync(fullcycleStatePath)) return fullcycleStatePath;
101
- if (existsSync(phaseStatePath)) return phaseStatePath;
102
- return null;
103
- }
104
-
105
- /**
106
- * @param {string} runId
107
- * @returns {string}
108
- */
109
- function resolveWritePath(runId) {
110
- const { fullcycleStatePath, phaseStatePath } = getRunPaths(runId);
111
- const fullcycleDir = dirname(fullcycleStatePath);
112
-
113
- if (
114
- existsSync(fullcycleStatePath) ||
115
- existsSync(fullcycleDir) ||
116
- !existsSync(phaseStatePath)
117
- ) {
118
- return fullcycleStatePath;
119
- }
120
-
121
- return phaseStatePath;
122
- }
123
-
124
- /**
125
- * @param {string} tempPath
126
- * @param {string} targetPath
127
- * @returns {Promise<void>}
128
- */
129
- async function safeReplaceFile(tempPath, targetPath) {
130
- try {
131
- await rename(tempPath, targetPath);
132
- } catch (error) {
133
- if (!["EEXIST", "EPERM", "EACCES"].includes(error?.code)) {
134
- try {
135
- await unlink(tempPath);
136
- } catch {}
137
- throw error;
138
- }
139
-
140
- try {
141
- await unlink(targetPath);
142
- } catch {}
143
-
144
- await rename(tempPath, targetPath);
145
- }
146
- }
147
-
148
- /**
149
- * @param {string} targetPath
150
- * @param {Record<string, unknown>} payload
151
- * @returns {Promise<void>}
152
- */
153
- async function writeJsonAtomic(targetPath, payload) {
154
- const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp`;
155
- await mkdir(dirname(targetPath), { recursive: true });
156
- await writeFile(tempPath, `${JSON.stringify(payload, null, 2)}\n`, {
157
- encoding: "utf8",
158
- mode: 0o600,
159
- });
160
- await safeReplaceFile(tempPath, targetPath);
161
- }
162
-
163
- /**
164
- * @param {string} targetPath
165
- * @param {string} content
166
- * @returns {Promise<void>}
167
- */
168
- async function writeTextAtomic(targetPath, content) {
169
- const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp`;
170
- await mkdir(dirname(targetPath), { recursive: true });
171
- await writeFile(tempPath, content, {
172
- encoding: "utf8",
173
- mode: 0o600,
174
- });
175
- await safeReplaceFile(tempPath, targetPath);
176
- }
177
-
178
- /**
179
- * @param {string} filePath
180
- * @returns {Promise<Record<string, unknown> | null>}
181
- */
182
- async function readJsonFile(filePath) {
183
- try {
184
- const raw = await readFile(filePath, "utf8");
185
- return JSON.parse(raw);
186
- } catch (error) {
187
- if (error?.code === "ENOENT") {
188
- return null;
189
- }
190
- throw error;
191
- }
192
- }
193
-
194
- /**
195
- * @param {Record<string, unknown>} data
196
- * @returns {PhaseRecord | null}
197
- */
198
- function phaseRecordFromData(data) {
199
- const explicitPhase = isWorkPhase(data.phase) ? data.phase : null;
200
- const explicitStatus = isPhaseStatus(data.phase_status)
201
- ? data.phase_status
202
- : null;
203
- const explicitLastPhase = isWorkPhase(data.last_phase)
204
- ? data.last_phase
205
- : null;
206
- const legacyLastPhase =
207
- explicitLastPhase || coerceWorkPhase(data.last_successful_phase);
208
-
209
- if (explicitPhase) {
210
- return {
211
- phase: explicitPhase,
212
- phaseStatus: explicitStatus || "active",
213
- lastPhase: legacyLastPhase,
214
- };
215
- }
216
-
217
- const legacyPhase = coerceLegacyPhase(data.current_phase);
218
- if (!legacyPhase) {
219
- return null;
220
- }
221
-
222
- if (legacyPhase === "complete") {
223
- return {
224
- phase: legacyLastPhase || "complete",
225
- phaseStatus: explicitStatus || "complete",
226
- lastPhase: legacyLastPhase,
227
- };
228
- }
229
-
230
- return {
231
- phase: legacyPhase,
232
- phaseStatus: explicitStatus || "active",
233
- lastPhase: legacyLastPhase,
234
- };
235
- }
236
-
237
- /**
238
- * @param {WorkPhase | "complete"} phase
239
- * @returns {string}
240
- */
241
- function toLegacyPhaseName(phase) {
242
- return phase === "complete" ? "complete" : phase.toLowerCase();
243
- }
244
-
245
- /**
246
- * @param {Record<string, unknown>} data
247
- * @param {PhaseRecord} record
248
- * @returns {Record<string, unknown>}
249
- */
250
- function applyPhaseRecord(data, record) {
251
- const next = {
252
- ...data,
253
- phase_status: record.phaseStatus,
254
- current_phase: toLegacyPhaseName(record.phase),
255
- };
256
-
257
- if (record.phase !== "complete") {
258
- next.phase = record.phase;
259
- }
260
-
261
- if (record.lastPhase) {
262
- next.last_phase = record.lastPhase;
263
- next.last_successful_phase = record.lastPhase.toLowerCase();
264
- }
265
-
266
- if (record.phaseStatus === "complete" && record.phase !== "complete") {
267
- next.last_phase = record.phase;
268
- next.last_successful_phase = record.phase.toLowerCase();
269
- }
270
-
271
- return next;
272
- }
273
-
274
- /**
275
- * @param {string} content
276
- * @returns {{lines: string[], bodyLines: string[], eol: string} | null}
277
- */
278
- function parseFrontmatter(content) {
279
- const eol = content.includes("\r\n") ? "\r\n" : "\n";
280
- const lines = content.split(/\r?\n/);
281
-
282
- if (lines[0] !== "---") {
283
- return null;
284
- }
285
-
286
- const closingIndex = lines.indexOf("---", 1);
287
- if (closingIndex === -1) {
288
- return null;
289
- }
290
-
291
- return {
292
- lines: lines.slice(1, closingIndex),
293
- bodyLines: lines.slice(closingIndex + 1),
294
- eol,
295
- };
296
- }
297
-
298
- /**
299
- * @param {string[]} lines
300
- * @param {Record<string, string>} fields
301
- * @returns {string[]}
302
- */
303
- function upsertFrontmatterLines(lines, fields) {
304
- const nextLines = [...lines];
305
-
306
- for (const [key, value] of Object.entries(fields)) {
307
- const keyPrefix = `${key}:`;
308
- const index = nextLines.findIndex((line) => line.startsWith(keyPrefix));
309
-
310
- if (index >= 0) {
311
- nextLines[index] = `${key}: ${value}`;
312
- continue;
313
- }
314
-
315
- nextLines.push(`${key}: ${value}`);
316
- }
317
-
318
- return nextLines;
319
- }
320
-
321
- /**
322
- * @param {string} content
323
- * @param {Record<string, string>} fields
324
- * @returns {string | null}
325
- */
326
- function injectFrontmatterFields(content, fields) {
327
- const parsed = parseFrontmatter(content);
328
- if (!parsed) {
329
- return null;
330
- }
331
-
332
- const updatedLines = upsertFrontmatterLines(parsed.lines, fields);
333
- return ["---", ...updatedLines, "---", ...parsed.bodyLines].join(parsed.eol);
334
- }
335
-
336
- /**
337
- * @param {string} runId
338
- * @returns {Promise<PhaseRecord | null>}
339
- */
340
- export async function readPhase(runId) {
341
- const statePath = resolveReadPath(runId);
342
- if (!statePath) {
343
- return null;
344
- }
345
-
346
- const data = await readJsonFile(statePath);
347
- if (!data) {
348
- return null;
349
- }
350
-
351
- const record = phaseRecordFromData(data);
352
- if (!record) {
353
- return null;
354
- }
355
-
356
- if (!isWorkPhase(data.phase)) {
357
- await writeJsonAtomic(statePath, applyPhaseRecord(data, record));
358
- }
359
-
360
- return record;
361
- }
362
-
363
- /**
364
- * @param {string} runId
365
- * @param {WorkPhase} phase
366
- * @param {PhaseStatus} [status="active"]
367
- * @returns {Promise<void>}
368
- */
369
- export async function writePhase(runId, phase, status = "active") {
370
- if (!isWorkPhase(phase)) {
371
- throw new Error(`Invalid phase: ${phase}`);
372
- }
373
-
374
- if (!isPhaseStatus(status)) {
375
- throw new Error(`Invalid phase status: ${status}`);
376
- }
377
-
378
- const statePath = resolveWritePath(runId);
379
- const current = (await readJsonFile(statePath)) || {};
380
- const next = {
381
- ...current,
382
- run_id: current.run_id || runId,
383
- phase,
384
- phase_status: status,
385
- current_phase: phase.toLowerCase(),
386
- };
387
-
388
- if (status === "complete") {
389
- next.last_phase = phase;
390
- next.last_successful_phase = phase.toLowerCase();
391
- }
392
-
393
- await writeJsonAtomic(statePath, next);
394
- }
395
-
396
- /**
397
- * @param {string} runId
398
- * @param {string} slug
399
- * @returns {Promise<void>}
400
- */
401
- export async function syncToGstack(runId, slug) {
402
- const gstackRoot = join(getHomeDir(), ".gstack");
403
- if (!existsSync(gstackRoot)) {
404
- return;
405
- }
406
-
407
- const record = await readPhase(runId);
408
- if (!record) {
409
- return;
410
- }
411
-
412
- const phase = record.phase === "complete" ? record.lastPhase : record.phase;
413
- if (!phase) {
414
- return;
415
- }
416
-
417
- const checkpointsDir = join(gstackRoot, "projects", slug, "checkpoints");
418
- if (!existsSync(checkpointsDir)) {
419
- return;
420
- }
421
-
422
- const entries = await readdir(checkpointsDir, { withFileTypes: true });
423
- const markdownFiles = entries
424
- .filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
425
- .map((entry) => join(checkpointsDir, entry.name));
426
-
427
- if (markdownFiles.length === 0) {
428
- return;
429
- }
430
-
431
- const latestFile = (
432
- await Promise.all(
433
- markdownFiles.map(async (filePath) => ({
434
- filePath,
435
- stats: await stat(filePath),
436
- })),
437
- )
438
- ).sort((left, right) => right.stats.mtimeMs - left.stats.mtimeMs)[0]
439
- ?.filePath;
440
-
441
- if (!latestFile) {
442
- return;
443
- }
444
-
445
- const currentContent = await readFile(latestFile, "utf8");
446
- const nextContent = injectFrontmatterFields(currentContent, {
447
- phase,
448
- triflux_run_id: runId,
449
- });
450
-
451
- if (!nextContent || nextContent === currentContent) {
452
- return;
453
- }
454
-
455
- await writeTextAtomic(latestFile, nextContent);
456
- }
457
-
458
- /**
459
- * @param {unknown} text
460
- * @returns {LegacyPhase | null}
461
- */
462
- export function coerceLegacyPhase(text) {
463
- if (text == null) {
464
- return null;
465
- }
466
-
467
- const value = String(text).trim();
468
- if (!value) {
469
- return null;
470
- }
471
-
472
- if (/^complete$/i.test(value)) {
473
- return "complete";
474
- }
475
-
476
- if (/interview|research|phase1/i.test(value)) {
477
- return "Research";
478
- }
479
-
480
- if (/plan|strategy|phase2/i.test(value)) {
481
- return "Strategy";
482
- }
483
-
484
- if (/exec|ship|phase3|phase4/i.test(value)) {
485
- return "Execution";
486
- }
487
-
488
- if (/valid|qa|review|phase5/i.test(value)) {
489
- return "Validation";
490
- }
491
-
492
- return null;
493
- }