vtcode-core 0.185.0

Core library for VT Code - a Rust-based terminal coding agent
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! Runtime verification evidence, independent of compactable source exposure.

use std::collections::{HashSet, VecDeque};

use serde_json::Value;

use super::ToolRegistry;
use crate::config::constants::tools;
use crate::tools::{command_args, tool_intent};

const OPTIONAL_EVIDENCE_LIMIT: usize = 32;

#[derive(Debug)]
struct VerifierEvidence {
    identity: u64,
    generation: u64,
    command: String,
    attempt_active: bool,
    session_id: Option<String>,
    exit_code: Option<i64>,
}

impl VerifierEvidence {
    /// Terminal exit-0 verdict recorded in the given mutation generation.
    fn succeeded_in(&self, generation: u64) -> bool {
        self.generation == generation && self.exit_code == Some(0)
    }
}

struct RestoredToolCall<'a> {
    name: &'a str,
    args: Value,
}

#[derive(Clone, Copy)]
enum MutationEvidenceSource {
    Live,
    Restored,
}

#[derive(Debug, Default)]
pub(super) struct CompletionVerification {
    generation: u64,
    history_initialized: bool,
    next_identity: u64,
    evidence: VecDeque<VerifierEvidence>,
    required_commands: Vec<String>,
    pending_mutation_sessions: HashSet<String>,
    repeat_recovery_admissions: HashSet<String>,
}

impl CompletionVerification {
    fn mutate(&mut self) {
        self.generation = self.generation.saturating_add(1);
        self.repeat_recovery_admissions.clear();
    }

    fn launch(&mut self, command: String) -> u64 {
        self.next_identity = self.next_identity.saturating_add(1);
        // New launches supersede older evidence for the same command.
        self.evidence.retain(|entry| entry.command != command);
        self.evidence.push_back(VerifierEvidence {
            identity: self.next_identity,
            generation: self.generation,
            command,
            attempt_active: true,
            session_id: None,
            exit_code: None,
        });
        self.prune_optional_evidence();
        self.next_identity
    }

    fn prune_optional_evidence(&mut self) {
        let mut pinned = HashSet::new();
        for command in &self.required_commands {
            let command = executed_command_identity(command);
            let mut newest = true;
            for entry in self
                .evidence
                .iter()
                .rev()
                .filter(|entry| command_covers_required(&entry.command, &command))
            {
                // Keep the newest verdict for each requirement and retain live
                // handles until their terminal result or cleanup is observed.
                if newest || entry.session_id.is_some() {
                    pinned.insert(entry.identity);
                }
                newest = false;
            }
        }
        let mut to_remove = self
            .evidence
            .iter()
            .filter(|entry| !pinned.contains(&entry.identity))
            .count()
            .saturating_sub(OPTIONAL_EVIDENCE_LIMIT);
        // Required verdicts and yielded handles remain available until their
        // command is replaced or released by the tracker.
        self.evidence.retain(|entry| {
            if to_remove == 0 || pinned.contains(&entry.identity) {
                true
            } else {
                to_remove -= 1;
                false
            }
        });
    }

    fn observe(&mut self, identity: u64, output: &Value) {
        let Some(evidence) = self.evidence.iter_mut().find(|entry| entry.identity == identity) else {
            return;
        };
        if output.get("error").is_some()
            || output.get("success").and_then(Value::as_bool) == Some(false)
            || ["blocked", "not_executed", "cancelled", "termination_requested"]
                .iter()
                .any(|key| output.get(key).and_then(Value::as_bool) == Some(true))
        {
            evidence.attempt_active = false;
            evidence.session_id = None;
            evidence.exit_code = None;
            self.prune_optional_evidence();
            return;
        }
        if evidence
            .session_id
            .as_deref()
            .is_some_and(|expected| command_args::session_id_text(output).is_some_and(|returned| returned != expected))
        {
            return;
        }
        if let Some(exit_code) = output.get("exit_code").and_then(Value::as_i64) {
            evidence.attempt_active = false;
            evidence.exit_code = Some(exit_code);
            evidence.session_id = None;
        } else if let Some(session_id) = command_args::session_id_text(output) {
            evidence.attempt_active = true;
            evidence.session_id = Some(session_id.to_owned());
        } else {
            evidence.attempt_active = false;
            evidence.session_id = None;
            evidence.exit_code = None;
        }
        self.prune_optional_evidence();
    }

