@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22import { assert , it } from "@effect/vitest" ;
33import {
44 CommandId ,
5+ EventId ,
56 MessageId ,
67 type ModelSelection ,
78 ProjectId ,
@@ -10,19 +11,23 @@ import {
1011 ThreadId ,
1112} from "@t3tools/contracts" ;
1213import * as Cause from "effect/Cause" ;
14+ import * as DateTime from "effect/DateTime" ;
1315import * as Effect from "effect/Effect" ;
1416import * as Exit from "effect/Exit" ;
1517import * as FileSystem from "effect/FileSystem" ;
18+ import * as Fiber from "effect/Fiber" ;
1619import * as Layer from "effect/Layer" ;
1720import * as Option from "effect/Option" ;
1821import * as Path from "effect/Path" ;
1922import * as Ref from "effect/Ref" ;
2023import * as SqlClient from "effect/unstable/sql/SqlClient" ;
24+ import * as Stream from "effect/Stream" ;
2125
2226import { makeSqlitePersistenceLive } from "../persistence/Layers/Sqlite.ts" ;
2327import { runV2RecoveryPhase } from "../serverRuntimeStartup.ts" ;
2428import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts" ;
2529import { CommandReceiptStoreV2 } from "./CommandReceiptStore.ts" ;
30+ import { EventSinkV2 } from "./EventSink.ts" ;
2631import * as Orchestrator from "./Orchestrator.ts" ;
2732import { ProviderRuntimeRecoveryService } from "./ProviderRuntimeRecoveryService.ts" ;
2833import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts" ;
@@ -211,6 +216,188 @@ it.effect("retries a transient deferred apply read before recording its receipt"
211216 ) ,
212217) ;
213218
219+ it . effect ( "promotes queued work when terminal deferred apply fails" , ( ) =>
220+ Effect . scoped (
221+ Effect . gen ( function * ( ) {
222+ const fs = yield * FileSystem . FileSystem ;
223+ const path = yield * Path . Path ;
224+ const tempDir = yield * Effect . acquireRelease (
225+ fs . makeTempDirectory ( { prefix : "t3-deferred-organization-terminal-" } ) ,
226+ ( directory ) => fs . remove ( directory , { recursive : true , force : true } ) . pipe ( Effect . orDie ) ,
227+ ) ;
228+ const databaseLayer = makeSqlitePersistenceLive ( path . join ( tempDir , "state.sqlite" ) ) . pipe (
229+ Layer . provide ( NodeServices . layer ) ,
230+ ) ;
231+ const registryLayer = makeProviderAdapterRegistryLayer ( [ adapter ] ) ;
232+ const threadId = ThreadId . make ( "thread:deferred-organization-terminal" ) ;
233+
234+ const seeded = yield * Effect . scoped (
235+ Effect . gen ( function * ( ) {
236+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
237+ yield * orchestrator . dispatch ( {
238+ type : "thread.create" ,
239+ createdBy : "user" ,
240+ creationSource : "web" ,
241+ commandId : CommandId . make ( "command:deferred-organization-terminal:create" ) ,
242+ threadId,
243+ projectId : ProjectId . make ( "project:deferred-organization-terminal" ) ,
244+ title : "Deferred organization terminal failure" ,
245+ modelSelection,
246+ runtimeMode : "full-access" ,
247+ interactionMode : "default" ,
248+ branch : null ,
249+ worktreePath : tempDir ,
250+ } ) ;
251+ yield * orchestrator . dispatch ( {
252+ type : "message.dispatch" ,
253+ createdBy : "user" ,
254+ creationSource : "web" ,
255+ commandId : CommandId . make ( "command:deferred-organization-terminal:active" ) ,
256+ threadId,
257+ messageId : MessageId . make ( "message:deferred-organization-terminal:active" ) ,
258+ text : "Complete this run before applying organization." ,
259+ attachments : [ ] ,
260+ modelSelection,
261+ dispatchMode : { type : "start_immediately" } ,
262+ } ) ;
263+ const activeRun = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs [ 0 ] ;
264+ assert . isDefined ( activeRun ) ;
265+ yield * orchestrator . dispatch ( {
266+ type : "thread.organization.defer" ,
267+ commandId : CommandId . make ( "command:deferred-organization-terminal:schedule" ) ,
268+ threadId,
269+ runId : activeRun . id ,
270+ action : "settle" ,
271+ } ) ;
272+ yield * orchestrator . dispatch ( {
273+ type : "message.dispatch" ,
274+ createdBy : "user" ,
275+ creationSource : "web" ,
276+ commandId : CommandId . make ( "command:deferred-organization-terminal:queued" ) ,
277+ threadId,
278+ messageId : MessageId . make ( "message:deferred-organization-terminal:queued" ) ,
279+ text : "Promote this run after the active run ends." ,
280+ attachments : [ ] ,
281+ modelSelection,
282+ dispatchMode : { type : "queue_after_active" } ,
283+ } ) ;
284+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
285+ const queuedRun = projection . runs . find ( ( run ) => run . status === "queued" ) ;
286+ assert . isDefined ( queuedRun ) ;
287+ return { activeRun, queuedRun } ;
288+ } ) . pipe (
289+ Effect . provide (
290+ makeOrchestratorV2ReplayLayerWithRegistry (
291+ {
292+ name : "deferred-organization-terminal:first-runtime" ,
293+ runtimePolicyOverride : { cwd : tempDir } ,
294+ } ,
295+ registryLayer ,
296+ { databaseLayer, runEffectWorker : false } ,
297+ ) ,
298+ ) ,
299+ ) ,
300+ ) ;
301+
302+ const armed = yield * Ref . make ( false ) ;
303+ const projectionReads = yield * Ref . make ( 0 ) ;
304+ const decorateProjectionStore = ( store : ProjectionStore . ProjectionStoreV2 [ "Service" ] ) =>
305+ ProjectionStore . ProjectionStoreV2 . of ( {
306+ ...store ,
307+ getThreadProjection : ( requestedThreadId ) =>
308+ Ref . get ( armed ) . pipe (
309+ Effect . flatMap ( ( isArmed ) =>
310+ isArmed
311+ ? Ref . updateAndGet ( projectionReads , ( count ) => count + 1 ) . pipe (
312+ Effect . flatMap ( ( count ) =>
313+ count === 3
314+ ? Effect . fail (
315+ new ProjectionStore . ProjectionStoreReadError ( {
316+ threadId : requestedThreadId ,
317+ cause : "simulated terminal deferred apply failure" ,
318+ } ) ,
319+ )
320+ : store . getThreadProjection ( requestedThreadId ) ,
321+ ) ,
322+ )
323+ : store . getThreadProjection ( requestedThreadId ) ,
324+ ) ,
325+ ) ,
326+ } ) ;
327+
328+ yield * Effect . scoped (
329+ Effect . gen ( function * ( ) {
330+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
331+ const eventSink = yield * EventSinkV2 ;
332+ const receipts = yield * CommandReceiptStoreV2 ;
333+ const afterSequence = yield * eventSink . latestSequence ( { threadId } ) ;
334+ const promoted = yield * eventSink . stream ( { threadId, afterSequence } ) . pipe (
335+ Stream . filter (
336+ ( stored ) =>
337+ stored . event . type === "run.updated" &&
338+ stored . event . payload . id === seeded . queuedRun . id &&
339+ stored . event . payload . status === "starting" ,
340+ ) ,
341+ Stream . runHead ,
342+ Effect . forkChild ,
343+ ) ;
344+ yield * Ref . set ( armed , true ) ;
345+ const completedAt = yield * DateTime . now ;
346+ yield * eventSink . write ( {
347+ events : [
348+ {
349+ id : EventId . make ( "event:deferred-organization-terminal:completed" ) ,
350+ type : "run.updated" ,
351+ threadId,
352+ runId : seeded . activeRun . id ,
353+ ...( seeded . activeRun . rootNodeId === null
354+ ? { }
355+ : { nodeId : seeded . activeRun . rootNodeId } ) ,
356+ providerInstanceId : seeded . activeRun . providerInstanceId ,
357+ occurredAt : completedAt ,
358+ payload : {
359+ ...seeded . activeRun ,
360+ status : "completed" ,
361+ completedAt,
362+ } ,
363+ } ,
364+ ] ,
365+ } ) ;
366+ assert . isTrue ( Option . isSome ( yield * Fiber . join ( promoted ) ) ) ;
367+ yield * Ref . set ( armed , false ) ;
368+
369+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
370+ assert . equal (
371+ projection . runs . find ( ( run ) => run . id === seeded . queuedRun . id ) ?. status ,
372+ "starting" ,
373+ ) ;
374+ assert . equal ( yield * Ref . get ( projectionReads ) , 4 ) ;
375+ assert . isTrue (
376+ Option . isNone (
377+ yield * receipts . getByCommandId (
378+ CommandId . make (
379+ `command:system:thread-organization-defer:${ threadId } :${ seeded . activeRun . id } ` ,
380+ ) ,
381+ ) ,
382+ ) ,
383+ ) ;
384+ } ) . pipe (
385+ Effect . provide (
386+ makeOrchestratorV2ReplayLayerWithRegistry (
387+ {
388+ name : "deferred-organization-terminal:second-runtime" ,
389+ runtimePolicyOverride : { cwd : tempDir } ,
390+ } ,
391+ registryLayer ,
392+ { databaseLayer, runEffectWorker : false , decorateProjectionStore } ,
393+ ) ,
394+ ) ,
395+ ) ,
396+ ) ;
397+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
398+ ) ,
399+ ) ;
400+
214401it . effect ( "discards a stale deferred organization intent after runtime restart" , ( ) =>
215402 Effect . scoped (
216403 Effect . gen ( function * ( ) {
0 commit comments