Skip to main content

agentmux_srv_core/backend/vault/
slots.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Which parts of which entries an agent may have filled into a web form, and the
5//! kind of form field each belongs in
6//! (docs/specs/SPEC_VAULT_PANE_AGENTS_USE_SECRETS_UNSEEN_2026_10_19.md §6.1, §6.3).
7//!
8//! The slot names are the ones the host's `vault_slots.rs` gives a field it
9//! recognises; a field fitting the entry's slot is what lets "Always on this
10//! site" be offered and used.
11
12use super::kinds::{self, Kind};
13
14/// What `fill` with `vault` names: an entry, and one of its parts.
15#[derive(Debug, PartialEq, Eq)]
16pub struct Target<'a> {
17    pub entry_id: &'a str,
18    pub part: &'static str,
19}
20
21/// Splits `"id"` or `"id.part"`; an id has no `.`.
22pub fn split(vault: &str) -> (&str, Option<&str>) {
23    match vault.trim().split_once('.') {
24        Some((id, part)) => (id, Some(part)),
25        None => (vault.trim(), None),
26    }
27}
28
29/// The part to fill (the named one, else the entry's main one), or why not.
30/// Passwords and one-time codes are never filled (spec §13 question 2).
31pub fn fillable_part(kind: &'static Kind, part: Option<&str>) -> Result<&'static str, String> {
32    if kind.name == "login" {
33        return Err("a login's password and one-time code aren't filled from the Vault: the person types them (agents never fill a password)".into());
34    }
35    let wanted = part.unwrap_or(kind.parts[0]);
36    kind.parts
37        .iter()
38        .find(|p| **p == wanted)
39        .copied()
40        .ok_or_else(|| format!("a {} entry has no part {wanted:?}; its parts are {}", kind.name, kind.parts.join(", ")))
41}
42
43/// The kind of form field this part belongs in, or `None` when no field can be
44/// known to fit it (an ID of another sort, a note), so the person is always asked.
45pub fn slot(kind: &Kind, part: &str) -> Option<&'static str> {
46    Some(match (kind.name, part) {
47        ("ssn" | "itin" | "tin", _) => "tax-id",
48        ("ein", _) => "ein",
49        ("date-of-birth", _) => "date-of-birth",
50        ("passport", _) => "passport",
51        ("drivers-licence", _) => "drivers-licence",
52        ("card", "number") => "card-number",
53        ("card", "expiry") => "card-expiry",
54        ("card", "exp_month") => "card-exp-month",
55        ("card", "exp_year") => "card-exp-year",
56        ("card", "cvc") => "card-cvc",
57        ("card", "name") => "card-name",
58        ("card", "zip") => "card-zip",
59        ("bank-account", "account") => "bank-account",
60        ("bank-account", "routing") => "bank-routing",
61        ("api-key", _) => "api-key",
62        _ => return None,
63    })
64}
65
66/// What a part is called to the person.
67pub fn what(kind: &Kind, part: &str) -> String {
68    let kind_label = match kind.name {
69        "ssn" => "Social Security number",
70        "itin" => "ITIN",
71        "tin" => "taxpayer ID",
72        "ein" => "EIN",
73        "date-of-birth" => "date of birth",
74        "passport" => "passport number",
75        "drivers-licence" => "driver's licence number",
76        "other-id" => "ID number",
77        "api-key" => "API key",
78        "note" => "secret note",
79        other => other,
80    };
81    match (kind.name, part) {
82        ("card", p) => format!(
83            "card {}",
84            match p {
85                "number" => "number",
86                "expiry" => "expiry",
87                "exp_month" => "expiry month",
88                "exp_year" => "expiry year",
89                "cvc" => "security code",
90                "zip" => "billing ZIP",
91                "name" => "holder name",
92                other => other,
93            }
94        ),
95        ("bank-account", "routing") => "bank routing number".to_string(),
96        ("bank-account", _) => "bank account number".to_string(),
97        _ => kind_label.to_string(),
98    }
99}
100
101/// Every kind, for the tests.
102#[cfg(test)]
103pub fn all_kinds() -> &'static [Kind] {
104    kinds::KINDS
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn an_id_and_an_optional_part_are_split() {
113        assert_eq!(split("ssn-alex"), ("ssn-alex", None));
114        assert_eq!(split(" card-company.number "), ("card-company", Some("number")));
115    }
116
117    #[test]
118    fn a_login_is_never_filled_and_a_part_must_exist() {
119        let login = kinds::kind("login").unwrap();
120        assert!(fillable_part(login, None).unwrap_err().contains("password"));
121        assert!(fillable_part(login, Some("totp")).is_err());
122        let card = kinds::kind("card").unwrap();
123        assert_eq!(fillable_part(card, None), Ok("number"));
124        assert_eq!(fillable_part(card, Some("cvc")), Ok("cvc"));
125        assert!(fillable_part(card, Some("pin")).unwrap_err().contains("its parts are"));
126        assert_eq!(fillable_part(kinds::kind("ssn").unwrap(), None), Ok("value"));
127    }
128
129    #[test]
130    fn every_part_of_a_card_is_named_for_what_it_is() {
131        let card = kinds::kind("card").unwrap();
132        let named: Vec<(&str, String)> = card.parts.iter().map(|p| (*p, what(card, p))).collect();
133        assert_eq!(
134            named,
135            [
136                ("number", "card number".to_string()),
137                ("exp_month", "card expiry month".to_string()),
138                ("exp_year", "card expiry year".to_string()),
139                ("expiry", "card expiry".to_string()),
140                ("cvc", "card security code".to_string()),
141                ("name", "card holder name".to_string()),
142                ("zip", "card billing ZIP".to_string()),
143            ]
144        );
145    }
146
147    #[test]
148    fn every_fillable_part_is_named_and_most_have_a_slot() {
149        for k in all_kinds().iter().filter(|k| k.name != "login") {
150            for p in k.parts {
151                assert!(!what(k, p).is_empty());
152            }
153        }
154        let card = kinds::kind("card").unwrap();
155        assert_eq!(slot(card, "number"), Some("card-number"));
156        assert_eq!(slot(card, "cvc"), Some("card-cvc"));
157        assert_eq!(slot(kinds::kind("ssn").unwrap(), "value"), Some("tax-id"));
158        assert_eq!(slot(kinds::kind("itin").unwrap(), "value"), Some("tax-id"));
159        assert_eq!(slot(kinds::kind("other-id").unwrap(), "value"), None, "an ID of another sort always asks");
160        assert_eq!(slot(kinds::kind("note").unwrap(), "text"), None);
161    }
162
163    #[test]
164    fn the_slots_are_the_ones_the_host_names() {
165        // crates/cef/src/browser_api/vault_slots.rs is where a field gets its slots.
166        let host = include_str!("../../../../cef/src/browser_api/vault_slots.rs");
167        for k in all_kinds().iter().filter(|k| k.name != "login") {
168            for p in k.parts {
169                if let Some(s) = slot(k, p) {
170                    assert!(host.contains(&format!("= \"{s}\";")), "the host doesn't know the slot {s}");
171                }
172            }
173        }
174    }
175}