Repository navigation
refactor(core): derive KrsEdge.cyclic at model construction so analyze() stops writing the AST - #3422
Conversation
…nalyze() (#2791) analyze() stamped edge.cyclic onto the KrsFile it was handed, so the same parsed model rendered differently before and after it ran. The cycle walk is now a pure findCyclicDependencies() under parser/, and markCyclicEdges() sets the flag while a model is built: in the Parser post-pass and again on the ImportResolver's merged model, where a cycle split across files shows up (TPL-2221). analyze() only takes the warnings. Diff renders opt out of cyclic styling so they stay as they were: every diff resolveStyles call leaves the cyclic virtual tag off, and diffEdgeArray copies only flagged edges without the flag, keeping the identity-keyed cross-system lookup intact. The per-hop edge scan in the walk now reads the hop's own outgoing edges, since the walk runs on every parse. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Deep-compares every parsed example and a cyclic fixture with its structuredClone after analyze, collectModelWarnings and each builder, and checks that a model renders the same on either side of analyze(). Fills in TPL-2791's related tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Updates the tags reference and its bundled skill copies, and adds the changeset for core, the CLI and the skills. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…flag-at-construction
…deep chains (#2791) The cycle walk now runs inside Parser.parse, where a recursive DFS threw RangeError on a long sync chain and turned a well-formed document into a parse failure (the nest submission gate, the LSP). The explicit-stack walk keeps the recursive visit order and one shared path, so the edge set, cyclePath and loc are unchanged; a differential run against main's walk over 400 random graphs matched. Adds the cases the review found unfenced: a 20000-service chain parses, and a flag a per-file parse set is cleared on the merged model. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ge-heavy chains (#2791) Marking a cycle walked every hop of the GRAY path per back edge, so a chain whose every node also calls the root took quadratic time, now on every parse (15000 services: 91 ms on main, 4.5 s here). A hop marked once has nothing left to add, so a downward union-find over path depths skips the hops already marked, and markCyclicEdges no longer builds the warnings or their cycle paths. Same edge set and warnings as main's walk over 3600 random graphs; the 15000-service chain parses in 126 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…whole out-list (#2791) Marking a hop scanned every outgoing edge of its source, so a hub calling k services that each call back took O(k^2) on every parse (20500 services: 116 ms on main, 1.1 s before this). The adjacency now also indexes edges by (from, to), and a hop visits only its own parallel edges; the walk still iterates the out-list, so the order is unchanged. Same edge set and warnings as main's walk over 3000 random graphs; the hub parses in 109 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Cycle discovery and model marking packages/core/src/parser/cyclic-dependencies.ts, packages/core/src/parser/parser.ts, packages/core/src/fs/import-resolver.ts, packages/core/src/parser/cyclic-dependencies.test.ts |
Cycle discovery returns cyclic edges and warnings. Parsing marks cycle edges, and import resolution re-marks edges after merging files. Tests cover large cycles and cycles across files. |
Warning analysis and read-pass behavior packages/core/src/resolver/warnings.ts, packages/core/src/resolver/warnings.test.ts, packages/core/src/resolver/model-immutability.test.ts, packages/core/src/renderer/svg-renderer.test.ts, docs/test-perspectives/TPL-2791-read-passes-do-not-write-the-model.md |
analyze() uses cycle discovery to report warnings without changing edge flags. Tests check model immutability and compare rendering before and after analysis. |
Diff views omit cyclic styling packages/core/src/resolver/style-resolver.ts, packages/core/src/compile/compile-diff.ts, packages/core/src/diff/view-diff.ts, packages/core/src/parser/cyclic-dependencies.test.ts, docs/spec/tags-annotations*.md, packages/skills/skills/*/reference/tags-annotations.md, .changeset/cyclic-flag-at-model-construction.md |
Diff style resolution disables the virtual cyclic tag, and diff edges have cyclic flags removed. Tests and tag references describe this behavior. The changeset declares patch releases for three packages. |
Estimated code review effort: 4 (Complex) | ~40 minutes
Change: Bug fix · Severity of issue fixed: Medium
Possibly related PRs
- kompiro/karasu#3406: Documents the cycle-marking design implemented in this pull request, including the diff-view opt-out.
Merge Risk: 
Minimal · up to 397c0
-
Cycle marking and diff rendering follow the documented design. No actionable issue remains to resolve before normal merge checks.
Security architecture risk: 
Low · up to 397c0
-
This is a contained change to when cycle information is computed and displayed. Construction avoids allocating diagnostic paths, and diff processing preserves source-model flags. No new privileged operation was identified. Resource limits for large untrusted diagrams remain unverified.
Retained concerns
No architecture-level concerns identified.Security review details
-
Security Blast Radius
- inferred — Attacker-controlled graph structure can influence computation, memory use, and rendered cycle state in the caller's process. The inspected change does not add external authority or establish a new tenant, service, or data-store access path. Deployment-specific shared-process exposure remains unestablished.
Trust Boundaries and Controls
- observed — Diff suppression is limited to derived edge presentation: flagged edges are copied without cyclic state, and the style option controls virtual-tag matching. Cycle-warning collection still uses the after model, so removing cycle styling does not remove that diagnostic path.
Resilience and Maintainability Implications
- observed — Warning-mode discovery can still allocate cycle paths whose aggregate size grows quadratically on adversarial back-edge graphs. The base analyzer already constructed these paths. New parse and merge calls explicitly disable them, so this existing resource concern is not established as introduced or worsened by the PR.
Hardening Proposals
- proposed — If untrusted diagrams are analyzed in a shared service, establish input and execution budgets or bound cycle-warning output. This would address the pre-existing diagnostic allocation behavior, not a verified new vulnerability in this PR.
-
Pre-merge checks: 
4
-
Full details: Title check
-
Explanation
The title follows the required Conventional Commits format, uses the allowed
refactor(core)type and scope, has an imperative subject without a trailing period, and accurately describes the main change.
Full details: Linked Issues check
-
Explanation
The PR addresses all coding requirements in
#2791. Parser and import resolution mark cyclic edges during model construction.analyze()reads cycle results without writing to the model. The reported immutability tests coveranalyze(), warning collection, and builders. The reported render tests compare output before and afteranalyze(). The reported example and Dify comparisons cover current entry points, including the documented Dify cycle change caused by correcting model-construction behavior. The implementation approach is not restricted by the issue acceptance criteria.
Full details: Out of Scope Changes check
-
Explanation
The changes stay within
#2791. The cycle-walk refactor, parser and import-resolver integration, diff styling opt-out, tests, documentation, skill copies, and changeset support immutable model construction and preserve diff behavior. No unrelated production feature or dependency change is identified.
-
Finishing Touches
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
detectCyclicDependencies left warnings.ts when #3422 moved the cyclic flag to model construction; the TPL body already names findCyclicDependencies and markCyclicEdges in parser/cyclic-dependencies.ts. Refs #3357 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ations (#3425) * docs(tpl): 2026-10 deprecation review, refresh 15 TPLs with stale citations Repoint line-number citations whose code moved (index.ts split in #2038, needsQuotes rewrite in #2707, diagLocFormatter in #2715, spec/CSS drift), drop citations of sites the VS Code shared viewer removed (#3312), record that the gh-aw consumer of TPL-2658/TPL-2786 left in #3423, and drop the "experimental" facet headings that language v2.0 removed (#2677). Refs #3357 Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(tpl): address local review on the 2026-10 TPL review Correct the BARE_ID_PATTERN history in TPL-1101 (the keyword copy was RESERVED_KEYWORDS), repoint TPL-239's drifted cli/index.ts:442 citation to program.parseAsync, drop an em dash in TPL-1503, and place TPL-2996's origin section before the known patterns like TPL-2658/TPL-2786. Refs #3357 Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(tpl): name #3423 as the gh-aw removal in absent-path markers #3421 tracks the revival; the workflows and their safety test were removed by #3423 (ADR-3421). Refs #3357 Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(tpl): drop TPL-2791's citation of the function #3422 removed detectCyclicDependencies left warnings.ts when #3422 moved the cyclic flag to model construction; the TPL body already names findCyclicDependencies and markCyclicEdges in parser/cyclic-dependencies.ts. Refs #3357 Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Purpose
Closes #2791
analyze()stampededge.cyclic = trueonto theKrsFileit was handed, so the same parsed model rendered differently before and afteranalyze()ran. This implements the approved Design Doc (docs/design/cyclic-flag-at-model-construction.md, #3406).Summary
KrsEdge.cyclicis now derived while a model is built: in the Parser's post-pass, and again by the ImportResolver on the merged model, where a cycle split across files first shows up (TPL-2221).analyze()only reads. The readers (edge[cyclic], layout, routing, draw.io) are unchanged. Diff renders opt out of cyclic styling, so their output stays the same.Changes
parser/cyclic-dependencies.ts(new):findCyclicDependencies(file)is the old walk made pure. It returns the cyclic edge set and the warnings.markCyclicEdges(file)clears the flag and then sets it from that set.analyze()alone used to see:RangeErroron a long sync chain.(from, to)index.cyclePath,loc), flagged edges and parse flags.parser/parser.tsandfs/import-resolver.ts: callmarkCyclicEdgesnext to theboundaryMembershipbuilds.resolver/warnings.ts:analyze()takesfindCyclicDependencies(file).warnings, anddetectCyclicDependenciesis removed.resolveStylesgets a trailingoptions.cyclicTag. EveryresolveStylescall incompile/compile-diff.tspassesNO_CYCLIC_TAG, including the deploy diff, whose unit-to-unit edges take the service edge's style.diffEdgeArraycopies only flagged edges, without the flag, so the identity-keyedcrossSystemSourcelookup still matches.resolver/model-immutability.test.tscovers AC 1 and AC 3. Every example and a cyclic fixture must stay deep-equal to theirstructuredCloneafteranalyze,collectModelWarningsand each builder, and the same model must render the same on either side ofanalyze().parser/cyclic-dependencies.test.tscovers:compileProjectparse → analyze → edge.cyclicnow assert that parse sets the flag and thatanalyzeleaves it alone.docs/spec/tags-annotations*.md(and the bundled skill copies):[cyclic]is assigned at parse time, and not in diff views.@karasu-tools/core,karasuandkarasu-skills.Measurements (main
61f3b2f7vs this branch; byte comparison rerun after every walk change)Byte comparison of each entry point over the 85 example files plus the Dify model:
compile,compileProject,buildAllViewsSvg,buildAllViewsSvgProject,buildDrawioProject,compileSystemDiff,buildAllViewsSvgDiffProjectbuildDrillDownSvg,buildAllLayersSvg,buildDrawiocompilealready drew it)Parser.parse, median of 40 runs:Shapes built to stress the walk (
Parser.parse, single run):analyzethrowsOn the chain where every node calls the root, main's
analyzedid not finish in 120 s; on the branch it takes about 0.7 s at 15000 services, which is the size of the warnings themselves. Per keystroke the walk now runs twice (once in parse, once inanalyze) instead of once.Before / after
No rendering change (0 acceptance-test fences, 65 example files)
Only a model with a sync cycle changes, and no example has one. In the Dify model, its one cycle is now red in the All-layers panel, the live entity view, the drill-down export and
buildDrawio. Before this change it was red only in the live view,karasu renderand the all-views export.Risk
High:
docs/spec/tags-annotations.md,docs/spec/tags-annotations.ja.md(plus Mediumpackages/core/**).How to revert
Revert the squash commit. Nothing outside the repo changes.
Preview URL
Scope filter (new-feature PRs only)
Dependency & license impact
Manual Verification Checklist
N/A — all covered by automated tests
TPL impact
Related Docs
🤖 Generated with Claude Code
Local review
Reviewers: /engineering:code-review (fresh subagent), coderabbit CLI --deep
Summary by CodeRabbit
[cyclic]edge rules; cycles are reported as warnings instead.[cyclic]behaves in model and diff views.