Skip to main content

agentmux_srv_core/backend/transcript/
withhold_sources.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The long-lived values srv keeps for the person's accounts and for itself, registered with [`withhold`] where srv
5//! reads or writes them, so none of them is written into an agent's transcript or shown to a paired device.
6//!
7//! - Every secret in the OS keychain (an account's token, the cloud sign-in, a peer's key): [`keychain`], from the
8//!   keychain's own read and write.
9//! - The cloud sign-in as srv assembles it (its tokens can be split across keychain entries): [`cloud_sign_in`].
10//! - An agent's relay credential, kept in the database: [`agent_relay_credential`].
11//! - A messaging bridge's token, read from settings: [`bridge_token`].
12//! - A key in the agent's own environment lines, put in its process as written: [`env_credential`].
13//! - The token that opens a container proxy relay: [`relay_token`].
14//!
15//! A value that carries its own expiry is withheld until then, even once a newer one replaces it; one with no known
16//! expiry stays withheld after another replaces it, up to `withhold::KEPT_PER_OWNER` per owner.
17
18use serde_json::Value;
19
20use super::withhold;
21
22/// Words in a field's name that say its value is a credential.
23const SECRET_WORDS: [&str; 9] = ["token", "key", "secret", "password", "passwd", "auth", "credential", "bearer", "cookie"];
24
25/// Withholds `value` for `owner`: until `expires_at` (unix ms) when it is known and still ahead, else as a kept value.
26fn hold(owner: &str, value: &str, expires_at: Option<i64>) {
27    if value.len() < withhold::MIN_LEN {
28        return;
29    }
30    match expires_at {
31        Some(at) if at > agentmux_common::time::now_ms() => withhold::hold_until(owner, value, at),
32        _ => withhold::hold_kept(owner, value),
33    }
34}
35
36fn secret_name(name: &str) -> bool {
37    let name = name.to_ascii_lowercase();
38    SECRET_WORDS.iter().any(|w| name.contains(w))
39}
40
41/// Withholds the credential-named string fields under `value`, each under an owner of its own.
42fn walk(owner: &str, value: &Value, in_secret: bool) -> bool {
43    match value {
44        Value::String(s) if in_secret && !s.is_empty() => {
45            hold(owner, s, None);
46            true
47        }
48        Value::Object(fields) => fields.iter().fold(false, |any, (name, inner)| {
49            walk(&format!("{owner}.{name}"), inner, in_secret || secret_name(name)) | any
50        }),
51        Value::Array(items) => items.iter().enumerate().fold(false, |any, (i, inner)| walk(&format!("{owner}.{i}"), inner, in_secret) | any),
52        _ => false,
53    }
54}
55
56/// A secret read from or written to the OS keychain under `account_id`. A JSON object (an OAuth account's tokens, a
57/// sign-in blob) is withheld field by field, for the fields whose names say they are credentials; anything else is
58/// withheld as it is.
59pub fn keychain(account_id: &str, secret: &str) {
60    let owner = format!("keychain:{account_id}");
61    let trimmed = secret.trim_start();
62    if trimmed.starts_with('{') {
63        if let Ok(json) = serde_json::from_str::<Value>(secret) {
64            if walk(&owner, &json, false) {
65                return;
66            }
67        }
68    }
69    hold(&owner, secret, None);
70}
71
72/// An API key account's key, read from or written to its Vault entry `entry`.
73pub fn account_key(entry: &str, key: &str) {
74    hold(&format!("account-key:{entry}"), key, None);
75}
76
77/// The cloud sign-in's tokens (`expires_at` in unix seconds, 0 when unknown). The access and id tokens are withheld
78/// until they expire, the refresh token until the sign-in ends.
79pub fn cloud_sign_in(access: &str, refresh: &str, id: &str, expires_at: i64) {
80    let until = (expires_at > 0).then(|| expires_at.saturating_mul(1000));
81    hold("cloud:access", access, until);
82    hold("cloud:id", id, until);
83    hold("cloud:refresh", refresh, None);
84}
85
86/// An agent's relay credential: its client secret, and the access token cached from it (`expires_at` in unix seconds).
87pub fn agent_relay_credential(agent_id: &str, client_secret: &str, access: &str, expires_at: i64) {
88    hold(&format!("relay-credential:{agent_id}:secret"), client_secret, None);
89    hold(&format!("relay-credential:{agent_id}:access"), access, (expires_at > 0).then(|| expires_at.saturating_mul(1000)));
90}
91
92/// A messaging bridge's token (`discord`, `telegram`, `slack:bot`, `whatsapp`).
93pub fn bridge_token(bridge: &str, token: &str) {
94    hold(&format!("bridge:{bridge}"), token, None);
95}
96
97/// One line of an agent's own environment, as it goes into its process: a literal value under a credential-style name.
98/// A Vault reference, an empty value and a placeholder word hold no secret and are left alone; a Vault value is
99/// withheld where it is resolved.
100pub fn env_credential(agent_id: &str, name: &str, value: &str) {
101    let value = value.trim();
102    if value.is_empty() || !crate::backend::agent_env::is_credential_name(name) {
103        return;
104    }
105    if crate::backend::agent_env::vault_ref(value).is_some() || crate::backend::agent_env::is_placeholder(value) {
106        return;
107    }
108    hold(&format!("env:{agent_id}:{}", name.to_ascii_uppercase()), value, None);
109}
110
111/// The token that opens a container proxy relay.
112pub fn relay_token(token: &str) {
113    hold("relay:container-proxy", token, None);
114}