Files
oh-my-pi/packages/coding-agent/test/main-session-resolution-error.test.ts
T
roboomp 54a4ca81f9 fix(cli): reject unknown --flags instead of starting an agent
Bare `omp --list-models` (or any other stale/typoed --flag) was silently
consumed by `parseArgs` and the agent went on to start a real session,
connect to the configured MCP servers, and hang waiting on the model.
Any positional after the unknown flag was reinterpreted as the initial
prompt, so a documentation drift turned into an unintended LLM invocation.

`parseArgs` now tracks flag-shaped tokens that did not match any built-in
or extension-registered flag in a new `unrecognizedFlags: string[]` field,
and `reportUnrecognizedFlags` prints a clean `Error: unknown flag(s): …`
line plus the `--help` hint. `runRootCommand` invokes the helper right
after the post-extension reparse and `process.exit(2)`s before any
session, MCP, or initial-message work runs.

The validation is gated on the extension-aware reparse, so extension
flags (`--spawn-peer`, `--headless`, `--plan`, …) still pass through
the same way `applyExtensionFlags` already handles them. `-` (stdin
marker) and `--` (POSIX separator) are deliberately allowed through.

Fixes #2459
2026-06-13 17:46:45 +00:00

94 lines
3.0 KiB
TypeScript

/**
* 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(),
unrecognizedFlags: [],
};
}
function buildForkArgs(fork: string, noSession = false): Args {
return {
fork,
noSession: noSession || undefined,
messages: [],
fileArgs: [],
unknownFlags: new Map(),
unrecognizedFlags: [],
};
}
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,
});
});
});