pi_agent_rust 0.7.1

Native AI coding agent CLI - Rust port of Pi Agent
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
//! The advisor (bd-cv653.3.3): a second model that reviews each agent turn
//! and injects notes inline — a quiet aside, a concern, or a hard blocker.
//!
//! It runs on its own provider and its own context, so it catches what the
//! doer rushed past. Design rules from the bead:
//! - Zero overhead when unconfigured (no digest is even built).
//! - Failure isolation: advisor errors NEVER fail the main turn; 3
//!   consecutive failures disable the advisor with a user-visible notice.
//! - Emission guard: rate-limited, deduped, and silent on trivial turns.
//! - Stack layering: this module emits structured verdicts; rendering is the
//!   transcript card registry's job (bd-cv653.9.2) — no bespoke painting here.

use crate::model::Message;
use crate::provider::Provider;
use crate::text_completion::{
    MAX_INPUT_BYTES, MAX_TEXT_BYTES, OMITTED_INPUT, RequestStop, collect_text, redact_inputs,
    with_timeout,
};
use serde_json::Value;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;

/// Advisor severity levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VerdictLevel {
    Note,
    Concern,
    Blocker,
}

impl VerdictLevel {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Note => "note",
            Self::Concern => "concern",
            Self::Blocker => "blocker",
        }
    }
}

/// A parsed advisor verdict.
#[derive(Debug, Clone)]
pub struct AdvisorVerdict {
    pub level: VerdictLevel,
    pub rationale: String,
}

/// The compact turn digest sent to the advisor (budget-capped).
#[derive(Debug, Default)]
pub struct TurnDigest {
    pub files_touched: Vec<String>,
    pub commands_run: Vec<String>,
    pub tool_errors: Vec<String>,
    pub final_text: String,
    pub tool_call_count: usize,
    pub is_trivial: bool,
}

impl TurnDigest {
    /// A turn is trivial when no tools ran and the reply is short — nothing
    /// worth an advisor's attention (or tokens).
    #[must_use]
    pub const fn is_trivial(&self) -> bool {
        self.is_trivial
    }
}

const MAX_DIGEST_FILES: usize = 25;
const MAX_DIGEST_COMMANDS: usize = 15;
const MAX_DIGEST_ERRORS: usize = 10;
const MAX_FINAL_TEXT_CHARS: usize = 2_000;
const MAX_DIGEST_PATH_CHARS: usize = 1_024;
// Reserve enough room to name an omission in every field. The retained raw
// fields plus those markers always fit the shared privacy scanner's budget.
const MAX_RAW_DIGEST_BYTES: usize = MAX_INPUT_BYTES
    - (MAX_DIGEST_FILES + MAX_DIGEST_COMMANDS + MAX_DIGEST_ERRORS + 1) * OMITTED_INPUT.len();

fn retain_digest_text(text: &str, remaining: &mut usize) -> String {
    if text.len() > *remaining {
        return OMITTED_INPUT.to_string();
    }
    *remaining -= text.len();
    text.to_string()
}

fn retain_error_text(result: &crate::model::ToolResultMessage, remaining: &mut usize) -> String {
    let mut output = String::new();
    let mut first = true;
    for block in &result.content {
        let crate::model::ContentBlock::Text(text) = block else {
            continue;
        };
        let next = output
            .len()
            .checked_add(usize::from(!first))
            .and_then(|bytes| bytes.checked_add(text.text.len()));
        if next.is_none_or(|bytes| bytes > *remaining) {
            // Never retain a partial private-key envelope or an uninspected
            // prefix when a later block makes the whole field too large.
            return OMITTED_INPUT.to_string();
        }
        if !first {
            output.push(' ');
        }
        output.push_str(&text.text);
        first = false;
    }
    *remaining -= output.len();
    output
}

/// Screen all fields together before applying presentation limits. This is
/// also the provider-admission boundary for SDK-supplied TurnDigest values;
/// callers cannot bypass it by avoiding build_digest.
fn screen_digest(digest: &TurnDigest) -> crate::error::Result<TurnDigest> {
    if digest.files_touched.len() > MAX_DIGEST_FILES
        || digest.commands_run.len() > MAX_DIGEST_COMMANDS
        || digest.tool_errors.len() > MAX_DIGEST_ERRORS
    {
        return Err(crate::error::Error::validation(
            "PI_ADVISOR_DIGEST_LIMIT: too many fields in advisor digest",
        ));
    }
    let inputs: Vec<&str> = digest
        .files_touched
        .iter()
        .chain(&digest.commands_run)
        .chain(&digest.tool_errors)
        .chain(std::iter::once(&digest.final_text))
        .map(String::as_str)
        .collect();
    let protected = redact_inputs(&inputs)?;
    if protected.len() != inputs.len() {
        return Err(crate::error::Error::validation(
            "advisor privacy projection changed field count",
        ));
    }
    let mut protected = protected.into_iter();
    let files_touched = protected
        .by_ref()
        .take(digest.files_touched.len())
        .map(|text| text.chars().take(MAX_DIGEST_PATH_CHARS).collect())
        .collect();
    let commands_run = protected
        .by_ref()
        .take(digest.commands_run.len())
        .map(|text| text.chars().take(200).collect())
        .collect();
    let tool_errors = protected
        .by_ref()
        .take(digest.tool_errors.len())
        .map(|text| text.chars().take(200).collect())
        .collect();
    let final_text = protected
        .next()
        .ok_or_else(|| {
            crate::error::Error::validation("advisor privacy projection lost final text")
        })?
        .chars()
        .take(MAX_FINAL_TEXT_CHARS)
        .collect();
    Ok(TurnDigest {
        files_touched,
        commands_run,
        tool_errors,
        final_text,
        tool_call_count: digest.tool_call_count,
        is_trivial: digest.is_trivial,
    })
}

