lean-ctx 3.12.1

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. CLI, MCP server, and supported proxy integrations.
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
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
// SPDX-License-Identifier: Apache-2.0
pub(crate) use lean_ctx::core::command_root::{
    detect_project_root, promote_to_git_root, require_project_root,
};
pub(crate) use lean_ctx::core::shell_history::load_shell_history;

pub(crate) fn print_savings(original: usize, sent: usize) {
    let footer = lean_ctx::core::protocol::format_savings(original, sent);
    if !footer.is_empty() {
        println!("{footer}");
    }
}

/// One tool run on behalf of a CLI command — the ONE place where the CLI does
/// what the MCP dispatcher does around a tool handler (G01, G03/G04, G05).
///
/// Order, fixed here and nowhere else in `cli/`:
///
/// 1. the handler runs inside the request scope (so the shared read path
///    admits its sources) AND inside the source capture (so every source the
///    admitted readers publish is known);
/// 2. the finished text passes the release point every MCP tool result
///    passes (`server::policy_guard::release_result`), then the legacy output
///    redaction (`redact_model_text`). The result of this step is the text
///    the command prints — a command must not change it afterwards;
/// 3. the lineage of exactly that text is recorded
///    (`core::archive::delivered::record_tool_delivery`), which decides by
///    its own rules whether anything is recorded: an undeclared tool, an
///    error, an absent or incomplete capture record nothing. Unknown stays
///    unknown; nothing is guessed here;
/// 4. the caller writes the returned text with [`print_released`].
///
/// Receipt (G08): where the receipt store is mandatory (protection is
/// active), steps 1 and 2 run inside one admission capture, and between
/// steps 2 and 3 the Decision Receipt of the output is built, validated and
/// stored. It binds exactly the bytes the writer writes — the text and its
/// final `\n`. An output whose owed receipt cannot be stored is not printed:
/// the content-free notice `receipt.not_persisted` is returned in its place,
/// as a refusal. Nothing is added to stdout either way (#498). The principal
/// is `unknown` unless the process showed a presenter capability.
///
/// One policy snapshot (G08): the run takes it once, where it starts (the
/// first of steps 1 and 2), and its capture carries it — the release point,
/// the receipt duty and the trace sidecar of the run decide under it.
///
/// PREPARED, not handed over: the receipt and its sidecar exist before a
/// byte is written and prove no write. The hand-over is observed by the ONE
/// writer, [`print_released`] (stdout) or [`exit_withheld`] (stderr and the
/// exit status), after `write_all` and `flush` of exactly the prepared bytes
/// succeeded. The receipt's own handle waits for that write on the thread
/// that prepared it; a command that prints the returned text any other way
/// leaves the delivery unconfirmed, which is what the inspector then says.
/// Variants for output that is not `text + "\n"` on stdout:
/// [`CliToolRun::deliver_json_exact`] with [`print_released_exact`] (no
/// final newline), [`CliToolRun::to_stderr`] with [`eprint_released`].
///
/// Digested bytes: the returned text, and the same text followed by the one
/// `\n` that the writer adds. A consumer that takes the CLI's stdout as a
/// tool result holds one of the two (hosts differ in whether they trim the
/// final newline); both are the same delivery from the same sources. No other
/// prefix or wrapper is added by the CLI.
pub(crate) struct CliToolRun<'a> {
    tool: &'a str,
    project_root: &'a str,
    /// The arguments of the call. The tool's own declaration for them
    /// (`McpTool::publishes_every_source`) is asked only when there is a
    /// captured source set to record — the registry is not built for a run
    /// that can record nothing (no protection, no source, an error).
    arguments: Option<serde_json::Map<String, serde_json::Value>>,
    /// `false` for output that is not a tool-result carrier (a human-facing
    /// table, or the text wrapped into another document such as `--json`):
    /// steps 1 and 2 still apply, step 3 records nothing.
    carrier: bool,
    /// Whether step 2 ends with the legacy output redaction.
    legacy_redaction: bool,
    /// The admission capture of the output being prepared; opened only where
    /// the receipt store is mandatory, taken when the receipt is recorded.
    admissions: std::sync::Mutex<
        Option<std::sync::Arc<lean_ctx::core::context_admission::capture::AdmissionCapture>>,
    >,
    /// The receipt duty of this run and the ONE policy snapshot it was
    /// decided under, taken where the run starts.
    duty: std::sync::OnceLock<lean_ctx::core::context_admission::receipt_store::Duty>,
    /// The receipt index the output is listed under when it is not the
    /// project's own ([`Self::receipt_index`]).
    receipt_index: Option<&'a str>,
    /// Where the command hands the output: stdout unless a builder says
    /// otherwise ([`Self::to_stderr`], [`Self::to_file`],
    /// [`Self::to_clipboard`]).
    channel: lean_ctx::core::context_admission::receipt_store::trace::Channel,
    /// The Ed25519 public key a package signature was VERIFIED with, when the
    /// document names it ([`Self::verified_signer_key`]).
    verified_signer_key: Option<&'a str>,
}

