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
//! Per-session prompter accounting and the `session.attach` forwarder that
//! holds one such claim (#8100). A child module of `registry`, split out for
//! the same 500-SLOC-cap reason as `events`.
//!
//! Why: an `ask` permission rule may only suspend a run when SOMEONE can see
//! the prompt for THAT session and answer it. The first cut of #8100 asked
//! `crate::events::bus().receiver_count()`, which is daemon-global: one
//! console watching one PM session marked every headless `task.run` sub-agent
//! as watched, restoring the 300 s stall the fix exists to remove.
//! What: [`PrompterGuard`], an RAII claim on one session's prompter count,
//! and the `claim_prompter`/`prompter_count` pair that mints and reads it. A
//! guard released by ANY means — detach, a dead connection, a dropped SSE
//! body, a panicking task — decrements, so a client that dies mid-ask stops
//! counting without anyone calling a teardown.
//! Test: `registry_tests::prompter_attached_while_a_session_attachment_lives`,
//! `registry_tests::prompter_claim_is_released_when_its_guard_drops`,
//! `registry_tests::prompter_attached_for_an_unknown_session_is_false`,
//! `registry_tests::prompter_count_survives_a_poisoned_registry_lock`.
use ;
use *;
/// One live prompter's claim on one session, released when dropped (#8100).
///
/// Why: every prompter path ends in a different way — `session.detach` fires
/// a cancel channel, an HTTP SSE body is dropped when the socket closes, a
/// `session.events` stream ends when its receiver goes. A guard whose `Drop`
/// does the decrementing needs none of them to agree on a teardown call, and
/// covers the case the timeout was hiding: a client that dies mid-ask.
/// What: holds the session's counter directly rather than a handle to the
/// registry, so releasing it never re-enters the registry lock and a session
/// removed from the map while a client is still attached cannot strand a
/// count on an entry that no longer exists.
/// Test: `registry_tests::prompter_claim_is_released_when_its_guard_drops`.
;
/// Spawn the background task that forwards one session's live event
/// envelopes to a connection until cancelled or the connection is gone.
///
/// Why: split out of `attach` for readability; also gives `registry_tests`
/// a single well-named spawn site to reason about. Subscribing to the bus
/// SYNCHRONOUSLY here — before `tokio::spawn` schedules the forwarder body
/// — matters: if the subscription happened inside the spawned `async`
/// block instead, a caller that calls `send()` immediately after `attach()`
/// returns could publish its event before the spawned task ever gets polled
/// and subscribes, and `broadcast::Receiver`s only see events published
/// after `subscribe()` was called — the event would be silently missed.
/// What: subscribes to `crate::events::subscribe()` immediately, then
/// spawns a task that forwards envelopes whose `session_id` matches
/// `session_id` as a JSON-RPC notification via `notify` (`params` is the
/// envelope itself — `seq`/`at`/`kind`/`event` all present), skips
/// envelopes for other sessions and lag gaps, and exits on `cancel_rx`
/// firing (either a real detach or the sender being dropped) or
/// `notify.send` failing (connection gone). #8100: `prompter` rides the
/// spawned task, so this connection stops counting as a prompter the moment
/// the loop ends, however it ends.
/// Test: `registry_tests::attach_forwards_live_events_until_detach`,
/// `registry_tests::prompter_attached_while_a_session_attachment_lives`.
pub