Add configurable goal token budget limits (#37878)

## What changed

- Add `goals.max_goal_token_budget` as a positive-integer configuration setting.
- Use the configured maximum as the default budget for new goals and when `tokenBudget` is reset to `null`.
- Reject goal creation and updates whose token budget exceeds the configured maximum, including requests through goal tools and `thread/goal/set`.
- Respect managed configuration precedence and per-thread configuration overrides.

## Testing

- Cover configuration parsing and managed overrides.
- Cover defaulting, resetting, and rejecting oversized budgets through the goal service, goal tools, and app-server API.

GitOrigin-RevId: f8d7e6418cdc237d454c8cf47bb32ba0d44a60cf
This commit is contained in:
Eric Traut authored and copyberry committed 2026-08-10 21:00:00 +00:00
1 parent a603d7ca5c
commit a9dee37f9c
14 files changed
+391 -26

No files matched your search

@@ -800,6 +800,36 @@ extra = true
assert_eq!(nested.get("extra"), Some(&TomlValue::Boolean(true)));
}
#[tokio::test]
async fn managed_goal_token_budget_overrides_user_config() -> anyhow::Result<()> {
let tmp = tempdir()?;
let managed_path = tmp.path().join("managed_config.toml");
std::fs::write(
tmp.path().join(CONFIG_TOML_FILE),
"[goals]\nmax_goal_token_budget = 20000\n",
)?;
std::fs::write(&managed_path, "[goals]\nmax_goal_token_budget = 5000\n")?;
let state = load_config_layers_state(
LOCAL_FS.as_ref(),
tmp.path(),
Some(AbsolutePathBuf::try_from(tmp.path())?),
&[] as &[(String, TomlValue)],
LoaderOverrides::with_managed_config_path_for_tests(managed_path),
&codex_config::NoopThreadConfigLoader,
)
.await?;
assert_eq!(
state
.effective_config()
.get("goals")
.and_then(|goals| goals.get("max_goal_token_budget")),
Some(&TomlValue::Integer(5_000))
);
Ok(())
}
#[tokio::test]
async fn returns_empty_when_all_layers_missing() {
let tmp = tempdir().expect("tempdir");
+22
View File
@@ -326,6 +326,28 @@ consolidation_model = "gpt-5.2"
);
}
#[tokio::test]
async fn goal_max_token_budget_requires_positive_integer() {
let config_toml = toml::from_str::<ConfigToml>("[goals]\nmax_goal_token_budget = 25000\n")
.expect("positive goal token budget should deserialize");
let config = Config::load_from_base_config_with_overrides(
config_toml,
ConfigOverrides::default(),
tempdir().expect("tempdir").abs(),
)
.await
.expect("positive goal token budget should load");
assert_eq!(config.max_goal_token_budget, Some(25_000));
for invalid in ["0", "-1", "1.5", "\"100\""] {
let config = format!("[goals]\nmax_goal_token_budget = {invalid}\n");
assert!(
toml::from_str::<ConfigToml>(&config).is_err(),
"invalid goal token budget should be rejected: {invalid}"
);
}
}
#[test]
fn parses_bundled_skills_config() {
let cfg: ConfigToml = toml::from_str(
+16
View File
@@ -898,6 +898,9 @@ pub struct Config {
/// User-defined role declarations keyed by role name.
pub agent_roles: BTreeMap<String, AgentRoleConfig>,
/// Maximum token budget allowed for a goal and default budget for new goals.
pub max_goal_token_budget: Option<i64>,
/// Memories subsystem settings.
pub memories: MemoriesConfig,
@@ -4193,6 +4196,19 @@ impl Config {
agent_default_subagent_reasoning_effort,
agent_max_depth,
agent_roles,
max_goal_token_budget: cfg
.goals
.as_ref()
.and_then(|goals| goals.max_goal_token_budget)
.map(|max_goal_token_budget| {
i64::try_from(max_goal_token_budget.get()).map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"goals.max_goal_token_budget exceeds the maximum supported token budget",
)
})
})
.transpose()?,
memories: memories_config,
agent_interrupt_message_enabled,
codex_home,