magi-code 0.96.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
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
mod allocator;
mod conversion;
mod memory;

use super::limits::{CodeModeLimits, budget_snapshot};
use crate::cancellation::AgentCancellation;
pub(crate) use conversion::{serialized_size, value_reservation};
use crossbeam_channel::{Receiver, Sender, bounded};
pub(crate) use memory::MemoryBudget;
use rquickjs::{Context, Ctx, Exception, Function, Runtime, Value as JsValue, function::MutFn};
use serde_json::Value;
use std::{
    cell::{Cell, RefCell},
    rc::Rc,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::{Duration, Instant},
};

#[derive(Debug, thiserror::Error)]
#[error("Code Mode {0} limit exceeded")]
pub(crate) struct ResourceLimitExceeded(pub(crate) &'static str);

impl ResourceLimitExceeded {
    pub(crate) fn recovery(&self) -> &'static str {
        match self.0 {
            "session persisted history bytes" => {
                "Compact the session before another Code Mode call. A smaller batch cannot restore session history space. Inspect execution evidence and re-read affected files; completed effects remain applied."
            }
            "batch persisted history bytes" | "inner calls" => {
                "Continue remaining work in a new, smaller Code Mode call. Use tools.budget() to stop at a safe boundary and return progress before exhaustion. Inspect execution evidence and re-read affected files; do not replay completed actions."
            }
            "source" => "Shorten the script or split it into separate Code Mode calls.",
            "return bytes" => {
                "Return a smaller summary instead of full tool outputs. Inspect execution evidence before retrying; completed effects remain applied."
            }
            "JavaScript execution time" => {
                "Split computation into smaller Code Mode calls. Host-tool waiting does not spend JavaScript time. Inspect execution evidence before continuing; completed effects remain applied."
            }
            _ => {
                "Read narrower file ranges, retain less intermediate data, or split work into smaller Code Mode calls. Use tools.budget() before exhaustion. Inspect execution evidence and re-read affected files; completed effects remain applied."
            }
        }
    }
}

pub(crate) struct SdkRequest {
    pub(crate) operation: String,
    pub(crate) name: String,
    pub(crate) arguments: Value,
    pub(crate) memory: Arc<MemoryBudget>,
}

pub(crate) trait CodeModeEngine {
    fn execute(
        &self,
        source: &str,
        limits: &CodeModeLimits,
        cancellation: &AgentCancellation,
        host: &mut dyn FnMut(SdkRequest) -> anyhow::Result<Value>,
    ) -> anyhow::Result<Value>;
}

pub(crate) struct QuickJsEngine;

impl CodeModeEngine for QuickJsEngine {
    fn execute(
        &self,
        source: &str,
        limits: &CodeModeLimits,
        cancellation: &AgentCancellation,
        host: &mut dyn FnMut(SdkRequest) -> anyhow::Result<Value>,
    ) -> anyhow::Result<Value> {
        limits.validate()?;
        if source.len() > limits.max_source_bytes {
            return Err(
                anyhow::Error::from(ResourceLimitExceeded("source")).context(format!(
                    "Script has {} bytes; maximum {}",
                    source.len(),
                    limits.max_source_bytes
                )),
            );
        }
        cancellation.check()?;
        // The JS runtime stays on one joined worker. Host execution stays on the caller,
        // reusing its guarded lifecycle and borrowed sink/session. No detached tool work.
        std::thread::scope(|scope| {
            // Scope-local endpoints disconnect the worker before joining on host unwind.
            let (requests, incoming) = bounded(0);
            let (responses, outgoing) = bounded(0);
            let worker = std::thread::Builder::new()
                .name("code-mode-js".into())
                .stack_size(2 * 1024 * 1024)
                .spawn_scoped(scope, || {
                    run_javascript(source, limits, cancellation.clone(), requests, outgoing)
                })?;
            let mut fatal = None;
            while let Ok(request) = incoming.recv() {
                let response = if fatal.is_some() {
                    Err("Code Mode host execution terminated".to_owned())
                } else {
                    host(request).map_err(|error| {
                        fatal = Some(error);
                        "Code Mode host execution terminated".to_owned()
                    })
                };
                if responses.send(response).is_err() {
                    break;
                }
            }
            let result = worker
                .join()
                .map_err(|_| anyhow::anyhow!("Code Mode engine terminated"))?;
            if let Some(error) = fatal {
                return Err(error);
            }
            cancellation.check()?;
            result
        })
    }
}