/// Build the digest from the tail of the conversation (last user message
/// onward), budgeted.
///
/// Complete selected fields are screened for built-in credential shapes
/// before clipping; tool-free advisor projections never need reversible
/// credentials. The source conversation is not modified.
#[must_use]
pub fn build_digest(messages: &[Message]) -> TurnDigest {
    // Start from the last user message (turn boundary).
    let boundary = messages
        .iter()
        .rposition(|m| matches!(m, Message::User(_)))
        .unwrap_or(0);
    let tail = &messages[boundary..];

    let mut digest = TurnDigest::default();
    let mut remaining = MAX_RAW_DIGEST_BYTES;
    let mut seen_files = HashSet::new();
    let mut final_text = "";
    for message in tail {
        match message {
            Message::Assistant(assistant) => {
                for block in &assistant.content {
                    match block {
                        crate::model::ContentBlock::ToolCall(call) => {
                            digest.tool_call_count = digest.tool_call_count.saturating_add(1);
                            match call.name.as_str() {
                                "write" | "edit" | "hashline_edit" | "ast_edit" => {
                                    if let Some(path) =
                                        call.arguments.get("path").and_then(Value::as_str)
                                        && digest.files_touched.len() < MAX_DIGEST_FILES
                                        && seen_files.insert(path)
                                    {
                                        digest
                                            .files_touched
                                            .push(retain_digest_text(path, &mut remaining));
                                    }
                                }
                                "bash" => {
                                    if let Some(command) =
                                        call.arguments.get("command").and_then(Value::as_str)
                                        && digest.commands_run.len() < MAX_DIGEST_COMMANDS
                                    {
                                        digest
                                            .commands_run
                                            .push(retain_digest_text(command, &mut remaining));
                                    }
                                }
                                _ => {}
                            }
                        }
                        crate::model::ContentBlock::Text(text) => {
                            // Keep only a borrow until the final block is known;
                            // discarded drafts must not consume the scan budget.
                            final_text = &text.text;
                        }
                        _ => {}
                    }
                }
            }
            Message::ToolResult(result)
                if result.is_error && digest.tool_errors.len() < MAX_DIGEST_ERRORS =>
            {
                digest
                    .tool_errors
                    .push(retain_error_text(result, &mut remaining));
            }
            _ => {}
        }
    }
    // Classification describes the source turn, not a short redaction marker.
    digest.is_trivial = digest.tool_call_count == 0 && final_text.len() < 400;
    digest.final_text = retain_digest_text(final_text, &mut remaining);
    screen_digest(&digest).unwrap_or_else(|_| {
        tracing::warn!(
            event = "pi.advisor.privacy_projection_refused",
            "Advisor digest omitted because its privacy projection failed"
        );
        TurnDigest {
            final_text: OMITTED_INPUT.to_string(),
            tool_call_count: digest.tool_call_count,
            is_trivial: digest.is_trivial,
            ..Default::default()
        }
    })
}

/// The review rubric prompt.
pub const REVIEW_SYSTEM_PROMPT: &str = "You are the advisor: a senior reviewer watching another agent's turn. \
Review the digest and answer with EXACTLY one verdict line, then a short rationale.\n\
Line 1 must be one of: NOTE, CONCERN, BLOCKER.\n\
- NOTE: fine, or a minor observation worth one sentence.\n\
- CONCERN: something likely wrong/risky the doer should fix before continuing.\n\
- BLOCKER: a hard problem (data loss risk, broken build, wrong target) that must stop work until addressed.\n\
Be terse. Cite file paths when relevant. No markdown, no headers.";

/// Render the digest as the advisor's user prompt. This is formatting only;
/// the runtime screens the typed fields before calling it.
#[must_use]
pub fn digest_prompt(digest: &TurnDigest) -> String {
    let mut out = String::from("Turn digest:\n");
    let _ = std::fmt::Write::write_fmt(
        &mut out,
        format_args!("tool calls: {}\n", digest.tool_call_count),
    );
    if !digest.files_touched.is_empty() {
        let _ = std::fmt::Write::write_fmt(
            &mut out,
            format_args!("files touched: {}\n", digest.files_touched.join(", ")),
        );
    }
    if !digest.commands_run.is_empty() {
        let _ = std::fmt::Write::write_fmt(
            &mut out,
            format_args!("commands run:\n{}\n", digest.commands_run.join("\n")),
        );
    }
    if !digest.tool_errors.is_empty() {
        let _ = std::fmt::Write::write_fmt(
            &mut out,
            format_args!("tool errors:\n{}\n", digest.tool_errors.join("\n")),
        );
    }
    let _ = std::fmt::Write::write_fmt(
        &mut out,
        format_args!("final message:\n{}\n", digest.final_text),
    );
    out
}

