Skip to content

Studio: stream durable chat run snapshots without re-parsing the stored request - #16302

Merged
danielhanchen merged 5 commits into
mainfrom
perf/r16-chat-runs-request-parse
Nov 1, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
perf/r16-chat-runs-request-parse

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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}/events and POST /api/chat-runs/{id}/cancel check that the run exists with get_run_state rather than get_run. The events check ran synchronously on the event loop.
  • The stream's opening check reads only the state.
  • A batch that has a non-chunk event reads get_run_frame. That returns the state plus the run already encoded as compact JSON, with the stored request_json text spliced in as is. The frame text is the same as json.dumps(data, ensure_ascii=False, separators=(",", ":")) with data["run"] = get_run(...). This holds because canonical_request writes request_json with the same options and sorted keys, so json.dumps(json.loads(text)) returns the same text. create_run is the only writer. The splice is only used when SQLite reports the stored text as json_valid with 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.
  • The supervisor's first read uses get_worker_token, since only the token is used before the fenced get_worker_run re-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.

request arm loads MB (loop / thread) dumps MB (loop / thread) JSON ms on loop JSON ms on threads total JSON ms
54 KB base 0.11 / 0.52 0.32 / 0.10 2.8 3.5 6.3
54 KB head 0.05 / 0.16 0.06 / 0.11 1.5 2.6 4.1
1.06 MB base 2.06 / 9.99 6.0 / 2.0 32.0 30.9 62.9
1.06 MB head 1.06 / 3.0 1.0 / 2.0 9.2 14.8 24.1
10.6 MB base 20.6 / 99.8 59.9 / 20.0 333.0 369.5 702.5
10.6 MB head 10.6 / 29.9 10.0 / 20.0 91.3 156.3 247.6

The table counts Python JSON calls only. The json_valid guard 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 in create_run, the fenced get_worker_run and finish_run.

Tests

  • A stored request that is not valid JSON, is truncated, or contains a raw LF or a trailing CR falls back to the old encoding (4 new cases; they fail without the guard).
  • Differential, base vs head: 300 generated requests written through 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, the GET / cancel / finish_run run dicts, the stored request_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 no get_run and 3 get_run_frame reads. There are new tests that frames equal json.dumps of the parsed run, including awkward values.
  • tests/test_chat_generation_*.py, tests/test_unattended_runs.py and 40 more test files that use the runs db or routes: 1942 passed on head. Base had 1938 passed, 0 failed.

Not covered

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

3 similar comments
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@danielhanchen
danielhanchen marked this pull request as ready for review November 1, 2026 09:45
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T10:28:35.066564Z 46ee4c9 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 8183d873d3

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +707 to +712
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 46ee4c90ff

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 2c2870f into main Nov 1, 2026
@danielhanchen
danielhanchen deleted the perf/r16-chat-runs-request-parse branch November 1, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant