mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
fix/plugin-launcher-isolated-python
6
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
07d5add5ba |
fix(memory): keep multimodal user content intact when injecting memories (#4648)
## Description Item 10 of #4362. `MemoryWrapper._inject_memories` (`headroom/memory/wrapper.py`, the path behind `with_memory(client).chat.completions.create`) searched on `str(user_content)` and prepended the memory context with `f"{context_block}\n\n{original}"`. For a list-typed (multimodal) user message, the content sent upstream became one string holding the Python repr of the parts, so images and files never reached the model, and the memory search ran on that repr. CONTRIBUTING.md asks that user content is never dropped. The search now uses the text parts of a list (and is skipped when there are none), and the context is added as a leading `{"type": "text"}` part while the original parts are kept as they are. String content is handled exactly as before. Refs #4362 ## 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/memory/wrapper.py`: new module helper `_text_of(content)` (a string as is, or the `text` of the list's `type: "text"` parts joined by newlines); `_inject_memories` searches on it and returns the messages unchanged when it is empty; when the first user message's content is a list, the context is inserted as a new first text part instead of formatting the list into a string. - `tests/test_memory_wrapper.py`: `test_inject_memories_keeps_multimodal_content_parts` (parts kept, context as a leading text part, search query is the text part) and `test_inject_memories_skips_search_for_content_without_text` (image-only content: no search, messages unchanged). ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) (run on `headroom/memory/wrapper.py` only, see below) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text # fail before (source at |
||
|
|
5ccec67f0c |
fix(memory): tolerate None message content in inline memory parser (#3211)
## Description `InlineMemoryWrapper.chat` / `.chat_with_response` read `response.choices[0].message.content` and hand it straight to `parse_response_with_memory`, which immediately does `re.search(pattern, response_text, ...)`: ```python raw_content = response.choices[0].message.content # str | None parsed = parse_response_with_memory(raw_content) ... match = re.search(memory_pattern, response_text, re.DOTALL | re.IGNORECASE) ``` A chat completion's `message.content` is `None` whenever the model answers with tool/function calls instead of text. `re.search` on `None` raises `TypeError: expected string or bytes-like object, got 'NoneType'`, crashing the whole request. So enabling tools on any request routed through the inline memory wrapper turns the first tool-call turn into an unhandled exception. This is the same robustness class the function already guards for the `<memory>` block payload (a non-object / non-list JSON is tolerated at `inline_extractor.py:132`-`:143`); `None` content was the remaining gap, one level up. ## 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/memory/inline_extractor.py`: `parse_response_with_memory` now returns early when `response_text` is not a `str`, preserving the payload unchanged (so a `None` tool-call turn is not coerced to text) with no memories extracted. Widened its parameter to `str | None` and `ParsedResponse.content` / `.raw` and the two `chat*` return types to `str | None` to match what a chat completion actually yields. - `tests/test_memory_wrapper.py`: added `test_parse_response_with_memory_tolerates_none_content` next to the existing non-object-block regression. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality ### Test Output ```text tests/test_memory_wrapper.py -> 7 passed in 0.78s uvx ruff@0.16.2 check headroom/memory/inline_extractor.py tests/test_memory_wrapper.py -> All checks passed! uvx mypy@1.20.2 headroom/memory/inline_extractor.py -> Success: no issues found in 1 source file ``` ## Real Behavior Proof - Environment: Windows 11, Python 3.12.11, project venv, pytest 9.1.1, ruff 0.16.2 and mypy 1.20.2 via uvx. - Exact command / steps: before the fix, `parse_response_with_memory(None)` raised `TypeError: expected string or bytes-like object, got 'NoneType'`. After the fix, the same call returns `ParsedResponse(content=None, memories=[], raw=None)`, and a normal text response with a `<memory>` block still parses (`content == "hello world"`, memories extracted). - Observed result: `None` content degrades gracefully and stays `None`; the string path is byte-for-byte unchanged. - Not tested: no live `chat.completions.create` tool-call round trip (the wrapper takes an injected client); the `None` content a tool-call turn produces is reproduced directly. ## Runtime Rollout Safety - Rollout-managed feature(s): none. `InlineMemoryWrapper` is an SDK-side convenience wrapper (`headroom.memory`), not a rollout-channel-gated runtime feature. - Minimum rollout channel: N/A. - Stable/default behavior changed: no. Non-`None` string inputs take exactly the previous path; only the previously-crashing `None` case changes, from `TypeError` to a graceful no-op parse. - Kill switch / disable path: N/A. - Unsafe override required: no. - Qualification impact: none; text responses are handled identically. - Rollback path: revert this PR; the parser returns to assuming a `str`. ## 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: internal behavior, unchanged semantics) - [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` |
||
|
|
e00c6ff81c |
fix(memory): don't crash inline memory extraction on a non-object <memory> block (#2470)
## Description
`parse_response_with_memory` extracts an inline `<memory>...</memory>`
block from a model response and parses its JSON:
```python
try:
data = json.loads(memory_json)
memories = data.get("memories", [])
except json.JSONDecodeError as e:
logger.warning(f"Failed to parse memory JSON: {e}")
```
The block content is fully model-controlled. `json.loads` succeeds on
any valid JSON, including a non-object such as a bare array
(`<memory>["x"]</memory>`), a string, or a number. `data.get("memories",
[])` then raises `AttributeError: 'list' object has no attribute 'get'`,
which the `except json.JSONDecodeError` does not catch, so the inline
memory path crashes on output a model can realistically produce.
## Fix
Guard the parsed value: read `memories` only when the block is a JSON
object, and accept it only when it is a list (logging and ignoring
otherwise). Malformed JSON is still handled by the existing decode
guard, and a well-formed object is unchanged. This mirrors the
non-object hardening already applied to the batch JSONL path.
## 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/memory/inline_extractor.py`: only read `memories` from a
dict-typed parsed block, and only when the field is a list; log and
ignore other shapes.
- `tests/test_memory_wrapper.py`: regression covering a non-object
memory block, a non-list `memories` field, malformed JSON, and a
well-formed 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_wrapper.py -q
6 passed
# with the fix reverted, the new test fails with
# AttributeError: 'list' object has no attribute 'get'
$ uvx ruff@0.15.17 check headroom/memory/inline_extractor.py tests/test_memory_wrapper.py
All checks passed!
$ uvx mypy@1.20.2 --ignore-missing-imports headroom/memory/inline_extractor.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 the real `parse_response_with_memory`
with a `<memory>["x"]</memory>` block, a `{"memories": "nope"}` block, a
malformed block, and a valid block; then reverted `inline_extractor.py`
and re-ran.
- Observed result: with the fix all four return cleanly (empty memories
for the bad shapes, the parsed list for the valid one) and the memory
block is still stripped from the content; with the fix reverted the
non-object block raises `AttributeError: 'list' object has no attribute
'get'`. Ran against the actual module.
- Not tested: a live end-to-end chat where a model emits a non-object
memory block.
## 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
|
||
|
|
a5a4486a30 |
test: apply linux ruff formatting
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |
||
|
|
6e9ea54a04 |
test: fix PR formatting and cover log compressor
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |
||
|
|
38bf3e639c |
test: expand coverage across helper slices
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |