refactor: move M_UNKNOWN_TOKEN recovery into client::build_and_restore

This commit is contained in:
damocles 2026-06-04 11:32:06 +02:00 committed by mara
commit 92ed989f8c
2 changed files with 34 additions and 41 deletions

View file

@ -60,7 +60,35 @@ pub async fn build_and_restore(
return Err(anyhow!("matrix token at {} is empty", token_file.display()));
}
let (user_id, device_id) = whoami(homeserver, &token).await?;
let (user_id, device_id) = match whoami(homeserver, &token).await {
Ok(ids) => ids,
Err(e) => {
let msg = format!("{e:#}");
if msg.contains("M_UNKNOWN_TOKEN") {
// Homeserver rejected our token — stale session after a homeserver
// state wipe or token expiry. Delete the token file (and the
// matrix-sdk sqlite state keyed to the now-invalid session) so
// hive-c0re's periodic `ensure_all` sweep re-provisions the account.
// Exit 0: systemd's Restart=on-failure must not loop us here; the
// systemd.paths watcher restarts us once the new token file appears.
tracing::warn!(
path = %token_file.display(),
"matrix token rejected (M_UNKNOWN_TOKEN); deleting stale token + \
sdk state for re-provisioning"
);
let _ = fs::remove_file(token_file).await;
if let Err(re) = fs::remove_dir_all(state_dir).await {
tracing::warn!(
path = %state_dir.display(),
err = %re,
"failed to remove sdk state dir; next startup may fail with stale state"
);
}
std::process::exit(0);
}
return Err(e);
}
};
fs::create_dir_all(state_dir)
.await