fn run_javascript(
    source: &str,
    limits: &CodeModeLimits,
    cancellation: AgentCancellation,
    requests: Sender<SdkRequest>,
    responses: Receiver<Result<Value, String>>,
) -> anyhow::Result<Value> {
    let execution_deadline = Rc::new(Cell::new(
        Instant::now() + Duration::from_millis(limits.max_runtime_ms),
    ));
    let stopped = Arc::new(AtomicBool::new(false));
    let termination_error = Rc::new(RefCell::new(None));
    let memory = memory::MemoryBudget::new(limits.max_memory_bytes, stopped.clone());
    let _source_buffer = memory.reserve(source.len().saturating_mul(2).saturating_add(4096))?;
    let runtime = Runtime::new_with_alloc(allocator::BoundedAllocator(memory.clone()))?;
    runtime.set_max_stack_size(512 * 1024);
    let interrupt_stopped = stopped.clone();
    let interrupt_cancellation = cancellation.clone();
    let interrupt_deadline = execution_deadline.clone();
    runtime.set_interrupt_handler(Some(Box::new(move || {
        if interrupt_cancellation.is_canceled() || Instant::now() >= interrupt_deadline.get() {
            interrupt_stopped.store(true, Ordering::SeqCst);
        }
        interrupt_stopped.load(Ordering::SeqCst)
    })));
    let context = Context::full(&runtime)?;
    let limits = limits.clone();
    let callback_stopped = stopped.clone();
    let result = context.with(|ctx| -> anyhow::Result<Value> {
        // Capture before scripts can replace it. Reading data descriptors avoids
        // running exception getters (and further tool calls) while reporting errors.
        let own_property: Function = ctx.eval("Object.getOwnPropertyDescriptor")?;
        let result = (|| -> rquickjs::Result<Value> {
            let mut count = 0usize;
            let mut total_bytes = 0usize;
            let mut response_reservation = None;
            let callback_memory = memory.clone();
            let callback_cancellation = cancellation.clone();
            let callback_limits = limits.clone();
            let callback_deadline = execution_deadline.clone();
            let callback_error = termination_error.clone();
            let bridge = Function::new(
                ctx.clone(),
                MutFn::new(
                    move |ctx: Ctx<'_>,
                          operation: rquickjs::String<'_>,
                          name: rquickjs::String<'_>,
                          args: rquickjs::String<'_>|
                          -> rquickjs::Result<String> {
                        let execute = || -> anyhow::Result<String> {
                            let cancellation = &callback_cancellation;
                            cancellation.check()?;
                            if callback_stopped.load(Ordering::SeqCst) {
                                return Err(ResourceLimitExceeded(
                                    if Instant::now() >= callback_deadline.get() {
                                        "JavaScript execution time"
                                    } else {
                                        "memory"
                                    },
                                )
                                .into());
                            }
                            response_reservation.take();
                            let operation = operation.to_cstring()?;
                            let name = name.to_cstring()?;
                            let args = args.to_cstring()?;
                            let operation = checked_utf8(&operation)?;
                            let name = checked_utf8(&name)?;
                            let args = checked_utf8(&args)?;
                            let argument_charge =
                                conversion::parse_reservation(args, cancellation)?
                                    .saturating_add(name.len())
                                    .saturating_add(operation.len());
                            let _arguments = callback_memory.reserve(argument_charge)?;
                            if operation == "call" {
                                if count >= callback_limits.max_inner_calls {
                                    return Err(anyhow::Error::from(ResourceLimitExceeded(
                                        "inner calls",
                                    ))
                                    .context(format!(
                                        "Rejected tool call: {count} calls used; maximum {}",
                                        callback_limits.max_inner_calls
                                    )));
                                }
                                count += 1;
                            }
                            let request = SdkRequest {
                                operation: operation.to_owned(),
                                name: name.to_owned(),
                                arguments: serde_json::from_str(args)?,
                                memory: callback_memory.clone(),
                            };
                            if Instant::now() >= callback_deadline.get() {
                                return Err(
                                    ResourceLimitExceeded("JavaScript execution time").into()
                                );
                            }
                            // Host tools keep their own timeouts; waiting spends no JS budget.
                            let waiting = Instant::now();
                            requests.send(request)?;
                            let response = responses.recv();
                            callback_deadline.set(callback_deadline.get() + waiting.elapsed());
                            let mut value = response?.map_err(anyhow::Error::msg)?;
                            cancellation.check()?;
                            if operation == "budget" {
                                let remaining_ms = callback_deadline
                                    .get()
                                    .saturating_duration_since(Instant::now())
                                    .as_millis()
                                    as u64;
                                value["inner_calls"] = budget_snapshot(
                                    count as u64,
                                    callback_limits.max_inner_calls as u64,
                                );
                                value["result_bytes"] = budget_snapshot(
                                    total_bytes as u64,
                                    callback_limits.max_total_result_bytes as u64,
                                );
                                value["runtime_ms"] = budget_snapshot(
                                    callback_limits.max_runtime_ms.saturating_sub(remaining_ms),
                                    callback_limits.max_runtime_ms,
                                );
                                value["memory_bytes"] = callback_memory.snapshot();
                                value["inner_result_bytes_limit"] =
                                    (callback_limits.max_inner_result_bytes as u64).into();
                                value["return_bytes_limit"] =
                                    (callback_limits.max_return_bytes as u64).into();
                            }
                            let _native_result = callback_memory
                                .reserve(conversion::value_reservation(&value, cancellation)?)?;
                            let remaining_bytes = callback_limits
                                .max_total_result_bytes
                                .saturating_sub(total_bytes);
                            let limit = if operation == "budget" {
                                4096
                            } else {
                                callback_limits.max_inner_result_bytes.min(remaining_bytes)
                            };
                            let size = conversion::serialized_size(&value, limit, cancellation)
                                .map_err(|error| {
                                    if error.downcast_ref::<ResourceLimitExceeded>().is_some() {
                                        anyhow::Error::from(ResourceLimitExceeded(
                                            if remaining_bytes
                                                < callback_limits.max_inner_result_bytes
                                            {
                                                "total result bytes"
                                            } else {
                                                "inner result bytes"
                                            },
                                        ))
                                        .context(format!("SDK result exceeded {limit} bytes"))
                                    } else {
                                        error
                                    }
                                })?;
                            response_reservation = Some(callback_memory.reserve(size)?);
                            let mut bytes = Vec::with_capacity(size);
                            serde_json::to_writer(&mut bytes, &value)?;
                            let text = String::from_utf8(bytes)?;
                            if operation != "budget" {
                                total_bytes = total_bytes.saturating_add(size);
                            }
                            Ok(text)
                        };
                        match execute() {
                            Ok(text) => Ok(text),
                            Err(error) => {
                                let message = format!("{error:#}");
                                if callback_error.borrow().is_none() {
                                    *callback_error.borrow_mut() = Some(error);
                                }
                                callback_stopped.store(true, Ordering::SeqCst);
                                Err(Exception::throw_message(&ctx, &message))
                            }
                        }
                    },
                ),
            )?;
            ctx.globals().set("__bridge", bridge)?;
            // Capture native JSON.stringify before user code can replace it. The bridge is
            // removed from globals; the SDK itself has no mutable host configuration.
            ctx.eval::<(), _>(
                r#"
            (() => {
                const nativeBridge = globalThis.__bridge;
                const stringify = JSON.stringify, parse = JSON.parse;
                const bridge = (...args) => parse(nativeBridge(...args));
                delete globalThis.__bridge;
                Object.defineProperty(globalThis, 'tools', {value: Object.freeze({
                    list: () => bridge('list', '', '{}'),
                    search: query => bridge('search', String(query), '{}'),
                    describe: name => bridge('describe', String(name), '{}'),
                    budget: () => bridge('budget', '', '{}'),
                    call: (name, args) => bridge('call', String(name), stringify(args))
                }), writable: false, configurable: false});
            })();
        "#,
            )?;
            let value: JsValue =
                ctx.eval(format!("(function() {{ 'use strict';\n{source}\n}})()"))?;
            if value.is_promise() {
                return Err(Exception::throw_message(
                    &ctx,
                    "Code Mode requires synchronous JavaScript",
                ));
            }
            let text = ctx
                .json_stringify(value)?
                .map(|text| text.to_cstring())
                .transpose()?;
            let Some(text) = text else {
                return Ok(Value::Null);
            };
            if text.len() > limits.max_return_bytes {
                if termination_error.borrow().is_none() {
                    *termination_error.borrow_mut() = Some(
                        anyhow::Error::from(ResourceLimitExceeded("return bytes")).context(
                            format!(
                                "Return has {} bytes; maximum {}",
                                text.len(),
                                limits.max_return_bytes
                            ),
                        ),
                    );
                }
                stopped.store(true, Ordering::SeqCst);
                return Err(Exception::throw_message(
                    &ctx,
                    "Code Mode return limit exceeded",
                ));
            }
            let text = checked_utf8(&text).map_err(|_| {
                Exception::throw_message(&ctx, "Code Mode return must be UTF-8 JSON")
            })?;
            let charge = conversion::parse_reservation(text, &cancellation)
                .and_then(|bytes| memory.reserve(bytes))
                .map_err(|_| {
                    Exception::throw_message(&ctx, "Code Mode return conversion limit exceeded")
                })?;
            let value = serde_json::from_str(text)
                .map_err(|_| Exception::throw_message(&ctx, "Code Mode return must be JSON"));
            drop(charge);
            value
        })();
        result.map_err(|error| {
            let detail = (error.is_exception() && !stopped.load(Ordering::SeqCst))
                .then(|| javascript_exception_message(&ctx, &own_property, &memory))
                .flatten()
                .unwrap_or_else(|| error.to_string());
            anyhow::anyhow!(
                "Code Mode JavaScript failed: {detail}; completed effects remain applied"
            )
        })
    });
    cancellation.check()?;
    if let Some(error) = termination_error.borrow_mut().take() {
        return Err(error);
    }
    if Instant::now() >= execution_deadline.get() {
        return Err(ResourceLimitExceeded("JavaScript execution time").into());
    }
    if stopped.load(Ordering::SeqCst) {
        return Err(ResourceLimitExceeded("memory").into());
    }
    result
}

