migraguard 0.8.4 → 0.9.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/README.md CHANGED
@@ -46,7 +46,11 @@ npx migraguard squash
46
46
  npx migraguard lint && npx migraguard check
47
47
  npx migraguard dump
48
48
 
49
- # In PRs, CI runs lint + check (+ optionally verify)
49
+ # LLM-powered safety audit (optional requires agent-contracts-runtime + API key)
50
+ npm install --save-dev agent-contracts-runtime
51
+ npx migraguard audit --adapter openai
52
+
53
+ # In PRs, CI runs lint + check (+ optionally verify + audit)
50
54
  ```
51
55
 
52
56
  ## Design Philosophy
@@ -56,6 +60,7 @@ npx migraguard dump
56
60
  - **One release = one file**: Migration files are squashed into a single file before release, simplifying error recovery. In DAG mode, independent DDL can be released individually
57
61
  - **Parallel releases via dependency tree**: DDL dependencies are analyzed to build a DAG, enabling parallel releases for independent changes
58
62
  - **Shift verification left**: Linting, checksum-based tamper detection, and schema dump diffs run at the PR stage
63
+ - **Agent-native**: Domain-specific semantic reasoning is encapsulated inside the toolchain itself. Higher-level agents do not need to know every PostgreSQL lock rule or expand/contract pattern — they invoke migraguard and consume structured findings
59
64
  - **Minimal footprint**: Two CLI tools (`psql`, `pg_dump`) and one npm library ([libpg-query](https://github.com/pganalyze/libpg-query)) for the primary PostgreSQL path. MySQL uses `mysql` + `mysqldump` CLIs + [mysql2](https://github.com/sidorares/node-mysql2) (optional peer dep); SQLite uses `sqlite3` CLI + [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) (optional peer dep). No external linter required — lint rules are built in via AST analysis. MySQL/SQLite linting uses [node-sql-parser](https://github.com/taozhi8833998/node-sql-parser) as a parallel, feature-limited engine
60
65
 
61
66
  ## Dialect support
@@ -210,6 +215,8 @@ See [docs/expand-contract.md](docs/expand-contract.md) for the complete guide: f
210
215
 
211
216
  ## Commands
212
217
 
218
+ ### Deterministic Commands
219
+
213
220
  | Command | Description |
214
221
  |---------|-------------|
215
222
  | `new <name>` | Generate a new migration SQL file |
@@ -218,6 +225,7 @@ See [docs/expand-contract.md](docs/expand-contract.md) for the complete guide: f
218
225
  | `apply` | Execute pending migrations via native CLI (`psql` / `mysql` / `sqlite3`) |
219
226
  | `apply --with-drift-check` | Local: drift check → apply → dump update |
220
227
  | `apply --from-baseline` | Apply `schema.sql` baseline, then remaining migrations |
228
+ | `apply --dry-run` | Preview pending migrations without executing |
221
229
  | `resolve <file>` | Mark a failed migration as skipped (explicit judgment) |
222
230
  | `status` | Display migration status per file |
223
231
  | `editable` | List currently editable files (tail / leaf) |
@@ -234,6 +242,29 @@ See [docs/expand-contract.md](docs/expand-contract.md) for the complete guide: f
234
242
  | `gate` | Evaluate deployment gate conditions |
235
243
  | `baseline` | Squash applied migrations into `schema.sql` |
236
244
 
245
+ ### LLM-Powered Commands
246
+
247
+ | Command | Description |
248
+ |---------|-------------|
249
+ | `audit [target]` | Semantic migration safety audit via LLM |
250
+ | `propose-expand-contract <file>` | Generate expand/contract migration group proposal |
251
+ | `explain` | Explain command output in human-readable form (accepts JSON or text from `lint`, `check`, `diff`, `deps`, `verify` via stdin) |
252
+
253
+ LLM-powered commands are read-only by default. `audit` and `explain` do not modify migration files or database state. `propose-expand-contract` produces a proposal; generated SQL should be reviewed before being written or applied. LLM commands do not replace deterministic gates — they are an additional semantic review layer on top of `lint`, `check`, `diff`, and `verify`.
254
+
255
+ All LLM commands require `agent-contracts-runtime` (optional peer dependency) and an adapter key, and support `--dry-run` to inspect the prompt without calling the LLM.
256
+
257
+ ```bash
258
+ # Audit a migration for operational risks
259
+ npx migraguard audit db/migrations/20260510__add_user_status.sql --adapter openai
260
+
261
+ # Propose expand/contract decomposition for unsafe DDL
262
+ npx migraguard propose-expand-contract db/migrations/20260510__rename_column.sql --adapter cursor
263
+
264
+ # Explain lint output for a PR comment
265
+ npx migraguard lint --format json | npx migraguard explain --adapter openai
266
+ ```
267
+
237
268
  All commands honor the `dialect` setting. See [Dialect support](#dialect-support) for per-dialect details.
238
269
 
239
270
  See [docs/commands.md](docs/commands.md) for detailed usage, options, and examples.
@@ -259,6 +290,10 @@ jobs:
259
290
  - run: npm ci
260
291
  - run: npx migraguard lint
261
292
  - run: npx migraguard check
293
+ # Optional: LLM semantic audit (requires API key)
294
+ # - run: npx migraguard audit --adapter openai --format json --fail-on error
295
+ # env:
296
+ # OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
262
297
  ```
