mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Emit host skill budget warnings from world state (#35766)
## Why Host skill catalogs supplied through extension world state should report budget warnings only when their catalog update is actually rendered. ## What changed - Stop rebuilding an extension-owned host catalog in core. - Emit host catalog budget warnings when the corresponding world-state fragment is rendered, and deduplicate them with executor catalog warnings. - Cover full, shortened, and omitted host and executor catalogs, including repeated turns and resumed persisted snapshots. GitOrigin-RevId: ba28b6e337281b49ed4da2175c193fd4a6898ab7
This commit is contained in:
@@ -63,6 +63,7 @@ use crate::state::SkillsThreadState;
|
||||
use crate::state::SkillsTurnState;
|
||||
use crate::tools::skill_tools;
|
||||
use crate::warnings::bounded_warnings;
|
||||
use crate::world_state::HostSkillsWarningEmitter;
|
||||
use crate::world_state::executor_skills_world_state_section;
|
||||
use crate::world_state::host_skills_world_state_section;
|
||||
|
||||
@@ -270,31 +271,46 @@ where
|
||||
} else {
|
||||
RenderedCatalog::default()
|
||||
};
|
||||
if let Some(message) = rendered.warning_message
|
||||
&& input
|
||||
.turn_store
|
||||
.get_or_init(EmittedCatalogBudgetWarnings::default)
|
||||
.insert(&message)
|
||||
{
|
||||
self.emit_warning(input.thread_store.level_id(), Some(input.turn_id), message);
|
||||
}
|
||||
let warning_message = rendered.warning_message;
|
||||
let executor_body = rendered.fragment.map(|fragment| fragment.body());
|
||||
let mut sections = vec![executor_skills_world_state_section(
|
||||
executor_body,
|
||||
config.include_instructions,
|
||||
)];
|
||||
let emitted_warnings = input
|
||||
.turn_store
|
||||
.get_or_init(EmittedCatalogBudgetWarnings::default);
|
||||
if let Some(host_snapshot) = input.turn_store.get::<HostSkillsSnapshot>()
|
||||
&& self.providers.has_host_provider()
|
||||
{
|
||||
input.turn_store.insert(HostSkillsCatalogInWorldState);
|
||||
let event_sink = Arc::clone(&self.event_sink);
|
||||
let emitted_warnings = Arc::clone(&emitted_warnings);
|
||||
let thread_id = input.thread_store.level_id().to_string();
|
||||
let turn_id = input.turn_id.to_string();
|
||||
let warning_emitter: HostSkillsWarningEmitter = Arc::new(move |message| {
|
||||
if emitted_warnings.insert(&message) {
|
||||
event_sink.emit_warning(ExtensionWarning {
|
||||
thread_id: thread_id.clone(),
|
||||
turn_id: Some(turn_id.clone()),
|
||||
message,
|
||||
});
|
||||
}
|
||||
});
|
||||
sections.push(host_skills_world_state_section(
|
||||
&host_snapshot,
|
||||
config.include_instructions,
|
||||
include_usage,
|
||||
metadata_budget,
|
||||
extension_metrics,
|
||||
warning_emitter,
|
||||
));
|
||||
}
|
||||
if let Some(message) = warning_message
|
||||
&& emitted_warnings.insert(&message)
|
||||
{
|
||||
self.emit_warning(input.thread_store.level_id(), Some(input.turn_id), message);
|
||||
}
|
||||
sections
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ const NO_HOST_SKILLS_BODY: &str =
|
||||
"\n## Host skills update\nNo host skills are currently available.\n";
|
||||
const HIDDEN_HOST_SKILLS_BODY: &str = "\n## Host skills update\nHost skills are not listed automatically. Explicit skill mentions can still be resolved when available.\n";
|
||||
|
||||
pub(crate) type HostSkillsWarningEmitter = Arc<dyn Fn(String) + Send + Sync>;
|
||||
|
||||
pub(crate) fn executor_skills_world_state_section(
|
||||
body: Option<String>,
|
||||
include_instructions: bool,
|
||||
@@ -84,6 +86,7 @@ pub(crate) fn host_skills_world_state_section(
|
||||
include_skills_usage_instructions: bool,
|
||||
metadata_budget: SkillMetadataBudget,
|
||||
extension_metrics: Option<Arc<dyn ExtensionMetrics>>,
|
||||
warning_emitter: HostSkillsWarningEmitter,
|
||||
) -> WorldStateSectionContribution {
|
||||
let outcome = host_snapshot.outcome();
|
||||
let metadata_budget = match metadata_budget {
|
||||
@@ -97,6 +100,9 @@ pub(crate) fn host_skills_world_state_section(
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let warning_message = available
|
||||
.as_ref()
|
||||
.and_then(|available| available.warning_message.clone());
|
||||
let render_metrics = include_instructions.then(|| {
|
||||
available
|
||||
.as_ref()
|
||||
@@ -118,6 +124,24 @@ pub(crate) fn host_skills_world_state_section(
|
||||
)
|
||||
.body()
|
||||
});
|
||||
rendered_host_skills_world_state_section(
|
||||
body,
|
||||
include_instructions,
|
||||
render_metrics,
|
||||
extension_metrics,
|
||||
warning_message,
|
||||
warning_emitter,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn rendered_host_skills_world_state_section(
|
||||
body: Option<String>,
|
||||
include_instructions: bool,
|
||||
render_metrics: Option<(usize, usize, usize, usize)>,
|
||||
extension_metrics: Option<Arc<dyn ExtensionMetrics>>,
|
||||
warning_message: Option<String>,
|
||||
warning_emitter: HostSkillsWarningEmitter,
|
||||
) -> WorldStateSectionContribution {
|
||||
let snapshot = json!({
|
||||
"body": body,
|
||||
"includeInstructions": include_instructions,
|
||||
@@ -159,6 +183,9 @@ pub(crate) fn host_skills_world_state_section(
|
||||
None if !include_instructions => HIDDEN_HOST_SKILLS_BODY,
|
||||
None => NO_HOST_SKILLS_BODY,
|
||||
};
|
||||
if let Some(message) = warning_message.as_ref() {
|
||||
warning_emitter(message.clone());
|
||||
}
|
||||
Some(RenderedWorldStateFragment::new(
|
||||
"developer",
|
||||
(SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
|
||||
|
||||
@@ -304,6 +304,112 @@ async fn host_world_state_records_catalog_metrics_on_publish_and_change() -> Tes
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn persisted_host_snapshot_deduplicates_warning_after_reinitialization() -> TestResult {
|
||||
let (event_tx, event_rx) = std::sync::mpsc::channel();
|
||||
let mut builder =
|
||||
ExtensionRegistryBuilder::with_event_sink(Arc::new(ChannelEventSink(event_tx)));
|
||||
install(&mut builder, skills_extension_config);
|
||||
let registry = builder.build();
|
||||
let session_store = ExtensionData::new("session");
|
||||
let config = default_config();
|
||||
let skill_path = AbsolutePathBuf::try_from(test_codex_home().join("skills/demo/SKILL.md"))?;
|
||||
let mut outcome = SkillLoadOutcome::default();
|
||||
outcome.skills.push(SkillMetadata {
|
||||
name: "demo".to_string(),
|
||||
description: "Demo skill.".to_string(),
|
||||
short_description: None,
|
||||
interface: None,
|
||||
dependencies: None,
|
||||
policy: None,
|
||||
path_to_skills_md: skill_path,
|
||||
scope: SkillScope::User,
|
||||
plugin_id: None,
|
||||
remote_plugin_id: None,
|
||||
});
|
||||
let host_snapshot = HostSkillsSnapshot::new(Arc::new(outcome));
|
||||
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
registry.thread_lifecycle_contributors()[0]
|
||||
.on_thread_start(ThreadStartInput {
|
||||
config: &config,
|
||||
session_source: &SessionSource::Cli,
|
||||
persistent_thread_state_available: true,
|
||||
environments: &[],
|
||||
mcp_resource_client: None,
|
||||
extension_metrics: None,
|
||||
session_store: &session_store,
|
||||
thread_store: &thread_store,
|
||||
})
|
||||
.await;
|
||||
let mut model_info = model_info_from_slug("test-model");
|
||||
model_info.context_window = Some(50);
|
||||
thread_store.insert(model_info);
|
||||
let turn_store = ExtensionData::new("turn-1");
|
||||
turn_store.insert(host_snapshot.clone());
|
||||
let sections = registry.context_contributors()[0]
|
||||
.contribute_world_state(WorldStateContributionInput {
|
||||
thread_id: codex_protocol::ThreadId::new(),
|
||||
turn_id: "turn-1",
|
||||
environments: &[],
|
||||
ready_selected_capability_roots: &[],
|
||||
executor_capability_discovery: None,
|
||||
extension_metrics: None,
|
||||
session_store: &session_store,
|
||||
thread_store: &thread_store,
|
||||
turn_store: &turn_store,
|
||||
})
|
||||
.await;
|
||||
let published_snapshot = sections[1].snapshot().clone();
|
||||
assert!(
|
||||
sections[1]
|
||||
.render_diff(PreviousWorldStateSection::Absent)
|
||||
.is_some()
|
||||
);
|
||||
event_rx.try_recv()?.into_warning();
|
||||
assert!(event_rx.try_recv().is_err());
|
||||
|
||||
let resumed_thread_store = ExtensionData::new("thread");
|
||||
registry.thread_lifecycle_contributors()[0]
|
||||
.on_thread_start(ThreadStartInput {
|
||||
config: &config,
|
||||
session_source: &SessionSource::Cli,
|
||||
persistent_thread_state_available: true,
|
||||
environments: &[],
|
||||
mcp_resource_client: None,
|
||||
extension_metrics: None,
|
||||
session_store: &session_store,
|
||||
thread_store: &resumed_thread_store,
|
||||
})
|
||||
.await;
|
||||
let mut model_info = model_info_from_slug("test-model");
|
||||
model_info.context_window = Some(50);
|
||||
resumed_thread_store.insert(model_info);
|
||||
let resumed_turn_store = ExtensionData::new("turn-2");
|
||||
resumed_turn_store.insert(host_snapshot);
|
||||
let sections = registry.context_contributors()[0]
|
||||
.contribute_world_state(WorldStateContributionInput {
|
||||
thread_id: codex_protocol::ThreadId::new(),
|
||||
turn_id: "turn-2",
|
||||
environments: &[],
|
||||
ready_selected_capability_roots: &[],
|
||||
executor_capability_discovery: None,
|
||||
extension_metrics: None,
|
||||
session_store: &session_store,
|
||||
thread_store: &resumed_thread_store,
|
||||
turn_store: &resumed_turn_store,
|
||||
})
|
||||
.await;
|
||||
assert!(
|
||||
sections[1]
|
||||
.render_diff(PreviousWorldStateSection::Known(&published_snapshot))
|
||||
.is_none()
|
||||
);
|
||||
assert!(event_rx.try_recv().is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nonempty_executor_empty_host_records_catalog_metrics() -> TestResult {
|
||||
let executor_provider = Arc::new(StaticSkillProvider {
|
||||
|
||||
Reference in New Issue
Block a user