@@ -3935,6 +3935,84 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
39353935 } ) ,
39363936 ) ;
39373937
3938+ it . effect ( "keeps a visit when a deferred mark-unread lands after a newer run completed" , ( ) =>
3939+ Effect . gen ( function * ( ) {
3940+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
3941+ const eventSink = yield * EventSink . EventSinkV2 ;
3942+ const threadId = ThreadId . make ( "runtime-layer-superseded-mark-unread-thread" ) ;
3943+ const runMessage = ( id : string ) =>
3944+ Effect . gen ( function * ( ) {
3945+ yield * orchestrator . dispatch ( {
3946+ type : "message.dispatch" ,
3947+ createdBy : "user" ,
3948+ creationSource : "web" ,
3949+ commandId : CommandId . make ( id ) ,
3950+ threadId,
3951+ messageId : MessageId . make ( id ) ,
3952+ text : id ,
3953+ attachments : [ ] ,
3954+ modelSelection,
3955+ dispatchMode : { type : "start_immediately" } ,
3956+ } ) ;
3957+ const run = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . at ( - 1 ) ;
3958+ if ( run === undefined ) return yield * Effect . die ( new Error ( "Run missing." ) ) ;
3959+ yield * TestClock . adjust ( "1 second" ) ;
3960+ const now = yield * DateTime . now ;
3961+ yield * eventSink . write ( {
3962+ commandId : CommandId . make ( `${ id } -completed` ) ,
3963+ events : [
3964+ {
3965+ id : EventId . make ( `${ id } -completed` ) ,
3966+ type : "run.updated" ,
3967+ threadId,
3968+ runId : run . id ,
3969+ occurredAt : now ,
3970+ payload : { ...run , status : "completed" , startedAt : now , completedAt : now } ,
3971+ } ,
3972+ ] ,
3973+ } ) ;
3974+ return { run, completedAt : now } ;
3975+ } ) ;
3976+
3977+ yield * orchestrator . dispatch ( {
3978+ type : "thread.create" ,
3979+ createdBy : "user" ,
3980+ creationSource : "web" ,
3981+ commandId : CommandId . make ( "runtime-layer-superseded-mark-unread-create" ) ,
3982+ threadId,
3983+ projectId : ProjectId . make ( "runtime-layer-superseded-mark-unread-project" ) ,
3984+ title : "Superseded mark unread" ,
3985+ modelSelection,
3986+ runtimeMode : "full-access" ,
3987+ interactionMode : "default" ,
3988+ branch : null ,
3989+ worktreePath : "/tmp/runtime-layer-superseded-mark-unread" ,
3990+ } ) ;
3991+ const older = yield * runMessage ( "runtime-layer-superseded-mark-unread-older" ) ;
3992+ const visitedAt = DateTime . add ( older . completedAt , { milliseconds : 500 } ) ;
3993+ yield * orchestrator . dispatch ( {
3994+ type : "thread.visit" ,
3995+ commandId : CommandId . make ( "runtime-layer-superseded-mark-unread-visit" ) ,
3996+ threadId,
3997+ visitedAt : DateTime . formatIso ( visitedAt ) ,
3998+ } ) ;
3999+ // The newer run completes after the visit, so only the runId guard keeps
4000+ // the older run's deferred mark from marking it unread.
4001+ yield * runMessage ( "runtime-layer-superseded-mark-unread-newer" ) ;
4002+
4003+ yield * orchestrator . dispatch ( {
4004+ type : "thread.mark-unread" ,
4005+ commandId : CommandId . make ( "runtime-layer-superseded-mark-unread-deferred" ) ,
4006+ threadId,
4007+ runId : older . run . id ,
4008+ } ) ;
4009+ assert . deepEqual (
4010+ ( yield * orchestrator . getThreadProjection ( threadId ) ) . thread . lastVisitedAt ,
4011+ visitedAt ,
4012+ ) ;
4013+ } ) ,
4014+ ) ;
4015+
39384016 it . effect ( "settles past held automatic runs but not held user messages" , ( ) =>
39394017 Effect . gen ( function * ( ) {
39404018 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
0 commit comments