263
298
 
264
299
  ### Automatic Apply on Merge
@@ -550,6 +585,8 @@ migraguard embeds operational policies into the tool and prevents incidents via
550
585
  | **Parallel releases** | ✅ DAG | ❌ | ❌ | ⚠️ | ❌ |
551
586
  | **Offline CI gate** | ✅ check | ❌ | ✅ atlas.sum | ❌ | ❌ |
552
587
  | **Failure handling** | DB-recorded, explicit resolve | repair overwrites | manual fix | revert scripts | manual fix |
588
+ | **LLM semantic audit** | ✅ audit, propose-expand-contract, explain | ❌ | ❌ | ❌ | ❌ |
589
+ | **Agent-readable contract** | ✅ cli-contract.yaml + DSL | ❌ | ❌ | ❌ | ❌ |
553
590
  | **Execution** | psql / mysql / sqlite3 (plain SQL) | Java / JDBC | Go / DB driver | psql / sqitch | pg (Node.js) |
554
591
 
555
592
  **vs Flyway / Liquibase**: migraguard adds offline CI tamper detection, regression detection, idempotency proof, and apply mutual exclusion. MySQL and SQLite are supported as secondary dialects with full DB runtime commands and 17 generic lint rules. No GUI or rich generic execution engine.
@@ -560,6 +597,70 @@ migraguard embeds operational policies into the tool and prevents incidents via
560
597
 
561
598
  **vs Graphile Migrate**: Graphile optimizes for development speed (current.sql). migraguard preserves iterative development (latest file is freely re-appliable) but adds "squash before release" for production-grade hotfix recovery.
562
599
 
