huskies: merge 1128 story Bounded event queues + EventStreamGap sentinel + observability for context assembly

This commit is contained in:
dave
2026-05-17 20:23:11 +00:00
parent 21b2efd268
commit d86cc38b2a
6 changed files with 182 additions and 21 deletions
+34 -6
View File
@@ -14,6 +14,7 @@ use serde_json::json;
use super::super::state::{apply_and_persist, get_crdt, rebuild_llm_session_index};
use super::super::types::{LlmSessionCrdt, LlmSessionView};
use super::event_log::GAP_PIPELINE_EVENT;
/// Write or upsert an LLM session entry keyed by `session_id`.
///
@@ -110,7 +111,7 @@ pub fn assemble_and_advance_session(session_id: &str) -> Vec<String> {
// Advance the high-water mark to the maximum new event_seq.
let new_max_seq = new_events.iter().map(|(seq, ..)| *seq).max().unwrap_or(0);
let mut new_high_water = current_high_water;
new_high_water.insert(local_sled_id, new_max_seq);
new_high_water.insert(local_sled_id.clone(), new_max_seq);
let new_hw_json = serde_json::to_string(&new_high_water).unwrap_or_else(|_| "{}".to_string());
// Upsert the session entry with the new high-water value.
@@ -135,14 +136,41 @@ pub fn assemble_and_advance_session(session_id: &str) -> Vec<String> {
state.llm_session_index = rebuild_llm_session_index(&state.crdt);
}
// Render each new event as a compact audit line.
// Observability: log event-log size and gap count for this sled.
let total_entries = state
.crdt
.doc
.event_log
.iter()
.filter(|e| matches!(e.sled_id.view(), JsonValue::String(s) if s == local_sled_id))
.count();
let gap_count = state
.crdt
.doc
.event_log
.iter()
.filter(|e| {
matches!(e.sled_id.view(), JsonValue::String(s) if s == local_sled_id)
&& matches!(e.pipeline_event.view(), JsonValue::String(s) if s == GAP_PIPELINE_EVENT)
})
.count();
crate::slog!(
"[event-log] assemble session={session_id} sled_entries={total_entries} gap_count={gap_count}"
);
// Render each new event as a compact audit line; gap sentinels get a
// human-readable message so the LLM is never presented with raw field data.
new_events
.into_iter()
.map(|(_, story_id, from_stage, to_stage, pipeline_event)| {
format!(
"pipeline_event story_id=\"{story_id}\" from=\"{from_stage}\" \
to=\"{to_stage}\" event=\"{pipeline_event}\""
)
if pipeline_event == GAP_PIPELINE_EVENT {
format!("events between {from_stage} and {to_stage} were dropped")
} else {
format!(
"pipeline_event story_id=\"{story_id}\" from=\"{from_stage}\" \
to=\"{to_stage}\" event=\"{pipeline_event}\""
)
}
})
.collect()
}