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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! `SessionStart`/`SessionEnd` hook correlation: linking a Claude Code session
//! UUID to the right managed session record (#1744, #4337).
//!
//! Why: `api.rs` was at its frozen 1272-SLOC allowlist budget
//! (`.line-cap-allowlist.tsv`); adding the #4337 re-sync logic there would
//! have breached it. This cluster — `correlate_session_start`,
//! `handle_session_end`, and the pure `session_end_pane_still_live` gate it
//! uses — is cohesive and self-contained (both hook handlers, called only
//! from `ingest_hook`), so it moves out wholesale, mirroring how
//! `control_routes.rs` / `claude_config_routes.rs` already keep `api.rs`
//! focused on routing.
//! What: two `pub(super)` async fns wired into `api::ingest_hook`'s
//! `SessionStart`/`SessionEnd` branches, plus the private pure liveness gate.
//! Test: the `#[cfg(test)]` suite in `api_tests.rs` (brought into scope there
//! via `api.rs`'s `use session_start_correlation::*;` and `use super::*;`).
use Arc;
use crateDaemonState;
/// Link a `SessionStart` Claude session UUID to the right managed session record.
///
/// Why (#1744): when `resume` calls `spawn_resume` with `--resume <id>`, it needs
/// the Claude Code internal session UUID persisted on the `SessionRecord`. The only
/// reliable source is the `SessionStart` hook, which fires as Claude Code starts
/// and delivers `CLAUDE_SESSION_ID` via the environment. The hook handler
/// (`tm hook`) now also embeds the caller's `cwd` in the payload (issue #1744);
/// this function uses that `cwd` to find the Active managed session running in that
/// directory and persists the UUID.
/// What: extracts `cwd` from `payload["cwd"]`; canonicalizes it and each record's
/// `workspace_path`/`cwd` (falling back to raw path on error so macOS
/// `/private/tmp` ↔ `/tmp` symlinks resolve); finds Active managed sessions whose
/// path matches. If more than one Active session matches the cwd (ambiguous) the
/// correlation is skipped with a warning to avoid mis-attribution. Single match →
/// `SessionManager::set_claude_session_id`. Best-effort — failures are logged and
/// silently swallowed so a missing correlation never blocks the hook response.
///
/// # Subagent overwrite guard + re-sync (#4337)
///
/// A subagent dispatched from a managed session's own pane (native `Task`/
/// `Agent` tool, or any other `claude` invocation that happens to share the
/// PM's cwd) is its OWN top-level Claude Code process from Claude Code's point
/// of view: it gets its own internal session UUID and fires its own
/// `SessionStart`, which lands here with the SAME `cwd` as the PM's managed
/// session — because trusty-mpm never registers a subagent as its own managed
/// session, cwd-matching alone finds exactly one (the PM's) record, the
/// pre-#4337 ambiguity guard above never triggers, and the subagent's id
/// could silently clobber the PM's own. The next in-place `--resume` would
/// then resume the SUBAGENT's transcript instead of the PM's conversation.
///
/// The fix: once a record already carries a `claude_session_id`, a
/// `SessionStart` reporting a DIFFERENT id for the same cwd is accepted ONLY
/// when the STORED id no longer resolves to a live session transcript
/// ([`stored_id_still_live`], reusing the exact staleness check
/// `runtime::claude_code::session_id_exists` the resume path already trusts
/// for the same question) — otherwise it is refused and logged. A subagent's
/// own transcript DOES exist and is live for as long as it runs, so its
/// report is refused; a genuinely stale id (the prior conversation's
/// transcript was pruned, or it never made it to disk) is accepted, so the
/// record can recover. The common case — the prior conversation cleanly
/// ending — is handled even earlier and more precisely by
/// [`handle_session_end`] clearing the field outright (and by
/// `SessionManager::mark_reactivated`, #4337, doing the same on an in-place
/// reactivate), so this staleness check is the residual, narrower safety net
/// for when that clear did not happen (a crash, a missed hook, or a request
/// that raced ahead of it).
/// Test: `session_start_hook_correlates_claude_id`,
/// `session_start_hook_still_refuses_a_live_subagent_id`,
/// `session_start_hook_reasserting_same_id_is_a_noop`,
/// `session_start_hook_re_correlates_a_stale_id` in `api_tests.rs`.
pub async
/// Whether a previously-stored `claude_session_id` still resolves to a live
/// Claude Code session transcript for `cwd` (#4337 re-sync gate).
///
/// Why: reuses `runtime::claude_code::session_id_exists` — the SAME
/// staleness check `spawn_resume` already trusts before honoring a stored id
/// with `--resume` — so the two call sites can never disagree about what
/// "stale" means. A subagent's transcript exists and is live for as long as
/// it runs, so this returns `true` for it; a genuinely stale id (pruned,
/// moved, or never flushed to disk) returns `false`.
/// What: resolves the managed `CLAUDE_CONFIG_DIR` via
/// [`crate::core::trusty_tools_config::managed_claude_config_dir`] and
/// delegates to [`crate::runtime::session_id_exists`].
/// Test: `session_start_hook_re_correlates_a_stale_id`,
/// `session_start_hook_still_refuses_a_live_subagent_id` in `api_tests.rs`.
/// Pure decision: should `handle_session_end` DEFER its `Stopped` transition
/// because the record's own tmux pane still shows a live runtime? (#2454)
///
/// Why: `SessionEnd` firing is Claude Code self-reporting that its session is
/// "ending / being torn down" ([`crate::core::hook::HookEvent::SessionEnd`])
/// — NOT proof the OS process (and therefore the tmux pane) has already fully
/// exited; the hook POST can race the pane's actual fall-back to a bare idle
/// shell. `SessionManager::resume` decides pane reuse purely on
/// `tmux.session_exists()`, so flipping the record to `Stopped` while the
/// pane is still occupied could let a `resume` (manual or the auto-resume
/// supervisor) type into a pane the outgoing runtime hasn't relinquished yet.
/// This reuses the EXACT SAME classification the 60-second runtime-exit
/// reaper already trusts for this question —
/// [`crate::daemon::runtime_reap::session_has_live_pane`] (any-pane-live
/// aggregation over the idle-shell allowlist and the [`ChildLivenessProbe`]
/// fail-closed gate) — instead of inventing a second one, so the two call
/// sites can never disagree about what "exited" means.
/// What: delegates directly to
/// [`session_has_live_pane`](crate::daemon::runtime_reap::session_has_live_pane),
/// which returns `true` (defer) when ANY pane whose `session_name` matches
/// `tmux_name` is NOT yet exited. Fixed by #2463: this previously used
/// `panes.iter().find(...)` — the FIRST matching pane only — which
/// misclassified a manually-split, multi-pane managed session as idle
/// whenever `tmux list-panes -a` happened to return an idle pane ahead of a
/// live one. When no matching pane is found (already gone, or tmux/pane
/// enumeration was unavailable and `panes` is empty) this returns `false`
/// (proceed) — there is nothing to protect from a destructive teardown here,
/// since `mark_runtime_exited_stopped` never touches the pane either way; a
/// genuinely vanished session is left to the #1744 reaper as before. Pure —
/// no I/O — so it is unit-testable without a live tmux.
/// Test: `session_end_pane_still_live_true_for_running_agent`,
/// `session_end_pane_still_live_false_for_idle_shell`,
/// `session_end_pane_still_live_false_when_pane_missing`,
/// `session_end_pane_still_live_true_when_any_of_multiple_panes_live`,
/// `session_end_pane_still_live_false_when_all_of_multiple_panes_idle` in
/// `api_tests.rs`.
pub
/// Immediately mark a managed session Stopped on `SessionEnd`, WITHOUT killing
/// its tmux pane (#2454).
///
/// Why (#1744, revised #2454): without this, a managed session that exits
/// ungracefully (tmux pane killed, terminal closed) stays `Active` in the store
/// until the 60-second reap loop fires. On `SessionEnd`, Claude Code's internal
/// session has already ended; marking the managed session `Stopped` immediately
/// keeps the daemon's view consistent and lets the operator see the correct
/// state right away. This originally called [`SessionManager::stop`], which is
/// the EXPLICIT-stop contract (`tm session stop`) and therefore also
/// `graceful_terminate_runtime`s / `kill_session`s the tmux pane — but a
/// `SessionEnd` correlation is a self-healing state reconcile, not a
/// human/client teardown request, exactly like the 60-second runtime-exit
/// reaper (see [`crate::daemon::runtime_reap`] #2023 A). Routing it through
/// `stop` could destroy a pane the operator was still attached to, purely
/// because the daemon correlated the exit a few hundred milliseconds before
/// they noticed. This now mirrors the reaper and calls
/// [`SessionManager::mark_runtime_exited_stopped`] instead, leaving the pane
/// alive. It also gates that transition behind
/// [`session_end_pane_still_live`] (#2454 follow-up): the hook can race the
/// pane's actual teardown, so if the pane is found and still shows a live
/// runtime the transition is DEFERRED for this hook receipt — the 60-second
/// reaper will pick the session up once its pane is genuinely idle. This gate
/// is best-effort: it needs a real `tmux` binary to enumerate panes, so on a
/// host with no `tmux` (or a `list_managed_panes` failure) it fails OPEN and
/// proceeds with the transition unchecked, same as pre-gate behavior.
///
/// #4337: BEFORE that pane-liveness gate, this ALSO clears the record's
/// `claude_session_id` via `SessionManager::clear_claude_session_id_if` — an
/// exact-match, race-safe clear (a concurrent fresher correlation is never
/// clobbered). `SessionEnd` is Claude Code's own unambiguous signal that
/// THIS conversation ended, independent of whether the pane still shows a
/// live runtime (that gate is about the TMUX PANE, not the Claude session),
/// so the id is stale from this point on regardless of the deferral below.
/// Clearing it here means the record's NEXT `SessionStart` — the real
/// relaunch — takes the plain "no existing id" branch in
/// [`correlate_session_start`] rather than depending on a later subagent's
/// report ever satisfying the narrower stale-transcript re-sync gate.
/// What: searches Active managed sessions for one whose `claude_session_id`
/// matches the hook's `session_id`. If found, clears that id (best-effort),
/// then best-effort discovers the live tmux panes
/// ([`crate::daemon::tmux::TmuxDriver::discover`] + `list_managed_panes`) and
/// calls [`session_end_pane_still_live`]; when that reports the pane is
/// still live, logs and returns WITHOUT transitioning. Otherwise calls
/// `SessionManager::mark_runtime_exited_stopped`, which marks the record
/// `Stopped` and persists WITHOUT calling `graceful_terminate_runtime` /
/// `kill_session`. Best-effort — failures are logged and swallowed so the
/// hook response is unaffected.
/// Test: `session_end_hook_marks_managed_stopped` and
/// `session_end_hook_does_not_kill_pane` in `api_tests.rs` cover the
/// transition itself (tmux is unavailable/pane-not-found in that harness, so
/// the gate fails open); the gate's own decision logic is covered by the
/// `session_end_pane_still_live_*` unit tests above;
/// `session_end_hook_clears_claude_session_id` covers the #4337 clear;
/// `an_http_session_end_leaves_the_sessions_delegations_live_8980` covers
/// that this function stales no delegation.
pub async