600
+ ## Agent-Native Toolchain
601
+
602
+ migraguard is designed for development workflows where AI agents are first-class participants in implementation, validation, and operations.
603
+
604
+ In traditional developer tooling, toolchains perform deterministic operations such as parsing, linting, validation, and diffing. Semantic judgment is left to humans or to an external agent runtime. This does not scale well because the outer agent must learn and maintain every domain-specific rule about PostgreSQL lock behavior, expand/contract safety, backfill batching, and deployment ordering.
605
+
606
+ migraguard takes a different approach: it encapsulates domain-specific semantic reasoning inside the toolchain itself. In addition to deterministic checks, the toolchain runs LLM-based semantic audits, expand/contract proposals, and command output explanations — returning structured results that humans, CI systems, and AI agents consume in the same way.
607
+
608
+ ### Deterministic checks first
609
+
610
+ Anything that can be validated mechanically is validated deterministically: AST-based lint rules (38 for PostgreSQL), checksum-based tamper detection, schema drift comparison, idempotency verification on shadow databases, and expand/contract phase enforcement.
611
+
612
+ ### Semantic audit inside the toolchain
613
+
614
+ Domain-specific reasoning that is difficult to express as static rules is handled by LLM-based commands:
615
+
616
+ - **`audit`** — Lock risk assessment under concurrent load, expand/contract necessity, backfill safety (batching, timeouts, resumability), deployment ordering with application releases, validity of `migraguard:allow` directives
617
+ - **`propose-expand-contract`** — Decompose unsafe DDL into phased expand/backfill/switch/contract SQL with deployment gates
618
+ - **`explain`** — Translate machine output into human-readable explanations for PR comments and release decisions
619
+
620
+ ### Structured findings
621
+
622
+ LLM output is not free-form text. Results conform to typed schemas such as `MigrationAuditResult`, `ExpandContractProposal`, and `ExplainResult`. Audit-style results are compatible with the common `AgentAuditResult` / `AgentFinding` shape so that higher-level workflow agents can aggregate findings across toolchains.
623
+
624
+ ### Tool-owned domain knowledge
625
+
626
+ migraguard owns the rules and reasoning for database migration safety. Instead of embedding PostgreSQL-specific knowledge into a top-level agent prompt, domain expertise is encapsulated inside the tool. Higher-level agents only need to invoke the command and interpret the structured output.
627
+
628
+ ### Agent-readable interface
629
+
630
+ Tool capabilities are described in machine-readable form via [cli-contract.yaml](cli-contract.yaml): artifacts read/written, side effects (`database_write`, `database_read`, `file_write`, `network`), risk levels, confirmation requirements, and output schemas.
631
+
632
+ ### LLM Adapter Configuration
633
+
634
+ | Adapter | Default Model | Environment Variable |
635
+ |---------|---------------|---------------------|
636
+ | `cursor` | runtime default | `CURSOR_API_KEY` |
637
+ | `openai` | runtime default | `OPENAI_API_KEY` |
638
+ | `gemini` | runtime default | `GEMINI_API_KEY` |
639
+ | `claude` | runtime default | `ANTHROPIC_API_KEY` |
640
+ | `mock` | — | — |
641
+
642
+ Default models are defined by `agent-contracts-runtime` and may change between releases. Use `--model` to pin a specific model.
643
+
644
+ ```bash
645
+ # Semantic safety audit
646
+ npx migraguard audit --adapter openai --model gpt-4o
647
+
648
+ # Propose expand/contract decomposition
649
+ npx migraguard propose-expand-contract migration.sql --adapter cursor
650
+
651
+ # Explain lint output for a PR comment
652
+ npx migraguard lint --format json | npx migraguard explain --adapter openai
653
+
654
+ # Inspect the prompt without calling the LLM
655
+ npx migraguard audit --dry-run
656
+ ```
657
+
658
+ Install the runtime dependency to enable LLM features:
659
+
660
+ ```bash
661
+ npm install agent-contracts-runtime
662
+ ```
663
+
563
664
  ## FAQ
564
665
 
565
666
  ### What happens if someone adds a comment to an already-applied migration?
@@ -602,6 +703,9 @@ No. `verify` creates a temporary shadow DB, applies migrations twice, then drops
602
703
  | DB state management (SQLite) | [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) (optional peer dep) |
603
704
  | SQL lint / parser (PostgreSQL) | [libpg-query](https://github.com/pganalyze/libpg-query) — 38 built-in rules |
604
705
  | SQL lint / parser (MySQL, SQLite) | [node-sql-parser](https://github.com/taozhi8833998/node-sql-parser) — 17 generic rules |
706
+ | LLM integration | [agent-contracts-runtime](https://www.npmjs.com/package/agent-contracts-runtime) (optional peer dep) |
707
+ | Agent DSL | [agent-contracts](https://www.npmjs.com/package/agent-contracts) — agent/task/workflow definitions |
708
+ | CLI contract | [cli-contracts](https://www.npmjs.com/package/cli-contracts) — machine-readable interface spec |
605
709
  | Package manager | npm |
606
710
 
607
711
  ## Detailed Documentation
@@ -614,3 +718,4 @@ No. `verify` creates a temporary shadow DB, applies migrations twice, then drops
614
718
  - [docs/safe-ddl.md](docs/safe-ddl.md) — Safe DDL patterns for PostgreSQL (lock timeout, CONCURRENTLY, batch backfills)
615
719
  - [docs/expand-contract.md](docs/expand-contract.md) — Expand/contract pattern: phased migrations, state machine, CI/CD deployment gate
616
720
  - [docs/typescript-api.md](docs/typescript-api.md) — TypeScript programmatic API: all commands as typed async functions
721
+ - [dsl/](dsl/) — Agent DSL definitions (agent, tasks, workflows, handoff types, guardrails)