@triflux/core 10.50.1 → 10.51.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.
- package/hooks/agy-session-hook.mjs +11 -56
- package/hooks/codex-session-hook.mjs +11 -101
- package/hub/bridge.mjs +1 -79
- package/hub/cli-adapter-base.mjs +12 -90
- package/hub/codex-adapter.mjs +0 -1
- package/hub/codex-preflight.mjs +5 -28
- package/hub/index.mjs +3 -9
- package/hub/lib/cto-env.mjs +0 -23
- package/hub/lib/known-errors.json +2 -2
- package/hub/lib/process-utils.mjs +0 -123
- package/hub/lib/spawn-trace.mjs +3 -2
- package/hub/lib/timeout-defaults.mjs +0 -1
- package/hub/paths.mjs +0 -6
- package/hub/pipeline/index.mjs +0 -190
- package/hub/pipeline/transitions.mjs +0 -10
- package/hub/reflexion.mjs +1 -84
- package/hub/role-contract.mjs +0 -2
- package/hud/cli-policy.mjs +0 -26
- package/hud/colors.mjs +0 -13
- package/hud/constants.mjs +7 -153
- package/hud/context-monitor.mjs +3 -64
- package/hud/hud-qos-status.mjs +54 -264
- package/hud/providers/claude.mjs +4 -16
- package/hud/providers/gemini.mjs +83 -689
- package/hud/renderers.mjs +59 -367
- package/hud/terminal.mjs +1 -63
- package/hud/utils.mjs +2 -26
- package/package.json +1 -4
- package/scripts/lib/agent-route-policy.mjs +162 -14
- package/scripts/lib/claudemd-scanner.mjs +2 -199
- package/scripts/lib/cli-claude.mjs +0 -2
- package/scripts/lib/context.mjs +3 -19
- package/scripts/lib/env-probe.mjs +5 -64
- package/scripts/lib/gemini-profiles.mjs +2 -69
- package/scripts/lib/legacy-mcp-cleanup.mjs +14 -3
- package/scripts/lib/machine-profile.mjs +3 -17
- package/scripts/lib/mcp-filter.mjs +109 -53
- package/scripts/lib/mcp-health.mjs +2 -2
- package/scripts/lib/mcp-server-catalog.mjs +2 -2
- package/scripts/lib/psmux-info.mjs +25 -104
- package/scripts/lib/session-presence.mjs +1 -1
- package/scripts/lib/skill-template.mjs +0 -271
- package/hub/codex-compat.mjs +0 -12
- package/hub/dynamic-routing-engine.mjs +0 -511
- package/hub/fullcycle.mjs +0 -113
- package/hub/gemini-adapter.mjs +0 -174
- package/hub/intent.mjs +0 -321
- package/hub/lib/mcp-response-cache.mjs +0 -239
- package/hub/lib/path-utils.mjs +0 -165
- package/hub/lib/risk-tier.mjs +0 -53
- package/hub/lib/ssh-command.mjs +0 -231
- package/hub/lib/staged-file-detect.mjs +0 -43
- package/hub/lib/tfx-route-args.mjs +0 -353
- package/hub/lib/timeline-adapter.mjs +0 -190
- package/hub/pipeline/gates/confidence.mjs +0 -75
- package/hub/pipeline/gates/consensus.mjs +0 -106
- package/hub/pipeline/gates/index.mjs +0 -9
- package/hub/pipeline/gates/selfcheck.mjs +0 -120
- package/hub/quality/deslop.mjs +0 -274
- package/hub/research.mjs +0 -155
- package/hub/role-control-events.mjs +0 -113
- package/hub/routing/complexity.mjs +0 -269
- package/hub/routing/index.mjs +0 -122
- package/hub/routing/q-learning.mjs +0 -387
- package/hub/routing-snapshot.mjs +0 -261
- package/hub/token-mode.mjs +0 -228
- package/hud/mission-board.mjs +0 -56
- package/hud/providers/cto.mjs +0 -76
- package/mesh/index.mjs +0 -69
- package/mesh/mesh-budget.mjs +0 -137
- package/mesh/mesh-heartbeat.mjs +0 -100
- package/mesh/mesh-protocol.mjs +0 -96
- package/mesh/mesh-queue.mjs +0 -163
- package/mesh/mesh-registry.mjs +0 -78
- package/mesh/mesh-router.mjs +0 -77
- package/scripts/lib/agent-json.mjs +0 -27
- package/scripts/lib/async.mjs +0 -174
- package/scripts/lib/cli-agy.mjs +0 -85
- package/scripts/lib/cli-codex.mjs +0 -62
- package/scripts/lib/cli-gemini.mjs +0 -37
- package/scripts/lib/dynamic-route-cli.mjs +0 -107
- package/scripts/lib/env.mjs +0 -14
- package/scripts/lib/hub.mjs +0 -48
- package/scripts/lib/pid.mjs +0 -63
- package/scripts/lib/quota.mjs +0 -47
- package/scripts/lib/skill-state.mjs +0 -220
- package/scripts/lib/team.mjs +0 -75
- package/scripts/lib/timeout.mjs +0 -66
- package/scripts/lib/tmp.mjs +0 -26
- package/scripts/lib/toml.mjs +0 -132
|
@@ -1,511 +0,0 @@
|
|
|
1
|
-
// hub/dynamic-routing-engine.mjs
|
|
2
|
-
//
|
|
3
|
-
// PRD: .omx/plans/prd-dynamic-routing-engine.md
|
|
4
|
-
//
|
|
5
|
-
// Phase 0 — pure deterministic classifier. Takes a `request`, a `snapshot`
|
|
6
|
-
// (from `hub/routing-snapshot.mjs`), and a `policy` (loaded from
|
|
7
|
-
// `config/routing-policy.json`), then emits a single `RouteDecision`.
|
|
8
|
-
//
|
|
9
|
-
// Phase 0 contract:
|
|
10
|
-
// - no callers yet (dormant). conductor/swarm/tfx-route.sh integration is Phase 1+.
|
|
11
|
-
// - pure function: same (request, snapshot, policy) → same RouteDecision.
|
|
12
|
-
// - no network, no disk reads, no clock reads outside the explicit `now` arg.
|
|
13
|
-
// - fallback policy = codex-default. fails closed.
|
|
14
|
-
// - scenario evaluators are first-match-wins in policy.scenarios order.
|
|
15
|
-
|
|
16
|
-
import { createHash } from "node:crypto";
|
|
17
|
-
import { readFileSync } from "node:fs";
|
|
18
|
-
import { dirname, join } from "node:path";
|
|
19
|
-
import { fileURLToPath } from "node:url";
|
|
20
|
-
|
|
21
|
-
import { buildRoutingSnapshot } from "./routing-snapshot.mjs";
|
|
22
|
-
|
|
23
|
-
function stableStringify(obj) {
|
|
24
|
-
if (obj === null || typeof obj !== "object") return JSON.stringify(obj);
|
|
25
|
-
if (Array.isArray(obj)) return `[${obj.map(stableStringify).join(",")}]`;
|
|
26
|
-
const keys = Object.keys(obj).sort();
|
|
27
|
-
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`).join(",")}}`;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function hashInputs(request, snapshot) {
|
|
31
|
-
return createHash("sha256")
|
|
32
|
-
.update(stableStringify({ request, snapshot }))
|
|
33
|
-
.digest("hex")
|
|
34
|
-
.slice(0, 16);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function makeFallback(policy, trigger, inputsHash, now) {
|
|
38
|
-
const fb = policy?.fallback ?? {};
|
|
39
|
-
return {
|
|
40
|
-
decisionId: inputsHash,
|
|
41
|
-
mode: fb.default_mode ?? "codex-default",
|
|
42
|
-
scenario: "fallback",
|
|
43
|
-
lane: "single",
|
|
44
|
-
shards: [
|
|
45
|
-
{
|
|
46
|
-
cli: fb.default_cli ?? "codex",
|
|
47
|
-
accountId: null,
|
|
48
|
-
model: fb.default_model ?? "gpt-6.1-sol",
|
|
49
|
-
riskTier: "safe",
|
|
50
|
-
reason: trigger,
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
rationale: {
|
|
54
|
-
inputsHash,
|
|
55
|
-
biasScore: null,
|
|
56
|
-
alternatives: [],
|
|
57
|
-
},
|
|
58
|
-
fallback: { trigger, action: "codex-default" },
|
|
59
|
-
snapshotAgeMs: snapshotAge(undefined, now),
|
|
60
|
-
confidence: 50,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function snapshotAge(snapshot, now) {
|
|
65
|
-
if (!snapshot || typeof snapshot !== "object") return null;
|
|
66
|
-
const observed = Number(snapshot.observedAt);
|
|
67
|
-
if (!Number.isFinite(observed)) return null;
|
|
68
|
-
return Math.max(0, now - observed);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function checkFreshness(snapshot, freshness, now) {
|
|
72
|
-
if (!snapshot) return "hud_null";
|
|
73
|
-
const max = freshness?.max_snapshot_age_ms ?? 900_000;
|
|
74
|
-
const skewMax = freshness?.max_cross_provider_skew_ms ?? 300_000;
|
|
75
|
-
|
|
76
|
-
const claude = snapshot.claude;
|
|
77
|
-
const codex = snapshot.codex;
|
|
78
|
-
if (!claude && !codex) return "hud_null";
|
|
79
|
-
if (claude && (claude.isStale || claude.error))
|
|
80
|
-
return "claude_stale_or_error";
|
|
81
|
-
if (codex && (codex.isStale || codex.error)) return "codex_stale_or_error";
|
|
82
|
-
|
|
83
|
-
const claudeAge = claude?.observedAt ? now - claude.observedAt : null;
|
|
84
|
-
const codexAge = codex?.observedAt ? now - codex.observedAt : null;
|
|
85
|
-
if (claudeAge !== null && claudeAge > max) return "claude_age_exceeded";
|
|
86
|
-
if (codexAge !== null && codexAge > max) return "codex_age_exceeded";
|
|
87
|
-
if (claudeAge !== null && codexAge !== null) {
|
|
88
|
-
if (Math.abs(claudeAge - codexAge) > skewMax)
|
|
89
|
-
return "cross_provider_skew_exceeded";
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (claude?.weeklyResetsAt !== null && claude?.weeklyResetsAt !== undefined) {
|
|
93
|
-
if (
|
|
94
|
-
claude.weeklyResetsAt <
|
|
95
|
-
now - (freshness?.local_clock_skew_guard_ms ?? 60_000)
|
|
96
|
-
) {
|
|
97
|
-
return "claude_weekly_reset_in_past";
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function brokerAvailableCount(snapshot, provider) {
|
|
105
|
-
const accounts = snapshot?.broker?.[provider] ?? [];
|
|
106
|
-
return accounts.filter((a) => a.circuitState !== "open").length;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function brokerAllCircuitsOpen(snapshot) {
|
|
110
|
-
const groups = snapshot?.broker;
|
|
111
|
-
if (!groups) return false;
|
|
112
|
-
const all = [];
|
|
113
|
-
for (const provider of Object.keys(groups)) {
|
|
114
|
-
for (const acct of groups[provider] ?? []) all.push(acct);
|
|
115
|
-
}
|
|
116
|
-
if (all.length === 0) return true;
|
|
117
|
-
return all.every((a) => a.circuitState === "open");
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// ─── Scenario evaluators ──────────────────────────────────────────────
|
|
121
|
-
// Each returns either null (no match) or a partial RouteDecision payload
|
|
122
|
-
// `{ scenario, lane, shards, riskTier?, reasonCodes }` to be wrapped by
|
|
123
|
-
// `makeScenarioDecision()` below.
|
|
124
|
-
|
|
125
|
-
function evalS1(ctx) {
|
|
126
|
-
const { snapshot, scenario, policy, now } = ctx;
|
|
127
|
-
const claude = snapshot.claude;
|
|
128
|
-
if (!claude) return null;
|
|
129
|
-
const resetIn = (claude.weeklyResetsAt ?? Number.POSITIVE_INFINITY) - now;
|
|
130
|
-
if (
|
|
131
|
-
resetIn >
|
|
132
|
-
(scenario.when.weeklyResetsAtLteMs ?? policy.thresholds.reset_imminent_ms)
|
|
133
|
-
)
|
|
134
|
-
return null;
|
|
135
|
-
if ((claude.weeklyPercent ?? 100) >= (scenario.when.weeklyPercentLt ?? 100))
|
|
136
|
-
return null;
|
|
137
|
-
if (
|
|
138
|
-
scenario.when.hasMultipleAccounts &&
|
|
139
|
-
brokerAvailableCount(snapshot, "claude") < 2
|
|
140
|
-
)
|
|
141
|
-
return null;
|
|
142
|
-
return {
|
|
143
|
-
lane: scenario.then.lane ?? "multi",
|
|
144
|
-
shards: [
|
|
145
|
-
{ cli: "claude", riskTier: "stretch", reason: scenario.then.reason },
|
|
146
|
-
],
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function evalS2(ctx) {
|
|
151
|
-
const { snapshot, scenario, policy, now } = ctx;
|
|
152
|
-
const codex = snapshot.codex;
|
|
153
|
-
if (!codex) return null;
|
|
154
|
-
const buckets = codex.buckets ?? {};
|
|
155
|
-
const window =
|
|
156
|
-
scenario.when.weeklyResetsAtLteMs ?? policy.thresholds.reset_imminent_ms;
|
|
157
|
-
const usageCeiling = scenario.when.weeklyPercentLt ?? 100;
|
|
158
|
-
// Inspect both primary and secondary legs so a fresh secondary tier with
|
|
159
|
-
// imminent reset is not silently ignored when primary is exhausted.
|
|
160
|
-
const anyLegMatches = Object.values(buckets).some((b) =>
|
|
161
|
-
[b?.primary, b?.secondary].some((leg) => {
|
|
162
|
-
if (!leg) return false;
|
|
163
|
-
if (!leg.resetsAt) return false;
|
|
164
|
-
if (leg.resetsAt - now > window) return false;
|
|
165
|
-
return (leg.usedPercent ?? 100) < usageCeiling;
|
|
166
|
-
}),
|
|
167
|
-
);
|
|
168
|
-
if (!anyLegMatches) return null;
|
|
169
|
-
return {
|
|
170
|
-
lane: scenario.then.lane ?? "swarm",
|
|
171
|
-
shards: [
|
|
172
|
-
{ cli: "codex", riskTier: "stretch", reason: scenario.then.reason },
|
|
173
|
-
],
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function evalS3(ctx) {
|
|
178
|
-
const { snapshot, scenario, now } = ctx;
|
|
179
|
-
const claude = snapshot.claude;
|
|
180
|
-
const codex = snapshot.codex;
|
|
181
|
-
if (!claude || !codex) return null;
|
|
182
|
-
if ((claude.weeklyPercent ?? 0) < scenario.when.claude_weekly_percent_gte)
|
|
183
|
-
return null;
|
|
184
|
-
const codexAnyBucket = Object.values(codex.buckets ?? {}).some(
|
|
185
|
-
(b) =>
|
|
186
|
-
(b?.primary?.usedPercent ?? 100) < scenario.when.codex_weekly_percent_lt,
|
|
187
|
-
);
|
|
188
|
-
if (!codexAnyBucket) return null;
|
|
189
|
-
const resetIn = (claude.weeklyResetsAt ?? 0) - now;
|
|
190
|
-
if (resetIn <= scenario.when.claude_weekly_resets_at_gt_ms) return null;
|
|
191
|
-
return {
|
|
192
|
-
lane: scenario.then.lane ?? "single",
|
|
193
|
-
shards: [{ cli: "codex", riskTier: "safe", reason: scenario.then.reason }],
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function evalS4(ctx) {
|
|
198
|
-
const { snapshot, scenario } = ctx;
|
|
199
|
-
const claude = snapshot.broker?.claude ?? [];
|
|
200
|
-
if (claude.length < (scenario.when.accountCountGte ?? 2)) return null;
|
|
201
|
-
// Staggered = at least 2 distinct cooldown/expiresAt anchors.
|
|
202
|
-
const anchors = new Set(
|
|
203
|
-
claude.map((a) => `${a.cooldownUntil ?? 0}-${a.expiresAt ?? 0}`),
|
|
204
|
-
);
|
|
205
|
-
if (scenario.when.staggered && anchors.size < 2) return null;
|
|
206
|
-
return {
|
|
207
|
-
lane: scenario.then.lane ?? "multi",
|
|
208
|
-
shards: claude.map((a) => ({
|
|
209
|
-
cli: "claude",
|
|
210
|
-
accountId: a.id,
|
|
211
|
-
riskTier: "stretch",
|
|
212
|
-
reason: scenario.then.reason,
|
|
213
|
-
})),
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function evalS5(ctx) {
|
|
218
|
-
const { request, snapshot, scenario } = ctx;
|
|
219
|
-
if ((request?.team_size ?? 1) < (scenario.when.request_team_size_gte ?? 2))
|
|
220
|
-
return null;
|
|
221
|
-
const claude = snapshot.broker?.claude ?? [];
|
|
222
|
-
if (claude.length < (scenario.when.accountCountGte ?? 2)) return null;
|
|
223
|
-
const wanted = Math.min(request.team_size, claude.length);
|
|
224
|
-
return {
|
|
225
|
-
lane: scenario.then.lane ?? "swarm",
|
|
226
|
-
atomic: scenario.then.atomic,
|
|
227
|
-
atomicPolicy: scenario.then.atomic_policy ?? "balanced",
|
|
228
|
-
shards: claude.slice(0, wanted).map((a) => ({
|
|
229
|
-
cli: "claude",
|
|
230
|
-
accountId: a.id,
|
|
231
|
-
riskTier: "stretch",
|
|
232
|
-
reason: scenario.then.reason,
|
|
233
|
-
})),
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function evalS6(ctx) {
|
|
238
|
-
const { snapshot, scenario, now } = ctx;
|
|
239
|
-
const all = [];
|
|
240
|
-
for (const p of Object.keys(snapshot.broker ?? {})) {
|
|
241
|
-
const accounts = snapshot.broker[p];
|
|
242
|
-
if (!Array.isArray(accounts)) continue;
|
|
243
|
-
for (const a of accounts) all.push({ ...a, provider: p });
|
|
244
|
-
}
|
|
245
|
-
const window = scenario.when.expiresAtLteMs ?? 604_800_000;
|
|
246
|
-
const usageCeiling = scenario.when.weeklyPercentLt ?? null;
|
|
247
|
-
const expiring = all.filter((a) => {
|
|
248
|
-
if (!a.expiresAt) return false;
|
|
249
|
-
// Past-dated expirations are dead accounts, not drain targets.
|
|
250
|
-
if (a.expiresAt - now <= 0) return false;
|
|
251
|
-
if (a.expiresAt - now > window) return false;
|
|
252
|
-
if (scenario.when.circuitStateNotOpen && a.circuitState === "open")
|
|
253
|
-
return false;
|
|
254
|
-
if (usageCeiling !== null) {
|
|
255
|
-
const usage = providerUsagePercent(snapshot, a.provider);
|
|
256
|
-
if (usage !== null && usage >= usageCeiling) return false;
|
|
257
|
-
}
|
|
258
|
-
return true;
|
|
259
|
-
});
|
|
260
|
-
if (expiring.length === 0) return null;
|
|
261
|
-
return {
|
|
262
|
-
lane: scenario.then.lane ?? "single",
|
|
263
|
-
riskTier: scenario.then.riskTier ?? "drain",
|
|
264
|
-
shards: expiring.map((a) => ({
|
|
265
|
-
cli: a.provider === "claude" ? "claude" : "codex",
|
|
266
|
-
accountId: a.id,
|
|
267
|
-
riskTier: "drain",
|
|
268
|
-
reason: scenario.then.reason,
|
|
269
|
-
})),
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function providerUsagePercent(snapshot, provider) {
|
|
274
|
-
if (provider === "claude") {
|
|
275
|
-
return typeof snapshot.claude?.weeklyPercent === "number"
|
|
276
|
-
? snapshot.claude.weeklyPercent
|
|
277
|
-
: null;
|
|
278
|
-
}
|
|
279
|
-
if (provider === "codex") {
|
|
280
|
-
const buckets = Object.values(snapshot.codex?.buckets ?? {});
|
|
281
|
-
if (buckets.length === 0) return null;
|
|
282
|
-
let max = 0;
|
|
283
|
-
for (const b of buckets) {
|
|
284
|
-
for (const leg of [b?.primary, b?.secondary]) {
|
|
285
|
-
if (typeof leg?.usedPercent === "number" && leg.usedPercent > max) {
|
|
286
|
-
max = leg.usedPercent;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return max;
|
|
291
|
-
}
|
|
292
|
-
return null;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const SCENARIO_EVALUATORS = {
|
|
296
|
-
S1: evalS1,
|
|
297
|
-
S2: evalS2,
|
|
298
|
-
S3: evalS3,
|
|
299
|
-
S4: evalS4,
|
|
300
|
-
S5: evalS5,
|
|
301
|
-
S6: evalS6,
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
function makeScenarioDecision({ match, scenario, inputsHash, snapshot, now }) {
|
|
305
|
-
return {
|
|
306
|
-
decisionId: inputsHash,
|
|
307
|
-
mode: "dynamic-overlay",
|
|
308
|
-
scenario: scenario.id,
|
|
309
|
-
lane: match.lane,
|
|
310
|
-
shards: match.shards,
|
|
311
|
-
atomic: match.atomic ?? null,
|
|
312
|
-
atomicPolicy: match.atomicPolicy ?? null,
|
|
313
|
-
rationale: {
|
|
314
|
-
inputsHash,
|
|
315
|
-
biasScore: null,
|
|
316
|
-
alternatives: [],
|
|
317
|
-
scenarioName: scenario.name,
|
|
318
|
-
},
|
|
319
|
-
fallback: { trigger: null, action: "codex-default" },
|
|
320
|
-
snapshotAgeMs: snapshotAge(snapshot, now),
|
|
321
|
-
confidence: 72,
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
function brokerIsEmpty(snapshot) {
|
|
326
|
-
const groups = snapshot?.broker;
|
|
327
|
-
if (!groups || typeof groups !== "object") return true;
|
|
328
|
-
for (const p of Object.keys(groups)) {
|
|
329
|
-
const accounts = groups[p];
|
|
330
|
-
if (Array.isArray(accounts) && accounts.length > 0) return false;
|
|
331
|
-
}
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* @param {object} request
|
|
337
|
-
* @param {object} snapshot
|
|
338
|
-
* @param {object} policy
|
|
339
|
-
* @param {object} options
|
|
340
|
-
* @param {number} options.now — REQUIRED. Pure-fn contract: callers must
|
|
341
|
-
* supply the current epoch; the engine itself never reads the clock.
|
|
342
|
-
*/
|
|
343
|
-
export function evaluateDynamicRoute(request, snapshot, policy, options) {
|
|
344
|
-
if (!options || typeof options.now !== "number") {
|
|
345
|
-
throw new TypeError(
|
|
346
|
-
"evaluateDynamicRoute: options.now (epoch ms) is required for pure-fn contract",
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
const { now } = options;
|
|
350
|
-
const inputsHash = hashInputs(request, snapshot);
|
|
351
|
-
|
|
352
|
-
if (!policy || !Array.isArray(policy.scenarios)) {
|
|
353
|
-
return makeFallback(
|
|
354
|
-
{ fallback: { default_mode: "codex-default" } },
|
|
355
|
-
"policy_missing",
|
|
356
|
-
inputsHash,
|
|
357
|
-
now,
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
const freshnessVeto = checkFreshness(snapshot, policy.freshness, now);
|
|
362
|
-
if (freshnessVeto)
|
|
363
|
-
return makeFallback(policy, freshnessVeto, inputsHash, now);
|
|
364
|
-
|
|
365
|
-
if (brokerIsEmpty(snapshot)) {
|
|
366
|
-
return makeFallback(policy, "broker_empty", inputsHash, now);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
if (brokerAllCircuitsOpen(snapshot)) {
|
|
370
|
-
return makeFallback(policy, "all_circuits_open", inputsHash, now);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
for (const scenario of policy.scenarios) {
|
|
374
|
-
const evaluator = SCENARIO_EVALUATORS[scenario.id];
|
|
375
|
-
if (!evaluator) continue;
|
|
376
|
-
const match = evaluator({
|
|
377
|
-
request: request ?? {},
|
|
378
|
-
snapshot,
|
|
379
|
-
scenario,
|
|
380
|
-
policy,
|
|
381
|
-
now,
|
|
382
|
-
});
|
|
383
|
-
if (match)
|
|
384
|
-
return makeScenarioDecision({
|
|
385
|
-
match,
|
|
386
|
-
scenario,
|
|
387
|
-
inputsHash,
|
|
388
|
-
snapshot,
|
|
389
|
-
now,
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
return makeFallback(policy, "no_scenario_matched", inputsHash, now);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// ─── Phase 1 — caller-friendly facade ─────────────────────────────────
|
|
397
|
-
//
|
|
398
|
-
// Phase 0 keeps `evaluateDynamicRoute()` pure. Callers (conductor,
|
|
399
|
-
// swarm-hypervisor, tfx-route.sh) would otherwise duplicate the same
|
|
400
|
-
// plumbing: snapshot build + policy load + clock read. The facade below
|
|
401
|
-
// folds that plumbing into a single factory with opt-in env gating.
|
|
402
|
-
//
|
|
403
|
-
// Contract:
|
|
404
|
-
// - `evaluateDynamicRoute()` pure-fn signature is preserved unchanged.
|
|
405
|
-
// - `createDynamicRouter()` returns a router whose `routeRequest()` is
|
|
406
|
-
// a `null-router` (always emits the fallback decision) unless the
|
|
407
|
-
// opt-in `TRIFLUX_DYNAMIC_ROUTING` env flag is set.
|
|
408
|
-
// - Callers can inject `policy`, `snapshot`, `snapshotProvider`, `nowFn`
|
|
409
|
-
// for deterministic testing without touching disk or HUD providers.
|
|
410
|
-
|
|
411
|
-
const DEFAULT_POLICY_PATH = join(
|
|
412
|
-
dirname(fileURLToPath(import.meta.url)),
|
|
413
|
-
"..",
|
|
414
|
-
"config",
|
|
415
|
-
"routing-policy.json",
|
|
416
|
-
);
|
|
417
|
-
|
|
418
|
-
export function isDynamicRoutingEnabled(env = process.env) {
|
|
419
|
-
const flag = env.TRIFLUX_DYNAMIC_ROUTING;
|
|
420
|
-
return flag === "1" || flag === "true";
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
function loadPolicy(policyPath = DEFAULT_POLICY_PATH) {
|
|
424
|
-
// Missing/invalid policy → null. evaluateDynamicRoute() will then fall
|
|
425
|
-
// back to codex-default, which is the documented fail-closed behaviour.
|
|
426
|
-
try {
|
|
427
|
-
const raw = readFileSync(policyPath, "utf8");
|
|
428
|
-
return JSON.parse(raw);
|
|
429
|
-
} catch {
|
|
430
|
-
return null;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Caller-friendly factory. Works with zero arguments — defaults to
|
|
436
|
-
* `process.env`, the production `routing-policy.json`, and a snapshot
|
|
437
|
-
* provider that calls `buildRoutingSnapshot()`. Opt-in env-gated:
|
|
438
|
-
* `TRIFLUX_DYNAMIC_ROUTING=1|true` enables scenario evaluation; anything
|
|
439
|
-
* else returns a null-router that always emits the fallback decision.
|
|
440
|
-
*
|
|
441
|
-
* @param {object} [opts]
|
|
442
|
-
* @param {string} [opts.policyPath] — override the policy file path.
|
|
443
|
-
* @param {object} [opts.policy] — inject a policy object directly (testing).
|
|
444
|
-
* @param {(args:{forceRefresh:boolean, now:number}) => Promise<object>} [opts.snapshotProvider]
|
|
445
|
-
* @param {NodeJS.ProcessEnv} [opts.env] — override env (testing).
|
|
446
|
-
* @param {() => number} [opts.nowFn] — override clock (testing).
|
|
447
|
-
*/
|
|
448
|
-
export function createDynamicRouter(opts = {}) {
|
|
449
|
-
const env = opts.env || process.env;
|
|
450
|
-
const enabled = isDynamicRoutingEnabled(env);
|
|
451
|
-
const policy =
|
|
452
|
-
opts.policy !== undefined
|
|
453
|
-
? opts.policy
|
|
454
|
-
: enabled
|
|
455
|
-
? loadPolicy(opts.policyPath)
|
|
456
|
-
: null;
|
|
457
|
-
const snapshotProvider =
|
|
458
|
-
opts.snapshotProvider ||
|
|
459
|
-
(async ({ forceRefresh, now }) =>
|
|
460
|
-
buildRoutingSnapshot({ forceRefresh, now }));
|
|
461
|
-
const nowFn = opts.nowFn || (() => Date.now());
|
|
462
|
-
|
|
463
|
-
return {
|
|
464
|
-
enabled,
|
|
465
|
-
/**
|
|
466
|
-
* @param {object} request
|
|
467
|
-
* @param {{ snapshot?: object, now?: number, forceRefresh?: boolean }} [callOpts]
|
|
468
|
-
* @returns {Promise<object>} RouteDecision
|
|
469
|
-
*/
|
|
470
|
-
async routeRequest(request, callOpts = {}) {
|
|
471
|
-
const now = typeof callOpts.now === "number" ? callOpts.now : nowFn();
|
|
472
|
-
if (!enabled) {
|
|
473
|
-
// null-router: opt-in env absent → always fallback decision.
|
|
474
|
-
return evaluateDynamicRoute(request, {}, null, { now });
|
|
475
|
-
}
|
|
476
|
-
const snapshot =
|
|
477
|
-
callOpts.snapshot !== undefined
|
|
478
|
-
? callOpts.snapshot
|
|
479
|
-
: await snapshotProvider({
|
|
480
|
-
forceRefresh: Boolean(callOpts.forceRefresh),
|
|
481
|
-
now,
|
|
482
|
-
});
|
|
483
|
-
return evaluateDynamicRoute(request, snapshot, policy, { now });
|
|
484
|
-
},
|
|
485
|
-
getPolicy() {
|
|
486
|
-
return policy;
|
|
487
|
-
},
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
/**
|
|
492
|
-
* Top-level convenience. Equivalent to
|
|
493
|
-
* `createDynamicRouter(opts).routeRequest(request, opts)`.
|
|
494
|
-
*/
|
|
495
|
-
export async function routeRequest(request, opts = {}) {
|
|
496
|
-
const router = createDynamicRouter(opts);
|
|
497
|
-
return router.routeRequest(request, opts);
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
export const __internal__ = {
|
|
501
|
-
hashInputs,
|
|
502
|
-
checkFreshness,
|
|
503
|
-
brokerAvailableCount,
|
|
504
|
-
brokerAllCircuitsOpen,
|
|
505
|
-
brokerIsEmpty,
|
|
506
|
-
providerUsagePercent,
|
|
507
|
-
SCENARIO_EVALUATORS,
|
|
508
|
-
loadPolicy,
|
|
509
|
-
isDynamicRoutingEnabled,
|
|
510
|
-
DEFAULT_POLICY_PATH,
|
|
511
|
-
};
|
package/hub/fullcycle.mjs
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// hub/fullcycle.mjs — tfx-fullcycle runtime artifact/state helpers
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
existsSync,
|
|
5
|
-
mkdirSync,
|
|
6
|
-
readdirSync,
|
|
7
|
-
readFileSync,
|
|
8
|
-
statSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
} from "node:fs";
|
|
11
|
-
import { join, resolve } from "node:path";
|
|
12
|
-
import { ensureTfxDirs, TFX_FULLCYCLE_DIR, TFX_PLANS_DIR } from "./paths.mjs";
|
|
13
|
-
|
|
14
|
-
function safeResolve(baseDir, relativePath) {
|
|
15
|
-
const base = resolve(baseDir);
|
|
16
|
-
const target = resolve(join(baseDir, relativePath));
|
|
17
|
-
if (!target.startsWith(base)) {
|
|
18
|
-
throw new Error("Invalid fullcycle path: path traversal detected");
|
|
19
|
-
}
|
|
20
|
-
return target;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** @experimental 런타임 미연결 — 향후 통합 예정 */
|
|
24
|
-
export function createFullcycleRunId(now = new Date()) {
|
|
25
|
-
return now
|
|
26
|
-
.toISOString()
|
|
27
|
-
.replace(/[:.]/g, "-")
|
|
28
|
-
.replace("T", "_")
|
|
29
|
-
.replace("Z", "Z");
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function getFullcycleRunDir(runId, baseDir = process.cwd()) {
|
|
33
|
-
return safeResolve(baseDir, join(TFX_FULLCYCLE_DIR, runId));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function ensureFullcycleRunDir(runId, baseDir = process.cwd()) {
|
|
37
|
-
ensureTfxDirs(baseDir);
|
|
38
|
-
const dir = getFullcycleRunDir(runId, baseDir);
|
|
39
|
-
mkdirSync(dir, { recursive: true });
|
|
40
|
-
return dir;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function saveFullcycleArtifact(
|
|
44
|
-
runId,
|
|
45
|
-
filename,
|
|
46
|
-
content,
|
|
47
|
-
baseDir = process.cwd(),
|
|
48
|
-
) {
|
|
49
|
-
if (!filename || typeof filename !== "string") {
|
|
50
|
-
throw new Error("Artifact filename is required");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const dir = ensureFullcycleRunDir(runId, baseDir);
|
|
54
|
-
const path = safeResolve(dir, filename);
|
|
55
|
-
writeFileSync(path, content, "utf8");
|
|
56
|
-
return path;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function readFullcycleArtifact(
|
|
60
|
-
runId,
|
|
61
|
-
filename,
|
|
62
|
-
baseDir = process.cwd(),
|
|
63
|
-
) {
|
|
64
|
-
const dir = getFullcycleRunDir(runId, baseDir);
|
|
65
|
-
const path = safeResolve(dir, filename);
|
|
66
|
-
if (!existsSync(path)) return null;
|
|
67
|
-
return readFileSync(path, "utf8");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function writeFullcycleState(runId, state, baseDir = process.cwd()) {
|
|
71
|
-
const payload = typeof state === "object" && state !== null ? state : {};
|
|
72
|
-
const serialized = JSON.stringify(payload, null, 2);
|
|
73
|
-
return saveFullcycleArtifact(runId, "state.json", serialized, baseDir);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function readFullcycleState(runId, baseDir = process.cwd()) {
|
|
77
|
-
const content = readFullcycleArtifact(runId, "state.json", baseDir);
|
|
78
|
-
if (!content) return null;
|
|
79
|
-
try {
|
|
80
|
-
return JSON.parse(content);
|
|
81
|
-
} catch {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function findLatestInterviewPlan(baseDir = process.cwd()) {
|
|
87
|
-
const plansDir = safeResolve(baseDir, TFX_PLANS_DIR);
|
|
88
|
-
if (!existsSync(plansDir)) return null;
|
|
89
|
-
|
|
90
|
-
const candidates = readdirSync(plansDir)
|
|
91
|
-
.filter((name) => /^interview-.*\.md$/i.test(name))
|
|
92
|
-
.map((name) => {
|
|
93
|
-
const path = join(plansDir, name);
|
|
94
|
-
const stats = statSync(path);
|
|
95
|
-
return { name, path, mtimeMs: stats.mtimeMs };
|
|
96
|
-
})
|
|
97
|
-
.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
98
|
-
|
|
99
|
-
return candidates[0]?.path || null;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function shouldStopQaLoop(failureHistory = [], maxRepeats = 3) {
|
|
103
|
-
if (!Array.isArray(failureHistory) || maxRepeats <= 1) return false;
|
|
104
|
-
|
|
105
|
-
const normalized = failureHistory
|
|
106
|
-
.map((entry) => String(entry ?? "").trim())
|
|
107
|
-
.filter(Boolean);
|
|
108
|
-
|
|
109
|
-
if (normalized.length < maxRepeats) return false;
|
|
110
|
-
const target = normalized.at(-1);
|
|
111
|
-
const tail = normalized.slice(-maxRepeats);
|
|
112
|
-
return tail.every((entry) => entry === target);
|
|
113
|
-
}
|