Skip to content

Commit e5cd68c

Browse files
committed
fix(composer): share explicit compaction guard across clients
1 parent 7c778e6 commit e5cd68c

4 files changed

Lines changed: 91 additions & 36 deletions

File tree

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ import {
8181
useComposerDraftSettings,
8282
} from "./use-composer-drafts";
8383
import {
84-
isCompactCommandText,
84+
isExplicitCompactionActive,
8585
resolveComposerDispatchMode,
8686
type ActiveTurnComposerAction,
8787
} from "@t3tools/client-runtime/state/composer-dispatch";
@@ -369,17 +369,15 @@ export function useThreadComposerState() {
369369
item.runId === activeRunId && item.type === "compaction" && item.status === "running",
370370
);
371371
const isCompacting = compactCommandIsActive || compactionRunning;
372-
// Only an explicit compaction refuses steering. Automatic compaction runs
373-
// inside an ordinary turn, which still accepts it.
374-
const explicitCompactionIsActive =
375-
compactCommandIsActive ||
376-
(compactionRunning &&
377-
selectedThreadVisibleTurnItems.some(
378-
({ item }) =>
379-
item.runId === activeRunId &&
380-
item.type === "user_message" &&
381-
isCompactCommandText(item.text),
382-
));
372+
const activeRunUserItem = selectedThreadVisibleTurnItems.find(
373+
({ item }) => item.type === "user_message" && item.runId === activeRunId,
374+
)?.item;
375+
const explicitCompactionIsActive = isExplicitCompactionActive({
376+
compactCommandIsActive,
377+
compactionRunning,
378+
activeRunUserMessageText:
379+
activeRunUserItem?.type === "user_message" ? activeRunUserItem.text : undefined,
380+
});
383381
const canSteerActiveTurn =
384382
queueWorkflow?.canPromoteToSteer === true && !explicitCompactionIsActive;
385383
// A stop or a restart holds the queue, completion wakes included, until it

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ import {
8686
workflowOpeningParentThreadId,
8787
} from "@t3tools/client-runtime/state/subagentRuntime";
8888
import { threadSupportsProviderHandoff } from "@t3tools/client-runtime/state/thread-workflows";
89-
import { isCompactCommandText } from "@t3tools/client-runtime/state/composer-dispatch";
89+
import { isExplicitCompactionActive } from "@t3tools/client-runtime/state/composer-dispatch";
9090
import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff";
9191
import { HandoffRow } from "./chat/V2LifecycleRow";
9292
import {
@@ -1367,14 +1367,15 @@ export default function ChatView(props: ChatViewProps) {
13671367
const isCompacting =
13681368
(isSendBusy || phase === "connecting" || phase === "running") &&
13691369
(compactionRunning || compactCommandIsActive);
1370-
// Only an explicit compaction refuses steering. Automatic compaction runs
1371-
// inside an ordinary turn, which still accepts it.
1372-
const explicitCompactionIsActive =
1373-
compactCommandIsActive ||
1374-
(compactionRunning &&
1375-
latestServerUserItem?.type === "user_message" &&
1376-
latestServerUserItem.runId === activeRuntime?.activeRunId &&
1377-
isCompactCommandText(latestServerUserItem.text));
1370+
const activeRunUserItem = serverVisibleTurnItems.find(
1371+
({ item }) => item.type === "user_message" && item.runId === activeRuntime?.activeRunId,
1372+
)?.item;
1373+
const explicitCompactionIsActive = isExplicitCompactionActive({
1374+
compactCommandIsActive,
1375+
compactionRunning,
1376+
activeRunUserMessageText:
1377+
activeRunUserItem?.type === "user_message" ? activeRunUserItem.text : undefined,
1378+
});
13781379
const isWorking =
13791380
phase === "running" ||
13801381
isSendBusy ||
Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,64 @@
11
import { expect, it } from "vite-plus/test";
22

3-
import { isCompactCommandText } from "./composerDispatch.ts";
3+
import { isExplicitCompactionActive, resolveComposerDispatchMode } from "./composerDispatch.ts";
44

5-
it("recognizes explicit compaction commands, with or without instructions", () => {
6-
expect(isCompactCommandText("/compact")).toBe(true);
7-
expect(isCompactCommandText(" /COMPACT ")).toBe(true);
8-
expect(isCompactCommandText("/compact keep the debugging notes")).toBe(true);
9-
expect(isCompactCommandText("/compactify")).toBe(false);
10-
expect(isCompactCommandText("please /compact")).toBe(false);
11-
expect(isCompactCommandText("Also end your reply with PINEAPPLE.")).toBe(false);
5+
it("allows steering during automatic compaction and preserves Queue and alternate dispatch", () => {
6+
const canSteer = !isExplicitCompactionActive({
7+
compactCommandIsActive: false,
8+
compactionRunning: true,
9+
activeRunUserMessageText: "Read the code",
10+
});
11+
expect(resolveComposerDispatchMode({ running: true, alternateModifier: false, canSteer })).toBe(
12+
"steer",
13+
);
14+
expect(resolveComposerDispatchMode({ running: true, alternateModifier: true, canSteer })).toBe(
15+
"queue",
16+
);
17+
expect(
18+
resolveComposerDispatchMode({
19+
running: true,
20+
alternateModifier: false,
21+
activeTurnDefault: "queue",
22+
canSteer,
23+
}),
24+
).toBe("queue");
25+
});
26+
27+
it("blocks steering only for a pending compact command or a running explicit compaction", () => {
28+
expect(
29+
isExplicitCompactionActive({
30+
compactCommandIsActive: true,
31+
compactionRunning: false,
32+
activeRunUserMessageText: undefined,
33+
}),
34+
).toBe(true);
35+
for (const text of ["/compact", " /COMPACT ", "/compact keep the debugging notes"]) {
36+
const canSteer = !isExplicitCompactionActive({
37+
compactCommandIsActive: false,
38+
compactionRunning: true,
39+
activeRunUserMessageText: text,
40+
});
41+
expect(resolveComposerDispatchMode({ running: true, alternateModifier: false, canSteer })).toBe(
42+
"queue",
43+
);
44+
expect(resolveComposerDispatchMode({ running: true, alternateModifier: true, canSteer })).toBe(
45+
"queue",
46+
);
47+
expect(
48+
isExplicitCompactionActive({
49+
compactCommandIsActive: false,
50+
compactionRunning: false,
51+
activeRunUserMessageText: text,
52+
}),
53+
).toBe(false);
54+
}
55+
for (const text of ["/compactify", "please /compact", undefined]) {
56+
expect(
57+
isExplicitCompactionActive({
58+
compactCommandIsActive: false,
59+
compactionRunning: true,
60+
activeRunUserMessageText: text,
61+
}),
62+
).toBe(false);
63+
}
1264
});

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ export function resolveComposerDispatchMode(input: {
1515
return defaultAction;
1616
}
1717

18-
/**
19-
* Whether a run's user message asks for an explicit compaction. Its running
20-
* compaction refuses steering, unlike automatic compaction inside an ordinary
21-
* turn. Pi and Claude also compact for `/compact <instructions>`.
22-
*/
23-
export function isCompactCommandText(text: string): boolean {
24-
return /^\/compact(?:\s|$)/i.test(text.trim());
18+
// Automatic compaction keeps accepting input; only explicit compaction blocks it.
19+
export function isExplicitCompactionActive(input: {
20+
readonly compactCommandIsActive: boolean;
21+
readonly compactionRunning: boolean;
22+
readonly activeRunUserMessageText: string | undefined;
23+
}): boolean {
24+
return (
25+
input.compactCommandIsActive ||
26+
(input.compactionRunning &&
27+
/^\/compact(?:\s|$)/i.test(input.activeRunUserMessageText?.trim() ?? ""))
28+
);
2529
}
2630

2731
export function alternateComposerDispatchAction(

0 commit comments

Comments
 (0)