subspace-agent 0.11.0

Subspace Framework agent runtime: Copilot, Pi and ACP chat engines. Internal crate; apps depend on `subspace-framework`.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Turns SDK session events into structured transcript rows.
//!
//! The mapper keeps the little state the SDK spreads across events: the tool
//! requests an `assistant.message` announced, which name and describe the
//! calls that follow, and the first and last timestamp of each `reasoningId`,
//! which give a thinking row its duration. Everything else is a pure function
//! of one event, so the whole mapping is testable from JSON.

use std::collections::{HashMap, HashSet};

use serde_json::Value;

use super::evidence::hosted_evidence;
use super::summary::{argument_summary, one_line};
use crate::{Entry, ToolCall, ToolStatus, TranscriptEntry, repeats};

/// The longest title a tool row shows, on one line.
const TITLE_LIMIT: usize = 80;
/// Milliseconds in a second, for the reasoning duration.
const MILLISECONDS: i64 = 1000;

/// What an `assistant.message` said about a call, kept until its execution
/// events arrive.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct Request {
    title: String,
    detail: String,
}

/// First and last timestamp seen for one `reasoningId`, in milliseconds.
#[derive(Clone, Copy, Debug)]
struct Span {
    first: i64,
    last: i64,
}

/// What a row shows for a call the SDK never reported on.
pub(super) const UNFINISHED: &str = "This call did not finish: the turn ended first.";

#[derive(Default)]
pub(super) struct Transcript {
    requests: HashMap<String, Request>,
    reasoning: HashMap<String, Span>,
    /// Every call an assistant message asked for, in order.
    announced: Vec<String>,
    /// The calls an execution event has mentioned.
    started: HashSet<String>,
}

/// The most specific name the SDK offered for a call that does not simply
/// repeat its arguments. A tool whose intention summary is the query it runs
/// would otherwise show the same text twice, so the plain tool title is used
/// instead and the arguments are shown once.
pub(super) fn request_title(request: &Value, detail: &str) -> String {
    for field in ["intentionSummary", "toolTitle", "name"] {
        let Some(title) = request[field]
            .as_str()
            .filter(|text| !text.trim().is_empty())
        else {
            continue;
        };
        if repeats(title, detail) {
            continue;
        }
        return one_line(title, TITLE_LIMIT);
    }
    // Every name repeated the arguments, so the tool's own name is the title.
    let fallback = request["toolTitle"]
        .as_str()
        .or_else(|| request["name"].as_str())
        .unwrap_or("Tool");
    one_line(fallback, TITLE_LIMIT)
}

/// Milliseconds for an event timestamp, whether the SDK sent a number or an
/// RFC 3339 string.
fn timestamp_ms(event: &Value) -> Option<i64> {
    let timestamp = &event["timestamp"];
    if let Some(milliseconds) = timestamp.as_i64() {
        return Some(milliseconds);
    }
    chrono::DateTime::parse_from_rfc3339(timestamp.as_str()?)
        .ok()
        .map(|moment| moment.timestamp_millis())
}

fn reasoning_text(data: &Value) -> Option<String> {
    data["content"]
        .as_str()
        .or_else(|| data["reasoningText"].as_str())
        .map(ToOwned::to_owned)
}

/// The result or the error a completed call reported.
fn completion_result(data: &Value) -> String {
    data["error"]["message"]
        .as_str()
        .or_else(|| data["result"]["detailedContent"].as_str())
        .or_else(|| data["result"]["content"].as_str())
        .unwrap_or("No result details were supplied by the SDK.")
        .to_owned()
}

/// One row per `toolCallId`, so a start and its completion share an id and
/// the view updates the same row.
fn tool_id(call_id: &str) -> String {
    format!("tool:{call_id}")
}

/// A row that keeps the SDK's own event id.
/// The wire value `user.message` carries when Copilot injected the message
/// into a turn that was already running.
const STEERING: &str = "steering";

/// Zack's own message row. `delivery` is absent on older events and on
/// history entries, which then read as an ordinary message.
fn user_entry(data: &Value) -> Option<Entry> {
    Some(Entry::User {
        text: data["content"].as_str()?.to_owned(),
        steering: data["delivery"].as_str() == Some(STEERING),
    })
}

