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
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
use super::{ProviderRunOptions, bounded_auto_compaction_diagnostic, reborrow_output_sink};
use crate::{
    agent::steering::AgentSteering,
    agent::{AgentRunOutput, AgentRunRequest, AgentSession},
    cancellation::is_run_canceled,
    config::AutoCompactionLimit,
    output::{OutputEvent, UserPromptOrigin},
};
use anyhow::Result;
use std::sync::{Arc, atomic::AtomicU64};

use super::preparation::PreparedRun;

pub(super) fn run(
    prepared: PreparedRun<'_>,
    instructions: &[crate::instructions::InstructionFile],
    options: &mut ProviderRunOptions<'_, '_>,
    steering: Option<AgentSteering>,
) -> Result<AgentRunOutput> {
    let PreparedRun {
        active_config,
        settings,
        context_budget,
        cancellation,
        parent_agent_for_provider,
        provider,
        hooks,
        tools,
        mut title_job,
        auto,
        auto_eligible,
        auto_policy,
        herdr_reporter,
    } = prepared;
    let config = active_config.as_ref();
    let mut prompt = options.prompt.to_string();
    let mut prompt_origin = UserPromptOrigin::User;
    let mut effective_prompt: Option<String> = None;
    let mut combined = AgentRunOutput::default();
    let mut auto_compaction_count = 0_usize;
    let compaction_limit: AutoCompactionLimit = auto.compaction_limit();
    let mut preflight_compaction_used = false;
    let request_sequence = Arc::new(AtomicU64::new(0));
    let mut projected_candidate: Option<(String, String)> = None;

    if auto_eligible {
        cancellation.check()?;
        let session = options
            .session
            .expect("auto-compaction eligibility requires session");
        let effective_current_prompt = AgentSession::effective_prompt_for_projection(
            &prompt,
            Some(&tools),
            options.invocation_mode,
        );
        let hard_threshold = context_budget.threshold_tokens();
        let static_tokens = parent_agent_for_provider
            .project_prompt_input_tokens_without_session(&effective_current_prompt, Some(&tools))?;
        effective_prompt = Some(effective_current_prompt.clone());
        if static_tokens <= hard_threshold && compaction_limit.allows(auto_compaction_count) {
            let current_tokens = parent_agent_for_provider.project_prompt_input_tokens(
                &effective_current_prompt,
                session,
                Some(&tools),
            )?;
            if current_tokens > hard_threshold {
                preflight_compaction_used = true;
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::CompactionTriggered {
                        current_tokens,
                        max_tokens: parent_agent_for_provider.context_max_tokens(),
                        threshold: format!("preflight hard budget ({hard_threshold} tokens)"),
                    })?;
                    sink.output_event(OutputEvent::CompactionStarted)?;
                }
                let compaction = compact_accounted(
                    crate::compaction::CompactSessionJob {
                        active_config: config.clone(),
                        settings: settings.clone(),
                        session: session.clone(),
                        cwd: options.cwd.to_path_buf(),
                        cancellation: cancellation.clone(),
                        custom_instructions: None,
                        additional_instructions: None,
                    },
                    &mut combined,
                    &request_sequence,
                    &mut options.output_sink,
                );
                let summary = match compaction {
                    Ok(Some(result)) => {
                        super::emit_compaction_fast_observation(
                            &mut options.output_sink,
                            &result,
                            &request_sequence,
                        )?;
                        if let Some(warning) = result.rotation_warning.as_ref()
                            && let Some(sink) = options.output_sink.as_deref_mut()
                        {
                            sink.output_event(OutputEvent::Diagnostic {
                                level: "warning".to_string(),
                                message: warning.clone(),
                            })?;
                        }
                        result.summary
                    }
                    Ok(None) => {
                        let message = bounded_auto_compaction_diagnostic(
                            "automatic preflight compaction produced no usable summary; old primary remains authoritative; no provider request was sent",
                        );
                        if let Some(sink) = options.output_sink.as_deref_mut() {
                            sink.output_event(OutputEvent::CompactionFailed {
                                message: message.clone(),
                                canceled: false,
                            })?;
                            sink.output_event(OutputEvent::Diagnostic {
                                level: "error".to_string(),
                                message: message.clone(),
                            })?;
                        }
                        return Err(anyhow::anyhow!(message));
                    }
                    Err(error) if is_run_canceled(&error) => {
                        let message = "automatic preflight compaction canceled; old primary remains authoritative; no provider request was sent".to_string();
                        if let Some(sink) = options.output_sink.as_deref_mut() {
                            sink.output_event(OutputEvent::CompactionFailed {
                                message: message.clone(),
                                canceled: true,
                            })?;
                            sink.output_event(OutputEvent::Diagnostic {
                                level: "warning".to_string(),
                                message: message.clone(),
                            })?;
                        }
                        return Err(error);
                    }
                    Err(error) => {
                        let authority = error
                            .downcast_ref::<crate::sessions::CompactionRotationError>()
                            .map(|_| "")
                            .unwrap_or(" old primary remains authoritative;");
                        let message = bounded_auto_compaction_diagnostic(format!(
                            "automatic preflight compaction failed;{authority} no provider request was sent: {error}"
                        ));
                        if let Some(sink) = options.output_sink.as_deref_mut() {
                            sink.output_event(OutputEvent::CompactionFailed {
                                message: message.clone(),
                                canceled: false,
                            })?;
                            sink.output_event(OutputEvent::Diagnostic {
                                level: "error".to_string(),
                                message: message.clone(),
                            })?;
                        }
                        return Err(anyhow::anyhow!(message));
                    }
                };
                let projected_tokens = parent_agent_for_provider.project_prompt_input_tokens(
                    &effective_current_prompt,
                    session,
                    Some(&tools),
                )?;
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::CompactionCompleted {
                        current_tokens: projected_tokens,
                        max_tokens: parent_agent_for_provider.context_max_tokens(),
                        summary: summary.clone(),
                    })?;
                }
                let _compacted_tokens = match parent_agent_for_provider.ensure_prompt_context_fits(
                    &effective_current_prompt,
                    session,
                    Some(&tools),
                ) {
                    Ok(tokens) => tokens,
                    Err(error) => {
                        let message = bounded_auto_compaction_diagnostic(format!(
                            "automatic preflight compaction completed, but current prompt still exceeds hard context budget; new checkpoint remains authoritative; no provider request was sent: {error}"
                        ));
                        if let Some(sink) = options.output_sink.as_deref_mut() {
                            sink.output_event(OutputEvent::Diagnostic {
                                level: "error".to_string(),
                                message: message.clone(),
                            })?;
                        }
                        return Err(anyhow::anyhow!(message));
                    }
                };
                auto_compaction_count = auto_compaction_count.saturating_add(1);
            }
        }
    }

    loop {
        let request = AgentRunRequest {
            prompt: &prompt,
            prompt_origin,
            effective_prompt: effective_prompt.as_deref(),
            tools: Some(&tools),
            hooks: Some(&hooks),
            session: options.session,
            cwd: options.cwd,
            output_sink: reborrow_output_sink(&mut options.output_sink),
            cancellation: cancellation.clone(),
            session_title_job: title_job.take(),
            semantic_progress_timeout: Some(settings.provider_stream.semantic_progress_timeout()),
            invocation_mode: options.invocation_mode,
            agent_id: options
                .selected_primary_agent
                .as_ref()
                .map(|profile| profile.id.clone()),
            initial_instructions: instructions,
            ttsr: settings.ttsr.clone(),
            herdr_reporter: herdr_reporter.clone(),
            continuation_auto_compaction_policy: (auto_eligible
                && compaction_limit.allows(auto_compaction_count))
            .then(|| auto_policy.clone())
            .flatten(),
        };
        let output_result = match steering.as_ref() {
            Some(steering) if auto_eligible => parent_agent_for_provider
                .run_print_with_tools_streaming_output_cancellable_untracked(
                    provider.as_ref(),
                    request,
                    Some(steering.clone()),
                    true,
                    Arc::clone(&request_sequence),
                ),
            Some(steering) => parent_agent_for_provider
                .run_print_with_tools_streaming_output_cancellable_untracked(
                    provider.as_ref(),
                    request,
                    Some(steering.clone()),
                    false,
                    Arc::clone(&request_sequence),
                ),
            None => parent_agent_for_provider
                .run_print_with_tools_streaming_output_cancellable_untracked(
                    provider.as_ref(),
                    request,
                    None,
                    false,
                    Arc::clone(&request_sequence),
                ),
        };
        let mut continuation_context_overflow = None;
        let output = match output_result {
            Ok(output) => output,
            Err(error)
                if auto_eligible
                    && compaction_limit.allows(auto_compaction_count)
                    && error
                        .downcast_ref::<crate::agent::ContextBudgetError>()
                        .is_some_and(|budget| {
                            budget.phase() == crate::agent::ContextBudgetPhase::Continuation
                        }) =>
            {
                let budget = error
                    .downcast::<crate::agent::ContextBudgetError>()
                    .expect("context budget error checked above");
                let threshold = budget.threshold_tokens();
                let threshold_display = auto_policy
                    .as_ref()
                    .filter(|(soft_threshold, _)| *soft_threshold == threshold)
                    .map(|(_, display)| display.clone())
                    .unwrap_or_else(|| format!("continuation hard budget ({threshold} tokens)"));
                continuation_context_overflow = Some((
                    budget.estimated_tokens(),
                    threshold_display,
                    budget.to_string(),
                ));
                budget.into_partial_output().unwrap_or_default()
            }
            Err(error)
                if preflight_compaction_used
                    && error
                        .downcast_ref::<crate::agent::RequiredUserInputPersistenceError>()
                        .is_some() =>
            {
                let message = bounded_auto_compaction_diagnostic(format!(
                    "current user input persistence failed after preflight compaction; new checkpoint remains authoritative; no provider request was sent: {error}"
                ));
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::Diagnostic {
                        level: "error".to_string(),
                        message: message.clone(),
                    })?;
                }
                return Err(anyhow::anyhow!(message));
            }
            Err(error)
                if auto_compaction_count > 0
                    && error
                        .downcast_ref::<crate::agent::RequiredUserInputPersistenceError>()
                        .is_some() =>
            {
                let message = bounded_auto_compaction_diagnostic(format!(
                    "automatic continuation persistence failed; new checkpoint remains authoritative; no provider continuation was sent: {error}"
                ));
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::Diagnostic {
                        level: "error".to_string(),
                        message: message.clone(),
                    })?;
                }
                return Err(anyhow::anyhow!(message));
            }
            Err(error) => return Err(error),
        };
        let persistence_degraded = output.persistence_degraded;
        let recovered_incomplete_stream = output.recovered_incomplete_stream;
        let auto_compaction_blocked_by_recovery = output.auto_compaction_blocked_by_recovery;
        if !combined.text.is_empty() && !output.text.is_empty() {
            combined.text.push('\n');
        }
        combined.text.push_str(&output.text);
        combined.usage = output.usage;
        if !matches!(output.fast_outcome, crate::fast::FastOutcome::NotRequested) {
            combined.fast_outcome = output.fast_outcome.clone();
        }
        combined.total_tokens = match (combined.total_tokens, output.total_tokens) {
            (Some(total), Some(next)) => Some(total.saturating_add(next)),
            (None, next) => next,
            (total, None) => total,
        };
        combined.tool_results.extend(output.tool_results);
        combined.persistence_degraded |= persistence_degraded;
        combined.recovered_incomplete_stream |= recovered_incomplete_stream;
        combined.auto_compaction_blocked_by_recovery |= auto_compaction_blocked_by_recovery;

        if !auto_eligible || !compaction_limit.allows(auto_compaction_count) {
            return Ok(combined);
        }
        if persistence_degraded || auto_compaction_blocked_by_recovery {
            if let Some((_, _, message)) = continuation_context_overflow {
                return Err(anyhow::anyhow!(message));
            }
            return Ok(combined);
        }
        cancellation.check()?;
        let session = options
            .session
            .expect("auto-compaction eligibility requires session");
        let observed_steering = steering.as_ref().and_then(AgentSteering::observe_collapsed);
        let candidate = observed_steering
            .as_ref()
            .map(|batch| batch.text.as_str())
            .unwrap_or("continue");
        let effective_candidate = match projected_candidate.as_ref() {
            Some((raw, effective)) if raw == candidate => effective.clone(),
            _ => {
                let effective = AgentSession::effective_prompt_for_projection(
                    candidate,
                    Some(&tools),
                    options.invocation_mode,
                );
                projected_candidate = Some((candidate.to_string(), effective.clone()));
                effective
            }
        };
        let current_tokens = parent_agent_for_provider.project_prompt_input_tokens(
            &effective_candidate,
            session,
            Some(&tools),
        )?;
        let max_tokens = parent_agent_for_provider.context_max_tokens();
        if continuation_context_overflow.is_none()
            && !auto.triggered(current_tokens as u64, max_tokens as u64)
        {
            if let Some(batch) = observed_steering {
                prompt = batch.text;
                effective_prompt = Some(effective_candidate);
                prompt_origin = UserPromptOrigin::Steering;
                continue;
            }
            return Ok(combined);
        }
        let (trigger_tokens, trigger_threshold) = continuation_context_overflow
            .as_ref()
            .map(|(estimated, threshold, _)| (*estimated, threshold.clone()))
            .unwrap_or_else(|| {
                (
                    current_tokens,
                    auto.threshold_display()
                        .unwrap_or_else(|| "invalid threshold".to_string()),
                )
            });
        if let Some(sink) = options.output_sink.as_deref_mut() {
            sink.output_event(OutputEvent::CompactionTriggered {
                current_tokens: trigger_tokens,
                max_tokens,
                threshold: trigger_threshold,
            })?;
            sink.output_event(OutputEvent::CompactionStarted)?;
        }

        let compaction = compact_accounted(
            crate::compaction::CompactSessionJob {
                active_config: config.clone(),
                settings: settings.clone(),
                session: session.clone(),
                cwd: options.cwd.to_path_buf(),
                cancellation: cancellation.clone(),
                custom_instructions: None,
                additional_instructions: None,
            },
            &mut combined,
            &request_sequence,
            &mut options.output_sink,
        );
        let result = match compaction {
            Ok(Some(result)) => result,
            Ok(None) => {
                let message = "automatic compaction produced no usable summary; old primary remains authoritative; no continuation was sent";
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::CompactionFailed {
                        message: message.to_string(),
                        canceled: false,
                    })?;
                    sink.output_event(OutputEvent::Diagnostic {
                        level: "error".to_string(),
                        message: message.to_string(),
                    })?;
                }
                anyhow::bail!(message)
            }
            Err(error) => {
                if is_run_canceled(&error) {
                    let message = "automatic compaction canceled; old primary remains authoritative; no continuation was sent";
                    if let Some(sink) = options.output_sink.as_deref_mut() {
                        sink.output_event(OutputEvent::CompactionFailed {
                            message: message.to_string(),
                            canceled: true,
                        })?;
                        sink.output_event(OutputEvent::Diagnostic {
                            level: "warning".to_string(),
                            message: message.to_string(),
                        })?;
                    }
                    return Err(error);
                }
                let message = if let Some(rotation_error) =
                    error.downcast_ref::<crate::sessions::CompactionRotationError>()
                {
                    bounded_auto_compaction_diagnostic(format!(
                        "automatic compaction failed; no continuation was sent: {rotation_error}"
                    ))
                } else {
                    bounded_auto_compaction_diagnostic(format!(
                        "automatic compaction failed; old primary remains authoritative; no continuation was sent: {error}"
                    ))
                };
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::CompactionFailed {
                        message: message.clone(),
                        canceled: false,
                    })?;
                    sink.output_event(OutputEvent::Diagnostic {
                        level: "error".to_string(),
                        message: message.clone(),
                    })?;
                }
                return Err(anyhow::anyhow!(message));
            }
        };
        super::emit_compaction_fast_observation(
            &mut options.output_sink,
            &result,
            &request_sequence,
        )?;
        auto_compaction_count = auto_compaction_count.saturating_add(1);

        let observed_steering = steering.as_ref().and_then(AgentSteering::observe_collapsed);
        if let Some(warning) = result.rotation_warning.as_ref()
            && let Some(sink) = options.output_sink.as_deref_mut()
        {
            sink.output_event(OutputEvent::Diagnostic {
                level: "warning".to_string(),
                message: warning.clone(),
            })?;
        }
        let candidate = observed_steering
            .as_ref()
            .map(|batch| batch.text.as_str())
            .unwrap_or("continue");
        let effective_candidate = match projected_candidate.as_ref() {
            Some((raw, effective)) if raw == candidate => effective.clone(),
            _ => {
                let effective = AgentSession::effective_prompt_for_projection(
                    candidate,
                    Some(&tools),
                    options.invocation_mode,
                );
                projected_candidate = Some((candidate.to_string(), effective.clone()));
                effective
            }
        };
        let projected_tokens = parent_agent_for_provider.project_prompt_input_tokens(
            &effective_candidate,
            session,
            Some(&tools),
        )?;
        if let Some(sink) = options.output_sink.as_deref_mut() {
            sink.output_event(OutputEvent::CompactionCompleted {
                current_tokens: projected_tokens,
                max_tokens,
                summary: result.summary.clone(),
            })?;
        }
        let compacted_tokens = match parent_agent_for_provider.ensure_prompt_context_fits(
            &effective_candidate,
            session,
            Some(&tools),
        ) {
            Ok(tokens) => tokens,
            Err(error) => {
                let message = bounded_auto_compaction_diagnostic(format!(
                    "automatic compaction completed, but projected continuation exceeds normal context budget; new checkpoint remains authoritative; no provider continuation was sent: {error}"
                ));
                if let Some(sink) = options.output_sink.as_deref_mut() {
                    sink.output_event(OutputEvent::Diagnostic {
                        level: "error".to_string(),
                        message: message.clone(),
                    })?;
                }
                return Err(anyhow::anyhow!(message));
            }
        };
        if auto.triggered(compacted_tokens as u64, max_tokens as u64) {
            let message = "automatic compaction completed, but projected context remains at or above threshold; new checkpoint remains authoritative; no continuation was sent";
            if let Some(sink) = options.output_sink.as_deref_mut() {
                sink.output_event(OutputEvent::Diagnostic {
                    level: "error".to_string(),
                    message: message.to_string(),
                })?;
            }
            anyhow::bail!(message)
        }
        if let Err(error) = cancellation.check() {
            let message = "automatic continuation canceled; new checkpoint remains authoritative; no provider continuation was sent";
            if let Some(sink) = options.output_sink.as_deref_mut() {
                sink.output_event(OutputEvent::Diagnostic {
                    level: "warning".to_string(),
                    message: message.to_string(),
                })?;
            }
            return Err(error);
        }
        match observed_steering {
            Some(batch) => {
                prompt = batch.text;
                effective_prompt = Some(effective_candidate);
                prompt_origin = UserPromptOrigin::Steering;
            }
            None => {
                prompt = "continue".to_string();
                effective_prompt = None;
                prompt_origin = UserPromptOrigin::AutomaticCompaction;
            }
        }
    }
}

fn compact_accounted(
    job: crate::compaction::CompactSessionJob,
    output: &mut AgentRunOutput,
    sequence: &AtomicU64,
    sink: &mut Option<&mut dyn crate::agent::AgentOutputSink>,
) -> Result<Option<crate::compaction::CompactionResult>> {
    let sequence = crate::agent::next_request_sequence(sequence);
    let mut usage = crate::agent::provider_stream::CompactionUsage::default();
    let result = crate::compaction::compact_session_observed(job, &mut |provider, event| {
        usage.observe(provider, event, sequence, sink)
    });
    if let Some(total) = usage.total {
        output.total_tokens = Some(output.total_tokens.unwrap_or(0).saturating_add(total));
    }
    result
}