Skip to content

perf(response-stream): prune unreachable paths and cache blocking edges - #271

Merged
laipz8200 merged 4 commits into
langgenius:mainfrom
amine-y:perf/response-stream-path-preparation
Nov 16, 2026
Merged

laipz8200 merged 4 commits into
langgenius:mainfrom
amine-y:perf/response-stream-path-preparation

Conversation

@amine-y

@amine-y amine-y commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Search existing issues and pull requests to confirm this change is not a duplicate
  3. Open or identify the issue this pull request resolves or advances
  4. Use a Conventional Commits title for this pull request, and mark breaking changes with !
  5. Remember that the pull request title will become the squash merge commit message
  6. If CLA Assistant prompts you, sign CLA.md in the pull request conversation

Related Issue

Refs langgenius/dify#41851. Also related to langgenius/dify#38852.

Summary

Response-stream initialization prepares paths before the first graph-start event.
For each response node, the current DFS explores branches that cannot reach it;
branch/merge-heavy graphs can therefore incur exponential useless traversal.
Blocking-edge classification is also repeated for edges shared by many paths.

This change:

  • Prunes forward traversal using reverse reachability for the target response.
  • Caches blocking-edge classification only within one response calculation.
  • Preserves complete-path ordering, multiplicity, cycle avoidance and blocking-path order.
  • Leaves streaming event handling, snapshot state and graph scheduling unchanged.

The regression tests use synthetic graphs only: no private workflows, prompts,
credentials, customer data or infrastructure identifiers are included.

Evidence and Limits

The equivalent backport was measured on Dify 1.15.0 / Graphon 0.5.3 before adapting
it to this repository's current main. A 157-node, 203-edge graph with nine response
nodes produced exactly the same 49,182 ordered blocking paths before and after.
A matched initialization comparison improved from 6.709 s to 0.409 s. Five direct
staging API requests subsequently started in 0.705-1.669 s, completed without node
exceptions, and preserved multi-turn recall and a tool-driven authentication-entry
branch. These are diagnostic samples, not a capacity/p95 benchmark or timings for
the current-main test runner.

Complete-path enumeration can still be exponential when all those paths genuinely
reach the target. This PR deliberately retains that representation; it does not
claim to fix every workflow latency source.

Synthetic operation counts are separate from those wall-clock samples: a
22-node/27-edge dead-branch graph makes 255 adjacency calls before and 24 after
(22 reverse-index scans plus two forward expansions), returning one identical
complete path. A shared-edge diamond classifies five distinct edges instead of
six path-edge occurrences. Peak live path-object count and peak memory have not
been measured; no peak-memory reduction is claimed.

Correctness Contracts

Both traversals use the same configured get_outgoing_edges() topology. Runtime
edge state and conditional handles do not filter that method; variable selectors
affect blocking classification, not path eligibility. Pruning does not introduce
early condition evaluation. A reverse-reachability superset remains conservative;
excluding any edge available to forward traversal would violate the contract.

The blocking cache is local to one _build_paths_map() call, with a fixed graph
and response selector set. It is not shared across response nodes, retained across
initializations, or serialized into snapshots.

No Dify dependency bump is included. After a Graphon release, Dify can consume the
fix through its normal dependency update; this PR should not prematurely close
the Dify tracking issue.

Validation

Validated on Python 3.12.11 against main at 788086fe16ab46b2ba290bb48873b6145862377c:

  • pytest -n 2 --tb=short: 798 passed.
  • Existing and new response-filter tests: 62 passed.
  • uv lock --check, ruff format --check, ruff check --no-fix, and ty check: passed (the recipe-equivalent checks; just is not installed locally).
  • The synthetic dead-branch regression reduces outgoing-edge lookups from 255 to 24 without wall-clock thresholds.
  • 24 deterministic cyclic graphs are compared against an independent unpruned DFS, including exact path order; seeded path/visited-state behavior is also checked.
  • Review follow-up adds explicit parallel conditional handles, skipped-edge topology, response-specific selectors/reachability, selector changes across reinitialization, and root/back-edge paths. Assertions cover exact paths and blocking decisions.

The upstream Python 3.12/3.13 CI matrix remains the final platform check.

