Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ values, and existing thinking defaults remain unchanged. This also lets Codex
Desktop's low-effort background title generator prefer the launch-default model
instead of loading another checkpoint solely for its effort metadata.

Codex App configuration rewrites preserve user-set
`model_auto_compact_token_limit` and `model_post_turn_compact_threshold_percent`
in `config.toml`, including on subsequent launches. Set these to compact earlier
if local memory constraints require extra headroom. These are global Codex
settings, not per-model limits. Each catalog entry records the server's effective
context window as both its context window and override ceiling, with an explicit
auto-compaction threshold of 90% of that window (Codex's default). A lower
user-set compaction limit can trigger earlier compaction. Relaunch Codex after
changing server context limits; already-running sessions can retain older model
metadata.

Deferred tool loading is opt-in. Start the server with `--responses-tool-search`
(or set `server.responses_tool_search` to `true` in `settings.json`), then relaunch
Codex through oMLX to refresh its `supports_search_tool` catalog capability.
Expand Down
24 changes: 19 additions & 5 deletions omlx/integrations/codex.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,24 @@ def codex_model_catalog(
models_from_status = []
for model_id in model_ids:
info = ctx.models_status_map.get(model_id, {})
model = known_models.get(model_id) or IntegrationModel(id=model_id)
model = known_models.get(model_id)
if model is None:
model = (
IntegrationModel(
id=model_id,
context_window=ctx.context_window,
model_type=ctx.model_type,
reasoning=ctx.reasoning,
reasoning_effort_options=ctx.reasoning_effort_options,
reasoning_effort_default=ctx.reasoning_effort_default,
)
if model_id == ctx.model
else IntegrationModel(id=model_id)
)
models_from_status.append(
replace(
model,
context_window=info.get("max_context_window", model.context_window),
context_window=info.get("max_context_window") or model.context_window,
model_type=info.get("model_type", model.model_type),
reasoning=info.get("reasoning", model.reasoning),
reasoning_effort_options=tuple(
Expand Down Expand Up @@ -190,6 +203,8 @@ def codex_model_catalog(
"base_instructions": instructions,
"shell_type": "shell_command",
"context_window": context_window,
"max_context_window": context_window,
"auto_compact_token_limit": context_window * 9 // 10,
"input_modalities": (
["text", "image"] if model.model_type == "vlm" else ["text"]
),
Expand Down Expand Up @@ -457,16 +472,15 @@ def write_codex_config(
in_any_section = False
in_omlx_section = False

# Model-specific limits and capabilities belong in the catalog so switching
# models does not retain the launch model's context or thinking settings.
# Context and capabilities belong in the catalog; preserve user compaction
# limits, which may reserve extra headroom for local memory constraints.
top_level_overrides = {
"model": json.dumps(ctx.model or "select-a-model", ensure_ascii=False),
"model_provider": '"omlx"',
"model_catalog_json": json.dumps(str(catalog_path), ensure_ascii=False),
}
managed_keys = {
"model_context_window",
"model_auto_compact_token_limit",
"model_reasoning_effort",
"model_reasoning_summary",
"model_supports_reasoning_summaries",
Expand Down
82 changes: 81 additions & 1 deletion tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,54 @@ def test_preserves_ids_and_per_model_capabilities(self):
assert all(model["base_instructions"] for model in models)
assert all(not model["prefer_websockets"] for model in models)

def test_context_and_compaction_limits_follow_each_served_model(self):
catalog = codex_model_catalog(
ctx(
model="flash-next",
context_window=262144,
models=(
IntegrationModel("flash-next", 262144, "llm"),
IntegrationModel("small", 8192, "llm"),
),
models_status_map={
"flash-next": {
"model_context_length": 262144,
"max_context_window": 204800,
},
},
),
["flash-next", "small"],
)

assert [
(
model["slug"],
model["context_window"],
model["max_context_window"],
model["auto_compact_token_limit"],
)
for model in catalog["models"]
] == [
("flash-next", 204800, 204800, 184320),
("small", 8192, 8192, 7372),
]

@pytest.mark.parametrize("status", [{}, {"max_context_window": None}])
def test_selected_model_context_survives_catalog_reconstruction(self, status):
catalog = codex_model_catalog(
ctx(
model="flash-next",
context_window=204800,
models_status_map={"flash-next": status},
),
["flash-next", "unknown"],
)

assert [model["context_window"] for model in catalog["models"]] == [
204800,
32768,
]

@pytest.mark.parametrize("listed", [True, False])
@pytest.mark.parametrize("reasoning", [True, False, None])
def test_models_without_native_efforts_advertise_compatibility_levels(
Expand Down Expand Up @@ -662,7 +710,6 @@ def test_writes_catalog_and_clears_global_model_overrides(self, tmp_path):
config_path = tmp_path / "config.toml"
config_path.write_text(
"model_context_window = 999999\n"
"model_auto_compact_token_limit = 900000\n"
'model_reasoning_effort = "high"\n'
'model_reasoning_summary = "auto"\n'
"model_supports_reasoning_summaries = true\n"
Expand Down Expand Up @@ -691,6 +738,39 @@ def test_writes_catalog_and_clears_global_model_overrides(self, tmp_path):
assert config["model_providers"]["omlx"]["wire_api"] == "responses"
assert config["model_providers"]["omlx"]["supports_websockets"] is False

def test_preserves_user_compaction_settings_across_reconfiguration(self, tmp_path):
config_path = tmp_path / "config.toml"
compaction_settings = (
"model_auto_compact_token_limit = 200000 # user safety margin\n"
"model_post_turn_compact_threshold_percent = 80\n"
)
config_path.write_text(
compaction_settings + '[model_providers.other]\nname = "Other"\n'
)

for model, context_window in (("first", 262144), ("second", 204800)):
write_codex_config(
config_path,
ctx(
model=model,
models=(IntegrationModel(model, context_window, "llm"),),
),
)

content = config_path.read_text()
config = tomllib.loads(content)
assert compaction_settings in content
assert config["model_auto_compact_token_limit"] == 200000
assert config["model_post_turn_compact_threshold_percent"] == 80
assert config["model_providers"]["other"]["name"] == "Other"
catalog = json.loads(Path(config["model_catalog_json"]).read_text())
assert catalog["models"][0]["context_window"] == context_window
assert catalog["models"][0]["max_context_window"] == context_window
assert (
catalog["models"][0]["auto_compact_token_limit"]
== context_window * 9 // 10
)

def test_backup_failure_does_not_overwrite_config(self, tmp_path):
config_path = tmp_path / "config.toml"
config_path.write_text('model = "old"\n')
Expand Down
Loading