Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/lib/actions/sandbox/gateway-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function mergeLivePolicyIntoSandboxOutput(

const before = rawLines.slice(0, policyLineIdx + 1).join("\n");
const delimIdx = livePolicyOutput.search(/^---\s*$/m);
const metadataPart = delimIdx !== -1 ? livePolicyOutput.slice(0, delimIdx) : "";
const yamlPart =
delimIdx !== -1
? livePolicyOutput.slice(delimIdx).replace(/^---\s*[\r\n]+/, "")
Expand All @@ -66,7 +67,13 @@ export function mergeLivePolicyIntoSandboxOutput(
return output;
}

const indented = trimmedYaml
const activeMatch = stripAnsi(metadataPart).match(/^Active:\s*(\d+)\s*$/m);
const rewrittenYaml =
activeMatch && /^version:\s*\d+/m.test(trimmedYaml)
? trimmedYaml.replace(/^version:\s*\d+/m, `version: ${activeMatch[1]}`)
: trimmedYaml;

const indented = rewrittenYaml
.split("\n")
.map((line: string) => (line ? ` ${line}` : line))
.join("\n");
Expand Down
36 changes: 36 additions & 0 deletions test/gateway-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
shouldSelectNamedGatewayForReuse,
parseSandboxPhase,
} from "../src/lib/state/gateway.js";
import { mergeLivePolicyIntoSandboxOutput } from "../dist/lib/actions/sandbox/gateway-state.js";

// Realistic CLI outputs
const STATUS_CONNECTED = `
Expand Down Expand Up @@ -287,3 +288,38 @@ describe("shouldSelectNamedGatewayForReuse", () => {
);
});
});

describe("mergeLivePolicyIntoSandboxOutput (#1961)", () => {
const sandboxOutput = "Sandbox:\n Id: abc\n Phase: Ready\n\nPolicy:\n schema-stub";

it("rewrites the YAML version line to the gateway active version", () => {
const livePolicy = [
"Version: 6",
"Hash: 738a54c8520a",
"Status: Loaded",
"Active: 6",
"---",
"version: 1",
"filesystem_policy:",
" include_workdir: false",
].join("\n");

const merged = mergeLivePolicyIntoSandboxOutput(sandboxOutput, livePolicy);
expect(merged).toContain(" version: 6");
expect(merged).not.toContain(" version: 1");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

it("leaves the YAML untouched when no Active metadata is provided", () => {
const livePolicy = ["---", "version: 1", "filesystem_policy:", " include_workdir: false"].join(
"\n",
);

const merged = mergeLivePolicyIntoSandboxOutput(sandboxOutput, livePolicy);
expect(merged).toContain(" version: 1");
});

it("returns the original output when livePolicy is an error string", () => {
const merged = mergeLivePolicyIntoSandboxOutput(sandboxOutput, "Error: not found");
expect(merged).toBe(sandboxOutput);
});
});
Loading