agentleFS
mantvmassmantvmass/js-leak-lab/CLAUDE.md

js-leak-lab

CLAUDE.md0 starsChanged 7 months agoMITView on GitHub

A teaching tool that demonstrates JavaScript/TypeScript memory leak patterns. Each pattern can be toggled "leaking" or "fixed" at runtime; the UI visualizes memory live. Patterns run either in the Bun backend process (server patterns) or in the browser tab (client patterns, including real React and Vue runtimes). Runs on Bun (not Node). The backend is TypeScript executed directly; the frontend is a Vite + React app that must be built for production. In dev, open :5173 — Vite proxies /api…

The file

We split this CLAUDE.md into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Tokens: All colors, fonts, and radii are CSS variables in web/src/globals.css, exposed to Tailwind via @theme inline.

    line 85
  2. Very specific:

    Mono & numbers: Use font-mono + tabular-nums for every number, id, count, byte size, duration, and timestamp.

    line 98
  3. Very specific:

    When adding or removing a pattern, also update the catalogue in .claude/skills/memory-leak-audit/reference.md so the agent skill stays in …

    line 125

≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
CLAUDE.md

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

What this is

A teaching tool that demonstrates JavaScript/TypeScript memory leak patterns. Each pattern can be toggled "leaking" or "fixed" at runtime; the UI visualizes memory live. Patterns run either in the Bun backend process (server patterns) or in the browser tab (client patterns, including real React and Vue runtimes).

Commands

Runs on Bun (not Node). The backend is TypeScript executed directly; the frontend is a Vite + React app that must be built for production.

bun install                # install deps (uses bun.lock, frozen in CI/Docker)
bun run dev                # backend (:3000) + Vite dev server (:5173) together — open :5173
bun run dev:server         # backend only
bun run dev:web            # Vite dev server only
bun run build              # build the frontend into web/dist
bun run start              # run the backend; serves web/dist at :3000 (build first)
bun run typecheck          # tsc for backend + tsc -p web/tsconfig.json for frontend
bun run validate           # drive every leak module and assert leak/stop/fix behaviour

In dev, open :5173 — Vite proxies /api and /ws to the backend on :3000. CI runs typecheck and a Docker build; a v* git tag publishes to ghcr.io. There is no unit test suite or linter — validate (scripts/validate-server.ts + validate-client.ts, the latter using happy-dom) is the closest thing: it runs every server and client leak module and checks the leak path grows, stop* fully resets, and the fix path stays bounded. The app intentionally leaks memory — cap RAM when running in Docker.

Architecture

Two halves: a Bun backend (src/) and a Vite/React frontend (web/). They share no code; the contract is the HTTP API + WebSocket wire format.

Backend — src/server/ (Bun, TypeScript)

