trusty-code 0.5.0

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! [`TcodeConnector`] — tcode's [`WorkstreamConnector`] implementation
//! (DOC-44 twin Phase 1, issue #3007).
//!
//! Why: DOC-44 §5.2 assigns tcode's connector to wrap the #2983 REST bridge
//! (`crate::serve::rest`) rather than reimplementing `session.*` business
//! logic — every REST route already just forwards to the identical
//! JSON-RPC method (`crate::session::protocol`) both `serve --stdio` and
//! `serve --http POST /rpc` dispatch through (see `crate::serve::rest`
//! module docs), so this connector calling REST is provably the SAME code
//! path a `POST /rpc` caller would hit. `attach` is the one exception —
//! there is no `GET`/`POST` REST route for `session.attach` (the vision spec
//! §4.4 SSE route, `GET /sessions/{id}/events`, is the real HTTP
//! live-streaming mechanism; `session.attach` itself only returns the
//! ring-buffer replay + that stream's URL) — so `attach` goes over
//! `POST /rpc` directly, exactly as `tests/session_e2e.rs`'s HTTP scenario
//! already does.
//! What: [`TcodeConnector::new`] targets the default `tcode serve --http`
//! port ([`crate::serve::DEFAULT_HTTP_PORT`], `7882`);
//! [`TcodeConnector::with_daemon_url`] targets an explicit daemon (every test
//! in `tests/connector_e2e.rs` uses this against a `--port 0` instance). Every
//! trait method's doc comment states the exact route/method it maps onto.
//! `delegate` is NOT implemented — see that method's docs (DOC-44 locked
//! decision 3).
//! Test: `tests/connector_e2e.rs` (a top-level integration test — trusty-code
//! has no `tests/support`-reachable unit-test seam for driving the REAL
//! `tcode` binary, so this mirrors `tests/session_e2e.rs`'s own placement)
//! drives every method against a real `tcode serve --http --port 0` process
//! via `support::spawn_http_daemon`.

use async_trait::async_trait;
use serde_json::{Value, json};
use trusty_agents_common::connectors::{
    AgentSpec, AttachHandle, BackendParams, ConnectorError, CreateSessionReq, DelegateHandle,
    SessionInfo, SessionStatus, WorkstreamConnector,
};

use super::model::Session;

/// tcode's [`WorkstreamConnector`] implementation over the #2983 REST bridge
/// (+ `POST /rpc` for `attach`).
///
/// Why/What: see module docs.
pub struct TcodeConnector {
    http: reqwest::Client,
    daemon_url: String,
}

impl TcodeConnector {
    /// Build a connector targeting `http://127.0.0.1:<DEFAULT_HTTP_PORT>`.
    ///
    /// Why: the common case for a caller that just wants "the local tcode
    /// daemon on its documented default port." Unlike tm, tcode has no
    /// lock-file/gateway discovery chain yet (DOC-44 §7.1 lists that as a
    /// tcode operational prerequisite, out of scope here) — this is a fixed
    /// default, matching how the CLI itself defaults `--port`.
    /// What: delegates to [`Self::with_daemon_url`] with
    /// `http://127.0.0.1:{DEFAULT_HTTP_PORT}`.
    /// Test: exercised indirectly — every `connector_e2e.rs` test builds via
    /// [`Self::with_daemon_url`] instead (an ephemeral `--port 0` instance),
    /// since asserting on this constructor's exact URL would require either
    /// binding the real default port (risking a collision with a developer's
    /// already-running `tcode serve`) or exposing `daemon_url` for
    /// inspection, which would leak an implementation detail no caller needs.
    pub fn new() -> Self {
        Self::with_daemon_url(format!(
            "http://127.0.0.1:{}",
            crate::serve::DEFAULT_HTTP_PORT
        ))
    }

