brokk-mj-core 2.6.2

Session control plane for ACP coding agents
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
use super::*;
use serde_json::{Value, json};
use tokio::io::{
    AsyncBufReadExt, AsyncWriteExt, BufReader, DuplexStream, Lines, ReadHalf, WriteHalf,
};

fn implement() -> ElicitationResponse {
    ElicitationResponse::Accept {
        content: BTreeMap::from([(
            PLAN_REVIEW_ACTION.into(),
            ElicitationValue::String("implement".into()),
        )]),
    }
}

fn permission(plan: &str, options: &[&str]) -> Value {
    json!({
        "sessionId": "plan-session",
        "toolCall": {
            "toolCallId": "plan-tool", "kind": "switch_mode", "title": "Ready to code?",
            "rawInput": {"plan": plan, "planFilePath": "/workspace/plan.md"}
        },
        "options": options.iter().map(|id| json!({
            "optionId": id, "name": id,
            "kind": if *id == "reject" {"reject_once"} else {"allow_always"}
        })).collect::<Vec<_>>()
    })
}

fn mode_config(current: &str) -> Value {
    json!([{
        "id": "mode", "name": "Mode", "category": "mode", "type": "select", "currentValue": current,
        "options": [
            {"value": "plan", "name": "Plan"}, {"value": "auto", "name": "Auto"},
            {"value": "bypassPermissions", "name": "Bypass"}
        ]
    }])
}

#[test]
fn claude_plan_approval_selects_the_deployment_mode_without_clearing_context() {
    for ids in [
        ["auto", "bypassPermissions", "default"],
        ["exit-plan-auto", "exit-plan-bypass", "exit-plan-default"],
    ] {
        for reverse in [false, true] {
            let mut options = vec![
                "exit-plan-clear-auto",
                "exit-plan-clear-bypass",
                ids[2],
                ids[0],
                ids[1],
            ];
            if reverse {
                options.reverse();
            }
            let request = serde_json::from_value(permission("Approved plan", &options)).unwrap();
            for (policy, expected) in [
                (ExecutionPolicy::ConfiguredApprovals, ids[0]),
                (ExecutionPolicy::Unconstrained, ids[1]),
            ] {
                let PlanPermissionAnswer::Native(answer) = policy_plan_permission_answer(
                    &request,
                    implement(),
                    HarnessKind::Claude,
                    policy,
                )
                .unwrap() else {
                    panic!("offered mode must be selected directly");
                };
                assert_eq!(
                    serde_json::to_value(answer).unwrap()["outcome"]["optionId"],
                    expected
                );
            }
        }
    }
}

#[test]
fn claude_missing_auto_is_an_error_and_missing_bypass_requires_a_continuation() {
    let request = serde_json::from_value(permission(
        "Plan",
        &["exit-plan-clear-auto", "default", "acceptEdits", "reject"],
    ))
    .unwrap();
    assert!(
        policy_plan_permission_answer(
            &request,
            implement(),
            HarnessKind::Claude,
            ExecutionPolicy::ConfiguredApprovals
        )
        .err()
        .unwrap()
        .to_string()
        .contains("required auto")
    );
    assert!(matches!(
        policy_plan_permission_answer(
            &request,
            implement(),
            HarnessKind::Claude,
            ExecutionPolicy::Unconstrained
        )
        .unwrap(),
        PlanPermissionAnswer::ContinueInBypass
    ));
    let PlanPermissionAnswer::Native(answer) = policy_plan_permission_answer(
        &request,
        ElicitationResponse::Decline,
        HarnessKind::Claude,
        ExecutionPolicy::Unconstrained,
    )
    .unwrap() else {
        panic!("decline is native")
    };
    assert_eq!(
        serde_json::to_value(answer).unwrap()["outcome"]["optionId"],
        "reject"
    );
}

/// A real ACP transport with the agent side controlled by the test. No fake
/// depends on runtime implementation order beyond the protocol under test.
struct PlanProbe {
    input: Lines<BufReader<ReadHalf<DuplexStream>>>,
    output: WriteHalf<DuplexStream>,
    commands: mpsc::Sender<CommandRequest>,
    events: mpsc::Receiver<RuntimeEvent>,
    driver: tokio::task::JoinHandle<Result<Option<String>>>,
}

impl Drop for PlanProbe {
    fn drop(&mut self) {
        self.driver.abort();
    }
}

impl PlanProbe {
    async fn new(policy: ExecutionPolicy) -> Self {
        Self::with_config(policy, false).await
    }

