A desktop coding agent with free access to frontier models.
TypeScript
fix: preview engine shells never get the policy token
ogsamrateef594f45ea9
diff --git a/apps/server/src/opencode-plugins/managed-policy-client.ts b/apps/server/src/opencode-plugins/managed-policy-client.tsindex 74dd9b22..d10f0855 100644--- a/apps/server/src/opencode-plugins/managed-policy-client.ts+++ b/apps/server/src/opencode-plugins/managed-policy-client.ts@@ -50,9 +50,22 @@ export interface PolicyVerdict {approvalMode: ApprovalMode;}+let heldPolicyToken: string | undefined;++/**+ * Keeps the policy token in this module and takes it out of the engine's+ * environment, for an engine that starts shells straight from process.env+ * with no shell.env hook (the preview engine): its commands then never get it.+ */+export function holdPolicyToken(): void {+ const token = process.env.OMNIRUSH_POLICY_TOKEN;+ if (token) heldPolicyToken = token;+ delete process.env.OMNIRUSH_POLICY_TOKEN;+}+export async function check(action: ManagedPolicyAction, input: Record<string, unknown>): Promise<PolicyVerdict> {const base = process.env.OMNIRUSH_SERVER_URL;- const token = process.env.OMNIRUSH_POLICY_TOKEN;+ const token = heldPolicyToken ?? process.env.OMNIRUSH_POLICY_TOKEN;if (!base || !token) throw new Error("The app policy service is unavailable.");const response = await fetch(`${base}/managed-policy/evaluate`, {method: "POST", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },diff --git a/apps/server/src/opencode-plugins/managed-policy-next.ts b/apps/server/src/opencode-plugins/managed-policy-next.tsindex 3c7fd058..adb18c03 100644--- a/apps/server/src/opencode-plugins/managed-policy-next.ts+++ b/apps/server/src/opencode-plugins/managed-policy-next.ts@@ -1,4 +1,4 @@-import { check, checkManagedTool } from "./managed-policy-client.js";+import { check, checkManagedTool, holdPolicyToken } from "./managed-policy-client.js";import { applyEnginePath } from "../engine-shell-path.js";// Plugin.define is the identity function in the pinned SDK. The structural// contract avoids loading either engine's SDK into the other engine.@@ -13,6 +13,8 @@ export default {// The next engine spawns shells from process.env; enrich it once so git// and gh resolve the way the packaged app resolves them.applyEnginePath();+ // Its shells copy process.env and there is no hook to empty a variable: the policy token leaves it.+ holdPolicyToken();await ctx.tool.hook("execute.before", (event) =>checkManagedTool(event.tool, event.input, { directory: ctx.directory, callID: event.callID, sessionID: event.sessionID }));await ctx.shell.hook("create.before", async (event) => { await check("shell", { command: event.command }); });diff --git a/apps/server/src/opencode-plugins/managed-policy.test.ts b/apps/server/src/opencode-plugins/managed-policy.test.tsindex 129f73df..f4902694 100644--- a/apps/server/src/opencode-plugins/managed-policy.test.ts+++ b/apps/server/src/opencode-plugins/managed-policy.test.ts@@ -321,6 +321,8 @@ describe("managed-policy engine plugin: git workflows", () => {shell: { hook: async (name, callback) => { hooks[`shell.${name}`] = callback as never; } },session: { hook: async (name, callback) => { hooks[`session.${name}`] = callback as never; } },});+ // Its shells copy process.env with no hook to empty a variable: the policy token is held by the plugin instead.+ expect(process.env.OMNIRUSH_POLICY_TOKEN).toBeUndefined();const input: Record<string, unknown> = { command: "git commit -m x && rm -rf build" };await hooks["tool.execute.before"]({ tool: "bash", input, callID: "call_8" } as never);expect(input.command).toBe(`git commit -m x && ${DESTRUCTIVE_MARKER} rm -rf build`);