ruvnet-brain 4.5.4 → 4.5.6

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 (58) hide show
  1. package/README.md +2 -2
  2. package/bin/install.mjs +162 -27
  3. package/config/model-router/catalog.template.json +126 -52
  4. package/config/model-router/claude-terminal-mod/.claude-plugin/plugin.json +1 -0
  5. package/config/model-router/claude-terminal-mod/README.md +22 -0
  6. package/config/model-router/claude-terminal-mod/hooks/hooks.json +1 -0
  7. package/config/model-router/claude-terminal-mod/hooks/policy.default.mjs +2 -0
  8. package/config/model-router/claude-terminal-mod/hooks/register.js +66 -0
  9. package/config/model-router/claude-terminal-mod/hooks/routing.js +55 -0
  10. package/config/model-router/claude-terminal-mod/hooks/runtime.js +2 -0
  11. package/config/model-router/claude-terminal-mod/tests/native.test.ts +86 -0
  12. package/config/model-router/policy.default.mjs +97 -75
  13. package/config/model-router/qualification-contract.json +124 -0
  14. package/config/model-router/routing-eval-cases.json +275 -0
  15. package/config/model-router/routing-policy.template.json +76 -0
  16. package/config/model-router/weekly-analyst-instruction.md +60 -0
  17. package/data/model-catalog.json +44 -49
  18. package/package.json +6 -3
  19. package/plugin/.claude-plugin/plugin.json +1 -1
  20. package/plugin/.codex-plugin/plugin.json +1 -1
  21. package/plugin/mcp/managed-cli-interface.mjs +9 -4
  22. package/plugin/scripts/capability-claim-evidence.mjs +9 -2
  23. package/plugin/scripts/codex-hook-adapter.mjs +18 -9
  24. package/plugin/scripts/project-capture-queue.mjs +7 -1
  25. package/plugin/scripts/project-progression-hook.mjs +16 -7
  26. package/plugin/scripts/project-progression-outbox.mjs +96 -15
  27. package/plugin/scripts/project-progression-producer.mjs +12 -1
  28. package/plugin/scripts/project-progression-store.mjs +53 -1
  29. package/plugin/scripts/project-transition-hook.mjs +19 -8
  30. package/plugin/scripts/session-snapshot-hook.mjs +2 -1
  31. package/scripts/claude-terminal-mod.mjs +89 -0
  32. package/scripts/codex-hook-trust-reconcile.mjs +247 -0
  33. package/scripts/codex-routed.sh +3 -36
  34. package/scripts/goldie-weekly.sh +8 -64
  35. package/scripts/metaharness-router.mjs +7 -1
  36. package/scripts/model-analyst-sandbox.mjs +54 -0
  37. package/scripts/model-currency-evidence.mjs +139 -0
  38. package/scripts/model-currency.mjs +230 -0
  39. package/scripts/model-native-catalog.mjs +111 -0
  40. package/scripts/model-native-qualification.mjs +251 -0
  41. package/scripts/model-router-agent-hook.mjs +136 -0
  42. package/scripts/model-router-dispatch.mjs +161 -0
  43. package/scripts/model-router-engine.mjs +155 -104
  44. package/scripts/model-routing-eval.mjs +108 -0
  45. package/scripts/model-routing-gateway.mjs +453 -0
  46. package/scripts/model-routing-launchers.mjs +209 -0
  47. package/scripts/model-routing-policy-promotion.mjs +203 -0
  48. package/scripts/model-terminal-gateway.mjs +310 -0
  49. package/scripts/model-terminal-launchers.mjs +300 -0
  50. package/scripts/model-weekly-analyst.mjs +299 -0
  51. package/scripts/model-weekly-assessment.mjs +91 -0
  52. package/scripts/model-weekly-cycle.mjs +183 -0
  53. package/scripts/model-weekly-qualification.mjs +362 -0
  54. package/scripts/native-subscription-usage.mjs +57 -0
  55. package/scripts/release-qualification-contract.mjs +50 -1
  56. package/scripts/release-qualification.mjs +9 -6
  57. package/scripts/security-guidance-codex-compat.mjs +142 -0
  58. package/scripts/user-model-prompt-hook.mjs +69 -0