/// Parse the advisor's reply into a verdict. Unparseable replies degrade to
/// NOTE (never blocker-by-accident).
#[must_use]
pub fn parse_verdict(reply: &str) -> AdvisorVerdict {
    let trimmed = reply.trim();
    let first_line = trimmed.lines().next().unwrap_or("");
    let upper = first_line
        .trim()
        .trim_matches([':', '.', '*', '#', ' '])
        .to_ascii_uppercase();
    let level = if upper.starts_with("BLOCKER") {
        VerdictLevel::Blocker
    } else if upper.starts_with("CONCERN") {
        VerdictLevel::Concern
    } else {
        VerdictLevel::Note
    };
    let rationale: String = if matches!(level, VerdictLevel::Note) && !upper.starts_with("NOTE") {
        // No protocol line at all: the whole reply is the note.
        trimmed.chars().take(600).collect()
    } else {
        trimmed
            .lines()
            .skip(1)
            .collect::<Vec<_>>()
            .join("\n")
            .trim()
            .chars()
            .take(600)
            .collect()
    };
    AdvisorVerdict {
        level,
        rationale: if rationale.is_empty() {
            first_line
                .trim()
                .trim_start_matches(|c: char| c.is_ascii_uppercase() || c == ' ')
                .trim_start_matches([':', '-', ' '])
                .chars()
                .take(600)
                .collect()
        } else {
            rationale
        },
    }
}

/// Rate/dedupe guard.
#[derive(Debug)]
pub struct EmissionGuard {
    pub max_per_window: usize,
    pub window_turns: usize,
    notes_in_window: usize,
    window_start_turn: u64,
    seen_hashes: HashSet<u64>,
}

impl EmissionGuard {
    #[must_use]
    pub fn new(max_per_window: usize, window_turns: usize) -> Self {
        Self {
            max_per_window,
            window_turns,
            notes_in_window: 0,
            window_start_turn: 0,
            seen_hashes: HashSet::new(),
        }
    }

    /// Whether this verdict may emit at `turn_index`.
    pub fn allow(&mut self, verdict: &AdvisorVerdict, turn_index: u64) -> bool {
        if turn_index >= self.window_start_turn + self.window_turns as u64 {
            self.window_start_turn = turn_index;
            self.notes_in_window = 0;
            self.seen_hashes.clear();
        }
        // Blockers always emit (they gate work); notes/concerns are limited.
        if verdict.level != VerdictLevel::Blocker {
            if self.notes_in_window >= self.max_per_window {
                return false;
            }
            let hash = fnv1a(&verdict.rationale);
            if !self.seen_hashes.insert(hash) {
                return false; // repeated rationale
            }
        }
        self.notes_in_window += 1;
        true
    }
}

fn fnv1a(text: &str) -> u64 {
    const OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
    const PRIME: u64 = 0x0000_0100_0000_01b3;
    let mut hash = OFFSET;
    for byte in text.as_bytes() {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(PRIME);
    }
    hash
}

/// Format the injection text delivered into the next turn.
#[must_use]
pub fn format_injection(verdict: &AdvisorVerdict) -> String {
    let tag = match verdict.level {
        VerdictLevel::Note => "ADVISOR:NOTE",
        VerdictLevel::Concern => "ADVISOR:CONCERN",
        VerdictLevel::Blocker => "ADVISOR:BLOCKER",
    };
    format!("[{tag}] {}", verdict.rationale)
}

/// Process-global advisor pause flag (bd-cv653.3.3): pi runs one session per
/// process, so /advisor pause|resume flips this single flag.
pub static ADVISOR_PAUSED: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

/// What a session needs to build its own [`AdvisorRuntime`]: hosts that
/// replace sessions (`/new`, `/resume`, `/fork`) keep this and build a fresh
/// runtime per session.
#[derive(Clone)]
pub struct AdvisorOptions {
    pub provider: Arc<dyn Provider>,
    pub label: String,
    pub timeout: Duration,
    pub api_key: Option<String>,
}

impl AdvisorOptions {
    #[must_use]
    pub fn runtime(&self) -> AdvisorRuntime {
        AdvisorRuntime::new(Arc::clone(&self.provider), self.label.clone())
            .with_timeout(self.timeout)
            .with_api_key(self.api_key.clone())
    }
}

/// The advisor runtime: owns the second provider, the guard, and failure
/// isolation state.
pub struct AdvisorRuntime {
    provider: Arc<dyn Provider>,
    label: String,
    timeout: Duration,
    guard: EmissionGuard,
    consecutive_failures: u32,
    /// Resolved credential for the advisor's provider; forwarded on every
    /// review call so keyed providers authenticate exactly like the doer.
    api_key: Option<String>,
    /// One-shot user notice. Consuming it does not clear the disabled state.
    pub disabled_notice: Option<String>,
}

/// What a review returned.
#[derive(Debug)]
pub enum AdvisorOutcome {
    /// A verdict worth injecting.
    Inject(AdvisorVerdict),
    /// Suppressed (trivial turn, guard, owner cancellation, or no useful note).
    Quiet,
    /// The advisor failed (isolated; counted toward the disable threshold).
    Failed,
}

const MAX_CONSECUTIVE_FAILURES: u32 = 3;

impl AdvisorRuntime {
    #[must_use]
    pub fn new(provider: Arc<dyn Provider>, label: String) -> Self {
        Self {
            provider,
            label,
            timeout: Duration::from_secs(15),
            guard: EmissionGuard::new(3, 10),
            consecutive_failures: 0,
            api_key: None,
            disabled_notice: None,
        }
    }

    #[must_use]
    pub const fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    #[must_use]
    pub fn with_api_key(mut self, api_key: Option<String>) -> Self {
        self.api_key = api_key;
        self
    }

    #[must_use]
    pub fn with_guard(mut self, max_per_window: usize, window_turns: usize) -> Self {
        self.guard = EmissionGuard::new(max_per_window, window_turns);
        self
    }

    #[must_use]
    pub const fn is_disabled(&self) -> bool {
        self.consecutive_failures >= MAX_CONSECUTIVE_FAILURES
    }

    /// Review one turn. Never fails the caller. Built-in credential shapes
    /// are always redacted for this tool-free secondary model, independently
    /// of the primary agent's configurable reversible secret mode.
    /// Owner cancellation suppresses the verdict without changing the failure
    /// streak or emission guard; it is not evidence of a broken provider.
    pub async fn review_turn(&mut self, digest: &TurnDigest, turn_index: u64) -> AdvisorOutcome {
        if self.is_disabled() || ADVISOR_PAUSED.load(std::sync::atomic::Ordering::SeqCst) {
            return AdvisorOutcome::Quiet;
        }
        if digest.is_trivial() {
            return AdvisorOutcome::Quiet;
        }
        let call = self.call_advisor(digest);
        let reply = match with_timeout(self.timeout, call).await {
            Ok(Ok(reply)) => reply,
            Err(RequestStop::Cancelled) => return AdvisorOutcome::Quiet,
            Ok(Err(_)) | Err(RequestStop::TimedOut | RequestStop::TimeUnavailable) => {
                self.consecutive_failures += 1;
                if self.consecutive_failures >= MAX_CONSECUTIVE_FAILURES {
                    self.disabled_notice = Some(format!(
                        "advisor ({}) disabled after {} consecutive failures",
                        self.label, self.consecutive_failures
                    ));
                }
                return AdvisorOutcome::Failed;
            }
        };
        self.consecutive_failures = 0;
        let verdict = parse_verdict(&reply);
        if verdict.level == VerdictLevel::Note && verdict.rationale.len() < 12 {
            return AdvisorOutcome::Quiet; // "looks fine" notes add noise
        }
        if !self.guard.allow(&verdict, turn_index) {
            return AdvisorOutcome::Quiet;
        }
        AdvisorOutcome::Inject(verdict)
    }

