Skip to content

Port to .cljc for JVM + CLJS support (0.3.0) - #3

Merged
mike-thompson-day8 merged 8 commits into
masterfrom
cljc-port
May 13, 2026
Merged

mike-thompson-day8 merged 8 commits into
masterfrom
cljc-port

Conversation

@mike-thompson-day8

Copy link
Copy Markdown
Contributor

Summary

  • Source moves from .cljs to .cljc with reader conditionals isolating the only platform-specific bit — the mutable hash-bucket store used during compression (js/Map on CLJS, java.util.HashMap on JVM).
  • The same 9 tests / 43 assertions now run on both platforms via clojure -M:test.
  • Version bumped to 0.3.0.

Why

JVM Clojure consumers (e.g. story-mcp in the re-frame2 repo) want to apply structural dedup at their wire boundary. Until now the library has been CLJS-only because of js/Map interop. The algorithm is platform-agnostic; only the bucket store needed isolation.

What changed

  • src/de_dupe/core (.cljs → .cljc)

    • new-bucket-store / bucket-get / bucket-set! helpers wrap js/Map (CLJS) and java.util.HashMap (JVM).
    • IMapEntry / IRecord checks switched from satisfies? to a small map-entry?* / record?* helper that picks the right predicate per platform.
    • The mutable cache-id-counter moved from def + set! (CLJS-only) to an atom so JVM can mutate it too.
  • test/de_dupe/test/core (.cljs → .cljc)

    • Test framework swapped via reader conditionals (clojure.test on JVM, cljs.test on CLJS).
    • EDN reader likewise (clojure.edn vs cljs.reader).
    • js/process.exitCode and *main-cli-fn* are CLJS-only and stay behind #?(:cljs …).
  • test/de_dupe/test_runner.clj

    • Now drives both targets: clojure -M:test runs the CLJS suite (compile + Node) and the JVM suite.
  • bench/ stays .cljs. The benchmark is informational; the JVM-reach acceptance doesn't depend on it.

  • Version: project.clj and package.json bumped 0.2.2 → 0.3.0. README updated.

Test plan

  • clojure -M:test passes on the CLJS suite (9 tests / 43 assertions).
  • clojure -M:test passes on the JVM suite (9 tests / 43 assertions).
  • JVM smoke: structural sharing is preserved across de-dupe → expand round-trip (identical? returns true for shared substructures).
  • Both platforms produce equivalent compressed output for the same input (the existing equality tests exercise the canonical fixtures on both targets).
  • CI (.github/workflows/test.yml) — already runs clojure -M:test, so the JVM job now exercises both halves once this lands.

Source moves from .cljs to .cljc with reader conditionals isolating
the only platform-specific bit — the mutable hash-bucket store used
during compression. On CLJS that's `js/Map`; on JVM it's
`java.util.HashMap`. The algorithm is otherwise platform-agnostic.

- core: .cljs -> .cljc. Reader-conditional helpers wrap `js/Map` /
  `java.util.HashMap` (`bucket-get`, `bucket-set!`, `new-bucket-store`).
  IMapEntry / IRecord checks switched from `satisfies?` to a small
  `map-entry?*` / `record?*` helper that picks the right test per
  platform. Mutable cache-id counter moved from `def` + `set!`
  (CLJS-only) to an atom so JVM can mutate it too.

- test: .cljs -> .cljc. Test framework swapped via reader conditionals
  (clojure.test on JVM, cljs.test on CLJS) and the EDN reader likewise
  (clojure.edn vs cljs.reader). `js/process.exitCode` and `*main-cli-fn*`
  are CLJS-only and stay behind `#?(:cljs …)`.

- test-runner: now drives both targets. `clojure -M:test` runs the CLJS
  suite (compile + Node) and then the JVM suite. The same 9 tests / 43
  assertions execute on both platforms, ensuring no platform-conditional
  drift.

