Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Segment subprocess.log into per-command blocks joined to trace.log (#3182)
Maximilian Roos59a1c9cbfd12
diff --git a/src/llm.rs b/src/llm.rsindex 4e2a1faac..72946f379 100644--- a/src/llm.rs+++ b/src/llm.rs@@ -9,7 +9,7 @@ use std::sync::atomic::{AtomicBool, Ordering};use worktrunk::config::CommitGenerationConfig;use worktrunk::git::{CommitMessageDetail, Repository};use worktrunk::path::format_path_for_display;-use worktrunk::shell_exec::{Cmd, SUBPROCESS_FULL_TARGET, ShellConfig};+use worktrunk::shell_exec::{Cmd, ShellConfig};use worktrunk::styling::{eprintln, warning_message};use minijinja::Environment;@@ -400,13 +400,6 @@ pub(crate) fn execute_llm_command(command: &str, prompt: &str) -> anyhow::Result// MB-scale diffs in our process memory and removes them from our logs// entirely. See conversation around PR #2136 for sketch.- // Log the prompt to subprocess.log alongside captured subprocess stdout —- // SUBPROCESS_FULL_TARGET routes to subprocess.log at `-vv`, never to stderr.- log::debug!(target: SUBPROCESS_FULL_TARGET, " Prompt (stdin):");- for line in prompt.lines() {- log::debug!(target: SUBPROCESS_FULL_TARGET, " {}", line);- }-let shell = ShellConfig::get()?;let output = Cmd::new(shell.executable.to_string_lossy()).args(&shell.args)diff --git a/src/log_files.rs b/src/log_files.rsindex 342f95478..6178bc941 100644--- a/src/log_files.rs+++ b/src/log_files.rs@@ -6,9 +6,10 @@//! headers, and bounded subprocess previews. High-signal, bounded size —//! safe to embed in `diagnostic.md` bug reports.//! - [`SUBPROCESS`] → `subprocess.log`: raw, uncapped subprocess-//! stdout/stderr bodies captured by `shell_exec::Cmd`. Potentially-//! multi-MB (full `git log -p` / patch-id output); opt-in for deep-//! dives.+//! stdout/stderr bodies captured by `shell_exec::Cmd`, each block+//! introduced by a `$ cmd … [seq=N tid=T]` header whose `seq` joins it to+//! the command's `[wt-trace]` record in `trace.log`. Potentially multi-MB+//! (full `git log -p` / patch-id output); opt-in for deep dives.//!//! Direct user-facing output (`info_message` / `eprintln!` from command//! code) is unaffected — it goes to stderr at every verbosity level. Thisdiff --git a/src/logging.rs b/src/logging.rsindex facff7686..fd78250a3 100644--- a/src/logging.rs+++ b/src/logging.rs@@ -8,7 +8,7 @@//! | ---------------- | --------------------------------------------------- | ----------------- |//! | stderr | `$RUST_LOG` or flag baseline (`Off`/`Info`/`Info`) | styled with ANSI |//! | `trace.log` | `-vv` only, excludes `SUBPROCESS_FULL_TARGET` | plain text |-//! | `subprocess.log` | `-vv` only, includes only `SUBPROCESS_FULL_TARGET` | raw (no prefix) |+//! | `subprocess.log` | `-vv` only, includes only `SUBPROCESS_FULL_TARGET` | raw bodies + `$ cmd … seq=N` headers |//!//! At `-vv` the stderr layer keeps its Info baseline — `-vv` is a strict//! superset of `-v`, with Debug-level records (the noisy ones, including@@ -201,6 +201,7 @@ struct WtTraceFields {kind: Option<String>,ts: Option<u64>,tid: Option<u64>,+ seq: Option<u64>,dur_us: Option<u64>,ok: Option<bool>,context: Option<String>,@@ -215,6 +216,7 @@ impl tracing::field::Visit for WtTraceFields {match field.name() {"ts" => self.ts = Some(value),"tid" => self.tid = Some(value),+ "seq" => self.seq = Some(value),"dur_us" => self.dur_us = Some(value),_ => {}}@@ -273,28 +275,32 @@ fn format_wt_trace(f: &WtTraceFields) -> String {match f.kind.as_deref() {Some("cmd_completed") => {+ let seq = f.seq.unwrap_or(0);let cmd = f.cmd.as_deref().unwrap_or("");let dur_us = f.dur_us.unwrap_or(0);let ok = f.ok.unwrap_or(false);match &f.context {Some(ctx) => format!(- r#"[wt-trace] ts={ts} tid={tid} context={ctx} cmd="{cmd}" dur_us={dur_us} ok={ok}"#+ r#"[wt-trace] ts={ts} tid={tid} seq={seq} context={ctx} cmd="{cmd}" dur_us={dur_us} ok={ok}"#),None => {- format!(r#"[wt-trace] ts={ts} tid={tid} cmd="{cmd}" dur_us={dur_us} ok={ok}"#)+ format!(+ r#"[wt-trace] ts={ts} tid={tid} seq={seq} cmd="{cmd}" dur_us={dur_us} ok={ok}"#+ )}}}Some("cmd_errored") => {+ let seq = f.seq.unwrap_or(0);let cmd = f.cmd.as_deref().unwrap_or("");let dur_us = f.dur_us.unwrap_or(0);let err = f.err.as_deref().unwrap_or("");match &f.context {Some(ctx) => format!(- r#"[wt-trace] ts={ts} tid={tid} context={ctx} cmd="{cmd}" dur_us={dur_us} err="{err}""#+ r#"[wt-trace] ts={ts} tid={tid} seq={seq} context={ctx} cmd="{cmd}" dur_us={dur_us} err="{err}""#),None => format!(- r#"[wt-trace] ts={ts} tid={tid} cmd="{cmd}" dur_us={dur_us} err="{err}""#+ r#"[wt-trace] ts={ts} tid={tid} seq={seq} cmd="{cmd}" dur_us={dur_us} err="{err}""#),}}@@ -314,8 +320,11 @@ fn format_wt_trace(f: &WtTraceFields) -> String {}}-/// `subprocess.log` formatter: the message verbatim. Subprocess bodies are-/// already prefixed (` …` / ` ! …`) by `shell_exec::format_stream_full`.+/// `subprocess.log` formatter: the message verbatim. Body lines are already+/// prefixed (` …` / ` ! …`) by `shell_exec::format_stream_full`, and each+/// command's block is introduced by a `$ cmd … seq=N` header line emitted by+/// `shell_exec::log_output` — both arrive pre-rendered, so this writer adds+/// nothing.struct SubprocessFileFormat;impl<S, N> FormatEvent<S, N> for SubprocessFileFormat@@ -506,7 +515,8 @@ whereSome(layer)}-/// `subprocess.log` layer: only `SUBPROCESS_FULL_TARGET` records, raw passthrough.+/// `subprocess.log` layer: only `SUBPROCESS_FULL_TARGET` records (raw bodies+/// and their `$ cmd … seq=N` headers), written through verbatim.fn build_subprocess_layer<S>(verbose_level: u8) -> Option<impl Layer<S>>whereS: Subscriber + for<'a> LookupSpan<'a>,@@ -532,9 +542,11 @@ wherefn announce_trace_destination() {// TRACE and SUBPROCESS open independently — `LogSink::init` succeeds per// file. The (Some, None) case (trace.log open, subprocess.log failed) is- // rare but real (path-type mismatch, fs quota); the reverse is- // possible too but `subprocess.log` alone has no `$ cmd` context, so we- // stay silent there. `diagnostic.md` is named even though it's written+ // rare but real (path-type mismatch, fs quota); the reverse is possible+ // too, but the pointer is anchored on trace.log's path (we derive the logs+ // dir from it below) and subprocess.log's uncapped raw bodies aren't worth+ // surfacing on their own, so we stay silent there. `diagnostic.md` is named+ // even though it's written// at exit (not init) — by the time the user reads the pointer and looks// for files, all three will be there.let Some(trace_path) = log_files::TRACE.path() else {@@ -687,6 +699,7 @@ mod tests {kind: Some("cmd_completed".into()),ts: Some(100),tid: Some(3),+ seq: Some(1),context: Some("worktree".into()),cmd: Some("git status".into()),dur_us: Some(12300),@@ -695,7 +708,7 @@ mod tests {};assert_eq!(format_wt_trace(&f),- r#"[wt-trace] ts=100 tid=3 context=worktree cmd="git status" dur_us=12300 ok=true"#+ r#"[wt-trace] ts=100 tid=3 seq=1 context=worktree cmd="git status" dur_us=12300 ok=true"#);// cmd_completed without context@@ -703,6 +716,7 @@ mod tests {kind: Some("cmd_completed".into()),ts: Some(100),tid: Some(3),+ seq: Some(2),cmd: Some("gh pr list".into()),dur_us: Some(45200),ok: Some(false),@@ -710,7 +724,7 @@ mod tests {};assert_eq!(format_wt_trace(&f),- r#"[wt-trace] ts=100 tid=3 cmd="gh pr list" dur_us=45200 ok=false"#+ r#"[wt-trace] ts=100 tid=3 seq=2 cmd="gh pr list" dur_us=45200 ok=false"#);// cmd_errored with context@@ -718,6 +732,7 @@ mod tests {kind: Some("cmd_errored".into()),ts: Some(100),tid: Some(3),+ seq: Some(3),context: Some("main".into()),cmd: Some("git merge-base".into()),dur_us: Some(100000),@@ -726,7 +741,7 @@ mod tests {};assert_eq!(format_wt_trace(&f),- r#"[wt-trace] ts=100 tid=3 context=main cmd="git merge-base" dur_us=100000 err="fatal: ...""#+ r#"[wt-trace] ts=100 tid=3 seq=3 context=main cmd="git merge-base" dur_us=100000 err="fatal: ...""#);// cmd_errored without context (standalone tools like gh)@@ -734,6 +749,7 @@ mod tests {kind: Some("cmd_errored".into()),ts: Some(100),tid: Some(3),+ seq: Some(4),cmd: Some("gh pr list".into()),dur_us: Some(1000),err: Some("network down".into()),@@ -741,7 +757,7 @@ mod tests {};assert_eq!(format_wt_trace(&f),- r#"[wt-trace] ts=100 tid=3 cmd="gh pr list" dur_us=1000 err="network down""#+ r#"[wt-trace] ts=100 tid=3 seq=4 cmd="gh pr list" dur_us=1000 err="network down""#);// instantdiff --git a/src/shell_exec.rs b/src/shell_exec.rsindex 6a8cf433f..ab1354179 100644--- a/src/shell_exec.rs+++ b/src/shell_exec.rs@@ -489,9 +489,11 @@ const LOG_OUTPUT_MAX_LINES: usize = 200;/// addition to [`LOG_OUTPUT_MAX_LINES`].const LOG_OUTPUT_MAX_BYTES: usize = 64 * 1024;-/// Log target used for *full* subprocess stdout/stderr. The tracing-subscriber-/// `subprocess.log` layer filters on this target, so raw subprocess bodies (diffs,-/// `git log -p`, patch-id pipelines) never reach stderr or `trace.log`.+/// Log target used for *full* subprocess stdin/stdout/stderr (and the per-command+/// `$ cmd … seq=N` header `log_output` prepends to each block). The+/// tracing-subscriber `subprocess.log` layer filters on this target, so raw+/// subprocess bodies (diffs, `git log -p`, patch-id pipelines) never reach+/// stderr or `trace.log`.pub const SUBPROCESS_FULL_TARGET: &str = "worktrunk::subprocess_full";/// Log target used for the *bounded* preview of subprocess output (capped@@ -501,30 +503,71 @@ pub const SUBPROCESS_FULL_TARGET: &str = "worktrunk::subprocess_full";/// uncapped version is captured via [`SUBPROCESS_FULL_TARGET`].pub const SUBPROCESS_BOUNDED_TARGET: &str = "worktrunk::subprocess_bounded";-/// Log captured stdout/stderr of a finished command.+/// The `$ <cmd> [<context>]` command header. Shared by the stderr/`trace.log`+/// start line ([`Cmd::log_run_start`] and friends) and the per-command header+/// that [`log_output`] prepends to each block in `subprocess.log`, so the two+/// render the command identically.+fn command_header(cmd: &str, context: Option<&str>) -> String {+ match context {+ Some(ctx) => format!("$ {cmd} [{ctx}]"),+ None => format!("$ {cmd}"),+ }+}++/// Log a command's captured stdin, stdout, and stderr to the debug targets.///-/// At `tracing::DEBUG` (`-vv`) each stream is emitted twice via separate-/// targets, and the tracing-subscriber layers route them:-/// - [`SUBPROCESS_FULL_TARGET`]: uncapped, line-per-record, `subprocess.log` only.-/// - [`SUBPROCESS_BOUNDED_TARGET`]: capped at [`LOG_OUTPUT_MAX_LINES`] and-/// [`LOG_OUTPUT_MAX_BYTES`] per stream with an elision marker, then-/// routed to `trace.log` at `-vv` or stderr otherwise.+/// At `tracing::DEBUG` (`-vv`) each stream is emitted twice, and the+/// tracing-subscriber layers route the two targets:+/// - [`SUBPROCESS_FULL_TARGET`] → `subprocess.log`: uncapped, one record per+/// line. Each command with I/O opens with a `$ cmd … [seq=N tid=T]` header+/// over its stdin (` < `), stdout (` `), and stderr (` ! `) lines, so the+/// otherwise-undelimited bytes segment into blocks and `seq` joins each+/// block to its `[wt-trace]` record.+/// - [`SUBPROCESS_BOUNDED_TARGET`] → `trace.log` at `-vv` (else stderr):+/// capped at [`LOG_OUTPUT_MAX_LINES`] / [`LOG_OUTPUT_MAX_BYTES`] with an+/// elision marker, under the `$ cmd` start line `trace.log` already carries.+///+/// One record per line keeps a command's own output contiguous but lets+/// concurrent commands interleave by line; `tid` groups a block and the atomic+/// header recovers the `seq` join.+///+/// `output` is `None` for a command that never produced any (spawn or wait+/// failure); its header and stdin still emit, so the input survives in the deep+/// log even when nothing ran.////// Below Debug both targets are disabled and this is a no-op.-fn log_output(output: &std::process::Output) {+fn log_output(trace: &CommandTrace, stdin: Option<&[u8]>, output: Option<&std::process::Output>) {if !log::log_enabled!(log::Level::Debug) {return;}- for line in format_stream_full(&output.stdout, " ") {+ let stdin = stdin.unwrap_or_default();+ // `None` when the command never produced output (spawn/wait failure); stdin+ // is still logged so the input survives in the deep log.+ let (stdout, stderr) = output+ .map(|o| (o.stdout.as_slice(), o.stderr.as_slice()))+ .unwrap_or_default();+ if !stdin.is_empty() || !stdout.is_empty() || !stderr.is_empty() {+ log::debug!(+ target: SUBPROCESS_FULL_TARGET,+ "{} [seq={} tid={}]",+ command_header(trace.cmd(), trace.context()),+ trace.seq(),+ trace.tid(),+ );+ }+ for line in format_stream_full(stdin, " < ") {+ log::debug!(target: SUBPROCESS_FULL_TARGET, "{}", line);+ }+ for line in format_stream_full(stdout, " ") {log::debug!(target: SUBPROCESS_FULL_TARGET, "{}", line);}- for line in format_stream_full(&output.stderr, " ! ") {+ for line in format_stream_full(stderr, " ! ") {log::debug!(target: SUBPROCESS_FULL_TARGET, "{}", line);}- for line in format_stream_bounded(&output.stdout, " ") {+ for line in format_stream_bounded(stdout, " ") {log::debug!(target: SUBPROCESS_BOUNDED_TARGET, "{}", line);}- for line in format_stream_bounded(&output.stderr, " ! ") {+ for line in format_stream_bounded(stderr, " ! ") {log::debug!(target: SUBPROCESS_BOUNDED_TARGET, "{}", line);}}@@ -750,20 +793,25 @@ impl ExternalCommandLog {}/// Resolve a [`CommandTrace`] from a finished `run`/`pipe_into` invocation and-/// surface the captured stdout/stderr to the debug log.+/// surface the command's captured stdin/stdout/stderr to the debug log.////// The buffered counterpart to calling `complete`/`fail` directly: `run` and-/// `pipe_into` capture output, so on success they also feed it to-/// [`log_output`]. `stream`/`delayed_stream` inherit or stream stdio and resolve-/// their `CommandTrace` directly at each exit point.-fn record_captured(trace: &mut CommandTrace, result: &std::io::Result<std::process::Output>) {+/// `pipe_into` capture output, so they also feed it to [`log_output`] — stdin+/// regardless of outcome, stdout/stderr when the command produced any.+/// `stream`/`delayed_stream` inherit or stream stdio and resolve their+/// `CommandTrace` directly at each exit point.+fn record_captured(+ trace: &mut CommandTrace,+ stdin: Option<&[u8]>,+ result: &std::io::Result<std::process::Output>,+) {match result {- Ok(output) => {- trace.complete(output.status.success());- log_output(output);- }+ Ok(output) => trace.complete(output.status.success()),Err(e) => trace.fail(e),}+ // stdin is logged either way; stdout/stderr only when the command produced+ // output — a command that failed to spawn still leaves its input behind.+ log_output(trace, stdin, result.as_ref().ok());}/// Structured error from [`Cmd::delayed_stream`].@@ -943,24 +991,23 @@ impl Cmd {}fn log_run_start(&self, cmd_str: &str) {- match &self.context {- Some(ctx) => log::debug!("$ {} [{}]", cmd_str, ctx),- None => log::debug!("$ {}", cmd_str),- }+ log::debug!("{}", command_header(cmd_str, self.context.as_deref()));}fn log_stream_start(&self, cmd_str: &str, exec_mode: &str) {- match &self.context {- Some(ctx) => log::debug!("$ {} [{}] (streaming, {})", cmd_str, ctx, exec_mode),- None => log::debug!("$ {} (streaming, {})", cmd_str, exec_mode),- }+ log::debug!(+ "{} (streaming, {})",+ command_header(cmd_str, self.context.as_deref()),+ exec_mode+ );}fn log_delayed_stream_start(&self, cmd_str: &str, delay_ms: i64) {- match &self.context {- Some(ctx) => log::debug!("$ {} [{}] (delayed stream, {}ms)", cmd_str, ctx, delay_ms),- None => log::debug!("$ {} (delayed stream, {}ms)", cmd_str, delay_ms),- }+ log::debug!(+ "{} (delayed stream, {}ms)",+ command_header(cmd_str, self.context.as_deref()),+ delay_ms+ );}/// Add a single argument.@@ -1185,7 +1232,7 @@ impl Cmd {// `Result<Output>` so spawn/write failures resolve the trace through// `record_captured` rather than `?`-ing past it (which would leave the// command unattributed and trip CommandTrace's drop assertion).- let result = if let Some(stdin_data) = self.stdin_data {+ let result = if let Some(stdin_data) = self.stdin_data.as_deref() {// Stdin piping requires spawn/write/wait// Note: stdin path doesn't support timeout (would need async I/O)cmd.stdin(Stdio::piped())@@ -1200,7 +1247,7 @@ impl Cmd {// forever. (ignore BrokenPipe - some commands exit early)let write_result = {let mut stdin = child.stdin.take().expect("stdin was configured as piped");- stdin.write_all(&stdin_data)+ stdin.write_all(stdin_data)};match write_result {Err(e) if e.kind() != std::io::ErrorKind::BrokenPipe => Err(e),@@ -1217,7 +1264,7 @@ impl Cmd {cmd.output()};- record_captured(&mut trace, &result);+ record_captured(&mut trace, self.stdin_data.as_deref(), &result);let exit_code = result.as_ref().ok().and_then(|output| output.status.code());external_log.record(exit_code);@@ -1234,6 +1281,10 @@ impl Cmd {/// `git diff-tree -p | git patch-id`) out of the `-vv` trace stream, where/// `log_output` would otherwise dump every line of the raw diff.///+ /// The source's *own* stdin (`stdin_bytes`, e.g. the `git rev-list` commit+ /// list) is logged under ` < ` like any `.run()` command — only the+ /// intermediate stream above is suppressed.+ ////// Each command is logged and traced individually (same format as/// `.run()`), so `-vv` still shows both commands and their exit status./// The returned tuple is `(source_output, sink_output)` — callers can@@ -1321,16 +1372,14 @@ impl Cmd {.stdout.take().expect("stdout was configured as piped");- // Pair the source's stdin pipe with the data to write; a writer- // thread (below) drains it concurrently with the rest of the pipeline.- let first_stdin = source_stdin.map(|data| {- (- first_child- .stdin- .take()- .expect("stdin was configured as piped"),- data,- )+ // Take the source's stdin pipe, keeping `source_stdin` owned so it can+ // also be logged below; a writer thread (in the scope) drains it+ // concurrently with the rest of the pipeline.+ let source_stdin_pipe = source_stdin.as_ref().map(|_| {+ first_child+ .stdin+ .take()+ .expect("stdin was configured as piped")});let mut second = next.direct_command();@@ -1378,18 +1427,22 @@ impl Cmd {// with the drain below — writing it all up front would deadlock// once the source's stdout pipe fills. A short write (the source// exited early) surfaces as its non-zero exit status, which- // callers already inspect.- if let Some((mut stdin, data)) = first_stdin {+ // callers already inspect. The scoped thread only borrows the+ // data, so `source_stdin` stays available to log after the+ // pipeline reaps the source.+ if let (Some(mut stdin), Some(data)) = (source_stdin_pipe, source_stdin.as_deref()) {s.spawn(move || {- let _ = stdin.write_all(&data);+ let _ = stdin.write_all(data);// `stdin` drops here, closing the pipe so the source sees EOF.});}// Drain `next` first (its `wait_with_output` reads its own- // stdout/stderr), so `first`'s writes can complete.+ // stdout/stderr), so `first`'s writes can complete. Its stdin is the+ // source's stdout (the OS pipe), not its own input, so it is logged+ // with `None` — the intermediate stream stays out of the deep log.let second_result = second_child.wait_with_output();- record_captured(&mut second_trace, &second_result);+ record_captured(&mut second_trace, None, &second_result);// Reap `first`. Its stderr is already being drained; combine// the captured stderr with the exit status into an Output.@@ -1403,7 +1456,10 @@ impl Cmd {stderr,})});- record_captured(&mut first_trace, &first_result);+ // The source's own stdin (the commit list) is logged under ` < `,+ // symmetric with `run`. Only the intermediate diff stream — the+ // source's stdout, routed to the sink via OS pipe — stays out.+ record_captured(&mut first_trace, source_stdin.as_deref(), &first_result);(first_result, second_result)});@@ -2359,6 +2415,19 @@ mod tests {assert_eq!(String::from_utf8_lossy(&sink.stdout), "hello");}+ #[test]+ #[cfg(unix)]+ fn test_cmd_pipe_into_feeds_source_stdin() {+ // The source's stdin must flow through to the sink: `cat` echoes its+ // stdin to stdout, piped into a second `cat`, so the bytes round-trip.+ let (source, sink) = Cmd::new("cat")+ .stdin_bytes(b"piped stdin".to_vec())+ .pipe_into(Cmd::new("cat"))+ .unwrap();+ assert!(source.status.success() && sink.status.success());+ assert_eq!(String::from_utf8_lossy(&sink.stdout), "piped stdin");+ }+#[test]fn test_cmd_pipe_into_source_spawn_failure_resolves_trace() {let err = Cmd::new(MISSING_CMD)diff --git a/src/trace/emit.rs b/src/trace/emit.rsindex 0bdb08c38..b0e4039bf 100644--- a/src/trace/emit.rs+++ b/src/trace/emit.rs@@ -9,17 +9,21 @@//! # Format//!//! ```text-//! [wt-trace] ts=1234567 tid=3 context=worktree cmd="git status" dur_us=12300 ok=true-//! [wt-trace] ts=1234567 tid=3 cmd="gh pr list" dur_us=45200 ok=false-//! [wt-trace] ts=1234567 tid=3 context=main cmd="git merge-base" dur_us=100000 err="fatal: ..."+//! [wt-trace] ts=1234567 tid=3 seq=1 context=worktree cmd="git status" dur_us=12300 ok=true+//! [wt-trace] ts=1234567 tid=3 seq=2 cmd="gh pr list" dur_us=45200 ok=false+//! [wt-trace] ts=1234567 tid=3 seq=3 context=main cmd="git merge-base" dur_us=100000 err="fatal: ..."//! [wt-trace] ts=1234567 tid=3 event="Showed skeleton"//! [wt-trace] ts=1234567 tid=3 span="build_hook_context" dur_us=8200//! ```//!+//! `seq` is a process-global monotonic command counter (command records only).+//! The same value is printed into the per-command header in `subprocess.log`,+//! so a raw output block there joins back to its command record here.+//!//! # Emission model//!//! Records emit as `tracing` events under [`WT_TRACE_TARGET`] with typed-//! structured fields (`kind`, `ts`, `tid`, `cmd`, `dur_us`, `ok`, `err`,+//! structured fields (`kind`, `ts`, `tid`, `seq`, `cmd`, `dur_us`, `ok`, `err`,//! `event`, `span`, `context`). The text grammar is produced downstream by//! the `trace.log` layer's `FormatEvent` impl in//! `src/logging.rs::TraceFileFormat`, which reads the structured fields@@ -70,11 +74,14 @@//! visible. Subprocess stdout/stderr continuations route through separate//! targets: the full output goes to `subprocess.log`, and a bounded preview//! shares the routing of all other records — `trace.log` at `-vv`, stderr-//! otherwise — so raw bodies don't spam `-vv`.+//! otherwise — so raw bodies don't spam `-vv`. Each block in `subprocess.log`+//! is prefixed with a `$ cmd … seq=N` header (see `shell_exec::log_output`)+//! so it stays segmentable and joins back to this record via `seq`.use std::borrow::Cow;use std::fmt::Display;use std::sync::OnceLock;+use std::sync::atomic::{AtomicU64, Ordering};use std::time::Instant;/// Tracing target the `trace.log` layer keys on to render the `[wt-trace]`@@ -108,18 +115,35 @@ pub fn thread_id() -> u64 {.unwrap_or(0)}+/// Process-global monotonic command counter. Each [`CommandTrace`] claims the+/// next value at construction; the same `seq` is printed into the `[wt-trace]`+/// record in `trace.log` and the per-command header in `subprocess.log`, so a+/// raw output block can be joined back to its command record. Claimed at+/// construction (outside any verbosity gate) so the sequence is dense+/// regardless of whether the file layers are active. Starts at 1.+static CMD_SEQ: AtomicU64 = AtomicU64::new(1);+/// Emit a completed-command record (`ok=true`/`ok=false`).////// Private: the sole caller is [`CommandTrace::complete`]. Keeping the writer/// module-private makes [`CommandTrace`] the only way to emit a command/// record — see the module-level "subprocess chokepoint" note.-fn command_completed(context: Option<&str>, cmd: &str, ts: u64, tid: u64, dur_us: u64, ok: bool) {+fn command_completed(+ context: Option<&str>,+ cmd: &str,+ ts: u64,+ tid: u64,+ seq: u64,+ dur_us: u64,+ ok: bool,+) {match context {Some(ctx) => tracing::debug!(target: WT_TRACE_TARGET,kind = "cmd_completed",ts,tid,+ seq,context = ctx,cmd,dur_us,@@ -130,6 +154,7 @@ fn command_completed(context: Option<&str>, cmd: &str, ts: u64, tid: u64, dur_uskind = "cmd_completed",ts,tid,+ seq,cmd,dur_us,ok,@@ -145,6 +170,7 @@ fn command_errored(cmd: &str,ts: u64,tid: u64,+ seq: u64,dur_us: u64,err: impl Display,) {@@ -155,6 +181,7 @@ fn command_errored(kind = "cmd_errored",ts,tid,+ seq,context = ctx,cmd,dur_us,@@ -165,6 +192,7 @@ fn command_errored(kind = "cmd_errored",ts,tid,+ seq,cmd,dur_us,err = %err,@@ -206,6 +234,7 @@ pub struct CommandTrace {start_ts_us: u64,start: Instant,tid: u64,+ seq: u64,resolved: bool,}@@ -219,10 +248,33 @@ impl CommandTrace {start_ts_us: now_us(),start: Instant::now(),tid: thread_id(),+ seq: CMD_SEQ.fetch_add(1, Ordering::Relaxed),resolved: false,}}+ /// The command's monotonic sequence number — the key shared by this+ /// command's `[wt-trace]` record (`trace.log`) and its raw output block+ /// (`subprocess.log`).+ pub(crate) fn seq(&self) -> u64 {+ self.seq+ }++ /// The thread that ran the command.+ pub(crate) fn tid(&self) -> u64 {+ self.tid+ }++ /// The command string (e.g. `git status --porcelain`).+ pub(crate) fn cmd(&self) -> &str {+ &self.cmd+ }++ /// The command's context label (typically a worktree name), if any.+ pub(crate) fn context(&self) -> Option<&str> {+ self.context.as_deref()+ }+/// The child ran to completion; `success` is its exit status. Emits an/// `ok=true`/`ok=false` record with the elapsed duration.pub fn complete(&mut self, success: bool) {@@ -232,6 +284,7 @@ impl CommandTrace {&self.cmd,self.start_ts_us,self.tid,+ self.seq,dur_us,success,);@@ -255,6 +308,7 @@ impl CommandTrace {&self.cmd,self.start_ts_us,self.tid,+ self.seq,dur_us,err,);diff --git a/src/trace/parse.rs b/src/trace/parse.rsindex 7a8a75a9f..51901ab8b 100644--- a/src/trace/parse.rs+++ b/src/trace/parse.rs@@ -2,11 +2,15 @@//!//! Trace lines are emitted by `shell_exec::Cmd` with this format://! ```text-//! [wt-trace] ts=1234567 tid=3 context=worktree cmd="git status" dur_us=12300 ok=true-//! [wt-trace] ts=1234567 tid=3 cmd="gh pr list" dur_us=45200 ok=false-//! [wt-trace] ts=1234567 tid=3 context=main cmd="git merge-base" dur_us=100000 err="fatal: ..."+//! [wt-trace] ts=1234567 tid=3 seq=1 context=worktree cmd="git status" dur_us=12300 ok=true+//! [wt-trace] ts=1234567 tid=3 seq=2 cmd="gh pr list" dur_us=45200 ok=false+//! [wt-trace] ts=1234567 tid=3 seq=3 context=main cmd="git merge-base" dur_us=100000 err="fatal: ..."//! ```//!+//! `seq` (a per-command counter, command records only) is parsed leniently —+//! it's dropped as an unknown key, since no consumer needs it; it correlates a+//! `trace.log` record with its raw output block in `subprocess.log`.+//!//! Instant events (milestones without duration) use this format://! ```text//! [wt-trace] ts=1234567 tid=3 event="Showed skeleton"diff --git a/tests/integration_tests/diagnostic.rs b/tests/integration_tests/diagnostic.rsindex 0e6983e81..cbf21375b 100644--- a/tests/integration_tests/diagnostic.rs+++ b/tests/integration_tests/diagnostic.rs@@ -360,6 +360,72 @@ fn test_vv_splits_full_and_bounded_output(repo: TestRepo) {);}+/// Each command's block in `subprocess.log` is introduced by a+/// `$ cmd … [seq=N tid=T]` header, and that `seq` also tags the command's+/// `[wt-trace]` record in `trace.log` — so an otherwise-undelimited raw output+/// block can be segmented and joined back to its timed command record. Guards+/// the segmentation + correlation contract the two files share.+#[rstest]+fn test_vv_subprocess_log_headers_join_trace(repo: TestRepo) {+ repo.wt_command().args(["list", "-vv"]).output().unwrap();++ let logs_dir = repo.root_path().join(".git").join("wt/logs");+ let subprocess = fs::read_to_string(logs_dir.join("subprocess.log")).unwrap();+ let trace = fs::read_to_string(logs_dir.join("trace.log")).unwrap();++ // Pick a known captured command (`wt list` runs `git worktree list+ // --porcelain`) so the join can be checked by command identity, not just by+ // a seq value that — being process-global and dense — exists on *some*+ // record regardless. A header starts with `$ ` (body lines start with ` `,+ // so blocks self-segment) and carries the `seq` join key.+ let header = subprocess+ .lines()+ .find(|l| l.starts_with("$ ") && l.contains("git worktree list") && l.contains("[seq="))+ .expect("subprocess.log should head the `git worktree list` block with `$ … [seq=N …]`");++ let seq: u64 = header+ .split("[seq=")+ .nth(1)+ .and_then(|s| s.split([' ', ']']).next())+ .and_then(|s| s.parse().ok())+ .expect("the subprocess.log header should carry a numeric seq");++ // The [wt-trace] record carrying that seq must be the SAME command —+ // asserting both `seq` and `cmd` closes the gap where a dense seq coincides+ // with an unrelated record.+ assert!(+ trace.lines().any(|l| {+ l.contains("[wt-trace]")+ && l.contains(&format!("seq={seq} "))+ && l.contains(r#"cmd="git worktree list"#)+ }),+ "trace.log should carry the [wt-trace] record for `git worktree list` with seq={seq} to join the subprocess.log block:\n{header}"+ );+}++/// A command that pipes stdin has it captured in its `subprocess.log` block+/// under a ` < ` prefix, so the deep log shows what was fed in, not just what+/// came out. `wt list --full` counts each worktree's diff including untracked+/// files, staging them via `git add --pathspec-from-file=-` (stdin), which+/// exercises the path.+#[rstest]+fn test_vv_subprocess_log_captures_stdin(repo: TestRepo) {+ for path in repo.worktrees.values() {+ std::fs::write(path.join("untracked-probe.txt"), "stdin probe\n").unwrap();+ }+ repo.wt_command()+ .args(["list", "--full", "-vv"])+ .output()+ .unwrap();++ let subprocess =+ fs::read_to_string(repo.root_path().join(".git").join("wt/logs/subprocess.log")).unwrap();+ assert!(+ subprocess.lines().any(|l| l.starts_with(" < ")),+ "subprocess.log should capture piped stdin under a ` < ` prefix:\n{subprocess}"+ );+}+/// Control bytes in captured subprocess output must be escaped on the/// human-facing routes (stderr + `trace.log`) but kept verbatim in/// `subprocess.log`. `wt list` runs `git for-each-ref --format=…%00…`