Skip to content

About

WebAssembly build of lever-runner carapace for browser deployment

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

Lever Runner WASM

A WebAssembly build of the lever-runner intent-to-command carapace, providing near-native performance for intent hashing, embedding, vector similarity search, and a three-gate routing pipeline — all running entirely in the browser with zero server round-trips.

Why It Matters

Command palettes (Raycast, Alfred, k9s) and AI-powered shell assistants (GitHub Copilot CLI, warp) traditionally rely on either:

  1. Server-side NLP — high latency, privacy concerns, requires network.
  2. Exact string matching — brittle, no semantic understanding.

Lever-runner WASM offers a third path: a complete intent-resolution engine compiled to WebAssembly, running at near-native speed inside the browser. The three-gate pipeline (exact hash → vector similarity → keyword heuristic) ensures O(1) best-case response with graceful degradation.

This matters for:

  • Edge computing: run intelligent command resolution on devices with no backend.
  • Privacy: user intents never leave the browser.
  • Latency: sub-millisecond hash lookups, ~10–50µs vector search — faster than any network call.

How It Works

Intent Hashing (BLAKE2b-128)

Every intent string is hashed using BLAKE2b with a 128-bit digest:

$$h = \text{BLAKE2b}_{128}(\text{intent})$$

BLAKE2b is a cryptographic hash function faster than MD5 and SHA-1, with a proven security margin. The hash provides O(1) exact-match lookup via hash-table comparison.

Position-Aware Character Embedding

The embed_intent function produces a 64-dimensional vector capturing both what characters appear and where they appear:

Dimensions 0–39: Character frequency with positional decay. Each character's contribution is weighted by:

$$w_{\text{pos}}(i) = 1 + \frac{2}{1 + i}$$

where $i$ is the character position. Earlier characters contribute more — this mirrors the primacy effect in human text comprehension. The result is L²-normalized.

Dimensions 40–55: Bigram frequency. 16 hash buckets capture character-pair co-occurrence:

$$\text{bucket}(c_i, c_{i+1}) = (c_i \cdot 31 + c_{i+1}) \mod 16$$

This is a feature-hashing trick (Weinberger et al., 2009), reducing the $|\Sigma|^2$ bigram space to 16 dimensions.

Dimensions 56–63: Structural features. Log-normalized length, word count, average word length, character diversity, digit/path presence, separator ratio.

Vector Search: Cosine Similarity

Similarity between query embedding $\mathbf{q}$ and stored embedding $\mathbf{d}$ is computed via cosine similarity:

$$\text{sim}(\mathbf{q}, \mathbf{d}) = \frac{\mathbf{q} \cdot \mathbf{d}}{|\mathbf{q}| \cdot |\mathbf{d}|}$$

The threshold for Gate 2 acceptance is $\text{sim} > 0.75$.

Three-Gate Pipeline

Gate Method Threshold Latency Accuracy
1 — Hash Exact BLAKE2b match 1.0 ~0µs Exact
2 — Vector Cosine similarity > 0.75 ~10–50µs Semantic
3 — Keyword Substring matching > 0.0 ~50–200µs Heuristic

The cascade ensures that easy cases (exact match) are resolved instantly, while harder cases escalate to more expensive (but more flexible) methods.

Complexity

Operation Complexity
hash_intent(s) O(|s|)
embed_intent(s) O(|s|)
vector_search(q, k) O(n \cdot d + n \log n) where $n$ = DB size, $d = 64$
gate_pipeline(s) O(n) worst case (Gate 3)

Quick Start

import init, { gate_pipeline, teach, seed_commands } from './lever_runner_wasm.js';

await init();

// Seed common commands
seed_commands(); // Loads ~28 default commands

// Teach a new mapping
teach("restart nginx", "systemctl restart nginx");

// Resolve an intent
const result = JSON.parse(gate_pipeline("restart nginx"));
console.log(result);
// { gate: 1, command: "systemctl restart nginx", confidence: 1.0, latency_us: 42 }

API

Exported WASM Functions

Function Parameters Returns Description
hash_intent(intent) &str String (32-char hex) BLAKE2b-128 hash
embed_intent(intent) &str Vec<f64> (64-dim) Position-aware embedding
vector_search(embedding, top_k) &[f64], usize JSON string Ranked results
gate_pipeline(intent) &str JSON string Full three-gate resolution
teach(intent, command) &str, &str void Add to database
load_commands(json) &str usize Bulk load
export_commands() — JSON string Export database
clear_commands() — void Clear database
command_count() — usize Database size
seed_commands() — usize Load defaults

Architecture Notes

The library satisfies γ + η = C in edge computing:

  • γ (correctness): the three-gate cascade provides a correctness floor. Gate 1 never produces false positives (exact hash match). Gate 2 uses a calibrated cosine threshold (0.75) balancing precision and recall. Gate 3 provides a deterministic keyword fallback — never returning empty.
  • η (efficiency): the entire pipeline runs in WASM with no I/O. The opt-level = "z" release profile and LTO minimize binary size for fast initial page load. Vector search is O(n) with $d=64$ — fast enough for thousands of commands.
  • C (contract): a complete intent-resolution system in a single .wasm binary, deployable to any browser or WASM runtime (Cloudflare Workers, Deno, Node.js) with zero dependencies beyond the WASM standard library.

References

  1. Aumasson, J.-P. et al. "BLAKE2: Simpler, Smaller, Fast as MD5." ACM SAC, 2013.
  2. Weinberger, K. et al. "Feature Hashing for Large Scale Multitask Learning." ICML, 2009.
  3. Haas, A. et al. "Bringing the Web up to Speed with WebAssembly." PLDI, 2017.
  4. Mikolov, T. et al. "Distributed Representations of Words and Phrases." NeurIPS, 2013. (Embedding foundations)

License

MIT

About

WebAssembly build of lever-runner carapace for browser deployment

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages