Skip to content

Commit a5d900d

Browse files
committed
fix(composer): retain manual compaction guard across history paging
1 parent e5cd68c commit a5d900d

4 files changed

Lines changed: 34 additions & 21 deletions

File tree

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,11 @@ export function useThreadComposerState() {
369369
item.runId === activeRunId && item.type === "compaction" && item.status === "running",
370370
);
371371
const isCompacting = compactCommandIsActive || compactionRunning;
372-
const activeRunUserItem = selectedThreadVisibleTurnItems.find(
373-
({ item }) => item.type === "user_message" && item.runId === activeRunId,
374-
)?.item;
375372
const explicitCompactionIsActive = isExplicitCompactionActive({
376373
compactCommandIsActive,
377374
compactionRunning,
378-
activeRunUserMessageText:
379-
activeRunUserItem?.type === "user_message" ? activeRunUserItem.text : undefined,
375+
run: selectedThreadProjection?.projection.runs.find((run) => run.id === activeRunId),
376+
messages: selectedThreadMessages,
380377
});
381378
const canSteerActiveTurn =
382379
queueWorkflow?.canPromoteToSteer === true && !explicitCompactionIsActive;

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,14 +1367,11 @@ export default function ChatView(props: ChatViewProps) {
13671367
const isCompacting =
13681368
(isSendBusy || phase === "connecting" || phase === "running") &&
13691369
(compactionRunning || compactCommandIsActive);
1370-
const activeRunUserItem = serverVisibleTurnItems.find(
1371-
({ item }) => item.type === "user_message" && item.runId === activeRuntime?.activeRunId,
1372-
)?.item;
13731370
const explicitCompactionIsActive = isExplicitCompactionActive({
13741371
compactCommandIsActive,
13751372
compactionRunning,
1376-
activeRunUserMessageText:
1377-
activeRunUserItem?.type === "user_message" ? activeRunUserItem.text : undefined,
1373+
run: serverProjection?.runs.find((run) => run.id === activeRuntime?.activeRunId),
1374+
messages: serverProjection?.messages,
13781375
});
13791376
const isWorking =
13801377
phase === "running" ||

‎packages/client-runtime/src/state/composerDispatch.test.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ import { expect, it } from "vite-plus/test";
22

33
import { isExplicitCompactionActive, resolveComposerDispatchMode } from "./composerDispatch.ts";
44

5+
it("uses the run's retained message when its initiating timeline item is paged out", () => {
6+
expect(
7+
isExplicitCompactionActive({
8+
compactCommandIsActive: false,
9+
compactionRunning: true,
10+
run: { userMessageId: "command" },
11+
messages: [
12+
{ id: "command", text: "/compact keep the debugging notes" },
13+
{ id: "steer", text: "A later follow-up" },
14+
],
15+
}),
16+
).toBe(true);
17+
});
18+
519
it("allows steering during automatic compaction and preserves Queue and alternate dispatch", () => {
620
const canSteer = !isExplicitCompactionActive({
721
compactCommandIsActive: false,
822
compactionRunning: true,
9-
activeRunUserMessageText: "Read the code",
23+
run: { userMessageId: "user" },
24+
messages: [{ id: "user", text: "Read the code" }],
1025
});
1126
expect(resolveComposerDispatchMode({ running: true, alternateModifier: false, canSteer })).toBe(
1227
"steer",
@@ -29,14 +44,16 @@ it("blocks steering only for a pending compact command or a running explicit com
2944
isExplicitCompactionActive({
3045
compactCommandIsActive: true,
3146
compactionRunning: false,
32-
activeRunUserMessageText: undefined,
47+
run: undefined,
48+
messages: undefined,
3349
}),
3450
).toBe(true);
3551
for (const text of ["/compact", " /COMPACT ", "/compact keep the debugging notes"]) {
3652
const canSteer = !isExplicitCompactionActive({
3753
compactCommandIsActive: false,
3854
compactionRunning: true,
39-
activeRunUserMessageText: text,
55+
run: { userMessageId: "user" },
56+
messages: text === undefined ? [] : [{ id: "user", text }],
4057
});
4158
expect(resolveComposerDispatchMode({ running: true, alternateModifier: false, canSteer })).toBe(
4259
"queue",
@@ -48,7 +65,8 @@ it("blocks steering only for a pending compact command or a running explicit com
4865
isExplicitCompactionActive({
4966
compactCommandIsActive: false,
5067
compactionRunning: false,
51-
activeRunUserMessageText: text,
68+
run: { userMessageId: "user" },
69+
messages: text === undefined ? [] : [{ id: "user", text }],
5270
}),
5371
).toBe(false);
5472
}
@@ -57,7 +75,8 @@ it("blocks steering only for a pending compact command or a running explicit com
5775
isExplicitCompactionActive({
5876
compactCommandIsActive: false,
5977
compactionRunning: true,
60-
activeRunUserMessageText: text,
78+
run: { userMessageId: "user" },
79+
messages: text === undefined ? [] : [{ id: "user", text }],
6180
}),
6281
).toBe(false);
6382
}

‎packages/client-runtime/src/state/composerDispatch.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export function resolveComposerDispatchMode(input: {
1919
export function isExplicitCompactionActive(input: {
2020
readonly compactCommandIsActive: boolean;
2121
readonly compactionRunning: boolean;
22-
readonly activeRunUserMessageText: string | undefined;
22+
readonly run: { readonly userMessageId: string } | undefined;
23+
readonly messages: ReadonlyArray<{ readonly id: string; readonly text: string }> | undefined;
2324
}): boolean {
24-
return (
25-
input.compactCommandIsActive ||
26-
(input.compactionRunning &&
27-
/^\/compact(?:\s|$)/i.test(input.activeRunUserMessageText?.trim() ?? ""))
28-
);
25+
if (input.compactCommandIsActive) return true;
26+
if (!input.compactionRunning || input.run === undefined) return false;
27+
const message = input.messages?.find((message) => message.id === input.run?.userMessageId);
28+
return /^\/compact(?:\s|$)/i.test(message?.text.trim() ?? "");
2929
}
3030

3131
export function alternateComposerDispatchAction(

0 commit comments

Comments
 (0)