    /// Build a connector targeting an explicit daemon URL.
    ///
    /// Why: tests need to target a `--port 0` ephemeral instance rather than
    /// the fixed default.
    /// What: stores `daemon_url` and a `reqwest::Client` carrying the daemon
    /// credential (#5439) as a DEFAULT header.
    ///
    /// A default header rather than a per-request one because this client
    /// talks to exactly one target — `daemon_url`, resolved once above — so
    /// there is no second destination it could reach, and the six request
    /// sites below cannot individually forget it. The header is marked
    /// sensitive so `reqwest`'s own `Debug` output redacts it. The
    /// never-off-loopback rule lives in
    /// `crate::tui_client::discovery::daemon_credential_for`, not here.
    pub fn with_daemon_url(daemon_url: impl Into<String>) -> Self {
        let daemon_url = daemon_url.into();
        Self {
            http: credentialed_client(&daemon_url),
            daemon_url,
        }
    }

    /// Build a connector presenting an explicitly supplied credential.
    ///
    /// Why: an integration test drives a daemon it spawned with its OWN
    /// isolated data directory (#5439's hermeticity fix), so the credential is
    /// not the one this process would resolve from disk. Passing it as an
    /// argument beats exporting `TCODE_DAEMON_TOKEN`, which is process-global
    /// and races the other tests in the same binary.
    /// What: identical to [`Self::with_daemon_url`] except the credential comes
    /// from the caller. The loopback gate still applies — a non-loopback
    /// `daemon_url` gets no header, whatever is passed here.
    /// Test: `tests/connector_e2e.rs`'s live-daemon arms.
    pub fn with_daemon_url_and_credential(daemon_url: impl Into<String>, credential: &str) -> Self {
        let daemon_url = daemon_url.into();
        let http = if trusty_common::daemon_token::url_targets_loopback(&daemon_url) {
            client_presenting(credential)
        } else {
            reqwest::Client::new()
        };
        Self { http, daemon_url }
    }
}

/// A `reqwest::Client` presenting the daemon credential for `daemon_url`.
///
/// Why: #5439 put every route behind a credential, so a connector built on a
/// bare client sees `401` on every call. Resolution is delegated to
/// `crate::tui_client::discovery::daemon_credential_for` so the "never send
/// the local token off loopback" rule has exactly one implementation.
/// What: falls back to a plain client when there is no credential to present
/// or the target is not loopback — the caller then reads the daemon's `401`
/// as an ordinary `ConnectorError`, which is a clearer failure than a client
/// that refused to be constructed.
/// Test: `create_session_full_lifecycle`,
/// `list_sessions_empty_fleet_returns_empty_vec` (both in
/// `tests/connector_e2e.rs`) exercise the credentialed path against a live
/// daemon; the loopback gate is covered by
/// `credential_is_withheld_from_a_non_loopback_url` in `tui_client::discovery`.
fn credentialed_client(daemon_url: &str) -> reqwest::Client {
    match crate::tui_client::discovery::daemon_credential_for(daemon_url) {
        Some(token) => client_presenting(&token),
        None => reqwest::Client::new(),
    }
}

/// A client sending `Authorization: Bearer <token>` on every request.
///
/// What: the header is marked sensitive so `reqwest`'s own `Debug` output
/// redacts it. A token that will not fit in a header value yields a plain
/// client rather than a panic — the caller then reads the daemon's `401`.
fn client_presenting(token: &str) -> reqwest::Client {
    let Ok(mut value) = reqwest::header::HeaderValue::from_str(&format!("Bearer {token}")) else {
        return reqwest::Client::new();
    };
    value.set_sensitive(true);
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert(reqwest::header::AUTHORIZATION, value);
    reqwest::Client::builder()
        .default_headers(headers)
        .build()
        .unwrap_or_else(|_| reqwest::Client::new())
}

impl Default for TcodeConnector {
    fn default() -> Self {
        Self::new()
    }
}

