feat: add warning message for the model (#7445)

Add a warning message as a user turn to the model if the model does not
behave as expected (here, for example, if the model opens too many
`unified_exec` sessions)
This commit is contained in:
jif-oai
2025-12-02 11:56:00 +00:00
committed by GitHub
parent 4b78e2ab09
commit 9ee855ec57
4 changed files with 87 additions and 9 deletions

View File

@@ -1035,6 +1035,22 @@ impl Session {
state.record_items(items.iter(), turn_context.truncation_policy);
}
pub(crate) async fn record_model_warning(&self, message: impl Into<String>, ctx: &TurnContext) {
if !self.enabled(Feature::ModelWarnings).await {
return;
}
let item = ResponseItem::Message {
id: None,
role: "user".to_string(),
content: vec![ContentItem::InputText {
text: format!("Warning: {}", message.into()),
}],
};
self.record_conversation_items(ctx, &[item]).await;
}
pub(crate) async fn replace_history(&self, items: Vec<ResponseItem>) {
let mut state = self.state.lock().await;
state.replace_history(items);
@@ -2806,6 +2822,40 @@ mod tests {
(session, turn_context, rx_event)
}
#[tokio::test]
async fn record_model_warning_appends_user_message() {
let (session, turn_context) = make_session_and_context();
session
.state
.lock()
.await
.session_configuration
.features
.enable(Feature::ModelWarnings);
session
.record_model_warning("too many unified exec sessions", &turn_context)
.await;
let mut history = session.clone_history().await;
let history_items = history.get_history();
let last = history_items.last().expect("warning recorded");
match last {
ResponseItem::Message { role, content, .. } => {
assert_eq!(role, "user");
assert_eq!(
content,
&vec![ContentItem::InputText {
text: "Warning: too many unified exec sessions".to_string(),
}]
);
}
other => panic!("expected user message, got {other:?}"),
}
}
#[derive(Clone, Copy)]
struct NeverEndingTask {
kind: TaskKind,