mirror of
https://ghproxy.cflmy.top/https://github.com/deepseek-ai/deepseek-harness.git
synced 2026-12-01 17:49:35 +08:00
Unix PowerShell emits cursor-position requests while PSReadLine starts and redraws prompts. The subprocess PTY is only a transport, so those requests went unanswered. Startup could then accept the dsh> literal echoed from its setup source as a rendered prompt, and later sends were lost or clipped. Feed raw PTY output into a zero-scrollback @xterm/headless state machine and write generated replies through the provider-owned terminal handle. Drain replies before caller input, repeat foreground inspection when terminal activity races the sample, and retain send ownership until parser and reply work quiesce. Coalesce raw chunks behind one active parser write so large Windows output cannot create thousands of queued parse callbacks. Publish pwsh only from backend stdin_read evidence and start one timeoutMs deadline before the complete startup retry loop, so inferred-idle follow-ups cannot reset the bound. Dispose the emulator when the terminal or cleanup fails. Document the fail-loud ConstrainedLanguage path and add focused coverage for split queries, reply ordering, foreground resampling, failure containment, batching, timeout, and disposal.
123 lines
5.1 KiB
TypeScript
123 lines
5.1 KiB
TypeScript
/** Validated configuration for the local PTY backend. */
|
|
|
|
import z from '@deepseek-ai/schemastery'
|
|
import { resolvePwshPath } from '@deepseek-ai/dsh-pwsh-local'
|
|
|
|
/** One supported interactive shell dialect. */
|
|
export type ShellDialect = 'bash' | 'pwsh'
|
|
|
|
/** Public plugin configuration. */
|
|
export interface Config {
|
|
/** Backend registry type (default: `shell`). */
|
|
backendType?: string
|
|
/** Interactive shell dialect (default: `bash`); selects the argv/env/startup defaults. */
|
|
shellDialect?: ShellDialect
|
|
/** Interactive shell executable (default per dialect: `/bin/bash`, or the resolved pwsh). */
|
|
shellPath?: string
|
|
/** Shell arguments (default per dialect: bash `--noprofile --norc -i`, pwsh `-NoLogo -NoProfile`). */
|
|
shellArgs?: string[]
|
|
/** Terminal rows. */
|
|
rows?: number
|
|
/** Terminal columns. */
|
|
cols?: number
|
|
/** Maximum retained logical lines. */
|
|
scrollbackLines?: number
|
|
/** Maximum retained UTF-8 bytes. */
|
|
scrollbackMaxBytes?: number
|
|
/** Maximum bytes returned by one read or settled viewport. */
|
|
maxReadBytes?: number
|
|
/** Readiness polling interval. */
|
|
pollIntervalMs?: number
|
|
/** Delay before Linux exact syscall probes. */
|
|
exactProbeAfterMs?: number
|
|
/** Silence duration that yields `inferred_idle`. */
|
|
idleSilenceMs?: number
|
|
/**
|
|
* Extra wait beyond `idleSilenceMs`, once a prompt marker was seen, for the shell to
|
|
* regain the foreground before `inferred_idle` settles; at least one `pollIntervalMs`.
|
|
*/
|
|
handoffGraceMs?: number
|
|
/** Absolute bound for one send and the complete pwsh startup sequence. */
|
|
timeoutMs?: number
|
|
/** Grace before teardown escalates to `SIGKILL`. */
|
|
disposeGraceMs?: number
|
|
}
|
|
|
|
/** Configuration after Schemastery defaults and dialect resolution. */
|
|
export type ResolvedConfig = Omit<Required<Config>, 'shellDialect' | 'shellPath' | 'shellArgs'> & {
|
|
shellDialect: ShellDialect
|
|
shellPath: string
|
|
shellArgs: string[]
|
|
}
|
|
|
|
/** Bash dialect default executable. */
|
|
export const DEFAULT_BASH_SHELL = '/bin/bash'
|
|
/** Bash dialect default arguments (interactive, profile-free). */
|
|
export const DEFAULT_BASH_ARGS = ['--noprofile', '--norc', '-i']
|
|
/** Pwsh dialect default arguments (interactive host, profile-free). */
|
|
export const DEFAULT_PWSH_ARGS = ['-NoLogo', '-NoProfile']
|
|
|
|
/**
|
|
* Resolve the effective per-dialect shell specification. Defaulting is this
|
|
* explicit step: an unset or empty `shellPath`/`shellArgs` selects the
|
|
* dialect's defaults, while a non-empty explicit value always wins.
|
|
* (Schemastery materializes an absent optional array as `[]`, so emptiness —
|
|
* not just `undefined` — means "dialect default".)
|
|
* @param config - Schemastery-resolved plugin configuration.
|
|
* @returns the fully resolved configuration.
|
|
*/
|
|
export function resolveConfig(config: Config): ResolvedConfig {
|
|
const shellDialect = config.shellDialect ?? 'bash'
|
|
return {
|
|
...(config as Required<Config>),
|
|
shellDialect,
|
|
shellPath: config.shellPath !== undefined && config.shellPath.length > 0
|
|
? config.shellPath
|
|
: (shellDialect === 'pwsh' ? resolvePwshPath() : DEFAULT_BASH_SHELL),
|
|
shellArgs: config.shellArgs !== undefined && config.shellArgs.length > 0
|
|
? config.shellArgs
|
|
: (shellDialect === 'pwsh' ? DEFAULT_PWSH_ARGS : DEFAULT_BASH_ARGS),
|
|
}
|
|
}
|
|
|
|
/** Schemastery config exposed by the plugin. */
|
|
export const Config: z<Config> = z.object({
|
|
backendType: z.string().default('shell'),
|
|
shellDialect: z.union(['bash', 'pwsh'] as const).default('bash'),
|
|
shellPath: z.string().required(false),
|
|
shellArgs: z.array(z.string()).required(false),
|
|
rows: z.number().default(40),
|
|
cols: z.number().default(160),
|
|
scrollbackLines: z.number().default(10_000),
|
|
scrollbackMaxBytes: z.number().default(4 * 1024 * 1024),
|
|
maxReadBytes: z.number().default(256 * 1024),
|
|
pollIntervalMs: z.number().default(50),
|
|
exactProbeAfterMs: z.number().default(150),
|
|
idleSilenceMs: z.number().default(3_000),
|
|
handoffGraceMs: z.number().default(500),
|
|
timeoutMs: z.number().default(30_000),
|
|
disposeGraceMs: z.number().default(3_000),
|
|
})
|
|
|
|
/**
|
|
* Assert every effective numeric config field is a positive safe integer and bounds compose.
|
|
* @param config - Schemastery-resolved plugin configuration.
|
|
* @returns Narrows the input to the fully resolved configuration.
|
|
*/
|
|
export function validateConfig(config: Config): asserts config is ResolvedConfig {
|
|
const resolved = config as ResolvedConfig
|
|
if (resolved.backendType.length === 0) throw new Error('terminal-bash: backendType must be non-empty')
|
|
if (resolved.shellPath.length === 0) throw new Error('terminal-bash: shellPath must be non-empty')
|
|
for (const [name, value] of Object.entries(resolved)) {
|
|
if (typeof value === 'number' && (!Number.isSafeInteger(value) || value <= 0)) {
|
|
throw new Error(`terminal-bash: ${name} must be a positive safe integer`)
|
|
}
|
|
}
|
|
if (resolved.maxReadBytes > resolved.scrollbackMaxBytes) {
|
|
throw new Error('terminal-bash: maxReadBytes must not exceed scrollbackMaxBytes')
|
|
}
|
|
if (resolved.handoffGraceMs < resolved.pollIntervalMs) {
|
|
throw new Error('terminal-bash: handoffGraceMs must be at least pollIntervalMs so one readiness poll runs inside the grace window')
|
|
}
|
|
}
|