Repository navigation
feat(policy): expose active presets, allowed hosts, and approval paths in agent context #4915
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8303efb
feat(policy): expose active presets, allowed hosts, and approval path…
laitingsheng 60fe251
fix(policy): address review feedback on policy-explain
laitingsheng 325de98
Merge remote-tracking branch 'origin/main' into feat/4629-policy-agen…
laitingsheng f65c805
test(policy): align hardcoded sandbox action lists with policy-explain
laitingsheng 8d30f57
fix(policy): keep policy-explain seed inert in tests and on no-op cha…
laitingsheng 9f732af
docs(policy): resync agent variant after policy-explain em-dash fix
laitingsheng 86d17ab
fix(policy): tighten policy-explain context and address review-adviso…
laitingsheng b631ea8
Merge remote-tracking branch 'origin/main' into feat/4629-policy-agen…
laitingsheng ad2549c
refactor(policy): split context, harden redaction, distinguish loader…
laitingsheng 741d2da
test(policy): pin channel-refresh contract, broaden link-local redact…
laitingsheng 0893975
fix(policy): shield rebuild from refresh-path exits, harden write aga…
laitingsheng 2122861
Merge remote-tracking branch 'origin/main' into feat/4629-policy-agen…
laitingsheng d899b42
fix(policy): classify block-code drift on matched preset as blocked-b…
laitingsheng 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
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,45 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Flags } from "@oclif/core"; | ||
|
|
||
| import { explainSandboxPolicy } from "../../../lib/actions/sandbox/policy-explain"; | ||
| import { NemoClawCommand } from "../../../lib/cli/nemoclaw-oclif-command"; | ||
| import { sandboxNameArg } from "../../../lib/sandbox/command-support"; | ||
|
|
||
| export default class SandboxPolicyExplainCommand extends NemoClawCommand { | ||
| static id = "sandbox:policy:explain"; | ||
| static strict = true; | ||
| static summary = "Explain the active policy context for the sandbox"; | ||
| static description = | ||
| "Print a redacted summary of the active policy presets, allowed host categories, approval paths, and support boundaries. The agent can read this output to decide whether a host or integration is allowed and what remediation step to suggest."; | ||
| static usage = ["<name> [--json] [--write]"]; | ||
| static examples = [ | ||
| "<%= config.bin %> sandbox policy explain alpha", | ||
| "<%= config.bin %> sandbox policy explain alpha --json", | ||
| "<%= config.bin %> sandbox policy explain alpha --write", | ||
| ]; | ||
| static args = { | ||
| sandboxName: sandboxNameArg, | ||
| }; | ||
| static flags = { | ||
| json: Flags.boolean({ | ||
| description: "Emit the policy context as JSON for agent consumption.", | ||
| default: false, | ||
| }), | ||
| write: Flags.boolean({ | ||
| description: | ||
| "Also write the rendered context to the sandbox at /sandbox/.openclaw/workspace/POLICY.md so the in-sandbox agent can read it.", | ||
| default: false, | ||
| }), | ||
| }; | ||
|
|
||
| public async run(): Promise<void> { | ||
| const { args, flags } = await this.parse(SandboxPolicyExplainCommand); | ||
| explainSandboxPolicy( | ||
| args.sandboxName, | ||
| { json: flags.json, writeToSandbox: flags.write }, | ||
| { logJson: (value) => this.logJson(value) }, | ||
| ); | ||
| } | ||
| } |
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,73 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("../../policy/context", () => ({ | ||
| buildPolicyContext: vi.fn(), | ||
| renderPolicyContextMarkdown: vi.fn(), | ||
| })); | ||
|
|
||
| import { | ||
| POLICY_CONTEXT_SANDBOX_PATH, | ||
| refreshSandboxPolicyContextFile, | ||
| } from "./policy-context-refresh"; | ||
|
|
||
| describe("refreshSandboxPolicyContextFile", () => { | ||
| it("reports `ok` when the write succeeds and does not warn", () => { | ||
| const warn = vi.fn(); | ||
| const unexpected = vi.fn(); | ||
| const write = vi.fn(() => ({ written: true })); | ||
|
|
||
| const outcome = refreshSandboxPolicyContextFile("alpha", { warn, unexpected, write }); | ||
|
|
||
| expect(outcome.outcome).toBe("ok"); | ||
| expect(warn).not.toHaveBeenCalled(); | ||
| expect(unexpected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("treats `sandbox unreachable` as a non-fatal `unreachable` outcome without warning", () => { | ||
| const warn = vi.fn(); | ||
| const unexpected = vi.fn(); | ||
| const write = vi.fn(() => ({ written: false, reason: "sandbox unreachable" })); | ||
|
|
||
| const outcome = refreshSandboxPolicyContextFile("alpha", { warn, unexpected, write }); | ||
|
|
||
| expect(outcome.outcome).toBe("unreachable"); | ||
| expect(warn).not.toHaveBeenCalled(); | ||
| expect(unexpected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("warns about explicit `failed` outcomes when the sandbox returns a non-zero exit", () => { | ||
| const warn = vi.fn(); | ||
| const unexpected = vi.fn(); | ||
| const write = vi.fn(() => ({ | ||
| written: false, | ||
| reason: "write failed (status 13): denied", | ||
| })); | ||
|
|
||
| const outcome = refreshSandboxPolicyContextFile("alpha", { warn, unexpected, write }); | ||
|
|
||
| expect(outcome.outcome).toBe("failed"); | ||
| expect(warn).toHaveBeenCalledTimes(1); | ||
| expect(warn.mock.calls[0][0]).toContain(POLICY_CONTEXT_SANDBOX_PATH); | ||
| expect(warn.mock.calls[0][0]).toContain("status 13"); | ||
| expect(unexpected).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("routes unexpected exceptions through the `unexpected` sink instead of swallowing them", () => { | ||
| const warn = vi.fn(); | ||
| const unexpected = vi.fn(); | ||
| const write = vi.fn(() => { | ||
| throw new Error("import regression: cannot find module"); | ||
| }); | ||
|
|
||
| const outcome = refreshSandboxPolicyContextFile("alpha", { warn, unexpected, write }); | ||
|
|
||
| expect(outcome.outcome).toBe("crashed"); | ||
| expect(unexpected).toHaveBeenCalledTimes(1); | ||
| const arg = unexpected.mock.calls[0][0]; | ||
| expect(arg instanceof Error ? arg.message : String(arg)).toContain("import regression"); | ||
| expect(warn).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,77 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { | ||
| POLICY_CONTEXT_SANDBOX_PATH, | ||
| writePolicyContextToSandbox, | ||
| type WritePolicyContextResult, | ||
| } from "./policy-explain"; | ||
|
|
||
| /** | ||
| * Result categories the refresh helper distinguishes when reporting outcomes: | ||
| * | ||
| * - `ok`: the sandbox confirmed the write. | ||
| * - `unreachable`: the sandbox is not reachable (expected during onboard | ||
| * transitions and on hosts without OpenShell installed). | ||
| * - `failed`: the sandbox is reachable but the write returned a non-zero | ||
| * status, which the caller should surface so the operator can react. | ||
| * - `crashed`: the build/render or executor threw before reaching the | ||
| * sandbox; treated as an unexpected regression and re-emitted via the | ||
| * `unexpected` callback so it does not vanish into a generic catch. | ||
| */ | ||
| export type PolicyContextRefreshOutcome = "ok" | "unreachable" | "failed" | "crashed"; | ||
|
|
||
| export interface RefreshOutcome extends WritePolicyContextResult { | ||
| outcome: PolicyContextRefreshOutcome; | ||
| errorMessage?: string; | ||
| } | ||
|
|
||
| export interface RefreshDeps { | ||
| write?: typeof writePolicyContextToSandbox; | ||
| warn?: (line: string) => void; | ||
| /** | ||
| * Sink for unexpected exceptions raised by the writer. Tests can inject | ||
| * a spy to assert that import/build/render regressions do not get | ||
| * swallowed silently. | ||
| */ | ||
| unexpected?: (error: unknown) => void; | ||
| } | ||
|
|
||
| const DEFAULT_WARN = (line: string) => console.error(line); | ||
|
|
||
| const DEFAULT_UNEXPECTED = (error: unknown) => { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| console.error(` Unexpected error refreshing ${POLICY_CONTEXT_SANDBOX_PATH}: ${message}`); | ||
| }; | ||
|
|
||
| export function refreshSandboxPolicyContextFile( | ||
| sandboxName: string, | ||
| deps: RefreshDeps = {}, | ||
| ): RefreshOutcome { | ||
| const write = deps.write ?? writePolicyContextToSandbox; | ||
| const warn = deps.warn ?? DEFAULT_WARN; | ||
| const unexpected = deps.unexpected ?? DEFAULT_UNEXPECTED; | ||
| let result: WritePolicyContextResult; | ||
| try { | ||
| result = write(sandboxName); | ||
| } catch (error: unknown) { | ||
| unexpected(error); | ||
| return { | ||
| written: false, | ||
| outcome: "crashed", | ||
| errorMessage: error instanceof Error ? error.message : String(error), | ||
| }; | ||
| } | ||
| if (result.written) { | ||
| return { ...result, outcome: "ok" }; | ||
| } | ||
| if (result.reason === "sandbox unreachable") { | ||
| return { ...result, outcome: "unreachable" }; | ||
| } | ||
| warn( | ||
| ` Could not refresh ${POLICY_CONTEXT_SANDBOX_PATH} for sandbox '${sandboxName}': ${result.reason ?? "unknown reason"}.`, | ||
| ); | ||
| return { ...result, outcome: "failed" }; | ||
| } | ||
|
|
||
| export { POLICY_CONTEXT_SANDBOX_PATH }; |
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.
Uh oh!
There was an error while loading. Please reload this page.