Skip to content

docs(adr-048): WASM graph transforms architecture (PROPOSED)#19

Merged
Rubentxu merged 1 commit into
mainfrom
docs/adr-048-wasm-graph-transforms
Jun 24, 2026
Merged

docs(adr-048): WASM graph transforms architecture (PROPOSED)#19
Rubentxu merged 1 commit into
mainfrom
docs/adr-048-wasm-graph-transforms

Conversation

@Rubentxu

Copy link
Copy Markdown
Owner

Summary

  • ADR-048 (NEW): Two-crate split (cognicode-graph-algos + cognicode-graph-wasm) with GraphBuilder (1 method) + pure algorithm functions
  • Spike PASSED: petgraph + serde + thiserror compile to wasm32 in 5.07s (engram obs-2856)
  • Roadmap: ADR-048 entry added to ARCHITECTURE-DEEPENING-2026 §4 ADRs table

Context

ADR-047 (v0.12.16) permitted WASM-for-shared-compute but left the architecture unspecified. This ADR specifies the crate structure and validates the approach with empirical evidence (spike).

Decision

Crate Target Role
cognicode-graph-algos native + wasm32 GraphBuilder (1 method) + pure functions + JSON adjacency builder
cognicode-graph-wasm wasm32 only Thin bindgen shim (wasm-bindgen + serde-wasm-bindgen + tsify)
  • petgraph-adapter feature gate: default = [], optional. Native builds enable it; WASM builds keep petgraph out of the dep tree.
  • Algorithms are free functions: page_rank(in_neighbors, out_degree, n, alpha, max_iter) + god_nodes(scores, percentile). Same .rs compiles to both targets — single source guaranteed by construction.
  • TypeScript types via tsify: #[derive(Tsify)] auto-generates .d.ts. No drift.

Spike Evidence

.sddk-spike-wasm-compat.json documents:

  • cargo check --target wasm32-unknown-unknown PASS (5.07s dev + release)
  • cargo test PASS (PageRank mass conservation verified)
  • PageRank body in graph_analytics.rs:78-177 operates on Vec<Vec<usize>> — NO petgraph API calls in the hot loop
  • god_nodes (:248-272) consumes HashMap<usize, f64> directly

Affected Areas

  • New crate: crates/cognicode-graph-algos/
  • New crate: crates/cognicode-graph-wasm/
  • Cargo.toml (workspace): add 2 new members
  • crates/cognicode-core/src/application/services/graph_analytics.rs: delegate to cognicode-graph-algos
  • .github/workflows/ci.yml: add wasm-build-and-test job + bundle size gate

Validation (post-merge, PRs #2-#5)

  • cargo test -p cognicode-graph-algos passes (native)
  • wasm-pack test --node crates/cognicode-graph-wasm passes (wasm32)
  • Parity: same input → byte-identical BTreeMap JSON on both targets
  • cognicode-core compiles with re-export (no native caller breakage)
  • Bundle size: .wasm gzipped < 500KB (512000 bytes)
  • tsify: .d.ts matches Rust struct shapes
  • ADR-047 §4 satisfied: algorithm body diff between targets is empty

Tag plan: v0.13.0 MINOR (after all 5 PRs land in this cycle)

Formalizes the two-crate split (cognicode-graph-algos +
cognicode-graph-wasm) with a 1-method GraphBuilder trait + pure
algorithm functions, tsify-generated TypeScript types, and a JSON
protocol reusing frontend DTOs.

Spike PASSED: /tmp/opencode/spike-petgraph-wasm/ validated that
petgraph 0.6.5 + serde + serde_json + thiserror compile to
wasm32-unknown-unknown in 5.07s (dev + release). Result captured
in .sddk-spike-wasm-compat.json.

Spike key findings (engram obs-2856):
- petgraph + all transitive deps are pure-Rust WASM-compatible
- PageRank body in graph_analytics.rs:78-177 operates on
  Vec<Vec<usize>> + Vec<f64> after a one-time petgraph setup —
  NO petgraph API calls in the hot loop
- god_nodes (graph_analytics.rs:248-272) consumes HashMap<usize, f64>
  directly — zero petgraph coupling
- 1-method GraphBuilder trait is sufficient (replaces original
  4-method GraphLike design)

Required trait imports for petgraph code:
  use petgraph::visit::{EdgeRef, NodeIndexable};

Architecture decision:
- cognicode-graph-algos: pure, both targets, deps = [petgraph*,
  serde, serde_json, thiserror]
- petgraph-adapter: optional feature (default = []) so WASM bundle
  stays clean
- cognicode-graph-wasm: bindgen shim, wasm32 only
- Algorithm bodies are free functions — same .rs compiles to both
- tsify auto-generates .d.ts from #[derive(Tsify)] structs

Tag: v0.13.0-pre (new feature: WASM graph transforms, M reserved
for after all 5 PRs land)
@Rubentxu Rubentxu merged commit ad1ca7d into main Jun 24, 2026
1 of 5 checks passed
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