@@ -12,6 +12,7 @@ import {
1212import * as DateTime from "effect/DateTime" ;
1313import * as Effect from "effect/Effect" ;
1414import * as Layer from "effect/Layer" ;
15+ import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts" ;
1516import * as FileSystem from "effect/FileSystem" ;
1617import { TestClock } from "effect/testing" ;
1718import { McpSchema , McpServer } from "effect/unstable/ai" ;
@@ -53,7 +54,10 @@ const scope: McpInvocationContext.McpInvocationScope = {
5354 capabilities : new Set ( [ "threads" ] ) ,
5455 issuedAt : 1 ,
5556} ;
56- function makeLayer ( db : string ) {
57+ function makeLayer (
58+ db : string ,
59+ wrapQuery = ( query : ProjectionSnapshotQuery . ProjectionSnapshotQueryShape ) => query ,
60+ ) {
5761 const orchestration = Layer . mergeAll (
5862 OrchestrationEngineLive . pipe (
5963 Layer . provide ( OrchestrationProjectionSnapshotQueryLive ) ,
@@ -70,7 +74,12 @@ function makeLayer(db: string) {
7074 ) ;
7175 return ThreadsToolkitRegistrationLive . pipe (
7276 Layer . provideMerge ( McpServer . McpServer . layer ) ,
73- Layer . provideMerge ( orchestration ) ,
77+ Layer . provideMerge (
78+ Layer . effect (
79+ ProjectionSnapshotQuery . ProjectionSnapshotQuery ,
80+ Effect . map ( ProjectionSnapshotQuery . ProjectionSnapshotQuery , wrapQuery ) ,
81+ ) . pipe ( Layer . provideMerge ( orchestration ) ) ,
82+ ) ,
7483 Layer . provideMerge ( ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-peer-tools-" } ) ) ,
7584 Layer . provideMerge ( NodeServices . layer ) ,
7685 ) ;
@@ -123,6 +132,44 @@ const seed = Effect.gen(function* () {
123132} ) ;
124133
125134describe ( "peer thread MCP" , ( ) => {
135+ it . effect ( "retries a read when projection changes between shell and detail" , ( ) =>
136+ Effect . scoped (
137+ Effect . gen ( function * ( ) {
138+ const fs = yield * FileSystem . FileSystem ;
139+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-peer-detail-" } ) ;
140+ let detailReads = 0 ;
141+ let rename = Effect . void ;
142+ yield * Effect . gen ( function * ( ) {
143+ yield * seed ;
144+ const engine = yield * OrchestrationEngineService ;
145+ rename = engine
146+ . dispatch ( {
147+ type : "thread.meta.update" ,
148+ threadId : callerId ,
149+ commandId : CommandId . make ( "rename-during-read" ) ,
150+ title : "New title" ,
151+ } )
152+ . pipe ( Effect . asVoid , Effect . orDie ) ;
153+ const read = result ( yield * call ( "read_thread" , { threadId : callerId } ) ) ;
154+ expect ( read . thread . title ) . toBe ( "New title" ) ;
155+ expect ( read . thread . hasPendingApprovals ) . toBe ( false ) ;
156+ expect ( detailReads ) . toBe ( 2 ) ;
157+ } ) . pipe (
158+ Effect . provide (
159+ makeLayer ( `${ directory } /state.sqlite` , ( query ) => ( {
160+ ...query ,
161+ getThreadDetailSnapshot : ( ...args ) =>
162+ Effect . gen ( function * ( ) {
163+ if ( ++ detailReads === 1 ) yield * rename ;
164+ return yield * query . getThreadDetailSnapshot ( ...args ) ;
165+ } ) ,
166+ } ) ) ,
167+ ) ,
168+ ) ;
169+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
170+ ) ,
171+ ) ;
172+
126173 it . effect ( "keeps retry IDs distinct across caller IDs containing delimiters" , ( ) =>
127174 Effect . scoped (
128175 Effect . gen ( function * ( ) {
0 commit comments