Guidance for coding agents working in this repository. Derived from CLAUDE.md.
npm run build- Development build in/buildnpm run build watch- Development build with file watchingnpm run build production- Production build in/distnpm run clean- Remove/buildand/distnpm run typecheck- Run TypeScript type checking (always when completing a task)
npm run test- Requires a specific test suite to be specifiednpm run test compute-engine/<test-name>- Run specific test file (e.g.,npm run test compute-engine/arithmetic)npm run test snapshot- Update test snapshotsnpm test- Alias fornpm run test
npm start- Development build with watch and local servernpm run lint- Run ESLint with auto-fixnpm run doc- Generate documentation
- Tests live under
/test/ - Pattern:
npm run test compute-engine/<test-name>maps to/test/compute-engine/<test-name>.test.ts
src/compute-engine/index.ts- ComputeEngine: parsing, evaluation, manipulation, scopes, precision, validationsrc/compute-engine/boxed-expression/- Boxed expression typesBoxedExpression,BoxedNumber,BoxedSymbol,BoxedFunction,BoxedString
src/common/type/- Type system with inference and subtype checkingsrc/compute-engine/latex-syntax/- LaTeX parsing to MathJSONsrc/math-json/- MathJSON structures and utilities
- Boxing system: expressions are boxed with consistent interfaces
- Canonical forms: normalized representation for efficiency
- Scoped evaluation: symbol definitions and assumptions in lexical scopes
- Library system: domain-specific math libraries loaded selectively
- Validation modes:
strict(default) vs non-strict - Numeric precision: machine (64-bit) and arbitrary precision (Decimal.js)
- Canonical (
{ canonical: true }or default)- Fully canonicalized,
bind()is called,isCanonicaltrue
- Fully canonicalized,
- Structural (
{ structural: true })- Bound, only structural normalization,
isStructuraltrue - Use to avoid specific canonical transforms (e.g., keep
Power(x, 1/3))
- Bound, only structural normalization,
- Non-canonical (
{ canonical: false }withoutstructural)- Not bound, not canonical/structural
- Cannot be used in arithmetic operations (
.mul(),.add(), etc.)
Pitfall: ce._fn('Power', ..., { canonical: false }).mul(...) will assert.
Use structural mode instead:
ce.function('Power', [base, exp], { structural: true }).mul(other)Note: ce._fn() does not support structural.
Critical: Do not call .simplify() inside simplification rules or functions
used by them. This can cause infinite recursion.
src/compute-engine/symbolic/simplify-rules.ts- Polynomial helpers called by rules (e.g.,
polynomialDivide,polynomialGCD,cancelCommonFactors) - Any function in the simplification pipeline
- Top-level APIs
- Tests
- Evaluation contexts
- On simple numeric coefficients (with care)
Return canonical expressions and let callers decide whether to simplify.
- Expression deduplication in
simplify.ts - Step limit guard (max 1000 steps)
- Loop detection in main simplify loop
- Parse (LaTeX → MathJSON → BoxedExpression)
- Canonicalize
- Evaluate
- Simplify
- Serialize (LaTeX/MathJSON)
- Core engine:
src/compute-engine/index.ts - Base expression:
src/compute-engine/boxed-expression/abstract-boxed-expression.ts - Arithmetic:
src/compute-engine/boxed-expression/arithmetic-*.ts - Validation:
src/compute-engine/boxed-expression/validate.ts - Libraries:
src/compute-engine/library/ - Type system:
src/common/type/ - Test helpers:
test/utils.ts - Generated:
API.md(do not edit manually)
Common problematic chain:
abstract-boxed-expression.ts → compile.ts → library/utils.ts → collections.ts → box.ts → abstract-boxed-expression.ts
Resolution strategy:
- Extract shared utilities (e.g.,
canonical-utils.ts) - Prefer static imports after breaking cycles
- Verify via:
npx tsx -e "import {ComputeEngine} from './src/compute-engine'; new ComputeEngine()"Warning signs:
ReferenceError: Cannot access '_BoxedExpression' before initialization- Build failures with circular dependency warnings
- ESLint:
Dependency cycle detected