    async fn with_config(policy: ExecutionPolicy, config: bool) -> Self {
        let (client, agent) = tokio::io::duplex(4096);
        let (client_read, client_write) = tokio::io::split(client);
        let (agent_read, agent_write) = tokio::io::split(agent);
        let (commands, mut requests) = mpsc::channel(16);
        let (event_tx, events) = mpsc::channel(128);
        let spec = LaunchSpec {
            command: "plan-probe".into(),
            args: vec![],
            environment: BTreeMap::new(),
            cwd: PathBuf::from("/workspace"),
            additional_directories: vec![],
            extra_mcp_servers: vec![],
            project_memory: None,
            resume_session: None,
            accepted_config: Default::default(),
            harness: HarnessKind::Claude,
            execution_policy: policy,
            acp_activity: AcpActivityClock::default(),
            step_clock: StepClock::default(),
        };
        let driver = tokio::spawn(async move {
            drive(
                ByteStreams::new(client_write.compat_write(), client_read.compat()),
                spec,
                &mut requests,
                event_tx,
                Arc::new(Mutex::new(None)),
                false,
            )
            .await
        });
        let mut probe = Self {
            input: BufReader::new(agent_read).lines(),
            output: agent_write,
            commands,
            events,
            driver,
        };
        let init = probe.message().await;
        assert_eq!(init["method"], "initialize");
        probe.result(&init, json!({"protocolVersion": 1})).await;
        let new = probe.message().await;
        assert_eq!(new["method"], "session/new");
        probe
            .result(
                &new,
                json!({"sessionId": "plan-session", "configOptions": if config {mode_config("plan")} else {json!([])}, "modes": {
                    "currentModeId": "plan", "availableModes": [
                        {"id": "plan", "name": "Plan"}, {"id": "auto", "name": "Auto"},
                        {"id": "bypassPermissions", "name": "Bypass"}
                    ]
                }}),
            )
            .await;
        if policy.is_unconstrained() {
            let mode = probe.message().await;
            if config {
                assert_eq!(mode["method"], "session/set_config_option");
                probe
                    .result(
                        &mode,
                        json!({"configOptions": mode_config("bypassPermissions")}),
                    )
                    .await;
            } else {
                assert_eq!(mode["method"], "session/set_mode");
                probe.result(&mode, json!({})).await;
            }
        }
        probe
    }

    async fn message(&mut self) -> Value {
        let line = tokio::time::timeout(Duration::from_secs(5), self.input.next_line())
            .await
            .expect("ACP message timed out")
            .unwrap()
            .expect("ACP closed unexpectedly");
        serde_json::from_str(&line).unwrap()
    }

    async fn send(&mut self, message: Value) {
        self.output
            .write_all(format!("{message}\n").as_bytes())
            .await
            .unwrap();
    }

    async fn result(&mut self, request: &Value, result: Value) {
        self.send(json!({"jsonrpc": "2.0", "id": request["id"], "result": result}))
            .await;
    }

    async fn event(&mut self) -> RuntimeEvent {
        tokio::time::timeout(Duration::from_secs(5), self.events.recv())
            .await
            .expect("runtime event timed out")
            .expect("runtime stopped")
    }

    async fn approve(&mut self, plan: &str, options: &[&str]) -> (Value, String, Value) {
        self.commands
            .send(CommandRequest::Prompt {
                request_id: "original-prompt".into(),
                prompt: vec![ContentBlock::Text(TextContent::new("Make a plan"))],
            })
            .await
            .unwrap();
        let prompt = self.message().await;
        assert_eq!(prompt["method"], "session/prompt");
        self.send(json!({"jsonrpc": "2.0", "id": "permission-1", "method": "session/request_permission", "params": permission(plan, options)})).await;
        let id = loop {
            if let RuntimeEvent::ElicitationRequested { request } = self.event().await {
                break request.id;
            }
        };
        self.answer(&id).await.unwrap();
        let answer = self.message().await;
        assert_eq!(answer["id"], "permission-1");
        (prompt, id, answer)
    }

    async fn answer(&mut self, id: &str) -> std::result::Result<(), String> {
        let (resolved, response) = oneshot::channel();
        self.commands
            .send(CommandRequest::ResolveElicitation {
                elicitation_id: id.into(),
                response: implement(),
                resolved,
            })
            .await
            .unwrap();
        response.await.unwrap()
    }

    async fn no_message(&mut self) {
        let message = tokio::time::timeout(Duration::from_millis(30), self.input.next_line()).await;
        assert!(
            message.is_err(),
            "unexpected ACP request before its prerequisite: {message:?}"
        );
    }

    async fn finished(&mut self) -> String {
        loop {
            if let RuntimeEvent::PromptFinished {
                request_id,
                stop_reason,
            } = self.event().await
            {
                assert_eq!(request_id, "original-prompt");
                return stop_reason;
            }
        }
    }

