mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
fix/plugin-launcher-isolated-python
28
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
8f50884bd5 |
fix(init): handle non-UTF-8 directory names when deriving the init profile (#4673)
## Description
`headroom init` derives a profile id in `_local_profile` with
`sha1(str(root).encode("utf-8"))`. On POSIX a directory name can hold
bytes that are not valid UTF-8; pathlib represents them as surrogate
escapes, and `.encode("utf-8")` raises `UnicodeEncodeError`. The fix
falls back to `os.fsencode(root)` only when UTF-8 encoding fails, so
existing profile ids for normal paths are unchanged and distinct byte
names stay distinct.
Closes #4267
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
## Changes Made
- `headroom/cli/init.py`: UTF-8 first, `os.fsencode` fallback in
`_local_profile`.
- `tests/test_cli/test_init_cli.py`: regression test using two distinct
non-UTF-8 byte names (POSIX only; fails on `main`, passes now).
## Testing
- [x] Unit tests pass: `tests/test_cli/test_init_cli.py`, 97 passed (1
failed on `main` with the new test)
- [x] Linting passes (`ruff check` / `ruff format --check` on touched
files)
- [ ] Type checking (`mypy headroom`): not run
- [x] New tests added
## Real Behavior Proof
- Environment: macOS, Python 3.12.
- Exact command / steps: called `headroom.cli.init._local_profile` on
`Path(os.fsdecode(b"/nonexistent/proj-\xff"))`, `...\xfe`, and a normal
UTF-8 name.
- Observed result: before, `UnicodeEncodeError: 'utf-8' codec can't
encode character '\udcff'`; after, three valid, distinct
`init-proj-<hash>` ids.
- Not tested: the actual `headroom init hook ensure` command in a real
non-UTF-8 directory (macOS/APFS cannot create such a directory, so the
paths above do not exist on disk), Linux, Windows, mypy, full suite,
pre-push hooks.
## Test Output
Author-reported results (not rerun by the maintainer for this body
clarification): `tests/test_cli/test_init_cli.py`: 97 passed; the new
regression failed on main before the fix. Scoped Ruff check and format
check passed. The Real Behavior Proof section above records the
helper-level before/after result and its untested scope.
## Runtime Rollout Safety
- Rollout-managed feature(s): none; this is a profile-ID derivation bug
fix in `headroom init`.
- Minimum rollout channel: no new rollout channel or gate.
- Stable/default behavior changed: paths that can be encoded as UTF-8
retain the existing digest input. Only `UnicodeEncodeError` selects
filesystem bytes for the digest.
- Kill switch / disable path: revert the fallback change; this restores
the previous error for the affected non-UTF-8 paths.
- Unsafe override required: no.
- Qualification impact: the reported helper regression covers distinct
surrogate-escaped byte names. Actual init/hook execution in a real
non-UTF-8 directory and broader platform behavior remain outside the
reported proof.
- Rollback path: revert the fallback change. This PR does not migrate
existing profiles or rewrite stored profile IDs.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] I did **not** edit `CHANGELOG.md`
This change was prepared with AI assistance (Claude Code).
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4673"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
|
||
|
|
4a851ed254 |
fix(codex): accept multi-line inline tables in Codex config.toml (#4436)
## Description
Codex accepts an inline table split across lines, with a trailing comma,
in `~/.codex/config.toml`:
```toml
env = {
DINGTALK_Client_ID = "**********",
}
```
That is TOML 1.1 syntax. Headroom reads the Codex config with the
standard library `tomllib`, which only implements TOML 1.0 before Python
3.15, so it rejects the file with `Invalid initial character for a key
part`. The result is what the issue shows: `headroom wrap codex` and
`headroom mcp install` refuse to register MCP servers ("is not valid
TOML ... refusing to overwrite") and session routing fails with "could
not read Codex config for session routing", even though Codex itself is
happy with the file.
`tomlkit` is already a runtime dependency (used for Codex config
recovery) and parses this syntax from 0.15.0. 0.13.3 and 0.14.0 both
fail on it (`Empty key at line 1 col 7`).
The fix adds one helper, `loads_codex_config()`, that parses with
`tomllib` first and only on `TOMLDecodeError` retries with `tomlkit`. If
`tomlkit` rejects the text too, it re-raises the original
`tomllib.TOMLDecodeError`, so every caller keeps its existing `except
tomllib.TOMLDecodeError` handling and its error messages unchanged. All
places that parse the user's Codex config now go through it.
The follow-up commit (`dc193870`) fixes a problem the first commit
exposed: once a multi-line inline table parses, a `profiles = {` table
can carry per-profile `model_provider` / `openai_base_url` keys on their
own indented lines, and the line-based "root key" edits in init, the
provider-scope install and persistent wrap mistook those nested lines
for root assignments. Init and install deleted the profile's provider,
and wrap rewrote it to `headroom` and then skipped adding a real root
`model_provider`. Those edits now only touch lines that are actual root
assignments.
Fixes #4378
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/providers/codex/config_toml.py` (new):
`loads_codex_config(content)`, `tomllib` first, `tomlkit` fallback,
original `tomllib` error if both fail.
- Routed the Codex config parse sites through it (15 call sites, 16
parse calls):
- `headroom/cli/wrap.py`: session routing read, the user-content check
and its fallback in `_inject_codex_provider_config`, and the two "never
write a malformed candidate" checks.
- `headroom/cli/init.py`: provider snapshot check, the candidate check
in `_ensure_codex_provider`, and `_codex_features`.
- `headroom/providers/codex/install.py`: `_codex_credential_store`,
`codex_auth_helper_is_referenced`, and the merged-config check before
writing.
- `headroom/providers/codex/project_context.py`: the `sqlite_home`
lookup.
- `headroom/mcp_registry/codex.py`: `_load_toml`, `_unmergeable_reason`,
and both sides of `_only_server_changed`. This registrar class is shared
with Grok, so a Grok `config.toml` with the same syntax is accepted too.
- Left on `tomllib` on purpose: `_table_path` (parses a single `[table]`
header line) and the auth snippet check in `build_codex_auth_config`
(validates text Headroom generates itself).
- `pyproject.toml` / `uv.lock`: raise the `tomlkit` minimum from
`>=0.13.0` to `>=0.15.0`, the first release that parses multi-line
inline tables. The lock already resolved 0.15.0, so `uv lock` only
changed the specifier line.
- Write paths: the user's text is never re-serialized by this change.
The registrar and the wrap/init injectors still append or splice marker
blocks as text, and the existing `tomlkit` round-trip in wrap keeps the
multi-line table byte for byte (checked below).
- Tests:
- `tests/test_provider_codex_config_toml.py`: the helper on the exact
shape from the issue (multi-line, trailing comma, whitespace-only line,
indented `}`), and that invalid TOML still raises
`tomllib.TOMLDecodeError`.
-
`tests/test_mcp_registry/test_codex_registrar.py::test_register_accepts_multiline_inline_table`:
registering into that config succeeds and the user's text is kept as a
byte-for-byte prefix.
-
`tests/test_cli/test_wrap_codex.py::test_codex_session_launch_settings_accept_multiline_inline_table`:
session routing reads that config and leaves the file untouched.
- Follow-up commit `dc193870` (multi-line inline profile overrides):
- `headroom/providers/codex/config_toml.py`:
`root_assignment_lines(content)` returns the indexes of lines that start
a key/value pair at the document root. It stops at the first table
header and skips lines inside a multi-line inline table, array or
string, with a small scanner that tracks quotes, comments and bracket
depth.
- `headroom/cli/init.py` (`_ensure_codex_provider`) and
`headroom/providers/codex/install.py`
(`_strip_root_provider_assignments`) only drop `model_provider` /
`openai_base_url` lines that are root assignments.
- `headroom/cli/wrap.py`: `_redirect_existing_top_level_keys` and
`_has_redirectable_top_level_key` only look at root assignments. Before,
they matched the key on any line, so a `[profiles.work]` section
override was also rewritten to `headroom` and counted as a root key;
that now stays the user's, matching how init, install and
`_strip_codex_headroom_blocks` already treat `[profiles.*]`.
- Tests:
`test_root_assignment_lines_skip_keys_nested_in_multiline_inline_table`,
`test_ensure_codex_provider_preserves_multiline_inline_profile_overrides`
(init),
`test_provider_scope_preserves_multiline_inline_profile_overrides`
(install) and `test_inject_preserves_multiline_inline_profile_overrides`
(wrap).
## Testing
- [x] Unit tests pass (`pytest`) - Codex, MCP registry, init and Grok
suites: 774 passed, 2 failed (the 2 failures are in
`tests/test_codex_responses_passthrough_bytes.py` and fail the same way
on `main` without this change)
- [x] Linting passes (`ruff check .`) - `ruff check` and `ruff format
--check` on the 9 changed Python files
- [x] Type checking passes (`mypy headroom`) - `mypy` on the 6 changed
source files, no issues
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
New end-to-end tests on `main` (fix stashed):
```text
$ pytest tests/test_mcp_registry/test_codex_registrar.py::test_register_accepts_multiline_inline_table tests/test_cli/test_wrap_codex.py::test_codex_session_launch_settings_accept_multiline_inline_table -q
E AssertionError: .../.codex/config.toml is not valid TOML (Invalid initial character for a key part (at line 3, column 8)); refusing to overwrite. Fix or remove the file, then re-run.
E click.exceptions.ClickException: could not read Codex config for session routing: Invalid initial character for a key part (at line 5, column 8)
============================== 2 failed in 0.38s ===============================
```
(`tests/test_provider_codex_config_toml.py` cannot import on `main`
because the helper does not exist yet.)
With the fix:
```text
$ pytest tests/test_provider_codex_config_toml.py tests/test_mcp_registry/test_codex_registrar.py::test_register_accepts_multiline_inline_table tests/test_cli/test_wrap_codex.py::test_codex_session_launch_settings_accept_multiline_inline_table -q
============================== 4 passed in 0.56s ===============================
$ pytest tests/test_provider_codex_config_toml.py tests/test_mcp_registry tests/test_cli/test_wrap_codex.py tests/test_cli/test_init_cli.py tests/test_install/test_codex_install.py tests/test_provider_codex*.py tests/test_provider_grok*.py tests/test_codex*.py -q
FAILED tests/test_codex_responses_passthrough_bytes.py::test_unmutated_zstd_post_forwards_decoded_bytes_and_strips_content_encoding - concurrent.futures._base.CancelledError
FAILED tests/test_codex_responses_passthrough_bytes.py::test_unmutated_plain_post_passes_original_bytes_through - concurrent.futures._base.CancelledError
================== 2 failed, 774 passed, 8 warnings in 50.43s ==================
$ pytest tests/test_codex_responses_passthrough_bytes.py -q # on main, without this change
========================= 2 failed, 1 warning in 4.09s =========================
$ mypy headroom/cli/init.py headroom/cli/wrap.py headroom/mcp_registry/codex.py headroom/providers/codex/install.py headroom/providers/codex/project_context.py headroom/providers/codex/config_toml.py
Success: no issues found in 6 source files
```
Follow-up commit, the three new write-path tests on the first commit
(`dfe93720`, source changes reverted):
```text
$ pytest tests/test_cli/test_init_cli.py tests/test_install/test_codex_install.py tests/test_cli/test_wrap_codex.py -k multiline_inline_profile -q
FAILED tests/test_cli/test_init_cli.py::test_ensure_codex_provider_preserves_multiline_inline_profile_overrides - AssertionError: assert {} == {'model_provi...y.example/v1'}
FAILED tests/test_install/test_codex_install.py::test_provider_scope_preserves_multiline_inline_profile_overrides - AssertionError: assert {} == {'model_provider': 'company'}
FAILED tests/test_cli/test_wrap_codex.py::TestSubscriptionRouting::test_inject_preserves_multiline_inline_profile_overrides - KeyError: 'model_provider'
====================== 3 failed, 220 deselected in 0.56s =======================
```
With `dc193870`:
```text
$ pytest tests/test_provider_codex_config_toml.py tests/test_cli/test_init_cli.py tests/test_install/test_codex_install.py tests/test_cli/test_wrap_codex.py -k "multiline_inline_profile or root_assignment" -q
======================= 4 passed, 222 deselected in 0.27s =======================
$ pytest tests/test_provider_codex_config_toml.py tests/test_cli/test_init_cli.py tests/test_install/ -q
================== 346 passed, 13 skipped, 1 warning in 3.94s ==================
$ pytest tests/test_cli/test_wrap_codex.py tests/test_cli/test_port_alignment.py -q
============================= 167 passed in 6.01s ==============================
$ pytest tests/test_mcp_registry/ tests/test_provider_codex_install.py tests/test_cli/test_install_cli.py tests/test_cli/test_recover_codex.py -q
============================= 364 passed in 4.71s ==============================
$ pytest tests/test_cli/test_wrap_persistent.py tests/test_codex_project_context.py tests/test_cli/test_wrap_stale_marker.py tests/test_cli/test_wrap_dead_marker_selfheal.py tests/test_provider_codex_runtime.py tests/test_install_tool_env_reconcile.py -q
======================== 4 failed, 261 passed in 9.01s =========================
```
The 4 failures are in `tests/test_cli/test_wrap_persistent.py`
(`AttributeError: '_Manifest' object has no attribute 'runtime_kind'`)
and fail the same way with this PR's changes stashed. `ruff check`,
`ruff format --check` (8 files) and `mypy` on the 4 changed source files
are clean.
tomlkit versions on the issue's snippet:
```text
0.13.3 FAIL Empty key at line 1 col 7
0.14.0 FAIL Empty key at line 1 col 7
0.15.0 {'env': {'A': 'x'}}
```
## Real Behavior Proof
- Environment: macOS 26.2 (Apple Silicon), Python 3.14.7, repo venv with
the `mcp` package installed, a throwaway `HOME`/`CODEX_HOME` whose
`config.toml` holds the issue's config:
```toml
[mcp_servers.dingtalk]
command = "npx"
env = {
DINGTALK_Client_ID = "**********",
}
```
- Exact command / steps: `HOME=<tmp>/home CODEX_HOME=<tmp>/home/.codex
python -m headroom.cli mcp install --agent codex`, then compare the
start of the resulting file with the original bytes and count
`[mcp_servers.headroom]` tables. Also called
`wrap._inject_codex_provider_config(8787)` and
`init._ensure_codex_provider(path, 8787)` on the same config to check
the write paths.
- Observed result: before (`main`):
```text
codex: install failed (failed): <tmp>/home/.codex/config.toml is not
valid TOML (Invalid initial character for a key part (at line 3, column
8)); refusing to overwrite. Fix or remove the file, then re-run.
```
After:
```text
codex: registered (restart the agent if it was already running)
```
The user's original bytes are an exact prefix of the new file and one
`[mcp_servers.headroom]` table was appended. The wrap injector printed
`Codex config: injected Headroom provider (WS + HTTP)`, and in both the
wrap and init outputs the user's multi-line table is still present
verbatim, the file parses, `env` is `{'DINGTALK_Client_ID':
'**********'}` and `model_provider` is `headroom`.
- Follow-up commit: with a throwaway `HOME`/`CODEX_HOME` holding
```toml
model_provider = "openai"
profiles = {
work = {
model_provider = "company",
},
}
```
I ran `init._ensure_codex_provider`, `install.apply_provider_scope`
(provider scope) and `wrap._inject_codex_provider_config` (same config
without the root key) and parsed the result:
```text
== before (
|
||
|
|
976aa714ef |
fix(cli): name the broken Codex config, write init configs atomically, keep the identity check (#4129)
## Description
This PR fixes four small CLI problems.
1. **`wrap codex` with a broken `~/.codex/config.toml`.** The warning
looked like this:
```
Warning: could not update Codex config: Invalid value (at line 6, column
10)
```
The line number pointed into Headroom's own merged text, not the user's
file, and the file was never named. The warning now names the file,
gives the parse error in the user's own lines, and says what to do. The
generic Codex and Grok config warnings now also name the file.
2. **`headroom init` truncated config files while rewriting them.** It
rewrote `~/.claude/settings.json`, Codex `hooks.json` and
`~/.codex/config.toml` with `Path.write_text`. That truncates the file
before writing, so a crash or ENOSPC mid-write left a half-written file.
`init` now uses `fsutil.write_text`, the atomic writer `wrap` already
uses for these same files.
3. **The runtime identity check could be silently skipped.** `init` and
`wrap` each had a `_wait_for_runtime_ready` wrapper. It caught
`TypeError` "for test doubles" and then retried `wait_ready` without
`require_identity=True`. So any real `TypeError` inside `wait_ready`
dropped the runtime identity check. Both wrappers are removed, and
callers now pass `require_identity=True` directly.
4. **A failed hook recovery left no trace.** `_ensure_profile_running`
(the init hook's recovery path) ended in `except Exception: return`. It
now catches only what the start paths raise (`click.ClickException`,
`RuntimeError`, `OSError`, `subprocess.SubprocessError`) and logs one
WARNING after the suppressed-output block: `headroom: could not start
persistent proxy '<profile>': <error>. Check it with `headroom install
status --profile <profile>`.` A start that succeeds but never becomes
ready within 45s logs the same line with `it did not become ready within
45s` as the reason. It goes to stderr; stdout stays clean for the hook.
Any other exception is a bug and now surfaces.
Refs #4136
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/cli/wrap.py` `_inject_codex_provider_config`: it now parses
the user's own content, with Headroom blocks stripped, before building
the candidate.
- If that content does not parse, it warns `could not update Codex
config <path>: it is not valid TOML (<user's own parse error>); Codex
WebSocket traffic will bypass Headroom. Fix or remove the file, then
re-run.`
- If the content parses only once the Headroom block is removed, the old
repair path runs instead.
- The snapshot still happens first, so `unwrap codex` restores the file
exactly as before.
- `headroom/cli/wrap.py`: the generic Codex and Grok config warnings now
include the file path (`grok_config_paths()`).
- `headroom/cli/init.py`: `_write_json`, `_ensure_codex_provider` and
the Codex feature-block writer now call `fsutil.write_text`.
- `headroom/cli/init.py` and `headroom/cli/wrap.py`: removed
`_wait_for_runtime_ready`; callers call `wait_ready(...,
require_identity=True)` directly.
- `headroom/cli/init.py` `_ensure_profile_running`: narrowed `except
Exception: return` to the start-path exceptions and log the failure
once, as above.
- Tests:
- New `test_invalid_user_config_is_named_and_left_untouched` in
`test_wrap_codex.py`.
- New `test_write_json_keeps_the_existing_file_when_the_write_fails` in
`test_init_cli.py`. It simulates ENOSPC at fsync and checks that the
original file survives and no temp file is left.
- New
`test_ensure_profile_running_never_retries_wait_ready_without_identity`:
its `wait_ready` double raises `TypeError` whenever `require_identity`
is passed. It asserts the error propagates and `wait_ready` was called
once, with `require_identity=True`. It fails if the fallback comes back.
- New `test_ensure_profile_running_logs_a_failed_recovery`: no log on a
clean start; exactly the one WARNING above when `start_supervisor`
raises.
- New
`test_ensure_profile_running_logs_a_recovery_that_never_becomes_ready`:
the start succeeds, the 45s `wait_ready` returns False, and the WARNING
names that reason.
- The `wait_ready` doubles in `test_init_cli.py` and
`test_wrap_persistent.py` now accept, or assert, `require_identity`;
`test_recover_persistent_proxy_routes_docker_task_to_docker_supervisor`
asserts `require_identity=True`.
- All four new tests fail on origin/main and pass here.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ pytest -q -p no:cacheprovider tests/test_cli/test_init_cli.py tests/test_cli/test_wrap_persistent.py tests/test_cli/test_wrap_codex.py tests/test_cli/test_wrap_grok.py tests/test_cli/test_init_enable_tool_search.py tests/test_cli/test_install_cli.py tests/test_install/test_providers.py tests/test_install/test_runtime.py tests/test_plugin_manifests.py tests/test_cli/test_wrap_helpers.py tests/test_cli/test_port_alignment.py
4 failed, 595 passed in 29.13s
# The 4 failures are in tests/test_cli/test_wrap_persistent.py ("'_Manifest' object has no attribute 'runtime_kind'"). They are pre-existing: clean origin/main gives "4 failed, 59 passed" for that file.
# The new tests against origin/main code:
test_invalid_user_config_is_named_and_left_untouched: 1 failed (old message names no file)
test_write_json_keeps_the_existing_file_when_the_write_fails: 1 failed (DID NOT RAISE: the old write_text never fsyncs and overwrote the file)
test_ensure_profile_running_never_retries_wait_ready_without_identity: 1 failed (DID NOT RAISE: the fallback retried without require_identity)
test_ensure_profile_running_logs_a_failed_recovery: 1 failed (no log record)
$ uvx ruff@0.16.8 check headroom/cli/init.py headroom/cli/wrap.py tests/test_cli/test_init_cli.py tests/test_cli/test_wrap_persistent.py tests/test_cli/test_wrap_codex.py
All checks passed!
$ mypy headroom/cli/init.py headroom/cli/wrap.py --ignore-missing-imports
Success: no issues found in 2 source files
```
## Real Behavior Proof
- Environment: macOS 25.6, Python 3.12, repo `.venv`.
- Exact command / steps:
1. Ran `python -m headroom.cli wrap codex --prepare-only` with a scratch
`HOME` whose `.codex/config.toml` is `model = [unterminated`. This is
the audit's reproduction, and the new test covers it.
2. Called `_recover_persistent_proxy` and `_restart_persistent_proxy`
directly, with the supervisor and `wait_ready` stubbed, and recorded the
arguments `wait_ready` received.
- Observed result:
1. Before: `Warning: could not update Codex config: Invalid value (at
line 6, column 10)`. After: the warning names
`<HOME>/.codex/config.toml`, reports the error at line 1 (the user's
line), and tells the user to fix or remove the file.
2. Both calls passed `{'timeout_seconds': 45, 'require_identity':
True}`.
- Smoke harness (real proxy started from this checkout against a local
fake upstream; temp HOME and workspace, offline):
```text
branch: cleanup/cli-integrations-config-errors-atomic-init @
|
||
|
|
a6523b1967 |
fix(codex): preserve auth.json API keys through Headroom (#3355)
## Description Closes #3354. When Codex uses an OpenAI API key stored in auth.json, Headroom's generated provider block did not configure authentication. Codex therefore sent requests to the local Headroom proxy without an Authorization header and received 401 Missing bearer. This is separate from #3206: that issue covers ChatGPT OAuth authentication, while this change covers file-backed API-key authentication. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature - [ ] Breaking change - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring ## Changes Made - Resolve API-key and OAuth credentials from `codex_home_dir()` for both project and user configurations, honoring `CODEX_HOME`. - Generate a private, config-specific helper beside `auth.json`; API keys stay out of generated TOML and Headroom environment variables. - Configure Codex's supported `auth.command` field for persistent install, init, and wrap. - Serialize command and argument paths with literal Unicode and validate generated TOML before writing provider configurations. - Abort provider updates with an actionable error if a helper collides with another file/symlink or cannot be created. - Use exclusive creation, 0600 permissions on Unix, and content checks for helper ownership. - Give project and user configurations independent helper lifetimes. Retain legacy shared helpers and helpers referenced by restored/remaining providers or unparseable configurations. - Preserve unrelated files and the original provider config when authentication setup fails. - Remove an unused config-specific helper on uninstall/unwrap. ## Testing - [x] Focused unit and integration tests pass - [x] Linting and formatting pass - [ ] Type checking - [x] New regression tests added - [x] Manual runtime testing performed ### Test Output ```text $ .venv/bin/python -m pytest tests/test_provider_codex_install.py tests/test_install/test_codex_install.py tests/test_cli/test_init_cli.py tests/test_install/test_providers.py tests/test_cli/test_wrap_codex.py -q 284 passed $ .venv/bin/ruff check . All checks passed! $ .venv/bin/ruff format --check . 1744 files already formatted ``` Ruff 0.16.8 matches this branch's CI pin. A focused coverage run reports 93% line/branch coverage for `headroom/providers/codex/install.py` and covers 44/45 newly added executable lines across the three production files in this follow-up. This is local focused coverage, not a claim about the hosted Codecov aggregate. Before the follow-up implementation, the added local-init/Unicode/helper-isolation regressions produced 6 failures while 118 existing tests passed. ## Real Behavior Proof - Environment: macOS 26.6.2, Apple Silicon arm64, Python 3.12.11, Headroom source from this PR, Codex CLI 0.160.0, Sub2API OpenAI-compatible provider, model `gpt-6-luna`, existing API key in the user's Codex `auth.json`. - Setup: start an isolated Headroom proxy on an unused loopback port with `ProxyConfig(optimize=False, cache_enabled=False, rate_limit_enabled=False, openai_api_url=<existing upstream>)`. Generate a temporary provider config with `_ensure_codex_provider(config_path, port)` while credentials resolve from the user's Codex home. Add `model = "gpt-6-luna"` and `model_reasoning_effort = "low"` at the document root. Run the child with its own temporary `CODEX_HOME` and no `OPENAI_API_KEY` or `OPENAI_BASE_URL` environment variable; its home contains the generated config, not a copied credential file. - Exact child command: ```sh CODEX_HOME=<temporary-home> codex exec --ephemeral --skip-git-repo-check --sandbox read-only --json -C <temporary-workspace> 'Reply with exactly OK. Do not call tools.' ``` Captured after-fix output on 2026-10-07: ```text Headroom test proxy: ready (optimization disabled) Codex: 0.160.0; model: gpt-6-luna; auth: generated command, no API-key environment variable codex exec exit: 0 agent messages: ['OK'] Responses bearer matches auth.json: True ``` The last line comes from an ASGI wrapper that compares the HTTP/WebSocket Responses request bearer with the file-backed key without logging either value. The command completed against the real upstream; no mocked model response was used. The temporary provider config/helper and test proxy were removed after validation. - Observed limitation: this upstream rejects WebSocket negotiation with HTTP 426, so Headroom's existing HTTP streaming fallback completed the request. This run proves file-backed command authentication and successful proxy routing, not a successful upstream WebSocket session. - Not tested in this follow-up: a new Windows/Linux runtime run, full test suite, native-extension rebuild, or optimization behavior. The focused Python suite reused the existing local native extension; no Rust code changed. ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: N/A. - Stable/default behavior changed: Yes, only when Codex has a file-backed API key and Headroom writes a provider block. - Kill switch / disable path: Remove the Headroom-managed provider block or run the existing Codex unwrap command. - Unsafe override required: No. - Qualification impact: None. - Rollback path: Revert the generated provider block; auth.json is unchanged. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of the code - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective - [x] New and existing unit tests pass locally with my changes - [x] I did not edit CHANGELOG.md ## Additional Notes Helpers contain no credential value and read the user's existing credential file at runtime, so key rotation does not require reinstalling. Helper names now include a hash of the owning config's canonical path. Legacy fixed-name helpers are retained conservatively because other project configurations may still use them. <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3355" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: JD Davis <mxjerrett@gmail.com> |
||
|
|
326079517b |
fix(codex): make unwrap codex remove headroom init routing (#3920)
## Description `headroom init -g codex` writes a block into `~/.codex/config.toml` that points Codex at the local proxy: ```toml # --- Headroom init provider --- model_provider = "headroom" openai_base_url = "http://127.0.0.1:8787/v1" ... # --- end Headroom init provider --- ``` There was no command to take it out again. `headroom unwrap codex` only recognises the markers that `headroom wrap codex` writes, so on an init-written config it prints "Nothing to undo" and then "✓ Codex is no longer routed through the Headroom proxy." The routing is still there. This matters most on Windows. `init` relies on Codex session hooks to start the proxy, and those hooks are disabled upstream on Windows (init already prints a warning about that). So after a reboot or a proxy crash, Codex keeps calling `127.0.0.1:8787`, nothing restarts the proxy, and the obvious undo command claims it worked. That matches the symptom in #3749: Codex stuck on a dead local proxy until the override was deleted by hand. This PR makes `unwrap codex` remove the init block. It also makes the Windows warning say what it means for the user, and documents how to remove init routing. Refs #3749. It doesn't close the issue, which also asks wider questions about the Windows lifecycle; I'll answer those on the issue. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `headroom/cli/wrap.py` (`_restore_codex_provider_config`): if the config contains the `# --- Headroom init provider ---` marker, strip it with the existing (already tested) `init._strip_codex_init_block` before the usual wrap cleanup. The existing `cleaned` / `removed` / thread re-tag flow then runs unchanged. `_codex_config_has_headroom_markers` is deliberately not changed, because wrap's pre-wrap backup logic also uses it. - `headroom/cli/wrap.py` (`_restore_codex_provider_config`, backup branch): a pre-wrap snapshot taken *after* `headroom init codex` carries init's block, so restoring it verbatim left Codex pinned to the proxy while unwrap printed "Restored". The snapshot is now passed through `_strip_codex_init_block` too (and the config removed if nothing else is left). The snapshot is read first and deleted only after the config has been written, so a failed write leaves it in place for a retry instead of losing it. Review findings from @JerrettDavis / Devin. - `headroom/cli/init.py` (new `_strip_codex_root_routing_orphans`): the orphan-key safety net (`model_provider = "headroom"`, loopback `openai_base_url`) in both `_strip_codex_init_block` and wrap's `_strip_codex_headroom_blocks` used to match anywhere in the file, so a newly-reachable unwrap would delete user-owned `[profiles.*]` overrides. It now only touches the document root, using the same first-table split `_ensure_codex_provider` already uses. Neither writer ever puts these keys inside a table. Review finding from @JerrettDavis / Devin. - `headroom/cli/init.py` (`_init_codex`): on Windows, after the existing hook warning, add one message. It says nothing starts the proxy there, and points to `headroom install apply` for a supervised proxy and to `headroom unwrap codex` (or deleting the project block) to remove the routing. - `docs/content/docs/proxy.mdx`: the "routing is persistent until you remove it" sentence now says how to remove it, that unwrap leaves init's Codex `SessionStart` hook in `hooks.json` (routing only), plus the Windows caveat. - Tests: five new `unwrap codex` cases: init block plus user content (user content preserved), init-only file (file removed), init → `wrap codex --prepare-only` → unwrap (snapshot restored without init routing), the same sequence with the first unwrap's config write failing (snapshot kept, a second unwrap restores it cleanly), and init → unwrap with a `[profiles.work]` that sets `model_provider = "headroom"` + a loopback `openai_base_url` (profile survives). One assertion added to the existing Windows init warning test. `TestSubscriptionRouting::test_strip_cleans_orphaned_openai_base_url` used a key inside `[profiles.default]` as its "orphan"; it now puts the orphan at the root and asserts a profile-level key survives. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output Both new tests fail on `main`; the failure reproduces the bug: ```text tests/test_cli/test_wrap_codex.py FF E AssertionError: assert 'Nothing to undo' not in '...' E Nothing to undo: ...\.codex\config.toml has no Headroom wrap markers. E ✓ Codex is no longer routed through the Headroom proxy. FAILED tests/test_cli/test_wrap_codex.py::test_unwrap_codex_removes_init_provider_routing FAILED tests/test_cli/test_wrap_codex.py::test_unwrap_codex_removes_init_only_config_file ``` With this branch: ```text $ pytest tests/test_cli/test_wrap_codex.py tests/test_cli/test_init_cli.py tests/test_cli/test_wrap_opencode.py -q -n 4 228 passed in 7.50s $ ruff check . All checks passed! $ ruff format --check headroom/cli/wrap.py headroom/cli/init.py tests/test_cli/test_wrap_codex.py tests/test_cli/test_init_cli.py 4 files already formatted $ mypy headroom --ignore-missing-imports --no-site-packages headroom\fileperms.py:71: error: Module has no attribute "fchmod" [attr-defined] Found 1 error in 1 file (checked 554 source files) ``` The mypy error is the Windows-only `os.fchmod` false positive in an untouched file; it passes on Linux CI. The full `tests/test_cli` suite on Windows: 907 passed, 12 failed. Five of those also fail on a clean `upstream/main` worktree with the same environment (`test_recover_codex` ×2, `test_wrap_stale_marker`, `test_unwrap_claude` ×2), so they're pre-existing Windows issues. The other seven (`test_wrap_persistent`, `test_install_apply_announces_windows_service_fallback`) only failed under `-n 6` and pass when run serially on this branch (63 passed). ### Review round (rebased on `upstream/main` |
||
|
|
6df4a963ae |
fix(codex): preserve remote compaction in init config (#3410)
## Description Closes #3407. `headroom init codex` writes a custom provider named `Headroom init proxy`. Codex 0.152.1 derives remote-compaction capability from the provider display name, so this configuration routes ordinary Responses traffic through Headroom but silently falls back to local compaction. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (breaking change that changes existing functionality) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - Keep the `headroom` provider id and loopback URLs unchanged for routing. - Emit the built-in `OpenAI` provider display name so Codex retains its remote-compaction capability detection. - Add a regression assertion covering the generated Codex configuration. ## Testing - [x] Focused unit tests pass (`tests/test_cli/test_init_cli.py`) - [x] Focused linting passes (`ruff check`) - [x] Focused formatting passes (`ruff format --check`) - [x] New regression coverage added - [ ] Full test suite - [ ] Live Codex 0.152.1 compaction run ### Test Output ```text PYTHONPATH=. python -m pytest tests/test_cli/test_init_cli.py -q 73 passed in 22.15s ruff check headroom/cli/init.py tests/test_cli/test_init_cli.py All checks passed! ruff format --check headroom/cli/init.py tests/test_cli/test_init_cli.py 2 files already formatted ``` The repository's documented `uv run --frozen --extra dev` setup was also attempted, but dependency resolution stopped while building `hnswlib==0.8.0`: its build environment raised `AttributeError: module 'pkgutil' has no attribute 'ImpImporter'`. The focused tests and checks ran successfully with the already-installed Python 3.11 environment. ## Real Behavior Proof - Environment: Windows 10, Python 3.11.9, generated temporary Codex configuration. - Exact command / steps: invoked `_ensure_codex_provider` against a temporary config with port `8787`, parsed the result with Python `tomllib`, and checked the provider id, display name, and loopback base URL. - Observed result: the generated `[model_providers.headroom]` section contains `name = "OpenAI"`, `base_url = "http://127.0.0.1:8787/v1"`, and `supports_websockets = true`; the root `model_provider` remains `headroom`. - Not tested: a live Codex 0.152.1 session against `/v1/responses/compact`, because Codex 0.152.1 is not installed in this environment. ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: Stable; this only changes the generated display name for new or regenerated init configurations. - Stable/default behavior changed: Codex-initialized Headroom configurations now advertise the OpenAI-compatible capability name expected by Codex while preserving the existing Headroom routing id and URLs. - Kill switch / disable path: Run `headroom unwrap codex` or revert the generated config. - Unsafe override required: No. - Qualification impact: Existing generated configs need to be regenerated with `headroom init codex` to receive the corrected name. - Rollback path: Revert this commit. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [x] I have added tests that prove my fix is effective - [x] Focused unit tests pass locally - [x] I did **not** edit `CHANGELOG.md` ## Additional Notes The provider remains registered as `headroom`; only its display name changes. This preserves Headroom's proxy URLs, WebSocket setting, and conditional `requires_openai_auth` behavior for both ChatGPT OAuth and API-key users. --------- Co-authored-by: UniversePeak <UniversePeak@users.noreply.github.com> |
||
|
|
f3d95ee275 |
fix(cli): keep non-ASCII working directory names out of the init profile slug (#3718)
## Description `headroom init` aborts with `Invalid profile name` from any working directory whose name is not pure ASCII. `_local_profile` (`headroom/cli/init.py:115`) builds its slug with `str.isalnum`, which is Unicode-aware and therefore keeps Cyrillic, CJK and accented Latin letters. `validate_profile_name` (`headroom/install/paths.py`) then rejects that slug against `^[A-Za-z0-9._-]+$`. This is not limited to Cyrillic as originally reported — `café-app` fails too, so it hits French, Spanish and Portuguese directory names as well. Closes #3467 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - `_local_profile` keeps only ASCII alphanumerics (plus `-._`) in the slug; everything else becomes `-`, as non-alphanumerics already did. - A name that slugs away entirely still falls back to `init-repo-<digest>`, and the sha1 of the full path still distinguishes two folders sharing a name. - ASCII directory names produce byte-identical output to before. - Regression tests in `tests/test_cli/test_init_cli.py`. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) — not run, see Not tested - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output New tests against **unpatched** `_local_profile` (fix stashed) — they fail: ```text $ python -m pytest tests/test_cli/test_init_cli.py -k local_profile -q FAILED tests/test_cli/test_init_cli.py::test_local_profile_accepts_non_ascii_directory_names[Мастеринг в HW] FAILED tests/test_cli/test_init_cli.py::test_local_profile_accepts_non_ascii_directory_names[项目] FAILED tests/test_cli/test_init_cli.py::test_local_profile_accepts_non_ascii_directory_names[caf\xe9-app] FAILED tests/test_cli/test_init_cli.py::test_local_profile_accepts_non_ascii_directory_names[\xdcnicode Projekt] FAILED tests/test_cli/test_init_cli.py::test_local_profile_distinguishes_identical_non_ascii_names 5 failed, 2 passed, 72 deselected, 1 warning in 0.34s ``` With the fix: ```text $ python -m pytest tests/test_cli/test_init_cli.py tests/test_install/test_paths.py -q 82 passed, 1 warning in 1.00s $ ruff check headroom/cli/init.py tests/test_cli/test_init_cli.py All checks passed! $ ruff format --check headroom/cli/init.py tests/test_cli/test_init_cli.py 2 files already formatted ``` ## Real Behavior Proof - Environment: macOS 15.6 (Darwin 25.6.0, arm64, M3 Pro), Python 3.12.14, headroom at `5ff4ea1` (main), APFS (case-insensitive, Unicode-preserving) - Exact command / steps: `mkdir -p "/tmp/hr demo/Мастеринг в HW" && cd "/tmp/hr demo/Мастеринг в HW" && python -c "from headroom.cli.init import _local_profile; print(_local_profile())"` — run once with the fix stashed and once with it applied; full form below. - Observed result: before the fix `ClickException: Invalid profile name 'init-мастеринг-в-hw-8db7d005'`; after the fix `init-hw-8db7d005`. Full transcript below. - Not tested: Windows (where it was reported), the full `headroom init hook ensure` CLI path, and `mypy`. Details below. Full form: ```bash mkdir -p "/tmp/hr demo/Мастеринг в HW" cd "/tmp/hr demo/Мастеринг в HW" python -c "from headroom.cli.init import _local_profile; print(_local_profile())" ``` Observed result: ```text cwd: /tmp/hr demo/Мастеринг в HW === BEFORE FIX === ClickException: Invalid profile name 'init-мастеринг-в-hw-8db7d005' === AFTER FIX === profile: init-hw-8db7d005 ``` The digest is identical across both runs (`8db7d005`), confirming only the slug changed and profile identity for a given path is otherwise preserved. Slug behaviour across scripts after the fix: ```text 'Мастеринг в HW' -> 'init-hw-080fa965' 'café-app' -> 'init-caf--app-b393d6a7' '项目' -> 'init-repo-5f1f15e6' 'Ünicode Projekt' -> 'init-nicode-projekt-b7b5d10e' 'my-repo' -> 'init-my-repo-c9d86e48' (unchanged by this patch) ``` Not tested, in detail: - Windows, which is where the issue was originally reported. I reproduced and fixed it on macOS; the code path is platform-independent (`Path.resolve().name`), but I have not run it on Windows. - The full `headroom init hook ensure` command end to end — this venv has the pure-Python dependencies only, not the compiled Rust extension, so I exercised `_local_profile` directly rather than through the CLI entry point. - `mypy`, for the same reason (the package does not fully import in this environment). ## Runtime Rollout Safety - Rollout-managed feature(s): none — this is a pure function used during `init`, behind no rollout flag. - Minimum rollout channel: n/a — not rollout-managed. - Stable/default behavior changed: only for directories that previously raised `Invalid profile name`; ASCII directory names are byte-identical. - Kill switch / disable path: n/a — no flag to disable; `--profile <name>` remains the explicit override it always was. - Unsafe override required: no. - Qualification impact: none — no qualification surface is touched. - Rollback path: revert this commit; slug generation returns to the previous behaviour. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation — n/a, no user-facing docs describe the slug format - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` ## Additional Notes Users with an existing profile for a non-ASCII directory cannot exist, since `init` could never create one there — so there is no migration concern. `café-app` slugs to `caf--app` (a doubled dash where `é` was). Valid per the regex and I kept the transformation minimal rather than collapsing dash runs, which would change output for ASCII names too. Happy to collapse them if you would prefer. AI assistance was used to investigate and prepare this change. I reviewed every changed line and ran the commands shown above. Signed-off-by: Iyed Sebai <iyed.se1ai@gmail.com> |
||
|
|
9a1110997a |
fix(init): raise the ensure hook's timeout above cold-start latency (#3438)
## Latest branch refresh Refreshed cleanly from upstream main to incorporate its Bash classification fix, which addresses the two inherited failing Bash tests. All 86 Bash/init/plugin-manifest tests pass after the refresh. The tested npm installer pin also addresses the Docker OpenClaw installation crash. New-head CI is running. ## Description The `headroom init <agent>` Claude Code plugin (and the equivalent hooks Headroom writes directly for Copilot and Codex) registers `headroom init hook ensure` on `SessionStart` and `PreToolUse` with a **15-second** timeout. Starting the proxy from cold measures **15.9-36.9 seconds**, and the hook's own internal `wait_ready()` call after spawning the proxy already allows up to **45 seconds**. The external 15s timeout kills the hook process well before either of those can complete. Because `PreToolUse` matches `Bash|PowerShell`, this becomes a **permanent 15-second stall on every single shell tool call, in every project, in every session**, with no path to self-recovery — the hook can never win from a cold start. Closes #3417 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `plugins/headroom-agent-hooks/hooks/hooks.json`: raised both `SessionStart` and `PreToolUse` hook timeouts from `15` to `60` seconds. This is the file installed into `~/.claude/plugins/cache/headroom-marketplace/...` that the issue's reproduction quotes directly. - `headroom/cli/init.py`: added `_HOOK_ENSURE_TIMEOUT_SECONDS = 60` and used it in `_ensure_claude_hooks`, `_ensure_copilot_hooks`, and `_ensure_codex_hooks` instead of the hardcoded `15`, so the hooks Headroom writes into a user's own `settings.json` / `hooks.json` carry the same fixed timeout as the plugin-provided one. - 60s was chosen to sit comfortably above the existing internal `wait_ready(timeout_seconds=45)` ceiling in `_ensure_profile_running`, so the host never kills the hook before that internal wait could have succeeded on its own. - `tests/test_cli/test_init_cli.py`: added regression tests asserting each of the three hook-writing functions emits a timeout greater than the internal 45s wait. - `tests/test_plugin_manifests.py`: added a regression test asserting every hook in the shipped `plugins/headroom-agent-hooks/hooks/hooks.json` carries a timeout greater than 45s. Out of scope for this PR (tracked separately as other aggravating factors in #3417): `unwrap` not disabling the plugin, hooks being written in two places at once, and adding a direct-to-upstream fallback when the proxy is unreachable. Those are independent fixes with their own risk surface; this PR addresses only the timeout mismatch that makes the hook unwinnable by construction. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) — not run; local `mypy` hits an unrelated numpy-stub/Python-version incompatibility in this environment, independent of this change - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ python3 -m pytest tests/test_cli/test_init_cli.py tests/test_plugin_manifests.py -q 81 passed in 0.47s $ python3 -m pytest tests/test_cli/ -q 740 passed, 1 skipped in 10.93s $ ruff check headroom/cli/init.py tests/test_cli/test_init_cli.py tests/test_plugin_manifests.py All checks passed! # Manual run against a sandboxed HOME (real `claude` CLI + real headroom init claude, # no side effects on the actual machine config): $ HOME=/tmp/fakehome HEADROOM_MARKETPLACE_SOURCE=$(pwd) python3 -m headroom.cli init --global claude Configured Claude Code (user scope). $ cat /tmp/fakehome/.claude/settings.json { "hooks": { "SessionStart": [{"hooks": [{"command": "... headroom init hook ensure ...", "timeout": 60}]}], "PreToolUse": [{"hooks": [{"command": "... headroom init hook ensure ...", "timeout": 60}]}] }, ... } ``` ## Real Behavior Proof - Environment: macOS (Darwin 24.6.0), Python 3.12.3, real `claude` CLI on PATH, `HOME` sandboxed to a throwaway directory so the run could not touch the actual machine's `~/.claude` config. - Exact command / steps: `HOME=<scratch> HEADROOM_MARKETPLACE_SOURCE=<repo> python3 -m headroom.cli init --global claude`, then inspected the written `<scratch>/.claude/settings.json`. - Observed result: the command ran end-to-end (added the marketplace, installed the plugin, wrote hooks), and the written `settings.json` carries `"timeout": 60` on both the `SessionStart` and `PreToolUse` `headroom init hook ensure` entries, confirming the fix takes effect through the real `headroom init claude` code path, not just the unit tests. Verified afterward that the real `~/.claude/settings.json` on this machine was untouched (unchanged mtime, no `headroom-marketplace` entry). - Not tested: an actual cold proxy start under a live Claude Code session timing out (or not) at the old vs. new limit — that requires a real multi-second proxy cold-start reproduction end-to-end inside Claude Code itself, which wasn't run. The 15.9-36.9s cold-start figures are taken from the issue's own measurements; this PR does not independently re-measure them, it only widens the external timeout to comfortably exceed the code's own internal 45s wait ceiling. ## Runtime Rollout Safety - Rollout-managed feature(s): None. The hook timeout is a static value written into user-owned config files (`settings.json` / `hooks.json`) by `headroom init`, and in the plugin's bundled `hooks.json` — it is not behind a feature flag or rollout channel. - Minimum rollout channel: N/A. - Stable/default behavior changed: Yes, for new/re-run `headroom init` invocations and for anyone picking up the updated bundled plugin: the ensure-hook timeout goes from 15s to 60s. Users who already ran `init` keep their existing `15` in their local config until they re-run `headroom init` (this is not auto-migrated). - Kill switch / disable path: None needed / a user can hand-edit the `timeout` value in their own `settings.json` or `hooks.json` at any time. - Unsafe override required: No. - Qualification impact: None — this changes only how long the host waits before killing the `ensure` subprocess; it does not change the proxy's request/response behavior, pricing, or compression paths. - Rollback path: Revert this PR; existing users' local config is unaffected until they re-run `headroom init`, and anyone who already has `60` written can hand-edit it back to `15` if needed. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation — N/A, no existing docs reference the 15s hook timeout value - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` ## Additional Notes The issue (#3417) also flags three other problems with the same hook system (`unwrap` not disabling the plugin, hooks being registered in two places at once, and no direct-to-upstream fallback when the proxy is unreachable). Those are left for separate PRs since each has its own risk surface and review scope; this PR fixes only the timeout mismatch that makes the hook unwinnable from a cold start. --------- Co-authored-by: Jerrett Davis <mxjerrett@gmail.com> |
||
|
|
daca1dd756 |
fix(cli/init): fail clearly on a target settings file with invalid JSON (#2227)
## Description
`headroom init` crashes with a raw traceback when a target's settings
file contains invalid JSON.
`_json_file` reads the JSON config files that init read-merge-writes
(Claude's `settings.json`, Codex's `hooks.json`, etc.):
```python
def _json_file(path: Path) -> dict[str, Any]:
if not path.exists():
return {}
content = path.read_text(encoding="utf-8").strip()
if not content:
return {}
payload = json.loads(content) # unguarded
return payload if isinstance(payload, dict) else {}
```
These are user-owned files that people hand-edit, so a stray trailing
comma or an unquoted key is entirely plausible. When that happens
`json.loads` raises `json.JSONDecodeError` and it propagates all the way
out, so `headroom init` dies with a Python traceback instead of a usable
message.
Returning `{}` on the error would be worse, not better: every caller
does `payload = _json_file(path)` then `_write_json(path, payload)`, so
an empty dict would make init overwrite the user's real settings with
just the hooks/env block — silent data loss.
## Fix
Guard the parse and convert it into an actionable `ClickException` that
names the file and the parse error, leaving the file untouched:
```python
try:
payload = json.loads(content)
except json.JSONDecodeError as e:
raise click.ClickException(
f"{path} contains invalid JSON ({e}); fix it and re-run, or move it aside."
) from e
```
`click.ClickException` is already the project's convention for
user-facing init failures (e.g. the `'claude' not found in PATH`
messages). The user now gets a clear instruction, and their file is
never clobbered.
Closes #
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/cli/init.py`: wrap the `json.loads` in `_json_file` and
raise a `ClickException` on `JSONDecodeError`.
- `tests/test_cli/test_init_cli.py`: new test asserting a malformed file
raises `ClickException` (matching "invalid JSON") and is left
byte-for-byte untouched.
- `CHANGELOG.md`: Bug Fixes entry.
## Testing
- [ ] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed
### Test Output
```text
$ uvx ruff@0.15.17 check headroom/cli/init.py tests/test_cli/test_init_cli.py
All checks passed!
$ uvx mypy@1.20.2 --ignore-missing-imports headroom/cli/init.py
Success: no issues found in 1 source file
```
## Real Behavior Proof
- Environment: Windows 11, Python 3.12, `uvx ruff@0.15.17` / `uvx
mypy@1.20.2`. A full `pytest` OOM-kills this box (ML stack import), so I
reproduced the behavior with a dependency-free script mirroring
`_json_file` and left the full pytest to CI.
- Exact command / steps: wrote a settings file containing `{"env": {"A":
"B",}}` (trailing comma), then called the OLD unguarded reader and the
NEW guarded reader; also re-checked a valid file round-trips.
- Observed result: OLD raises a raw `json.JSONDecodeError` (the init
traceback); NEW raises a `ClickException` containing "invalid JSON" and
leaves the file byte-for-byte unchanged; valid JSON still parses to the
same dict.
- Not tested: a full `headroom init` end-to-end run; full local `pytest`
deferred to CI (OOM).
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [x] I have updated the CHANGELOG.md if applicable
## Additional Notes
The "unit tests pass locally" box is unchecked because the full suite
imports the ML stack, which I can't run here. The new test uses the
existing `_load_init_module` harness (the same one the neighbouring
`test_json_file_*` tests use), so it runs under the normal CI pytest
job; behaviour is additionally verified by the standalone proof above.
|
||
|
|
84f66da36f |
fix(init/codex): merge into hooks.json instead of overwriting it (#2173)
## Description
`headroom init codex` destroys a user's existing Codex hooks.
`_ensure_codex_hooks` builds a fresh payload containing only Headroom's
two hooks and writes it wholesale:
```python
payload = { "hooks": { "SessionStart": [...headroom...], "PreToolUse": [...headroom...] } }
path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
```
It never reads the existing file, so any user-managed hooks (and any
other top-level keys) in `~/.codex/hooks.json` are silently replaced
with just Headroom's entries — data loss on every `init codex` run.
The sibling registrars do it correctly: `_ensure_claude_hooks` and
`_ensure_copilot_hooks` both read via `_json_file`, then merge per event
and dedup on the Headroom marker, preserving unrelated user entries. The
codex path was the lone writer that overwrote.
## Fix
Read-merge-write, mirroring `_ensure_claude_hooks`: load the existing
payload, keep each event's entries that don't carry the
`headroom-init-codex` marker, append Headroom's, and write back. User
hooks and other top-level keys survive; Headroom's are deduped
(idempotent re-runs).
Closes #
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/cli/init.py`: `_ensure_codex_hooks` reads via `_json_file`,
merges per event with marker dedup, and writes via `_write_json` (no
more wholesale overwrite).
- `tests/test_cli/test_init_cli.py`: add
`test_ensure_codex_hooks_preserves_user_hooks` — a user hook and an
unrelated top-level key survive; Headroom's hook is appended once.
- `CHANGELOG.md`: Bug Fixes entry.
## Testing
- [ ] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed
### Test Output
```text
$ uvx ruff@0.15.17 check headroom/cli/init.py tests/test_cli/test_init_cli.py
All checks passed!
$ python -m py_compile headroom/cli/init.py tests/test_cli/test_init_cli.py
OK
```
## Real Behavior Proof
- Environment: Windows 11, Python 3.12, `uvx ruff@0.15.17`. Importing
`headroom` pulls in the torch/transformers stack and a full `pytest`
gets OOM-killed on this box, so I verified the merge with a
dependency-free script that replicates the old (overwrite) vs new
(merge) logic, and left the full pytest to CI.
- Exact command / steps: gave a config with a user `SessionStart` hook
(`echo my-own-hook`) and an unrelated top-level key (`notify: true`),
then ran both the old and new logic.
- Observed result: old drops both the user hook and the top-level key;
new keeps both and appends Headroom's hook exactly once. The new test
asserts this against the real `_ensure_codex_hooks`.
- Not tested: a live `headroom init codex` end to end; full local
`pytest` deferred to CI (OOM, per above).
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [x] I have updated the CHANGELOG.md if applicable
## Additional Notes
The "unit tests pass locally" and "type checking" boxes are unchecked
because the full suite imports the ML stack, which I can't run in this
environment; the change mirrors the existing `_ensure_claude_hooks`
merge logic, verified by the standalone proof and the new test (which
reuses the file's existing hooks-test harness).
|
||
|
|
8da4384bfc |
fix(init/codex): don't delete per-profile provider settings (#2146)
## Description `headroom init codex` silently deletes a user's per-profile provider settings. `_ensure_codex_provider` owns the root-level `model_provider` / `openai_base_url` keys, and to avoid emitting a duplicate top-level key it strips any prior assignment before re-inserting its block: ```python content = re.sub(r"(?m)^[ \t]*model_provider[ \t]*=.*\r?\n", "", content) content = re.sub(r"(?m)^[ \t]*openai_base_url[ \t]*=.*\r?\n", "", content) ``` Those multiline regexes match the keys at any indentation, **in any TOML table**. Codex supports per-profile overrides: ```toml [profiles.work] model_provider = "azure" [profiles.gpt5] model_provider = "openai" ``` So a user with named Codex profiles who runs `headroom init codex` has every `[profiles.*]` `model_provider` / `openai_base_url` line silently removed. Those profiles then fall through to the injected root `model_provider = "headroom"` default — their routing is quietly changed. That collateral deletion isn't needed to prevent the root-level duplicate the strip exists for (#260); the unwrap-side sibling `_strip_codex_init_block` proves the intent is precise (it only removes the Headroom-owned value). ## Fix Scope the strip to the document root — everything before the first table header. Root-level `model_provider` / `openai_base_url` are still replaced (init owns them), but keys inside `[profiles.*]` (or any other table) are left untouched. Closes # ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `headroom/cli/init.py`: `_ensure_codex_provider` splits the config at the first table header and strips `model_provider`/`openai_base_url` only from the root section. - `tests/test_cli/test_init_cli.py`: add `test_ensure_codex_provider_preserves_profile_overrides` — a `[profiles.work]` override survives init while the root key is replaced by `headroom`. - `CHANGELOG.md`: Bug Fixes entry. ## Testing - [ ] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed ### Test Output ```text $ uvx ruff@0.15.17 check headroom/cli/init.py tests/test_cli/test_init_cli.py All checks passed! $ python -m py_compile headroom/cli/init.py tests/test_cli/test_init_cli.py OK ``` ## Real Behavior Proof - Environment: Windows 11, Python 3.12, `uvx ruff@0.15.17`. Importing `headroom` pulls in the torch/transformers stack and a full `pytest` gets OOM-killed on this box, so I verified the strip with a dependency-free script that replicates the old (whole-file) vs new (root-scoped) regex, and left the full pytest to CI. - Exact command / steps: ran both strippers on a config with a root `model_provider = "openai"` and a `[profiles.work]` block overriding `model_provider`/`openai_base_url`. - Observed result: the old strip deletes the `[profiles.work]` overrides too; the new strip keeps them and still removes the root assignment. The new test asserts the profile override survives and the root becomes `headroom`. - Not tested: a live `headroom init codex` end to end; full local `pytest` deferred to CI (OOM, per above). ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [x] I have updated the CHANGELOG.md if applicable ## Additional Notes The "unit tests pass locally" and "type checking" boxes are unchecked because the full suite imports the ML stack, which I can't run in this environment; the change scopes an existing regex strip to the document root, verified by the standalone proof and the new test (the two existing `_ensure_codex_provider` tests only exercise root-level and block-placement behavior, both preserved). I kept the fix to root-scoping rather than also matching only the `"headroom"` value, since that preserves the #260 duplicate-key guard without the broad deletion. Co-authored-by: JerrettDavis <mxjerrett@gmail.com> |
||
|
|
e6bbc40b11 |
fix(codex): retag threads on init so Codex Desktop history stays visible (#961) (#1349)
## Description
Installing Headroom for Codex via `headroom init` can make Codex Desktop
appear to lose its local chat/thread history. The data is never deleted
— Codex filters its sidebar/search by the active `model_provider`, and
the init path set `model_provider = "headroom"` without retagging
existing threads, so native `openai` threads disappeared from the menu.
The install (`headroom.providers.codex.install`) and wrap
(`headroom.cli.wrap`) paths already reconcile thread provider tags
across the proxy boundary (retag `openai -> headroom` on enable). The
init path — `_ensure_codex_provider` in `headroom/cli/init.py`, which is
exactly what the issue reproduces ("Headroom init proxy" provider,
`headroom-init-codex` hook) — was the one place that injected the
provider without retagging. This wires the same reconciliation into the
init path. The revert direction is already handled by `headroom unwrap
codex`.
Closes #961
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/cli/init.py`: `_ensure_codex_provider` now calls
`retag_to_headroom(path.parent)` after writing the init provider block,
so existing native threads stay visible under the active `headroom`
provider. Third-party providers (e.g. `anthropic`) are left untouched
(existing `retag_thread_providers` behaviour).
- `tests/test_cli/test_init_cli.py`: regression test seeding a Codex
Desktop `state_5.sqlite` and asserting `_ensure_codex_provider` retags
`openai -> headroom` while leaving other providers alone.
- `CHANGELOG.md`: Unreleased → Bug Fixes entry.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ uv run pytest tests/test_cli/test_init_cli.py tests/test_provider_codex_threads.py tests/test_provider_codex_install.py -q
84 passed in 0.84s
$ uv run ruff check headroom/cli/init.py tests/test_cli/test_init_cli.py
All checks passed!
$ uv run mypy headroom/cli/init.py
Success: no issues found in 1 source file
```
#### RED → GREEN proof
RED — new test with the prod fix (the `retag_to_headroom` call)
reverted:
```text
E AssertionError: existing openai threads not retagged: {'anthropic': 1, 'openai': 2}
1 failed in 0.44s
```
GREEN — with the fix applied:
```text
tests/test_cli/test_init_cli.py::test_init_codex_provider_retags_existing_threads
1 passed in 0.36s
```
## Real Behavior Proof
- Environment: Linux, Python 3.13, headroom @ this branch.
- Exact command / steps: seed a Codex Desktop store
(`<codex_home>/sqlite/state_5.sqlite`) with native threads, then run the
init provider injection:
```text
before init: {'anthropic': 1, 'openai': 2}
after init: {'anthropic': 1, 'headroom': 2}
config model_provider line: ['model_provider = "headroom"',
'[model_providers.headroom]']
```
- Observed result: after init, the two `openai` threads are retagged to
`headroom` (so they stay visible under the now-active provider), while
the `anthropic` thread is left untouched. Before the fix they stayed
`openai` and were filtered out of Codex Desktop's menu.
- Not tested: the live Codex Desktop GUI itself (proprietary, no
sandbox); the filtering behaviour is the documented `thread/list`
provider filter described in the issue, and the store-level retag that
makes history visible is covered above and by the unit test.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I have updated the CHANGELOG.md if applicable
## Additional Notes
Scope is limited to the init provider path; the install and wrap paths
already perform this reconciliation. Screenshots N/A (no UI change on
Headroom's side).
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
|
||
|
|
500ec2b7fa |
fix(init): set ENABLE_TOOL_SEARCH=true so Claude Code keeps deferring tools (#746) (#995)
## Description Claude Code disables on-demand tool loading (Tool Search) when `ANTHROPIC_BASE_URL` is a custom host and `ENABLE_TOOL_SEARCH` is unset, materializing all MCP/system tool schemas into its context window (#746). With many MCP servers this overflows the window — breaking sub-agent spawns ("prompt too long, ~214k > 200k") and forcing constant compaction. `headroom wrap claude` already sets it; `init`/install did not. Refs #746. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - Keep tool deferral on at both entry points, sharing one `TOOL_SEARCH_ENV` / `TOOL_SEARCH_DEFAULT` constant from the Claude provider package (`providers/claude/runtime.py`) so the key/default can't drift: - `init` (`_ensure_claude_hooks`): sets `ENABLE_TOOL_SEARCH=true` via `setdefault`, respecting a pre-existing user-provided value. - `install` (`build_install_env`): always writes `ENABLE_TOOL_SEARCH=true`. This is the headroom-managed install env (recorded and reverted on uninstall), so it is authoritative rather than deferring to an existing value. ## Testing - [x] Unit tests pass (`pytest`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ pytest tests/test_cli/test_init_enable_tool_search.py -q 3 passed in 0.63s ``` ## Real Behavior Proof - Environment: Python 3.14, headroom proxy on :8799, ~19 MCP servers connected - Exact command / steps: launched `claude` through the proxy with vs without `ENABLE_TOOL_SEARCH=true`, asked each to spawn 5 parallel sub-agents - Observed result: without it, all 5 sub-agents fail ("prompt too long, ~214k > 200k"); with `ENABLE_TOOL_SEARCH=true` all 5 succeed and traffic compresses - Not tested: non-Claude-Code agents ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> |
||
|
|
8c00f7103c |
fix(codex): poll /wham/usage for subscription limits (handshake no longer sends x-codex-* headers) (#924)
## Description Codex's subscription usage window (primary/secondary rate-limit gauges) stopped populating for ChatGPT-OAuth sessions. This PR restores it by polling Codex's dedicated usage endpoint instead of relying on response headers that are no longer sent. ### Why the previous approach no longer works The existing code populates `CodexRateLimitState` from `x-codex-*` rate-limit headers captured on the `/v1/responses` WebSocket handshake (`update_from_headers` at WS accept). That worked when OpenAI returned `x-codex-primary-used-percent`, `x-codex-primary-window-minutes`, etc. on the handshake response. OpenAI has since stopped sending those headers on the ChatGPT WebSocket handshake. I confirmed this by faithfully replaying a real Plus-account handshake (both `prewarm` and regular `request_kind`): no `x-codex-*` headers come back on either. This matches OpenAI's own move to a dedicated usage endpoint (`GET /backend-api/codex/usage` in CodexApi mode) and reports such as openai/codex#14728. So `update_from_headers` now runs on every accept but finds nothing to parse, and the window silently stays empty. The headers aren't coming back, so there is nothing to fix in the parsing path. The data now lives behind a request we have to make ourselves. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - `subscription/codex_rate_limits.py`: - `parse_codex_usage_payload()` / `update_from_usage_payload()` — map the `GET /backend-api/wham/usage` JSON (`rate_limit.primary_window` / `secondary_window` with `used_percent`, `limit_window_seconds`, `reset_at`; `credits`; `rate_limit_reached_type`) into the existing `CodexRateLimitState`. `limit_window_seconds` is converted to window-minutes with the same round-up codex-rs uses (`(secs + 59) // 60`). - `maybe_schedule_usage_poll()` — fire-and-forget, throttled to one request per 60s, scoped to ChatGPT sessions (requires both a Bearer token and `ChatGPT-Account-Id`; API-key traffic is skipped). Uses an in-flight guard so concurrent accepts don't stack polls. Endpoint is overridable via `HEADROOM_CODEX_USAGE_URL`. - `proxy/handlers/openai.py`: - At the Codex WS accept site, after the now-usually-empty `update_from_headers` block, schedule the usage poll. Wrapped in `contextlib.suppress` and fully non-blocking so it can never delay or fail the WebSocket accept. The old header-capture path is intentionally left in place as a no-cost fallback in case OpenAI restores the headers. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed (live `/wham/usage` replay against a Plus account returned HTTP 200 with the expected schema; payload fixture in tests mirrors that real shape) ## Test Output ``` $ uv run pytest tests/test_codex_rate_limits.py -q ........................................ [100%] 41 passed in 0.16s $ uv run ruff check headroom/subscription/codex_rate_limits.py headroom/proxy/handlers/openai.py tests/test_codex_rate_limits.py All checks passed! $ uv run mypy headroom/subscription/codex_rate_limits.py Success: no issues found in 1 source file ``` ## Additional Notes - New tests cover: full-payload mapping, window-minutes round-up, credits balance kept only when `has_credits`, promo object vs string, empty payload returns `None`, missing `used_percent` skipped, header-gating (requires Bearer + account-id), poll throttling, and no-event-loop safety. - Scoping to `ChatGPT-Account-Id` keeps the poll off API-key traffic, and the 60s throttle plus in-flight guard bound it to at most one lightweight GET per minute per running proxy. --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> |
||
|
|
dff6a19946 |
fix(codex): write canonical hooks feature flag and migrate deprecated codex_hooks (#743)
## Description `headroom init codex` writes the hooks feature flag into `.codex/config.toml` under the key `codex_hooks`. Codex renamed the canonical key to `hooks` and kept `codex_hooks` as a legacy alias (openai/codex#20522). Current Codex builds warn about `[features].codex_hooks` and tell users to use `[features].hooks` instead, so configs written by headroom should stop emitting the deprecated key. This PR switches headroom to write the canonical `hooks` key and **migrates existing configs in place**. The migration is the tricky part: a config can already contain `codex_hooks`, `hooks`, or both, in any order, inside or outside headroom's marker block — and a naive replace can emit a *duplicate* `hooks` key, which is invalid TOML that Codex rejects outright. The fix strips every `codex_hooks` line up front (any value, anywhere) — mirroring the existing top-level key cleanup in `_ensure_codex_provider` (#260) — then guarantees `hooks` is present without ever duplicating it, and respects a user-managed `hooks` value that lives outside our marker block. Fixes: N/A (no tracking issue — surfaced while aligning with Codex >= 0.129; related upstream context: openai/codex#20522 and the warning behavior discussed in openai/codex#22148) ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - Write the canonical `hooks` key instead of the deprecated `codex_hooks` in `_ensure_codex_feature_flag` (`headroom/cli/init.py`). - Strip any `codex_hooks` line (any value, inside or outside the marker block) before ensuring the flag, so re-running `init` migrates a legacy config instead of leaving a stale key or producing a duplicate `hooks` key (invalid TOML). - Respect a user-managed `hooks` value found outside headroom's marker block (e.g. `hooks = false`); only the deprecated alias is removed. - Make the insert/create paths match `_replace_marker_block`'s normalisation so re-running `init` is byte-idempotent. - Extract a `_codex_feature_block()` helper to remove the 4x duplicated marker block assembly. - Add regression tests for the previously-broken edge cases. ## Testing - [x] Unit tests pass (`pytest`) — affected module fully green (see output) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom/cli/init.py`) - [x] New tests added for new functionality - [x] Manual testing performed (reproduced each edge case against the patched function via `tomllib.loads`) ## Test Output ``` $ pytest -v tests/test_cli/test_init_cli.py -k "feature_flag or hooks_feature" tests/test_cli/test_init_cli.py::test_init_codex_merges_feature_flag_into_existing_table PASSED tests/test_cli/test_init_cli.py::test_init_codex_creates_hooks_feature_flag_on_first_init PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_replaces_existing_marker PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_migrates_legacy_codex_hooks_key PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_migrates_when_both_keys_present PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_migrates_when_keys_reversed PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_drops_legacy_key_outside_marker PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_is_idempotent PASSED tests/test_cli/test_init_cli.py::test_ensure_codex_feature_flag_creates_features_section_when_missing PASSED ======================= 9 passed, 45 deselected in 0.24s ======================= $ pytest -q tests/test_cli/test_init_cli.py 54 passed $ ruff check . All checks passed! $ mypy headroom/cli/init.py Success: no issues found in 1 source file ``` Note: the broader `tests/test_cli/` run has one unrelated failure (`test_wrap_copilot_auto_detects_running_proxy_backend`) caused by a real proxy already bound to port 8787 in the local environment — it fails identically on a clean checkout without this change. ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation (none required) - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] I have updated the CHANGELOG.md if applicable (managed by release-please; generated from the conventional commit, not edited by hand) ## Screenshots (if applicable) N/A ## Additional Notes - **Why the duplicate-key path matters:** TOML forbids duplicate keys, so a `[features]` table containing both `codex_hooks` and `hooks` (which the old in-place migration could produce) makes Codex reject `config.toml` entirely. The new "strip then ensure" approach can never emit two `hooks` lines. - **Version provenance:** the `codex_hooks` -> `hooks` rename landed in openai/codex#20522, first shipped in Codex `rust-v0.129.0`. `codex_hooks` remains a working legacy alias, but current Codex builds can warn users to move to `[features].hooks`. - **Idempotency:** running `headroom init codex` repeatedly now produces a byte-stable `config.toml`, so there is no churn on re-init. |
||
|
|
b4395993ae |
fix(init): suppress hook recovery output (#760)
## Summary - silence best-effort profile recovery while `headroom init hook ensure` runs from installed hooks - suppress both Python-level stdout/stderr and child process file-descriptor output so SessionStart hooks do not emit invalid JSON - add a regression test for noisy supervisor recovery failures ## Verification - `python3 -m py_compile headroom/cli/init.py` - live local hook probe: `headroom init hook ensure --profile default --marker headroom-init-codex` exits 0 with empty output - targeted pytest was not runnable locally because `uv.lock` currently fails to parse due to an inconsistent GitPython wheel version entry |
||
|
|
6ea6e31f09 |
fix(init): normalize Windows hook paths to forward slashes (#788)
## Description
On Windows, `_command_string()` preserves backslash paths from
`shutil.which()` (e.g. `C:\Users\...\headroom.exe`). Claude Code
executes hooks via Git Bash, which interprets backslashes as escape
characters, corrupting the path and failing with "command not found".
This PR normalizes backslash separators to forward slashes before
passing parts to `subprocess.list2cmdline()`. Forward slashes work in
bash, PowerShell, and cmd.exe on Windows.
Fixes #724
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
## Changes Made
- `headroom/cli/init.py`: Normalize backslash path separators to forward
slashes in `_command_string()` on Windows, before calling
`subprocess.list2cmdline()`
- `tests/test_cli/test_init_cli.py`: Add
`test_command_string_normalizes_backslashes_on_windows` verifying no
backslashes remain in the output and the forward-slash path is preserved
## Real behavior proof
**Setup:** Windows 11 (build 26200), Python 3.10.18, headroom repo at
commit
|
||
|
|
9252d852c5 |
fix(init): guard persistent task startup (#616)
## Description Prevent `headroom init` hooks from spawning duplicate persistent-task runners while a proxy is still starting. Fixes #615 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Problem `_ensure_profile_running()` checked readiness for only one second and then launched `start_detached_agent()` whenever the proxy was not ready yet. When Claude/Codex hooks fired close together, each hook could race through that path and spawn another detached persistent-task runner. ## Changes Made - Add a profile-local, nonblocking runtime start lock around init hook startup. - Re-check readiness after acquiring the lock so late-arriving hooks do not start a duplicate runner. - If a runtime is already alive, wait up to 15 seconds for readiness before stopping and restarting it. - Add regression tests for lock contention, slow startup, and cross-process lock behavior. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ## Test Output ``` UV_SKIP_WHEEL_FILENAME_CHECK=1 uv run --frozen --extra dev --extra proxy pytest tests/test_cli/test_init_cli.py tests/test_cli/test_install_cli.py tests/test_cli/test_wrap_persistent.py tests/test_install/test_runtime.py # 89 passed in 0.61s UV_SKIP_WHEEL_FILENAME_CHECK=1 uv run --frozen --extra dev --extra proxy ruff check . # All checks passed! UV_SKIP_WHEEL_FILENAME_CHECK=1 uv run --frozen --extra dev --extra proxy ruff format --check . # 775 files already formatted UV_SKIP_WHEEL_FILENAME_CHECK=1 uv run --frozen --extra dev --extra proxy mypy headroom --ignore-missing-imports # Success: no issues found in 346 source files ``` Manual sandbox check: ``` # before this change: 3 ensure calls spawned 3 detached starts # after this change: 3 ensure calls spawned 1 detached start while the runtime was still starting ``` ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] I have updated the CHANGELOG.md if applicable ## Additional Notes Docs and CHANGELOG were left unchanged because this is a small runtime bug fix with no user-facing CLI/API change. |
||
|
|
849b46de59 |
fix(codex): keep init model_provider at config root (#260)
`headroom init codex` appended its provider block to the end of ~/.codex/config.toml via _replace_marker_block. When the file ended in a table (e.g. [features]), TOML scoped the block's root keys (model_provider, openai_base_url) under that table, so Codex refused to start with: invalid type: string "headroom", expected a boolean in features. Add an at_root option to _replace_marker_block that inserts the block before the first table header (reusing the module's line-based header detection), and have _ensure_codex_provider use it. _ensure_codex_provider also strips any prior top-level model_provider/openai_base_url assignment first, so init replaces an existing value (or one an older version mis-scoped under a table) instead of emitting a duplicate top-level key. Files with no tables still append, so the keys stay at the root. Add regression tests that parse the result with tomllib and assert model_provider lands at the document root, not under [features], and that a pre-existing model_provider is replaced rather than duplicated. Verified end-to-end against the real codex CLI across 7 config shapes (trailing [features], fresh, no-tables, root-key+table, other trailing table, double-init, pre-existing provider): `codex doctor` reports "config could not be loaded" before the fix and "config loaded / parse ok" after. |
||
|
|
bf1e31b27c |
fix(codex): inject openai_base_url in init and persistent-install paths
Bug 3 fix is now consistent across all three Codex entry points. Subscription (ChatGPT plan) users will always have their traffic routed through headroom regardless of whether they reached Codex config via `headroom wrap codex`, `headroom init codex`, or the persistent-install provider scope — all three now write `openai_base_url` at the TOML top-level (outside any `[model_providers.*]` block) so Codex's built-in openai provider is intercepted even when subscription auth bypasses the `model_provider = "headroom"` selection. Changes: - headroom/cli/init.py: add `openai_base_url` line to `_ensure_codex_provider` block; add `_strip_codex_init_block` helper with orphan-key cleanup (mirrors `_strip_codex_headroom_blocks` in wrap.py) - headroom/providers/codex/install.py: add `openai_base_url` line to `apply_provider_scope` section; add orphan-cleanup regexes and apply them in `revert_provider_scope` to handle crash-recovery scenarios - tests/test_install/test_providers.py: add `test_apply_provider_scope_writes_openai_base_url`, `test_persistent_install_strip_removes_openai_base_url` - tests/test_cli/test_init_cli.py: add `test_init_codex_writes_openai_base_url`, `test_init_codex_strip_removes_openai_base_url` Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|
|
06428d20fd |
fix: preserve Codex OAuth proxy delivery
Preserve Codex OAuth-safe provider config across init, wrap, and persistent install paths, and strengthen coverage so Codex requests are proven to reach Headroom and the mock upstream. The wrap e2e now sends a real chat-completions probe and checks Headroom /stats. Runtime tests cover temporary launch env, install env, init config, provider-scope config delivery, and the Python 3.11 ws bootstrap path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |
||
|
|
301563f11d |
test(init): make verbose stderr assertion click-version-agnostic
The test added in
|
||
|
|
bb91cfe688 |
feat(init): add -v/--verbose flag for debug diagnostics
When users hit an init regression it's opaque why: no visible state about which agents were probed, which paths were written, which subprocesses ran. Add a top-level flag to ``headroom init`` that routes debug-level logging from the ``headroom.cli.init`` logger to stderr. Instrumented decision points: * detect_init_targets / _probe_init_targets — scope + per-target shutil.which result * _write_json, _ensure_claude_hooks, _ensure_copilot_hooks, _ensure_codex_hooks, _ensure_codex_provider — file paths being written * _apply_user_env — chosen scope (windows vs unix) and env-var keys * _run_checked — each subprocess command + exit code + truncated stdout/stderr (useful when ``claude plugin install`` fails) * _run_init_targets — target dispatch order and resolved profile * top-level init callback — all flag values and invoked_subcommand Log output goes to stderr so stdout stays clean for pipes. The handler attached by ``_enable_verbose_logging`` is idempotent - nested subcommand invocations don't duplicate output. The logger does not propagate to the root logger, so enabling ``headroom init -v`` does not affect the rest of the process. The flag is declared on the parent Click group. Subcommands (claude, codex, copilot, openclaw) inherit the enabled logger automatically because the group callback runs before dispatch. Added tests cover: * ``init -v`` emits the expected markers to stderr, including ``detect_init_targets``, ``global_scope=True``, and each agent name * ``_enable_verbose_logging`` is safe to call repeatedly (handler remains singular) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|
|
4c062319f0 |
fix(init): guide users when no agents are auto-detected
Fixes #245. Running ``headroom init -g`` with no supported agents on PATH previously produced a single-line ClickException that read like the -g flag had been removed: Error: No supported user init targets were auto-detected. Specify one explicitly. This left reporter #245 concluding the feature was gone. Replace that message with a structured diagnostic that: * states which scope (user / local) was tried * lists every target probed (claude, codex, copilot, openclaw) and the shutil.which() result for each * explicitly confirms that -g / --global is still a supported flag * shows the concrete per-target invocation for each agent (``headroom init -g claude``, ...) so the user knows the escape hatch The implementation factors ``detect_init_targets`` into a ``_probe_init_targets`` helper that returns ``[(name, which_result)]``. ``detect_init_targets`` keeps its existing signature so the test suite and external imports aren't broken; the new helper backs both the auto-detection path and the diagnostic error formatter. Unit tests in tests/test_cli/test_init_cli.py cover: * the end-to-end message shape (structural markers + every target name + the example invocation) * the local-scope variant omitting global-only agents (copilot / openclaw) * that found binaries are surfaced with their absolute path so users can debug cases where shutil.which returns an unexpected result No behavior change when at least one target is detected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|
|
9ba9a59f78 |
test: isolate windows init branches from os globals
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |
||
|
|
c1b648664e |
test: raise init command branch coverage
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |
||
|
|
a278a7b0ba |
test: cover init install flows end to end
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |
||
|
|
3a999d1562 |
feat: add durable init command for agent hooks
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |