/** * In-distro file watching for `WslFileSystem.watch`. * * A 9p share gives Node no usable change notification — chokidar over * `\\wsl.localhost` is the unreliability that made this provider refuse to * watch at all. The seam's contract, however, asks for far less than events: * `watch(target, changed, signal)` wants ONE invalidation callback, invoked * whenever something under `target` may have changed. That is cheap to honor * from inside the distro, where the kernel does know. * * When `inotifywait` is installed and the target is an unbounded directory, * the watcher uses inotify: recursive kernel watches, one `C` per 0.5s of * events (a build's event storm is coalesced, the first event is immediate). * Otherwise it falls back to the long-lived polling loop: every cadence, * `find -newer ` — the directory itself is in the scan, so * creations AND deletions (which bump the parent's mtime) are both seen — and * one `C` line per hit. A FILE target uses the `mtime:size` fingerprint (so a * rename-over or an in-place write is seen), and a positive `maxDepth` keeps * the polling scan because inotify cannot bound its own walk. * * The loop speaks five one-letter lines, the watcher protocol: * * - `R` — READY: the activation barrier; the host resolves the watch promise * on it, never before. * - `C` — CHANGED: the target may have changed. * - `E` — ERROR: the watched path no longer exists, and the loop has * exited. Without it a vanished target would sit armed forever — `find` * over a missing directory prints nothing, indistinguishable from "no * changes" — so the loop says so and the host reports it. * - `P ` — the loop shell's PID, sent before READY. The host uses it to * stop the loop with a second `wsl.exe` on close: killing the transport * alone does not reliably close the distro-side pipe. * - `H` — HEARTBEAT: liveness only, ignored by the host. It exists so the * loop notices a closed stdout pipe: the host's close kills `wsl.exe` but * cannot signal the distro-side shell, and without a write that shell * would sleep and poll forever. The `PIPE` trap turns that write's * SIGPIPE into a clean exit through the stamp-removing EXIT trap. * * Known blind spot, accepted and documented: a file created with an mtime * older than the stamp (`cp -p`, `tar -x`) is not seen until something else * touches the tree. * * This is a TypeScript source built to `lib/watcher.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/watcher */ import type { ChildProcess } from "node:child_process"; /** * The shell loop the watcher runs, parameterised by argv so no path can break * quoting: `$1` is the Linux path (a directory or a file), `$2` the poll * interval in seconds. Pure text, so it is unit-testable. * * @param intervalSeconds - poll cadence; must be >= 1. * @param maxDepth - bound the scan's depth for a DIRECTORY target, 1 = the * directory itself and its direct children; 0 (the default) scans the whole * tree, which is the contract's semantics. Positive values select the * polling scan (inotify is used only for the unbounded walk). A file target * ignores it. * @returns the `sh -c` script body. */ export declare function watchLoopScript(intervalSeconds?: number, maxDepth?: number): string; /** * Arm one in-distro watcher over a Linux directory or file. * * @param options - the watch request. * @returns resolves once observation is active, with the async close function * the seam's contract asks for. */ export declare function armDistroWatcher({ wslPath, distro, linuxPath, onChange, onError, signal, intervalSeconds, maxDepth, spawn: spawnProcess }: { /** The `wsl.exe` path. */ wslPath: string; /** The distro to watch inside. */ distro: string; /** The absolute Linux path to observe. */ linuxPath: string; /** Watcher-level failures (an exited loop, a vanished target); the seam reports these through its own channel. */ onError?: (error: Error) => void; /** The invalidation callback. */ onChange: () => void; /** Cancels initialization; the CALLER invokes the returned close to stop an armed watcher. */ signal: AbortSignal; /** Poll cadence, default 2. */ intervalSeconds?: number; /** Scan depth bound, 0 = the whole tree. */ maxDepth?: number; /** Injectable process factory for tests. */ spawn?: (wslPath: string, args: string[]) => ChildProcess; }): Promise<() => Promise>;