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
//! Shared pane-identity cross-check for `TM_MANAGED_SESSION_ID` consumers
//! (issue #3600).
//!
//! Why: `TM_MANAGED_SESSION_ID` is published via `tmux set-environment -t
//! <session>` (`runtime::claude_code::session_id_export_prefix`,
//! `session_manager::manager`) — SESSION-scoped, not pane-scoped. tmux
//! applies a session's stored environment to every NEW pane/window created
//! afterward in that same tmux session, so a sibling pane opened after a
//! runtime-exit heal inherits an IDENTICAL `TM_MANAGED_SESSION_ID` and would
//! wrongly believe it IS that session. `guided.rs`'s nested-session guard hit
//! this exact hijack (the "#2456 CORRECTION", see its module doc) and closed
//! it by comparing tmux's own stable `pane_id` (never inherited across
//! panes, unlike an env var or `pane_pid`) against the resolved record's OWN
//! captured `pane_id` before ever trusting the env var for a destructive
//! action. Two more CLI consumers read the bare env var with NO such check
//! (`commands::rename::session_rename`'s in-session form,
//! `commands::pm_guard_deny_by_default::persona_status`) — issue #3600. This
//! module is the ONE shared primitive all three now use, so the guard cannot
//! rot back into two divergent (or missing) copies.
//! What: [`pane_identity_confirmed`] is the pure bool predicate originally
//! defined in `guided.rs` (moved here unchanged, still re-exported from
//! there so its existing call sites/imports keep resolving).
//! [`EnvSessionIdentity`] is a richer three-way outcome
//! (`Confirmed`/`Mismatch`/`Unavailable`) for callers — `rename.rs` and
//! `pm_guard_deny_by_default.rs` — that must render a SPECIFIC refusal
//! message (naming both the env-var session and the actual pane) rather than
//! a bare bool.
//! Test: `pane_identity_confirmed_true_when_pane_id_matches_record`,
//! `pane_identity_confirmed_false_when_current_pane_id_absent`,
//! `pane_identity_confirmed_false_when_record_pane_id_absent`,
//! `pane_identity_confirmed_false_when_pane_ids_differ`,
//! `pane_identity_confirmed_false_when_inherited_env_but_different_pane_id`
//! (moved from `guided.rs`'s own test file, `tests_behavior_c_tests.rs`);
//! `env_session_identity_confirmed_when_pane_ids_match`,
//! `env_session_identity_mismatch_when_pane_ids_differ`,
//! `env_session_identity_unavailable_when_current_pane_id_absent`,
//! `env_session_identity_unavailable_when_record_pane_id_absent`.
/// Confirm the CURRENT tmux pane is provably the one bound to a resolved
/// session record, by comparing tmux's own stable `pane_id` — never
/// `TM_MANAGED_SESSION_ID` alone.
///
/// Why: a process-level env-var comparison was tried and EMPIRICALLY
/// DISPROVEN (live tmux 3.6b, #2456 review finding 1 round 2): the
/// runtime-exit healing step calls `tmux set-environment -t <session>` —
/// SESSION-scoped — and tmux applies a session's stored environment to the
/// process env of every NEW pane/window created in that session AFTERWARD.
/// Any session that has exited-and-relaunched once therefore has a
/// "poisoned" session env that a genuinely different sibling window
/// inherits into its OWN process env — an env-based gate would wrongly pass
/// that sibling. tmux's own `pane_id` (e.g. `"%5"`, distinct from
/// `pane_pid`, which the OS can reuse across a pane's lifetime) is NEVER
/// inherited across panes.
/// What: `true` only when BOTH `current_pane_id` and `record_pane_id` are
/// `Some` and equal. `false` when either is `None` (a legacy record that
/// never had a `pane_id` captured, or a tmux query failure) OR they differ —
/// fails CLOSED in every ambiguous case, meaning the caller must refuse (or
/// fall back to a weaker, explicit confirmation) rather than proceed.
/// Test: `pane_identity_confirmed_true_when_pane_id_matches_record`,
/// `pane_identity_confirmed_false_when_current_pane_id_absent`,
/// `pane_identity_confirmed_false_when_record_pane_id_absent`,
/// `pane_identity_confirmed_false_when_inherited_env_but_different_pane_id`
/// (the exact two-idle-panes/inherited-session-env hijack scenario).
pub
/// Three-way outcome of cross-checking an env-var-resolved session against
/// this process's own CURRENT tmux pane (issue #3600).
///
/// Why: [`pane_identity_confirmed`]'s bare bool is enough for `guided.rs`'s
/// own gate (which only ever needs "safe to proceed or not"), but a CLI
/// command that REFUSES on failure (`rename`) or gates on it
/// (`pm_guard_deny_by_default`) should tell the operator WHY: a genuine
/// cross-pane mismatch (naming both pane ids) is a different, more alarming
/// situation than simply being unable to determine identity at all (not
/// inside tmux, a legacy record, or a daemon/tmux query failure).
/// What: [`Self::evaluate`] is the pure classifier; [`Self::is_confirmed`] is
/// the single collapse back to a pass/fail decision for callers that don't
/// need the distinction.
/// Test: `env_session_identity_confirmed_when_pane_ids_match`,
/// `env_session_identity_mismatch_when_pane_ids_differ`,
/// `env_session_identity_unavailable_when_current_pane_id_absent`,
/// `env_session_identity_unavailable_when_record_pane_id_absent`.
pub