/// The stdout text one handler produced, with what the helper has to know
/// about it to decide on a lineage record.
pub(crate) struct Printable {
    /// The text, without the final newline the writer adds.
    pub text: String,
    /// `false` when the handler mixed session-derived or hint text into the
    /// output: such a text is never recorded as a delivery of its sources.
    pub source_only: bool,
    /// The tool itself reports a failure (`ShellOutcome::is_error`, a non-zero
    /// exit status of the primitive): an error is never a delivery.
    pub failed: bool,
}

impl Printable {
    /// A successful tool text that consists of the tool's own output only.
    pub(crate) fn tool_text(text: String) -> Self {
        Self {
            text,
            source_only: true,
            failed: false,
        }
    }
}

impl<'a> CliToolRun<'a> {
    /// A run of the registered tool `tool` with `arguments` for `project_root`.
    /// Whether the tool declares every source is asked of the tool itself,
    /// for these arguments; an unknown tool declares nothing.
    pub(crate) fn new(
        tool: &'a str,
        project_root: &'a str,
        arguments: Option<&serde_json::Map<String, serde_json::Value>>,
    ) -> Self {
        Self {
            tool,
            project_root,
            arguments: arguments.cloned(),
            carrier: true,
            legacy_redaction: true,
            admissions: std::sync::Mutex::new(None),
            duty: std::sync::OnceLock::new(),
            receipt_index: None,
            channel: lean_ctx::core::context_admission::receipt_store::trace::Channel::Stdout,
            verified_signer_key: None,
        }
    }

    /// The document names the public key the Engine has just verified a
    /// package signature with (`"signer_public_key"`). The release then runs
    /// inside [`lean_ctx::core::redaction::with_verified_signer_key`]: the
    /// ORIGINAL document, real key included, passes the policy pack, the
    /// configured redaction patterns, the gateway and the redactor as always;
    /// only the built-in generic-secret rule does not take that one member
    /// for a secret. Nothing is substituted and nothing else is exempt.
    pub(crate) fn verified_signer_key(mut self, key: Option<&'a str>) -> Self {
        self.verified_signer_key = key;
        self
    }

