dsh-bash-terminal-ts 0.9.1 → 0.9.2

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