Skip to content

Commit 4884259

Browse files
fix(terminal): identify T3 Code terminal sessions
Adapted from lavindeep’s original proposal in [#8473](#8473). Preserves explicit per-terminal overrides and adds restart coverage.
1 parent e5a87e8 commit 4884259

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as TestClock from "effect/testing/TestClock";
2727
import { ChildProcessSpawner } from "effect/unstable/process";
2828
import { expect } from "vite-plus/test";
2929

30+
import packageJson from "../../package.json" with { type: "json" };
3031
import * as ProcessRunner from "../processRunner.ts";
3132
import * as TerminalManager from "./Manager.ts";
3233
import * as PtyAdapter from "./PtyAdapter.ts";
@@ -1712,6 +1713,39 @@ it.layer(
17121713
}),
17131714
);
17141715

1716+
it.effect.each([
1717+
{ name: "the parent has no identity", env: {} },
1718+
{
1719+
name: "the parent identifies a different terminal",
1720+
env: { TERM_PROGRAM: "Apple_Terminal", TERM_PROGRAM_VERSION: "2.14" },
1721+
},
1722+
] as const)("sets T3 Code terminal identity when $name", ({ env: parentEnv }) =>
1723+
Effect.gen(function* () {
1724+
const env = Object.freeze({ ...parentEnv });
1725+
const { manager, ptyAdapter } = yield* createManager(5, { env });
1726+
yield* manager.open(openInput());
1727+
1728+
expect(ptyAdapter.spawnInputs[0]?.env.TERM_PROGRAM).toBe("t3code");
1729+
expect(ptyAdapter.spawnInputs[0]?.env.TERM_PROGRAM_VERSION).toBe(packageJson.version);
1730+
expect(env).toEqual(parentEnv);
1731+
}),
1732+
);
1733+
1734+
it.effect("preserves partial and blank terminal identity overrides on restart", () =>
1735+
Effect.gen(function* () {
1736+
const { manager, ptyAdapter } = yield* createManager(5, {
1737+
env: { TERM_PROGRAM: "Apple_Terminal", TERM_PROGRAM_VERSION: "2.14" },
1738+
});
1739+
yield* manager.open(openInput({ env: { TERM_PROGRAM_VERSION: "custom-version" } }));
1740+
expect(ptyAdapter.spawnInputs[0]?.env.TERM_PROGRAM).toBe("t3code");
1741+
expect(ptyAdapter.spawnInputs[0]?.env.TERM_PROGRAM_VERSION).toBe("custom-version");
1742+
1743+
yield* manager.restart(restartInput({ env: { TERM_PROGRAM: "" } }));
1744+
expect(ptyAdapter.spawnInputs[1]?.env.TERM_PROGRAM).toBe("");
1745+
expect(ptyAdapter.spawnInputs[1]?.env.TERM_PROGRAM_VERSION).toBe(packageJson.version);
1746+
}),
1747+
);
1748+
17151749
it.effect("filters app runtime env variables from terminal sessions", () =>
17161750
Effect.gen(function* () {
17171751
const { manager, ptyAdapter } = yield* createManager(5, {
@@ -1809,6 +1843,8 @@ it.layer(
18091843
T3CODE_PROJECT_ROOT: "/repo",
18101844
T3CODE_WORKTREE_PATH: "/repo/worktree-a",
18111845
CUSTOM_FLAG: "1",
1846+
TERM_PROGRAM: "custom-terminal",
1847+
TERM_PROGRAM_VERSION: "custom-version",
18121848
},
18131849
}),
18141850
);
@@ -1819,6 +1855,8 @@ it.layer(
18191855
assert.equal(spawnInput.env.T3CODE_PROJECT_ROOT, "/repo");
18201856
assert.equal(spawnInput.env.T3CODE_WORKTREE_PATH, "/repo/worktree-a");
18211857
assert.equal(spawnInput.env.CUSTOM_FLAG, "1");
1858+
assert.equal(spawnInput.env.TERM_PROGRAM, "custom-terminal");
1859+
assert.equal(spawnInput.env.TERM_PROGRAM_VERSION, "custom-version");
18221860
}),
18231861
);
18241862

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import * as Scope from "effect/Scope";
5151
import * as Semaphore from "effect/Semaphore";
5252
import * as SynchronizedRef from "effect/SynchronizedRef";
5353

54+
import packageJson from "../../package.json" with { type: "json" };
5455
import * as ServerConfig from "../config.ts";
5556
import {
5657
increment,
@@ -1265,6 +1266,8 @@ function createTerminalSpawnEnv(
12651266
if (shouldExcludeTerminalEnvKey(key)) continue;
12661267
spawnEnv[key] = value;
12671268
}
1269+
spawnEnv.TERM_PROGRAM = "t3code";
1270+
spawnEnv.TERM_PROGRAM_VERSION = packageJson.version;
12681271
if (runtimeEnv) {
12691272
for (const [key, value] of Object.entries(runtimeEnv)) {
12701273
spawnEnv[key] = value;

0 commit comments

Comments
 (0)