/// Map a non-success HTTP response onto a [`ConnectorError`] (shared by the
/// REST-route methods below).
///
/// Why: `rpc_error_to_status` (`crate::serve::rest::rpc_error_to_status`)
/// already maps every domain error code onto the right HTTP status
/// (404/400/403/500) before this connector ever sees the response — so
/// classifying purely by status code, rather than re-parsing the JSON-RPC
/// error envelope in the body, is sufficient and keeps this connector
/// decoupled from that internal mapping's exact error-code table.
/// What: 404 -> `NotFound`, 400 -> `InvalidRequest`, else -> `Backend`.
async fn map_error_response(resp: reqwest::Response) -> ConnectorError {
    let status = resp.status();
    let body = resp.text().await.unwrap_or_default();
    let message = if body.trim().is_empty() {
        status.to_string()
    } else {
        format!("{status}: {}", body.trim())
    };
    if status == reqwest::StatusCode::NOT_FOUND {
        ConnectorError::NotFound(message)
    } else if status == reqwest::StatusCode::BAD_REQUEST {
        ConnectorError::InvalidRequest(message)
    } else {
        ConnectorError::Backend(message)
    }
}

/// Map a JSON-RPC error OBJECT (the `error` field of a `POST /rpc` envelope)
/// onto a [`ConnectorError`] — used only by `attach`, since `POST /rpc`
/// always returns HTTP 200 and carries its error in the envelope (unlike the
/// REST routes, which get a real HTTP status from `rpc_error_to_status`).
///
/// Why: mirrors `crate::serve::rest::rpc_error_to_status`'s code table so
/// `attach`'s error classification agrees with every REST-routed method's.
/// What: `-32007`/`-32002` -> `NotFound`; `-32003`/`-32602` ->
/// `InvalidRequest`; everything else -> `Backend`.
fn map_rpc_error_object(err: &Value) -> ConnectorError {
    let code = err.get("code").and_then(Value::as_i64).unwrap_or(0);
    let message = err
        .get("message")
        .and_then(Value::as_str)
        .unwrap_or("unknown JSON-RPC error")
        .to_string();
    match code {
        -32007 | -32002 => ConnectorError::NotFound(message),
        -32003 | -32602 => ConnectorError::InvalidRequest(message),
        _ => ConnectorError::Backend(message),
    }
}

/// Map a `reqwest` transport-level failure onto [`ConnectorError::Transport`].
fn transport_err(e: reqwest::Error) -> ConnectorError {
    ConnectorError::Transport(e.to_string())
}

/// Map a JSON-body-decode failure onto [`ConnectorError::Transport`].
fn decode_err(e: reqwest::Error) -> ConnectorError {
    ConnectorError::Transport(format!("failed to decode daemon response: {e}"))
}

/// Map a [`Session`] onto the portable [`SessionInfo`].
///
/// Why: tcode's `Session` has no separate display-name field (unlike tm's
/// tmux session name) — `name` mirrors `task` so callers always have SOME
/// human-readable label, documented here rather than silently duplicating
/// the field with no explanation.
fn session_to_info(session: Session) -> SessionInfo {
    SessionInfo {
        id: session.id,
        name: session.task.clone(),
        state: session.status.as_str().to_string(),
        task: Some(session.task),
    }
}

