magi-code 0.105.0

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
use super::{
    ToolResult, ToolResultDisplay,
    args::{FILE_READ_MAX_BYTES, FILE_READ_MAX_FILES, FILE_WRITE_MAX_BYTES, ReadArgs, WriteArgs},
    contract::{metadata_key as meta, tool_name},
    skill_provenance::digest_hex,
};
use crate::{
    cancellation::AgentCancellation,
    checkpoints::SnapshotTool,
    persistence::{CrossProcessFileLock, atomic_write},
};
use serde_json::{Value, json};
use std::{
    fs,
    path::{Path, PathBuf},
    sync::Mutex,
};

mod mutation;
mod path_policy;
mod path_suggestions;
mod read_format;
pub(super) use mutation::MutationContext;
pub(super) use path_policy::{ExistingPathPolicy, PathContext, resolve_existing_path_with_policy};
use read_format::{MultiReadAppend, append_multi_read_section, format_read_failure};

pub(super) struct ReadSnapshot {
    pub(super) path: PathBuf,
    pub(super) text: String,
    pub(super) seen_lines: Vec<usize>,
}

pub(super) struct ReadContext<'a> {
    pub(super) paths: PathContext<'a>,
    pub(super) settings: &'a crate::config::ReadToolSettings,
    pub(super) hashline_snapshots: &'a Mutex<super::hash_edit::HashlineSnapshotStore>,
    pub(super) pending_snapshots: Option<&'a Mutex<Vec<ReadSnapshot>>>,
    pub(super) skills: super::skill::SkillContext<'a>,
}

pub(super) struct WriteContext<'a> {
    pub(super) paths: PathContext<'a>,
    pub(super) settings: &'a crate::config::WriteToolSettings,
    pub(super) mutation: MutationContext<'a>,
    pub(super) humanize_protection: &'a Option<crate::protection::humanize::HumanizeProtection>,
}

