Skip to content

Commit 257e407

Browse files
fix(pi): the T3 MCP extension file is written atomically (#32231)
Co-authored-by: maria-rcks <[email protected]>
1 parent 984fe2d commit 257e407

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

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

Lines changed: 63 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,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

1223
const 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+
});

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,16 @@ 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: stage it in a per-call directory
236+
// and rename so readers and other writers never see a partial file.
237+
yield* Effect.scoped(
238+
Effect.gen(function* () {
239+
const stagingDir = yield* fs.makeTempDirectoryScoped({ directory: cacheDir });
240+
const staging = `${stagingDir.replace(/\\/g, "/")}/${PI_T3_MCP_EXTENSION_FILENAME}`;
241+
yield* fs.writeFileString(staging, PI_T3_MCP_EXTENSION_SOURCE);
242+
yield* fs.rename(staging, dest).pipe(Effect.retry({ times: 3 }));
243+
}),
244+
);
236245
}
237246
return dest;
238247
});

0 commit comments

Comments
 (0)