monoloop-interpreter 0.1.4

Async incremental dialect interpreter and canonical-unit assembler
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
#![allow(clippy::while_let_loop)]
//! Interpreter fragmentation, sentence, and ACP mapping tests.

use monoloop_contracts::{
    CanonicalUnit, CanonicalUnitEvent, DialectBinding, DialectDescriptor, InterpretationEndKind,
    InterpreterOutputEvent, TextChannel, ToolRequestState, UnitState,
};
use monoloop_interpreter::{
    ConnectionId, DefaultInterpreterFactory, InterpretationId, InterpretationLimits,
    InterpreterFactory, StartInterpretation,
};

fn acp() -> DialectBinding {
    DialectBinding::negotiated(DialectDescriptor::acp_json_rpc("1"))
}

fn test_d() -> DialectBinding {
    DialectBinding::fixed(DialectDescriptor::test_raw())
}

async fn run_chunks(dialect: DialectBinding, chunks: &[&[u8]]) -> Vec<InterpreterOutputEvent> {
    let factory = DefaultInterpreterFactory::new();
    let interp = factory
        .start(StartInterpretation {
            interpretation_id: InterpretationId::new("i1"),
            connection_id: ConnectionId::new("c1"),
            external_session_id: None,
            dialect,
            limits: InterpretationLimits::default(),
        })
        .unwrap();
    for c in chunks {
        interp
            .input
            .push_bytes(bytes::Bytes::copy_from_slice(c))
            .await
            .unwrap();
    }
    interp.input.finish_clean().await.unwrap();
    let mut out = Vec::new();
    loop {
        match interp.events.recv().await {
            Some(ev) => {
                let done = matches!(ev, InterpreterOutputEvent::Ended(_));
                out.push(ev);
                if done {
                    break;
                }
            }
            None => break,
        }
    }
    out
}

fn text_contents(events: &[InterpreterOutputEvent]) -> Vec<String> {
    events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match u.as_ref() {
                CanonicalUnitEvent::Created(s) => match &s.unit {
                    CanonicalUnit::Text(t) => Some(t.content.clone()),
                    _ => None,
                },
                _ => None,
            },
            _ => None,
        })
        .collect()
}

#[tokio::test]
async fn fragmentation_invariant_plain_text() {
    let full = b"Hello world. Next sentence!";
    let a = run_chunks(test_d(), &[full]).await;
    let b = run_chunks(test_d(), &[b"Hel", b"lo wor", b"ld. Ne", b"xt sentence!"]).await;
    let c = run_chunks(
        test_d(),
        &full.iter().map(std::slice::from_ref).collect::<Vec<_>>(),
    )
    .await;

    assert_eq!(text_contents(&a), text_contents(&b));
    assert_eq!(text_contents(&a), text_contents(&c));
    assert_eq!(
        text_contents(&a),
        vec!["Hello world.".to_string(), "Next sentence!".to_string()]
    );
}

#[tokio::test]
async fn no_partial_sentence_on_abrupt_end() {
    let factory = DefaultInterpreterFactory::new();
    let interp = factory
        .start(StartInterpretation {
            interpretation_id: InterpretationId::new("i2"),
            connection_id: ConnectionId::new("c2"),
            external_session_id: None,
            dialect: test_d(),
            limits: InterpretationLimits::default(),
        })
        .unwrap();
    interp
        .input
        .push_bytes(bytes::Bytes::from_static(b"The implementation will"))
        .await
        .unwrap();
    interp.input.transport_failed().await.unwrap();
    let mut texts = Vec::new();
    let mut end_kind = None;
    loop {
        match interp.events.recv().await {
            Some(InterpreterOutputEvent::Unit(u)) => {
                if let CanonicalUnitEvent::Created(s) = u.as_ref() {
                    if let CanonicalUnit::Text(t) = &s.unit {
                        texts.push(t.content.clone());
                    }
                }
            }
            Some(InterpreterOutputEvent::Ended(e)) => {
                end_kind = Some(e.kind);
                assert!(e.unresolved_text_bytes > 0);
                break;
            }
            None => break,
        }
    }
    assert!(texts.is_empty());
    assert_eq!(end_kind, Some(InterpretationEndKind::TransportFailed));
}

