Skip to content

Commit b6f506e

Browse files
committed
fix(pi): the T3 MCP extension file is written atomically
materializePiT3McpExtension wrote the extension directly to the shared cache path: the write truncates the file first, so a pi process loading the extension while the write is in flight reads an empty or partial file and starts with a broken extension. Write to a sibling staging file and rename into place - the rename is atomic, so a reader sees the previous file or the complete new one, never the truncated state. The committed regression test runs a concurrent reader against repeated materializations and fails on main with 'empty: truncated mid-write'.
1 parent b90e554 commit b6f506e

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎packages/provider-pi/src/server/mcpInjection.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, describe, it } from "@effect/vitest";
1+
import { assert, describe, expect, it } from "@effect/vitest";
22
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
33

44
import {
@@ -7,7 +7,14 @@ 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 { buildPiRpcLaunch, materializePiT3McpExtension, resolvePiLaunchArgs } from "./mcpInjection.ts";
17+
import { PI_T3_MCP_EXTENSION_SOURCE } from "./mcpExtensionSource.ts";
1118

1219
const threadId = ThreadId.make("thread-pi-t3-mcp");
1320

@@ -145,3 +152,49 @@ describe("pi T3 MCP injection", () => {
145152
assert.isTrue(resolvePiLaunchArgs("--model deepseek/v4").ok);
146153
});
147154
});
155+
156+
describe("materializePiT3McpExtension", () => {
157+
// A pi launch concurrent with the materialize reads the extension file from
158+
// disk; a truncate-then-write exposes an empty or partial file mid-write and
159+
// the launch loads a broken extension.
160+
it.live("never exposes a torn extension file to a concurrent reader", () =>
161+
Effect.gen(function* () {
162+
const fs = yield* NodeFS.FileSystem;
163+
const path = yield* NodePath.Path;
164+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pi-atomic-" });
165+
const anomalies: Array<string> = [];
166+
const stopAfter = 120;
167+
const writer = yield* Effect.forkChild(
168+
Effect.gen(function* () {
169+
for (let index = 0; index < stopAfter; index += 1) {
170+
yield* fs.remove(
171+
path.join(directory, "pi-t3-mcp-extension.ts"),
172+
{ force: true },
173+
).pipe(Effect.ignore);
174+
yield* materializePiT3McpExtension(directory);
175+
}
176+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
177+
);
178+
const reader = yield* Effect.forkChild(
179+
Effect.forever(
180+
Effect.gen(function* () {
181+
const text = yield* fs.readFileString(
182+
path.join(directory, "pi-t3-mcp-extension.ts"),
183+
).pipe(Effect.option);
184+
if (Option.isNone(text)) return; // Missing between writes: legitimate.
185+
// A read that SUCCEEDS must return the full extension: an empty or
186+
// partial read means the writer truncated the file mid-write.
187+
if (text.value.length === 0) {
188+
anomalies.push("empty: truncated mid-write");
189+
} else if (text.value.length !== PI_T3_MCP_EXTENSION_SOURCE.length) {
190+
anomalies.push(`partial: ${text.value.length} of ${PI_T3_MCP_EXTENSION_SOURCE.length} chars`);
191+
}
192+
}),
193+
),
194+
);
195+
yield* Fiber.join(writer);
196+
yield* Fiber.interrupt(reader);
197+
expect(anomalies).toEqual([]);
198+
}).pipe(Effect.provide(NodeServices.layer)),
199+
);
200+
});

‎packages/provider-pi/src/server/mcpInjection.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ export const materializePiT3McpExtension = Effect.fn("materializePiT3McpExtensio
232232
const dest = piT3McpExtensionDestPath(cacheDir);
233233
const existing = yield* fs.readFileString(dest).pipe(Effect.orElseSucceed(() => ""));
234234
if (existing !== PI_T3_MCP_EXTENSION_SOURCE) {
235-
yield* fs.writeFileString(dest, PI_T3_MCP_EXTENSION_SOURCE);
235+
// Concurrent pi launches read this file: write to a sibling and rename so
236+
// a reader never observes the truncated mid-write state.
237+
const staging = `${dest}.materializing`;
238+
yield* fs.writeFileString(staging, PI_T3_MCP_EXTENSION_SOURCE);
239+
yield* fs.rename(staging, dest).pipe(Effect.retry({ times: 3 }));
236240
}
237241
return dest;
238242
});

0 commit comments

Comments
 (0)