Skip to content

Commit a625e0b

Browse files
fix(terminal): identify T3 Code terminal sessions (#9954)
Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 3ed5fb2 commit a625e0b

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import * as ProjectSetupScriptRunner from "../project/ProjectSetupScriptRunner.t
4141
import * as TerminalManager from "./Manager.ts";
4242
import { terminalOutputStream } from "./OutputProtocol.ts";
4343
import * as PtyAdapter from "@t3tools/shared/PtyAdapter";
44+
import packageJson from "../../package.json" with { type: "json" };
4445

4546
vi.mock("effect/SynchronizedRef", async (importOriginal) => {
4647
const original = await importOriginal<typeof SynchronizedRef>();
@@ -1853,6 +1854,51 @@ it.layer(
18531854
}),
18541855
);
18551856

1857+
it.effect.each([
1858+
{
1859+
platform: "linux",
1860+
parentEnv: { TERM_PROGRAM: "Apple_Terminal", TERM_PROGRAM_VERSION: "455" },
1861+
runtimeEnv: { TERM_PROGRAM: "", TERM_PROGRAM_VERSION: "custom-version" },
1862+
},
1863+
{
1864+
platform: "win32",
1865+
parentEnv: { term_program: "Apple_Terminal", Term_Program_Version: "455" },
1866+
runtimeEnv: { Term_Program: "", term_program_version: "custom-version" },
1867+
},
1868+
] as const)(
1869+
"identifies as T3 Code unless the terminal env overrides it on $platform",
1870+
({ platform, parentEnv, runtimeEnv }) =>
1871+
Effect.gen(function* () {
1872+
const originalEnv = { ...parentEnv };
1873+
const env = Object.freeze(parentEnv);
1874+
const { manager, ptyAdapter } = yield* createManager(5, { env }).pipe(
1875+
Effect.provide(layerWithHostPlatform(platform)),
1876+
);
1877+
1878+
yield* manager.open(openInput());
1879+
yield* manager.open(openInput({ terminalId: "override", env: runtimeEnv }));
1880+
1881+
expect(ptyAdapter.spawnInputs).toHaveLength(2);
1882+
for (const [index, expected] of [
1883+
{ TERM_PROGRAM: "t3code", TERM_PROGRAM_VERSION: packageJson.version },
1884+
{ TERM_PROGRAM: "", TERM_PROGRAM_VERSION: "custom-version" },
1885+
].entries()) {
1886+
const spawnEnv = ptyAdapter.spawnInputs[index]!.env;
1887+
for (const [key, value] of Object.entries(expected)) {
1888+
expect
1889+
.soft(
1890+
Object.entries(spawnEnv)
1891+
.filter(([candidate]) => candidate.toUpperCase() === key)
1892+
.map(([, entryValue]) => entryValue),
1893+
`${index === 0 ? "default" : "override"} ${key}`,
1894+
)
1895+
.toEqual([value]);
1896+
}
1897+
}
1898+
expect(env).toEqual(originalEnv);
1899+
}),
1900+
);
1901+
18561902
it.effect("filters app runtime env variables from terminal sessions", () =>
18571903
Effect.gen(function* () {
18581904
const { manager, ptyAdapter } = yield* createManager(5, {

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import * as Scope from "effect/Scope";
6262
import * as Stream from "effect/Stream";
6363
import * as SynchronizedRef from "effect/SynchronizedRef";
6464

65+
import packageJson from "../../package.json" with { type: "json" };
6566
import * as ServerConfig from "../config.ts";
6667
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
6768
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
@@ -1363,8 +1364,16 @@ function createTerminalSpawnEnv(
13631364
for (const [key, value] of Object.entries(baseEnv)) {
13641365
if (value === undefined) continue;
13651366
if (shouldExcludeTerminalEnvKey(key)) continue;
1367+
if (platform === "win32") {
1368+
const normalizedKey = key.toUpperCase();
1369+
if (normalizedKey === "TERM_PROGRAM" || normalizedKey === "TERM_PROGRAM_VERSION") continue;
1370+
}
13661371
spawnEnv[key] = value;
13671372
}
1373+
// The server inherits its launcher's identity (e.g. Apple_Terminal); shells
1374+
// and tools inside the integrated terminal should see T3 Code instead.
1375+
spawnEnv.TERM_PROGRAM = "t3code";
1376+
spawnEnv.TERM_PROGRAM_VERSION = packageJson.version;
13681377
if (runtimeEnv) {
13691378
for (const [key, value] of Object.entries(runtimeEnv)) {
13701379
const existingKey =

0 commit comments

Comments
 (0)