#[tokio::test]
async fn acp_message_chunks_assemble_sentences() {
    let m1 = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Hello "}}}}"#;
    let m2 = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"world."}}}}"#;
    let m3 = br#"{"jsonrpc":"2.0","id":1,"result":{"stopReason":"end_turn"}}"#;

    // Also test byte-level fragmentation across JSON boundaries
    let mut stream = Vec::new();
    stream.extend_from_slice(m1);
    stream.extend_from_slice(m2);
    stream.extend_from_slice(m3);

    let whole = run_chunks(acp(), &[&stream]).await;
    let split_at = stream.len() / 3;
    let frag = run_chunks(
        acp(),
        &[
            &stream[..split_at],
            &stream[split_at..split_at * 2],
            &stream[split_at * 2..],
        ],
    )
    .await;

    assert_eq!(text_contents(&whole), text_contents(&frag));
    assert_eq!(text_contents(&whole), vec!["Hello world.".to_string()]);
}

#[tokio::test]
async fn acp_tool_waiting_then_ready() {
    let wait = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"tool_call","toolCallId":"t1","title":"read_file","status":"pending"}}}"#;
    let ready = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"tool_call_update","toolCallId":"t1","title":"read_file","status":"pending","rawInput":{"path":"/tmp/x"}}}}"#;

    let events = run_chunks(acp(), &[wait, ready]).await;
    let tools: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match &u.snapshot().unit {
                CanonicalUnit::Tool(t) => Some((
                    t.tool_action_id.as_str().to_string(),
                    t.request_state,
                    t.request_payload.clone(),
                    u.snapshot().unit_state,
                )),
                _ => None,
            },
            _ => None,
        })
        .collect();

    assert!(tools.len() >= 2);
    assert_eq!(tools[0].1, ToolRequestState::Assembling);
    assert!(tools[0].2.is_none(), "waiting must not expose partial args");
    assert_eq!(tools[0].3, UnitState::Waiting);
    let ready_ev = tools
        .iter()
        .find(|t| t.1 == ToolRequestState::Ready)
        .unwrap();
    assert!(ready_ev.2.as_ref().unwrap().contains("path"));
}

/// Live Grok Build: no status field; result arrives as `content` on tool_call_update.
#[tokio::test]
async fn acp_grok_tool_content_update_reaches_terminal_success() {
    let call = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"tool_call","toolCallId":"call-1","title":"write","rawInput":{"file_path":"/tmp/x.txt","content":"hello monoloop crud\n"}}}}"#;
    let upd = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"tool_call_update","toolCallId":"call-1","title":"Write `/tmp/x.txt`","kind":"edit","rawInput":{"file_path":"/tmp/x.txt","content":"hello monoloop crud\n"},"content":[{"type":"diff","path":"/tmp/x.txt","oldText":"","newText":"hello monoloop crud\n"}],"locations":[]}}}"#;

    let events = run_chunks(acp(), &[call, upd]).await;
    let tools: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match &u.snapshot().unit {
                CanonicalUnit::Tool(t) => Some((
                    t.request_state,
                    t.execution_state,
                    t.terminal_outcome,
                    t.request_payload.clone(),
                    t.result_payload.clone(),
                    u.snapshot().unit_state,
                )),
                _ => None,
            },
            _ => None,
        })
        .collect();

    assert!(
        tools.iter().any(|(req, _, _, payload, _, _)| {
            *req == ToolRequestState::Ready
                && payload.as_ref().is_some_and(|p| p.contains("file_path"))
        }),
        "ready with args: {tools:?}"
    );
    assert!(
        tools.iter().any(|(_, exec, term, _, result, state)| {
            *exec == monoloop_contracts::ToolExecutionState::Terminal
                && *term == Some(monoloop_contracts::ToolTerminalOutcome::Success)
                && result.as_ref().is_some_and(|r| r.contains("diff"))
                && *state == UnitState::Complete
        }),
        "terminal success with result: {tools:?}"
    );
}

