-
Notifications
You must be signed in to change notification settings - Fork 503
Track local agent profiles and enforce scoped Cloud deployments #874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chhhee10
wants to merge
7
commits into
feat/cloud-jev-policies
Choose a base branch
from
feat/agent-scoped-cloud-policies
base: feat/cloud-jev-policies
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,134
−152
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f26c6e
feat(enforcement): track agent profiles and enforce scoped Cloud poli…
chhhee10 1020299
fix(enforcement): bind agent scope to the originating hook
chhhee10 cc41294
test(enforcement): replay shared agent target selector cases
chhhee10 2f920d7
docs(policies): explain agent-scoped Cloud deployments
chhhee10 5d8343a
refactor(cloud): validate authority targets once per policy
chhhee10 18b5f11
fix(ipc): require v2 daemon for scoped agent identity
chhhee10 8c22f31
fix(roster): preserve existing profile IDs at capacity
chhhee10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // @vitest-environment node | ||
| import { describe, expect, it } from "vitest"; | ||
| import { createHash } from "node:crypto"; | ||
| import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { createFixtureEnv } from "../helpers/fixture-env"; | ||
| import { assertAllow, assertPreToolUseDeny, runHook } from "../helpers/hook-runner"; | ||
|
|
||
| describe("real CLI hook distinguishes simultaneous project and user profiles", () => { | ||
| it("applies targeted Cloud JS only when the installed hook carries its actual settings scope", () => { | ||
| const fixture = createFixtureEnv(); | ||
| const projectSettings = join(fixture.cwd, ".claude", "settings.json"); | ||
| const userSettings = join(fixture.home, ".claude", "settings.json"); | ||
| mkdirSync(join(fixture.cwd, ".claude"), { recursive: true }); | ||
| mkdirSync(join(fixture.home, ".claude"), { recursive: true }); | ||
| writeFileSync(projectSettings, '{"hooks":"failproofai --hook PreToolUse --agent-scope project"}'); | ||
| writeFileSync(userSettings, '{"hooks":"failproofai --hook PreToolUse --agent-scope user"}'); | ||
|
|
||
| const fpHome = join(fixture.home, ".failproofai"); | ||
| const rosterDir = join(fpHome, "agents"); | ||
| mkdirSync(rosterDir, { recursive: true }); | ||
| const projectId = "agt_1234567890abcdef"; | ||
| const userId = "agt_abcdef1234567890"; | ||
| const roster = join(rosterDir, "roster.json"); | ||
| writeFileSync(roster, JSON.stringify({ | ||
| schemaVersion: 1, generation: 2, | ||
| agents: [ | ||
| { integration: "claude", instanceId: projectId, settingsPath: projectSettings, | ||
| profileLabel: "project", scope: "project", hookInstalled: true }, | ||
| { integration: "claude", instanceId: userId, settingsPath: userSettings, | ||
| profileLabel: "user", scope: "user", hookInstalled: true }, | ||
| ], | ||
| }), { mode: 0o600 }); | ||
| chmodSync(roster, 0o600); | ||
|
|
||
| const cloudDir = join(fpHome, "policies", "cloud-policies"); | ||
| mkdirSync(join(cloudDir, "artifacts"), { recursive: true }); | ||
| const source = `import { customPolicies, deny } from "failproofai"; | ||
| customPolicies.add({ | ||
| name: "only-project", description: "Only this installation", | ||
| match: { events: ["PreToolUse"] }, | ||
| fn: async () => deny("project agent"), | ||
| });`; | ||
| const digest = createHash("sha256").update(source).digest("hex"); | ||
| const artifact = `artifacts/${digest}.mjs`; | ||
| writeFileSync(join(cloudDir, artifact), source); | ||
| writeFileSync(join(cloudDir, "active.json"), JSON.stringify({ | ||
| schemaVersion: 3, deployment: 1, | ||
| policies: [{ | ||
| id: "scope-check", version: 1, sha256: digest, path: artifact, effect: "enforce", | ||
| agentTargets: [{ integration: "claude", instanceId: projectId }], | ||
| }], | ||
| })); | ||
|
|
||
| const payload = { | ||
| session_id: "scope-test", hook_event_name: "PreToolUse", | ||
| tool_name: "Bash", tool_input: { command: "ls" }, cwd: fixture.cwd, | ||
| }; | ||
| const project = runHook("PreToolUse", payload, { | ||
| homeDir: fixture.home, cwd: fixture.cwd, agentScope: "project", | ||
| }); | ||
| assertPreToolUseDeny(project); | ||
|
|
||
| const user = runHook("PreToolUse", payload, { | ||
| homeDir: fixture.home, cwd: fixture.cwd, agentScope: "user", | ||
| }); | ||
| assertAllow(user); | ||
|
|
||
| const legacyAmbiguous = runHook("PreToolUse", payload, { | ||
| homeDir: fixture.home, cwd: fixture.cwd, | ||
| }); | ||
| assertAllow(legacyAmbiguous); | ||
| const report = JSON.parse(readFileSync(join(cloudDir, "errors.json"), "utf8")); | ||
| expect(report.errors).toContainEqual(expect.objectContaining({ | ||
| id: "agentScope", message: expect.stringContaining("agent_scope_unresolved"), | ||
| })); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "cases": [ | ||
| {"name":"unscoped", "schemaVersion":2, "targets":null, "agent":null, "valid":true, "matches":true}, | ||
| {"name":"integration-all-profiles", "schemaVersion":3, "targets":[{"integration":"claude"}], "agent":{"integration":"claude","instanceId":"agt_1111111111111111"}, "valid":true, "matches":true}, | ||
| {"name":"integration-other-agent", "schemaVersion":3, "targets":[{"integration":"claude"}], "agent":{"integration":"codex","instanceId":"agt_1111111111111111"}, "valid":true, "matches":false}, | ||
| {"name":"profile-exact", "schemaVersion":3, "targets":[{"integration":"hermes","instanceId":"agt_2222222222222222"}], "agent":{"integration":"hermes","instanceId":"agt_2222222222222222"}, "valid":true, "matches":true}, | ||
| {"name":"profile-other-instance", "schemaVersion":3, "targets":[{"integration":"hermes","instanceId":"agt_2222222222222222"}], "agent":{"integration":"hermes","instanceId":"agt_3333333333333333"}, "valid":true, "matches":false}, | ||
| {"name":"profile-other-integration", "schemaVersion":3, "targets":[{"integration":"hermes","instanceId":"agt_2222222222222222"}], "agent":{"integration":"codex","instanceId":"agt_2222222222222222"}, "valid":true, "matches":false}, | ||
| {"name":"scope-unresolved", "schemaVersion":3, "targets":[{"integration":"codex"}], "agent":null, "valid":true, "matches":false}, | ||
| {"name":"or-combination", "schemaVersion":3, "targets":[{"integration":"claude"},{"integration":"hermes","instanceId":"agt_2222222222222222"}], "agent":{"integration":"hermes","instanceId":"agt_2222222222222222"}, "valid":true, "matches":true}, | ||
| {"name":"both-halves-same-scope", "schemaVersion":3, "targets":[{"integration":"codex"}], "agent":{"integration":"codex","instanceId":"agt_4444444444444444"}, "valid":true, "matches":true}, | ||
| {"name":"empty-array", "schemaVersion":3, "targets":[], "agent":null, "valid":false}, | ||
| {"name":"duplicate-selector", "schemaVersion":3, "targets":[{"integration":"codex"},{"integration":"codex"}], "agent":null, "valid":false}, | ||
| {"name":"unknown-integration", "schemaVersion":3, "targets":[{"integration":"invented"}], "agent":null, "valid":false}, | ||
| {"name":"invalid-profile-id", "schemaVersion":3, "targets":[{"integration":"hermes","instanceId":"bad"}], "agent":null, "valid":false}, | ||
| {"name":"scoped-in-schema-two", "schemaVersion":2, "targets":[{"integration":"claude"}], "agent":null, "valid":false} | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // @vitest-environment node | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
| import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { readRuntimeAgentIdentity, runtimeAgentSettingsPath } from "../../src/hooks/agent-roster"; | ||
| import { agentTargetsMatch, parseAgentTargets } from "../../src/hooks/agent-targets"; | ||
|
|
||
| const roots: string[] = []; | ||
| const previousHome = process.env.FAILPROOFAI_HOME; | ||
| const previousHermes = process.env.HERMES_HOME; | ||
| const previousClaude = process.env.CLAUDE_CONFIG_DIR; | ||
| afterEach(() => { | ||
| if (previousHome === undefined) delete process.env.FAILPROOFAI_HOME; | ||
| else process.env.FAILPROOFAI_HOME = previousHome; | ||
| if (previousHermes === undefined) delete process.env.HERMES_HOME; | ||
| else process.env.HERMES_HOME = previousHermes; | ||
| if (previousClaude === undefined) delete process.env.CLAUDE_CONFIG_DIR; | ||
| else process.env.CLAUDE_CONFIG_DIR = previousClaude; | ||
| for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("runtime agent identity", () => { | ||
| it("detects a project-only hook and refuses to guess when project and user hooks both apply", () => { | ||
| const root = mkdtempSync(join(tmpdir(), "fpai-agent-scopes-")); | ||
| roots.push(root); | ||
| delete process.env.CLAUDE_CONFIG_DIR; | ||
| const user = join(root, "user"); | ||
| const project = join(root, "repo"); | ||
| const nested = join(project, "src"); | ||
| const projectSettings = join(project, ".claude", "settings.json"); | ||
| const userSettings = join(user, ".claude", "settings.json"); | ||
| mkdirSync(nested, { recursive: true }); | ||
| mkdirSync(join(project, ".claude"), { recursive: true }); | ||
| mkdirSync(join(user, ".claude"), { recursive: true }); | ||
| writeFileSync(projectSettings, '{"hooks":"failproofai --hook PreToolUse"}'); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user)).toBe(projectSettings); | ||
| writeFileSync(userSettings, '{"hooks":"failproofai --hook PreToolUse"}'); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user)).toBeNull(); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user, "project")).toBe(projectSettings); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user, "user")).toBe(userSettings); | ||
| const localSettings = join(project, ".claude", "settings.local.json"); | ||
| writeFileSync(localSettings, '{"hooks":"failproofai --hook PreToolUse"}'); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user, "local")).toBe(localSettings); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user, "project")).toBe(projectSettings); | ||
| rmSync(projectSettings); | ||
| rmSync(localSettings); | ||
| expect(runtimeAgentSettingsPath("claude", nested, user)).toBe(userSettings); | ||
| }); | ||
|
|
||
| it("counts Pi's relative project extension when deciding whether two scopes are ambiguous", () => { | ||
| const root = mkdtempSync(join(tmpdir(), "fpai-pi-scopes-")); | ||
| roots.push(root); | ||
| const user = join(root, "user"); | ||
| const project = join(root, "repo"); | ||
| const projectSettings = join(project, ".pi", "settings.json"); | ||
| const userSettings = join(user, ".pi", "agent", "settings.json"); | ||
| mkdirSync(join(project, ".pi"), { recursive: true }); | ||
| mkdirSync(join(user, ".pi", "agent"), { recursive: true }); | ||
| writeFileSync(projectSettings, '{"packages":["../pi-extension"]}'); | ||
| expect(runtimeAgentSettingsPath("pi", project, user)).toBe(projectSettings); | ||
| writeFileSync(userSettings, '{"packages":["/opt/failproofai/pi-extension"]}'); | ||
| expect(runtimeAgentSettingsPath("pi", project, user)).toBeNull(); | ||
| // A package-level Pi extension cannot stamp which settings file loaded | ||
| // it. Without that proof an exact-profile assignment matches neither. | ||
| expect(readRuntimeAgentIdentity("pi", runtimeAgentSettingsPath("pi", project, user))).toBeNull(); | ||
| }); | ||
|
|
||
| it("resolves a named profile from the invoking hook's config path", () => { | ||
| const root = mkdtempSync(join(tmpdir(), "fpai-agent-roster-")); | ||
| roots.push(root); | ||
| process.env.FAILPROOFAI_HOME = root; | ||
| process.env.HERMES_HOME = join(root, "profiles", "work"); | ||
| const path = runtimeAgentSettingsPath("hermes"); | ||
| const rosterPath = join(root, "agents", "roster.json"); | ||
| mkdirSync(join(root, "agents")); | ||
| writeFileSync(rosterPath, JSON.stringify({ | ||
| schemaVersion: 1, generation: 5, | ||
| agents: [ | ||
| { integration: "hermes", instanceId: "agt_1234567890abcdef", | ||
| settingsPath: path, profileLabel: "work", scope: "user", hookInstalled: true }, | ||
| { integration: "hermes", instanceId: "agt_abcdef1234567890", | ||
| settingsPath: join(root, "profiles", "personal", "config.yaml"), | ||
| profileLabel: "personal", scope: "user", hookInstalled: true }, | ||
| ], | ||
| })); | ||
| chmodSync(rosterPath, 0o600); | ||
| expect(readRuntimeAgentIdentity("hermes", path)).toEqual({ | ||
| integration: "hermes", instanceId: "agt_1234567890abcdef", | ||
| }); | ||
| expect(readRuntimeAgentIdentity("hermes", join(root, "profiles", "absent", "config.yaml"))).toBeNull(); | ||
| expect(readRuntimeAgentIdentity("codex", path)).toBeNull(); | ||
| const exact = parseAgentTargets([{ integration: "hermes", instanceId: "agt_1234567890abcdef" }], 3); | ||
| expect(agentTargetsMatch(exact, readRuntimeAgentIdentity("hermes", path))).toBe(true); | ||
| expect(agentTargetsMatch(exact, null)).toBe(false); | ||
| }); | ||
|
|
||
| it("withholds a scoped identity if the roster is unreadable or not owner-only", () => { | ||
| const root = mkdtempSync(join(tmpdir(), "fpai-agent-roster-")); | ||
| roots.push(root); | ||
| process.env.FAILPROOFAI_HOME = root; | ||
| const path = join(root, "agent", "config.yaml"); | ||
| const rosterPath = join(root, "agents", "roster.json"); | ||
| mkdirSync(join(root, "agents")); | ||
| writeFileSync(rosterPath, JSON.stringify({ | ||
| schemaVersion: 1, agents: [ | ||
| { integration: "hermes", instanceId: "agt_1234567890abcdef", settingsPath: path }, | ||
| ], | ||
| })); | ||
| chmodSync(rosterPath, 0o644); | ||
| expect(readRuntimeAgentIdentity("hermes", path)).toBeNull(); | ||
| chmodSync(rosterPath, 0o600); | ||
| writeFileSync(rosterPath, "not JSON"); | ||
| expect(readRuntimeAgentIdentity("hermes", path)).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // @vitest-environment node | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getIntegration } from "../../src/hooks/integrations"; | ||
| import type { IntegrationType } from "../../src/hooks/types"; | ||
|
|
||
| const SHELL_INTEGRATIONS: IntegrationType[] = [ | ||
| "claude", "codex", "copilot", "cursor", | ||
| "factory", "devin", "antigravity", "goose", | ||
| ]; | ||
|
|
||
| describe("installed shell hooks carry their originating scope", () => { | ||
| for (const cli of SHELL_INTEGRATIONS) { | ||
| it(`${cli}: user and project commands carry different scope hints`, () => { | ||
| const integration = getIntegration(cli); | ||
| for (const scope of ["user", "project"] as const) { | ||
| const entry = integration.buildHookEntry("/usr/local/bin/failproofai", "PreToolUse", scope); | ||
| const command = typeof entry.command === "string" ? entry.command : entry.bash; | ||
| expect(command).toContain(`--agent-scope ${scope}`); | ||
| if (cli === "copilot") expect(entry.powershell).toContain(`--agent-scope ${scope}`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| it("Claude's local hook identifies the local settings file", () => { | ||
| expect(getIntegration("claude").buildHookEntry("/bin/failproofai", "PreToolUse", "local")) | ||
| .toMatchObject({ command: expect.stringContaining("--agent-scope local") }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use the current October 2026 date for the changelog heading.
CHANGELOG.mduses2026-09-30, which is earlier than the current date in October 2026. Update the heading to today's date and keep version1.0.10-beta.0frompackage.json.🤖 Prompt for AI Agents