Skip to content

Commit 6323c6a

Browse files
committed
fix: tighten acp session list filters
1 parent 089edb7 commit 6323c6a

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎src/acp/translator.lifecycle.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,72 @@ describe("acp translator stable lifecycle handlers", () => {
206206
sessionStore.clearAllSessionsForTest();
207207
});
208208

209+
it("does not include sessions without workspace metadata in cwd-filtered lists", async () => {
210+
const allRows = [
211+
createSessionRow({ key: "agent:main:unknown", title: "Unknown workspace" }),
212+
createSessionRow({ key: "agent:main:a1", cwd: "/work/a", title: "A1" }),
213+
createSessionRow({ key: "agent:main:b1", cwd: "/work/b", title: "B1" }),
214+
];
215+
const request = vi.fn(async (method: string) => {
216+
if (method === "sessions.list") {
217+
return createGatewaySessions(allRows);
218+
}
219+
return { ok: true };
220+
}) as GatewayClient["request"];
221+
const sessionStore = createInMemorySessionStore();
222+
const agent = new AcpGatewayAgent(createAcpConnection(), createAcpGateway(request), {
223+
sessionStore,
224+
});
225+
226+
const result = await agent.listSessions(createListSessionsRequest({ cwd: "/work/a" }));
227+
228+
expect(result.sessions.map((session) => session.sessionId)).toEqual(["agent:main:a1"]);
229+
expect(result.sessions.every((session) => session.cwd === "/work/a")).toBe(true);
230+
231+
sessionStore.clearAllSessionsForTest();
232+
});
233+
234+
it("rejects session/list cursors when the cwd filter changes", async () => {
235+
const allRows = [
236+
createSessionRow({ key: "agent:main:a1", cwd: "/work/a", title: "A1" }),
237+
createSessionRow({ key: "agent:main:a2", cwd: "/work/a", title: "A2" }),
238+
createSessionRow({ key: "agent:main:b1", cwd: "/work/b", title: "B1" }),
239+
];
240+
const request = vi.fn(async (method: string, params?: { limit?: number }) => {
241+
if (method === "sessions.list") {
242+
const limit = params?.limit ?? allRows.length;
243+
return {
244+
...createGatewaySessions(allRows.slice(0, limit)),
245+
totalCount: allRows.length,
246+
hasMore: limit < allRows.length,
247+
};
248+
}
249+
return { ok: true };
250+
}) as GatewayClient["request"];
251+
const sessionStore = createInMemorySessionStore();
252+
const agent = new AcpGatewayAgent(createAcpConnection(), createAcpGateway(request), {
253+
sessionStore,
254+
});
255+
256+
const unfiltered = await agent.listSessions(createListSessionsRequest({ limit: 1 }));
257+
expect(unfiltered.nextCursor).toEqual(expect.any(String));
258+
await expect(
259+
agent.listSessions(
260+
createListSessionsRequest({ cwd: "/work/a", cursor: unfiltered.nextCursor }),
261+
),
262+
).rejects.toThrow(/cursor does not match the cwd filter/i);
263+
264+
const filtered = await agent.listSessions(
265+
createListSessionsRequest({ cwd: "/work/a", limit: 1 }),
266+
);
267+
expect(filtered.nextCursor).toEqual(expect.any(String));
268+
await expect(
269+
agent.listSessions(createListSessionsRequest({ cursor: filtered.nextCursor })),
270+
).rejects.toThrow(/cursor does not match the cwd filter/i);
271+
272+
sessionStore.clearAllSessionsForTest();
273+
});
274+
209275
it("rejects relative cwd filters for session/list", async () => {
210276
const sessionStore = createInMemorySessionStore();
211277
const agent = new AcpGatewayAgent(createAcpConnection(), createAcpGateway(), {

‎src/acp/translator.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,9 @@ export class AcpGatewayAgent implements Agent {
679679
assertAbsoluteCwd(requestedCwd, "session/list");
680680
}
681681
const fallbackCwd = requestedCwd ?? process.cwd();
682-
const cursor = decodeListSessionsCursor(params.cursor);
683-
if (cursor.cwd && cursor.cwd !== requestedCwd) {
682+
const rawCursor = normalizeOptionalString(params.cursor);
683+
const cursor = decodeListSessionsCursor(rawCursor);
684+
if (rawCursor && cursor.cwd !== requestedCwd) {
684685
throw new Error("ACP session list cursor does not match the cwd filter.");
685686
}
686687

@@ -696,8 +697,13 @@ export class AcpGatewayAgent implements Agent {
696697
includeDerivedTitles: true,
697698
});
698699
rows = result.sessions
699-
.map((session) => this.mapGatewaySessionToAcpSessionInfo(session, fallbackCwd))
700-
.filter((session) => !requestedCwd || session.cwd === requestedCwd);
700+
.filter((session) => {
701+
if (!requestedCwd) {
702+
return true;
703+
}
704+
return normalizeOptionalString(session.spawnedWorkspaceDir) === requestedCwd;
705+
})
706+
.map((session) => this.mapGatewaySessionToAcpSessionInfo(session, fallbackCwd));
701707
if (
702708
rows.length > end ||
703709
result.hasMore !== true ||

0 commit comments

Comments
 (0)