/// Dialect stream steps (`stepIdx` / numeric `messageId`) attach as source_step.
#[tokio::test]
async fn acp_source_step_propagates_to_sentences_and_tools() {
    let tool = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"tool_call","toolCallId":"call-1","title":"Create","status":"completed","rawInput":{"path":"/tmp/x"},"content":[{"type":"diff","path":"/tmp/x"}],"_meta":{"stepIdx":3}}}}"#;
    let t1 = br#"{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","messageId":"11","content":{"type":"text","text":"All done."}}}}"#;

    let events = run_chunks(acp(), &[tool, t1]).await;

    let tool_steps: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match &u.snapshot().unit {
                CanonicalUnit::Tool(_) => u.snapshot().source_step,
                _ => None,
            },
            _ => None,
        })
        .collect();
    assert!(tool_steps.contains(&3), "tool source_step: {tool_steps:?}");

    let text_steps: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match &u.snapshot().unit {
                CanonicalUnit::Text(t) => Some((t.content.clone(), u.snapshot().source_step)),
                _ => None,
            },
            _ => None,
        })
        .collect();
    assert!(
        text_steps
            .iter()
            .any(|(c, s)| c.contains("All done") && *s == Some(11)),
        "text source_step: {text_steps:?}"
    );
}

/// Dialect `agentTimestampMs` is attached as observational source_time on complete units.
#[tokio::test]
async fn acp_agent_timestamp_propagates_to_sentences_and_tools() {
    let t1 = br#"{"jsonrpc":"2.0","method":"session/update","params":{"_meta":{"agentTimestampMs":1000},"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Hello "}}}}"#;
    let t2 = br#"{"jsonrpc":"2.0","method":"session/update","params":{"_meta":{"agentTimestampMs":1005},"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"world."}}}}"#;
    let tool = br#"{"jsonrpc":"2.0","method":"session/update","params":{"_meta":{"agentTimestampMs":2000},"sessionId":"s1","update":{"sessionUpdate":"tool_call","toolCallId":"call-1","title":"write","rawInput":{"path":"/tmp/x"}}}}"#;
    let t3 = br#"{"jsonrpc":"2.0","method":"session/update","params":{"_meta":{"agentTimestampMs":3000},"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" Done."}}}}"#;

    let events = run_chunks(acp(), &[t1, t2, tool, t3]).await;

    let texts: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match &u.snapshot().unit {
                CanonicalUnit::Text(t) => Some((t.content.clone(), u.snapshot().source_time)),
                _ => None,
            },
            _ => None,
        })
        .collect();
    assert!(
        texts.iter().any(|(c, st)| {
            c == "Hello world." && st.is_some_and(|s| s.first_ms == 1000 && s.last_ms == 1005)
        }),
        "sentence must span first/last fragment times: {texts:?}"
    );
    // " Done." may seal at clean end without trailing space after period.
    assert!(
        texts.iter().any(|(c, st)| {
            c.trim() == "Done." && st.is_some_and(|s| s.first_ms == 3000 && s.last_ms == 3000)
        }),
        "later sentence time: {texts:?}"
    );

    let tools: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            InterpreterOutputEvent::Unit(u) => match &u.snapshot().unit {
                CanonicalUnit::Tool(_) => u.snapshot().source_time,
                _ => None,
            },
            _ => None,
        })
        .collect();
    assert!(
        tools
            .iter()
            .any(|s| s.first_ms == 2000 && s.last_ms == 2000),
        "tool source time: {tools:?}"
    );

    // Emit order can place the tool *before* the completed sentence when the
    // period lacks trailing whitespace until a later chunk — that is why
    // source_time is observational metadata, not emit-order causality.
    let first_sentence_t = texts
        .iter()
        .find(|(c, _)| c == "Hello world.")
        .and_then(|(_, st)| *st)
        .unwrap();
    assert!(
        first_sentence_t.first_ms < 2000,
        "dialect source time of prose starts before tool: {first_sentence_t:?}"
    );
}

