Skip to content

Commit e3adff9

Browse files
committed
fix(chat): gate cli import resolver on ignore flag to stop history recursion
1 parent 50371f6 commit e3adff9

2 files changed

Lines changed: 112 additions & 5 deletions

File tree

‎src/gateway/server-methods/chat.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,11 +3208,16 @@ async function readChatHistoryPage(params: {
32083208
),
32093209
overreadContextMessage,
32103210
);
3211-
const cliHistory = resolveChatHistoryWithCliSessionImports({
3212-
entry,
3213-
provider,
3214-
localMessages: localMessagesWithBoundaryFilter,
3215-
});
3211+
// The ignore flag must gate this resolver too: the tail-window merge can report
3212+
// imported=true while the full merge below dedupes everything to imported=false,
3213+
// and an ungated re-resolve here would recurse through this branch forever.
3214+
const cliHistory = params.ignoreCliSessionImports
3215+
? { messages: localMessagesWithBoundaryFilter, imported: false as const }
3216+
: resolveChatHistoryWithCliSessionImports({
3217+
entry,
3218+
provider,
3219+
localMessages: localMessagesWithBoundaryFilter,
3220+
});
32163221
if (offset !== undefined && !cliHistory.imported) {
32173222
return readChatHistoryPage({ ...params, ignoreCliSessionImports: true });
32183223
}

‎src/gateway/server.chat.gateway-server-chat-b.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3699,6 +3699,108 @@ describe("gateway server chat", () => {
36993699
});
37003700
});
37013701

3702+
test("chat.history terminates when the full local read dedupes every claude-cli import", async () => {
3703+
await withGatewayChatHarness(async ({ ws, createSessionDir }) => {
3704+
await connectOk(ws);
3705+
const sessionDir = await createSessionDir();
3706+
const sessionId = "sess-claude-cli-dedupe-loop";
3707+
const homeEnvSnapshot = captureEnv(["HOME"]);
3708+
const homeDir = path.join(sessionDir, "home");
3709+
const cliSessionId = "0f5b202c-f6bb-4046-9475-d2f15fd07531";
3710+
const claudeProjectsDir = path.join(homeDir, ".claude", "projects", "workspace");
3711+
const dupBaseMs = Date.parse("2026-03-26T16:29:54.800Z");
3712+
await fs.mkdir(claudeProjectsDir, { recursive: true });
3713+
await fs.writeFile(
3714+
path.join(claudeProjectsDir, `${cliSessionId}.jsonl`),
3715+
[
3716+
JSON.stringify({
3717+
type: "user",
3718+
uuid: "dup-user-1",
3719+
timestamp: new Date(dupBaseMs).toISOString(),
3720+
message: { role: "user", content: "dup user question" },
3721+
}),
3722+
JSON.stringify({
3723+
type: "assistant",
3724+
uuid: "dup-assistant-1",
3725+
timestamp: new Date(dupBaseMs + 1000).toISOString(),
3726+
message: {
3727+
role: "assistant",
3728+
model: "claude-sonnet-4-6",
3729+
content: [{ type: "text", text: "dup assistant reply" }],
3730+
},
3731+
}),
3732+
].join("\n"),
3733+
"utf-8",
3734+
);
3735+
setTestEnvValue("HOME", homeDir);
3736+
try {
3737+
await writeSessionStore({
3738+
entries: {
3739+
main: {
3740+
sessionId,
3741+
sessionFile: testSessionFilePath(sessionDir, sessionId),
3742+
updatedAt: futureFixtureUpdatedAt(),
3743+
modelProvider: "claude-cli",
3744+
model: "claude-sonnet-4-6",
3745+
cliSessionBindings: {
3746+
"claude-cli": { sessionId: cliSessionId },
3747+
},
3748+
},
3749+
},
3750+
});
3751+
// The two import copies are the oldest local records; 45 newer
3752+
// local-only records push them past the limit-1 tail window (40 raw
3753+
// messages), so the tail merge incorporates the import while the full
3754+
// read dedupes everything. This layout used to recurse forever.
3755+
await writeMainSessionTranscript(
3756+
sessionDir,
3757+
[
3758+
JSON.stringify({
3759+
message: {
3760+
role: "user",
3761+
content: [{ type: "text", text: "dup user question" }],
3762+
timestamp: dupBaseMs,
3763+
},
3764+
}),
3765+
JSON.stringify({
3766+
message: {
3767+
role: "assistant",
3768+
content: [{ type: "text", text: "dup assistant reply" }],
3769+
timestamp: dupBaseMs + 1000,
3770+
},
3771+
}),
3772+
...Array.from({ length: 45 }, (_, index) =>
3773+
JSON.stringify({
3774+
message: {
3775+
role: index % 2 === 0 ? "user" : "assistant",
3776+
content: [{ type: "text", text: `local-only message ${index + 1}` }],
3777+
timestamp: dupBaseMs + 60_000 + index,
3778+
},
3779+
}),
3780+
),
3781+
],
3782+
sessionId,
3783+
);
3784+
3785+
const history = await rpcReq<{
3786+
messages?: unknown[];
3787+
hasMore?: boolean;
3788+
nextOffset?: number;
3789+
totalMessages?: number;
3790+
}>(ws, "chat.history", { sessionKey: "main", limit: 1 });
3791+
expect(history.ok).toBe(true);
3792+
expect(history.payload?.totalMessages).toBe(47);
3793+
expect(history.payload?.hasMore).toBe(true);
3794+
expect(history.payload?.nextOffset).toBeGreaterThan(0);
3795+
expect(JSON.stringify(history.payload?.messages?.at(-1))).toContain(
3796+
"local-only message 45",
3797+
);
3798+
} finally {
3799+
homeEnvSnapshot.restore();
3800+
}
3801+
});
3802+
});
3803+
37023804
test("chat.history overreads one local message to drop stale announce pairs at the limit boundary", async () => {
37033805
await withGatewayChatHarness(async ({ ws, createSessionDir }) => {
37043806
await connectOk(ws);

0 commit comments

Comments
 (0)