Skip to content

Commit dcd5cee

Browse files
wesbillmanPinky
andcommitted
fix(desktop): hash effective spawn config the way spawn actually resolves it
Review feedback on #1602: the restart badge hash had two false-negatives. 1. Persona prompt/model/provider/env edits were excluded, but start/restore re-snapshot the linked persona onto the record right before spawning, so those edits DO apply on a plain restart. spawn_config_hash now applies the same prospective re-snapshot (persona_snapshot_with_agent_config_fallback) before hashing, so the badge trips when a restart would pick up the edit. 2. A blank record relay_url resolves to the active workspace relay at spawn, but the raw "" was hashed — a workspace relay change never badged. The relay is now hashed in resolved form (effective_agent_relay_url) with the workspace relay threaded from both call sites, keeping the hash pure. Flipped persona_prompt_edit test to assert_ne and added blank-relay / pinned-relay workspace-change regression tests. Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Wes <[email protected]>
1 parent c1c2168 commit dcd5cee

3 files changed

Lines changed: 103 additions & 32 deletions

File tree

‎desktop/src-tauri/src/managed_agents/runtime.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,8 +1360,14 @@ pub fn build_managed_agent_summary(
13601360
// tracked live process can drift — stopped agents spawn fresh, and
13611361
// adopted (runtime_pid-only) processes have no stamped hash to compare.
13621362
let needs_restart = runtimes.get(&record.pubkey).is_some_and(|runtime| {
1363+
use tauri::Manager;
1364+
let state = app.state::<crate::app_state::AppState>();
13631365
runtime.spawn_config_hash
1364-
!= crate::managed_agents::spawn_hash::spawn_config_hash(record, personas)
1366+
!= crate::managed_agents::spawn_hash::spawn_config_hash(
1367+
record,
1368+
personas,
1369+
&crate::relay::relay_ws_url_with_override(&state),
1370+
)
13651371
});
13661372

13671373
// Resolve the effective harness the same way, then derive args/mcp from it,
@@ -1873,7 +1879,10 @@ pub fn spawn_agent_child(
18731879

18741880
// Stamp the effective spawn config so the summary builder can flag
18751881
// needs_restart when disk state drifts from what this process runs.
1876-
let spawn_config_hash = super::spawn_hash::spawn_config_hash(record, &personas);
1882+
// `effective_relay_url` is already resolved, and resolution is idempotent,
1883+
// so it serves as the workspace-relay input here.
1884+
let spawn_config_hash =
1885+
super::spawn_hash::spawn_config_hash(record, &personas, &effective_relay_url);
18771886

18781887
let _ = super::write_agent_pid_file(app, &record.pubkey, child.id());
18791888

‎desktop/src-tauri/src/managed_agents/spawn_hash.rs‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
//! spawn) against a recomputation from current disk state and show a
77
//! "restart required" badge only when a restart would change what runs.
88
//!
9-
//! Scope rules (decided in #centralize-personas-and-agents):
10-
//! - Inputs mirror `spawn_agent_child`: the live-persona-resolved harness
11-
//! command (persona runtime edits DO propagate on restart), its derived
12-
//! args/mcp command, the effective env layering, and the record fields the
13-
//! spawn env writes read.
14-
//! - Persona prompt/model/provider edits are EXCLUDED: spawn reads the pinned
15-
//! record snapshot, so a restart would not apply them — that drift is
16-
//! `persona_out_of_date`'s respawn signal, not a restart signal.
9+
//! Scope rules (decided in #centralize-personas-and-agents, revised in PR
10+
//! #1602 review):
11+
//! - Inputs mirror what a start would actually run: the start/restore paths
12+
//! re-snapshot the linked persona's prompt/model/provider/env onto the
13+
//! record immediately before spawning (`start_local_agent_with_preflight`,
14+
//! `restore_managed_agents_on_launch`), so persona edits to those fields DO
15+
//! apply on a plain restart and are hashed via the same prospective
16+
//! re-snapshot. Harness command, args/mcp, env layering, and the record
17+
//! fields the spawn env writes read are hashed as spawn resolves them.
18+
//! - The relay URL is hashed in resolved form (`effective_agent_relay_url`):
19+
//! a record with a blank relay spawns against the active workspace relay,
20+
//! so a workspace relay change means a restart would change what runs.
1721
//! - Channel membership is not an input: agents pick up channel changes live
1822
//! (#1468), never via restart.
1923
//!
@@ -23,13 +27,43 @@
2327
use std::hash::{DefaultHasher, Hash, Hasher};
2428

2529
use super::{
26-
effective_agent_command, known_acp_runtime, normalize_agent_args, resolve_effective_agent_env,
30+
effective_agent_command, known_acp_runtime, normalize_agent_args,
31+
persona_events::persona_snapshot_with_agent_config_fallback,
32+
resolve_effective_agent_env,
2733
types::{ManagedAgentRecord, PersonaRecord},
2834
};
2935

3036
/// Digest the effective spawn configuration of `record` under the current
31-
/// `personas`. Pure — no `AppHandle`, no disk, no keyring.
32-
pub(crate) fn spawn_config_hash(record: &ManagedAgentRecord, personas: &[PersonaRecord]) -> u64 {
37+
/// `personas`, resolving a blank record relay against `workspace_relay`.
38+
/// Pure — no `AppHandle`, no disk, no keyring.
39+
pub(crate) fn spawn_config_hash(
40+
record: &ManagedAgentRecord,
41+
personas: &[PersonaRecord],
42+
workspace_relay: &str,
43+
) -> u64 {
44+
// Prospective re-snapshot: mirror the mutation start/restore apply to the
45+
// record right before spawning, so the hash covers what a restart would
46+
// actually run. Idempotent, so the spawn-time stamp (post-snapshot record)
47+
// and later recomputes (persisted record) agree when nothing changed.
48+
let mut record = record.clone();
49+
if let Some(persona_id) = record.persona_id.clone() {
50+
if let Some(persona) = personas.iter().find(|p| p.id == persona_id) {
51+
let snapshot = persona_snapshot_with_agent_config_fallback(
52+
persona,
53+
&record.env_vars,
54+
record.model.as_deref(),
55+
record.provider.as_deref(),
56+
);
57+
if let Some(prompt) = snapshot.system_prompt {
58+
record.system_prompt = Some(prompt);
59+
}
60+
record.model = snapshot.model;
61+
record.provider = snapshot.provider;
62+
record.env_vars = snapshot.env_vars;
63+
}
64+
}
65+
let record = &record;
66+
3367
let effective_command = effective_agent_command(
3468
record.persona_id.as_deref(),
3569
personas,
@@ -53,8 +87,10 @@ pub(crate) fn spawn_config_hash(record: &ManagedAgentRecord, personas: &[Persona
5387
// BTreeMap iteration is ordered, so this is deterministic.
5488
effective.env.hash(&mut hasher);
5589

56-
// Record fields the spawn env writes read directly.
57-
record.relay_url.hash(&mut hasher);
90+
// Record fields the spawn env writes read directly. The relay is hashed
91+
// resolved: a blank record relay spawns on the workspace relay, so a
92+
// workspace relay change must trip the badge.
93+
crate::relay::effective_agent_relay_url(&record.relay_url, workspace_relay).hash(&mut hasher);
5894
record.system_prompt.hash(&mut hasher);
5995
record.model.hash(&mut hasher);
6096
record.provider.hash(&mut hasher);

‎desktop/src-tauri/src/managed_agents/spawn_hash/tests.rs‎

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ fn persona(id: &str, runtime: Option<&str>, prompt: &str) -> PersonaRecord {
6767
#[test]
6868
fn hash_is_deterministic() {
6969
let rec = record();
70-
assert_eq!(spawn_config_hash(&rec, &[]), spawn_config_hash(&rec, &[]));
70+
assert_eq!(
71+
spawn_config_hash(&rec, &[], "wss://ws.example"),
72+
spawn_config_hash(&rec, &[], "wss://ws.example")
73+
);
7174
}
7275

7376
#[test]
@@ -78,8 +81,8 @@ fn record_env_var_edit_changes_hash() {
7881
.env_vars
7982
.insert("SOME_KEY".into(), "some-value".into());
8083
assert_ne!(
81-
spawn_config_hash(&rec, &[]),
82-
spawn_config_hash(&edited, &[])
84+
spawn_config_hash(&rec, &[], "wss://ws.example"),
85+
spawn_config_hash(&edited, &[], "wss://ws.example")
8386
);
8487
}
8588

@@ -89,8 +92,8 @@ fn record_prompt_edit_changes_hash() {
8992
let mut edited = record();
9093
edited.system_prompt = Some("Edited prompt.".into());
9194
assert_ne!(
92-
spawn_config_hash(&rec, &[]),
93-
spawn_config_hash(&edited, &[])
95+
spawn_config_hash(&rec, &[], "wss://ws.example"),
96+
spawn_config_hash(&edited, &[], "wss://ws.example")
9497
);
9598
}
9699

@@ -103,23 +106,46 @@ fn persona_runtime_edit_changes_hash() {
103106
let before = [persona("pers", Some("goose"), "prompt")];
104107
let after = [persona("pers", Some("claude"), "prompt")];
105108
assert_ne!(
106-
spawn_config_hash(&rec, &before),
107-
spawn_config_hash(&rec, &after)
109+
spawn_config_hash(&rec, &before, "wss://ws.example"),
110+
spawn_config_hash(&rec, &after, "wss://ws.example")
108111
);
109112
}
110113

111114
#[test]
112-
fn persona_prompt_edit_does_not_change_hash() {
113-
// Spawn reads the pinned record snapshot for the prompt — a restart would
114-
// NOT apply a persona prompt edit, so it must not trip the restart badge
115-
// (that drift is persona_out_of_date's respawn signal).
115+
fn persona_prompt_edit_changes_hash() {
116+
// Start/restore re-snapshot the persona prompt onto the record right
117+
// before spawning, so a persona prompt edit DOES apply on a plain
118+
// restart → the badge must trip.
116119
let mut rec = record();
117120
rec.persona_id = Some("pers".into());
118121
let before = [persona("pers", Some("goose"), "old prompt")];
119122
let after = [persona("pers", Some("goose"), "new prompt")];
123+
assert_ne!(
124+
spawn_config_hash(&rec, &before, "wss://ws.example"),
125+
spawn_config_hash(&rec, &after, "wss://ws.example")
126+
);
127+
}
128+
129+
#[test]
130+
fn workspace_relay_change_trips_hash_for_blank_record_relay() {
131+
// A blank record relay spawns against the active workspace relay, so a
132+
// workspace relay change means a restart would change what runs.
133+
let mut rec = record();
134+
rec.relay_url = String::new();
135+
assert_ne!(
136+
spawn_config_hash(&rec, &[], "wss://relay-a.example"),
137+
spawn_config_hash(&rec, &[], "wss://relay-b.example")
138+
);
139+
}
140+
141+
#[test]
142+
fn workspace_relay_change_ignored_for_pinned_record_relay() {
143+
// An explicit per-agent relay pins the agent regardless of workspace, so
144+
// a workspace relay change must NOT badge a pinned agent.
145+
let rec = record();
120146
assert_eq!(
121-
spawn_config_hash(&rec, &before),
122-
spawn_config_hash(&rec, &after)
147+
spawn_config_hash(&rec, &[], "wss://relay-a.example"),
148+
spawn_config_hash(&rec, &[], "wss://relay-b.example")
123149
);
124150
}
125151

@@ -129,8 +155,8 @@ fn respond_to_allowlist_edit_changes_hash() {
129155
let mut edited = record();
130156
edited.respond_to_allowlist = vec!["a".repeat(64)];
131157
assert_ne!(
132-
spawn_config_hash(&rec, &[]),
133-
spawn_config_hash(&edited, &[])
158+
spawn_config_hash(&rec, &[], "wss://ws.example"),
159+
spawn_config_hash(&edited, &[], "wss://ws.example")
134160
);
135161
}
136162

@@ -145,7 +171,7 @@ fn non_spawn_bookkeeping_fields_do_not_change_hash() {
145171
edited.last_started_at = Some("later".into());
146172
edited.last_exit_code = Some(0);
147173
assert_eq!(
148-
spawn_config_hash(&rec, &[]),
149-
spawn_config_hash(&edited, &[])
174+
spawn_config_hash(&rec, &[], "wss://ws.example"),
175+
spawn_config_hash(&edited, &[], "wss://ws.example")
150176
);
151177
}

0 commit comments

Comments
 (0)