    /// The command writes this run's output to stderr
    /// ([`eprint_released`]): the receipt is prepared for `Channel::Stderr`,
    /// for the text and its final `\n`. Everything else is unchanged.
    #[expect(
        clippy::wrong_self_convention,
        reason = "builder that redirects the run"
    )]
    pub(crate) fn to_stderr(mut self) -> Self {
        self.channel = lean_ctx::core::context_admission::receipt_store::trace::Channel::Stderr;
        self
    }

    /// The command writes this run's output to a FILE instead of a stream:
    /// the receipt is prepared for the channel `file`, for exactly the
    /// returned text (no `\n` appended). The path is not recorded. After the
    /// file was written successfully the command calls
    /// [`confirm_written_to_file`] with the bytes it wrote.
    #[expect(
        clippy::wrong_self_convention,
        reason = "builder that redirects the run"
    )]
    pub(crate) fn to_file(mut self) -> Self {
        self.channel = lean_ctx::core::context_admission::receipt_store::trace::Channel::File;
        self
    }

    /// The command hands this run's output to the system CLIPBOARD: the
    /// receipt is prepared for the channel `clipboard`, for exactly the
    /// returned text. After the hand-off succeeded the command calls
    /// [`confirm_copied_to_clipboard`] with the text it copied.
    #[expect(
        clippy::wrong_self_convention,
        reason = "builder that redirects the run"
    )]
    pub(crate) fn to_clipboard(mut self) -> Self {
        self.channel = lean_ctx::core::context_admission::receipt_store::trace::Channel::Clipboard;
        self
    }

    /// The channel this run's output is prepared for.
    fn output_channel(&self) -> lean_ctx::core::context_admission::receipt_store::trace::Channel {
        self.channel
    }

    /// Whether the writer of this run's channel adds one final `\n` to the
    /// returned text (the stream writers do; a file or the clipboard takes
    /// exactly the text).
    fn writer_adds_newline(&self) -> bool {
        use lean_ctx::core::context_admission::receipt_store::trace::Channel;
        matches!(self.channel, Channel::Stdout | Channel::Stderr)
    }

    /// List the output's receipt under `key` instead of the project's recent
    /// deliveries. For a command that reports ON those deliveries (`lean-ctx
    /// inspect`): its own output is receipted like any other, but must not
    /// displace the deliveries it shows. The receipt itself is unchanged.
    pub(crate) fn receipt_index(mut self, key: &'a str) -> Self {
        self.receipt_index = Some(key);
        self
    }

    /// Run `operation` inside this run's admission capture, so every gateway
    /// decision about the output reaches its receipt (G08). A capture that
    /// is already open (the run is part of a receipted call) is kept. The
    /// capture carries the run's policy snapshot, so everything inside it —
    /// the release point included — decides under the rules of the run's
    /// start. Must be called inside the request scope.
    ///
    /// The capture is opened in every mode, as the MCP call does: without
    /// protection a run whose capture describes an admitted object — a
    /// source it read, the text its release point admitted — owes the same
    /// plain receipt (`receipt_store::record_delivery`), and a run whose
    /// capture recorded nothing owes none ([`Self::receipted`]).
    /// Opening it only under protection left the decision of a short-lived
    /// `lean-ctx read` unrecorded: `lean-ctx inspect` could not show it.
    fn captured<T>(&self, operation: impl FnOnce() -> T) -> T {
        use lean_ctx::core::context_admission::capture;
        if capture::current().is_some() {
            return operation();
        }
        let admissions = self
            .admissions
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .get_or_insert_with(|| {
                capture::AdmissionCapture::for_call(self.duty().snapshot().clone())
            })
            .clone();
        capture::ADMISSIONS.sync_scope(Some(admissions), operation)
    }

    /// The receipt duty of this run under its ONE policy snapshot. The first
    /// call takes the snapshot — the start of the run; every later use is
    /// that same snapshot. Must be called inside the request scope.
    fn duty(&self) -> &lean_ctx::core::context_admission::receipt_store::Duty {
        self.duty
            .get_or_init(lean_ctx::core::context_admission::receipt_store::Duty::at_entry)
    }

    /// Whether the receipt store is mandatory for this run: decided once,
    /// under the run's snapshot, for acquisition, release and receipt.
    /// Must be called inside the request scope.
    fn mandatory(&self) -> bool {
        self.duty().mandatory
    }

    /// The digest of `text` as it is before the release point, for the trace
    /// sidecar; computed only where a receipt will be recorded.
    fn prepared_digest(&self, text: &str) -> Option<lean_ctx_protocol::Sha256Digest> {
        self.mandatory().then(|| {
            lean_ctx::core::context_admission::receipt_store::output_digest(text.as_bytes())
        })
    }

    /// Build, validate and store the receipt of exactly `written` — the
    /// bytes PREPARED for `channel` — before a byte of it is written, with
    /// its trace sidecar (transport CLI, digest before the release point,
    /// intended channel, digest of the prepared bytes) and audit anchor.
    /// Nothing here says the bytes were written: that is observed by the
    /// writer ([`print_released`], [`exit_withheld`]). `false`: the receipt
    /// is owed and missing, so `written` must not be written. Without a
    /// capture (a run inside a receipted call) there is nothing to record.
    /// Without protection a run whose capture recorded nothing owes nothing,
    /// and one that admitted something gets the plain receipt — no sidecar, no anchor
    /// duty, never a withheld output: a failed write is logged and the text
    /// is printed. Must be called inside the request scope and outside the
    /// run's capture.
    fn receipted(
        &self,
        written: &str,
        is_error: bool,
        before_release: Option<lean_ctx_protocol::Sha256Digest>,
        channel: lean_ctx::core::context_admission::receipt_store::trace::Channel,
    ) -> bool {
        use lean_ctx::core::context_admission::{capture::Delivered, receipt_store};
        let Some(admissions) = self
            .admissions
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take()
        else {
            return true;
        };
        let Some(duty) = self.duty.get() else {
            return true;
        };
        // The MCP call's rule (`finish_gateway_receipt`): without protection
        // a run whose capture recorded nothing owes nothing.
        if admissions.is_empty() && !duty.mandatory {
            return true;
        }
        let delivery = receipt_store::trace::Delivery::cli(self.tool, channel, duty.snapshot())
            .before_release(before_release);
        let presenter_ref = lean_ctx::core::archive::presented::shown_presenter_reference();
        let record = receipt_store::record_delivery(
            &admissions,
            &receipt_store::ReceiptSubject {
                tool: self.tool,
                agent_id: None,
                presenter_ref: presenter_ref.as_deref(),
                project_root: self.receipt_index.unwrap_or(self.project_root),
                delivery: &delivery,
            },
            &Delivered {
                text: written,
                is_error,
                tokens: lean_ctx::core::tokens::count_tokens(written) as u64,
            },
            duty,
        );
        tracing::debug!("context gateway receipt: {}", record.summary());
        let released = !record.withholds(duty.mandatory);
        // The receipt's own handle waits for this thread's next write of
        // exactly these bytes to the channel; no other write confirms it.
        if let Some(prepared) = record.into_prepared() {
            prepared.await_write();
        }
        released
    }

    /// The printed output is not a tool-result carrier: record no lineage.
    pub(crate) fn human_facing(mut self) -> Self {
        self.carrier = false;
        self
    }

    /// The command prints text that its own admitted reader produced and has
    /// never passed the legacy output redactor (`read`, `grep`, `glob`,
    /// `ls`). Step 2 then ends after the release point: running the legacy
    /// redactor on top could change bytes of an already admitted read, and
    /// those bytes are pinned (#498). Everything else is unchanged.
    pub(crate) fn admitted_reader_output(mut self) -> Self {
        self.legacy_redaction = false;
        self
    }

    /// The whole sequence for a handler that also returns a value of its own
    /// (an exit status, a recording): steps 1–3 above. `handler` returns its
    /// value and, when it produced stdout text, that text as a [`Printable`].
    /// The returned text is what the caller writes with [`print_released`]; `None`
    /// means nothing is printed, released or recorded.
    pub(crate) fn run<T>(
        &self,
        handler: impl FnOnce() -> (T, Option<Printable>),
    ) -> (T, Option<String>) {
        let ((value, printable), capture) = self.acquire(handler);
        let printed = printable.map(|printable| self.finish_checked(printable, capture, false).0);
        (value, printed)
    }

    /// [`Self::run`] that also says whether the release point refused the
    /// text: `(text, true)` is the content-free notice of a refusal, not the
    /// handler's output. For a command that also sends the released text to a
    /// destination of its own (a file, the clipboard) and must write no
    /// notice there. Steps 2 and 3 are those of [`Self::finish`].
    pub(crate) fn run_checked<T>(
        &self,
        handler: impl FnOnce() -> (T, Option<Printable>),
    ) -> (T, Option<(String, bool)>) {
        let ((value, printable), capture) = self.acquire(handler);
        // A caller that asks whether the text was refused does not print the
        // notice in place of the output: it ends through [`exit_withheld`],
        // which writes it to stderr. The refusal is prepared for that.
        let printed = printable.map(|printable| self.finish_checked(printable, capture, true));
        (value, printed)
    }

    /// Step 1 on its own, for a command whose acquisition and publication are
    /// separate phases (the protected CLI path prepares its output, verifies
    /// its authority and only then publishes): run `handler` inside the
    /// request scope and the source capture, and hand back what it acquired.
    /// The capture goes to [`Self::finish`] unchanged.
    pub(crate) fn acquire<T>(
        &self,
        handler: impl FnOnce() -> T,
    ) -> (
        T,
        Option<lean_ctx::core::archive::authority::ArchiveAuthority>,
    ) {
        lean_ctx_server::tools::admitted_sources::as_agent_acquisition(self.project_root, || {
            self.captured(|| lean_ctx::core::archive::authority::capture_sync(handler))
        })
    }

    /// Steps 2 and 3 for a text acquired under [`Self::acquire`]: release,
    /// redaction, lineage record of the returned text. The caller prints the
    /// result with [`print_released`] and changes nothing in it.
    pub(crate) fn finish(
        &self,
        printable: Printable,
        capture: Option<lean_ctx::core::archive::authority::ArchiveAuthority>,
    ) -> String {
        self.finish_checked(printable, capture, false).0
    }

    /// The one body of steps 2 and 3, for [`Self::finish`] and
    /// [`Self::run_checked`]: the text to print, and whether the release
    /// point refused it.
    fn finish_checked(
        &self,
        printable: Printable,
        capture: Option<lean_ctx::core::archive::authority::ArchiveAuthority>,
        refusal_on_stderr: bool,
    ) -> (String, bool) {
        lean_ctx_server::tools::admitted_sources::as_agent_acquisition(self.project_root, || {
            let prepared = self.prepared_digest(&printable.text);
            let (mut printed, mut refused) = self.captured(|| self.released(printable.text));
            let mut with_newline = format!("{printed}\n");
            // G08: the receipt of the bytes prepared for the run's stream is
            // stored before they are written; without it they are not
            // written. A refusal of the release point is printed like any
            // text: its bytes are bound in the trace sidecar (V1 names no
            // output for it).
            let failed = printable.failed || refused;
            // Prepared for the channel the bytes are really written to. A
            // stream run writes text + `\n` to its stream, a refusal too. A
            // file or clipboard run hands over exactly the text — but its
            // REFUSAL never goes to the file or the clipboard: the command
            // ends through [`exit_withheld`], which writes the notice and
            // `\n` to stderr. So that refusal is prepared for stderr — and so
            // is the refusal of any run whose caller ends that way
            // (`refusal_on_stderr`, [`Self::run_checked`]).
            let stderr = lean_ctx::core::context_admission::receipt_store::trace::Channel::Stderr;
            let (handed, channel) = if refused && (refusal_on_stderr || !self.writer_adds_newline())
            {
                (with_newline.as_str(), stderr)
            } else if self.writer_adds_newline() {
                (with_newline.as_str(), self.output_channel())
            } else {
                (printed.as_str(), self.output_channel())
            };
            if !self.receipted(handed, failed, prepared, channel) {
                printed = lean_ctx::core::context_admission::receipt_store::not_persisted_notice();
                with_newline = format!("{printed}\n");
                refused = true;
            }
            let is_error = printable.failed || refused;
            if self.carrier {
                // Session or hint text mixed into the output is expressed the
                // way `record_tool_delivery` expects it: the call then does
                // not declare every source.
                let declares = printable.source_only
                    && !is_error
                    && capture.is_some()
                    && lean_ctx_server::tools::registered::ctx_call::declares_every_source(
                        &lean_ctx_server::server::registry::build_registry(),
                        self.tool,
                        self.arguments.as_ref(),
                    );
                lean_ctx::core::archive::delivered::record_tool_delivery(
                    declares,
                    is_error,
                    capture,
                    &[printed.as_str(), with_newline.as_str()],
                );
            }
            (printed, refused)
        })
    }

    /// Step 2 for one text: the release point, then (unless the command
    /// prints an admitted reader's output) the legacy output redaction.
    /// Returns the text to print and whether the release point refused it.
    /// Must be called inside the request scope.
    fn released(&self, text: String) -> (String, bool) {
        use rmcp::model::{CallToolResult, ContentBlock};
        let released = lean_ctx_server::server::policy_guard::release_result(
            self.tool,
            CallToolResult::success(vec![ContentBlock::text(text)]),
        );
        let refused = released.is_error == Some(true);
        let released_text = released
            .content
            .iter()
            .filter_map(|block| block.as_text().map(|text| text.text.clone()))
            .collect::<Vec<_>>()
            .join("\n");
        let printed = if self.legacy_redaction {
            lean_ctx_server::server::policy_guard::redact_model_text(&released_text)
        } else {
            released_text
        };
        (printed, refused)
    }

    /// The sequence for a machine-readable document (`--json`): the handler
    /// runs inside the request scope, and the document it returns is released
    /// WITHOUT ever breaking it. A document is never recorded as a delivery of
    /// its sources — it wraps the tool's text into another representation.
    ///
    /// - `Ok(document)`: print it. An admitted document keeps its bytes. When
    ///   a value had to be masked, the masked value stands in its place in a
    ///   still valid document, and the content-free notice went to stderr —
    ///   the schema is not extended and nothing is prefixed to stdout.
    /// - `Err(reason)`: the document is withheld as a whole; `reason` is the
    ///   content-free notice for stderr. Nothing is printed to stdout.
    pub(crate) fn deliver_json(&self, handler: impl FnOnce() -> String) -> Result<String, String> {
        self.deliver_document(handler, true)
    }

    /// [`Self::deliver_json`] for a command that writes the document WITHOUT
    /// a final newline ([`print_released_exact`]): the receipt binds exactly
    /// the document's bytes, nothing appended.
    pub(crate) fn deliver_json_exact(
        &self,
        handler: impl FnOnce() -> String,
    ) -> Result<String, String> {
        self.deliver_document(handler, false)
    }

    /// The one body of [`Self::deliver_json`] and
    /// [`Self::deliver_json_exact`]. `final_newline`: the writer adds one
    /// `\n` after the document, so the receipt binds it too.
    fn deliver_document(
        &self,
        handler: impl FnOnce() -> String,
        final_newline: bool,
    ) -> Result<String, String> {
        use lean_ctx::core::context_admission::receipt_store::trace::Channel;
        let (document, _capture) = self.acquire(handler);
        let prepared = lean_ctx_server::tools::admitted_sources::as_agent_acquisition(
            self.project_root,
            || self.prepared_digest(&document),
        );
        let released = self.release_document(document);
        // G08: the receipt binds exactly the bytes the writer writes — the
        // document, and the final `\n` where the writer adds one. A
        // withheld document prepares nothing for stdout: its reason is
        // prepared for stderr. That those stderr bytes were written, and the
        // status the command ends with, is recorded by [`exit_withheld`]
        // when it has written them — never inferred from this `Err`.
        let (written, is_error, channel) = match &released {
            Ok((document, _)) if final_newline => {
                (format!("{document}\n"), false, self.output_channel())
            }
            Ok((document, _)) => (document.clone(), false, self.output_channel()),
            Err(reason) => (format!("{reason}\n"), true, Channel::Stderr),
        };
        let receipted = lean_ctx_server::tools::admitted_sources::as_agent_acquisition(
            self.project_root,
            || self.receipted(&written, is_error, prepared, channel),
        );
        if !receipted {
            return Err(lean_ctx::core::context_admission::receipt_store::not_persisted_notice());
        }
        let (document, notice) = released?;
        if let Some(notice) = notice {
            eprintln!("{notice}");
        }
        Ok(document)
    }

    /// [`Self::deliver_json`] without the handler run and without stderr: the
    /// released document and, when something was masked in it, the notice.
    /// Runs inside the run's admission capture (G08).
    fn release_document(&self, document: String) -> Result<(String, Option<String>), String> {
        let release = || {
            lean_ctx_server::tools::admitted_sources::as_agent_acquisition(
                self.project_root,
                || self.captured(|| self.release_document_scoped(document)),
            )
        };
        match self.verified_signer_key {
            Some(key) => lean_ctx::core::redaction::with_verified_signer_key(key, release),
            None => release(),
        }
    }

    fn release_document_scoped(
        &self,
        document: String,
    ) -> Result<(String, Option<String>), String> {
        use lean_ctx::core::context_admission::carrier::{ReleaseGate, Withheld};
        lean_ctx_server::tools::admitted_sources::as_agent_acquisition(self.project_root, || {
            // The whole document through the one release sequence first: an
            // admitted document passes unchanged and keeps its bytes (#498).
            let (whole, refused) = self.released(document.clone());
            if !refused && whole == document {
                return Ok((document, None));
            }
            let unadmitted = || Withheld::new(&["release.unadmitted_content"]).notice();
            // Something has to be masked. Masking the serialized text could
            // cut through its syntax, so every string value is released on
            // its own and the document is serialized again around them.
            let Ok(mut value) = serde_json::from_str::<serde_json::Value>(&document) else {
                return Err(if refused { whole } else { unadmitted() });
            };
            let gate = ReleaseGate::current();
            let mut notice = None;
            self.mask_leaves(&gate, &mut value, 0, &mut notice)?;
            let serialized = if document.contains('\n') {
                serde_json::to_string_pretty(&value)
            } else {
                serde_json::to_string(&value)
            };
            let Ok(masked) = serialized else {
                return Err(unadmitted());
            };
            // The result must itself pass the release sequence unchanged;
            // what value-wise masking cannot settle (a policy-pack refusal,
            // a finding in a key) withholds the document as a whole.
            let (verified, refused) = self.released(masked.clone());
            if refused {
                return Err(verified);
            }
            if verified != masked {
                return Err(unadmitted());
            }
            let notice =
                notice.unwrap_or_else(|| "[lean-ctx gateway: content redacted]".to_string());
            Ok((masked, Some(notice)))
        })
    }

    /// Release every string value of `value` in place. A key is never
    /// rewritten (that would change the schema): a finding in a key, a
    /// withheld value or a document nested too deep to inspect withholds the
    /// whole document with a content-free reason.
    fn mask_leaves(
        &self,
        gate: &lean_ctx::core::context_admission::carrier::ReleaseGate,
        value: &mut serde_json::Value,
        depth: usize,
        notice: &mut Option<String>,
    ) -> Result<(), String> {
        use lean_ctx::core::context_admission::carrier::{Release, Withheld};
        use serde_json::Value;
        const MAX_DEPTH: usize = 64;
        if depth > MAX_DEPTH {
            return Err(Withheld::new(&["coverage.partial"]).notice());
        }
        match value {
            Value::String(text) => {
                match gate.text(text) {
                    Release::Unchanged => {}
                    Release::Redacted {
                        text: masked,
                        notice: line,
                    } => {
                        *text = masked;
                        notice.get_or_insert(line);
                    }
                    Release::Withheld(withheld) => return Err(withheld.notice()),
                }
                if self.legacy_redaction {
                    *text = lean_ctx_server::server::policy_guard::redact_model_text(text);
                }
            }
            Value::Array(items) => {
                for item in items {
                    self.mask_leaves(gate, item, depth + 1, notice)?;
                }
            }
            Value::Object(map) => {
                for (key, item) in map {
                    if !matches!(gate.text(key), Release::Unchanged) {
                        return Err(Withheld::new(&["release.unadmitted_content"]).notice());
                    }
                    self.mask_leaves(gate, item, depth + 1, notice)?;
                }
            }
            Value::Null | Value::Bool(_) | Value::Number(_) => {}
        }
        Ok(())
    }

    /// Run `handler` and return the text to print (steps 1–3 above). A
    /// handler error is passed through; nothing is released or recorded for
    /// it.
    pub(crate) fn deliver<E>(
        &self,
        handler: impl FnOnce() -> Result<String, E>,
    ) -> Result<String, E> {
        let (outcome, printed) = self.run(|| match handler() {
            Ok(text) => (Ok(()), Some(Printable::tool_text(text))),
            Err(error) => (Err(error), None),
        });
        outcome.map(|()| printed.unwrap_or_default())
    }

    /// [`Self::deliver`] for a handler that cannot fail.
    pub(crate) fn deliver_text(&self, handler: impl FnOnce() -> String) -> String {
        match self.deliver(|| Ok::<String, std::convert::Infallible>(handler())) {
            Ok(text) => text,
            Err(never) => match never {},
        }
    }
}

