11import {
22 type AgentSessionImportSource ,
3+ ChatAttachment ,
34 CheckpointRef ,
45 EventId ,
56 MessageId ,
@@ -12,6 +13,7 @@ import { assert, it } from "@effect/vitest";
1213import * as NodeServices from "@effect/platform-node/NodeServices" ;
1314import * as Effect from "effect/Effect" ;
1415import * as Layer from "effect/Layer" ;
16+ import * as Option from "effect/Option" ;
1517import * as Schema from "effect/Schema" ;
1618import * as SqlClient from "effect/unstable/sql/SqlClient" ;
1719
@@ -31,6 +33,9 @@ const asTurnId = (value: string): TurnId => TurnId.make(value);
3133const asMessageId = ( value : string ) : MessageId => MessageId . make ( value ) ;
3234const asEventId = ( value : string ) : EventId => EventId . make ( value ) ;
3335const asCheckpointRef = ( value : string ) : CheckpointRef => CheckpointRef . make ( value ) ;
36+ const encodeChatAttachments = Schema . encodeEffect (
37+ Schema . fromJsonString ( Schema . Array ( ChatAttachment ) ) ,
38+ ) ;
3439
3540const projectionSnapshotLayer = it . layer (
3641 OrchestrationProjectionSnapshotQueryLive . pipe (
@@ -598,6 +603,142 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
598603 } ) ,
599604 ) ;
600605
606+ it . effect ( "reads one turn-start message without decoding unrelated history" , ( ) =>
607+ Effect . gen ( function * ( ) {
608+ const query = yield * ProjectionSnapshotQuery ;
609+ const sql = yield * SqlClient . SqlClient ;
610+ const threadId = ThreadId . make ( "thread-turn-start-read" ) ;
611+ const messageId = MessageId . make ( "message-turn-start-read" ) ;
612+ const createdAt = "2026-09-05T00:00:00.000Z" ;
613+ const attachments = [
614+ {
615+ type : "file" as const ,
616+ id : "notes" ,
617+ name : "notes.txt" ,
618+ mimeType : "text/plain" ,
619+ sizeBytes : 8 ,
620+ } ,
621+ ] ;
622+ const attachmentsJson = yield * encodeChatAttachments ( attachments ) ;
623+ yield * sql `
624+ WITH RECURSIVE history(n) AS (
625+ VALUES (1) UNION ALL SELECT n + 1 FROM history WHERE n < 2000
626+ )
627+ INSERT INTO projection_thread_messages (
628+ message_id, thread_id, turn_id, role, text, attachments_json,
629+ is_streaming, created_at, updated_at
630+ )
631+ SELECT 'turn-start-history:' || n, ${ threadId } , 'old-turn:' || n, 'assistant',
632+ 'Unrelated assistant output', 'not-json', 0, ${ createdAt } , ${ createdAt }
633+ FROM history
634+ ` ;
635+ yield * sql `
636+ INSERT INTO projection_thread_messages (
637+ message_id, thread_id, role, text, attachments_json, is_streaming, created_at, updated_at
638+ ) VALUES (${ messageId } , ${ threadId } , 'user', 'Read these notes',
639+ ${ attachmentsJson } , 0, ${ createdAt } , ${ createdAt } )
640+ ` ;
641+ yield * sql `
642+ INSERT INTO projection_thread_messages (
643+ message_id, thread_id, role, text, attachments_json, is_streaming, created_at, updated_at
644+ ) VALUES ('turn-start-unrelated-user', 'thread-turn-start-unrelated', 'user', 'Unrelated prompt',
645+ 'not-json', 0, ${ createdAt } , ${ createdAt } )
646+ ` ;
647+
648+ const counter = makeSqlStatementCounter ( ) ;
649+ const context = yield * query
650+ . getTurnStartMessage ( { threadId, messageId } )
651+ . pipe ( Effect . withTracer ( counter . tracer ) ) ;
652+ assert . equal ( counter . count ( ) , 1 ) ;
653+ assert . deepEqual (
654+ context ,
655+ Option . some ( {
656+ message : {
657+ id : messageId ,
658+ role : "user" ,
659+ text : "Read these notes" ,
660+ turnId : null ,
661+ streaming : false ,
662+ createdAt,
663+ updatedAt : createdAt ,
664+ attachments,
665+ } ,
666+ hasOtherUserMessages : false ,
667+ } ) ,
668+ ) ;
669+ assert . equal (
670+ ( yield * query . getTurnStartMessage ( {
671+ threadId : ThreadId . make ( "thread-turn-start-unrelated" ) ,
672+ messageId,
673+ } ) ) . _tag ,
674+ "None" ,
675+ ) ;
676+ assert . equal (
677+ ( yield * query . getTurnStartMessage ( { threadId, messageId : MessageId . make ( "missing" ) } ) ) . _tag ,
678+ "None" ,
679+ ) ;
680+ } ) . pipe (
681+ Effect . ensuring (
682+ Effect . gen ( function * ( ) {
683+ const sql = yield * SqlClient . SqlClient ;
684+ yield * sql `
685+ DELETE FROM projection_thread_messages
686+ WHERE thread_id IN ('thread-turn-start-read', 'thread-turn-start-unrelated')
687+ ` ;
688+ } ) . pipe ( Effect . orDie ) ,
689+ ) ,
690+ ) ,
691+ ) ;
692+
693+ it . effect ( "keeps compaction and queued-message eligibility in the turn-start query" , ( ) =>
694+ Effect . gen ( function * ( ) {
695+ const query = yield * ProjectionSnapshotQuery ;
696+ const sql = yield * SqlClient . SqlClient ;
697+ const threadId = ThreadId . make ( "thread-turn-start-eligibility" ) ;
698+ const messageId = MessageId . make ( "message-turn-start-eligibility" ) ;
699+ const createdAt = "2026-09-05T00:00:00.000Z" ;
700+ yield * sql `
701+ INSERT INTO projection_thread_messages (
702+ message_id, thread_id, role, text, is_streaming, created_at, updated_at
703+ ) VALUES (${ messageId } , ${ threadId } , 'user', 'Start a turn', 0, ${ createdAt } , ${ createdAt } )
704+ ` ;
705+ yield * sql `
706+ INSERT INTO projection_thread_messages (
707+ message_id, thread_id, role, text, attachments_json, is_streaming, created_at, updated_at
708+ ) VALUES ('turn-start-other-user', ${ threadId } , 'user', '/compact', NULL, 0,
709+ '2026-09-05T00:00:01.000Z', '2026-09-05T00:00:01.000Z')
710+ ` ;
711+
712+ for ( const { text, attachments, hasOtherUserMessages } of [
713+ { text : "/compact" , attachments : null , hasOtherUserMessages : false } ,
714+ {
715+ text : "\t\n\r /CoMpAcT\u00a0\u2028\ufeff" ,
716+ attachments : "[ ]" ,
717+ hasOtherUserMessages : false ,
718+ } ,
719+ { text : "/compact keep recent errors" , attachments : "[]" , hasOtherUserMessages : true } ,
720+ { text : "" , attachments : null , hasOtherUserMessages : true } ,
721+ { text : "Queued prompt" , attachments : null , hasOtherUserMessages : true } ,
722+ {
723+ text : "/compact" ,
724+ attachments :
725+ '[{"type":"file","id":"notes","name":"notes.txt","mimeType":"text/plain","sizeBytes":8}]' ,
726+ hasOtherUserMessages : true ,
727+ } ,
728+ ] ) {
729+ yield * sql `
730+ UPDATE projection_thread_messages SET text = ${ text } , attachments_json = ${ attachments }
731+ WHERE message_id = 'turn-start-other-user'
732+ ` ;
733+ const context = yield * query . getTurnStartMessage ( { threadId, messageId } ) ;
734+ assert . equal ( context . _tag , "Some" ) ;
735+ if ( context . _tag === "Some" ) {
736+ assert . equal ( context . value . hasOtherUserMessages , hasOtherUserMessages ) ;
737+ }
738+ }
739+ } ) ,
740+ ) ;
741+
601742 it . effect ( "keeps archived threads out of the main shell snapshot" , ( ) =>
602743 Effect . gen ( function * ( ) {
603744 const snapshotQuery = yield * ProjectionSnapshotQuery ;
0 commit comments