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
//! The session-control seam the SM drives its fleet through (DOC-14 §2.6).
//!
//! Why: SM-8's delegation loop (§3.4) and the SM-STDIO adapter (§1A.1) both need
//! to drive managed sessions — launch, observe, send, stop — WITHOUT carrying
//! session-lifecycle business logic and WITHOUT a real tmux/workspace in tests.
//! Hiding that surface behind a narrow trait (Dependency Inversion) lets the
//! delegation loop and the dispatcher depend on the trait while production wires
//! the real `DaemonSessionControl` (in `daemon::sm_stdio`) and tests inject a
//! deterministic mock. SM-STDIO originally defined this trait under
//! `daemon::sm_stdio`; SM-8 RELOCATES it here (a feature-independent core
//! location) so the agent-side delegation loop can depend on it too, with
//! `sm_stdio` re-exporting it so its public surface is unchanged.
//! What: [`LaunchParams`] (the launch inputs), [`SessionControlError`] (the typed
//! failure), and [`SessionControl`] — the seven async verbs the spec's session
//! methods need, each returning a plain `serde_json::Value` (the wire result
//! body) or a [`SessionControlError`]. The production `DaemonSessionControl`
//! impl lives in `daemon::sm_stdio::control` (it needs daemon-internal handles);
//! the agent-side mock lives in `agent/delegate/mock_control.rs`.
//! Test: the dispatcher's `sm.sessions.*` round-trips and the SM-8 delegation
//! tests both inject mock impls of this trait.
use async_trait;
use ;
use crateActivityState;
/// Inputs for `sessions.launch` (§1A.1: `{ workdir, model?, prompt?, goal_id? }`).
///
/// Why: the spec's launch params differ from the managed-session `SpawnParams`
/// shape (`{ repo_url, ref, task }`); keeping the adapter's own owned input
/// struct lets callers (the dispatcher AND the SM-8 delegation loop) parse the
/// spec params once and hand them to ANY [`SessionControl`] impl (real or mock)
/// without re-deriving the mapping at the call site.
/// What: `workdir` (the directory/repo the session launches against, mapped to
/// the managed `repo_url`), an optional `model` (runtime selector), an optional
/// `prompt` (the task/intent, mapped to the managed `task`), an optional
/// `goal_id` the caller wants the new session linked to (§9.3), an optional
/// `repo_url` (the canonical repository URL for git-backed provisioning, WI-A
/// #1585), and an optional `ref_` (the git branch/tag/SHA to check out, WI-A
/// #1585).
/// Test: `tests.rs::launch_round_trips` and `delegate` tests construct this;
/// `chat_action_tests.rs::execute_launch_threads_repo_url_and_ref_` covers the
/// new fields.
/// A failure surfaced by a [`SessionControl`] operation.
///
/// Why: callers map these onto JSON-RPC error responses (the dispatcher) or onto
/// goal-link failures (the SM-8 loop); a typed enum lets them choose
/// `INVALID_PARAMS` (bad id) vs `INTERNAL_ERROR` (control failure) by VARIANT
/// rather than string-matching, and keeps the adapter panic-free per the
/// workspace convention.
/// What: [`NotFound`](SessionControlError::NotFound) is reserved for a malformed
/// session id (UUID parse failure) OR a genuinely-absent session
/// (`ManagedError::SessionNotFound`); [`Backend`](SessionControlError::Backend)
/// for any other control-surface failure (provision/tmux/store/invalid-state).
/// Test: `sm_stdio::tests` mock returns both variants; the dispatcher asserts the
/// mapped JSON-RPC code.
/// How an injected text is committed to a session's runtime (#1461).
///
/// Why: the harness-agnostic `inject_text` verb must express the THREE distinct
/// keystroke intents every runtime needs without leaking tmux argv into the
/// trait: "type this line and run it", "type this but DON'T submit" (e.g. stage
/// a partial command, or fill a prompt the human will edit), and "interrupt the
/// running task". Modelling them as one closed enum keeps the seam runtime-neutral
/// — a future tcode/SDK backend maps the same three variants onto its own
/// submit/interrupt primitives.
/// What: [`Enter`](Submit::Enter) sends the literal text then a newline (today's
/// `send_line`); [`NoSubmit`](Submit::NoSubmit) sends the literal text with NO
/// trailing newline; [`Interrupt`](Submit::Interrupt) ignores any text and sends
/// the runtime's interrupt (Ctrl-C in tmux). Serde-friendly (snake_case) so it
/// round-trips through the wire/config.
/// Test: `inject_dispatch_enter_sends_literal_then_enter`,
/// `inject_dispatch_nosubmit_sends_literal_only`,
/// `inject_dispatch_interrupt_sends_ctrl_c` in tests/session_control_api.rs.
/// A raw, LLM-free observation of a session's current surface (#1461).
///
/// Why: `observe` must ALWAYS be available — even on a host with no
/// `OPENROUTER_API_KEY` — so any harness can read a session's actual pane and
/// make its OWN inference (the session-manager-driver does exactly this). It is
/// the cheap, deterministic counterpart to [`Summary`]: no tokens, no network.
/// What: the captured `raw_pane` (last N lines), `runtime_active` (is the
/// runtime process still live in the pane?), and the two escalation fields the
/// managed surface already tracks — `pending_decision` (a question awaiting a
/// human) and its `proposed_default` answer.
/// Test: `observe_returns_raw_pane_without_llm`,
/// `observe_reports_runtime_active` in tests/session_control_api.rs.
/// An optional, LLM-derived summary of a session's activity state (#1461).
///
/// Why: `summarize` is the OPTIONAL, inference-backed counterpart to `observe`.
/// It MUST degrade gracefully: on a host with no `OPENROUTER_API_KEY` it returns
/// `state = Unknown` and `summary = None` rather than erroring, preserving the
/// LLM-optional invariant the activity monitor already guarantees.
/// What: reuses the existing [`ActivityState`] enum (NOT a duplicate) for the
/// classified `state`; `summary` is the human-readable note (None when no
/// genuine verdict was produced — i.e. degraded/unconfigured); `confidence` is
/// the classifier's `[0.0, 1.0]` score (0.0 when degraded).
/// Test: `summarize_degrades_to_unknown_without_key`,
/// `summarize_returns_fake_verdict`, `summarize_serves_cached_verdict` in
/// tests/session_control_api.rs.
/// The narrow session-control surface the SM drives its fleet through (§2.6).
///
/// Why: keeps both `sm.sessions.*` (a thin translation) AND the SM-8 delegation
/// loop decoupled from the concrete managed-session surface. Callers depend on
/// this trait (Dependency Inversion) so production wires `DaemonSessionControl`
/// (the real managed-session surface) while tests wire a deterministic mock with
/// NO tmux/workspace. Every method returns the wire `result` body directly so the
/// dispatcher does no shaping.
/// What: `launch` → `{ session_id }`; `list` → `{ sessions: [...] }`; `get` →
/// the full record JSON; `send` → `{ ok }`; `stop`/`resume`/`kill` → `{ ok }`.
/// All are `Send + Sync` to live behind `Arc<dyn SessionControl>`.
/// Test: `sm_stdio::tests` mock; the SM-8 `delegate` tests mock; the real
/// `DaemonSessionControl` via the managed integration tests.