/** * The distro-side activity observation for GUI distro terminals, and the * handle wrapper that installs it. * * ## The gap this closes * * The terminal controller reclaims unattended terminals after * `unattendedTimeoutMs` of observed idleness, but its observation is * host-side: `dsh-subprocess-local`'s shell-activity integration is gated to * interactive `bash`/`zsh` launched directly on a POSIX host, and a distro * terminal is `wsl.exe` on win32. Every branch of the shipped * `inspectActivity` therefore settles on `unknown` for these terminals, and * `unknown` never accumulates idle — a distro terminal had to be closed by * hand. * * ## The observation * * The terminal is MARKED at launch: a per-terminal `DSH_TERMINAL_ID` rides * the `WSLENV` forwarding this provider already does, so the distro shell — * and every process it spawns — carries it in `/proc//environ`. The * controller's 30 s retention poll then asks the resident agent to count the * marked processes: exactly one (the shell alone, at its prompt) is IDLE; * more than one (a command, a pipeline, a nested shell is running) is BUSY; * none at all defers to the shipped handle (the shell has exited; its own * exit bookkeeping is more precise than this scan). The agent being out * yields `unknown` — the same never-reclaim posture as before, which is the * safe direction: a probe that cannot answer must not authorize a close. * * Pure builders and a transparent proxy here; the agent wiring lives in the * subprocess provider. * * This is a TypeScript source built to `lib/terminal-activity.js`; edit THIS * file and run `pnpm run build` — the artifact under `lib/` is generated, and * `pnpm test` fails when it drifts. * * @module dsh-plugin-wsl-env/terminal-activity */ import type { SubprocessTerminalHandle } from "@deepseek-ai/dsh-subprocess"; /** The env name that marks one terminal's process tree inside the distro. */ export declare const TERMINAL_ID_ENV = "DSH_TERMINAL_ID"; /** * The seam's activity observation — structurally the upstream * `SubprocessTerminalActivity` (`subprocess-local/src/terminal.ts`): `state` * is `'idle' | 'busy' | 'unknown'` and `revision` is a counter that changes * when the reported state changes, which is what the controller's retention * loop keys its idle accumulation on. */ export interface SubprocessTerminalActivity { state: "idle" | "busy" | "unknown"; revision: number; } /** * The probe: count the `/proc` processes carrying the terminal marker. * * POSIX sh on purpose (the distro's `sh` is the only guaranteed interpreter). * The count is the whole decision: the terminal's login shell is exec'd WITH * the marker (wsl.exe imports it into the session before `--exec`), so an * idle-at-prompt shell is exactly one marked process, and anything the shell * runs — a command, a pipeline, a nested shell — is exec'd with the marker * inherited, pushing the count past one. `/proc//environ` shows the * exec-time environment, which is precisely why the count works: runtime * exports would be invisible, and exec-time inheritance is the signal. * * The marker check reads the NUL-separated environ through `tr`, which is why * the id is passed as `$1` rather than interpolated into the script. * * @param terminalId - the `DSH_TERMINAL_ID` value one terminal was launched with. * @returns the agent `exec` target: the probe script with the id as `$1`. */ export declare function terminalActivityProbe(terminalId: string): { cwd: string; argv: string[]; }; /** * Parse the probe's stdout into an activity state. * * @param stdout - the probe's raw stdout. * @returns `"busy"` or `"idle"` as the probe reported, or undefined when the * output is anything else (the caller answers `unknown`). */ export declare function parseTerminalActivity(stdout: string | Buffer): "busy" | "idle" | undefined; /** * Wrap a terminal handle so its activity is observed inside the distro. * * Everything except `inspectActivity` forwards to the wrapped handle * transparently — methods stay bound to the real handle, so the controller's * `write`/`resize`/`signalForeground`/stream access behave exactly as on the * shipped object. `inspectActivity` is replaced wholesale: the shipped * implementation can only answer `unknown` for a distro terminal, and the * replacement answers from the marker scan (or `unknown` itself, when the * probe cannot answer). * * @param handle - the handle `spawnTerminal` returned. * @param inspectActivity - the replacement observation, matching the seam's * `() => Promise` shape. * @returns the transparent proxy handle. */ export declare function wrapTerminalHandle(handle: SubprocessTerminalHandle, inspectActivity: () => Promise): SubprocessTerminalHandle;