#[async_trait]
impl WorkstreamConnector for TcodeConnector {
    /// `POST /sessions` -> `session.create` (#2983 Slice 3).
    ///
    /// Why: mints a new tcode session bound to an existing local project.
    /// `req.backend` must be [`BackendParams::Tcode`] — a
    /// [`BackendParams::Tm`] request is a caller bug (asked the tcode
    /// connector to provision a tm-shaped worktree session) and is rejected
    /// with [`ConnectorError::InvalidRequest`] before any HTTP call is made.
    /// What: `req.name_hint` has no tcode equivalent and is silently
    /// ignored (see [`CreateSessionReq`]'s docs); `req.agent` maps onto
    /// `session.create`'s `agent` param. A `400` (empty task, or `project`
    /// naming no real directory — see `session::protocol::create`'s AC-16.2
    /// docs) maps to [`ConnectorError::InvalidRequest`].
    /// Test: `connector_e2e::create_session_full_lifecycle`,
    /// `connector_e2e::create_session_wrong_backend_params_is_invalid_request`.
    async fn create_session(&self, req: CreateSessionReq) -> Result<SessionInfo, ConnectorError> {
        let CreateSessionReq {
            task,
            name_hint: _name_hint,
            agent,
            backend,
        } = req;
        let project = match backend {
            BackendParams::Tcode { project } => project,
            BackendParams::Tm { .. } => {
                return Err(ConnectorError::InvalidRequest(
                    "tcode connector requires CreateSessionReq::backend = BackendParams::Tcode"
                        .into(),
                ));
            }
        };
        let body = json!({
            "task": task,
            "agent": agent,
            "project": project.to_string_lossy(),
        });
        let url = format!("{}/sessions", self.daemon_url);
        let resp = self
            .http
            .post(&url)
            .json(&body)
            .send()
            .await
            .map_err(transport_err)?;
        if !resp.status().is_success() {
            return Err(map_error_response(resp).await);
        }
        let session: Session = resp.json().await.map_err(decode_err)?;
        Ok(session_to_info(session))
    }

    /// `GET /sessions` -> `session.list` (#2983 Slice 2).
    ///
    /// Why: enumerates every session the daemon currently owns.
    /// What: unwraps the `{"sessions": [...]}` envelope and maps each
    /// `Session` onto a portable `SessionInfo`.
    /// Test: `connector_e2e::list_sessions_empty_fleet_returns_empty_vec`.
    async fn list_sessions(&self) -> Result<Vec<SessionInfo>, ConnectorError> {
        #[derive(serde::Deserialize)]
        struct SessionsEnvelope {
            sessions: Vec<Session>,
        }
        let url = format!("{}/sessions", self.daemon_url);
        let resp = self.http.get(&url).send().await.map_err(transport_err)?;
        if !resp.status().is_success() {
            return Err(map_error_response(resp).await);
        }
        let listed: SessionsEnvelope = resp.json().await.map_err(decode_err)?;
        Ok(listed.sessions.into_iter().map(session_to_info).collect())
    }

    /// `GET /sessions/{id}` -> `session.status` (#2983 Slice 2).
    ///
    /// Why: point lookup for one session's current state.
    /// What: a `404` (unknown id, `-32007 session_not_found`) maps to
    /// [`ConnectorError::NotFound`]. `pending_decision` is always `None` —
    /// tcode has no equivalent concept in this phase (tm's managed-session
    /// pending-decision surface has no tcode counterpart yet).
    /// Test: `connector_e2e::session_status_unknown_id_is_not_found`.
    async fn session_status(&self, session_id: &str) -> Result<SessionStatus, ConnectorError> {
        let url = format!("{}/sessions/{session_id}", self.daemon_url);
        let resp = self.http.get(&url).send().await.map_err(transport_err)?;
        if !resp.status().is_success() {
            return Err(map_error_response(resp).await);
        }
        let session: Session = resp.json().await.map_err(decode_err)?;
        Ok(SessionStatus {
            id: session.id,
            state: session.status.as_str().to_string(),
            pending_decision: None,
        })
    }

    /// `POST /sessions/{id}/messages` -> `session.send` (#2983 Slice 3).
    ///
    /// Why: the client -> daemon input path.
    /// What: a `404` (unknown id) maps to [`ConnectorError::NotFound`];
    /// success is `()` — the daemon's `{"acknowledged": true}` confirmation
    /// is not surfaced through this trait.
    /// Test: `connector_e2e::send_input_unknown_id_is_not_found`.
    async fn send_input(&self, session_id: &str, input: &str) -> Result<(), ConnectorError> {
        let url = format!("{}/sessions/{session_id}/messages", self.daemon_url);
        let resp = self
            .http
            .post(&url)
            .json(&json!({ "input": input }))
            .send()
            .await
            .map_err(transport_err)?;
        if !resp.status().is_success() {
            return Err(map_error_response(resp).await);
        }
        Ok(())
    }

