flowgraph-ai 0.1.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.
@@ -0,0 +1,252 @@
1
+ # FlowGraph Specification v2.1
2
+
3
+ A contract system optimized for AI agent verification and code modification.
4
+
5
+ ## Core Idea
6
+
7
+ FlowGraph captures **maintenance contracts** — the relationships where changing one thing silently breaks another. It is NOT a comprehensive map of the codebase, and it is NOT documentation. Every element must justify its maintenance cost.
8
+
9
+ **Principles:**
10
+
11
+ 1. **High-value only** — if the source code already says it (type signatures, call sites, method names), don't repeat it. Capture contracts that are invisible in any single file.
12
+ 2. **Maintenance-aware** — every element must prevent a real bug when code changes. If it drifts silently without causing bugs, it shouldn't be in the graph.
13
+ 3. **Query-oriented** — structured for "what breaks if I change X?" and "what must I also update?" questions.
14
+ 4. **Context-efficient** — minimize tokens. Nodes need only `kind` + `loc` unless they carry high-value metadata (schemas, enum values, foreign keys).
15
+
16
+ ## File Format
17
+
18
+ ```json
19
+ {
20
+ "$flowgraph": "2.1",
21
+ "meta": { "name": "...", "root": "src/" },
22
+ "nodes": { ... },
23
+ "edges": [ ... ],
24
+ "flows": { ... },
25
+ "invariants": [ ... ]
26
+ }
27
+ ```
28
+
29
+ `meta.root` is the base path for all `loc` references. Set it to your source root (e.g., `"src/"`, `"app/"`, `"lib/"`, or `""` for repo root).
30
+
31
+ ## Nodes
32
+
33
+ Each node has a namespaced ID: `kind:Identifier`. Most nodes need only `kind` + `loc` — additional fields are listed below but should only be added when they carry information not visible in the source file.
34
+
35
+ ### Common Fields (all nodes)
36
+
37
+ | Field | Type | Required | Description |
38
+ |-------|------|----------|-------------|
39
+ | `kind` | string | yes | One of the built-in kinds (`type`, `method`, `table`, `endpoint`, `event`) or a custom kind (see [Extensibility](#extensibility)). |
40
+ | `loc` | string | yes | File path relative to `meta.root`, optionally `:line` |
41
+
42
+ ### Node Kind: `type`
43
+
44
+ Lean reference to a type, interface, struct, or schema definition.
45
+
46
+ | Field | Type | Description |
47
+ |-------|------|-------------|
48
+ | `schema` | string | Runtime validation schema name (e.g., Zod, Joi, Pydantic). **High-value** — tells the agent a validation boundary exists. Include when a `validates` edge references this type. |
49
+ | `values` | array | For enums only. Enum values define state machines and drive switch/match statements. |
50
+
51
+ ### Node Kind: `method`
52
+
53
+ A reference to a method or function. In practice, **most method nodes need only `kind` + `loc`**. A method node exists in the graph because it's referenced by a co_change edge, validates edge, invariant scope, or flow step — not to document its behavior.
54
+
55
+ | Field | Type | Description |
56
+ |-------|------|-------------|
57
+ | `pre` | array | Preconditions not enforced by the type system. **Rarely needed** — only for critical system boundaries. |
58
+ | `post` | array | Postconditions beyond the return type. **Rarely needed** — usually restates what the method name says. |
59
+ | `side_effects` | array | Non-graph effects: `fs:`, `shell:`, `git:`, `http:`, `ext:`. Only include when truly non-obvious. |
60
+ | `errors` | object | Error contract: `{ condition: consequence }`. **Rarely needed.** |
61
+
62
+ ### Node Kind: `table`
63
+
64
+ Reference to a persistent data store. Tables are high-value nodes because they're almost always co_change sources.
65
+
66
+ | Field | Type | Description |
67
+ |-------|------|-------------|
68
+ | `fk` | array | Foreign key references. Captures cross-entity relationships. |
69
+ | `indexes` | array | Index definitions. |
70
+ | `triggers` | array | Trigger definitions. |
71
+
72
+ ### Node Kind: `endpoint`
73
+
74
+ Most endpoint nodes need only `kind` + `loc`. Request/response shapes are documentation, not contracts — put them in docs if needed.
75
+
76
+ | Field | Type | Description |
77
+ |-------|------|-------------|
78
+ | `request` | object | `{ body?, query?, params? }` shape descriptions. **Optional** — only if the shape isn't obvious from the handler code. |
79
+ | `response` | object | Status code to response shape. **Optional.** |
80
+
81
+ ### Node Kind: `event`
82
+
83
+ Minimal: just `kind` and `loc`. Include only when referenced by a flow step.
84
+
85
+ | Field | Type | Description |
86
+ |-------|------|-------------|
87
+ | `payload` | array | Parameter types. Only include if non-obvious. |
88
+
89
+ ## Edges
90
+
91
+ Edges capture relationships where changes propagate silently. **co_change and validates are the primary edge types.** Other edge types exist for completeness but should be used sparingly.
92
+
93
+ ### Primary edges (include these):
94
+
95
+ | Relation | When to include | Example |
96
+ |----------|----------------|---------|
97
+ | `co_change` | **Changing the `from` node requires also changing the `to` node.** This is the highest-value edge. Every table should have co_change edges to its repository methods. Every enum/config type should have co_change edges to code that branches on its values. | table:orders -> method:OrderRepo.create |
98
+ | `validates` | Method performs runtime schema validation on a type. Marks a trust boundary between unvalidated and validated data. | method:OrderService.create -> type:Order |
99
+
100
+ ### Secondary edges (include sparingly):
101
+
102
+ | Relation | When to include | Example |
103
+ |----------|----------------|---------|
104
+ | `calls` | Cross-file calls through deep indirection (constructor injection, plugin systems) where the call is truly non-obvious. **Most calls are visible by reading the caller — don't add edges for those.** | |
105
+ | `writes` | **Usually redundant with co_change.** Only include if the write relationship isn't captured by a co_change edge and is non-obvious. | |
106
+ | `reads` | Same as writes — usually redundant. | |
107
+ | `emits` | Method emits an event. Only include if the wiring is in a different file and non-obvious. | |
108
+ | `listens` | Something subscribes to an event. Same criteria as emits. | |
109
+
110
+ ### What to OMIT:
111
+
112
+ - `calls` edges where the call is visible by reading the method at `loc`
113
+ - `writes`/`reads` edges when a co_change edge already captures the table->method relationship
114
+ - `emits`/`listens` edges when the event wiring is in the same file or obvious from the emitter
115
+ - Any edge that would be caught by the compiler or type checker if broken
116
+
117
+ ### Edge Format
118
+
119
+ ```json
120
+ { "from": "node:id", "to": "node:id", "rel": "relation", "note": "optional context" }
121
+ ```
122
+
123
+ The `note` field explains the relationship when non-obvious (e.g., "column changes require INSERT query update").
124
+
125
+ ## Flows
126
+
127
+ Flows document complex execution paths that cross multiple files and have non-trivial branching. **Most projects have 2-5 of these.** If a flow is a straight sequence of calls visible in one method, it doesn't need a flow definition.
128
+
129
+ ```json
130
+ {
131
+ "flow-name": {
132
+ "trigger": "what initiates this",
133
+ "steps": [
134
+ { "node": "method:X", "then": "next" },
135
+ { "node": "event:Y", "then": { "condition_a": "next", "condition_b": "method:Z", "condition_c": "FAIL" } }
136
+ ]
137
+ }
138
+ }
139
+ ```
140
+
141
+ ### `then` values:
142
+ - `"next"` — proceed to next step
143
+ - `"DONE"` / `"FAIL"` — terminal states
144
+ - `"node:id"` — jump (loop or skip)
145
+ - `{ condition: target, ... }` — conditional branch
146
+
147
+ ### When to add a flow:
148
+
149
+ - The execution path crosses 3+ files
150
+ - There are 3+ branching cases (not just success/failure)
151
+ - The path includes loops, retries, or async handoffs
152
+ - A developer reading one file can't see the full picture
153
+
154
+ ### When NOT to add a flow:
155
+
156
+ - Linear sequence: endpoint -> service -> repo -> done
157
+ - Simple success/failure branching visible in one method
158
+ - The edges and source code are sufficient to understand the path
159
+
160
+ ## Invariants
161
+
162
+ Rules that span multiple components. These are the highest-value elements alongside co_change edges — they capture constraints an agent would violate without knowing about them.
163
+
164
+ | Field | Type | Description |
165
+ |-------|------|-------------|
166
+ | `id` | string | Unique ID (e.g. `INV-001`) |
167
+ | `rule` | string | What must hold |
168
+ | `scope` | array | Node IDs this applies to. Required — unscoped invariants are too vague to verify. |
169
+ | `enforce` | string | How it's enforced. Required — tells the agent where to look. |
170
+
171
+ ## Extensibility
172
+
173
+ The five built-in node kinds (`type`, `method`, `table`, `endpoint`, `event`) cover most projects. If your project has domain concepts that don't fit, define custom node kinds:
174
+
175
+ | Custom Kind | Use Case | Example |
176
+ |-------------|----------|---------|
177
+ | `queue` | Message queues, job queues, task brokers | `queue:order-processing` |
178
+ | `service` | External service dependencies | `service:stripe-payments` |
179
+ | `config` | Configuration groups that affect behavior | `config:feature-flags` |
180
+ | `job` | Background jobs, cron tasks | `job:nightly-cleanup` |
181
+ | `migration` | Database migrations | `migration:003_add_indexes` |
182
+
183
+ Custom kinds use the same common fields (`kind`, `loc`) and participate in edges, flows, and invariants like any built-in kind.
184
+
185
+ ## Impact Analysis
186
+
187
+ The primary use case for an AI agent is: **"I need to change X. What else is affected?"**
188
+
189
+ A verification script should support an `--impact node:id` flag that traverses the graph:
190
+
191
+ 1. Find the node being changed.
192
+ 2. Follow all `co_change` edges (both directions) — these are required co-modifications.
193
+ 3. Check which flows include the node — the flow may need updating.
194
+ 4. Check which invariants scope the node — the invariant may be violated.
195
+
196
+ ## Verification Procedure
197
+
198
+ ### Structural
199
+ For each node, confirm the artifact exists at `loc` with expected shape.
200
+
201
+ ### Relational
202
+ For each edge, confirm the relationship exists in source. Pay special attention to `co_change` edges — these are maintenance contracts. `validates` edges should confirm the schema parse call exists.
203
+
204
+ ### Sequential
205
+ For each flow, trace the path and confirm all step nodes exist and branch targets are reachable.
206
+
207
+ ### Invariant
208
+ For each invariant, examine scoped code and confirm the rule holds.
209
+
210
+ ### Output
211
+ - **PASS** — verified
212
+ - **FAIL** — does not match source
213
+ - **WARN** — partially matches or requires semantic judgment
214
+
215
+ ## Getting Started
216
+
217
+ FlowGraph is designed for incremental adoption. Start with the elements that deliver the most value.
218
+
219
+ ### Step 1: Start with co_change edges
220
+
221
+ These deliver the most immediate value. Identify the pairs where changing one thing silently breaks another:
222
+ - Database schema -> repository/DAO methods that use raw queries
223
+ - Enum/union types -> switch/match statements that branch on values
224
+ - Config schemas -> code that reads config values by key
225
+ - Interface types -> implementation classes
226
+
227
+ Even 5-10 `co_change` edges with their connected nodes make a useful flowgraph.
228
+
229
+ ### Step 2: Add invariants
230
+
231
+ Write down the cross-cutting rules that a new contributor (or AI agent) would violate without being told:
232
+ - "All user input must be validated before persistence"
233
+ - "Every database write must be wrapped in a transaction"
234
+ - "Feature flags must be checked before calling experimental endpoints"
235
+
236
+ Include `scope` and `enforce` — vague invariants are useless.
237
+
238
+ ### Step 3: Add validates edges for runtime boundaries
239
+
240
+ Identify where unvalidated data becomes validated (Zod parse, JSON schema validation, etc.). These mark trust boundaries that must not be removed.
241
+
242
+ ### Step 4: Add flows for complex paths
243
+
244
+ Only model execution paths that cross multiple files and have non-trivial branching (3+ cases). Most projects have 2-5 of these.
245
+
246
+ ### What NOT to do
247
+
248
+ - Don't add method nodes with pre/post/errors unless the contract is truly non-obvious and high-value.
249
+ - Don't add calls/writes/reads/emits edges for relationships visible by reading the source.
250
+ - Don't add endpoint request/response shapes — those are documentation, not contracts.
251
+ - Don't add flows for straight-line code — edges are sufficient.
252
+ - Don't add nodes that aren't referenced by any edge, invariant, or flow.
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "flowgraph-ai",
3
+ "version": "0.1.0",
4
+ "description": "Machine-verifiable maintenance contracts for codebases. Capture what breaks when code changes.",
5
+ "type": "module",
6
+ "bin": {
7
+ "flowgraph-ai": "./bin/flowgraph.mjs"
8
+ },
9
+ "files": [
10
+ "bin/",
11
+ "flowgraph-spec-v2.1.md",
12
+ "LICENSE"
13
+ ],
14
+ "keywords": [
15
+ "flowgraph",
16
+ "verification",
17
+ "maintenance",
18
+ "contracts",
19
+ "architecture",
20
+ "co-change",
21
+ "impact-analysis",
22
+ "ai-agents"
23
+ ],
24
+ "author": "404FoundingFather",
25
+ "license": "MIT",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/404FoundingFather/flowgraph.git"
29
+ },
30
+ "engines": {
31
+ "node": ">=18"
32
+ }
33
+ }