@linxin666/dsh-win-bash 0.0.0-stage → 0.4.7

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 (118) hide show
  1. package/LICENSE +202 -0
  2. package/PROVENANCE.md +215 -0
  3. package/README.i18n.yaml +6 -0
  4. package/README.md +257 -2
  5. package/README.zh.md +217 -0
  6. package/cordis.patch.yml +13 -0
  7. package/lib/index.js +2697 -0
  8. package/lib/types/background.d.ts +169 -0
  9. package/lib/types/config.d.ts +130 -0
  10. package/lib/types/gate.d.ts +179 -0
  11. package/lib/types/index.d.ts +104 -0
  12. package/lib/types/protocol.d.ts +90 -0
  13. package/lib/types/pwsh-replacement.d.ts +140 -0
  14. package/lib/types/runtime.d.ts +115 -0
  15. package/lib/types/shell.d.ts +184 -0
  16. package/lib/types/tool.d.ts +108 -0
  17. package/package.json +58 -3
  18. package/runtime/win32-x64/checksums.sha256 +91 -0
  19. package/runtime/win32-x64/etc/bash.bashrc +69 -0
  20. package/runtime/win32-x64/etc/fstab +6 -0
  21. package/runtime/win32-x64/etc/msystem +27 -0
  22. package/runtime/win32-x64/etc/msystem.d/CLANG64 +7 -0
  23. package/runtime/win32-x64/etc/msystem.d/CLANGARM64 +7 -0
  24. package/runtime/win32-x64/etc/msystem.d/MINGW32 +7 -0
  25. package/runtime/win32-x64/etc/msystem.d/MINGW64 +7 -0
  26. package/runtime/win32-x64/etc/msystem.d/MSYS +4 -0
  27. package/runtime/win32-x64/etc/msystem.d/UCRT64 +7 -0
  28. package/runtime/win32-x64/etc/nsswitch.conf +12 -0
  29. package/runtime/win32-x64/etc/package-versions.txt +67 -0
  30. package/runtime/win32-x64/etc/profile +146 -0
  31. package/runtime/win32-x64/etc/profile.d/bash_profile.sh +11 -0
  32. package/runtime/win32-x64/etc/profile.d/lang.sh +19 -0
  33. package/runtime/win32-x64/licenses/GPL-2.0.txt +338 -0
  34. package/runtime/win32-x64/licenses/GPL-3.0.txt +674 -0
  35. package/runtime/win32-x64/licenses/LGPL-2.1.txt +501 -0
  36. package/runtime/win32-x64/licenses/LGPL-3.0.txt +165 -0
  37. package/runtime/win32-x64/licenses/expat-COPYING.txt +21 -0
  38. package/runtime/win32-x64/licenses/gcc-libs-RUNTIME.LIBRARY.EXCEPTION.txt +73 -0
  39. package/runtime/win32-x64/licenses/gettext-runtime-COPYING.LIB.txt +501 -0
  40. package/runtime/win32-x64/licenses/gettext-runtime-COPYING.txt +13 -0
  41. package/runtime/win32-x64/licenses/git-for-windows-LICENSE.txt +360 -0
  42. package/runtime/win32-x64/licenses/heimdal-LICENSE.txt +33 -0
  43. package/runtime/win32-x64/licenses/libcbor-LICENSE.md +21 -0
  44. package/runtime/win32-x64/licenses/libffi-LICENSE.txt +21 -0
  45. package/runtime/win32-x64/licenses/libfido2-LICENSE.txt +26 -0
  46. package/runtime/win32-x64/licenses/libiconv-COPYING.LIB.txt +501 -0
  47. package/runtime/win32-x64/licenses/libiconv-COPYING.txt +674 -0
  48. package/runtime/win32-x64/licenses/libopenssl-LICENSE.txt +177 -0
  49. package/runtime/win32-x64/licenses/libpcre-LICENCE.txt +93 -0
  50. package/runtime/win32-x64/licenses/libsqlite-LICENSE +25 -0
  51. package/runtime/win32-x64/licenses/ncurses-LICENSE +29 -0
  52. package/runtime/win32-x64/licenses/openssh-LICENCE.txt +412 -0
  53. package/runtime/win32-x64/licenses/zlib-LICENSE +22 -0
  54. package/runtime/win32-x64/usr/bin/awk.exe +0 -0
  55. package/runtime/win32-x64/usr/bin/b2sum.exe +0 -0
  56. package/runtime/win32-x64/usr/bin/basename.exe +0 -0
  57. package/runtime/win32-x64/usr/bin/basenc.exe +0 -0
  58. package/runtime/win32-x64/usr/bin/bash.exe +0 -0
  59. package/runtime/win32-x64/usr/bin/cat.exe +0 -0
  60. package/runtime/win32-x64/usr/bin/cmp.exe +0 -0
  61. package/runtime/win32-x64/usr/bin/comm.exe +0 -0
  62. package/runtime/win32-x64/usr/bin/cp.exe +0 -0
  63. package/runtime/win32-x64/usr/bin/cut.exe +0 -0
  64. package/runtime/win32-x64/usr/bin/dash.exe +0 -0
  65. package/runtime/win32-x64/usr/bin/date.exe +0 -0
  66. package/runtime/win32-x64/usr/bin/diff.exe +0 -0
  67. package/runtime/win32-x64/usr/bin/diff3.exe +0 -0
  68. package/runtime/win32-x64/usr/bin/dirname.exe +0 -0
  69. package/runtime/win32-x64/usr/bin/echo.exe +0 -0
  70. package/runtime/win32-x64/usr/bin/env.exe +0 -0
  71. package/runtime/win32-x64/usr/bin/expr.exe +0 -0
  72. package/runtime/win32-x64/usr/bin/false.exe +0 -0
  73. package/runtime/win32-x64/usr/bin/find.exe +0 -0
  74. package/runtime/win32-x64/usr/bin/grep.exe +0 -0
  75. package/runtime/win32-x64/usr/bin/head.exe +0 -0
  76. package/runtime/win32-x64/usr/bin/ls.exe +0 -0
  77. package/runtime/win32-x64/usr/bin/mkdir.exe +0 -0
  78. package/runtime/win32-x64/usr/bin/mktemp.exe +0 -0
  79. package/runtime/win32-x64/usr/bin/msys-2.0.dll +0 -0
  80. package/runtime/win32-x64/usr/bin/msys-gcc_s-seh-1.dll +0 -0
  81. package/runtime/win32-x64/usr/bin/msys-gmp-10.dll +0 -0
  82. package/runtime/win32-x64/usr/bin/msys-iconv-2.dll +0 -0
  83. package/runtime/win32-x64/usr/bin/msys-intl-8.dll +0 -0
  84. package/runtime/win32-x64/usr/bin/msys-mpfr-6.dll +0 -0
  85. package/runtime/win32-x64/usr/bin/msys-ncursesw6.dll +0 -0
  86. package/runtime/win32-x64/usr/bin/msys-pcre-1.dll +0 -0
  87. package/runtime/win32-x64/usr/bin/msys-readline8.dll +0 -0
  88. package/runtime/win32-x64/usr/bin/mv.exe +0 -0
  89. package/runtime/win32-x64/usr/bin/pathchk.exe +0 -0
  90. package/runtime/win32-x64/usr/bin/printf.exe +0 -0
  91. package/runtime/win32-x64/usr/bin/pwd.exe +0 -0
  92. package/runtime/win32-x64/usr/bin/readlink.exe +0 -0
  93. package/runtime/win32-x64/usr/bin/rm.exe +0 -0
  94. package/runtime/win32-x64/usr/bin/rmdir.exe +0 -0
  95. package/runtime/win32-x64/usr/bin/sdiff.exe +0 -0
  96. package/runtime/win32-x64/usr/bin/sed.exe +0 -0
  97. package/runtime/win32-x64/usr/bin/seq.exe +0 -0
  98. package/runtime/win32-x64/usr/bin/sh.exe +0 -0
  99. package/runtime/win32-x64/usr/bin/sleep.exe +0 -0
  100. package/runtime/win32-x64/usr/bin/sort.exe +0 -0
  101. package/runtime/win32-x64/usr/bin/tail.exe +0 -0
  102. package/runtime/win32-x64/usr/bin/test.exe +0 -0
  103. package/runtime/win32-x64/usr/bin/touch.exe +0 -0
  104. package/runtime/win32-x64/usr/bin/tr.exe +0 -0
  105. package/runtime/win32-x64/usr/bin/true.exe +0 -0
  106. package/runtime/win32-x64/usr/bin/uname.exe +0 -0
  107. package/runtime/win32-x64/usr/bin/uniq.exe +0 -0
  108. package/runtime/win32-x64/usr/bin/wc.exe +0 -0
  109. package/runtime/win32-x64/usr/bin/xargs.exe +0 -0
  110. package/src/background.ts +270 -0
  111. package/src/config.ts +209 -0
  112. package/src/gate.ts +308 -0
  113. package/src/index.ts +277 -0
  114. package/src/protocol.ts +130 -0
  115. package/src/pwsh-replacement.ts +435 -0
  116. package/src/runtime.ts +240 -0
  117. package/src/shell.ts +630 -0
  118. package/src/tool.ts +417 -0
