Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/lib/onboard/agent-fixed-forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function ensureAgentFixedForward(
const stopForwardForSandbox = (portToStop: string | number) =>
bestEffortForwardStopForSandbox(
deps.runOpenshell,
(args, opts) => (deps.runCaptureOpenshell(args, opts) ?? "") as string,
(args, opts) => deps.runCaptureOpenshell(args, opts),
portToStop,
sandboxName,
);
Expand Down
12 changes: 11 additions & 1 deletion src/lib/onboard/forward-cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("bestEffortForwardStopForSandbox", () => {

it("returns no-entry and runs a sandbox-scoped stop when no live forward is on that port", () => {
const run = vi.fn();
const fetch = vi.fn().mockReturnValue(forwardListWith([]));
const fetch = vi.fn().mockReturnValue("");

const outcome = bestEffortForwardStopForSandbox(run, fetch, 18789, "my-sandbox");

Expand All @@ -80,6 +80,16 @@ describe("bestEffortForwardStopForSandbox", () => {
});
});

it("returns list-failed and skips stop when the capture seam returns null", () => {
const run = vi.fn();
const fetch = vi.fn().mockReturnValue(null);

const outcome = bestEffortForwardStopForSandbox(run, fetch, 18789, "my-sandbox");

expect(outcome).toBe("list-failed");
expect(run).not.toHaveBeenCalled();
});

it("skips the stop entirely when `forward list` itself throws (owner unknown)", () => {
const run = vi.fn();
const fetch = vi.fn().mockImplementation(() => {
Expand Down
13 changes: 7 additions & 6 deletions src/lib/onboard/forward-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ForwardStopRunner = (
export type ForwardListRunner = (
args: string[],
opts: { ignoreError?: boolean; timeout?: number },
) => string;
) => string | null;

/**
* `openshell forward stop <port>` — port-scoped, kills whatever forward is
Expand Down Expand Up @@ -64,19 +64,20 @@ export function bestEffortForwardStopForSandbox(
port: string | number,
sandboxName: string,
): "stopped" | "owned-other" | "no-entry" | "list-failed" {
// Let runCaptureOpenshell throw on failure/timeout so the catch branch
// returns "list-failed". With ignoreError: true the runner would swallow
// the error and return "", which getOccupiedPorts parses as an empty map
// and the "no-entry" branch below would still run the stop — exactly the
// Let runCaptureOpenshell throw on failure/timeout, or return null, so the
// failure branch returns "list-failed". Treating either failure signal as
// an empty string would make getOccupiedPorts return an empty map and let
// the "no-entry" branch run a stop without ownership data — exactly the
// collateral-damage case this helper exists to avoid.
let listOutput = "";
let listOutput: string | null;
try {
listOutput = runCaptureOpenshell(["forward", "list"], {
timeout: OPENSHELL_PROBE_TIMEOUT_MS,
});
} catch {
return "list-failed";
}
if (listOutput === null) return "list-failed";
const owner = getOccupiedPorts(listOutput).get(String(port)) ?? null;
if (owner && owner !== sandboxName) {
return "owned-other";
Expand Down
Loading