Skip to content

Commit ab597e5

Browse files
committed
fix(server): keep prompt overrides current and isolate Pi revisions
1 parent 09b46df commit ab597e5

6 files changed

Lines changed: 82 additions & 28 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ServerSettings from "../../serverSettings.ts";
12
import { historyResponseItems } from "@t3tools/provider-core/server/handoffBudget";
23
import {
34
makeProviderTextDeltaCoalescer,
@@ -1886,6 +1887,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
18861887
it.effect("preserves T3 context on the wire and restores it after compaction", () =>
18871888
Effect.scoped(
18881889
Effect.gen(function* () {
1890+
const instructionOverrides = { visuals: "Return the published artifact link." };
18891891
const nativeThreadId = "context-thread";
18901892
const nativeTurnId = "context-turn";
18911893
const params = yield* CodexAdapterV2.buildCodexTurnStartParams({
@@ -1894,6 +1896,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
18941896
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
18951897
modelSelection: CODEX_TEST_MODEL_SELECTION,
18961898
hasT3Mcp: true,
1899+
instructionOverrides,
18971900
});
18981901
assert.include(
18991902
params.additionalContext?.t3_code_orchestration_01?.value ?? "",
@@ -1953,6 +1956,9 @@ describe("CodexAdapterV2 post-settle continuation", () => {
19531956
],
19541957
});
19551958
const harness = yield* makeCodexReplayHarness(transcript);
1959+
yield* (yield* ServerSettings.ServerSettingsService).updateSettings({
1960+
agentInstructionOverrides: instructionOverrides,
1961+
});
19561962
yield* (yield* McpProviderSessions.McpProviderSessions).set({
19571963
environmentId: EnvironmentId.make("test"),
19581964
threadId: harness.threadId,
@@ -1975,7 +1981,12 @@ describe("CodexAdapterV2 post-settle continuation", () => {
19751981
assert.equal(harness.terminalEvents()[0]?.status, "completed");
19761982
}).pipe(
19771983
Effect.provide(
1978-
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
1984+
Layer.mergeAll(
1985+
IdAllocator.layer,
1986+
McpProviderSessions.layer,
1987+
NodeServices.layer,
1988+
ServerSettings.ServerSettingsService.layerTest(),
1989+
),
19791990
),
19801991
),
19811992
),

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,9 +1637,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
16371637
}),
16381638
openSession: Effect.fn("CodexAdapterV2.openSession")(
16391639
function* (input) {
1640-
const instructionOverrides = Option.isSome(serverSettings)
1641-
? (yield* serverSettings.value.getSettings).agentInstructionOverrides
1642-
: null;
16431640
const scope = yield* Scope.Scope;
16441641
const resolvedRuntime =
16451642
adapterOptions.resolveRuntime === undefined
@@ -6488,6 +6485,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
64886485
) {
64896486
const threadId = yield* getNativeThreadId(turnInput.providerThread);
64906487
const mcpSession = yield* mcpSessions.read(turnInput.threadId);
6488+
const instructionOverrides = Option.isSome(serverSettings)
6489+
? (yield* serverSettings.value.getSettings).agentInstructionOverrides
6490+
: null;
64916491
const turnStartParams = yield* buildCodexTurnStartParams({
64926492
nativeThreadId: threadId,
64936493
codexInput,

‎docs/user/html-renders.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ The available sections are `orchestration`, `visuals`, `browser`, and `devices`.
2929
T3's visual-delivery and preview-usage guidance; `orchestration` covers coordination with other
3030
threads and agents. A string replaces that section, an empty string omits it, and `null` restores
3131
its built-in text. Set `agentInstructionOverrides` itself to `null` to restore every section.
32+
Each override applies where T3 already sends that section; it does not add sections to providers
33+
that do not receive them.
3234
Overrides affect guidance, not tool availability or permissions. Other instructions, including
3335
provider-native rules and MCP tool descriptions, still apply.
3436

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as Crypto from "effect/Crypto";
2+
import * as Path from "effect/Path";
13
import * as HostProcess from "@t3tools/shared/HostProcess";
24
import { AgentScope } from "@t3tools/shared/AgentScope";
35
import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
@@ -414,6 +416,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
414416
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
415417
const agentScope = yield* AgentScope;
416418
const fileSystem = yield* FileSystem.FileSystem;
419+
const crypto = yield* Crypto.Crypto;
420+
const path = yield* Path.Path;
417421
const idAllocator = yield* IdAllocator.IdAllocatorV2;
418422
const host = yield* ProviderHost.ProviderHost;
419423
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
@@ -451,21 +455,22 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
451455
const scope = yield* Effect.scope;
452456
const cwd = input.runtimePolicy.cwd ?? host.paths.cwd;
453457
const mcpSession = yield* mcpSessions.read(input.threadId);
454-
const provideCacheFs = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem>) =>
455-
effect.pipe(
456-
Effect.provideService(FileSystem.FileSystem, fileSystem),
457-
Effect.mapError(
458-
(cause) =>
459-
new ProviderAdapter.ProviderAdapterOpenSessionError({
460-
driver: PI_PROVIDER,
461-
providerSessionId: input.providerSessionId,
462-
cause,
463-
}),
464-
),
465-
);
466458
// Always inject the permission hook even without MCP credentials; otherwise Supervised becomes unrestricted.
467-
const extensionPath = yield* provideCacheFs(
468-
materializePiT3McpExtension(host.paths.providerStatusCacheDir, instructionOverrides),
459+
const extensionPath = yield* materializePiT3McpExtension(
460+
host.paths.providerStatusCacheDir,
461+
instructionOverrides,
462+
).pipe(
463+
Effect.provideService(FileSystem.FileSystem, fileSystem),
464+
Effect.provideService(Crypto.Crypto, crypto),
465+
Effect.provideService(Path.Path, path),
466+
Effect.mapError(
467+
(cause) =>
468+
new ProviderAdapter.ProviderAdapterOpenSessionError({
469+
driver: PI_PROVIDER,
470+
providerSessionId: input.providerSessionId,
471+
cause,
472+
}),
473+
),
469474
);
470475
const resolvedLaunchArgs = resolvePiLaunchArgs(options.settings.launchArgs);
471476
if (!resolvedLaunchArgs.ok) {
@@ -3312,6 +3317,8 @@ function piUiResponse(
33123317
}
33133318

33143319
export type PiAdapterV2DriverEnv =
3320+
| Crypto.Crypto
3321+
| Path.Path
33153322
| ChildProcessSpawner.ChildProcessSpawner
33163323
| FileSystem.FileSystem
33173324
| IdAllocator.IdAllocatorV2

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import * as Effect from "effect/Effect";
3+
import * as FileSystem from "effect/FileSystem";
14
import { assert, describe, it } from "@effect/vitest";
25
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
36

47
import {
8+
buildPiT3McpExtensionSource,
59
T3_MCP_BEARER_ENV,
610
T3_MCP_URL_ENV,
711
T3_PI_RUNTIME_MODE_ENV,
812
T3_PI_MCP_EXTENSION_PATH_ENV,
913
} from "./mcpExtensionSource.ts";
10-
import { buildPiRpcLaunch, resolvePiLaunchArgs } from "./mcpInjection.ts";
14+
import {
15+
materializePiT3McpExtension,
16+
buildPiRpcLaunch,
17+
resolvePiLaunchArgs,
18+
} from "./mcpInjection.ts";
1119

1220
const threadId = ThreadId.make("thread-pi-t3-mcp");
1321

@@ -134,3 +142,28 @@ describe("pi T3 MCP injection", () => {
134142
assert.isTrue(resolvePiLaunchArgs("--model deepseek/v4").ok);
135143
});
136144
});
145+
146+
it.effect("keeps concurrently published Pi guidance revisions isolated and reusable", () =>
147+
Effect.gen(function* () {
148+
const fs = yield* FileSystem.FileSystem;
149+
const cacheDir = yield* fs.makeTempDirectoryScoped();
150+
const first = { visuals: "Publish the document and return a link." };
151+
const second = { visuals: "Show the comparison in chat." };
152+
const overrides = [first, second, first];
153+
const paths = yield* Effect.forEach(
154+
overrides,
155+
(value) => materializePiT3McpExtension(cacheDir, value),
156+
{ concurrency: "unbounded" },
157+
);
158+
assert.notEqual(paths[0], paths[1]);
159+
assert.equal(paths[0], paths[2]);
160+
for (const [index, filePath] of paths.entries()) {
161+
assert.equal(
162+
yield* fs.readFileString(filePath),
163+
buildPiT3McpExtensionSource(overrides[index]),
164+
);
165+
}
166+
assert.equal(yield* materializePiT3McpExtension(cacheDir, first), paths[0]);
167+
assert.equal(yield* fs.readFileString(paths[1]!), buildPiT3McpExtensionSource(second));
168+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
169+
);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as Crypto from "effect/Crypto";
2+
import * as Hex from "effect/encoding/Hex";
3+
import * as Path from "effect/Path";
4+
import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
15
import type { AgentInstructionOverrides } from "@t3tools/contracts";
26
import { tokenizeCliArgs } from "@t3tools/shared/cliArgs";
37
import * as Effect from "effect/Effect";
@@ -221,21 +225,18 @@ function withoutToolSelectionArgs(args: ReadonlyArray<string>): ReadonlyArray<st
221225
return filtered;
222226
}
223227

224-
function piT3McpExtensionDestPath(cacheDir: string): string {
225-
return `${cacheDir.replace(/\\/g, "/")}/${PI_T3_MCP_EXTENSION_FILENAME}`;
226-
}
227-
228228
export const materializePiT3McpExtension = Effect.fn("materializePiT3McpExtension")(function* (
229229
cacheDir: string,
230230
overrides?: AgentInstructionOverrides | null,
231231
) {
232232
const fs = yield* FileSystem.FileSystem;
233-
yield* fs.makeDirectory(cacheDir, { recursive: true });
234-
const dest = piT3McpExtensionDestPath(cacheDir);
235-
const existing = yield* fs.readFileString(dest).pipe(Effect.orElseSucceed(() => ""));
233+
const path = yield* Path.Path;
234+
const crypto = yield* Crypto.Crypto;
236235
const source = buildPiT3McpExtensionSource(overrides);
237-
if (existing !== source) {
238-
yield* fs.writeFileString(dest, source);
236+
const revision = Hex.encode(yield* crypto.digest("SHA-256", new TextEncoder().encode(source)));
237+
const dest = path.join(cacheDir, "pi-mcp", revision, PI_T3_MCP_EXTENSION_FILENAME);
238+
if (!(yield* fs.exists(dest))) {
239+
yield* writeFileStringAtomically({ filePath: dest, contents: source });
239240
}
240241
return dest;
241242
});

0 commit comments

Comments
 (0)