🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(cli): policy trust mirror and key honour HOME/XDG_CONFIG_HOME; guardrail test runs in a scratch project (#3919)
r1c1060171672
diff --git a/v3/@claude-flow/cli/__tests__/mcp-client-guardrail.test.ts b/v3/@claude-flow/cli/__tests__/mcp-client-guardrail.test.tsindex 59e3f245d0..b2f242033c 100644--- a/v3/@claude-flow/cli/__tests__/mcp-client-guardrail.test.ts+++ b/v3/@claude-flow/cli/__tests__/mcp-client-guardrail.test.ts@@ -13,7 +13,10 @@* (numbers, nested objects) are returned as-is — we only walk one level.*/-import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';+import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';// Stub the tool registry with a single tool whose handler returns whatever// payload we hand it. Every other tool import is mocked to an empty array so@@ -72,6 +75,26 @@ vi.mock('../src/mcp-tools/agent-tools.js', () => ({import { callMCPTool } from '../src/mcp-client.js';describe('callMCPTool — ADR-146 P2 content-boundary guardrail', () => {+ // #3919: callMCPTool is policy-gated, which writes a ledger under the cwd and+ // an anchor mirror under $HOME. Run in a scratch project with a scratch HOME so+ // the test never touches the package folder or the developer's real home, and+ // a reused path can never find a stale mirror.+ const scratch = mkdtempSync(join(tmpdir(), 'ruflo-guardrail-'));+ const origCwd = process.cwd();+ const origHome = process.env.HOME;+ const origXdg = process.env.XDG_CONFIG_HOME;+ beforeAll(() => {+ process.env.HOME = join(scratch, 'home');+ delete process.env.XDG_CONFIG_HOME;+ process.chdir(scratch);+ });+ afterAll(() => {+ process.chdir(origCwd);+ if (origHome === undefined) delete process.env.HOME; else process.env.HOME = origHome;+ if (origXdg !== undefined) process.env.XDG_CONFIG_HOME = origXdg;+ rmSync(scratch, { recursive: true, force: true });+ });+const ORIG_STRICT = process.env.CLAUDE_FLOW_STRICT_GUARDRAIL;beforeEach(() => {diff --git a/v3/@claude-flow/cli/__tests__/policy-trust-home-3919.test.ts b/v3/@claude-flow/cli/__tests__/policy-trust-home-3919.test.tsnew file mode 100644index 0000000000..cebbdf5d0b--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/policy-trust-home-3919.test.ts@@ -0,0 +1,74 @@+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync } from 'node:fs';+import { join } from 'node:path';+import { tmpdir, userInfo } from 'node:os';+import { createHash } from 'node:crypto';+import { evaluatePolicyRequest } from '../src/services/policy-runtime.js';+import { anchorPosture } from '../src/services/policy-ledger-anchor.js';++// #3919: the anchor mirror and trust key were resolved with userInfo().homedir,+// which ignores a throwaway HOME, so tests (and sandboxed runs) wrote into the+// developer's real ~/.config/ruflo/policy-trust and later failed closed there.++const saved = { HOME: process.env.HOME, XDG: process.env.XDG_CONFIG_HOME };+const dirs: string[] = [];+const scratch = (prefix: string) => {+ const d = mkdtempSync(join(tmpdir(), prefix));+ dirs.push(d);+ return d;+};+const projectId = (root: string) => createHash('sha256').update(realpathSync(root)).digest('hex');+const decide = (root: string) => evaluatePolicyRequest({+ identity: { id: 'agent:1', type: 'agent' },+ action: { type: 'code.read', resource: 'f' },+}, root);++beforeEach(() => { delete process.env.XDG_CONFIG_HOME; });+afterEach(() => {+ if (saved.HOME === undefined) delete process.env.HOME; else process.env.HOME = saved.HOME;+ if (saved.XDG === undefined) delete process.env.XDG_CONFIG_HOME; else process.env.XDG_CONFIG_HOME = saved.XDG;+ for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });+});++describe('policy trust files honour a throwaway HOME (#3919)', () => {+ it('writes the mirror and key under $HOME, never the account home', async () => {+ const home = scratch('ruflo-3919-home-');+ const root = scratch('ruflo-3919-proj-');+ mkdirSync(join(root, '.claude-flow'), { recursive: true });+ process.env.HOME = home;+ const real = join(userInfo().homedir, '.config', 'ruflo', 'policy-trust', projectId(root));+ try {+ await decide(root);+ expect(existsSync(join(home, '.config', 'ruflo', 'policy-trust', projectId(root), 'ledger-anchor-head.json'))).toBe(true);+ expect(anchorPosture(root).mirror).toBe(true);+ if (userInfo().homedir !== home) expect(existsSync(real)).toBe(false);+ } finally {+ rmSync(real, { recursive: true, force: true });+ }+ });++ it('honours XDG_CONFIG_HOME ahead of $HOME/.config', async () => {+ const home = scratch('ruflo-3919-home-');+ const xdg = scratch('ruflo-3919-xdg-');+ const root = scratch('ruflo-3919-proj-');+ mkdirSync(join(root, '.claude-flow'), { recursive: true });+ process.env.HOME = home;+ process.env.XDG_CONFIG_HOME = xdg;+ await decide(root);+ expect(existsSync(join(xdg, 'ruflo', 'policy-trust', projectId(root), 'ledger-anchor-head.json'))).toBe(true);+ expect(existsSync(join(home, '.config', 'ruflo', 'policy-trust'))).toBe(false);+ });++ it('keeps using the legacy ~/.config location for an XDG user who already has trust state there', async () => {+ const home = scratch('ruflo-3919-home-');+ const xdg = scratch('ruflo-3919-xdg-');+ const root = scratch('ruflo-3919-proj-');+ mkdirSync(join(root, '.claude-flow'), { recursive: true });+ mkdirSync(join(home, '.config', 'ruflo', 'policy-trust'), { recursive: true });+ process.env.HOME = home;+ process.env.XDG_CONFIG_HOME = xdg;+ await decide(root);+ expect(existsSync(join(home, '.config', 'ruflo', 'policy-trust', projectId(root), 'ledger-anchor-head.json'))).toBe(true);+ expect(existsSync(join(xdg, 'ruflo'))).toBe(false);+ });+});diff --git a/v3/@claude-flow/cli/src/services/policy-ledger-anchor.ts b/v3/@claude-flow/cli/src/services/policy-ledger-anchor.tsindex d9560430b8..99c4aa0e8a 100644--- a/v3/@claude-flow/cli/src/services/policy-ledger-anchor.ts+++ b/v3/@claude-flow/cli/src/services/policy-ledger-anchor.ts@@ -13,8 +13,8 @@*/import { createHash } from 'node:crypto';import { existsSync, mkdirSync, readFileSync, realpathSync } from 'node:fs';-import { dirname, join, resolve } from 'node:path';-import { userInfo } from 'node:os';+import { dirname, isAbsolute, join, resolve } from 'node:path';+import { homedir } from 'node:os';import type { PolicyState } from '@claude-flow/security';export type AnchorEvent = 'append' | 'migrated-from-state' | 'establish-anchor';@@ -42,9 +42,24 @@ const MIRROR_FILE = 'ledger-anchor-head.json';export const anchorLogPath = (projectRoot: string): string => join(resolve(projectRoot), '.claude-flow', 'policy', LOG_FILE);+/**+ * Directory holding per-project trust material (anchor mirror, HMAC key).+ * Resolved from the environment at call time (#3919): $XDG_CONFIG_HOME when it+ * is absolute, else $HOME/.config (os.homedir() honours $HOME), so a scratch+ * HOME keeps tests and sandboxed runs out of the developer's real home. An XDG+ * user who already has trust state under the legacy ~/.config keeps using it.+ */+export function policyTrustRoot(): string {+ const legacy = join(homedir(), '.config', 'ruflo', 'policy-trust');+ const xdg = process.env.XDG_CONFIG_HOME;+ if (!xdg || !isAbsolute(xdg)) return legacy;+ const preferred = join(xdg, 'ruflo', 'policy-trust');+ return !existsSync(preferred) && existsSync(legacy) ? legacy : preferred;+}+function mirrorPath(projectRoot: string): string {const id = createHash('sha256').update(realpathSync(projectRoot)).digest('hex');- return join(userInfo().homedir, '.config', 'ruflo', 'policy-trust', id, MIRROR_FILE);+ return join(policyTrustRoot(), id, MIRROR_FILE);}function entryHash(entry: Omit<AnchorEntry, 'hash'>): string {diff --git a/v3/@claude-flow/cli/src/services/policy-runtime.ts b/v3/@claude-flow/cli/src/services/policy-runtime.tsindex 3cafab7619..2672cf9732 100644--- a/v3/@claude-flow/cli/src/services/policy-runtime.ts+++ b/v3/@claude-flow/cli/src/services/policy-runtime.ts@@ -36,7 +36,7 @@ import {import { dirname, join, resolve } from 'node:path';import { hostname, userInfo } from 'node:os';import { syncPolicyProjection } from '../mods/policy-projection.js';-import { assessAnchors, recordAnchor, type AnchorEvent } from './policy-ledger-anchor.js';+import { assessAnchors, policyTrustRoot, recordAnchor, type AnchorEvent } from './policy-ledger-anchor.js';const POLICY_DIR = join('.claude-flow', 'policy');const POLICY_FILE = 'state.json';@@ -208,7 +208,7 @@ async function writeJsonAtomic(file: string, value: unknown): Promise<void> {}function trustPaths(projectRoot: string): { key: string; anchor: string } {- const trustRoot = join(userInfo().homedir, '.config', 'ruflo', 'policy-trust');+ const trustRoot = policyTrustRoot();const projectId = createHash('sha256').update(realpathSync(projectRoot)).digest('hex');const dir = join(trustRoot, projectId);return { key: join(dir, 'anchor.key'), anchor: join(dir, 'state.anchor.json') };