OpenFini

Docs / Guides

Refactoring audit

The living record of code-health audits: what was measured, what was changed, and what was considered and deliberately not changed (with the reason). Append a new dated section for every future audit; do not rewrite old ones, so the numbers stay comparable.

Principles#

  1. Refactor only for a measurable payoff: maintainability, correctness, performance or bundle size, and only where tests cover the code (add characterization tests before touching an untested hot spot).
  2. No churn for taste: no renames, file moves or style rewrites without a payoff someone can measure.
  3. Prefer deleting code over abstracting it. A shared helper must remove more code than it adds.
  4. Behavior stays identical: the whole test suite stays green; user-visible behavior does not change.
  5. Rejected ideas are written down ("considered, not done because …"), so nobody repeats the analysis.
  6. Small PRs, one theme each, with before/after numbers (npm run build && node scripts/check-size.mjs).

Tools#

ScriptWhat it answers
node scripts/analyze-bundle.mjs [--top N] [--json]Runs the real production build and reads esbuild's metafile: chunks by gzip size, start-up vs lazy, start-up modules by estimated gzip size, lazy-loading candidates (start-up bytes reachable only through one module), size per folder.
node scripts/import-report.mjs [--why <file>]Static import graph without a build: largest modules, fan-in/fan-out, circular imports, and the import chain that pulls a file into start-up.
node scripts/find-dead-exports.mjsExports nothing imports (split into real dead code and needless export keywords), exports only tests use. A report, never a delete list (see the blind spots in its header).
node scripts/check-size.mjsThe CI size gate (budgets in scripts/size-budget.json).

The graph scripts share scripts/lib/import-graph.mjs (regex based, dependency-free, also used by tests/architecture/).


Audit 1 (October 2026, after 0.8.0)#

Baseline: main at 39405b0 (Vibe UI v2). 494 modules (app + fflate + polygon-clipping), 131,480 lines.

a) Size#

node scripts/check-size.mjs on the baseline build (KiB, gzip level 9):

MeasureActualBudgetHeadroom
Shell JS (all app JS outside vendor/)1271.512808.5
Start-up JS (index.html modules + static imports)373.53806.5
Total download (no fonts, no on-demand libraries)2154.0216511.0

The budget file has been raised 13 times; every recent feature took most of the headroom.

Bundle composition. 271 app chunks. The two largest are lazy shared chunks: 112.7 KiB (Code studio engines: LaTeX, Markdown, templates) and 66.8 KiB (Vibe tool definitions). The start-up closure is 196 source modules in 20 chunks.

Start-up gzip per folder (estimated, top): engine/layout 69.2, core 47.1, ui 46.2, engine/pixel 41.2, studios/tools 30.9, commands 24.1, customize 15.3, ui/panels 14.6, engine/vector 12.1, vendor/fflate 11.8, engine/web 9.6, vendor/polygon-clipping 9.2.

Already lazy (good): i18n catalogs, tour steps, help, What's new, settings dialog, customize editors, collaboration, VS Code, extensions, Office, Code and Vibe workspaces, emmet, PDF/KaTeX/CodeMirror vendors. The one big literal table still at start-up is the HTML entity table in engine/web/charcodec.js (see the text-tool row below).

Lazy-loading candidates (start-up bytes reachable only through one module, from analyze-bundle.mjs):

ModuleStart-up KiB that would leaveWhy it is at start-upVerdict
engine/pixel/index.js41.2the renderer needs the effect registry (getEffect) for live effects and adjustment layersLater: needs an async effect registry; touches rendering
engine/layout/hyphenation(-en-us).js17.0engine/dl.js creates the hyphenator lazily, but the pattern table is a static importLater: layout is synchronous; lazy patterns would change the first layout of hyphenated text (reflow) and exports
vendor/fflate11.8.ofini read/write (core/ofini.js)Rejected: needed to open the session's documents at start
engine/vector/boolean.js + polygon-clipping10.7core/ops.js (booleanOp, expandStrokeOp)Later: both are synchronous public APIs (Vibe tools, tests)
core/io.js10.0import/export entry pointsLater: many call sites, mostly already async
ui/export-dialog.js9.5static import in commands/file.js and commands/code.js, only used when a command runsDo now
commands/text-tools.js → engine/web/texttools.js + charcodec.js8.3the palette registers ~50 Code text commands at start and needs only their ids/labels, but the transforms pull the 1100+ entry HTML entity tableDo now (split the catalog from the implementation)

