ruvnet-brain 4.5.4 → 4.5.5

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 (37) hide show
  1. package/README.md +2 -2
  2. package/bin/install.mjs +144 -27
  3. package/config/model-router/catalog.template.json +126 -52
  4. package/config/model-router/policy.default.mjs +94 -75
  5. package/config/model-router/qualification-contract.json +124 -0
  6. package/config/model-router/routing-eval-cases.json +275 -0
  7. package/config/model-router/routing-policy.template.json +76 -0
  8. package/config/model-router/weekly-analyst-instruction.md +60 -0
  9. package/data/model-catalog.json +44 -49
  10. package/package.json +3 -1
  11. package/plugin/.claude-plugin/plugin.json +1 -1
  12. package/plugin/.codex-plugin/plugin.json +1 -1
  13. package/plugin/scripts/codex-hook-adapter.mjs +18 -9
  14. package/scripts/codex-hook-trust-reconcile.mjs +247 -0
  15. package/scripts/codex-routed.sh +3 -36
  16. package/scripts/goldie-weekly.sh +8 -64
  17. package/scripts/metaharness-router.mjs +7 -1
  18. package/scripts/model-analyst-sandbox.mjs +54 -0
  19. package/scripts/model-currency-evidence.mjs +139 -0
  20. package/scripts/model-currency.mjs +230 -0
  21. package/scripts/model-native-catalog.mjs +111 -0
  22. package/scripts/model-native-qualification.mjs +251 -0
  23. package/scripts/model-router-agent-hook.mjs +136 -0
  24. package/scripts/model-router-dispatch.mjs +161 -0
  25. package/scripts/model-router-engine.mjs +155 -104
  26. package/scripts/model-routing-eval.mjs +108 -0
  27. package/scripts/model-routing-gateway.mjs +420 -0
  28. package/scripts/model-routing-launchers.mjs +174 -0
  29. package/scripts/model-routing-policy-promotion.mjs +203 -0
  30. package/scripts/model-weekly-analyst.mjs +299 -0
  31. package/scripts/model-weekly-assessment.mjs +91 -0
  32. package/scripts/model-weekly-cycle.mjs +183 -0
  33. package/scripts/model-weekly-qualification.mjs +362 -0
  34. package/scripts/native-subscription-usage.mjs +57 -0
  35. package/scripts/release-qualification-contract.mjs +41 -0
  36. package/scripts/security-guidance-codex-compat.mjs +142 -0
  37. package/scripts/user-model-prompt-hook.mjs +69 -0
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env node
2
+ // Per-user PreToolUse boundary for native agent launches. Root owns host registration.
3
+ // Codex v2 spawn_agent model/reasoning_effort/fork_turns fields checked in official source and
4
+ // installed CLI. Claude's installed Agent schema lacks per-call effort and permits model aliases
5
+ // only; do not invent unsupported fields. Actual rewritten dispatch requires host acceptance proof.
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import os from 'node:os';
9
+ import { execFileSync } from 'node:child_process';
10
+ import { fileURLToPath, pathToFileURL } from 'node:url';
11
+
12
+ import { readCodexAllowance } from './native-subscription-usage.mjs';
13
+
14
+ const DIR = path.dirname(fileURLToPath(import.meta.url));
15
+ const TASK_CLASSES = new Set(['fast', 'medium', 'substantial', 'hard', 'exceptional']);
16
+ const EFFORTS = new Set(['low', 'medium', 'high', 'xhigh', 'max']);
17
+ const CODEX_AGENT = /^(?:(?:functions|collaboration|multi_agent_v2)\.)?spawn_agent$/;
18
+ const CLAUDE_AGENT = /^(?:Agent|Task)$/;
19
+
20
+ function deny(reason) {
21
+ return { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: reason } };
22
+ }
23
+
24
+ function advisory(reason) {
25
+ return { hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext: reason } };
26
+ }
27
+
28
+ export function loadScope(harness, { routerDir = process.env.MODEL_ROUTER_CONFIG_DIR || path.join(os.homedir(), '.claude', 'model-router') } = {}) {
29
+ try {
30
+ const profile = JSON.parse(fs.readFileSync(process.env.MODEL_ROUTER_PROFILE || path.join(routerDir, 'profile.json'), 'utf8'));
31
+ const selection = JSON.parse(fs.readFileSync(process.env.MODEL_ROUTER_SELECTION || path.join(routerDir, 'routing-policy.json'), 'utf8'));
32
+ const user = profile.nativeAgentRouting?.[harness];
33
+ const reviewed = selection.nativeAgentRouting?.[harness];
34
+ return { enabled: user?.enabled === true && reviewed?.enabled === true,
35
+ strict: user?.strict === true && reviewed?.strict === true };
36
+ } catch { return { enabled: false, strict: false }; }
37
+ }
38
+
39
+ export function decideViaEngine(prompt, harness, { engine = process.env.MODEL_ROUTER_ENGINE || path.join(DIR, 'model-router-engine.mjs'),
40
+ run = execFileSync } = {}) {
41
+ return JSON.parse(run(process.execPath, [engine, '--harness', harness, '--policy-only', '--json'], {
42
+ input: prompt, encoding: 'utf8', timeout: 2000, maxBuffer: 131072,
43
+ // No policy errors or raw task text are emitted by this hook on child failure.
44
+ stdio: ['pipe', 'pipe', 'pipe'],
45
+ }));
46
+ }
47
+
48
+ export function loadNativeModels(file = process.env.MODEL_ROUTER_NATIVE_MODELS ||
49
+ path.join(process.env.CODEX_HOME || path.join(os.homedir(), '.codex'), 'models_cache.json')) {
50
+ const cache = JSON.parse(fs.readFileSync(file, 'utf8'));
51
+ if (!Array.isArray(cache.models)) throw new Error('Native model support cache missing');
52
+ return cache.models;
53
+ }
54
+
55
+ export function routeAgentLaunch(event, { harness, decide = decideViaEngine, nativeModels = loadNativeModels, scope = loadScope, parentState = null } = {}) {
56
+ const tool = event?.tool_name || event?.toolName;
57
+ const isAgent = harness === 'codex' ? CODEX_AGENT.test(tool || '') :
58
+ harness === 'claude-code' ? CLAUDE_AGENT.test(tool || '') : false;
59
+ if (!isAgent || (event.hook_event_name && event.hook_event_name !== 'PreToolUse')) return {};
60
+ const routingScope = scope(harness);
61
+ if (!routingScope.enabled) return {};
62
+ const refuse = (reason) => harness === 'codex' && routingScope.strict ? deny(reason) : advisory(reason);
63
+ const input = event.tool_input || event.toolInput;
64
+ if (!input || typeof input !== 'object' || Array.isArray(input)) return refuse('Agent launch arguments unavailable; use a classified managed worker.');
65
+ const prompt = harness === 'codex' ? input.message : input.prompt;
66
+ if (typeof prompt !== 'string' || !prompt.trim()) return refuse('Agent task unavailable for classification; use a classified managed worker.');
67
+ let decision;
68
+ try { decision = decide(prompt, harness); }
69
+ catch { return refuse('Current reviewed model/effort allocation unavailable; review routing policy before agent dispatch.'); }
70
+ if (!decision?.subscriptionCovered || decision.harness !== harness || !TASK_CLASSES.has(decision.taskClass) ||
71
+ !EFFORTS.has(decision.effort) || !/^[a-zA-Z0-9][a-zA-Z0-9._-]+$/.test(decision.model || '')) {
72
+ return refuse('Agent routing did not produce a qualified native subscription model and effort.');
73
+ }
74
+ if (harness === 'codex' && ['xhigh', 'max'].includes(decision.effort) &&
75
+ (decision.taskClass !== 'exceptional' || decision.effort !== 'xhigh' || !/^[a-z][a-z0-9-]{2,79}$/.test(decision.exceptionalReason || ''))) {
76
+ return refuse('Exceptional native agent effort requires an explicit named reason and qualified xhigh route.');
77
+ }
78
+ if (harness === 'claude-code') {
79
+ return advisory(`Native Claude Agent cannot enforce this reviewed ${decision.taskClass} model and ${decision.effort} effort through its current per-call schema. Use model-router-dispatch.mjs; do not add unsupported effort fields.`);
80
+ }
81
+ // Never convert all-history forks into context-free tasks to make overrides fit.
82
+ if ('fork_context' in input) return refuse('Legacy fork_context dispatch is unsupported by this v2 routing hook; preserve context through a supported launch.');
83
+ const fork = typeof input.fork_turns === 'string' ? input.fork_turns.trim().toLowerCase() : 'all';
84
+ if (fork === 'all' || !fork) {
85
+ // Parent evidence must come from a verified host adapter, never the requested tool arguments.
86
+ // Current native hook has no proven effective-effort field, so main supplies no parent evidence.
87
+ if (parentState?.observed === true && parentState.model === decision.model && parentState.effort === decision.effort) {
88
+ return advisory('Full-history fork already matches the reviewed model and effort; input and inherited context preserved.');
89
+ }
90
+ return refuse('Full-history agent forks inherit parent model and effort; a matching effective parent allocation is not verified. Routing will not discard context; use an explicitly authorized bounded fork or managed worker.');
91
+ }
92
+ if (fork !== 'none' && !/^[1-9]\d*$/.test(fork)) return refuse('Invalid fork_turns; routing will not alter the context boundary.');
93
+ let model;
94
+ try { model = nativeModels().find((m) => m.slug === decision.model); }
95
+ catch { return refuse('Native agent model support unavailable; refresh the host model cache before dispatch.'); }
96
+ if (!model || model.multi_agent_version !== 'v2' ||
97
+ !(model.supported_reasoning_levels || []).some((entry) => entry.effort === decision.effort)) {
98
+ return refuse('Reviewed model/effort is not supported by this native v2 agent host.');
99
+ }
100
+ // updatedInput replaces the COMPLETE object. No context, capabilities, auth, or task fields dropped.
101
+ return { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'allow',
102
+ updatedInput: { ...input, model: decision.model, reasoning_effort: decision.effort },
103
+ ...(decision.exceptionalReason ? { additionalContext: `Qualified exceptional reason: ${decision.exceptionalReason}` } : {}) } };
104
+ }
105
+
106
+ async function main(argv) {
107
+ const at = argv.indexOf('--harness');
108
+ const harness = at < 0 ? null : argv[at + 1];
109
+ if (!['codex', 'claude-code'].includes(harness)) throw new Error('Explicit supported --harness required');
110
+ let event;
111
+ try { event = JSON.parse(fs.readFileSync(0, 'utf8')); }
112
+ catch {
113
+ const scope = loadScope(harness);
114
+ const output = !scope.enabled ? {} : harness === 'codex' && scope.strict
115
+ ? deny('Malformed agent hook input; launch was not classified.') : advisory('Malformed agent hook input; launch was not classified.');
116
+ process.stdout.write(JSON.stringify(output) + '\n'); return;
117
+ }
118
+ let output = routeAgentLaunch(event, { harness });
119
+ if (harness === 'codex' && output.hookSpecificOutput?.permissionDecision === 'allow') {
120
+ try { await readCodexAllowance(); }
121
+ catch {
122
+ output = loadScope(harness).strict ? deny('Current native ordinary subscription allowance unavailable; no credit fallback authorized.')
123
+ : advisory('Current native ordinary subscription allowance unavailable; managed dispatch cannot proceed without fresh allowance.');
124
+ }
125
+ }
126
+ // The host's normal transcript records its tool input. This hook persists no task text.
127
+ process.stdout.write(JSON.stringify(output) + '\n');
128
+ }
129
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
130
+ main(process.argv.slice(2)).catch(() => {
131
+ const args = process.argv.slice(2); const harness = args[args.indexOf('--harness') + 1];
132
+ const result = harness === 'codex' && loadScope(harness).strict ? deny('Agent routing hook failed; managed dispatch required.')
133
+ : advisory('Agent routing hook failed; managed dispatch remains the supported enforcement path.');
134
+ process.stdout.write(JSON.stringify(result) + '\n');
135
+ });
136
+ }
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env node
2
+ // Managed native worker launcher. A prompt hook advises; this boundary enforces launch arguments.
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import os from 'node:os';
6
+ import { spawn, execFileSync } from 'node:child_process';
7
+ import { fileURLToPath, pathToFileURL } from 'node:url';
8
+
9
+ import { assertCurrentSelection, selectionEvidenceStatus, loadSelection, loadCatalog, loadProfile, applyProfile, eligibleCandidates, TASK_CLASSES } from './model-router-engine.mjs';
10
+
11
+ import { readCodexAllowance } from './native-subscription-usage.mjs';
12
+
13
+ const DIR = path.dirname(fileURLToPath(import.meta.url));
14
+ const BILLING_ENV = /^(OPENAI_API_KEY|OPENAI_BASE_URL|ANTHROPIC_API_KEY|ANTHROPIC_AUTH_TOKEN|ANTHROPIC_BASE_URL|ANTHROPIC_CUSTOM_HEADERS|CLAUDE_CODE_USE_BEDROCK|CLAUDE_CODE_USE_VERTEX|CLAUDE_CODE_USE_FOUNDRY|CODEX_API_KEY)$/;
15
+
16
+ export function subscriptionEnvironment(env = process.env) {
17
+ return Object.fromEntries(Object.entries(env).filter(([key]) => !BILLING_ENV.test(key)));
18
+ }
19
+
20
+ export function assertSubscriptionAuth(harness, { env = subscriptionEnvironment(), read = fs.readFileSync,
21
+ probe = execFileSync } = {}) {
22
+ if (harness === 'codex') {
23
+ let auth;
24
+ try { auth = JSON.parse(read(path.join(env.CODEX_HOME || path.join(os.homedir(), '.codex'), 'auth.json'), 'utf8')); }
25
+ catch { throw new Error('Native Codex subscription authentication unreadable; dispatch blocked'); }
26
+ if (!auth.tokens || auth.OPENAI_API_KEY || (auth.auth_mode && auth.auth_mode !== 'chatgpt')) {
27
+ throw new Error('Codex subscription OAuth authentication required; no API-key fallback');
28
+ }
29
+ } else if (harness === 'claude-code') {
30
+ let status;
31
+ try { status = JSON.parse(probe('claude', ['auth', 'status', '--json'], {
32
+ env, encoding: 'utf8', timeout: 10000, stdio: ['ignore', 'pipe', 'pipe'],
33
+ })); } catch { throw new Error('Native Claude subscription authentication unavailable; dispatch blocked'); }
34
+ if (!status.loggedIn || status.authMethod !== 'claude.ai' || status.apiProvider !== 'firstParty' || !status.subscriptionType) {
35
+ throw new Error('Claude native subscription authentication required; no API fallback');
36
+ }
37
+ } else throw new Error(`Unsupported harness: ${harness}`);
38
+ }
39
+
40
+ export function loadNativeCodexModels() {
41
+ const file = process.env.MODEL_ROUTER_NATIVE_MODELS || path.join(process.env.CODEX_HOME || path.join(os.homedir(), '.codex'), 'models_cache.json');
42
+ try {
43
+ const cache = JSON.parse(fs.readFileSync(file, 'utf8'));
44
+ if (Array.isArray(cache.models)) return cache.models;
45
+ } catch { /* no inferred capability from API or catalog pricing */ }
46
+ throw new Error('Native Codex model support cache unavailable; refresh host metadata before dispatch');
47
+ }
48
+
49
+ export function validateDispatchDecision(decision, { selection = loadSelection(), profile = loadProfile(),
50
+ candidates = applyProfile(loadCatalog(), profile), nativeModels } = {}) {
51
+ const evidence = selectionEvidenceStatus(selection);
52
+ if (selection.reviewedAt !== decision.selectionReviewedAt || !evidence.routeDigest || evidence.routeDigest !== decision.selectionRouteDigest) throw new Error('Allocation changed since selection; reclassify prompt');
53
+ const approved = selection.routes?.[decision.harness]?.[decision.taskClass];
54
+ const efforts = [approved?.effort];
55
+ if (decision.harness === 'claude-code' && decision.taskClass === 'medium') {
56
+ efforts.push(selection.routes?.[decision.harness]?.codingEffort);
57
+ }
58
+ if (decision.harness === 'codex' && ['xhigh', 'max'].includes(decision.effort) &&
59
+ (decision.taskClass !== 'exceptional' || decision.effort !== 'xhigh' || !approved?.requiresNamedReason ||
60
+ !/^[a-z][a-z0-9-]{2,79}$/.test(decision.exceptionalReason || ''))) {
61
+ throw new Error('Exceptional xhigh dispatch requires explicit qualified route and named reason');
62
+ }
63
+ if (approved?.model !== decision.model || !efforts.includes(decision.effort) ||
64
+ !eligibleCandidates(candidates, profile, decision.harness).some((m) => m.id === decision.model)) {
65
+ throw new Error('Launch no longer satisfies reviewed native subscription allocation');
66
+ }
67
+ if (decision.harness === 'codex') {
68
+ const native = (nativeModels || loadNativeCodexModels()).find((m) => m.slug === decision.model);
69
+ if (!native || !(native.supported_reasoning_levels || []).some((m) => m.effort === decision.effort)) {
70
+ throw new Error('Native Codex host does not support the reviewed model/effort dispatch');
71
+ }
72
+ }
73
+ return evidence;
74
+ }
75
+
76
+ export function buildLaunch(decision, { cwd = process.cwd(), interactive = false } = {}) {
77
+ if (interactive) throw new Error('Interactive host cannot keep managed-worker prompts on stdin; use managed worker launch');
78
+ assertCurrentSelection({ schemaVersion: 1, reviewedAt: decision.selectionReviewedAt, maxAgeMs: decision.selectionMaxAgeMs });
79
+ if (!decision.subscriptionCovered || !TASK_CLASSES.includes(decision.taskClass) ||
80
+ !['low', 'medium', 'high', 'xhigh', 'max'].includes(decision.effort) || !/^[a-zA-Z0-9][a-zA-Z0-9._-]+$/.test(decision.model || '')) {
81
+ throw new Error('Invalid or unauthorized routing decision');
82
+ }
83
+ if (decision.harness === 'codex' && decision.provider === 'openai') {
84
+ return { command: 'codex', args: ['exec', '--ignore-user-config',
85
+ '--model', decision.model, '-c', `model_reasoning_effort="${decision.effort}"`,
86
+ '-c', 'model_provider="openai"', '-c', 'service_tier="default"', '-c', 'features.fast_mode=false', '--cd', cwd, '-'] };
87
+ }
88
+ if (decision.harness === 'claude-code' && decision.provider === 'anthropic') {
89
+ // Ignore user/project API-key helpers and provider redirects. Native OAuth remains readable.
90
+ return { command: 'claude', args: ['--print', '--model', decision.model,
91
+ '--effort', decision.effort, '--setting-sources', '', '--settings', '{"apiKeyHelper":""}'] };
92
+ }
93
+ throw new Error('Decision does not target a supported native subscription host');
94
+ }
95
+
96
+ export async function dispatch(decision, prompt, { spawnWorker = spawn, checkAuth = assertSubscriptionAuth, verifyDecision = validateDispatchDecision,
97
+ env = process.env, cwd = process.cwd(), interactive = false, checkAllowance = readCodexAllowance,
98
+ receiptFile = process.env.MODEL_ROUTER_DISPATCH_RECEIPTS || path.join(os.homedir(), '.claude', 'metaharness', 'dispatch-decisions.jsonl') } = {}) {
99
+ const evidence = verifyDecision(decision) || selectionEvidenceStatus({ schemaVersion: 1,
100
+ reviewedAt: decision.selectionReviewedAt, maxAgeMs: decision.selectionMaxAgeMs });
101
+ const launch = buildLaunch(decision, { cwd, interactive });
102
+ const cleanEnv = subscriptionEnvironment(env);
103
+ checkAuth(decision.harness, { env: cleanEnv });
104
+ const allowance = decision.harness === 'codex' ? await checkAllowance({ env: cleanEnv })
105
+ : { checkedAt: null, ordinaryUsageAllowed: null, status: 'native-claude-allowance-not-verified' };
106
+ const receipt = { ts: new Date().toISOString(), harness: decision.harness, model: decision.model,
107
+ effort: decision.effort, taskClass: decision.taskClass, exceptionalReason: decision.exceptionalReason, subscriptionCovered: true,
108
+ selectionReviewedAt: evidence.reviewedAt, selectionRouteDigest: evidence.routeDigest || decision.selectionRouteDigest,
109
+ selectionEvidenceStale: evidence.stale,
110
+ status: 'launch-requested', modelObserved: false, serviceMode: decision.harness === 'codex' ? 'standard' : 'existing-claude-policy',
111
+ allowance: { checkedAt: allowance.checkedAt, ordinaryUsageAllowed: allowance.ordinaryUsageAllowed, reservation: false, raceSafe: false } };
112
+ // Durable receipt is required. It never contains the prompt or policy-supplied reason.
113
+ fs.mkdirSync(path.dirname(receiptFile), { recursive: true });
114
+ fs.appendFileSync(receiptFile, JSON.stringify(receipt) + '\n', { mode: 0o600 });
115
+ const child = spawnWorker(launch.command, launch.args, {
116
+ cwd, env: cleanEnv, shell: false, stdio: ['pipe', 'inherit', 'inherit'],
117
+ });
118
+ child.stdin.end(prompt);
119
+ return await new Promise((resolve, reject) => {
120
+ child.once('error', reject);
121
+ child.once('exit', (code, signal) => {
122
+ fs.appendFileSync(receiptFile, JSON.stringify({ ...receipt, ts: new Date().toISOString(),
123
+ status: code === 0 ? 'process-completed' : 'process-failed', exitCode: code, signal }) + '\n');
124
+ resolve(code ?? 1);
125
+ });
126
+ });
127
+ }
128
+
129
+ async function main(argv) {
130
+ let harness = 'codex', policy, interactive = false, dryRun = false, requestJson = false;
131
+ const promptParts = [];
132
+ for (let i = 0; i < argv.length; i++) {
133
+ if (argv[i] === '--harness') harness = argv[++i];
134
+ else if (argv[i] === '--policy') policy = argv[++i];
135
+ else if (argv[i] === '--request-json') requestJson = true;
136
+ else if (argv[i] === '--interactive') interactive = true;
137
+ else if (argv[i] === '--dry-run') dryRun = true;
138
+ else if (argv[i] === '--') { promptParts.push(...argv.slice(i + 1)); break; }
139
+ else if (argv[i].startsWith('-')) throw new Error(`Unsupported launch option: ${argv[i]}`);
140
+ else promptParts.push(argv[i]);
141
+ }
142
+ const raw = promptParts.length ? promptParts.join(' ') : fs.readFileSync(0, 'utf8');
143
+ const request = requestJson ? JSON.parse(raw) : { prompt: raw };
144
+ const prompt = request.prompt;
145
+ if (typeof prompt !== 'string') throw new Error('Request prompt must be a string');
146
+ const args = [path.join(DIR, 'model-router-engine.mjs'), '--harness', harness, '--json'];
147
+ if (policy) args.push('--policy', policy);
148
+ if (requestJson) args.push('--request-json');
149
+ const decision = JSON.parse(execFileSync(process.execPath, args, {
150
+ input: requestJson ? JSON.stringify(request) : prompt, encoding: 'utf8', timeout: 30000, maxBuffer: 1024 * 1024,
151
+ }));
152
+ if (dryRun) {
153
+ validateDispatchDecision(decision);
154
+ const launch = buildLaunch(decision, { interactive });
155
+ process.stdout.write(JSON.stringify({ ...launch, model: decision.model, effort: decision.effort,
156
+ taskClass: decision.taskClass, executed: false }) + '\n');
157
+ } else process.exitCode = await dispatch(decision, prompt, { interactive });
158
+ }
159
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
160
+ main(process.argv.slice(2)).catch((e) => { process.stderr.write(`model-router-dispatch: ${e.message}\n`); process.exitCode = 1; });
161
+ }