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.
- package/CHANGELOG.md +205 -205
- package/CHANGELOG.pt.md +151 -151
- package/README.de.md +206 -206
- package/README.en.md +212 -212
- package/README.es.md +206 -206
- package/README.fr.md +206 -206
- package/README.it.md +206 -206
- package/README.md +423 -423
- package/README.pt.md +212 -212
- package/README.ru.md +206 -206
- package/agent-presets/README.md +171 -171
- package/agent-presets/install-presets.mjs +180 -180
- package/agent-presets/standard-win/agent.cordis.yml +271 -271
- package/agent-presets/standard-win/preset.yml +2 -2
- package/agent-presets/standard-win-gitbash/agent.cordis.yml +271 -271
- package/agent-presets/standard-win-gitbash/preset.yml +2 -2
- package/agent-presets/standard-win-msys2/agent.cordis.yml +272 -272
- package/agent-presets/standard-win-msys2/preset.yml +2 -2
- package/agent-presets/standard-win-niu/agent.cordis.yml +270 -270
- package/agent-presets/standard-win-niu/preset.yml +2 -2
- package/agent-presets/standard-win-payload/agent.cordis.yml +271 -271
- package/agent-presets/standard-win-payload/preset.yml +2 -2
- package/agent-presets/standard-win-wsl2/agent.cordis.yml +271 -271
- package/agent-presets/standard-win-wsl2/preset.yml +2 -2
- package/cordis.patch.yml +72 -84
- package/dist/client.core.js +6 -16
- package/dist/client.js +6 -16
- package/docs/niu-backend.md +197 -197
- package/dsh.plugin.json +23 -23
- package/lib/client.js +6 -16
- package/lib/dsh-types.d.ts +0 -2
- package/lib/index.d.ts +2 -23
- package/lib/index.js +4 -34
- package/lib/locales.js +0 -10
- package/lib/payload.d.ts +72 -72
- package/lib/payload.js +279 -279
- package/lib/shell-id.d.ts +2 -2
- package/lib/shells.d.ts +2 -11
- package/lib/shells.js +4 -14
- package/lib/terminal.js +0 -1
- package/lib/webterminals/catalog.d.ts +0 -2
- package/lib/webterminals/catalog.js +0 -1
- package/lib/webterminals/mount.d.ts +0 -5
- package/lib/webterminals/mount.js +0 -4
- package/locale/de.json +5 -5
- package/locale/en.json +5 -5
- package/locale/es.json +5 -5
- package/locale/fr.json +5 -5
- package/locale/it.json +5 -5
- package/locale/ja.json +5 -5
- package/locale/ko.json +5 -5
- package/locale/pt.json +6 -6
- package/locale/ru.json +5 -5
- package/locale/zh.json +5 -5
- package/package.json +1 -5
- package/scripts/check-artifacts.mjs +96 -96
- package/scripts/link-shells.mjs +0 -21
- package/src/client.tsx +685 -685
- package/src/dsh-types.ts +330 -332
- package/src/index.ts +1267 -1316
- package/src/locales.ts +499 -509
- package/src/shell-id.ts +11 -11
- package/src/shells.ts +263 -279
- package/src/terminal.ts +615 -616
- package/src/webterminals/catalog.ts +0 -3
- package/src/webterminals/host.ts +361 -361
- package/src/webterminals/mount.ts +228 -237
- package/scripts/payload-alias.mjs +0 -97
- package/scripts/provision-payload.mjs +0 -233
- package/src/payload.ts +0 -322
package/src/index.ts
CHANGED
|
@@ -1,1316 +1,1267 @@
|
|
|
1
|
-
// dsh-bash-terminal-ts - one shell tool, four Windows terminals.
|
|
2
|
-
//
|
|
3
|
-
// Registers a model-facing shell tool. The terminal backend (powershell /
|
|
4
|
-
// gitbash / msys2 / wsl) is chosen by the USER in the Web UI settings (default
|
|
5
|
-
// terminal); the model cannot pick it — the tool always obeys the user's
|
|
6
|
-
// choice:
|
|
7
|
-
// - powershell: pwsh -NoLogo -NoProfile -NonInteractive -Command <cmd>
|
|
8
|
-
// - gitbash: Git for Windows bash -lc <cmd> (POSIX; /d/... paths)
|
|
9
|
-
// - msys2: MSYS2 bash -lc <cmd> (POSIX; /c/... paths, full GCC/mingw64 toolchain)
|
|
10
|
-
// - wsl: wsl [-d <distro>] -e bash -lc <cmd> (Linux; /mnt/d/... paths)
|
|
11
|
-
//
|
|
12
|
-
// The tool spawns through the shared ctx.subprocess seam (process-tree
|
|
13
|
-
// termination, SIGTERM->grace->SIGKILL, spill files) and registers background
|
|
14
|
-
// handles with the generic ctx.jobs registry, mirroring the shipped
|
|
15
|
-
// dsh-tool-bash / dsh-tool-pwsh story call-for-call. It deliberately
|
|
16
|
-
// does NOT consume the ctx.shell capability seam: the platform's own
|
|
17
|
-
// sandboxed PowerShell executor keeps serving the pwsh tool. This tool is
|
|
18
|
-
// an additional, user-selected terminal that still honors the DSH sandbox:
|
|
19
|
-
// confined modes wrap PowerShell's spawn argv through ctx.sandbox
|
|
20
|
-
// (fail-closed), while danger-full-access, Git Bash, MSYS2, and WSL run
|
|
21
|
-
// unconfined. Git Bash and MSYS2 are unconfined because the DSH Windows ACL
|
|
22
|
-
// restricted-token runner cannot host Cygwin/MSYS2: bash aborts with
|
|
23
|
-
// "CreateFileMapping ... Win32 error 5" during startup.
|
|
24
|
-
|
|
25
|
-
import { lstatSync } from "node:fs";
|
|
26
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
27
|
-
import { mountBrowserTerminals } from "./webterminals/mount.js";
|
|
28
|
-
import {
|
|
29
|
-
ESCALATION_TARGETS,
|
|
30
|
-
TOOL_ABORTED,
|
|
31
|
-
approveEscalation,
|
|
32
|
-
defineTool,
|
|
33
|
-
escalationHintMarker,
|
|
34
|
-
clampTimeout,
|
|
35
|
-
deadline,
|
|
36
|
-
parseExitStatus,
|
|
37
|
-
timeoutOf,
|
|
38
|
-
sandboxDenialMarker,
|
|
39
|
-
validateEscalationArgs,
|
|
40
|
-
z,
|
|
41
|
-
HarnessError
|
|
42
|
-
} from "./dsh.js";
|
|
43
|
-
import type {
|
|
44
|
-
BashTerminalContext,
|
|
45
|
-
ExecAgent,
|
|
46
|
-
JobsRegistry,
|
|
47
|
-
JsonSchemaNode,
|
|
48
|
-
ResolvedPaths,
|
|
49
|
-
SandboxFacts,
|
|
50
|
-
SandboxPolicy,
|
|
51
|
-
ShellId,
|
|
52
|
-
ToolRunContext
|
|
53
|
-
} from "./dsh-types.js";
|
|
54
|
-
import { createTerminalRegistry, terminalTool } from "./terminal.js";
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
shellNiu: boolean
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (trimmed.
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const candidates = [
|
|
255
|
-
|
|
256
|
-
"
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
if (
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
export function
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
return
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
:
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
if (
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
if (
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
return
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
};
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
*/
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
const
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
title: args.command as string,
|
|
1269
|
-
description: args.description as string,
|
|
1270
|
-
...(args.workdir !== undefined ? { cwd: args.workdir } : {})
|
|
1271
|
-
};
|
|
1272
|
-
},
|
|
1273
|
-
presentResult: (args, result) => {
|
|
1274
|
-
const block = result.content.length === 1 ? result.content[0] : undefined;
|
|
1275
|
-
if (block === undefined || block.type !== "text") return undefined;
|
|
1276
|
-
const raw = block.text ?? "";
|
|
1277
|
-
if (args.run_in_background === true || result.isError) {
|
|
1278
|
-
return { card: "generic", content: [{ type: "text", text: "```console\n" + raw.replace(/\n+$/, "") + "\n```" }] };
|
|
1279
|
-
}
|
|
1280
|
-
const { body, ...exit } = parseExitStatus(raw);
|
|
1281
|
-
return { card: "terminal", output: body, ...exit };
|
|
1282
|
-
}
|
|
1283
|
-
}));
|
|
1284
|
-
}
|
|
1285
|
-
|
|
1286
|
-
//#region internals for tests
|
|
1287
|
-
export const internals = {
|
|
1288
|
-
candidateExists,
|
|
1289
|
-
resolveAllPaths,
|
|
1290
|
-
resolveWorkdir,
|
|
1291
|
-
renderResult,
|
|
1292
|
-
processOutcome,
|
|
1293
|
-
startBackground,
|
|
1294
|
-
runForeground,
|
|
1295
|
-
spawnSpec,
|
|
1296
|
-
buildEnv,
|
|
1297
|
-
buildArgv,
|
|
1298
|
-
validateArgs,
|
|
1299
|
-
toolDescription,
|
|
1300
|
-
SHELL_DESCRIPTIONS,
|
|
1301
|
-
DEFAULT_TIMEOUT_MS,
|
|
1302
|
-
MAX_TIMEOUT_MS,
|
|
1303
|
-
DEFAULT_MAX_OUTPUT_BYTES
|
|
1304
|
-
};
|
|
1305
|
-
//#endregion
|
|
1306
|
-
|
|
1307
|
-
// re-export for plugin consumers (type-only)
|
|
1308
|
-
export type {
|
|
1309
|
-
ExecAgent,
|
|
1310
|
-
JobsRegistry,
|
|
1311
|
-
JsonSchemaNode,
|
|
1312
|
-
SandboxFacts,
|
|
1313
|
-
SandboxPolicy,
|
|
1314
|
-
ShellId,
|
|
1315
|
-
ToolRunContext
|
|
1316
|
-
};
|
|
1
|
+
// dsh-bash-terminal-ts - one shell tool, four Windows terminals.
|
|
2
|
+
//
|
|
3
|
+
// Registers a model-facing shell tool. The terminal backend (powershell /
|
|
4
|
+
// gitbash / msys2 / wsl) is chosen by the USER in the Web UI settings (default
|
|
5
|
+
// terminal); the model cannot pick it — the tool always obeys the user's
|
|
6
|
+
// choice:
|
|
7
|
+
// - powershell: pwsh -NoLogo -NoProfile -NonInteractive -Command <cmd>
|
|
8
|
+
// - gitbash: Git for Windows bash -lc <cmd> (POSIX; /d/... paths)
|
|
9
|
+
// - msys2: MSYS2 bash -lc <cmd> (POSIX; /c/... paths, full GCC/mingw64 toolchain)
|
|
10
|
+
// - wsl: wsl [-d <distro>] -e bash -lc <cmd> (Linux; /mnt/d/... paths)
|
|
11
|
+
//
|
|
12
|
+
// The tool spawns through the shared ctx.subprocess seam (process-tree
|
|
13
|
+
// termination, SIGTERM->grace->SIGKILL, spill files) and registers background
|
|
14
|
+
// handles with the generic ctx.jobs registry, mirroring the shipped
|
|
15
|
+
// dsh-tool-bash / dsh-tool-pwsh story call-for-call. It deliberately
|
|
16
|
+
// does NOT consume the ctx.shell capability seam: the platform's own
|
|
17
|
+
// sandboxed PowerShell executor keeps serving the pwsh tool. This tool is
|
|
18
|
+
// an additional, user-selected terminal that still honors the DSH sandbox:
|
|
19
|
+
// confined modes wrap PowerShell's spawn argv through ctx.sandbox
|
|
20
|
+
// (fail-closed), while danger-full-access, Git Bash, MSYS2, and WSL run
|
|
21
|
+
// unconfined. Git Bash and MSYS2 are unconfined because the DSH Windows ACL
|
|
22
|
+
// restricted-token runner cannot host Cygwin/MSYS2: bash aborts with
|
|
23
|
+
// "CreateFileMapping ... Win32 error 5" during startup.
|
|
24
|
+
|
|
25
|
+
import { lstatSync } from "node:fs";
|
|
26
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
27
|
+
import { mountBrowserTerminals } from "./webterminals/mount.js";
|
|
28
|
+
import {
|
|
29
|
+
ESCALATION_TARGETS,
|
|
30
|
+
TOOL_ABORTED,
|
|
31
|
+
approveEscalation,
|
|
32
|
+
defineTool,
|
|
33
|
+
escalationHintMarker,
|
|
34
|
+
clampTimeout,
|
|
35
|
+
deadline,
|
|
36
|
+
parseExitStatus,
|
|
37
|
+
timeoutOf,
|
|
38
|
+
sandboxDenialMarker,
|
|
39
|
+
validateEscalationArgs,
|
|
40
|
+
z,
|
|
41
|
+
HarnessError
|
|
42
|
+
} from "./dsh.js";
|
|
43
|
+
import type {
|
|
44
|
+
BashTerminalContext,
|
|
45
|
+
ExecAgent,
|
|
46
|
+
JobsRegistry,
|
|
47
|
+
JsonSchemaNode,
|
|
48
|
+
ResolvedPaths,
|
|
49
|
+
SandboxFacts,
|
|
50
|
+
SandboxPolicy,
|
|
51
|
+
ShellId,
|
|
52
|
+
ToolRunContext
|
|
53
|
+
} from "./dsh-types.js";
|
|
54
|
+
import { createTerminalRegistry, terminalTool } from "./terminal.js";
|
|
55
|
+
|
|
56
|
+
/** Stable Cordis plugin name. */
|
|
57
|
+
export const name = "bash-terminal";
|
|
58
|
+
/** Composition entry id (the `tool-bash-terminal` row the bundle patch declares). */
|
|
59
|
+
const ENTRY_ID = "tool-bash-terminal";
|
|
60
|
+
/** Services required before the tool can register. */
|
|
61
|
+
export const inject = ["tools", "systemPrompt", "shellEnv", "subprocess", "settings", "sandbox", "sandboxPolicy"];
|
|
62
|
+
|
|
63
|
+
/** The terminal backends this tool exposes, in catalog order. */
|
|
64
|
+
export const SHELLS = ["powershell", "gitbash", "msys2", "wsl", "niu"] as const;
|
|
65
|
+
/** The backend used when the caller does not name one. */
|
|
66
|
+
export const DEFAULT_SHELL: ShellId = "powershell";
|
|
67
|
+
/** Default per-command timeout (ms). */
|
|
68
|
+
const DEFAULT_TIMEOUT_MS = 120000;
|
|
69
|
+
/** Upper bound a caller's timeoutMs is capped to. */
|
|
70
|
+
const MAX_TIMEOUT_MS = 600000;
|
|
71
|
+
/** SIGTERM->SIGKILL grace (ms), matching dsh-bash-local's default. */
|
|
72
|
+
const DEFAULT_GRACE_MS = 3000;
|
|
73
|
+
/** Per-stream in-memory cap before spilling (bytes). */
|
|
74
|
+
const DEFAULT_MAX_OUTPUT_BYTES = 64 * 1024;
|
|
75
|
+
/** Per-stream spill file cap (bytes). */
|
|
76
|
+
const DEFAULT_MAX_SPILL_BYTES = 64 * 1024 * 1024;
|
|
77
|
+
/** Timeout code stamped on the deadline's TimeoutReason. */
|
|
78
|
+
const TIMEOUT_CODE = "SHELL_TIMEOUT";
|
|
79
|
+
|
|
80
|
+
/** Model-friendly environment overrides (same set dsh-bash-local hardcodes). */
|
|
81
|
+
const ENV_OVERRIDES: Record<string, string> = {
|
|
82
|
+
NO_COLOR: "1",
|
|
83
|
+
TERM: "dumb",
|
|
84
|
+
PAGER: "cat",
|
|
85
|
+
GIT_PAGER: "cat"
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/** Live reference the 0.1.7 loader hands `apply` for `.volatile()` config fields. */
|
|
89
|
+
interface VolatileRef<T> {
|
|
90
|
+
get(): T;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Static shape of the runtime configuration schema. */
|
|
94
|
+
export interface ConfigValues {
|
|
95
|
+
defaultShell: string;
|
|
96
|
+
timeoutMs: number;
|
|
97
|
+
maxTimeoutMs: number;
|
|
98
|
+
pwshPath: string;
|
|
99
|
+
gitBashPath: string;
|
|
100
|
+
msys2Path: string;
|
|
101
|
+
wslPath: string;
|
|
102
|
+
/**
|
|
103
|
+
* The bottom terminal's MASTER switch. Off outranks every child choice: the
|
|
104
|
+
* settings row hides the per-shell switches (a switch for something that cannot
|
|
105
|
+
* run is a lie) and the routes refuse allocations.
|
|
106
|
+
*/
|
|
107
|
+
bottomTerminal: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Opt in to publishing the client-side `webTerminals` service, which is what powers
|
|
110
|
+
* dsh-better-sidebar's OFFICIAL bottom "Terminal" row on a deployment that has no
|
|
111
|
+
* terminal controller. Default false: the name is also provided by the host's own
|
|
112
|
+
* controller client, cordis refuses the second provider, and the controller's provide
|
|
113
|
+
* is unguarded - so claiming it by default breaks the first-party terminal and the
|
|
114
|
+
* whole web boot on DSH 0.2.0+ (measured). Turn it on only where the official row is
|
|
115
|
+
* dead because no controller exists.
|
|
116
|
+
*/
|
|
117
|
+
shimOfficialTerminal: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Browser-terminal activation (R1/R2). The default is `true` for all three:
|
|
120
|
+
* a switch means "allowed", and availability is ANDed with it by the catalog -
|
|
121
|
+
* so "detected means on" needs no probe at schema level, while an explicit
|
|
122
|
+
* `false` is a real opt-out even on a machine that has the shell.
|
|
123
|
+
*/
|
|
124
|
+
shellGitBash: boolean;
|
|
125
|
+
shellMsys2: boolean;
|
|
126
|
+
shellWsl: boolean;
|
|
127
|
+
/** WSL distribution for the browser terminal; '' means the WSL default. */
|
|
128
|
+
wslDistro: string;
|
|
129
|
+
/** Fixed niu.exe path; '' = auto-discovery via candidateNiuPaths. */
|
|
130
|
+
niuPath: string;
|
|
131
|
+
/** Activation switch for the Niubash row (default true: detected means on). */
|
|
132
|
+
shellNiu: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Runtime configuration schema. */
|
|
136
|
+
export const Config = z.object({
|
|
137
|
+
defaultShell: z.string().default(DEFAULT_SHELL).volatile(),
|
|
138
|
+
timeoutMs: z.number().default(DEFAULT_TIMEOUT_MS),
|
|
139
|
+
maxTimeoutMs: z.number().default(MAX_TIMEOUT_MS),
|
|
140
|
+
pwshPath: z.string().default(""),
|
|
141
|
+
gitBashPath: z.string().default(""),
|
|
142
|
+
msys2Path: z.string().default(""),
|
|
143
|
+
wslPath: z.string().default(""),
|
|
144
|
+
// `.volatile()` on the new fields: the browser terminal reads them on every
|
|
145
|
+
// allocation, and a flip in the settings page has to take effect without a
|
|
146
|
+
// restart (0.1.7+ hands apply a live ref; older lines re-read the same value).
|
|
147
|
+
bottomTerminal: z.boolean().default(true).volatile(),
|
|
148
|
+
shimOfficialTerminal: z.boolean().default(false).volatile(),
|
|
149
|
+
shellGitBash: z.boolean().default(true).volatile(),
|
|
150
|
+
shellMsys2: z.boolean().default(true).volatile(),
|
|
151
|
+
shellWsl: z.boolean().default(true).volatile(),
|
|
152
|
+
wslDistro: z.string().default("").volatile(),
|
|
153
|
+
niuPath: z.string().default(""),
|
|
154
|
+
shellNiu: z.boolean().default(true).volatile()
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ---- executable resolution ------------------------------------------------
|
|
158
|
+
|
|
159
|
+
function candidateExists(candidate: string): boolean {
|
|
160
|
+
try {
|
|
161
|
+
const stat = lstatSync(candidate);
|
|
162
|
+
return stat.isFile() || stat.isSymbolicLink();
|
|
163
|
+
} catch {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function resolveFromCandidates(candidates: readonly string[]): string | undefined {
|
|
169
|
+
for (const candidate of candidates) {
|
|
170
|
+
if (candidateExists(candidate)) return candidate;
|
|
171
|
+
}
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Well-known PowerShell install locations plus PATH entries, newest first. */
|
|
176
|
+
export function candidatePwshPaths(env: NodeJS.ProcessEnv = process.env): string[] {
|
|
177
|
+
const programFiles = env.ProgramFiles ?? "C:\\Program Files";
|
|
178
|
+
const systemRoot = env.SystemRoot ?? "C:\\Windows";
|
|
179
|
+
const candidates = [join(programFiles, "PowerShell", "7", "pwsh.exe")];
|
|
180
|
+
for (const entry of (env.PATH ?? "").split(";")) {
|
|
181
|
+
const trimmed = entry.trim().replace(/^"|"$/g, "");
|
|
182
|
+
if (trimmed.length === 0) continue;
|
|
183
|
+
candidates.push(join(trimmed, "pwsh.exe"));
|
|
184
|
+
}
|
|
185
|
+
candidates.push(join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"));
|
|
186
|
+
return candidates;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Git for Windows locations, then PATH bash.exe entries EXCLUDING the
|
|
191
|
+
* System32 launcher (c:\\windows\\system32\\bash.exe is the WSL
|
|
192
|
+
* forwarder, not a Git Bash shell).
|
|
193
|
+
*/
|
|
194
|
+
export function candidateGitBashPaths(env: NodeJS.ProcessEnv = process.env): string[] {
|
|
195
|
+
const programFiles = env.ProgramFiles ?? "C:\\Program Files";
|
|
196
|
+
const systemRoot = (env.SystemRoot ?? "C:\\Windows").toLowerCase();
|
|
197
|
+
const localAppData = env.LOCALAPPDATA ?? "";
|
|
198
|
+
const candidates = [
|
|
199
|
+
join(programFiles, "Git", "bin", "bash.exe"),
|
|
200
|
+
join(programFiles, "Git", "usr", "bin", "bash.exe")
|
|
201
|
+
];
|
|
202
|
+
if (localAppData.length > 0) candidates.push(join(localAppData, "Programs", "Git", "bin", "bash.exe"));
|
|
203
|
+
for (const entry of (env.PATH ?? "").split(";")) {
|
|
204
|
+
const trimmed = entry.trim().replace(/^"|"$/g, "");
|
|
205
|
+
if (trimmed.length === 0) continue;
|
|
206
|
+
if (trimmed.toLowerCase().includes(systemRoot)) continue;
|
|
207
|
+
candidates.push(join(trimmed, "bash.exe"));
|
|
208
|
+
}
|
|
209
|
+
return candidates;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* MSYS2 locations, in preference order: the real `bash.exe` under usr\bin
|
|
214
|
+
* first, then bin\bash.exe, and `msys2.exe` dead last.
|
|
215
|
+
*
|
|
216
|
+
* msys2.exe is NOT a usable backend for piped execution: it is the console-
|
|
217
|
+
* allocating Cygwin launcher, so a spawn with piped stdio returns exit 0 with
|
|
218
|
+
* zero bytes on both stdout and stderr (measured on this machine against
|
|
219
|
+
* MSYS2 with bash 5.3.15). Keeping it in the list only as a last-resort
|
|
220
|
+
* fallback preserves the path the config docs reference, but a working
|
|
221
|
+
* bash.exe always wins.
|
|
222
|
+
*
|
|
223
|
+
* MSYS2 uses the same Cygwin/MSYS2 runtime as Git Bash, so it cannot run
|
|
224
|
+
* under the DSH Windows ACL restricted-token sandbox.
|
|
225
|
+
*/
|
|
226
|
+
export function candidateMsys2Paths(env: NodeJS.ProcessEnv = process.env): string[] {
|
|
227
|
+
const candidates = [
|
|
228
|
+
"C:\\msys64\\usr\\bin\\bash.exe",
|
|
229
|
+
"C:\\msys64\\bin\\bash.exe"
|
|
230
|
+
];
|
|
231
|
+
for (const entry of (env.PATH ?? "").split(";")) {
|
|
232
|
+
const trimmed = entry.trim().replace(/^"|"$/g, "");
|
|
233
|
+
if (trimmed.length === 0) continue;
|
|
234
|
+
const lower = trimmed.toLowerCase();
|
|
235
|
+
if (lower.includes("msys64") || lower.includes("mingw64")) {
|
|
236
|
+
candidates.push(join(trimmed, "bash.exe"));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
candidates.push("C:\\msys64\\msys2.exe"); // last resort: see the note above
|
|
240
|
+
return candidates;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Niubash locations, in preference order: the per-user installer default
|
|
245
|
+
* (LOCALAPPDATA\Programs\Niubash) first, then the winget shim link, then a
|
|
246
|
+
* machine-wide install, then PATH entries. Read from the registry on this
|
|
247
|
+
* machine: the installer writes a USER Path entry pointing at the capitalized
|
|
248
|
+
* directory, so the first candidate must match that exact casing on
|
|
249
|
+
* case-sensitive filesystems (Windows itself is case-insensitive).
|
|
250
|
+
*/
|
|
251
|
+
export function candidateNiuPaths(env: NodeJS.ProcessEnv = process.env): string[] {
|
|
252
|
+
const localAppData = env.LOCALAPPDATA ?? "";
|
|
253
|
+
const programFiles = env.ProgramFiles ?? "C:\\Program Files";
|
|
254
|
+
const candidates: string[] = [];
|
|
255
|
+
if (localAppData.length > 0) {
|
|
256
|
+
candidates.push(join(localAppData, "Programs", "Niubash", "niu.exe"));
|
|
257
|
+
candidates.push(join(localAppData, "Microsoft", "WinGet", "Links", "niu.exe"));
|
|
258
|
+
}
|
|
259
|
+
candidates.push(join(programFiles, "Niubash", "niu.exe"));
|
|
260
|
+
for (const entry of (env.PATH ?? "").split(";")) {
|
|
261
|
+
const trimmed = entry.trim().replace(/^"|"$/g, "");
|
|
262
|
+
if (trimmed.length === 0) continue;
|
|
263
|
+
candidates.push(join(trimmed, "niu.exe"));
|
|
264
|
+
}
|
|
265
|
+
return candidates;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function defaultWslPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
269
|
+
const systemRoot = env.SystemRoot ?? "C:\\Windows";
|
|
270
|
+
return join(systemRoot, "System32", "wsl.exe");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Executable paths, possibly undefined when a backend is not installed. */
|
|
274
|
+
export type { ResolvedPaths };
|
|
275
|
+
|
|
276
|
+
type PathConfig = Partial<Pick<ConfigValues, "pwshPath" | "gitBashPath" | "msys2Path" | "wslPath" | "niuPath">>;
|
|
277
|
+
|
|
278
|
+
export function resolveAllPaths(
|
|
279
|
+
config: PathConfig = {},
|
|
280
|
+
env: NodeJS.ProcessEnv = process.env
|
|
281
|
+
): ResolvedPaths {
|
|
282
|
+
const pwsh = config.pwshPath && config.pwshPath.trim().length > 0
|
|
283
|
+
? config.pwshPath
|
|
284
|
+
: resolveFromCandidates(candidatePwshPaths(env));
|
|
285
|
+
const gitbash = config.gitBashPath && config.gitBashPath.trim().length > 0
|
|
286
|
+
? config.gitBashPath
|
|
287
|
+
: resolveFromCandidates(candidateGitBashPaths(env));
|
|
288
|
+
const msys2 = config.msys2Path && config.msys2Path.trim().length > 0
|
|
289
|
+
? config.msys2Path
|
|
290
|
+
: resolveFromCandidates(candidateMsys2Paths(env));
|
|
291
|
+
const wsl = config.wslPath && config.wslPath.trim().length > 0
|
|
292
|
+
? config.wslPath
|
|
293
|
+
: defaultWslPath(env);
|
|
294
|
+
const niu = config.niuPath && config.niuPath.trim().length > 0
|
|
295
|
+
? config.niuPath
|
|
296
|
+
: resolveFromCandidates(candidateNiuPaths(env));
|
|
297
|
+
return { pwsh, gitbash, msys2, wsl, niu };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ---- argv / env construction ------------------------------------------------
|
|
301
|
+
|
|
302
|
+
export function buildArgv(
|
|
303
|
+
shell: string,
|
|
304
|
+
command: string,
|
|
305
|
+
paths: ResolvedPaths,
|
|
306
|
+
distro?: string
|
|
307
|
+
): Array<string | undefined> {
|
|
308
|
+
switch (shell) {
|
|
309
|
+
case "powershell":
|
|
310
|
+
return [paths.pwsh, "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command];
|
|
311
|
+
case "gitbash":
|
|
312
|
+
return [paths.gitbash, "-lc", command];
|
|
313
|
+
case "msys2":
|
|
314
|
+
// -lc, not -c: a login shell sources /etc/profile, which is what puts
|
|
315
|
+
// /usr/bin and /mingw64/bin on PATH. With a bare -c, `tr`, `sed`, `gcc`
|
|
316
|
+
// and friends are all "command not found".
|
|
317
|
+
return [paths.msys2, "-lc", command];
|
|
318
|
+
case "niu":
|
|
319
|
+
// Same login-shell rule: -lc sources the profile chain so winuxcmd's
|
|
320
|
+
// bin directory joins PATH. (niu has no MSYS runtime; see findings F2
|
|
321
|
+
// for the sandbox question this backend answers.)
|
|
322
|
+
return [paths.niu, "-lc", command];
|
|
323
|
+
case "wsl": {
|
|
324
|
+
const distroArg = distro !== undefined && distro.trim().length > 0 ? ["-d", distro.trim()] : [];
|
|
325
|
+
return [paths.wsl, ...distroArg, "-e", "bash", "-lc", command];
|
|
326
|
+
}
|
|
327
|
+
default:
|
|
328
|
+
throw new Error(`invalid shell: ${JSON.stringify(shell)} (expected one of ${SHELLS.join(", ")})`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Merge the DSH_* environment over the process environment. For WSL, only
|
|
334
|
+
* variables explicitly listed in WSLENV cross the boundary, so every DSH_*
|
|
335
|
+
* key is appended there — and the list is *layered onto* whatever WSLENV the
|
|
336
|
+
* host already had (Windows Terminal exports e.g. `WT_SESSION:WT_PROFILE_ID:`),
|
|
337
|
+
* never rebuilt from scratch: WSLENV is an allow-list, so dropping the
|
|
338
|
+
* inherited entries would silently stop them crossing into WSL.
|
|
339
|
+
*
|
|
340
|
+
* @param inheritedWslenv - the ambient WSLENV to layer onto. Callers that
|
|
341
|
+
* replace the child environment wholesale (the PTY path) must pass
|
|
342
|
+
* `process.env.WSLENV` explicitly, because the ambient value is not visible
|
|
343
|
+
* through `dshEnv`.
|
|
344
|
+
*/
|
|
345
|
+
export function buildEnv(
|
|
346
|
+
shell: string,
|
|
347
|
+
dshEnv?: Record<string, string>,
|
|
348
|
+
inheritedWslenv: string | undefined = process.env.WSLENV
|
|
349
|
+
): Record<string, string | undefined> {
|
|
350
|
+
const env: Record<string, string | undefined> = { ...ENV_OVERRIDES, ...dshEnv };
|
|
351
|
+
if (shell === "msys2") {
|
|
352
|
+
// Picks the MINGW64 environment, so /mingw64/bin (gcc, make, ...) joins
|
|
353
|
+
// PATH via /etc/profile. Without it MSYS2 defaults to the bare MSYS
|
|
354
|
+
// environment. An explicit caller-supplied value wins.
|
|
355
|
+
if (env.MSYSTEM === undefined) env.MSYSTEM = "MINGW64";
|
|
356
|
+
}
|
|
357
|
+
if (shell === "wsl") {
|
|
358
|
+
const keys = Object.keys(dshEnv ?? {});
|
|
359
|
+
if (keys.length > 0) {
|
|
360
|
+
// An explicit WSLENV in dshEnv wins over the inherited one; otherwise the
|
|
361
|
+
// inherited value is the base. Either way the base is SPLIT into entries
|
|
362
|
+
// (the host value ends with a trailing ":", so string concatenation would
|
|
363
|
+
// produce a malformed empty entry) and the WSLENV key itself is excluded
|
|
364
|
+
// (it is a key of dshEnv too, and appending it would add a bogus entry).
|
|
365
|
+
const declared = Object.prototype.hasOwnProperty.call(dshEnv ?? {}, "WSLENV");
|
|
366
|
+
const base = declared
|
|
367
|
+
? env.WSLENV
|
|
368
|
+
: (typeof inheritedWslenv === "string" && inheritedWslenv.length > 0 ? inheritedWslenv : env.WSLENV);
|
|
369
|
+
const parts = typeof base === "string" ? base.split(":") : [];
|
|
370
|
+
env.WSLENV = [...parts, ...keys.filter((k) => k !== "WSLENV").flatMap((k) => k.split(":"))]
|
|
371
|
+
.map((p) => p.trim())
|
|
372
|
+
.filter((p) => p.length > 0)
|
|
373
|
+
.join(":");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return env;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// ---- spawn plumbing over ctx.subprocess ---------------------------------------
|
|
380
|
+
|
|
381
|
+
export interface SpawnResolution {
|
|
382
|
+
command: string;
|
|
383
|
+
workdir: string;
|
|
384
|
+
timeoutMs: number;
|
|
385
|
+
stdoutMaxBytes: number;
|
|
386
|
+
stdin?: string;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function spawnSpec(resolved: SpawnResolution, argv: string[], env: Record<string, string | undefined>, signal: AbortSignal) {
|
|
390
|
+
const collect = (maxBytes: number) => ({ maxBytes, spill: { maxBytes: DEFAULT_MAX_SPILL_BYTES } });
|
|
391
|
+
return {
|
|
392
|
+
argv,
|
|
393
|
+
cwd: resolved.workdir,
|
|
394
|
+
stdio: {
|
|
395
|
+
stdin: resolved.stdin !== undefined ? { data: resolved.stdin } : "ignore",
|
|
396
|
+
stdout: collect(resolved.stdoutMaxBytes),
|
|
397
|
+
stderr: collect(DEFAULT_MAX_OUTPUT_BYTES)
|
|
398
|
+
},
|
|
399
|
+
graceMs: DEFAULT_GRACE_MS,
|
|
400
|
+
signal,
|
|
401
|
+
env
|
|
402
|
+
} as const;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function collectedOutput(handle: { collected: { stdout: unknown; stderr: unknown } }) {
|
|
406
|
+
const { stdout, stderr } = handle.collected;
|
|
407
|
+
if (!isCollectedStream(stdout) || !isCollectedStream(stderr)) {
|
|
408
|
+
throw new Error("dsh-bash-terminal-ts: subprocess implementation dropped a requested collect stream");
|
|
409
|
+
}
|
|
410
|
+
return { stdout, stderr };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function isCollectedStream(value: unknown): value is CollectedLike {
|
|
414
|
+
return typeof value === "object" && value !== null && typeof (value as CollectedLike).readFrom === "function";
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
interface CollectedLike {
|
|
418
|
+
readFrom(offset: number): { text: string; lossy: boolean; nextOffset: number; spillPath?: string };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
interface StreamOutput {
|
|
422
|
+
text: string;
|
|
423
|
+
truncated: boolean;
|
|
424
|
+
spillPath?: string;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function finalOutput(reader: CollectedLike): StreamOutput {
|
|
428
|
+
const read = reader.readFrom(0);
|
|
429
|
+
return {
|
|
430
|
+
text: read.text,
|
|
431
|
+
truncated: read.lossy,
|
|
432
|
+
...(read.spillPath !== undefined ? { spillPath: read.spillPath } : {})
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export interface ForegroundOutcome {
|
|
437
|
+
exitCode: number | null;
|
|
438
|
+
signal: string | null;
|
|
439
|
+
timedOut: boolean;
|
|
440
|
+
aborted: boolean;
|
|
441
|
+
timeoutMs: number;
|
|
442
|
+
stdout: StreamOutput;
|
|
443
|
+
stderr: StreamOutput;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async function runForeground(
|
|
447
|
+
ctx: BashTerminalContext,
|
|
448
|
+
argv: string[],
|
|
449
|
+
resolved: SpawnResolution,
|
|
450
|
+
env: Record<string, string | undefined>,
|
|
451
|
+
signal: AbortSignal,
|
|
452
|
+
timeoutMs: number
|
|
453
|
+
): Promise<ForegroundOutcome> {
|
|
454
|
+
const subprocess = requireSubprocess(ctx);
|
|
455
|
+
const d = deadline(signal, timeoutMs, TIMEOUT_CODE);
|
|
456
|
+
try {
|
|
457
|
+
const handle = subprocess.spawn(spawnSpec(resolved, argv, env, d.signal));
|
|
458
|
+
const outcome = await handle.done;
|
|
459
|
+
const collected = collectedOutput(handle);
|
|
460
|
+
const timedOut = timeoutOf(d.signal, TIMEOUT_CODE) !== undefined;
|
|
461
|
+
const aborted = d.signal.aborted && !timedOut;
|
|
462
|
+
return {
|
|
463
|
+
exitCode: outcome.exitCode,
|
|
464
|
+
signal: outcome.signal,
|
|
465
|
+
timedOut,
|
|
466
|
+
aborted,
|
|
467
|
+
timeoutMs,
|
|
468
|
+
stdout: finalOutput(collected.stdout),
|
|
469
|
+
stderr: finalOutput(collected.stderr)
|
|
470
|
+
};
|
|
471
|
+
} finally {
|
|
472
|
+
d[Symbol.dispose]();
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function requireSubprocess(ctx: BashTerminalContext) {
|
|
477
|
+
const subprocess = ctx.subprocess;
|
|
478
|
+
if (subprocess === null) {
|
|
479
|
+
throw new Error("dsh-bash-terminal-ts: ctx.subprocess seam unavailable (missing inject service)");
|
|
480
|
+
}
|
|
481
|
+
return subprocess;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
export interface ProcessRead {
|
|
485
|
+
delta: string;
|
|
486
|
+
lossy: boolean;
|
|
487
|
+
stdoutSpillPath?: string;
|
|
488
|
+
stderrSpillPath?: string;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
interface BackgroundProc {
|
|
492
|
+
status: "running" | "killed" | "completed";
|
|
493
|
+
exitCode: number | null;
|
|
494
|
+
signal: string | null;
|
|
495
|
+
done: Promise<void>;
|
|
496
|
+
readOutput(): ProcessRead;
|
|
497
|
+
kill(): boolean;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function startBackground(
|
|
501
|
+
ctx: BashTerminalContext,
|
|
502
|
+
argv: string[],
|
|
503
|
+
resolved: SpawnResolution,
|
|
504
|
+
env: Record<string, string | undefined>,
|
|
505
|
+
signal: AbortSignal
|
|
506
|
+
): BackgroundProc {
|
|
507
|
+
const subprocess = requireSubprocess(ctx);
|
|
508
|
+
const running = subprocess.spawn(spawnSpec(resolved, argv, env, signal));
|
|
509
|
+
const collected = collectedOutput(running);
|
|
510
|
+
let stdoutOffset = 0;
|
|
511
|
+
let stderrOffset = 0;
|
|
512
|
+
let spawnFailureNote: string | undefined;
|
|
513
|
+
const consumeSpawnFailure = (): string => {
|
|
514
|
+
const note = spawnFailureNote ?? "";
|
|
515
|
+
spawnFailureNote = undefined;
|
|
516
|
+
return note;
|
|
517
|
+
};
|
|
518
|
+
const proc: BackgroundProc = {
|
|
519
|
+
status: "running",
|
|
520
|
+
exitCode: null,
|
|
521
|
+
signal: null,
|
|
522
|
+
done: running.done.then((outcome) => {
|
|
523
|
+
if (proc.status === "running") {
|
|
524
|
+
proc.status = signal?.aborted === true || outcome.signal !== null ? "killed" : "completed";
|
|
525
|
+
}
|
|
526
|
+
proc.exitCode = outcome.exitCode;
|
|
527
|
+
proc.signal = outcome.signal;
|
|
528
|
+
}, (error: unknown) => {
|
|
529
|
+
proc.status = "killed";
|
|
530
|
+
spawnFailureNote = `spawn failed: ${String(error)}`;
|
|
531
|
+
}),
|
|
532
|
+
readOutput: () => {
|
|
533
|
+
const out = collected.stdout.readFrom(stdoutOffset);
|
|
534
|
+
const err = collected.stderr.readFrom(stderrOffset);
|
|
535
|
+
stdoutOffset = out.nextOffset;
|
|
536
|
+
stderrOffset = err.nextOffset;
|
|
537
|
+
const errText = err.text.length > 0 ? err.text : consumeSpawnFailure();
|
|
538
|
+
const separator = out.text.length > 0 && !out.text.endsWith("\n") ? "\n" : "";
|
|
539
|
+
return {
|
|
540
|
+
delta: out.text + (errText.length > 0 ? `${separator}[stderr]\n${errText}` : ""),
|
|
541
|
+
lossy: out.lossy || err.lossy,
|
|
542
|
+
...(out.spillPath !== undefined ? { stdoutSpillPath: out.spillPath } : {}),
|
|
543
|
+
...(err.spillPath !== undefined ? { stderrSpillPath: err.spillPath } : {})
|
|
544
|
+
};
|
|
545
|
+
},
|
|
546
|
+
kill: () => {
|
|
547
|
+
if (proc.status !== "running") return false;
|
|
548
|
+
proc.status = "killed";
|
|
549
|
+
running.terminate();
|
|
550
|
+
return true;
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
return proc;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function processOutcome(proc: BackgroundProc): { status: string; detail: string } {
|
|
557
|
+
if (proc.status === "killed") {
|
|
558
|
+
return { status: "killed", detail: proc.signal !== null ? `signal: ${proc.signal}` : "killed before exit" };
|
|
559
|
+
}
|
|
560
|
+
return { status: "completed", detail: `exit code: ${proc.exitCode ?? 0}` };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// ---- model-facing rendering ---------------------------------------------------
|
|
564
|
+
|
|
565
|
+
function streamText(output: StreamOutput): string {
|
|
566
|
+
if (!output.truncated) return output.text;
|
|
567
|
+
return `${output.text}\n[output truncated; full output: ${output.spillPath ?? "(unavailable)"}]`;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export interface RenderedResult {
|
|
571
|
+
stdout: StreamOutput;
|
|
572
|
+
stderr: StreamOutput;
|
|
573
|
+
exitCode: number | null;
|
|
574
|
+
signal: string | null;
|
|
575
|
+
timedOut: boolean;
|
|
576
|
+
timeoutMs: number;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export function renderResult(result: RenderedResult): string {
|
|
580
|
+
const out = streamText(result.stdout);
|
|
581
|
+
const err = streamText(result.stderr);
|
|
582
|
+
let body = out;
|
|
583
|
+
if (err.length > 0) {
|
|
584
|
+
if (body.length > 0 && !body.endsWith("\n")) body += "\n";
|
|
585
|
+
body += `[stderr]\n${err}`;
|
|
586
|
+
}
|
|
587
|
+
if (body.length === 0) body = "(no output)";
|
|
588
|
+
const markers: string[] = [];
|
|
589
|
+
if (result.timedOut) markers.push(`[timed out after ${result.timeoutMs}ms]`);
|
|
590
|
+
if (result.signal !== null) markers.push(`[killed by signal: ${result.signal}]`);
|
|
591
|
+
else if (result.exitCode !== 0) markers.push(`[exit code: ${result.exitCode}]`);
|
|
592
|
+
if (markers.length === 0) return body;
|
|
593
|
+
if (!body.endsWith("\n")) body += "\n";
|
|
594
|
+
return body + markers.join("\n");
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function renderProcessRead(read: ProcessRead): string {
|
|
598
|
+
const notices: string[] = [];
|
|
599
|
+
if (read.lossy) {
|
|
600
|
+
const paths = [read.stdoutSpillPath, read.stderrSpillPath].filter((p) => p !== undefined);
|
|
601
|
+
notices.push(`[some output was dropped from memory; full output: ${paths.length > 0 ? paths.join(", ") : "(unavailable)"}]`);
|
|
602
|
+
}
|
|
603
|
+
if (notices.length === 0) return read.delta;
|
|
604
|
+
return `${read.delta}${read.delta.length > 0 && !read.delta.endsWith("\n") ? "\n" : ""}${notices.join("\n")}`;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// ---- tool description / validation --------------------------------------------
|
|
608
|
+
|
|
609
|
+
/** Model-facing lead sentence for each backend. The user's default terminal is
|
|
610
|
+
* stated up front so the model never has to guess which syntax applies. */
|
|
611
|
+
export const SHELL_DESCRIPTIONS: Record<ShellId, string> = {
|
|
612
|
+
powershell: "Execute a PowerShell command (pwsh -NoLogo -NoProfile -NonInteractive -Command <command>) and return its stdout/stderr. PowerShell syntax; native Windows paths (C:\\...); environment variables via $env:NAME.",
|
|
613
|
+
gitbash: "Execute a bash command (Git for Windows bash -lc <command>) and return its stdout/stderr. POSIX syntax; paths like /d/WorkSpace; PATH includes /usr/bin and /mingw64/bin so git, npm, ssh etc. work; environment variables via $NAME.",
|
|
614
|
+
msys2: "Execute a bash command (MSYS2 bash -lc <command>) and return its stdout/stderr. POSIX syntax; paths like /c/...; PATH includes /usr/bin and /mingw64/bin so git, npm, gcc, make etc. work; environment variables via $NAME. MSYS2 provides a full GCC/mingw64 toolchain.",
|
|
615
|
+
wsl: "Execute a Linux bash command (wsl [-d <distro>] -e bash -lc <command>) and return its stdout/stderr. Linux syntax; Windows files under /mnt/d/...; environment variables via $NAME.",
|
|
616
|
+
niu: "Execute a bash command (Niubash niu -lc <command>) and return its stdout/stderr. GNU bash 5.3 semantics; WinuxCmd applets (ls, cp, rm, grep, find, cat, sed, awk, ...) are on PATH; native Windows paths such as C:\\... are kept as-is while /c/... style arguments are translated; environment variables via $NAME. Niubash is a native binary with no MSYS emulation layer, so /dev/* redirects other than /dev/null do not exist; its startup file is ~/.niubashrc."
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Render the model-facing tool description for one backend. The backend is
|
|
621
|
+
* whatever the user chose in Settings -> General -> Default terminal; the
|
|
622
|
+
* description names it explicitly so the model writes the right syntax.
|
|
623
|
+
* @param backgroundEnabled - advertise `run_in_background` controls.
|
|
624
|
+
* @param shell - active backend; unknown values fall back to the default.
|
|
625
|
+
*/
|
|
626
|
+
export function toolDescription(backgroundEnabled: boolean, shell: string = DEFAULT_SHELL): string {
|
|
627
|
+
const active: ShellId = (SHELLS as readonly string[]).includes(shell) ? (shell as ShellId) : DEFAULT_SHELL;
|
|
628
|
+
const lead = SHELL_DESCRIPTIONS[active];
|
|
629
|
+
const base = [
|
|
630
|
+
`The user's chosen default terminal (Settings -> General -> Default terminal) is ${active}; commands run there.`,
|
|
631
|
+
lead,
|
|
632
|
+
"Each call spawns a fresh shell: no state (cwd, variables, aliases) persists between calls - pass workdir instead of using cd. Non-zero exits are reported as [exit code: N] markers; investigate failures before moving on. Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. Commands run under the DSH sandbox: confined modes (read-only / workspace-write) are enforced through ctx.sandbox and deny fail-closed for PowerShell; danger-full-access, Git Bash, MSYS2, the pinned POSIX payload, Niubash, and WSL run unconfined."
|
|
633
|
+
].join(" ");
|
|
634
|
+
if (!backgroundEnabled) return base;
|
|
635
|
+
return base + " Set run_in_background: true for long-running commands: the call returns a job id immediately; read its output with job_output and stop it with job_kill. No timeout applies to background runs.";
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/** The shell tool's arguments after runtime validation. */
|
|
639
|
+
export type ValidatedShellArgs = {
|
|
640
|
+
command: string;
|
|
641
|
+
description: string;
|
|
642
|
+
timeoutMs?: number;
|
|
643
|
+
distro?: string;
|
|
644
|
+
sandbox_permissions?: string;
|
|
645
|
+
justification?: string;
|
|
646
|
+
workdir?: string;
|
|
647
|
+
run_in_background?: boolean;
|
|
648
|
+
stdin?: string;
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
export function validateArgs(args: Record<string, unknown>): ValidatedShellArgs {
|
|
652
|
+
if (typeof args.command !== "string" || args.command.trim().length === 0) {
|
|
653
|
+
throw new Error("invalid command: expected a non-empty string");
|
|
654
|
+
}
|
|
655
|
+
if (typeof args.description !== "string" || args.description.trim().length === 0) {
|
|
656
|
+
throw new Error("invalid description: expected a non-empty string");
|
|
657
|
+
}
|
|
658
|
+
if (args.timeoutMs !== undefined && (typeof args.timeoutMs !== "number" || !Number.isFinite(args.timeoutMs) || args.timeoutMs <= 0)) {
|
|
659
|
+
throw new Error(`invalid timeoutMs: expected a positive number, got ${JSON.stringify(args.timeoutMs)}`);
|
|
660
|
+
}
|
|
661
|
+
validateEscalationArgs(args.sandbox_permissions, args.justification);
|
|
662
|
+
return args as ValidatedShellArgs;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Official sandbox seam (mirrors dsh-tool-bash / dsh-pwsh-sandbox): resolve the
|
|
667
|
+
* per-call policy from ctx.sandboxPolicy, and — unless the call runs
|
|
668
|
+
* danger-full-access — confine the spawn argv through ctx.sandbox. WSL is a
|
|
669
|
+
* self-contained Linux VM and is not confined (its isolation IS the sandbox).
|
|
670
|
+
* Git Bash and MSYS2 are also not confined: DSH's Windows ACL restricted-token
|
|
671
|
+
* runner cannot start Cygwin/MSYS2 (CreateFileMapping Win32 error 5), so
|
|
672
|
+
* attempting to wrap them would abort every Git Bash / MSYS2 command. A
|
|
673
|
+
* requested confined mode
|
|
674
|
+
* with no usable backend throws the fail-closed SandboxUnavailableError,
|
|
675
|
+
* exactly like the shipped executors.
|
|
676
|
+
*/
|
|
677
|
+
function confineSpawn(
|
|
678
|
+
ctx: BashTerminalContext,
|
|
679
|
+
argv: string[],
|
|
680
|
+
policy: SandboxPolicy,
|
|
681
|
+
shell: string
|
|
682
|
+
): { argv: string[]; sandbox?: SandboxFacts } {
|
|
683
|
+
if (policy.mode === "danger-full-access" || shell === "wsl" || shell === "gitbash" || shell === "msys2" || shell === "payload") {
|
|
684
|
+
const sandbox = shell === "wsl"
|
|
685
|
+
? { mode: policy.mode, enforcement: "wsl-isolation" }
|
|
686
|
+
: shell === "gitbash"
|
|
687
|
+
? { mode: policy.mode, enforcement: "gitbash-unconfined" }
|
|
688
|
+
: shell === "msys2"
|
|
689
|
+
? { mode: policy.mode, enforcement: "msys2-unconfined" }
|
|
690
|
+
: shell === "payload"
|
|
691
|
+
// The payload cannot be confined either (same Cygwin/MSYS runtime
|
|
692
|
+
// constraint as Git Bash) - its safety layer is the digest gate.
|
|
693
|
+
? { mode: policy.mode, enforcement: "payload-unconfined" }
|
|
694
|
+
: undefined;
|
|
695
|
+
return { argv, sandbox };
|
|
696
|
+
}
|
|
697
|
+
const confined = ctx.sandbox.confine(argv, policy);
|
|
698
|
+
return {
|
|
699
|
+
argv: confined.argv,
|
|
700
|
+
sandbox: { mode: policy.mode, enforcement: confined.enforcement, denialSignatures: confined.denialSignatures }
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function resolveWorkdir(modelWorkdir: string | undefined, exec: ToolRunContext): string | undefined {
|
|
705
|
+
const headerCwd = exec.agent?.session.header.cwd;
|
|
706
|
+
if (modelWorkdir === undefined) return headerCwd;
|
|
707
|
+
if (headerCwd !== undefined && !isAbsolute(modelWorkdir)) return resolve(headerCwd, modelWorkdir);
|
|
708
|
+
return modelWorkdir;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
export interface ForegroundResult {
|
|
712
|
+
kind: "foreground";
|
|
713
|
+
exitCode: number | null;
|
|
714
|
+
signal: string | null;
|
|
715
|
+
timedOut: boolean;
|
|
716
|
+
aborted: boolean;
|
|
717
|
+
timeoutMs: number;
|
|
718
|
+
stdout: StreamOutput;
|
|
719
|
+
stderr: StreamOutput;
|
|
720
|
+
sandbox?: SandboxFacts;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
export interface BackgroundResult {
|
|
724
|
+
kind: "background";
|
|
725
|
+
jobId: string;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
export type ShellToolResult = ForegroundResult | BackgroundResult;
|
|
729
|
+
|
|
730
|
+
function canonicalResult(result: ForegroundOutcome & { sandbox?: SandboxFacts }): ForegroundResult {
|
|
731
|
+
const output = (stream: StreamOutput): StreamOutput => ({
|
|
732
|
+
text: stream.text,
|
|
733
|
+
truncated: stream.truncated,
|
|
734
|
+
...(stream.spillPath !== undefined ? { spillPath: stream.spillPath } : {})
|
|
735
|
+
});
|
|
736
|
+
return {
|
|
737
|
+
kind: "foreground",
|
|
738
|
+
exitCode: result.exitCode,
|
|
739
|
+
signal: result.signal,
|
|
740
|
+
timedOut: result.timedOut,
|
|
741
|
+
aborted: result.aborted,
|
|
742
|
+
timeoutMs: result.timeoutMs,
|
|
743
|
+
stdout: output(result.stdout),
|
|
744
|
+
stderr: output(result.stderr),
|
|
745
|
+
...(result.sandbox !== undefined ? { sandbox: result.sandbox } : {})
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const BACKGROUND_OUTPUT_PROPERTIES: Record<string, JsonSchemaNode> = {
|
|
750
|
+
kind: { type: "string", required: true, const: "background" },
|
|
751
|
+
jobId: { type: "string", required: true }
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Persist one config path through the host settings service.
|
|
756
|
+
*
|
|
757
|
+
* Shared by the model tool's `defaultShell` write and the browser terminal's
|
|
758
|
+
* activation switches, because the line-dependent part is the whole difficulty:
|
|
759
|
+
* ≤0.1.6 loaders do not register a plugin's settings namespace automatically, so
|
|
760
|
+
* the first `mutate` fails with "not registered" and the caller has to register it
|
|
761
|
+
* (with the written value as the base, so the retry sees it) and retry. On rc.1+
|
|
762
|
+
* the namespace is already owned by the loader, that branch never fires, and the
|
|
763
|
+
* plain `mutate` is the whole story.
|
|
764
|
+
* @param svc - the host `settings` service (untyped: shapes differ per line).
|
|
765
|
+
* @param path - the config path inside this plugin's entry (one level today).
|
|
766
|
+
* @throws when the line has no `mutate` at all - the caller reports it, and the
|
|
767
|
+
* value then applies for this process only.
|
|
768
|
+
*/
|
|
769
|
+
export async function persistConfigValue(svc: unknown, path: readonly string[], value: unknown): Promise<void> {
|
|
770
|
+
const service = svc as {
|
|
771
|
+
mutate?: (id: string, ops: { op: "set"; path: string[]; value: unknown }[]) => Promise<unknown>;
|
|
772
|
+
register?: (id: string, schema: unknown, opts?: unknown) => unknown;
|
|
773
|
+
};
|
|
774
|
+
if (typeof service?.mutate !== "function") {
|
|
775
|
+
throw new Error(
|
|
776
|
+
"settings service exposes no mutate on this host line - the value applies for this process only; persist via cordis.patch.yml"
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
const ops = [{ op: "set" as const, path: [...path], value }];
|
|
780
|
+
try {
|
|
781
|
+
await service.mutate(ENTRY_ID, ops);
|
|
782
|
+
} catch (e) {
|
|
783
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
784
|
+
if (!/not registered/i.test(message) || typeof service.register !== "function") {
|
|
785
|
+
throw e;
|
|
786
|
+
}
|
|
787
|
+
console.warn(`[dsh-bash-terminal-ts] legacy line: registering settings namespace ${ENTRY_ID}`);
|
|
788
|
+
service.register(ENTRY_ID, Config, { base: { [path.join(".")]: value } });
|
|
789
|
+
await service.mutate(ENTRY_ID, ops);
|
|
790
|
+
console.warn("[dsh-bash-terminal-ts] legacy settings namespace registered");
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// ---- plugin -------------------------------------------------------------------
|
|
795
|
+
|
|
796
|
+
export function apply(ctx: BashTerminalContext, config: Partial<ConfigValues> = {}): void {
|
|
797
|
+
const paths = resolveAllPaths(config, process.env);
|
|
798
|
+
// 0.1.7: `defaultShell` is a `.volatile()` config field — the loader hands
|
|
799
|
+
// `apply` a live ref and the latest value is resolved on every read; there
|
|
800
|
+
// is no settings registration call anymore (removed with the 0.1.7 host).
|
|
801
|
+
// compat-legacy: ≤0.1.6 的宿主没有配置热重载,bridge 写入走这个覆盖值
|
|
802
|
+
// (rc.1+ 上 volatile 与覆盖值同源同写,二者一致)。
|
|
803
|
+
let shellOverride: string | undefined;
|
|
804
|
+
/** The stored value, verbatim - including one that is no longer selectable. */
|
|
805
|
+
const storedShell = (): string => {
|
|
806
|
+
if (typeof shellOverride === "string" && (SHELLS as readonly string[]).includes(shellOverride)) {
|
|
807
|
+
return shellOverride;
|
|
808
|
+
}
|
|
809
|
+
const v = config.defaultShell as string | VolatileRef<string> | undefined;
|
|
810
|
+
if (typeof v === "string") return v;
|
|
811
|
+
const got = typeof v?.get === "function" ? v.get() : undefined;
|
|
812
|
+
return typeof got === "string" ? got : DEFAULT_SHELL;
|
|
813
|
+
};
|
|
814
|
+
let warnedStale = false;
|
|
815
|
+
/**
|
|
816
|
+
* The shell the TOOL will run. Deliberately the stored value, un-sanitised:
|
|
817
|
+
* `buildArgv`/`terminalArgv` throw for anything outside SHELLS, so an
|
|
818
|
+
* out-of-enum choice fails closed at call time rather than quietly running the
|
|
819
|
+
* command in a different shell than the model was told about.
|
|
820
|
+
*/
|
|
821
|
+
const currentShell = storedShell;
|
|
822
|
+
/**
|
|
823
|
+
* The activation switches are ONE truth: a switch off hides the backend from
|
|
824
|
+
* the dock AND makes it an invalid shell-tool target (powershell is the
|
|
825
|
+
* floor and always on). A stored defaultShell pointing at a switched-off
|
|
826
|
+
* backend is therefore stale, not executable.
|
|
827
|
+
*/
|
|
828
|
+
const switchOn = (shell: string): boolean => {
|
|
829
|
+
const read = <T>(v: T | VolatileRef<T> | undefined, fallback: T): T =>
|
|
830
|
+
typeof v === "object" && v !== null && typeof (v as VolatileRef<T>).get === "function"
|
|
831
|
+
? ((v as VolatileRef<T>).get() ?? fallback)
|
|
832
|
+
: ((v as T | undefined) ?? fallback);
|
|
833
|
+
if (shell === "powershell") return true;
|
|
834
|
+
if (shell === "gitbash") return read(config.shellGitBash as boolean | undefined, true);
|
|
835
|
+
if (shell === "msys2") return read(config.shellMsys2 as boolean | undefined, true);
|
|
836
|
+
if (shell === "wsl") return read(config.shellWsl as boolean | undefined, true);
|
|
837
|
+
if (shell === "niu") return read(config.shellNiu as boolean | undefined, true);
|
|
838
|
+
return false;
|
|
839
|
+
};
|
|
840
|
+
/**
|
|
841
|
+
* What the model is TOLD. Sanitised, because this one runs on every prompt
|
|
842
|
+
* assembly: an invalid stored value must not break the whole prompt, and naming
|
|
843
|
+
* the floor is honest about what a successful call would use.
|
|
844
|
+
*/
|
|
845
|
+
const describedShell = (): string => {
|
|
846
|
+
const stored = storedShell();
|
|
847
|
+
if ((SHELLS as readonly string[]).includes(stored) && switchOn(stored)) return stored;
|
|
848
|
+
if (!warnedStale) {
|
|
849
|
+
warnedStale = true;
|
|
850
|
+
ctx.logger?.warn?.(`dsh-bash-terminal-ts: defaultShell ${JSON.stringify(stored)} is not a selectable/activated backend; shell calls will fail closed and the bottom terminal falls back to ${DEFAULT_SHELL}`);
|
|
851
|
+
}
|
|
852
|
+
return DEFAULT_SHELL;
|
|
853
|
+
};
|
|
854
|
+
|
|
855
|
+
// The browser terminal (bottom dock / embedded shell). Mounted BEFORE the
|
|
856
|
+
// win32 return below: the model tool is Windows-only, the terminal is not
|
|
857
|
+
// (finding F1). Its routes report their own availability, so a host without
|
|
858
|
+
// `registerUpgrade` or without any PTY backend shows a reason instead of a
|
|
859
|
+
// dead panel.
|
|
860
|
+
const browserTerminals = mountBrowserTerminals({
|
|
861
|
+
ctx: ctx as never,
|
|
862
|
+
config: config as Record<string, unknown>,
|
|
863
|
+
paths,
|
|
864
|
+
configuredShell: storedShell,
|
|
865
|
+
persist: async (svc, path, value) => {
|
|
866
|
+
await persistConfigValue(svc, path, value);
|
|
867
|
+
// ≤0.1.6 hosts have no config hot-reload, so mirror a defaultShell write into
|
|
868
|
+
// the same in-process override the legacy bridge uses. Without it, choosing a
|
|
869
|
+
// shell in the bottom-terminal section would only reach the model tool after
|
|
870
|
+
// a restart - the exact divergence the bridge's `shellOverride` exists to kill.
|
|
871
|
+
if (path.length === 1 && path[0] === "defaultShell" && typeof value === "string") {
|
|
872
|
+
shellOverride = value;
|
|
873
|
+
}
|
|
874
|
+
},
|
|
875
|
+
});
|
|
876
|
+
ctx.effect(() => () => browserTerminals.dispose(), "bash-terminal: browser terminal teardown");
|
|
877
|
+
|
|
878
|
+
if (process.platform !== "win32") {
|
|
879
|
+
ctx.logger?.info?.("dsh-bash-terminal-ts: only meaningful on win32; skipping tool registration");
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
const backgroundEnabled = true;
|
|
883
|
+
// R5/R6 and finding F8: an invalid stored value used to throw here, which failed
|
|
884
|
+
// the whole entry - the tool, the settings row and the browser terminal all
|
|
885
|
+
// disappeared. The entry now stays mounted, the tool fails closed per call, and
|
|
886
|
+
// the browser terminal substitutes the floor and says so.
|
|
887
|
+
/** Official sandbox-escalation surface (mirrors tool-bash): advertise the
|
|
888
|
+
* escalation modes whenever the deployment confines. */
|
|
889
|
+
const escalationModes = ESCALATION_TARGETS;
|
|
890
|
+
const approveShellEscalation = (
|
|
891
|
+
mode: string,
|
|
892
|
+
justification: string,
|
|
893
|
+
exec: ToolRunContext,
|
|
894
|
+
standingPolicy: SandboxPolicy
|
|
895
|
+
): Promise<string> => {
|
|
896
|
+
return approveEscalation({
|
|
897
|
+
requestedMode: mode,
|
|
898
|
+
justification,
|
|
899
|
+
effectiveMode: standingPolicy.mode,
|
|
900
|
+
subject: "command"
|
|
901
|
+
}, {
|
|
902
|
+
approver: ctx.get("approval"),
|
|
903
|
+
agent: exec.agent,
|
|
904
|
+
callId: exec.callId,
|
|
905
|
+
toolName: "shell",
|
|
906
|
+
signal: exec.signal
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
ctx.systemPrompt.section({
|
|
911
|
+
name: "tool:bash-terminal",
|
|
912
|
+
order: 105,
|
|
913
|
+
text: "Use the shell tool for terminal commands: it runs in the terminal the user chose in Settings -> General -> Default terminal (PowerShell, Git Bash, MSYS2, or WSL) and honors the DSH sandbox. Prefer it over the pwsh tool for everyday commands; keep the pwsh tool for cases that specifically need the sandboxed PowerShell surface."
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
// compat-legacy: 六线统一配置通道(≤0.1.6 无 client-store 模块表,client 走本
|
|
917
|
+
// bridge 读写 defaultShell)。写路径 = settings.mutate;≤0.1.6 的 loader 不会自动
|
|
918
|
+
// 注册命名空间,复用 perm-gate 自门控(not registered → register → 重试);
|
|
919
|
+
// rc.1+ 命名空间由 loader 占用,该分支自然不命中。GET 带回当前值与可选集。
|
|
920
|
+
// 宿主 context 类型未收录 inject/局部服务形状,compat 桥按宽化面调用。
|
|
921
|
+
// 窄面型只覆盖本桥用到的三个字段,避免 any 外泄。
|
|
922
|
+
interface BridgeReq {
|
|
923
|
+
socket?: { remoteAddress?: string };
|
|
924
|
+
method?: string;
|
|
925
|
+
on(event: "data", cb: (chunk: Buffer) => void): void;
|
|
926
|
+
on(event: "end", cb: () => void): void;
|
|
927
|
+
}
|
|
928
|
+
interface BridgeRes {
|
|
929
|
+
writeHead(status: number, headers: Record<string, string>): void;
|
|
930
|
+
end(body: string): void;
|
|
931
|
+
}
|
|
932
|
+
type BridgeSvcCtx = {
|
|
933
|
+
settings: unknown;
|
|
934
|
+
webServer: { register: (route: { kind: string; path: string; handler: (req: BridgeReq, res: BridgeRes) => void }) => () => void };
|
|
935
|
+
effect: (fn: () => (() => void) | void, label?: string) => void;
|
|
936
|
+
};
|
|
937
|
+
const compatCtx = ctx as unknown as { inject?: (deps: string[], fn: (svcCtx: BridgeSvcCtx) => void) => void };
|
|
938
|
+
if (typeof compatCtx.inject !== "function") {
|
|
939
|
+
// A context without `inject` (a bare mock, or a host that resolves services
|
|
940
|
+
// only synchronously) cannot receive the bridge at all. Skipping it must not
|
|
941
|
+
// take the tool down with it - the tool is what the model calls.
|
|
942
|
+
ctx.logger?.warn?.("dsh-bash-terminal-ts: ctx.inject unavailable - the settings bridge is not mounted (defaultShell reads still work, writes will not)");
|
|
943
|
+
} else compatCtx.inject(["webServer", "settings"], (svcCtx: BridgeSvcCtx) => {
|
|
944
|
+
const svc = svcCtx.settings;
|
|
945
|
+
const writeShell = async (value: string): Promise<void> => {
|
|
946
|
+
// One implementation for every config write (the browser terminal's switches
|
|
947
|
+
// go through the same helper), so the legacy self-registration dance exists
|
|
948
|
+
// in exactly one place.
|
|
949
|
+
await persistConfigValue(svc, ["defaultShell"], value);
|
|
950
|
+
shellOverride = value;
|
|
951
|
+
};
|
|
952
|
+
svcCtx.effect(
|
|
953
|
+
() => {
|
|
954
|
+
const json = (res: unknown, status: number, body: unknown): void => {
|
|
955
|
+
(res as { writeHead: (s: number, h: Record<string, string>) => void; end: (b: string) => void }).writeHead(status, { "content-type": "application/json; charset=utf-8" });
|
|
956
|
+
(res as { end: (b: string) => void }).end(JSON.stringify(body));
|
|
957
|
+
};
|
|
958
|
+
const handler = (req: BridgeReq, res: BridgeRes): void => {
|
|
959
|
+
const addr = req.socket?.remoteAddress ?? "";
|
|
960
|
+
if (addr !== "127.0.0.1" && addr !== "::1" && addr !== "::ffff:127.0.0.1") {
|
|
961
|
+
json(res, 403, { ok: false, code: "loopback-only" });
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
if (req.method === "GET") {
|
|
965
|
+
// 缝内省:compat 验证用——当前线 ctx.subprocess 的真实能力面
|
|
966
|
+
const seam = ctx as unknown as { subprocess?: Record<string, unknown> };
|
|
967
|
+
const sp = seam.subprocess as { spawn?: unknown; spawnTerminal?: unknown } | undefined;
|
|
968
|
+
json(res, 200, {
|
|
969
|
+
ok: true,
|
|
970
|
+
value: {
|
|
971
|
+
defaultShell: currentShell(),
|
|
972
|
+
shells: SHELLS,
|
|
973
|
+
seam: { spawn: typeof sp?.spawn, spawnTerminal: typeof sp?.spawnTerminal },
|
|
974
|
+
},
|
|
975
|
+
});
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
if (req.method !== "POST") {
|
|
979
|
+
json(res, 405, { ok: false, code: "method-not-allowed" });
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
const chunks: Buffer[] = [];
|
|
983
|
+
let size = 0;
|
|
984
|
+
req.on("data", (c: Buffer) => {
|
|
985
|
+
size += c.length;
|
|
986
|
+
if (size <= 65536) chunks.push(c);
|
|
987
|
+
});
|
|
988
|
+
req.on("end", () => {
|
|
989
|
+
let body: { defaultShell?: unknown };
|
|
990
|
+
try {
|
|
991
|
+
body = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}");
|
|
992
|
+
} catch {
|
|
993
|
+
json(res, 400, { ok: false, code: "bad-json" });
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
const value = typeof body.defaultShell === "string" ? body.defaultShell : "";
|
|
997
|
+
if (!(SHELLS as readonly string[]).includes(value)) {
|
|
998
|
+
json(res, 400, { ok: false, code: "invalid-shell" });
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
void writeShell(value)
|
|
1002
|
+
.then(() => json(res, 200, { ok: true, value: { defaultShell: value } }))
|
|
1003
|
+
.catch((e: unknown) => json(res, 500, { ok: false, code: "write-failed", message: e instanceof Error ? e.message : String(e) }));
|
|
1004
|
+
});
|
|
1005
|
+
};
|
|
1006
|
+
const registered = [
|
|
1007
|
+
svcCtx.webServer.register({ kind: "exact", path: "/api/bash-terminal/config", handler }),
|
|
1008
|
+
];
|
|
1009
|
+
return () => {
|
|
1010
|
+
for (const dispose of registered) dispose();
|
|
1011
|
+
};
|
|
1012
|
+
}, "bash-terminal: compat config bridge");
|
|
1013
|
+
});
|
|
1014
|
+
|
|
1015
|
+
const toolName = "shell";
|
|
1016
|
+
const terminalRegistry = createTerminalRegistry(ctx);
|
|
1017
|
+
// The interactive terminal obeys the SAME activation gate as the shell tool:
|
|
1018
|
+
// the getter throws for a switched-off backend, so a session cannot open on
|
|
1019
|
+
// a shell the user just disabled.
|
|
1020
|
+
ctx.tools.register(terminalTool(ctx, terminalRegistry, paths, () => {
|
|
1021
|
+
const shell = currentShell();
|
|
1022
|
+
if ((SHELLS as readonly string[]).includes(shell) && !switchOn(shell)) {
|
|
1023
|
+
throw new Error(`dsh-bash-terminal-ts: ${shell} backend is not activated - enable its switch in the plugin settings first.`);
|
|
1024
|
+
}
|
|
1025
|
+
return shell;
|
|
1026
|
+
}));
|
|
1027
|
+
|
|
1028
|
+
// The model-facing description must track the user's chosen default
|
|
1029
|
+
// terminal: a static description listing every backend leaves the model
|
|
1030
|
+
// guessing which syntax applies. Re-render it on every prompt assembly —
|
|
1031
|
+
// settings are hot-reloaded, so a change shows up on the next request
|
|
1032
|
+
// without a restart.
|
|
1033
|
+
ctx.on("system-prompt/assemble", async (_assembly, _context, next) => {
|
|
1034
|
+
const assembled = await next();
|
|
1035
|
+
const description = toolDescription(backgroundEnabled, describedShell());
|
|
1036
|
+
const tools = Array.isArray(assembled.tools)
|
|
1037
|
+
? assembled.tools.map((tool) => (tool.name === toolName ? { ...tool, description } : tool))
|
|
1038
|
+
: assembled.tools;
|
|
1039
|
+
return { ...assembled, tools };
|
|
1040
|
+
});
|
|
1041
|
+
|
|
1042
|
+
ctx.tools.register(defineTool<ShellToolResult>({
|
|
1043
|
+
name: toolName,
|
|
1044
|
+
description: toolDescription(backgroundEnabled, currentShell()),
|
|
1045
|
+
parameters: {
|
|
1046
|
+
command: {
|
|
1047
|
+
type: "string",
|
|
1048
|
+
required: true,
|
|
1049
|
+
description: "The command to execute in the selected terminal."
|
|
1050
|
+
},
|
|
1051
|
+
description: {
|
|
1052
|
+
type: "string",
|
|
1053
|
+
required: true,
|
|
1054
|
+
description: "Clear, concise description of what this command does in active voice, 5-10 words (shown in the UI). Examples: \"ls\" -> \"List files in current directory\"; \"git status\" -> \"Show working tree status\"; \"npm install\" -> \"Install package dependencies\"."
|
|
1055
|
+
},
|
|
1056
|
+
distro: {
|
|
1057
|
+
type: "string",
|
|
1058
|
+
description: "WSL distribution to use (only when the configured default terminal is wsl). Defaults to the system default distribution."
|
|
1059
|
+
},
|
|
1060
|
+
sandbox_permissions: {
|
|
1061
|
+
type: "string",
|
|
1062
|
+
enum: escalationModes,
|
|
1063
|
+
description: "The wider sandbox mode this command needs. Only valid as a one-shot retry of a command the sandbox just denied; requires justification and user approval."
|
|
1064
|
+
},
|
|
1065
|
+
justification: {
|
|
1066
|
+
type: "string",
|
|
1067
|
+
description: "Required with sandbox_permissions: one sentence for the user explaining why this exact command needs the wider access."
|
|
1068
|
+
},
|
|
1069
|
+
workdir: {
|
|
1070
|
+
type: "string",
|
|
1071
|
+
description: "Working directory for this command. Defaults to the session workspace; a relative path is resolved against it."
|
|
1072
|
+
},
|
|
1073
|
+
timeoutMs: {
|
|
1074
|
+
type: "number",
|
|
1075
|
+
description: "Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry."
|
|
1076
|
+
},
|
|
1077
|
+
run_in_background: {
|
|
1078
|
+
type: "boolean",
|
|
1079
|
+
description: "Run in the background and return a job id immediately (collect with job_output, stop with job_kill). No timeout applies."
|
|
1080
|
+
}
|
|
1081
|
+
},
|
|
1082
|
+
output: {
|
|
1083
|
+
schema: {
|
|
1084
|
+
oneOf: [
|
|
1085
|
+
{ type: "object", additionalProperties: false, properties: BACKGROUND_OUTPUT_PROPERTIES },
|
|
1086
|
+
{
|
|
1087
|
+
type: "object",
|
|
1088
|
+
additionalProperties: false,
|
|
1089
|
+
properties: {
|
|
1090
|
+
kind: { type: "string", required: true, const: "foreground" },
|
|
1091
|
+
exitCode: { required: true, oneOf: [{ type: "integer" }, { type: "null" }] },
|
|
1092
|
+
signal: { required: true, oneOf: [{ type: "string" }, { type: "null" }] },
|
|
1093
|
+
timedOut: { type: "boolean", required: true },
|
|
1094
|
+
aborted: { type: "boolean", required: true },
|
|
1095
|
+
timeoutMs: { type: "number", required: true },
|
|
1096
|
+
sandbox: {
|
|
1097
|
+
type: "object",
|
|
1098
|
+
additionalProperties: false,
|
|
1099
|
+
properties: {
|
|
1100
|
+
mode: { type: "string", required: true },
|
|
1101
|
+
enforcement: { type: "string", required: true },
|
|
1102
|
+
denied: { type: "boolean" }
|
|
1103
|
+
}
|
|
1104
|
+
},
|
|
1105
|
+
stdout: {
|
|
1106
|
+
type: "object",
|
|
1107
|
+
additionalProperties: false,
|
|
1108
|
+
required: true,
|
|
1109
|
+
properties: {
|
|
1110
|
+
text: { type: "string", required: true },
|
|
1111
|
+
truncated: { type: "boolean", required: true },
|
|
1112
|
+
spillPath: { type: "string" }
|
|
1113
|
+
}
|
|
1114
|
+
},
|
|
1115
|
+
stderr: {
|
|
1116
|
+
type: "object",
|
|
1117
|
+
additionalProperties: false,
|
|
1118
|
+
required: true,
|
|
1119
|
+
properties: {
|
|
1120
|
+
text: { type: "string", required: true },
|
|
1121
|
+
truncated: { type: "boolean", required: true },
|
|
1122
|
+
spillPath: { type: "string" }
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
]
|
|
1128
|
+
},
|
|
1129
|
+
render: (_args, value) => {
|
|
1130
|
+
if (value.kind === "background") return [{ type: "text", text: `started background job ${value.jobId}` }];
|
|
1131
|
+
let text = renderResult(value);
|
|
1132
|
+
if (value.sandbox?.denied === true) {
|
|
1133
|
+
if (!text.endsWith("\n")) text += "\n";
|
|
1134
|
+
text += sandboxDenialMarker(value.sandbox.mode) + "\n" + escalationHintMarker("command");
|
|
1135
|
+
}
|
|
1136
|
+
return [{ type: "text", text }];
|
|
1137
|
+
}
|
|
1138
|
+
},
|
|
1139
|
+
async execute(args, exec) {
|
|
1140
|
+
const v = validateArgs(args);
|
|
1141
|
+
const shell = currentShell();
|
|
1142
|
+
if ((SHELLS as readonly string[]).includes(shell) && !switchOn(shell)) {
|
|
1143
|
+
throw new Error(`dsh-bash-terminal-ts: ${shell} backend is not activated - ask the user to enable its switch in the plugin settings (bottom terminal), or choose another default terminal.`);
|
|
1144
|
+
}
|
|
1145
|
+
const argv0 = buildArgv(shell, v.command, paths, v.distro);
|
|
1146
|
+
if (argv0[0] === undefined) {
|
|
1147
|
+
throw new Error(`dsh-bash-terminal-ts: ${shell} backend unavailable - executable not found. Install it or set the corresponding *Path config.`);
|
|
1148
|
+
}
|
|
1149
|
+
// Only element 0 (the resolved executable) can be undefined; guarded above.
|
|
1150
|
+
const argv = argv0 as string[];
|
|
1151
|
+
const workdir = resolveWorkdir(v.workdir, exec);
|
|
1152
|
+
const timeoutMs = clampTimeout(v.timeoutMs, config.timeoutMs ?? DEFAULT_TIMEOUT_MS, config.maxTimeoutMs ?? MAX_TIMEOUT_MS, "shell timeoutMs");
|
|
1153
|
+
const dshEnv = ctx.shellEnv.collect(exec);
|
|
1154
|
+
const env = buildEnv(shell, dshEnv);
|
|
1155
|
+
let policy = ctx.sandboxPolicy.resolve(exec.agent ? { session: exec.agent.session } : {});
|
|
1156
|
+
if (v.sandbox_permissions !== undefined && v.justification !== undefined) {
|
|
1157
|
+
const approvedMode = await approveShellEscalation(v.sandbox_permissions, v.justification, exec, policy);
|
|
1158
|
+
policy = { ...policy, mode: approvedMode };
|
|
1159
|
+
}
|
|
1160
|
+
const { argv: confinedArgv, sandbox } = confineSpawn(ctx, argv, policy, shell);
|
|
1161
|
+
const resolved: SpawnResolution = {
|
|
1162
|
+
command: v.command,
|
|
1163
|
+
workdir: workdir ?? process.cwd(),
|
|
1164
|
+
timeoutMs,
|
|
1165
|
+
stdoutMaxBytes: DEFAULT_MAX_OUTPUT_BYTES,
|
|
1166
|
+
...(v.stdin !== undefined ? { stdin: v.stdin } : {})
|
|
1167
|
+
};
|
|
1168
|
+
if (v.run_in_background === true) {
|
|
1169
|
+
const jobs = ctx.get("jobs") as JobsRegistry | undefined;
|
|
1170
|
+
if (jobs === undefined) throw new Error("background jobs unavailable: load @deepseek-ai/dsh-jobs and @deepseek-ai/dsh-tool-jobs");
|
|
1171
|
+
if (exec.signal.aborted) {
|
|
1172
|
+
const error = new HarnessError("tool call aborted", TOOL_ABORTED);
|
|
1173
|
+
error.name = "AbortError";
|
|
1174
|
+
throw error;
|
|
1175
|
+
}
|
|
1176
|
+
return {
|
|
1177
|
+
kind: "background",
|
|
1178
|
+
jobId: jobs.start({
|
|
1179
|
+
kind: `shell/${shell}`,
|
|
1180
|
+
label: v.command,
|
|
1181
|
+
...(exec.agent ? { owner: exec.agent } : {}),
|
|
1182
|
+
run: () => {
|
|
1183
|
+
const proc = startBackground(ctx, confinedArgv, resolved, env, exec.signal);
|
|
1184
|
+
return {
|
|
1185
|
+
cancel: () => void proc.kill(),
|
|
1186
|
+
done: proc.done.then(() => processOutcome(proc)),
|
|
1187
|
+
readOutput: () => renderProcessRead(proc.readOutput())
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
})
|
|
1191
|
+
};
|
|
1192
|
+
}
|
|
1193
|
+
const result = await runForeground(ctx, confinedArgv, resolved, env, exec.signal, timeoutMs);
|
|
1194
|
+
if (result.aborted) {
|
|
1195
|
+
const error = new HarnessError("tool call aborted", TOOL_ABORTED);
|
|
1196
|
+
error.name = "AbortError";
|
|
1197
|
+
throw error;
|
|
1198
|
+
}
|
|
1199
|
+
const denied = sandbox?.denialSignatures !== undefined
|
|
1200
|
+
? sandbox.denialSignatures.some((sig) => result.stderr.text.toLowerCase().includes(sig.toLowerCase()))
|
|
1201
|
+
: false;
|
|
1202
|
+
return canonicalResult({
|
|
1203
|
+
...result,
|
|
1204
|
+
...(sandbox !== undefined ? { sandbox: { mode: sandbox.mode, enforcement: sandbox.enforcement, denied } } : {})
|
|
1205
|
+
});
|
|
1206
|
+
},
|
|
1207
|
+
presentCall: (args) => {
|
|
1208
|
+
if (args.run_in_background === true) {
|
|
1209
|
+
return {
|
|
1210
|
+
card: "generic",
|
|
1211
|
+
title: args.command as string,
|
|
1212
|
+
kind: "execute",
|
|
1213
|
+
rawInput: args.command,
|
|
1214
|
+
content: [{ type: "text", text: args.description as string }]
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
return {
|
|
1218
|
+
card: "terminal",
|
|
1219
|
+
title: args.command as string,
|
|
1220
|
+
description: args.description as string,
|
|
1221
|
+
...(args.workdir !== undefined ? { cwd: args.workdir } : {})
|
|
1222
|
+
};
|
|
1223
|
+
},
|
|
1224
|
+
presentResult: (args, result) => {
|
|
1225
|
+
const block = result.content.length === 1 ? result.content[0] : undefined;
|
|
1226
|
+
if (block === undefined || block.type !== "text") return undefined;
|
|
1227
|
+
const raw = block.text ?? "";
|
|
1228
|
+
if (args.run_in_background === true || result.isError) {
|
|
1229
|
+
return { card: "generic", content: [{ type: "text", text: "```console\n" + raw.replace(/\n+$/, "") + "\n```" }] };
|
|
1230
|
+
}
|
|
1231
|
+
const { body, ...exit } = parseExitStatus(raw);
|
|
1232
|
+
return { card: "terminal", output: body, ...exit };
|
|
1233
|
+
}
|
|
1234
|
+
}));
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
//#region internals for tests
|
|
1238
|
+
export const internals = {
|
|
1239
|
+
candidateExists,
|
|
1240
|
+
resolveAllPaths,
|
|
1241
|
+
resolveWorkdir,
|
|
1242
|
+
renderResult,
|
|
1243
|
+
processOutcome,
|
|
1244
|
+
startBackground,
|
|
1245
|
+
runForeground,
|
|
1246
|
+
spawnSpec,
|
|
1247
|
+
buildEnv,
|
|
1248
|
+
buildArgv,
|
|
1249
|
+
validateArgs,
|
|
1250
|
+
toolDescription,
|
|
1251
|
+
SHELL_DESCRIPTIONS,
|
|
1252
|
+
DEFAULT_TIMEOUT_MS,
|
|
1253
|
+
MAX_TIMEOUT_MS,
|
|
1254
|
+
DEFAULT_MAX_OUTPUT_BYTES
|
|
1255
|
+
};
|
|
1256
|
+
//#endregion
|
|
1257
|
+
|
|
1258
|
+
// re-export for plugin consumers (type-only)
|
|
1259
|
+
export type {
|
|
1260
|
+
ExecAgent,
|
|
1261
|
+
JobsRegistry,
|
|
1262
|
+
JsonSchemaNode,
|
|
1263
|
+
SandboxFacts,
|
|
1264
|
+
SandboxPolicy,
|
|
1265
|
+
ShellId,
|
|
1266
|
+
ToolRunContext
|
|
1267
|
+
};
|