Commit graph

2 commits

Author SHA1 Message Date
Rod Boev
701e4616d9
fix(kimi): route managed Kimi Code through the proxy (#3223)
## Description

Managed Kimi Code reads KIMI_CODE_BASE_URL while headroom wrap kimi
previously supplied only KIMI_BASE_URL. The managed client can therefore
keep its direct endpoint while the wrapper appears healthy. Emit both
provider-owned keys and recompute them through the existing launch
callback at the proxy's actual port. Preserve the legacy route and
unrelated wrappers. Closes #3207

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (feature that would cause existing behavior to
change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- Set KIMI_CODE_BASE_URL and KIMI_BASE_URL from one project-aware proxy
URL.
- Recompute both values and their display lines through the Kimi
configure_launch callback after port fallback.
- Remove the generic display rewrite from _launch_tool so other wrappers
retain their base behavior.
- Add production-boundary child, fallback-port, legacy-preservation, and
non-Kimi negative-space tests.

## Testing

- [x] Unit tests pass (`uv run pytest tests/test_cli/test_wrap_kimi.py
-q`)
- [x] Linting passes (`uv run ruff check .`)
- [ ] Type checking passes (`uv run mypy headroom`)
- [x] New tests added for the regression
- [x] Manual testing performed through the production subprocess
boundary

### Test Output

```text
uv run pytest tests/test_cli/test_wrap_kimi.py -q
10 passed in 0.40s
uv run pytest tests/test_cli/test_wrap_grok.py -q
2 passed
uv run ruff check .
All checks passed!
uv run ruff format . --check
1534 files already formatted
git diff --check
```

## Real Behavior Proof

- Environment: Windows, isolated Kimi wrapper subprocess harness.
- Exact command / steps: launch a contract-compatible child through the
Kimi wrapper; exercise requested and fallback ports, project prefixes,
legacy selection, and a non-Kimi wrapper.
- Observed result: the child receives the effective project-aware proxy
URL in both Kimi keys; the displayed URL matches it after fallback;
legacy and non-Kimi behavior remain unchanged.
- Not tested: live authenticated Kimi Code managed request

## Runtime Rollout Safety

- Rollout-managed feature(s): None; managed Kimi Code routing is
selected by the existing wrapper mode.
- Minimum rollout channel: Stable; no staged rollout mechanism exists
for this wrapper path.
- Stable/default behavior changed: Yes, managed Kimi Code launches now
receive the effective proxy URL in both provider-owned keys.
- Kill switch / disable path: Stop using the managed Kimi wrapper path
or revert the release commit.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: Revert the release 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
- [x] I have commented my code, particularly in hard-to-understand areas
- [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 have updated the CHANGELOG.md if applicable

## Additional Notes

Kimi Code owns OAuth credentials and the /login flow. Headroom does not
read or modify Kimi config or credential files. The changelog is
generated by the release pipeline.
2026-08-23 19:55:14 -07:00
kaz
eac49656a1
feat(wrap): add headroom wrap kimi for Kimi CLI (#1426)
## Description

Adds `headroom wrap kimi`, routing Kimi CLI through the Headroom proxy.

Kimi CLI speaks an OpenAI-compatible `/chat/completions` API (its
`kosong` backend wraps `AsyncOpenAI`) and lets the base URL be
overridden via `KIMI_BASE_URL`. This wrapper points it at the local
proxy. Kimi's own OAuth bearer is forwarded upstream unchanged, so —
unlike the Copilot subscription path — no extra login or token exchange
is needed.

## Type of Change

- [x] New feature (non-breaking change that adds functionality)

## Changes Made

- `headroom/providers/kimi/`: new slice; `build_launch_env` sets
`KIMI_BASE_URL` with the per-project base-URL prefix, mirroring the
aider/vibe slices.
- `headroom/cli/wrap.py`: `kimi` subcommand; falls back to the
`kimi-cli` binary when `kimi` is not on `PATH`; `--kimi-api-url`
overrides the upstream coding endpoint (default
`https://api.kimi.com/coding/v1`).
- `tests/test_cli/test_wrap_kimi.py`: 8 tests for the wrap command.
- `README.md`: Kimi CLI row in the agent-compatibility matrix.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ pytest tests/test_cli/test_wrap_kimi.py -q
........                                                                 [100%]
8 passed in 0.36s

$ ruff check headroom/providers/kimi headroom/cli/wrap.py tests/test_cli/test_wrap_kimi.py
All checks passed!

$ ruff format --check headroom/providers/kimi headroom/cli/wrap.py tests/test_cli/test_wrap_kimi.py
4 files already formatted
```

## Real Behavior Proof

- Environment: macOS; Kimi CLI (`kimi` / `kimi-cli`); `headroom proxy`
started with `--openai-api-url https://api.kimi.com/coding/v1`.
- Exact command / steps: start `headroom proxy --port 8787
--openai-api-url https://api.kimi.com/coding/v1`, then `curl -s
http://localhost:8787/v1/chat/completions` with the Kimi OAuth bearer
and a one-line `kimi-for-coding` chat request (`"Reply with exactly:
PONG"`).
- Observed result: `HTTP 200`; `choices[0].message.content == "PONG"`
from `kimi-for-coding`; the OAuth bearer was forwarded and accepted
upstream; the per-project path `/p/<name>/v1/chat/completions` also
returned `HTTP 200`.
- Not tested: Windows/Linux PATH discovery; the `--learn` / `--memory`
live paths beyond flag wiring.

## 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 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

## Additional Notes

- `ruff check` and `ruff format --check` pass locally; `mypy` was run on
the new `headroom/providers/kimi` slice only (clean), so the full-tree
`mypy headroom` box is left unchecked and is left to CI.
- The slice deliberately reuses `codex.proxy_base_url` and
`with_project_prefix`, identical to the aider/vibe wrappers, so
per-project savings attribution works without Kimi sending custom
headers.
- Kimi's separate search/fetch services are out of scope for
`KIMI_BASE_URL` and continue to hit Kimi directly; only the LLM
`/chat/completions` traffic is compressed.

Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-07-15 21:40:58 +00:00