    fn repeated_required_command_recovery_generation(&self, command: &str) -> Option<u64> {
        let command = executed_command_identity(command);
        let is_missing_required_command = self
            .required_commands
            .iter()
            .any(|required| executed_command_identity(required) == command && !self.command_passed(required));
        if !is_missing_required_command || self.repeat_recovery_admissions.contains(&command) {
            return None;
        }

        // Required identities cost a JSON build plus shell parsing each;
        // compute once per pass instead of once per evidence entry below.
        let required_identities: Vec<String> = self
            .required_commands
            .iter()
            .map(|required| executed_command_identity(required))
            .collect();
        let has_current_attempt = self.evidence.iter().any(|entry| {
            entry.generation == self.generation
                && required_identities.iter().any(|required| {
                    *required == command
                        && command_covers_required(&entry.command, required)
                        && (entry.attempt_active || entry.session_id.is_some() || entry.exit_code.is_some())
                })
        });
        (!has_current_attempt).then_some(self.generation)
    }

    fn consume_repeated_required_command_recovery(&mut self, command: &str, generation: u64) -> bool {
        let command = executed_command_identity(command);
        if self.generation != generation || self.repeated_required_command_recovery_generation(&command).is_none() {
            return false;
        }
        self.repeat_recovery_admissions.insert(command)
    }

    fn command_passed(&self, command: &str) -> bool {
        let command = executed_command_identity(command);
        self.evidence
            .iter()
            .rev()
            .find(|entry| command_covers_required(&entry.command, &command))
            .is_some_and(|entry| entry.succeeded_in(self.generation))
    }

    fn requires_command(&self, command: &str) -> bool {
        let command = executed_command_identity(command);
        self.required_commands
            .iter()
            .any(|required| command_covers_required(&command, &executed_command_identity(required)))
    }

    /// True when every check obligation is satisfied in the current generation.
    fn completion_checks_satisfied(&self) -> bool {
        if self.required_commands.is_empty() {
            self.evidence.back().is_some_and(|entry| entry.succeeded_in(self.generation))
        } else {
            self.required_commands.iter().all(|command| self.command_passed(command))
        }
    }

    fn pending(&self) -> bool {
        if !self.pending_mutation_sessions.is_empty() {
            return true;
        }
        if self.generation == 0 {
            return false;
        }
        !self.completion_checks_satisfied()
    }
}

fn executed_command_identity(command: &str) -> String {
    let args = serde_json::json!({"action":"run", "command":command});
    let executed = tool_intent::shell_args_as_executed(tools::UNIFIED_EXEC, &args);
    command_args::raw_command_text(&executed)
        .unwrap_or_else(|| command.to_owned())
        .trim()
        .to_owned()
}

/// Defense boundary: ingest-time verification (`task_tracking::metadata_from_input`,
/// `task_tracking::apply_metadata_line`) already strips code spans, but
/// restored/hand-written tracker content may reach here unnormalized, so the
/// store keeps stripping before `command_passed` compares or harness
/// auto-verification re-runs the value verbatim.
use crate::tools::handlers::task_tracking::unwrap_verify_code_span;

fn command_covers_required(command: &str, required: &str) -> bool {
    command == required
        || (command.contains("&&") && tool_intent::activity::shell_command_covers_required_check(command, required))
}

fn is_session_cleanup(args: &Value) -> bool {
    tool_intent::is_exec_session_cleanup_call(tools::WRITE_STDIN, args)
        || matches!(tool_intent::command_session_action(args), Some("close" | "terminate"))
}

fn mutation_targets_are_exempt(name: &str, args: &Value, output: &Value) -> bool {
    let exempt = |tool_name: &str, targets: &Value| {
        tool_intent::is_plan_artifact_write(tool_name, targets) || tool_intent::is_docs_only_write(tool_name, targets)
    };
    if !exempt(name, args) {
        return false;
    }
    // Built-in file results report canonical targets. Requested prose/planning
    // aliases must not exempt writes through symlinks to code.
    let effective_paths: Vec<_> = ["path", "from", "to"]
        .iter()
        .filter_map(|key| output.get(*key).and_then(Value::as_str))
        .chain(
            output
                .get("modified_files")
                .and_then(Value::as_array)
                .into_iter()
                .flatten()
                .filter_map(Value::as_str),
        )
        .collect();
    effective_paths.is_empty() || exempt(tools::WRITE_FILE, &serde_json::json!({"paths":effective_paths}))
}