    async fn close(&mut self) {
        self.commands
            .send(CommandRequest::Close {
                request_id: "close".into(),
            })
            .await
            .unwrap();
        let mut close = self.message().await;
        if close["method"] == "session/cancel" {
            close = self.message().await;
        }
        assert_eq!(close["method"], "session/close");
        self.result(&close, json!({})).await;
        assert!(
            tokio::time::timeout(Duration::from_secs(5), &mut self.driver)
                .await
                .unwrap()
                .unwrap()
                .unwrap()
                .is_none()
        );
    }
}

#[tokio::test]
async fn approved_plan_waits_for_turn_and_mode_ack_then_continues_once_in_same_session() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let plan = "Implement the parser and its behavior tests.\n".repeat(2048);
    assert!(plan.len() > 65536);
    let (prompt, id, answer) = probe
        .approve(
            &plan,
            &[
                "exit-plan-clear-auto",
                "exit-plan-default",
                "exit-plan-auto",
                "reject",
            ],
        )
        .await;
    assert_eq!(answer["result"]["outcome"]["outcome"], "cancelled");
    assert!(
        probe.answer(&id).await.is_err(),
        "a duplicate answer must not schedule another continuation"
    );
    probe.no_message().await;
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    let mode = probe.message().await;
    assert_eq!(mode["method"], "session/set_mode");
    assert_eq!(mode["params"]["modeId"], "bypassPermissions");
    probe.no_message().await;
    while let Ok(event) = probe.events.try_recv() {
        assert!(
            !matches!(event, RuntimeEvent::PromptFinished { .. }),
            "the relay command must stay active through the transition"
        );
    }
    probe.result(&mode, json!({})).await;
    let continuation = probe.message().await;
    assert_eq!(continuation["method"], "session/prompt");
    assert_eq!(continuation["params"]["sessionId"], "plan-session");
    let text = continuation["params"]["prompt"][0]["text"]
        .as_str()
        .unwrap();
    assert!(text.starts_with("The user approved"));
    assert!(text.ends_with(&plan));
    probe
        .result(&continuation, json!({"stopReason": "end_turn"}))
        .await;
    assert_eq!(probe.finished().await, "EndTurn");
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn guardian_approval_uses_auto_without_a_followup_prompt() {
    let mut probe = PlanProbe::new(ExecutionPolicy::ConfiguredApprovals).await;
    let (prompt, _, answer) = probe
        .approve(
            "Plan",
            &[
                "exit-plan-default",
                "exit-plan-clear-auto",
                "exit-plan-auto",
                "reject",
            ],
        )
        .await;
    assert_eq!(answer["result"]["outcome"]["optionId"], "exit-plan-auto");
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    assert_eq!(probe.finished().await, "EndTurn");
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn rejected_mode_change_never_submits_the_approved_plan() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
    probe
        .result(&prompt, json!({"stopReason": "cancelled"}))
        .await;
    let mode = probe.message().await;
    probe.send(json!({"jsonrpc":"2.0", "id": mode["id"], "error": {"code": -32603, "message": "mode unavailable"}})).await;
    let mut warned = false;
    loop {
        match probe.event().await {
            RuntimeEvent::Warning { message }
                if message.contains("could not restore bypassPermissions") =>
            {
                warned = true
            }
            RuntimeEvent::PromptFinished { stop_reason, .. } => {
                assert_eq!(stop_reason, "error");
                break;
            }
            _ => {}
        }
    }
    assert!(warned);
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn cancelling_during_mode_restoration_discards_the_continuation() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    let mode = probe.message().await;
    probe
        .commands
        .send(CommandRequest::Cancel {
            request_id: "cancel".into(),
            steering_prompt: None,
        })
        .await
        .unwrap();
    assert_eq!(probe.message().await["method"], "session/cancel");
    assert_eq!(probe.finished().await, "Cancelled");
    let cancelled_request = probe.message().await;
    assert_eq!(cancelled_request["method"], "$/cancel_request");
    assert_eq!(cancelled_request["params"]["requestId"], mode["id"]);
    probe.result(&mode, json!({})).await;
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn closing_during_mode_restoration_discards_the_continuation() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    assert_eq!(probe.message().await["method"], "session/set_mode");
    probe.close().await;
}

