/** * The dsh transport pi-dsh drives: one `dsh --profile
--json` process per
* run, its newline-delimited events consumed as they arrive.
*
* A run is a bounded packet rather than a transcript: dsh keeps its own session
* history, so a follow-up resumes that session instead of re-sending context.
*/
import { spawn } from "node:child_process"
import type { DshCommand } from "./resolve.ts"
/** One event from the dsh run stream. */
export interface DshEvent {
type: string
[key: string]: unknown
}
/** Token accounting dsh reports per step, in its own event vocabulary. */
export interface DshUsage {
input: number
output: number
cacheRead: number
totalTokens: number
}
/** dsh's per-step usage fields, as they appear on a `step_end` event. */
interface DshStepUsage {
inputTokens?: number
outputTokens?: number
cacheReadTokens?: number
totalTokens?: number
}
/** What one run produced. */
export interface RunResult {
/** Session the answer came from; pass it to resume. */
sessionId: string | undefined
/** The answer text, empty when the run failed before answering. */
text: string
/** Steps the run took. */
steps: number
/** Token usage summed over steps. */
usage: DshUsage
/** Why the run ended, from dsh's own reason vocabulary. */
outcome: string
/** Wall-clock duration in milliseconds. */
durationMs: number
/** dsh's diagnostics, kept for the failure path. */
stderr: string
}
/** Options for one run. */
export interface RunOptions {
/** dsh profile to boot. */
profile: string
/** Resolved dsh invocation; defaults to `dsh` on PATH. */
dshCommand?: DshCommand
/** Test seam for the child process; defaults to node:child_process spawn. */
spawnFn?: typeof spawn
/** Working directory dsh runs in. */
cwd: string
/** The task text. */
task: string
/** Session to resume; omit to start a new one. */
sessionId?: string
/** Wall-clock limit. */
timeoutMs: number
/** Called for every streamed event. */
onEvent?: (event: DshEvent) => void
/** Cancels the run. */
signal?: AbortSignal
/** Launcher overlays passed as `--patch`, each overriding profile rows for this run. */
patchPaths?: string[]
/** The child's environment; defaults to a scrubbed allowlist of ours. */
env?: NodeJS.ProcessEnv
}
/**
* Variables a dsh run needs whatever the configuration says.
*
* The child is a full agent with a shell and network access, so handing it
* everything the user's shell holds would hand it every credential in the
* session — including the provider keys this run needs, which are added back
* explicitly by {@link adviserEnv} and nothing else.
*/
const ENV_NAMES = [
"PATH", "HOME", "USERPROFILE", "SHELL", "TMPDIR", "TMP", "TEMP",
"LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE", "TZ", "TERM",
"NODE_EXTRA_CA_CERTS", "SSL_CERT_FILE", "SSL_CERT_DIR",
]
/** Variable prefixes kept whole, so dsh's and pi-dsh's own settings survive. */
const ENV_PREFIXES = ["DSH_", "PI_DSH_", "npm_config_"]
/**
* Build the environment a dsh run is allowed to see.
*
* Allowlist rather than denylist: a new secret in the user's shell is excluded by
* default instead of leaking until someone remembers to add it here. Provider keys
* are the deliberate exception, passed as `extraKeys`, because dsh reads them from
* the environment by name.
*
* @param extraKeys - variable names the run genuinely needs, such as provider keys
* @param base - the environment to filter; defaults to this process's
* @returns the filtered environment
*/
export function adviserEnv(extraKeys: readonly string[] = [], base: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
const wanted = new Set([...ENV_NAMES, ...extraKeys])
const env: NodeJS.ProcessEnv = {}
for (const [key, value] of Object.entries(base)) {
if (value === undefined) continue
if (wanted.has(key) || ENV_PREFIXES.some((prefix) => key.startsWith(prefix))) env[key] = value
}
return env
}
/** Raised when a run cannot be started at all. */
export class RunError extends Error {}
/**
* Sum per-step usage into one total.
*
* @param totals - the accumulator to add into
* @param usage - the step's usage as dsh reports it, or undefined when it reported none
* @returns {void}
*/
function accumulate(totals: DshUsage, usage: DshStepUsage | undefined): void {
if (usage === undefined) return
totals.input += usage.inputTokens ?? 0
totals.output += usage.outputTokens ?? 0
totals.cacheRead += usage.cacheReadTokens ?? 0
totals.totalTokens += usage.totalTokens ?? 0
}
/**
* Run one dsh turn and resolve when it ends.
*
* The child is killed on timeout or abort; dsh keeps its session either way, so a
* later run can resume it.
*
* @param options - the profile, task, session, limits, and event sink
* @returns the answer, usage, and outcome
* @throws {RunError} when dsh cannot be started or exits without an outcome
*/
export async function run(options: RunOptions): Promise