@@ -54,6 +54,68 @@ const layerTest = Layer.mergeAll(
5454 ) ,
5555) ;
5656
57+ it . effect ( "marks a never-visited thread's first completion unread" , ( ) =>
58+ Effect . gen ( function * ( ) {
59+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
60+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
61+ const threadId = ThreadId . make ( "thread:completion-watermark" ) ;
62+ yield * orchestrator . dispatch ( {
63+ type : "thread.create" ,
64+ commandId : CommandId . make ( "create-completion-watermark" ) ,
65+ threadId,
66+ projectId : ProjectId . make ( "project:completion-watermark" ) ,
67+ title : "Background task" ,
68+ modelSelection,
69+ runtimeMode : "full-access" ,
70+ interactionMode : "default" ,
71+ branch : null ,
72+ worktreePath : null ,
73+ createdBy : "user" ,
74+ creationSource : "web" ,
75+ } ) ;
76+ assert . isNull ( ( yield * projections . getThreadShell ( threadId ) ) ?. lastVisitedAt ) ;
77+ const dispatch = ( id : string ) =>
78+ orchestrator . dispatch ( {
79+ type : "message.dispatch" ,
80+ commandId : CommandId . make ( id ) ,
81+ threadId,
82+ messageId : MessageId . make ( id ) ,
83+ text : "Run in the background" ,
84+ attachments : [ ] ,
85+ dispatchMode : { type : "defer_start" } ,
86+ createdBy : "agent" ,
87+ creationSource : "mcp" ,
88+ } ) ;
89+ yield * TestClock . adjust ( "1 second" ) ;
90+ const dispatchedAt = yield * DateTime . now ;
91+ yield * dispatch ( "first-completion-watermark" ) ;
92+ yield * orchestrator . dispatch ( {
93+ type : "run.interrupt" ,
94+ commandId : CommandId . make ( "interrupt-completion-watermark" ) ,
95+ threadId,
96+ runId : ( yield * projections . getThreadProjection ( threadId ) ) . runs [ 0 ] ! . id ,
97+ } ) ;
98+ // The run ends in the dispatch's millisecond and must still be unread.
99+ const ended = yield * projections . getThreadShell ( threadId ) ;
100+ assert . ok ( ended ?. lastVisitedAt && ended . latestRunCompletedAt ) ;
101+ assert . equal (
102+ DateTime . toEpochMillis ( ended . lastVisitedAt ) ,
103+ DateTime . toEpochMillis ( dispatchedAt ) - 1 ,
104+ ) ;
105+ assert . isAbove (
106+ DateTime . toEpochMillis ( ended . latestRunCompletedAt ) ,
107+ DateTime . toEpochMillis ( ended . lastVisitedAt ) ,
108+ ) ;
109+
110+ yield * TestClock . adjust ( "1 second" ) ;
111+ yield * dispatch ( "second-completion-watermark" ) ;
112+ assert . deepEqual (
113+ ( yield * projections . getThreadShell ( threadId ) ) ?. lastVisitedAt ,
114+ ended . lastVisitedAt ,
115+ ) ;
116+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
117+ ) ;
118+
57119it . effect (
58120 "dispatches metadata, queue resume and request controls without hydrating unrelated history" ,
59121 ( ) =>
0 commit comments