Skip to content

Commit a6375ec

Browse files
praffiiiclaude
andcommitted
fix(mobile): compact-before-send uses the model picked at send time
The shared helper read the model from the last render, while the send enqueues the draft's current model. A model switched just before sending could decide compaction for the wrong provider. The send now passes its own selection, as it did before the helper existed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 26cde9a commit a6375ec

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

‎apps/mobile/src/state/thread-task-permissions.test.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const state = vi.hoisted(() => ({
3838
pendingRequests: { approvals: [], userInputs: [] } as PendingThreadRequests,
3939
enqueue: vi.fn(async () => undefined),
4040
freshPending: false,
41-
compactDue: false,
41+
compactModel: null as string | null,
4242
setFreshPending: vi.fn(),
4343
clearDraft: vi.fn(),
4444
approve: vi.fn(),
@@ -79,7 +79,8 @@ vi.mock("react", () => ({
7979
vi.mock("expo-crypto", () => ({ randomUUID: () => "uuid" }));
8080
vi.mock("@t3tools/client-runtime/state/context-window", async (importOriginal) => ({
8181
...(await importOriginal<typeof import("@t3tools/client-runtime/state/context-window")>()),
82-
compactBeforeSendTokens: () => (state.compactDue ? 248_000 : null),
82+
compactBeforeSendTokens: (input: { readonly selection: ModelSelection }) =>
83+
input.selection.model === state.compactModel ? 248_000 : null,
8384
}));
8485
vi.mock("../lib/clock", () => ({ useNowMinuteMs: () => 0 }));
8586
vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: vi.fn() }));
@@ -204,7 +205,7 @@ beforeEach(() => {
204205
runtimeMode: undefined,
205206
};
206207
state.freshPending = false;
207-
state.compactDue = false;
208+
state.compactModel = null;
208209
state.thread.latestRun = null;
209210
state.projection = null;
210211
state.serverConfig = {
@@ -315,13 +316,20 @@ describe("mobile task permissions", () => {
315316
visibleTurnItems: [],
316317
updatedAt: DateTime.makeUnsafe(0),
317318
} as unknown as OrchestrationV2ThreadProjection;
318-
state.compactDue = true;
319-
const composer = useThreadComposerState(NO_SIDE_CHATS);
320-
expect(composer.compactBeforeSend?.tokens).toBe(248_000);
321-
await composer.onSendMessage();
319+
state.compactModel = "gpt-5.5";
320+
const stale = useThreadComposerState(NO_SIDE_CHATS);
321+
expect(stale.compactBeforeSend).toBeNull();
322+
// A model picked after the last render still decides the send.
323+
state.draft = {
324+
...state.draft,
325+
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.5" },
326+
};
327+
await stale.onSendMessage();
322328
expect(state.enqueue).toHaveBeenLastCalledWith(
323329
expect.objectContaining({ compactBeforeSend: true }),
324330
);
331+
const composer = useThreadComposerState(NO_SIDE_CHATS);
332+
expect(composer.compactBeforeSend?.tokens).toBe(248_000);
325333
composer.compactBeforeSend?.keepFullHistory();
326334
const kept = useThreadComposerState(NO_SIDE_CHATS);
327335
expect(kept.compactBeforeSend).toBeNull();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,15 @@ export function useThreadComposerState(input: {
400400
: null;
401401

402402
const activeThreadBusy = threadRuntimeIsActive(selectedThreadRuntime);
403-
const resumeCompactionTokens = (text: string, now: string) =>
403+
const resumeCompactionTokens = (selection: ModelSelection | null, text: string, now: string) =>
404404
selectedEnvironmentRuntime?.connectionState === "connected" &&
405405
selectedThreadProjection !== null &&
406406
serverConfig != null &&
407-
modelSelection !== null
407+
selection !== null
408408
? compactBeforeSendTokens({
409409
projection: selectedThreadProjection.projection,
410410
visibleTurnItems: selectedThreadVisibleTurnItems,
411-
selection: modelSelection,
411+
selection,
412412
providers: serverConfig.providers,
413413
settings: serverConfig.settings,
414414
text,
@@ -424,7 +424,7 @@ export function useThreadComposerState(input: {
424424
const compactTokens =
425425
selectedDraft.freshContext === true
426426
? null
427-
: resumeCompactionTokens("", new Date(nowMinuteMs).toISOString());
427+
: resumeCompactionTokens(modelSelection, "", new Date(nowMinuteMs).toISOString());
428428
const fullHistoryThreadKeys = useAtomValue(fullHistoryThreadKeysAtom);
429429
const keepFullHistory =
430430
selectedThreadKey !== null && fullHistoryThreadKeys[selectedThreadKey] === true;
@@ -766,7 +766,7 @@ export function useThreadComposerState(input: {
766766
!freshContext &&
767767
followUpDispatchMode === null &&
768768
appAtomRegistry.get(fullHistoryThreadKeysAtom)[threadKey] !== true &&
769-
resumeCompactionTokens(text, metadata.createdAt) !== null;
769+
resumeCompactionTokens(modelSelection, text, metadata.createdAt) !== null;
770770
const messageId = MessageId.make(metadata.messageId);
771771
onWillEnqueue?.();
772772
clearThreadComposerError(threadKey);

0 commit comments

Comments
 (0)