impl ToolRegistry {
    /// True when current-generation evidence satisfies every local obligation.
    pub fn has_current_completion_verification(&self) -> bool {
        let state = self.harness_context.completion_verification.lock();
        let checks_passed = state.completion_checks_satisfied();
        !state.pending()
            && checks_passed
            && !state
                .evidence
                .iter()
                .any(|entry| entry.generation == state.generation && entry.session_id.is_some())
    }

    /// Drain terminal command results already returned by exec tools. Notification
    /// consumers retain these identities, independently of compactable history.
    pub fn take_observed_terminal_exec_results(&self) -> Vec<String> {
        self.harness_context.terminal_exec_results.lock().drain().collect()
    }

    /// Reconstruct mutation debt once from restored history; historical checks are not runtime verdicts.
    pub fn restore_completion_verification_from_history(&self, history: &[crate::llm::provider::Message]) {
        use crate::llm::provider::MessageRole;
        {
            let mut state = self.harness_context.completion_verification.lock();
            if state.history_initialized {
                return;
            }
            state.history_initialized = true;
            // A live registry already owns reliable runtime evidence. Only a
            // newly restored registry reconstructs debt from archived messages.
            if state.generation != 0 || !state.evidence.is_empty() {
                return;
            }
        }
        let mut pending = std::collections::HashMap::new();
        for message in history {
            if message.role == MessageRole::Assistant {
                pending.clear();
                for call in message.tool_calls.iter().flatten() {
                    if let Some(function) = &call.function
                        && let Ok(args) = serde_json::from_str::<Value>(&function.arguments)
                    {
                        pending.insert(call.id.as_str(), RestoredToolCall { name: function.name.as_str(), args });
                    }
                }
            } else if message.role == MessageRole::Tool
                && let Some(call) = message.tool_call_id.as_deref().and_then(|id| pending.remove(id))
                && let Ok(output) = serde_json::from_str::<Value>(message.content.as_text().as_ref())
            {
                if self.tracker_adopted_for_request() && call.name == tools::TASK_TRACKER {
                    self.observe_required_completion_verifiers(&output);
                }
                if output.get("cleared").is_some() {
                    self.observe_completion_mutation_from(
                        call.name,
                        &call.args,
                        &serde_json::json!({"success":true,"exit_code":0}),
                        MutationEvidenceSource::Restored,
                    );
                } else {
                    self.observe_completion_mutation_from(
                        call.name,
                        &call.args,
                        &output,
                        MutationEvidenceSource::Restored,
                    );
                }
            }
        }
    }

    /// Code changes require an observed exit zero from the same mutation generation.
    pub fn completion_verification_pending(&self) -> bool {
        self.harness_context.completion_verification.lock().pending()
    }

    /// Monotonic identity of the code state used to reject stale completion drafts.
    pub fn completion_mutation_generation(&self) -> u64 {
        self.harness_context.completion_verification.lock().generation
    }

    pub(super) fn observe_required_completion_verifiers(&self, output: &Value) {
        let Some(items) = output
            .get("checklist")
            .and_then(|checklist| checklist.get("items"))
            .and_then(Value::as_array)
        else {
            return;
        };
        let mut commands = Vec::new();
        for command in items
            .iter()
            .filter_map(|item| item.get("verify"))
            .filter_map(Value::as_array)
            .flatten()
            .filter_map(Value::as_str)
        {
            let command = command.trim();
            if !command.is_empty() && !commands.iter().any(|existing| existing == command) {
                commands.push(command.to_owned());
            }
        }
        self.set_required_completion_verifiers(commands);
    }

    /// Existing tracker verification commands are mandatory, even when another check passes.
    pub(super) fn set_required_completion_verifiers(&self, commands: Vec<String>) {
        // Store executable identities: a code-span-wrapped literal can never
        // match the bare executed command and is itself rejected by preflight
        // (code spans are command substitution), deadlocking the gate.
        let mut normalized = Vec::with_capacity(commands.len());
        for command in commands {
            let command = unwrap_verify_code_span(&command).to_owned();
            if !command.is_empty() && !normalized.contains(&command) {
                normalized.push(command);
            }
        }
        let mut state = self.harness_context.completion_verification.lock();
        state.required_commands = normalized;
        state.prune_optional_evidence();
    }

