style(server): cargo fmt for SPEC-004 Task 2 + Task 4
All checks were successful
All checks were successful
Pure rustfmt reflow of the Task 2 (machine_uid dedup) and Task 4 (session reaping) code; no logic change. The CI Build-Server-Linux job gates on cargo fmt --check, which the two feature commits failed because local validation ran check/clippy/test but not fmt --check. fmt --check, check, and clippy -D warnings all clean now. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -399,7 +399,10 @@ mod tests {
|
|||||||
let m1 = upsert_machine(&pool, agent, "LEGACY-HOST", true, None)
|
let m1 = upsert_machine(&pool, agent, "LEGACY-HOST", true, None)
|
||||||
.await
|
.await
|
||||||
.expect("legacy upsert (no uid)");
|
.expect("legacy upsert (no uid)");
|
||||||
assert_eq!(m1.machine_uid, None, "legacy row must have NULL machine_uid");
|
assert_eq!(
|
||||||
|
m1.machine_uid, None,
|
||||||
|
"legacy row must have NULL machine_uid"
|
||||||
|
);
|
||||||
|
|
||||||
let m2 = upsert_machine(&pool, agent, "LEGACY-HOST-RENAMED", true, None)
|
let m2 = upsert_machine(&pool, agent, "LEGACY-HOST-RENAMED", true, None)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -283,11 +283,7 @@ async fn main() -> Result<()> {
|
|||||||
machine.machine_uid.as_deref()
|
machine.machine_uid.as_deref()
|
||||||
};
|
};
|
||||||
sessions
|
sessions
|
||||||
.restore_offline_machine(
|
.restore_offline_machine(&machine.agent_id, &machine.hostname, restore_uid)
|
||||||
&machine.agent_id,
|
|
||||||
&machine.hostname,
|
|
||||||
restore_uid,
|
|
||||||
)
|
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -277,9 +277,9 @@ impl SessionManager {
|
|||||||
session_data.info.is_online = true;
|
session_data.info.is_online = true;
|
||||||
session_data.info.last_heartbeat = chrono::Utc::now();
|
session_data.info.last_heartbeat = chrono::Utc::now();
|
||||||
session_data.info.agent_name = agent_name; // Update name in case it changed
|
session_data.info.agent_name = agent_name; // Update name in case it changed
|
||||||
// Record the reported uid on the session so the reaper can log it
|
// Record the reported uid on the session so the reaper can log it
|
||||||
// and supersede can match it later. Only set it from a reported
|
// and supersede can match it later. Only set it from a reported
|
||||||
// uid; never clear an existing one on a None (legacy) reconnect.
|
// uid; never clear an existing one on a None (legacy) reconnect.
|
||||||
if let Some(uid) = machine_uid {
|
if let Some(uid) = machine_uid {
|
||||||
session_data.info.machine_uid = Some(uid.to_string());
|
session_data.info.machine_uid = Some(uid.to_string());
|
||||||
}
|
}
|
||||||
@@ -1111,7 +1111,12 @@ mod tests {
|
|||||||
|
|
||||||
// Attended (support-code) session: is_persistent = false.
|
// Attended (support-code) session: is_persistent = false.
|
||||||
let (session_id, _frame_tx, _input_rx) = mgr
|
let (session_id, _frame_tx, _input_rx) = mgr
|
||||||
.register_agent("agent-att".to_string(), "Attended PC".to_string(), false, None)
|
.register_agent(
|
||||||
|
"agent-att".to_string(),
|
||||||
|
"Attended PC".to_string(),
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Starts Pending.
|
// Starts Pending.
|
||||||
@@ -1143,7 +1148,12 @@ mod tests {
|
|||||||
|
|
||||||
// Managed/persistent session: is_persistent = true.
|
// Managed/persistent session: is_persistent = true.
|
||||||
let (session_id, _frame_tx, _input_rx) = mgr
|
let (session_id, _frame_tx, _input_rx) = mgr
|
||||||
.register_agent("agent-mgd".to_string(), "Managed PC".to_string(), true, None)
|
.register_agent(
|
||||||
|
"agent-mgd".to_string(),
|
||||||
|
"Managed PC".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -1294,7 +1304,12 @@ mod tests {
|
|||||||
|
|
||||||
// First connect: agent_id #1, persistent (managed).
|
// First connect: agent_id #1, persistent (managed).
|
||||||
let (sid1, _f1, _i1) = mgr
|
let (sid1, _f1, _i1) = mgr
|
||||||
.register_agent("agent-uuid-1".to_string(), "HOST-A".to_string(), true, Some(uid))
|
.register_agent(
|
||||||
|
"agent-uuid-1".to_string(),
|
||||||
|
"HOST-A".to_string(),
|
||||||
|
true,
|
||||||
|
Some(uid),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Agent disconnects; persistent session is preserved offline.
|
// Agent disconnects; persistent session is preserved offline.
|
||||||
@@ -1307,15 +1322,27 @@ mod tests {
|
|||||||
|
|
||||||
// Reconnect with a FRESH agent_id but the SAME machine_uid (config loss).
|
// Reconnect with a FRESH agent_id but the SAME machine_uid (config loss).
|
||||||
let (sid2, _f2, _i2) = mgr
|
let (sid2, _f2, _i2) = mgr
|
||||||
.register_agent("agent-uuid-2".to_string(), "HOST-A".to_string(), true, Some(uid))
|
.register_agent(
|
||||||
|
"agent-uuid-2".to_string(),
|
||||||
|
"HOST-A".to_string(),
|
||||||
|
true,
|
||||||
|
Some(uid),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Reattached to the SAME session, now back online.
|
// Reattached to the SAME session, now back online.
|
||||||
assert_eq!(sid2, sid1, "same machine_uid must reattach the existing session");
|
assert_eq!(
|
||||||
|
sid2, sid1,
|
||||||
|
"same machine_uid must reattach the existing session"
|
||||||
|
);
|
||||||
assert_eq!(mgr.get_session(sid1).await.map(|s| s.is_online), Some(true));
|
assert_eq!(mgr.get_session(sid1).await.map(|s| s.is_online), Some(true));
|
||||||
|
|
||||||
// Exactly ONE session total — no duplicate was created.
|
// Exactly ONE session total — no duplicate was created.
|
||||||
assert_eq!(mgr.list_sessions().await.len(), 1, "must not duplicate the session");
|
assert_eq!(
|
||||||
|
mgr.list_sessions().await.len(),
|
||||||
|
1,
|
||||||
|
"must not duplicate the session"
|
||||||
|
);
|
||||||
|
|
||||||
// The session's agent_id was repointed to the current one, and the agent_id
|
// The session's agent_id was repointed to the current one, and the agent_id
|
||||||
// index resolves the new id (and no longer the stale one).
|
// index resolves the new id (and no longer the stale one).
|
||||||
@@ -1368,7 +1395,12 @@ mod tests {
|
|||||||
|
|
||||||
// Keyed agent: relay passes None for machine_uid.
|
// Keyed agent: relay passes None for machine_uid.
|
||||||
let (sid1, _f1, _i1) = mgr
|
let (sid1, _f1, _i1) = mgr
|
||||||
.register_agent("keyed-machine".to_string(), "HOST-K".to_string(), true, None)
|
.register_agent(
|
||||||
|
"keyed-machine".to_string(),
|
||||||
|
"HOST-K".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
mgr.mark_agent_disconnected(sid1).await;
|
mgr.mark_agent_disconnected(sid1).await;
|
||||||
|
|
||||||
@@ -1391,9 +1423,17 @@ mod tests {
|
|||||||
// The keyed agent reconnects (relay again passes None) and reattaches its OWN
|
// The keyed agent reconnects (relay again passes None) and reattaches its OWN
|
||||||
// session by agent_id.
|
// session by agent_id.
|
||||||
let (sid3, _f3, _i3) = mgr
|
let (sid3, _f3, _i3) = mgr
|
||||||
.register_agent("keyed-machine".to_string(), "HOST-K".to_string(), true, None)
|
.register_agent(
|
||||||
|
"keyed-machine".to_string(),
|
||||||
|
"HOST-K".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(sid3, sid1, "keyed agent reattaches on its authoritative agent_id");
|
assert_eq!(
|
||||||
|
sid3, sid1,
|
||||||
|
"keyed agent reattaches on its authoritative agent_id"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removing a session purges its machine_uid index entry so a later reconnect
|
/// Removing a session purges its machine_uid index entry so a later reconnect
|
||||||
@@ -1404,14 +1444,24 @@ mod tests {
|
|||||||
let uid = "muid-purge";
|
let uid = "muid-purge";
|
||||||
|
|
||||||
let (sid1, _f1, _i1) = mgr
|
let (sid1, _f1, _i1) = mgr
|
||||||
.register_agent("purge-agent".to_string(), "HOST-P".to_string(), true, Some(uid))
|
.register_agent(
|
||||||
|
"purge-agent".to_string(),
|
||||||
|
"HOST-P".to_string(),
|
||||||
|
true,
|
||||||
|
Some(uid),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
mgr.remove_session(sid1).await;
|
mgr.remove_session(sid1).await;
|
||||||
assert!(mgr.get_session(sid1).await.is_none());
|
assert!(mgr.get_session(sid1).await.is_none());
|
||||||
|
|
||||||
// Reconnect with the same uid -> a brand-new session (no stale reattach).
|
// Reconnect with the same uid -> a brand-new session (no stale reattach).
|
||||||
let (sid2, _f2, _i2) = mgr
|
let (sid2, _f2, _i2) = mgr
|
||||||
.register_agent("purge-agent-2".to_string(), "HOST-P".to_string(), true, Some(uid))
|
.register_agent(
|
||||||
|
"purge-agent-2".to_string(),
|
||||||
|
"HOST-P".to_string(),
|
||||||
|
true,
|
||||||
|
Some(uid),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_ne!(sid2, sid1, "removed session's uid must not reattach");
|
assert_ne!(sid2, sid1, "removed session's uid must not reattach");
|
||||||
assert_eq!(mgr.list_sessions().await.len(), 1);
|
assert_eq!(mgr.list_sessions().await.len(), 1);
|
||||||
@@ -1432,7 +1482,12 @@ mod tests {
|
|||||||
|
|
||||||
// Agent reconnects post-restart with a FRESH agent_id but the same uid.
|
// Agent reconnects post-restart with a FRESH agent_id but the same uid.
|
||||||
let (sid2, _f2, _i2) = mgr
|
let (sid2, _f2, _i2) = mgr
|
||||||
.register_agent("fresh-agent".to_string(), "HOST-R".to_string(), true, Some(uid))
|
.register_agent(
|
||||||
|
"fresh-agent".to_string(),
|
||||||
|
"HOST-R".to_string(),
|
||||||
|
true,
|
||||||
|
Some(uid),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(sid2, sid, "restored machine must reattach by machine_uid");
|
assert_eq!(sid2, sid, "restored machine must reattach by machine_uid");
|
||||||
assert_eq!(mgr.list_sessions().await.len(), 1);
|
assert_eq!(mgr.list_sessions().await.len(), 1);
|
||||||
@@ -1448,15 +1503,23 @@ mod tests {
|
|||||||
let ttl = std::time::Duration::from_secs(600);
|
let ttl = std::time::Duration::from_secs(600);
|
||||||
|
|
||||||
let (sid, _f, _i) = mgr
|
let (sid, _f, _i) = mgr
|
||||||
.register_agent("reap-agent".to_string(), "HOST-REAP".to_string(), true, None)
|
.register_agent(
|
||||||
|
"reap-agent".to_string(),
|
||||||
|
"HOST-REAP".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
mgr.mark_agent_disconnected(sid).await; // now offline
|
mgr.mark_agent_disconnected(sid).await; // now offline
|
||||||
// Age it well past the TTL.
|
// Age it well past the TTL.
|
||||||
mgr.set_last_heartbeat_age(sid, std::time::Duration::from_secs(1200))
|
mgr.set_last_heartbeat_age(sid, std::time::Duration::from_secs(1200))
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let reaped = mgr.reap_stale_persistent(ttl).await;
|
let reaped = mgr.reap_stale_persistent(ttl).await;
|
||||||
assert_eq!(reaped, 1, "a stale offline persistent session must be reaped");
|
assert_eq!(
|
||||||
|
reaped, 1,
|
||||||
|
"a stale offline persistent session must be reaped"
|
||||||
|
);
|
||||||
assert!(mgr.get_session(sid).await.is_none());
|
assert!(mgr.get_session(sid).await.is_none());
|
||||||
assert_eq!(mgr.list_sessions().await.len(), 0);
|
assert_eq!(mgr.list_sessions().await.len(), 0);
|
||||||
// Index purged too: the agent_id no longer resolves.
|
// Index purged too: the agent_id no longer resolves.
|
||||||
@@ -1470,7 +1533,12 @@ mod tests {
|
|||||||
let ttl = std::time::Duration::from_secs(600);
|
let ttl = std::time::Duration::from_secs(600);
|
||||||
|
|
||||||
let (sid, _f, _i) = mgr
|
let (sid, _f, _i) = mgr
|
||||||
.register_agent("fresh-agent".to_string(), "HOST-FRESH".to_string(), true, None)
|
.register_agent(
|
||||||
|
"fresh-agent".to_string(),
|
||||||
|
"HOST-FRESH".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
mgr.mark_agent_disconnected(sid).await;
|
mgr.mark_agent_disconnected(sid).await;
|
||||||
// Aged, but not past the TTL.
|
// Aged, but not past the TTL.
|
||||||
@@ -1490,7 +1558,12 @@ mod tests {
|
|||||||
let ttl = std::time::Duration::from_secs(600);
|
let ttl = std::time::Duration::from_secs(600);
|
||||||
|
|
||||||
let (sid, _f, _i) = mgr
|
let (sid, _f, _i) = mgr
|
||||||
.register_agent("online-agent".to_string(), "HOST-ON".to_string(), true, None)
|
.register_agent(
|
||||||
|
"online-agent".to_string(),
|
||||||
|
"HOST-ON".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
// Still online (no disconnect). Backdate anyway to prove online wins.
|
// Still online (no disconnect). Backdate anyway to prove online wins.
|
||||||
mgr.set_last_heartbeat_age(sid, std::time::Duration::from_secs(99999))
|
mgr.set_last_heartbeat_age(sid, std::time::Duration::from_secs(99999))
|
||||||
@@ -1540,7 +1613,12 @@ mod tests {
|
|||||||
|
|
||||||
// Support (attended) session: is_persistent = false.
|
// Support (attended) session: is_persistent = false.
|
||||||
let (sid, _f, _i) = mgr
|
let (sid, _f, _i) = mgr
|
||||||
.register_agent("support-agent".to_string(), "HOST-S".to_string(), false, None)
|
.register_agent(
|
||||||
|
"support-agent".to_string(),
|
||||||
|
"HOST-S".to_string(),
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
// Force offline + stale (a support session that lingered, hypothetically).
|
// Force offline + stale (a support session that lingered, hypothetically).
|
||||||
mgr.force_offline_for_test(sid).await;
|
mgr.force_offline_for_test(sid).await;
|
||||||
@@ -1548,7 +1626,10 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let reaped = mgr.reap_stale_persistent(ttl).await;
|
let reaped = mgr.reap_stale_persistent(ttl).await;
|
||||||
assert_eq!(reaped, 0, "the persistent reaper must never reap a support session");
|
assert_eq!(
|
||||||
|
reaped, 0,
|
||||||
|
"the persistent reaper must never reap a support session"
|
||||||
|
);
|
||||||
assert!(mgr.get_session(sid).await.is_some());
|
assert!(mgr.get_session(sid).await.is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1577,10 +1658,18 @@ mod tests {
|
|||||||
// reattach lookups miss (uid was never indexed; agent_id differs), so a NEW
|
// reattach lookups miss (uid was never indexed; agent_id differs), so a NEW
|
||||||
// session is created — and supersede must remove the stranded old one.
|
// session is created — and supersede must remove the stranded old one.
|
||||||
let (sid_new, _f2, _i2) = mgr
|
let (sid_new, _f2, _i2) = mgr
|
||||||
.register_agent("fresh-id".to_string(), "HOST-SUP".to_string(), true, Some(uid))
|
.register_agent(
|
||||||
|
"fresh-id".to_string(),
|
||||||
|
"HOST-SUP".to_string(),
|
||||||
|
true,
|
||||||
|
Some(uid),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
assert_ne!(sid_new, sid_old, "a fresh agent_id with an unindexed uid creates a new session");
|
assert_ne!(
|
||||||
|
sid_new, sid_old,
|
||||||
|
"a fresh agent_id with an unindexed uid creates a new session"
|
||||||
|
);
|
||||||
// The stranded old session is gone; exactly one live session remains.
|
// The stranded old session is gone; exactly one live session remains.
|
||||||
assert!(
|
assert!(
|
||||||
mgr.get_session(sid_old).await.is_none(),
|
mgr.get_session(sid_old).await.is_none(),
|
||||||
@@ -1608,7 +1697,12 @@ mod tests {
|
|||||||
let ttl = std::time::Duration::from_secs(600);
|
let ttl = std::time::Duration::from_secs(600);
|
||||||
|
|
||||||
let (sid, _f, _i) = mgr
|
let (sid, _f, _i) = mgr
|
||||||
.register_agent("race-agent".to_string(), "HOST-RACE".to_string(), true, None)
|
.register_agent(
|
||||||
|
"race-agent".to_string(),
|
||||||
|
"HOST-RACE".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
// Make it a valid reap candidate first: offline + stale.
|
// Make it a valid reap candidate first: offline + stale.
|
||||||
mgr.mark_agent_disconnected(sid).await;
|
mgr.mark_agent_disconnected(sid).await;
|
||||||
@@ -1618,7 +1712,12 @@ mod tests {
|
|||||||
// Simulate the agent reconnecting in the snapshot->remove window: it is now
|
// Simulate the agent reconnecting in the snapshot->remove window: it is now
|
||||||
// ONLINE again (register_agent's reattach marks the session online).
|
// ONLINE again (register_agent's reattach marks the session online).
|
||||||
let (sid_re, _f2, _i2) = mgr
|
let (sid_re, _f2, _i2) = mgr
|
||||||
.register_agent("race-agent".to_string(), "HOST-RACE".to_string(), true, None)
|
.register_agent(
|
||||||
|
"race-agent".to_string(),
|
||||||
|
"HOST-RACE".to_string(),
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(sid_re, sid, "reconnect reattaches the same offline session");
|
assert_eq!(sid_re, sid, "reconnect reattaches the same offline session");
|
||||||
assert_eq!(mgr.get_session(sid).await.map(|s| s.is_online), Some(true));
|
assert_eq!(mgr.get_session(sid).await.map(|s| s.is_online), Some(true));
|
||||||
@@ -1633,7 +1732,10 @@ mod tests {
|
|||||||
&& data.last_heartbeat_instant.elapsed() > ttl
|
&& data.last_heartbeat_instant.elapsed() > ttl
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
assert!(!removed, "a session that went online must not be removed by the guard");
|
assert!(
|
||||||
|
!removed,
|
||||||
|
"a session that went online must not be removed by the guard"
|
||||||
|
);
|
||||||
assert!(
|
assert!(
|
||||||
mgr.get_session(sid).await.is_some(),
|
mgr.get_session(sid).await.is_some(),
|
||||||
"the now-online session must survive the guarded removal"
|
"the now-online session must survive the guarded removal"
|
||||||
|
|||||||
Reference in New Issue
Block a user