mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
fix(cli): remap bracketed Cursor wires onto bare/SKU catalogs (#1430)
* fix(cli): remap bracketed Cursor wires onto bare/SKU catalogs #1271 only handled the grok-4.5 legacy alias family. Persisted wires like gpt-5.3-codex[fast=false] still failed agent --model on resume against today's bare ACP catalogs. Remap non-legacy wires to bare bases / CLI SKUs (and nearest configOption wires), and prefer spawn-safe ids in session state. Fixes #1428 Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cli): harden Cursor stale-model remap against silent wrong picks Address Codex Major review on #1430: prefer spawn-safe ids even on exact bracket cache hits, require explicit wire params when ranking configOption candidates, and stop downgrading unavailable CLI SKU variants. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cli): preserve desired Cursor variant across spawn-safe remap Do not overwrite session.model with the bare/SKU spawn id before ACP apply, and keep spawn-safe requested ids in wireIdForCursorSessionState so apply does not re-poison hub state with bracket wires. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cli): remap-retry Cursor session/new on stale model reject Initialize and session/load already retried once after Cannot use this model; session/new had the same failure mode with an empty shared cache. Mirror the one-shot remap+respawn path and cover it with a launcher test. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cli): resolve Cursor apply against ACP option values first Spawn-safe remap prefers bare/SKU ids, but set_config_option may only accept full bracket wires. Resolve against the model option list before falling back to metadata so apply does not pick a rejected bare base. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cli): fail Cursor launch when remapped spawn cannot restore model When --model was remapped for spawn, restoring the original desired variant via ACP is mandatory. Soft-failing left sessions running on the wrong model. Cover success and hard-fail paths in launcher tests. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,9 +6,12 @@ const harness = vi.hoisted(() => ({
|
||||
initializeError: null as Error | null,
|
||||
initializeAttempts: 0,
|
||||
loadSessionError: null as Error | null,
|
||||
newSessionError: null as Error | null,
|
||||
failSetConfigOption: false,
|
||||
supportsLoadSession: true,
|
||||
loadSessionCalled: false,
|
||||
newSessionCalled: false,
|
||||
newSessionAttempts: 0,
|
||||
promptCalls: 0,
|
||||
prompts: [] as unknown[][],
|
||||
backendArgs: null as { command: string; args?: string[] } | null,
|
||||
@@ -60,7 +63,16 @@ vi.mock('./utils/cursorAcpBackend', () => ({
|
||||
return 'loaded-acp-session';
|
||||
}),
|
||||
newSession: vi.fn(async () => {
|
||||
harness.newSessionAttempts += 1;
|
||||
harness.newSessionCalled = true;
|
||||
if (harness.newSessionError && harness.newSessionAttempts === 1) {
|
||||
harness.stderrErrorHandler?.({
|
||||
type: 'model_not_found',
|
||||
message: harness.newSessionError.message,
|
||||
raw: harness.newSessionError.message
|
||||
});
|
||||
throw harness.newSessionError;
|
||||
}
|
||||
return 'new-acp-session';
|
||||
}),
|
||||
setMode: vi.fn(async () => {}),
|
||||
@@ -69,13 +81,20 @@ vi.mock('./utils/cursorAcpBackend', () => ({
|
||||
if (configId === 'model-opt' && harness.deferSetConfigOption) {
|
||||
await harness.deferSetConfigOption;
|
||||
}
|
||||
if (harness.failSetConfigOption && configId === 'model-opt') {
|
||||
throw new Error('set_config_option rejected');
|
||||
}
|
||||
harness.setConfigOptionCalls.push({ sessionId, configId, value });
|
||||
}),
|
||||
pinSessionModelWireId: vi.fn(),
|
||||
getSessionModelsMetadata: vi.fn(() => ({
|
||||
availableModels: [
|
||||
{ modelId: 'composer-2.5[fast=true]' },
|
||||
{ modelId: 'composer-2.5[fast=false]' }
|
||||
{ modelId: 'composer-2.5[fast=false]' },
|
||||
{ modelId: 'gpt-5.3-codex[reasoning=medium,fast=false]' },
|
||||
{ modelId: 'gpt-5.3-codex[reasoning=medium,fast=true]' },
|
||||
{ modelId: 'cursor-grok-4.5-medium' },
|
||||
{ modelId: 'cursor-grok-4.5-medium-fast' },
|
||||
],
|
||||
currentModelId: 'composer-2.5[fast=true]'
|
||||
})),
|
||||
@@ -96,7 +115,11 @@ vi.mock('./utils/cursorAcpBackend', () => ({
|
||||
options: [
|
||||
{ value: 'default[]' },
|
||||
{ value: 'composer-2.5[fast=true]' },
|
||||
{ value: 'composer-2.5[fast=false]' }
|
||||
{ value: 'composer-2.5[fast=false]' },
|
||||
{ value: 'gpt-5.3-codex[reasoning=medium,fast=false]' },
|
||||
{ value: 'gpt-5.3-codex[reasoning=medium,fast=true]' },
|
||||
{ value: 'cursor-grok-4.5-medium' },
|
||||
{ value: 'cursor-grok-4.5-medium-fast' },
|
||||
]
|
||||
};
|
||||
}
|
||||
@@ -167,6 +190,10 @@ import { classifyCursorAcpLoadError, cursorAcpRemoteLauncher } from './cursorAcp
|
||||
import { createCursorAcpBackend } from './utils/cursorAcpBackend';
|
||||
import { CursorSession } from './session';
|
||||
import { ApiSessionClient } from '@/api/apiSession';
|
||||
import {
|
||||
_resetSharedCursorModelsCacheForTests,
|
||||
writeSharedCursorModelsCache
|
||||
} from '@/modules/common/cursorModelsSharedCache';
|
||||
|
||||
function makeSession(sessionId: string | null): CursorSession {
|
||||
const queue = new MessageQueue2<EnhancedMode>(() => 'mode');
|
||||
@@ -213,9 +240,12 @@ describe('cursorAcpRemoteLauncher', () => {
|
||||
harness.initializeError = null;
|
||||
harness.initializeAttempts = 0;
|
||||
harness.loadSessionError = null;
|
||||
harness.newSessionError = null;
|
||||
harness.failSetConfigOption = false;
|
||||
harness.supportsLoadSession = true;
|
||||
harness.loadSessionCalled = false;
|
||||
harness.newSessionCalled = false;
|
||||
harness.newSessionAttempts = 0;
|
||||
harness.promptCalls = 0;
|
||||
harness.prompts = [];
|
||||
harness.setConfigOptionCalls = [];
|
||||
@@ -233,6 +263,7 @@ describe('cursorAcpRemoteLauncher', () => {
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
_resetSharedCursorModelsCacheForTests();
|
||||
});
|
||||
|
||||
it('spawns agent acp backend, not stream-json', async () => {
|
||||
@@ -323,6 +354,145 @@ describe('cursorAcpRemoteLauncher', () => {
|
||||
expect(legacyLauncher).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('retries session/new once after remapping a rejected bracket wire (#1430)', async () => {
|
||||
_resetSharedCursorModelsCacheForTests();
|
||||
harness.newSessionError = new Error(
|
||||
'ACP process exited (code=1, signal=null). stderr: Cannot use this model: gpt-5.3-codex[fast=false]. Available models: auto, gpt-5.3-codex, gpt-5.3-codex-fast'
|
||||
);
|
||||
|
||||
const queue = new MessageQueue2<EnhancedMode>((mode) => mode.permissionMode);
|
||||
const client = {
|
||||
rpcHandlerManager: { registerHandler: vi.fn() },
|
||||
updateMetadata: vi.fn(),
|
||||
flushMetadata: vi.fn(async () => true),
|
||||
sendSessionEvent: vi.fn(),
|
||||
sendAgentMessage: vi.fn(),
|
||||
keepAlive: vi.fn(),
|
||||
emitSessionReady: vi.fn()
|
||||
} as unknown as ApiSessionClient;
|
||||
|
||||
const session = new CursorSession({
|
||||
api: {} as never,
|
||||
client,
|
||||
path: '/tmp/project',
|
||||
logPath: '/tmp/log',
|
||||
sessionId: null,
|
||||
messageQueue: queue,
|
||||
onModeChange: vi.fn(),
|
||||
mode: 'remote',
|
||||
startedBy: 'runner',
|
||||
startingMode: 'remote',
|
||||
permissionMode: 'default',
|
||||
model: 'gpt-5.3-codex[fast=false]'
|
||||
});
|
||||
session.onSessionFoundWithProtocol = vi.fn();
|
||||
queue.push('hold-open', { permissionMode: 'default' });
|
||||
|
||||
const runPromise = cursorAcpRemoteLauncher(session);
|
||||
await vi.waitFor(() => expect(harness.newSessionAttempts).toBe(2));
|
||||
await vi.waitFor(() => {
|
||||
expect(harness.backendArgs?.args).toEqual(['--model', 'gpt-5.3-codex', 'acp']);
|
||||
expect(
|
||||
harness.setConfigOptionCalls.some(
|
||||
(call) =>
|
||||
call.configId === 'model-opt'
|
||||
&& call.value === 'gpt-5.3-codex[reasoning=medium,fast=false]'
|
||||
)
|
||||
).toBe(true);
|
||||
expect(session.model).toBe('gpt-5.3-codex[fast=false]');
|
||||
});
|
||||
|
||||
queue.close();
|
||||
await runPromise;
|
||||
});
|
||||
|
||||
it('fails launch when remapped spawn cannot restore the desired variant (#1430)', async () => {
|
||||
_resetSharedCursorModelsCacheForTests();
|
||||
harness.failSetConfigOption = true;
|
||||
harness.newSessionError = new Error(
|
||||
'ACP process exited (code=1, signal=null). stderr: Cannot use this model: composer-2.5[fast=true]. Available models: auto, composer-2.5'
|
||||
);
|
||||
|
||||
const queue = new MessageQueue2<EnhancedMode>((mode) => mode.permissionMode);
|
||||
const client = makeClient();
|
||||
const session = new CursorSession({
|
||||
api: {} as never,
|
||||
client,
|
||||
path: '/tmp/project',
|
||||
logPath: '/tmp/log',
|
||||
sessionId: null,
|
||||
messageQueue: queue,
|
||||
onModeChange: vi.fn(),
|
||||
mode: 'remote',
|
||||
startedBy: 'runner',
|
||||
startingMode: 'remote',
|
||||
permissionMode: 'default',
|
||||
model: 'composer-2.5[fast=true]'
|
||||
});
|
||||
session.onSessionFoundWithProtocol = vi.fn();
|
||||
queue.close();
|
||||
|
||||
await expect(cursorAcpRemoteLauncher(session)).rejects.toThrow(
|
||||
/Cursor model is not available via ACP: composer-2\.5\[fast=true\]/
|
||||
);
|
||||
expect(harness.newSessionAttempts).toBe(2);
|
||||
expect(harness.backendArgs?.args).toEqual(['--model', 'composer-2.5', 'acp']);
|
||||
});
|
||||
|
||||
it('spawns bare remap but reapplies original fast=true variant via ACP (#1430)', async () => {
|
||||
writeSharedCursorModelsCache({
|
||||
success: true,
|
||||
availableModels: [{ modelId: 'composer-2.5' }],
|
||||
currentModelId: 'composer-2.5',
|
||||
cliModelSkus: [{ modelId: 'composer-2.5' }],
|
||||
});
|
||||
|
||||
const queue = new MessageQueue2<EnhancedMode>((mode) => mode.permissionMode);
|
||||
const keepAlive = vi.fn();
|
||||
const client = {
|
||||
rpcHandlerManager: { registerHandler: vi.fn() },
|
||||
updateMetadata: vi.fn(),
|
||||
flushMetadata: vi.fn(async () => true),
|
||||
sendSessionEvent: vi.fn(),
|
||||
sendAgentMessage: vi.fn(),
|
||||
keepAlive,
|
||||
emitSessionReady: vi.fn()
|
||||
} as unknown as ApiSessionClient;
|
||||
|
||||
const session = new CursorSession({
|
||||
api: {} as never,
|
||||
client,
|
||||
path: '/tmp/project',
|
||||
logPath: '/tmp/log',
|
||||
sessionId: null,
|
||||
messageQueue: queue,
|
||||
onModeChange: vi.fn(),
|
||||
mode: 'remote',
|
||||
startedBy: 'runner',
|
||||
startingMode: 'remote',
|
||||
permissionMode: 'default',
|
||||
model: 'composer-2.5[fast=true]'
|
||||
});
|
||||
session.onSessionFoundWithProtocol = vi.fn();
|
||||
queue.push('hold-open', { permissionMode: 'default' });
|
||||
|
||||
const runPromise = cursorAcpRemoteLauncher(session);
|
||||
await vi.waitFor(() => expect(harness.newSessionCalled).toBe(true));
|
||||
await vi.waitFor(() => {
|
||||
expect(harness.backendArgs?.args).toEqual(['--model', 'composer-2.5', 'acp']);
|
||||
expect(
|
||||
harness.setConfigOptionCalls.some(
|
||||
(call) => call.configId === 'model-opt' && call.value === 'composer-2.5[fast=true]'
|
||||
)
|
||||
).toBe(true);
|
||||
expect(session.model).toBe('composer-2.5[fast=true]');
|
||||
});
|
||||
|
||||
queue.close();
|
||||
await runPromise;
|
||||
_resetSharedCursorModelsCacheForTests();
|
||||
});
|
||||
|
||||
it('remaps stale spawn model and retries initialize once on model rejection', async () => {
|
||||
harness.initializeError = new Error(
|
||||
'ACP process exited (code=1, signal=null). stderr: Cannot use this model: grok-4.5[fast=false]. Available models: auto, cursor-grok-4.5-medium, cursor-grok-4.5-medium-fast'
|
||||
|
||||
Reference in New Issue
Block a user