npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@superinstance/ct-bridge

v0.1.0

Published

Constraint Theory solver bridge — CSP compilation and FLUX execution for Node.js

Downloads

67

Readme

@cocapn/ct-bridge

Constraint Theory solver bridge — CSP compilation and FLUX execution for Node.js.

Wraps the Python constraint-theory package for use in Node.js via a persistent subprocess bridge with JSON-RPC messaging.

Requirements

  • Node.js >= 18
  • Python >= 3.11
  • constraint-theory pip package: pip install constraint-theory

Installation

npm install @cocapn/ct-bridge

Quick Start

import { CTBridge } from "@cocapn/ct-bridge";

async function main() {
  const ct = new CTBridge();
  await ct.init();

  const solution = await ct.solve(
    ["x", "y", "z"],
    {
      x: { type: "range", min: 1, max: 10 },
      y: { type: "range", min: 1, max: 10 },
      z: { type: "range", min: 1, max: 10 },
    },
    [
      { id: "c1", variables: ["x", "y"], expression: "x + y == 10" },
      { id: "c2", variables: ["y", "z"], expression: "y < z" },
    ],
    "backtracking",
  );

  console.log(solution.assignments); // { x: 1, y: 9, z: 10 } (example)
  console.log(solution.consistent);  // true

  // Verify the solution
  const verification = await ct.verify(solution.assignments, [
    { id: "c1", variables: ["x", "y"], expression: "x + y == 10" },
    { id: "c2", variables: ["y", "z"], expression: "y < z" },
  ]);
  console.log(verification.valid); // true

  ct.destroy();
}

API Reference

CTBridge

Main class. Manages the Python subprocess lifecycle.

new CTBridge(options?)

| Option | Type | Default | Description | |--------|------|---------|-------------| | pythonPath | string | "python3" | Path to Python binary | | callTimeout | number | 30000 | Timeout per call (ms) | | maxRestarts | number | 3 | Max auto-restarts on crash |

init(): Promise<void>

Start the Python bridge process. Call once before any other method.

solve(variables, domains, constraints, method?): Promise<Solution>

Solve a constraint satisfaction problem.

| Parameter | Type | Description | |-----------|------|-------------| | variables | string[] | Variable names | | domains | Record<string, Domain> | Per-variable domains | | constraints | Constraint[] | Boolean predicates | | method | SolveMethod | Solver strategy (default: "backtracking") |

Returns a Solution with assignments, consistent, and solveTimeMs.

Solver methods:

  • "backtracking" — Classic depth-first search with backtracking
  • "forward_checking" — Backtracking with forward checking
  • "arc_consistency" — AC-3 preprocessing + backtracking
  • "min_conflicts" — Local search for optimization problems

compile(problem): Promise<FLUXBytecode>

Compile a CSP to FLUX bytecode. Returns the full instruction list with variable and constraint maps.

const bytecode = await ct.compile({
  variables: ["x", "y"],
  domains: { x: { type: "set", values: [1, 2, 3] }, y: { type: "set", values: [4, 5, 6] } },
  constraints: [{ id: "c1", variables: ["x", "y"], expression: "x != y" }],
});
console.log(bytecode.count);           // instruction count
console.log(bytecode.variableMap);     // { x: 0, y: 1 }
console.log(bytecode.sourceHash);      // deterministic hash

verify(solution, constraints): Promise<VerificationResult>

Check whether a variable assignment satisfies all constraints.

const result = await ct.verify(
  { x: 3, y: 7 },
  [{ id: "c1", variables: ["x", "y"], expression: "x + y == 10" }],
);
// result.valid === true
// result.violations === []

destroy(): void

Kill the Python subprocess and clean up.

Types

type Domain =
  | { type: "set"; values: number[] }
  | { type: "range"; min: number; max: number }
  | { type: "range_step"; min: number; max: number; step: number };

interface Constraint {
  id: string;
  variables: string[];
  expression: string;
}

interface Solution {
  assignments: Record<string, number>;
  consistent: boolean;
  solveTimeMs: number;
}

interface VerificationResult {
  valid: boolean;
  violations: string[];
  checkedCount: number;
}

FLUX ISA Overview

FLUX is the intermediate bytecode used by constraint-theory. The opcode space is divided into functional groups:

| Range | Category | Example opcodes | |-------|----------|-----------------| | 0x00-0x06 | Control flow | NOP, HALT, JMP, JZ, CALL, RET | | 0x10-0x15 | Stack / data | PUSH, POP, DUP, LOAD, STORE | | 0x20-0x26 | Domain ops | DOMAIN_INIT, DOMAIN_RESTRICT, DOMAIN_INTERSECT | | 0x30-0x33 | Constraint eval | CONSTRAINT_LOAD, CONSTRAINT_EVAL, CONSTRAINT_PROPAGATE | | 0x40-0x58 | Arithmetic / logic | ADD, EQ, LT, AND, NOT | | 0x60-0x64 | Solver strategy | BACKTRACK, FORWARD_CHECK, SELECT_VARIABLE | | 0x70-0x72 | Solution | SOLUTION_EMIT, SOLUTION_COUNT, VERIFY |

Each FLUX instruction is [opcode, ...operands]. The compile() method returns the full instruction list with metadata for debugging and introspection.

Error Handling

  • Missing Python: Bridge startup fails with clear error message
  • Missing constraint-theory: Detected during init(), reports installation instructions
  • Call timeout: Configurable via callTimeout option
  • Process crash: Auto-restart up to maxRestarts times
  • Invalid constraints: Returned as BridgeError with code and message

License

Apache-2.0