@@ -0,0 +1,139 @@
1
+ // DISTINCT-FROM: scripts/refresh-model-catalog.mjs — independent effort-specific evaluation evidence, never router authority.
2
+ import { createHash } from 'node:crypto';
3
+
4
+ export const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
5
+ export const digest = (bytes) => createHash('sha256').update(bytes).digest('hex');
6
+ const finite = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0;
7
+
8
+ /** Captured AA Next Flight contract. No eval/script execution; a changed schema fails closed. */
9
+ export function parseArtificialAnalysis(html, { url, checkedAt, identityBindings = {} } = {}) {
10
+ const version = html.match(/Artificial Analysis Intelligence Index v(\d+\.\d+(?:\.\d+)?)/)?.[1];
11
+ if (!version) throw new Error('AA Intelligence Index version is absent');
12
+ const rows = new Map();
13
+ const walk = (value) => {
14
+ if (Array.isArray(value)) { for (const item of value) walk(item); return; }
15
+ if (!value || typeof value !== 'object') return;
16
+ if (Array.isArray(value.intelligenceIndexEvaluations) && typeof value.id === 'string') {
17
+ rows.set(value.id, value); return;
18
+ }
19
+ for (const child of Object.values(value)) walk(child);
20
+ };
21
+ for (const match of html.matchAll(/self\.__next_f\.push\((.*?)\)<\/script>/gs)) {
22
+ try {
23
+ const envelope = JSON.parse(match[1]);
24
+ if (typeof envelope[1] !== 'string' || !envelope[1].includes('intelligenceIndexEvaluations')) continue;
25
+ const line = envelope[1];
26
+ walk(JSON.parse(line.slice(line.indexOf(':') + 1)));
27
+ } catch { /* unrelated or chunked flight frame; required rows checked below */ }
28
+ }
29
+ const source = { url, checkedAt, sha256: digest(html), parser: 'aa-next-flight-v1' };
30
+ const records = [];
31
+ for (const row of rows.values()) {
32
+ const effortLabel = typeof row.suffix === 'string' ? row.suffix : row.name?.match(/\((Low|Medium|High|Xhigh|Max|Minimal|None)(?:,|\))/i)?.[1];
33
+ const effort = effortLabel?.split(',')[0]?.trim().toLowerCase();
34
+ if (!['low', 'medium', 'high', 'xhigh', 'max', 'minimal', 'none'].includes(effort)) continue;
35
+ if (!row.release?.slug || !finite(row.intelligenceIndex) || row.intelligenceIndexIsEstimated !== false) continue;
36
+ const benchmarks = row.intelligenceIndexEvaluations.filter((b) => typeof b.slug === 'string'
37
+ && Number.isFinite(b.score) && finite(b.costPerTask) && finite(b.timePerTask))
38
+ .map((b) => ({ suite: b.slug, score: b.score, costUsd: b.costPerTask, timeSeconds: b.timePerTask }));
39
+ const binding = identityBindings[row.release.slug];
40
+ if (binding && (typeof binding.model !== 'string' || !binding.evidence || !binding.checkedAt)) {
41
+ throw new Error(`Invalid native identity binding: ${row.release.slug}`);
42
+ }
43
+ records.push({
44
+ sourceModelId: row.id, sourceReleaseSlug: row.release.slug, sourceName: row.name,
45
+ model: binding?.model ?? null, identityEvidence: binding ?? null, effort,
46
+ benchmark: { suite: 'artificial-analysis-intelligence-index', version },
47
+ quality: { intelligenceIndex: row.intelligenceIndex, estimated: false }, benchmarks,
48
+ costPerTaskUsd: finite(row.intelligenceIndexCostPerTask?.cost?.total) ? row.intelligenceIndexCostPerTask.cost.total : null,
49
+ timePerTaskSeconds: finite(row.intelligenceIndexTimePerTask) ? row.intelligenceIndexTimePerTask : null,
50
+ speedTokensPerSecond: finite(row.outputSpeedVariance?.median ?? row.medianOutputSpeed) ? (row.outputSpeedVariance?.median ?? row.medianOutputSpeed) : null,
51
+ inputUsdPerMillion: finite(row.price1mInputTokens) ? row.price1mInputTokens : null,
52
+ outputUsdPerMillion: finite(row.price1mOutputTokens) ? row.price1mOutputTokens : null,
53
+ source,
54
+ });
55
+ }
56
+ if (records.length === 0 || !records.some((r) => r.benchmarks.length > 0 && r.costPerTaskUsd !== null && r.timePerTaskSeconds !== null)) {
57
+ throw new Error('AA effort benchmark matrix absent or incomplete');
58
+ }
59
+ return { source, records };
60
+ }
61
+
62
+ export function parseInventory(body, source) {
63
+ const data = JSON.parse(body).data;
64
+ if (!Array.isArray(data) || data.length < 50) throw new Error('OpenRouter inventory is suspiciously thin');
65
+ const models = data.map((row) => {
66
+ const input = Number(row.pricing?.prompt); const output = Number(row.pricing?.completion);
67
+ if (typeof row.id !== 'string' || !row.id || !Number.isFinite(input) || !Number.isFinite(output)) throw new Error('Invalid OpenRouter model/pricing');
68
+ return { id: row.id, pricing: { inputUsdPerMillion: finite(input) ? input * 1e6 : null, outputUsdPerMillion: finite(output) ? output * 1e6 : null },
69
+ supportedParameters: Array.isArray(row.supported_parameters) ? row.supported_parameters.filter((p) => typeof p === 'string') : [] };
70
+ });
71
+ return { checkedAt: source.checkedAt, source, models, discoveryOnly: true };
72
+ }
73
+
74
+ /** No benchmark row can certify native access, supported effort, or default-selection eligibility. */
75
+ export function currencyStatus(record, now = Date.now()) {
76
+ const fresh = (section) => {
77
+ const checked = Date.parse(section?.checkedAt);
78
+ return Number.isFinite(checked) && checked <= now && now - checked < WEEK_MS;
79
+ };
80
+ const inventoryFresh = record?.schemaVersion === 1 && fresh(record.inventory)
81
+ && record.inventory.discoveryOnly === true && record.inventory.models?.length >= 50;
82
+ const evaluationsFresh = record?.schemaVersion === 1 && fresh(record.evaluations)
83
+ && record.evaluations.sources?.length > 0 && record.evaluations.records?.length > 0
84
+ && record.evaluations.sources.every((source) => fresh(source) && /^[a-f0-9]{64}$/.test(source.sha256 ?? ''));
85
+ const failed = !!record?.lastAttempt && record.lastAttempt.status !== 'complete';
86
+ return { status: inventoryFresh && evaluationsFresh && !failed ? 'current' : 'stale', inventoryFresh: !!inventoryFresh,
87
+ evaluationsFresh: !!evaluationsFresh, selectionQualified: false, maxAgeMs: WEEK_MS,
88
+ errors: record?.lastAttempt?.errors ?? [], assessment: record?.assessment ?? null, reason: failed ? 'last refresh incomplete' : inventoryFresh && evaluationsFresh ? 'fresh independent evidence' : 'weekly evidence required' };
89
+ }
90
+
91
+ /** Coding-agent workloads are a separate suite and harness/configuration, never model-index aliases. */
92
+ export function parseCodingAgentEvidence(html, methodology, { url, checkedAt, identityBindings = {} } = {}) {
93
+ const version = methodology.match(/Coding Agent Index v(\d+\.\d+(?:\.\d+)?)/)?.[1];
94
+ if (!version) throw new Error('Coding Agent Index methodology version absent');
95
+ const rows = new Map();
96
+ const walk = (value) => {
97
+ if (Array.isArray(value)) { for (const child of value) walk(child); return; }
98
+ if (!value || typeof value !== 'object') return;
99
+ if (typeof value.hostModelSlug === 'string' && typeof value.id === 'string' && value.indexScore !== undefined) { rows.set(value.id, value); return; }
100
+ for (const child of Object.values(value)) walk(child);
101
+ };
102
+ for (const match of html.matchAll(/self\.__next_f\.push\((.*?)\)<\/script>/gs)) {
103
+ try {
104
+ const envelope = JSON.parse(match[1]);
105
+ if (typeof envelope[1] !== 'string') continue;
106
+ for (const line of envelope[1].split('\n')) {
107
+ if (!line.includes('hostModelSlug')) continue;
108
+ try { walk(JSON.parse(line.slice(line.indexOf(':') + 1))); } catch { /* unrelated frame */ }
109
+ }
110
+ } catch { /* required contract checked below */ }
111
+ }
112
+ const source = { url, checkedAt, sha256: digest(html), parser: 'aa-coding-agents-flight-v1' };
113
+ const records = [];
114
+ for (const row of rows.values()) {
115
+ if (!finite(row.indexScore) || row.indexScore > 1 || !finite(row.mean?.costUsd) || !finite(row.mean?.agentWallTimeSec)
116
+ || !row.display?.model || !Array.isArray(row.evals) || row.evals.length !== row.indexComponentCount || !row.versions
117
+ || row.evals.some((e) => !e.datasetIndexName || !finite(e.mean?.reward))) {
118
+ throw new Error(`Incomplete coding-agent record: ${row.id}`);
119
+ }
120
+ const nativeHost = row.agentName === 'Codex' && row.provider === 'openai' ? 'codex'
121
+ : row.agentName === 'Claude Code' && row.provider === 'anthropic' ? 'claude-code' : null;
122
+ const binding = nativeHost ? identityBindings[row.modelRelease?.slug] : null;
123
+ if (binding && (!binding.model || !binding.evidence || !binding.checkedAt)) throw new Error('Invalid coding-agent native identity binding');
124
+ const effort = nativeHost ? row.display.model.match(/\((low|medium|high|xhigh|max|minimal|none)\)/i)?.[1]?.toLowerCase() ?? null : null;
125
+ records.push({ sourceId: row.id, harness: row.agentName, provider: row.provider, nativeHost,
126
+ sourceModelSlug: row.hostModelSlug, sourceReleaseSlug: row.modelRelease?.slug ?? null,
127
+ displayModel: row.display.model, configurationLabel: row.displayLabel, effort,
128
+ model: binding?.model ?? null, identityEvidence: binding ?? null,
129
+ fallback: /fallback/i.test(row.displayLabel ?? row.display.model),
130
+ benchmark: { suite: 'artificial-analysis-coding-agent-index', version }, codingAgentIndexFraction: row.indexScore,
131
+ apiBenchmarkCostPerTaskUsd: row.mean.costUsd, timePerTaskSeconds: row.mean.agentWallTimeSec,
132
+ versions: row.versions, components: row.evals.map((e) => ({ suite: e.datasetIndexName,
133
+ dataset: e.refDatasetName, score: e.mean.reward, mean: e.mean })), source,
134
+ selectionQualified: false });
135
+ }
136
+ if (!records.length) throw new Error('Coding-agent structured records absent');
137
+ return { source, records, coverage: { scope: 'public serialized agent/configuration records; native entitlements unverified',
138
+ recordCount: records.length, renderedChartSelectionVerified: false }, selectionQualified: false };
139
+ }
@@ -0,0 +1,230 @@
1
+ #!/usr/bin/env node
2
+ // DISTINCT-FROM: scripts/model-router-catalog.mjs — per-user evidence currency, never candidate/default mutation.
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import os from 'node:os';
6
+ import { spawn } from 'node:child_process';
7
+ import { randomUUID } from 'node:crypto';
8
+ import { buildWeeklyAssessment } from './model-weekly-assessment.mjs';
9
+ import { fileURLToPath } from 'node:url';
10
+ import { WEEK_MS, digest, parseInventory, parseArtificialAnalysis, parseCodingAgentEvidence, currencyStatus } from './model-currency-evidence.mjs';
11
+
12
+ export const DEFAULT_ROUTER_DIR = path.join(os.homedir(), '.claude', 'model-router');
13
+ export const AA_URLS = [
14
+ 'https://artificialanalysis.ai/models/comparisons',
15
+ 'https://artificialanalysis.ai/models/comparisons/claude-fable-5-1-medium-vs-claude-opus-5-medium',
16
+ 'https://artificialanalysis.ai/models/releases/comparisons/gpt-6-1-sol-vs-claude-sonnet-5-5',
17
+ 'https://artificialanalysis.ai/models/releases/comparisons/gpt-6-luna-vs-gpt-6-astra',
18
+ 'https://artificialanalysis.ai/models/releases/comparisons/claude-opus-5-5-vs-gpt-6-astra',
19
+ ];
20
+ export const OFFICIAL_SOURCES = [
21
+ { provider: 'openai', url: 'https://developers.openai.com/api/docs/models' },
22
+ { provider: 'anthropic', url: 'https://platform.claude.com/docs/en/models/overview' },
23
+ ];
24
+ export const AGENT_SOURCE_URLS = [
25
+ 'https://artificialanalysis.ai/agents/coding-agents',
26
+ 'https://artificialanalysis.ai/methodology/coding-agents-benchmarking',
27
+ ];
28
+ export const CODING_BENCHMARK_SOURCES = [
29
+ { suite: 'vulcanbench', url: 'https://vulcanbench.com/' },
30
+ { suite: 'vulcanbench-swe-v4', url: 'https://vulcanbench.com/benchmarks/swe-v4-gpt61-sol-v318.html' },
31
+ { suite: 'terminal-bench', url: 'https://www.tbench.ai/' },
32
+ { suite: 'swe-bench', url: 'https://www.swebench.com/' },
33
+ ];
34
+ const INVENTORY_URL = 'https://openrouter.ai/api/v1/models';
35
+ const LOCK_MS = 10 * 60 * 1000;
36
+ const RETRY_MS = 60 * 60 * 1000;
37
+ const SELF = fileURLToPath(import.meta.url);
38
+ function readRecord(routerDir) {
39
+ try {
40
+ const target = path.join(routerDir, 'currency.json');
41
+ if (fs.statSync(target).size > 8 * 1024 * 1024) return null;
42
+ return JSON.parse(fs.readFileSync(target, 'utf8'));
43
+ } catch { return null; }
44
+ }
45
+ export function readCurrencyStatus({ routerDir = DEFAULT_ROUTER_DIR, now = Date.now() } = {}) {
46
+ return currencyStatus(readRecord(routerDir), now);
47
+ }
48
+ function atomicWrite(target, bytes, beforeCommit = () => {}) {
49
+ fs.mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
50
+ const tmp = `${target}.tmp-${process.pid}-${Date.now()}`;
51
+ try {
52
+ const fd = fs.openSync(tmp, 'wx', 0o600);
53
+ try { fs.writeFileSync(fd, bytes); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
54
+ beforeCommit();
55
+ fs.renameSync(tmp, target);
56
+ } finally { try { fs.unlinkSync(tmp); } catch { /* renamed */ } }
57
+ }
58
+ // This guard is held only for synchronous filesystem transactions, never during fetches.
59
+ // Never reap it: a crash inside this tiny transaction must fail closed rather than overlap writers.
60
+ function transaction(routerDir, operation) {
61
+ fs.mkdirSync(routerDir, { recursive: true, mode: 0o700 });
62
+ const guard = path.join(routerDir, 'currency-mutation.lock');
63
+ try { fs.mkdirSync(guard, { mode: 0o700 }); } catch (error) { if (error.code === 'EEXIST') return null; throw error; }
64
+ try { return operation(); } finally { fs.rmdirSync(guard); }
65
+ }
66
+ function ownerPath(routerDir) { return path.join(routerDir, 'currency-refresh-owner.json'); }
67
+ function readOwner(routerDir) {
68
+ try { return JSON.parse(fs.readFileSync(ownerPath(routerDir), 'utf8')); }
69
+ catch (error) { if (error.code === 'ENOENT') return null; throw error; }
70
+ }
71
+ function claim(routerDir, now) {
72
+ return transaction(routerDir, () => {
73
+ const owner = readOwner(routerDir);
74
+ if (owner && (!Number.isFinite(owner.claimedAt) || now - owner.claimedAt <= LOCK_MS)) return null;
75
+ const next = { token: randomUUID(), claimedAt: now };
76
+ atomicWrite(ownerPath(routerDir), JSON.stringify(next));
77
+ return next.token;
78
+ });
79
+ }
80
+ function release(routerDir, token) {
81
+ return transaction(routerDir, () => {
82
+ if (readOwner(routerDir)?.token !== token) return false;
83
+ fs.unlinkSync(ownerPath(routerDir)); return true;
84
+ });
85
+ }
86
+ function fencedWrite(routerDir, token, target, bytes) {
87
+ const written = transaction(routerDir, () => {
88
+ if (readOwner(routerDir)?.token !== token) throw new Error('refresh superseded: ownership token changed');
89
+ atomicWrite(target, bytes, () => {
90
+ if (readOwner(routerDir)?.token !== token) throw new Error('refresh superseded before commit');
91
+ }); return true;
92
+ });
93
+ if (!written) throw new Error('refresh transaction busy; no write committed');
94
+ }
95
+
96
+ /** Prompt path: local bounded read and one detached worker; never await a network request. */
97
+ export function maybeLaunchCurrencyRefresh({ routerDir = DEFAULT_ROUTER_DIR, now = Date.now(), launch = spawn } = {}) {
98
+ const record = readRecord(routerDir); const status = currencyStatus(record, now);
99
+ if (status.status === 'current') return { ...status, launched: false };
100
+ const attempted = Date.parse(record?.lastAttempt?.checkedAt);
101
+ if (Number.isFinite(attempted) && attempted <= now && now - attempted < RETRY_MS) return { ...status, launched: false, deferred: 'retry cooldown' };
102
+ const lock = claim(routerDir, now);
103
+ if (!lock) return { ...status, launched: false, deferred: fs.existsSync(path.join(routerDir, 'currency-mutation.lock'))
104
+ ? 'refresh transaction blocked; inspect mutation guard before recovery' : 'refresh already running' };
105
+ try {
106
+ const child = launch(process.execPath, [SELF, '--refresh', '--claim-token', lock, '--router-dir', routerDir], { detached: true, stdio: 'ignore' });
107
+ child.once?.('error', () => release(routerDir, lock));
108
+ child.unref();
109
+ return { ...status, launched: true };
110
+ } catch (error) { release(routerDir, lock); return { ...status, launched: false, errors: [error.message] }; }
111
+ }
112
+
113
+ export async function refreshModelCurrency({ routerDir = DEFAULT_ROUTER_DIR, now = Date.now(), fetchImpl = fetch,
114
+ identityBindings, aaUrls = AA_URLS, officialSources = OFFICIAL_SOURCES, agentSourceUrls = AGENT_SOURCE_URLS, codingBenchmarkSources = CODING_BENCHMARK_SOURCES, claimToken = null } = {}) {
115
+ const lock = claimToken ?? claim(routerDir, now);
116
+ if (!lock) return { action: 'busy', status: 'stale', reason: 'refresh ownership or transaction guard unavailable' };
117
+ try {
118
+ const prior = readRecord(routerDir) ?? { schemaVersion: 1 };
119
+ const checkedAt = new Date(now).toISOString(); const errors = [];
120
+ let bindings = identityBindings;
121
+ if (!bindings) {
122
+ try { bindings = JSON.parse(fs.readFileSync(path.join(routerDir, 'identity-bindings.json'), 'utf8')); }
123
+ catch { bindings = {}; }
124
+ }
125
+ const collect = async (url) => {
126
+ const response = await fetchImpl(url, { signal: AbortSignal.timeout(20_000) });
127
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
128
+ const bytes = await response.text();
129
+ if (bytes.length > 6 * 1024 * 1024) throw new Error('source exceeds 6 MiB limit');
130
+ const source = { url, checkedAt, sha256: digest(bytes) };
131
+ fencedWrite(routerDir, lock, path.join(routerDir, 'evidence', `${source.sha256}.${url === INVENTORY_URL ? 'json' : 'html'}`), bytes);
132
+ return { source, bytes };
133
+ };
134
+ const results = await Promise.allSettled([INVENTORY_URL, ...aaUrls, ...officialSources.map((s) => s.url), ...agentSourceUrls, ...codingBenchmarkSources.map((s) => s.url)].map(collect));
135
+ let inventory = prior.inventory; let evaluations = prior.evaluations;
136
+ if (results[0].status === 'fulfilled') {
137
+ try { inventory = parseInventory(results[0].value.bytes, results[0].value.source); }
138
+ catch (error) { errors.push(`inventory: ${error.message}`); }
139
+ } else errors.push(`inventory: ${results[0].reason.message}`);
140
+ const parsed = [];
141
+ for (let i = 1; i <= aaUrls.length; i++) {
142
+ const result = results[i];
143
+ try {
144
+ if (result.status !== 'fulfilled') throw result.reason;
145
+ parsed.push(parseArtificialAnalysis(result.value.bytes, { ...result.value.source, identityBindings: bindings }));
146
+ } catch (error) { errors.push(`evaluations ${aaUrls[i - 1]}: ${error.message}`); }
147
+ }
148
+ // All requested pages must parse before certifying a new matrix; partial pages are evidence only.
149
+ if (parsed.length === aaUrls.length && parsed.length > 0) {
150
+ const records = new Map();
151
+ for (const page of parsed) for (const record of page.records) records.set(record.sourceModelId, record);
152
+ evaluations = { checkedAt, sources: parsed.map((p) => p.source), records: [...records.values()],
153
+ selectionQualified: false, limitation: 'Independent benchmark evidence; native access and supported effort require separate verification. Arena is not collected.' };
154
+ }
155
+ let official = prior.officialSources;
156
+ const publicDocs = [];
157
+ for (let i = 0; i < officialSources.length; i++) {
158
+ const result = results[1 + aaUrls.length + i];
159
+ if (result.status === 'fulfilled') publicDocs.push({ ...result.value.source, provider: officialSources[i].provider,
160
+ scope: 'official public/API documentation; not native subscription access', semanticallyQualified: false });
161
+ else errors.push(`official ${officialSources[i].url}: ${result.reason.message}`);
162
+ }
163
+ if (publicDocs.length === officialSources.length) official = { checkedAt, sources: publicDocs };
164
+ let agents = prior.agentSources;
165
+ if (agentSourceUrls.length) {
166
+ const agentResults = results.slice(1 + aaUrls.length + officialSources.length, 1 + aaUrls.length + officialSources.length + agentSourceUrls.length);
167
+ try {
168
+ if (agentResults.length !== 2) throw new Error('coding-agent source and methodology pair required');
169
+ for (const result of agentResults) if (result.status !== 'fulfilled') throw result.reason;
170
+ const parsedAgents = parseCodingAgentEvidence(agentResults[0].value.bytes, agentResults[1].value.bytes,
171
+ { ...agentResults[0].value.source, identityBindings: bindings });
172
+ agents = { checkedAt, sources: agentResults.map((r) => r.value.source), ...parsedAgents };
173
+ } catch (error) { errors.push(`coding agents: ${error.message}`); }
174
+ }
175
+ const additional = [];
176
+ for (let i = 0; i < codingBenchmarkSources.length; i++) {
177
+ const result = results[1 + aaUrls.length + officialSources.length + agentSourceUrls.length + i];
178
+ if (result.status === 'fulfilled' && result.value.bytes.trim().length >= 100) {
179
+ additional.push({ ...result.value.source, suite: codingBenchmarkSources[i].suite,
180
+ scope: 'raw source archive; independent suite, score parsing and semantic qualification not implemented' });
181
+ } else errors.push(`coding benchmark ${codingBenchmarkSources[i].url}: ${result.status === 'rejected' ? result.reason.message : 'empty or truncated source'}`);
182
+ }
183
+ if (additional.length === codingBenchmarkSources.length && agents && agents !== prior.agentSources) agents = { ...agents, additionalSources: additional };
184
+ else if (additional.length !== codingBenchmarkSources.length) agents = prior.agentSources;
185
+ const instructionPath = path.join(routerDir, 'weekly-analyst-instruction.md');
186
+ let instruction; let instructionSource = 'packaged-fallback';
187
+ try {
188
+ const fd = fs.openSync(instructionPath, 'r');
189
+ try {
190
+ const buffer = Buffer.alloc(128 * 1024 + 1);
191
+ const size = fs.readSync(fd, buffer, 0, buffer.length, 0);
192
+ if (size > 128 * 1024) throw new Error('effective weekly instruction exceeds 128 KiB');
193
+ instruction = buffer.subarray(0, size).toString('utf8');
194
+ if (!instruction.trim()) throw new Error('effective weekly instruction is empty');
195
+ instructionSource = 'effective-per-user-file';
196
+ } finally { fs.closeSync(fd); }
197
+ } catch (error) {
198
+ if (error.code !== 'ENOENT') { errors.push(`instruction: ${error.message}`); instruction = undefined; instructionSource = 'fallback-after-read-error'; }
199
+ }
200
+ const next = { schemaVersion: 1, maxAgeMs: WEEK_MS, inventory, evaluations, officialSources: official, agentSources: agents,
201
+ lastAttempt: { checkedAt, status: errors.length ? (inventory === prior.inventory && evaluations === prior.evaluations ? 'failed' : 'partial') : 'complete', errors } };
202
+ let priorPolicyBytes = null; let policy = null;
203
+ try { priorPolicyBytes = fs.readFileSync(path.join(routerDir, 'routing-policy.json'), 'utf8'); policy = JSON.parse(priorPolicyBytes); }
204
+ catch { /* no policy: report missing allocation, never synthesize one */ }
205
+ const assessment = buildWeeklyAssessment({ currency: next, policy, priorPolicyBytes, now, previousAssessment: prior.assessment, instruction, instructionSource });
206
+ const assessmentDir = path.join(routerDir, 'assessments', `${checkedAt.replaceAll(':', '-')}-${lock}`);
207
+ for (const [name, bytes] of [['report.json', JSON.stringify(assessment.report, null, 2)],
208
+ ['proposal.json', JSON.stringify(assessment.proposal, null, 2)], ['report.md', assessment.markdown],
209
+ ['instruction.md', assessment.instruction], ['prior-policy.json', priorPolicyBytes ?? 'null']]) {
210
+ fencedWrite(routerDir, lock, path.join(assessmentDir, name), bytes);
211
+ }
212
+ if (!fs.existsSync(instructionPath)) fencedWrite(routerDir, lock, instructionPath, assessment.instruction);
213
+ next.assessment = { instructionPath, instructionSha256: assessment.report.instructionSha256, instructionSource, checkedAt, signature: assessment.report.signature, reportPath: path.join(assessmentDir, 'report.json'),
214
+ proposalPath: path.join(assessmentDir, 'proposal.json'), status: 'complete-unqualified', analystExecuted: false,
215
+ notification: assessment.report.notification };
216
+ fencedWrite(routerDir, lock, path.join(routerDir, 'currency.json'), `${JSON.stringify(next, null, 2)}\n`);
217
+ return { action: 'refreshed', ...currencyStatus(next, now), lastAttempt: next.lastAttempt };
218
+ } finally { release(routerDir, lock); }
219
+ }
220
+
221
+ if (process.argv[1] && path.resolve(process.argv[1]) === SELF) {
222
+ const dirIndex = process.argv.indexOf('--router-dir');
223
+ const routerDir = dirIndex >= 0 ? process.argv[dirIndex + 1] : DEFAULT_ROUTER_DIR;
224
+ if (!routerDir || !path.isAbsolute(routerDir)) throw new Error('--router-dir must be absolute');
225
+ if (process.argv.includes('--refresh')) {
226
+ refreshModelCurrency({ routerDir, claimToken: process.argv.includes('--claim-token') ? process.argv[process.argv.indexOf('--claim-token') + 1] : null }).then((result) => {
227
+ console.log(JSON.stringify(result)); if (result.status === 'stale') process.exitCode = 1;
228
+ }).catch((error) => { console.error(error.message); process.exitCode = 1; });
229
+ } else console.log(JSON.stringify(process.argv.includes('--catch-up') ? maybeLaunchCurrencyRefresh({ routerDir }) : readCurrencyStatus({ routerDir })));
230
+ }
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env node
2
+ // DISTINCT-FROM: model-router-catalog.mjs — fresh native subscription metadata, never model execution or policy promotion.
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import os from 'node:os';
6
+ import { randomUUID } from 'node:crypto';
7
+ import { fileURLToPath } from 'node:url';
8
+ const HOSTS = { codex: 'openai', 'claude-code': 'anthropic' };
9
+ const EFFORTS = new Set(['low', 'medium', 'high', 'xhigh', 'max']);
10
+ const read = (file, optional = false) => {
11
+ try { const bytes = fs.readFileSync(file); if (bytes.length > 1048576) throw new Error('Native catalog input exceeds limit'); return bytes; }
12
+ catch (error) { if (optional && error.code === 'ENOENT') return null; throw error; }
13
+ };
14
+ function stage(file, bytes) {
15
+ const temporary = `${file}.${randomUUID()}.tmp`; const fd = fs.openSync(temporary, 'wx', 0o600);
16
+ try { fs.writeFileSync(fd, bytes); fs.fsyncSync(fd); } catch (error) { fs.closeSync(fd); fs.unlinkSync(temporary); throw error; }
17
+ fs.closeSync(fd);
18
+ return temporary;
19
+ }
20
+ const equal = (a, b) => a === null ? b === null : b !== null && a.equals(b);
21
+ export async function refreshNativeCatalog({ routerDir = path.join(os.homedir(), '.claude', 'model-router'),
22
+ deadline = Date.now() + 60000, inspect, now = Date.now() } = {}) {
23
+ const checkedAt = new Date(now).toISOString();
24
+ const failed = reason => ({ status: 'failed', checkedAt, newNativeModelIds: [], policyApplied: false, reason });
25
+ const paths = { profile: path.join(routerDir, 'profile.json'), catalog: path.join(routerDir, 'catalog.json'),
26
+ availability: path.join(routerDir, 'native-availability.json') };
27
+ let guard; let acquired = false; const temporary = [];
28
+ try {
29
+ if (!path.isAbsolute(routerDir) || !Number.isFinite(deadline) || deadline <= Date.now()) return failed('Invalid native metadata directory or deadline');
30
+ const before = Object.fromEntries(Object.entries(paths).map(([key, file]) => [key, read(file, key === 'availability')]));
31
+ const profile = JSON.parse(before.profile); const catalog = JSON.parse(before.catalog);
32
+ if (profile.automaticModelRoutingUpdates !== true) return failed('Automatic native model registration is not authorized by this profile');
33
+ if (!Array.isArray(catalog.candidates) || catalog.candidates.some(c => typeof c.id !== 'string')
34
+ || new Set(catalog.candidates.map(c => c.id)).size !== catalog.candidates.length) return failed('Existing model catalog is malformed');
35
+ const hosts = Object.keys(HOSTS).filter(host => profile.harnesses?.[host]?.available === true
36
+ && profile.harnesses[host].subscription === true && profile.harnesses[host].enabled !== false);
37
+ if (!hosts.length) return failed('No enabled native subscription host in this profile');
38
+ inspect ??= (await import('./model-native-qualification.mjs')).inspectNativeQualificationHost;
39
+ const observations = []; const eligibleIds = []; const nativeIds = []; const unknownEffortIds = []; const withheldIds = [];
40
+ const candidates = [...catalog.candidates];
41
+ for (const host of hosts) {
42
+ if (Date.now() >= deadline) return failed('Native metadata deadline expired');
43
+ const result = await inspect({ host, deadline });
44
+ if (result?.failure || result?.available !== true || result.nativeSubscription !== true || result.sourceReceipt?.host !== host
45
+ || result.sourceReceipt.allowance?.verified !== true || result.catalogStatus !== 'verified'
46
+ || !Array.isArray(result.models) || !result.models.length) return failed(`Verified native model list unavailable for ${host}`);
47
+ const models = [];
48
+ for (const model of result.models) {
49
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]+$/.test(model.id ?? '')) return failed(`Exact native model identity unavailable for ${host}`);
50
+ const id = `${HOSTS[host]}/${model.id}`; nativeIds.push(id);
51
+ const nativeEfforts = Array.isArray(model.efforts) ? model.efforts.filter(e => typeof e === 'string' && /^[a-z][a-z0-9_-]{0,31}$/.test(e)) : [];
52
+ const efforts = [...new Set(nativeEfforts.filter(e => EFFORTS.has(e)))].sort();
53
+ const unrecognizedEfforts = [...new Set(nativeEfforts.filter(e => !EFFORTS.has(e)))].sort();
54
+ const knownEfforts = efforts.length > 0;
55
+ models.push({ id: model.id, efforts, effortSupport: knownEfforts ? 'verified' : 'unknown',
56
+ ...(unrecognizedEfforts.length ? { unrecognizedEfforts } : {}) });
57
+ if (!knownEfforts) { unknownEffortIds.push(id); continue; }
58
+ const existing = candidates.find(c => c.id === model.id);
59
+ if (existing) {
60
+ if (existing.provider !== HOSTS[host] || existing.disabled === true || existing.enabled === false
61
+ || !existing.harness?.includes(host) || !existing.subscription?.includes(host)) { withheldIds.push(id); continue; }
62
+ } else candidates.push({ id: model.id, provider: HOSTS[host], harness: [host], subscription: [host],
63
+ supportedEfforts: efforts, costPerMTok: null, verified: `${checkedAt} native subscription metadata; execution not tested`,
64
+ note: 'Native catalog availability is not role-quality qualification or active routing approval.' });
65
+ eligibleIds.push(id);
66
+ }
67
+ if (new Set(models.map(m => m.id)).size !== models.length) return failed(`Duplicate native model identity for ${host}`);
68
+ observations.push({ host, provider: HOSTS[host], checkedAt: result.checkedAt, harnessVersion: result.harnessVersion, allowanceVerified: true, models: models.sort((a, b) => a.id.localeCompare(b.id)) });
69
+ }
70
+ if (Date.now() >= deadline) return failed('Native metadata deadline expired');
71
+ const previous = before.availability ? JSON.parse(before.availability) : null;
72
+ if (previous && (previous.schemaVersion !== 1 || !Array.isArray(previous.nativeModelIds)
73
+ || previous.nativeModelIds.some(id => typeof id !== 'string'))) return failed('Previous native availability baseline is malformed');
74
+ const ids = [...new Set(eligibleIds)].sort(); const nativeModelIds = [...new Set(nativeIds)].sort();
75
+ const newNativeModelIds = previous ? nativeModelIds.filter(id => !previous.nativeModelIds.includes(id)) : [];
76
+ const snapshot = { schemaVersion: 1, status: 'current', checkedAt, hosts: observations,
77
+ nativeModelIds, eligibleModelIds: ids, unknownEffortModelIds: unknownEffortIds.sort(), withheldExistingModelIds: [...new Set(withheldIds)].sort(), inferenceRequests: 0, policyApplied: false };
78
+ const changedCatalog = candidates.length !== catalog.candidates.length;
79
+ const catalogBytes = changedCatalog ? Buffer.from(JSON.stringify({ ...catalog, candidates }, null, 2) + '\n') : before.catalog;
80
+ const snapshotBytes = Buffer.from(JSON.stringify(snapshot, null, 2) + '\n');
81
+ guard = path.join(routerDir, 'native-catalog-mutation.lock'); fs.mkdirSync(guard, { mode: 0o700 }); acquired = true;
82
+ for (const [key, file] of Object.entries(paths)) if (!equal(before[key], read(file, key === 'availability'))) throw new Error('Native catalog inputs changed');
83
+ const history = path.join(routerDir, 'native-catalog-history', `${now}-${randomUUID()}`); fs.mkdirSync(history, { recursive: true, mode: 0o700 });
84
+ for (const [name, bytes] of [['catalog.json', before.catalog], ['native-availability.json', before.availability]]) {
85
+ if (!bytes) continue; const target = path.join(history, name); const temp = stage(target, bytes); temporary.push(temp); fs.renameSync(temp, target);
86
+ }
87
+ const catalogTemp = changedCatalog ? stage(paths.catalog, catalogBytes) : null;
88
+ if (catalogTemp) temporary.push(catalogTemp);
89
+ const snapshotTemp = stage(paths.availability, snapshotBytes); temporary.push(snapshotTemp);
90
+ if (catalogTemp) fs.renameSync(catalogTemp, paths.catalog);
91
+ try { fs.renameSync(snapshotTemp, paths.availability); }
92
+ catch (error) {
93
+ if (catalogTemp && equal(catalogBytes, read(paths.catalog))) {
94
+ const restore = stage(paths.catalog, before.catalog); temporary.push(restore); fs.renameSync(restore, paths.catalog);
95
+ }
96
+ throw error;
97
+ }
98
+ return { status: 'current', checkedAt, newNativeModelIds, baseline: previous === null,
99
+ catalogAdded: candidates.length - catalog.candidates.length, unknownEffortModelIds: snapshot.unknownEffortModelIds, withheldExistingModelIds: snapshot.withheldExistingModelIds,
100
+ inferenceRequests: 0, policyApplied: false };
101
+ } catch { return failed('Native catalog refresh refused; previous availability and owner catalog retained where unchanged'); }
102
+ finally {
103
+ for (const file of temporary) { try { fs.unlinkSync(file); } catch { /* renamed */ } }
104
+ if (acquired) { try { fs.rmdirSync(guard); } catch { /* not acquired */ } }
105
+ }
106
+ }
107
+ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
108
+ const option = name => { const at = process.argv.indexOf(name); return at < 0 ? undefined : process.argv[at + 1]; };
109
+ refreshNativeCatalog({ routerDir: option('--router-dir'), deadline: option('--deadline') === undefined ? undefined : Number(option('--deadline')) })
110
+ .then(result => { console.log(JSON.stringify(result)); if (result.status !== 'current') process.exitCode = 1; });
111
+ }