/// The ONE stdout writer for a text a [`CliToolRun`] returned: `text` and
/// its final `\n`, written and flushed, and — only after both succeeded —
/// the observation that exactly the bytes a receipt was prepared for were
/// handed to stdout (G08, `receipt_store::observed`). Text no receipt was
/// prepared for (no protection) is written the same way and records
/// nothing. A failed write ends the command with status 1; it records no
/// hand-over.
pub(crate) fn print_released(text: &str) {
    write_stdout_observed(format!("{text}\n").as_bytes());
}

/// [`print_released`] without the final `\n`: exactly `text` is written. For
/// output whose receipt was prepared for exactly its bytes
/// ([`CliToolRun::deliver_json_exact`]).
pub(crate) fn print_released_exact(text: &str) {
    write_stdout_observed(text.as_bytes());
}

fn write_stdout_observed(bytes: &[u8]) {
    use lean_ctx::core::context_admission::receipt_store::{observed, trace::Channel};
    let written =
        observed::write_observed(&mut std::io::stdout().lock(), Channel::Stdout, bytes, None);
    if let Err(error) = written {
        eprintln!("lean-ctx: output could not be written: {error}");
        std::process::exit(1);
    }
}

/// The ONE stderr writer for a text a [`CliToolRun`] prepared for stderr
/// ([`CliToolRun::to_stderr`]): `text` and its final `\n`, written and
/// flushed, then the observation for the receipt this thread prepared for
/// exactly those bytes. Returns to the caller. `exit_status`: the status the
/// caller ends the process with right after, when it does; it is recorded
/// with the observation. A failed write records nothing.
pub(crate) fn eprint_released(text: &str, exit_status: Option<i32>) {
    use lean_ctx::core::context_admission::receipt_store::{observed, trace::Channel};
    let line = format!("{text}\n");
    let _ = observed::write_observed(
        &mut std::io::stderr().lock(),
        Channel::Stderr,
        line.as_bytes(),
        exit_status,
    );
}