Duplicates. Small helpers are re-implemented locally: esc/escapeHtml (7 one-liners in engine, web, office, vscode), clamp (8), uid (2: core/util.js, engine/office/model.js), crc32 (2: ai/providers/mock.js, engine/formats/embedded.js), debounce (2). Verdict: rejected: each is one to ten lines, the engines must stay importable on their own (pure, no core/ dependency), and a shared module would add imports without removing measurable bytes (the copies are one-liners that live in different lazy chunks).

Dead code. find-dead-exports.mjs: 0 orphan modules; 321 exports nothing imports, of which 93 are not referenced in their own module either. Most of those 93 are re-exports of public engine barrels (engine/layout/index.js, studios/index.js), which stay. Real dead functions: core/color.js (withAlpha, luminance, mixColors, WHITE, BLACK), core/util.js (lerp, approx), core/office.js ensureOffice, core/pixel.js readRect, engine/office/report.js copyReport, ui/menus.js isContextMenuOpen, ui/controls.js ofIcon, ui/themes.js (hasTheme, currentTheme), core/app.js STUDIO_ORDER, core/web.js DEFAULT_WEB_SETTINGS. Payoff: lines only (esbuild already drops unused code, so 0 KiB). Verdict: Do now (small, safe deletions).

Vendored libraries (gzip): CodeMirror 218.0 (lazy, Code studio), pdf.js 147.9 + worker 383.0 + wasm (on demand, not precached), fflate 11.8 (start-up), polygon-clipping 9.2 (start-up), Shoelace components (loaded per component).