impl ReadContext<'_> {
    pub(super) fn read(
        &self,
        args: ReadArgs,
        cancellation: &AgentCancellation,
    ) -> anyhow::Result<ToolResult> {
        if !args.is_multi_file() {
            let requested_path = args
                .requested_paths()
                .first()
                .ok_or_else(|| anyhow::anyhow!("read requires paths"))?;
            let mut resource = match self.read_one_resource(requested_path, cancellation) {
                Ok(resource) => resource,
                Err(error) => {
                    cancellation.check()?;
                    let message = error.to_string();
                    let error_text = format_read_failure(requested_path, "error", &message);
                    let mut content = String::new();
                    if append_multi_read_section(&mut content, &error_text, 0)
                        == MultiReadAppend::Omitted
                    {
                        anyhow::bail!("read resource identifier exceeds the output limit");
                    }
                    return Ok(ToolResult {
                        tool_name: tool_name::READ.to_string(),
                        success: false,
                        metadata: json!({
                            "path": requested_path,
                            "structured_result": {
                                "files": [{"path": requested_path, "text": content,
                                    "success": false, "complete": false,
                                    "request_complete": false, "error": message}],
                                "complete": false, "request_complete": false, "outcome": "failed",
                            },
                        }),
                        content,
                        display: ToolResultDisplay::default(),
                    });
                }
            };
            let delivered_bytes = resource.content.len();
            self.register_read_snapshot(&mut resource, delivered_bytes)?;
            let mut metadata = resource.metadata;
            if let Some(provenance) = resource.provenance {
                metadata.insert("skill_reads".to_string(), json!([provenance]));
            }
            metadata.insert(meta::BYTE_LIMIT.to_string(), json!(FILE_READ_MAX_BYTES));
            metadata.insert(
                "structured_result".to_string(),
                json!({
                    "files": [{
                        "path": requested_path, "text": resource.content,
                        "success": true,
                        "complete": !resource.truncated,
                        "request_complete": resource.request_complete,
                    }],
                    "complete": !resource.truncated,
                    "request_complete": resource.request_complete,
                    "outcome": if resource.request_complete { "complete" } else { "partial" },
                }),
            );
            return Ok(ToolResult {
                tool_name: tool_name::READ.to_string(),
                success: true,
                content: resource.content,
                metadata: Value::Object(metadata),
                display: ToolResultDisplay::default(),
            });
        }

        let requested_paths = args.requested_paths();
        let omitted_sections: Vec<_> = requested_paths
            .iter()
            .map(|path| {
                format_read_failure(path, "omitted", "aggregate read output limit exceeded")
            })
            .collect();
        let mut reserved_bytes = omitted_sections
            .iter()
            .map(|text| text.len() + 2)
            .sum::<usize>();
        anyhow::ensure!(
            reserved_bytes <= FILE_READ_MAX_BYTES as usize,
            "read resource identifiers exceed the aggregate output limit"
        );
        let mut output = String::new();
        let mut summaries = Vec::with_capacity(requested_paths.len());
        let mut succeeded = 0usize;
        let mut failed = 0usize;
        let mut truncated = false;
        let mut aggregate_truncated = false;
        let mut skill_reads = Vec::new();
        let mut files = Vec::new();

        for (index, requested_path) in requested_paths.iter().enumerate() {
            reserved_bytes -= omitted_sections[index].len() + 2;
            match self.read_one_resource(requested_path, cancellation) {
                Ok(mut resource) => {
                    let appended =
                        append_multi_read_section(&mut output, &resource.content, reserved_bytes);
                    match &appended {
                        MultiReadAppend::Complete => {
                            let delivered_bytes = resource.content.len();
                            self.register_read_snapshot(&mut resource, delivered_bytes)?;
                        }
                        MultiReadAppend::Truncated { body_bytes, .. } => {
                            self.register_read_snapshot(&mut resource, *body_bytes)?;
                        }
                        MultiReadAppend::Omitted => {}
                    }
                    let body = resource.content;
                    let provenance = resource.provenance;
                    let mut summary = resource.metadata;
                    summary.insert("requested_path".to_string(), json!(requested_path));
                    match appended {
                        MultiReadAppend::Complete => {
                            files.push(json!({"path": requested_path, "text": body,
                                "success": true, "complete": !resource.truncated,
                                "request_complete": resource.request_complete}));
                            succeeded += 1;
                            truncated |= resource.truncated;
                            summary.insert("success".to_string(), json!(true));
                            summaries.push(Value::Object(summary));
                            if let Some(mut provenance) = provenance {
                                provenance.start = output.len() - body.len();
                                provenance.end = output.len();
                                provenance.sha256 = digest_hex(
                                    &output.as_bytes()[provenance.start..provenance.end],
                                );
                                skill_reads.push(provenance);
                            }
                        }
                        MultiReadAppend::Truncated { text, .. } => {
                            files.push(json!({"path": requested_path, "text": text,
                                "success": true, "complete": false, "request_complete": false,
                                "error": "aggregate read output limit exceeded"}));
                            succeeded += 1;
                            aggregate_truncated = true;
                            truncated = true;
                            summary.insert("success".to_string(), json!(true));
                            summary.insert("truncated".to_string(), json!(true));
                            summary.insert("request_complete".to_string(), json!(false));
                            summaries.push(Value::Object(summary));
                            break;
                        }
                        MultiReadAppend::Omitted => {
                            aggregate_truncated = true;
                            truncated = true;
                            break;
                        }
                    }
                }
                Err(error) => {
                    cancellation.check()?;
                    let message = error.to_string();
                    let error_text = format_read_failure(requested_path, "error", &message);
                    let appended =
                        append_multi_read_section(&mut output, &error_text, reserved_bytes);
                    let (text, clipped) = match appended {
                        MultiReadAppend::Complete => (error_text, false),
                        MultiReadAppend::Truncated { text, .. } => (text, true),
                        MultiReadAppend::Omitted => {
                            aggregate_truncated = true;
                            truncated = true;
                            break;
                        }
                    };
                    failed += 1;
                    files.push(
                        json!({"path": requested_path, "text": text, "success": false,
                        "complete": false, "request_complete": false, "error": message}),
                    );
                    summaries.push(json!({
                        "requested_path": requested_path,
                        "success": false,
                        "error": message,
                    }));
                    if clipped {
                        aggregate_truncated = true;
                        truncated = true;
                        break;
                    }
                }
            }
        }

        cancellation.check()?;
        let omitted = requested_paths.len() - files.len();
        for (path, text) in requested_paths[files.len()..]
            .iter()
            .zip(&omitted_sections[files.len()..])
        {
            if !output.is_empty() {
                output.push_str("\n\n");
            }
            output.push_str(text);
            files.push(
                json!({"path": path, "text": text, "success": false, "omitted": true,
                "complete": false, "request_complete": false,
                "error": "aggregate read output limit exceeded"}),
            );
            summaries.push(json!({"requested_path": path, "success": false,
                "omitted": true, "request_complete": false,
                "error": "aggregate read output limit exceeded"}));
        }

        let request_complete = files.iter().all(|file| file["request_complete"] == true);
        let outcome = if succeeded == 0 {
            "failed"
        } else if request_complete {
            "complete"
        } else {
            "partial"
        };

        if failed > 0 || truncated || aggregate_truncated || skill_reads.len() > 8 {
            skill_reads.clear();
        }
        Ok(ToolResult {
            tool_name: tool_name::READ.to_string(),
            success: succeeded > 0,
            content: output,
            metadata: json!({
                (meta::PATHS): requested_paths,
                (meta::FILES): requested_paths.len(),
                (meta::SUCCEEDED): succeeded,
                (meta::FAILED): failed,
                "omitted": omitted,
                (meta::BYTE_LIMIT): FILE_READ_MAX_BYTES,
                "file_limit": FILE_READ_MAX_FILES,
                (meta::TRUNCATED): truncated,
                "aggregate_truncated": aggregate_truncated,
                "results": summaries,
                "skill_reads": skill_reads,
                "structured_result": {
                    "files": files,
                    "complete": failed == 0 && !truncated && !aggregate_truncated,
                    "request_complete": request_complete,
                    "outcome": outcome,
                },
            }),
            display: ToolResultDisplay::default(),
        })
    }
}

impl WriteContext<'_> {
    pub(super) fn write_file_outcome(
        &self,
        args: WriteArgs,
        cancellation: &AgentCancellation,
    ) -> anyhow::Result<super::dispatch::FilesystemOutcome> {
        self.write_file_with(args, cancellation, atomic_write)
    }

    fn write_file_with(
        &self,
        args: WriteArgs,
        cancellation: &AgentCancellation,
        write: impl FnOnce(&Path, &[u8]) -> anyhow::Result<()>,
    ) -> anyhow::Result<super::dispatch::FilesystemOutcome> {
        cancellation.check()?;
        if args.content.len() > FILE_WRITE_MAX_BYTES {
            anyhow::bail!(
                "write content is {} bytes; limit is {FILE_WRITE_MAX_BYTES} bytes",
                args.content.len()
            );
        }
        let path = self.resolve_for_write(&args.path)?;
        let lock = self.mutation.file_lock(&path)?;
        let _guard = lock
            .lock()
            .map_err(|_| anyhow::anyhow!("file mutation lock poisoned for '{}'", path.display()))?;
        let _cross_process_guard = CrossProcessFileLock::acquire(&path)?;
        let pre_snapshot = self.mutation.pre_snapshot_bytes(&path)?;
        let prose_check = if let Some(protection) = &self.humanize_protection {
            let before = (|| -> anyhow::Result<String> {
                match crate::persistence::open_regular_file(&path, false) {
                    Ok(file) => Ok(String::from_utf8(
                        crate::persistence::read_regular_file_bounded(
                            &file,
                            FILE_WRITE_MAX_BYTES as u64,
                        )?,
                    )?),
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(String::new()),
                    Err(error) => Err(error.into()),
                }
            })();
            Some(match before {
                Ok(before) => protection.check_changes([(
                    path.as_path(),
                    before.as_str(),
                    args.content.as_str(),
                )]),
                Err(error) => protection.unavailable(error),
            })
        } else {
            None
        };
        cancellation.check()?;
        if let Some(result) = prose_check
            .as_ref()
            .and_then(|check| check.blocked_result(tool_name::WRITE))
        {
            return Ok(super::dispatch::FilesystemOutcome {
                result,
                paths: Vec::new(),
            });
        }
        if let Some(parent) = path.parent() {
            cancellation.check()?;
            fs::create_dir_all(parent)?;
            self.ensure_parent_inside_after_creation(
                parent,
                self.settings.absolute_paths && PathBuf::from(&args.path).is_absolute(),
            )?;
        }
        cancellation.check()?;
        match write(&path, args.content.as_bytes()) {
            Ok(()) => {}
            Err(error)
                if error
                    .downcast_ref::<crate::persistence::AtomicWriteCommittedButUndurable>()
                    .is_some() =>
            {
                self.mutation.record_file_snapshot(
                    SnapshotTool::WriteFile,
                    &path,
                    pre_snapshot.as_deref(),
                    Some(args.content.as_bytes()),
                );
                return Ok(super::dispatch::FilesystemOutcome {
                    result: ToolResult {
                        tool_name: tool_name::WRITE.to_string(),
                        success: false,
                        content: error.to_string(),
                        metadata: json!({
                            (meta::PATH): path,
                            (meta::BYTES): args.content.len(),
                            "outcome": "committed_but_undurable",
                        }),
                        display: ToolResultDisplay::default(),
                    },
                    paths: vec![path],
                });
            }
            Err(error) => return Err(error),
        }
        self.mutation.record_file_snapshot(
            SnapshotTool::WriteFile,
            &path,
            pre_snapshot.as_deref(),
            Some(args.content.as_bytes()),
        );
        let mut outcome = super::dispatch::FilesystemOutcome {
            result: ToolResult {
                tool_name: tool_name::WRITE.to_string(),
                success: true,
                content: format!("wrote {} bytes", args.content.len()),
                metadata: json!({(meta::PATH): path, (meta::BYTES): args.content.len()}),
                display: ToolResultDisplay::default(),
            },
            paths: vec![path],
        };
        if let Some(check) = prose_check {
            outcome.result.metadata["humanize_protection"] = check.metadata;
        }
        Ok(outcome)
    }
}