fn javascript_exception_message<'js>(
    ctx: &Ctx<'js>,
    own_property: &Function<'js>,
    memory: &Arc<MemoryBudget>,
) -> Option<String> {
    let exception = ctx.catch();
    let location = exception
        .is_error()
        .then(|| user_script_error_location(own_property, &exception))
        .flatten();
    let message = if exception.is_error() {
        own_data_property_string(own_property, &exception, "message")?
    } else {
        exception.into_string()?
    };
    let text = message.to_cstring().ok()?;
    let _redaction = memory.reserve(text.len().saturating_mul(4)).ok()?;
    let text = crate::output::redact_sensitive_text(checked_utf8(&text).ok()?);
    let text: String = text.chars().take(2048).collect();
    Some(match location {
        Some((line, column)) => format!("{text} (line {line}, column {column})"),
        None => text,
    })
}

fn own_data_property_string<'js>(
    own_property: &Function<'js>,
    object: &JsValue<'js>,
    name: &str,
) -> Option<rquickjs::String<'js>> {
    let descriptor: JsValue = own_property.call((object.clone(), name)).ok()?;
    if !descriptor.is_object() {
        return None;
    }
    // Accessor descriptors have no own `value`. A second descriptor lookup
    // also avoids inherited getters on a script-modified Object.prototype.
    let value: JsValue = own_property.call((descriptor, "value")).ok()?;
    value.as_object()?.get::<_, rquickjs::String>("value").ok()
}

