Skip to content

Build your personal knowledge base with Trilium Notes

perf(llm): share one title session per ACP agent process

Elian Doran7832732a840c
diff --git a/apps/server/src/services/llm/providers/acp_agent.ts b/apps/server/src/services/llm/providers/acp_agent.ts
index 097ee035f9..ae5844cbc1 100644
--- a/apps/server/src/services/llm/providers/acp_agent.ts
+++ b/apps/server/src/services/llm/providers/acp_agent.ts
@@ -110,6 +110,13 @@ const PROMPT_TIMEOUT_MS = 15 * 60_000;
/** Session mappings kept per chat note; bounded to avoid unbounded growth. */
const MAX_TRACKED_SESSIONS = 200;
+/**
+ * Titles one session answers before it is replaced. Titles share a session to
+ * skip `session/new` (~2.5 s on Antigravity, which also keeps every session on
+ * disk); the cap bounds the history each title prompt carries.
+ */
+export const MAX_TITLES_PER_SESSION = 20;
+
/** The name the loopback MCP server is registered under in `session/new`. */
export const NOTE_TOOLS_MCP_SERVER_NAME = "trilium";
@@ -147,6 +154,18 @@ interface ProviderState {
agentCwd?: string;
/** The agent process shared by chat turns and titles. */
pool: AcpClientPool;
+ /** The session titles are generated in. */
+ titleSession?: TitleSession;
+ /** Settles when the title in progress does; a session answers one prompt at a time. */
+ titleQueue: Promise<unknown>;
+}
+
+interface TitleSession {
+ sessionId: string;
+ /** The {@link AcpLease.generation} of the process that holds the session. */
+ generation: number;
+ /** Titles the session has been asked for. */
+ titles: number;
}
const stateByProvider = new Map<string, ProviderState>();
@@ -542,17 +561,26 @@ export abstract class AcpAgentProvider implements LlmProvider {
}
}
+ /**
+ * Name a chat after its first message, in the session titles share. Titles
+ * take turns, because a session answers one prompt at a time.
+ */
async generateTitle(firstMessage: string): Promise<string> {
+ const state = this.state();
+ const title = state.titleQueue.then(() => this.requestTitle(firstMessage));
+ state.titleQueue = title;
+ return title;
+ }
+
+ private async requestTitle(firstMessage: string): Promise<string> {
const pool = this.state().pool;
let lease: AcpLease | undefined;
- let attachedSession: string | undefined;
+ let sessionId: string | undefined;
+ let answered = false;
try {
let title = "";
lease = await pool.acquire(connection => this.connect(connection));
- const client = lease.client;
-
- const { sessionId } = await this.createSession(client, { cwd: this.agentCwd(), mcpServers: [] }, false, SESSION_TIMEOUT_MS);
- attachedSession = sessionId;
+ sessionId = await this.titleSessionFor(lease);
pool.attach(sessionId, {
onUpdate: params => {
const update = (params as AcpSessionUpdate).update;
@@ -561,36 +589,31 @@ export abstract class AcpAgentProvider implements LlmProvider {
}
}
});
- const titleModel = this.titleModelId();
- if (titleModel) {
- try {
- await client.request("session/set_model", { sessionId, modelId: titleModel }, INIT_TIMEOUT_MS);
- } catch {
- // Title generation works on any model; ignore selection failures.
- }
- }
- await client.request(
+ await lease.client.request(
"session/prompt",
{
sessionId,
prompt: [{
type: "text",
- text: `Generate a short title (at most 5 words) summarizing this chat message. Reply with only the title, no quotes or punctuation around it:\n\n${firstMessage.substring(0, 500)}`
+ text: `Generate a short title (at most 5 words) summarizing the chat message below. It is a new request: earlier messages in this conversation do not apply. Reply with only the title, no quotes or punctuation around it:
+
+${firstMessage.substring(0, 500)}`
}]
},
SESSION_TIMEOUT_MS
);
+ answered = true;
return title.trim().replace(/^["']|["']$/g, "").substring(0, 100);
} catch (error) {
getLog().error(`${this.logLabel} title generation failed: ${this.describeFailure(error)}`);
return "";
} finally {
- if (attachedSession) {
- pool.detach(attachedSession);
- // The title session is used once, and the process outlives it.
- if (lease && sessionClosers.has(lease.client)) {
- await this.closeAgentSession(lease.client, attachedSession);
- }
+ if (sessionId) {
+ pool.detach(sessionId);
+ }
+ // A failed prompt leaves the session in an unknown state.
+ if (lease && sessionId && !answered) {
+ await this.retireTitleSession(lease);
}
if (lease) {
pool.release();
@@ -598,6 +621,43 @@ export abstract class AcpAgentProvider implements LlmProvider {
}
}
+ /**
+ * The title session on the lease's process, opening one on the title model
+ * when the process has none or the current one reached
+ * {@link MAX_TITLES_PER_SESSION}.
+ */
+ private async titleSessionFor(lease: AcpLease): Promise<string> {
+ const state = this.state();
+ const current = state.titleSession;
+ if (current?.generation === lease.generation && current.titles < MAX_TITLES_PER_SESSION) {
+ current.titles++;
+ return current.sessionId;
+ }
+ await this.retireTitleSession(lease);
+
+ const { sessionId } = await this.createSession(lease.client, { cwd: this.agentCwd(), mcpServers: [] }, false, SESSION_TIMEOUT_MS);
+ const titleModel = this.titleModelId();
+ if (titleModel) {
+ try {
+ await lease.client.request("session/set_model", { sessionId, modelId: titleModel }, INIT_TIMEOUT_MS);
+ } catch {
+ // Title generation works on any model; ignore selection failures.
+ }
+ }
+ state.titleSession = { sessionId, generation: lease.generation, titles: 1 };
+ return sessionId;
+ }
+
+ /** Stop using the title session, closing it when its process is the lease's and the agent supports that. */
+ private async retireTitleSession(lease: AcpLease): Promise<void> {
+ const state = this.state();
+ const retired = state.titleSession;
+ state.titleSession = undefined;
+ if (retired?.generation === lease.generation && sessionClosers.has(lease.client)) {
+ await this.closeAgentSession(lease.client, retired.sessionId);
+ }
+ }
+
/** Ask the agent to drop a session it no longer needs, logging a refusal. */
private async closeAgentSession(client: AcpClient, sessionId: string): Promise<void> {
try {
@@ -706,7 +766,7 @@ export abstract class AcpAgentProvider implements LlmProvider {
private state(): ProviderState {
let state = stateByProvider.get(this.name);
if (!state) {
- state = { sessionsByChatNote: new Map(), pool: new AcpClientPool(this.logLabel) };
+ state = { sessionsByChatNote: new Map(), pool: new AcpClientPool(this.logLabel), titleQueue: Promise.resolve() };
stateByProvider.set(this.name, state);
}
return state;
diff --git a/apps/server/src/services/llm/providers/copilot_agent.spec.ts b/apps/server/src/services/llm/providers/copilot_agent.spec.ts
index db9be570b6..2eec9cef52 100644
--- a/apps/server/src/services/llm/providers/copilot_agent.spec.ts
+++ b/apps/server/src/services/llm/providers/copilot_agent.spec.ts
@@ -141,7 +141,7 @@ class FakeAcpError extends Error {
vi.mock("./acp_client.js", () => ({ AcpClient: FakeAcpClient, AcpError: FakeAcpError }));
-const { buildPromptBlocks, createUpdateCollector, denyPermission } = await import("./acp_agent.js");
+const { buildPromptBlocks, createUpdateCollector, denyPermission, MAX_TITLES_PER_SESSION } = await import("./acp_agent.js");
const { IDLE_TIMEOUT_MS } = await import("./acp_client_pool.js");
const { buildCopilotModelList, CopilotAgentProvider, resetModelCatalogCacheForTests } = await import("./copilot_agent.js");
@@ -871,25 +871,78 @@ describe("CopilotAgentProvider.generateTitle", () => {
expect(FakeAcpClient.current?.disposed).toBe(false);
});
- it("closes the title session when the agent supports session/close", async () => {
+ it("names chats in one shared session on the process, one title at a time", async () => {
const provider = new CopilotAgentProvider();
- await provider.generateTitle("hi");
- expect(FakeAcpClient.current?.requests.some(r => r.method === "session/close")).toBe(false);
+ let prompts = 0;
+ let concurrent = 0;
+ let maxConcurrent = 0;
+ FakeAcpClient.promptScript = async client => {
+ concurrent++;
+ maxConcurrent = Math.max(maxConcurrent, concurrent);
+ prompts++;
+ const title = `Title ${prompts}`;
+ await new Promise(resolve => setTimeout(resolve, 5));
+ client.update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: title } });
+ concurrent--;
+ return { stopReason: "end_turn" };
+ };
- resetModelCatalogCacheForTests();
+ expect(await Promise.all([provider.generateTitle("a"), provider.generateTitle("b"), provider.generateTitle("c")]))
+ .toEqual(["Title 1", "Title 2", "Title 3"]);
+ expect(maxConcurrent).toBe(1);
+ const methods = FakeAcpClient.current?.requests.map(r => r.method);
+ expect(methods).toEqual(["initialize", "session/new", "session/set_model", "session/prompt", "session/prompt", "session/prompt"]);
+ const prompt = FakeAcpClient.current?.requests.find(r => r.method === "session/prompt")?.params as { prompt: { text: string }[] };
+ expect(prompt.prompt[0].text).toContain("earlier messages in this conversation do not apply");
+ });
+
+ it("replaces the title session when it is full, failed, or its process ended, closing it where the agent can", async () => {
+ const provider = new CopilotAgentProvider();
FakeAcpClient.initializeResult = { agentCapabilities: { sessionCapabilities: { close: {} } } };
- FakeAcpClient.newSessionId = "sess-title";
+ const newSessions = () => FakeAcpClient.current?.requests.filter(r => r.method === "session/new").length ?? 0;
+ const closed = () => FakeAcpClient.current?.requests.filter(r => r.method === "session/close").map(r => r.params);
+
+ // Full: the next title opens a new session and closes the old one.
+ for (let i = 0; i <= MAX_TITLES_PER_SESSION; i++) {
+ FakeAcpClient.newSessionId = `sess-title-${newSessions()}`;
+ await provider.generateTitle("hi");
+ }
+ expect(newSessions()).toBe(2);
+ expect(closed()).toEqual([{ sessionId: "sess-title-0" }]);
+
+ // Failed: the session is closed, and the next title opens another.
+ FakeAcpClient.promptScript = async () => { throw new Error("prompt failed"); };
+ expect(await provider.generateTitle("hi")).toBe("");
+ expect(closed()).toEqual([{ sessionId: "sess-title-0" }, { sessionId: "sess-title-1" }]);
+ FakeAcpClient.promptScript = async () => ({ stopReason: "end_turn" });
+ FakeAcpClient.newSessionId = "sess-title-2";
await provider.generateTitle("hi");
- expect(FakeAcpClient.current?.requests.at(-1)).toEqual({ method: "session/close", params: { sessionId: "sess-title" } });
+ expect(newSessions()).toBe(3);
- // A refused close leaves the title intact.
+ // A refused close is logged and changes nothing else.
FakeAcpClient.sessionCloseError = new Error("unknown session");
- FakeAcpClient.promptScript = async client => {
- client.update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Kept" } }, "sess-title");
- return { stopReason: "end_turn" };
- };
- expect(await provider.generateTitle("hi")).toBe("Kept");
+ FakeAcpClient.promptScript = async () => { throw new Error("prompt failed"); };
+ await provider.generateTitle("hi");
expect(infoLogMock).toHaveBeenCalledWith(expect.stringContaining("session/close failed (unknown session)"));
+
+ // Ended: the new process gets a session of its own, with no close sent to the old one.
+ FakeAcpClient.sessionCloseError = undefined;
+ FakeAcpClient.promptScript = async () => ({ stopReason: "end_turn" });
+ await provider.generateTitle("hi");
+ FakeAcpClient.current?.die();
+ await provider.generateTitle("hi");
+ expect(FakeAcpClient.current?.requests.map(r => r.method)).toEqual(["initialize", "session/new", "session/set_model", "session/prompt"]);
+ });
+
+ it("keeps using a session on an agent that cannot close one", async () => {
+ const provider = new CopilotAgentProvider();
+ FakeAcpClient.promptScript = async () => { throw new Error("prompt failed"); };
+ await provider.generateTitle("hi");
+ FakeAcpClient.promptScript = async () => ({ stopReason: "end_turn" });
+ for (let i = 0; i <= MAX_TITLES_PER_SESSION; i++) {
+ await provider.generateTitle("hi");
+ }
+ expect(FakeAcpClient.current?.requests.some(r => r.method === "session/close")).toBe(false);
});
it("still produces a title when the cheap model cannot be selected", async () => {