@@ -23,6 +23,7 @@ import * as Layer from "effect/Layer";
2323import * as SqlClient from "effect/unstable/sql/SqlClient" ;
2424import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts" ;
2525import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts" ;
26+ import * as EffectOutbox from "./EffectOutbox.ts" ;
2627import * as Orchestrator from "./Orchestrator.ts" ;
2728import * as ProjectionStore from "./ProjectionStore.ts" ;
2829import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts" ;
@@ -42,13 +43,94 @@ const database = SqlitePersistenceMemory;
4243const testLayer = Layer . mergeAll (
4344 database ,
4445 ProjectionStore . layer . pipe ( Layer . provide ( database ) ) ,
46+ EffectOutbox . layer . pipe ( Layer . provide ( database ) ) ,
4547 makeOrchestratorV2ReplayLayerWithRegistry (
4648 { name : "control-reads" } ,
4749 ProviderAdapterRegistry . makeLayer ( [ adapter ] ) ,
4850 { databaseLayer : database , runEffectWorker : false } ,
4951 ) ,
5052) ;
5153
54+ for ( const terminalCommand of [ "thread.archive" , "thread.delete" ] as const ) {
55+ it . effect ( `rejects prepared-run.release after ${ terminalCommand } ` , ( ) =>
56+ Effect . gen ( function * ( ) {
57+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
58+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
59+ const outbox = yield * EffectOutbox . EffectOutboxV2 ;
60+ const threadId = ThreadId . make ( "thread:terminal-preparation" ) ;
61+ yield * orchestrator . dispatch ( {
62+ type : "thread.create" ,
63+ commandId : CommandId . make ( "create-terminal-preparation" ) ,
64+ threadId,
65+ projectId : ProjectId . make ( "project:terminal-preparation" ) ,
66+ title : "Preparing thread" ,
67+ modelSelection,
68+ runtimeMode : "full-access" ,
69+ interactionMode : "default" ,
70+ branch : null ,
71+ worktreePath : null ,
72+ createdBy : "user" ,
73+ creationSource : "web" ,
74+ } ) ;
75+ yield * orchestrator . dispatch ( {
76+ type : "message.dispatch" ,
77+ commandId : CommandId . make ( "prepare-terminal-message" ) ,
78+ threadId,
79+ messageId : MessageId . make ( "preparing-input" ) ,
80+ text : "Prepare workspace" ,
81+ attachments : [ ] ,
82+ dispatchMode : { type : "defer_start" } ,
83+ createdBy : "user" ,
84+ creationSource : "web" ,
85+ } ) ;
86+ yield * orchestrator . dispatch ( {
87+ type : terminalCommand ,
88+ commandId : CommandId . make ( "close-terminal-preparation" ) ,
89+ threadId,
90+ } ) ;
91+ const before = yield * projections . getThreadProjection ( threadId ) ;
92+ const runId = before . runs [ 0 ] ! . id ;
93+ const commandId = CommandId . make ( "release-terminal-preparation" ) ;
94+ const error = yield * orchestrator
95+ . dispatch ( {
96+ type : "prepared-run.release" ,
97+ commandId,
98+ threadId,
99+ runId,
100+ } )
101+ . pipe ( Effect . flip ) ;
102+
103+ assert . equal ( error . _tag , "OrchestratorDispatchError" ) ;
104+ assert . equal ( error . cause , `Thread ${ threadId } is not active.` ) ;
105+ assert . deepEqual ( yield * projections . getThreadProjection ( threadId ) , before ) ;
106+ assert . deepEqual ( yield * outbox . listByCommandId ( commandId ) , [ ] ) ;
107+ if ( terminalCommand === "thread.archive" ) {
108+ yield * orchestrator . dispatch ( {
109+ type : "thread.unarchive" ,
110+ commandId : CommandId . make ( "unarchive-terminal-preparation" ) ,
111+ threadId,
112+ } ) ;
113+ const resumedCommandId = CommandId . make ( "release-unarchived-preparation" ) ;
114+ yield * orchestrator . dispatch ( {
115+ type : "prepared-run.release" ,
116+ commandId : resumedCommandId ,
117+ threadId,
118+ runId,
119+ } ) ;
120+ assert . equal (
121+ ( yield * projections . getThreadProjection ( threadId ) ) . runs [ 0 ] ! . status ,
122+ "starting" ,
123+ ) ;
124+ assert . isTrue (
125+ ( yield * outbox . listByCommandId ( resumedCommandId ) ) . some (
126+ ( effect ) => effect . request . type === "provider-turn.start" ,
127+ ) ,
128+ ) ;
129+ }
130+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
131+ ) ;
132+ }
133+
52134it . effect (
53135 "dispatches metadata, queue resume and request controls without hydrating unrelated history" ,
54136 ( ) =>
0 commit comments