Skip to content

Commit 02227d7

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(desktop): strip legacy baked team instructions from stored prompts (#3035)
Agents in a team receive two `Team Instructions` blocks per turn, and the observer feed renders two Team Instructions cards for them. There are two producers. `with_team()` in `crates/buzz-acp/src/pool.rs` appends the LIVE `[Team Instructions]` block from the runtime `TeamRecord` — that one is correct. The second is baked into the stored `system_prompt` itself: records written before the runtime framing landed were composed by the now-removed `compose_prompt()` in `buzz-persona`, which appended `"\n\n---\n# Team Instructions\n"` plus a frozen copy of the team instructions. So an affected agent is fed a stale roster ahead of the current one, and the transcript parser — correctly — reports both. Fixing this in `parseSystemPromptSections` would hide the symptom while the agent kept receiving the stale bytes, so the suffix is removed at rest by a boot migration. `strip_baked_team_instructions` splits each stored `system_prompt` at the LAST occurrence of the exact delimiter and keeps the text before it. Last-occurrence matches the parser's own `lastIndexOf` guard: a persona body may quote a delimiter-shaped passage, and only the final one is the producer boundary. The match is byte-exact — a bare `---`, a `# Team Instructions` heading at a different position, or a single preceding newline are author content and are left alone. It applies to every record regardless of `team_id` / `persona_id` / `pubkey`: the key-less definition records carry the suffix exactly as the instances minted from them do. A prompt that was nothing but the suffix becomes `None`, not `Some("")`, matching the absent-prompt convention in `AgentDefinition::into_agent_record`. Stripping a definition's prompt changes its `persona_content_hash`, which is the drift basis behind the Agents-menu "out of date" badge. Left alone, every linked instance would light up stale for a change the user never made. The migration therefore advances the pin of instances whose `persona_source_version` still equals the definition's PRE-strip hash — the same conditional `refresh_builtin_agent_avatars` already uses. An instance that had genuinely drifted keeps its stale pin, and its badge. The migration runs after `fold_personas_into_agent_store` so definitions lifted out of the legacy `personas.json` are cleaned in the same boot, and before `backfill_standalone_agents` so a manufactured definition never snapshots a suffix about to be removed. It writes only when at least one record changed, so a second boot is a true no-op, and takes a create-if-absent backup at `managed-agents.json.pre-team-suffix-strip.bak` following the `pre-backfill.bak` contract — a re-run after a partial failure cannot replace the pristine backup with a half-migrated snapshot. An unparseable store errors without writing and without taking a backup, leaving the file for manual recovery. Pass 5's legacy branch in `agentSessionTranscriptHelpers.ts` is deliberately untouched: un-migrated installs and snapshot imports still need it. --------- Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
1 parent d43fc70 commit 02227d7

6 files changed

Lines changed: 737 additions & 7 deletions

File tree

‎desktop/src-tauri/src/migration.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ fn run_boot_migrations_inner(app: &tauri::AppHandle, reset_completed: bool) {
176176
// Post-fold readers of the runtime map (`load_persona_runtimes`) fall
177177
// back to the unified store's definitions.
178178
fold_personas_into_agent_store(app);
179+
// Clean the legacy baked team-instructions suffix out of stored prompts
180+
// AFTER the fold (so definitions lifted out of personas.json are cleaned in
181+
// the same boot) and BEFORE backfill_standalone_agents (so a manufactured
182+
// definition never snapshots a suffix this strips).
183+
strip_baked_team_instructions(app);
179184
refresh_builtin_agent_avatars(app);
180185
// B5: manufacture definitions for standalone agents AFTER the fold (so
181186
// pre-existing definition slugs are present for collision checks) and
@@ -1362,7 +1367,6 @@ pub fn migrate_persona_provider_to_runtime(app: &tauri::AppHandle) {
13621367
}
13631368
rename_provider_to_runtime_in_personas(&path);
13641369
}
1365-
13661370
mod materialize;
13671371
pub use materialize::materialize_agent_runtimes;
13681372
mod fold;
@@ -1372,6 +1376,8 @@ mod backfill;
13721376
pub use backfill::backfill_standalone_agents;
13731377
mod detach;
13741378
pub use detach::detach_directory_backed_teams;
1379+
mod team_suffix;
1380+
pub use team_suffix::strip_baked_team_instructions;
13751381

13761382
#[cfg(test)]
13771383
#[path = "migration_test_support.rs"]

‎desktop/src-tauri/src/migration/backfill.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ fn backfill_standalone_agents_in_dir(base_dir: &Path) -> Result<usize, String> {
6565
}
6666

6767
// Pre-migration backup, taken ONCE: a re-run after a partial failure must
68-
// not overwrite the pristine backup with a half-migrated snapshot.
69-
let bak_path = base_dir.join("managed-agents.json.pre-backfill.bak");
70-
if !bak_path.exists() {
71-
std::fs::write(&bak_path, &content)
72-
.map_err(|e| format!("failed to write pre-backfill backup: {e}"))?;
73-
}
68+
// not overwrite the pristine backup with a half-migrated snapshot. Owner-only
69+
// from the initial open, and sited next to the resolved store — see
70+
// `create_restricted_backup_once` and `resolved_backup_path`.
71+
let bak_path =
72+
crate::util::resolved_backup_path(&agents_path, "managed-agents.json.pre-backfill.bak");
73+
crate::util::create_restricted_backup_once(&bak_path, content.as_bytes())
74+
.map_err(|e| format!("failed to write pre-backfill backup: {e}"))?;
7475

7576
let existing_slugs: std::collections::HashSet<String> =
7677
all.iter().filter_map(|r| r.slug.clone()).collect();

‎desktop/src-tauri/src/migration/backfill_tests.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,34 @@ fn slug_collision_fails_loudly_per_record_and_continues() {
299299
let clean_rec = records.iter().find(|r| r.pubkey == clean).unwrap();
300300
assert_eq!(clean_rec.persona_id.as_deref(), Some(clean.as_str()));
301301
}
302+
303+
#[cfg(unix)]
304+
#[test]
305+
fn backfill_creates_the_backup_owner_only() {
306+
// Same contract as the live store writer: this backup is a verbatim copy of
307+
// a file that carries plaintext agent nsecs when the keyring is unreachable,
308+
// so it is owner-only from the initial open rather than via a post-write
309+
// chmod that would leave a umask window.
310+
use std::os::unix::fs::PermissionsExt;
311+
312+
let dir = tempfile::tempdir().unwrap();
313+
let pubkey = "e".repeat(64);
314+
let mut record = standalone_agent_json("Solo", &pubkey, Some("P"));
315+
record["private_key_nsec"] = serde_json::json!("nsec1exampleplaintextkey");
316+
write_agents_json(dir.path(), &serde_json::json!([record]));
317+
318+
assert_eq!(
319+
backfill_standalone_agents_in_dir(&base(dir.path())).unwrap(),
320+
1
321+
);
322+
323+
let bak = base(dir.path()).join("managed-agents.json.pre-backfill.bak");
324+
let mode = std::fs::metadata(&bak).unwrap().permissions().mode() & 0o777;
325+
assert_eq!(mode, 0o600, "backup of an inline nsec must be owner-only");
326+
assert!(
327+
std::fs::read_to_string(&bak)
328+
.unwrap()
329+
.contains("nsec1exampleplaintextkey"),
330+
"the fixture really did carry an inline key"
331+
);
332+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
//! Strip the legacy baked team-instructions suffix from stored prompts.
2+
//!
3+
//! Records written before the runtime team framing landed have their team
4+
//! instructions BAKED into `system_prompt` by the now-removed
5+
//! `compose_prompt()` in buzz-persona:
6+
//!
7+
//! ```text
8+
//! {persona_prompt}\n\n---\n# Team Instructions\n{instructions}
9+
//! ```
10+
//!
11+
//! `with_team()` in `buzz-acp/src/pool.rs` now appends the LIVE
12+
//! `[Team Instructions]` section on top of that stored value, so an affected
13+
//! agent receives two team-instruction blocks per turn — the frozen copy first,
14+
//! the live one second — and the observer feed renders two Team Instructions
15+
//! cards. The frozen copy is not merely redundant: it carries whatever the team
16+
//! roster said the day it was written, so the agent is fed a stale roster ahead
17+
//! of the current one.
18+
//!
19+
//! The fix has to happen at rest. Suppressing the duplicate in the observer
20+
//! parser would hide the symptom while the agent kept receiving the stale bytes.
21+
//!
22+
//! Stripping a definition's prompt also changes its `persona_content_hash`, the
23+
//! drift basis behind the Agents-menu "out of date" badge, so the migration
24+
//! advances the pin of instances that were current before the strip (see
25+
//! [`repin_current_instances`]).
26+
27+
use std::collections::HashMap;
28+
use std::path::Path;
29+
30+
use crate::managed_agents::{
31+
persona_events::{persona_content_hash, persona_event_content},
32+
ManagedAgentRecord,
33+
};
34+
35+
/// The exact producer boundary emitted by the removed `compose_prompt()`.
36+
/// Matched byte-for-byte: a bare `---`, a `# Team Instructions` heading at a
37+
/// different position, or a single preceding newline are author content, not a
38+
/// producer boundary, and are left alone.
39+
const TEAM_DELIMITER: &str = "\n\n---\n# Team Instructions\n";
40+
41+
/// Strip the baked team-instructions suffix from every stored `system_prompt`.
42+
///
43+
/// Ordering (see `run_boot_migrations`): runs AFTER
44+
/// `fold_personas_into_agent_store` so definitions folded out of the legacy
45+
/// `personas.json` are cleaned in the same boot, and BEFORE
46+
/// `backfill_standalone_agents` so a manufactured definition never snapshots
47+
/// a suffix this migration is about to remove.
48+
pub fn strip_baked_team_instructions(app: &tauri::AppHandle) {
49+
let Ok(base_dir) = crate::managed_agents::managed_agents_base_dir(app) else {
50+
return;
51+
};
52+
match strip_baked_team_instructions_in_dir(&base_dir) {
53+
Ok(0) => {}
54+
Ok(stripped) => eprintln!(
55+
"buzz-desktop: team-suffix-strip: removed the baked team-instructions suffix from \
56+
{stripped} record(s)"
57+
),
58+
Err(e) => eprintln!("buzz-desktop: team-suffix-strip: {e}"),
59+
}
60+
}
61+
62+
/// Core logic, decoupled from the Tauri `AppHandle` for testing.
63+
///
64+
/// `base_dir` is the managed-agents base directory (`<AppDataDir>/agents/`).
65+
/// Returns the number of records changed; `Ok(0)` means nothing to do and
66+
/// nothing was written, so a second boot is a clean no-op.
67+
pub(super) fn strip_baked_team_instructions_in_dir(base_dir: &Path) -> Result<usize, String> {
68+
let agents_path = base_dir.join("managed-agents.json");
69+
if !agents_path.exists() {
70+
return Ok(0);
71+
}
72+
let content = std::fs::read_to_string(&agents_path)
73+
.map_err(|e| format!("failed to read managed-agents.json: {e}"))?;
74+
let mut all: Vec<ManagedAgentRecord> = serde_json::from_str(&content)
75+
.map_err(|e| format!("failed to parse managed-agents.json: {e}"))?;
76+
77+
// Definition hashes BEFORE the strip: stripping a definition's
78+
// `system_prompt` changes its `persona_content_hash`, which is the drift
79+
// basis the Agents menu compares each linked instance's pinned
80+
// `persona_source_version` against. Captured here so instances that were
81+
// current before the strip can be re-pinned after it.
82+
let pre_strip_hashes = definition_hashes(&all);
83+
84+
// Applies to every record: the definition records carry the suffix as
85+
// surely as the instances minted from them, and neither `team_id` nor
86+
// `persona_id` nor a key is evidence either way.
87+
let mut stripped = 0usize;
88+
for record in all.iter_mut() {
89+
let Some(prompt) = record.system_prompt.as_deref() else {
90+
continue;
91+
};
92+
// Last occurrence only, mirroring the observer parser's own
93+
// `lastIndexOf` guard (`agentSessionTranscriptHelpers.ts`): a persona
94+
// body may legitimately quote a delimiter-shaped passage, and only the
95+
// final one is the boundary `compose_prompt()` appended.
96+
let Some(at) = prompt.rfind(TEAM_DELIMITER) else {
97+
continue;
98+
};
99+
let head = &prompt[..at];
100+
// An empty head means the record held nothing but the baked suffix.
101+
// Store `None` rather than `Some("")`, matching the empty-prompt
102+
// convention in `AgentDefinition::into_agent_record`, so no phantom
103+
// empty prompt is left behind.
104+
record.system_prompt = (!head.is_empty()).then(|| head.to_string());
105+
stripped += 1;
106+
}
107+
108+
if stripped == 0 {
109+
return Ok(0);
110+
}
111+
112+
repin_current_instances(&mut all, &pre_strip_hashes);
113+
114+
// Pre-migration backup, taken ONCE (same contract as the B5 backfill): a
115+
// re-run after a partial failure must not replace the pristine backup with
116+
// a half-migrated snapshot. Owner-only from the initial open, and sited next
117+
// to the resolved store — see `create_restricted_backup_once` and
118+
// `resolved_backup_path`.
119+
let bak_path = crate::util::resolved_backup_path(
120+
&agents_path,
121+
"managed-agents.json.pre-team-suffix-strip.bak",
122+
);
123+
crate::util::create_restricted_backup_once(&bak_path, content.as_bytes())
124+
.map_err(|e| format!("failed to write pre-strip backup: {e}"))?;
125+
126+
let payload = serde_json::to_vec_pretty(&all)
127+
.map_err(|e| format!("failed to serialize managed-agents.json: {e}"))?;
128+
crate::managed_agents::atomic_write_json_restricted(&agents_path, &payload)?;
129+
Ok(stripped)
130+
}
131+
132+
/// `slug → persona_content_hash` for every definition record, computed through
133+
/// the same projection the drift indicator uses (`persona_drift_state` in
134+
/// `managed_agents/runtime.rs`).
135+
fn definition_hashes(records: &[ManagedAgentRecord]) -> HashMap<String, String> {
136+
records
137+
.iter()
138+
.filter(|record| record.pubkey.is_empty())
139+
.filter_map(|record| {
140+
let definition = record.to_definition_view()?;
141+
let hash = persona_content_hash(&persona_event_content(&definition));
142+
Some((definition.id, hash))
143+
})
144+
.collect()
145+
}
146+
147+
/// Advance the drift pin of instances that were current before the strip.
148+
///
149+
/// Stripping a definition's prompt changes its `persona_content_hash`, so every
150+
/// linked instance would otherwise light up "out of date" in the Agents menu
151+
/// for a change the user never made — a badge that only clears on the next
152+
/// start, when `apply_persona_snapshot` re-pins. Same conditional as
153+
/// `refresh_builtin_agent_avatars`: move the pin only when it still equals the
154+
/// definition's PRE-strip hash. An instance that had genuinely drifted keeps
155+
/// its stale pin, and its badge.
156+
fn repin_current_instances(
157+
records: &mut [ManagedAgentRecord],
158+
pre_strip_hashes: &HashMap<String, String>,
159+
) {
160+
let post_strip_hashes = definition_hashes(records);
161+
for record in records.iter_mut() {
162+
if record.pubkey.is_empty() {
163+
continue;
164+
}
165+
let Some(persona_id) = record.persona_id.as_deref() else {
166+
continue;
167+
};
168+
let (Some(old), Some(new)) = (
169+
pre_strip_hashes.get(persona_id),
170+
post_strip_hashes.get(persona_id),
171+
) else {
172+
continue;
173+
};
174+
if old != new && record.persona_source_version.as_deref() == Some(old.as_str()) {
175+
record.persona_source_version = Some(new.clone());
176+
}
177+
}
178+
}
179+
180+
#[cfg(test)]
181+
#[path = "team_suffix_tests.rs"]
182+
mod tests;

0 commit comments

Comments
 (0)