Skip to content

fix: drain SSE buffers and key OpenAI tool-call index - #4

Merged
undivisible merged 3 commits into
mfrom
maintenance/stream-and-cache
Sep 16, 2026
Merged

undivisible merged 3 commits into
mfrom
maintenance/stream-and-cache

Conversation

@undivisible

@undivisible undivisible commented Sep 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Portkey and Cloudflare SSE parsers re-scanned the entire accumulated buffer on every chunk, re-emitting earlier TextDeltas (and [DONE]) with quadratic cost. They now drain completed newline-terminated frames the same way xAI already does.
  • OpenAI-compatible streaming keyed pending tool calls by choice index, so parallel delta.tool_calls[].index values collided. Parser now uses the tool-call index, with serde defaults for omitted delta fields (id/role/function.name).
  • CacheMiddleware includes max_steps in the cache key so agent-loop depth cannot return a cached one-shot result.

Tests

  • split_sse_chunks_emit_each_delta_once on Portkey and Cloudflare
  • parallel_tool_calls_use_tool_index_not_choice_index
  • different_max_steps_is_a_cache_miss
  • cargo test --workspace --all-features

Notes

Backward-compatible. Does not touch OAuth/auth. Safe to auto-merge after CI is green.

Made with Amp


Note

Medium Risk
Streaming and parallel tool-call parsing changes affect live provider behavior; cache key change only affects correctness of cached agent-loop results, not security.

Overview
Fixes SSE streaming for Cloudflare and Portkey: instead of re-parsing the full accumulated buffer on every chunk (which duplicated TextDelta events and scaled poorly), each chunk now drains only newline-complete frames via parse_stream_chunk, matching the incremental pattern used elsewhere.

For OpenAI-compatible streaming, pending tool calls are keyed by delta.tool_calls[].index (with choice index as fallback), so parallel tool streams no longer collide. API types gain serde(default) (and optional index) so partial tool-call deltas deserialize cleanly; outbound tool calls set index: None.

CacheMiddleware now hashes max_steps into the cache key so different agent loop depths cannot share a cached response.

Adds targeted tests for split SSE chunks, parallel tool-call indexing, and max_steps cache misses.

Reviewed by Cursor Bugbot for commit ba5661c. Configure here.

Pin rs_ai_oauth to workspace 0.2.35, run cargo test --all-features in CI
(matching Makefile), allow clippy::result_large_err on realtime/live
WebSocket error types so stable clippy stays green, and add .agents
setup/resume for Amp orbs.

Co-authored-by: Max Carter <undivisible@vk.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a0a5f1-add5-707e-9a8f-326da4a2366c
@cursor

cursor Bot commented Sep 15, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_467e4a13-5409-4727-8fc6-566c7e78dbba)

@mergify

mergify Bot commented Sep 15, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@undivisible
undivisible force-pushed the maintenance/stream-and-cache branch from 33b8ae0 to ba5661c Compare September 15, 2026 17:04
Portkey and Cloudflare were re-parsing the entire SSE buffer on every
chunk, duplicating TextDelta events with quadratic cost. Drain completed
lines like xAI. OpenAI-compatible streaming now keys pending tool calls
by delta.tool_calls[].index so parallel tools do not collide. Cache keys
include max_steps.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0a5f1-add5-707e-9a8f-326da4a2366c
Co-authored-by: Max Carter <undivisible@vk.com>
@undivisible
undivisible force-pushed the maintenance/stream-and-cache branch from a4f941c to 12e9cc5 Compare September 15, 2026 17:08
@undivisible
undivisible merged commit caf45ea into m Sep 16, 2026
10 of 11 checks passed
@undivisible undivisible mentioned this pull request Sep 16, 2026
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.

2 participants