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
//! `SessionRegistry::bind_workstream` (DOC-48 §4.1/§5.3, issue #3298). Split
//! out of `registry.rs` for the same 500-SLOC-cap reason as
//! `registry_events.rs`/`registry_goals.rs` — this is a child module of
//! `registry` (declared via `#[path = ...] mod workstream_binding;`), so it
//! shares full access to `SessionRegistry`'s private `lock`/`record` helpers
//! and `SessionEntry`'s fields exactly as if this method were still defined
//! in that file.
//!
//! Why: `session::protocol::create` and `task::protocol::task_run` both need
//! to stamp a freshly-minted session's immutable workstream binding once
//! they've resolved it (either an explicit `workstream_id` param, §4.1, or
//! §4.2's ambient active-workstream default) and PERSISTED it into
//! [`crate::workstreams::store::WorkstreamStore`] (via
//! `WorkstreamStore::bind_session`) — this is the registry-side half that
//! also makes the binding observable on the `Session` snapshot
//! (`session.status`/`session.list`) and publishes the DOC-48 §5.3
//! `SessionAdded` event.
//!
//! Test: `registry_tests::bind_workstream_sets_field_and_publishes_session_added`,
//! `registry_tests::bind_workstream_unknown_session_errors`.
use super::*;
impl SessionRegistry {
/// Stamp a freshly-minted session's immutable workstream binding, and
/// publish `Event::SessionAdded`.
///
/// Why: `session::protocol::create` and `task::protocol::task_run` are
/// the ONLY callers, each calling this at most once — immediately after
/// `Self::create` mints the session's id and the caller has separately
/// persisted the binding into the workstream store — so there is no
/// setter that changes `Session.workstream_id` a second time; this
/// method itself does not enforce that (it would silently overwrite),
/// the immutability guarantee comes from the call sites never invoking
/// it twice for the same session (a REUSED session on `task.run` never
/// calls this again — see `task::protocol::task_run`'s docs on the
/// mismatch rejection it performs instead).
/// What: `Err(session_not_found)` if `id` is unknown; otherwise sets
/// `entry.session.workstream_id = Some(workstream_id)` and records+
/// publishes `Event::SessionAdded { session_id, workstream_id,
/// binding_time }` through the normal per-session ring buffer/live bus
/// (`Self::record`) — see that event's own docs for why this is what
/// lets `crate::workstreams::sse::aggregate_live`'s dynamic membership
/// re-check pick it up with no changes.
pub fn bind_workstream(
&self,
id: &str,
workstream_id: crate::workstreams::model::WorkstreamId,
) -> Result<(), RpcError> {
{
let mut sessions = self.lock();
let entry = sessions
.get_mut(id)
.ok_or_else(|| RpcError::session_not_found(id))?;
entry.session.workstream_id = Some(workstream_id);
}
self.record(
id,
Event::SessionAdded {
session_id: id.to_string(),
workstream_id: workstream_id.to_string(),
binding_time: Utc::now(),
},
);
Ok(())
}
/// Record+publish `Event::SessionActivityUpdate` for `id` — a no-op for
/// an UNBOUND session.
///
/// Why: `Self::set_run_outcome`'s sole activity-signal hook (module
/// docs) — split out here purely so that method's own diff stays small
/// enough for `registry.rs`'s 500-SLOC cap; behaviourally this is still
/// just `Self::record` with the event's shape.
/// What: `activity` is `None` when the session is not workstream-bound
/// — publish NOTHING, keeping an unbound session's event stream
/// byte-identical to the pre-#3298 baseline the M1 cutline e2e contract
/// freezes (see `set_run_outcome`'s docs for the full rationale) — and
/// `Some(has_running_task)` when it is. `last_turn_at` is stamped
/// `Utc::now()` at call time; the caller computes `activity` while
/// already holding the lock (reading `Session.workstream_id` and
/// `entry.execution`), so this helper does not re-lock.
pub(super) fn publish_activity_update(&self, id: &str, activity: Option<bool>) {
let Some(has_running_task) = activity else {
return;
};
self.record(
id,
Event::SessionActivityUpdate {
session_id: id.to_string(),
last_turn_at: Utc::now(),
has_running_task,
},
);
}
}
#[cfg(test)]
mod tests {
use super::*;
/// `bind_workstream` must set the field and publish `SessionAdded` on
/// the session's own ring buffer/live bus.
#[tokio::test]
async fn bind_workstream_sets_field_and_publishes_session_added() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, ProjectBinding::None);
let ws_id = crate::workstreams::model::WorkstreamId::new();
registry.bind_workstream(&session.id, ws_id).expect("bind");
let status = registry.status(&session.id).expect("status");
assert_eq!(status.workstream_id, Some(ws_id));
let events = registry.replay(&session.id).expect("replay");
assert!(events.iter().any(|e| matches!(
&e.event,
Event::SessionAdded { workstream_id, .. } if *workstream_id == ws_id.to_string()
)));
}
/// An unknown session id must map to `session_not_found`.
#[tokio::test]
async fn bind_workstream_unknown_session_errors() {
let registry = SessionRegistry::new();
let ws_id = crate::workstreams::model::WorkstreamId::new();
let err = registry.bind_workstream("nope", ws_id).unwrap_err();
assert_eq!(err.code, -32007);
}
}