@linxin666/dsh-win-bash 0.0.0-stage → 0.4.8
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 +264 -2
- package/README.zh.md +221 -0
- package/cordis.patch.yml +13 -0
- package/lib/index.js +2756 -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 +123 -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 +481 -0
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire protocol for the session's persistent bash shell.
|
|
3
|
+
*
|
|
4
|
+
* The shell is one long-lived 'bash -s' process per session, so every tool
|
|
5
|
+
* call must (a) inject its command without racing the previous one and (b)
|
|
6
|
+
* find the exact end of that command's output and its exit status inside one
|
|
7
|
+
* continuous byte stream. Both jobs ride this module and nothing else, so the
|
|
8
|
+
* framing is unit-testable without a process.
|
|
9
|
+
*
|
|
10
|
+
* Frame shape, written on stdin for each call:
|
|
11
|
+
*
|
|
12
|
+
* printf '\n%s\n' '<M>BEGIN'
|
|
13
|
+
* . /dev/stdin <<'<M>END'
|
|
14
|
+
* <the model's command, verbatim, any number of lines>
|
|
15
|
+
* <M>END
|
|
16
|
+
* __dsh_win_bash_status=$?
|
|
17
|
+
* printf '\n%s%d\n' '<M>END' "$__dsh_win_bash_status"
|
|
18
|
+
*
|
|
19
|
+
* Why a heredoc and not a plain brace-group: the shell parses a brace group
|
|
20
|
+
* one command at a time, so a syntactic error inside it aborts the REST of the
|
|
21
|
+
* group — a model line with an unterminated quote would swallow the end marker
|
|
22
|
+
* and leave the call (and the shell) wedged forever. The command is therefore
|
|
23
|
+
* read through a heredoc into the source builtin, which the shell parses as
|
|
24
|
+
* ONE unit: a syntax error is reported and the shell moves on to the next
|
|
25
|
+
* line, where the status capture and the end marker still run. Measured on the
|
|
26
|
+
* vendored bash: an unterminated quote produces 'unexpected EOF while looking
|
|
27
|
+
* for matching' on stderr, and the very next call succeeds on the same
|
|
28
|
+
* process.
|
|
29
|
+
*
|
|
30
|
+
* The begin/end markers carry a per-call nonce, so a command cannot forge the
|
|
31
|
+
* end of its own frame even if it echoes marker-shaped text.
|
|
32
|
+
* @module @linxin666/dsh-win-bash/protocol
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/** Literal prefix of every frame marker; the nonce follows it. */
|
|
36
|
+
export const MARKER_PREFIX = '__DSH_WIN_BASH_'
|
|
37
|
+
|
|
38
|
+
/** Suffix closing the nonce, keeping the marker a single shell word. */
|
|
39
|
+
export const MARKER_SUFFIX = '__'
|
|
40
|
+
|
|
41
|
+
/** Begin/end marker pair for one call. */
|
|
42
|
+
export interface FrameMarker {
|
|
43
|
+
/** Printed before the command runs; the command's output follows it. */
|
|
44
|
+
readonly begin: string
|
|
45
|
+
/** Printed after the command's status is captured; the digits follow it. */
|
|
46
|
+
readonly end: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Build the marker pair for one call. The nonce is stripped to alphanumerics
|
|
51
|
+
* so the markers stay valid heredoc delimiters and single shell words.
|
|
52
|
+
* @param nonce - caller-supplied uniqueness source (a UUID).
|
|
53
|
+
* @returns the begin/end markers, both printable and both nonce-bearing.
|
|
54
|
+
*/
|
|
55
|
+
export function markerFor(nonce: string): FrameMarker {
|
|
56
|
+
const safe = nonce.replace(/[^A-Za-z0-9]/g, '')
|
|
57
|
+
const base = MARKER_PREFIX + safe + MARKER_SUFFIX
|
|
58
|
+
return { begin: base + 'BEGIN', end: base + 'END' }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Render the stdin frame for one command. The command is inserted verbatim:
|
|
63
|
+
* the heredoc is quoted, so the shell performs no expansion on it while
|
|
64
|
+
* reading, and the sourced text is executed exactly as the model wrote it.
|
|
65
|
+
* @param command - the model's command, any number of lines.
|
|
66
|
+
* @param marker - the markers for this call.
|
|
67
|
+
* @returns the text to write to the shell's stdin, newline-terminated.
|
|
68
|
+
*/
|
|
69
|
+
export function buildFrame(command: string, marker: FrameMarker): string {
|
|
70
|
+
return [
|
|
71
|
+
"printf '\\n%s\\n' '" + marker.begin + "'",
|
|
72
|
+
". /dev/stdin <<'" + marker.end + "'",
|
|
73
|
+
command,
|
|
74
|
+
marker.end,
|
|
75
|
+
'__dsh_win_bash_status=$?',
|
|
76
|
+
"printf '\\n%s%d\\n' '" + marker.end + '\' "$__dsh_win_bash_status"',
|
|
77
|
+
'',
|
|
78
|
+
].join('\n')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** One complete command frame recovered from the output stream. */
|
|
82
|
+
export interface FrameOutcome {
|
|
83
|
+
/** The command's stdout: everything between the two markers, verbatim. */
|
|
84
|
+
readonly stdout: string
|
|
85
|
+
/** The command's exit status. */
|
|
86
|
+
readonly exitCode: number
|
|
87
|
+
/**
|
|
88
|
+
* Whether the begin marker had already slid out of the retained output
|
|
89
|
+
* window, so 'stdout' is a suffix of the real output rather than all of it.
|
|
90
|
+
*/
|
|
91
|
+
readonly lostPrefix: boolean
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Recover one command's stdout and exit status from accumulated output.
|
|
96
|
+
* Unframed or still-running text returns undefined, so the caller keeps
|
|
97
|
+
* waiting instead of reporting a truncated result.
|
|
98
|
+
* @param text - accumulated stdout for the current call.
|
|
99
|
+
* @param marker - the markers for this call.
|
|
100
|
+
* @param sawBegin - whether the begin marker was observed before the window slid.
|
|
101
|
+
* @returns the outcome once the end marker and its digits arrived.
|
|
102
|
+
*/
|
|
103
|
+
export function parseFrame(text: string, marker: FrameMarker, sawBegin: boolean): FrameOutcome | undefined {
|
|
104
|
+
const beginIndex = text.indexOf(marker.begin)
|
|
105
|
+
const endIndex = text.indexOf(marker.end, beginIndex < 0 ? 0 : beginIndex + marker.begin.length)
|
|
106
|
+
if (endIndex < 0) return undefined
|
|
107
|
+
const status = /^(\d+)\r?\n/.exec(text.slice(endIndex + marker.end.length))
|
|
108
|
+
if (status === null) return undefined
|
|
109
|
+
if (beginIndex < 0 && !sawBegin) return undefined
|
|
110
|
+
let bodyStart = beginIndex < 0 ? 0 : beginIndex + marker.begin.length
|
|
111
|
+
// The begin printf writes a newline after its marker; drop that one so the
|
|
112
|
+
// recovered stdout starts at the command's first byte.
|
|
113
|
+
if (text[bodyStart] === '\n') bodyStart += 1
|
|
114
|
+
let stdout = text.slice(bodyStart, endIndex)
|
|
115
|
+
// The end printf writes a newline BEFORE its marker; drop exactly that one
|
|
116
|
+
// and keep whatever newline the command itself produced.
|
|
117
|
+
if (stdout.endsWith('\n')) stdout = stdout.slice(0, -1)
|
|
118
|
+
return { stdout, exitCode: Number(status[1]), lostPrefix: beginIndex < 0 }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Whether a frame's END marker is already visible in the accumulated text.
|
|
123
|
+
* A cheap pre-check used to decide when to run the full parse.
|
|
124
|
+
* @param text - accumulated stdout for the current call.
|
|
125
|
+
* @param marker - the markers for this call.
|
|
126
|
+
* @returns true once the end marker appears.
|
|
127
|
+
*/
|
|
128
|
+
export function hasEndMarker(text: string, marker: FrameMarker): boolean {
|
|
129
|
+
return text.includes(marker.end)
|
|
130
|
+
}
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host pwsh replacement: while this plugin's win_bash tool actually SERVES a
|
|
3
|
+
* session, the host's own PowerShell tool comes off that session's
|
|
4
|
+
* model-facing surface.
|
|
5
|
+
*
|
|
6
|
+
* WHY. The session then carries two shells, and nothing in either tool's
|
|
7
|
+
* schema says which one to reach for first: a model defaults to the shell it
|
|
8
|
+
* knows, which on Windows is pwsh, so bash-syntax commands keep arriving at a
|
|
9
|
+
* PowerShell that answers them with a syntax error. Hiding pwsh is what makes
|
|
10
|
+
* the preference structural instead of advisory — the ladder is win_bash
|
|
11
|
+
* first, and pwsh stays the fallback for the Windows-native work only it can
|
|
12
|
+
* do.
|
|
13
|
+
*
|
|
14
|
+
* WITHDRAWN ONLY WHERE WIN_BASH CAN SERVE. The precondition is "win_bash can
|
|
15
|
+
* serve this session", never "win_bash is mounted". win_bash runs only in a
|
|
16
|
+
* danger-full-access session (see ./tool.ts and the measured MSYS limitation in
|
|
17
|
+
* the package README): under any restricted mode it refuses every call before a
|
|
18
|
+
* process is started. Taking pwsh away there would leave the session with NO
|
|
19
|
+
* shell at all — win_bash refuses, and the shell that would have run the
|
|
20
|
+
* command is gone - which is precisely the capability loss this module exists
|
|
21
|
+
* to prevent. So a session keeps its pwsh unless its OWN policy reads
|
|
22
|
+
* danger-full-access.
|
|
23
|
+
*
|
|
24
|
+
* AN UNREADABLE POLICY IS NOT A YES. When the deployment composes no sandbox
|
|
25
|
+
* policy service, when the service throws, when it answers with a mode outside
|
|
26
|
+
* the closed vocabulary, or when the agent exposes no session to ask about,
|
|
27
|
+
* this module does NOT withdraw: win_bash refuses to run in exactly those
|
|
28
|
+
* deployments, so the session would lose its only usable shell on the strength
|
|
29
|
+
* of a guess that cannot be verified. The failure direction is deliberate and
|
|
30
|
+
* one-way — a session that keeps a shell it did not need is a preference not
|
|
31
|
+
* yet applied; a session that loses the only shell it had cannot run anything.
|
|
32
|
+
*
|
|
33
|
+
* THE MODE CAN CHANGE MID-SESSION. A permission-preset switch appends one
|
|
34
|
+
* `sandbox/mode` event to that session's log, so "can win_bash serve this
|
|
35
|
+
* session" is not a boot-time fact. The module re-evaluates on the same
|
|
36
|
+
* `internal/dispatch` seam terminal-bash uses for the identical purpose:
|
|
37
|
+
* entering a restricted mode releases the restriction immediately, and
|
|
38
|
+
* returning to danger-full-access withdraws again. The listener is registered
|
|
39
|
+
* with `{ global: true }` because a session's own log event is not dispatched to
|
|
40
|
+
* this plugin's scope, and it re-evaluates synchronously — no poll, no timer,
|
|
41
|
+
* nothing on a boot path.
|
|
42
|
+
*
|
|
43
|
+
* WINDOWS ONLY. The ladder this module encodes is a Windows fact: win_bash is
|
|
44
|
+
* the pinned MSYS2 shell a Windows session gets, and the host shell beside it
|
|
45
|
+
* is PowerShell. POSIX sessions keep exactly the behavior they had — the
|
|
46
|
+
* host's shell there is bash, which is not on the deny list in any case, and
|
|
47
|
+
* this module returns before it reads, listens to, or restricts anything.
|
|
48
|
+
*
|
|
49
|
+
* VISIBILITY ONLY, DELIBERATELY. This module withdraws the tool from the wire
|
|
50
|
+
* and from the generated SDK; it installs no execution guard and no
|
|
51
|
+
* pre-execute denial. A model that has a reason to call pwsh anyway — a
|
|
52
|
+
* Windows-native cmdlet, or a fallback after win_bash failed - must still
|
|
53
|
+
* reach it, which is the ladder's lower rung. A hard refusal here would trade
|
|
54
|
+
* a preference for a capability loss.
|
|
55
|
+
*
|
|
56
|
+
* SCOPED, NEVER GLOBAL. The restriction is installed through each agent's own
|
|
57
|
+
* scoped tools view (`agent.ctx.tools.restrict`), so it covers exactly the
|
|
58
|
+
* sessions win_bash serves and nothing else in the process. A deployment that
|
|
59
|
+
* mounts no win_bash (a refused gate, a missing payload, a non-Windows host,
|
|
60
|
+
* or `replacePwsh: false`) never calls this module at all.
|
|
61
|
+
*
|
|
62
|
+
* TIMING. The host's pwsh is registered by the host's own composition, whose
|
|
63
|
+
* group can settle after this plugin mounts, so a name that is not on the
|
|
64
|
+
* readable surface yet is not a verdict: the registry's own `tools/change`
|
|
65
|
+
* event retries every agent this module watches. Nothing here waits, polls, or
|
|
66
|
+
* holds a boot path open.
|
|
67
|
+
*
|
|
68
|
+
* LIFECYCLE. Every restriction is remembered per agent scope and lifted when
|
|
69
|
+
* that agent is disposed or when this plugin unloads, one isolated callback at
|
|
70
|
+
* a time: a throwing lift must not leave the remaining sessions with a shell
|
|
71
|
+
* they cannot see, and it must not leave the map holding a dead scope.
|
|
72
|
+
* @module @linxin666/dsh-win-bash/pwsh-replacement
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
76
|
+
import type { SandboxMode } from '@deepseek-ai/dsh-sandbox'
|
|
77
|
+
|
|
78
|
+
/** The host tool this module withdraws while win_bash serves a session. */
|
|
79
|
+
export const HOST_PWSH_TOOL = 'pwsh'
|
|
80
|
+
|
|
81
|
+
/** The one session mode win_bash can actually serve; every other mode refuses. */
|
|
82
|
+
export const SERVING_MODE: SandboxMode = 'danger-full-access'
|
|
83
|
+
|
|
84
|
+
/** Every mode the policy service may answer with. */
|
|
85
|
+
const SANDBOX_MODES: readonly SandboxMode[] = ['read-only', 'workspace-write', 'danger-full-access']
|
|
86
|
+
|
|
87
|
+
/** The scoped restriction call this module consumes from an agent's tools view. */
|
|
88
|
+
export interface ScopedRestrictor {
|
|
89
|
+
/**
|
|
90
|
+
* Mask global tool names for the calling agent scope.
|
|
91
|
+
* @param filter - the mask; only deny is used here.
|
|
92
|
+
* @returns the exact disposer that lifts the restriction.
|
|
93
|
+
*/
|
|
94
|
+
restrict(filter: { deny: readonly string[] }): () => void
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The policy read this module consumes from the sandbox-policy service. */
|
|
98
|
+
export interface SessionPolicyReader {
|
|
99
|
+
/**
|
|
100
|
+
* Resolve one session's effective execution policy.
|
|
101
|
+
* @param request - the session whose own `sandbox/mode` log outranks the
|
|
102
|
+
* deployment default; omitting it answers for the deployment.
|
|
103
|
+
* @returns the policy whose mode decides whether win_bash can serve it.
|
|
104
|
+
*/
|
|
105
|
+
resolve(request?: { session?: unknown, mode?: SandboxMode }): { mode: SandboxMode }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** One agent scope, as much of it as this module reads. */
|
|
109
|
+
interface AgentScopeLike {
|
|
110
|
+
/** The agent's own scoped context, when it exposes one. */
|
|
111
|
+
readonly ctx?: { readonly tools?: ScopedRestrictor }
|
|
112
|
+
/** The session this agent owns; its mode decides whether win_bash serves it. */
|
|
113
|
+
readonly session?: unknown
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The registry read this module consumes. */
|
|
117
|
+
interface SurfaceReader {
|
|
118
|
+
/** The model-facing surface one scope sees, or the global view when omitted. */
|
|
119
|
+
schemas(agent?: unknown): unknown
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** One watched agent scope and the restriction it currently carries. */
|
|
123
|
+
interface WatchedScope {
|
|
124
|
+
/** The session this scope belongs to, read once: an agent's session is fixed. */
|
|
125
|
+
readonly session: unknown
|
|
126
|
+
/** The live disposer; present exactly while the restriction is installed. */
|
|
127
|
+
dispose?: () => void
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** What one install run reports and where. */
|
|
131
|
+
export interface PwshReplacementOptions {
|
|
132
|
+
/**
|
|
133
|
+
* The host platform this run may act on; defaults to `process.platform`.
|
|
134
|
+
* Any value other than `win32` makes the call a total no-op, so no POSIX
|
|
135
|
+
* deployment can acquire a restriction, a listener, or a disposer from this
|
|
136
|
+
* module. Tests pass it explicitly instead of depending on the runner's own
|
|
137
|
+
* platform.
|
|
138
|
+
*/
|
|
139
|
+
readonly platform?: string
|
|
140
|
+
/** Tool names to withdraw; defaults to {@link HOST_PWSH_TOOL}. */
|
|
141
|
+
readonly denied?: readonly string[]
|
|
142
|
+
/**
|
|
143
|
+
* One-time warning sink. The default writes through the plugin logger; a
|
|
144
|
+
* caller may replace it so a host with no logger still sees the message.
|
|
145
|
+
*/
|
|
146
|
+
readonly warn?: (message: string) => void
|
|
147
|
+
/**
|
|
148
|
+
* The authority that decides whether win_bash can serve a session; defaults
|
|
149
|
+
* to the deployment's `sandboxPolicy` service. Injectable so a host can hand
|
|
150
|
+
* over an equivalent authority, and so a test can pin the decision without
|
|
151
|
+
* composing a service.
|
|
152
|
+
*/
|
|
153
|
+
readonly policy?: SessionPolicyReader
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** What the policy says about one session, including "cannot be read". */
|
|
157
|
+
type ServingVerdict = 'serves' | 'refuses' | 'unknown'
|
|
158
|
+
|
|
159
|
+
/** The message of a thrown value, whatever it is. */
|
|
160
|
+
function messageOf(error: unknown): string {
|
|
161
|
+
return error instanceof Error ? error.message : String(error)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* One optional service, read without letting a refused lookup escape.
|
|
166
|
+
* @param ctx - the plugin context.
|
|
167
|
+
* @param name - the service name.
|
|
168
|
+
* @returns the service, or undefined when it is not resolvable.
|
|
169
|
+
*/
|
|
170
|
+
function readService(ctx: Context, name: string): unknown {
|
|
171
|
+
try {
|
|
172
|
+
return (ctx as unknown as { get(key: string): unknown }).get(name)
|
|
173
|
+
} catch {
|
|
174
|
+
return undefined
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The policy authority for this install: the injected reader, else the
|
|
180
|
+
* deployment's sandbox-policy service.
|
|
181
|
+
* @param ctx - the plugin context.
|
|
182
|
+
* @param injected - the caller-supplied reader, when any.
|
|
183
|
+
* @returns the reader, or undefined when no usable one exists.
|
|
184
|
+
*/
|
|
185
|
+
function policyReader(ctx: Context, injected: SessionPolicyReader | undefined): SessionPolicyReader | undefined {
|
|
186
|
+
if (injected !== undefined) return injected
|
|
187
|
+
const service = readService(ctx, 'sandboxPolicy')
|
|
188
|
+
if (service === null || typeof service !== 'object') return undefined
|
|
189
|
+
if (typeof (service as SessionPolicyReader).resolve !== 'function') return undefined
|
|
190
|
+
return service as SessionPolicyReader
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Whether win_bash can serve one session, from that session's own policy.
|
|
195
|
+
*
|
|
196
|
+
* The SESSION goes into the request on purpose: the service ranks a session's
|
|
197
|
+
* own `sandbox/mode` log above the deployment default, so a read without it
|
|
198
|
+
* would answer for the deployment and could withdraw pwsh from a session the
|
|
199
|
+
* user restricted. An absent reader, a missing session, a throwing `resolve` and
|
|
200
|
+
* a mode outside the closed vocabulary all answer 'unknown', which the caller
|
|
201
|
+
* treats exactly like 'refuses' — see the module head for why the failure
|
|
202
|
+
* direction is one-way.
|
|
203
|
+
*
|
|
204
|
+
* THE MODE IS NOT THE ONLY THING WIN_BASH REFUSES ON. `tool.ts` also refuses a
|
|
205
|
+
* session with no working directory, and `SessionHeader.cwd` is OPTIONAL in the
|
|
206
|
+
* SDK, so such a session reads as full access here while it cannot run a single
|
|
207
|
+
* command there. That is the same "no shell" state reached without any mode
|
|
208
|
+
* switch, so it is decided here too rather than left to the mode alone.
|
|
209
|
+
* @param read - the policy authority, when one exists.
|
|
210
|
+
* @param session - the session to ask about.
|
|
211
|
+
* @returns what win_bash would do for this session.
|
|
212
|
+
*/
|
|
213
|
+
function servingVerdict(read: SessionPolicyReader | undefined, session: unknown): ServingVerdict {
|
|
214
|
+
if (read === undefined || session === undefined) return 'unknown'
|
|
215
|
+
const cwd = (session as { header?: { cwd?: unknown } }).header?.cwd
|
|
216
|
+
if (typeof cwd !== 'string' || cwd === '') return 'refuses'
|
|
217
|
+
try {
|
|
218
|
+
const mode = read.resolve({ session })?.mode
|
|
219
|
+
if (!SANDBOX_MODES.includes(mode)) return 'unknown'
|
|
220
|
+
return mode === SERVING_MODE ? 'serves' : 'refuses'
|
|
221
|
+
} catch {
|
|
222
|
+
return 'unknown'
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The tool names one agent scope currently sees.
|
|
228
|
+
* @param ctx - the plugin context.
|
|
229
|
+
* @param agent - the scope to read for.
|
|
230
|
+
* @returns the names, or undefined when no usable projection is readable.
|
|
231
|
+
*/
|
|
232
|
+
function visibleNames(ctx: Context, agent: unknown): readonly string[] | undefined {
|
|
233
|
+
const service = readService(ctx, 'tools') as SurfaceReader | undefined
|
|
234
|
+
if (service === null || typeof service !== 'object' || typeof service.schemas !== 'function') return undefined
|
|
235
|
+
try {
|
|
236
|
+
const schemas = service.schemas(agent)
|
|
237
|
+
if (!Array.isArray(schemas)) return undefined
|
|
238
|
+
return schemas
|
|
239
|
+
.map(schema => (schema as { name?: unknown } | undefined)?.name)
|
|
240
|
+
.filter((name): name is string => typeof name === 'string' && name !== '')
|
|
241
|
+
} catch {
|
|
242
|
+
return undefined
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Withdraw {@link HOST_PWSH_TOOL} from every session this plugin can serve,
|
|
248
|
+
* and remember how to give it back.
|
|
249
|
+
*
|
|
250
|
+
* Called from the mount path ONLY after win_bash itself registered, so a
|
|
251
|
+
* deployment that ends up without win_bash keeps the host's shell untouched.
|
|
252
|
+
* @param ctx - context carrying tools and the agent registry.
|
|
253
|
+
* @param options - the names to withdraw, the warning sink, and the policy authority.
|
|
254
|
+
*/
|
|
255
|
+
export function installPwshReplacement(ctx: Context, options: PwshReplacementOptions = {}): void {
|
|
256
|
+
// Checked FIRST, before the roster read, the listeners, and the effect: on a
|
|
257
|
+
// POSIX host this call must leave no trace at all, so an early return above
|
|
258
|
+
// every side effect is the only shape that satisfies it.
|
|
259
|
+
if ((options.platform ?? process.platform) !== 'win32') return
|
|
260
|
+
|
|
261
|
+
const denied = [...(options.denied ?? [HOST_PWSH_TOOL])]
|
|
262
|
+
const read = policyReader(ctx, options.policy)
|
|
263
|
+
/** Agent scope -> the session it owns and the live restriction, if any. */
|
|
264
|
+
const watched = new Map<AgentScopeLike, WatchedScope>()
|
|
265
|
+
let warned = false
|
|
266
|
+
const warn = (message: string): void => {
|
|
267
|
+
if (options.warn !== undefined) {
|
|
268
|
+
try {
|
|
269
|
+
options.warn(message)
|
|
270
|
+
} catch {
|
|
271
|
+
// A broken sink must not become a new failure.
|
|
272
|
+
}
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
ctx.logger?.warn?.(message)
|
|
276
|
+
}
|
|
277
|
+
const warnOnce = (message: string): void => {
|
|
278
|
+
if (warned) return
|
|
279
|
+
warned = true
|
|
280
|
+
warn(message)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Remember one scope, so a later mode switch can be re-evaluated even while
|
|
285
|
+
* no restriction is installed: the session is the key the `sandbox/mode` event
|
|
286
|
+
* has to be matched against.
|
|
287
|
+
* @param scope - the agent scope to watch.
|
|
288
|
+
* @returns the scope's record.
|
|
289
|
+
*/
|
|
290
|
+
const watch = (scope: AgentScopeLike): WatchedScope => {
|
|
291
|
+
const existing = watched.get(scope)
|
|
292
|
+
if (existing !== undefined) return existing
|
|
293
|
+
const record: WatchedScope = { session: scope.session }
|
|
294
|
+
watched.set(scope, record)
|
|
295
|
+
return record
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Lift one scope's restriction, isolating the callback: this restores a
|
|
300
|
+
* shell the session should have back, so a throwing disposer must not stop
|
|
301
|
+
* the ones after it. The scope stays watched, because the session it belongs
|
|
302
|
+
* to may still switch back to a mode win_bash can serve.
|
|
303
|
+
* @param scope - the agent scope to release.
|
|
304
|
+
*/
|
|
305
|
+
const release = (scope: AgentScopeLike): void => {
|
|
306
|
+
const record = watched.get(scope)
|
|
307
|
+
const dispose = record?.dispose
|
|
308
|
+
if (record !== undefined) record.dispose = undefined
|
|
309
|
+
if (typeof dispose !== 'function') return
|
|
310
|
+
try {
|
|
311
|
+
dispose()
|
|
312
|
+
} catch (error) {
|
|
313
|
+
warn('dsh-win-bash: the host pwsh tool could not be restored for one session ('
|
|
314
|
+
+ messageOf(error) + '); pwsh stays hidden there until the next mount.')
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Stop watching one scope and lift whatever it carries: the agent is gone,
|
|
320
|
+
* so nothing about it can change again.
|
|
321
|
+
* @param scope - the agent scope to forget.
|
|
322
|
+
*/
|
|
323
|
+
const forget = (scope: AgentScopeLike): void => {
|
|
324
|
+
release(scope)
|
|
325
|
+
watched.delete(scope)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Bring one scope to the state its session's policy asks for: withdraw pwsh
|
|
330
|
+
* when win_bash can serve that session, release it otherwise.
|
|
331
|
+
*
|
|
332
|
+
* The visible surface is read before restricting because `restrict()` refuses a
|
|
333
|
+
* name the registry does not know as a global tool — and "not registered yet"
|
|
334
|
+
* is an ordinary boot state here, not a defect, which is why this retries off
|
|
335
|
+
* `tools/change` instead of reporting it.
|
|
336
|
+
* @param scope - the scope to bring up to date.
|
|
337
|
+
*/
|
|
338
|
+
const evaluate = (scope: AgentScopeLike): void => {
|
|
339
|
+
if (scope === null || typeof scope !== 'object') return
|
|
340
|
+
const record = watch(scope)
|
|
341
|
+
const verdict = servingVerdict(read, record.session)
|
|
342
|
+
if (verdict === 'unknown') {
|
|
343
|
+
// Not a per-session defect worth repeating: the cause is deployment-wide
|
|
344
|
+
// (no policy service, or one that cannot answer), and the effect is that
|
|
345
|
+
// pwsh stays visible, which an operator may otherwise read as this
|
|
346
|
+
// replacement being broken.
|
|
347
|
+
warnOnce('dsh-win-bash: the sandbox policy of a session could not be resolved, so the host pwsh tool stays'
|
|
348
|
+
+ ' visible beside win_bash there (win_bash refuses every call in a session whose mode is unknown).')
|
|
349
|
+
}
|
|
350
|
+
if (verdict !== 'serves') {
|
|
351
|
+
// win_bash refuses this session, so its pwsh is the only shell it has.
|
|
352
|
+
release(scope)
|
|
353
|
+
return
|
|
354
|
+
}
|
|
355
|
+
if (record.dispose !== undefined) return
|
|
356
|
+
const names = denied.filter(name => (visibleNames(ctx, scope) ?? []).includes(name))
|
|
357
|
+
if (names.length === 0) return
|
|
358
|
+
const restrict = scope.ctx?.tools?.restrict
|
|
359
|
+
if (typeof restrict !== 'function') {
|
|
360
|
+
warnOnce('dsh-win-bash: this deployment exposes no scoped tools view, so the host pwsh tool stays visible beside win_bash.')
|
|
361
|
+
return
|
|
362
|
+
}
|
|
363
|
+
// Reserve the slot BEFORE restricting: the registry emits `tools/change` for
|
|
364
|
+
// a restriction change, and a listener that re-entered here with the slot
|
|
365
|
+
// still empty would restrict the same scope again, forever.
|
|
366
|
+
record.dispose = () => {}
|
|
367
|
+
let dispose: unknown
|
|
368
|
+
try {
|
|
369
|
+
dispose = restrict.call(scope.ctx?.tools, { deny: names })
|
|
370
|
+
} catch (error) {
|
|
371
|
+
record.dispose = undefined
|
|
372
|
+
warnOnce('dsh-win-bash: the host pwsh tool could not be withdrawn from this session ('
|
|
373
|
+
+ messageOf(error) + '); pwsh stays visible beside win_bash.')
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
if (typeof dispose !== 'function') {
|
|
377
|
+
record.dispose = undefined
|
|
378
|
+
warnOnce('dsh-win-bash: the tool registry did not confirm the pwsh restriction, so pwsh stays visible beside win_bash.')
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
record.dispose = dispose as () => void
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Re-evaluate every scope that owns the session an event came from.
|
|
386
|
+
* @param session - the session whose log changed.
|
|
387
|
+
*/
|
|
388
|
+
const reevaluate = (session: unknown): void => {
|
|
389
|
+
for (const [scope, record] of [...watched]) {
|
|
390
|
+
if (record.session !== session) continue
|
|
391
|
+
evaluate(scope)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// The created listener returns undefined explicitly: the event is declared
|
|
396
|
+
// serial, so its listeners are typed as returning undefined | Promise<undefined>
|
|
397
|
+
// and a bare void arrow is not assignable to that.
|
|
398
|
+
ctx.on('agent/created', ({ agent }) => {
|
|
399
|
+
evaluate(agent as AgentScopeLike)
|
|
400
|
+
return undefined
|
|
401
|
+
})
|
|
402
|
+
ctx.on('agent/disposed', ({ agent }) => { forget(agent as AgentScopeLike) })
|
|
403
|
+
// The host's own shell row may mount after this one. A name that is not on
|
|
404
|
+
// the surface yet is caught here rather than guessed at mount time.
|
|
405
|
+
ctx.on('tools/change', () => {
|
|
406
|
+
for (const scope of [...watched.keys()]) evaluate(scope)
|
|
407
|
+
})
|
|
408
|
+
// A permission-preset switch writes sandbox/mode into the session's own log,
|
|
409
|
+
// and that is not dispatched to this plugin's scope, so this listener must be
|
|
410
|
+
// global to see it. It re-evaluates synchronously and installs no timer: the
|
|
411
|
+
// restriction follows the mode as it is switched, in both directions.
|
|
412
|
+
ctx.on('internal/dispatch', (_mode, eventName, args) => {
|
|
413
|
+
if (eventName !== 'session/event') return
|
|
414
|
+
if (!Array.isArray(args)) return
|
|
415
|
+
const [session, event] = args as [unknown, { type?: unknown } | undefined]
|
|
416
|
+
if (event?.type !== 'sandbox/mode') return
|
|
417
|
+
reevaluate(session)
|
|
418
|
+
}, { global: true })
|
|
419
|
+
|
|
420
|
+
// Sessions that were already running when this plugin mounted.
|
|
421
|
+
const registry = readService(ctx, 'agents') as { list?: () => unknown[] } | undefined
|
|
422
|
+
if (registry !== undefined && typeof registry.list === 'function') {
|
|
423
|
+
try {
|
|
424
|
+
for (const agent of registry.list() ?? []) evaluate(agent as AgentScopeLike)
|
|
425
|
+
} catch (error) {
|
|
426
|
+
warnOnce('dsh-win-bash: the session roster could not be read, so the host pwsh tool stays visible in sessions that were already open ('
|
|
427
|
+
+ messageOf(error) + ').')
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
ctx.effect(() => () => {
|
|
432
|
+
// Snapshot first: forget deletes from the map it is iterating.
|
|
433
|
+
for (const scope of [...watched.keys()]) forget(scope)
|
|
434
|
+
}, 'dsh-win-bash: host pwsh replacement')
|
|
435
|
+
}
|