1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! `run show` — full manifest + counters for one run.
use serde::Serialize;
use serde_json::Value;
use octl_core::{read_manifest_opt, read_node_opt, NodeId, RunLock, Status};
use crate::error::CliError;
use crate::output::{self, OutputFormat, OutputSpec};
use crate::run::dto::{ManifestView, SupervisorView};
use crate::run::{from_core, run_paths};
/// The single reporting node of a single-worker worktree run (`n-0001`);
/// mirrors `run merge` / `run wait`'s `DEFAULT_NODE_ID`. Its terminal report is
/// where a supervisor stamps the `recoverable_work` stranded-work signal.
const DEFAULT_NODE_ID: &str = "n-0001";
#[derive(Serialize)]
struct ShowPayload<'a> {
manifest: ManifestView<'a>,
counts: Counts,
/// Rebase-robust landing signal for the reporting node: true when the
/// worker's committed work has landed in the target, confirmed by patch-id
/// equivalence against the *current* target tip (`git cherry`) — NOT by
/// branch-ref ancestry, which a caller-side `git rebase` invalidates. Falls
/// back to the durable merge marker when git verification is unavailable
/// (issue `landing-signal-reliable-after-rebase`). Callers should trust this
/// instead of running `git merge-base --is-ancestor` by hand.
landed: bool,
/// How `landed` was decided: `git-verified` | `report-marker` | `unverified`.
landed_method: &'static str,
/// The `recoverable_work` block from the default node's terminal report,
/// present only when a dead agent left unmerged commits ahead of source
/// (issue `agent-death-strands-recoverable-work`). Surfaced so a caller can
/// spot salvageable work on a `failed` run without inspecting the node
/// projection or running `git log <source>..<branch>`.
#[serde(skip_serializing_if = "Option::is_none")]
recoverable_work: Option<Value>,
}
/// The run/node fields read under the shared lock to compute `landed` after the
/// lock is released (the git shell-out must not run while the flock is held).
struct LandingFields {
source_repo: Option<String>,
source_branch: Option<String>,
worktree_path: Option<String>,
branch: Option<String>,
base_sha: Option<String>,
report: Option<Value>,
}
#[derive(Serialize)]
struct Counts {
nodes: u64,
discussions: u64,
spinoffs: u64,
}
pub fn run(run_id: &str, spec: &OutputSpec, warnings: &[String]) -> Result<(), CliError> {
let root = crate::home::root_dir()?;
let paths = run_paths(&root, run_id)?;
// Hold the run's shared lock for the whole manifest + projection-dir scan so
// a concurrent reducer cannot leave us with a manifest counter that
// disagrees with the projection files we count (design.md §4). The lock is
// released before any output is formatted.
let scanned = RunLock::with_shared_lock(&paths.lock(), || {
let Some(manifest) = read_manifest_opt(&paths)? else {
return Ok(None);
};
let counts = Counts {
nodes: count_jsons(&paths.nodes_dir()),
discussions: count_jsons(&paths.discussions_dir()),
spinoffs: count_jsons(&paths.spinoffs_dir()),
};
// Probe supervisor liveness INSIDE the shared lock: the whole point of
// the field is to let a caller reason "status pending + supervisor dead
// => orphaned", so `manifest.status` and `supervisor` are a single
// decision and must be read as one consistent snapshot (design.md §4).
// Reading the pid file outside the lock could emit a
// `{status: pending, supervisor: dead}` pair that never actually existed
// (the supervisor may roll status up and remove its pid file between the
// two reads).
let supervisor = SupervisorView::probe(&paths);
// Read the reporting node ONCE inside the shared-lock window, alongside
// the manifest/counters, so the `landed` git-verification inputs and the
// `recoverable_work` signal are a single consistent snapshot with
// `manifest.status` (state-integrity invariant 3). The git shell-out that
// turns these inputs into `landed` runs AFTER the lock is released — never
// hold the flock across a subprocess.
let node_id =
NodeId::parse_str(DEFAULT_NODE_ID).expect("DEFAULT_NODE_ID is a valid node id");
let node = read_node_opt(&paths, &node_id)?;
// `recoverable_work` is gated on a `failed` status: the supervisor only
// stamps the block on the failed-synthesis path, so a block on a
// non-failed report is stale/spoofed (unknown report fields are permitted
// by the validator) and is not surfaced. A run with no `n-0001` node (or
// no such block) simply yields `None`.
let recoverable_work = if matches!(manifest.status, Status::Failed) {
node.as_ref()
.and_then(|n| n.last_report.as_ref())
.and_then(|r| r.get("recoverable_work").cloned())
.filter(Value::is_object)
} else {
None
};
let landing = LandingFields {
source_repo: manifest.source_repo.clone(),
source_branch: manifest.source_branch.clone(),
worktree_path: node.as_ref().and_then(|n| n.worktree_path.clone()),
branch: node.as_ref().and_then(|n| n.branch.clone()),
base_sha: node.as_ref().and_then(|n| n.base_sha.clone()),
report: node.and_then(|n| n.last_report),
};
Ok(Some((
manifest,
counts,
supervisor,
recoverable_work,
landing,
)))
})
.map_err(from_core)?;
let (manifest, counts, supervisor, recoverable_work, landing) = match scanned {
Some(v) => v,
None => {
return Err(
CliError::user("run_not_found", format!("no run with id {run_id}"))
.with_invalid_value(run_id),
);
}
};
// Git-verified `landed` (issue `landing-signal-reliable-after-rebase`),
// computed outside the shared lock: the rebase-robust signal a caller should
// trust instead of hand-rolling `git merge-base --is-ancestor`.
let signal = crate::run::landed::landing_signal(
&crate::run::landed::LandingInputs {
source_repo: landing.source_repo.as_deref(),
source_branch: landing.source_branch.as_deref(),
worktree_path: landing.worktree_path.as_deref(),
branch: landing.branch.as_deref(),
base_sha: landing.base_sha.as_deref(),
report: landing.report.as_ref(),
},
&crate::supervise::cleanup::git_bin(),
);
let payload = ShowPayload {
manifest: ManifestView::from(&manifest).with_supervisor(supervisor),
counts,
landed: signal.landed,
landed_method: signal.method.wire(),
recoverable_work,
};
match spec.format {
OutputFormat::Json | OutputFormat::Jsonl => {
output::emit_envelope(&payload, spec, warnings)?;
}
OutputFormat::Text => {
println!("run-id: {}", payload.manifest.run_id);
println!(
"title: {}",
output::escape_one_line(payload.manifest.title)
);
println!("status: {}", payload.manifest.status);
println!(
"landed: {} ({})",
payload.landed, payload.landed_method
);
println!("kind: {}", payload.manifest.kind);
println!("lifecycle: {}", payload.manifest.lifecycle);
println!("created_at: {}", payload.manifest.created_at);
println!("updated_at: {}", payload.manifest.updated_at);
println!("nodes: {}", payload.counts.nodes);
println!("discussions: {}", payload.counts.discussions);
println!("spinoffs: {}", payload.counts.spinoffs);
match payload.manifest.supervisor.pid {
Some(pid) if payload.manifest.supervisor.alive => {
println!("supervisor: pid {pid} (alive)");
}
Some(pid) => println!(
"supervisor: pid {pid} (dead — run `orchestratectl run reattach {}` to recover)",
payload.manifest.run_id
),
None => println!("supervisor: (none recorded)"),
}
if let Some(line) =
crate::run::wait::recoverable_summary(payload.recoverable_work.as_ref())
{
println!("recoverable: {line}");
}
output::emit_text_warnings(warnings);
}
}
Ok(())
}
fn count_jsons(dir: &std::path::Path) -> u64 {
let entries = match std::fs::read_dir(dir) {
Ok(e) => e,
Err(_) => return 0,
};
entries
.filter_map(Result::ok)
.filter(|e| {
e.path()
.extension()
.and_then(|s| s.to_str())
.is_some_and(|s| s.eq_ignore_ascii_case("json"))
})
.count() as u64
}