@@ -0,0 +1,270 @@
1
+ /**
2
+ * Background execution for win_bash: one command in its OWN pinned bash
3
+ * process, registered with the host's job registry (`ctx.jobs`) so the model
4
+ * can collect it with `job_output` and stop it with `job_kill`.
5
+ *
6
+ * WHY A SEPARATE PROCESS AND NOT A SHELL '&'. The session's persistent shell
7
+ * lives inside the subprocess provider's managed process range, and disposing
8
+ * that range terminates every process still running in it. A job started with
9
+ * a trailing '&' is therefore bound to the shell that started it: the next
10
+ * call's timeout, its abort, or the agent's disposal kills the background work
11
+ * with the shell, and nothing says so. A background job must own its own
12
+ * process, which is what this module starts.
13
+ *
14
+ * SHAPE, NOT IMPORT. Like ./gate.ts, this module consumes the job registry
15
+ * STRUCTURALLY: a leaf plugin takes no dependency on @deepseek-ai/dsh-jobs to
16
+ * call one method on an optional service, and the same decision keeps this
17
+ * package out of the job registry's implementation. The members below are
18
+ * mirrored from the published declaration of @deepseek-ai/dsh-jobs@0.2.1-rc.2:
19
+ *
20
+ * lib/types/index.d.ts
21
+ * abstract class JobRegistry {
22
+ * abstract start(spec: JobSpec): JobId
23
+ * }
24
+ * lib/types/types.d.ts
25
+ * interface JobSpec {
26
+ * kind: JobKind
27
+ * label: string
28
+ * owner?: SessionId
29
+ * outputLimitBytes?: number
30
+ * output?: readonly JobOutputSource[] // { channel?, read(fromByte): JobSourceRead }
31
+ * run(job: JobHandle): JobHooks
32
+ * }
33
+ * interface JobHooks { cancel(reason?: string): void; done: Promise<JobOutcome> }
34
+ * interface JobOutcome { status: 'completed' | 'killed' | 'failed'; detail?: string; result?: string }
35
+ *
36
+ * The registry pumps the pull sources it is given into one bounded per-job
37
+ * output ring at its own cadence, so this module never buffers a stream
38
+ * itself: it hands over the process's own offset-based collected readers and
39
+ * lets the ring's retention be the only bound.
40
+ * @module @linxin666/dsh-win-bash/background
41
+ */
42
+
43
+ import type { SubprocessHandle, SubprocessRuntime } from '@deepseek-ai/dsh-subprocess'
44
+ import { DEFAULT_SPILL_BYTES, GRACE_MS, SHELL_STDERR_CAPTURE_BYTES } from './shell.ts'
45
+
46
+ /**
47
+ * Producer kind of every job this plugin starts. It namespaces the ids the
48
+ * registry issues (`win-bash-1`), so a background command from this tool is
49
+ * never confused with the host's own `bash` job in `job_list`.
50
+ */
51
+ export const BACKGROUND_JOB_KIND = 'win-bash'
52
+
53
+ /** One incremental read from a job pull source, as this module produces it. */
54
+ export interface JobSourceReadLike {
55
+ /** Text captured since the requested offset. */
56
+ readonly text: string
57
+ /** Whole-stream offset to resume from on the next read. */
58
+ readonly nextOffset: number
59
+ /** True when the requested offset slid out of the source's retained window. */
60
+ readonly lossy: boolean
61
+ /** Host path of a file holding the complete stream, when one is kept. */
62
+ readonly spillPath?: string
63
+ }
64
+
65
+ /** One pull source the registry pumps into a job's output ring. */
66
+ export interface JobOutputSourceLike {
67
+ /** Stream label attached to every chunk this source yields. */
68
+ readonly channel?: 'stdout' | 'stderr'
69
+ /** Read everything captured since `fromByte` without consuming it. */
70
+ read(fromByte: number): JobSourceReadLike
71
+ }
72
+
73
+ /** The terminal outcome a producer reports to the registry. */
74
+ export interface JobOutcomeLike {
75
+ /** How the job ended. */
76
+ readonly status: 'completed' | 'killed' | 'failed'
77
+ /** Terminal reason rendered into the model's status line. */
78
+ readonly detail?: string
79
+ }
80
+
81
+ /** The producer face the registry hands to the starter. */
82
+ export interface JobHandleLike {
83
+ /** The registry-issued id (`<kind>-N`). */
84
+ readonly id: string
85
+ /** Append one chunk to the output ring. */
86
+ append(text: string, options?: { readonly channel?: 'stdout' | 'stderr' | 'log', readonly gapBefore?: true }): void
87
+ }
88
+
89
+ /** The controls the runtime observes producer work through. */
90
+ export interface JobHooksLike {
91
+ /** Request termination; synchronous, idempotent, and eventually settles `done`. */
92
+ cancel(reason?: string): void
93
+ /** Resolves after the producer released its resources. */
94
+ readonly done: Promise<JobOutcomeLike>
95
+ }
96
+
97
+ /** One production declaration, as much of it as this plugin uses. */
98
+ export interface JobSpecLike {
99
+ /** Producer kind, also the id prefix. */
100
+ kind: string
101
+ /** One-line model-facing label. */
102
+ label: string
103
+ /** Owning session; access to the job is fenced by it. */
104
+ owner?: string
105
+ /** Pull sources the registry pumps into the ring. */
106
+ output?: readonly JobOutputSourceLike[]
107
+ /** Starts the work and synchronously returns its hooks. */
108
+ run(job: JobHandleLike): JobHooksLike
109
+ }
110
+
111
+ /** The one registry method this module consumes. */
112
+ export interface JobRegistryLike {
113
+ /** Preflight, then atomically register the work. */
114
+ start(spec: JobSpecLike): string
115
+ }
116
+
117
+ /**
118
+ * One command to run in its own pinned bash process.
119
+ *
120
+ * Both halves of the process contract are supplied by the caller: `argv` is
121
+ * the same pinned payload argv the session's persistent shell uses (this tool
122
+ * never consults PATH, and it refuses restricted sessions outright rather than
123
+ * confining them — see ./tool.ts), and `env` is the same environment.
124
+ */
125
+ export interface BackgroundCommand {
126
+ /** The host's managed subprocess seam. */
127
+ readonly subprocess: SubprocessRuntime
128
+ /** The job registry publishing this command's lifecycle. */
129
+ readonly jobs: JobRegistryLike
130
+ /** The owning session id, so the job is collectable by that session alone. */
131
+ readonly owner?: string
132
+ /** The command, exactly as the model wrote it (after any workdir prefix). */
133
+ readonly script: string
134
+ /** The executable and its arguments: the pinned payload bash. */
135
+ readonly argv: readonly string[]
136
+ /** Working directory for the process. */
137
+ readonly cwd: string
138
+ /** Environment entries for the process. */
139
+ readonly env: NodeJS.ProcessEnv
140
+ /** Label shown for this job. */
141
+ readonly label: string
142
+ /** In-memory capture ceiling for stdout, in bytes. */
143
+ readonly stdoutBytes: number
144
+ /** In-memory capture ceiling for stderr, in bytes. */
145
+ readonly stderrBytes: number
146
+ }
147
+
148
+ /** The message of a thrown value, whatever it is. */
149
+ function messageOf(error: unknown): string {
150
+ return error instanceof Error ? error.message : String(error)
151
+ }
152
+
153
+ /**
154
+ * Map a settled process onto the job-outcome vocabulary.
155
+ *
156
+ * A nonzero command exit is reported, not failed: the model reads the exit
157
+ * code in the job's detail line exactly as it reads one from a foreground
158
+ * call. `killed` is reserved for a process this tool terminated, which is the
159
+ * one case where the work did not reach its own end.
160
+ * @param outcome - the process exit facts.
161
+ * @param failedBeforeStart - the starter's failure, when nothing ran.
162
+ * @param terminated - whether this tool terminated the process.
163
+ * @returns the outcome for the registry.
164
+ */
165
+ export function processOutcome(
166
+ outcome: { exitCode: number | null, signal: NodeJS.Signals | null } | undefined,
167
+ failedBeforeStart: string | undefined,
168
+ terminated: boolean,
169
+ ): JobOutcomeLike {
170
+ if (failedBeforeStart !== undefined) return { status: 'failed', detail: failedBeforeStart }
171
+ if (terminated) {
172
+ return {
173
+ status: 'killed',
174
+ detail: outcome?.signal != null ? 'signal: ' + outcome.signal : 'terminated before exit',
175
+ }
176
+ }
177
+ return { status: 'completed', detail: 'exit code: ' + (outcome?.exitCode ?? 0).toString() }
178
+ }
179
+
180
+ /**
181
+ * Register one command as a background job and return the registry's id.
182
+ *
183
+ * The process is spawned INSIDE the starter, after the registry's own
184
+ * admission: a refused registration (the owner's job limit, no attached
185
+ * controller) therefore leaves no process behind, and this function's caller
186
+ * has nothing to clean up on that path.
187
+ * @param command - the process, its wiring, and the registry to register with.
188
+ * @returns the job id the model collects it by.
189
+ */
190
+ export function startBackgroundJob(command: BackgroundCommand): string {
191
+ let process: SubprocessHandle | undefined
192
+ let failedBeforeStart: string | undefined
193
+ let terminated = false
194
+
195
+ const source = (channel: 'stdout' | 'stderr'): JobOutputSourceLike => ({
196
+ channel,
197
+ read: (fromByte) => {
198
+ const reader = process?.collected[channel]
199
+ // A read before the spawn yields nothing rather than an error: the
200
+ // registry pumps on its own cadence and may reach a source before the
201
+ // starter has published the process handle.
202
+ if (reader === undefined) return { text: '', nextOffset: fromByte, lossy: false }
203
+ return reader.readFrom(fromByte)
204
+ },
205
+ })
206
+
207
+ return command.jobs.start({
208
+ kind: BACKGROUND_JOB_KIND,
209
+ label: command.label,
210
+ ...(command.owner === undefined ? {} : { owner: command.owner }),
211
+ output: [source('stdout'), source('stderr')],
212
+ run: () => {
213
+ try {
214
+ process = command.subprocess.spawn({
215
+ argv: [...command.argv],
216
+ cwd: command.cwd,
217
+ // The batch shape: the command is written to the child's stdin and
218
+ // the descriptor is closed, so this 'bash -s' reads the script and
219
+ // runs it. No frame protocol is involved — a background job has one
220
+ // command and one end, and the process's own exit is that end.
221
+ stdio: {
222
+ stdin: { data: command.script },
223
+ stdout: { maxBytes: command.stdoutBytes, spill: { maxBytes: DEFAULT_SPILL_BYTES } },
224
+ stderr: { maxBytes: command.stderrBytes, spill: { maxBytes: DEFAULT_SPILL_BYTES } },
225
+ },
226
+ graceMs: GRACE_MS,
227
+ env: command.env,
228
+ })
229
+ } catch (error) {
230
+ failedBeforeStart = messageOf(error)
231
+ }
232
+ const spawned = process
233
+ const done: Promise<JobOutcomeLike> = spawned === undefined
234
+ ? Promise.resolve(processOutcome(undefined, failedBeforeStart, false))
235
+ : spawned.done.then(
236
+ async (outcome) => {
237
+ // The managed range, not just the spawned command, is what the
238
+ // job owns: a command that leaves a child behind must not report
239
+ // settled while that child still runs. A provider that can no
240
+ // longer observe its range must not turn a real exit into a
241
+ // failure, so the wait is contained.
242
+ try {
243
+ await spawned.waitForExit()
244
+ } catch {
245
+ // Nothing left to observe; the exit facts are already known.
246
+ }
247
+ return processOutcome(outcome, undefined, terminated)
248
+ },
249
+ (error: unknown) => {
250
+ failedBeforeStart = messageOf(error)
251
+ return processOutcome(undefined, failedBeforeStart, false)
252
+ },
253
+ )
254
+ return {
255
+ cancel: () => {
256
+ if (terminated) return
257
+ terminated = true
258
+ process?.terminate()
259
+ },
260
+ done,
261
+ }
262
+ },
263
+ })
264
+ }
265
+
266
+ /** Capture ceilings for one job's streams; exported so the tool and tests agree. */
267
+ export const BACKGROUND_STDOUT_BYTES = 4 * 1024 * 1024
268
+
269
+ /** In-memory stderr ceiling for one background job. */
270
+ export const BACKGROUND_STDERR_BYTES = SHELL_STDERR_CAPTURE_BYTES
package/src/config.ts ADDED
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Plugin configuration: the win_bash tool's budgets and description.
3
+ *
4
+ * Every field is optional in the profile patch and defaulted here, so a row
5
+ * with no config block still mounts a working tool. The schema is also the
6
+ * settings page: the Host renders one form per profile entry from this same
7
+ * Config, which is why the defaults are stated in the field comments as well
8
+ * as in the schema.
9
+ * @module @linxin666/dsh-win-bash/config
10
+ */
11
+
12
+ import z from '@deepseek-ai/schemastery'
13
+ import { DEFAULT_MAX_STDERR_BYTES, DEFAULT_MAX_STDOUT_BYTES } from './shell.ts'
14
+
15
+ /** Whole-call deadline floor, in milliseconds. */
16
+ export const TIMEOUT_MIN_MS = 1000
17
+
18
+ /** Whole-call deadline ceiling, in milliseconds. */
19
+ export const TIMEOUT_MAX_MS = 3_600_000
20
+
21
+ /** Default whole-call deadline, matching the host's own persistent shell. */
22
+ export const DEFAULT_TIMEOUT_MS = 300_000
23
+
24
+ /** Visible stdout budget floor, in bytes. */
25
+ export const STDOUT_MIN_BYTES = 1024
26
+
27
+ /** Visible stdout budget ceiling, in bytes. */
28
+ export const STDOUT_MAX_BYTES = 1024 * 1024
29
+
30
+ /** Visible stderr budget floor, in bytes. */
31
+ export const STDERR_MIN_BYTES = 1024
32
+
33
+ /** Visible stderr budget ceiling, in bytes. */
34
+ export const STDERR_MAX_BYTES = 512 * 1024
35
+
36
+ /**
37
+ * Whether win_bash follows the LiangShen agent preset's declaration by
38
+ * default. When true (the shipped setting) the tool registers only for a
39
+ * deployment that declares the `liangshen` preset; false skips the gate and
40
+ * always registers, which is the escape hatch for a session that wants this
41
+ * shell without the preset.
42
+ */
43
+ export const DEFAULT_SYNC_WITH_LIANGSHEN = true
44
+
45
+ /**
46
+ * Model-facing tool description. Active voice; states the shell preference and
47
+ * the persistence guarantee, so a model knows which shell to reach for before
48
+ * it knows anything else about this tool.
49
+ *
50
+ * The preference is a TRUNCATION CONTRACT, not a flourish: a preset that
51
+ * curates its catalog caps each uncurated line at 200 characters, so whatever
52
+ * sentence states which shell to use must fit in that window. The first
53
+ * sentence therefore carries it alone, and the qualification that follows
54
+ * explains the ladder without restating it.
55
+ */
56
+ export const DEFAULT_DESCRIPTION = [
57
+ "Run a command in this session's persistent pinned bash shell, the shell to use for command work on Windows: prefer it over pwsh.",
58
+ "pwsh stays the only other shell here: use it for Windows-native work (cmdlets, the registry, services) and as the fallback when a command is not bash; where this plugin replaces it, it is withdrawn from this session's tool surface while win_bash serves it, which hides it rather than forbidding it.",
59
+ 'The working directory, exported variables, functions and shell history persist across win_bash calls in this session;',
60
+ 'each call is a foreground command with bounded stdout and stderr and a real exit code.',
61
+ 'Long output is kept as its TAIL and the result says it was truncated, naming the file holding the complete stream when one exists.',
62
+ 'Set run_in_background to start the command in its own process and return a job id at once: collect that job with job_output and stop it with job_kill.',
63
+ 'Use this instead of PowerShell when the command is bash or Unix syntax (pipes, $VAR, globs, single quotes, here-documents).',
64
+ 'It runs only in a danger-full-access session: in a restricted session it refuses the call rather than confining the shell, because the pinned MSYS2 bash cannot start under a restricted token. Use the host bash/pwsh tools there.',
65
+ ].join(' ')
66
+
67
+ /**
68
+ * Whether win_bash may run a command as a background job, by default.
69
+ *
70
+ * The shipped setting is true, matching the host's own bash tool. The flag is
71
+ * one half of a pair: the background surface is only advertised when this is
72
+ * true AND the deployment composes a job registry, because a tool that
73
+ * advertises a capability its deployment cannot honour teaches the model
74
+ * something false about the session.
75
+ */
76
+ export const DEFAULT_ENABLE_RUN_IN_BACKGROUND = true
77
+
78
+ /**
79
+ * Whether the host's pwsh tool is withdrawn from a session this plugin serves,
80
+ * by default.
81
+ *
82
+ * The shipped setting is true: with win_bash registered the session's command
83
+ * work belongs to bash, and leaving pwsh on the wire beside it is what makes a
84
+ * model pick the shell it already knows. The withdrawal is visibility only (a
85
+ * deliberate pwsh call still runs), so false is the escape hatch for an
86
+ * operator who wants both shells listed.
87
+ */
88
+ export const DEFAULT_REPLACE_PWSH = true
89
+
90
+ /** Config shape a profile row may supply; every field is optional. */
91
+ export interface WinBashConfigInput {
92
+ /** Whole-call deadline in milliseconds. */
93
+ timeoutMs?: number
94
+ /** Visible stdout byte budget. */
95
+ maxStdoutBytes?: number
96
+ /** Visible stderr byte budget. */
97
+ maxStderrBytes?: number
98
+ /** Model-facing tool description override. */
99
+ description?: string
100
+ /** Whether the tool's availability follows the LiangShen preset declaration. */
101
+ syncWithLiangShen?: boolean
102
+ /** Whether the host's pwsh tool is hidden while win_bash is registered. */
103
+ replacePwsh?: boolean
104
+ /** Whether run_in_background is available (also requires a job registry). */
105
+ enableRunInBackground?: boolean
106
+ }
107
+
108
+ /** Config shape the plugin runs with; every field resolved. */
109
+ export interface WinBashConfig {
110
+ /** Whole-call deadline in milliseconds. */
111
+ timeoutMs: number
112
+ /** Visible stdout byte budget. */
113
+ maxStdoutBytes: number
114
+ /** Visible stderr byte budget. */
115
+ maxStderrBytes: number
116
+ /** Model-facing tool description. */
117
+ description: string
118
+ /** Whether the tool's availability follows the LiangShen preset declaration. */
119
+ syncWithLiangShen: boolean
120
+ /** Whether the host's pwsh tool is hidden while win_bash is registered. */
121
+ replacePwsh: boolean
122
+ /** Whether run_in_background is available (also requires a job registry). */
123
+ enableRunInBackground: boolean
124
+ }
125
+
126
+ /** Runtime configuration schema for the win_bash tool. */
127
+ export const Config: z<WinBashConfigInput, WinBashConfig> = z.object({
128
+ timeoutMs: z.number().min(TIMEOUT_MIN_MS).max(TIMEOUT_MAX_MS).default(DEFAULT_TIMEOUT_MS),
129
+ maxStdoutBytes: z.number().min(STDOUT_MIN_BYTES).max(STDOUT_MAX_BYTES).default(DEFAULT_MAX_STDOUT_BYTES),
130
+ maxStderrBytes: z.number().min(STDERR_MIN_BYTES).max(STDERR_MAX_BYTES).default(DEFAULT_MAX_STDERR_BYTES),
131
+ description: z.string().default(DEFAULT_DESCRIPTION),
132
+ syncWithLiangShen: z.boolean().default(DEFAULT_SYNC_WITH_LIANGSHEN),
133
+ replacePwsh: z.boolean().default(DEFAULT_REPLACE_PWSH),
134
+ enableRunInBackground: z.boolean().default(DEFAULT_ENABLE_RUN_IN_BACKGROUND),
135
+ })
136
+
137
+ /** The values an omitted field takes. */
138
+ export const CONFIG_DEFAULTS: WinBashConfig = {
139
+ timeoutMs: DEFAULT_TIMEOUT_MS,
140
+ maxStdoutBytes: DEFAULT_MAX_STDOUT_BYTES,
141
+ maxStderrBytes: DEFAULT_MAX_STDERR_BYTES,
142
+ description: DEFAULT_DESCRIPTION,
143
+ syncWithLiangShen: DEFAULT_SYNC_WITH_LIANGSHEN,
144
+ replacePwsh: DEFAULT_REPLACE_PWSH,
145
+ enableRunInBackground: DEFAULT_ENABLE_RUN_IN_BACKGROUND,
146
+ }
147
+
148
+ /** One rejected configuration field. */
149
+ export interface ConfigViolation {
150
+ /** Field name. */
151
+ readonly field: string
152
+ /** Why the value cannot be used. */
153
+ readonly message: string
154
+ }
155
+
156
+ /**
157
+ * Resolve a raw config object into the running configuration.
158
+ *
159
+ * The Host validates the row through {@link Config} before apply runs, so this
160
+ * is the second gate: it exists because tests and hand-built contexts call the
161
+ * plugin's apply directly, and because a bounded integer is a contract the
162
+ * shell's budgets depend on (a NaN budget would make every output look
163
+ * truncated).
164
+ * @param input - the raw config, or undefined.
165
+ * @returns the resolved configuration.
166
+ * @throws when a supplied value is outside its contract.
167
+ */
168
+ export function resolveConfig(input: WinBashConfigInput | undefined): WinBashConfig {
169
+ const resolved: WinBashConfig = { ...CONFIG_DEFAULTS, ...(input ?? {}) }
170
+ const violations = validateConfig(resolved)
171
+ if (violations.length > 0) {
172
+ throw new Error(
173
+ 'win_bash config rejected: '
174
+ + violations.map(v => v.field + ': ' + v.message).join('; '),
175
+ )
176
+ }
177
+ return resolved
178
+ }
179
+
180
+ /**
181
+ * Check every field of a resolved configuration.
182
+ * @param config - the candidate configuration.
183
+ * @returns one entry per rejected field; empty means accepted.
184
+ */
185
+ export function validateConfig(config: WinBashConfig): ConfigViolation[] {
186
+ const violations: ConfigViolation[] = []
187
+ const bounded = (field: 'timeoutMs' | 'maxStdoutBytes' | 'maxStderrBytes', min: number, max: number): void => {
188
+ const value = config[field]
189
+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < min || value > max) {
190
+ violations.push({ field, message: 'must be an integer between ' + min.toString() + ' and ' + max.toString() })
191
+ }
192
+ }
193
+ bounded('timeoutMs', TIMEOUT_MIN_MS, TIMEOUT_MAX_MS)
194
+ bounded('maxStdoutBytes', STDOUT_MIN_BYTES, STDOUT_MAX_BYTES)
195
+ bounded('maxStderrBytes', STDERR_MIN_BYTES, STDERR_MAX_BYTES)
196
+ if (typeof config.description !== 'string' || config.description.trim() === '') {
197
+ violations.push({ field: 'description', message: 'must be a non-empty string' })
198
+ }
199
+ if (typeof config.syncWithLiangShen !== 'boolean') {
200
+ violations.push({ field: 'syncWithLiangShen', message: 'must be a boolean' })
201
+ }
202
+ if (typeof config.replacePwsh !== 'boolean') {
203
+ violations.push({ field: 'replacePwsh', message: 'must be a boolean' })
204
+ }
205
+ if (typeof config.enableRunInBackground !== 'boolean') {
206
+ violations.push({ field: 'enableRunInBackground', message: 'must be a boolean' })
207
+ }
208
+ return violations
209
+ }