import { AsyncLocalStorage } from 'node:async_hooks' import type { Secret } from './secret.ts' /** One finished child process. `argv` may still hold Secret values; the log redacts them. */ export type CommandNote = { readonly argv: readonly (string | Secret)[] readonly cwd: string readonly exitCode: number readonly durationMs: number readonly stdout: string readonly stderr: string } type Sink = (note: CommandNote) => Promise const current = new AsyncLocalStorage() /** Commands started inside `fn` are handed to `sink`. Concurrent runs do not share a sink. */ export function withCommandSink(sink: Sink, fn: () => Promise): Promise { return current.run(sink, fn) } export function commandSinkActive(): boolean { return Boolean(current.getStore()) } export function traceCommand(note: CommandNote): Promise { const sink = current.getStore() return sink ? sink(note) : Promise.resolve() }