fn message_row(event: &Value, entry: Entry) -> TranscriptEntry {
    TranscriptEntry {
        id: event["id"].as_str().unwrap_or_default().to_owned(),
        entry,
    }
}

impl Transcript {
    /// Remembers what an `assistant.message` said about the calls it asked for.
    fn remember_requests(&mut self, data: &Value) {
        let Some(requests) = data["toolRequests"].as_array() else {
            return;
        };
        for request in requests {
            let Some(call_id) = request["toolCallId"].as_str() else {
                continue;
            };
            let detail = argument_summary(&request["arguments"]);
            self.requests.insert(
                call_id.to_owned(),
                Request {
                    title: request_title(request, &detail),
                    detail,
                },
            );
            self.announced.push(call_id.to_owned());
        }
    }

    /// The calls an `assistant.message` asked for that no execution event
    /// ever mentioned. A turn aborted before its tools ran leaves these, and
    /// a replay would otherwise lose them entirely.
    pub(super) fn unstarted_rows(&self) -> Vec<TranscriptEntry> {
        self.announced
            .iter()
            .filter(|call_id| !self.started.contains(*call_id))
            .map(|call_id| self.aborted_row(call_id))
            .collect()
    }

    fn aborted_row(&self, call_id: &str) -> TranscriptEntry {
        let request = self.requests.get(call_id).cloned().unwrap_or_default();
        TranscriptEntry {
            id: tool_id(call_id),
            entry: Entry::Tool(ToolCall {
                call_id: call_id.to_owned(),
                title: request.title,
                detail: request.detail,
                status: ToolStatus::Aborted,
                result: UNFINISHED.to_owned(),
            }),
        }
    }

    /// Extends the span of `reasoning_id` and returns its duration in whole
    /// seconds, if the events carried usable timestamps.
    fn reasoning_seconds(&mut self, reasoning_id: &str, event: &Value) -> Option<u64> {
        let now = timestamp_ms(event)?;
        let span = self
            .reasoning
            .entry(reasoning_id.to_owned())
            .or_insert(Span {
                first: now,
                last: now,
            });
        span.last = span.last.max(now);
        span.first = span.first.min(now);
        u64::try_from((span.last - span.first) / MILLISECONDS).ok()
    }

    /// The row for one call. A call no `assistant.message` announced still
    /// shows, named by the execution event's own tool name.
    fn tool_row(&self, call_id: &str, data: &Value, status: ToolStatus) -> TranscriptEntry {
        let request = self
            .requests
            .get(call_id)
            .cloned()
            .unwrap_or_else(|| Request {
                title: data["toolName"]
                    .as_str()
                    .filter(|name| !name.trim().is_empty())
                    .unwrap_or("Tool")
                    .to_owned(),
                detail: String::new(),
            });
        let result = match status {
            ToolStatus::Running => String::new(),
            _ => completion_result(data),
        };
        TranscriptEntry {
            id: tool_id(call_id),
            entry: Entry::Tool(ToolCall {
                call_id: call_id.to_owned(),
                title: request.title,
                detail: request.detail,
                status,
                result,
            }),
        }
    }

