~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

An assistant once connected can connect again

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/assistant-clients
crates/group-host/src/assistants.rs+7−2
@@ -121,9 +121,12 @@ impl Store {
now: i64,
) -> Result<()> {
// Registering is anyone's: one never connected goes after a day.
let stale = "DELETE FROM assistant_client WHERE created_at < ?1
AND id NOT IN (SELECT client_id FROM assistant_grant)";
// One a member connected stays, to connect again; its connections
// go once their refresh tokens lapse.
let stale = "DELETE FROM assistant_client WHERE created_at < ?1 AND connected = 0";
self.conn().execute(stale, [now - UNUSED_FOR])?;
let lapsed = "DELETE FROM assistant_grant WHERE expires_at <= ?1";
self.conn().execute(lapsed, [now])?;
let sql = "INSERT INTO assistant_client (id, name, redirect_uris, created_at) VALUES (?1, ?2, ?3, ?4)";
let redirects = serde_json::to_string(redirects)?;
self.conn()
@@ -223,6 +226,8 @@ impl Store {
now
];
self.conn().execute(sql, row)?;
let connected = "UPDATE assistant_client SET connected = 1 WHERE id = ?1";
self.conn().execute(connected, [&c.client_id])?;
Ok(())
}
crates/group-host/src/store.rs+5−0
@@ -394,6 +394,11 @@ pub const MIGRATIONS: &[&str] = &[
expires_at INTEGER NOT NULL,
started INTEGER NOT NULL
);
",
"
ALTER TABLE assistant_client ADD COLUMN connected INTEGER NOT NULL DEFAULT 0;
UPDATE assistant_client SET connected = 1
WHERE id IN (SELECT client_id FROM assistant_grant);
",
];
crates/group-host/tests/assistants.rs+45−0
@@ -715,3 +715,48 @@ async fn forgets_a_client_that_never_connected() {
assert!(store.registered_assistant("old").unwrap().is_none());
assert!(store.registered_assistant("new").unwrap().is_some());
}
#[tokio::test]
async fn keeps_a_client_once_connected_and_forgets_a_lapsed_connection() {
use group_host::assistants::Connected;
let h = start(&[], false).await;
let [alice, ..] = people(&h).await;
let group = start_group(
&h,
&alice,
json!({"handle": RIDERS, "displayName": "Riders"}),
)
.await;
let store = &h.host.store;
let day = 24 * 60 * 60_000;
let now = 100 * day;
let back = vec![BACK.to_string()];
let connect = |id: &str, client: &str, at: i64| {
let c = Connected {
id: id.into(),
group: group.clone(),
member: alice.did.clone(),
client_id: client.into(),
client_name: "Kept".into(),
created_at: at,
used_at: at,
};
store
.connect_assistant(&c, &format!("a-{id}"), &format!("r-{id}"), at)
.unwrap();
};
// Connected, then disconnected: kept, to connect again.
store
.register_assistant("kept", "Kept", &back, now - 2 * day)
.unwrap();
connect("one", "kept", now - 2 * day);
store
.disconnect_assistant(&group, &alice.did, "kept")
.unwrap();
// A connection whose refresh token lapsed long ago.
connect("two", "lapsed", now - 40 * day);
store.register_assistant("new", "New", &back, now).unwrap();
assert!(store.registered_assistant("kept").unwrap().is_some());
let lapsed = store.disconnect_assistant(&group, &alice.did, "lapsed");
assert_eq!(lapsed.unwrap(), 0);
}
docs/design/assistants.md+3−2
@@ -40,8 +40,9 @@ An assistant names itself in one of two ways. It can give the https URL
of a client metadata document, a JSON file that describes it; the host
fetches that document at each sign-in and stores nothing. Or it can
register at `/mcp/register` as a public client, with no secret;
registration is open, and a registered client with no connection is
forgotten after a day.
registration is open, and a registered client never connected is
forgotten after a day. One a member has connected is kept, to connect
again, and its connections are forgotten once they lapse.
The member approves on the console. The assistant sends the member's
browser to `/mcp/authorize`, and the host passes them to the console's