magi-code 0.77.1

Repository-aware CLI coding agent for terminal work
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
//! Real processes and sockets; synthetic execution is confined to this test binary.
use super::*;
use serde_json::{Value, json};
use std::process::{Child, Command, Stdio};

const CHILD_TEST: &str = "service::unix::survival_tests::synthetic_process_child";
const ROOT_ENV: &str = "MAGI_SYNTHETIC_SOCKET_TEST_ROOT";
const ROLE_ENV: &str = "MAGI_SYNTHETIC_SOCKET_TEST_ROLE";

struct TestProcess(Child);
impl Drop for TestProcess {
    fn drop(&mut self) {
        let _ = self.0.kill();
        let _ = self.0.wait();
    }
}

fn spawn_child(root: &Path, role: &str) -> TestProcess {
    TestProcess(
        Command::new(std::env::current_exe().unwrap())
            .args(["--exact", CHILD_TEST, "--nocapture"])
            .env(ROOT_ENV, root)
            .env(ROLE_ENV, role)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .spawn()
            .unwrap(),
    )
}

fn wait_until(mut ready: impl FnMut() -> bool) {
    let deadline = Instant::now() + Duration::from_secs(15);
    while !ready() {
        assert!(
            Instant::now() < deadline,
            "synthetic process did not settle"
        );
        thread::sleep(Duration::from_millis(10));
    }
}

struct Frontend {
    socket: UnixStream,
    instance: Value,
    connection: Value,
}
impl Frontend {
    fn connect(identity: &Identity) -> Self {
        let mut socket = UnixStream::connect(&identity.socket).unwrap();
        socket
            .set_read_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        socket
            .set_write_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        send_json(
            &mut socket,
            &Hello {
                version: VERSION.into(),
                workspace: identity.workspace.clone(),
                state_root: identity.state_root.clone(),
                action: "connect".into(),
            },
        )
        .unwrap();
        let hello: Reply =
            serde_json::from_slice(&read_line(&mut socket, HANDSHAKE_LIMIT).unwrap()).unwrap();
        assert_eq!(hello.status, "ready");
        let mut frontend = Self {
            socket,
            instance: Value::Null,
            connection: Value::Null,
        };
        let init = frontend.call(
            "initialize",
            Value::Null,
            Value::Null,
            Value::Null,
            json!({"supported_protocol_versions":[2],"requested_capabilities":[]}),
        );
        frontend.instance = init["instance_id"].clone();
        frontend.connection = init["connection_id"].clone();
        frontend
    }

    fn call(
        &mut self,
        method: &str,
        session: Value,
        control: Value,
        operation: Value,
        payload: Value,
    ) -> Value {
        let request_id = uuid::Uuid::new_v4().to_string();
        send_json(
            &mut self.socket,
            &json!({
                "protocol_version":2,"kind":"request","request_id":request_id,
                "instance_id":self.instance,"connection_id":self.connection,
                "session_id":session,"control":control,"operation_id":operation,
                "method":method,"payload":payload,
            }),
        )
        .unwrap();
        loop {
            let reply: Value = serde_json::from_slice(
                &read_line(&mut self.socket, crate::service::protocol::MAX_RECORD_BYTES).unwrap(),
            )
            .unwrap();
            if reply["kind"] == "response" && reply["request_id"] == request_id {
                assert!(reply["error"].is_null(), "{reply}");
                return reply["payload"].clone();
            }
        }
    }
}