    async fn call_advisor(&self, digest: &TurnDigest) -> crate::error::Result<String> {
        let digest = screen_digest(digest)?;
        let context = crate::provider::Context {
            system_prompt: Some(REVIEW_SYSTEM_PROMPT.to_string().into()),
            messages: vec![crate::model::Message::User(crate::model::UserMessage {
                content: crate::model::UserContent::Text(digest_prompt(&digest)),
                timestamp: chrono::Utc::now().timestamp_millis(),
            })]
            .into(),
            tools: Vec::new().into(),
        };
        let options = crate::provider::StreamOptions {
            max_tokens: Some(512),
            api_key: self.api_key.clone(),
            ..Default::default()
        };
        let stream = self.provider.stream(&context, &options).await?;
        collect_text(stream, MAX_TEXT_BYTES).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_verdict_levels() {
        let blocker =
            parse_verdict("BLOCKER: the edit deletes the migration\nIt removes drop columns.");
        assert_eq!(blocker.level, VerdictLevel::Blocker);
        assert!(blocker.rationale.contains("drop columns"));
        let concern = parse_verdict("CONCERN: broad catch\nThis swallows IO errors.");
        assert_eq!(concern.level, VerdictLevel::Concern);
        assert!(concern.rationale.contains("IO errors"));
        let note = parse_verdict("NOTE: fine\nLooks right.");
        assert_eq!(note.level, VerdictLevel::Note);
        // No protocol line → whole reply becomes a note (never a blocker).
        let free = parse_verdict("seems okay to me");
        assert_eq!(free.level, VerdictLevel::Note);
        assert!(free.rationale.contains("seems okay"));
    }

    #[test]
    fn guard_rate_limits_and_dedupes() {
        let mut guard = EmissionGuard::new(2, 5);
        let concern = || AdvisorVerdict {
            level: VerdictLevel::Concern,
            rationale: "same concern".to_string(),
        };
        assert!(guard.allow(&concern(), 0));
        assert!(
            !guard.allow(&concern(), 1),
            "dedupe: same rationale blocked"
        );
        let other = AdvisorVerdict {
            level: VerdictLevel::Concern,
            rationale: "different concern".to_string(),
        };
        assert!(guard.allow(&other, 2));
        let third = AdvisorVerdict {
            level: VerdictLevel::Concern,
            rationale: "third distinct concern".to_string(),
        };
        assert!(!guard.allow(&third, 3), "rate limit: 2 per 5-turn window");
        // New window resets.
        assert!(guard.allow(&third, 6));
        // Blockers always emit.
        let blocker = AdvisorVerdict {
            level: VerdictLevel::Blocker,
            rationale: "stop".to_string(),
        };
        assert!(guard.allow(&blocker, 3));
    }

    #[test]
    fn digest_collects_files_commands_errors_and_text() {
        let messages = vec![
            Message::User(crate::model::UserMessage {
                content: crate::model::UserContent::Text("fix it".to_string()),
                timestamp: 0,
            }),
            Message::Assistant(Arc::new(crate::model::AssistantMessage {
                content: vec![
                    crate::model::ContentBlock::ToolCall(crate::model::ToolCall {
                        id: "1".to_string(),
                        name: "edit".to_string(),
                        arguments: serde_json::json!({"path": "src/a.rs"}),
                        thought_signature: None,
                    }),
                    crate::model::ContentBlock::ToolCall(crate::model::ToolCall {
                        id: "2".to_string(),
                        name: "bash".to_string(),
                        arguments: serde_json::json!({"command": "cargo test"}),
                        thought_signature: None,
                    }),
                    crate::model::ContentBlock::Text(crate::model::TextContent::new("done")),
                ],
                api: "x".to_string(),
                provider: "y".to_string(),
                model: "z".to_string(),
                usage: crate::model::Usage::default(),
                stop_reason: crate::model::StopReason::Stop,
                stop_details: None,
                error_message: None,
                timestamp: 0,
            })),
            Message::ToolResult(Arc::new(crate::model::ToolResultMessage {
                tool_call_id: "2".to_string(),
                tool_name: "bash".to_string(),
                content: vec![crate::model::ContentBlock::Text(
                    crate::model::TextContent::new("permission denied"),
                )],
                is_error: true,
                details: None,
                timestamp: 0,
            })),
        ];
        let digest = build_digest(&messages);
        assert!(!digest.is_trivial());
        assert_eq!(digest.files_touched, vec!["src/a.rs".to_string()]);
        assert_eq!(digest.commands_run, vec!["cargo test".to_string()]);
        assert_eq!(digest.tool_errors.len(), 1);
        assert!(digest.tool_errors[0].contains("permission denied"));
        assert_eq!(digest.final_text, "done");
        assert_eq!(digest.tool_call_count, 2);
    }

    #[test]
    fn digest_flags_trivial_turns() {
        let messages = vec![Message::User(crate::model::UserMessage {
            content: crate::model::UserContent::Text("hi".to_string()),
            timestamp: 0,
        })];
        let digest = build_digest(&messages);
        assert!(digest.is_trivial(), "no-tool short turns are trivial");
    }

    #[test]
    fn injection_format_tags_level() {
        let verdict = AdvisorVerdict {
            level: VerdictLevel::Blocker,
            rationale: "stops now".to_string(),
        };
        assert_eq!(format_injection(&verdict), "[ADVISOR:BLOCKER] stops now");
    }

    #[test]
    fn timeout_reports_deadline_on_slow_future() {
        asupersync::test_utils::run_test(|| async {
            let outcome = with_timeout(Duration::from_millis(30), async {
                asupersync::time::sleep(asupersync::time::wall_now(), Duration::from_secs(60))
                    .await;
                42
            })
            .await;
            assert_eq!(outcome, Err(RequestStop::TimedOut));
        });
    }

    struct ScriptedProvider {
        responses: std::sync::Mutex<std::collections::VecDeque<Vec<crate::model::StreamEvent>>>,
        calls: std::sync::atomic::AtomicUsize,
        prompts: std::sync::Mutex<Vec<String>>,
        cancel_on_call: Option<asupersync::Cx>,
    }

    #[allow(clippy::unnecessary_literal_bound)]
    #[async_trait::async_trait]
    impl Provider for ScriptedProvider {
        fn name(&self) -> &str {
            "advisor-test"
        }

        fn api(&self) -> &str {
            "advisor-test"
        }

        fn model_id(&self) -> &str {
            "advisor-test-model"
        }

        async fn stream(
            &self,
            context: &crate::provider::Context<'_>,
            options: &crate::provider::StreamOptions,
        ) -> crate::error::Result<
            std::pin::Pin<
                Box<
                    dyn futures::Stream<Item = crate::error::Result<crate::model::StreamEvent>>
                        + Send,
                >,
            >,
        > {
            assert!(context.tools.is_empty());
            assert_eq!(context.system_prompt.as_deref(), Some(REVIEW_SYSTEM_PROMPT));
            assert_eq!(options.api_key.as_deref(), Some("test-key"));
            self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            let Some(Message::User(user)) = context.messages.first() else {
                panic!("expected an advisor user prompt");
            };
            let crate::model::UserContent::Text(text) = &user.content else {
                panic!("expected a text-only advisor prompt");
            };
            self.prompts.lock().unwrap().push(text.clone());
            let events = self
                .responses
                .lock()
                .unwrap()
                .pop_front()
                .expect("unexpected advisor provider call");
            if let Some(owner) = &self.cancel_on_call {
                owner.cancel_with(
                    asupersync::types::CancelKind::User,
                    Some("review cancelled"),
                );
            }
            Ok(Box::pin(futures::stream::iter(events.into_iter().map(Ok))))
        }
    }

    fn scripted_runtime(
        responses: Vec<Vec<crate::model::StreamEvent>>,
    ) -> (AdvisorRuntime, Arc<ScriptedProvider>) {
        let provider = Arc::new(ScriptedProvider {
            responses: std::sync::Mutex::new(responses.into()),
            calls: std::sync::atomic::AtomicUsize::new(0),
            prompts: std::sync::Mutex::new(Vec::new()),
            cancel_on_call: None,
        });
        let runtime = AdvisorRuntime::new(provider.clone(), "test".to_string())
            .with_api_key(Some("test-key".to_string()));
        (runtime, provider)
    }

    fn review_digest() -> TurnDigest {
        TurnDigest {
            tool_call_count: 1,
            final_text: "edited src/example.rs".to_string(),
            ..Default::default()
        }
    }

    fn completed_reply(text: &str) -> Vec<crate::model::StreamEvent> {
        vec![crate::model::StreamEvent::Done {
            reason: crate::model::StopReason::Stop,
            message: crate::model::AssistantMessage {
                content: vec![crate::model::ContentBlock::Text(
                    crate::model::TextContent::new(text),
                )],
                stop_reason: crate::model::StopReason::Stop,
                ..Default::default()
            },
        }]
    }

    fn disconnected_reply() -> Vec<crate::model::StreamEvent> {
        vec![crate::model::StreamEvent::TextDelta {
            content_index: 0,
            delta: "BLOCKER\nThis is only an unfinished review".to_string(),
        }]
    }

    #[test]
    fn terminal_only_review_produces_a_verdict() {
        asupersync::test_utils::run_test(|| async {
            let (mut runtime, _) = scripted_runtime(vec![completed_reply(
                "CONCERN\nCheck the error path in src/example.rs.",
            )]);
            let AdvisorOutcome::Inject(verdict) = runtime.review_turn(&review_digest(), 0).await
            else {
                panic!("a completed terminal-only review must be usable");
            };
            assert_eq!(verdict.level, VerdictLevel::Concern);
            assert!(verdict.rationale.contains("src/example.rs"));
        });
    }

    #[test]
    fn disconnected_blocker_is_a_failure_not_an_injection() {
        asupersync::test_utils::run_test(|| async {
            let (mut runtime, _) = scripted_runtime(vec![disconnected_reply()]);
            assert!(matches!(
                runtime.review_turn(&review_digest(), 0).await,
                AdvisorOutcome::Failed
            ));
            assert_eq!(runtime.consecutive_failures, 1);
        });
    }

    #[test]
    fn truncated_terminal_review_is_not_injected() {
        asupersync::test_utils::run_test(|| async {
            let mut events = completed_reply("BLOCKER\nIncomplete review");
            if let crate::model::StreamEvent::Done { reason, message } = &mut events[0] {
                *reason = crate::model::StopReason::Length;
                message.stop_reason = crate::model::StopReason::Length;
            }
            let (mut runtime, _) = scripted_runtime(vec![events]);
            assert!(matches!(
                runtime.review_turn(&review_digest(), 0).await,
                AdvisorOutcome::Failed
            ));
        });
    }

    #[test]
    fn disable_survives_consumption_of_one_shot_notice() {
        asupersync::test_utils::run_test(|| async {
            let (mut runtime, provider) = scripted_runtime(vec![
                disconnected_reply(),
                disconnected_reply(),
                disconnected_reply(),
            ]);
            for turn in 0..3 {
                assert!(matches!(
                    runtime.review_turn(&review_digest(), turn).await,
                    AdvisorOutcome::Failed
                ));
            }
            assert!(runtime.is_disabled());
            assert!(runtime.disabled_notice.take().is_some());
            assert!(
                runtime.is_disabled(),
                "delivering a notice must not resume billing"
            );
            assert!(matches!(
                runtime.review_turn(&review_digest(), 3).await,
                AdvisorOutcome::Quiet
            ));
            assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 3);
            assert!(runtime.disabled_notice.is_none());
        });
    }

