@linxin666/dsh-win-bash 0.4.21 → 0.4.23
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/README.i18n.yaml +2 -2
- package/README.md +17 -0
- package/README.zh.md +14 -0
- package/lib/index.js +455 -28
- package/lib/types/dsh-home.d.ts +13 -3
- package/lib/types/environment.d.ts +162 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/pwsh-replacement.d.ts +16 -4
- package/package.json +9 -9
- package/src/dsh-home.ts +35 -12
- package/src/environment.ts +516 -0
- package/src/index.ts +11 -0
- package/src/pwsh-replacement.ts +78 -14
- package/src/tool.ts +54 -5
package/src/pwsh-replacement.ts
CHANGED
|
@@ -37,8 +37,18 @@
|
|
|
37
37
|
* entering a restricted mode releases the restriction immediately, and
|
|
38
38
|
* returning to danger-full-access withdraws again. The listener is registered
|
|
39
39
|
* with `{ global: true }` because a session's own log event is not dispatched to
|
|
40
|
-
* this plugin's scope
|
|
41
|
-
*
|
|
40
|
+
* this plugin's scope. `internal/dispatch` fires while the event is still being
|
|
41
|
+
* validated, BEFORE the session log and the policy's mode projection see it, so
|
|
42
|
+
* a read made inside the listener answers with the OLD mode (issue #2096); the
|
|
43
|
+
* re-evaluation is therefore queued as one microtask, which runs once the
|
|
44
|
+
* synchronous append has committed — no poll, no timer, nothing on a boot path.
|
|
45
|
+
*
|
|
46
|
+
* THE POLICY SERVICE CAN ARRIVE AFTER THIS MODULE. On a cold start the
|
|
47
|
+
* `sandboxPolicy` service may not be registered yet when win_bash mounts, so
|
|
48
|
+
* the authority is resolved on every evaluation instead of being captured
|
|
49
|
+
* once at install time (a captured `undefined` made every later verdict
|
|
50
|
+
* 'unknown' forever, issue #2096), and its registration (`internal/service`)
|
|
51
|
+
* re-evaluates every watched scope.
|
|
42
52
|
*
|
|
43
53
|
* WINDOWS ONLY. The ladder this module encodes is a Windows fact: win_bash is
|
|
44
54
|
* the pinned MSYS2 shell a Windows session gets, and the host shell beside it
|
|
@@ -115,8 +125,12 @@ interface AgentScopeLike {
|
|
|
115
125
|
|
|
116
126
|
/** The registry read this module consumes. */
|
|
117
127
|
interface SurfaceReader {
|
|
118
|
-
/**
|
|
119
|
-
|
|
128
|
+
/** Single-tool lookup on DSH 0.2.3-alpha.3+ (`tool-registry-roots`). */
|
|
129
|
+
get?(name: string, agent?: unknown): unknown
|
|
130
|
+
/** All visible model-facing definitions on DSH 0.2.3-alpha.3+ (`tool-registry-roots`). */
|
|
131
|
+
getAll?(agent?: unknown): unknown
|
|
132
|
+
/** Legacy model-facing surface lookup (pre-0.2.3-alpha.3 / test stubs). */
|
|
133
|
+
schemas?(agent?: unknown): unknown
|
|
120
134
|
}
|
|
121
135
|
|
|
122
136
|
/** One watched agent scope and the restriction it currently carries. */
|
|
@@ -140,8 +154,10 @@ export interface PwshReplacementOptions {
|
|
|
140
154
|
/** Tool names to withdraw; defaults to {@link HOST_PWSH_TOOL}. */
|
|
141
155
|
readonly denied?: readonly string[]
|
|
142
156
|
/**
|
|
143
|
-
* One-time warning sink. The default writes through the plugin logger
|
|
144
|
-
*
|
|
157
|
+
* One-time warning sink. The default writes through the plugin logger AND to
|
|
158
|
+
* stderr: the `dsh web` boot exporter keeps logger warnings in memory only, so
|
|
159
|
+
* a logger-only warning never reaches the operator's terminal (issue #2096).
|
|
160
|
+
* A caller may replace it.
|
|
145
161
|
*/
|
|
146
162
|
readonly warn?: (message: string) => void
|
|
147
163
|
/**
|
|
@@ -224,14 +240,46 @@ function servingVerdict(read: SessionPolicyReader | undefined, session: unknown)
|
|
|
224
240
|
}
|
|
225
241
|
|
|
226
242
|
/**
|
|
227
|
-
* The tool names one agent scope currently sees
|
|
243
|
+
* The tool names one agent scope currently sees (or `denied` directly under a
|
|
244
|
+
* PTC root where `tools.get('run_code', agent)` hides program bindings from
|
|
245
|
+
* `getAll()`).
|
|
228
246
|
* @param ctx - the plugin context.
|
|
229
247
|
* @param agent - the scope to read for.
|
|
248
|
+
* @param denied - the tool names this replacement intends to withdraw.
|
|
230
249
|
* @returns the names, or undefined when no usable projection is readable.
|
|
231
250
|
*/
|
|
232
|
-
function visibleNames(ctx: Context, agent: unknown): readonly string[] | undefined {
|
|
251
|
+
function visibleNames(ctx: Context, agent: unknown, denied: readonly string[]): readonly string[] | undefined {
|
|
233
252
|
const service = readService(ctx, 'tools') as SurfaceReader | undefined
|
|
234
|
-
if (service === null || typeof service !== 'object'
|
|
253
|
+
if (service === null || typeof service !== 'object') return undefined
|
|
254
|
+
// Under DSH 0.2.3-alpha.3 PTC roots, `getAll()` exposes only `run_code` while
|
|
255
|
+
// `pwsh` remains callable inside PTC programs; `restrict({ deny })` accepts
|
|
256
|
+
// arbitrary tool names and strips `pwsh` from the program bindings.
|
|
257
|
+
if (typeof service.get === 'function') {
|
|
258
|
+
try {
|
|
259
|
+
if (service.get('run_code', agent) !== undefined) return [...denied]
|
|
260
|
+
} catch {
|
|
261
|
+
// Fall through to surface listing when single lookup refuses.
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (typeof service.getAll === 'function') {
|
|
265
|
+
try {
|
|
266
|
+
const entries = service.getAll(agent)
|
|
267
|
+
if (!Array.isArray(entries)) return undefined
|
|
268
|
+
return entries
|
|
269
|
+
.map(entry => (entry as { name?: unknown } | undefined)?.name)
|
|
270
|
+
.filter((name): name is string => typeof name === 'string' && name !== '')
|
|
271
|
+
} catch {
|
|
272
|
+
return undefined
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (typeof service.get === 'function') {
|
|
276
|
+
try {
|
|
277
|
+
return denied.filter(name => service.get!(name, agent) !== undefined)
|
|
278
|
+
} catch {
|
|
279
|
+
return undefined
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (typeof service.schemas !== 'function') return undefined
|
|
235
283
|
try {
|
|
236
284
|
const schemas = service.schemas(agent)
|
|
237
285
|
if (!Array.isArray(schemas)) return undefined
|
|
@@ -259,7 +307,9 @@ export function installPwshReplacement(ctx: Context, options: PwshReplacementOpt
|
|
|
259
307
|
if ((options.platform ?? process.platform) !== 'win32') return
|
|
260
308
|
|
|
261
309
|
const denied = [...(options.denied ?? [HOST_PWSH_TOOL])]
|
|
262
|
-
|
|
310
|
+
// Resolved per evaluation, never captured: see "THE POLICY SERVICE CAN ARRIVE
|
|
311
|
+
// AFTER THIS MODULE" in the module head.
|
|
312
|
+
const read = (): SessionPolicyReader | undefined => policyReader(ctx, options.policy)
|
|
263
313
|
/** Agent scope -> the session it owns and the live restriction, if any. */
|
|
264
314
|
const watched = new Map<AgentScopeLike, WatchedScope>()
|
|
265
315
|
let warned = false
|
|
@@ -273,6 +323,11 @@ export function installPwshReplacement(ctx: Context, options: PwshReplacementOpt
|
|
|
273
323
|
return
|
|
274
324
|
}
|
|
275
325
|
ctx.logger?.warn?.(message)
|
|
326
|
+
try {
|
|
327
|
+
process.stderr.write(message + '\n')
|
|
328
|
+
} catch {
|
|
329
|
+
// A closed stream must not become a new failure.
|
|
330
|
+
}
|
|
276
331
|
}
|
|
277
332
|
const warnOnce = (message: string): void => {
|
|
278
333
|
if (warned) return
|
|
@@ -338,7 +393,7 @@ export function installPwshReplacement(ctx: Context, options: PwshReplacementOpt
|
|
|
338
393
|
const evaluate = (scope: AgentScopeLike): void => {
|
|
339
394
|
if (scope === null || typeof scope !== 'object') return
|
|
340
395
|
const record = watch(scope)
|
|
341
|
-
const verdict = servingVerdict(read, record.session)
|
|
396
|
+
const verdict = servingVerdict(read(), record.session)
|
|
342
397
|
if (verdict === 'unknown') {
|
|
343
398
|
// Not a per-session defect worth repeating: the cause is deployment-wide
|
|
344
399
|
// (no policy service, or one that cannot answer), and the effect is that
|
|
@@ -353,7 +408,8 @@ export function installPwshReplacement(ctx: Context, options: PwshReplacementOpt
|
|
|
353
408
|
return
|
|
354
409
|
}
|
|
355
410
|
if (record.dispose !== undefined) return
|
|
356
|
-
const
|
|
411
|
+
const visible = visibleNames(ctx, scope, denied) ?? []
|
|
412
|
+
const names = denied.filter(name => visible.includes(name))
|
|
357
413
|
if (names.length === 0) return
|
|
358
414
|
const restrict = scope.ctx?.tools?.restrict
|
|
359
415
|
if (typeof restrict !== 'function') {
|
|
@@ -405,16 +461,24 @@ export function installPwshReplacement(ctx: Context, options: PwshReplacementOpt
|
|
|
405
461
|
ctx.on('tools/change', () => {
|
|
406
462
|
for (const scope of [...watched.keys()]) evaluate(scope)
|
|
407
463
|
})
|
|
464
|
+
// A cold start can mount this row before the sandbox policy service exists;
|
|
465
|
+
// its registration is the moment every watched scope can first be decided.
|
|
466
|
+
ctx.on('internal/service', (name: unknown) => {
|
|
467
|
+
if (options.policy !== undefined || name !== 'sandboxPolicy') return
|
|
468
|
+
for (const scope of [...watched.keys()]) evaluate(scope)
|
|
469
|
+
})
|
|
408
470
|
// A permission-preset switch writes sandbox/mode into the session's own log,
|
|
409
471
|
// and that is not dispatched to this plugin's scope, so this listener must be
|
|
410
|
-
// global to see it.
|
|
472
|
+
// global to see it. The dispatch is observed before the log and the mode
|
|
473
|
+
// projection commit the event, so the re-evaluation runs one microtask later,
|
|
474
|
+
// once the synchronous append has finished; no timer is installed and the
|
|
411
475
|
// restriction follows the mode as it is switched, in both directions.
|
|
412
476
|
ctx.on('internal/dispatch', (_mode, eventName, args) => {
|
|
413
477
|
if (eventName !== 'session/event') return
|
|
414
478
|
if (!Array.isArray(args)) return
|
|
415
479
|
const [session, event] = args as [unknown, { type?: unknown } | undefined]
|
|
416
480
|
if (event?.type !== 'sandbox/mode') return
|
|
417
|
-
reevaluate(session)
|
|
481
|
+
queueMicrotask(() => { reevaluate(session) })
|
|
418
482
|
}, { global: true })
|
|
419
483
|
|
|
420
484
|
// Sessions that were already running when this plugin mounted.
|
package/src/tool.ts
CHANGED
|
@@ -20,6 +20,7 @@ import type { ToolDefinition } from '@deepseek-ai/dsh-tools'
|
|
|
20
20
|
import type { JobRegistryLike } from './background.ts'
|
|
21
21
|
import { BACKGROUND_STDERR_BYTES, BACKGROUND_STDOUT_BYTES, startBackgroundJob } from './background.ts'
|
|
22
22
|
import type { WinBashConfig } from './config.ts'
|
|
23
|
+
import { createRemoteWinBashRunner } from './environment.ts'
|
|
23
24
|
import { shellArgv, shellEnvironment, shellTempDir, type ShellRegistry } from './shell.ts'
|
|
24
25
|
|
|
25
26
|
/** The registered tool name. Deliberately not bash. */
|
|
@@ -324,8 +325,10 @@ export function buildWinBashTool(
|
|
|
324
325
|
// no job registry, so run_in_background is not advertised where it could
|
|
325
326
|
// never be honoured.
|
|
326
327
|
const jobs = config.enableRunInBackground ? readJobRegistry(ctx) : undefined
|
|
328
|
+
const remote = createRemoteWinBashRunner(ctx)
|
|
327
329
|
return defineTool({
|
|
328
330
|
name: WIN_BASH_TOOL,
|
|
331
|
+
environment: 'bound',
|
|
329
332
|
description: config.description,
|
|
330
333
|
parameters: {
|
|
331
334
|
command: {
|
|
@@ -389,10 +392,6 @@ export function buildWinBashTool(
|
|
|
389
392
|
if (agent === undefined) {
|
|
390
393
|
throw new Error('win_bash needs an owning agent session: the persistent shell belongs to the session that calls it.')
|
|
391
394
|
}
|
|
392
|
-
const cwd = sessionCwd(agent)
|
|
393
|
-
if (cwd === undefined) {
|
|
394
|
-
throw new Error('win_bash needs a calling session with a working directory.')
|
|
395
|
-
}
|
|
396
395
|
const policy = resolvePolicy(ctx, agent)
|
|
397
396
|
if (policy === undefined) {
|
|
398
397
|
throw new Error(
|
|
@@ -401,10 +400,60 @@ export function buildWinBashTool(
|
|
|
401
400
|
+ ' whenever win_bash cannot serve a call.',
|
|
402
401
|
)
|
|
403
402
|
}
|
|
403
|
+
const timeoutMs = clampTimeout(args.timeoutMs, config.timeoutMs)
|
|
404
|
+
|
|
405
|
+
// Remote execution environment branch (DSH 0.2.3-alpha.4+): forwards to
|
|
406
|
+
// `ctx.environments` with `remoteSandboxPolicy`, so confined modes work
|
|
407
|
+
// on POSIX target hosts without hitting Windows MSYS2's restricted-token crash.
|
|
408
|
+
if (exec.environment !== undefined) {
|
|
409
|
+
const workdir = args.workdir === undefined || args.workdir === '' ? undefined : args.workdir
|
|
410
|
+
if (args.run_in_background === true) {
|
|
411
|
+
if (!config.enableRunInBackground) {
|
|
412
|
+
throw new Error(
|
|
413
|
+
'win_bash: run_in_background is disabled for this deployment (enableRunInBackground: false),'
|
|
414
|
+
+ ' so this call was not started.',
|
|
415
|
+
)
|
|
416
|
+
}
|
|
417
|
+
const registry = readJobRegistry(ctx)
|
|
418
|
+
if (registry === undefined) {
|
|
419
|
+
throw new Error(
|
|
420
|
+
'BACKGROUND_UNAVAILABLE: this deployment composes no background job registry (ctx.jobs), so win_bash cannot'
|
|
421
|
+
+ ' register a background command. This call was not run in the foreground instead. Run it without'
|
|
422
|
+
+ ' run_in_background, or start it in the background with the host pwsh tool, which is the native shell for'
|
|
423
|
+
+ ' this platform and stays available whenever win_bash cannot serve a call.',
|
|
424
|
+
)
|
|
425
|
+
}
|
|
426
|
+
const jobId = remote.startBackground({
|
|
427
|
+
jobs: registry,
|
|
428
|
+
owner: agent.id,
|
|
429
|
+
target: exec.environment,
|
|
430
|
+
command: args.command,
|
|
431
|
+
workdir,
|
|
432
|
+
maxStdoutBytes: BACKGROUND_STDOUT_BYTES,
|
|
433
|
+
maxStderrBytes: BACKGROUND_STDERR_BYTES,
|
|
434
|
+
policy,
|
|
435
|
+
})
|
|
436
|
+
return { stdout: '', stderr: '', exitCode: 0, jobId }
|
|
437
|
+
}
|
|
438
|
+
return await remote.runForeground({
|
|
439
|
+
target: exec.environment,
|
|
440
|
+
signal: exec.signal,
|
|
441
|
+
command: args.command,
|
|
442
|
+
workdir,
|
|
443
|
+
timeoutMs,
|
|
444
|
+
maxStdoutBytes: config.maxStdoutBytes,
|
|
445
|
+
maxStderrBytes: config.maxStderrBytes,
|
|
446
|
+
policy,
|
|
447
|
+
})
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const cwd = sessionCwd(agent)
|
|
451
|
+
if (cwd === undefined) {
|
|
452
|
+
throw new Error('win_bash needs a calling session with a working directory.')
|
|
453
|
+
}
|
|
404
454
|
// Before anything is spawned or registered: a restricted session is told
|
|
405
455
|
// so, rather than handed a shell that dies before running its command.
|
|
406
456
|
assertUnconfined(policy)
|
|
407
|
-
const timeoutMs = clampTimeout(args.timeoutMs, config.timeoutMs)
|
|
408
457
|
const command = args.workdir === undefined || args.workdir === ''
|
|
409
458
|
? args.command
|
|
410
459
|
: 'cd ' + shellQuote(normalizeWorkdir(args.workdir)) + ' && ' + args.command
|