#[test]
fn synthetic_process_child() {
    let Some(root) = std::env::var_os(ROOT_ENV) else {
        return;
    };
    let root = PathBuf::from(root);
    let identity = Identity::resolve(&root, &root.join("state")).unwrap();
    match std::env::var(ROLE_ENV).unwrap().as_str() {
        "daemon" => {
            let (release, wait) = crossbeam_channel::bounded(1);
            let (cleanup, cleanup_wait) = crossbeam_channel::bounded(1);
            let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
            let service = Arc::new(PersistentService::synthetic_socket_fixture(
                &root,
                wait,
                cleanup_wait,
                Arc::clone(&calls),
            ));
            let _lock = lock_identity(&identity).unwrap();
            if identity.socket.exists() {
                check_path(&identity.socket, Kind::Socket).unwrap();
                assert_eq!(
                    connect::connect(&identity.socket).unwrap_err().kind(),
                    std::io::ErrorKind::ConnectionRefused
                );
                fs::remove_file(&identity.socket).unwrap();
            }
            let listener = UnixListener::bind(&identity.socket).unwrap();
            fs::set_permissions(&identity.socket, fs::Permissions::from_mode(0o600)).unwrap();
            listener.set_nonblocking(true).unwrap();
            fs::write(root.join("ready"), "").unwrap();
            let mut clients = Vec::new();
            let mut released = false;
            let mut cleaned = false;
            let deadline = Instant::now() + Duration::from_secs(30);
            while !service.is_finished() {
                assert!(Instant::now() < deadline, "daemon fixture timed out");
                fs::write(
                    root.join("provider-calls"),
                    calls.load(Ordering::SeqCst).to_string(),
                )
                .unwrap();
                if !released && root.join("release").exists() {
                    release.send(()).unwrap();
                    released = true;
                }
                if !cleaned && root.join("cleanup").exists() {
                    cleanup.send(()).unwrap();
                    cleaned = true;
                }
                match listener.accept() {
                    Ok((stream, _)) => {
                        let first_frontend = clients.is_empty();
                        let service = Arc::clone(&service);
                        let identity = identity.clone();
                        let root = root.clone();
                        clients.push(thread::spawn(move || {
                            let _ = serve(stream, &identity, &service, &AtomicBool::new(false));
                            // The first socket is the submitting frontend. serve has now
                            // synchronously disconnected it from the real coordinator.
                            if first_frontend {
                                fs::write(root.join("disconnected"), "").unwrap();
                            }
                        }));
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                        thread::sleep(Duration::from_millis(5))
                    }
                    Err(error) => panic!("listener failed: {error}"),
                }
            }
            for client in clients {
                client.join().unwrap();
            }
            fs::write(
                root.join("provider-calls"),
                calls.load(Ordering::SeqCst).to_string(),
            )
            .unwrap();
            drop(service);
            fs::remove_file(&identity.socket).unwrap();
        }
        "frontend" => {
            let mut frontend = Frontend::connect(&identity);
            let session = frontend.call(
                "session.create",
                Value::Null,
                Value::Null,
                json!("create"),
                json!({}),
            )["session_id"]
                .clone();
            let claim = frontend.call(
                "session.claim",
                session.clone(),
                Value::Null,
                json!("claim"),
                json!({}),
            );
            let accepted = frontend.call(
                "turn.start",
                session.clone(),
                json!({"grant_id":claim["grant_id"],"generation":claim["generation"]}),
                json!("surviving-turn"),
                json!({"prompt":"Read the synthetic fixture"}),
            );
            assert_eq!(accepted["status"], "accepted");
            fs::write(
                root.join("accepted.tmp"),
                serde_json::to_vec(&json!({"session":session,"instance":frontend.instance}))
                    .unwrap(),
            )
            .unwrap();
            fs::rename(root.join("accepted.tmp"), root.join("accepted")).unwrap();
            // Keep the socket alive until the parent kills this process, not graceful EOF.
            loop {
                thread::sleep(Duration::from_secs(1));
            }
        }
        role => panic!("unknown test role: {role}"),
    }
}

#[test]
fn accepted_synthetic_turn_survives_frontend_process_death_and_rejects_busy_stop() {
    let root = tempfile::TempDir::new().unwrap();
    let identity = Identity::resolve(root.path(), &root.path().join("state")).unwrap();
    let mut daemon = spawn_child(root.path(), "daemon");
    wait_until(|| root.path().join("ready").exists());
    let mut frontend = spawn_child(root.path(), "frontend");
    wait_until(|| root.path().join("accepted").exists());
    let accepted: Value =
        serde_json::from_slice(&fs::read(root.path().join("accepted")).unwrap()).unwrap();
    frontend.0.kill().unwrap();
    assert!(!frontend.0.wait().unwrap().success());
    wait_until(|| root.path().join("disconnected").exists());
    // No frontend remains, and the worker is blocked before producing output.
    assert_eq!(request(&identity, "stop").unwrap().status, "busy");
    assert!(daemon.0.try_wait().unwrap().is_none());
    fs::write(root.path().join("release"), "").unwrap();

    let mut reconnected = Frontend::connect(&identity);
    assert_eq!(reconnected.instance, accepted["instance"]);
    let sessions = crate::sessions::SessionManager::new(root.path().join("state/sessions"));
    let session = sessions
        .open_existing(accepted["session"].as_str().unwrap())
        .unwrap();
    wait_until(|| {
        let replay = match session.frontend_replay(None, 32) {
            Ok(replay) => replay,
            Err(error) if error.is::<crate::sessions::FrontendSnapshotBusy>() => return false,
            Err(error) => panic!("replay failed: {error}"),
        };
        serde_json::to_value(replay)
            .unwrap()
            .to_string()
            .contains("after tool")
    });
    assert!(session.try_frontend_writer().unwrap().is_none());
    let pending = reconnected.call(
        "operation.lookup",
        Value::Null,
        Value::Null,
        Value::Null,
        json!({"target_instance_id":accepted["instance"],"operation_id":"surviving-turn"}),
    );
    assert_eq!(pending["state"], "accepted", "cleanup still owns the lease");
    assert_eq!(request(&identity, "stop").unwrap().status, "busy");
    fs::write(root.path().join("cleanup"), "").unwrap();
    let mut outcome = Value::Null;
    wait_until(|| {
        outcome = reconnected.call(
            "operation.lookup",
            Value::Null,
            Value::Null,
            Value::Null,
            json!({"target_instance_id":accepted["instance"],"operation_id":"surviving-turn"}),
        );
        outcome["state"] == "terminal"
    });
    assert_eq!(outcome["result"]["persistence"], "committed");
    assert!(session.try_frontend_writer().unwrap().is_some());
    let claimed = reconnected.call(
        "session.claim",
        accepted["session"].clone(),
        Value::Null,
        json!("reclaim"),
        json!({}),
    );
    assert_eq!(claimed["snapshot"]["phase"], "idle");
    assert_eq!(claimed["snapshot"]["terminal"]["status"], "completed");
    assert_eq!(
        claimed["snapshot"]["terminal"]["assistant_text"],
        "before tool\n\nafter tool"
    );
    assert!(daemon.0.try_wait().unwrap().is_none());
    assert!(
        session.try_frontend_writer().unwrap().is_none(),
        "an idle controller must exclude a standalone writer"
    );
    drop(reconnected);
    wait_until(|| request(&identity, "stop").unwrap().status == "stopped");
    wait_until(|| daemon.0.try_wait().unwrap().is_some());
    assert!(daemon.0.wait().unwrap().success());
    assert!(!identity.socket.exists());
}

