fix(coding-agent): convert createSessionManager throws into friendly CLI errors
`omp --resume <id>` and `omp --fork <id>` previously crashed with `[Uncaught Exception] Error: Session "..." not found.` followed by a stack trace whenever the id did not match an existing session. The throws in `createSessionManager` were never caught by `runRootCommand`, so they fell through to the global `unhandledRejection` handler in `postmortem.ts` and printed the raw stack instead of a clean message. Add `SessionResolutionError` (a dedicated subclass of `Error` with an optional usage `hint`) and use it for every user-facing resolution failure: unknown `--resume`/`--fork` id, `--fork` combined with `--no-session`, and the non-interactive cross-project / moved-cwd prompts. `runRootCommand` catches it around the `createSessionManager` call, writes `Error: <msg>` (and the hint when present) to stderr, and exits with code 1. Other (unexpected) errors still propagate so they remain visible to the postmortem handler. Fixes #2084
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Regression for #2084: `createSessionManager` must reject with
|
||||
* `SessionResolutionError` (and a usage hint) when `--resume` / `--fork` are
|
||||
* given a non-existent session id, so `runRootCommand` can convert it into a
|
||||
* clean stderr message + non-zero exit instead of letting it surface as
|
||||
* `[Uncaught Exception]`.
|
||||
*/
|
||||
import { describe, expect, it, vi } from "bun:test";
|
||||
import type { Args } from "@oh-my-pi/pi-coding-agent/cli/args";
|
||||
import type { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
||||
import { createSessionManager, SessionResolutionError } from "@oh-my-pi/pi-coding-agent/main";
|
||||
import * as sessionManagerModule from "@oh-my-pi/pi-coding-agent/session/session-manager";
|
||||
|
||||
function buildResumeArgs(resume: string): Args {
|
||||
return {
|
||||
resume,
|
||||
messages: [],
|
||||
fileArgs: [],
|
||||
unknownFlags: new Map(),
|
||||
};
|
||||
}
|
||||
|
||||
function buildForkArgs(fork: string, noSession = false): Args {
|
||||
return {
|
||||
fork,
|
||||
noSession: noSession || undefined,
|
||||
messages: [],
|
||||
fileArgs: [],
|
||||
unknownFlags: new Map(),
|
||||
};
|
||||
}
|
||||
|
||||
const stubSettings = { get: () => undefined } as unknown as Settings;
|
||||
|
||||
describe("createSessionManager — missing session (#2084)", () => {
|
||||
it("rejects --resume with SessionResolutionError carrying a usage hint", async () => {
|
||||
vi.spyOn(sessionManagerModule, "resolveResumableSession").mockResolvedValue(undefined);
|
||||
try {
|
||||
await expect(
|
||||
createSessionManager(
|
||||
buildResumeArgs("019ea530-0000-7000-0000-000000000000"),
|
||||
"/current/project",
|
||||
stubSettings,
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
name: "SessionResolutionError",
|
||||
message: 'Session "019ea530-0000-7000-0000-000000000000" not found.',
|
||||
hint: expect.stringContaining("omp --resume"),
|
||||
});
|
||||
|
||||
// Confirm it's the exported class so `runRootCommand`'s `instanceof` check works.
|
||||
const caught = await createSessionManager(
|
||||
buildResumeArgs("019ea530-0000-7000-0000-000000000000"),
|
||||
"/current/project",
|
||||
stubSettings,
|
||||
).catch((err: unknown) => err);
|
||||
expect(caught).toBeInstanceOf(SessionResolutionError);
|
||||
} finally {
|
||||
vi.restoreAllMocks();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects --fork with SessionResolutionError carrying a usage hint", async () => {
|
||||
vi.spyOn(sessionManagerModule, "resolveResumableSession").mockResolvedValue(undefined);
|
||||
try {
|
||||
await expect(
|
||||
createSessionManager(
|
||||
buildForkArgs("019ea530-0000-7000-0000-000000000000"),
|
||||
"/current/project",
|
||||
stubSettings,
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
name: "SessionResolutionError",
|
||||
message: 'Session "019ea530-0000-7000-0000-000000000000" not found.',
|
||||
hint: expect.stringContaining("omp --resume"),
|
||||
});
|
||||
} finally {
|
||||
vi.restoreAllMocks();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects --fork combined with --no-session as a SessionResolutionError (no hint)", async () => {
|
||||
await expect(
|
||||
createSessionManager(buildForkArgs("019ea530", true), "/current/project", stubSettings),
|
||||
).rejects.toMatchObject({
|
||||
name: "SessionResolutionError",
|
||||
message: "--fork requires session persistence",
|
||||
hint: undefined,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user