    /// `POST /rpc` `session.attach` — ring-buffer replay + SSE stream URL.
    ///
    /// Why: there is no REST route for `session.attach` (see module docs) —
    /// this is the one method that must speak raw JSON-RPC. tcode's attach
    /// surface is fundamentally different from tm's: it hands back replayed
    /// history plus a URL a caller GETs (as SSE) for live events, rather
    /// than a command a human runs — see [`AttachHandle::EventStream`]'s
    /// docs for why this is NOT forced into tm's shell-command shape.
    /// What: `POST /rpc` always returns HTTP 200 (errors are carried IN the
    /// envelope, per `crate::serve::http::rpc_handler`'s docs) — a non-200
    /// here means transport failure, mapped via [`map_error_response`]; an
    /// `error` field in the envelope is mapped via
    /// [`map_rpc_error_object`] (`-32007 session_not_found` ->
    /// [`ConnectorError::NotFound`]). On success, wraps
    /// `result.{session_id,stream_url,events}` in
    /// [`AttachHandle::EventStream`].
    /// Test: `connector_e2e::create_session_full_lifecycle` (asserts the
    /// returned handle is an `EventStream` with the expected `stream_url`),
    /// `connector_e2e::attach_unknown_id_is_not_found`.
    async fn attach(&self, session_id: &str) -> Result<AttachHandle, ConnectorError> {
        let rpc_req = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "session.attach",
            "params": { "session_id": session_id },
        });
        let url = format!("{}/rpc", self.daemon_url);
        let resp = self
            .http
            .post(&url)
            .json(&rpc_req)
            .send()
            .await
            .map_err(transport_err)?;
        if !resp.status().is_success() {
            return Err(map_error_response(resp).await);
        }
        let envelope: Value = resp.json().await.map_err(decode_err)?;
        if let Some(err) = envelope.get("error") {
            return Err(map_rpc_error_object(err));
        }
        let result = envelope.get("result").cloned().unwrap_or(Value::Null);
        let session_id_out = result
            .get("session_id")
            .and_then(Value::as_str)
            .unwrap_or(session_id)
            .to_string();
        let stream_url = result
            .get("stream_url")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string();
        let replayed_events = result
            .get("events")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default();
        Ok(AttachHandle::EventStream {
            session_id: session_id_out,
            stream_url,
            replayed_events,
        })
    }

    /// Always [`ConnectorError::NotSupported`] — tcode has no delegate
    /// surface in this phase (DOC-44 locked decision 3).
    ///
    /// Why: tm's `delegate` wraps `agent_delegate`'s gate+record-only MCP
    /// tool; tcode has no equivalent endpoint, and adding one is explicit
    /// scope creep for issue #3007 (the issue's locked design calls this out
    /// by name). Returning a typed `NotSupported` — rather than a generic
    /// `Backend`/`Transport` failure, and rather than making an HTTP call
    /// that would 404/`METHOD_NOT_FOUND` against a route that will never
    /// exist — lets a caller (the future lead agent, or a test) distinguish
    /// "this backend never does this" from "this particular call failed."
    /// What: returns `Err(ConnectorError::NotSupported(_))` unconditionally,
    /// with no network call at all.
    /// Test: `connector_e2e::delegate_is_not_supported`.
    async fn delegate(
        &self,
        _session_id: &str,
        _agent_spec: &AgentSpec,
    ) -> Result<DelegateHandle, ConnectorError> {
        Err(ConnectorError::NotSupported(
            "tcode has no delegate surface in this phase (DOC-44 locked decision 3, issue #3007)"
                .into(),
        ))
    }
}