qubit-redact 0.9.0

Rule-driven redaction for fields, diagnostics, HTTP data, and Rust domain objects
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
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Shared accounting capabilities implemented by every transaction mode.

use std::mem::take;

#[cfg(feature = "json")]
use qubit_budget::json::JsonValueBudget;
#[cfg(feature = "json")]
use serde_json::Value;

#[cfg(feature = "json")]
use super::rendered_operation::RenderedOperation;
#[cfg(feature = "json")]
use super::rendered_summary::rendered_summary;
use super::runtime_core::RuntimeCore;
use super::transaction_phase::TransactionPhase;
use crate::RedactionPolicy;
#[cfg(any(feature = "json", feature = "http", feature = "uri"))]
use crate::RedactionReason;
use crate::RedactionSummary;
use crate::Sensitivity;
use crate::policy::ResolvedField;

/// Exposes publication-independent transaction accounting to format writers.
pub(crate) trait RuntimeSession {
    /// Returns the immutable shared accounting core.
    ///
    /// # Returns
    ///
    /// The shared accounting core borrowed for observation.
    #[must_use]
    fn runtime(&self) -> &RuntimeCore;

    /// Returns the mutable shared accounting core.
    ///
    /// # Returns
    ///
    /// The shared accounting core borrowed for state changes.
    #[must_use]
    fn runtime_mut(&mut self) -> &mut RuntimeCore;

    /// Reports whether this session performs inspection without rendering.
    ///
    /// # Returns
    ///
    /// Whether this transaction observes sensitivity without rendering text.
    #[must_use]
    fn is_inspection(&self) -> bool;

    /// Records one policy-resolved sensitivity for inspection.
    ///
    /// # Parameters
    ///
    /// - `sensitivity`: Newly observed policy classification.
    fn observe_sensitivity(&mut self, sensitivity: Sensitivity);

    /// Returns the immutable policy snapshot.
    ///
    /// # Returns
    ///
    /// The immutable policy snapshot retained by this transaction.
    #[must_use]
    #[inline(always)]
    fn policy(&self) -> &RedactionPolicy {
        self.runtime().policy()
    }

    /// Starts per-item accounting unless an outer operation owns the scope.
    ///
    /// # Returns
    ///
    /// `true` if this call owns a newly created item scope; `false` when an
    /// outer operation already owns it. Only the owner may close the scope.
    ///
    /// # Panics
    ///
    /// In debug builds, panics if internal summary and usage scope ownership
    /// disagree while creating an item scope.
    #[must_use]
    #[inline(always)]
    fn begin_item_summary(&mut self) -> bool {
        self.runtime_mut().begin_item_summary()
    }

    /// Ends per-item accounting when the caller created the active scope.
    ///
    /// # Parameters
    ///
    /// - `owns_item_summary`: Ownership returned by `begin_item_summary`.
    #[inline(always)]
    fn end_item_summary(&mut self, owns_item_summary: bool) {
        self.runtime_mut().end_item_summary(owns_item_summary);
    }

    /// Merges one accounting delta into transaction and active item summaries.
    ///
    /// # Parameters
    ///
    /// - `delta`: Observed completion and reason facts to merge.
    #[inline(always)]
    fn record_summary(&mut self, delta: RedactionSummary) {
        self.runtime_mut().record_summary(delta);
    }

    /// Adds retained output bytes to transaction and active item accounting.
    ///
    /// # Parameters
    ///
    /// - `bytes`: Newly retained final escaped bytes.
    #[inline(always)]
    fn record_output_bytes(&mut self, bytes: usize) {
        self.runtime_mut().record_output_bytes(bytes);
    }

    /// Starts one structured domain value.
    ///
    /// # Returns
    ///
    /// Whether a node was admitted and its nesting scope entered. Call
    /// `leave_domain_value` only after successful entry.
    #[must_use]
    #[inline(always)]
    fn begin_domain_value(&mut self) -> bool {
        self.runtime_mut().begin_domain_value()
    }

    /// Charges one domain field before its value is accessed.
    ///
    /// # Returns
    ///
    /// Whether one field node was charged before value access.
    #[must_use]
    #[inline(always)]
    fn admit_domain_field(&mut self) -> bool {
        self.runtime_mut().admit_domain_field()
    }

    /// Charges one collection item before its iterator advances.
    ///
    /// # Returns
    ///
    /// Whether one cumulative collection item was charged before value access.
    #[must_use]
    #[inline(always)]
    fn admit_domain_collection_item(&mut self) -> bool {
        self.runtime_mut().admit_domain_collection_item()
    }