- bench/ stays .cljs (Node-only; benchmark is informational, not part
  of the JVM-reach acceptance).

- Version bumped from 0.2.2 to 0.3.0 (project.clj + package.json) to
  reflect added JVM support. README updated.

Unblocks JVM consumers (e.g. story-mcp) wanting to apply structural
dedup at the wire boundary; previously the library was unreachable from
JVM Clojure because of `js/Map` interop.
mike-thompson-day8 pushed a commit to day8/re-frame2 that referenced this pull request May 13, 2026
Mike Thompson added 3 commits May 13, 2026 20:22
Closes coverage gaps from the cljc-port audit:
  - nil / empty / single-element round-trips
  - sets, sorted-map, sorted-set
  - deeply nested (200 levels) and wide (1000 elements) collections
  - quoted forms and symbols that collide with the cache namespace
  - explicit platform-parity shape assertion (cache count + id layout)

Suite grows from 9 tests / 43 assertions to 16 tests / 77 assertions
on both CLJS (Node) and JVM legs.

Hardens test-runner.clj against silent-failure pass: it now parses the
clojure.test summary line from each platform and enforces a per-leg
floor of 60 assertions. A test namespace that fails to load (broken
reader-conditional, missing require) emits "Ran 0 tests" and gets
rejected with exit 2 instead of slipping through as exit 0.

Cycles are documented as unsupported (immutable Clojure collections
cannot express them, and the algorithm walks rather than tracks
identity, so cyclic input would not terminate).
@mike-thompson-day8

Copy link
Copy Markdown
Contributor Author

Test-coverage audit (rf2-y9p2s)

Verified the .cljc port both has good coverage and that CI actually exercises it.

What was already solid

  • Round-trip identity covered for maps, vectors, records, lists, lazy seqs.
  • Structural sharing across round-trip (identical? on shared substructures) verified in identity-check and test-serialized-round-trip.
  • de-dupe-eq hash-collision path covered by test-eq-cache-hash-collisions.
  • CI runs the same .cljc test source against both CLJS (Node) and JVM legs via clojure -M:test.

Gaps closed in 7d5a7a4

  • Edge cases: nil, empty [] / {} / #{} / '(), single-element collections, primitives.
  • Collection types: sets, sorted-map (key order preserved), sorted-set (element order preserved).
  • Depth & width: 200-deep nesting, 1000-element vector / 500-key map / 500-element set.
  • Quoting & cache-namespace collisions: bare cache-1 symbols stay data; namespaced cache tokens stay tokens.
  • Platform-parity shape: explicit assertion that de-dupe produces a cache with a known count and id layout for a fixed input — guards against map-iteration order leaking into key assignment.

Suite grew from 9 tests / 43 assertions → 16 tests / 77 assertions, identical on both legs.

Silent-failure gate (the bigger find)

The previous test-runner.clj had two soft spots:

  1. The CLJS leg shelled out to node; if de-dupe.test.core failed to load (broken reader-conditional, missing require), Node could exit 0 having run zero tests.
  2. The JVM leg trusted clojure.test's :fail/:error summary. A namespace that fails to define any deftest returns {:test 0 :pass 0 :fail 0 :error 0} — also exit 0.

The runner now parses the Ran N tests containing M assertions. line from each leg and enforces a per-platform floor (currently 60). Verified the gate trips correctly: temporarily setting the floor to 9999 produced exit 2: CLJS ran 77 assertions (< floor 9999) — tests may have been silently dropped.

CI log on the latest run shows both legs printing their counts and the gate's confirmation line:

Ran 16 tests containing 77 assertions.
  → CLJS: 16 tests / 77 assertions ✓
Ran 16 tests containing 77 assertions.
  → JVM: 16 tests / 77 assertions ✓
All test legs passed assertion-floor gate.

Cycles

