fix(reflection): 修复 state restore 竞态 — peek+pop 替代直接 pop

根因: pop_restored_state 在 getHistory 读取前删除数据。
修复: 先 peek 非破坏性读取,apply 后再 pop,确保数据可被多次读取。
This commit is contained in:
iven
2026-04-11 12:51:26 +08:00
parent 8a869f6990
commit d974af3042

View File

@@ -112,12 +112,15 @@ pub async fn post_conversation_hook(
// Step 2: Record conversation for reflection
let mut engine = reflection_state.lock().await;
// Apply restored state on first call (one-shot after app restart)
if let Some(restored_state) = crate::intelligence::reflection::pop_restored_state(agent_id) {
// Apply restored state on first call (peek-then-pop to avoid race with getHistory)
if let Some(restored_state) = crate::intelligence::reflection::peek_restored_state(agent_id) {
engine.apply_restored_state(restored_state);
// Pop after successful apply to prevent re-processing
crate::intelligence::reflection::pop_restored_state(agent_id);
}
if let Some(restored_result) = crate::intelligence::reflection::pop_restored_result(agent_id) {
if let Some(restored_result) = crate::intelligence::reflection::peek_restored_result(agent_id) {
engine.apply_restored_result(restored_result);
crate::intelligence::reflection::pop_restored_result(agent_id);
}
engine.record_conversation();