From 8ea32990bcfc47f8c50c7aaa5ea456afd3b677ae Mon Sep 17 00:00:00 2001 From: felixxia-oai Date: Tue, 1 Dec 2026 16:07:16 +0000 Subject: [PATCH] Add message-count batching to the Guardian v3 housekeeper (#65028) ## What changed Set `batch_limit = { mode = "messages", limit = 9 }` in the housekeeper configuration to route up to nine new evidence records per batch. Each tool call and each tool result counts separately. In this mode, batches can exceed the default 100,000 estimated tokens while still fitting within the routing model's input limit. ## Testing Add a housekeeping test that routes nine large records in the first batch and defers the tenth to the next batch. GitOrigin-RevId: 99fdfc71f432007958c793a46b251f2e32a3e8ba --- codex-rs/core/config.schema.json | 22 +++++++++++++++++++ codex-rs/ext/guardian-v3/src/housekeeper.rs | 13 +++++++---- .../ext/guardian-v3/tests/housekeeping.rs | 11 ++++++++++ codex-rs/features/src/feature_configs.rs | 2 ++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 336bec38b5..7fb07cf499 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -2595,6 +2595,28 @@ "mode" ], "type": "object" + }, + { + "additionalProperties": false, + "description": "Number of new evidence records, counting each tool call and result separately.", + "properties": { + "limit": { + "format": "uint32", + "minimum": 1.0, + "type": "integer" + }, + "mode": { + "enum": [ + "messages" + ], + "type": "string" + } + }, + "required": [ + "limit", + "mode" + ], + "type": "object" } ] }, diff --git a/codex-rs/ext/guardian-v3/src/housekeeper.rs b/codex-rs/ext/guardian-v3/src/housekeeper.rs index 4beb141ce2..52730da1b3 100644 --- a/codex-rs/ext/guardian-v3/src/housekeeper.rs +++ b/codex-rs/ext/guardian-v3/src/housekeeper.rs @@ -167,9 +167,14 @@ impl Housekeeper { break; } } - let token_limit = match self.batch_limit { - None => 100_000, - Some(GuardianV3HousekeeperBatchLimit::Tokens(limit)) => limit.get() as usize, + let (token_limit, max_sources) = match self.batch_limit { + None => (100_000, pending.len()), + Some(GuardianV3HousekeeperBatchLimit::Tokens(limit)) => { + (limit.get() as usize, pending.len()) + } + Some(GuardianV3HousekeeperBatchLimit::Messages(limit)) => { + (self.sampler.input_limit, limit.get() as usize) + } }; let policy = TruncationPolicy::Tokens(token_limit.min(self.sampler.input_limit)); let input_budget = policy.byte_budget(); @@ -181,7 +186,7 @@ impl Housekeeper { // adds its serialized bytes and, after the first, one separating comma. let mut input_bytes = to_json_string_bounded(&empty_input, input_budget)?.len(); let mut texts = Vec::new(); - for source in pending { + for source in pending.iter().take(max_sources) { // Preserve the existing board's post-size limit, including source metadata. let text = to_json_string_bounded(source, 64 * 1024)?; // Every valid batch must route the first original to at least one channel. diff --git a/codex-rs/ext/guardian-v3/tests/housekeeping.rs b/codex-rs/ext/guardian-v3/tests/housekeeping.rs index 58faf1b5a2..04c6a3c975 100644 --- a/codex-rs/ext/guardian-v3/tests/housekeeping.rs +++ b/codex-rs/ext/guardian-v3/tests/housekeeping.rs @@ -93,6 +93,7 @@ use board_host::board_host; enum RoutingCase { Success, DefaultBatchLimit, + MessageBatchLimit, RetryExhausted, WindowPressure, InstructionsExceedWindow, @@ -108,6 +109,7 @@ enum RoutingCase { #[test_case::test_case(RoutingCase::Success; "transient_failures_route_each_original_once")] #[test_case::test_case(RoutingCase::DefaultBatchLimit; "default_token_limit_groups_large_originals")] +#[test_case::test_case(RoutingCase::MessageBatchLimit; "message_count_splits_batches_above_the_default_token_limit")] #[test_case::test_case(RoutingCase::RetryExhausted; "exhausted_retries_leave_the_batch_pending")] #[test_case::test_case(RoutingCase::WindowPressure; "splits_batches_to_fit_routing_instructions_and_evicts_completed_batches")] #[test_case::test_case(RoutingCase::InstructionsExceedWindow; "budgets_configured_routing_instructions")] @@ -171,6 +173,13 @@ async fn routes_captured_originals_and_updates_descriptions( let (first_batch_size, later_text, batch_limit) = match case { // Eight 44 KiB originals fit the default 400,000-byte batch. The ninth starts another. RoutingCase::DefaultBatchLimit => (8, "x".repeat(44 * 1024), None), + RoutingCase::MessageBatchLimit => ( + 9, + "x".repeat(44 * 1024), + Some(GuardianV3HousekeeperBatchLimit::Messages( + NonZeroU32::new(9).expect("positive limit"), + )), + ), RoutingCase::WindowPressure => ( 2, "The later message is outside this capture.".repeat(50), @@ -270,6 +279,7 @@ async fn routes_captured_originals_and_updates_descriptions( RoutingCase::InvalidResponse => r#"{"private-evidence-canary":true}"#, RoutingCase::Success | RoutingCase::DefaultBatchLimit + | RoutingCase::MessageBatchLimit | RoutingCase::RetryExhausted | RoutingCase::WindowPressure | RoutingCase::InstructionsExceedWindow @@ -385,6 +395,7 @@ async fn routes_captured_originals_and_updates_descriptions( case, RoutingCase::Success | RoutingCase::DefaultBatchLimit + | RoutingCase::MessageBatchLimit | RoutingCase::WindowPressure | RoutingCase::DuplicateDestination ) { diff --git a/codex-rs/features/src/feature_configs.rs b/codex-rs/features/src/feature_configs.rs index fd4a4dfbb2..dc3851dcd6 100644 --- a/codex-rs/features/src/feature_configs.rs +++ b/codex-rs/features/src/feature_configs.rs @@ -382,6 +382,8 @@ pub struct GuardianV3HousekeeperConfigToml { pub enum GuardianV3HousekeeperBatchLimit { /// Estimated tokens in the serialized channel catalog and new evidence. Tokens(NonZeroU32), + /// Number of new evidence records, counting each tool call and result separately. + Messages(NonZeroU32), } #[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]