1+ // @effect -diagnostics nodeBuiltinImport:off
2+ import * as NodeFSP from "node:fs/promises" ;
3+ import * as NodeOS from "node:os" ;
4+ import * as NodePath from "node:path" ;
5+
16import {
27 ApprovalRequestId ,
38 EventId ,
@@ -27,7 +32,10 @@ import { PersistenceSqlError } from "../../persistence/Errors.ts";
2732import { OrchestrationCommandReceiptRepositoryLive } from "../../persistence/Layers/OrchestrationCommandReceipts.ts" ;
2833import * as OrchestrationCommandReceipts from "../../persistence/Services/OrchestrationCommandReceipts.ts" ;
2934import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts" ;
30- import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts" ;
35+ import {
36+ makeSqlitePersistenceLive ,
37+ SqlitePersistenceMemory ,
38+ } from "../../persistence/Layers/Sqlite.ts" ;
3139import {
3240 OrchestrationEventStore ,
3341 type OrchestrationEventStoreShape ,
@@ -51,7 +59,10 @@ const asMessageId = (value: string): MessageId => MessageId.make(value);
5159const asTurnId = ( value : string ) : TurnId => TurnId . make ( value ) ;
5260const asCheckpointRef = ( value : string ) : CheckpointRef => CheckpointRef . make ( value ) ;
5361
54- function makeOrchestrationLayer ( ) {
62+ function makeOrchestrationLayer ( databasePath ?: string ) {
63+ const persistence = databasePath
64+ ? makeSqlitePersistenceLive ( databasePath )
65+ : SqlitePersistenceMemory ;
5566 const ServerConfigLayer = ServerConfig . layerTest ( process . cwd ( ) , {
5667 prefix : "t3-orchestration-engine-test-" ,
5768 } ) ;
@@ -67,19 +78,21 @@ function makeOrchestrationLayer() {
6778 Layer . provide ( OrchestrationEventStoreLive ) ,
6879 Layer . provideMerge ( OrchestrationCommandReceiptRepositoryLive ) ,
6980 Layer . provide ( RepositoryIdentityResolver . layer ) ,
70- Layer . provide ( SqlitePersistenceMemory ) ,
81+ Layer . provide ( persistence ) ,
7182 Layer . provideMerge ( ServerConfigLayer ) ,
7283 Layer . provideMerge ( NodeServices . layer ) ,
7384 ) ;
7485}
7586
76- async function createOrchestrationSystem ( ) {
77- const runtime = ManagedRuntime . make ( makeOrchestrationLayer ( ) ) ;
87+ async function createOrchestrationSystem ( databasePath ?: string ) {
88+ const runtime = ManagedRuntime . make ( makeOrchestrationLayer ( databasePath ) ) ;
7889 const engine = await runtime . runPromise ( Effect . service ( OrchestrationEngineService ) ) ;
7990 const snapshotQuery = await runtime . runPromise ( Effect . service ( ProjectionSnapshotQuery ) ) ;
8091 return {
8192 engine,
8293 readModel : ( ) => runtime . runPromise ( snapshotQuery . getSnapshot ( ) ) ,
94+ readThread : ( threadId : ThreadId ) =>
95+ runtime . runPromise ( snapshotQuery . getThreadDetailById ( threadId ) ) ,
8396 run : < A , E > ( effect : Effect . Effect < A , E > ) => runtime . runPromise ( effect ) ,
8497 dispose : ( ) => runtime . dispose ( ) ,
8598 } ;
@@ -102,9 +115,13 @@ const hasMetricSnapshot = (
102115
103116describe ( "OrchestrationEngine" , ( ) => {
104117 it . each ( [ "running" , "stopped" ] as const ) (
105- "sends async answers as messages with a %s session and rejects duplicate replies" ,
118+ "sends async answers with a %s session and rejects old duplicate replies" ,
106119 async ( status ) => {
107- const system = await createOrchestrationSystem ( ) ;
120+ const directory = await NodeFSP . mkdtemp (
121+ NodePath . join ( NodeOS . tmpdir ( ) , "t3-async-questions-" ) ,
122+ ) ;
123+ const databasePath = NodePath . join ( directory , "state.sqlite" ) ;
124+ let system = await createOrchestrationSystem ( databasePath ) ;
108125 const threadId = ThreadId . make ( "async-thread" ) ;
109126 const projectId = ProjectId . make ( "async-project" ) ;
110127 const requestId = ApprovalRequestId . make ( "codex-async:question-1" ) ;
@@ -185,30 +202,36 @@ describe("OrchestrationEngine", () => {
185202 } ,
186203 } ) ,
187204 ) ;
188- // The question must stay available when later work fills the activity window.
189- for ( let index = 0 ; index < 501 ; index += 1 ) {
190- await system . run (
191- system . engine . dispatch ( {
192- type : "thread.activity.append" ,
193- commandId : CommandId . make ( `work-${ index } ` ) ,
194- threadId,
195- createdAt : "2026-01-01T00:00:01.000Z" ,
196- activity : {
197- id : EventId . make ( `work-${ index } ` ) ,
198- kind : "tool.completed" ,
199- summary : "Work continued" ,
200- payload : { } ,
201- tone : "info" ,
202- turnId : TurnId . make ( "turn-1" ) ,
203- createdAt : "2026-01-01T00:00:01.000Z" ,
204- } ,
205- } ) ,
206- ) ;
207- }
205+ const appendWork = async ( prefix : string , createdAt : string ) => {
206+ for ( let index = 0 ; index < 501 ; index += 1 ) {
207+ await system . run (
208+ system . engine . dispatch ( {
209+ type : "thread.activity.append" ,
210+ commandId : CommandId . make ( `${ prefix } -${ index } ` ) ,
211+ threadId,
212+ createdAt,
213+ activity : {
214+ id : EventId . make ( `${ prefix } -${ index } ` ) ,
215+ kind : "tool.completed" ,
216+ summary : "Work continued" ,
217+ payload : { } ,
218+ tone : "info" ,
219+ turnId : TurnId . make ( "turn-1" ) ,
220+ createdAt,
221+ } ,
222+ } ) ,
223+ ) ;
224+ }
225+ } ;
226+ await appendWork ( "work" , "2026-01-01T00:00:01.000Z" ) ;
208227 const before = await system . readModel ( ) ;
209228 expect (
210229 before . threads [ 0 ] ?. activities . some ( ( activity ) => activity . id === "async-question" ) ,
211230 ) . toBe ( true ) ;
231+ if ( status === "stopped" ) {
232+ await system . dispose ( ) ;
233+ system = await createOrchestrationSystem ( databasePath ) ;
234+ }
212235 const response = {
213236 type : "thread.user-input.respond" as const ,
214237 commandId : CommandId . make ( "async-response" ) ,
@@ -257,8 +280,26 @@ describe("OrchestrationEngine", () => {
257280 } ) ,
258281 ) ,
259282 ) . rejects . toThrow ( "This question has already been answered." ) ;
283+ await appendWork ( "later-work" , "2026-01-01T00:00:03.000Z" ) ;
284+ const afterEviction = Option . getOrThrow ( await system . readThread ( threadId ) ) ;
285+ expect (
286+ afterEviction . activities . some ( ( activity ) => activity . kind === "user-input.resolved" ) ,
287+ ) . toBe ( false ) ;
288+ if ( status === "stopped" ) {
289+ await system . dispose ( ) ;
290+ system = await createOrchestrationSystem ( databasePath ) ;
291+ }
292+ await expect (
293+ system . run (
294+ system . engine . dispatch ( {
295+ ...response ,
296+ commandId : CommandId . make ( "reply-after-eviction" ) ,
297+ } ) ,
298+ ) ,
299+ ) . rejects . toThrow ( "This question has already been answered." ) ;
260300 } finally {
261301 await system . dispose ( ) ;
302+ await NodeFSP . rm ( directory , { recursive : true , force : true } ) ;
262303 }
263304 } ,
264305 ) ;
@@ -347,6 +388,7 @@ describe("OrchestrationEngine", () => {
347388 const layer = OrchestrationEngineLive . pipe (
348389 Layer . provide (
349390 Layer . succeed ( ProjectionSnapshotQuery , {
391+ getUserInputActivity : ( ) => Effect . die ( "unused" ) ,
350392 getCommandReadModel : ( ) => Effect . succeed ( commandReadModel ) ,
351393 getSnapshot : ( ) =>
352394 Effect . sync ( ( ) => {
0 commit comments