fix(proxy): track response.-prefixed item and content-part events in Responses SSE state (#4695)

## Description

`ResponseState::apply` in
`crates/headroom-proxy/src/sse/openai_responses.rs` matched
`output_item.added`, `output_item.done`, `content_part.added` and
`content_part.done` only in their bare form. Every other arm of the same
`match` accepts both the `response.`-prefixed name and the bare name
(for example `"response.output_text.delta" | "output_text.delta"`). The
prefixed names for these four events therefore reached the `other =>`
arm: one `sse_unknown_event` warning per event and no item or
content-part state. The repository's own Python emitter
(`headroom/proxy/handlers/openai.py`) writes the prefixed names. This PR
accepts both forms in the four arms.

Fixes #4687

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

- `crates/headroom-proxy/src/sse/openai_responses.rs`: the four
bare-only arms now also match `response.output_item.added`,
`response.output_item.done`, `response.content_part.added`,
`response.content_part.done` (4 lines).
- `crates/headroom-proxy/tests/sse_openai_responses.rs`: new test
`prefixed_item_and_content_part_events_update_state`; the existing
bare-name tests are unchanged.

## Testing

- [x] Unit tests pass (`pytest`): not applicable, Rust-only change;
`cargo test -p headroom-proxy` below
- [x] Linting passes (`ruff check .`): not applicable; `cargo fmt` and
`cargo clippy -D warnings` below
- [x] Type checking passes (`mypy headroom`): not applicable
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

Fail before (new test against unmodified `src`, base `12864fc8`):

```text
test prefixed_item_and_content_part_events_update_state ... FAILED
thread 'prefixed_item_and_content_part_events_update_state' panicked at crates\headroom-proxy\tests\sse_openai_responses.rs:252:5:
assertion `left == right` failed
  left: ""
 right: "message"
test result: FAILED. 8 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.27s
```

Pass after:

```text
test prefixed_item_and_content_part_events_update_state ... ok
test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.09s
```

Full crate suite, `cargo test -p headroom-proxy --locked --no-fail-fast`
(Windows 11): every test binary passes except
`integration_path_traversal`:

- `forwarder_keeps_requests_under_the_base_prefix` fails with `Os {
code: 10054, kind: ConnectionReset }` at
`integration_path_traversal.rs:48`. It fails the same way on unmodified
`main` (`12864fc8`, checked in a separate worktree) and on every rerun
here.
- `vertex_dot_segment_project_is_rejected_unsigned` failed once in the
full run with the same os error 10054 and passed on `main` and on three
reruns of the file on this branch, so I treat it as a flaky Windows
socket reset, not related to this change.

```text
cargo fmt -p headroom-proxy -- --check            exit 0
cargo clippy -p headroom-proxy --all-targets --locked -- -D warnings   exit 0
```

## Real Behavior Proof

- Environment: Windows 11, Rust 1.95.0 (pinned by
`rust-toolchain.toml`), branch from `main` at `12864fc8`,
`headroom-proxy` crate; no network, no live OpenAI API.
- Exact command / steps: a throwaway integration test (deleted before
commit) fed an SSE byte stream through the real `SseFramer` and
`ResponseState::apply` with `cargo test -p headroom-proxy --test <tmp>
-- --nocapture`. The six events use the `response.`-prefixed names and
the payload shapes copied from the repository's Python emitter
(`headroom/proxy/handlers/openai.py`, around lines 1720-1745):
`response.output_item.added`, `response.content_part.added`,
`response.output_text.delta`, `response.output_text.done`,
`response.content_part.done`, `response.output_item.done`. I printed the
resulting item state.
- Observed result: before the change (library built from unmodified
`main`): `ITEM msg_1: type="" complete=false status=null parts={}
text="hello"` (only the delta, which was already matched, registered).
After the change: `ITEM msg_1: type="message" complete=true
status="completed" parts={0: ContentPartState { part_type:
"output_text", text: "hello", complete: true }} text="hello"`.
- Not tested: a live OpenAI Responses stream (no live stream was used;
the shapes come from the repository's own emitter); the `tracing`
warning count itself (the unknown-event arm cannot update state, so
state tracking is the observable proof); which consumers read item and
content-part state.

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: only for streams that use the
prefixed names: item and content-part state is now tracked, and the
per-event `sse_unknown_event` warning no longer fires for them. The
stream bytes forwarded to the client are unchanged.
- Kill switch / disable path: none needed (no new feature or config)
- Unsafe override required: no
- Qualification impact: none expected
- Rollback path: revert this 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] I have made corresponding changes to the documentation (none
needed)
- [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 (except
the Windows-only `integration_path_traversal` failure that also fails on
`main`, see above)
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

N/A

## Additional Notes

- The other arms already accept both forms; this change makes the four
item and content-part arms consistent with them. No handler function
changed.
- Not checked: how much of the item and content-part state is read by
other code, so how visible the effect is to users.

Produced with AI assistance (Claude Code) operated by me (HetCreep); the
tests and proof above were run on my machine against a clone of the
repository.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4695"
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: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-11-07 20:36:34 -06:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 33f067a243
commit 8f4122f4e5
2 changed files with 49 additions and 4 deletions
@@ -142,10 +142,10 @@ impl ResponseState {
match name {
"response.created" => self.on_response_created(&v),
"response.in_progress" => Ok(()),
"output_item.added" => self.on_output_item_added(&v),
"output_item.done" => self.on_output_item_done(&v),
"content_part.added" => self.on_content_part_added(&v),
"content_part.done" => self.on_content_part_done(&v),
"response.output_item.added" | "output_item.added" => self.on_output_item_added(&v),
"response.output_item.done" | "output_item.done" => self.on_output_item_done(&v),
"response.content_part.added" | "content_part.added" => self.on_content_part_added(&v),
"response.content_part.done" | "content_part.done" => self.on_content_part_done(&v),
"response.output_text.delta" | "output_text.delta" => self.on_output_text_delta(&v),
"response.output_text.done" | "output_text.done" => self.on_output_text_done(&v),
"response.function_call_arguments.delta" | "function_call_arguments.delta" => {
@@ -224,3 +224,48 @@ fn minimal_upstream_response_pr_c4() {
// machine status. Genuine completion goes through `response.completed`.
assert_eq!(s.status, StreamStatus::Open);
}
/// The `response.`-prefixed item / content-part names (the form the
/// Python emitter in `headroom/proxy/handlers/openai.py` writes) must
/// update state exactly like the bare names, not fall into the
/// unknown-event arm (which never updates state).
#[test]
fn prefixed_item_and_content_part_events_update_state() {
let mut s = ResponseState::new();
let raw = concat!(
"event: response.created\n",
"data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_p\",\"model\":\"gpt-5\"}}\n\n",
"event: response.output_item.added\n",
"data: {\"type\":\"response.output_item.added\",\"output_index\":0,\"item\":{\"id\":\"msg_P\",\"type\":\"message\",\"status\":\"in_progress\",\"content\":[]}}\n\n",
"event: response.content_part.added\n",
"data: {\"type\":\"response.content_part.added\",\"item_id\":\"msg_P\",\"output_index\":0,\"content_index\":0,\"part\":{\"type\":\"output_text\",\"text\":\"\"}}\n\n",
"event: response.output_text.delta\n",
"data: {\"type\":\"response.output_text.delta\",\"item_id\":\"msg_P\",\"output_index\":0,\"content_index\":0,\"delta\":\"hi\"}\n\n",
"event: response.content_part.done\n",
"data: {\"type\":\"response.content_part.done\",\"item_id\":\"msg_P\",\"output_index\":0,\"content_index\":0,\"part\":{\"type\":\"output_text\",\"text\":\"hi\"}}\n\n",
"event: response.output_item.done\n",
"data: {\"type\":\"response.output_item.done\",\"output_index\":0,\"item\":{\"id\":\"msg_P\",\"type\":\"message\",\"status\":\"completed\"}}\n\n",
);
run(&mut s, raw.as_bytes());
let item = s
.items
.get("msg_P")
.expect("prefixed output_item.added must register the item");
assert_eq!(item.item_type, "message");
assert!(
item.complete,
"prefixed output_item.done must complete the item"
);
assert_eq!(item.metadata["status"], "completed");
let part = item
.content_parts
.get(&0)
.expect("prefixed content_part.added must register the part");
assert_eq!(part.part_type, "output_text");
assert!(
part.complete,
"prefixed content_part.done must complete the part"
);
assert_eq!(item.output_text, "hi");
}