feat: add metric to track the number of turns with memory usage (#18662)

Add a metric `codex.turn.memory` to know if a turn used memories or not.
This is not part of the other turn metrics as a label to limit
cardinality
This commit is contained in:
jif-oai authored and GitHub committed 2026-04-20 14:31:22 +01:00
1 parent 1c24347772
commit 2c59806fe0
8 files changed
+138 -26

No files matched your search

+16 -7
View File
@@ -9,8 +9,8 @@ use codex_utils_stream_parser::strip_citations;
use tokio_util::sync::CancellationToken;
use crate::function_tool::FunctionCallError;
use crate::memories::citations::get_thread_id_from_citations;
use crate::memories::citations::parse_memory_citation;
use crate::memories::citations::thread_ids_from_memory_citation;
use crate::parse_turn_item;
use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
@@ -137,7 +137,12 @@ pub(crate) async fn record_completed_response_item(
.await;
}
mark_thread_memory_mode_polluted_if_external_context(sess, turn_context, item).await;
record_stage1_output_usage_for_completed_item(turn_context, item).await;
let has_memory_citation =
record_stage1_output_usage_and_detect_memory_citation(turn_context, item).await;
if has_memory_citation {
sess.record_memory_citation_for_turn(&turn_context.sub_id)
.await;
}
}
fn response_item_may_include_external_context(item: &ResponseItem) -> bool {
@@ -167,23 +172,27 @@ pub(crate) async fn mark_thread_memory_mode_polluted_if_external_context(
.await;
}
async fn record_stage1_output_usage_for_completed_item(
async fn record_stage1_output_usage_and_detect_memory_citation(
turn_context: &TurnContext,
item: &ResponseItem,
) {
) -> bool {
let Some(raw_text) = raw_assistant_output_text_from_item(item) else {
return;
return false;
};
let (_, citations) = strip_citations(&raw_text);
let thread_ids = get_thread_id_from_citations(citations);
let Some(memory_citation) = parse_memory_citation(citations) else {
return false;
};
let thread_ids = thread_ids_from_memory_citation(&memory_citation);
if thread_ids.is_empty() {
return;
return true;
}
if let Some(db) = state_db::get_state_db(turn_context.config.as_ref()).await {
let _ = db.record_stage1_output_usage(&thread_ids).await;
}
true
}
/// Handle a completed output item from the model stream, recording it and