@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
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote execution environment forwarding for `win_bash` (DSH 0.2.3-alpha.4+).
|
|
3
|
+
*
|
|
4
|
+
* When a call carries `exec.environment`, `win_bash` forwards the command to
|
|
5
|
+
* the host's optional `environments` registry (`ctx.environments`) as a
|
|
6
|
+
* self-contained daemon function invoking `/usr/bin/env bash -c <command>`.
|
|
7
|
+
* Unlike the local Windows MSYS2 persistent shell (which runs only under
|
|
8
|
+
* `danger-full-access` due to `NtCreateDirectoryObject 0xC0000022`), remote
|
|
9
|
+
* POSIX environments enforce the resolved session sandbox policy (`read-only`,
|
|
10
|
+
* `workspace-write`, or `danger-full-access`) through `remoteSandboxPolicy`.
|
|
11
|
+
* Each remote call runs an ephemeral `bash -c` invocation (matching upstream
|
|
12
|
+
* `@deepseek-ai/dsh-tool-bash/environment`).
|
|
13
|
+
*
|
|
14
|
+
* SHAPE, NOT IMPORT: consumes `ctx.environments` structurally (the same leaf
|
|
15
|
+
* discipline as `./background.ts`), requiring no runtime dependency on
|
|
16
|
+
* `@deepseek-ai/dsh-environment` or `zod`.
|
|
17
|
+
* @module @linxin666/dsh-win-bash/environment
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
21
|
+
import { remoteSandboxPolicy, type SandboxExecutionPolicy, type SandboxMode } from '@deepseek-ai/dsh-sandbox'
|
|
22
|
+
import { EnvironmentUnavailableError, ToolArgsError, type ToolEnvironmentTarget } from '@deepseek-ai/dsh-tools'
|
|
23
|
+
import { BACKGROUND_JOB_KIND, type JobHandleLike, type JobHooksLike, type JobOutcomeLike, type JobRegistryLike } from './background.ts'
|
|
24
|
+
import { TIMEOUT_EXIT_CODE } from './shell.ts'
|
|
25
|
+
|
|
26
|
+
/** Environment error code emitted when cancellation confirmed every remote process stopped. */
|
|
27
|
+
const ENVIRONMENT_RUN_STOPPED = 'ENVIRONMENT_RUN_STOPPED'
|
|
28
|
+
/** Environment error code emitted when the connection dropped before the daemon answered. */
|
|
29
|
+
const ENVIRONMENT_OUTCOME_UNKNOWN = 'ENVIRONMENT_OUTCOME_UNKNOWN'
|
|
30
|
+
/** Exit code used when a remote process ended by signal without a numeric exit code. */
|
|
31
|
+
const SIGNAL_EXIT_CODE = 128
|
|
32
|
+
|
|
33
|
+
const SANDBOX_MODES: ReadonlySet<string> = new Set(['read-only', 'workspace-write', 'danger-full-access'])
|
|
34
|
+
|
|
35
|
+
/** Request object structured-cloned to the remote daemon function. */
|
|
36
|
+
export interface RemoteBashRequest {
|
|
37
|
+
readonly command: string
|
|
38
|
+
readonly cwd: string | undefined
|
|
39
|
+
readonly workdir: string | undefined
|
|
40
|
+
readonly maxStdoutBytes: number
|
|
41
|
+
readonly maxStderrBytes: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Captured stream tail from the remote daemon. */
|
|
45
|
+
export interface RemoteStreamCapture {
|
|
46
|
+
readonly text: string
|
|
47
|
+
readonly truncated: boolean
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Optional sandbox report returned by the remote daemon. */
|
|
51
|
+
export interface RemoteSandboxReport {
|
|
52
|
+
readonly mode: SandboxMode
|
|
53
|
+
readonly enforcement?: 'full' | 'none'
|
|
54
|
+
readonly denied: boolean
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Discriminated outcome returned by {@link remoteEnvironmentBash}. */
|
|
58
|
+
export type RemoteBashOutcome =
|
|
59
|
+
| {
|
|
60
|
+
readonly kind: 'exited'
|
|
61
|
+
readonly cwd: string
|
|
62
|
+
readonly exitCode: number | null
|
|
63
|
+
readonly signal: string | null
|
|
64
|
+
readonly stdout: RemoteStreamCapture
|
|
65
|
+
readonly stderr: RemoteStreamCapture
|
|
66
|
+
readonly sandbox?: RemoteSandboxReport
|
|
67
|
+
}
|
|
68
|
+
| { readonly kind: 'workdir'; readonly cwd: string }
|
|
69
|
+
| { readonly kind: 'platform'; readonly platform: string }
|
|
70
|
+
|
|
71
|
+
/** Minimal daemon-side `env` surface provided by `@deepseek-ai/dsh-environment`. */
|
|
72
|
+
export interface RemoteEnvironmentContextLike {
|
|
73
|
+
readonly platform: string
|
|
74
|
+
readonly cwd: string
|
|
75
|
+
readonly limits: { readonly maxMessageBytes: number }
|
|
76
|
+
readonly path: { resolve(base: string, target: string): string }
|
|
77
|
+
readonly fs: { stat(path: string): Promise<{ isDirectory(): boolean }> }
|
|
78
|
+
exec(
|
|
79
|
+
argv: readonly string[],
|
|
80
|
+
options: { readonly cwd: string },
|
|
81
|
+
): Promise<{
|
|
82
|
+
readonly exitCode: number | null
|
|
83
|
+
readonly signal: string | null
|
|
84
|
+
readonly stdout: string
|
|
85
|
+
readonly stderr: string
|
|
86
|
+
readonly stdoutTruncated: boolean
|
|
87
|
+
readonly stderrTruncated: boolean
|
|
88
|
+
readonly sandbox?: RemoteSandboxReport
|
|
89
|
+
}>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Structural result parser compatible with `EnvironmentRegistry.define`'s `safeParse` contract. */
|
|
93
|
+
export interface ResultValidatorLike<T> {
|
|
94
|
+
safeParse(value: unknown): { readonly success: true; readonly data: T } | { readonly success: false; readonly error: { readonly message: string } }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Runner returned by `EnvironmentRegistry.define`. */
|
|
98
|
+
export type EnvironmentRunnerLike<A, T> = (
|
|
99
|
+
handle: unknown,
|
|
100
|
+
args: A,
|
|
101
|
+
options: { readonly signal: AbortSignal; readonly awaitStop?: boolean; readonly sandbox?: unknown },
|
|
102
|
+
) => Promise<T>
|
|
103
|
+
|
|
104
|
+
/** Structural face of the optional `environments` host service (`EnvironmentRegistry`). */
|
|
105
|
+
export interface EnvironmentRegistryLike {
|
|
106
|
+
handle(value: string): unknown
|
|
107
|
+
capabilities(handle: unknown, options: { readonly signal: AbortSignal }): Promise<{ readonly cwd: string }>
|
|
108
|
+
define<A, T>(
|
|
109
|
+
fn: (request: A, env: RemoteEnvironmentContextLike) => Promise<T>,
|
|
110
|
+
options: { readonly result: ResultValidatorLike<T> },
|
|
111
|
+
): EnvironmentRunnerLike<A, T>
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Foreground return payload matching `win_bash`'s canonical output schema. */
|
|
115
|
+
export interface RemoteForegroundResult {
|
|
116
|
+
readonly stdout: string
|
|
117
|
+
readonly stderr: string
|
|
118
|
+
readonly exitCode: number
|
|
119
|
+
readonly stdoutTruncated: boolean
|
|
120
|
+
readonly stderrTruncated: boolean
|
|
121
|
+
readonly stdoutLostHead: boolean
|
|
122
|
+
readonly stderrLostHead: boolean
|
|
123
|
+
readonly frameOverflow: boolean
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
127
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function parseStream(value: unknown, label: string): RemoteStreamCapture | string {
|
|
131
|
+
if (!isRecord(value)) return `${label} must be an object`
|
|
132
|
+
if (typeof value.text !== 'string') return `${label}.text must be a string`
|
|
133
|
+
if (typeof value.truncated !== 'boolean') return `${label}.truncated must be a boolean`
|
|
134
|
+
return { text: value.text, truncated: value.truncated }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function parseSandbox(value: unknown): RemoteSandboxReport | undefined | string {
|
|
138
|
+
if (value === undefined) return undefined
|
|
139
|
+
if (!isRecord(value)) return 'sandbox must be an object'
|
|
140
|
+
if (typeof value.mode !== 'string' || !SANDBOX_MODES.has(value.mode)) return 'sandbox.mode is invalid'
|
|
141
|
+
if (typeof value.denied !== 'boolean') return 'sandbox.denied must be a boolean'
|
|
142
|
+
if (value.enforcement !== undefined && value.enforcement !== 'full' && value.enforcement !== 'none') {
|
|
143
|
+
return 'sandbox.enforcement is invalid'
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
mode: value.mode as SandboxMode,
|
|
147
|
+
denied: value.denied,
|
|
148
|
+
...(value.enforcement === undefined ? {} : { enforcement: value.enforcement }),
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Validate a raw structured-cloned reply returned from a remote environment daemon. */
|
|
153
|
+
export const remoteBashOutcomeValidator: ResultValidatorLike<RemoteBashOutcome> = {
|
|
154
|
+
safeParse(value: unknown) {
|
|
155
|
+
if (!isRecord(value) || typeof value.kind !== 'string') {
|
|
156
|
+
return { success: false, error: { message: 'outcome must be a tagged object' } }
|
|
157
|
+
}
|
|
158
|
+
if (value.kind === 'workdir') {
|
|
159
|
+
if (typeof value.cwd !== 'string') return { success: false, error: { message: 'workdir.cwd must be a string' } }
|
|
160
|
+
return { success: true, data: { kind: 'workdir', cwd: value.cwd } }
|
|
161
|
+
}
|
|
162
|
+
if (value.kind === 'platform') {
|
|
163
|
+
if (typeof value.platform !== 'string') return { success: false, error: { message: 'platform.platform must be a string' } }
|
|
164
|
+
return { success: true, data: { kind: 'platform', platform: value.platform } }
|
|
165
|
+
}
|
|
166
|
+
if (value.kind === 'exited') {
|
|
167
|
+
if (typeof value.cwd !== 'string') return { success: false, error: { message: 'exited.cwd must be a string' } }
|
|
168
|
+
if (value.exitCode !== null && (typeof value.exitCode !== 'number' || !Number.isInteger(value.exitCode))) {
|
|
169
|
+
return { success: false, error: { message: 'exited.exitCode must be an integer or null' } }
|
|
170
|
+
}
|
|
171
|
+
if (value.signal !== null && typeof value.signal !== 'string') {
|
|
172
|
+
return { success: false, error: { message: 'exited.signal must be a string or null' } }
|
|
173
|
+
}
|
|
174
|
+
const stdout = parseStream(value.stdout, 'stdout')
|
|
175
|
+
if (typeof stdout === 'string') return { success: false, error: { message: stdout } }
|
|
176
|
+
const stderr = parseStream(value.stderr, 'stderr')
|
|
177
|
+
if (typeof stderr === 'string') return { success: false, error: { message: stderr } }
|
|
178
|
+
const sandbox = parseSandbox(value.sandbox)
|
|
179
|
+
if (typeof sandbox === 'string') return { success: false, error: { message: sandbox } }
|
|
180
|
+
return {
|
|
181
|
+
success: true,
|
|
182
|
+
data: {
|
|
183
|
+
kind: 'exited',
|
|
184
|
+
cwd: value.cwd,
|
|
185
|
+
exitCode: value.exitCode,
|
|
186
|
+
signal: value.signal,
|
|
187
|
+
stdout,
|
|
188
|
+
stderr,
|
|
189
|
+
...(sandbox === undefined ? {} : { sandbox }),
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { success: false, error: { message: `unknown outcome kind: ${value.kind}` } }
|
|
194
|
+
},
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Daemon-side command function sent via `fn.toString()`.
|
|
199
|
+
* Must remain strictly self-contained: no module-scope closures or helper imports.
|
|
200
|
+
*/
|
|
201
|
+
export async function remoteEnvironmentBash(
|
|
202
|
+
request: RemoteBashRequest,
|
|
203
|
+
env: RemoteEnvironmentContextLike,
|
|
204
|
+
): Promise<RemoteBashOutcome> {
|
|
205
|
+
if (env.platform === 'win32') return { kind: 'platform', platform: env.platform }
|
|
206
|
+
const base = request.cwd === undefined ? env.cwd : request.cwd
|
|
207
|
+
const cwd = request.workdir === undefined || request.workdir === ''
|
|
208
|
+
? base
|
|
209
|
+
: env.path.resolve(base, request.workdir)
|
|
210
|
+
let directory = false
|
|
211
|
+
try {
|
|
212
|
+
directory = (await env.fs.stat(cwd)).isDirectory()
|
|
213
|
+
} catch {
|
|
214
|
+
// Missing or unreadable directory is reported cleanly below.
|
|
215
|
+
}
|
|
216
|
+
if (!directory) return { kind: 'workdir', cwd }
|
|
217
|
+
const maxPerStream = Math.max(0, Math.floor((env.limits.maxMessageBytes - 4096 - 2 * cwd.length) / 4))
|
|
218
|
+
const stdoutBudget = Math.max(0, Math.min(request.maxStdoutBytes, maxPerStream))
|
|
219
|
+
const stderrBudget = Math.max(0, Math.min(request.maxStderrBytes, maxPerStream))
|
|
220
|
+
const tail = (text: string, discarded: boolean, budget: number): RemoteStreamCapture => {
|
|
221
|
+
const bytes = Buffer.from(text, 'utf8')
|
|
222
|
+
if (bytes.length <= budget) return { text, truncated: discarded }
|
|
223
|
+
let start = bytes.length - budget
|
|
224
|
+
while (start < bytes.length && ((bytes[start] as number) & 0xc0) === 0x80) start += 1
|
|
225
|
+
return { text: bytes.subarray(start).toString('utf8'), truncated: true }
|
|
226
|
+
}
|
|
227
|
+
const result = await env.exec(['/usr/bin/env', 'bash', '-c', request.command], { cwd })
|
|
228
|
+
const out: {
|
|
229
|
+
kind: 'exited'
|
|
230
|
+
cwd: string
|
|
231
|
+
exitCode: number | null
|
|
232
|
+
signal: string | null
|
|
233
|
+
stdout: RemoteStreamCapture
|
|
234
|
+
stderr: RemoteStreamCapture
|
|
235
|
+
sandbox?: RemoteSandboxReport
|
|
236
|
+
} = {
|
|
237
|
+
kind: 'exited',
|
|
238
|
+
cwd,
|
|
239
|
+
exitCode: result.exitCode,
|
|
240
|
+
signal: result.signal,
|
|
241
|
+
stdout: tail(result.stdout, result.stdoutTruncated, stdoutBudget),
|
|
242
|
+
stderr: tail(result.stderr, result.stderrTruncated, stderrBudget),
|
|
243
|
+
}
|
|
244
|
+
if (result.sandbox !== undefined) out.sandbox = result.sandbox
|
|
245
|
+
return out
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function readEnvironmentRegistry(ctx: Context): EnvironmentRegistryLike | undefined {
|
|
249
|
+
let candidate: unknown
|
|
250
|
+
try {
|
|
251
|
+
candidate = (ctx as unknown as { get(key: string): unknown }).get('environments')
|
|
252
|
+
} catch {
|
|
253
|
+
return undefined
|
|
254
|
+
}
|
|
255
|
+
if (typeof candidate !== 'object' || candidate === null) return undefined
|
|
256
|
+
const reg = candidate as Partial<EnvironmentRegistryLike>
|
|
257
|
+
if (typeof reg.handle !== 'function' || typeof reg.capabilities !== 'function' || typeof reg.define !== 'function') {
|
|
258
|
+
return undefined
|
|
259
|
+
}
|
|
260
|
+
return reg as EnvironmentRegistryLike
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function errorCodeOf(error: unknown): string | undefined {
|
|
264
|
+
if (typeof error !== 'object' || error === null) return undefined
|
|
265
|
+
const code = (error as { code?: unknown }).code
|
|
266
|
+
return typeof code === 'string' ? code : undefined
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function errorMessageOf(error: unknown): string {
|
|
270
|
+
return error instanceof Error ? error.message : String(error)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function refusalError(outcome: Exclude<RemoteBashOutcome, { kind: 'exited' }>, environmentId: string): Error {
|
|
274
|
+
if (outcome.kind === 'workdir') {
|
|
275
|
+
return new ToolArgsError([`workdir "${outcome.cwd}" is not a directory in environment "${environmentId}"`])
|
|
276
|
+
}
|
|
277
|
+
const err = new Error(
|
|
278
|
+
`REMOTE_SHELL_UNSUPPORTED: win_bash forwards remote calls only to POSIX environments; environment "${environmentId}" runs on ${outcome.platform}.`,
|
|
279
|
+
)
|
|
280
|
+
;(err as { code?: string }).code = 'REMOTE_SHELL_UNSUPPORTED'
|
|
281
|
+
return err
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function formatExitedStderr(stderr: string, exitCode: number | null, signal: string | null): string {
|
|
285
|
+
if (exitCode !== null || signal === null) return stderr
|
|
286
|
+
const note = `[terminated by signal ${signal}]`
|
|
287
|
+
return stderr === '' ? note : `${stderr}\n${note}`
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Remote runner bound to one plugin context's optional `environments` registry. */
|
|
291
|
+
export interface RemoteWinBashRunner {
|
|
292
|
+
runForeground(input: {
|
|
293
|
+
readonly target: ToolEnvironmentTarget
|
|
294
|
+
readonly signal: AbortSignal
|
|
295
|
+
readonly command: string
|
|
296
|
+
readonly workdir: string | undefined
|
|
297
|
+
readonly timeoutMs: number
|
|
298
|
+
readonly maxStdoutBytes: number
|
|
299
|
+
readonly maxStderrBytes: number
|
|
300
|
+
readonly policy: SandboxExecutionPolicy
|
|
301
|
+
}): Promise<RemoteForegroundResult>
|
|
302
|
+
|
|
303
|
+
startBackground(input: {
|
|
304
|
+
readonly jobs: JobRegistryLike
|
|
305
|
+
readonly owner: string | undefined
|
|
306
|
+
readonly target: ToolEnvironmentTarget
|
|
307
|
+
readonly command: string
|
|
308
|
+
readonly workdir: string | undefined
|
|
309
|
+
readonly maxStdoutBytes: number
|
|
310
|
+
readonly maxStderrBytes: number
|
|
311
|
+
readonly policy: SandboxExecutionPolicy
|
|
312
|
+
}): string
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Create the optional `ctx.environments` forwarding runner for `win_bash`.
|
|
317
|
+
* Tracks registry load/unload through `ctx.inject(['environments'], ...)` when
|
|
318
|
+
* available, and falls back to lazy `ctx.get('environments')` lookup keyed per
|
|
319
|
+
* registry instance so synchronous host stubs work without waiting a microtask.
|
|
320
|
+
*/
|
|
321
|
+
export function createRemoteWinBashRunner(ctx: Context): RemoteWinBashRunner {
|
|
322
|
+
const runners = new WeakMap<EnvironmentRegistryLike, EnvironmentRunnerLike<RemoteBashRequest, RemoteBashOutcome>>()
|
|
323
|
+
let activeRegistry: EnvironmentRegistryLike | undefined
|
|
324
|
+
|
|
325
|
+
const defineFor = (registry: EnvironmentRegistryLike): {
|
|
326
|
+
registry: EnvironmentRegistryLike
|
|
327
|
+
run: EnvironmentRunnerLike<RemoteBashRequest, RemoteBashOutcome>
|
|
328
|
+
} => {
|
|
329
|
+
let run = runners.get(registry)
|
|
330
|
+
if (run === undefined) {
|
|
331
|
+
run = registry.define(remoteEnvironmentBash, { result: remoteBashOutcomeValidator })
|
|
332
|
+
runners.set(registry, run)
|
|
333
|
+
}
|
|
334
|
+
return { registry, run }
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const injectFn = (ctx as unknown as { inject?: (deps: readonly string[], cb: (scope: Context) => void) => unknown }).inject
|
|
338
|
+
if (typeof injectFn === 'function') {
|
|
339
|
+
injectFn.call(ctx, ['environments'], (scoped) => {
|
|
340
|
+
const reg = readEnvironmentRegistry(scoped)
|
|
341
|
+
if (reg === undefined) return
|
|
342
|
+
defineFor(reg)
|
|
343
|
+
activeRegistry = reg
|
|
344
|
+
scoped.effect(() => () => {
|
|
345
|
+
if (activeRegistry === reg) activeRegistry = undefined
|
|
346
|
+
}, 'dsh-win-bash: remote environment runner')
|
|
347
|
+
})
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const resolve = (target: ToolEnvironmentTarget) => {
|
|
351
|
+
const registry = activeRegistry ?? readEnvironmentRegistry(ctx)
|
|
352
|
+
if (registry === undefined) throw new EnvironmentUnavailableError(target.id)
|
|
353
|
+
return defineFor(registry)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const invoke = async (
|
|
357
|
+
target: ToolEnvironmentTarget,
|
|
358
|
+
signal: AbortSignal,
|
|
359
|
+
request: RemoteBashRequest,
|
|
360
|
+
policy: SandboxExecutionPolicy,
|
|
361
|
+
awaitStop: boolean,
|
|
362
|
+
): Promise<RemoteBashOutcome> => {
|
|
363
|
+
const { registry, run } = resolve(target)
|
|
364
|
+
const handle = registry.handle(target.id)
|
|
365
|
+
const sandbox = await remoteSandboxPolicy(
|
|
366
|
+
policy,
|
|
367
|
+
target.cwd,
|
|
368
|
+
async () => (await registry.capabilities(handle, { signal })).cwd,
|
|
369
|
+
)
|
|
370
|
+
return run(handle, request, {
|
|
371
|
+
signal,
|
|
372
|
+
awaitStop,
|
|
373
|
+
...(sandbox === undefined ? {} : { sandbox }),
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return {
|
|
378
|
+
async runForeground(input): Promise<RemoteForegroundResult> {
|
|
379
|
+
const timeout = new AbortController()
|
|
380
|
+
const timer = setTimeout(() => {
|
|
381
|
+
timeout.abort(new Error(`timed out after ${input.timeoutMs}ms`))
|
|
382
|
+
}, input.timeoutMs)
|
|
383
|
+
let outcome: RemoteBashOutcome
|
|
384
|
+
try {
|
|
385
|
+
outcome = await invoke(
|
|
386
|
+
input.target,
|
|
387
|
+
AbortSignal.any([input.signal, timeout.signal]),
|
|
388
|
+
{
|
|
389
|
+
command: input.command,
|
|
390
|
+
cwd: input.target.cwd,
|
|
391
|
+
workdir: input.workdir,
|
|
392
|
+
maxStdoutBytes: input.maxStdoutBytes,
|
|
393
|
+
maxStderrBytes: input.maxStderrBytes,
|
|
394
|
+
},
|
|
395
|
+
input.policy,
|
|
396
|
+
false,
|
|
397
|
+
)
|
|
398
|
+
} catch (error) {
|
|
399
|
+
if (input.signal.aborted || !timeout.signal.aborted) throw error
|
|
400
|
+
return {
|
|
401
|
+
stdout: '',
|
|
402
|
+
stderr: `[timed out after ${input.timeoutMs}ms in remote environment "${input.target.id}"]`,
|
|
403
|
+
exitCode: TIMEOUT_EXIT_CODE,
|
|
404
|
+
stdoutTruncated: true,
|
|
405
|
+
stderrTruncated: false,
|
|
406
|
+
stdoutLostHead: true,
|
|
407
|
+
stderrLostHead: false,
|
|
408
|
+
frameOverflow: false,
|
|
409
|
+
}
|
|
410
|
+
} finally {
|
|
411
|
+
clearTimeout(timer)
|
|
412
|
+
}
|
|
413
|
+
if (outcome.kind !== 'exited') throw refusalError(outcome, input.target.id)
|
|
414
|
+
const exitCode = outcome.exitCode ?? SIGNAL_EXIT_CODE
|
|
415
|
+
return {
|
|
416
|
+
stdout: outcome.stdout.text,
|
|
417
|
+
stderr: formatExitedStderr(outcome.stderr.text, outcome.exitCode, outcome.signal),
|
|
418
|
+
exitCode,
|
|
419
|
+
stdoutTruncated: outcome.stdout.truncated,
|
|
420
|
+
stderrTruncated: outcome.stderr.truncated,
|
|
421
|
+
stdoutLostHead: outcome.stdout.truncated,
|
|
422
|
+
stderrLostHead: outcome.stderr.truncated,
|
|
423
|
+
frameOverflow: false,
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
|
|
427
|
+
startBackground(input): string {
|
|
428
|
+
return input.jobs.start({
|
|
429
|
+
kind: BACKGROUND_JOB_KIND,
|
|
430
|
+
label: input.command,
|
|
431
|
+
...(input.owner === undefined ? {} : { owner: input.owner }),
|
|
432
|
+
run: (job: JobHandleLike): JobHooksLike => {
|
|
433
|
+
// Resolve synchronously inside start() so an absent registry leaves no orphan job behind.
|
|
434
|
+
const { registry, run } = resolve(input.target)
|
|
435
|
+
const controller = new AbortController()
|
|
436
|
+
const done: Promise<JobOutcomeLike> = (async () => {
|
|
437
|
+
let outcome: RemoteBashOutcome
|
|
438
|
+
try {
|
|
439
|
+
const handle = registry.handle(input.target.id)
|
|
440
|
+
const sandbox = await remoteSandboxPolicy(
|
|
441
|
+
input.policy,
|
|
442
|
+
input.target.cwd,
|
|
443
|
+
async () => (await registry.capabilities(handle, { signal: controller.signal })).cwd,
|
|
444
|
+
)
|
|
445
|
+
outcome = await run(
|
|
446
|
+
handle,
|
|
447
|
+
{
|
|
448
|
+
command: input.command,
|
|
449
|
+
cwd: input.target.cwd,
|
|
450
|
+
workdir: input.workdir,
|
|
451
|
+
maxStdoutBytes: input.maxStdoutBytes,
|
|
452
|
+
maxStderrBytes: input.maxStderrBytes,
|
|
453
|
+
},
|
|
454
|
+
{
|
|
455
|
+
signal: controller.signal,
|
|
456
|
+
awaitStop: true,
|
|
457
|
+
...(sandbox === undefined ? {} : { sandbox }),
|
|
458
|
+
},
|
|
459
|
+
)
|
|
460
|
+
} catch (error) {
|
|
461
|
+
const code = errorCodeOf(error)
|
|
462
|
+
if (code === ENVIRONMENT_RUN_STOPPED || (controller.signal.aborted && error === controller.signal.reason)) {
|
|
463
|
+
return { status: 'killed', detail: `stopped in environment "${input.target.id}"` }
|
|
464
|
+
}
|
|
465
|
+
const message = errorMessageOf(error)
|
|
466
|
+
if (code === ENVIRONMENT_OUTCOME_UNKNOWN) {
|
|
467
|
+
const when = controller.signal.aborted
|
|
468
|
+
? 'before it confirmed that the command stopped'
|
|
469
|
+
: 'before the command finished'
|
|
470
|
+
return {
|
|
471
|
+
status: 'failed',
|
|
472
|
+
detail: `the connection to environment "${input.target.id}" ended ${when}, so whether the command still runs there is unknown: ${message}`,
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return { status: 'failed', detail: message }
|
|
476
|
+
}
|
|
477
|
+
if (outcome.kind !== 'exited') {
|
|
478
|
+
return { status: 'failed', detail: refusalError(outcome, input.target.id).message }
|
|
479
|
+
}
|
|
480
|
+
if (outcome.stdout.text !== '' || outcome.stdout.truncated) {
|
|
481
|
+
job.append(outcome.stdout.text, {
|
|
482
|
+
channel: 'stdout',
|
|
483
|
+
...(outcome.stdout.truncated ? { gapBefore: true as const } : {}),
|
|
484
|
+
})
|
|
485
|
+
}
|
|
486
|
+
if (outcome.stderr.text !== '' || outcome.stderr.truncated) {
|
|
487
|
+
job.append(outcome.stderr.text, {
|
|
488
|
+
channel: 'stderr',
|
|
489
|
+
...(outcome.stderr.truncated ? { gapBefore: true as const } : {}),
|
|
490
|
+
})
|
|
491
|
+
}
|
|
492
|
+
if (outcome.exitCode === null) {
|
|
493
|
+
return {
|
|
494
|
+
status: 'killed',
|
|
495
|
+
detail: outcome.signal === null
|
|
496
|
+
? `stopped in environment "${input.target.id}"`
|
|
497
|
+
: `terminated by signal ${outcome.signal}`,
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return outcome.exitCode === 0
|
|
501
|
+
? { status: 'completed' }
|
|
502
|
+
: { status: 'completed', detail: `exit code ${outcome.exitCode}` }
|
|
503
|
+
})()
|
|
504
|
+
return {
|
|
505
|
+
cancel: () => {
|
|
506
|
+
if (!controller.signal.aborted) {
|
|
507
|
+
controller.abort(new Error('the background job was stopped'))
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
done,
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
})
|
|
514
|
+
},
|
|
515
|
+
}
|
|
516
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -91,6 +91,17 @@ export {
|
|
|
91
91
|
} from './pwsh-replacement.ts'
|
|
92
92
|
export { WIN_BASH_TOOL, renderResult, readJobRegistry, stripTerminalEscapes, truncationNotice } from './tool.ts'
|
|
93
93
|
export type { RenderFacts } from './tool.ts'
|
|
94
|
+
export {
|
|
95
|
+
createRemoteWinBashRunner,
|
|
96
|
+
remoteBashOutcomeValidator,
|
|
97
|
+
remoteEnvironmentBash,
|
|
98
|
+
type EnvironmentRegistryLike,
|
|
99
|
+
type RemoteBashOutcome,
|
|
100
|
+
type RemoteBashRequest,
|
|
101
|
+
type RemoteEnvironmentContextLike,
|
|
102
|
+
type RemoteForegroundResult,
|
|
103
|
+
type RemoteWinBashRunner,
|
|
104
|
+
} from './environment.ts'
|
|
94
105
|
export {
|
|
95
106
|
SHELL_TEMP_PREFIX,
|
|
96
107
|
ShellRegistry,
|