fix: better validate for dev_brain and 50k reminder not triggering rebuild

This commit is contained in:
Georg Bauer
2026-07-27 23:08:47 +02:00
parent d371d9d659
commit 90a445cafd
2 changed files with 119 additions and 20 deletions

View File

@@ -568,14 +568,15 @@ impl Generator {
mut progress: impl FnMut(u32, u32, Option<f32>),
mut phase: impl FnMut(&'static str),
) -> Result<GenerationOutput, String> {
let history = messages
.split_last()
.map_or(messages, |(_, history)| history);
let checkpoint_present = checkpoint.is_file();
let selected = self.select_checkpoint(
checkpoint,
conversation_tag(&settings.system_prompt, settings.reasoning_mode, history),
)?;
let selected = self.select_checkpoint(checkpoint, |tag| {
checkpoint_matches_prefix(
tag,
&settings.system_prompt,
settings.reasoning_mode,
messages,
)
})?;
if checkpoint_present && !selected.found {
phase(checkpoint_rebuild_activity(selected.incompatible));
}
@@ -788,7 +789,10 @@ impl Generator {
return Ok(self.checkpoint.clone());
}
if let Some(entry) = store.find(key, self.executor.context()) {
if self.select_checkpoint(&entry.checkpoint, entry.tag)?.found {
if self
.select_checkpoint(&entry.checkpoint, |tag| tag == entry.tag)?
.found
{
store.touch(&entry)?;
self.last_store_tokens = entry.tokens;
return Ok(Some(entry.checkpoint));
@@ -955,10 +959,10 @@ impl Generator {
fn select_checkpoint(
&mut self,
checkpoint: &Path,
expected_tag: [u8; 32],
matches: impl Fn([u8; 32]) -> bool,
) -> Result<CheckpointSelection, String> {
let resident_hit = self.activate_resident(checkpoint.to_owned())?;
if resident_hit && self.executor.checkpoint_tag() == expected_tag {
if resident_hit && matches(self.executor.checkpoint_tag()) {
self.checkpoint = Some(checkpoint.to_owned());
self.metrics.kv_lookup(KvLookup::MemoryHit);
return Ok(CheckpointSelection {
@@ -976,7 +980,7 @@ impl Generator {
incompatible: false,
});
}
let found = self.executor.checkpoint_tag() == expected_tag;
let found = matches(self.executor.checkpoint_tag());
self.metrics.kv_lookup(if found {
KvLookup::MemoryHit
} else {
@@ -995,7 +999,7 @@ impl Generator {
.load_checkpoint(checkpoint, &mut |bytes| self.metrics.kv_read_bytes(bytes));
self.metrics
.kv_read_finished(started.elapsed(), loaded.is_err());
let found = matches!(loaded, Ok(true)) && self.executor.checkpoint_tag() == expected_tag;
let found = matches!(loaded, Ok(true)) && matches(self.executor.checkpoint_tag());
let incompatible = loaded.is_err();
let lookup = match loaded {
Ok(true) if found => KvLookup::DiskHit,
@@ -1527,6 +1531,20 @@ fn conversation_tag(system: &str, reasoning: ReasoningMode, messages: &[ChatTurn
Sha256::digest(conversation_key(system, reasoning, messages)).into()
}
#[cfg(target_os = "macos")]
fn checkpoint_matches_prefix(
checkpoint: [u8; 32],
system: &str,
reasoning: ReasoningMode,
messages: &[ChatTurn],
) -> bool {
// ponytail: appended control messages are few; carry incremental hashes if
// scanning a genuinely changed, very long history becomes measurable.
(0..messages.len())
.rev()
.any(|end| checkpoint == conversation_tag(system, reasoning, &messages[..end]))
}
#[cfg(target_os = "macos")]
fn resident_key(directory: &Path, tag: [u8; 32]) -> PathBuf {
let mut name = String::with_capacity(64);
@@ -1735,6 +1753,53 @@ mod sampling_tests {
assert!(conversation_key("System", ReasoningMode::High, &messages).starts_with(&prefix));
}
#[test]
fn checkpoint_tag_accepts_an_unchanged_prefix_before_reminders() {
let mut messages = vec![ChatTurn {
user: true,
tool: false,
system: false,
skip_previous_eos: false,
reasoning: None,
reasoning_complete: true,
content: "Question".into(),
}];
let checkpoint = conversation_tag("System", ReasoningMode::High, &messages);
messages.extend([
ChatTurn {
user: false,
tool: true,
system: false,
skip_previous_eos: false,
reasoning: None,
reasoning_complete: true,
content: "Tool result".into(),
},
ChatTurn {
user: false,
tool: false,
system: true,
skip_previous_eos: false,
reasoning: None,
reasoning_complete: true,
content: "System prompt reminder".into(),
},
]);
assert!(checkpoint_matches_prefix(
checkpoint,
"System",
ReasoningMode::High,
&messages,
));
assert!(!checkpoint_matches_prefix(
checkpoint,
"Changed",
ReasoningMode::High,
&messages,
));
}
#[test]
fn bootstrap_key_is_the_prefix_before_dynamic_session_context() {
let system = "System\n\nProject instructions from AGENTS.md:\n\nkeep this";