1- import { assert , describe , it } from "@effect/vitest" ;
1+ import { assert , describe , expect , it } from "@effect/vitest" ;
22import { EnvironmentId , ProviderInstanceId , ThreadId } from "@t3tools/contracts" ;
33
44import {
@@ -7,7 +7,18 @@ import {
77 T3_PI_RUNTIME_MODE_ENV ,
88 T3_PI_MCP_EXTENSION_PATH_ENV ,
99} from "./mcpExtensionSource.ts" ;
10- import { buildPiRpcLaunch , resolvePiLaunchArgs } from "./mcpInjection.ts" ;
10+ import * as Effect from "effect/Effect" ;
11+ import * as Fiber from "effect/Fiber" ;
12+ import * as Option from "effect/Option" ;
13+ import * as NodePath from "effect/Path" ;
14+ import * as NodeFS from "effect/FileSystem" ;
15+ import * as NodeServices from "@effect/platform-node/NodeServices" ;
16+ import {
17+ buildPiRpcLaunch ,
18+ materializePiT3McpExtension ,
19+ resolvePiLaunchArgs ,
20+ } from "./mcpInjection.ts" ;
21+ import { PI_T3_MCP_EXTENSION_SOURCE } from "./mcpExtensionSource.ts" ;
1122
1223const threadId = ThreadId . make ( "thread-pi-t3-mcp" ) ;
1324
@@ -145,3 +156,53 @@ describe("pi T3 MCP injection", () => {
145156 assert . isTrue ( resolvePiLaunchArgs ( "--model deepseek/v4" ) . ok ) ;
146157 } ) ;
147158} ) ;
159+
160+ describe ( "materializePiT3McpExtension" , ( ) => {
161+ // A pi launch concurrent with the materialize reads the extension file from
162+ // disk; a truncate-then-write exposes an empty or partial file mid-write and
163+ // the launch loads a broken extension.
164+ it . live ( "never exposes a torn extension file to a concurrent reader" , ( ) =>
165+ Effect . gen ( function * ( ) {
166+ const fs = yield * NodeFS . FileSystem ;
167+ const path = yield * NodePath . Path ;
168+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-pi-atomic-" } ) ;
169+ const anomalies : Array < string > = [ ] ;
170+ const stopAfter = 120 ;
171+ const writer = yield * Effect . forkChild (
172+ Effect . gen ( function * ( ) {
173+ for ( let index = 0 ; index < stopAfter ; index += 1 ) {
174+ yield * fs
175+ . remove ( path . join ( directory , "pi-t3-mcp-extension.ts" ) , { force : true } )
176+ . pipe ( Effect . ignore ) ;
177+ yield * Effect . all (
178+ [ materializePiT3McpExtension ( directory ) , materializePiT3McpExtension ( directory ) ] ,
179+ { concurrency : "unbounded" } ,
180+ ) ;
181+ }
182+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
183+ ) ;
184+ const reader = yield * Effect . forkChild (
185+ Effect . forever (
186+ Effect . gen ( function * ( ) {
187+ const text = yield * fs
188+ . readFileString ( path . join ( directory , "pi-t3-mcp-extension.ts" ) )
189+ . pipe ( Effect . option ) ;
190+ if ( Option . isNone ( text ) ) return ; // Missing between writes: legitimate.
191+ // A read that SUCCEEDS must return the full extension: an empty or
192+ // partial read means the writer truncated the file mid-write.
193+ if ( text . value . length === 0 ) {
194+ anomalies . push ( "empty: truncated mid-write" ) ;
195+ } else if ( text . value . length !== PI_T3_MCP_EXTENSION_SOURCE . length ) {
196+ anomalies . push (
197+ `partial: ${ text . value . length } of ${ PI_T3_MCP_EXTENSION_SOURCE . length } chars` ,
198+ ) ;
199+ }
200+ } ) ,
201+ ) ,
202+ ) ;
203+ yield * Fiber . join ( writer ) ;
204+ yield * Fiber . interrupt ( reader ) ;
205+ expect ( anomalies ) . toEqual ( [ ] ) ;
206+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
207+ ) ;
208+ } ) ;
0 commit comments