    pub fn completion_verifier_passed(&self, command: &str) -> bool {
        self.harness_context.completion_verification.lock().command_passed(command)
    }

    pub(crate) fn missing_required_completion_verifiers(&self) -> Vec<String> {
        let state = self.harness_context.completion_verification.lock();
        state
            .required_commands
            .iter()
            .filter(|command| !state.command_passed(command))
            .cloned()
            .collect()
    }

    /// Resume a current-generation verifier covering the requested check.
    pub fn pending_completion_verifier_session(&self, command: &str) -> Option<String> {
        let command = executed_command_identity(command);
        let state = self.harness_context.completion_verification.lock();
        state
            .evidence
            .iter()
            .rev()
            .find(|entry| command_covers_required(&entry.command, &command) && entry.generation == state.generation)
            .and_then(|entry| entry.session_id.clone())
    }

    pub fn next_required_completion_verifier(&self) -> Option<String> {
        let state = self.harness_context.completion_verification.lock();
        state
            .required_commands
            .iter()
            .find(|command| !state.command_passed(command))
            .cloned()
    }

    /// Restore conservatively: historical success cannot establish fresh runtime evidence.
    /// Call once when restoring a session with recorded code changes or verification debt.
    pub fn restore_completion_verification_debt(&self) {
        let mut state = self.harness_context.completion_verification.lock();
        if state.generation == 0 {
            state.mutate();
        }
    }

    pub fn unverified_completion_reason(&self) -> String {
        let state = self.harness_context.completion_verification.lock();
        let evidence = state
            .evidence
            .back()
            .map(|entry| {
                format!(
                    " Last command: `{}`; terminal exit: {:?}; launch generation: {}; current generation: {}.",
                    entry.command, entry.exit_code, entry.generation, state.generation
                )
            })
            .unwrap_or_else(|| " No terminal verifier evidence was observed.".to_string());
        let remaining = state.required_commands.iter().find(|command| !state.command_passed(command));
        let required = remaining.map_or_else(String::new, |command| {
            let preview = vtcode_commons::formatting::truncate_within(command, 1024, "...");
            format!(
                " Next required tracker check: `{preview}`. Run this exact command; a different passing check does not satisfy it. \
                If the check itself cannot pass for reasons outside this task (missing toolchain, pre-existing failures elsewhere), \
                correct the tracker `verify` metadata to an assertion covering this task and run it; metadata alone never verifies."
            )
        });
        format!(
            "Verification is still pending: Unverified: changed code requires fresh verification before successful completion.{evidence}{required}"
        )
    }

    pub(super) fn begin_completion_verifier(&self, args: &Value) -> Option<u64> {
        let command = self.completion_verifier_command(args)?;
        Some(self.harness_context.completion_verification.lock().launch(command))
    }

    /// Snapshot the generation for the one repeat-limit retry of an exact missing tracker check.
    pub(super) fn completion_verifier_repeat_recovery_generation(&self, name: &str, args: &Value) -> Option<u64> {
        if !tool_intent::is_command_tool(crate::tools::names::canonical_tool_name(name)) {
            return None;
        }
        let command = self.completion_verifier_command(args)?;
        self.harness_context
            .completion_verification
            .lock()
            .repeated_required_command_recovery_generation(&command)
    }

    /// Consume the retry only after the regular execution pipeline admits the command.
    pub(super) fn consume_completion_verifier_repeat_recovery(
        &self,
        name: &str,
        args: &Value,
        generation: u64,
    ) -> bool {
        if !tool_intent::is_command_tool(crate::tools::names::canonical_tool_name(name)) {
            return false;
        }
        let Some(command) = self.completion_verifier_command(args) else {
            return false;
        };
        self.harness_context
            .completion_verification
            .lock()
            .consume_repeated_required_command_recovery(&command, generation)
    }

