Repository navigation
fix: parse Rehoboam SSE causality responses #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import httpx | ||
| import pytest | ||
|
|
||
| from server.tools._core.exceptions import ValidationError | ||
| from server.tools._core.handlers import _parse_response_payload | ||
|
|
||
|
|
||
| def test_parse_response_payload_preserves_json(): | ||
| response = httpx.Response(200, json={"is_causal": True}) | ||
|
|
||
| assert _parse_response_payload(response) == {"is_causal": True} | ||
|
|
||
|
|
||
| def test_parse_response_payload_returns_final_sse_result_data(): | ||
| body = "\n\n".join( | ||
| [ | ||
| 'data: {"type":"progress","step":"checking"}', | ||
| 'data: {"type":"result","data":{"is_causal":true,"suggestions":[]}}', | ||
| ] | ||
| ) | ||
| response = httpx.Response( | ||
| 200, | ||
| text=body, | ||
| headers={"content-type": "text/event-stream; charset=utf-8"}, | ||
| ) | ||
|
|
||
| assert _parse_response_payload(response) == { | ||
| "is_causal": True, | ||
| "suggestions": [], | ||
| } | ||
|
|
||
|
|
||
| def test_parse_response_payload_rejects_sse_without_result(): | ||
| response = httpx.Response( | ||
| 200, | ||
| text='data: {"type":"progress","step":"checking"}\n\n', | ||
| headers={"content-type": "text/event-stream"}, | ||
| ) | ||
|
|
||
| with pytest.raises(ValidationError, match="final result"): | ||
| _parse_response_payload(response) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: Subconscious-ai/ghostshell
Length of output: 26488
🌐 Web query:
WHATWG Server-sent events specification multiple data fields joined by newline blank line event dispatch💡 Result:
Under the WHATWG Server-sent events (SSE) specification, the dispatching of events and the handling of multiple data fields are governed by specific parsing rules [1]. Event Dispatching Events are dispatched only when a blank line is encountered in the event stream [1][2][3]. A blank line is defined as a line that is empty, meaning it consists of no characters or is simply a line terminator (U+000A LINE FEED, U+000D CARRIAGE RETURN, or the CRLF pair) [1][4]. When the parser reaches such a blank line, it triggers the dispatch of the accumulated event [1][2]. Handling Multiple Data Fields When a message contains multiple consecutive lines that begin with the field name data, the following process occurs: 1. Concatenation: The specification dictates that for every line beginning with data:, the field value is appended to the event's data buffer [1][3]. 2. Newline Insertion: A single U+000A LINE FEED character is appended to the data buffer after each individual data field value [3][4]. This effectively joins multiple data lines with a newline character [5][4]. 3. Final Processing: When the event is ultimately dispatched, the trailing newline character (the one added after the final data field) is removed from the data buffer [5][4]. For example, a stream formatted as: data: Line 1 data: Line 2 will result in an event with a data attribute containing the string "Line 1\nLine 2" [5][6]. The specification treats U+000A, U+000D, and the U+000D U+000A sequence as valid line terminators [1][4]. Consequently, a blank line can be formed by two consecutive line terminators of any of these types [1][4]. The parser reads these lines sequentially, ignoring comment lines (lines starting with a U+003A COLON character) and processing non-blank, non-comment lines to update the event's internal buffers (data, event type, and last event ID) before dispatching upon encountering the blank line [1][2].
Citations:
Parse complete SSE events before decoding JSON.
_api_requestsendstext/event-streamresponses to_parse_response_payload, which decodes eachdata:line separately. Standard SSE joins consecutivedata:lines until a blank-line separator. A multiline JSON event can therefore raiseValidationError("Backend returned malformed SSE data"). Buffer each event, join itsdata:fields, and decode the complete payload. Add a fixture that splits one result JSON object across twodata:lines.🤖 Prompt for AI Agents