#[tokio::test]
async fn failed_planning_turn_does_not_restore_mode_or_continue() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
    probe.send(json!({"jsonrpc":"2.0", "id": prompt["id"], "error": {"code": -32603, "message": "planning failed"}})).await;
    assert_eq!(probe.finished().await, "error");
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn cancelling_while_waiting_for_the_planning_turn_prevents_mode_restoration() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
    probe
        .commands
        .send(CommandRequest::Cancel {
            request_id: "cancel".into(),
            steering_prompt: None,
        })
        .await
        .unwrap();
    assert_eq!(probe.message().await["method"], "session/cancel");
    probe
        .result(&prompt, json!({"stopReason": "cancelled"}))
        .await;
    assert_eq!(probe.finished().await, "Cancelled");
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn plan_transition_timeout_requests_a_restart_without_replaying_implementation() {
    for awaiting_mode in [false, true] {
        let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
        let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
        if awaiting_mode {
            probe
                .result(&prompt, json!({"stopReason": "end_turn"}))
                .await;
            assert_eq!(probe.message().await["method"], "session/set_mode");
        }
        loop {
            if let RuntimeEvent::Warning { message } = probe.event().await
                && message.contains("waiting for Claude")
            {
                break;
            }
        }
        tokio::time::pause();
        tokio::time::advance(CANCEL_ACK_TIMEOUT + Duration::from_secs(1)).await;
        let mut warned = false;
        loop {
            match probe.event().await {
                RuntimeEvent::Warning { message }
                    if message.contains("Plan implementation timed out") =>
                {
                    warned = true
                }
                RuntimeEvent::CommandInterrupted { request_id, .. } => {
                    assert_eq!(request_id, "original-prompt");
                    break;
                }
                RuntimeEvent::PromptFinished { .. } => panic!("timeout must interrupt the command"),
                _ => {}
            }
        }
        assert!(warned);
        assert_eq!(
            (&mut probe.driver).await.unwrap().unwrap(),
            Some("plan-session".into())
        );
        tokio::time::resume();
        while let Some(line) = probe.input.next_line().await.unwrap() {
            let message: Value = serde_json::from_str(&line).unwrap();
            assert_ne!(message["method"], "session/prompt");
        }
    }
}

#[tokio::test]
async fn dropping_bridge_connection_discards_a_pending_implementation() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, _) = probe.approve("Plan", &["exit-plan-auto", "reject"]).await;
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    assert_eq!(probe.message().await["method"], "session/set_mode");
    // run_bridge drops the driver when its child exits; exercise that same
    // teardown while the mode acknowledgement is still outstanding.
    probe.driver.abort();
    tokio::time::timeout(Duration::from_secs(5), &mut probe.driver)
        .await
        .expect("transport loss must stop the connection")
        .expect_err("the driver must be cancelled");
    while let Some(line) = probe.input.next_line().await.unwrap() {
        let message: Value = serde_json::from_str(&line).unwrap();
        assert_ne!(message["method"], "session/prompt");
    }
}

#[tokio::test]
async fn guardian_without_auto_reports_failure_instead_of_selecting_manual_mode() {
    let mut probe = PlanProbe::new(ExecutionPolicy::ConfiguredApprovals).await;
    let (prompt, _, answer) = probe
        .approve(
            "Plan",
            &["exit-plan-default", "exit-plan-clear-auto", "reject"],
        )
        .await;
    assert_eq!(answer["result"]["outcome"]["outcome"], "cancelled");
    loop {
        if let RuntimeEvent::Warning { message } = probe.event().await
            && message.contains("required auto")
        {
            break;
        }
    }
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    probe.finished().await;
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn offered_bypass_is_selected_without_cancelling_the_plan_turn() {
    let mut probe = PlanProbe::new(ExecutionPolicy::Unconstrained).await;
    let (prompt, _, answer) = probe
        .approve(
            "Plan",
            &[
                "exit-plan-clear-bypass",
                "exit-plan-default",
                "exit-plan-bypass",
                "exit-plan-auto",
                "reject",
            ],
        )
        .await;
    assert_eq!(answer["result"]["outcome"]["optionId"], "exit-plan-bypass");
    probe
        .result(&prompt, json!({"stopReason": "end_turn"}))
        .await;
    probe.finished().await;
    probe.no_message().await;
    probe.close().await;
}

#[tokio::test]
async fn config_mode_restoration_checks_the_mode_returned_by_claude() {
    for returned_mode in ["bypassPermissions", "auto"] {
        let mut probe = PlanProbe::with_config(ExecutionPolicy::Unconstrained, true).await;
        let (prompt, _, _) = probe
            .approve("The approved plan", &["exit-plan-auto", "reject"])
            .await;
        probe
            .result(&prompt, json!({"stopReason": "end_turn"}))
            .await;
        let mode = probe.message().await;
        assert_eq!(mode["method"], "session/set_config_option");
        assert_eq!(mode["params"]["value"], "bypassPermissions");
        probe.no_message().await;
        probe
            .result(&mode, json!({"configOptions": mode_config(returned_mode)}))
            .await;
        if returned_mode == "bypassPermissions" {
            let continuation = probe.message().await;
            assert_eq!(continuation["method"], "session/prompt");
            probe
                .result(&continuation, json!({"stopReason": "end_turn"}))
                .await;
            assert_eq!(probe.finished().await, "EndTurn");
        } else {
            assert_eq!(probe.finished().await, "error");
        }
        probe.no_message().await;
        probe.close().await;
    }
}