index.ts loads modules then starts Bun.serve: WebSocket upgrades on /ws, everything else goes to handleRequest.

  • modules.ts — defines the LeakModule interface and loadModules(), which dynamically imports every src/leaks/*/index.ts (directories sorted alphabetically, so the NN- prefix orders them). Each module's default export must satisfy LeakModule.
  • routes.ts — GET /api/modules (metadata for all server patterns), GET /api/snapshot, POST /api/leak/:id/:action (start-leak|stop-leak|start-fix|stop-fix), and static serving of web/dist with SPA fallback.
  • ws.ts / snapshot.ts / state.ts — broadcast a memory snapshot to all WS clients every 500ms: process.memoryUsage(), a rolling 120-point heap history, each module's getSize(), and per-module running state.

Server leak modules — src/leaks/NN-name/index.ts

A default-exported object satisfying LeakModule. Key fields: runtime: "server", group (mechanism category), framework, localized title/description as { th, en }, badCode/goodCode (illustrative source strings, not executed), and the executable startLeak/stopLeak/startFix/stopFix + getSize(). By convention start* calls its matching stop* first, and stop* clears state and calls Bun.gc(true). To add one: create src/leaks/NN-name/ with an index.ts — it is auto-discovered, no registration.

Frontend — web/src/ (Vite + React + TypeScript + Tailwind v4)

  • lib/catalog.ts — shared pattern types (PatternMeta, ClientLeakModule, Pattern, GroupId, FrameworkId).
  • lib/i18n.ts — i18next (th/en) for UI chrome only; pattern title/description are localized inline in each module's metadata, not via i18n keys.
  • App.tsx — fetches server patterns from /api/modules, merges them with the local client registry into one catalog, applies search/group/runtime filters, and resolves each pattern's run state + size from the right source (server snapshot vs. client hook).
  • lib/use-server-snapshot.ts — WebSocket subscription with reconnect backoff.
  • lib/use-client-leaks.ts — drives client modules' controllers and polls getSize().
  • lib/use-client-memory.ts — polls performance.memory (Chromium-only).
  • components/ui/ — design-system primitives (cva + Radix): Button, Badge, Card, Input, Tabs, Tooltip, CodeBlock. Design tokens live in globals.css.

UI design system

The interface has a deliberate look — a compact, sharp "control panel" aesthetic with a gold accent. When adding or changing UI, match what exists; do not introduce a generic AI-styled look, and do not invent new colors or spacing scales.

  • Tokens. All colors, fonts, and radii are CSS variables in web/src/globals.css, exposed to Tailwind via @theme inline. Dark is the default theme (<html class="dark"> in web/index.html); light values also exist. Never hardcode hex colors in components — use token utilities (bg-card, text-muted-foreground, border-border, bg-primary, text-success, …).
  • Brand color. Gold #F0B90B is --primary. Use it only for primary CTAs, focus rings, and highlighted data — never as default chrome (borders, plain text, surfaces).
  • Status palette. success / warning / destructive / info, each with a matching -foreground. Status indicators go through <Badge variant=…> (primary | secondary | success | warning | destructive | info | outline).
  • Sizing & shape. Compact and sharp: Button h-8, Input h-9, border-radius 6px (--radius). Body/data text is text-[13px]; text-sm for prose; text-xs for muted text. Headings are tracking-tight font-semibold.
  • Mono & numbers. Use font-mono + tabular-nums for every number, id, count, byte size, duration, and timestamp. Eyebrow labels and column headers use the eyebrow utility (defined in globals.css — 10px, uppercase, 0.18em tracking).
  • Typography. Inter (Latin) + Noto Sans Thai for font-sans; JetBrains Mono for font-mono. Loaded from Google Fonts in web/index.html.
  • Motion. Restrained, 120–200ms ease-out. Use the anim-pop-in / anim-modal-in / anim-overlay-in utilities in globals.css. Keyframes set individual scale / translate / rotate properties — never the transform shorthand (Tailwind v4 composes transform from variables, so the shorthand breaks animations).
  • Icons. lucide-react only — no emoji. The brand mark is the FlaskConical icon in gold; web/public/favicon.svg and og.svg reuse it.

Client leak modules — web/src/leaks/*.ts(x)

Each default-exports a ClientLeakModule (runtime: "client") — same metadata shape as server modules plus a controller with the start/stop/getSize methods that execute in the browser. React patterns mount/unmount real components via createRoot; Vue patterns use the real vue runtime via createApp. New client modules must be added to the clientModules array in web/src/leaks/index.ts (no auto-discovery).

Adding a pattern — which side?

Server patterns leak the Bun process and are driven over the HTTP API. Client patterns leak the browser tab and run via their controller. Framework patterns: client-side React/ Vue run on real runtimes; server-side Next.js/Nuxt patterns are faithful reproductions in the Bun process (the frameworks themselves are not run).

When adding or removing a pattern, also update the catalogue in .claude/skills/memory-leak-audit/reference.md so the agent skill stays in sync, and run bun run validate to confirm the new lab works.

Agent skill

.claude/skills/memory-leak-audit/ is a distributable Claude Code skill (SKILL.md + reference.md) that audits arbitrary codebases for these leak patterns. It is also surfaced in the app's "AI Skill" tab (web/src/components/SkillPage.tsx). It is content, not code — keep reference.md consistent with the actual pattern library.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.