automation: a settings change reads the agent token file again (photocraft#17006)

The token a failed start checked was kept for its retries, but also
across a settings change: after a taken port, agents off and on again
reused it without reading the token file, so a file changed in between
left the server on a token clients no longer find there. Only a retry
with the same settings reuses it now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David Mann
2026-11-26 01:08:43 +01:00
co-authored by Claude Opus 5.5
parent e1fcf7e17a
commit 54c74e91e6
2 changed files with 31 additions and 3 deletions
+9 -3
View File
@@ -123,8 +123,8 @@ pub struct AgentLink {
applied: Option<Option<AgentSettings>>,
retry_at: Option<Instant>,
retry: Duration,
/// The token a failed start already read and checked, for its retries; cleared once a start
/// succeeds. On Windows the check starts `whoami` and PowerShell (`owner_only`), about 300 ms
/// The token a failed start already read and checked, for its retries with the same settings;
/// cleared once a start succeeds or the settings change. On Windows the check starts `whoami` and PowerShell (`owner_only`), about 300 ms
/// on the UI thread, which every retry of a taken port would otherwise pay again.
checked_token: Option<String>,
status: AgentLinkStatus,
@@ -184,9 +184,15 @@ impl AgentLink {
}
let now = motion::now();
let retry_due = self.retry_at.is_some_and(|at| now >= at);
if self.applied.as_ref().is_some_and(|applied| applied.as_ref() == want) && !retry_due {
let unchanged = self.applied.as_ref().is_some_and(|applied| applied.as_ref() == want);
if unchanged && !retry_due {
return update;
}
// Only a retry with the same settings reuses the token its failed start checked: after a
// change (agents off and on again, another port) the file may hold another token by now.
if !unchanged {
self.checked_token = None;
}
let before = (self.status.clone(), self.applied.clone().flatten());
self.applied = Some(want.cloned());
self.retry_at = None;
+22
View File
@@ -376,6 +376,28 @@ fn a_taken_port_retry_reuses_the_checked_token() {
assert!(matches!(link.status().problem, Some(LinkProblem::TokenFile(_))), "{:?}", link.status());
}
/// The checked token is kept only for retries with the same settings: turning agents off and on again reads the token
/// file again, so a server never listens with a token the file no longer holds.
#[trial]
fn a_settings_change_reads_the_token_file_again() {
let dir = temp_dir("link-token-settings");
let host = host();
let mut link = AgentLink::new(Some(dir.path().to_path_buf()), "x").retry_after(Duration::ZERO);
let taken = TcpListener::bind("127.0.0.1:0").unwrap();
let port = u32::from(taken.local_addr().unwrap().port());
assert!(link.sync(Some(&on(port)), &host).requests.is_none());
assert!(matches!(link.status().problem, Some(LinkProblem::Listen { .. })), "{:?}", link.status());
assert!(!link.sync(None, &host).stopped, "nothing was running");
// While agents are off the token file changes; here it can't be read at all, which the next start must report.
std::fs::remove_file(dir.path().join(TOKEN_FILE)).unwrap();
std::fs::create_dir(dir.path().join(TOKEN_FILE)).unwrap();
drop(taken);
let back = link.sync(Some(&on(port)), &host);
assert!(back.requests.is_none(), "started with the token the file no longer holds");
assert!(matches!(link.status().problem, Some(LinkProblem::TokenFile(_))), "{:?}", link.status());
}
/// Dropping the link stops its server and removes its connection file.
#[trial]
fn dropping_the_link_removes_its_connection_file() {