    /// Share verifier accounting with the pacing gate; this grants no execution permission.
    pub fn is_completion_verifier_call(&self, name: &str, args: &Value) -> bool {
        tool_intent::is_command_tool(crate::tools::names::canonical_tool_name(name))
            && self.completion_verifier_command(args).is_some()
    }

    fn completion_verifier_command(&self, args: &Value) -> Option<String> {
        let executed = tool_intent::shell_args_as_executed(tools::UNIFIED_EXEC, args);
        let command = command_args::raw_command_text(&executed)?.trim().to_owned();
        // A declared repair action still invalidates verification. Inspect all
        // static segments so a harmless prefix cannot hide a source rewrite.
        if tool_intent::readonly::static_shell_command_words(&command)
            .or_else(|| tool_intent::readonly::static_shell_command_words_with_output_plumbing(&command))
            .is_some_and(|segments| segments.iter().any(|words| command_args::command_uses_source_repair(words)))
        {
            return None;
        }
        if tool_intent::classify_shell_activity(tools::UNIFIED_EXEC, &executed)
            != tool_intent::ShellActivity::Verification
            && !self.harness_context.completion_verification.lock().requires_command(&command)
        {
            return None;
        }
        let cwd = self
            .pty_manager()
            .working_dir_candidate(command_args::working_dir_text(args))
            .ok()?;
        if cwd != *self.workspace_root() {
            return None;
        }
        // Empty argv/environment containers are native adapter defaults. Effective
        // launch context cannot be dropped from a command-only verifier identity.
        if args
            .get("args")
            .is_some_and(|value| !matches!(value, Value::Array(args) if args.is_empty()))
            || args.get("env").is_some_and(|value| match value {
                Value::Array(entries) => !entries.is_empty(),
                Value::Object(env) => !env.is_empty(),
                _ => true,
            })
            || ["shell", "login", "stdin"].iter().any(|key| args.get(key).is_some())
        {
            return None;
        }
        Some(command)
    }

    pub(super) fn observe_completion_verifier(
        &self,
        launch: Option<u64>,
        args: &Value,
        result: &anyhow::Result<Value>,
    ) {
        if let Ok(output) = result
            && output.get("exit_code").and_then(Value::as_i64).is_some()
            && output.get("error").is_none()
            && output.get("cancelled").and_then(Value::as_bool) != Some(true)
            && let Some(session_id) =
                command_args::session_id_text(output).or_else(|| command_args::session_id_text(args))
        {
            self.harness_context.terminal_exec_results.lock().insert(session_id.to_owned());
        }
        let mut state = self.harness_context.completion_verification.lock();
        if let Some(session_id) = command_args::session_id_text(args)
            && state.pending_mutation_sessions.contains(session_id)
            && result.as_ref().is_ok_and(|output| {
                !command_args::session_id_text(output).is_some_and(|returned| returned != session_id)
                    && (output.get("exit_code").and_then(Value::as_i64).is_some()
                        || (is_session_cleanup(args)
                            && output.get("error").is_none()
                            && output.get("success").and_then(Value::as_bool) != Some(false)))
            })
        {
            state.pending_mutation_sessions.remove(session_id);
            // A check run while a mutator was active cannot verify its final output.
            state.mutate();
        }
        let identity = launch.or_else(|| {
            let session_id = command_args::session_id_text(args)?;
            if tool_intent::classify_tool_intent(tools::UNIFIED_EXEC, args).mutating && !is_session_cleanup(args) {
                return None;
            }
            state
                .evidence
                .iter()
                .rev()
                .find(|entry| entry.session_id.as_deref() == Some(session_id))
                .map(|entry| entry.identity)
        });
        let Some(identity) = identity else {
            return;
        };
        match result {
            Ok(output) if !is_session_cleanup(args) => state.observe(identity, output),
            _ => {
                // Lost, failed, denied, cancelled, or cleaned-up attempts carry no verdict.
                if let Some(entry) = state.evidence.iter_mut().find(|entry| entry.identity == identity) {
                    entry.attempt_active = false;
                    entry.session_id = None;
                    entry.exit_code = None;
                }
                state.prune_optional_evidence();
            }
        }
    }

    pub(super) fn observe_completion_mutation(&self, name: &str, args: &Value, output: &Value) {
        self.observe_completion_mutation_from(name, args, output, MutationEvidenceSource::Live);
    }

