Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ It gates CI on per-Metric Budgets and on Regressions against a base Git ref.
Languages and Metrics are Plugins: TypeScript/JavaScript, Rust, PHP, and Dart ship as built-in Language plugins, and third-party languages and Metrics load as WebAssembly Plugins.

<p align="center">
<a href="./docs/guide/README.md">Guide</a> ·
<a href="./docs/guide/philosophy-and-architecture.md">Philosophy</a> ·
<a href="#install">Install</a> ·
<a href="#use">Use</a> ·
<a href="#configure">Configure</a> ·
<a href="#what-to-expect">What to expect</a> ·
<a href="#github-actions">GitHub Actions</a> ·
<a href="./docs/reference.md">Reference</a> ·
<a href="./docs/programmatic.md">Programmatic API</a> ·
<a href="./docs/plugins.md">Plugins</a> ·
<a href="#development">Development</a>
<a href="./llms.txt">llms.txt</a>
</p>

[GLOSSARY.md](./GLOSSARY.md) defines every term on this page.
Expand Down
21 changes: 21 additions & 0 deletions docs/guide/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# codeint Guide

Welcome to the modular user guide for codeint.
Each page below focuses on one topic and links to [docs/reference.md](../reference.md) for exact command flags, exit codes, and `codeint.json` schema keys.

## Reading map

1. [Philosophy and Architecture](./philosophy-and-architecture.md): why codeint measures Software entropy, how the four Metrics work, and how the codebase is structured.
2. [Getting Started](./getting-started.md): install codeint, initialize `codeint.json`, and run your first `check`, `diff`, and `trend`.
3. [Configuration (`codeint.json`)](./configuration.md): set Budgets, Regression limits, Source-set globs, and multi-language folders.
4. [Built-in Languages](./languages.md): how TypeScript/JavaScript, Rust, PHP, and Dart are measured, plus their calibrated Reference ranges.
5. [CI and GitHub Actions](./ci-and-github-actions.md): gate pull requests with `codeint diff` and upload SARIF findings to GitHub code scanning.
6. [Programmatic API](../programmatic.md): call codeint from JavaScript, TypeScript, or Rust.
7. [Plugins](../plugins.md): build, hash, declare, and test external WebAssembly Language and Metric plugins.

## Reference and domain docs

- [CLI and `codeint.json` Reference](../reference.md): tested reference for every command, option, exit code, report format, and schema key.
- [Glossary](../../GLOSSARY.md): canonical definitions of every domain term.
- [Architecture Decision Records](../adr/0001-review-mode-never.md): recorded design decisions and calibrated Reference range panels.
- [llms.txt](../../llms.txt): compact documentation index for coding agents and LLM tools.
45 changes: 45 additions & 0 deletions docs/guide/ci-and-github-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# CI and GitHub Actions

Run `codeint diff` on every pull request so Software entropy Regressions are caught before merge.

## GitHub Actions workflow

Add `.github/workflows/codeint.yml` to diff against the pull request base branch and upload SARIF findings to GitHub code scanning:

```yaml
name: codeint

on:
pull_request:

permissions:
contents: read
security-events: write

jobs:
codeint:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
fetch-depth: 0
- name: Install codeint
run: |
curl -fsSL https://github.com/avidianity/codeint/releases/latest/download/install.sh | bash
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Diff against pull request base
env:
BASE_REF: ${{ github.base_ref }}
run: codeint diff "origin/$BASE_REF" --format sarif --output codeint.sarif
- name: Upload SARIF
if: always() && hashFiles('codeint.sarif') != ''
uses: github/codeql-action/upload-sarif@1bd86f2fdb4b86187fa4059f16f31a3202acc9ea
with:
sarif_file: codeint.sarif
```

## Notes

- `fetch-depth: 0` fetches full branch history so `codeint diff` can compute the Git merge-base.
- `if: always() && hashFiles('codeint.sarif') != ''` ensures SARIF annotations still upload when `codeint diff` exits `1` on a Budget or Regression failure.
- To tune Budgets or Regression tolerances, edit `codeint.json`; see [Configuration](./configuration.md).
48 changes: 48 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Configuration (`codeint.json`)

codeint reads `codeint.json` from the root of your Git repository.
Without `codeint.json`, codeint measures every supported language with default settings and no hard Budgets.

## Starter configuration

Run `codeint init` to generate a starter file with a versioned `$schema` URL for editor autocomplete.
When you upgrade via `codeint update`, codeint updates that `$schema` tag automatically.

```json
{
"$schema": "https://raw.githubusercontent.com/avidianity/codeint/v0.3.0/schema/codeint.schema.json",
"source_sets": {
"generated": ["src/**/*.generated.ts"],
"vendored": ["vendor/**"]
},
"budgets": {
"production": {
"erosion": 0.35,
"duplication": 0.08,
"verbosity": 0.08,
"import_cycles": 0.05
}
},
"regression_limits": {
"generated": {
"duplication": { "ratio_rise": 1, "new_findings": "never" }
}
},
"languages": [
{ "plugin": "typescript-javascript", "include": ["web"], "exclude": ["web/dist"] },
{ "plugin": "rust", "include": ["crates"], "exclude": [] },
{ "plugin": "php", "include": ["app", "src"], "exclude": [] },
{ "plugin": "dart", "include": ["lib"], "exclude": [] }
]
}
```

## Key sections

- **`source_sets`**: globs that override built-in classification into `production`, `test`, `generated`, or `vendored`.
- **`budgets`**: hard maximum ratios (`0` to `1`) per Source set and Metric; `codeint check` and `codeint diff` exit `1` when exceeded.
- **`regression_limits`**: per-Metric or per-Source-set tolerances for `codeint diff` (`ratio_rise` defaults to `0.01`; `new_findings` accepts `any_new_finding`, `numerator_rise_with_new_findings`, or `never`).
- **`languages`**: restrict built-in or external Language plugins to specific folders and exclusions.
- **`plugins`** and **`plugin_limits`**: load external WebAssembly Language or Metric plugins with SHA-256 verification and memory/time caps.

See [docs/reference.md#codeintjson](../reference.md#codeintjson) for the complete schema table and tested examples.
63 changes: 63 additions & 0 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Getting Started

This guide walks through installing codeint, initializing `codeint.json`, and running your first measurements.

## Install

Install the standalone binary on macOS or Linux:

```sh
curl -fsSL https://github.com/avidianity/codeint/releases/latest/download/install.sh | bash
```

Install the standalone binary on Windows (PowerShell):

```powershell
irm https://github.com/avidianity/codeint/releases/latest/download/install.ps1 | iex
```

Or run directly through npm without a global install:

```sh
npx codeint check
```

## Run inside a Git repository

1. Create a starter `codeint.json` pinned to your installed version's JSON schema:

```sh
codeint init
```

2. Measure the current working tree:

```sh
codeint check
```

3. Compare your branch against `main` and fail on any Regression:

```sh
codeint diff main
```

4. Inspect how each Metric evolved over the latest 20 main-line commits:

```sh
codeint trend
```

5. Upgrade a standalone binary in place (and keep `codeint.json`'s `$schema` tag in sync):

```sh
codeint update
```

## Exit codes

- `0`: succeeded with no Budget or Regression failures.
- `1`: succeeded, and at least one Budget or Regression failed.
- `2`: operational error (invalid `codeint.json`, missing folder, unknown option, or not inside a Git repository).

See [docs/reference.md](../reference.md) for every command flag and output format (`toon`, `json`, `sarif`, `markdown`).
28 changes: 28 additions & 0 deletions docs/guide/languages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Built-in Languages

codeint ships four built-in Language plugins in pure, safe Rust (`#![forbid(unsafe_code)]`).
Each plugin parses the modern superset of its language family with no language-version pin needed in `codeint.json`.

## Summary

| Plugin name | Extensions | Workspace manifest | Default Test / Generated rules |
| ----------- | ---------- | ------------------ | ------------------------------ |
| `typescript-javascript` | `.ts`, `.tsx`, `.mts`, `.cts`, `.js`, `.jsx`, `.mjs`, `.cjs` | `package.json` / `pnpm-workspace.yaml` | `*.test.*`, `*.spec.*`, `__tests__/`, `test/`, `tests/` |
| `rust` | `.rs` | `Cargo.toml` | `tests/`, `benches/`, `examples/`, `tests.rs`, `*_test.rs`, `*_tests.rs`; inline `#[cfg(test)]` excluded from Production |
| `php` | `.php` | `composer.json` (`repositories` of type `path`) | `tests/`, `test/`, `*Test.php`, `*.test.php` |
| `dart` | `.dart` | `pubspec.yaml` (`workspace`) | `test/`, `tests/`, `integration_test/`, `*_test.dart`; `*.g.dart`, `*.freezed.dart`, `*.gr.dart`, `*.mocks.dart`, `*.pb*.dart` -> Generated |

Core rules also classify files under `vendor/` or `third_party/` as `vendored` and files with generated-file header comments (`@generated`, `GENERATED CODE`, `DO NOT EDIT`) as `generated`.

## Calibrated Reference ranges

Measured across 20 pre-2023 public repositories per language (10th to 90th percentile):

| Language | Erosion | Duplication | Verbosity | Import cycles | Record |
| -------- | ------- | ----------- | --------- | ------------- | ------ |
| TypeScript / JavaScript | 35.2% to 60.0% | 1.7% to 34.6% | 1.8% to 34.7% | 0.0% to 30.8% | [ADR 0008](../adr/0008-typescript-reference-ranges.md) |
| Rust | 38.0% to 64.2% | 5.0% to 32.5% | 6.0% to 32.9% | 2.1% to 50.6% | [ADR 0010](../adr/0010-rust-reference-ranges.md) |
| PHP | 5.5% to 67.7% | 4.1% to 29.0% | 4.2% to 29.2% | 0.0% to 10.7% | [ADR 0011](../adr/0011-php-reference-ranges.md) |
| Dart | 4.7% to 41.9% | 7.3% to 25.6% | 8.0% to 25.9% | 7.3% to 52.4% | [ADR 0012](../adr/0012-dart-reference-ranges.md) |

To add another language as an external WebAssembly plugin, see [docs/plugins.md](../plugins.md).
70 changes: 70 additions & 0 deletions docs/guide/philosophy-and-architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Philosophy and Architecture

This page explains why codeint exists, the logic behind its Metrics, and how the tool is implemented.

## Why Software entropy matters

Coding agents write locally valid code very quickly.
Each pull request can pass tests and linters while quietly adding copied blocks, redundant branches, deeper functions, and tangled imports.
Over dozens of changes, that structural drag compounds until both humans and agents slow down.

codeint measures **Software entropy** directly from the shape of source code so teams see it grow before it compounds.

## Core design principles

### 1. Four independent Metrics, never one blended score

codeint reports four Metrics side by side and never collapses them into a single grade or index ([ADR 0003](../adr/0003-no-combined-score.md)).
A combined score hides regressions when one Metric improves while another worsens.

- **Erosion**: the share of total **Function mass** (`cyclomatic_complexity * sqrt(source_lines)`) held by functions with cyclomatic complexity over 10.
Weighting by both complexity and size highlights the few large, tangled functions that hurt maintainability most.
- **Duplication**: the share of source lines inside **Clone groups** (two or more fragments matching at least 75 normalized tokens and 14 distinct normalized tokens, [ADR 0007](../adr/0007-default-minimum-clone-size.md)).
Normalizing identifiers and literals catches renamed copy-paste while ignoring import blocks and repetitive data tables.
- **Verbosity**: the share of source lines that are **Flagged lines**, clone lines, or both.
Each Flagged-line rule targets a zero-false-positive redundant construct (such as `else` after an unconditional `return` or `if (c) return true; else return false;`).
- **Import cycles**: the share of modules that belong to at least one runtime **Import cycle** (strongly connected component of size 2 or more).

### 2. Strict Source-set separation

Every file belongs to exactly one **Source set**: `production`, `test`, `generated`, or `vendored`.
Test fixtures, generated code (`*.g.dart`, `*.pb.dart`, `@generated`), and third-party vendor code are measured separately so they never distort Production ratios.

### 3. Pre-2023 human-written Reference ranges

Raw ratios are hard to interpret without context.
For each built-in language, codeint measures a panel of 20 public repositories pinned before `2023-01-01` (predating widespread coding-agent adoption) and publishes the 10th to 90th percentile as its **Reference range** ([ADR 0008](../adr/0008-typescript-reference-ranges.md), [ADR 0010](../adr/0010-rust-reference-ranges.md), [ADR 0011](../adr/0011-php-reference-ranges.md), [ADR 0012](../adr/0012-dart-reference-ranges.md)).

### 4. Budgets and Regressions

- A **Budget** (`codeint check` and `codeint diff`) sets a hard ceiling for a Metric in a Source set.
- A **Regression** (`codeint diff <base>`) compares the working tree against the Git merge-base and fails when a change introduces new Findings or raises a ratio past its limit.
Together, Budgets cap absolute entropy while Regressions act as a ratchet on every pull request.

### 5. Version-free language parsing

codeint is a structural entropy tool, not a compiler.
Every built-in Language plugin parses the modern superset of its language family and uses version-agnostic Flagged-line rules, so older and newer codebases work without version pins in `codeint.json`.

## How codeint is implemented

codeint is built in safe Rust (`#![forbid(unsafe_code)]`, [ADR 0004](../adr/0004-forbid-unsafe-code.md)) around two clean seams:

```text
Source files + Git history (crates/git)
│
▼ Seam 2: Language plugin (built-in Rust or Wasm component)
Language-neutral Code model (crates/model)
│
▼ Seam 3: Metric plugin (built-in Rust or Wasm component)
Metrics + Findings + Diff + Trend (crates/metrics, crates/report)
│
▼
CLI + Rust SDK (crates/cli) & JS/TS SDK (npm/codeint)
```

- **Git-native measurement (`crates/git`)**: reads tracked working-tree files and historical commits directly from Git objects via `gix`, never modifying the working tree during `diff` or `trend`, with a content-addressed result cache for fast repeated runs.
- **Language plugins (`plugins/languages/*`)**: own every language-specific rule (parsing, functions, decision points, tokens, Flagged lines, imports, workspaces, Source-set defaults) and lower files into the language-neutral **Code model** (`crates/model`).
TypeScript/JavaScript uses `oxc_parser`; Rust uses `syn` and `proc-macro2`; PHP and Dart use pure-Rust zero-FFI lexers sharing common helpers in `crates/model/src/scan.rs`.
- **WebAssembly plugin host (`crates/plugin-host`)**: loads third-party Language and Metric plugins as WebAssembly components ([ADR 0002](../adr/0002-plugins-as-wasm-components.md)) with mandatory SHA-256 verification, zero host capabilities (no filesystem, network, env, or clock access), and configurable memory/timeout caps.
- **Zero-duplication programmatic surfaces**: `crates/cli/src/lib.rs` exposes the Rust library API directly, and `npm/codeint` wraps the bundled native binary with zero-dependency ESM, CommonJS, and TypeScript declarations.
32 changes: 32 additions & 0 deletions llms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# codeint

> codeint is a command-line tool and programmatic SDK (JavaScript/TypeScript and Rust) that measures Software entropy (Erosion, Duplication, Verbosity, and Import cycles) across TypeScript/JavaScript, Rust, PHP, Dart, and external WebAssembly Language and Metric plugins.

## Guide

- [Guide Index](docs/guide/README.md): reading map for all user documentation.
- [Philosophy and Architecture](docs/guide/philosophy-and-architecture.md): why Software entropy matters, how the four Metrics work, and how the codebase is structured.
- [Getting Started](docs/guide/getting-started.md): install codeint, initialize `codeint.json`, and run `check`, `diff`, `trend`, and `update`.
- [Configuration](docs/guide/configuration.md): configure `codeint.json` Budgets, Regression limits, Source sets, and multi-language folders.
- [Built-in Languages](docs/guide/languages.md): TypeScript/JavaScript, Rust, PHP, and Dart rules and calibrated Reference ranges.
- [CI and GitHub Actions](docs/guide/ci-and-github-actions.md): pull-request diff gating and SARIF code scanning setup.

## Reference and APIs

- [CLI and `codeint.json` Reference](docs/reference.md): tested reference for every command, flag, exit code, report format, and schema key.
- [Programmatic API](docs/programmatic.md): JavaScript/TypeScript SDK (`codeint` npm package) and Rust library API (`codeint-cli`).
- [Plugins](docs/plugins.md): build, hash, declare, and test external WebAssembly Language and Metric plugins against the versioned WIT contract.
- [JSON Schema](schema/codeint.schema.json): JSON Schema for `codeint.json`.
- [Glossary](GLOSSARY.md): canonical domain terminology.

## Architecture Decision Records

- [ADR 0002: Plugins as WebAssembly components](docs/adr/0002-plugins-as-wasm-components.md)
- [ADR 0003: No combined score](docs/adr/0003-no-combined-score.md)
- [ADR 0004: Forbid unsafe code](docs/adr/0004-forbid-unsafe-code.md)
- [ADR 0007: Default minimum clone size](docs/adr/0007-default-minimum-clone-size.md)
- [ADR 0008: TypeScript and JavaScript Reference ranges](docs/adr/0008-typescript-reference-ranges.md)
- [ADR 0009: Rust inline test exclusion](docs/adr/0009-rust-inline-test-exclusion.md)
- [ADR 0010: Rust Reference ranges](docs/adr/0010-rust-reference-ranges.md)
- [ADR 0011: PHP Reference ranges](docs/adr/0011-php-reference-ranges.md)
- [ADR 0012: Dart Reference ranges](docs/adr/0012-dart-reference-ranges.md)