The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 02:00 UTC
fix(pi): the T3 MCP extension file is written atomically (#32231)
Co-authored-by: maria-rcks <maria@kuuro.net>
Rishabh_t106 committed
Nov 7, 2026 at 12:08 UTC
257e4073c1c51dcae291ecc7ca482dc66900f961
2 files changed
+73
-3
packages/provider-pi/src/server/mcpInjection.test.ts
+63
-2
@@ -1,4 +1,4 @@
1
-import { assert, describe, it } from "@effect/vitest";
1
+import { assert, describe, expect, it } from "@effect/vitest";
2
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
3
4
import {
@@ -7,7 +7,18 @@ import {
7
T3_PI_RUNTIME_MODE_ENV,
8
T3_PI_MCP_EXTENSION_PATH_ENV,
9
} 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";
22
23
const threadId = ThreadId.make("thread-pi-t3-mcp");
24
@@ -145,3 +156,53 @@ describe("pi T3 MCP injection", () => {
156
assert.isTrue(resolvePiLaunchArgs("--model deepseek/v4").ok);
157
});
158
});
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
+10
-1
@@ -232,7 +232,16 @@ export const materializePiT3McpExtension = Effect.fn("materializePiT3McpExtensio
232
const dest = piT3McpExtensionDestPath(cacheDir);
233
const existing = yield* fs.readFileString(dest).pipe(Effect.orElseSucceed(() => ""));
234
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
+ );
245
}
246
return dest;
247
});