Skip to content

perf(response-stream): replace path enumeration with O(V+E) reachability gating - #399

Closed
tang-ontheway wants to merge 2 commits into
langgenius:mainfrom
tang-ontheway:fix/response-stream-polynomial-gating
Closed

tang-ontheway wants to merge 2 commits into
langgenius:mainfrom
tang-ontheway:fix/response-stream-polynomial-gating

Conversation

@tang-ontheway

Copy link
Copy Markdown

Fixes #398

Summary

  • Replaced per-path enumeration (_find_all_paths during _register_response) in ResponseStreamFilter's _FrameSession with O(V + E) edge classification (_compute_blocking_edges) and BFS reachability (_is_response_unblocked) over non-skipped, non-blocking-or-taken edges.
  • Added taken_edges to v2.Snapshot (src/graphon/engine/filter/builtin/response_stream/v2.py) so mid-traversal snapshots stay O(V + E) in size without materializing exponential paths_map lists, while preserving backward compatibility with v1/v2 snapshots that carry explicit paths_map entries.
  • Updated tests/engine/test_response_stream_filter_performance.py and added test_response_filter_handles_diamond_dag_without_path_enumeration (12-stage diamond DAG, 4,096 paths) asserting deterministic graph-query budget, compact snapshot size, and mid-traversal snapshot restore + final chunk output.

Testing

  • uv run pytest tests/engine/test_response_stream_filter.py tests/engine/test_response_stream_filter_performance.py -q -> 69 passed
  • uv run ruff check src/graphon/engine/filter/builtin/response_stream/ tests/engine/test_response_stream_filter_performance.py -> All checks passed!
  • uv run ty check src/graphon/engine/filter/builtin/response_stream/ -> All checks passed!

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

All contributors on this pull request have signed the CLA.
Posted by the CLA Assistant Lite bot.

@tang-ontheway

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@QuantumGhost QuantumGhost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new representation removes the exponential path enumeration reported in #398, but two issues need to be addressed before merging: the persisted snapshot compatibility break (P1) and the repeated-traversal performance regression (P2). See the inline comments for reproductions and deterministic operation counts.

AI disclosure: This review was drafted with Codex using gpt-6.1-sol and is submitted under the human reviewer's direction and responsibility.

version: Literal["2.0"] = "2.0"
# Missing metadata retains the historical paired-graph skip lookup.
skipped_edges: list[str] | None = None
taken_edges: list[str] = Field(default_factory=list)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P1] Version the new persisted representation instead of reusing 2.0

This changes the meaning of the persisted state, rather than merely adding an optional field: newly initialized sessions now write an empty paths_map and rely on taken_edges plus graph reachability. The existing 2.0 reader accepts that snapshot, ignores the new field, and restores no blocking paths. Pending responses then never activate, even after their blocking edges are taken, without any error indicating an incompatible snapshot.

Reproduced against base 069cd554: buffer hello before the blocking edge, save a snapshot with this PR, restore it, and deliver the edge-taken event. This PR's reader emits hello; the base reader accepts the same 2.0 snapshot and emits nothing. Loading an old snapshot with the new reader works, but that does not protect mixed-version workers or rollback.

A workflow with multiple HITL nodes can remain alive across deployments and suspend/resume repeatedly. Any such boundary can hand a new-format instance to an old reader, so silently accepting an incompatible format is a correctness issue for persisted workflows. This is a cross-version risk; the reproduction does not imply that repeated suspensions within the same new version fail.

Please write the new representation as a new snapshot version (for example, v3), keep the v1/v2 loading and migration paths, and cover version rejection and repeated suspend/resume recovery.

has_reachable_path = True

if has_reachable_path:
if self._is_response_unblocked(response_node_id):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] Avoid restarting a full reachability walk for every taken edge

Every traversal event now restarts BFS from the root for every pending response, including events on nonblocking edges that cannot change its eligibility. Consider an N-edge linear graph whose only blocking edge is the final branch before Answer: handling the N edge events performs N full walks. This is quadratic with indexed root adjacency. In child frames, _GraphDefinition.get_outgoing_edges() scans the entire edge dictionary on each query, making the same event-handling phase cubic.

A deterministic counter using the actual child _GraphDefinition and _FrameSession produces 10,000 / 40,000 / 160,000 adjacency queries for N = 100 / 200 / 400. Because each query tests all N edges, that is 1,000,000 / 8,000,000 / 64,000,000 edge-tail tests during edge-event handling alone, excluding initialization and initial activation. The previous implementation needs only one blocking-path update per event on this single-path graph. The existing diamond test does not exercise this regression.

Please retain and advance reachability incrementally per response, and avoid the full-edge scan in child adjacency lookup. Add a deterministic work-budget regression for a long nonblocking prefix followed by a blocking branch, covering the complete traversal rather than initialization alone.

@tang-ontheway

tang-ontheway commented Nov 16, 2026 •

Copy link
Copy Markdown
Author

Thanks for the detailed review @QuantumGhost! Both points are addressed in b8a1949:

  1. [P1] Snapshot versioning (v3.0): Reverted v2.py to its original schema and introduced v3.py (version: "3.0", taken_edges) for reachability-gated sessions while keeping v1/v2 loading and migration paths intact, with tests covering v2.loads rejection of 3.0 snapshots and repeated suspend/resume recovery.
  2. [P2] Incremental reachability & child outgoing-edge indexing: Added _ResponseGate (unblocked_nodes, frontier_edges, traversed_edges) so nonblocking edge events perform zero graph queries and blocking edge events expand only from the unblocked frontier head; indexed _outgoing_by_node in _GraphDefinition; and added a deterministic work-budget regression test for a 100-edge nonblocking prefix before a blocking branch.

@tang-ontheway

Copy link
Copy Markdown
Author

Thanks @QuantumGhost for the detailed review and deterministic reproductions!

Both issues have been addressed in commit b8a1949:

  1. P1 (Snapshot versioning): Added a dedicated v3 persisted representation (src/graphon/engine/filter/builtin/response_stream/v3.py) instead of reusing 2.0, while preserving v1/v2 loading and migration paths and adding tests for version handling and suspend/resume recovery.
  2. P2 (Incremental reachability & indexed adjacency): Updated frame_graph.py and frame_session.py to index outgoing edges without full-dict scans and maintain incremental reachability per response across edge events rather than restarting BFS from the root on every event, along with a deterministic work-budget regression test in tests/engine/test_response_stream_filter_performance.py.

Verified locally with uv run pytest tests/ -k response_stream (101 passed).

@tang-ontheway

Copy link
Copy Markdown
Author

Closing as superseded by #400, which has been merged to fix #398. Thanks for the review!

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.

Response-stream initialization remains exponential when all paths reach a response

2 participants