Skip to content

Commit fe1f1a5

Browse files
ianrowe12claudemaria-rcks
authored
fix(acp): Devin terminals no longer inherit the launching thread's T3 MCP credentials (pingdotgg#26652)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 1c41c5e commit fe1f1a5

2 files changed

Lines changed: 69 additions & 17 deletions

File tree

‎packages/provider-acp/src/server/clientTerminals.test.ts‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,59 @@ describe("AcpClientTerminals", () => {
9999

100100
it.live("resolves terminal environment from the requesting ACP session", () =>
101101
Effect.gen(function* () {
102+
const inheritedEnvironment = yield* HostProcess.Environment;
102103
const terminals = yield* makeAcpClientTerminals({
103104
defaultCwd: process.cwd(),
104-
environmentForSession: (sessionId) => ({
105-
T3_TEST_ACP_SESSION_TOKEN: sessionId === "session-a" ? "token-a" : "token-b",
106-
}),
105+
environmentForSession: (sessionId) =>
106+
sessionId === "unknown"
107+
? undefined
108+
: {
109+
T3_ACP_MCP_AUTHORIZATION: sessionId === "session-a" ? "token-a" : "token-b",
110+
T3CODE_THREAD_ID: sessionId,
111+
},
107112
});
108113
yield* Effect.addFinalizer(() => terminals.disposeAll);
114+
inheritedEnvironment.T3_TEST_INHERITED = "refreshed";
109115

110116
for (const [sessionId, expected] of [
111-
["session-a", "token-a"],
112-
["session-b", "token-b"],
117+
["session-a", "token-a||session-a|refreshed|T3_TEST_OVERRIDE=request"],
118+
["session-b", "token-b||session-b|refreshed|T3_TEST_OVERRIDE=request"],
119+
["unknown", "|||refreshed|T3_TEST_OVERRIDE=inherited"],
113120
] as const) {
114121
const created = yield* terminals.create({
115122
sessionId,
116123
command: process.execPath,
117-
args: ["-e", "process.stdout.write(process.env.T3_TEST_ACP_SESSION_TOKEN ?? '')"],
124+
args: [
125+
"-e",
126+
"const env = process.env; process.stdout.write([env.T3_ACP_MCP_AUTHORIZATION ?? '', env.t3_Acp_Mcp_Endpoint ?? '', env.T3CODE_THREAD_ID ?? '', env.T3_TEST_INHERITED, ...Object.keys(env).filter((name) => name.toUpperCase() === 'T3_TEST_OVERRIDE').map((name) => name + '=' + env[name])].join('|'))",
127+
],
128+
...(sessionId === "unknown"
129+
? {}
130+
: {
131+
env: [
132+
{ name: "T3_TEST_OVERRIDE", value: "first" },
133+
{ name: "t3_Test_Override", value: "second" },
134+
{ name: "T3_TEST_OVERRIDE", value: "request" },
135+
],
136+
}),
118137
});
119138
yield* terminals.waitForExit({ sessionId, terminalId: created.terminalId });
120139
const output = yield* terminals.output({ sessionId, terminalId: created.terminalId });
121140
expect(output.output).toBe(expected);
122141
}
123-
}).pipe(Effect.provide(NodeServices.layer)),
142+
}).pipe(
143+
Effect.provideService(HostProcess.Environment, {
144+
...process.env,
145+
T3_ACP_MCP_AUTHORIZATION: "launching-thread-token",
146+
t3_Acp_Mcp_Endpoint: "launching-thread-endpoint",
147+
T3CODE_THREAD_ID: "launching-thread",
148+
T3_TEST_INHERITED: "kept",
149+
T3_TEST_OVERRIDE: "inherited",
150+
}),
151+
// Exercise Windows' case-insensitive variable names on every OS.
152+
Effect.provideService(HostProcess.Platform, "win32"),
153+
Effect.provide(NodeServices.layer),
154+
),
124155
);
125156

126157
it.live("runs Devin shell source with arguments, pipes, and session fallback variables", () =>

‎packages/provider-acp/src/server/clientTerminals.ts‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1212
import * as EffectAcpErrors from "effect-acp/errors";
1313
import type * as EffectAcpSchema from "effect-acp/compat";
1414
import * as NodeBuffer from "node:buffer";
15+
import * as HostProcess from "@t3tools/shared/HostProcess";
1516

1617
const MAX_LIVE_TERMINALS = 16;
1718
const MAX_UNRELEASED_TERMINALS = 64;
@@ -137,22 +138,38 @@ function acpTerminalCommand(input: {
137138
readonly request: EffectAcpSchema.CreateTerminalRequest;
138139
readonly shellCommands?: boolean | undefined;
139140
readonly defaultCwd: string;
140-
readonly environment?: NodeJS.ProcessEnv | undefined;
141+
readonly environment: NodeJS.ProcessEnv;
141142
readonly forceKillAfter: Duration.Input;
142143
}) {
143144
return ChildProcess.make(input.request.command, input.request.args ?? [], {
144145
cwd: input.request.cwd ?? input.defaultCwd,
145-
...(input.environment === undefined ? {} : { env: input.environment }),
146+
env: input.environment,
146147
shell: input.shellCommands === true && (input.request.args?.length ?? 0) === 0,
147148
forceKillAfter: input.forceKillAfter,
148149
});
149150
}
150151

152+
// Windows variable names are case-insensitive and Node passes only one spelling to the child,
153+
// so a later layer's `Path` must replace an earlier `PATH` rather than sit beside it.
154+
function mergeEnvironments(
155+
isWindows: boolean,
156+
layers: ReadonlyArray<Iterable<readonly [string, string | undefined]>>,
157+
) {
158+
const variables = new Map<string, readonly [string, string | undefined]>();
159+
for (const layer of layers) {
160+
for (const [name, value] of layer) {
161+
variables.set(isWindows ? name.toUpperCase() : name, [name, value]);
162+
}
163+
}
164+
return Object.fromEntries(variables.values());
165+
}
166+
151167
export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(function* (
152168
options: AcpClientTerminalsOptions,
153169
): Effect.fn.Return<AcpClientTerminals, never, ChildProcessSpawner.ChildProcessSpawner> {
154170
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
155171
const forceKillAfter = options.forceKillAfter ?? "5 seconds";
172+
const isWindows = yield* HostProcess.isWindows;
156173
const creationPermit = yield* Semaphore.make(1);
157174
const terminals = new Map<string, ManagedAcpTerminal>();
158175
let nextTerminalNumber = 1;
@@ -215,16 +232,20 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
215232
`ACP terminal/create exceeded the limit of ${MAX_LIVE_TERMINALS} concurrent terminals.`,
216233
);
217234
}
218-
const requestEnvironment = Object.fromEntries(
219-
(request.env ?? []).map((variable) => [variable.name, variable.value] as const),
235+
// Thread credentials and identity must come from the requesting session.
236+
const inheritedEnvironment = Object.fromEntries(
237+
Object.entries(options.environment ?? (yield* HostProcess.Environment)).filter(
238+
([name]) =>
239+
!name.toUpperCase().startsWith("T3_ACP_MCP_") &&
240+
name.toUpperCase() !== "T3CODE_THREAD_ID",
241+
),
220242
);
221243
const sessionEnvironment = options.environmentForSession?.(request.sessionId);
222-
const environment =
223-
options.environment === undefined &&
224-
sessionEnvironment === undefined &&
225-
(request.env?.length ?? 0) === 0
226-
? undefined
227-
: { ...options.environment, ...sessionEnvironment, ...requestEnvironment };
244+
const environment = mergeEnvironments(isWindows, [
245+
Object.entries(inheritedEnvironment),
246+
Object.entries(sessionEnvironment ?? {}),
247+
(request.env ?? []).map((variable) => [variable.name, variable.value] as const),
248+
]);
228249
const terminalScope = yield* Scope.make();
229250
const child = yield* restore(
230251
spawner

0 commit comments

Comments
 (0)