dsh-bash-terminal-ts 0.8.7 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.de.md +152 -0
  2. package/CHANGELOG.es.md +152 -0
  3. package/CHANGELOG.fr.md +152 -0
  4. package/CHANGELOG.it.md +152 -0
  5. package/CHANGELOG.md +201 -0
  6. package/CHANGELOG.ru.md +152 -0
  7. package/README.de.md +1 -1
  8. package/README.en.md +3 -2
  9. package/README.es.md +1 -1
  10. package/README.it.md +1 -1
  11. package/README.md +4 -3
  12. package/agent-presets/README.md +171 -170
  13. package/agent-presets/install-presets.mjs +180 -179
  14. package/agent-presets/standard-win-niu/agent.cordis.yml +271 -0
  15. package/agent-presets/standard-win-niu/preset.yml +2 -0
  16. package/cordis.patch.yml +84 -83
  17. package/dist/client.core.js +10 -1
  18. package/dist/client.js +10 -1
  19. package/docs/dsh-plugin-development.md +169 -0
  20. package/docs/niu-backend.md +197 -0
  21. package/dsh.plugin.json +22 -22
  22. package/lib/client.js +10 -1
  23. package/lib/dsh-types.d.ts +14 -2
  24. package/lib/index.d.ts +15 -2
  25. package/lib/index.js +43 -5
  26. package/lib/locales.js +9 -0
  27. package/lib/shell-id.d.ts +1 -1
  28. package/lib/shells.d.ts +1 -0
  29. package/lib/shells.js +8 -2
  30. package/lib/terminal.js +4 -0
  31. package/lib/webterminals/host.js +2 -2
  32. package/lib/webterminals/mount.js +1 -0
  33. package/locale/de.json +6 -4
  34. package/locale/en.json +6 -4
  35. package/locale/es.json +6 -4
  36. package/locale/fr.json +6 -4
  37. package/locale/it.json +6 -4
  38. package/locale/ja.json +6 -4
  39. package/locale/ko.json +6 -4
  40. package/locale/ru.json +6 -4
  41. package/locale/zh.json +6 -4
  42. package/package.json +25 -11
  43. package/scripts/check-artifacts.mjs +96 -93
  44. package/src/client.tsx +685 -685
  45. package/src/dsh-types.ts +332 -320
  46. package/src/index.ts +1316 -1275
  47. package/src/locales.ts +461 -452
  48. package/src/shell-id.ts +11 -11
  49. package/src/shells.ts +279 -272
  50. package/src/terminal.ts +616 -612
  51. package/src/webterminals/host.ts +361 -361
  52. package/src/webterminals/mount.ts +237 -236
