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
94 changes: 94 additions & 0 deletions packages/core/src/__tests__/session-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,100 @@ describe("cleanup", () => {
expect(result.skipped).toContain("app-orchestrator");
});

it("skips dead-runtime sessions with open PRs (fixes #146)", async () => {
const deadRuntime: Runtime = {
...mockRuntime,
isAlive: vi.fn().mockResolvedValue(false),
};
const openPRSCM: SCM = {
name: "mock-scm",
detectPR: vi.fn(),
getPRState: vi.fn().mockResolvedValue("open"),
mergePR: vi.fn(),
closePR: vi.fn(),
getCIChecks: vi.fn(),
getCISummary: vi.fn(),
getReviews: vi.fn(),
getReviewDecision: vi.fn(),
getPendingComments: vi.fn(),
getAutomatedComments: vi.fn(),
getMergeability: vi.fn(),
};
const registryWithDeadAndSCM: PluginRegistry = {
...mockRegistry,
get: vi.fn().mockImplementation((slot: string) => {
if (slot === "runtime") return deadRuntime;
if (slot === "agent") return mockAgent;
if (slot === "workspace") return mockWorkspace;
if (slot === "scm") return openPRSCM;
return null;
}),
};

writeMetadata(sessionsDir, "app-1", {
worktree: "/tmp",
branch: "feat/create-from-scratch",
status: "starting",
project: "my-app",
pr: "https://github.com/org/repo/pull/99",
runtimeHandle: JSON.stringify(makeHandle("rt-1")),
});

const sm = createSessionManager({ config, registry: registryWithDeadAndSCM });
const result = await sm.cleanup();

// Session with open PR should NOT be killed even though runtime is dead
expect(result.killed).toHaveLength(0);
expect(result.skipped).toContain("app-1");
});

it("conservatively skips dead-runtime sessions when PR state check fails", async () => {
const deadRuntime: Runtime = {
...mockRuntime,
isAlive: vi.fn().mockResolvedValue(false),
};
const failingSCM: SCM = {
name: "mock-scm",
detectPR: vi.fn(),
getPRState: vi.fn().mockRejectedValue(new Error("API error")),
mergePR: vi.fn(),
closePR: vi.fn(),
getCIChecks: vi.fn(),
getCISummary: vi.fn(),
getReviews: vi.fn(),
getReviewDecision: vi.fn(),
getPendingComments: vi.fn(),
getAutomatedComments: vi.fn(),
getMergeability: vi.fn(),
};
const registryWithDeadAndFailing: PluginRegistry = {
...mockRegistry,
get: vi.fn().mockImplementation((slot: string) => {
if (slot === "runtime") return deadRuntime;
if (slot === "agent") return mockAgent;
if (slot === "workspace") return mockWorkspace;
if (slot === "scm") return failingSCM;
return null;
}),
};

writeMetadata(sessionsDir, "app-1", {
worktree: "/tmp",
branch: "feat/something",
status: "working",
project: "my-app",
pr: "https://github.com/org/repo/pull/50",
runtimeHandle: JSON.stringify(makeHandle("rt-1")),
});

const sm = createSessionManager({ config, registry: registryWithDeadAndFailing });
const result = await sm.cleanup();

// When PR state can't be verified, be conservative and skip
expect(result.killed).toHaveLength(0);
expect(result.skipped).toContain("app-1");
});

it("kills sessions with dead runtimes", async () => {
const deadRuntime: Runtime = {
...mockRuntime,
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,27 @@ export function createSessionManager(deps: SessionManagerDeps): SessionManager {
}
}

// Check if runtime is dead
// Check if runtime is dead — but never kill a session with an open PR.
// A dead runtime with an open PR means the session crashed or failed to
// start; the PR is orphaned and needs human attention, not silent removal.
// (fixes #146)
if (!shouldKill && session.runtimeHandle && plugins.runtime) {
try {
const alive = await plugins.runtime.isAlive(session.runtimeHandle);
if (!alive) shouldKill = true;
if (!alive) {
// Guard: if session has an open PR, skip cleanup
let hasOpenPR = false;
if (session.pr && plugins.scm) {
try {
const prState = await plugins.scm.getPRState(session.pr);
if (prState === PR_STATE.OPEN) hasOpenPR = true;
} catch {
// Can't verify PR state — be conservative, assume open
hasOpenPR = true;
}
}
if (!hasOpenPR) shouldKill = true;
}
} catch {
// Can't check — skip
}
Expand Down