mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description
`inject_memory_instruction` appends the memory-extraction instruction to
the system prompt like this:
```python
if msg.get("role") == "system":
messages[i] = {**msg, "content": msg.get("content", "") + instruction}
```
`instruction` is a `str`. But a system message's `content` can be a
**list** of content-part dicts — OpenAI allows `content` as a list of
`{"type":"text",...}` parts, and Anthropic system prompts are commonly a
list of text blocks. `list + str` raises `TypeError: can only
concatenate list (not "str") to list`, which propagates out of the
inline-memory path and fails the whole request.
Confirmed:
```python
inject_memory_instruction([{"role":"system","content":[{"type":"text","text":"You are X."}]}])
# -> TypeError: can only concatenate list (not "str") to list
```
Fix: append through a small `_append_instruction` helper — when
`content` is a list, add the instruction as a trailing
`{"type":"text","text":instruction}` part; when it's a `str`,
concatenate as before; otherwise fall back to a string join. The
original messages list is not mutated (the function already copies).
## 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`: added
`_append_instruction(content, instruction)` handling list / str / other
content shapes; `inject_memory_instruction` uses it instead of the bare
`+`.
- `tests/test_inline_extractor_inject.py` (new): str content
concatenates, list content gets a trailing text part (short and long
instruction), missing-system prepends the default, and the original
messages are not mutated.
## 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_inline_extractor_inject.py -> 5 passed
uvx ruff@0.16.8 check headroom/memory/inline_extractor.py tests/test_inline_extractor_inject.py -> All checks passed!
uvx mypy@1.20.2 headroom/memory/inline_extractor.py -> clean (no error in this file)
```
## Real Behavior Proof
- Environment: Windows 11, Python 3.12.11, project venv, pytest 9.1.1,
ruff 0.16.8 and mypy 1.20.2 via uvx.
- Exact command / steps: called `inject_memory_instruction` with a
list-shaped system content — before the fix it raised `TypeError`;
after, the system content is a list with the original block(s) plus a
trailing `{"type":"text","text":<instruction>}` part. Verified the str
path still concatenates and the no-system path still prepends.
Red/green: the three list-content tests fail with the fix reverted
(TypeError); all 5 pass with it.
- Observed result: inline memory injection no longer crashes on
list-shaped system prompts (the shape Claude Code / the Anthropic SDK
send).
- Not tested: a live wrapped-client round-trip (the injection shaping is
covered by unit tests).
## Runtime Rollout Safety
- Rollout-managed feature(s): none.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: no for str content (byte-identical
concatenation); list content now gets the instruction as a text part
instead of raising.
- Kill switch / disable path: N/A.
- Unsafe override required: no.
- Qualification impact: none.
- Rollback path: revert this commit; list-shaped system content again
raises TypeError.
## 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 injection helper)
- [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
Appending a trailing text part mirrors how the parser side already
tolerates non-string content; the instruction stays a distinct block so
it's easy to see in the assembled system prompt.
Co-authored-by: JD Davis <mxjerrett@gmail.com>
62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
"""Tests for inline_extractor.inject_memory_instruction system-prompt handling.
|
|
|
|
The system prompt's ``content`` can be a plain string or a list of content-part
|
|
dicts (OpenAI allows a list; Anthropic system prompts are commonly a list of
|
|
``{"type": "text", ...}`` blocks). The instruction must be appended without
|
|
crashing on the list form.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from headroom.memory.inline_extractor import (
|
|
MEMORY_INSTRUCTION,
|
|
MEMORY_INSTRUCTION_SHORT,
|
|
inject_memory_instruction,
|
|
)
|
|
|
|
|
|
def test_str_system_content_is_concatenated() -> None:
|
|
out = inject_memory_instruction(
|
|
[{"role": "system", "content": "You are X."}, {"role": "user", "content": "hi"}],
|
|
short=True,
|
|
)
|
|
assert out[0]["content"] == "You are X." + MEMORY_INSTRUCTION_SHORT
|
|
|
|
|
|
def test_list_system_content_appends_a_text_part() -> None:
|
|
"""Regression: a list-shaped system content used to raise
|
|
``TypeError: can only concatenate list (not "str") to list``."""
|
|
out = inject_memory_instruction(
|
|
[
|
|
{"role": "system", "content": [{"type": "text", "text": "You are X."}]},
|
|
{"role": "user", "content": "hi"},
|
|
],
|
|
short=True,
|
|
)
|
|
content = out[0]["content"]
|
|
assert isinstance(content, list)
|
|
assert content[0] == {"type": "text", "text": "You are X."}
|
|
assert content[-1] == {"type": "text", "text": MEMORY_INSTRUCTION_SHORT}
|
|
# The original list is not mutated in place.
|
|
assert len(content) == 2
|
|
|
|
|
|
def test_list_system_content_long_instruction() -> None:
|
|
out = inject_memory_instruction(
|
|
[{"role": "system", "content": [{"type": "text", "text": "sys"}]}],
|
|
short=False,
|
|
)
|
|
assert out[0]["content"][-1] == {"type": "text", "text": MEMORY_INSTRUCTION}
|
|
|
|
|
|
def test_missing_system_prepends_default() -> None:
|
|
out = inject_memory_instruction([{"role": "user", "content": "hi"}], short=True)
|
|
assert out[0]["role"] == "system"
|
|
assert MEMORY_INSTRUCTION_SHORT in out[0]["content"]
|
|
|
|
|
|
def test_original_messages_not_mutated() -> None:
|
|
original = [{"role": "system", "content": [{"type": "text", "text": "sys"}]}]
|
|
inject_memory_instruction(original, short=True)
|
|
assert original[0]["content"] == [{"type": "text", "text": "sys"}]
|