    #[test]
    fn completed_review_resets_only_consecutive_failure_count() {
        asupersync::test_utils::run_test(|| async {
            let (mut runtime, _) = scripted_runtime(vec![
                disconnected_reply(),
                completed_reply("NOTE\nThe edit preserves the existing error handling."),
                disconnected_reply(),
                disconnected_reply(),
                disconnected_reply(),
            ]);
            assert!(matches!(
                runtime.review_turn(&review_digest(), 0).await,
                AdvisorOutcome::Failed
            ));
            assert!(matches!(
                runtime.review_turn(&review_digest(), 1).await,
                AdvisorOutcome::Inject(_)
            ));
            assert_eq!(runtime.consecutive_failures, 0);
            for turn in 2..4 {
                assert!(matches!(
                    runtime.review_turn(&review_digest(), turn).await,
                    AdvisorOutcome::Failed
                ));
                assert!(!runtime.is_disabled());
            }
            assert!(matches!(
                runtime.review_turn(&review_digest(), 4).await,
                AdvisorOutcome::Failed
            ));
            assert!(runtime.is_disabled());
        });
    }

    #[test]
    fn zero_deadline_does_not_admit_provider_request() {
        asupersync::test_utils::run_test(|| async {
            let (runtime, provider) = scripted_runtime(Vec::new());
            let mut runtime = runtime.with_timeout(Duration::ZERO);
            assert!(matches!(
                runtime.review_turn(&review_digest(), 0).await,
                AdvisorOutcome::Failed
            ));
            assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 0);
        });
    }

    fn assistant(content: Vec<crate::model::ContentBlock>) -> Message {
        Message::Assistant(Arc::new(crate::model::AssistantMessage {
            content,
            ..Default::default()
        }))
    }

    fn call(name: &str, arguments: Value) -> crate::model::ContentBlock {
        crate::model::ContentBlock::ToolCall(crate::model::ToolCall {
            id: "digest-test".to_string(),
            name: name.to_string(),
            arguments,
            thought_signature: None,
        })
    }

    fn text(value: impl Into<String>) -> crate::model::ContentBlock {
        crate::model::ContentBlock::Text(crate::model::TextContent::new(value))
    }

    fn tool_error(content: Vec<crate::model::ContentBlock>) -> Message {
        Message::ToolResult(Arc::new(crate::model::ToolResultMessage {
            tool_call_id: "digest-test".to_string(),
            tool_name: "bash".to_string(),
            content,
            is_error: true,
            details: None,
            timestamp: 0,
        }))
    }

    #[test]
    fn digest_screens_commands_errors_and_final_text_before_clipping() {
        let key = "sk-abcdefghijklmnopqrstuvwxyz012345";
        let messages = vec![
            assistant(vec![
                call(
                    "bash",
                    serde_json::json!({"command": format!("{} {key}", "x".repeat(190))}),
                ),
                text(format!("{} {key}", "x".repeat(1_990))),
            ]),
            tool_error(vec![text(format!("{} {key}", "x".repeat(190)))]),
        ];
        let projection = build_digest(&messages);
        let rendered = digest_prompt(&projection);
        assert!(
            !rendered.contains("sk-"),
            "a clipped key prefix must never escape"
        );
        assert!(!rendered.contains("abcdef"));
        assert!(projection.commands_run[0].chars().count() <= 200);
        assert!(projection.tool_errors[0].chars().count() <= 200);
        assert!(projection.final_text.chars().count() <= MAX_FINAL_TEXT_CHARS);
        assert!(serde_json::to_string(&messages).unwrap().contains(key));
    }

    #[test]
    fn discovery_beyond_final_text_limit_protects_an_earlier_command_echo() {
        let secret = "r4nd0mCredentialValue123456";
        let messages = vec![assistant(vec![
            call(
                "bash",
                serde_json::json!({"command": format!("echo {secret}")}),
            ),
            text(format!("{} API_KEY={secret}", "x".repeat(2_100))),
        ])];
        let projection = build_digest(&messages);
        assert_eq!(projection.commands_run, ["echo <pi-secret:redacted>"]);
        assert!(!digest_prompt(&projection).contains(secret));
    }

    #[test]
    fn error_blocks_are_screened_as_one_complete_private_key_envelope() {
        let projection = build_digest(&[tool_error(vec![
            text(concat!("-----BEGIN ", "PRIVATE KEY-----")),
            text("PRIVATE-MATERIAL-CANARY"),
            text("-----END PRIVATE KEY-----"),
        ])]);
        assert_eq!(projection.tool_errors, ["<pi-secret:redacted>"]);
        assert!(!digest_prompt(&projection).contains("PRIVATE-MATERIAL-CANARY"));
    }

    #[test]
    fn oversized_fields_are_omitted_without_losing_safe_digest_fields() {
        let huge = format!("PRIVATE-PREFIX{}", "x".repeat(MAX_INPUT_BYTES));
        let messages = vec![
            assistant(vec![
                call("edit", serde_json::json!({"path": huge})),
                call("bash", serde_json::json!({"command": "cargo test"})),
                text("finished"),
            ]),
            tool_error(vec![text(&huge)]),
        ];
        let projection = build_digest(&messages);
        assert_eq!(projection.files_touched, [OMITTED_INPUT]);
        assert_eq!(projection.commands_run, ["cargo test"]);
        assert_eq!(projection.tool_errors, [OMITTED_INPUT]);
        assert_eq!(projection.final_text, "finished");
        assert_eq!(projection.tool_call_count, 2);
        assert!(!digest_prompt(&projection).contains("PRIVATE-PREFIX"));
    }

    #[test]
    fn direct_digest_is_screened_at_provider_admission_without_mutating_the_caller() {
        asupersync::test_utils::run_test(|| async {
            let secret = "r4nd0mCredentialValue123456";
            let digest = TurnDigest {
                files_touched: vec![secret.to_string()],
                commands_run: vec![format!("echo {secret}")],
                tool_errors: vec![format!("API_KEY={secret}")],
                final_text: "finished".to_string(),
                tool_call_count: 2,
                is_trivial: false,
            };
            let (mut runtime, provider) = scripted_runtime(vec![completed_reply(
                "CONCERN\nAvoid placing credentials in command arguments.",
            )]);
            assert!(matches!(
                runtime.review_turn(&digest, 0).await,
                AdvisorOutcome::Inject(_)
            ));
            let prompts = provider.prompts.lock().unwrap();
            assert_eq!(prompts.len(), 1);
            assert!(!prompts[0].contains(secret));
            assert!(prompts[0].contains("<pi-secret:redacted>"));
            assert!(!prompts[0].contains("<pi-secret:000001>"));
            drop(prompts);
            assert!(digest.commands_run[0].contains(secret));
            assert_eq!(runtime.consecutive_failures, 0);
        });
    }

    #[test]
    fn oversized_direct_digest_is_isolated_and_never_admits_a_provider_request() {
        asupersync::test_utils::run_test(|| async {
            let (mut runtime, provider) = scripted_runtime(Vec::new());
            let digest = TurnDigest {
                final_text: format!("PRIVATE-CANARY{}", "x".repeat(MAX_INPUT_BYTES)),
                tool_call_count: 1,
                ..Default::default()
            };
            for turn in 0..3 {
                assert!(matches!(
                    runtime.review_turn(&digest, turn).await,
                    AdvisorOutcome::Failed
                ));
            }
            assert!(runtime.is_disabled());
            assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 0);
            assert!(provider.prompts.lock().unwrap().is_empty());
            assert!(
                !runtime
                    .disabled_notice
                    .as_deref()
                    .unwrap()
                    .contains("PRIVATE-CANARY")
            );
        });
    }

    #[test]
    fn direct_digest_field_count_is_bounded_before_projection_allocation() {
        let digest = TurnDigest {
            files_touched: vec![String::new(); MAX_DIGEST_FILES + 1],
            ..Default::default()
        };
        let error = screen_digest(&digest).unwrap_err();
        assert!(error.to_string().contains("PI_ADVISOR_DIGEST_LIMIT"));
    }

    #[test]
    fn cancelled_reviews_preserve_failure_streak_and_do_not_disable_the_advisor() {
        asupersync::test_utils::run_test(|| async {
            let (mut runtime, provider) = scripted_runtime(vec![completed_reply(
                "CONCERN\nCheck the preserved error path before continuing.",
            )]);
            runtime.consecutive_failures = 2;
            let owner = crate::agent_cx::AgentCx::for_request();
            owner.cancel_with(
                asupersync::types::CancelKind::User,
                Some("review cancelled"),
            );
            for turn in 0..4 {
                assert!(matches!(
                    owner
                        .with_current(runtime.review_turn(&review_digest(), turn))
                        .await,
                    AdvisorOutcome::Quiet
                ));
            }
            assert_eq!(runtime.consecutive_failures, 2);
            assert!(!runtime.is_disabled());
            assert!(runtime.disabled_notice.is_none());
            assert_eq!(runtime.guard.notes_in_window, 0);
            assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 0);
            assert!(matches!(
                runtime.review_turn(&review_digest(), 4).await,
                AdvisorOutcome::Inject(_)
            ));
            assert_eq!(runtime.consecutive_failures, 0);
            assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 1);
        });
    }

    #[test]
    fn cancellation_racing_ready_blocker_does_not_inject_or_count_a_failure() {
        let executor = asupersync::runtime::RuntimeBuilder::current_thread()
            .build()
            .unwrap();
        let owner = crate::agent_cx::AgentCx::from_cx(
            executor.request_cx_with_budget(asupersync::Budget::new()),
        );
        let provider = Arc::new(ScriptedProvider {
            responses: std::sync::Mutex::new(
                vec![completed_reply(
                    "BLOCKER\nThis completed review belongs to cancelled work.",
                )]
                .into(),
            ),
            calls: std::sync::atomic::AtomicUsize::new(0),
            prompts: std::sync::Mutex::new(Vec::new()),
            cancel_on_call: Some(owner.cx().clone()),
        });
        let mut runtime = AdvisorRuntime::new(provider.clone(), "test".to_string())
            .with_api_key(Some("test-key".to_string()));
        runtime.consecutive_failures = 2;
        let outcome =
            executor.block_on(owner.with_current(runtime.review_turn(&review_digest(), 0)));
        assert!(matches!(outcome, AdvisorOutcome::Quiet));
        assert_eq!(runtime.consecutive_failures, 2);
        assert!(!runtime.is_disabled());
        assert!(runtime.disabled_notice.is_none());
        assert_eq!(runtime.guard.notes_in_window, 0);
        assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[test]
    fn timerless_review_is_isolated_without_admitting_the_provider() {
        let executor = asupersync::runtime::RuntimeBuilder::current_thread()
            .build()
            .unwrap();
        let owner = {
            let _guard = asupersync::Cx::for_request()
                .restrict::<asupersync::cx::cap::None>()
                .set_current_restricted();
            crate::agent_cx::AgentCx::for_current_or_request()
        };
        let (mut runtime, provider) = scripted_runtime(Vec::new());
        let outcome =
            executor.block_on(owner.with_current(runtime.review_turn(&review_digest(), 0)));
        assert!(matches!(outcome, AdvisorOutcome::Failed));
        assert_eq!(runtime.consecutive_failures, 1);
        assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 0);
    }
}