Documented as unsupported in the test file: persistent Clojure collections can't express cycles natively, and the algorithm walks rather than tracks identity, so cyclic input wouldn't terminate. Out of scope for the wire-boundary dedup use case.

Verdict

PR #3 is good to merge from a test-coverage perspective. story-mcp can adopt this with confidence.

Mike Thompson added 2 commits May 13, 2026 20:42
…loses #2)

Addresses long-standing confusion (issue #2, 2015) about the library
making small inputs grow. Explains the structural-sharing-preservation
purpose, gives three worked examples (tiny no-sharing input, repeated
map, real-world pair2-mcp ~89% number), and a rule of thumb for when
to reach for the library.
@mike-thompson-day8

Copy link
Copy Markdown
Contributor Author

Swapped lein → tools.build + deps-deploy (commit 58d201a). Local jar + install verified.

@mike-thompson-day8
mike-thompson-day8 merged commit ec52641 into master May 13, 2026
2 checks passed
@mike-thompson-day8
mike-thompson-day8 deleted the cljc-port branch May 13, 2026 13:08
mike-thompson-day8 pushed a commit to day8/re-frame2 that referenced this pull request May 25, 2026
…-90eft)

Mirror of pair-mcp's rf2-obpa9 wire-boundary structural-dedup transform,
applied selectively to the three story-mcp tools whose payloads carry
repeated subtrees:

- preview-variant / run-variant  re-key the same :app-db slice into
  :rendered-hiccup and :snapshot (three references collapse into one
  cache slot).
- record-as-variant  the :captured event vector re-uses argument
  shapes across event records.

Measured reduction on a realistic run-variant fixture (256-key
:app-db re-keyed into :rendered-hiccup + :snapshot + assertion
:actual/:expected):  raw=340166 chars  wrapped=68385 chars  = **79.9%
reduction**.  Round-trips byte-exact via de-dupe.core/expand.

Pipeline ordering (mirrors pair-mcp): dedup BEFORE cap.  Dedup
shrinks the payload first so the cap-honest size is post-dedup.

Wire shape unchanged from pair-mcp: `{:rf.mcp/dedup-table <cache-map>}`,
key sourced from `re-frame.mcp-base.vocab/dedup-table-key`.  Cross-MCP
shape pinned by `tools/mcp-conformance/wire-vocab/`'s canonical-marker
fixture; story-mcp added to the marker's `:servers` set with a
representative fixture.

Selective by design  the eligibility contract is the descriptor flag
`:dedup-eligible? true`, pinned by `descriptor-dedup-eligibility-
matches-the-documented-set`.  The other sixteen tools ship small,
bespoke shapes where the cache-of-one wrap would add bytes for zero
compression; they emit raw `:structuredContent` and carry no `:dedup`
slot.  Mirrors pair-mcp's selective `dedup-property` assignment in
`descriptors_data.cljs`.

Sibling change: pair-mcp's day8/de-dupe pin bumps to `v0.3.0` (peeled
commit `367de784...`)  the first tagged release carrying the `.cljc`
port (rf2-nw6sj  day8/de-dupe#3, merged 2026-05-13).  Tags age better
than SHAs; both servers lockstep on the same tag so the two MCPs
can't drift on a dedup-algorithm change.  pair-mcp's compiled
`out/server.js` rebuilt.

Spec: `tools/story-mcp/spec/Principles.md`  Structural dedup at the
wire boundary section added, modelled on pair-mcp's wording from
rf2-obpa9.

Tests:  171  172 (855  858 assertions) on `clojure -M:test`; pair-mcp
604 tests / 1613 assertions still green; mcp-conformance wire-vocab
45 tests / 518 assertions still green; clj-kondo 0 errors.

Cross-refs:
- rf2-obpa9 (pair-mcp adoption pattern mirrored here)
- rf2-nw6sj (cross-repo .cljc port that unblocked JVM-side reach)
- rf2-rvyzy (the cap pipeline this dedup step composes with)
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