@@ -3,7 +3,7 @@ import * as Dialog from "../components/ui/Dialog";
33import { AuthOrchestrationOperateScope } from "@t3tools/contracts" ;
44import { readEnvironmentScope } from "./session" ;
55import { useAtomValue } from "@effect/atom-react" ;
6- import { shouldCompactBeforeSend } from "@t3tools/client-runtime/state/context-window" ;
6+ import { compactBeforeSendTokens } from "@t3tools/client-runtime/state/context-window" ;
77import { threadRuntimeIsActive } from "@t3tools/client-runtime/state/shell" ;
88import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints" ;
99import { threadSteersDuringCompaction } from "@t3tools/client-runtime/state/thread-workflows" ;
@@ -42,6 +42,7 @@ import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLega
4242import { restoreAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences" ;
4343import { composerContextSendBlockReason , reidentifyComposerContext } from "../lib/composerContext" ;
4444import { uuidv4 } from "../lib/uuid" ;
45+ import { useNowMinuteMs } from "../lib/clock" ;
4546
4647import { makeQueuedMessageMetadata } from "../lib/commandMetadata" ;
4748import {
@@ -120,6 +121,17 @@ import { useAtomCommand } from "./use-atom-command";
120121const EMPTY_QUEUE_WORKFLOW_ATOM = Atom . make < null > ( null ) . pipe (
121122 Atom . withLabel ( "mobile-thread-queue-workflow:empty" ) ,
122123) ;
124+ // Threads whose next send keeps the full history instead of compacting first.
125+ const fullHistoryThreadKeysAtom = Atom . make < Readonly < Record < string , true > > > ( { } ) . pipe (
126+ Atom . keepAlive ,
127+ ) ;
128+
129+ function setKeepFullHistory ( threadKey : string , keep : boolean ) {
130+ const { [ threadKey ] : kept , ...rest } = appAtomRegistry . get ( fullHistoryThreadKeysAtom ) ;
131+ if ( ( kept === true ) === keep ) return ;
132+ appAtomRegistry . set ( fullHistoryThreadKeysAtom , keep ? { ...rest , [ threadKey ] : true } : rest ) ;
133+ }
134+
123135const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom . make < ReadonlyArray < ThreadCheckpointSummary > > ( [ ] ) ;
124136const EMPTY_THREAD_SEQUENCE_ATOM = Atom . make ( 0 ) ;
125137
@@ -398,6 +410,34 @@ export function useThreadComposerState(input: {
398410 : null ;
399411
400412 const activeThreadBusy = threadRuntimeIsActive ( selectedThreadRuntime ) ;
413+ const resumeCompactionTokens = ( selection : ModelSelection | null , text : string , now : string ) =>
414+ selectedEnvironmentRuntime ?. connectionState === "connected" &&
415+ selectedThreadProjection !== null &&
416+ serverConfig != null &&
417+ selection !== null
418+ ? compactBeforeSendTokens ( {
419+ projection : selectedThreadProjection . projection ,
420+ visibleTurnItems : selectedThreadVisibleTurnItems ,
421+ selection,
422+ providers : serverConfig . providers ,
423+ settings : serverConfig . settings ,
424+ text,
425+ threadIdle :
426+ ! activeThreadBusy &&
427+ ! runlessWorkActive &&
428+ ! isCompacting &&
429+ selectedThreadQueuedMessages . length === 0 ,
430+ now,
431+ } )
432+ : null ;
433+ const nowMinuteMs = useNowMinuteMs ( ) ;
434+ const compactTokens =
435+ selectedDraft . freshContext === true
436+ ? null
437+ : resumeCompactionTokens ( modelSelection , "" , new Date ( nowMinuteMs ) . toISOString ( ) ) ;
438+ const fullHistoryThreadKeys = useAtomValue ( fullHistoryThreadKeysAtom ) ;
439+ const keepFullHistory =
440+ selectedThreadKey !== null && fullHistoryThreadKeys [ selectedThreadKey ] === true ;
401441 const interruptibleRunId = threadRuntimeHasInterruptibleRun ( selectedThreadRuntime )
402442 ? ( selectedThreadRuntime ?. activeRunId ?? null )
403443 : null ;
@@ -731,27 +771,14 @@ export function useThreadComposerState(input: {
731771 // A redirect belongs to the approval it answers; the armed session waits for the next message.
732772 const freshContext = beforeDispatch === undefined && isFreshSessionPending ( threadKey ) ;
733773 const metadata = makeQueuedMessageMetadata ( ) ;
734- // A fresh session drops the context a compaction would summarize.
774+ // A fresh session drops the context a compaction would summarize. Only a send the notice
775+ // announced compacts, since the notice's minute clock can trail the exact deadline.
735776 const compactBeforeSend =
777+ compactTokens !== null &&
736778 ! freshContext &&
737779 followUpDispatchMode === null &&
738- selectedEnvironmentRuntime ?. connectionState === "connected" &&
739- selectedThreadProjection !== null &&
740- serverConfig != null &&
741- shouldCompactBeforeSend ( {
742- projection : selectedThreadProjection . projection ,
743- visibleTurnItems : selectedThreadVisibleTurnItems ,
744- selection : modelSelection ,
745- providers : serverConfig . providers ,
746- settings : serverConfig . settings ,
747- text,
748- threadIdle :
749- ! activeThreadBusy &&
750- ! runlessWorkActive &&
751- ! isCompacting &&
752- selectedThreadQueuedMessages . length === 0 ,
753- now : metadata . createdAt ,
754- } ) ;
780+ appAtomRegistry . get ( fullHistoryThreadKeysAtom ) [ threadKey ] !== true &&
781+ resumeCompactionTokens ( modelSelection , text , metadata . createdAt ) !== null ;
755782 const messageId = MessageId . make ( metadata . messageId ) ;
756783 onWillEnqueue ?.( ) ;
757784 clearThreadComposerError ( threadKey ) ;
@@ -777,7 +804,10 @@ export function useThreadComposerState(input: {
777804 clearComposerDraftContent ( threadKey , { deferAttachmentCleanup : true } ) ;
778805 if ( freshContext ) setFreshSessionPending ( threadKey , false ) ;
779806 enqueuePromise . then (
780- ( ) => scheduleUnusedComposerAttachmentCleanup ( attachments ) ,
807+ ( ) => {
808+ setKeepFullHistory ( threadKey , false ) ;
809+ scheduleUnusedComposerAttachmentCleanup ( attachments ) ;
810+ } ,
781811 ( error : unknown ) => {
782812 // Restore with uncapped append; capped merge could drop attachments added during the failed write.
783813 void mergeComposerDraftContent ( threadKey , {
@@ -861,6 +891,13 @@ export function useThreadComposerState(input: {
861891 ) ,
862892 providerSubagentStatus,
863893 isCompacting,
894+ compactBeforeSend :
895+ compactTokens === null || selectedThreadKey === null || keepFullHistory
896+ ? null
897+ : {
898+ tokens : compactTokens ,
899+ keepFullHistory : ( ) => setKeepFullHistory ( selectedThreadKey , true ) ,
900+ } ,
864901 composerDraftKey,
865902 followUpBehavior,
866903 canSteerActiveTurn,
0 commit comments