@forwardimpact/libinvariant 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Zachary Rice
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # libinvariant
2
+
3
+ <!-- BEGIN:description — Do not edit. Generated from package.json. -->
4
+
5
+ Repository invariant checks — instruction-layer length caps, JTBD block
6
+ validation, and a declarative rule-module runner over a caller-supplied rules
7
+ directory.
8
+
9
+ <!-- END:description -->
10
+
11
+ ## Getting Started
12
+
13
+ libinvariant is an import-only library: it ships no CLI. To run the checks,
14
+ hire the Jidoka product — `npx @forwardimpact/jidoka` or the installed
15
+ `jidoka` binary — which wires these handlers to a command surface.
16
+
17
+ ```js
18
+ import {
19
+ checkInstructions,
20
+ checkJtbd,
21
+ checkInvariants,
22
+ } from "@forwardimpact/libinvariant";
23
+
24
+ const findings = await checkInstructions({ root, runtime });
25
+ const { findings: jtbdFindings, stale } = await checkJtbd({ root, runtime });
26
+ const ruleFindings = await checkInvariants({
27
+ root,
28
+ rulesDir: ".jidoka/invariants",
29
+ runtime,
30
+ });
31
+ ```
32
+
33
+ The `checkInstructions` and `checkJtbd` handlers implement the contract
34
+ described in
35
+ [JIDOKA.md](https://github.com/forwardimpact/monorepo/blob/main/JIDOKA.md):
36
+
37
+ - `checkInstructions` — every layer (L1 CLAUDE.md, L2 CONTRIBUTING.md /
38
+ JTBD.md, L3 agent profile, L4 agent reference, L5 SKILL.md, L6 skill
39
+ reference, L7 checklist block) is gated by a line cap **and** a word cap.
40
+ Either breach fails.
41
+ - `checkJtbd` — each `package.json .jobs` entry is validated against the JTBD
42
+ schema; with `fix`, marker-delimited blocks in `<dir>/README.md`,
43
+ `<dir>/<pkg>/README.md`, and root `JTBD.md` are regenerated.
44
+
45
+ ## Invariants
46
+
47
+ `checkInvariants` is a generic host for a repository's own invariant checks.
48
+ It loads every `*.rules.mjs` module under the caller-supplied `rulesDir` and
49
+ runs each module's declarative rule catalogue through the shared rules
50
+ engine. The library carries no discovery default — the calling product or
51
+ script names the directory (the Jidoka CLI supplies `.jidoka/invariants`).
52
+ The policies stay in the repository; the library ships only the engine.
53
+
54
+ A rule module's default export is:
55
+
56
+ ```js
57
+ export default {
58
+ name: "ambient-deps",
59
+ // `build` (and `seed`) receive the injected build kit; the module never
60
+ // imports the engine (it loads into consuming repos via npx, where the
61
+ // package is not resolvable from the rules directory). Return plain
62
+ // subjects per scope, plus optional shared ctx the rules read.
63
+ build: (kit) => ({
64
+ subjects: { "src-file": kit.scanAst({ dirs, match, extract }) },
65
+ ctx: { deny: kit.config("ambient-deps.deny.yml", {}) },
66
+ }),
67
+ // Declarative rules over those subjects: either a static array, or a
68
+ // `(ruleKit) => array` factory that builds them from the rule helpers.
69
+ rules: ({ parseError, failAll }) => [
70
+ parseError("src-file"),
71
+ { id, scope, severity, when, check, message, hint },
72
+ ],
73
+ // Optional: seed text (e.g. a regenerated grandfather deny-list) the
74
+ // caller can print. Also receives the build kit.
75
+ seed: (kit) => "…",
76
+ };
77
+ ```
78
+
79
+ ### The build kit
80
+
81
+ The engine binds a kit per run to the repo `root`, the module's own `dir`
82
+ (for co-located config), and the `runtime` bag (fs and ripgrep route through
83
+ it, so the engine carries no ambient dependencies). The module declares only
84
+ policy; the kit owns the mechanism:
85
+
86
+ - `scan({ dirs, match, skip?, under?, read? })` — collect files as
87
+ `{ path, rel, text? }`; `under` restricts to the per-package `src`/`test`
88
+ shape.
89
+ - `scanAst({ dirs, match, extract, locations?, … })` — read + parse each file
90
+ and merge `extract(ast)`; a parse failure becomes `{ path, rel, parseError }`.
91
+ - `parse(src, path, opts?)`, `walk(ast, visit)` — the lower-level AST seam.
92
+ - `grep({ pattern | patterns, paths?, globs?, caseSensitive?, onlyMatching?,
93
+ dedupe? })` — ripgrep matches as `{ path, lineNo, text, reason? }`, with
94
+ per-entry `exclude` and built-in de-duplication.
95
+ - `restatementDrift({ entries, equal })` — the shared "single source restated
96
+ across consumers" scan + compare (service URLs, scalar values).
97
+ - `enumDrift.build(registry)` / `enumDrift.seed(registry)` — the
98
+ enumeration-drift engine: assert (or seed) that every consumer's fenced
99
+ `<!-- enum:TOPIC:PROPERTY -->` block matches its source-of-truth set (an
100
+ fs-glob or md-table probe). Pass a parsed topics registry (e.g.
101
+ `config(topicsFile)`); pair with the rule kit's `enumDriftRules`.
102
+ - `readText`, `readJson`, `config(name, fallback?)` (co-located JSON/YAML),
103
+ `listDir(path, { dirsOnly? })`.
104
+ - `lineAt(text, offset)`, `glob(pattern)`.
105
+
106
+ ### The rule kit
107
+
108
+ When `rules` is a function it receives the rule helpers:
109
+
110
+ - `parseError(scope, { id?, hint? })` — fails any subject carrying a
111
+ `parseError` (paired with `scanAst`).
112
+ - `failAll(scope, { id, message, hint?, when? })` — fails every subject in
113
+ scope (the build step already decided each is a violation).
114
+ - `enumDriftRules` — the enumeration-drift rule set, paired with the build
115
+ kit's `enumDrift` (expose via `rules: (kit) => kit.enumDriftRules`).
116
+
117
+ Findings render in the same ESLint-style format across the handlers
118
+ (`emitFindingsJson` for machine output); any finding fails the run.
119
+
120
+ ## Documentation home
121
+
122
+ libinvariant shares the **Run a Predictable Platform** job goal with the
123
+ service-lifecycle libraries (librc, libsupervise, libtelemetry, libpreflight),
124
+ but its full guide home is the Jidoka standard at
125
+ <https://www.jidoka.team/> and [JIDOKA.md](../../JIDOKA.md), not the
126
+ service-lifecycle guide tree under `websites/fit/docs/libraries/`.
127
+
128
+ **Decision (2026-06-27):** this is deliberate scope separation, not a gap. The
129
+ invariant checks run at **authoring time** against a repository's instruction
130
+ layers and JTBD blocks; the service-lifecycle libraries run at **service
131
+ runtime** against a live process. Mixing the two into one guide would blur the
132
+ audience. The service-lifecycle Big Hire carries a one-line cross-link to the
133
+ Jidoka standard so a reader who lands there can find this check, and that is
134
+ the only link the service-lifecycle tree should carry. Future doc audits should
135
+ treat the absence of a service-lifecycle guide page for the invariant checks
136
+ as intended.
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@forwardimpact/libinvariant",
3
+ "version": "0.2.0",
4
+ "description": "Repository invariant checks — instruction-layer length caps, JTBD block validation, and a declarative rule-module runner over a caller-supplied rules directory.",
5
+ "keywords": [
6
+ "invariants",
7
+ "instructions",
8
+ "jtbd",
9
+ "rules",
10
+ "checklist",
11
+ "agent"
12
+ ],
13
+ "homepage": "https://www.forwardimpact.team",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/forwardimpact/monorepo.git",
17
+ "directory": "libraries/libinvariant"
18
+ },
19
+ "license": "Apache-2.0",
20
+ "author": "D. Olsson <hi@senzilla.io>",
21
+ "jobs": [
22
+ {
23
+ "user": "Platform Builders",
24
+ "goal": "Run a Predictable Platform",
25
+ "trigger": "A service fails on a customer machine and the cause is a missing precondition, an unsupervised process, missing telemetry, or stale instructions.",
26
+ "bigHire": "check preconditions before anything heavy runs, supervise long-running processes, emit structured telemetry, and keep instruction files honest.",
27
+ "littleHire": "verify a docs change before commit and trust the layered architecture has not drifted.",
28
+ "competesWith": "failing deep in execution instead of at startup; ad-hoc process management; console.log debugging; instruction files nobody validates"
29
+ }
30
+ ],
31
+ "type": "module",
32
+ "main": "./src/index.js",
33
+ "exports": {
34
+ ".": "./src/index.js"
35
+ },
36
+ "files": [
37
+ "src/**/*.js",
38
+ "README.md"
39
+ ],
40
+ "scripts": {
41
+ "test": "bun test test/*.test.js"
42
+ },
43
+ "dependencies": {
44
+ "@forwardimpact/libcli": "^0.1.9",
45
+ "@forwardimpact/libutil": "*",
46
+ "acorn": "^8.17.0",
47
+ "prettier": "^3.8.4",
48
+ "yaml": "^2.9.0"
49
+ },
50
+ "devDependencies": {
51
+ "@forwardimpact/libmock": "^0.1.0"
52
+ },
53
+ "engines": {
54
+ "bun": ">=1.2.0",
55
+ "node": ">=22.0.0"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }