Skip to main content

agentmux_srv_core/backend/vault/
agent_set.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A value an agent had the Vault read is not trusted until the person has seen it
5//! (docs/specs/SPEC_VAULT_AGENT_ADDS_AND_EDITS_WITHOUT_ASKING_2026_11_25.md R11).
6//!
7//! An `import` reads with the host's own AWS setup and a `capture` reads a page or a command's output, and neither asks the
8//! person first. So the entry they write is marked unseen: a *use* of it never goes ahead on silence and offers no *Always*,
9//! and the card says who set the value and when. The mark goes when the person marks the change seen, or answers a use of
10//! the entry with a yes. Marking also drops the entry's *Always* grants, which skip the card on a use. A value the Vault made (`generate`, `rotate`) is not marked: nobody had it before.
11//!
12//! A site, realm or proxy an agent chose for a login is marked the same way (R9): a use at a place the person never saw is
13//! the way a password reaches a site an agent picked.
14
15use agentmux_common::time::now_ms;
16
17use super::uses::UseRequest;
18use crate::backend::storage::store::Store;
19use crate::backend::storage::vault_changes::{SITE, VALUE};
20
21/// Marks the value of `entry_id` as set by `agent` and not yet seen by the person. A failure to mark is logged: the write
22/// it follows is already made, and the history line records it.
23pub fn mark(store: &Store, entry_id: &str, agent: &str) {
24    mark_what(store, entry_id, agent, VALUE);
25}
26
27/// [`mark`] for a site, realm or proxy an agent set on the login.
28pub fn mark_site(store: &Store, entry_id: &str, agent: &str) {
29    mark_what(store, entry_id, agent, SITE);
30}
31
32fn mark_what(store: &Store, entry_id: &str, agent: &str, what: &str) {
33    let marked = store.vault_get(entry_id).ok().flatten().map(|row| store.vault_unseen_mark(entry_id, row.created_at, agent, now_ms(), what));
34    if !matches!(marked, Some(Ok(()))) {
35        tracing::warn!(entry = entry_id, "vault: couldn't mark an agent's value as unseen");
36    }
37}
38
39/// The person has seen the entry's value, or answered a use of it: it is trusted as any other.
40pub fn clear(store: &Store, entry_id: &str) -> bool {
41    store.vault_unseen_clear(entry_id).unwrap_or(false)
42}
43
44/// Whether the entry's value is one an agent set and the person has not seen.
45pub fn is_unseen(store: &Store, entry_id: &str) -> bool {
46    matches!(store.vault_unseen_get(entry_id), Ok(Some(_)))
47}
48
49/// Tells `request` that its entry's value was set by an agent and not seen, when it was: who and when. A use of it then never
50/// goes ahead on silence and offers no *Always*. Leaves a use of an entry the person has seen as it is.
51pub fn apply(store: &Store, request: &mut UseRequest) {
52    if request.entry_id.is_empty() {
53        return;
54    }
55    if let Ok(Some(unseen)) = store.vault_unseen_get(&request.entry_id) {
56        request.set_by = unseen.by_agent;
57        request.set_ms = u64::try_from(unseen.at).unwrap_or(0);
58        request.set_what = unseen.what;
59        request.offer_always = false;
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use crate::backend::vault::uses::goes_ahead;
67
68    fn store() -> Store {
69        crate::backend::vault::seal::use_test_key();
70        let store = Store::open_in_memory().unwrap();
71        store.apply_identity_schema_for_tests().unwrap();
72        store
73    }
74
75    fn entry(store: &Store, id: &str) {
76        let entry: crate::backend::vault::NewEntry = serde_json::from_value(serde_json::json!({ "kind": "api-key", "label": "Mail key", "parts": { "value": "sk_test_invented_value" } })).unwrap();
77        assert_eq!(crate::backend::vault::create(store, entry).unwrap(), id);
78    }
79
80    fn use_of(id: &str) -> UseRequest {
81        UseRequest { entry_id: id.into(), kind: "api-key".into(), offer_always: true, ..Default::default() }
82    }
83
84    #[test]
85    fn an_unseen_value_never_goes_ahead_on_silence_and_offers_no_always_until_it_is_seen() {
86        let store = store();
87        entry(&store, "api-key-mail-key");
88        let mut plain = use_of("api-key-mail-key");
89        apply(&store, &mut plain);
90        assert!(goes_ahead(&plain) && plain.offer_always && plain.set_by.is_empty(), "a value nobody marked is used as before");
91        mark(&store, "api-key-mail-key", "builder");
92        let mut unseen = use_of("api-key-mail-key");
93        apply(&store, &mut unseen);
94        assert_eq!(unseen.set_by, "builder");
95        assert!(unseen.set_ms > 0 && !unseen.offer_always && !goes_ahead(&unseen), "silence is a no, and there is no Always");
96        assert!(clear(&store, "api-key-mail-key"));
97        let mut seen = use_of("api-key-mail-key");
98        apply(&store, &mut seen);
99        assert!(goes_ahead(&seen), "seen, it is used as any other");
100        assert!(!clear(&store, "api-key-mail-key"), "nothing left to clear");
101    }
102
103    #[test]
104    fn marking_a_value_unseen_drops_the_always_grants_given_for_the_value_the_person_saw() {
105        let store = store();
106        entry(&store, "api-key-mail-key");
107        let created = store.vault_get("api-key-mail-key").unwrap().unwrap().created_at;
108        store.vault_grant_add("api-key-mail-key", "https://mail.example", 1, created).unwrap();
109        assert!(store.vault_grant_check("api-key-mail-key", "https://mail.example").unwrap());
110        mark(&store, "api-key-mail-key", "builder");
111        assert!(!store.vault_grant_check("api-key-mail-key", "https://mail.example").unwrap(), "a use on a grant skips the card, so the grant goes");
112    }
113
114    #[test]
115    fn a_mark_belongs_to_the_entry_it_was_made_on_and_not_to_one_made_again_under_its_id() {
116        let store = store();
117        entry(&store, "api-key-mail-key");
118        mark(&store, "api-key-mail-key", "builder");
119        store.vault_delete("api-key-mail-key").unwrap();
120        std::thread::sleep(std::time::Duration::from_millis(3));
121        entry(&store, "api-key-mail-key");
122        let mut again = use_of("api-key-mail-key");
123        apply(&store, &mut again);
124        assert!(again.set_by.is_empty(), "the new entry was made by the person, not by that agent");
125    }
126}