/// The command wrote exactly `bytes` to the file it was asked for and the
/// write SUCCEEDED: record `handed_over` (channel `file`) for the receipt
/// this thread prepared with [`CliToolRun::to_file`], if these are the
/// prepared bytes. Call it only after the successful write; other bytes, or
/// no prepared receipt, record nothing.
pub(crate) fn confirm_written_to_file(bytes: &[u8]) {
    use lean_ctx::core::context_admission::receipt_store::{observed, trace::Channel};
    observed::handed_over(Channel::File, bytes, None);
}

/// The command handed exactly `text` to the system clipboard and the
/// hand-off SUCCEEDED: record `handed_over` (channel `clipboard`) for the
/// receipt this thread prepared with [`CliToolRun::to_clipboard`]. Call it
/// only after that success.
pub(crate) fn confirm_copied_to_clipboard(text: &str) {
    use lean_ctx::core::context_admission::receipt_store::{observed, trace::Channel};
    observed::handed_over(Channel::Clipboard, text.as_bytes(), None);
}

/// End a command whose machine-readable document was withheld as a whole
/// ([`CliToolRun::deliver_json`]): the content-free reason on stderr, exit 1,
/// nothing on stdout. The ONE writer of that reason: after the bytes were
/// written and flushed, the observation binds them and the exit status the
/// process is ended with on the next line (G08). A failed write records
/// nothing; the process ends with status 1 either way.
pub(crate) fn exit_withheld(reason: &str) -> ! {
    eprint_released(reason, Some(1));
    std::process::exit(1);
}