#[tokio::test]
async fn sentence_complete_before_response_end() {
    let factory = DefaultInterpreterFactory::new();
    let interp = factory
        .start(StartInterpretation {
            interpretation_id: InterpretationId::new("i3"),
            connection_id: ConnectionId::new("c3"),
            external_session_id: None,
            dialect: test_d(),
            limits: InterpretationLimits::default(),
        })
        .unwrap();
    interp
        .input
        .push_bytes(bytes::Bytes::from_static(b"First sentence. "))
        .await
        .unwrap();
    // Should already have a sentence event without finish
    let ev = tokio::time::timeout(std::time::Duration::from_secs(1), interp.events.recv())
        .await
        .expect("timeout")
        .expect("event");
    match ev {
        InterpreterOutputEvent::Unit(u) => {
            let CanonicalUnitEvent::Created(s) = u.as_ref() else {
                panic!("expected created unit");
            };
            assert_eq!(s.unit_state, UnitState::Complete);
            match &s.unit {
                CanonicalUnit::Text(t) => {
                    assert_eq!(t.content, "First sentence.");
                    assert_eq!(t.channel, TextChannel::PublicResponse);
                }
                _ => panic!("expected text"),
            }
        }
        other => panic!("unexpected {other:?}"),
    }
    interp.input.finish_clean().await.unwrap();
}

/// Reasoning and response lanes get independent ordinal sequences (D-006).
#[tokio::test]
async fn reasoning_and_response_lane_ordinals_independent() {
    // Public reasoning summary requires public:true in ACP mapping; feed via test dialect
    // by using ACP public thought + response chunks.
    let thought = br#"{"jsonrpc":"2.0","method":"session/update","params":{"update":{"sessionUpdate":"agent_thought_chunk","public":true,"content":{"type":"text","text":"Thinking carefully. "}}}}"#;
    let resp = br#"{"jsonrpc":"2.0","method":"session/update","params":{"update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Answer here."}}}}"#;
    let events = run_chunks(acp(), &[thought, resp]).await;

    let mut response_ords = Vec::new();
    let mut reasoning_ords = Vec::new();
    for e in &events {
        let InterpreterOutputEvent::Unit(u) = e else {
            continue;
        };
        let CanonicalUnitEvent::Created(s) = u.as_ref() else {
            continue;
        };
        let CanonicalUnit::Text(t) = &s.unit else {
            continue;
        };
        match t.channel {
            TextChannel::PublicResponse => {
                response_ords.push((
                    s.lane_id.as_str().to_string(),
                    s.lane_ordinal,
                    t.sentence_ordinal,
                ));
            }
            TextChannel::PublicReasoningSummary => {
                reasoning_ords.push((
                    s.lane_id.as_str().to_string(),
                    s.lane_ordinal,
                    t.sentence_ordinal,
                ));
            }
            _ => {}
        }
    }
    // At least one response unit with matching ordinals on response lane.
    assert!(
        response_ords
            .iter()
            .any(|(lane, lo, so)| lane == "response" && *lo == *so && *lo >= 1),
        "response lane ordinals: {response_ords:?}"
    );
    if !reasoning_ords.is_empty() {
        assert!(
            reasoning_ords
                .iter()
                .all(|(lane, lo, so)| lane == "reasoning" && *lo == *so && *lo >= 1),
            "reasoning lane ordinals: {reasoning_ords:?}"
        );
        // Independent sequences: both can start at 1.
        assert_eq!(reasoning_ords[0].1, 1);
        assert_eq!(response_ords[0].1, 1);
    }
}