1+ import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection" ;
2+ import { createEnvironmentThreadShellAtoms } from "@t3tools/client-runtime/state/threads" ;
3+ import {
4+ applyShellStreamEvent ,
5+ createEnvironmentSnapshotAtom ,
6+ type EnvironmentShellState ,
7+ type EnvironmentShellStatus ,
8+ } from "@t3tools/client-runtime/state/shell" ;
9+ import {
10+ v2ShellSnapshot ,
11+ v2ThreadShell ,
12+ } from "../../../../packages/client-runtime/src/state/orchestrationV2TestFixtures" ;
13+ import * as Option from "effect/Option" ;
14+ import { AsyncResult , Atom , AtomRegistry } from "effect/reactivity" ;
115import { afterEach , describe , expect , it } from "@effect/vitest" ;
216import {
317 CommandId ,
@@ -7,6 +21,9 @@ import {
721 ProjectId ,
822 ProviderInstanceId ,
923 ThreadId ,
24+ type OrchestrationV2ThreadShell ,
25+ type OrchestrationV2ShellSnapshot ,
26+ type ModelSelection ,
1027} from "@t3tools/contracts" ;
1128import { onTestFinished , vi } from "vite-plus/test" ;
1229
@@ -162,6 +179,7 @@ import {
162179 archiveCloudComposerDrafts ,
163180 clearComposerDraftContent ,
164181 clearComposerDraftContentState ,
182+ subscribeComposerDraftModelReconciliation ,
165183 clearComposerDraftsEnvironment ,
166184 ComposerDraftPersistenceError ,
167185 composerDraftsAtom ,
@@ -196,6 +214,7 @@ import {
196214 stickyComposerModelSelectionAtom ,
197215 undoComposerDraftMerge ,
198216 undoComposerDraftMergeState ,
217+ updateComposerDraftSettings ,
199218} from "./use-composer-drafts" ;
200219import { retainComposerAttachmentFileForPreview } from "../lib/composerAttachmentPreviewRetention" ;
201220
@@ -242,6 +261,98 @@ function contextDraft(start: number, count: number): ComposerDraft {
242261 } ;
243262}
244263
264+ function modelReconciliationHarness (
265+ environmentId : EnvironmentId ,
266+ threadId : ThreadId ,
267+ initialStatus : EnvironmentShellStatus = "live" ,
268+ ) {
269+ const tasks : Array < ( ) => void > = [ ] ;
270+ const registry = AtomRegistry . make ( {
271+ scheduleTask : ( task ) => {
272+ tasks . push ( task ) ;
273+ return ( ) => { } ;
274+ } ,
275+ } ) ;
276+ onTestFinished ( ( ) => registry . dispose ( ) ) ;
277+ const flush = ( ) => {
278+ for ( let task = tasks . shift ( ) ; task !== undefined ; task = tasks . shift ( ) ) task ( ) ;
279+ } ;
280+ const selectedThreadId = ThreadId . make ( "another-selected-thread" ) ;
281+ const initialSnapshot : OrchestrationV2ShellSnapshot = {
282+ ...v2ShellSnapshot ,
283+ threads : [
284+ { ...v2ThreadShell , id : threadId } ,
285+ { ...v2ThreadShell , id : selectedThreadId } ,
286+ ] ,
287+ } ;
288+ const shellStateAtom = Atom . make < EnvironmentShellState > ( {
289+ snapshot : Option . some ( initialSnapshot ) ,
290+ status : initialStatus ,
291+ error : Option . none ( ) ,
292+ } ) ;
293+ const shellResultAtom = Atom . map ( shellStateAtom , AsyncResult . success ) ;
294+ const snapshotAtom = createEnvironmentSnapshotAtom ( ( ) => shellResultAtom ) ( environmentId ) ;
295+ const catalogValueAtom = Atom . make ( {
296+ isReady : true ,
297+ entries : new Map ( [
298+ [
299+ environmentId ,
300+ {
301+ target : new PrimaryConnectionTarget ( {
302+ environmentId,
303+ label : "Environment" ,
304+ httpBaseUrl : "https://example.test" ,
305+ wsBaseUrl : "wss://example.test" ,
306+ } ) ,
307+ profile : Option . none ( ) ,
308+ enabled : true ,
309+ } ,
310+ ] ,
311+ ] ) ,
312+ } ) ;
313+ const shells = createEnvironmentThreadShellAtoms ( {
314+ catalogValueAtom,
315+ snapshotAtom : ( ) => snapshotAtom ,
316+ } ) ;
317+ const selectedShell = shells . threadShellAtom ( { environmentId, threadId : selectedThreadId } ) ;
318+ onTestFinished ( registry . mount ( selectedShell ) ) ;
319+ onTestFinished (
320+ subscribeComposerDraftModelReconciliation ( {
321+ registry,
322+ threadShellAtom : shells . threadShellAtom ,
323+ threadRefsAtom : shells . threadRefsAtom ,
324+ shellStateAtom : ( ) => shellResultAtom ,
325+ } ) ,
326+ ) ;
327+ flush ( ) ;
328+ return {
329+ flush,
330+ setStatus ( status : EnvironmentShellStatus ) {
331+ registry . set ( shellStateAtom , { ...registry . get ( shellStateAtom ) , status } ) ;
332+ flush ( ) ;
333+ } ,
334+ update ( changes : Partial < OrchestrationV2ThreadShell > , status ?: EnvironmentShellStatus ) {
335+ const state = registry . get ( shellStateAtom ) ;
336+ const snapshot = Option . getOrThrow ( state . snapshot ) ;
337+ registry . set ( shellStateAtom , {
338+ ...state ,
339+ status : status ?? state . status ,
340+ snapshot : Option . some (
341+ applyShellStreamEvent ( snapshot , {
342+ kind : "thread.updated" ,
343+ location : "active" ,
344+ sequence : snapshot . snapshotSequence + 1 ,
345+ thread : { ...snapshot . threads [ 0 ] ! , ...changes } ,
346+ } ) ,
347+ ) ,
348+ } ) ;
349+ flush ( ) ;
350+ expect ( registry . get ( selectedShell ) ?. id ) . toBe ( selectedThreadId ) ;
351+ } ,
352+ model : ( ) => registry . get ( shells . threadShellAtom ( { environmentId, threadId } ) ) ! . modelSelection ,
353+ } ;
354+ }
355+
245356describe ( "mobile composer drafts" , ( ) => {
246357 it ( "restores a PR checkout source after restarting the composer" , ( ) => {
247358 const draft : ComposerDraft = {
@@ -428,6 +539,114 @@ describe("mobile composer drafts", () => {
428539 expect ( draft . text ) . toContain ( String ( record ?. contextId ) ) ;
429540 } ) ;
430541
542+ it ( "retains a pick matching cached state until the live shell confirms it" , ( ) => {
543+ const environmentId = EnvironmentId . make ( "environment-1" ) ;
544+ const threadId = ThreadId . make ( "thread-1" ) ;
545+ const key = `${ environmentId } :${ threadId } ` ;
546+ const h = modelReconciliationHarness ( environmentId , threadId , "cached" ) ;
547+ const pick = h . model ( ) ;
548+ updateComposerDraftSettings ( key , { modelSelection : pick } ) ;
549+ h . flush ( ) ;
550+ const before = getComposerDraftSnapshot ( key ) ;
551+ expect ( before . modelSelection ) . toEqual ( pick ) ;
552+
553+ for ( const status of [ "empty" , "synchronizing" , "cached" ] as const ) {
554+ h . setStatus ( status ) ;
555+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( before ) ;
556+ }
557+ h . update ( { modelSelection : { ...pick , model : "remote-model" } } , "live" ) ;
558+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( before ) ;
559+ h . update ( { modelSelection : pick } , "synchronizing" ) ;
560+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( before ) ;
561+ h . setStatus ( "live" ) ;
562+ expect ( appAtomRegistry . get ( composerDraftsAtom ) [ key ] ) . toBeUndefined ( ) ;
563+ } ) ;
564+
565+ it ( "releases an inactive thread pick only after its queued model becomes durable" , ( ) => {
566+ const environmentId = EnvironmentId . make ( "environment-1" ) ;
567+ const threadId = ThreadId . make ( "thread-1" ) ;
568+ const key = `${ environmentId } :${ threadId } ` ;
569+ const pick = { instanceId : ProviderInstanceId . make ( "codex" ) , model : "gpt-5.5" } ;
570+ setComposerDraftText ( key , "keep this unsent follow-up" ) ;
571+ appendComposerDraftAttachments (
572+ key ,
573+ [
574+ {
575+ type : "file" ,
576+ id : "file" ,
577+ name : "notes.txt" ,
578+ mimeType : "text/plain" ,
579+ sizeBytes : 4 ,
580+ fileUri : "file:///notes.txt" ,
581+ } ,
582+ ] ,
583+ { appendReference : true } ,
584+ ) ;
585+ updateComposerDraftSettings ( key , {
586+ modelSelection : pick ,
587+ runtimeMode : "approval-required" ,
588+ interactionMode : "plan" ,
589+ } ) ;
590+ const before = getComposerDraftSnapshot ( key ) ;
591+ const remoteKey = `environment-remote:${ threadId } ` ;
592+ updateComposerDraftSettings ( remoteKey , { modelSelection : pick } ) ;
593+ const remoteBefore = getComposerDraftSnapshot ( remoteKey ) ;
594+ const h = modelReconciliationHarness ( environmentId , threadId ) ;
595+
596+ h . update ( { status : "queued" } ) ;
597+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( before ) ;
598+ h . update ( {
599+ status : "running" ,
600+ modelSelection : { ...pick , options : [ { id : "fastMode" , value : false } ] } ,
601+ } ) ;
602+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( { ...before , modelSelection : undefined } ) ;
603+ expect ( getComposerDraftSnapshot ( remoteKey ) ) . toEqual ( remoteBefore ) ;
604+
605+ h . update ( {
606+ modelSelection : {
607+ instanceId : ProviderInstanceId . make ( "claudeAgent" ) ,
608+ model : "claude-opus-5-5" ,
609+ } ,
610+ } ) ;
611+ expect ( getComposerDraftSnapshot ( key ) . modelSelection ?? h . model ( ) ) . toEqual ( h . model ( ) ) ;
612+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( { ...before , modelSelection : undefined } ) ;
613+ } ) ;
614+
615+ it ( "retains an unavailable provider pick through differing durable model updates" , ( ) => {
616+ const environmentId = EnvironmentId . make ( "environment-1" ) ;
617+ const threadId = ThreadId . make ( "thread-1" ) ;
618+ const key = `${ environmentId } :${ threadId } ` ;
619+ const pick : ModelSelection = {
620+ instanceId : ProviderInstanceId . make ( "codex-secondary" ) ,
621+ model : "gpt-5.5" ,
622+ options : [
623+ { id : "reasoningEffort" , value : "high" } ,
624+ { id : "fastMode" , value : true } ,
625+ ] ,
626+ } ;
627+ updateComposerDraftSettings ( key , { modelSelection : pick } ) ;
628+ const before = getComposerDraftSnapshot ( key ) ;
629+ const h = modelReconciliationHarness ( environmentId , threadId ) ;
630+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( before ) ;
631+ for ( const modelSelection of [
632+ { ...pick , instanceId : ProviderInstanceId . make ( "codex" ) } ,
633+ { ...pick , model : "gpt-5.4" } ,
634+ { ...pick , options : [ { id : "reasoningEffort" , value : "high" } ] } ,
635+ {
636+ ...pick ,
637+ options : [
638+ { id : "reasoningEffort" , value : "low" } ,
639+ { id : "fastMode" , value : true } ,
640+ ] ,
641+ } ,
642+ ] ) {
643+ h . update ( { modelSelection } ) ;
644+ expect ( getComposerDraftSnapshot ( key ) ) . toEqual ( before ) ;
645+ }
646+ h . update ( { modelSelection : { ...pick , options : pick . options ! . toReversed ( ) } } ) ;
647+ expect ( appAtomRegistry . get ( composerDraftsAtom ) [ key ] ) . toBeUndefined ( ) ;
648+ } ) ;
649+
431650 it ( "drops chips and records for attachments a replace no longer keeps" , ( ) => {
432651 const key = "new-task:draft-1" ;
433652 const kept = {
0 commit comments