    /// Retire verification bookkeeping for exec sessions removed by bulk
    /// cleanup paths (`terminate_*_exec_sessions_async`, TUI force-cancel)
    /// that bypass per-call observation. Without this, a terminated mutating
    /// session stays in `pending_mutation_sessions` forever and every later
    /// turn reports unverified completion even after fresh checks pass.
    /// Reuses the single-session cleanup contract (retire the pending boundary
    /// and any live verifier handle); unknown or still-live ids are harmless
    /// no-ops. Callers must pass only sessions that are actually gone so a
    /// live mutator is never released early.
    pub(super) fn observe_completion_exec_sessions_terminated(&self, session_ids: &[String]) {
        for session_id in session_ids {
            let args = serde_json::json!({"action":"close", "session_id":session_id});
            let result: anyhow::Result<Value> = Ok(serde_json::json!({"success":true}));
            self.observe_completion_verifier(None, &args, &result);
        }
    }

    fn observe_completion_mutation_from(
        &self,
        name: &str,
        args: &Value,
        output: &Value,
        source: MutationEvidenceSource,
    ) {
        let name = crate::tools::names::canonical_tool_name(name);
        // Mutation intent also covers coordination and metadata tools. Only
        // workspace writes, shell mutations, or explicit changed paths incur code debt.
        if !tool_intent::is_command_tool(name)
            && !tool_intent::is_edited_file_conflict_guarded_call(name, args)
            && !matches!(
                name,
                tools::DELETE_FILE | tools::MOVE_FILE | tools::COPY_FILE | tools::SEARCH_REPLACE | tools::UNIFIED_FILE
            )
            && !output
                .get("modified_files")
                .and_then(Value::as_array)
                .is_some_and(|files| !files.is_empty())
        {
            return;
        }
        let exec_changed = tool_intent::is_command_tool(name) && tool_intent::activity::exec_output_has_changes(output);
        if !exec_changed
            && (mutation_targets_are_exempt(name, args, output)
                || !tool_intent::classify_tool_intent(name, args).mutating
                || output.get("error").is_some()
                || output.get("success").and_then(Value::as_bool) == Some(false)
                || ["blocked", "not_executed", "cancelled"]
                    .iter()
                    .any(|key| output.get(key).and_then(Value::as_bool) == Some(true)))
        {
            return;
        }
        if tool_intent::is_command_tool(name) && !exec_changed {
            let executed = tool_intent::shell_args_as_executed(name, args);
            if tool_intent::classify_shell_activity(name, &executed) != tool_intent::ShellActivity::Mutation
                || tool_intent::shell_command_is_admitted_verification_attempt(&executed)
                // Declared checks use the same eligibility rules as verifier launch.
                // This affects accounting only; execution admission remains authoritative.
                || self.completion_verifier_command(args).is_some()
                || (output.get("exit_code").and_then(Value::as_i64) != Some(0)
                    && command_args::session_id_text(output).is_none())
            {
                return;
            }
            // Dynamic inspections need a complete nonmutation proof before
            // they may preserve prior verifier evidence. Admission is unchanged.
            if let Some(command_text) = command_args::raw_command_text(&executed)
                .or_else(|| command_args::shell_command_text_with_args(&executed))
                && !tool_intent::activity::exec_mutation_should_incur_debt(&command_text, output)
            {
                return;
            }
        }
        match crate::tools::file_ops::diff_output_has_effective_change(output) {
            Some(false) if !exec_changed => return,
            Some(false) | Some(true) => {}
            None if output.get("success").and_then(Value::as_bool) != Some(true)
                && output.get("exit_code").and_then(Value::as_i64) != Some(0)
                && command_args::session_id_text(output).is_none()
                && !output
                    .get("modified_files")
                    .and_then(Value::as_array)
                    .is_some_and(|files| !files.is_empty()) =>
            {
                return;
            }
            None => {}
        }
        let mut state = self.harness_context.completion_verification.lock();
        state.mutate();
        if matches!(source, MutationEvidenceSource::Live)
            && tool_intent::is_command_tool(name)
            && output.get("exit_code").and_then(Value::as_i64).is_none()
            && let Some(session_id) = command_args::session_id_text(output)
        {
            state.pending_mutation_sessions.insert(session_id.to_owned());
        }
    }
}

#[cfg(test)]
mod tests;