lean-ctx 3.9.3

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
macro_rules! static_regex {
    ($pattern:expr_2021) => {{
        static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
        RE.get_or_init(|| {
            regex::Regex::new($pattern).expect(concat!("BUG: invalid static regex: ", $pattern))
        })
    }};
}

pub fn redaction_enabled_for_active_role() -> bool {
    let role = crate::core::roles::active_role();
    if role.role.name == "admin" {
        role.io.redact_outputs
    } else {
        // Contract: redaction never disabled for non-admin roles.
        true
    }
}

pub fn redact_text_if_enabled(input: &str) -> String {
    if !redaction_enabled_for_active_role() {
        return input.to_string();
    }
    redact_text_with_excludes(input, &config_exclude_patterns())
}

/// #718: unquoted identifier or property-access chains (`SvelteKit`,
/// `inputEnv.POCKETBASE_SUPERUSER_PASSWORD`, `serverEnv.getStripeSecretKey`,
/// `confirmRequiredEndpointKeySchema`) are code REFERENCES to a secret, never
/// the literal value — the value lives in a gitignored `.env`. Digits make a
/// token secret-shaped (base64/hex), so any digit keeps the redaction
/// (conservative: `password=hunter2` stays covered).
fn is_identifier_reference(value: &str) -> bool {
    let v = value.trim();
    if v.is_empty()
        || v.starts_with('"')
        || v.starts_with('\'')
        || v.starts_with('`')
        || v.contains(|c: char| c.is_ascii_digit())
    {
        return false;
    }
    v.split('.').all(|segment| {
        let mut chars = segment.chars();
        matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_' || c == '$')
            && chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
    })
}

/// #718: obvious placeholder/example values (`ghp_change_me`, `your_key_here`,
/// `<insert-token>`) are documentation, not secrets — `.env.example` files
/// must survive ctx_read verbatim.
fn is_placeholder_value(value: &str) -> bool {
    let v = value
        .trim()
        .trim_matches(|c| c == '"' || c == '\'' || c == '`')
        .to_ascii_lowercase();
    if v.starts_with('<') && v.ends_with('>') {
        return true;
    }
    const MARKERS: &[&str] = &[
        "change_me",
        "change-me",
        "changeme",
        "example",
        "placeholder",
        "your_",
        "your-",
        "xxx",
        "dummy",
        "sample",
        "todo",
        "fixme",
        "replace_me",
        "replace-me",
    ];
    MARKERS.iter().any(|m| v.contains(m))
}

/// Right-hand sides that look like `key: value` but are obviously not secrets:
/// TypeScript type annotations and language literals. Redacting these corrupts
/// source files read through `ctx_read` (GH #430), so the key/value rules skip
/// them. Compared case-insensitively after trimming surrounding quotes.
fn is_non_secret_literal(value: &str) -> bool {
    let v = value
        .trim()
        .trim_matches(|c| c == '"' || c == '\'' || c == '`');
    // Type expressions are never flat secret tokens: real keys/tokens are drawn
    // from `[A-Za-z0-9+/=_-]`, whereas type annotations carry angle brackets,
    // unions, arrays or call/object syntax. `password: Promise<string>` and
    // `apiKey: Record<string, unknown>` must survive ctx_read verbatim (GH #430).
    if v.contains(['<', '>', '|', '(', ')', '[', ']', '{', '}']) {
        return true;
    }
    matches!(
        v.to_ascii_lowercase().as_str(),
        "" | "undefined"
            | "null"
            | "none"
            | "nil"
            | "true"
            | "false"
            | "string"
            | "number"
            | "boolean"
            | "bigint"
            | "symbol"
            | "object"
            | "any"
            | "unknown"
            | "never"
            | "void"
            | "nan"
            | "date"
    )
}

/// One redaction rule: a labelled regex plus how the match is rebuilt.
struct Rule {
    label: &'static str,
    re: &'static regex::Regex,
    /// When set, group 1 is a prefix to keep and group 2 is the secret value;
    /// the match is left untouched if that value is a non-secret literal
    /// (`password: undefined`), an identifier reference
    /// (`serverEnv.getStripeSecretKey`) or a placeholder (`ghp_change_me`) —
    /// see `is_benign_secret_value` (GH #430, #718).
    guard_value: bool,
}

/// Combined benign-value check for key/value secret rules (#430 + #718):
/// language literals and type annotations, unquoted identifier/property
/// references, and documentation placeholders are never redacted. Quoted
/// string values stay protected — they ARE literal values.
pub(crate) fn is_benign_secret_value(value: &str) -> bool {
    is_non_secret_literal(value) || is_identifier_reference(value) || is_placeholder_value(value)
}

/// The single source of truth for secret patterns. `shell::redact` delegates
/// here so the two layers can never drift apart again.
///
/// #718 word boundaries: the key/value alternations start with
/// `(?:^|[^a-z0-9])` (consumed into the kept prefix — the regex crate has no
/// lookbehind) so camelCase subwords (`superuserPassword`,
/// `getStripeSecretKey`) never trigger a rule, while SNAKE_CASE env names
/// (`GITHUB_FEEDBACK_TOKEN`) still do: `_` remains a permitted predecessor.
fn redaction_rules() -> Vec<Rule> {
    vec![
        Rule {
            label: "Bearer token",
            re: static_regex!(r"(?i)(bearer\s+)[a-zA-Z0-9\-_\.]{8,}"),
            guard_value: false,
        },
        Rule {
            label: "Authorization header",
            re: static_regex!(r"(?i)(authorization:\s*(?:basic|bearer|token)\s+)[^\s\r\n]+"),
            guard_value: false,
        },
        // Key/value secrets: group 1 = predecessor + `name=`/`name: ` prefix
        // (kept), group 2 = the value (redacted unless benign — GH #430/#718).
        Rule {
            label: "API key param",
            re: static_regex!(
                r#"(?im)((?:^|[^a-z0-9])(?:api[_-]?key|apikey|access[_-]?key|secret[_-]?key|token|password|passwd|pwd|secret)\s*[=:]\s*)([^\s\r\n,;&"']+)"#
            ),
            guard_value: true,
        },
        // Whole token is the secret — no prefix group, so the entire match is
        // replaced. (Previously group 1 captured the key itself and leaked it.)
        Rule {
            label: "AWS key",
            re: static_regex!(r"AKIA[0-9A-Z]{12,}"),
            guard_value: false,
        },
        Rule {
            label: "Private key block",
            re: static_regex!(
                r"(?s)(-----BEGIN\s+(?:RSA\s+)?PRIVATE\s+KEY-----).+?-----END\s+(?:RSA\s+)?PRIVATE\s+KEY-----"
            ),
            guard_value: false,
        },
        Rule {
            label: "GitHub token",
            re: static_regex!(r"(gh[pousr]_)[a-zA-Z0-9]{20,}"),
            guard_value: false,
        },
        // Group 1 = prefix (kept), group 2 = the 32+ char value. Guarded since
        // #718: 32-char identifiers like `confirmRequiredEndpointKeySchema`
        // are references, not secrets.
        Rule {
            label: "Generic long secret",
            re: static_regex!(
                r#"(?im)((?:^|[^a-z0-9])(?:key|token|secret|password|credential|auth)\s*[=:]\s*)(['"]?[a-zA-Z0-9+/=\-_]{32,}['"]?)"#
            ),
            guard_value: true,
        },
    ]
}

pub fn redact_text(input: &str) -> String {
    redact_text_with_excludes(input, &[])
}

/// #718: `redact_text` with subtractive user patterns from
/// `[secret_detection].exclude_patterns` — a match covered by any exclude
/// regex is kept verbatim, so known-safe naming conventions can be carved out
/// without disabling secret detection wholesale.
pub fn redact_text_with_excludes(input: &str, excludes: &[regex::Regex]) -> String {
    let mut out = input.to_string();
    for rule in redaction_rules() {
        out = rule
            .re
            .replace_all(&out, |caps: &regex::Captures| {
                let whole = caps.get(0).map_or("", |m| m.as_str());
                if excludes.iter().any(|ex| ex.is_match(whole)) {
                    return whole.to_string();
                }
                if rule.guard_value
                    && let Some(value) = caps.get(2)
                    && is_benign_secret_value(value.as_str())
                {
                    // Not a secret (identifier reference, literal, placeholder)
                    // — keep verbatim (#430, #718).
                    return whole.to_string();
                }
                match caps.get(1) {
                    Some(prefix) => format!("{}[REDACTED:{}]", prefix.as_str(), rule.label),
                    None => format!("[REDACTED:{}]", rule.label),
                }
            })
            .to_string();
    }
    out
}

/// Compile the configured `exclude_patterns` (#718). Invalid regexes are
/// skipped — a broken exclude must never disable redaction.
pub fn config_exclude_patterns() -> Vec<regex::Regex> {
    crate::core::config::Config::load()
        .secret_detection
        .exclude_patterns
        .iter()
        .filter_map(|p| regex::Regex::new(p).ok())
        .collect()
}

/// Apply caller-supplied policy redaction patterns on top of the built-in
/// secret rules: each regex match becomes `[REDACTED:<label>]`. Returns the
/// transformed text and the number of redactions applied (for audit counts).
///
/// Used by context policy packs (GL #673) so a pack's `[redaction]` block
/// actually removes matching content from what the model sees. The patterns are
/// the pack's `[redaction]` entries, precompiled by
/// [`crate::core::policy::runtime`].
#[must_use]
pub fn redact_with_patterns(input: &str, patterns: &[(String, regex::Regex)]) -> (String, usize) {
    let mut out = input.to_string();
    let mut hits = 0usize;
    for (label, re) in patterns {
        let mut local = 0usize;
        out = re
            .replace_all(&out, |_caps: &regex::Captures| {
                local += 1;
                format!("[REDACTED:{label}]")
            })
            .to_string();
        hits += local;
    }
    (out, hits)
}

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

    #[test]
    fn redacts_bearer_token() {
        let s = "Authorization: Bearer abcdefghijklmnopqrstuvwxyz012345";
        let out = redact_text(s);
        assert!(out.contains("[REDACTED"));
        assert!(!out.contains("abcdefghijklmnopqrstuvwxyz"));
    }

    #[test]
    fn redacts_private_key_block() {
        let s = "-----BEGIN PRIVATE KEY-----\nabc\n-----END PRIVATE KEY-----";
        let out = redact_text(s);
        assert!(out.contains("[REDACTED"));
        assert!(!out.contains("\nabc\n"));
    }

    #[test]
    fn redacts_api_key_param_value() {
        let out = redact_text("password=hunter2-super-secret-value");
        assert!(
            out.contains("password=[REDACTED:API key param]"),
            "got: {out}"
        );
        assert!(!out.contains("hunter2"));
    }

    /// GH #430: TypeScript type annotations and language literals must NOT be
    /// redacted — over-eager masking corrupted source files read via ctx_read.
    #[test]
    fn keeps_non_secret_literals() {
        for s in [
            "password: undefined",
            "secret: string",
            "token: null",
            "apiKey: boolean",
            "password = false",
            "secret: any",
            "let pwd: number = 1",
        ] {
            assert_eq!(redact_text(s), s, "must not redact non-secret literal: {s}");
        }
    }

    /// GH #430: TS type annotations (generics, unions, arrays, function/object
    /// types) carry angle brackets / brackets that real secret tokens never do,
    /// so they must survive verbatim even when the key looks sensitive.
    #[test]
    fn keeps_type_annotations() {
        for s in [
            "password: Promise<string>",
            "apiKey: Record<string, unknown>",
            "token: string[]",
            "secret: () => void",
            "password: string | undefined",
            "credential: { value: string }",
        ] {
            assert_eq!(redact_text(s), s, "must not redact type annotation: {s}");
        }
    }

    /// Whole-token secrets must be removed, not annotated in place — previously
    /// the closure kept group 1 (the key itself) and only appended `[REDACTED]`.
    #[test]
    fn fully_redacts_aws_key() {
        let out = redact_text("AKIAIOSFODNN7EXAMPLE");
        assert!(
            !out.contains("AKIAIOSFODNN7EXAMPLE"),
            "AWS key leaked: {out}"
        );
        assert!(out.contains("[REDACTED:AWS key]"));
    }

    #[test]
    fn fully_redacts_generic_long_secret() {
        // `credential=` is not covered by the API-key-param rule, so this
        // exercises the generic fallback (the previously leaky path).
        let secret = "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6"; // 32 chars
        let out = redact_text(&format!("credential={secret}"));
        assert!(!out.contains(secret), "long secret leaked: {out}");
        assert!(
            out.contains("credential=[REDACTED:Generic long secret]"),
            "got: {out}"
        );
    }

    #[test]
    fn redacts_github_token_keeping_prefix() {
        let out = redact_text("ghp_abcdefghijklmnopqrstuvwxyz0123");
        assert!(out.starts_with("ghp_[REDACTED:GitHub token]"), "got: {out}");
        assert!(!out.contains("abcdefghijklmnopqrstuvwxyz"));
    }

    // ── #718: benign identifier references, prose and placeholders ──

    /// Repro 1: prose that mentions a keyword must not have the following
    /// word redacted — "token: SvelteKit's…" is documentation, not a secret.
    #[test]
    fn keeps_prose_identifier_after_keyword() {
        let s = "the CSRF token: SvelteKit's native origin-check on form actions";
        assert_eq!(redact_text(s), s, "prose must survive verbatim");
    }

    /// Repro 2: camelCase subwords must not trigger the keyword alternation,
    /// and identifier/property-access RHS values are references, not secrets.
    #[test]
    fn keeps_identifier_and_property_references() {
        for s in [
            "superuserPassword: inputEnv.POCKETBASE_SUPERUSER_PASSWORD",
            "export const getStripeSecretKey = serverEnv.getStripeSecretKey;",
            "const apiKey = config.stripeApiKey",
        ] {
            assert_eq!(redact_text(s), s, "identifier reference redacted: {s}");
        }
    }

    /// Repro 3: a 32+ char identifier (Zod schema name) is a reference —
    /// "Generic long secret" needs the same value guard as the API-key rule.
    #[test]
    fn keeps_long_schema_identifier() {
        let s = "endpoint_key: confirmRequiredEndpointKeySchema,";
        assert_eq!(redact_text(s), s, "schema identifier must not be redacted");
    }

    /// Repro 4: obvious placeholder values (.env.example) are documentation.
    #[test]
    fn keeps_placeholder_values() {
        for s in [
            "GITHUB_FEEDBACK_TOKEN=ghp_change_me",
            "API_KEY=your_key_here",
            "password=<insert-password>",
            "SECRET_KEY=xxxxxxxx",
        ] {
            assert_eq!(redact_text(s), s, "placeholder redacted: {s}");
        }
    }

    /// The flip side: real secret-shaped values must STILL be redacted after
    /// the #718 guards.
    #[test]
    fn still_redacts_real_secret_values() {
        // Digit-bearing value after a snake_case env name.
        let out = redact_text("GITHUB_TOKEN=ghpA1b2c3d4e5f6g7h8");
        assert!(!out.contains("ghpA1b2c3d4e5f6g7h8"), "leaked: {out}");
        // SNAKE_CASE env assignment with digits (the _ predecessor stays a
        // word boundary that MATCHES).
        let out = redact_text("MY_SECRET=abc123def456ghi789");
        assert!(!out.contains("abc123def456ghi789"), "leaked: {out}");
        // Quoted 32+ char literal: a quoted value is never an identifier
        // reference, so the Generic-long-secret guard keeps redacting it.
        let quoted = "key: 'abcdefghijklmnopqrstuvwxyzabcdef'";
        let out = redact_text(quoted);
        assert!(
            !out.contains("abcdefghijklmnopqrstuvwxyzabcdef"),
            "leaked: {out}"
        );
    }

    /// #718: exclude_patterns carve matches out subtractively.
    #[test]
    fn exclude_patterns_skip_matching_redactions() {
        let excludes = vec![regex::Regex::new(r"LCTX_TEST_\w+").unwrap()];
        let input = "token=LCTX_TEST_a1b2c3d4e5";
        assert_eq!(
            redact_text_with_excludes(input, &excludes),
            input,
            "excluded match must stay verbatim"
        );
        // Without the exclude the same value IS redacted (digits → secret).
        assert!(redact_text(input).contains("[REDACTED"));
    }

    #[test]
    fn identifier_and_placeholder_heuristics() {
        assert!(is_identifier_reference("serverEnv.getStripeSecretKey"));
        assert!(is_identifier_reference("confirmRequiredEndpointKeySchema"));
        assert!(is_identifier_reference("$scope._private"));
        assert!(!is_identifier_reference("abc123"), "digits → secret-shaped");
        assert!(!is_identifier_reference("\"quoted\""), "literal value");
        assert!(!is_identifier_reference("a-b"), "dash is not identifier");
        assert!(is_placeholder_value("ghp_change_me"));
        assert!(is_placeholder_value("<token>"));
        assert!(is_placeholder_value("your_api_key_123"));
        assert!(!is_placeholder_value("A1b2C3d4E5f6G7h8"));
    }

    #[test]
    fn policy_patterns_redact_with_label_and_count() {
        let patterns = vec![(
            "employee_id".to_string(),
            regex::Regex::new(r"EMP-\d{4}").unwrap(),
        )];
        let (out, hits) = redact_with_patterns("user EMP-1234 and EMP-5678", &patterns);
        assert_eq!(hits, 2);
        assert!(!out.contains("EMP-1234"));
        assert!(out.contains("[REDACTED:employee_id]"));
    }

    #[test]
    fn policy_patterns_noop_when_no_match() {
        let patterns = vec![("iban".to_string(), regex::Regex::new(r"CH\d{2}").unwrap())];
        let (out, hits) = redact_with_patterns("nothing sensitive here", &patterns);
        assert_eq!(hits, 0);
        assert_eq!(out, "nothing sensitive here");
    }
}