/// Maps the first QuickJS `eval_script:LINE:COLUMN` stack frame to the user's
/// source, whose first line follows the one-line strict-mode wrapper.
fn user_script_error_location<'js>(
    own_property: &Function<'js>,
    exception: &JsValue<'js>,
) -> Option<(usize, usize)> {
    let stack = own_data_property_string(own_property, exception, "stack")?
        .to_string()
        .ok()?;
    let (_, frame) = stack.split_once("eval_script:")?;
    let mut parts = frame.splitn(3, ':');
    let line: usize = parts.next()?.parse().ok()?;
    let column: usize = parts
        .next()?
        .trim_end_matches(|character: char| !character.is_ascii_digit())
        .parse()
        .ok()?;
    Some((line.checked_sub(1).filter(|line| *line > 0)?, column))
}

fn checked_utf8<'a>(text: &'a rquickjs::CString<'_>) -> anyhow::Result<&'a str> {
    // QuickJS preserves unmatched UTF-16 surrogates in C strings. Do not use
    // rquickjs 0.9's unchecked CString::as_str on script-controlled input.
    // SAFETY: CString owns a live allocation of len bytes until the borrow ends.
    let bytes = unsafe { std::slice::from_raw_parts(text.as_ptr().cast::<u8>(), text.len()) };
    Ok(std::str::from_utf8(bytes)?)
}