#[test]
fn daemon_crash_does_not_replay_an_accepted_active_turn_after_restart() {
    use crate::sessions::{SessionEventKind, SessionManager};

    let root = tempfile::TempDir::new().unwrap();
    let identity = Identity::resolve(root.path(), &root.path().join("state")).unwrap();
    let mut daemon = spawn_child(root.path(), "daemon");
    wait_until(|| root.path().join("ready").exists());
    let mut frontend = spawn_child(root.path(), "frontend");
    wait_until(|| root.path().join("accepted").exists());
    let accepted: Value =
        serde_json::from_slice(&fs::read(root.path().join("accepted")).unwrap()).unwrap();
    // One provider invocation proves this is executing work, not merely queued admission.
    // The provider blocks before any output until the release file exists.
    wait_until(|| fs::read_to_string(root.path().join("provider-calls")).unwrap() == "1");
    let sessions = SessionManager::new(root.path().join("state/sessions"));
    let session = sessions
        .open_existing(accepted["session"].as_str().unwrap())
        .unwrap();
    assert_eq!(request(&identity, "stop").unwrap().status, "busy");
    assert!(session.try_frontend_writer().unwrap().is_none());
    daemon.0.kill().unwrap();
    assert!(!daemon.0.wait().unwrap().success());
    frontend.0.kill().unwrap();
    assert!(!frontend.0.wait().unwrap().success());
    assert!(
        identity.socket.exists(),
        "crash must leave a stale endpoint"
    );
    assert_eq!(
        connect::connect(&identity.socket).unwrap_err().kind(),
        std::io::ErrorKind::ConnectionRefused
    );
    assert!(session.try_frontend_writer().unwrap().is_some());
    let events = session.read_events().unwrap();
    assert_eq!(
        events
            .iter()
            .filter(|event| event.kind() == Some(SessionEventKind::UserInput))
            .count(),
        1
    );
    assert!(
        !events.iter().any(|event| matches!(
            event.kind(),
            Some(
                SessionEventKind::TurnStatus
                    | SessionEventKind::AssistantOutput
                    | SessionEventKind::AssistantChunk
                    | SessionEventKind::ToolCall
                    | SessionEventKind::ToolResult
            )
        )),
        "a crash must not invent output or a durable terminal record"
    );
    let interrupted_history = fs::read(session.path()).unwrap();

    fs::remove_file(root.path().join("ready")).unwrap();
    // Unblock both execution and cleanup on restart: accidental replay cannot hide
    // behind the fixture gates or leave an unjoinable worker during shutdown.
    fs::write(root.path().join("release"), "").unwrap();
    fs::write(root.path().join("cleanup"), "").unwrap();
    let mut restarted = spawn_child(root.path(), "daemon");
    wait_until(|| root.path().join("ready").exists());
    let mut reconnected = Frontend::connect(&identity);
    assert_ne!(reconnected.instance, accepted["instance"]);
    let outcome = reconnected.call(
        "operation.lookup",
        Value::Null,
        Value::Null,
        Value::Null,
        json!({"target_instance_id":accepted["instance"],"operation_id":"surviving-turn"}),
    );
    assert_eq!(outcome["state"], "unknown");
    assert!(outcome["result"].is_null());
    let claimed = reconnected.call(
        "session.claim",
        accepted["session"].clone(),
        Value::Null,
        json!("claim-after-crash"),
        json!({}),
    );
    assert_eq!(claimed["snapshot"]["phase"], "idle");
    assert!(claimed["snapshot"]["turn"].is_null());
    assert!(claimed["snapshot"]["terminal"].is_null());
    drop(reconnected);
    wait_until(|| request(&identity, "stop").unwrap().status == "stopped");
    wait_until(|| restarted.0.try_wait().unwrap().is_some());
    assert!(restarted.0.wait().unwrap().success());
    assert!(!identity.socket.exists());
    assert_eq!(
        fs::read_to_string(root.path().join("provider-calls")).unwrap(),
        "0",
        "restart must not invoke a provider for the interrupted turn"
    );
    assert_eq!(
        fs::read(session.path()).unwrap(),
        interrupted_history,
        "restart and claim must preserve interrupted history without continuing or completing it"
    );
}