    /// Checks one raw domain key before lookup, rendering, or value access.
    ///
    /// Returns whether the key fits the active per-key byte limit.
    ///
    /// # Parameters
    ///
    /// - `key`: Raw UTF-8 key checked before normalization or value access.
    ///
    /// # Returns
    ///
    /// Whether the key fits and structural traversal remains open.
    #[must_use]
    #[inline(always)]
    fn admit_domain_key(&mut self, key: &str) -> bool {
        self.runtime_mut().admit_domain_key(key)
    }

    /// Admits one format node through the shared structural ledger.
    ///
    /// # Parameters
    ///
    /// - `depth`: Root-inclusive format depth, starting at one.
    ///
    /// # Returns
    ///
    /// Whether the node was charged; rejection records its depth or traversal
    /// cause.
    #[must_use]
    #[inline(always)]
    fn admit_format_node(&mut self, depth: usize) -> bool {
        self.runtime_mut().admit_format_node(depth)
    }

    /// Admits one format collection item through the shared ledger.
    ///
    /// # Returns
    ///
    /// Whether a cumulative collection item was admitted and charged.
    #[must_use]
    #[inline(always)]
    fn admit_format_collection_item(&mut self) -> bool {
        self.admit_domain_collection_item()
    }

    /// Checks structural capacity before advancing an untrusted iterator.
    ///
    /// # Parameters
    ///
    /// - `depth`: Root-inclusive depth of the next format item.
    ///
    /// # Returns
    ///
    /// Whether both collection and node capacity permit advancing the iterator;
    /// this preflight does not charge the item itself.
    #[must_use]
    #[inline(always)]
    fn preflight_format_item(&mut self, depth: usize) -> bool {
        self.runtime_mut().preflight_format_item(depth)
    }

    /// Checks collection capacity before advancing an untrusted iterator.
    ///
    /// # Returns
    ///
    /// Whether collection capacity permits advancing the iterator; the caller
    /// must subsequently charge any yielded item.
    #[must_use]
    #[inline(always)]
    fn preflight_collection_item(&mut self) -> bool {
        self.runtime_mut().preflight_collection_item()
    }

    /// Admits a borrowed JSON value through the shared JSON ledger.
    ///
    /// # Parameters
    ///
    /// - `value`: Parsed tree to account before it is rendered.
    ///
    /// # Returns
    ///
    /// Whether the entire tree fits the shared JSON-specific limits.
    #[cfg(feature = "json")]
    #[must_use]
    #[inline(always)]
    fn admit_json_value(&mut self, value: &Value) -> bool {
        self.runtime_mut().admit_json_value(value)
    }

    /// Splits JSON structure accounting from lexical value accounting.
    ///
    /// # Returns
    ///
    /// Disjoint structural-accounting capability and lexical JSON value budget.
    #[cfg(feature = "json")]
    #[inline(always)]
    #[must_use]
    fn split_json_admission(&mut self) -> (super::JsonStructureAdmission<'_>, &mut JsonValueBudget) {
        self.runtime_mut().split_json_admission()
    }

    /// Records rejection by the transaction-wide JSON value budget.
    #[cfg(feature = "json")]
    #[inline(always)]
    fn record_json_value_limit_reached(&mut self) {
        self.runtime_mut().record_json_value_limit_reached();
    }

    /// Releases one active domain-value depth.
    ///
    /// # Panics
    ///
    /// In debug builds, panics if there is no successfully entered domain scope
    /// to leave. Every successful entry must have exactly one matching leave.
    #[inline(always)]
    fn leave_domain_value(&mut self) {
        self.runtime_mut().leave_domain_value();
    }

    /// Reports whether the active domain frame has stopped writing.
    ///
    /// # Returns
    ///
    /// Whether the current domain frame has stopped accepting content.
    #[must_use]
    #[inline(always)]
    fn domain_frame_is_truncated(&self) -> bool {
        self.runtime().domain_frame_truncated
    }

    /// Marks an inspection inconclusive for one machine-readable cause.
    ///
    /// # Parameters
    ///
    /// - `reason`: Cause preventing a conclusive inspection.
    ///
    /// # Panics
    ///
    /// In debug builds, panics if called on a rendering session.
    #[cfg(any(feature = "json", feature = "http", feature = "uri"))]
    #[inline]
    fn fail_inspection(&mut self, reason: RedactionReason) {
        debug_assert!(self.is_inspection());
        self.record_summary(RedactionSummary::truncated(reason));
    }

    /// Classifies one named scalar field without rendering its value.
    ///
    /// # Parameters
    ///
    /// - `field`: Raw field name used for classification.
    /// - `value`: Raw field value whose byte length participates in input
    ///   admission.
    ///
    /// # Panics
    ///
    /// In debug builds, panics if called on a rendering session.
    fn inspect_field(&mut self, field: &str, value: &str) {
        debug_assert!(self.is_inspection());
        if !self.admit_input(field.len().saturating_add(value.len())) {
            return;
        }
        if let ResolvedField::Sensitive { sensitivity } = self.policy().resolve_field(field) {
            self.observe_sensitivity(sensitivity);
        }
    }

