11import * as NodeServices from "@effect/platform-node/NodeServices" ;
2- import { DEFAULT_MODEL , ProjectId , ProviderInstanceId , ThreadId } from "@t3tools/contracts" ;
2+ import {
3+ DEFAULT_MODEL ,
4+ type OrchestrationCommand ,
5+ type OrchestrationSession ,
6+ type OrchestrationThread ,
7+ ProjectId ,
8+ ProviderDriverKind ,
9+ ProviderInstanceId ,
10+ ThreadId ,
11+ TurnId ,
12+ } from "@t3tools/contracts" ;
313import { assert , it } from "@effect/vitest" ;
414import * as Crypto from "effect/Crypto" ;
515import * as Deferred from "effect/Deferred" ;
@@ -11,11 +21,40 @@ import * as Ref from "effect/Ref";
1121import * as Stream from "effect/Stream" ;
1222
1323import * as ServerConfig from "./config.ts" ;
24+ import { OrchestrationCommandInvariantError } from "./orchestration/Errors.ts" ;
1425import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts" ;
1526import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts" ;
27+ import * as ProviderSessionDirectory from "./provider/Services/ProviderSessionDirectory.ts" ;
28+ import { ProviderSessionDirectoryPersistenceError } from "./provider/Errors.ts" ;
1629import * as AnalyticsService from "./telemetry/AnalyticsService.ts" ;
1730import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts" ;
1831
32+ const sessionUpdatedAt = "2026-01-01T00:00:00.000Z" ;
33+
34+ function sessionThread (
35+ threadId : ThreadId ,
36+ status : OrchestrationSession [ "status" ] ,
37+ activeTurnId : TurnId | null = null ,
38+ ) : Pick < OrchestrationThread , "id" | "session" | "deletedAt" > {
39+ return {
40+ id : threadId ,
41+ deletedAt : null ,
42+ session : {
43+ threadId,
44+ status,
45+ providerName : "codex" as const ,
46+ runtimeMode : "full-access" as const ,
47+ activeTurnId,
48+ lastError : null ,
49+ updatedAt : sessionUpdatedAt ,
50+ } ,
51+ } ;
52+ }
53+
54+ const queryWithThreads = (
55+ threads : ReadonlyArray < Pick < OrchestrationThread , "id" | "session" | "deletedAt" > > ,
56+ ) => ( { getCommandReadModel : ( ) => Effect . succeed ( { threads } ) } ) as never ;
57+
1958it ( "uses the canonical Codex default for auto-bootstrapped model selection" , ( ) => {
2059 assert . deepStrictEqual ( ServerRuntimeStartup . getAutoBootstrapDefaultModelSelection ( ) , {
2160 instanceId : ProviderInstanceId . make ( "codex" ) ,
@@ -70,6 +109,109 @@ it.effect("enqueueCommand fails queued work when readiness fails", () =>
70109 ) ,
71110) ;
72111
112+ it . effect ( "reconciles provider sessions inherited from the previous server" , ( ) => {
113+ const threadId = ThreadId . make ( "thread-startup-reconcile" ) ;
114+ const startingThreadId = ThreadId . make ( "thread-startup-starting" ) ;
115+ const resumeCursor = { schemaVersion : 1 , sessionId : "session-startup-reconcile" } ;
116+ const bindingFailure = new ProviderSessionDirectoryPersistenceError ( {
117+ operation : "ProviderSessionDirectory.getBinding" ,
118+ detail : "simulated invalid binding" ,
119+ } ) ;
120+ const bindingWriteFailure = new ProviderSessionDirectoryPersistenceError ( {
121+ operation : "ProviderSessionDirectory.upsert" ,
122+ detail : "simulated binding write failure" ,
123+ } ) ;
124+ const upserts : Array < ProviderSessionDirectory . ProviderRuntimeBinding > = [ ] ;
125+ const dispatches : Array < OrchestrationCommand > = [ ] ;
126+
127+ return ServerRuntimeStartup . reconcileProviderSessions . pipe (
128+ Effect . provideService (
129+ ProjectionSnapshotQuery . ProjectionSnapshotQuery ,
130+ queryWithThreads ( [
131+ {
132+ ...sessionThread ( threadId , "running" , TurnId . make ( "turn-startup-reconcile" ) ) ,
133+ deletedAt : "2026-01-02T00:00:00.000Z" ,
134+ } ,
135+ sessionThread ( startingThreadId , "starting" ) ,
136+ sessionThread ( ThreadId . make ( "thread-startup-ready" ) , "ready" ) ,
137+ ] ) ,
138+ ) ,
139+ Effect . provideService ( ProviderSessionDirectory . ProviderSessionDirectory , {
140+ getBinding : ( candidate : ThreadId ) =>
141+ candidate === threadId
142+ ? Effect . succeed (
143+ Option . some ( {
144+ threadId,
145+ provider : ProviderDriverKind . make ( "codex" ) ,
146+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
147+ status : "running" ,
148+ resumeCursor,
149+ } ) ,
150+ )
151+ : Effect . fail ( bindingFailure ) ,
152+ upsert : ( binding : ProviderSessionDirectory . ProviderRuntimeBinding ) =>
153+ Effect . sync ( ( ) => upserts . push ( binding ) ) . pipe (
154+ Effect . andThen ( Effect . fail ( bindingWriteFailure ) ) ,
155+ ) ,
156+ } satisfies Partial < ProviderSessionDirectory . ProviderSessionDirectory [ "Service" ] > as never ) ,
157+ Effect . provideService ( OrchestrationEngine . OrchestrationEngineService , {
158+ dispatch : ( command : OrchestrationCommand ) =>
159+ Effect . sync ( ( ) => dispatches . push ( command ) ) . pipe ( Effect . as ( { sequence : 1 } ) ) ,
160+ } satisfies Partial < OrchestrationEngine . OrchestrationEngineService [ "Service" ] > as never ) ,
161+ Effect . provide ( NodeServices . layer ) ,
162+ Effect . tap ( ( ) =>
163+ Effect . sync ( ( ) => {
164+ assert . equal ( upserts . length , 1 ) ;
165+ assert . equal ( upserts [ 0 ] ?. status , "stopped" ) ;
166+ assert . deepStrictEqual ( upserts [ 0 ] ?. resumeCursor , resumeCursor ) ;
167+ assert . deepStrictEqual ( upserts [ 0 ] ?. runtimePayload , { activeTurnId : null } ) ;
168+
169+ assert . equal ( dispatches . length , 2 ) ;
170+ const command = dispatches [ 0 ] ;
171+ assert . equal ( command ?. type , "thread.session.set" ) ;
172+ if ( command ?. type === "thread.session.set" ) {
173+ assert . equal ( command . threadId , threadId ) ;
174+ assert . equal ( command . session . status , "stopped" ) ;
175+ assert . equal ( command . session . activeTurnId , null ) ;
176+ assert . notEqual ( command . session . updatedAt , sessionUpdatedAt ) ;
177+ assert . equal ( command . session . updatedAt , command . createdAt ) ;
178+ }
179+ const startingCommand = dispatches [ 1 ] ;
180+ assert . equal ( startingCommand ?. type , "thread.session.set" ) ;
181+ if ( startingCommand ?. type === "thread.session.set" ) {
182+ assert . equal ( startingCommand . threadId , startingThreadId ) ;
183+ }
184+ } ) ,
185+ ) ,
186+ ) ;
187+ } ) ;
188+
189+ it . effect ( "fails startup reconciliation when a session cannot be settled" , ( ) => {
190+ const threadId = ThreadId . make ( "thread-startup-reconcile-failure" ) ;
191+ const failure = new OrchestrationCommandInvariantError ( {
192+ commandType : "thread.session.set" ,
193+ detail : "simulated reconciliation failure" ,
194+ } ) ;
195+
196+ return ServerRuntimeStartup . reconcileProviderSessions . pipe (
197+ Effect . provideService (
198+ ProjectionSnapshotQuery . ProjectionSnapshotQuery ,
199+ queryWithThreads ( [
200+ sessionThread ( threadId , "running" , TurnId . make ( "turn-startup-reconcile-failure" ) ) ,
201+ ] ) ,
202+ ) ,
203+ Effect . provideService ( ProviderSessionDirectory . ProviderSessionDirectory , {
204+ getBinding : ( ) => Effect . succeed ( Option . none ( ) ) ,
205+ } satisfies Partial < ProviderSessionDirectory . ProviderSessionDirectory [ "Service" ] > as never ) ,
206+ Effect . provideService ( OrchestrationEngine . OrchestrationEngineService , {
207+ dispatch : ( ) => Effect . fail ( failure ) ,
208+ } satisfies Partial < OrchestrationEngine . OrchestrationEngineService [ "Service" ] > as never ) ,
209+ Effect . provide ( NodeServices . layer ) ,
210+ Effect . flip ,
211+ Effect . tap ( ( error ) => Effect . sync ( ( ) => assert . equal ( error , failure ) ) ) ,
212+ ) ;
213+ } ) ;
214+
73215it . effect ( "launchStartupHeartbeat does not block the caller while counts are loading" , ( ) =>
74216 Effect . scoped (
75217 Effect . gen ( function * ( ) {
0 commit comments