package/src/shell-id.ts CHANGED
@@ -1,11 +1,11 @@
1
- // The shell identifier, in its own module on purpose.
2
- //
3
- // `shells.ts` needs `NodeJS.Platform` and the resolved-path types, so it belongs
4
- // to the host compile graph only. The browser half still has to name the same
5
- // four ids (in the socket frames and the catalog rows it renders), so the id type
6
- // lives here - a module with no Node references at all, which is what lets it sit
7
- // in both tsconfig graphs without dragging `@types/node` into the client purity
8
- // gate.
9
-
10
- /** The five backends, in catalog order (`payload` = the pinned POSIX runtime). */
11
- export type ShellId = "powershell" | "gitbash" | "msys2" | "wsl" | "payload";
1
+ // The shell identifier, in its own module on purpose.
2
+ //
3
+ // `shells.ts` needs `NodeJS.Platform` and the resolved-path types, so it belongs
4
+ // to the host compile graph only. The browser half still has to name the same
5
+ // four ids (in the socket frames and the catalog rows it renders), so the id type
6
+ // lives here - a module with no Node references at all, which is what lets it sit
7
+ // in both tsconfig graphs without dragging `@types/node` into the client purity
8
+ // gate.
9
+
10
+ /** The five backends, in catalog order (`payload` = the pinned POSIX runtime). */
11
+ export type ShellId = "powershell" | "gitbash" | "msys2" | "wsl" | "payload" | "niu";
package/src/shells.ts CHANGED
@@ -1,272 +1,279 @@
1
- // dsh-bash-terminal-ts: the shell catalog for the browser-side terminal.
2
- //
3
- // Why this file exists separately from `buildArgv` / `terminalArgv` / `buildEnv`
4
- // in index.ts and terminal.ts: those three serve the MODEL tool, where a dumb
5
- // terminal and a stripped environment are correct (no colour, no pager, one
6
- // command per call). The browser terminal is the opposite contract - a real
7
- // xterm emulator driven by a human who expects their profile, their prompt and
8
- // their colours. Sharing the tool's argv/env would hand the user a shell with
9
- // `TERM=dumb`, `NO_COLOR=1` and `-NoProfile`.
10
- //
11
- // So this module owns the UI-facing rules and REUSES the parts that are
12
- // genuinely shared: the executable candidate lists (injected by index.ts, which
13
- // keeps the dependency one-way and this file unit-testable), the MSYS2 `MSYSTEM`
14
- // rule, and the WSL interactive argv shape.
15
- //
16
- // Everything here is pure: no fs, no child_process, no module-level cache. The
17
- // caller injects `fsProbe` / `distros` and owns the TTL.
18
-
19
- import type { ResolvedPaths } from "./dsh-types.js";
20
- import type { ShellId } from "./shell-id.js";
21
-
22
- /** Re-exported so existing importers keep working (see shell-id.ts for why). */
23
- export type { ShellId };
24
-
25
- /** Catalog order = fallback order: powershell is BOTH first choice and floor. The
26
- * pinned POSIX payload sits last: it only earns a seat when the machine's own
27
- * shells are absent, and a detected WSL distro vetoes it before that. */
28
- export const SHELL_PRIORITY: readonly ShellId[] = ["powershell", "gitbash", "msys2", "wsl", "payload"];
29
-
30
- /** Why one backend is not launchable (drives the settings-row badge). */
31
- export type ShellUnavailable = "installed" | "not-installed" | "no-distro" | "launcher-only" | "wsl-preferred";
32
-
33
- /** One catalog row: what the UI shows and what the host would spawn. */
34
- export interface ShellEntry {
35
- readonly id: ShellId;
36
- /** Display label (a catalog constant, never inferred from the file name). */
37
- readonly label: string;
38
- /** Resolved executable, absent when the backend is not usable. */
39
- readonly path?: string;
40
- readonly available: boolean;
41
- readonly reason: ShellUnavailable;
42
- /** The activation switch state (powershell is always true: the floor). */
43
- readonly activated: boolean;
44
- /** False only for powershell, which the user cannot turn off. */
45
- readonly togglable: boolean;
46
- /** Interactive argv, present only when `available`. */
47
- readonly argv?: readonly string[];
48
- }
49
-
50
- /** The activation switches as persisted in the plugin config. */
51
- export interface ShellSwitches {
52
- gitbash: boolean;
53
- msys2: boolean;
54
- wsl: boolean;
55
- payload: boolean;
56
- }
57
-
58
- /** Everything the catalog builder needs from the outside world. */
59
- export interface CatalogFacts {
60
- /** Resolved executables (config override or candidate-list winner). */
61
- paths: ResolvedPaths;
62
- /** Whether one path is a launchable file. */
63
- fsProbe(path: string): boolean;
64
- /** `wsl -l -q` output. `undefined` means the probe could not answer (a timeout
65
- * is not evidence of absence); `[]` means it answered "no distro installed". */
66
- readonly distros: readonly string[] | undefined;
67
- switches: ShellSwitches;
68
- /** WSL distro override from config ('' = the WSL default). */
69
- wslDistro: string;
70
- /**
71
- * True when the payload gate was vetoed by a positively-detected WSL distro
72
- * (a real Linux bash outranks the MSYS payload). It only colors the payload
73
- * row's badge when no payload path resolved - "why" information, not a gate.
74
- */
75
- payloadSuppressed?: boolean;
76
- }
77
-
78
- /** Display labels. The catalog id/label pair is DECIDED BY THIS TABLE. */
79
- const LABELS: Record<ShellId, string> = {
80
- powershell: "PowerShell",
81
- gitbash: "Git Bash",
82
- msys2: "MSYS2",
83
- wsl: "WSL bash",
84
- payload: "POSIX payload",
85
- };
86
-
87
- /**
88
- * `<SystemRoot>\System32\bash.exe` (and the sysnative mirror) is the deprecated
89
- * WSL forwarder, not a shell. `candidateGitBashPaths` already skips it; this
90
- * guard protects the second way a path reaches the catalog - a user-supplied
91
- * `gitBashPath` / `msys2Path` config value.
92
- */
93
- export function isWslLegacyBash(path: string): boolean {
94
- const stem = /([^\\/]+)\.exe$/i.exec(path)?.[1]?.toLowerCase();
95
- if (stem !== "bash") return false;
96
- return /\\(windows\\system32|sysnative)\\bash\.exe$/i.test(path.replace(/\//g, "\\"));
97
- }
98
-
99
- /**
100
- * Console-allocating launchers: `git-bash.exe`, `msys2.exe`, `msys2_shell.cmd`.
101
- * They open their own window and return zero bytes on a piped stdio (measured
102
- * against MSYS2 with bash 5.3.15), so they can never host an embedded terminal.
103
- */
104
- export function isConsoleLauncher(path: string): boolean {
105
- const name = path.slice(Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1).toLowerCase();
106
- return name === "git-bash.exe" || name === "msys2.exe" || name === "msys2_shell.cmd";
107
- }
108
-
109
- /**
110
- * Interactive argv for the BROWSER terminal.
111
- *
112
- * Differences from the tool's `terminalArgv`: PowerShell keeps its profile (the
113
- * user's aliases and prompt are the point of a UI terminal), and the three bash
114
- * families are unchanged because those rules are about the shell itself:
115
- * Git Bash needs `-i`, MSYS2 needs `-l` and NEVER `-lc` (this argv carries no
116
- * command, and `bash -lc` with no operand exits 2), WSL needs `-- bash -i` on the
117
- * default distro (bare `-e` fails under ConPTY with RPC 0x8007072c).
118
- */
119
- export function uiArgv(shell: ShellId, paths: ResolvedPaths, wslDistro = ""): string[] {
120
- switch (shell) {
121
- case "powershell":
122
- return [requirePath(paths.pwsh, shell), "-NoLogo"];
123
- case "gitbash":
124
- return [requirePath(paths.gitbash, shell), "-i"];
125
- case "msys2":
126
- return [requirePath(paths.msys2, shell), "-l"];
127
- case "payload":
128
- // The payload is a Git-for-Windows tree: the interactive rules are the
129
- // gitbash ones (an interactive non-login shell; its /etc still works).
130
- return [requirePath(paths.payload, shell), "-i"];
131
- case "wsl": {
132
- const distro = wslDistro.trim();
133
- return distro.length > 0
134
- ? [requirePath(paths.wsl, shell), "-d", distro, "-e", "bash", "-i"]
135
- : [requirePath(paths.wsl, shell), "--", "bash", "-i"];
136
- }
137
- }
138
- }
139
-
140
- /**
141
- * Environment for the BROWSER terminal: the host process environment plus a real
142
- * terminal type. `buildEnv` is deliberately NOT reused - it layers
143
- * `TERM=dumb`/`NO_COLOR=1`/`PAGER=cat` over an agent-scoped map, which is right
144
- * for a one-shot tool call and wrong for an interactive screen.
145
- *
146
- * The one rule carried over is MSYS2's `MSYSTEM`: without it MSYS2 starts in the
147
- * bare MSYS environment and `/etc/profile` never puts `/mingw64/bin` on PATH, so
148
- * gcc/make are missing in the very shell the user chose to get them from. An
149
- * explicit value in the base environment wins, exactly as in `buildEnv`.
150
- */
151
- export function uiEnv(shell: ShellId, baseEnv: NodeJS.ProcessEnv): Record<string, string | undefined> {
152
- const env: Record<string, string | undefined> = { ...baseEnv };
153
- // A UI terminal is an xterm: the tool's `dumb` would kill colours, cursor
154
- // control and full-screen programs.
155
- env.TERM = "xterm-256color";
156
- if (shell === "msys2" && env.MSYSTEM === undefined) env.MSYSTEM = "MINGW64";
157
- return env;
158
- }
159
-
160
- function requirePath(path: string | undefined, shell: ShellId): string {
161
- if (path === undefined || path.length === 0) {
162
- throw new Error(`terminal: ${shell} backend unavailable - no executable resolved`);
163
- }
164
- return path;
165
- }
166
-
167
- /** One row's availability, given the injected facts. */
168
- function probe(id: ShellId, facts: CatalogFacts): { available: boolean; reason: ShellUnavailable; path?: string } {
169
- const path =
170
- id === "powershell" ? facts.paths.pwsh
171
- : id === "gitbash" ? facts.paths.gitbash
172
- : id === "msys2" ? facts.paths.msys2
173
- : id === "payload" ? facts.paths.payload
174
- : facts.paths.wsl;
175
- if (path === undefined || path.length === 0) {
176
- if (id === "payload" && facts.payloadSuppressed === true) return { available: false, reason: "wsl-preferred" };
177
- return { available: false, reason: "not-installed" };
178
- }
179
- // A configured path is honoured, but a launcher is not a shell: trusting the
180
- // config here is how `msys2.exe` ends up in the menu and returns no output.
181
- if (isConsoleLauncher(path)) return { available: false, reason: "launcher-only", path };
182
- if (isWslLegacyBash(path)) return { available: false, reason: "launcher-only", path };
183
- if (!facts.fsProbe(path)) return { available: false, reason: "not-installed", path };
184
- if (id === "wsl" && facts.distros !== undefined && facts.distros.length === 0) return { available: false, reason: "no-distro", path };
185
- return { available: true, reason: "installed", path };
186
- }
187
-
188
- /**
189
- * The catalog the settings page and the spawn path both read.
190
- *
191
- * `powershell` is never `activated: false` even if a switch said so: it is the
192
- * floor of the fallback chain (R2/R6), and a floor that can be removed is not a
193
- * floor. The caller sees that through `togglable: false`.
194
- */
195
- export function buildCatalog(facts: CatalogFacts): ShellEntry[] {
196
- return SHELL_PRIORITY.map((id) => {
197
- const probed = probe(id, facts);
198
- const activated = id === "powershell" ? true : probed.available && facts.switches[id] === true;
199
- const argv = activated && probed.available ? uiArgv(id, facts.paths, facts.wslDistro) : undefined;
200
- return {
201
- id,
202
- label: LABELS[id],
203
- ...(probed.path !== undefined ? { path: probed.path } : {}),
204
- available: probed.available,
205
- reason: probed.reason,
206
- activated,
207
- // Only powershell is un-togglable, and that is the floor rule above.
208
- togglable: id !== "powershell",
209
- ...(argv !== undefined ? { argv } : {}),
210
- };
211
- });
212
- }
213
-
214
- /** The result of resolving which shell a terminal should start with. */
215
- export interface ResolvedDefault {
216
- readonly id: ShellId | "cmd";
217
- readonly path: string;
218
- readonly argv: readonly string[];
219
- /** True when the stored value was replaced (the UI shows why). */
220
- readonly fellBack: boolean;
221
- /** True when nothing is activated and the floor was used. */
222
- readonly floor: boolean;
223
- /** The stored value that was not honoured, when there was one. */
224
- readonly from?: string;
225
- }
226
-
227
- /**
228
- * Decide the shell for one allocation (R4/R5/R6).
229
- *
230
- * Three distinct outcomes, because the UI has to say different things about them:
231
- * - `""` means the user never chose: take the first candidate, no notice;
232
- * - a non-empty value outside the candidates means the choice went stale (a
233
- * switch was turned off, or a shell was uninstalled): fall back AND report
234
- * `fellBack` with the original value;
235
- * - no candidate at all: the floor is PowerShell's own chain (pwsh 7 -> Windows
236
- * PowerShell), and `cmd.exe` is the last resort when even that is gone.
237
- *
238
- * The stored config is never rewritten here - a fallback that persists would
239
- * silently eat the user's choice.
240
- */
241
- export function resolveDefaultShell(catalog: readonly ShellEntry[], configured: string, paths: ResolvedPaths): ResolvedDefault {
242
- const usable = SHELL_PRIORITY
243
- .map((id) => catalog.find((entry) => entry.id === id))
244
- .filter((entry): entry is ShellEntry => entry !== undefined && entry.activated && entry.available && entry.path !== undefined);
245
- const wanted = configured.trim();
246
- const chosen = wanted.length === 0 ? undefined : usable.find((entry) => entry.id === wanted);
247
- const picked = chosen ?? usable[0];
248
- if (picked !== undefined) {
249
- return {
250
- id: picked.id,
251
- path: picked.path!,
252
- argv: picked.argv ?? uiArgv(picked.id, paths),
253
- fellBack: chosen === undefined && wanted.length > 0,
254
- floor: false,
255
- ...(chosen === undefined && wanted.length > 0 ? { from: wanted } : {}),
256
- };
257
- }
258
- // Nothing activated: the floor. `paths.pwsh` is already the candidate-list
259
- // winner (pwsh 7 -> PATH pwsh -> Windows PowerShell 5.1), so the only step
260
- // below it is cmd.exe, which exists on every Windows host and is enough to
261
- // type in.
262
- const floorPath = paths.pwsh ?? "C:\\Windows\\System32\\cmd.exe";
263
- const isCmd = /\cmd\.exe$/i.test(floorPath);
264
- return {
265
- id: isCmd ? "cmd" : "powershell",
266
- path: floorPath,
267
- argv: isCmd ? [floorPath] : [floorPath, "-NoLogo"],
268
- fellBack: wanted.length > 0,
269
- floor: true,
270
- ...(wanted.length > 0 ? { from: wanted } : {}),
271
- };
272
- }
1
+ // dsh-bash-terminal-ts: the shell catalog for the browser-side terminal.
2
+ //
3
+ // Why this file exists separately from `buildArgv` / `terminalArgv` / `buildEnv`
4
+ // in index.ts and terminal.ts: those three serve the MODEL tool, where a dumb
5
+ // terminal and a stripped environment are correct (no colour, no pager, one
6
+ // command per call). The browser terminal is the opposite contract - a real
7
+ // xterm emulator driven by a human who expects their profile, their prompt and
8
+ // their colours. Sharing the tool's argv/env would hand the user a shell with
9
+ // `TERM=dumb`, `NO_COLOR=1` and `-NoProfile`.
10
+ //
11
+ // So this module owns the UI-facing rules and REUSES the parts that are
12
+ // genuinely shared: the executable candidate lists (injected by index.ts, which
13
+ // keeps the dependency one-way and this file unit-testable), the MSYS2 `MSYSTEM`
14
+ // rule, and the WSL interactive argv shape.
15
+ //
16
+ // Everything here is pure: no fs, no child_process, no module-level cache. The
17
+ // caller injects `fsProbe` / `distros` and owns the TTL.
18
+
19
+ import type { ResolvedPaths } from "./dsh-types.js";
20
+ import type { ShellId } from "./shell-id.js";
21
+
22
+ /** Re-exported so existing importers keep working (see shell-id.ts for why). */
23
+ export type { ShellId };
24
+
25
+ /** Catalog order = fallback order: powershell is BOTH first choice and floor. The
26
+ * pinned POSIX payload sits last: it only earns a seat when the machine's own
27
+ * shells are absent, and a detected WSL distro vetoes it before that. */
28
+ export const SHELL_PRIORITY: readonly ShellId[] = ["powershell", "gitbash", "msys2", "wsl", "niu", "payload"];
29
+
30
+ /** Why one backend is not launchable (drives the settings-row badge). */
31
+ export type ShellUnavailable = "installed" | "not-installed" | "no-distro" | "launcher-only" | "wsl-preferred";
32
+
33
+ /** One catalog row: what the UI shows and what the host would spawn. */
34
+ export interface ShellEntry {
35
+ readonly id: ShellId;
36
+ /** Display label (a catalog constant, never inferred from the file name). */
37
+ readonly label: string;
38
+ /** Resolved executable, absent when the backend is not usable. */
39
+ readonly path?: string;
40
+ readonly available: boolean;
41
+ readonly reason: ShellUnavailable;
42
+ /** The activation switch state (powershell is always true: the floor). */
43
+ readonly activated: boolean;
44
+ /** False only for powershell, which the user cannot turn off. */
45
+ readonly togglable: boolean;
46
+ /** Interactive argv, present only when `available`. */
47
+ readonly argv?: readonly string[];
48
+ }
49
+
50
+ /** The activation switches as persisted in the plugin config. */
51
+ export interface ShellSwitches {
52
+ gitbash: boolean;
53
+ msys2: boolean;
54
+ wsl: boolean;
55
+ niu: boolean;
56
+ payload: boolean;
57
+ }
58
+
59
+ /** Everything the catalog builder needs from the outside world. */
60
+ export interface CatalogFacts {
61
+ /** Resolved executables (config override or candidate-list winner). */
62
+ paths: ResolvedPaths;
63
+ /** Whether one path is a launchable file. */
64
+ fsProbe(path: string): boolean;
65
+ /** `wsl -l -q` output. `undefined` means the probe could not answer (a timeout
66
+ * is not evidence of absence); `[]` means it answered "no distro installed". */
67
+ readonly distros: readonly string[] | undefined;
68
+ switches: ShellSwitches;
69
+ /** WSL distro override from config ('' = the WSL default). */
70
+ wslDistro: string;
71
+ /**
72
+ * True when the payload gate was vetoed by a positively-detected WSL distro
73
+ * (a real Linux bash outranks the MSYS payload). It only colors the payload
74
+ * row's badge when no payload path resolved - "why" information, not a gate.
75
+ */
76
+ payloadSuppressed?: boolean;
77
+ }
78
+
79
+ /** Display labels. The catalog id/label pair is DECIDED BY THIS TABLE. */
80
+ const LABELS: Record<ShellId, string> = {
81
+ powershell: "PowerShell",
82
+ gitbash: "Git Bash",
83
+ msys2: "MSYS2",
84
+ wsl: "WSL bash",
85
+ niu: "Niubash",
86
+ payload: "POSIX payload",
87
+ };
88
+
89
+ /**
90
+ * `<SystemRoot>\System32\bash.exe` (and the sysnative mirror) is the deprecated
91
+ * WSL forwarder, not a shell. `candidateGitBashPaths` already skips it; this
92
+ * guard protects the second way a path reaches the catalog - a user-supplied
93
+ * `gitBashPath` / `msys2Path` config value.
94
+ */
95
+ export function isWslLegacyBash(path: string): boolean {
96
+ const stem = /([^\\/]+)\.exe$/i.exec(path)?.[1]?.toLowerCase();
97
+ if (stem !== "bash") return false;
98
+ return /\\(windows\\system32|sysnative)\\bash\.exe$/i.test(path.replace(/\//g, "\\"));
99
+ }
100
+
101
+ /**
102
+ * Console-allocating launchers: `git-bash.exe`, `msys2.exe`, `msys2_shell.cmd`.
103
+ * They open their own window and return zero bytes on a piped stdio (measured
104
+ * against MSYS2 with bash 5.3.15), so they can never host an embedded terminal.
105
+ */
106
+ export function isConsoleLauncher(path: string): boolean {
107
+ const name = path.slice(Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1).toLowerCase();
108
+ return name === "git-bash.exe" || name === "msys2.exe" || name === "msys2_shell.cmd";
109
+ }
110
+
111
+ /**
112
+ * Interactive argv for the BROWSER terminal.
113
+ *
114
+ * Differences from the tool's `terminalArgv`: PowerShell keeps its profile (the
115
+ * user's aliases and prompt are the point of a UI terminal), and the three bash
116
+ * families are unchanged because those rules are about the shell itself:
117
+ * Git Bash needs `-i`, MSYS2 needs `-l` and NEVER `-lc` (this argv carries no
118
+ * command, and `bash -lc` with no operand exits 2), WSL needs `-- bash -i` on the
119
+ * default distro (bare `-e` fails under ConPTY with RPC 0x8007072c).
120
+ */
121
+ export function uiArgv(shell: ShellId, paths: ResolvedPaths, wslDistro = ""): string[] {
122
+ switch (shell) {
123
+ case "powershell":
124
+ return [requirePath(paths.pwsh, shell), "-NoLogo"];
125
+ case "gitbash":
126
+ return [requirePath(paths.gitbash, shell), "-i"];
127
+ case "msys2":
128
+ return [requirePath(paths.msys2, shell), "-l"];
129
+ case "payload":
130
+ // The payload is a Git-for-Windows tree: the interactive rules are the
131
+ // gitbash ones (an interactive non-login shell; its /etc still works).
132
+ return [requirePath(paths.payload, shell), "-i"];
133
+ case "niu":
134
+ // Interactive non-login: niu reads ~/.niubashrc (its only Windows
135
+ // startup file), so the user's prompt and colours survive.
136
+ return [requirePath(paths.niu, shell), "-i"];
137
+ case "wsl": {
138
+ const distro = wslDistro.trim();
139
+ return distro.length > 0
140
+ ? [requirePath(paths.wsl, shell), "-d", distro, "-e", "bash", "-i"]
141
+ : [requirePath(paths.wsl, shell), "--", "bash", "-i"];
142
+ }
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Environment for the BROWSER terminal: the host process environment plus a real
148
+ * terminal type. `buildEnv` is deliberately NOT reused - it layers
149
+ * `TERM=dumb`/`NO_COLOR=1`/`PAGER=cat` over an agent-scoped map, which is right
150
+ * for a one-shot tool call and wrong for an interactive screen.
151
+ *
152
+ * The one rule carried over is MSYS2's `MSYSTEM`: without it MSYS2 starts in the
153
+ * bare MSYS environment and `/etc/profile` never puts `/mingw64/bin` on PATH, so
154
+ * gcc/make are missing in the very shell the user chose to get them from. An
155
+ * explicit value in the base environment wins, exactly as in `buildEnv`.
156
+ */
157
+ export function uiEnv(shell: ShellId, baseEnv: NodeJS.ProcessEnv): Record<string, string | undefined> {
158
+ const env: Record<string, string | undefined> = { ...baseEnv };
159
+ // A UI terminal is an xterm: the tool's `dumb` would kill colours, cursor
160
+ // control and full-screen programs.
161
+ env.TERM = "xterm-256color";
162
+ if (shell === "msys2" && env.MSYSTEM === undefined) env.MSYSTEM = "MINGW64";
163
+ return env;
164
+ }
165
+
166
+ function requirePath(path: string | undefined, shell: ShellId): string {
167
+ if (path === undefined || path.length === 0) {
168
+ throw new Error(`terminal: ${shell} backend unavailable - no executable resolved`);
169
+ }
170
+ return path;
171
+ }
172
+
173
+ /** One row's availability, given the injected facts. */
174
+ function probe(id: ShellId, facts: CatalogFacts): { available: boolean; reason: ShellUnavailable; path?: string } {
175
+ const path =
176
+ id === "powershell" ? facts.paths.pwsh
177
+ : id === "gitbash" ? facts.paths.gitbash
178
+ : id === "msys2" ? facts.paths.msys2
179
+ : id === "payload" ? facts.paths.payload
180
+ : id === "niu" ? facts.paths.niu
181
+ : facts.paths.wsl;
182
+ if (path === undefined || path.length === 0) {
183
+ if (id === "payload" && facts.payloadSuppressed === true) return { available: false, reason: "wsl-preferred" };
184
+ return { available: false, reason: "not-installed" };
185
+ }
186
+ // A configured path is honoured, but a launcher is not a shell: trusting the
187
+ // config here is how `msys2.exe` ends up in the menu and returns no output.
188
+ if (isConsoleLauncher(path)) return { available: false, reason: "launcher-only", path };
189
+ if (isWslLegacyBash(path)) return { available: false, reason: "launcher-only", path };
190
+ if (!facts.fsProbe(path)) return { available: false, reason: "not-installed", path };
191
+ if (id === "wsl" && facts.distros !== undefined && facts.distros.length === 0) return { available: false, reason: "no-distro", path };
192
+ return { available: true, reason: "installed", path };
193
+ }
194
+
195
+ /**
196
+ * The catalog the settings page and the spawn path both read.
197
+ *
198
+ * `powershell` is never `activated: false` even if a switch said so: it is the
199
+ * floor of the fallback chain (R2/R6), and a floor that can be removed is not a
200
+ * floor. The caller sees that through `togglable: false`.
201
+ */
202
+ export function buildCatalog(facts: CatalogFacts): ShellEntry[] {
203
+ return SHELL_PRIORITY.map((id) => {
204
+ const probed = probe(id, facts);
205
+ const activated = id === "powershell" ? true : probed.available && facts.switches[id] === true;
206
+ const argv = activated && probed.available ? uiArgv(id, facts.paths, facts.wslDistro) : undefined;
207
+ return {
208
+ id,
209
+ label: LABELS[id],
210
+ ...(probed.path !== undefined ? { path: probed.path } : {}),
211
+ available: probed.available,
212
+ reason: probed.reason,
213
+ activated,
214
+ // Only powershell is un-togglable, and that is the floor rule above.
215
+ togglable: id !== "powershell",
216
+ ...(argv !== undefined ? { argv } : {}),
217
+ };
218
+ });
219
+ }
220
+
221
+ /** The result of resolving which shell a terminal should start with. */
222
+ export interface ResolvedDefault {
223
+ readonly id: ShellId | "cmd";
224
+ readonly path: string;
225
+ readonly argv: readonly string[];
226
+ /** True when the stored value was replaced (the UI shows why). */
227
+ readonly fellBack: boolean;
228
+ /** True when nothing is activated and the floor was used. */
229
+ readonly floor: boolean;
230
+ /** The stored value that was not honoured, when there was one. */
231
+ readonly from?: string;
232
+ }
233
+
234
+ /**
235
+ * Decide the shell for one allocation (R4/R5/R6).
236
+ *
237
+ * Three distinct outcomes, because the UI has to say different things about them:
238
+ * - `""` means the user never chose: take the first candidate, no notice;
239
+ * - a non-empty value outside the candidates means the choice went stale (a
240
+ * switch was turned off, or a shell was uninstalled): fall back AND report
241
+ * `fellBack` with the original value;
242
+ * - no candidate at all: the floor is PowerShell's own chain (pwsh 7 -> Windows
243
+ * PowerShell), and `cmd.exe` is the last resort when even that is gone.
244
+ *
245
+ * The stored config is never rewritten here - a fallback that persists would
246
+ * silently eat the user's choice.
247
+ */
248
+ export function resolveDefaultShell(catalog: readonly ShellEntry[], configured: string, paths: ResolvedPaths): ResolvedDefault {
249
+ const usable = SHELL_PRIORITY
250
+ .map((id) => catalog.find((entry) => entry.id === id))
251
+ .filter((entry): entry is ShellEntry => entry !== undefined && entry.activated && entry.available && entry.path !== undefined);
252
+ const wanted = configured.trim();
253
+ const chosen = wanted.length === 0 ? undefined : usable.find((entry) => entry.id === wanted);
254
+ const picked = chosen ?? usable[0];
255
+ if (picked !== undefined) {
256
+ return {
257
+ id: picked.id,
258
+ path: picked.path!,
259
+ argv: picked.argv ?? uiArgv(picked.id, paths),
260
+ fellBack: chosen === undefined && wanted.length > 0,
261
+ floor: false,
262
+ ...(chosen === undefined && wanted.length > 0 ? { from: wanted } : {}),
263
+ };
264
+ }
265
+ // Nothing activated: the floor. `paths.pwsh` is already the candidate-list
266
+ // winner (pwsh 7 -> PATH pwsh -> Windows PowerShell 5.1), so the only step
267
+ // below it is cmd.exe, which exists on every Windows host and is enough to
268
+ // type in.
269
+ const floorPath = paths.pwsh ?? "C:\\Windows\\System32\\cmd.exe";
270
+ const isCmd = /\cmd\.exe$/i.test(floorPath);
271
+ return {
272
+ id: isCmd ? "cmd" : "powershell",
273
+ path: floorPath,
274
+ argv: isCmd ? [floorPath] : [floorPath, "-NoLogo"],
275
+ fellBack: wanted.length > 0,
276
+ floor: true,
277
+ ...(wanted.length > 0 ? { from: wanted } : {}),
278
+ };
279
+ }