Commit Graph
1 Commits
Author SHA1 Message Date
Abhay SinghandJD Davis 717527bc86 fix(memory): handle list-shaped system content in inline memory injection (#3794)
## 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>
2026-09-30 11:08:53 -05:00