Checklist

  • This pull request links the issue it resolves or advances
  • This pull request title follows Conventional Commits, and any breaking change is marked with !
  • If CLA Assistant prompted me, I signed CLA.md in the pull request conversation

Copilot AI balanced review requested due to automatic review settings September 5, 2026 10:59

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

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

@amine-y

amine-y commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the contribution. Before we can review or merge this pull request, please read our CLA and add this exact comment once: I have read the CLA Document and I hereby sign the CLA

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

You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

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

@amine-y

amine-y commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

recheck

@amine-y

amine-y commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

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

@amine-y

amine-y commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

recheck

Copilot AI review requested due to automatic review settings September 5, 2026 11:17

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@wylswz
wylswz requested a review from laipz8200 September 7, 2026 01:49
@amine-y

amine-y commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi @laipz8200, Could you please review this when you have time?

Copilot AI balanced review requested due to automatic review settings October 19, 2026 15:55

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@quhaobo11

Copy link
Copy Markdown

Thanks for working on this! In an offline A/B test on the Graphon 0.8.0 revision pinned by Dify 1.19.0, adapting this change brought median ResponseStreamFilter.initialize() time on our branch-heavy workflow from about 22 seconds to under 1 second, with identical ordered blocking paths.

This would substantially reduce startup latency for our project. We’d appreciate it if a maintainer could approve the pending CI run and review this PR when possible.

@amine-y

amine-y commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

thanks @quhaobo11 , I hope that they merge this soon

Bring the repository knowledge review from langgenius#394 into PR langgenius#271 to resolve the documentation freshness failures. Preserve the response-stream path pruning and per-response blocking-edge cache while integrating the current main branch.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 04:06

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@laipz8200 laipz8200 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.

LGTM, thank you for your contribution!

@laipz8200
laipz8200 merged commit 5b9c6e0 into langgenius:main Nov 16, 2026
6 checks passed
@QuantumGhost

QuantumGhost commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

AI-assisted analysis and drafting with Codex using gpt-6.1-sol.


I tested the final PR head, 2f555de19f652743ebce86cb2035a67a5e27ef73, in an isolated temporary worktree. The results confirm both the improvement and the remaining limitation already documented in the PR.

Results

  • The response-stream filter and PR performance tests passed: 68 passed.
  • On the synthetic dead-branch graph (22 nodes, 27 edges), outgoing-edge lookups decreased from 255 with the unpruned reference DFS to 24 with this PR.
  • On a synthetic DAG with 41 conditional stages, where both handles at each stage converge on the next node, there are 43 nodes, 83 edges, and 2^41 = 2,199,023,255,552 structural edge paths to the End node. Raw engine execution completed in approximately 21 ms, with 43 node starts. With ResponseStreamFilter enabled, DSL loading completed, but the child process produced no first graph event before its 3-second timeout and was terminated.

The timing is a single local diagnostic sample, not a capacity benchmark. No workflow YAML or customer content is included here.

Smaller instances of the same all-paths-reach-End topology produced these counts:

Conditional stages Materialized blocking paths Recursive walk() calls _is_blocking_edge() calls
12 4,096 8,192 25
16 65,536 131,072 33
18 262,144 524,288 37

Complexity

Building the reverse adjacency index costs O(V + E), and the per-response cache reduces blocking classification to at most one call per distinct path edge. However, _find_all_paths() still materializes every complete path, and _build_paths_map() still constructs a blocking-path object for each one.

If P is the number of paths to a response and L is their average length, materializing the complete paths alone requires at least Omega(P * L) time and storage; copying path and visited state adds further traversal cost. For the n-stage topology above, P = 2^n and L grows linearly with n, so initialization time and retained path storage remain Theta(n * 2^n).

_handle_edge_taken() also continues to scan the retained path list. In this topology, its worst-case cost is Theta(n * 2^n) per event, or Theta(n^2 * 2^n) across the n selected branch edges.

Reverse-reachability pruning is useful when large subgraphs cannot reach the response. It cannot prune this topology because every branch can reach the same End. A follow-up using a compact response-eligibility representation, such as incremental reachability, is still needed to eliminate this remaining exponential case.

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.

5 participants