/// Strip savings footers from daemon output when the CLI client has footer suppressed.
#[cfg(unix)]
pub(crate) fn filter_daemon_output(text: &str) -> String {
    if lean_ctx::core::protocol::savings_footer_visible() {
        return text.to_string();
    }
    text.lines()
        .filter(|l| {
            let t = l.trim();
            !(t.starts_with('[')
                && t.contains("tok")
                && t.ends_with(']')
                && (t.contains("tok saved") || t.contains("lean-ctx:") || t.contains("vs native")))
        })
        .collect::<Vec<_>>()
        .join("\n")
}

pub(crate) fn daemon_fallback_hint() {
    use std::sync::Once;
    static HINT: Once = Once::new();
    HINT.call_once(|| {
        if lean_ctx::core::protocol::meta_visible() {
            eprintln!("\x1b[2;33mhint: daemon not running — stats tracked locally (lean-ctx serve -d for full tracking)\x1b[0m");
        }
    });
}

pub(crate) fn format_tokens_cli(tokens: u64) -> String {
    if tokens >= 1_000_000_000_000 {
        format!("{:.2}T", tokens as f64 / 1_000_000_000_000.0)
    } else if tokens >= 1_000_000_000 {
        // Heavy users cross 1B; keep growing visibly instead of "1310.0M".
        format!("{:.2}B", tokens as f64 / 1_000_000_000.0)
    } else if tokens >= 1_000_000 {
        format!("{:.1}M", tokens as f64 / 1_000_000.0)
    } else if tokens >= 1_000 {
        format!("{:.1}K", tokens as f64 / 1_000.0)
    } else {
        format!("{tokens}")
    }
}

