Skip to content

refactor(core): dedupe diff pipelines, CJK width heuristic, and micro-duplications (first batch of #2014) - #2026

Merged
kompiro merged 3 commits into
mainfrom
refactor/core-dedup-first-batch
Jul 16, 2026
Merged

kompiro merged 3 commits into
mainfrom
refactor/core-dedup-first-batch

Conversation

@kompiro

@kompiro kompiro commented Jul 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

First batch of the packages/core refactoring opportunities analyzed in #2014 (points 1, 3, 7, 8), plus the fixes for all 8 code-review findings.

Behavior change scope: everything is byte-identical except one deliberate fix — node-card description truncation now goes through the shared truncateToWidth, so CJK descriptions are truncated by display width instead of raw char count (they used to overflow the node border). Details and full verification below.

Point 1 — dedupe the three compile*Diff pipelines (src/index.ts)

Extracted shared helpers and used them from compileSystemDiff, compileDeployDiff, compileOrgDiff, and buildAllViewsSvgDiffProject:

  • resolveBeforeAfter(fs, beforePath, afterPath) — shared ImportResolver + before/after resolution
  • beforeAfterDiagnostics(resolved) — fresh concat of the two sides' resolver diagnostics
  • assembleDiffSheets(before, after, theme, badgeLabels, displayMode) — builtin + before + after sheets, icon-theme sheet appended in icon mode
  • toDiffStateMaps(diffed) — flatten diff metadata into the Map<id, state> shape the renderers consume

Review follow-up: each compile*Diff is now split into a thin resolve wrapper + a sync compile*DiffFromResolved body, and buildAllViewsSvgDiffProject feeds its single upfront resolver pass to all applicable view diffs — 2 resolver passes per bundled diff instead of up to 8. Output unchanged.

Point 3 — consolidate the CJK text-width heuristic

  • Added charDisplayWidth(ch, charWidth) / estimateTextWidth(text, charWidth) to renderer/rendering-constants.ts (CJK = 1.5× char width, threshold > U+2E80 — values untouched)
  • renderer/layout.ts and renderer/deploy-layout.ts drop their line-for-line identical private copies; the three inline copies in svg-builder.ts's truncateToWidth / wrapToWidth now call the shared helper
  • Named the 0.8 ratio in svg-renderer.ts as RENDERED_DESC_FONT_RATIO (value not unified to layout's 0.85 — that would change output geometry) and fixed the misleading // DESCRIPTION_FONT_RATIO comment
  • Added a cross-reference comment on matrix-svg's intentionally different, wider-coverage heuristic; its private estimator is renamed estimateTableTextWidth to avoid colliding with the shared estimateTextWidth

Deliberate behavior fix: description truncation on node cards

renderNode's description truncation (used by system and deploy cards via renderFromLayout) previously did a manual floor(availableWidth / charWidth) char-count slice: it counted CJK chars as 1× width (so CJK descriptions were truncated too late and overflowed the node border) and appended … beyond the width budget (so even truncated ASCII descriptions overflowed by up to one char). It now calls the shared truncateToWidth, which counts CJK as 1.5× and reserves the ellipsis width.

Verified by rendering all 13 example projects (bundled + bundled-diff × default + icon = 52 SVGs) on main and on this branch:

  • 42 of 52 SVGs differ; a scripted element-level comparison confirms every single change is a description-truncation text string — 91 distinct strings, zero geometry/attribute/other changes.
  • Two change shapes, both fixes: (a) CJK descriptions that previously rendered overflowing are now truncated by display width (e.g. 在庫データの一元管理 → 在庫データの…, 公開前チェック・違反通報の処理 → 公開前チェック・違反…); (b) already-truncated descriptions (ASCII and CJK) end one character earlier because the ellipsis now fits inside the width budget (e.g. AI assistant… → AI assistan…, Kubernetes (GKE) → Kubernetes (GKE…).
  • Affected examples: client-mcp, deploy-only, getting-started, hato, multi-file-system (en+ja), and cross-diff renders that embed them. The 10 SVGs without descriptions near the width limit are byte-identical.
  • New unit test asserts CJK descriptions are truncated by display width (1.5×/char), and two existing tests that asserted the old overflowing output were updated.

Point 7 — fix the illegal LayoutNode["kind"] cast (type-only)

Widened LayoutNode["kind"] with a new OrgLayoutNodeKind union in renderer/layout-types.ts and deleted the "member" as unknown as LayoutNode["kind"] cast in exporter/drawio/org-layout.ts. Per review, the union is narrowed to just "member" — teams are emitted as ContainerRect, never as LayoutNode.

Point 8 — placement and micro-duplication cleanups

  • Moved src/render/matrix-svg.ts (+ its test) into src/renderer/; updated the index.ts re-export, ADR-20260502-01's file assumptions, and (per review) the stale paths in docs/acceptance/1062-crud-matrix-view.md and docs/test-perspectives/TPL-20260510-21-scoped-glance-drill-down.md
  • Hoisted the byte-identical toPascalCase from translate/db.ts / translate/openapi.ts into a shared translate/identifier.ts. Per review, the module comment now states honestly that translate/wrangler.ts keeps its own toIdentifier on purpose (undefined-return for unusable input, leading-digit guard, drops trailing punctuation — folding it in would change wrangler output), with cross-references in both files
  • Documented why the two escapeXml variants exist (renderer/svg-builder.ts vs exporter/drawio/mxgraph-builder.ts — the &apos; escaping is load-bearing for mxGraph); intentionally not merged
  • renderNode now uses NODE_PADDING_X instead of the hardcoded 40 * 2 (value unchanged)

Verification

Gate Result
pnpm --filter @karasu-tools/core test ✅ 94 files, 2323 tests passed (incl. new CJK truncation test)
pnpm typecheck (full repo) ✅ all packages pass
pnpm check:cycles ✅ no circular dependency
pnpm knip ✅ exit 0 (only the pre-existing @resvg/resvg-wasm config hint, identical on main)
pnpm lint ✅ 0 warnings / 0 errors
Output comparison ✅ 52 example SVGs rendered on main vs branch: 10 byte-identical; the 42 that differ contain only the documented description-truncation fix (element-level scripted check: 0 non-truncation changes)
pnpm adr:check-assumptions ✅ 479 OK, 0 failing

Changeset: .changeset/description-truncation-display-width.md (patch for @karasu-tools/core and karasu — the truncation fix is a user-visible rendering change; the refactoring itself is internal).

Follow-ups (out of scope here, tracked in #2014): point 2 (compile facade extraction, builds on point 1's helpers), points 4–6 (layout / renderNode / extractView decomposition).

Refs #2014

🤖 Generated with Claude Code

kompiro and others added 3 commits July 16, 2026 15:22
…-duplications

First batch of the packages/core refactoring analysis (#2014). Zero
behavior change — output SVGs are byte-identical (verified by rendering
all example projects on main and this branch and diffing).

- Extract resolveBeforeAfter / assembleDiffSheets / toDiffStateMaps
  helpers in index.ts and use them from compileSystemDiff,
  compileDeployDiff, compileOrgDiff, and buildAllViewsSvgDiffProject
  (point 1).
- Consolidate the CJK text-width heuristic (CJK = 1.5x char width,
  threshold > U+2E80) into charDisplayWidth / estimateTextWidth in
  rendering-constants.ts; layout.ts, deploy-layout.ts, and the three
  inline copies in svg-builder.ts now share it. Name svg-renderer's 0.8
  ratio as RENDERED_DESC_FONT_RATIO (value untouched) and fix the
  misleading DESCRIPTION_FONT_RATIO comment; cross-reference
  matrix-svg's intentionally different heuristic (point 3).
- Widen LayoutNode["kind"] with OrgLayoutNodeKind and drop the
  'as unknown as' cast in exporter/drawio/org-layout.ts (point 7,
  type-only).
- Move src/render/matrix-svg.ts (+ test) into src/renderer/, hoist the
  duplicated toPascalCase into translate/identifier.ts, and document
  why the two escapeXml variants (svg-builder vs mxgraph-builder) are
  kept separate (point 8).

Refs #2014

Co-Authored-By: Claude <[email protected]>
…rix-svg move

ADR-20260502-01 tracks packages/core/src/render/matrix-svg.ts as a file
assumption; update it to the new renderer/ location. Also apply oxfmt to
layout.ts (leftover blank line from the estimateTextWidth removal).

Refs #2014

Co-Authored-By: Claude <[email protected]>
@kompiro
kompiro merged commit 97fb071 into main Jul 16, 2026
12 checks passed
@kompiro
kompiro deleted the refactor/core-dedup-first-batch branch July 16, 2026 22:41
kompiro added a commit that referenced this pull request Jul 16, 2026
The CJK/ellipsis truncation fix in PR #2026 changes rendered SVG output,
so it is a user-visible patch to @karasu-tools/core and the karasu CLI
(which bundles core as a devDependency, so the bump does not cascade).

Refs #2014

Co-Authored-By: Claude <[email protected]>

This branch was successfully deployed

1 active deployment
preview — 129fced1 Deployed Jul 16, 2026 by github-actions[bot]
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