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): require metering for explicit envelope ceilings

Rudy Celeklid216230a74b3
diff --git a/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts b/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts
new file mode 100644
index 0000000000..9f5bd27154
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/policy-envelope-metering.test.ts
@@ -0,0 +1,44 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+const state = vi.hoisted(() => ({ root: '' }));
+vi.mock('node:os', async importOriginal => {
+ const actual = await importOriginal<typeof import('node:os')>();
+ return { ...actual, userInfo: () => ({ ...actual.userInfo(), homedir: state.root }) };
+});
+// Exercise the actual policy and caller-identity exports without loading
+// unrelated password-hashing/native security dependencies in this fixture.
+vi.mock('@claude-flow/security', async () => ({
+ ...await import('../../security/src/policy/index.js'),
+ ...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';
+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 }));
+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 => {
+ await upsertPolicyRule({ id: 'allow-fixture', effect: 'allow', actions: ['*'] }, state.root);
+ await setPolicyMode(mode, state.root);
+ const result = await evaluate.handler({ request: {
+ identity: { id: 'agent:fixture', type: 'agent' },
+ action: { type: 'memory.read', namespace: 'notes' },
+ context: { envelope: { [ceiling]: 1 } },
+ } }, { projectRoot: state.root });
+ expect(result).toMatchObject({ outcome: 'denied', enforcedOutcome: 'denied' });
+ expect(await verifyPolicyLedger(state.root)).toMatchObject({ valid: true });
+ });
+ it('permits explicitly metered zero-cost reads within zero ceilings', async () => {
+ await upsertPolicyRule({ id: 'allow-fixture', effect: 'allow', actions: ['*'] }, state.root);
+ await setPolicyMode(mode, state.root);
+ expect(await evaluate.handler({ request: {
+ identity: { id: 'agent:fixture', type: 'agent' },
+ action: { type: 'memory.read', namespace: 'notes', costUsd: 0, tokens: 0, concurrency: 0 },
+ context: { envelope: { maxCostUsd: 0, maxTokens: 0, maxConcurrency: 0 } },
+ } }, { projectRoot: state.root })).toMatchObject({ enforcedOutcome: 'allowed' });
+ });
+ });
+}
diff --git a/v3/@claude-flow/security/__tests__/agentic-policy-engine.test.ts b/v3/@claude-flow/security/__tests__/agentic-policy-engine.test.ts
index 4e474dddd9..53969caa8b 100644
--- a/v3/@claude-flow/security/__tests__/agentic-policy-engine.test.ts
+++ b/v3/@claude-flow/security/__tests__/agentic-policy-engine.test.ts
@@ -266,19 +266,22 @@ describe('CapabilityEnvelope', () => {
resource: 'src/app.ts',
tool: 'apply_patch',
concurrency: 2,
+ costUsd: 0,
}, parent, 1_000).allowed).toBe(true);
expect(checkCapabilityEnvelope({
type: 'code.write',
resource: 'src/app.ts',
tool: 'apply_patch',
network: true,
+ costUsd: 0,
+ concurrency: 1,
}, parent, 1_000)).toEqual({ allowed: false, reason: 'network-outside-envelope' });
});
it('enforces delegated envelope boundaries even in legacy mode', () => {
const engine = new AgenticPolicyEngine({ mode: 'legacy', now: () => 1_000 });
const decision = engine.evaluate(request({
- action: { type: 'code.write', resource: 'src/app.ts', tool: 'apply_patch', network: true },
+ action: { type: 'code.write', resource: 'src/app.ts', tool: 'apply_patch', network: true, costUsd: 0, concurrency: 1 },
context: { envelope: parent },
}));
expect(decision.outcome).toBe('denied');
diff --git a/v3/@claude-flow/security/src/policy/envelope.ts b/v3/@claude-flow/security/src/policy/envelope.ts
index 4b3fb36e14..c28409571b 100644
--- a/v3/@claude-flow/security/src/policy/envelope.ts
+++ b/v3/@claude-flow/security/src/policy/envelope.ts
@@ -43,14 +43,14 @@ export function checkCapabilityEnvelope(
: envelope.writeNamespaces;
checks.push([matches(namespaces, action.namespace), 'namespace-outside-envelope']);
}
- if (action.costUsd !== undefined && envelope.maxCostUsd !== undefined) {
- checks.push([action.costUsd <= envelope.maxCostUsd, 'cost-outside-envelope']);
+ if (envelope.maxCostUsd !== undefined) {
+ checks.push([action.costUsd !== undefined && action.costUsd <= envelope.maxCostUsd, 'cost-outside-envelope']);
}
- if (action.tokens !== undefined && envelope.maxTokens !== undefined) {
- checks.push([action.tokens <= envelope.maxTokens, 'tokens-outside-envelope']);
+ if (envelope.maxTokens !== undefined) {
+ checks.push([action.tokens !== undefined && action.tokens <= envelope.maxTokens, 'tokens-outside-envelope']);
}
- if (action.concurrency !== undefined && envelope.maxConcurrency !== undefined) {
- checks.push([action.concurrency <= envelope.maxConcurrency, 'concurrency-outside-envelope']);
+ if (envelope.maxConcurrency !== undefined) {
+ checks.push([action.concurrency !== undefined && action.concurrency <= envelope.maxConcurrency, 'concurrency-outside-envelope']);
}
if (action.network === true) checks.push([envelope.network === true, 'network-outside-envelope']);
if (action.destructive === true) checks.push([envelope.destructive === true, 'destructive-outside-envelope']);