/// Shared set-up for the tests of the CLI commands that run through
/// [`CliToolRun`]. Every test starts with the isolation lines
/// (`isolated_data_dir`, `TestPolicyOverride`) itself.
#[cfg(test)]
pub(crate) mod carrier_tests_support {
    /// A value the gateway masks in every mode.
    pub(crate) const AWS_KEY: &str = concat!("AK", "IAIOSFODNN7EXAMPLE");

    pub(crate) use lean_ctx::core::config::test_support::governed;

    /// A project directory and its canonical root.
    pub(crate) fn project() -> (tempfile::TempDir, String) {
        let project = tempfile::tempdir().expect("project dir");
        let root = std::fs::canonicalize(project.path())
            .expect("root")
            .to_string_lossy()
            .into_owned();
        (project, root)
    }

    /// Write `content` to `name` under `root`; returns the absolute path.
    pub(crate) fn source_file(root: &str, name: &str, content: &str) -> String {
        let path = std::path::Path::new(root).join(name);
        std::fs::write(&path, content).expect("fixture");
        path.to_string_lossy().into_owned()
    }

    /// What a handler does that derives its output from a project source:
    /// one read through the admitted reader, which publishes the source.
    pub(crate) fn acquire_source(root: &str, path: &str) {
        lean_ctx_server::tools::ctx_read::read_file_for_tool_rooted(path, root, "ctx_read")
            .expect("admitted source");
    }

    /// The lineage on record for exactly `printed`, looked up and judged
    /// inside the project scope, as the proxy does. `None`: not registered.
    pub(crate) fn lineage(root: &str, printed: &str) -> Option<bool> {
        lean_ctx_server::tools::admitted_sources::as_agent_acquisition(root, || {
            lean_ctx::core::archive::delivered::lineage_of(
                &lean_ctx::core::archive::delivered::digest(printed),
            )
            .map(|lineage| lineage.still_admitted().is_ok())
        })
    }

    /// The daemon route switched off for the lifetime of the guard.
    pub(crate) struct NoDaemon;

    impl NoDaemon {
        pub(crate) fn set() -> Self {
            lean_ctx::test_env::set_var("__LEAN_CTX_NO_DAEMON", "1");
            Self
        }
    }

    impl Drop for NoDaemon {
        fn drop(&mut self) {
            lean_ctx::test_env::remove_var("__LEAN_CTX_NO_DAEMON");
        }
    }
}

#[cfg(test)]
#[path = "common_tests.rs"]
mod tests;