mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
fix/openclaw-python-launcher-path
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
effa5a5886 |
fix(proxy/memory): harden _extract_tool_calls against malformed choices and blocks (#2483)
## Description
`MemoryHandler._extract_tool_calls` reads tool calls from an upstream
response. The OpenAI Chat Completions branch only truthiness-checked
`choices` before indexing:
```python
choices = response.get("choices", [])
if choices:
message = choices[0].get("message", {})
...
```
An OpenAI-compatible gateway can send `choices: [null]` on a
content-filtered or usage-only response (the same shape the sibling
`CCRResponseHandler._extract_assistant_message` was explicitly hardened
for, see its comment: "a present-but-empty `choices: []` (or `[null]`)
which OpenAI-compatible gateways can send"). With `[null]`, `choices` is
truthy, so `choices[0].get("message", {})` runs on `None` and raises
`AttributeError`. A non-dict first element (`choices: ["..."]`) breaks
the same way.
This is not caught locally: `_extract_tool_calls` runs from
`has_memory_tool_calls`, which the OpenAI handler calls in the `if
(...)` condition **before** the `try` that wraps the rest of memory
handling, so the `AttributeError` surfaces to the request handler rather
than being swallowed and logged.
The Anthropic branch had a smaller version of the same gap: `[block for
block in content if block.get("type") == "tool_use"]` guards `content`
for list-ness but not each block, so a null element in a reconstructed
response crashes `block.get`.
## Fix
Guard the first choice and its `message` for dict-ness before calling
`.get`, mirroring `_extract_assistant_message`. A malformed choice now
yields no tool calls and correctly falls through to the Responses-API
`output[]` check. Skip non-dict blocks in the Anthropic branch.
Well-formed responses are unchanged.
## 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/proxy/memory_handler.py`: dict-guard the first choice and
its `message` in the OpenAI branch of `_extract_tool_calls`; skip
non-dict content blocks in the Anthropic branch.
- `tests/test_memory_handler_null_function.py`: regressions for
`choices: [null]` / non-dict choice / null message, fall-through to a
Responses `output[]` function_call, and a non-dict Anthropic content
block.
## Testing
- [x] 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
$ python -m pytest tests/test_memory_handler_null_function.py -q
5 passed
# with the fix reverted, the three new tests fail with
# AttributeError: 'NoneType' object has no attribute 'get'
$ uvx ruff@0.15.17 check headroom/proxy/memory_handler.py tests/test_memory_handler_null_function.py
All checks passed!
$ uvx mypy@1.20.2 --ignore-missing-imports headroom/proxy/memory_handler.py
Success: no issues found in 1 source file
```
## Real Behavior Proof
- Environment: Windows 11, Python 3.12, project venv (`uv sync --extra
proxy`), `uvx ruff@0.15.17` / `uvx mypy@1.20.2`, pytest in the venv.
- Exact command / steps: called `_extract_tool_calls` /
`has_memory_tool_calls` on a bare `MemoryHandler` (via `object.__new__`)
with `choices: [null]`, `choices: ["x"]`, `choices: [{"message":
null}]`, a `[null]`-choices response that also carries a Responses
`output[]` function_call, and an Anthropic response whose content list
starts with a null block; then reverted `memory_handler.py` and re-ran.
- Observed result: with the fix the malformed choices return `[]` /
`False`, the `output[]` fall-through is still detected as `True`, and
the valid Anthropic tool_use block is returned; with the fix reverted
the `choices: [null]` and non-dict-block cases raise `AttributeError:
'NoneType' object has no attribute 'get'`. Ran against the actual module
via `tests/test_memory_handler_null_function.py`.
- Not tested: a live OpenAI-compatible gateway emitting `choices:
[null]` on a memory-enabled request, end to end.
## 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
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/2483"
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: JD Davis <mxjerrett@gmail.com>
|
||
|
|
8b7e797ed4 |
fix(proxy/memory): don't crash memory tool-call detection on a null function (#2272)
## Description
`MemoryHandler` crashes when an upstream response carries a tool call
whose `function` field is explicitly `null`.
Three sites read the nested name like `tool_call.get("function",
{}).get("name")`:
- `has_memory_tool_calls` (line ~1043) — over the response's tool calls.
- `handle_tool_calls` (line ~1110/1119) — resolving the tool name and
arguments.
- the memory tool-injection dedup (line ~562) — over the request's
tools.
`dict.get("function", {})` only substitutes `{}` for a *missing* key. A
present-but-null `{"id": "c1", "type": "function", "function": null}` —
a shape upstreams and gateways emit for a partial or streamed tool call
— makes the result `None`, and `None.get("name")` raises
`AttributeError`.
`has_memory_tool_calls` and `handle_tool_calls` both iterate the
untrusted upstream response, so a single malformed tool call takes down
memory tool-call detection and handling for the whole response.
## Fix
Coalesce `function` with `or {}` at all three sites, so a null (or any
falsy) value collapses to `{}`:
```python
name = tc.get("name") or (tc.get("function") or {}).get("name")
args_str = tc.get("arguments") or (tc.get("function") or {}).get("arguments") or "{}"
```
Real tool calls resolve exactly as before.
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/proxy/memory_handler.py`: coalesce `function` with `or {}`
in `has_memory_tool_calls`, `handle_tool_calls`, and the tool-injection
dedup.
- `tests/test_memory_handler_null_function.py`: new tests that a
null-function tool call doesn't crash detection and the real memory call
is still seen.
- `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/proxy/memory_handler.py tests/test_memory_handler_null_function.py
All checks passed!
$ uvx mypy@1.20.2 --ignore-missing-imports headroom/proxy/memory_handler.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 name-resolution logic with a dependency-free script and
left the full pytest to CI.
- Exact command / steps: ran a `{"function": null}` tool call (plus a
real `memory_save` call) through the OLD `get("function", {})` and NEW
`get("function") or {}` name resolution.
- Observed result: OLD raises `AttributeError` on the null function; NEW
returns `None` for it and still resolves the real `memory_save` name and
a plain `{"name": "memory"}`.
- Not tested: a live upstream emitting a null-function tool call; 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. `has_memory_tool_calls`
and `_extract_tool_calls` use no instance state, so the new test
exercises them on a bare instance via `object.__new__` — it runs under
the normal CI pytest job, and the standalone proof above corroborates
it. This is the memory-handler sibling of the same null-`function`
hazard I'm fixing in the CCR tool-call detection and the memory tool
adapter.
---------
Co-authored-by: Tejas Chopra <chopratejas@gmail.com>
|