CSS. A class-name scan of app/styles/*.css against all JS/HTML found 93 class names never mentioned literally; almost all are CodeMirror classes (cm-*), dynamic names (lv-${level}) or documented hooks (of-native-scroll). Provably dead: 12 classes in vibe.css left over from Vibe UI v1 (of-vcanvas*, of-slash*, of-vruns*, of-vmsg__tools, of-approval__done). Verdict: Do now.

b) Structure#

ModuleLinesNote
engine/layout/text-layout.js2058cohesive (line breaking, frames, caret); split rejected: one algorithm, well tested
engine/web/latex.js1848cohesive converter, lazy
engine/web/markdown.js1462cohesive, lazy
ui/office/editor.js1303largest untested UI module
engine/formats/psd.js1263cohesive reader/writer, tested
core/app.js / ui/shell.js / commands/file.js / ui/settings-dialog.js / vibe/session.js481 / 265 / 426 / 457 / 460the suspected god objects are moderate in size; no split has a clear payoff now

Fan-in: core/util.js 157, ui/controls.js 57, ui/dialogs.js 50, core/nodes.js 40, i18n/index.js 40. Fan-out: ui/code/workspace.js 28, vibe/tools/index.js 25, engine/web/index.js 20, ui/shell.js 19.

Circular imports: 1, core/data-nodes.js ↔ core/ops.js (remapLinks vs addNodes). Harmless at runtime (function bindings) but it blocks a "no cycles in core" guardrail. Verdict: Do now (move remapLinks, a pure tree helper, next to walk in core/nodes.js).

Layering. core/ imports ui/ only through import() in core/open-foreign.js (dialogs on demand): accepted. engine/ never imports ui/. Engine DOM-freedom is tested for engine/web only (tests/web/purity.test.js).

Events and leaks. Emitter.on returns an unsubscribe function. 127 app.on( calls, mostly in singletons (shell, canvas view, panels; the dock creates each panel once). Panels and panes that are rebuilt each time a dialog opens must unsubscribe. Found: ui/customize/toolbox-editor.js keeps a studio listener (and with it the detached pane) for every opening of Settings ▸ Toolbox and menus. Verdict: Do now. addEventListener 828 vs removeEventListener 60: almost all on elements that are discarded together with their listeners (fine). setInterval 15 / clearInterval 15. ResizeObserver 10 / disconnect 4: the observers are on elements of singletons or observe their own element (collected with it).

Logging and errors. core/log.js is used in 20 files; raw console.* in 19 files (29 calls), mostly in bootstrap, workers and sandboxes where the logger is unavailable. 71 empty catch {} blocks (best-effort storage and feature detection). Verdict: Later (no measurable payoff now; review case by case when touching a file).

Test coverage. 492 app modules; 436 are loaded by at least one test, 251 directly. 82 modules (16,492 lines) are never loaded by a test, all UI: ui/code 6800 lines, ui/vibe 3179, ui 2379, ui/office 1774, commands 1388. Largest: ui/office/editor.js, ui/code/workspace.js, ui/canvas-view.js. Refactors of these need characterization tests or browser checks first.

c) Risk ranking#

#CandidatePayoffRiskEffortTestsDecision
1Load the export dialog on demand−9.5 KiB start-uplow (commands are async already)Scommand tests, buildDo now
2Split the text-tool catalog from the transforms−8.3 KiB start-uplowStests/web/texttools.test.js + new catalog testDo now
3Break the data-nodes ↔ ops cycle0 KiB, enables a guardraillowStests/layout/table-chart-doc.test.jsDo now
4Fix the toolbox editor listener leakmemory per settings visitlowSbrowser checkDo now
5Delete dead functions and Vibe UI v1 CSSlines, ~0.5 KiB CSSlowSfull suiteDo now
6Guardrails (cycles, layering, engine purity, start-up closure) + lower budgetsprevents regressionslowMnew testsDo now
7Lazy hyphenation patterns−17 KiB start-upmedium (first layout, exports)Mlayout testsDone (budget diet): ensureHyphenation() loads the patterns after start-up (idle) and before raster/PDF export; text laid out before they arrive keeps words whole and reflows once
8Async pixel effect registry−41 KiB start-uphigh (renderer)LpartialLater: the canvas backend calls getEffect synchronously while painting
9Lazy boolean engine−10.7 KiB start-upmedium (sync API used by Vibe tools)Mcore testsDone (budget diet): booleanOp/expandStrokeOp are async and import the engine
10Shared esc/clamp/crc32 helpers~0 KiBlowSyesRejected: no bytes saved, couples pure engines to core/
11Split core/app.js, ui/shell.js, commands/file.jsnone measurablemediumMpartialRejected: < 500 lines each, no clear seam that removes code
12Replace console.* with core/log.js everywhereconsistencylowM—Later: most calls are in places without the logger
13Lazy fflate−11.8 KiBhigh——Rejected: needed to restore documents at start
14PSD fuzz test with a 4 s wall-clock limit (flaked at 4.8 s on a macOS runner)CI stabilitylowSitselfDone: kept as a hang guard with a 15 s limit and the slowest input in the message

Results#

Numbers from check-size.mjs, KiB gzip; PR rows are changes, each measured against the main it was based on.

PRChangeStart-upShell JSTotal
baselinemain @ 39405b0373.51271.52154.0
#42Export dialog on demand−9.700
#44Text-tool catalog split from the transforms−7.1+0.7+0.8
#45Toolbox editor listener leak000
#47Dead functions and Vibe UI v1 CSS (CSS 73.2 → 72.9)0−0.2−0.4
guardrailsArchitecture tests, remapLinks moved (cycle removed), start-up budget 380 → 365000
dietLazy hyphenation patterns and boolean engine (main @ a55a1e7: 371.0 / 1621.1 / 2547.8)−26.7+2.5+2.5
finalmain after all of the above, including other teams' merges (collaboration UX)357.21290.62175.6

Start-up JavaScript went down by 16.3 KiB (−4.4 %). Shell JS and total grew in the same period because of features merged in parallel (their budgets were raised by those PRs); the audit itself changed them by less than 1 KiB.

Edit on GitHub → docs/refactoring-audit.md