mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-25 05:07:05 +00:00
feat(acp): re-enable change_title for ACP launchers with manual title precedence (#1672)
* feat(acp): re-enable change_title for ACP launchers with manual title precedence opencode/cursor/kimi disabled the hapi change_title MCP tool (#1028), relying solely on native ACP session-title sync. When the agent never generates a native title there was no way to set one short of hub API calls. Re-enable change_title on those launchers. The shared title-sync controller marks manual titles, so later native sessionInfoUpdate titles no longer overwrite them. * fix(acp): include copilot in change_title re-enable and harden tests Copilot has the same ACP title-sync shape as opencode/cursor/kimi, so it gets the same shared controller wiring. Add a cursor regression test asserting one controller instance is shared across backend (re)creation paths and cover the onChangeTitle failure path. * fix(acp): persist manual title precedence in session metadata The manual flag lived only in one controller closure, so a recreated remote launcher or a resumed session started fresh and let the next native ACP sessionInfoUpdate overwrite a change_title rename. Store acpManualTitle in session metadata and initialize new controllers from it. * fix(agent): preserve acpManualTitle through existing-session bootstrap bootstrapExistingSession rebuilds metadata through an allowlist, so the manual-title marker was dropped on process resume and native ACP title sync could overwrite a change_title rename. * fix(cursor): preserve manual title precedence across live relaunch
This commit is contained in:
@@ -13,7 +13,7 @@ const harness = vi.hoisted(() => ({
|
||||
promptCount: 0,
|
||||
promptContents: [] as PromptContent[][],
|
||||
refreshSessionInfoCalls: [] as Array<{ sessionId: string; cwd: string }>,
|
||||
bridgeOptions: null as { enableChangeTitle?: boolean; skillLookup?: { workingDirectory: string; flavor: string } } | null,
|
||||
bridgeOptions: null as { onChangeTitle?: () => void; skillLookup?: { workingDirectory: string; flavor: string } } | null,
|
||||
events: [] as string[],
|
||||
cleanupEvents: [] as string[],
|
||||
setModelImpl: null as null | ((sessionId: string, modelId: string) => Promise<void>),
|
||||
@@ -225,7 +225,7 @@ vi.mock('./utils/opencodeBackend', () => ({
|
||||
}),}));
|
||||
|
||||
vi.mock('@/codex/utils/buildHapiMcpBridge', () => ({
|
||||
buildHapiMcpBridge: async (_client: unknown, options?: { enableChangeTitle?: boolean; skillLookup?: { workingDirectory: string; flavor: string } }) => {
|
||||
buildHapiMcpBridge: async (_client: unknown, options?: { onChangeTitle?: () => void; skillLookup?: { workingDirectory: string; flavor: string } }) => {
|
||||
harness.bridgeOptions = options ?? null;
|
||||
return {
|
||||
server: { stop: () => { harness.cleanupEvents.push('cleanup:server-stop'); if (harness.serverStopError) throw harness.serverStopError; } },
|
||||
@@ -467,6 +467,10 @@ function createSessionStub(
|
||||
}
|
||||
},
|
||||
sendAgentMessage(message: unknown) { sentAgentMessages.push(message); },
|
||||
getMetadata() {
|
||||
return null;
|
||||
},
|
||||
updateMetadata(_handler: (metadata: unknown) => unknown) {},
|
||||
sendClaudeSessionMessage(message: unknown) {
|
||||
claudeSessionMessages.push(message);
|
||||
},
|
||||
@@ -1833,7 +1837,7 @@ describe('opencodeRemoteLauncher inline model switch', () => {
|
||||
expect(JSON.stringify(harness.promptContents[0])).toContain('$name');
|
||||
expect(JSON.stringify(harness.promptContents[0])).toContain('skill_lookup');
|
||||
expect(JSON.stringify(harness.promptContents[0])).toContain('hapi_display_image');
|
||||
expect(JSON.stringify(harness.promptContents[0])).not.toContain('hapi_change_title');
|
||||
expect(JSON.stringify(harness.promptContents[0])).toContain('hapi_change_title');
|
||||
expect(JSON.stringify(harness.promptContents[1])).not.toContain('skill_lookup');
|
||||
});
|
||||
|
||||
@@ -1943,7 +1947,7 @@ describe('opencodeRemoteLauncher inline model switch', () => {
|
||||
await opencodeRemoteLauncher(session as never);
|
||||
|
||||
expect(harness.bridgeOptions).toEqual({
|
||||
enableChangeTitle: false,
|
||||
onChangeTitle: expect.any(Function),
|
||||
skillLookup: { workingDirectory: '/tmp/hapi-opencode-test', flavor: 'opencode' }
|
||||
});
|
||||
expect(harness.refreshSessionInfoCalls).toEqual([
|
||||
@@ -2169,7 +2173,7 @@ describe('opencodeRemoteLauncher inline model switch', () => {
|
||||
expect(content[0]?.text).toContain('You are in plan mode');
|
||||
expect(content[0]?.text).toContain('Do not execute tools');
|
||||
expect(content[0]?.text).toContain('design the fix');
|
||||
expect(content[0]?.text).not.toContain('hapi_change_title');
|
||||
expect(content[0]?.text).toContain('hapi_change_title');
|
||||
});
|
||||
|
||||
it('registers a listOpencodeModels RPC handler that returns the backend cache', async () => {
|
||||
@@ -4707,6 +4711,8 @@ describe('opencodeRemoteLauncher v2 flavor wiring', () => {
|
||||
harness.servePid = null;
|
||||
harness.servePidCalls = [];
|
||||
harness.events = [];
|
||||
harness.eventStreamOptions = [];
|
||||
harness.eventStreamCloseCount = 0;
|
||||
harness.sessionModelsMetadata = undefined;
|
||||
harness.promptCount = 0;
|
||||
harness.promptImpl = null;
|
||||
@@ -4725,6 +4731,28 @@ describe('opencodeRemoteLauncher v2 flavor wiring', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it.each(['v1', 'v2'] as const)('%s: preserves manual title precedence after backend selection', async (version) => {
|
||||
harness.detectedVersion = version;
|
||||
harness.discoveredPort = 41234;
|
||||
const { session, claudeSessionMessages } = createSessionStub([{ message: 'hello', mode: createMode() }]);
|
||||
|
||||
await opencodeRemoteLauncher(session as never);
|
||||
|
||||
const { createOpencodeBackend } = await import('./utils/opencodeBackend');
|
||||
const backend = vi.mocked(createOpencodeBackend).mock.results.at(-1)?.value;
|
||||
const listener = vi.mocked(backend!.setSessionInfoUpdateListener).mock.calls[0]?.[0];
|
||||
expect(listener).toBeTypeOf('function');
|
||||
expect(harness.bridgeOptions?.onChangeTitle).toBeTypeOf('function');
|
||||
const initialCount = claudeSessionMessages.length;
|
||||
listener!({ sessionId: 'acp-session-1', title: 'Native Title' });
|
||||
expect(claudeSessionMessages).toHaveLength(initialCount + 1);
|
||||
expect(claudeSessionMessages.at(-1)).toMatchObject({ type: 'summary', summary: 'Native Title' });
|
||||
|
||||
harness.bridgeOptions!.onChangeTitle!();
|
||||
listener!({ sessionId: 'acp-session-1', title: 'Later Native Title' });
|
||||
expect(claudeSessionMessages).toHaveLength(initialCount + 1);
|
||||
});
|
||||
|
||||
it('v2: reports one automatic application start without changing turn thinking for preparation', async () => {
|
||||
harness.detectedVersion = 'v2';
|
||||
harness.discoveredPort = 41234;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React from 'react';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import { registerAcpSessionTitleSync } from '@/agent/acpSessionTitle';
|
||||
import { createAcpSessionTitleSync, registerAcpSessionTitleSync } from '@/agent/acpSessionTitle';
|
||||
import { logger } from '@/ui/logger';
|
||||
import { buildHapiMcpBridge } from '@/codex/utils/buildHapiMcpBridge';
|
||||
import type { AcpSdkBackend } from '@/agent/backends/acp';
|
||||
@@ -278,8 +278,9 @@ class OpencodeRemoteLauncher extends RemoteLauncherBase {
|
||||
// advertises "not steerable" rather than a stale value.
|
||||
this.setSteeringActive(false);
|
||||
|
||||
const titleSync = createAcpSessionTitleSync(session.client);
|
||||
const { server: happyServer, mcpServers } = await buildHapiMcpBridge(session.client, {
|
||||
enableChangeTitle: false,
|
||||
onChangeTitle: () => titleSync.markManualTitle(),
|
||||
skillLookup: { workingDirectory: session.path, flavor: 'opencode' }
|
||||
});
|
||||
this.happyServer = happyServer;
|
||||
@@ -320,7 +321,7 @@ class OpencodeRemoteLauncher extends RemoteLauncherBase {
|
||||
hostname
|
||||
});
|
||||
this.backend = backend;
|
||||
registerAcpSessionTitleSync(backend, session.client);
|
||||
registerAcpSessionTitleSync(backend, session.client, titleSync);
|
||||
|
||||
backend.onStderrError((error) => {
|
||||
this.handleAcpStderrError(error);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mkdtemp, readFile, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
@@ -28,3 +28,29 @@ describe('OpenCode local HAPI instructions', () => {
|
||||
expect(instructions).toContain('skill_lookup')
|
||||
})
|
||||
})
|
||||
|
||||
describe('OpenCode ACP title and peer instructions', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs()
|
||||
vi.resetModules()
|
||||
})
|
||||
|
||||
it.each([false, true])('keeps change_title available when peer tools are disabled=%s', async (disabled) => {
|
||||
vi.stubEnv('HAPI_DISABLE_PEER_TOOLS', disabled ? '1' : '')
|
||||
vi.resetModules()
|
||||
const { TITLE_INSTRUCTION, OPENCODE_NATIVE_TOOL_INSTRUCTION } = await import('./systemPrompt')
|
||||
|
||||
for (const prompt of [TITLE_INSTRUCTION, OPENCODE_NATIVE_TOOL_INSTRUCTION]) {
|
||||
expect(prompt).toContain('hapi_change_title')
|
||||
expect(prompt).toContain('hapi_display_image')
|
||||
expect(prompt).toContain('skill_lookup')
|
||||
for (const tool of ['hapi_list_peers', 'hapi_inspect_peer', 'hapi_ping_peer']) {
|
||||
if (disabled) {
|
||||
expect(prompt).not.toContain(tool)
|
||||
} else {
|
||||
expect(prompt).toContain(tool)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
import { trimIdent } from '@/utils/trimIdent';
|
||||
import { buildSessionCitationSteerInstruction, SESSION_CITATION_DISABLED_STEER } from '@hapi/protocol/sessionCitation';
|
||||
import { isPeerToolsDisabled } from '@/modules/common/peerTools';
|
||||
import { HAPI_MCP_BRIDGE_PROMPT } from '@/modules/common/hapiMcpBridgePrompt';
|
||||
import { HAPI_MCP_BRIDGE_PROMPT, HAPI_MCP_TITLE_INSTRUCTION } from '@/modules/common/hapiMcpBridgePrompt';
|
||||
import {
|
||||
DISPLAY_IMAGE_PROMPT_HAPI_MCP,
|
||||
DISPLAY_MEDIA_PROMPT_HAPI_MCP,
|
||||
@@ -36,10 +36,11 @@ export function getTitleInstruction(env: NodeJS.ProcessEnv = process.env): strin
|
||||
}
|
||||
|
||||
/**
|
||||
* Tool instructions for native ACP sessions. Title updates come from ACP, so
|
||||
* advertise only the MCP tools that remain available to the model.
|
||||
* Instruction prepended to native ACP session prompts. change_title rides the
|
||||
* same MCP bridge; manual titles take precedence over native ACP title sync.
|
||||
*/
|
||||
export const OPENCODE_NATIVE_TOOL_INSTRUCTION = trimIdent(`
|
||||
${HAPI_MCP_TITLE_INSTRUCTION}
|
||||
${DISPLAY_IMAGE_PROMPT_HAPI_MCP}
|
||||
${DISPLAY_VIDEO_PROMPT_HAPI_MCP}
|
||||
${DISPLAY_MEDIA_PROMPT_HAPI_MCP}
|
||||
|
||||
Reference in New Issue
Block a user