Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(ruflo-mods): classify risky host calls and intercepting hooks (#3802)

Dragan Spiridonovb6f3988de749
diff --git a/.github/workflows/all-plugins-smoke.yml b/.github/workflows/all-plugins-smoke.yml
index b6587fc90c..098240a4ab 100644
--- a/.github/workflows/all-plugins-smoke.yml
+++ b/.github/workflows/all-plugins-smoke.yml
@@ -62,7 +62,7 @@ jobs:
- name: esbuild for the guard probe (scripts/probe-mod-guards.mjs bundles each plugin guard)
run: |
- npm install --prefix "$RUNNER_TEMP/esbuild" --no-audit --no-fund --no-package-lock [email protected]
+ npm install --prefix "$RUNNER_TEMP/esbuild" --no-audit --no-fund --no-package-lock [email protected] [email protected]
echo "ESBUILD=$RUNNER_TEMP/esbuild/node_modules/.bin/esbuild" >> "$GITHUB_ENV"
- name: Discover and run every plugin smoke contract (parallel)
# --timeout 300 caps each individual plugin smoke at 5 minutes.
@@ -73,8 +73,9 @@ jobs:
# smoke while letting the metaharness fleet finish.
# Save the report from the same execution. Re-running the whole fleet
# solely to obtain JSON can exceed this job's ten-minute deadline.
- # pipefail preserves the smoke runner's failure exit through tee.
- run: |
+ # Explicit bash and pipefail preserve a failing smoke exit through tee.
+ shell: bash
+ run: |-
set -o pipefail
node scripts/smoke-all-plugins.mjs --timeout 300 --format json | tee /tmp/smoke-all-plugins.json
@@ -83,8 +84,10 @@ jobs:
# Fails when any plugins/*/hooks/screen.ts drifted from plugins/ruflo-agentdb/hooks/screen.ts between the
# SHARED SCREEN markers, or when it finds no copies. Fix: node scripts/sync-mod-screen.mjs
- name: Guard tool names exist in the registry
+ env:
+ NODE_PATH: ${{ runner.temp }}/esbuild/node_modules
run: |
- node --test tests/check-guard-tool-names.test.mjs
+ node --test tests/check-guard-tool-names.test.mjs tests/all-plugins-smoke-workflow.test.mjs
node scripts/check-guard-tool-names.mjs
# A renamed MCP tool leaves a plugin guard watching a name that no
# longer exists, so it silently stops guarding. Fails loud if zero
diff --git a/plugins/ruflo-mods/.claude-plugin/plugin.json b/plugins/ruflo-mods/.claude-plugin/plugin.json
index 88d30dbcc5..5e0574b233 100644
--- a/plugins/ruflo-mods/.claude-plugin/plugin.json
+++ b/plugins/ruflo-mods/.claude-plugin/plugin.json
@@ -113,7 +113,7 @@
"modTrust": {
"type": "string",
"title": "Mod trust gate",
- "description": "How ruflo treats a hooks module installed or hot-reloaded after it (a mod Claude wrote, say). \"observe\" (default): name what it can do (host commands, network, environment, file writes, tool verdicts) in the transcript. \"refuse-risky\": refuse user-tier mods that can do any of those, unless listed in modTrustAllow. \"off\": do not watch.",
+ "description": "How ruflo treats a hooks module installed or hot-reloaded after it (a mod Claude wrote, say). \"observe\" (default): name what it can do (host commands, network, MCP tools, messages to other agents, settings, environment, file writes, starting agents, submitting prompts, running tools; hooks, including globs and negations, on tool verdicts, tool calls, prompts, agent spawns, tool descriptions, the transcript, compaction, model requests, other mods, or the file reads, settings and environment other mods decide by) in the transcript. \"refuse-risky\": refuse user-tier mods that can do any of those, unless listed in modTrustAllow. \"off\": do not watch.",
"required": false,
"default": "observe",
"options": [
diff --git a/plugins/ruflo-mods/README.md b/plugins/ruflo-mods/README.md
index e44e46cf89..0fc788fdeb 100644
--- a/plugins/ruflo-mods/README.md
+++ b/plugins/ruflo-mods/README.md
@@ -99,7 +99,7 @@ Claude Code reads a plugin's options from `pluginConfigs["ruflo-mods@ruflo"].opt
| `sessionAttention` | `false` | Observability only (ADR-486): one `attention:` row in `/ruflo-mods` with ruflo-console's four counts (to approve, questions, failed, finished unread), read from `.claude-flow/console/attention.json`. Counts only; nothing gates, denies or delays. |
| `selfCheck` | `true` | Startup self-check (ADR-404 addendum): once per session, a toast when a ruflo plugin your settings enable is missing from the ruflo marketplace clone with no cached install (Claude Code then skips it without a word), with the one-line repair. Read-only; fixed text; never adds anything for the model. |
| `grounding` | `false` | Status only (ADR-487): `/ruflo-mods` gains `grounding: RuvNet Brain: on / not installed / installed, disabled / unknown`, read from `installed_plugins.json` and your settings. It detects Stuart Kerr's optional third-party [ruvnet-brain](https://github.com/stuinfla/ruvnet-brain) plugin by name only; it never calls, installs, blocks or rewrites anything, and ruflo works the same without it |
-| `modTrust` | `observe` | `observe` / `refuse-risky` / `off`: the mod trust gate. Evidence: [capability review](../../v3/docs/validation/mod-capability-review-2026-10.md) |
+| `modTrust` | `observe` | `observe` / `refuse-risky` / `off`: the mod trust gate. Risky: host commands, network, MCP tools, messages to other agents, settings, environment and file writes, starting agents, submitting prompts or running tools; hooks on tool verdicts, tool calls, prompts, agent spawns, tool descriptions, the transcript, model requests, compaction, other mods, the file reads, settings and environment other mods decide by, or on any of those calls (a hook can rewrite another mod's call). The gate is a list over the engine API as of Claude Code 2.1.289: an event added later is not judged until it is listed. A glob or `!` negation is judged by what it selects. Evidence: [capability review](../../v3/docs/validation/mod-capability-review-2026-10.md) |
| `modTrustAllow` | empty | Comma-separated plugin ids (`name@marketplace`, e.g. `ruflo-swarm@ruflo,ruflo-console@ruflo`) the gate never refuses. Under `refuse-risky`, list every ruflo mod you use: nearly all write a status file or hook `tool.call`. Evidence: [capability review](../../v3/docs/validation/mod-capability-review-2026-10.md) |
| `guidanceContext` | `false` | Screened lexical excerpts (at most five, 4096 characters) from a compiler-exported guidance projection; advisory, cannot authorize tool calls. Design in [ADR-447](../../v3/docs/adr/ADR-447-native-mod-guidance-observation-loop.md); evidence: [native guidance](../../v3/docs/validation/ADR-447-native-guidance-evidence.md) |
| `guidanceLearning` | `false` | Unverified activity observations for independent review; no training or promotion. Evidence: [native guidance](../../v3/docs/validation/ADR-447-native-guidance-evidence.md) |
diff --git a/plugins/ruflo-mods/hooks/trust.ts b/plugins/ruflo-mods/hooks/trust.ts
index 83e56d639f..24a664ccee 100644
--- a/plugins/ruflo-mods/hooks/trust.ts
+++ b/plugins/ruflo-mods/hooks/trust.ts
@@ -32,9 +32,14 @@ const RISKY_CALLS: Record<string, string> = {
'mcp.connect': 'connects MCP servers',
'session.send': 'sends messages to other agents',
'config.set': 'changes Claude Code settings',
+ 'agent.spawn': 'starts subagents (they run tools)',
+ 'agent.register': 'defines agent types the model can start',
+ 'prompt.submit': 'submits prompts the model acts on',
+ 'session.append': 'adds rows to the conversation',
+ 'tool.call': 'runs any tool (Bash included)',
+ 'command.run': 'runs slash commands',
'env.set': 'changes the environment of later hooks and tools',
'fs.write': 'writes files (settings, hooks, helpers included)',
- 'agent.spawn': 'starts agents with a prompt of its own',
}
/** Hooks that decide for, or over, everything else. */
@@ -47,23 +52,57 @@ const RISKY_EVENTS: Record<string, string> = {
'prompt.compose': 'can rewrite the system prompt',
'prompt.submit': 'can add to or rewrite every prompt you send',
'agent.spawn': 'can rewrite or answer every agent spawn',
+ 'tool.describe': 'can rewrite the tool descriptions the model reads',
+ 'session.append': 'can rewrite every row added to the conversation',
+ 'prompt.section': 'can rewrite system prompt sections',
+ 'prompt.context': 'can rewrite the context added to prompts',
+ 'prompt.attachment': 'can rewrite prompt attachments',
+ 'skill.prompt': 'can rewrite skill prompts',
+ 'session.receive': 'can rewrite or drop messages from other agents',
+ 'turn.step': 'can rewrite or answer every model request',
+ 'tool.register': 'can rewrite tools other mods register',
+ 'session.compact': 'can replace the whole conversation at compaction',
+ 'prompt.edit': 'can rewrite what the person types',
+ 'attribution.text': 'can rewrite git text the model reads',
+ // Reads other mods decide by: forging them can switch off an enforced policy (the projection reads as absent).
+ 'fs.read': "can feed other mods false file contents (ruflo's policy projection included)",
+ 'fs.stat': "can hide files from other mods (ruflo's policy projection included)",
+ 'fs.exists': 'can hide files from other mods',
+ 'fs.list': 'can hide files from other mods',
+ 'settings.read': 'can forge the settings other mods decide by',
+ 'env.get': 'can forge the environment other mods decide by',
+ 'session.root': "can move other mods' project root (ruflo's policy projection read from elsewhere)",
+ 'session.cwd': 'can move the directory other mods resolve paths from',
+ 'fs.ancestors': 'can hide parent directories from other mods',
+ 'command.register': 'can rewrite commands other mods register',
+}
+
+/**
+ * Every call on `$` is also an event a hook above the caller can rewrite or
+ * answer for every other mod, so a hook on a risky call is a risky hook too
+ * (a hook on fs.write can redirect another mod's write; one on env.set can change
+ * the value it sets).
+ */
+const HOOK_RISK: Record<string, string> = {
+ ...Object.fromEntries(Object.entries(RISKY_CALLS).map(([call, does]) => [call, `can rewrite or answer another mod's ${call}, which ${does}`])),
+ ...RISKY_EVENTS,
}
/**
* The risky events a registered pattern reaches. The scan reports patterns as
- * written, so a glob (`tool.*`) or a negation (`!tool.describe`: every event
- * but one) must be judged by what it selects, not by how it is spelled.
+ * written, so a glob (`tool.*`) or a negation (`!tool.describe`, every event
+ * but one) must be judged by what it selects, not by its spelling.
*/
const escapeRe = (text: string) => text.replace(/[.+?^$()|[\]{}\\]/g, '\\$&')
function riskyEventsOf(pattern: string): string[] {
- if (Object.hasOwn(RISKY_EVENTS, pattern)) return [pattern]
+ if (Object.hasOwn(HOOK_RISK, pattern)) return [pattern]
if (pattern.startsWith('!')) return ['*']
- // A settings hook by name, or a glob of them, can answer any of them.
+ // Any settings hook, one by name or a glob of them, can answer it.
if (pattern.startsWith('classic.')) return ['classic.*']
if (!pattern.includes('*')) return []
const glob = new RegExp(`^${pattern.split('*').map(escapeRe).join('.*')}$`)
- return Object.keys(RISKY_EVENTS).filter(name => name !== '*' && name !== 'classic.*' && glob.test(name))
+ return Object.keys(HOOK_RISK).filter(name => name !== '*' && name !== 'classic.*' && glob.test(name))
}
const isStrings = (v: unknown): v is readonly string[] => Array.isArray(v) && v.every(s => typeof s === 'string')
@@ -74,7 +113,7 @@ export function riskOf(scan: ModuleScan): string[] {
const events = isStrings(scan.uses?.events) ? scan.uses.events : []
return [
...calls.filter(c => Object.hasOwn(RISKY_CALLS, c)).map(c => `${c} (${RISKY_CALLS[c]})`),
- ...events.flatMap(ev => riskyEventsOf(ev).map(name => `on ${ev === name ? ev : `${ev} β†’ ${name}`} (${RISKY_EVENTS[name]})`)),
+ ...events.flatMap(ev => riskyEventsOf(ev).map(name => `on ${ev === name ? ev : `${ev} β†’ ${name}`} (${HOOK_RISK[name]})`)),
]
}
diff --git a/plugins/ruflo-mods/tests/trust.test.ts b/plugins/ruflo-mods/tests/trust.test.ts
index ae21ea5cf9..bbd22e36cb 100644
--- a/plugins/ruflo-mods/tests/trust.test.ts
+++ b/plugins/ruflo-mods/tests/trust.test.ts
@@ -118,8 +118,39 @@ const spawnSteer: Plugin = {
},
}
+const callingPlugin = (name: string, call: (on: Parameters<Plugin['register']>[0]) => void): Plugin => ({ name, tier: 'user', register: call })
+/** Starts a subagent: it runs tools, Bash included. */
+const agentStarter = callingPlugin('agent-starter', on => {
+ on('turn.complete', async ($, e, next) => {
+ await $.agent.spawn({ prompt: 'p', description: 'd', subagentType: 'coder' } as never)
+ return next(e)
+ })
+})
+/** Submits a prompt the model then acts on. */
+const promptSubmitter = callingPlugin('prompt-submitter', on => {
+ on('turn.complete', async ($, e, next) => {
+ await $.prompt.submit({ text: 'p' } as never)
+ return next(e)
+ })
+})
+/** Runs any tool through the permission check. */
+const toolCaller = callingPlugin('tool-caller', on => {
+ on('turn.complete', async ($, e, next) => {
+ await $.tool.call({ tool: 'Bash', input: { command: 'true' } } as never)
+ return next(e)
+ })
+})
+/** Rewrites the tool descriptions the model reads. */
+const describer = callingPlugin('describer', on => {
+ on('tool.describe', ($, e, next) => next(e))
+})
+
describe('trust: every call and hook that reaches outside the session is risky', () => {
for (const [plugin, why] of [
+ [agentStarter, /agent\.spawn \(starts subagents/],
+ [promptSubmitter, /prompt\.submit \(submits prompts the model acts on\)/],
+ [toolCaller, /tool\.call \(runs any tool/],
+ [describer, /on tool\.describe \(can rewrite the tool descriptions/],
[spawner, /process\.spawn \(runs host commands\)/],
[mcpCaller, /mcp\.call \(calls MCP tools/],
[promptSteer, /on prompt\.submit \(can add to or rewrite every prompt you send\)/],
@@ -156,6 +187,36 @@ describe('trust: a glob or a negation is judged by what it selects (#3801)', ()
})
}
+ const writeHook: Plugin = { name: 'write-hook', tier: 'user', register: on => { on('fs.write', ($, e, next) => next(e)) } }
+ const envHook: Plugin = { name: 'env-hook', tier: 'user', register: on => { on('env.set', ($, e, next) => next(e)) } }
+ const fsGlob: Plugin = { name: 'fs-glob', tier: 'user', register: on => { on('fs.*', ($, e, next) => next(e)) } }
+ for (const [plugin, why] of [
+ [writeHook, /on fs\.write \(can rewrite or answer another mod's fs\.write, which writes files/],
+ [envHook, /on env\.set \(can rewrite or answer another mod's env\.set, which changes/],
+ [fsGlob, /on fs\.\* β†’ fs\.write/],
+ ] as const) {
+ test(`refuse-risky refuses ${plugin.name}: hooking a risky call rewrites it for every other mod`, { plugins: [plugin], options: { modTrust: 'refuse-risky' } }, async ($, on) => {
+ world(on)
+ await expect($.session.start(START)).rejects.toThrow(new RegExp(`${plugin.name}: refused by ruflo-mods: .*${why.source}`))
+ })
+ }
+
+ const statForger: Plugin = { name: 'stat-forger', tier: 'user', register: on => { on('fs.stat', ($, e, next) => next(e)) } }
+ const readForger: Plugin = { name: 'read-forger', tier: 'user', register: on => { on('fs.read', ($, e, next) => next(e)) } }
+ const rootForger: Plugin = { name: 'root-forger', tier: 'user', register: on => { on('session.root', ($, e, next) => next(e)) } }
+ const compactForger: Plugin = { name: 'compact-forger', tier: 'user', register: on => { on('session.compact', ($, e, next) => next(e)) } }
+ for (const [plugin, why] of [
+ [statForger, /on fs\.stat \(can hide files from other mods/],
+ [readForger, /on fs\.read \(can feed other mods false file contents/],
+ [compactForger, /on session\.compact \(can replace the whole conversation/],
+ [rootForger, /on session\.root \(can move other mods' project root/],
+ ] as const) {
+ test(`refuse-risky refuses ${plugin.name}: a forged read or compaction is a risky hook`, { plugins: [plugin], options: { modTrust: 'refuse-risky' } }, async ($, on) => {
+ world(on)
+ await expect($.session.start(START)).rejects.toThrow(new RegExp(`${plugin.name}: refused by ruflo-mods: .*${why.source}`))
+ })
+ }
+
test('a glob that selects nothing risky still loads', { plugins: [clockGlob], options: { modTrust: 'refuse-risky' } }, async ($, on) => {
const w = world(on)
await $.session.start(START)
diff --git a/plugins/ruflo-plugin-creator/skills/create-mod/SKILL.md b/plugins/ruflo-plugin-creator/skills/create-mod/SKILL.md
index 246f5d04ec..29101de781 100644
--- a/plugins/ruflo-plugin-creator/skills/create-mod/SKILL.md
+++ b/plugins/ruflo-plugin-creator/skills/create-mod/SKILL.md
@@ -30,4 +30,4 @@ Scaffold a mod the governed way, instead of an ad-hoc one: the template already
claude -p --plugin-dir <dir> "/<name>-status" # writes .claude-plugin/types/ and proves a live load
npx tsc -p <dir> # against those generated types
```
-8. **Ship it in a plugin marketplace** (`marketplace.json` entry), installed with `/plugin install <name>@<marketplace>` and picked up with `/reload-plugins`. If ruflo-mods is installed with `modTrust: refuse-risky`, a mod using `process.run`, `http.fetch`, `env.set` or `fs.write`, or hooking `tool.check`/`tool.call`/`*`, is refused unless its plugin id (`<name>@<marketplace>`) is in `modTrustAllow` β€” the template sets `env.set` (the fallback handshake), so allow-list it by id.
+8. **Ship it in a plugin marketplace** (`marketplace.json` entry), installed with `/plugin install <name>@<marketplace>` and picked up with `/reload-plugins`. If ruflo-mods is installed with `modTrust: refuse-risky`, a mod using `process.run`/`process.spawn`, `http.fetch`, `mcp.call`/`mcp.connect`, `session.send`, `config.set`, `env.set`, `fs.write`, `agent.spawn`/`agent.register`, `prompt.submit`, `session.append`, `tool.call` or `command.run`, or hooking any event in its `RISKY_EVENTS` (`tool.check`, `tool.call`, `prompt.*`, `agent.spawn`, `tool.describe`, `session.append`, `session.receive`, `turn.step`, `skill.prompt`, `session.compact`, `prompt.edit`, `fs.read`/`fs.stat`/`fs.exists`/`fs.list`, `settings.read`, `env.get`, a hook on any of the calls above (it can rewrite another mod's call), `plugin.register`, `classic.*`, `*`; a glob or a `!` negation is judged by what it selects), is refused unless its plugin id (`<name>@<marketplace>`) is in `modTrustAllow` β€” the template sets `env.set` (the fallback handshake), so allow-list it by id.
diff --git a/scripts/ci-test-excluded.txt b/scripts/ci-test-excluded.txt
index e12efc5cfe..8a878b45aa 100644
--- a/scripts/ci-test-excluded.txt
+++ b/scripts/ci-test-excluded.txt
@@ -81,6 +81,7 @@ tests/plugins/codex-mod-hooks.test.mjs
tests/plugins/codex-swarm-hook-manifest.test.mjs
scripts/__tests__/guard-label.test.mjs
scripts/__tests__/mod-capability-matrix.test.mjs
+tests/all-plugins-smoke-workflow.test.mjs
tests/check-guard-tool-names.test.mjs
tests/check-status-forwarding.test.mjs
plugins/ruflo-console/tests/result-display.test.ts
diff --git a/tests/all-plugins-smoke-workflow.test.mjs b/tests/all-plugins-smoke-workflow.test.mjs
new file mode 100644
index 0000000000..b454067499
--- /dev/null
+++ b/tests/all-plugins-smoke-workflow.test.mjs
@@ -0,0 +1,64 @@
+import assert from 'node:assert/strict';
+import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { spawnSync } from 'node:child_process';
+import { test } from 'node:test';
+import { createRequire } from 'node:module';
+
+// CI provisions pinned YAML into its isolated tool prefix. CommonJS resolution
+// honors the step-scoped NODE_PATH without installing into the source checkout.
+const { parse } = createRequire(import.meta.url)('yaml');
+
+const workflow = parse(readFileSync(new URL('../.github/workflows/all-plugins-smoke.yml', import.meta.url), 'utf8'));
+const ownRunnerFiles = readFileSync(new URL('../scripts/ci-test-excluded.txt', import.meta.url), 'utf8')
+ .split(/\r?\n/).filter(line => line && !line.startsWith('#'));
+const job = workflow.jobs['smoke-all'];
+const producers = job.steps.filter(step => step.run?.includes('node scripts/smoke-all-plugins.mjs'));
+
+test('node:test regression is registered with its explicit CI runner', () => {
+ assert.equal(ownRunnerFiles.filter(file => file === 'tests/all-plugins-smoke-workflow.test.mjs').length, 1);
+ assert.equal(job.steps.filter(step => step.run?.includes('node --test tests/check-guard-tool-names.test.mjs tests/all-plugins-smoke-workflow.test.mjs')).length, 1);
+});
+
+test('smoke workflow runs one suite within the unchanged deadline and always uploads its result', () => {
+ const provision = job.steps.find(step => step.run?.includes('npm install --prefix'));
+ assert.match(provision.run, /yaml@2\.8\.0\b/);
+ const regression = job.steps.find(step => step.run?.includes('node --test tests/check-guard-tool-names.test.mjs'));
+ assert.equal(regression.env.NODE_PATH, '${{ runner.temp }}/esbuild/node_modules');
+ assert.equal(job['timeout-minutes'], 10);
+ assert.equal(producers.length, 1);
+ assert.equal(producers[0].shell, 'bash');
+ assert.match(producers[0].run, /--timeout 300\b/);
+ assert.match(producers[0].run, /--format json\b/);
+ const upload = job.steps.find(step => step.uses?.startsWith('actions/upload-artifact@'));
+ assert.equal(upload.if, 'always()');
+ assert.equal(upload.with.path, '/tmp/smoke-all-plugins.json');
+ assert.match(producers[0].run, /\| tee \/tmp\/smoke-all-plugins\.json$/);
+});
+
+for (const exitCode of [0, 1]) {
+ test(`smoke pipeline preserves exit ${exitCode} and records the same single execution`, () => {
+ const dir = mkdtempSync(join(tmpdir(), 'smoke-workflow-'));
+ try {
+ mkdirSync(join(dir, 'scripts'));
+ writeFileSync(join(dir, 'scripts/smoke-all-plugins.mjs'), `
+ import { appendFileSync } from 'node:fs';
+ appendFileSync('calls.txt', 'run\\n');
+ console.log(JSON.stringify({ plugins: [], summary: { pluginsFailed: ${exitCode} } }));
+ process.exit(${exitCode});
+ `);
+ const report = join(dir, 'report.json');
+ // Exercise the workflow's actual command with only its absolute artifact
+ // destination relocated. GitHub's explicit bash shell uses these flags.
+ const command = producers[0].run.replace('/tmp/smoke-all-plugins.json', report);
+ const result = spawnSync('bash', ['--noprofile', '--norc', '-e', '-o', 'pipefail', '-c', command], { cwd: dir, encoding: 'utf8' });
+ assert.equal(result.status, exitCode, result.stderr);
+ assert.equal(readFileSync(join(dir, 'calls.txt'), 'utf8'), 'run\n');
+ assert.equal(JSON.parse(readFileSync(report, 'utf8')).summary.pluginsFailed, exitCode);
+ assert.equal(result.stdout, readFileSync(report, 'utf8'));
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+}
diff --git a/v3/@claude-flow/cli/__tests__/router-bandit.test.ts b/v3/@claude-flow/cli/__tests__/router-bandit.test.ts
index 37efb6c6df..375f60fa97 100644
--- a/v3/@claude-flow/cli/__tests__/router-bandit.test.ts
+++ b/v3/@claude-flow/cli/__tests__/router-bandit.test.ts
@@ -144,35 +144,47 @@ describe('ModelRouter β€” Thompson sampling bandit (#1772, ADR-142 bucketed)', (
expect(router.getBanditPriors('high').haiku).toEqual({ alpha: 9, beta: 2 }); // seeded
});
- it('converges toward Haiku on a low-complexity workload (~50 trials)', async () => {
+ it('converges toward Haiku on a low-complexity workload (100 seeded trials)', async () => {
const router = new ModelRouter();
- let seed = 0x1234567;
- const rng = () => {
- seed |= 0;
- seed = (seed + 0x6D2B79F5) | 0;
- let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
- t = t + Math.imul(t ^ (t >>> 7), 61 | t) ^ t;
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+ const seededRandom = (initialSeed: number) => {
+ let seed = initialSeed;
+ return () => {
+ seed |= 0;
+ seed = (seed + 0x6D2B79F5) | 0;
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
+ t = t + Math.imul(t ^ (t >>> 7), 61 | t) ^ t;
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+ };
};
+ const rng = seededRandom(0x1234567);
+ // Seed exploration too: the real sampleBeta Gamma/Box-Muller path uses
+ // Math.random. Outcome seeds alone left this finite-horizon assertion
+ // dependent on an unrelated random trajectory (CI failure on #3802).
+ // Independent streams keep sampler rejection counts out of outcome draws.
+ const samplerRandom = vi.spyOn(Math, 'random').mockImplementation(seededRandom(0x2468ace));
const N = 100;
let haikuPicked = 0;
let opusPicked = 0;
- for (let i = 0; i < N; i++) {
- const complexity = 0.1 + rng() * 0.4; // 0.1 .. 0.5, avg ~0.3
- const task = `simple task ${i}`;
- const r = await router.route(task);
- if (r.model === 'haiku') haikuPicked++;
- if (r.model === 'opus') opusPicked++;
- const outcome = syntheticOutcome(r.model as 'haiku' | 'sonnet' | 'opus', complexity, rng);
- router.recordOutcome(task, r.model, outcome);
- }
+ try {
+ for (let i = 0; i < N; i++) {
+ const complexity = 0.1 + rng() * 0.4; // 0.1 .. 0.5, avg ~0.3
+ const task = `simple task ${i}`;
+ const r = await router.route(task);
+ if (r.model === 'haiku') haikuPicked++;
+ if (r.model === 'opus') opusPicked++;
+ const outcome = syntheticOutcome(r.model as 'haiku' | 'sonnet' | 'opus', complexity, rng);
+ router.recordOutcome(task, r.model, outcome);
+ }
- const priors = aggregatePriors(router);
- const meanHaiku = priors.haiku.alpha / (priors.haiku.alpha + priors.haiku.beta);
- const meanOpus = priors.opus.alpha / (priors.opus.alpha + priors.opus.beta);
- expect(meanHaiku).toBeGreaterThan(meanOpus);
- expect(haikuPicked).toBeGreaterThan(opusPicked);
+ const priors = aggregatePriors(router);
+ const meanHaiku = priors.haiku.alpha / (priors.haiku.alpha + priors.haiku.beta);
+ const meanOpus = priors.opus.alpha / (priors.opus.alpha + priors.opus.beta);
+ expect(meanHaiku).toBeGreaterThan(meanOpus);
+ expect(haikuPicked).toBeGreaterThan(opusPicked);
+ } finally {
+ samplerRandom.mockRestore();
+ }
}, 30_000);
it('does not lock in early β€” recovers from a bad initial draw', async () => {
diff --git a/v3/plugins/hyperbolic-reasoning/src/mcp-tools.ts b/v3/plugins/hyperbolic-reasoning/src/mcp-tools.ts
index a60b8e12fc..011820bdfa 100644
--- a/v3/plugins/hyperbolic-reasoning/src/mcp-tools.ts
+++ b/v3/plugins/hyperbolic-reasoning/src/mcp-tools.ts
@@ -9,6 +9,8 @@
* - hyperbolic/entailment-graph: Build and query entailment graphs
*/
+import { randomUUID } from 'node:crypto';
+
import type {
MCPTool,
MCPToolResult,
@@ -102,7 +104,7 @@ async function embedHierarchyHandler(
const result = await bridge.embedHierarchy(data.hierarchy, data.parameters);
// Store for later use
- const indexId = `embed_${Date.now()}`;
+ const indexId = `embed_${randomUUID()}`;
embeddingStore.set(indexId, result);
// Create index for search
diff --git a/v3/plugins/hyperbolic-reasoning/tests/mcp-tools.test.ts b/v3/plugins/hyperbolic-reasoning/tests/mcp-tools.test.ts
index ae6d54cec0..44921e7cc1 100644
--- a/v3/plugins/hyperbolic-reasoning/tests/mcp-tools.test.ts
+++ b/v3/plugins/hyperbolic-reasoning/tests/mcp-tools.test.ts
@@ -117,6 +117,22 @@ describe('hyperbolic_embed_hierarchy handler', () => {
expect(result.isError).toBeUndefined();
});
+ it('keeps same-millisecond hierarchy indexes distinct', async () => {
+ const clock = vi.spyOn(Date, 'now').mockReturnValue(1900000000000);
+ try {
+ const hierarchy = { nodes: [{ id: 'root', parent: null }] };
+ const first = await tool.handler({ hierarchy, model: 'poincare_ball' });
+ const second = await tool.handler({ hierarchy, model: 'klein' });
+ expect(first.isError).toBeUndefined();
+ expect(second.isError).toBeUndefined();
+ const firstData = JSON.parse(first.content[0].text);
+ const secondData = JSON.parse(second.content[0].text);
+ expect(firstData.indexId).not.toBe(secondData.indexId);
+ } finally {
+ clock.mockRestore();
+ }
+ });
+
it('should handle klein model', async () => {
const input = {
hierarchy: {