    /// Returns output capacity still available to the active domain frame.
    ///
    /// # Returns
    ///
    /// Escaped bytes still available within this frame; inspection returns
    /// `usize::MAX` because it does not construct an output frame.
    #[must_use]
    #[inline(always)]
    fn remaining_domain_frame_output_bytes(&self) -> usize {
        if self.is_inspection() {
            return usize::MAX;
        }
        self.remaining_output_bytes()
            .saturating_sub(self.runtime().domain_frame_output_bytes)
    }

    /// Appends one complete fragment to the transaction-owned domain frame.
    ///
    /// # Parameters
    ///
    /// - `text`: Fragment already checked against the frame's escaped-byte
    ///   allowance.
    fn append_domain_frame_fragment(&mut self, text: &str) {
        if self.is_inspection() {
            return;
        }
        for character in text.chars() {
            self.runtime_mut().domain_frame.push(character);
            self.runtime_mut().domain_frame_output_bytes += encoded_log_safe_len(character);
        }
    }

    /// Appends a fragment while enforcing the shared output limit.
    ///
    /// # Parameters
    ///
    /// - `text`: Raw fragment escaped and charged one Unicode scalar at a time.
    ///
    /// # Returns
    ///
    /// Whether the full fragment was appended; rejection closes the frame and
    /// retains a safe fallback according to its remaining output capacity.
    fn write_domain_fragment(&mut self, text: &str) -> bool {
        if self.runtime().domain_frame_truncated {
            return false;
        }
        if self.is_inspection() {
            return true;
        }
        for character in text.chars() {
            if encoded_log_safe_len(character) > self.remaining_domain_frame_output_bytes() {
                self.mark_domain_frame_output_limit_reached();
                self.truncate_domain_frame_without_output_limit();
                return false;
            }
            self.runtime_mut().domain_frame.push(character);
            self.runtime_mut().domain_frame_output_bytes += encoded_log_safe_len(character);
        }
        true
    }

    /// Marks the active domain frame as having reached the output limit.
    #[inline(always)]
    fn mark_domain_frame_output_limit_reached(&mut self) {
        self.runtime_mut().domain_frame_output_limit_reached = true;
    }

    /// Marks the active domain frame as closed to later field access.
    #[inline(always)]
    fn mark_domain_frame_truncated(&mut self) {
        self.runtime_mut().domain_frame_truncated = true;
    }

    /// Removes raw characters until the encoded frame fits `limit` bytes.
    ///
    /// # Parameters
    ///
    /// - `limit`: Maximum retained escaped bytes after removing complete
    ///   characters.
    fn truncate_domain_frame_to(&mut self, limit: usize) {
        while self.runtime().domain_frame_output_bytes > limit {
            let Some(character) = self.runtime_mut().domain_frame.pop() else {
                self.runtime_mut().domain_frame_output_bytes = 0;
                return;
            };
            self.runtime_mut().domain_frame_output_bytes = self
                .runtime()
                .domain_frame_output_bytes
                .saturating_sub(encoded_log_safe_len(character));
        }
    }

    /// Appends the standard marker after structural or input truncation.
    fn truncate_domain_frame_without_output_limit(&mut self) {
        if self.runtime().domain_frame_truncated {
            return;
        }
        if self.is_inspection() {
            self.mark_domain_frame_truncated();
            return;
        }
        const MARKER: &str = "<truncated>";
        if MARKER.len() > self.remaining_output_bytes() {
            self.truncate_domain_frame_to(0);
            self.mark_domain_frame_output_limit_reached();
            self.runtime_mut().phase = TransactionPhase::OutputExhausted;
        } else {
            let limit = self.remaining_output_bytes().saturating_sub(MARKER.len());
            self.truncate_domain_frame_to(limit);
            self.append_domain_frame_fragment(MARKER);
        }
        self.mark_domain_frame_truncated();
    }

    /// Removes a final separator from the transaction-owned domain frame.
    fn trim_domain_frame_separator(&mut self) {
        if self.is_inspection() {
            return;
        }
        if self.runtime().domain_frame.ends_with(", ") {
            let length = self.runtime().domain_frame.len();
            self.runtime_mut().domain_frame.truncate(length - 2);
            self.runtime_mut().domain_frame_output_bytes = self.runtime().domain_frame_output_bytes.saturating_sub(2);
        }
    }

