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(policy): keep unmetered worker ceilings opt-in

Rudy Celeklidbdceb6184b9
diff --git a/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts b/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts
index 9f5bd27154..7188d07f88 100644
--- a/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts
+++ b/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { mkdtempSync, rmSync } from 'node:fs';
+import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { execFileSync } from 'node:child_process';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
const state = vi.hoisted(() => ({ root: '' }));
@@ -14,10 +15,42 @@ vi.mock('@claude-flow/security', async () => ({
...await import('../../security/src/mcp-caller-identity.js'),
}));
import { policyTools } from '../src/mcp-tools/policy-tools.js';
-import { setPolicyMode, upsertPolicyRule, verifyPolicyLedger } from '../src/services/policy-runtime.js';
+import { loadPolicyState, setPolicyMode, upsertPolicyRule, verifyPolicyLedger } from '../src/services/policy-runtime.js';
+import { callMCPTool } from '../src/mcp-client.js';
+import { configTools } from '../src/mcp-tools/config-tools.js';
+import { DualModeOrchestrator, type WorkerConfig } from '../../codex/src/dual-mode/orchestrator.js';
+import { delegateEnvelope, type CapabilityEnvelope } from '../../security/src/policy/index.js';
const evaluate = policyTools.find(t => t.name === 'policy_evaluate')!;
beforeEach(() => { state.root = mkdtempSync(join(tmpdir(), 'ruflo-envelope-meter-')); });
afterEach(() => rmSync(state.root, { recursive: true, force: true }));
+
+async function asWorker(envelope: CapabilityEnvelope | undefined, action: () => Promise<void>): Promise<void> {
+ execFileSync('git', ['init', '-q', state.root]);
+ const orchestrator = new DualModeOrchestrator({ projectPath: state.root }) as unknown as {
+ workerEnvironment(worker: WorkerConfig): NodeJS.ProcessEnv;
+ };
+ const environment = orchestrator.workerEnvironment({
+ id: 'ordinary-worker', platform: 'codex', role: 'reader', prompt: 'inspect',
+ readOnly: true, capabilityEnvelope: envelope,
+ });
+ const names = ['CLAUDE_FLOW_CAPABILITY_ENVELOPE', 'CLAUDE_FLOW_PRINCIPAL_ID', 'CLAUDE_FLOW_MCP_CALLER_AUTH'] as const;
+ const previous = names.map(name => process.env[name]);
+ const cwd = process.cwd();
+ try {
+ process.chdir(state.root);
+ process.env.CLAUDE_FLOW_CAPABILITY_ENVELOPE = environment.CLAUDE_FLOW_CAPABILITY_ENVELOPE;
+ process.env.CLAUDE_FLOW_PRINCIPAL_ID = environment.CLAUDE_FLOW_PRINCIPAL_ID;
+ process.env.CLAUDE_FLOW_MCP_CALLER_AUTH = 'false';
+ await action();
+ } finally {
+ process.chdir(cwd);
+ names.forEach((name, index) => {
+ if (previous[index] === undefined) delete process.env[name];
+ else process.env[name] = previous[index];
+ });
+ }
+}
+
for (const mode of ['legacy', 'observe', 'enforce'] as const) {
describe(`${mode} public policy evaluation`, () => {
it.each(['maxCostUsd', 'maxTokens', 'maxConcurrency'])('does not bypass %s by omitting metering', async ceiling => {
@@ -40,5 +73,60 @@ for (const mode of ['legacy', 'observe', 'enforce'] as const) {
context: { envelope: { maxCostUsd: 0, maxTokens: 0, maxConcurrency: 0 } },
} }, { projectRoot: state.root })).toMatchObject({ enforcedOutcome: 'allowed' });
});
+
+ it('admits an ordinary generated worker envelope through real MCP dispatch', async () => {
+ await upsertPolicyRule({ id: 'allow-fixture', effect: 'allow', actions: ['mcp.tool.call'] }, state.root);
+ await setPolicyMode(mode, state.root);
+ const tool = configTools.find(t => t.name === 'config_set')!;
+ const handler = vi.spyOn(tool, 'handler');
+ try {
+ await asWorker(undefined, async () => {
+ await expect(callMCPTool('config_set', { key: 'workerProof', value: 'admitted' }, { projectRoot: state.root }))
+ .resolves.toMatchObject({ success: true });
+ expect(handler).toHaveBeenCalledOnce();
+ expect(readFileSync(join(state.root, '.claude-flow', 'config.json'), 'utf8')).toContain('admitted');
+ const receipt = loadPolicyState(state.root).receipts.at(-1)!;
+ expect(receipt.payload.request.identity.id).toBe('agent:ordinary-worker');
+ expect(receipt.payload.request.action.costUsd).toBeUndefined();
+ expect(receipt.payload.request.action.tokens).toBeUndefined();
+ expect(receipt.payload.request.action.concurrency).toBeUndefined();
+ expect(receipt.payload.request.context?.envelope?.maxConcurrency).toBeUndefined();
+ expect(receipt.payload.decision.enforcedOutcome).toBe('allowed');
+ });
+ } finally {
+ handler.mockRestore();
+ }
+ });
+
+ it.each([
+ ['maxCostUsd', 'cost-outside-envelope'],
+ ['maxTokens', 'tokens-outside-envelope'],
+ ['maxConcurrency', 'concurrency-outside-envelope'],
+ ] as const)('preserves an opted-in %s and denies an unmetered worker before its handler', async (ceiling, reason) => {
+ await upsertPolicyRule({ id: 'allow-fixture', effect: 'allow', actions: ['mcp.tool.call'] }, state.root);
+ await setPolicyMode(mode, state.root);
+ const tool = configTools.find(t => t.name === 'config_set')!;
+ const handler = vi.spyOn(tool, 'handler');
+ const requested: CapabilityEnvelope = { actions: ['*'], [ceiling]: 1 };
+ try {
+ await asWorker(requested, async () => {
+ await expect(callMCPTool('config_set', { key: 'workerProof', value: 'must-not-run' }, { projectRoot: state.root }))
+ .rejects.toThrow(reason);
+ expect(handler).not.toHaveBeenCalled();
+ expect(existsSync(join(state.root, '.claude-flow', 'config.json'))).toBe(false);
+ const receipt = loadPolicyState(state.root).receipts.at(-1)!;
+ expect(receipt.payload.request.context?.envelope?.[ceiling]).toBe(1);
+ expect(receipt.payload.decision.enforcedOutcome).toBe('denied');
+ });
+ } finally {
+ handler.mockRestore();
+ }
+ });
});
}
+
+it.each(['maxCostUsd', 'maxTokens', 'maxConcurrency'] as const)('retains an explicit %s during delegation', ceiling => {
+ const parent: CapabilityEnvelope = { actions: ['*'], [ceiling]: 1, delegationDepth: 1 };
+ expect(delegateEnvelope(parent, { actions: ['memory.read'] })[ceiling]).toBe(1);
+ expect(() => delegateEnvelope(parent, { [ceiling]: 2 })).toThrow('capability-envelope-cannot-grow');
+});
diff --git a/v3/@claude-flow/codex/src/dual-mode/orchestrator.ts b/v3/@claude-flow/codex/src/dual-mode/orchestrator.ts
index 798e3ea265..bb0593d91a 100644
--- a/v3/@claude-flow/codex/src/dual-mode/orchestrator.ts
+++ b/v3/@claude-flow/codex/src/dual-mode/orchestrator.ts
@@ -20,6 +20,7 @@ export interface WorkerCapabilityEnvelope {
actions?: string[];
resources?: string[];
tools?: string[];
+ /** Opt-in per-tool ceiling: requires a trusted concurrency meter at dispatch. */
maxConcurrency?: number;
network?: boolean;
destructive?: boolean;
@@ -561,7 +562,9 @@ Remember: Other agents depend on your results in shared memory. Be concise and s
actions: ['*'],
resources: ['*'],
tools: ['*'],
- maxConcurrency: 1,
+ // MCP dispatch does not yet supply a worker-wide concurrency meter.
+ // Leave this dimension unconstrained unless the caller opts in; the
+ // scheduler still bounds worker processes with config.maxConcurrent.
network: false,
destructive: false,
delegationDepth: 0,
diff --git a/v3/docs/adr/ADR-324-agentic-policy-engine-codex-swarm.md b/v3/docs/adr/ADR-324-agentic-policy-engine-codex-swarm.md
index 74649df7fa..9c8b744598 100644
--- a/v3/docs/adr/ADR-324-agentic-policy-engine-codex-swarm.md
+++ b/v3/docs/adr/ADR-324-agentic-policy-engine-codex-swarm.md
@@ -80,6 +80,13 @@ Unmatched actions allow only in `legacy`; `observe` records default-deny but doe
An envelope may constrain actions, resources, tools, MCP servers, read/write namespaces, environments, network/destructive access, cost, tokens, concurrency, expiry, and delegation depth.
+Cost, token, and per-tool concurrency ceilings are opt-in: omit a dimension
+when no trusted dispatch meter is available. An explicitly supplied ceiling
+requires its meter, including in `legacy` and `observe` modes; missing usage
+is not zero. Dual-mode workers do not receive an implicit per-tool concurrency
+ceiling. The orchestrator's `maxConcurrent` scheduler limit still bounds worker
+processes and does not measure concurrent MCP calls across those processes.
+
Every child envelope must be a monotonic reduction. A child cannot:
- add a pattern not granted by its parent;