    /// The row an event produces, if it produces one. A reasoning delta only
    /// extends the span; an assistant message without text is dropped, so a
    /// turn that merely calls tools shows no empty row.
    pub(super) fn entry(&mut self, event: &Value) -> Option<TranscriptEntry> {
        let data = &event["data"];
        match event["type"].as_str()? {
            "user.message" => Some(message_row(event, user_entry(data)?)),
            "assistant.message" => {
                self.remember_requests(data);
                let content = data["content"].as_str().unwrap_or_default();
                let evidence = hosted_evidence(data);
                if content.trim().is_empty() && evidence.is_none() {
                    return None;
                }
                let text = match evidence {
                    Some(evidence) => format!(
                        "{}\n\nUntrusted web-search evidence:\n{evidence}",
                        content.trim_end()
                    ),
                    None => content.to_owned(),
                };
                Some(message_row(event, Entry::Assistant(text)))
            }
            "assistant.reasoning_delta" => {
                self.reasoning_seconds(data["reasoningId"].as_str().unwrap_or_default(), event);
                None
            }
            "assistant.reasoning" => {
                let text = reasoning_text(data)?;
                let seconds =
                    self.reasoning_seconds(data["reasoningId"].as_str().unwrap_or_default(), event);
                Some(message_row(event, Entry::Thinking { text, seconds }))
            }
            "tool.execution_start" => {
                let call_id = data["toolCallId"].as_str()?;
                self.started.insert(call_id.to_owned());
                Some(self.tool_row(call_id, data, ToolStatus::Running))
            }
            "tool.execution_complete" => {
                let call_id = data["toolCallId"].as_str()?;
                self.started.insert(call_id.to_owned());
                let status = if data["success"].as_bool() == Some(true) {
                    ToolStatus::Done
                } else {
                    ToolStatus::Failed
                };
                Some(self.tool_row(call_id, data, status))
            }
            "assistant.server_tool_progress" => {
                let kind = data["kind"].as_str().unwrap_or("hosted server tool");
                let status = data["status"].as_str().unwrap_or("in progress");
                Some(message_row(
                    event,
                    Entry::Info(format!("Untrusted {kind} progress: {status}")),
                ))
            }
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Transcript, argument_summary, one_line};
    use crate::{Entry, ToolStatus};
    use serde_json::json;

    fn at(seconds: i64) -> String {
        format!("2026-10-04T12:00:{seconds:02}Z")
    }

    #[test]
    fn scalar_arguments_read_as_one_line_led_by_the_action() {
        assert_eq!(
            argument_summary(&json!({"action": "github_search", "query": "is:open"})),
            "github_search query=is:open"
        );
        let fields = argument_summary(&json!({"limit": 20, "all": true}));
        assert!(fields.contains("limit=20"), "{fields}");
        assert!(
            fields.contains("all=true"),
            "every scalar field is summarised: {fields}"
        );
        assert_eq!(
            argument_summary(&json!({"action": "scope", "body": {"deep": 1}, "ids": [1, 2]})),
            "scope",
            "objects and arrays stay out of the one-line summary"
        );
        assert_eq!(argument_summary(&json!("not an object")), "");
        let long = argument_summary(&json!({"action": "x".repeat(200)}));
        assert_eq!(
            long.chars().count(),
            121,
            "cut to the limit plus an ellipsis"
        );
        assert!(long.ends_with('…'));
    }

    #[test]
    fn a_tool_call_is_one_row_named_by_its_request() {
        let mut transcript = Transcript::default();
        assert_eq!(
            transcript.entry(&json!({
                "type": "assistant.message",
                "id": "m1",
                "data": {
                    "content": "   ",
                    "toolRequests": [{
                        "toolCallId": "c1",
                        "name": "subspace_read",
                        "toolTitle": "Read services",
                        "intentionSummary": "Find the critical risks",
                        "arguments": {"action": "github_search", "query": "is:open"}
                    }]
                }
            })),
            None,
            "a turn that only calls tools shows no empty assistant row"
        );
        let start = transcript
            .entry(&json!({
                "type": "tool.execution_start",
                "data": {"toolCallId": "c1", "toolName": "subspace_read"}
            }))
            .expect("the start opens a row");
        let done = transcript
            .entry(&json!({
                "type": "tool.execution_complete",
                "data": {"toolCallId": "c1", "success": true, "result": {"content": "3 risks"}}
            }))
            .expect("the completion updates it");
        assert_eq!(start.id, done.id, "both events address one row");
        let Entry::Tool(call) = &done.entry else {
            panic!("expected a tool row, got {:?}", done.entry);
        };
        assert_eq!(call.call_id, "c1");
        assert_eq!(call.title, "Find the critical risks");
        assert_eq!(call.detail, "github_search query=is:open");
        assert_eq!(call.status, ToolStatus::Done);
        assert_eq!(call.result, "3 risks");
        assert_eq!(
            start.entry.tool().map(|call| call.status),
            Some(ToolStatus::Running)
        );
    }

    #[test]
    fn a_title_falls_back_and_a_failure_keeps_its_error() {
        let mut transcript = Transcript::default();
        transcript.entry(&json!({
            "type": "assistant.message",
            "id": "m1",
            "data": {"content": "Checking", "toolRequests": [
                {"toolCallId": "c1", "name": "subspace_read", "toolTitle": "Read services"},
                {"toolCallId": "c2", "name": "subspace_write"}
            ]}
        }));
        let titles = |transcript: &mut Transcript, call: &str| {
            let row = transcript
                .entry(&json!({
                    "type": "tool.execution_complete",
                    "data": {
                        "toolCallId": call,
                        "success": false,
                        "error": {"message": "Denied"}
                    }
                }))
                .expect("a completion is a row");
            let Entry::Tool(call) = row.entry else {
                panic!("expected a tool row");
            };
            call
        };
        let titled = titles(&mut transcript, "c1");
        assert_eq!(
            titled.title, "Read services",
            "the tool title names the call"
        );
        assert_eq!(titled.status, ToolStatus::Failed);
        assert_eq!(titled.result, "Denied");
        assert_eq!(
            titles(&mut transcript, "c2").title,
            "subspace_write",
            "the tool name is the last resort"
        );
        assert_eq!(
            titles(&mut transcript, "unknown").title,
            "Tool",
            "a call nothing announced still shows a row"
        );
    }

    #[test]
    fn hosted_search_progress_and_sources_are_untrusted_transcript_data() {
        let mut transcript = Transcript::default();
        let progress = transcript
            .entry(&json!({
                "type": "assistant.server_tool_progress",
                "id": "progress-1",
                "data": {"kind": "web_search", "status": "searching"}
            }))
            .expect("progress is visible");
        assert_eq!(
            progress.entry.text(),
            "Untrusted web_search progress: searching"
        );

        let reply = transcript
            .entry(&json!({
                "type": "assistant.message",
                "id": "reply-1",
                "data": {
                    "content": "Summary",
                    "serverTools": {"provider": "copilot", "items": [
                        {"title": "Example", "url": "https://example.test/result"}
                    ]}
                }
            }))
            .expect("hosted citations are visible");
        assert!(
            reply
                .entry
                .text()
                .contains("Untrusted web-search evidence:")
        );
        assert!(reply.entry.text().contains("https://example.test/result"));
    }

    #[test]
    fn thinking_spans_the_first_to_the_last_reasoning_event() {
        let mut transcript = Transcript::default();
        for second in [0, 7] {
            assert_eq!(
                transcript.entry(&json!({
                    "type": "assistant.reasoning_delta",
                    "timestamp": at(second),
                    "data": {"reasoningId": "r1", "deltaContent": "thinking"}
                })),
                None,
                "a delta only extends the span"
            );
        }
        let row = transcript
            .entry(&json!({
                "type": "assistant.reasoning",
                "id": "r-final",
                "timestamp": at(49),
                "data": {"reasoningId": "r1", "content": "I checked the risks"}
            }))
            .expect("the final reasoning event is a row");
        assert_eq!(
            row.entry,
            Entry::Thinking {
                text: "I checked the risks".into(),
                seconds: Some(49)
            }
        );
    }

    #[test]
    fn a_reasoning_event_without_a_usable_timestamp_has_no_duration() {
        let mut transcript = Transcript::default();
        let row = transcript
            .entry(&json!({
                "type": "assistant.reasoning",
                "id": "r1",
                "data": {"reasoningId": "r1", "reasoningText": "quick"}
            }))
            .expect("the row still appears");
        assert_eq!(
            row.entry,
            Entry::Thinking {
                text: "quick".into(),
                seconds: None
            }
        );
    }

    #[test]
    fn a_title_that_repeats_its_arguments_gives_way_to_the_tool_name() {
        let mut transcript = Transcript::default();
        transcript.entry(&json!({
            "type": "assistant.message",
            "id": "m1",
            "data": {"content": "", "toolRequests": [{
                "toolCallId": "c1",
                "name": "run_sql",
                "toolTitle": "Run query",
                "intentionSummary": "SELECT id FROM work_items WHERE state = 'Active'",
                "arguments": {"query": "SELECT id FROM work_items WHERE state = 'Active'"}
            }]}
        }));
        let row = transcript
            .entry(&json!({
                "type": "tool.execution_start",
                "data": {"toolCallId": "c1", "toolName": "run_sql"}
            }))
            .expect("a row");
        let Entry::Tool(call) = row.entry else {
            panic!("expected a tool row");
        };
        assert_eq!(
            call.title, "Run query",
            "the query is shown once, in the detail"
        );
        assert_eq!(
            call.detail,
            "query=SELECT id FROM work_items WHERE state = 'Active'"
        );
    }

    #[test]
    fn a_long_title_is_one_line_and_cut() {
        assert_eq!(one_line("  keep   this\n line  ", 80), "keep this line");
        let long = one_line(&"word ".repeat(40), 80);
        assert_eq!(long.chars().count(), 81);
        assert!(long.ends_with('…'));
    }

    #[test]
    fn a_turn_aborted_before_its_tools_ran_still_shows_them() {
        let mut transcript = Transcript::default();
        transcript.entry(&json!({
            "type": "assistant.message",
            "id": "m1",
            "data": {"content": "", "toolRequests": [
                {"toolCallId": "c1", "name": "subspace_read", "toolTitle": "Read services",
                 "arguments": {"action": "github_search"}},
                {"toolCallId": "c2", "name": "subspace_read", "toolTitle": "Read services",
                 "arguments": {"action": "scope"}}
            ]}
        }));
        // Only the first call ever started, and nothing completed.
        transcript.entry(&json!({
            "type": "tool.execution_start",
            "data": {"toolCallId": "c1", "toolName": "subspace_read"}
        }));
        let unstarted = transcript.unstarted_rows();
        assert_eq!(unstarted.len(), 1, "the call that never ran is reported");
        let Entry::Tool(call) = &unstarted[0].entry else {
            panic!("expected a tool row");
        };
        assert_eq!(call.call_id, "c2");
        assert_eq!(call.status, ToolStatus::Aborted);
        assert_eq!(call.detail, "scope");
        assert!(call.result.contains("did not finish"));
    }

    #[test]
    fn a_message_delivered_while_the_turn_ran_is_marked_as_steering() {
        let mut transcript = Transcript::default();
        let steered = transcript
            .entry(&json!({
                "type": "user.message",
                "id": "u2",
                "data": {"content": "use the other branch", "delivery": "steering"}
            }))
            .expect("a user message is a row");
        assert_eq!(
            steered.entry,
            Entry::User {
                text: "use the other branch".into(),
                steering: true
            }
        );
        let queued = transcript
            .entry(&json!({
                "type": "user.message",
                "id": "u3",
                "data": {"content": "and then stop", "delivery": "queued"}
            }))
            .expect("a user message is a row");
        assert_eq!(
            queued.entry.kind(),
            crate::EntryKind::User,
            "only steering carries the label"
        );
        assert_eq!(
            queued.entry,
            Entry::User {
                text: "and then stop".into(),
                steering: false
            }
        );
    }

    #[test]
    fn messages_and_unknown_events_map_as_expected() {
        let mut transcript = Transcript::default();
        let user = transcript
            .entry(&json!({
                "type": "user.message",
                "id": "u1",
                "data": {"content": "Review this"}
            }))
            .expect("a user message is a row");
        assert_eq!(user.id, "u1");
        assert_eq!(
            user.entry,
            Entry::User {
                text: "Review this".into(),
                steering: false
            }
        );
        assert_eq!(
            transcript
                .entry(&json!({
                    "type": "assistant.message",
                    "id": "a1",
                    "data": {"content": "Done"}
                }))
                .map(|row| row.entry),
            Some(Entry::Assistant("Done".into()))
        );
        assert_eq!(transcript.entry(&json!({"type": "session.idle"})), None);
        assert_eq!(transcript.entry(&json!({"data": {}})), None);
    }

    #[test]
    fn numeric_timestamps_also_give_a_duration() {
        let mut transcript = Transcript::default();
        transcript.entry(&json!({
            "type": "assistant.reasoning_delta",
            "timestamp": 1_000_i64,
            "data": {"reasoningId": "r1"}
        }));
        let row = transcript
            .entry(&json!({
                "type": "assistant.reasoning",
                "timestamp": 4_000_i64,
                "data": {"reasoningId": "r1", "content": "ok"}
            }))
            .expect("a row");
        assert_eq!(
            row.entry,
            Entry::Thinking {
                text: "ok".into(),
                seconds: Some(3)
            }
        );
    }
}