    /// Takes the completed domain frame and resets its local state.
    ///
    /// # Returns
    ///
    /// Owned raw frame text, omission flag, and output-limit flag, in that
    /// order. All local frame state is reset; callers retain shared
    /// transaction accounting.
    #[must_use]
    #[inline]
    fn finish_domain_frame(&mut self) -> (String, bool, bool) {
        let output = take(&mut self.runtime_mut().domain_frame);
        let truncated = take(&mut self.runtime_mut().domain_frame_truncated);
        let output_limit_reached = take(&mut self.runtime_mut().domain_frame_output_limit_reached);
        self.runtime_mut().domain_frame_output_bytes = 0;
        (output, truncated, output_limit_reached)
    }

    /// Records provenance for a format result embedded in a domain writer.
    ///
    /// # Parameters
    ///
    /// - `operation`: Unpublished rendering whose completion facts belong to
    ///   this frame.
    #[cfg(feature = "json")]
    #[inline]
    fn record_rendered_provenance(&mut self, operation: &RenderedOperation) {
        let summary = rendered_summary(operation.completion(), operation.reasons());
        self.record_summary(summary);
    }

    /// Reports whether the transaction exhausted its output budget.
    ///
    /// # Returns
    ///
    /// Whether no later rendering operation may inspect input or emit output.
    #[must_use]
    #[inline(always)]
    fn is_output_exhausted(&self) -> bool {
        !self.is_inspection() && self.runtime().is_output_exhausted()
    }

    /// Stops an operation before it observes input after output exhaustion.
    ///
    /// # Returns
    ///
    /// Whether the caller must skip this operation. A skipped rendering records
    /// output exhaustion without inspecting additional input.
    #[must_use]
    #[inline(always)]
    fn skip_aggregate_for_exhausted_output(&mut self) -> bool {
        !self.is_inspection() && self.runtime_mut().skip_aggregate_for_exhausted_output()
    }

    /// Returns output capacity still available to one renderer.
    ///
    /// # Returns
    ///
    /// Remaining final escaped-output bytes; inspection sessions use an
    /// unbounded sentinel because they produce no text.
    #[must_use]
    #[inline(always)]
    fn remaining_output_bytes(&self) -> usize {
        if self.is_inspection() {
            usize::MAX
        } else {
            self.runtime().remaining_output_bytes()
        }
    }

    /// Returns input capacity not yet inspected by this transaction.
    ///
    /// # Returns
    ///
    /// Remaining admitted input capacity; the inspection adapter reports an
    /// unbounded sentinel here but still enforces input through admission
    /// methods.
    #[must_use]
    #[inline(always)]
    fn remaining_input_bytes(&self) -> usize {
        if self.is_inspection() {
            usize::MAX
        } else {
            self.runtime().remaining_input_bytes()
        }
    }

    /// Admits encoded input before any parser or renderer observes it.
    ///
    /// # Parameters
    ///
    /// - `bytes`: Size of the complete raw input unit submitted for admission.
    ///
    /// # Returns
    ///
    /// Whether the whole unit was admitted. Rejected units count as presented
    /// but contribute no inspected bytes.
    #[inline(always)]
    fn admit_input(&mut self, bytes: usize) -> bool {
        self.runtime_mut().admit_input(bytes)
    }

    /// Records the measured input of a bounded scalar formatter.
    ///
    /// # Parameters
    ///
    /// - `presented`: Bytes submitted by the bounded formatter.
    /// - `inspected`: Accepted bytes, including bytes discarded after formatter
    ///   failure.
    #[inline(always)]
    fn record_input_usage(&mut self, presented: usize, inspected: usize) {
        self.runtime_mut().record_input_usage(presented, inspected);
    }

    /// Admits a captured source whose complete length may be unknown.
    ///
    /// # Parameters
    ///
    /// - `total`: `Some(length)` is the complete source length; `None` means
    ///   unknown.
    /// - `inspectable`: Captured bytes available for whole-unit admission.
    ///
    /// # Returns
    ///
    /// Whether the complete captured unit was admitted; omitted source
    /// accounting retains the distinction between a known count and unknown
    /// length.
    #[cfg(feature = "http")]
    #[inline(always)]
    fn admit_source_input(&mut self, total: Option<usize>, inspectable: usize) -> bool {
        self.runtime_mut().admit_source_input(total, inspectable)
    }
}

/// Returns the final log-safe byte count of one source character.
///
/// # Parameters
///
/// - `character`: Source Unicode scalar to measure.
///
/// # Returns
///
/// Its final encoded byte length under the common log-control escape rules.
#[must_use]
#[inline]
fn encoded_log_safe_len(character: char) -> usize {
    let mut buffer = [0_u8; 12];
    crate::output::log_escape::encode_log_safe_character(character, &mut buffer)
        .map_or(character.len_utf8(), |encoded| encoded.len())
}