@@ -6278,6 +6278,114 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
62786278 assert . instanceOf ( invalidVisit , Orchestrator . OrchestratorDispatchError ) ;
62796279 } ) ,
62806280 ) ;
6281+
6282+ it . effect ( "marks unread before the user's last turn when an automatic compaction followed" , ( ) =>
6283+ Effect . gen ( function * ( ) {
6284+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
6285+ const threadId = ThreadId . make ( "runtime-layer-mark-unread-compacted" ) ;
6286+ const turnCompletedAt = DateTime . makeUnsafe ( "2026-07-24T10:00:00.000Z" ) ;
6287+ yield * orchestrator . dispatch ( {
6288+ type : "thread.create" ,
6289+ createdBy : "user" ,
6290+ creationSource : "web" ,
6291+ commandId : CommandId . make ( `${ threadId } :create` ) ,
6292+ threadId,
6293+ projectId : ProjectId . make ( `${ threadId } :project` ) ,
6294+ title : "Compacted thread" ,
6295+ modelSelection,
6296+ runtimeMode : "full-access" ,
6297+ interactionMode : "default" ,
6298+ branch : null ,
6299+ worktreePath : null ,
6300+ } ) ;
6301+ const runs = [
6302+ { text : "Continue" , at : turnCompletedAt } ,
6303+ {
6304+ text : "/compact" ,
6305+ idleCompaction : "beforeCacheExpires" as const ,
6306+ at : DateTime . makeUnsafe ( "2026-07-24T11:00:00.000Z" ) ,
6307+ } ,
6308+ ] ;
6309+ yield * ( yield * EventSink . EventSinkV2 ) . write ( {
6310+ commandId : CommandId . make ( `${ threadId } :seed` ) ,
6311+ events : runs . flatMap ( ( { at, ...message } , index ) => {
6312+ const runId = RunId . make ( `${ threadId } :run-${ index } ` ) ;
6313+ const messageId = MessageId . make ( `${ threadId } :message-${ index } ` ) ;
6314+ return [
6315+ {
6316+ id : EventId . make ( `${ messageId } :event` ) ,
6317+ type : "message.updated" as const ,
6318+ threadId,
6319+ occurredAt : at ,
6320+ payload : {
6321+ ...message ,
6322+ id : messageId ,
6323+ threadId,
6324+ runId,
6325+ nodeId : null ,
6326+ role : "user" as const ,
6327+ attachments : [ ] ,
6328+ streaming : false ,
6329+ createdBy : "user" as const ,
6330+ creationSource : "web" as const ,
6331+ createdAt : at ,
6332+ updatedAt : at ,
6333+ } ,
6334+ } ,
6335+ {
6336+ id : EventId . make ( `${ runId } :event` ) ,
6337+ type : "run.created" as const ,
6338+ threadId,
6339+ runId,
6340+ providerInstanceId : modelSelection . instanceId ,
6341+ occurredAt : at ,
6342+ payload : {
6343+ id : runId ,
6344+ threadId,
6345+ ordinal : index + 1 ,
6346+ providerInstanceId : modelSelection . instanceId ,
6347+ modelSelection,
6348+ providerThreadId : null ,
6349+ userMessageId : messageId ,
6350+ rootNodeId : null ,
6351+ activeAttemptId : null ,
6352+ status : "completed" as const ,
6353+ queuePosition : null ,
6354+ requestedAt : at ,
6355+ startedAt : at ,
6356+ completedAt : at ,
6357+ checkpointId : null ,
6358+ contextHandoffId : null ,
6359+ } ,
6360+ } ,
6361+ ] ;
6362+ } ) ,
6363+ } ) ;
6364+
6365+ yield * orchestrator . dispatch ( {
6366+ type : "thread.mark-unread" ,
6367+ commandId : CommandId . make ( `${ threadId } :mark-unread` ) ,
6368+ threadId,
6369+ } ) ;
6370+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
6371+ assert . deepEqual (
6372+ projection . thread . lastVisitedAt ,
6373+ DateTime . subtract ( turnCompletedAt , { milliseconds : 1 } ) ,
6374+ ) ;
6375+ const shell = yield * orchestrator . getThreadShell ( threadId ) ;
6376+ assert . deepEqual ( shell ! . latestRunCompletedAt , turnCompletedAt ) ;
6377+ assert . deepEqual ( shell ! . lastVisitedAt , projection . thread . lastVisitedAt ) ;
6378+
6379+ yield * orchestrator . dispatch ( {
6380+ type : "thread.visit" ,
6381+ commandId : CommandId . make ( `${ threadId } :visit` ) ,
6382+ threadId,
6383+ visitedAt : DateTime . formatIso ( turnCompletedAt ) ,
6384+ } ) ;
6385+ const revisited = yield * orchestrator . getThreadShell ( threadId ) ;
6386+ assert . deepEqual ( revisited ! . lastVisitedAt , turnCompletedAt ) ;
6387+ } ) ,
6388+ ) ;
62816389} ) ;
62826390
62836391it . layer ( Layer . fresh ( layerSharedApplicationDataPlaneTest ) ) ( "thread opens" , ( it ) => {
0 commit comments