Repository navigation
Studio: stream durable chat run snapshots without re-parsing the stored request - #16302
Conversation
…ed request Event streams check the run with its state row, and run frames splice the stored request_json text into the encoded run instead of parsing and re-encoding it per event. The supervisor's first read takes only the worker token.
…st that is not one line of JSON
|
@codex review |
3 similar comments
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 946cf389f5
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| row = conn.execute( | ||
| """SELECT *, json_valid(request_json) AND instr(request_json, char(10))=0 | ||
| AND instr(request_json, char(13))=0 AS splice_ok | ||
| FROM chat_generation_runs WHERE id=?""", | ||
| (run_id,), | ||
| ).fetchone() |
There was a problem hiding this comment.
Preserve the fallback for SQLite builds without JSON1
On SQLite builds without JSON1, this unconditional json_valid call raises sqlite3.OperationalError: no such function: json_valid, terminating the events stream on its first non-chunk batch, normally run.created. The existing list_events(raw=True) explicitly handles this environment by retrying with splice_ok = 0, but the new snapshot query bypasses that fallback. Apply the same fallback here and encode the parsed run when JSON validation is unavailable; otherwise durable chat streams that previously worked on those builds now disconnect before delivering their run events.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 46ee4c9: get_run_frame now shares list_events' missing-json_valid fallback (one _execute_splice helper), re-encoding the parsed run; covered by test_run_frame_without_json1_encodes_the_parsed_run.
…ist_events' fallback
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
What changes
A durable chat run stores the whole sanitized request (all prior messages and tool outputs) as
request_json. During one turn, that text was parsed back into a dict on most run reads and then re-encoded inside every non-chunk SSE frame, even though nothing in those places reads the request.POST /api/chat-runs/{id}/eventsandPOST /api/chat-runs/{id}/cancelcheck that the run exists withget_run_staterather thanget_run. The events check ran synchronously on the event loop.get_run_frame. That returns the state plus the run already encoded as compact JSON, with the storedrequest_jsontext spliced in as is. The frame text is the same asjson.dumps(data, ensure_ascii=False, separators=(",", ":"))withdata["run"] = get_run(...). This holds becausecanonical_requestwritesrequest_jsonwith the same options and sorted keys, sojson.dumps(json.loads(text))returns the same text.create_runis the only writer. The splice is only used when SQLite reports the stored text asjson_validwith no raw CR or LF (the same guard as the durable replay path); any other row is encoded the old way, so a corrupt or hand-edited row still produces the base frame ("requestPayload":{}) instead of a broken SSE line.get_worker_token, since only the token is used before the fencedget_worker_runre-read.canonical_request,create_run, the stored bytes and every function that returns a run dict are unchanged.Results
One durable turn, CPU only: POST, then a live events stream through to finish. The turn has 60 chunks and 2 tool events, so 5 non-chunk frames. Time is the median of 5 reps spent in
json.loads/json.dumps/jsonable_encoder, split by event loop and worker thread.The table counts Python JSON calls only. The
json_validguard runs inside SQLite on the worker thread: building one run snapshot for a 1.2 MB request took 6.90 ms on base, 3.86 ms with this change (median of 60 interleaved reps), so the per-turn total gain is about 2 ms per non-chunk batch smaller than the table suggests. The event loop time does not include it.What remains is the request body parse, the POST response encoding,
canonical_request's two dumps, and the parses increate_run, the fencedget_worker_runandfinish_run.Tests
create_run. They cover unicode, emoji, control characters, escapes, large ints, floats (-0.0,5e-324,1e16), NaN and Infinity, nested containers and 5000-deep nesting. For each one the test compares the SSE stream text, theGET/cancel/finish_runrun dicts, the storedrequest_json, and the 404s for a missing run. The outputs are byte-identical.tests/test_chat_generation_events_reads.py: the read-count test now expects noget_runand 3get_run_framereads. There are new tests that frames equaljson.dumpsof the parsed run, including awkward values.tests/test_chat_generation_*.py,tests/test_unattended_runs.pyand 40 more test files that use the runs db or routes: 1942 passed on head. Base had 1938 passed, 0 failed.Not covered
json_valid, so a corrupt row that happens to be valid JSON5 would still be spliced there (same as the replay path).routes/chat_generation_runs.py; whichever lands second keeps both (Studio: stream durable chat events without re-parsing stored payloads #16159's raw payload prefix and this PR'sget_run_framerun text).create_runand encodes it on the loop.