mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-22 22:01:18 +00:00
* feat(cli,web,hub): migrate Cursor remote sessions to ACP with model/effort pickers Move stream-json remote launcher to legacy path and add ACP launcher with set_config_option model/mode sync, optimistic keepalive on config changes, and shared catalog caching. Web gets dual base/effort Cursor pickers for session and new-session flows; hide composer status bar when Cursor sends no usage_update. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cli,web,shared): Cursor model picker — ACP wires + CLI sku variants Enrich the web/mobile picker with agent --list-models SKUs grouped under ACP wire bases, fix session-open base highlight, and keep catalog discovery safe while the ACP transport holds the CLI lock. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(cursor-acp): apply ACP default model when web resets to Default Web sends model: null for Default; push session/set_config_option with the ACP default[] wire so Cursor backend matches hub state. Regression tests for setModel(null) and applyModelConfig(null). Co-authored-by: Cursor <cursoragent@cursor.com> * fix(acp): clear stale agent-acp lock when owning process is gone Check lock pid with signal 0; remove orphaned lock dirs after SIGKILL or crash so listCursorModels can run cold probes again. Regression tests for guard and catalog discovery. Co-authored-by: Cursor <cursoragent@cursor.com> * test(cursor): use live pid for ACP lock handler tests Stale-lock cleanup clears dead pids; handler tests must simulate an active lock with the current process pid to avoid cold probes/timeouts. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(acp): scope agent CLI lock guard to Cursor agent command only Gemini/OpenCode/Kimi ACP sessions must not register agent-acp-active; that blocked listCursorModels while unrelated backends were running. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): reject Cursor model changes for local sessions Hub returns 409 when controlledByUser is set, matching Codex. Web hides model and variant pickers for local Cursor sessions so users do not hit a dead RPC path. Document pre-push-review in AGENTS.md. Verified: bun typecheck; bun run test (919 cli + 243 hub + 768 web + 46 shared). Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): send stable ids for Cursor ask_question replies Parse and submit question.id and option.id so ACP receives keys like { approach: ['a'] } instead of index/label. Verified: bun typecheck && bun run test. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
462 lines
14 KiB
TypeScript
462 lines
14 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import type { ApiSessionClient } from '@/api/apiSession';
|
|
import type { AgentBackend, PermissionRequest, PermissionResponse } from './types';
|
|
import { PermissionAdapter } from './permissionAdapter';
|
|
|
|
type FakeAgentState = {
|
|
requests: Record<string, unknown>;
|
|
completedRequests: Record<string, unknown>;
|
|
};
|
|
|
|
type Harness = ReturnType<typeof createHarness>;
|
|
|
|
function createHarness() {
|
|
let agentState: FakeAgentState = {
|
|
requests: {},
|
|
completedRequests: {}
|
|
};
|
|
|
|
const rpcHandlers = new Map<string, (params: unknown) => Promise<unknown> | unknown>();
|
|
let permissionHandler: ((request: PermissionRequest) => void) | null = null;
|
|
const respondCalls: Array<{
|
|
sessionId: string;
|
|
request: PermissionRequest;
|
|
response: PermissionResponse;
|
|
}> = [];
|
|
|
|
const session = {
|
|
rpcHandlerManager: {
|
|
registerHandler(method: string, handler: (params: unknown) => Promise<unknown> | unknown) {
|
|
rpcHandlers.set(method, handler);
|
|
}
|
|
},
|
|
updateAgentState(handler: (state: FakeAgentState) => FakeAgentState) {
|
|
agentState = handler(agentState);
|
|
}
|
|
} as unknown as ApiSessionClient;
|
|
|
|
const backend: AgentBackend = {
|
|
async initialize() {},
|
|
async newSession() {
|
|
return 'agent-session';
|
|
},
|
|
async prompt() {},
|
|
async cancelPrompt() {},
|
|
async respondToPermission(sessionId, request, response) {
|
|
respondCalls.push({ sessionId, request, response });
|
|
},
|
|
onPermissionRequest(handler) {
|
|
permissionHandler = handler;
|
|
},
|
|
async disconnect() {}
|
|
};
|
|
|
|
new PermissionAdapter(session, backend);
|
|
|
|
return {
|
|
rpcHandlers,
|
|
respondCalls,
|
|
getAgentState: () => agentState,
|
|
emitPermissionRequest(request: PermissionRequest) {
|
|
if (!permissionHandler) {
|
|
throw new Error('Permission handler was not registered');
|
|
}
|
|
permissionHandler(request);
|
|
}
|
|
};
|
|
}
|
|
|
|
function createHarnessWithInterceptor(
|
|
intercept: (response: { id: string; approved: boolean; decision?: string }) => Promise<boolean>
|
|
) {
|
|
let agentState: FakeAgentState = {
|
|
requests: {},
|
|
completedRequests: {}
|
|
};
|
|
|
|
const rpcHandlers = new Map<string, (params: unknown) => Promise<unknown> | unknown>();
|
|
let permissionHandler: ((request: PermissionRequest) => void) | null = null;
|
|
const respondCalls: Array<{
|
|
sessionId: string;
|
|
request: PermissionRequest;
|
|
response: PermissionResponse;
|
|
}> = [];
|
|
|
|
const session = {
|
|
rpcHandlerManager: {
|
|
registerHandler(method: string, handler: (params: unknown) => Promise<unknown> | unknown) {
|
|
rpcHandlers.set(method, handler);
|
|
}
|
|
},
|
|
updateAgentState(handler: (state: FakeAgentState) => FakeAgentState) {
|
|
agentState = handler(agentState);
|
|
}
|
|
} as unknown as ApiSessionClient;
|
|
|
|
const backend: AgentBackend = {
|
|
async initialize() {},
|
|
async newSession() {
|
|
return 'agent-session';
|
|
},
|
|
async prompt() {},
|
|
async cancelPrompt() {},
|
|
async respondToPermission(sessionId, request, response) {
|
|
respondCalls.push({ sessionId, request, response });
|
|
},
|
|
onPermissionRequest(handler) {
|
|
permissionHandler = handler;
|
|
},
|
|
async disconnect() {}
|
|
};
|
|
|
|
new PermissionAdapter(session, backend, undefined, intercept);
|
|
|
|
return {
|
|
rpcHandlers,
|
|
respondCalls,
|
|
getAgentState: () => agentState,
|
|
emitPermissionRequest(request: PermissionRequest) {
|
|
if (!permissionHandler) {
|
|
throw new Error('Permission handler was not registered');
|
|
}
|
|
permissionHandler(request);
|
|
}
|
|
};
|
|
}
|
|
|
|
function createHarnessWithMode(getPermissionMode: () => 'default' | 'read-only' | 'safe-yolo' | 'yolo') {
|
|
let agentState: FakeAgentState = {
|
|
requests: {},
|
|
completedRequests: {}
|
|
};
|
|
|
|
const rpcHandlers = new Map<string, (params: unknown) => Promise<unknown> | unknown>();
|
|
let permissionHandler: ((request: PermissionRequest) => void) | null = null;
|
|
const respondCalls: Array<{
|
|
sessionId: string;
|
|
request: PermissionRequest;
|
|
response: PermissionResponse;
|
|
}> = [];
|
|
|
|
const session = {
|
|
rpcHandlerManager: {
|
|
registerHandler(method: string, handler: (params: unknown) => Promise<unknown> | unknown) {
|
|
rpcHandlers.set(method, handler);
|
|
}
|
|
},
|
|
updateAgentState(handler: (state: FakeAgentState) => FakeAgentState) {
|
|
agentState = handler(agentState);
|
|
}
|
|
} as unknown as ApiSessionClient;
|
|
|
|
const backend: AgentBackend = {
|
|
async initialize() {},
|
|
async newSession() {
|
|
return 'agent-session';
|
|
},
|
|
async prompt() {},
|
|
async cancelPrompt() {},
|
|
async respondToPermission(sessionId, request, response) {
|
|
respondCalls.push({ sessionId, request, response });
|
|
},
|
|
onPermissionRequest(handler) {
|
|
permissionHandler = handler;
|
|
},
|
|
async disconnect() {}
|
|
};
|
|
|
|
new PermissionAdapter(session, backend, getPermissionMode);
|
|
|
|
return {
|
|
rpcHandlers,
|
|
respondCalls,
|
|
getAgentState: () => agentState,
|
|
emitPermissionRequest(request: PermissionRequest) {
|
|
if (!permissionHandler) {
|
|
throw new Error('Permission handler was not registered');
|
|
}
|
|
permissionHandler(request);
|
|
}
|
|
};
|
|
}
|
|
|
|
async function flushAsyncWork(): Promise<void> {
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
}
|
|
|
|
function buildRequest(overrides?: Partial<PermissionRequest>): PermissionRequest {
|
|
return {
|
|
id: 'perm-1',
|
|
sessionId: 'session-1',
|
|
toolCallId: 'perm-1',
|
|
title: 'Read',
|
|
rawInput: { path: 'README.md' },
|
|
options: [
|
|
{
|
|
optionId: 'allow-once',
|
|
name: 'Allow once',
|
|
kind: 'allow_once'
|
|
},
|
|
{
|
|
optionId: 'allow-always',
|
|
name: 'Allow always',
|
|
kind: 'allow_always'
|
|
}
|
|
],
|
|
...overrides
|
|
};
|
|
}
|
|
|
|
describe('PermissionAdapter', () => {
|
|
it('auto-approves change_title permissions without queueing them', async () => {
|
|
const harness = createHarness();
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-title',
|
|
toolCallId: 'perm-title',
|
|
title: 'hapi_change_title',
|
|
rawInput: { title: 'Rename chat' }
|
|
}));
|
|
|
|
await flushAsyncWork();
|
|
|
|
expect(harness.respondCalls).toEqual([
|
|
{
|
|
sessionId: 'session-1',
|
|
request: expect.objectContaining({
|
|
id: 'perm-title',
|
|
title: 'hapi_change_title'
|
|
}),
|
|
response: { outcome: 'selected', optionId: 'allow-once' }
|
|
}
|
|
]);
|
|
expect(harness.getAgentState().requests).toEqual({});
|
|
expect(harness.getAgentState().completedRequests).toMatchObject({
|
|
'perm-title': {
|
|
tool: 'hapi_change_title',
|
|
status: 'approved',
|
|
decision: 'approved'
|
|
}
|
|
});
|
|
});
|
|
|
|
it('auto-approves change_title aliases detected from the tool call id', async () => {
|
|
const harness = createHarness();
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'mcp__hapi__change_title-1',
|
|
toolCallId: 'mcp__hapi__change_title-1',
|
|
title: undefined,
|
|
rawInput: { title: 'Rename chat' }
|
|
}));
|
|
|
|
await flushAsyncWork();
|
|
|
|
expect(harness.respondCalls).toHaveLength(1);
|
|
expect(harness.getAgentState().requests).toEqual({});
|
|
expect(harness.getAgentState().completedRequests).toMatchObject({
|
|
'mcp__hapi__change_title-1': {
|
|
status: 'approved',
|
|
decision: 'approved'
|
|
}
|
|
});
|
|
});
|
|
|
|
it('keeps non-title permissions pending until the hub responds', async () => {
|
|
const harness = createHarness();
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-read',
|
|
toolCallId: 'perm-read',
|
|
title: 'Read'
|
|
}));
|
|
|
|
expect(harness.respondCalls).toEqual([]);
|
|
expect(harness.getAgentState().requests).toMatchObject({
|
|
'perm-read': {
|
|
tool: 'Read'
|
|
}
|
|
});
|
|
|
|
const permissionRpc = harness.rpcHandlers.get('permission');
|
|
expect(permissionRpc).toBeTypeOf('function');
|
|
|
|
await permissionRpc?.({
|
|
id: 'perm-read',
|
|
approved: true,
|
|
decision: 'approved'
|
|
});
|
|
|
|
expect(harness.respondCalls).toEqual([
|
|
{
|
|
sessionId: 'session-1',
|
|
request: expect.objectContaining({
|
|
id: 'perm-read',
|
|
title: 'Read'
|
|
}),
|
|
response: { outcome: 'selected', optionId: 'allow-once' }
|
|
}
|
|
]);
|
|
expect(harness.getAgentState().requests).toEqual({});
|
|
expect(harness.getAgentState().completedRequests).toMatchObject({
|
|
'perm-read': {
|
|
tool: 'Read',
|
|
status: 'approved',
|
|
decision: 'approved'
|
|
}
|
|
});
|
|
});
|
|
|
|
it('auto-approves non-title tools once in safe-yolo mode', async () => {
|
|
const harness = createHarnessWithMode(() => 'safe-yolo');
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-safe',
|
|
toolCallId: 'perm-safe',
|
|
title: 'Read'
|
|
}));
|
|
|
|
await flushAsyncWork();
|
|
|
|
expect(harness.respondCalls).toEqual([
|
|
{
|
|
sessionId: 'session-1',
|
|
request: expect.objectContaining({
|
|
id: 'perm-safe',
|
|
title: 'Read'
|
|
}),
|
|
response: { outcome: 'selected', optionId: 'allow-once' }
|
|
}
|
|
]);
|
|
expect(harness.getAgentState().requests).toEqual({});
|
|
expect(harness.getAgentState().completedRequests).toMatchObject({
|
|
'perm-safe': {
|
|
tool: 'Read',
|
|
status: 'approved',
|
|
decision: 'approved'
|
|
}
|
|
});
|
|
});
|
|
|
|
it('auto-approves non-title tools for the session in yolo mode', async () => {
|
|
const harness = createHarnessWithMode(() => 'yolo');
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-yolo',
|
|
toolCallId: 'perm-yolo',
|
|
title: 'Read'
|
|
}));
|
|
|
|
await flushAsyncWork();
|
|
|
|
expect(harness.respondCalls).toEqual([
|
|
{
|
|
sessionId: 'session-1',
|
|
request: expect.objectContaining({
|
|
id: 'perm-yolo',
|
|
title: 'Read'
|
|
}),
|
|
response: { outcome: 'selected', optionId: 'allow-always' }
|
|
}
|
|
]);
|
|
expect(harness.getAgentState().requests).toEqual({});
|
|
expect(harness.getAgentState().completedRequests).toMatchObject({
|
|
'perm-yolo': {
|
|
tool: 'Read',
|
|
status: 'approved',
|
|
decision: 'approved_for_session'
|
|
}
|
|
});
|
|
});
|
|
|
|
it('auto-approves read-only non-write tools but keeps writes pending', async () => {
|
|
const harness = createHarnessWithMode(() => 'read-only');
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-read-only-read',
|
|
toolCallId: 'perm-read-only-read',
|
|
title: 'Read'
|
|
}));
|
|
|
|
await flushAsyncWork();
|
|
|
|
expect(harness.respondCalls).toEqual([
|
|
{
|
|
sessionId: 'session-1',
|
|
request: expect.objectContaining({
|
|
id: 'perm-read-only-read',
|
|
title: 'Read'
|
|
}),
|
|
response: { outcome: 'selected', optionId: 'allow-once' }
|
|
}
|
|
]);
|
|
expect(harness.getAgentState().completedRequests).toMatchObject({
|
|
'perm-read-only-read': {
|
|
tool: 'Read',
|
|
status: 'approved',
|
|
decision: 'approved'
|
|
}
|
|
});
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-read-only-write',
|
|
toolCallId: 'perm-read-only-write',
|
|
title: 'Patch'
|
|
}));
|
|
|
|
expect(harness.respondCalls).toHaveLength(1);
|
|
expect(harness.getAgentState().requests).toMatchObject({
|
|
'perm-read-only-write': {
|
|
tool: 'Patch'
|
|
}
|
|
});
|
|
});
|
|
|
|
it('delegates permission RPC to interceptor before handling standard permissions', async () => {
|
|
const intercept = vi.fn(async () => true);
|
|
const harness = createHarnessWithInterceptor(intercept);
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-intercept',
|
|
toolCallId: 'perm-intercept',
|
|
title: 'Read'
|
|
}));
|
|
|
|
const permissionRpc = harness.rpcHandlers.get('permission');
|
|
await permissionRpc?.({
|
|
id: 'perm-intercept',
|
|
approved: true,
|
|
decision: 'approved'
|
|
});
|
|
|
|
expect(intercept).toHaveBeenCalledWith({
|
|
id: 'perm-intercept',
|
|
approved: true,
|
|
decision: 'approved'
|
|
});
|
|
expect(harness.respondCalls).toEqual([]);
|
|
});
|
|
|
|
it('handles permission RPC when interceptor returns false', async () => {
|
|
const intercept = vi.fn(async () => false);
|
|
const harness = createHarnessWithInterceptor(intercept);
|
|
|
|
harness.emitPermissionRequest(buildRequest({
|
|
id: 'perm-standard',
|
|
toolCallId: 'perm-standard',
|
|
title: 'Read'
|
|
}));
|
|
|
|
const permissionRpc = harness.rpcHandlers.get('permission');
|
|
await permissionRpc?.({
|
|
id: 'perm-standard',
|
|
approved: true,
|
|
decision: 'approved'
|
|
});
|
|
|
|
expect(intercept).toHaveBeenCalledOnce();
|
|
expect(harness.respondCalls).toHaveLength(1);
|
|
});
|
|
});
|