@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.
- package/LICENSE +202 -0
- package/PROVENANCE.md +215 -0
- package/README.i18n.yaml +6 -0
- package/README.md +257 -2
- package/README.zh.md +217 -0
- package/cordis.patch.yml +13 -0
- package/lib/index.js +2697 -0
- package/lib/types/background.d.ts +169 -0
- package/lib/types/config.d.ts +130 -0
- package/lib/types/gate.d.ts +179 -0
- package/lib/types/index.d.ts +104 -0
- package/lib/types/protocol.d.ts +90 -0
- package/lib/types/pwsh-replacement.d.ts +140 -0
- package/lib/types/runtime.d.ts +115 -0
- package/lib/types/shell.d.ts +184 -0
- package/lib/types/tool.d.ts +108 -0
- package/package.json +58 -3
- package/runtime/win32-x64/checksums.sha256 +91 -0
- package/runtime/win32-x64/etc/bash.bashrc +69 -0
- package/runtime/win32-x64/etc/fstab +6 -0
- package/runtime/win32-x64/etc/msystem +27 -0
- package/runtime/win32-x64/etc/msystem.d/CLANG64 +7 -0
- package/runtime/win32-x64/etc/msystem.d/CLANGARM64 +7 -0
- package/runtime/win32-x64/etc/msystem.d/MINGW32 +7 -0
- package/runtime/win32-x64/etc/msystem.d/MINGW64 +7 -0
- package/runtime/win32-x64/etc/msystem.d/MSYS +4 -0
- package/runtime/win32-x64/etc/msystem.d/UCRT64 +7 -0
- package/runtime/win32-x64/etc/nsswitch.conf +12 -0
- package/runtime/win32-x64/etc/package-versions.txt +67 -0
- package/runtime/win32-x64/etc/profile +146 -0
- package/runtime/win32-x64/etc/profile.d/bash_profile.sh +11 -0
- package/runtime/win32-x64/etc/profile.d/lang.sh +19 -0
- package/runtime/win32-x64/licenses/GPL-2.0.txt +338 -0
- package/runtime/win32-x64/licenses/GPL-3.0.txt +674 -0
- package/runtime/win32-x64/licenses/LGPL-2.1.txt +501 -0
- package/runtime/win32-x64/licenses/LGPL-3.0.txt +165 -0
- package/runtime/win32-x64/licenses/expat-COPYING.txt +21 -0
- package/runtime/win32-x64/licenses/gcc-libs-RUNTIME.LIBRARY.EXCEPTION.txt +73 -0
- package/runtime/win32-x64/licenses/gettext-runtime-COPYING.LIB.txt +501 -0
- package/runtime/win32-x64/licenses/gettext-runtime-COPYING.txt +13 -0
- package/runtime/win32-x64/licenses/git-for-windows-LICENSE.txt +360 -0
- package/runtime/win32-x64/licenses/heimdal-LICENSE.txt +33 -0
- package/runtime/win32-x64/licenses/libcbor-LICENSE.md +21 -0
- package/runtime/win32-x64/licenses/libffi-LICENSE.txt +21 -0
- package/runtime/win32-x64/licenses/libfido2-LICENSE.txt +26 -0
- package/runtime/win32-x64/licenses/libiconv-COPYING.LIB.txt +501 -0
- package/runtime/win32-x64/licenses/libiconv-COPYING.txt +674 -0
- package/runtime/win32-x64/licenses/libopenssl-LICENSE.txt +177 -0
- package/runtime/win32-x64/licenses/libpcre-LICENCE.txt +93 -0
- package/runtime/win32-x64/licenses/libsqlite-LICENSE +25 -0
- package/runtime/win32-x64/licenses/ncurses-LICENSE +29 -0
- package/runtime/win32-x64/licenses/openssh-LICENCE.txt +412 -0
- package/runtime/win32-x64/licenses/zlib-LICENSE +22 -0
- package/runtime/win32-x64/usr/bin/awk.exe +0 -0
- package/runtime/win32-x64/usr/bin/b2sum.exe +0 -0
- package/runtime/win32-x64/usr/bin/basename.exe +0 -0
- package/runtime/win32-x64/usr/bin/basenc.exe +0 -0
- package/runtime/win32-x64/usr/bin/bash.exe +0 -0
- package/runtime/win32-x64/usr/bin/cat.exe +0 -0
- package/runtime/win32-x64/usr/bin/cmp.exe +0 -0
- package/runtime/win32-x64/usr/bin/comm.exe +0 -0
- package/runtime/win32-x64/usr/bin/cp.exe +0 -0
- package/runtime/win32-x64/usr/bin/cut.exe +0 -0
- package/runtime/win32-x64/usr/bin/dash.exe +0 -0
- package/runtime/win32-x64/usr/bin/date.exe +0 -0
- package/runtime/win32-x64/usr/bin/diff.exe +0 -0
- package/runtime/win32-x64/usr/bin/diff3.exe +0 -0
- package/runtime/win32-x64/usr/bin/dirname.exe +0 -0
- package/runtime/win32-x64/usr/bin/echo.exe +0 -0
- package/runtime/win32-x64/usr/bin/env.exe +0 -0
- package/runtime/win32-x64/usr/bin/expr.exe +0 -0
- package/runtime/win32-x64/usr/bin/false.exe +0 -0
- package/runtime/win32-x64/usr/bin/find.exe +0 -0
- package/runtime/win32-x64/usr/bin/grep.exe +0 -0
- package/runtime/win32-x64/usr/bin/head.exe +0 -0
- package/runtime/win32-x64/usr/bin/ls.exe +0 -0
- package/runtime/win32-x64/usr/bin/mkdir.exe +0 -0
- package/runtime/win32-x64/usr/bin/mktemp.exe +0 -0
- package/runtime/win32-x64/usr/bin/msys-2.0.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-gcc_s-seh-1.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-gmp-10.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-iconv-2.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-intl-8.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-mpfr-6.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-ncursesw6.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-pcre-1.dll +0 -0
- package/runtime/win32-x64/usr/bin/msys-readline8.dll +0 -0
- package/runtime/win32-x64/usr/bin/mv.exe +0 -0
- package/runtime/win32-x64/usr/bin/pathchk.exe +0 -0
- package/runtime/win32-x64/usr/bin/printf.exe +0 -0
- package/runtime/win32-x64/usr/bin/pwd.exe +0 -0
- package/runtime/win32-x64/usr/bin/readlink.exe +0 -0
- package/runtime/win32-x64/usr/bin/rm.exe +0 -0
- package/runtime/win32-x64/usr/bin/rmdir.exe +0 -0
- package/runtime/win32-x64/usr/bin/sdiff.exe +0 -0
- package/runtime/win32-x64/usr/bin/sed.exe +0 -0
- package/runtime/win32-x64/usr/bin/seq.exe +0 -0
- package/runtime/win32-x64/usr/bin/sh.exe +0 -0
- package/runtime/win32-x64/usr/bin/sleep.exe +0 -0
- package/runtime/win32-x64/usr/bin/sort.exe +0 -0
- package/runtime/win32-x64/usr/bin/tail.exe +0 -0
- package/runtime/win32-x64/usr/bin/test.exe +0 -0
- package/runtime/win32-x64/usr/bin/touch.exe +0 -0
- package/runtime/win32-x64/usr/bin/tr.exe +0 -0
- package/runtime/win32-x64/usr/bin/true.exe +0 -0
- package/runtime/win32-x64/usr/bin/uname.exe +0 -0
- package/runtime/win32-x64/usr/bin/uniq.exe +0 -0
- package/runtime/win32-x64/usr/bin/wc.exe +0 -0
- package/runtime/win32-x64/usr/bin/xargs.exe +0 -0
- package/src/background.ts +270 -0
- package/src/config.ts +209 -0
- package/src/gate.ts +308 -0
- package/src/index.ts +277 -0
- package/src/protocol.ts +130 -0
- package/src/pwsh-replacement.ts +435 -0
- package/src/runtime.ts +240 -0
- package/src/shell.ts +630 -0
- package/src/tool.ts +417 -0
package/src/gate.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LiangShen availability gate for the win_bash tool.
|
|
3
|
+
*
|
|
4
|
+
* win_bash serves sessions that run the LiangShen (梁神) preset, so its
|
|
5
|
+
* availability follows that preset's DECLARATION: this gate asks the harness's
|
|
6
|
+
* agent-preset registry whether a preset with id liangshen is declared, and the
|
|
7
|
+
* entry registers no tool at all when it is not. The alternative — mounting a
|
|
8
|
+
* tool whose every call refuses — teaches the model a capability the session
|
|
9
|
+
* does not have, which is worse than an honest absence.
|
|
10
|
+
*
|
|
11
|
+
* The registry read is STRUCTURAL, not an import: this package does not depend
|
|
12
|
+
* on @deepseek-ai/dsh-agent-preset-registry (a leaf plugin takes no SDK
|
|
13
|
+
* dependency to read one optional service), so the single method consumed is
|
|
14
|
+
* mirrored from the published declaration of
|
|
15
|
+
* @deepseek-ai/dsh-agent-preset-registry@0.2.1-rc.2, lib/types/index.d.ts:
|
|
16
|
+
*
|
|
17
|
+
* export declare class AgentPresetRegistry {
|
|
18
|
+
* list(): Promise<AgentPreset[]>
|
|
19
|
+
* }
|
|
20
|
+
* export interface AgentPreset {
|
|
21
|
+
* readonly id: string
|
|
22
|
+
* readonly name?: string
|
|
23
|
+
* readonly broken?: string
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* list() is the registry's public DEFINITION roster: it resolves every declared
|
|
27
|
+
* definition, including activation failures (which each row reports), so it
|
|
28
|
+
* answers exactly the "is this preset declared here" question. The narrower
|
|
29
|
+
* resolve(id) cannot: it REJECTS for an undeclared id, so a probe built on it
|
|
30
|
+
* would have to read a rejection as "absent" and could not tell that apart from
|
|
31
|
+
* a real failure of a preset that IS declared.
|
|
32
|
+
*
|
|
33
|
+
* The service is read with ctx.get, never a hard inject: a deployment may
|
|
34
|
+
* compose no preset registry at all, and waiting on one would pend this row
|
|
35
|
+
* forever — which under the Host's boot gate fails the whole web boot rather
|
|
36
|
+
* than this one plugin (the trap dsh-liangshen documents as issue #1712).
|
|
37
|
+
*
|
|
38
|
+
* Timing. Two facts about the pieces this talks to shape the verdict:
|
|
39
|
+
*
|
|
40
|
+
* 1. Rows in one loader group mount concurrently (the loader starts every entry
|
|
41
|
+
* of a group in the same tick), so this row cannot assume the LiangShen row
|
|
42
|
+
* ran first. The verdict therefore waits for the Host plugin tree to settle
|
|
43
|
+
* before its first read, rather than reading during the boot race.
|
|
44
|
+
* 2. The declaration does not arrive with that row's activation return: the
|
|
45
|
+
* declaring plugin schedules it on its own detached promise chain (it
|
|
46
|
+
* persists module rows, so it cannot block activation on the mount). The
|
|
47
|
+
* tree can therefore settle one or more turns BEFORE the registry holds the
|
|
48
|
+
* definition, and a single read would answer "not declared" for a
|
|
49
|
+
* deployment that is about to declare it.
|
|
50
|
+
*
|
|
51
|
+
* So a read that already sees the declaration decides immediately and costs
|
|
52
|
+
* nothing; a read that does not re-reads on a bounded schedule until the
|
|
53
|
+
* declaration appears or the deadline passes. The wait is never paid in the
|
|
54
|
+
* common case, and it never turns a late declaration into a refusal. It runs
|
|
55
|
+
* entirely off the plugin's activation path (see the entry), so it delays no
|
|
56
|
+
* boot work; it only decides when win_bash becomes available.
|
|
57
|
+
*
|
|
58
|
+
* The registry exposes NO declaration-change event. Its only declared preset
|
|
59
|
+
* event is agent-preset/selected, which records one SESSION's committed preset
|
|
60
|
+
* to that session's durable log (lib/types/types.d.ts) and says nothing about
|
|
61
|
+
* the roster. The verdict is therefore decided once per mount, and a switch
|
|
62
|
+
* toggle takes effect through the plugin manager's row refresh, which remounts
|
|
63
|
+
* this row and re-runs the gate — the same freshness model the family's row
|
|
64
|
+
* ledger uses.
|
|
65
|
+
* @module @linxin666/dsh-win-bash/gate
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
69
|
+
|
|
70
|
+
/** Preset id whose declaration makes win_bash available. */
|
|
71
|
+
export const LIANGSHEN_PRESET_ID = 'liangshen'
|
|
72
|
+
|
|
73
|
+
/** Upper bound on the wait for the Host plugin tree to settle, in milliseconds. */
|
|
74
|
+
export const GATE_SETTLE_TIMEOUT_MS = 10_000
|
|
75
|
+
|
|
76
|
+
/** Upper bound on one registry roster read, in milliseconds. */
|
|
77
|
+
export const GATE_READ_TIMEOUT_MS = 10_000
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* How long a roster that does not yet declare LiangShen keeps being re-read,
|
|
81
|
+
* in milliseconds. See the module header: the declaring plugin publishes the
|
|
82
|
+
* definition on a detached chain, so the tree can settle first. Only paid when
|
|
83
|
+
* the declaration is genuinely absent, since a visible one returns at once.
|
|
84
|
+
*/
|
|
85
|
+
export const GATE_DECLARATION_WAIT_MS = 5_000
|
|
86
|
+
|
|
87
|
+
/** Re-read cadence during {@link GATE_DECLARATION_WAIT_MS}, in milliseconds. */
|
|
88
|
+
export const GATE_POLL_INTERVAL_MS = 250
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The deadlines and cadence one gate run uses. The defaults are the shipped
|
|
92
|
+
* values; tests pass small ones so a bounded wait is exercised without a slow
|
|
93
|
+
* suite.
|
|
94
|
+
*/
|
|
95
|
+
export interface GateOptions {
|
|
96
|
+
/** Bound on the Host plugin-tree settle wait, in milliseconds. */
|
|
97
|
+
settleTimeoutMs?: number
|
|
98
|
+
/** Bound on one registry roster read, in milliseconds. */
|
|
99
|
+
readTimeoutMs?: number
|
|
100
|
+
/** Total time a late declaration may take to appear, in milliseconds. */
|
|
101
|
+
declarationWaitMs?: number
|
|
102
|
+
/** Re-read cadence during that wait, in milliseconds. */
|
|
103
|
+
pollIntervalMs?: number
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** One roster row, as much of it as this gate reads. */
|
|
107
|
+
export interface PresetRosterRow {
|
|
108
|
+
/** Stable preset identifier. */
|
|
109
|
+
readonly id: string
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The one registry method this gate consumes. */
|
|
113
|
+
export interface PresetRosterReader {
|
|
114
|
+
/** Read every declared preset, including activation failures. */
|
|
115
|
+
list(): Promise<readonly PresetRosterRow[]>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Why the gate reached its verdict. */
|
|
119
|
+
export type GateReason =
|
|
120
|
+
/** The registry declares the LiangShen preset. */
|
|
121
|
+
| 'declared'
|
|
122
|
+
/** The registry answered and declares no preset with the LiangShen id. */
|
|
123
|
+
| 'not-declared'
|
|
124
|
+
/** No agent-preset registry usable for this read is published. */
|
|
125
|
+
| 'registry-absent'
|
|
126
|
+
/** A registry is published, but its roster never answered. */
|
|
127
|
+
| 'registry-unreadable'
|
|
128
|
+
|
|
129
|
+
/** How far the Host plugin-tree settle got before the roster was read. */
|
|
130
|
+
export type SettleStatus =
|
|
131
|
+
/** Every Host loader entry and plugin lifecycle task finished. */
|
|
132
|
+
| 'settled'
|
|
133
|
+
/** No loader service is published, so there was nothing to settle. */
|
|
134
|
+
| 'unavailable'
|
|
135
|
+
/** The tree did not settle within the settle bound. */
|
|
136
|
+
| 'timed-out'
|
|
137
|
+
|
|
138
|
+
/** The gate's verdict: whether win_bash may register, and why. */
|
|
139
|
+
export interface GateOutcome {
|
|
140
|
+
/** True only for {@link GateReason} declared. */
|
|
141
|
+
readonly allowed: boolean
|
|
142
|
+
/** The reason, for the caller's log line. */
|
|
143
|
+
readonly reason: GateReason
|
|
144
|
+
/** Whether the Host tree settled before the roster was read. */
|
|
145
|
+
readonly settled: SettleStatus
|
|
146
|
+
/** How many roster reads the verdict cost. */
|
|
147
|
+
readonly reads: number
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Whether a roster declares the preset this tool follows.
|
|
152
|
+
* @param presets - rows as {@link PresetRosterReader.list} returns them.
|
|
153
|
+
* @returns true when one row carries the LiangShen id.
|
|
154
|
+
*/
|
|
155
|
+
export function declaresLiangShen(presets: readonly PresetRosterRow[]): boolean {
|
|
156
|
+
return presets.some(preset => preset?.id === LIANGSHEN_PRESET_ID)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Read one optional service without letting a refused lookup escape: a cordis
|
|
161
|
+
* service proxy throws for a name it cannot resolve, and an absent optional
|
|
162
|
+
* service is an ordinary state here, not an error.
|
|
163
|
+
* @param ctx - the plugin context.
|
|
164
|
+
* @param name - the service name.
|
|
165
|
+
* @returns the service, or undefined when it is not resolvable.
|
|
166
|
+
*/
|
|
167
|
+
function readService(ctx: Context, name: string): unknown {
|
|
168
|
+
try {
|
|
169
|
+
return (ctx as unknown as { get(key: string): unknown }).get(name)
|
|
170
|
+
} catch {
|
|
171
|
+
return undefined
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The published registry, when this deployment has one shaped like the one this
|
|
177
|
+
* gate consumes.
|
|
178
|
+
* @param ctx - the plugin context.
|
|
179
|
+
* @returns the reader, or undefined when no usable registry is published.
|
|
180
|
+
*/
|
|
181
|
+
export function readPresetRegistry(ctx: Context): PresetRosterReader | undefined {
|
|
182
|
+
const candidate = readService(ctx, 'agentPresets')
|
|
183
|
+
if (candidate === null || typeof candidate !== 'object') return undefined
|
|
184
|
+
if (typeof (candidate as { list?: unknown }).list !== 'function') return undefined
|
|
185
|
+
return candidate as PresetRosterReader
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Run one promise with a deadline. The timer is always cleared, so a fast
|
|
190
|
+
* answer leaves no stray handle behind.
|
|
191
|
+
* @param work - the promise to bound.
|
|
192
|
+
* @param timeoutMs - the bound in milliseconds.
|
|
193
|
+
* @returns the promise's value.
|
|
194
|
+
* @throws when the deadline passes first.
|
|
195
|
+
*/
|
|
196
|
+
async function withDeadline<T>(work: Promise<T>, timeoutMs: number): Promise<T> {
|
|
197
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
198
|
+
try {
|
|
199
|
+
return await Promise.race([
|
|
200
|
+
work,
|
|
201
|
+
new Promise<never>((_resolve, reject) => {
|
|
202
|
+
timer = setTimeout(() => { reject(new Error('timed out after ' + timeoutMs.toString() + 'ms')) }, timeoutMs)
|
|
203
|
+
}),
|
|
204
|
+
])
|
|
205
|
+
} finally {
|
|
206
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Wait until the Host loader tree has no pending import or lifecycle work.
|
|
212
|
+
*
|
|
213
|
+
* Waiting for the whole tree rather than this row's neighbours is deliberate:
|
|
214
|
+
* the roster belongs to the deployment, not to one group, and the loader's own
|
|
215
|
+
* await() is the only predicate that means "every row has been applied". A row
|
|
216
|
+
* merely waiting for a service it will never get carries no pending task, so it
|
|
217
|
+
* cannot hold this wait open; one that is genuinely mid-activation can, which is
|
|
218
|
+
* exactly the case the bound covers.
|
|
219
|
+
* @param ctx - the plugin context.
|
|
220
|
+
* @param options - the deadlines for this run.
|
|
221
|
+
* @returns how far the settle got.
|
|
222
|
+
*/
|
|
223
|
+
export async function settleHostTree(ctx: Context, options: GateOptions = {}): Promise<SettleStatus> {
|
|
224
|
+
const loader = readService(ctx, 'loader')
|
|
225
|
+
if (loader === null || typeof loader !== 'object') return 'unavailable'
|
|
226
|
+
const wait = (loader as { await?: unknown }).await
|
|
227
|
+
if (typeof wait !== 'function') return 'unavailable'
|
|
228
|
+
try {
|
|
229
|
+
await withDeadline(
|
|
230
|
+
Promise.resolve((wait as () => Promise<void>).call(loader)).then(() => undefined),
|
|
231
|
+
options.settleTimeoutMs ?? GATE_SETTLE_TIMEOUT_MS,
|
|
232
|
+
)
|
|
233
|
+
return 'settled'
|
|
234
|
+
} catch {
|
|
235
|
+
return 'timed-out'
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Sleep one timer turn.
|
|
241
|
+
*
|
|
242
|
+
* Production code, not a test helper: the wait IS the declaration window the
|
|
243
|
+
* module header describes, and the gate is only ever reached from a mount.
|
|
244
|
+
* @param ms - how long to wait.
|
|
245
|
+
*/
|
|
246
|
+
function waitFor(ms: number): Promise<void> {
|
|
247
|
+
return new Promise<void>((resolve) => { setTimeout(resolve, ms) })
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** One roster read, or undefined when the registry could not answer. */
|
|
251
|
+
async function readRoster(
|
|
252
|
+
registry: PresetRosterReader,
|
|
253
|
+
timeoutMs: number,
|
|
254
|
+
): Promise<readonly PresetRosterRow[] | undefined> {
|
|
255
|
+
try {
|
|
256
|
+
const rows = await withDeadline(Promise.resolve(registry.list()), timeoutMs)
|
|
257
|
+
return Array.isArray(rows) ? rows as readonly PresetRosterRow[] : undefined
|
|
258
|
+
} catch {
|
|
259
|
+
return undefined
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Decide whether the LiangShen preset is declared in this deployment.
|
|
265
|
+
*
|
|
266
|
+
* Call this OFF the plugin's activation path — never awaited from a plugin
|
|
267
|
+
* apply. list() is not a leaf read: it audits every declared preset and, for one
|
|
268
|
+
* whose mounted subtree still has pending rows, awaits the Host loader tree
|
|
269
|
+
* (agent-preset-registry lib/index.js diagnostic). A plugin whose activation
|
|
270
|
+
* awaited this would wait on the loader tree while its OWN activation is among
|
|
271
|
+
* the tasks that tree is waiting for, and neither wait would ever settle. The
|
|
272
|
+
* entry calls this after its synchronous activation returned, which is what
|
|
273
|
+
* keeps this a wait and not a deadlock.
|
|
274
|
+
* @param ctx - the plugin context.
|
|
275
|
+
* @param options - the deadlines and cadence for this run.
|
|
276
|
+
* @returns the verdict, with the reason for the caller's log line.
|
|
277
|
+
*/
|
|
278
|
+
export async function startLiangShenGate(ctx: Context, options: GateOptions = {}): Promise<GateOutcome> {
|
|
279
|
+
const settled = await settleHostTree(ctx, options)
|
|
280
|
+
const registry = readPresetRegistry(ctx)
|
|
281
|
+
if (registry === undefined) return { allowed: false, reason: 'registry-absent', settled, reads: 0 }
|
|
282
|
+
const readTimeoutMs = options.readTimeoutMs ?? GATE_READ_TIMEOUT_MS
|
|
283
|
+
const pollIntervalMs = options.pollIntervalMs ?? GATE_POLL_INTERVAL_MS
|
|
284
|
+
const waitMs = options.declarationWaitMs ?? GATE_DECLARATION_WAIT_MS
|
|
285
|
+
|
|
286
|
+
let reads = 0
|
|
287
|
+
const first = await readRoster(registry, readTimeoutMs)
|
|
288
|
+
reads += 1
|
|
289
|
+
if (first === undefined) return { allowed: false, reason: 'registry-unreadable', settled, reads }
|
|
290
|
+
if (declaresLiangShen(first)) return { allowed: true, reason: 'declared', settled, reads }
|
|
291
|
+
|
|
292
|
+
// Not declared yet. Re-read until the declaration appears or the window
|
|
293
|
+
// closes: the declaring plugin publishes it off its activation return, so
|
|
294
|
+
// \"not yet\" and \"not here\" are only distinguishable by waiting. A read
|
|
295
|
+
// that fails mid-window is not the verdict — only a window that never
|
|
296
|
+
// produced a successful answer at all is reported as unreadable.
|
|
297
|
+
const deadline = Date.now() + waitMs
|
|
298
|
+
let answered = false
|
|
299
|
+
while (Date.now() < deadline) {
|
|
300
|
+
await waitFor(Math.min(pollIntervalMs, Math.max(0, deadline - Date.now())))
|
|
301
|
+
const rows = await readRoster(registry, readTimeoutMs)
|
|
302
|
+
reads += 1
|
|
303
|
+
if (rows === undefined) continue
|
|
304
|
+
answered = true
|
|
305
|
+
if (declaresLiangShen(rows)) return { allowed: true, reason: 'declared', settled, reads }
|
|
306
|
+
}
|
|
307
|
+
return { allowed: false, reason: answered ? 'not-declared' : 'registry-unreadable', settled, reads }
|
|
308
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-half entry for the dsh-win-bash plugin.
|
|
3
|
+
*
|
|
4
|
+
* The plugin owns exactly one model-facing capability — the win_bash tool over
|
|
5
|
+
* a per-session persistent bash process — and nothing else. It registers no
|
|
6
|
+
* settings section, no HTTP route and no browser half: the Host renders the
|
|
7
|
+
* configuration form for this row from the exported Config schema.
|
|
8
|
+
*
|
|
9
|
+
* Mount-time integrity: before the tool is registered at all, the vendored
|
|
10
|
+
* payload is checked against its shipped SHA256 manifest. A mismatch refuses
|
|
11
|
+
* the mount with a specific message rather than running commands through
|
|
12
|
+
* bytes nobody verified. The tool is also refused on every non-Windows host
|
|
13
|
+
* and on a Windows host that is not x64, because no PATH bash is ever used as
|
|
14
|
+
* a substitute.
|
|
15
|
+
*
|
|
16
|
+
* Availability gate: win_bash serves sessions that run the LiangShen (梁神)
|
|
17
|
+
* preset, so its registration follows that preset's DECLARATION in the
|
|
18
|
+
* harness's agent-preset registry — declared means registered, undeclared
|
|
19
|
+
* means the tool is never registered and one info line says why. The gate's
|
|
20
|
+
* config escape hatch is syncWithLiangShen: false, which skips it and always
|
|
21
|
+
* registers. See ./gate.ts for the API the gate reads and the timing
|
|
22
|
+
* constraints it satisfies.
|
|
23
|
+
*
|
|
24
|
+
* Host shell replacement: once win_bash is registered, the host's own pwsh
|
|
25
|
+
* tool is withdrawn from each session win_bash can actually SERVE - the
|
|
26
|
+
* sessions whose own sandbox mode is danger-full-access - so their command work
|
|
27
|
+
* goes to bash rather than to the shell a model already knows. A session in any
|
|
28
|
+
* other mode keeps its pwsh, because win_bash refuses to run there and pwsh is
|
|
29
|
+
* then the only shell it has. Windows only, visibility only, per agent scope,
|
|
30
|
+
* and re-evaluated when a session switches mode; see ./pwsh-replacement.ts for
|
|
31
|
+
* why it is neither global nor an execution denial.
|
|
32
|
+
*
|
|
33
|
+
* Lifecycle: one shell process per calling agent, released when that agent is
|
|
34
|
+
* disposed and when the plugin fiber unloads. The pwsh restriction follows the
|
|
35
|
+
* same lifecycle, lifted with the agent and with the fiber. apply() returns as
|
|
36
|
+
* soon as the plugin is configured: the gate decides in mountWithGate, whose
|
|
37
|
+
* promise is deliberately NOT returned from apply (see that function).
|
|
38
|
+
* @module @linxin666/dsh-win-bash
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
42
|
+
import type {} from '@deepseek-ai/dsh-agent'
|
|
43
|
+
import type {} from '@deepseek-ai/dsh-sandbox'
|
|
44
|
+
import type {} from '@deepseek-ai/dsh-subprocess'
|
|
45
|
+
import type {} from '@deepseek-ai/dsh-tools'
|
|
46
|
+
import { Config, resolveConfig, type WinBashConfig, type WinBashConfigInput } from './config.ts'
|
|
47
|
+
import { startLiangShenGate, LIANGSHEN_PRESET_ID, type GateOptions, type GateOutcome } from './gate.ts'
|
|
48
|
+
import { installPwshReplacement } from './pwsh-replacement.ts'
|
|
49
|
+
import { resolveRuntime, type RuntimeReady, type HostPlatform } from './runtime.ts'
|
|
50
|
+
import { ShellRegistry } from './shell.ts'
|
|
51
|
+
import { buildWinBashTool } from './tool.ts'
|
|
52
|
+
|
|
53
|
+
export { BACKGROUND_JOB_KIND, startBackgroundJob } from './background.ts'
|
|
54
|
+
export type {
|
|
55
|
+
BackgroundCommand,
|
|
56
|
+
JobHandleLike,
|
|
57
|
+
JobHooksLike,
|
|
58
|
+
JobOutcomeLike,
|
|
59
|
+
JobOutputSourceLike,
|
|
60
|
+
JobRegistryLike,
|
|
61
|
+
JobSourceReadLike,
|
|
62
|
+
} from './background.ts'
|
|
63
|
+
|
|
64
|
+
export { Config }
|
|
65
|
+
export type { WinBashConfig, WinBashConfigInput }
|
|
66
|
+
export {
|
|
67
|
+
CONFIG_DEFAULTS,
|
|
68
|
+
DEFAULT_DESCRIPTION,
|
|
69
|
+
DEFAULT_ENABLE_RUN_IN_BACKGROUND,
|
|
70
|
+
DEFAULT_REPLACE_PWSH,
|
|
71
|
+
DEFAULT_SYNC_WITH_LIANGSHEN,
|
|
72
|
+
validateConfig,
|
|
73
|
+
} from './config.ts'
|
|
74
|
+
export {
|
|
75
|
+
HOST_PWSH_TOOL,
|
|
76
|
+
SERVING_MODE,
|
|
77
|
+
installPwshReplacement,
|
|
78
|
+
type PwshReplacementOptions,
|
|
79
|
+
type ScopedRestrictor,
|
|
80
|
+
type SessionPolicyReader,
|
|
81
|
+
} from './pwsh-replacement.ts'
|
|
82
|
+
export { WIN_BASH_TOOL, renderResult, readJobRegistry, truncationNotice } from './tool.ts'
|
|
83
|
+
export type { RenderFacts } from './tool.ts'
|
|
84
|
+
export {
|
|
85
|
+
ShellRegistry,
|
|
86
|
+
ShellUnavailableError,
|
|
87
|
+
shellArgv,
|
|
88
|
+
shellEnvironment,
|
|
89
|
+
shellTempDir,
|
|
90
|
+
} from './shell.ts'
|
|
91
|
+
export { buildFrame, markerFor, parseFrame } from './protocol.ts'
|
|
92
|
+
export {
|
|
93
|
+
GATE_DECLARATION_WAIT_MS,
|
|
94
|
+
GATE_POLL_INTERVAL_MS,
|
|
95
|
+
GATE_READ_TIMEOUT_MS,
|
|
96
|
+
GATE_SETTLE_TIMEOUT_MS,
|
|
97
|
+
LIANGSHEN_PRESET_ID,
|
|
98
|
+
declaresLiangShen,
|
|
99
|
+
readPresetRegistry,
|
|
100
|
+
settleHostTree,
|
|
101
|
+
startLiangShenGate,
|
|
102
|
+
type GateOptions,
|
|
103
|
+
type GateOutcome,
|
|
104
|
+
type GateReason,
|
|
105
|
+
type PresetRosterReader,
|
|
106
|
+
type PresetRosterRow,
|
|
107
|
+
type SettleStatus,
|
|
108
|
+
} from './gate.ts'
|
|
109
|
+
export {
|
|
110
|
+
bashPath,
|
|
111
|
+
parseChecksums,
|
|
112
|
+
resolveRuntime,
|
|
113
|
+
runtimeRoot,
|
|
114
|
+
shPath,
|
|
115
|
+
verifyPayload,
|
|
116
|
+
} from './runtime.ts'
|
|
117
|
+
|
|
118
|
+
/** Cordis plugin name; also the loader row id this package mounts under. */
|
|
119
|
+
export const name = 'dsh-win-bash'
|
|
120
|
+
|
|
121
|
+
/** Required services: the tool registry and the managed subprocess seam. */
|
|
122
|
+
export const inject = ['tools', 'subprocess']
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* One human-readable line explaining a refused gate verdict.
|
|
126
|
+
* @param outcome - the gate's verdict.
|
|
127
|
+
* @returns the message the entry logs.
|
|
128
|
+
*/
|
|
129
|
+
export function gateRefusalMessage(outcome: GateOutcome): string {
|
|
130
|
+
const settleNote = outcome.settled === 'timed-out'
|
|
131
|
+
? '; the host plugin tree did not settle in time, so the roster was read during boot'
|
|
132
|
+
: ''
|
|
133
|
+
if (outcome.reason === 'registry-absent') {
|
|
134
|
+
return 'win_bash was not registered (syncWithLiangShen is on and this deployment publishes no agent-preset registry,'
|
|
135
|
+
+ ' so a preset with id ' + LIANGSHEN_PRESET_ID + ' cannot be declared)' + settleNote
|
|
136
|
+
}
|
|
137
|
+
if (outcome.reason === 'registry-unreadable') {
|
|
138
|
+
return 'win_bash was not registered (syncWithLiangShen is on and the agent-preset registry roster could not be read)'
|
|
139
|
+
+ settleNote
|
|
140
|
+
}
|
|
141
|
+
return 'win_bash was not registered (syncWithLiangShen is on and no preset with id '
|
|
142
|
+
+ LIANGSHEN_PRESET_ID + ' is declared; enable the dsh-liangshen plugin row, or set syncWithLiangShen to false to'
|
|
143
|
+
+ ' register this shell without the preset)' + settleNote
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Register the win_bash tool and its per-session shell registry on the calling
|
|
148
|
+
* fiber. Called only once per fiber: the caller decides whether to call at all.
|
|
149
|
+
*
|
|
150
|
+
* The host pwsh replacement is installed from HERE and not from the gate or
|
|
151
|
+
* apply, because its precondition is exactly "win_bash is now on the wire": a
|
|
152
|
+
* deployment whose gate refused, whose payload failed verification, or that
|
|
153
|
+
* runs on a POSIX host never reaches this function and therefore never touches
|
|
154
|
+
* the host's shell.
|
|
155
|
+
* @param ctx - context carrying tools and subprocess.
|
|
156
|
+
* @param config - the resolved configuration.
|
|
157
|
+
* @param runtime - the verified payload paths.
|
|
158
|
+
*/
|
|
159
|
+
export function registerWinBash(ctx: Context, config: WinBashConfig, runtime: RuntimeReady): void {
|
|
160
|
+
const shells = new ShellRegistry(ctx.subprocess)
|
|
161
|
+
const tool = buildWinBashTool(ctx, config, runtime, shells)
|
|
162
|
+
|
|
163
|
+
ctx.effect(() => {
|
|
164
|
+
const unregister = ctx.tools.register(tool)
|
|
165
|
+
return async () => {
|
|
166
|
+
unregister()
|
|
167
|
+
// Unload must reach quiescence: the shells are real processes the host
|
|
168
|
+
// would otherwise keep hosting with nobody able to talk to them.
|
|
169
|
+
await shells.disposeAll('the dsh-win-bash plugin was unloaded')
|
|
170
|
+
}
|
|
171
|
+
}, 'dsh-win-bash: win_bash tool and its persistent shells')
|
|
172
|
+
|
|
173
|
+
// One session's shell dies with that session. The agent object is the key,
|
|
174
|
+
// so a session that ends for any reason (close, archive, reload) releases
|
|
175
|
+
// its process instead of leaving it to the plugin's own disposal.
|
|
176
|
+
ctx.on('agent/disposed', ({ agent }) => {
|
|
177
|
+
void shells.dispose(agent, 'the owning agent was disposed')
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
if (config.replacePwsh) installPwshReplacement(ctx)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Apply the availability gate, then register the tool when it allows it.
|
|
185
|
+
*
|
|
186
|
+
* The returned promise is deliberately NOT awaited by apply: the gate's roster
|
|
187
|
+
* read audits every declared preset and can await the Host loader tree, which
|
|
188
|
+
* still counts this very activation among its pending tasks — awaiting it from
|
|
189
|
+
* inside the activation would make the two waits depend on each other and
|
|
190
|
+
* neither would settle. Keeping the decision off the activation path also
|
|
191
|
+
* keeps the loader entry's own inertia free of it, so the tree can settle.
|
|
192
|
+
*
|
|
193
|
+
* A fiber disposed while the gate was deciding registers nothing: its tool
|
|
194
|
+
* effect would be refused anyway, and its shells would outlive it.
|
|
195
|
+
* @param ctx - context carrying tools and subprocess.
|
|
196
|
+
* @param config - the resolved configuration.
|
|
197
|
+
* @param runtime - the verified payload paths.
|
|
198
|
+
* @param options - gate deadlines; the defaults are the shipped bounds.
|
|
199
|
+
* @returns the decision, for the caller to observe (never rejects).
|
|
200
|
+
*/
|
|
201
|
+
export async function mountWithGate(
|
|
202
|
+
ctx: Context,
|
|
203
|
+
config: WinBashConfig,
|
|
204
|
+
runtime: RuntimeReady,
|
|
205
|
+
options: GateOptions = {},
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
let closed = false
|
|
208
|
+
ctx.effect(() => () => { closed = true }, 'dsh-win-bash: gate continuation lifetime')
|
|
209
|
+
|
|
210
|
+
const mount = (announcement?: string): void => {
|
|
211
|
+
if (announcement !== undefined) ctx.logger?.info?.(announcement)
|
|
212
|
+
// Registration is deliberately OUTSIDE the gate's try: a tool registry that
|
|
213
|
+
// refuses the registration is not a gate defect, and reporting it as one
|
|
214
|
+
// would send an operator to the wrong place. It must also not escape as a
|
|
215
|
+
// rejection, because this continuation is not awaited by the loader's own
|
|
216
|
+
// activation and an unhandled rejection here kills the whole process.
|
|
217
|
+
try {
|
|
218
|
+
registerWinBash(ctx, config, runtime)
|
|
219
|
+
} catch (error) {
|
|
220
|
+
ctx.logger?.error?.('dsh-win-bash: win_bash could not be registered: '
|
|
221
|
+
+ (error instanceof Error ? error.message : String(error)))
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (!config.syncWithLiangShen) {
|
|
226
|
+
mount()
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let outcome: GateOutcome
|
|
231
|
+
try {
|
|
232
|
+
outcome = await startLiangShenGate(ctx, options)
|
|
233
|
+
} catch (error) {
|
|
234
|
+
// The gate contains its own failures; this arm keeps a defect in it from
|
|
235
|
+
// escaping as an unhandled rejection, which the host's fail-loud guard
|
|
236
|
+
// turns into a whole-process kill.
|
|
237
|
+
if (closed) return
|
|
238
|
+
ctx.logger?.warn?.('dsh-win-bash: win_bash was not registered (the LiangShen availability gate failed: '
|
|
239
|
+
+ (error instanceof Error ? error.message : String(error)) + ')')
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
if (closed) return
|
|
243
|
+
if (!outcome.allowed) {
|
|
244
|
+
ctx.logger?.info?.('dsh-win-bash: ' + gateRefusalMessage(outcome))
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
mount('dsh-win-bash: win_bash registered (the ' + LIANGSHEN_PRESET_ID + ' preset is declared)')
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Mount the plugin.
|
|
252
|
+
* @param ctx - context carrying tools and subprocess.
|
|
253
|
+
* @param config - the row's config, validated by {@link Config}.
|
|
254
|
+
*/
|
|
255
|
+
export function apply(ctx: Context, config?: WinBashConfigInput): void {
|
|
256
|
+
const host: HostPlatform = { platform: process.platform, arch: process.arch }
|
|
257
|
+
const runtime = resolveRuntime(host)
|
|
258
|
+
if ('code' in runtime) {
|
|
259
|
+
// Refusing the mount is the point: there is no fallback shell, and a
|
|
260
|
+
// mounted tool that always errors is worse than an honest absence.
|
|
261
|
+
ctx.logger?.warn?.('dsh-win-bash: win_bash was not mounted (' + runtime.code + '): ' + runtime.message)
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
let resolved: WinBashConfig
|
|
265
|
+
try {
|
|
266
|
+
resolved = resolveConfig(config)
|
|
267
|
+
} catch (error) {
|
|
268
|
+
ctx.logger?.error?.('dsh-win-bash: ' + (error instanceof Error ? error.message : String(error)))
|
|
269
|
+
throw error
|
|
270
|
+
}
|
|
271
|
+
// Deliberately detached (see mountWithGate), with a last-resort catch so no
|
|
272
|
+
// defect above can surface as the unhandled rejection that kills the process.
|
|
273
|
+
void mountWithGate(ctx, resolved, runtime).catch((error: unknown) => {
|
|
274
|
+
ctx.logger?.error?.('dsh-win-bash: the mount continuation failed: '
|
|
275
|
+
+ (error instanceof Error ? error.message : String(error)))
|
|
276
|
+
})
|
|
277
|
+
}
|