@bonesofspring/ai-rules 0.2.57 → 0.2.60
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/CHANGELOG.md +50 -0
- package/README.md +41 -3
- package/bin/cli.js +399 -27
- package/bin/lib/engine-scripts.js +3 -0
- package/bin/lib/mcp/catalog.js +97 -0
- package/bin/lib/mcp/clients.js +35 -2
- package/bin/lib/mcp/credentials.js +85 -1
- package/bin/lib/mcp/doctor.js +93 -17
- package/bin/lib/mcp/registry.js +10 -4
- package/bin/lib/seed-ai-local-team.js +8 -5
- package/docs/MCP-CATALOG.md +56 -0
- package/docs/MCP-SETUP.md +38 -6
- package/docs/PRESET-CONTRIBUTION.md +1 -0
- package/mcp/github-proxy.mjs +133 -0
- package/mcp/lib/github.mjs +187 -0
- package/mcp/lib/streamable-http.mjs +185 -0
- package/mcp/lib/yandex.mjs +16 -137
- package/package.json +3 -1
- package/presets/_shared/ai-local/scripts/check-write-scope.mjs +212 -0
- package/presets/_shared/ai-local/scripts/rule-improver.mjs +343 -50
- package/presets/_shared/ai-local/scripts/run.mjs +122 -3
- package/presets/_shared/core/agent-team/agent-team-orchestrator.md +12 -0
- package/presets/_shared/core/review/code-review-mr.md +10 -0
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +143 -1
- package/presets/claude/android-kotlin/commands/update.md +1 -0
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/android-kotlin/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/go/commands/update.md +1 -0
- package/presets/claude/go/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/go/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/ios-swift/commands/update.md +1 -0
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/ios-swift/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/java/commands/update.md +1 -0
- package/presets/claude/java/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/java/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/mcp-ts/commands/update.md +1 -0
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/mcp-ts/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/mol/commands/update.md +1 -0
- package/presets/claude/mol/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/mol/rules/tooling-and-review/code-review-mr.md +10 -0
- package/presets/claude/next/agents/build-verifier.md +2 -0
- package/presets/claude/next/agents/code-reviewer.md +8 -0
- package/presets/claude/next/commands/task-continue.md +3 -1
- package/presets/claude/next/commands/update.md +1 -0
- package/presets/claude/next/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/next/rules/tooling-and-review/code-review-mr.md +10 -0
- package/presets/claude/nuxt/commands/update.md +1 -0
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/nuxt/rules/tooling-and-review/code-review-mr.md +10 -0
- package/presets/claude/php-hexagonal/commands/update.md +1 -0
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/php-hexagonal/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/php-laravel/commands/update.md +1 -0
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/php-laravel/rules/tooling-and-review/code-review.md +10 -0
- package/presets/claude/svelte/commands/update.md +1 -0
- package/presets/claude/svelte/hooks/chain-team-phases.sh +143 -1
- package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +12 -0
- package/presets/claude/svelte/rules/tooling-and-review/code-review-mr.md +10 -0
- package/presets/cursor/android-kotlin/commands/update.md +1 -0
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/android-kotlin/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/go/commands/update.md +1 -0
- package/presets/cursor/go/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/go/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/ios-swift/commands/update.md +1 -0
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/ios-swift/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/java/commands/update.md +1 -0
- package/presets/cursor/java/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/java/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/mcp-ts/commands/update.md +1 -0
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/mcp-ts/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/mol/MCP.md +4 -3
- package/presets/cursor/mol/commands/update.md +1 -0
- package/presets/cursor/mol/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/mol/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/next/MCP.md +4 -3
- package/presets/cursor/next/agents/build-verifier.md +2 -0
- package/presets/cursor/next/agents/code-reviewer.md +8 -0
- package/presets/cursor/next/commands/task-continue.md +3 -1
- package/presets/cursor/next/commands/update.md +1 -0
- package/presets/cursor/next/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/next/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/nuxt/commands/update.md +1 -0
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/nuxt/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/php-hexagonal/commands/update.md +1 -0
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/php-hexagonal/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/php-laravel/commands/update.md +1 -0
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/php-laravel/rules/code-review-mr.mdc +10 -0
- package/presets/cursor/svelte/commands/update.md +1 -0
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +143 -1
- package/presets/cursor/svelte/rules/code-review-mr.mdc +10 -0
- package/presets/mavis/android-kotlin/adapter-map.json +1 -1
- package/presets/mavis/android-kotlin/commands/update.md +1 -0
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/android-kotlin/rules/code-review-mr.md +10 -0
- package/presets/mavis/go/adapter-map.json +1 -1
- package/presets/mavis/go/commands/update.md +1 -0
- package/presets/mavis/go/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/go/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/go/rules/code-review-mr.md +10 -0
- package/presets/mavis/ios-swift/adapter-map.json +1 -1
- package/presets/mavis/ios-swift/commands/update.md +1 -0
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/ios-swift/rules/code-review-mr.md +10 -0
- package/presets/mavis/java/adapter-map.json +1 -1
- package/presets/mavis/java/commands/update.md +1 -0
- package/presets/mavis/java/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/java/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/java/rules/code-review-mr.md +10 -0
- package/presets/mavis/mcp-ts/adapter-map.json +1 -1
- package/presets/mavis/mcp-ts/commands/update.md +1 -0
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/mcp-ts/rules/code-review-mr.md +10 -0
- package/presets/mavis/mol/MCP.md +4 -3
- package/presets/mavis/mol/adapter-map.json +1 -1
- package/presets/mavis/mol/commands/update.md +1 -0
- package/presets/mavis/mol/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/mol/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/mol/rules/code-review-mr.md +10 -0
- package/presets/mavis/next/MCP.md +4 -3
- package/presets/mavis/next/adapter-map.json +1 -1
- package/presets/mavis/next/agents/build-verifier.md +2 -0
- package/presets/mavis/next/agents/code-reviewer.md +8 -0
- package/presets/mavis/next/commands/task-continue.md +3 -1
- package/presets/mavis/next/commands/update.md +1 -0
- package/presets/mavis/next/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/next/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/next/rules/code-review-mr.md +10 -0
- package/presets/mavis/nuxt/adapter-map.json +1 -1
- package/presets/mavis/nuxt/commands/update.md +1 -0
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/nuxt/rules/code-review-mr.md +10 -0
- package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
- package/presets/mavis/php-hexagonal/commands/update.md +1 -0
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/php-hexagonal/rules/code-review-mr.md +10 -0
- package/presets/mavis/php-laravel/adapter-map.json +1 -1
- package/presets/mavis/php-laravel/commands/update.md +1 -0
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/php-laravel/rules/code-review-mr.md +10 -0
- package/presets/mavis/svelte/adapter-map.json +1 -1
- package/presets/mavis/svelte/commands/update.md +1 -0
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +143 -1
- package/presets/mavis/svelte/rules/agent-team-orchestrator.md +12 -0
- package/presets/mavis/svelte/rules/code-review-mr.md +10 -0
- package/presets/opencode/android-kotlin/adapter-map.json +1 -1
- package/presets/opencode/android-kotlin/commands/update.md +1 -0
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/android-kotlin/rules/code-review-mr.md +10 -0
- package/presets/opencode/go/adapter-map.json +1 -1
- package/presets/opencode/go/commands/update.md +1 -0
- package/presets/opencode/go/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/go/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/go/rules/code-review-mr.md +10 -0
- package/presets/opencode/ios-swift/adapter-map.json +1 -1
- package/presets/opencode/ios-swift/commands/update.md +1 -0
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/ios-swift/rules/code-review-mr.md +10 -0
- package/presets/opencode/java/adapter-map.json +1 -1
- package/presets/opencode/java/commands/update.md +1 -0
- package/presets/opencode/java/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/java/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/java/rules/code-review-mr.md +10 -0
- package/presets/opencode/mcp-ts/adapter-map.json +1 -1
- package/presets/opencode/mcp-ts/commands/update.md +1 -0
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/mcp-ts/rules/code-review-mr.md +10 -0
- package/presets/opencode/mol/MCP.md +4 -3
- package/presets/opencode/mol/adapter-map.json +1 -1
- package/presets/opencode/mol/commands/update.md +1 -0
- package/presets/opencode/mol/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/mol/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/mol/rules/code-review-mr.md +10 -0
- package/presets/opencode/next/MCP.md +4 -3
- package/presets/opencode/next/adapter-map.json +1 -1
- package/presets/opencode/next/agents/build-verifier.md +2 -0
- package/presets/opencode/next/agents/code-reviewer.md +8 -0
- package/presets/opencode/next/commands/task-continue.md +3 -1
- package/presets/opencode/next/commands/update.md +1 -0
- package/presets/opencode/next/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/next/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/next/rules/code-review-mr.md +10 -0
- package/presets/opencode/nuxt/adapter-map.json +1 -1
- package/presets/opencode/nuxt/commands/update.md +1 -0
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/nuxt/rules/code-review-mr.md +10 -0
- package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
- package/presets/opencode/php-hexagonal/commands/update.md +1 -0
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/php-hexagonal/rules/code-review-mr.md +10 -0
- package/presets/opencode/php-laravel/adapter-map.json +1 -1
- package/presets/opencode/php-laravel/commands/update.md +1 -0
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/php-laravel/rules/code-review-mr.md +10 -0
- package/presets/opencode/svelte/adapter-map.json +1 -1
- package/presets/opencode/svelte/commands/update.md +1 -0
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +143 -1
- package/presets/opencode/svelte/rules/agent-team-orchestrator.md +12 -0
- package/presets/opencode/svelte/rules/code-review-mr.md +10 -0
- package/presets/zcode/android-kotlin/adapter-map.json +1 -1
- package/presets/zcode/android-kotlin/commands/update.md +1 -0
- package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/android-kotlin/rules/code-review-mr.md +10 -0
- package/presets/zcode/go/adapter-map.json +1 -1
- package/presets/zcode/go/commands/update.md +1 -0
- package/presets/zcode/go/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/go/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/go/rules/code-review-mr.md +10 -0
- package/presets/zcode/ios-swift/adapter-map.json +1 -1
- package/presets/zcode/ios-swift/commands/update.md +1 -0
- package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/ios-swift/rules/code-review-mr.md +10 -0
- package/presets/zcode/java/adapter-map.json +1 -1
- package/presets/zcode/java/commands/update.md +1 -0
- package/presets/zcode/java/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/java/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/java/rules/code-review-mr.md +10 -0
- package/presets/zcode/mcp-ts/adapter-map.json +1 -1
- package/presets/zcode/mcp-ts/commands/update.md +1 -0
- package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/mcp-ts/rules/code-review-mr.md +10 -0
- package/presets/zcode/mol/MCP.md +4 -3
- package/presets/zcode/mol/adapter-map.json +1 -1
- package/presets/zcode/mol/commands/update.md +1 -0
- package/presets/zcode/mol/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/mol/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/mol/rules/code-review-mr.md +10 -0
- package/presets/zcode/next/MCP.md +4 -3
- package/presets/zcode/next/adapter-map.json +1 -1
- package/presets/zcode/next/agents/build-verifier.md +2 -0
- package/presets/zcode/next/agents/code-reviewer.md +8 -0
- package/presets/zcode/next/commands/task-continue.md +3 -1
- package/presets/zcode/next/commands/update.md +1 -0
- package/presets/zcode/next/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/next/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/next/rules/code-review-mr.md +10 -0
- package/presets/zcode/nuxt/adapter-map.json +1 -1
- package/presets/zcode/nuxt/commands/update.md +1 -0
- package/presets/zcode/nuxt/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/nuxt/rules/code-review-mr.md +10 -0
- package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
- package/presets/zcode/php-hexagonal/commands/update.md +1 -0
- package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/php-hexagonal/rules/code-review-mr.md +10 -0
- package/presets/zcode/php-laravel/adapter-map.json +1 -1
- package/presets/zcode/php-laravel/commands/update.md +1 -0
- package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/php-laravel/rules/code-review-mr.md +10 -0
- package/presets/zcode/svelte/adapter-map.json +1 -1
- package/presets/zcode/svelte/commands/update.md +1 -0
- package/presets/zcode/svelte/hooks/chain-team-phases.sh +143 -1
- package/presets/zcode/svelte/rules/agent-team-orchestrator.md +12 -0
- package/presets/zcode/svelte/rules/code-review-mr.md +10 -0
|
@@ -200,6 +200,77 @@ export function resolveLedgerTarget(target, roots) {
|
|
|
200
200
|
return fromRepo;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
+
/**
|
|
204
|
+
* The overlay root a rollback may write inside: the directory holding
|
|
205
|
+
* `rule-changes.jsonl` (normally `.ai.local`) or its nearest ancestor named
|
|
206
|
+
* `.ai.local` when a legacy ledger sits deeper. The repo root is never a
|
|
207
|
+
* rollback write surface.
|
|
208
|
+
*/
|
|
209
|
+
function overlayRootFor(ledgerPath) {
|
|
210
|
+
const ledgerDirectory = path.dirname(path.resolve(ledgerPath));
|
|
211
|
+
let directory = ledgerDirectory;
|
|
212
|
+
for (;;) {
|
|
213
|
+
if (path.basename(directory) === '.ai.local') return directory;
|
|
214
|
+
const parent = path.dirname(directory);
|
|
215
|
+
if (parent === directory) return ledgerDirectory;
|
|
216
|
+
directory = parent;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Realpath of a target that may not exist yet: resolve the deepest existing
|
|
222
|
+
* ancestor, then append the missing suffix. `realpathSync` rejects a missing
|
|
223
|
+
* path, and a rollback target is often a file this entry is about to create.
|
|
224
|
+
*/
|
|
225
|
+
function realpathNearest(target) {
|
|
226
|
+
let existing = path.resolve(target);
|
|
227
|
+
const missing = [];
|
|
228
|
+
for (;;) {
|
|
229
|
+
try {
|
|
230
|
+
return path.join(fs.realpathSync.native(existing), ...missing);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
if (!error || (error.code !== 'ENOENT' && error.code !== 'ENOTDIR')) throw error;
|
|
233
|
+
const parent = path.dirname(existing);
|
|
234
|
+
if (parent === existing) throw error;
|
|
235
|
+
missing.unshift(path.basename(existing));
|
|
236
|
+
existing = parent;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A rollback write must land inside the ledger's `.ai.local` overlay. The
|
|
243
|
+
* lexical check alone lets a directory symlink inside the overlay redirect the
|
|
244
|
+
* write outside it, so every existing component below the root is rejected when
|
|
245
|
+
* it is a symlink, and the real paths must agree as well.
|
|
246
|
+
*/
|
|
247
|
+
function assertInsideOverlay(target, ledgerPath) {
|
|
248
|
+
const root = overlayRootFor(ledgerPath);
|
|
249
|
+
const resolvedTarget = path.resolve(target);
|
|
250
|
+
const relative = path.relative(root, resolvedTarget);
|
|
251
|
+
if (relative === '' || relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
252
|
+
throw new Error(`ledger target escapes the .ai.local overlay: ${target}`);
|
|
253
|
+
}
|
|
254
|
+
let current = root;
|
|
255
|
+
for (const part of relative.split(path.sep)) {
|
|
256
|
+
current = path.join(current, part);
|
|
257
|
+
let stat;
|
|
258
|
+
try {
|
|
259
|
+
stat = fs.lstatSync(current);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
if (error && error.code === 'ENOENT') break;
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
if (stat.isSymbolicLink()) {
|
|
265
|
+
throw new Error(`ledger target traverses a symlink: ${current}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const realRelative = path.relative(realpathNearest(root), realpathNearest(resolvedTarget));
|
|
269
|
+
if (realRelative === '' || realRelative.startsWith('..') || path.isAbsolute(realRelative)) {
|
|
270
|
+
throw new Error(`ledger target escapes the .ai.local overlay: ${target}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
203
274
|
function appendLedgerLine(ledgerPath, entry, roots) {
|
|
204
275
|
const root = typeof roots === 'string' ? roots : roots.repo;
|
|
205
276
|
const normalized = {
|
|
@@ -984,10 +1055,12 @@ function tryUnloadOne({
|
|
|
984
1055
|
prior,
|
|
985
1056
|
signalsPath,
|
|
986
1057
|
conventionsPath,
|
|
987
|
-
ledgerPath,
|
|
988
1058
|
now,
|
|
989
1059
|
canonical,
|
|
990
1060
|
roots,
|
|
1061
|
+
read = readExisting,
|
|
1062
|
+
writeTarget,
|
|
1063
|
+
dryRun = false,
|
|
991
1064
|
}) {
|
|
992
1065
|
const candidates = [...latestConventionsAddedBySignature(prior).entries()]
|
|
993
1066
|
.sort(([a], [b]) => a.localeCompare(b));
|
|
@@ -995,7 +1068,7 @@ function tryUnloadOne({
|
|
|
995
1068
|
const line = addedLine(appliedEntry.before, appliedEntry.after);
|
|
996
1069
|
if (line === null) continue;
|
|
997
1070
|
const target = resolveLedgerTarget(appliedEntry.target, roots);
|
|
998
|
-
const current =
|
|
1071
|
+
const current = read(target);
|
|
999
1072
|
if (current === null || !conventionsFileHasTrimmedLine(current, line)) continue;
|
|
1000
1073
|
const signals = readSignalsForSignature(signalsPath, signature, canonical);
|
|
1001
1074
|
if (recurrenceSlugs({ signals, appliedAt: appliedEntry.at }).length < 2) continue;
|
|
@@ -1011,9 +1084,9 @@ function tryUnloadOne({
|
|
|
1011
1084
|
agent: appliedEntry.agent,
|
|
1012
1085
|
slug: appliedEntry.slug,
|
|
1013
1086
|
};
|
|
1014
|
-
|
|
1087
|
+
writeTarget({ target, content: after, entry });
|
|
1015
1088
|
prior.push(entry);
|
|
1016
|
-
markSignalsRolledBack(signature, { signalsPath, canonical });
|
|
1089
|
+
if (!dryRun) markSignalsRolledBack(signature, { signalsPath, canonical });
|
|
1017
1090
|
return { signature, target: appliedEntry.target };
|
|
1018
1091
|
}
|
|
1019
1092
|
return null;
|
|
@@ -1044,6 +1117,7 @@ export function rollbackInverse(entry, options = {}) {
|
|
|
1044
1117
|
// not overwrite the intent file and lose the pending change.
|
|
1045
1118
|
recoverPendingCommit(ledgerPath, roots);
|
|
1046
1119
|
const target = resolveLedgerTarget(entry.target, roots);
|
|
1120
|
+
assertInsideOverlay(target, ledgerPath);
|
|
1047
1121
|
commitWrite({ ledgerPath, target, content: inverse.after ?? null, entry: inverse, roots });
|
|
1048
1122
|
return inverse;
|
|
1049
1123
|
} finally {
|
|
@@ -1051,7 +1125,155 @@ export function rollbackInverse(entry, options = {}) {
|
|
|
1051
1125
|
}
|
|
1052
1126
|
}
|
|
1053
1127
|
|
|
1054
|
-
// ---
|
|
1128
|
+
// --- Rollback CLI ------------------------------------------------------------
|
|
1129
|
+
|
|
1130
|
+
const ISO_REF_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/;
|
|
1131
|
+
|
|
1132
|
+
function hasLaterRollback(entries, index, entry) {
|
|
1133
|
+
for (let later = index + 1; later < entries.length; later += 1) {
|
|
1134
|
+
const row = entries[later];
|
|
1135
|
+
if (!row || row.action !== 'rollback') continue;
|
|
1136
|
+
if (row.signature === entry.signature && row.target === entry.target) return true;
|
|
1137
|
+
}
|
|
1138
|
+
return false;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
function describeContent(content) {
|
|
1142
|
+
if (content == null) return '(absent)';
|
|
1143
|
+
const lines = String(content).split('\n');
|
|
1144
|
+
const first = lines.find((line) => line.trim() !== '') ?? '';
|
|
1145
|
+
return `${lines.length} line(s): ${first.trim().slice(0, 100)}`;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* Resolve the newest invertible ledger entry. A ref is an exact ISO timestamp,
|
|
1150
|
+
* a target path (repo-relative or absolute), or a signature. A later `rollback`
|
|
1151
|
+
* row closes its entry, so a second rollback of the same row is a refusal.
|
|
1152
|
+
*/
|
|
1153
|
+
export function resolveRollbackEntry({ ref, ledgerPath, roots }) {
|
|
1154
|
+
const wanted = typeof ref === 'string' ? ref.trim() : '';
|
|
1155
|
+
if (!wanted) return { ok: false, reason: 'no rollback reference given' };
|
|
1156
|
+
const entries = parseLedger(readExisting(ledgerPath) ?? '');
|
|
1157
|
+
const byTimestamp = ISO_REF_RE.test(wanted);
|
|
1158
|
+
const wantedTargets = [];
|
|
1159
|
+
if (!byTimestamp && (wanted.includes('/') || /\.md$/.test(wanted))) {
|
|
1160
|
+
// Ledger rows are repo-relative. A nested overlay's project-relative ref
|
|
1161
|
+
// must also resolve, so try the repo root and the project root.
|
|
1162
|
+
for (const root of [roots.repo, roots.project]) {
|
|
1163
|
+
try {
|
|
1164
|
+
const value = toLedgerTarget(path.isAbsolute(wanted) ? wanted : path.resolve(root, wanted), roots.repo);
|
|
1165
|
+
if (!wantedTargets.includes(value)) wantedTargets.push(value);
|
|
1166
|
+
} catch { /* try the other root */ }
|
|
1167
|
+
}
|
|
1168
|
+
if (wantedTargets.length === 0) wantedTargets.push(wanted);
|
|
1169
|
+
}
|
|
1170
|
+
const candidates = [];
|
|
1171
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
1172
|
+
const entry = entries[index];
|
|
1173
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
1174
|
+
if (typeof entry.target !== 'string' || entry.target === '') continue;
|
|
1175
|
+
if (entry.action === 'rollback') continue;
|
|
1176
|
+
if (!('before' in entry) || !('after' in entry)) continue;
|
|
1177
|
+
if (hasLaterRollback(entries, index, entry)) continue;
|
|
1178
|
+
const matches = byTimestamp
|
|
1179
|
+
? entry.at === wanted
|
|
1180
|
+
: wantedTargets.length > 0
|
|
1181
|
+
? wantedTargets.includes(entry.target)
|
|
1182
|
+
: entry.signature === wanted;
|
|
1183
|
+
if (matches) candidates.push({ entry, index });
|
|
1184
|
+
}
|
|
1185
|
+
if (candidates.length === 0) {
|
|
1186
|
+
return { ok: false, reason: `no invertible ledger entry matches "${wanted}"` };
|
|
1187
|
+
}
|
|
1188
|
+
candidates.sort((left, right) => {
|
|
1189
|
+
const delta = (Date.parse(right.entry.at) || 0) - (Date.parse(left.entry.at) || 0);
|
|
1190
|
+
return delta !== 0 ? delta : right.index - left.index;
|
|
1191
|
+
});
|
|
1192
|
+
const newest = candidates[0];
|
|
1193
|
+
const sameInstant = candidates.filter((candidate) => candidate.entry.at === newest.entry.at);
|
|
1194
|
+
if (sameInstant.some((candidate) => candidate.entry.target !== newest.entry.target
|
|
1195
|
+
|| candidate.entry.signature !== newest.entry.signature)) {
|
|
1196
|
+
return {
|
|
1197
|
+
ok: false,
|
|
1198
|
+
reason: `ambiguous: ${sameInstant.length} ledger entries share timestamp ${newest.entry.at}`,
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
return {
|
|
1202
|
+
ok: true,
|
|
1203
|
+
entry: newest.entry,
|
|
1204
|
+
resolvedBy: byTimestamp ? 'timestamp' : wantedTargets.length > 0 ? 'target' : 'signature',
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* Undo one ledger entry through `rollbackInverse`, fail closed.
|
|
1210
|
+
*
|
|
1211
|
+
* Pre-checks the target still holds the entry's `after` state, so a file edited
|
|
1212
|
+
* since the entry is never overwritten, and refuses a target outside the
|
|
1213
|
+
* ledger's `.ai.local` root. The inverse is itself a ledger row
|
|
1214
|
+
* (`action: "rollback"`), which is the safety row this command prints.
|
|
1215
|
+
*/
|
|
1216
|
+
export function runRollback(options = {}) {
|
|
1217
|
+
const ref = options.ref;
|
|
1218
|
+
const baseDir = options.baseDir ?? path.join(process.cwd(), '.ai.local');
|
|
1219
|
+
const ledgerPath = options.ledgerPath ?? path.join(baseDir, 'rule-changes.jsonl');
|
|
1220
|
+
const lockPath = options.lockPath ?? path.join(baseDir, '.rules.lock');
|
|
1221
|
+
const now = options.now ?? (() => new Date().toISOString());
|
|
1222
|
+
const roots = resolveRoots(ledgerPath, options);
|
|
1223
|
+
const refuse = (reason) => ({
|
|
1224
|
+
ok: false,
|
|
1225
|
+
ref,
|
|
1226
|
+
reason,
|
|
1227
|
+
report: `Rollback\nref: ${ref}\nrefused: ${reason}. Nothing was written.\n`,
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
const resolution = resolveRollbackEntry({ ref, ledgerPath, roots });
|
|
1231
|
+
if (!resolution.ok) return refuse(resolution.reason);
|
|
1232
|
+
const entry = resolution.entry;
|
|
1233
|
+
const target = resolveLedgerTarget(entry.target, roots);
|
|
1234
|
+
const current = readExisting(target);
|
|
1235
|
+
const expected = entry.after ?? null;
|
|
1236
|
+
const unchanged = expected === null ? current === null : current === expected;
|
|
1237
|
+
if (!unchanged) {
|
|
1238
|
+
return refuse(`target ${entry.target} changed since the ledger entry`);
|
|
1239
|
+
}
|
|
1240
|
+
try {
|
|
1241
|
+
assertInsideOverlay(target, ledgerPath);
|
|
1242
|
+
} catch (error) {
|
|
1243
|
+
return refuse(error instanceof Error ? error.message : String(error));
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
let inverse;
|
|
1247
|
+
try {
|
|
1248
|
+
inverse = rollbackInverse(entry, {
|
|
1249
|
+
ledgerPath,
|
|
1250
|
+
lockPath,
|
|
1251
|
+
now,
|
|
1252
|
+
projectRoot: roots.project,
|
|
1253
|
+
repoRoot: roots.repo,
|
|
1254
|
+
});
|
|
1255
|
+
} catch (error) {
|
|
1256
|
+
return refuse(`the rollback write failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
const lines = [
|
|
1260
|
+
'Rollback',
|
|
1261
|
+
`ref: ${ref} (resolved by ${resolution.resolvedBy})`,
|
|
1262
|
+
`entry: ${entry.signature} ${entry.action} → ${entry.target} at ${entry.at}`,
|
|
1263
|
+
`before: ${describeContent(entry.before)}`,
|
|
1264
|
+
`after: ${describeContent(entry.after)}`,
|
|
1265
|
+
`inverse: ${inverse.action} → ${inverse.target} at ${inverse.at}`,
|
|
1266
|
+
`safety row: ${JSON.stringify(inverse)}`,
|
|
1267
|
+
];
|
|
1268
|
+
return {
|
|
1269
|
+
ok: true,
|
|
1270
|
+
ref,
|
|
1271
|
+
entry,
|
|
1272
|
+
inverse,
|
|
1273
|
+
report: `${lines.join('\n')}\n`,
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1055
1277
|
|
|
1056
1278
|
function enrichGroups(groups, signalsPath, canonical = (signature) => signature) {
|
|
1057
1279
|
const bySignature = new Map(
|
|
@@ -1161,7 +1383,7 @@ function planConventions(ctx, decision) {
|
|
|
1161
1383
|
const target = ctx.conventionsPath;
|
|
1162
1384
|
const section = KIND_SECTION[decision.kind] ?? 'Patterns';
|
|
1163
1385
|
const entry = decision.conventionsEntry ?? imperativeSentence(decision.kind, decision.phrase);
|
|
1164
|
-
const before =
|
|
1386
|
+
const before = ctx.read(target) ?? '';
|
|
1165
1387
|
if (conventionsEntryExists(before, entry)) {
|
|
1166
1388
|
return { type: 'skipped', reason: 'dedup: identical entry already present', target };
|
|
1167
1389
|
}
|
|
@@ -1236,7 +1458,7 @@ function hasOpenStage(entries, signature) {
|
|
|
1236
1458
|
}
|
|
1237
1459
|
|
|
1238
1460
|
function proposedConventions(ctx, decision) {
|
|
1239
|
-
const before =
|
|
1461
|
+
const before = ctx.read(ctx.conventionsPath);
|
|
1240
1462
|
if (before === null) return { before, after: null };
|
|
1241
1463
|
const section = KIND_SECTION[decision.kind] ?? 'Patterns';
|
|
1242
1464
|
const bullet = decision.conventionsEntry ?? imperativeSentence(decision.kind, decision.phrase);
|
|
@@ -1247,17 +1469,17 @@ function proposedConventions(ctx, decision) {
|
|
|
1247
1469
|
}
|
|
1248
1470
|
}
|
|
1249
1471
|
|
|
1250
|
-
function appendStage({ prior,
|
|
1472
|
+
function appendStage({ prior, appendLedger, decision, action, target, before, after, now, staged, reason }) {
|
|
1251
1473
|
staged.push({ signature: decision.signature, target, reason });
|
|
1252
1474
|
if (hasOpenStage(prior, decision.signature)) return;
|
|
1253
1475
|
const entry = ledgerEntry(decision, action, target, before, after, now);
|
|
1254
|
-
|
|
1476
|
+
appendLedger(entry);
|
|
1255
1477
|
prior.push(entry);
|
|
1256
1478
|
}
|
|
1257
1479
|
|
|
1258
|
-
function stageNewRule({ decision, ctx, prior,
|
|
1480
|
+
function stageNewRule({ decision, ctx, prior, now, staged, rulesDir, appendLedger, writeTarget }) {
|
|
1259
1481
|
const rulePath = path.join(rulesDir, `${decision.stem}.md`);
|
|
1260
|
-
const editsExisting =
|
|
1482
|
+
const editsExisting = ctx.read(rulePath) !== null;
|
|
1261
1483
|
const band = classifyBand({
|
|
1262
1484
|
denied: false,
|
|
1263
1485
|
compliance: Boolean(decision.compliance),
|
|
@@ -1269,21 +1491,20 @@ function stageNewRule({ decision, ctx, prior, ledgerPath, now, staged, rulesDir,
|
|
|
1269
1491
|
if (editsExisting) {
|
|
1270
1492
|
appendStage({
|
|
1271
1493
|
prior,
|
|
1272
|
-
|
|
1494
|
+
appendLedger,
|
|
1273
1495
|
decision,
|
|
1274
1496
|
action: 'staged',
|
|
1275
1497
|
target: rulePath,
|
|
1276
|
-
before:
|
|
1498
|
+
before: ctx.read(rulePath),
|
|
1277
1499
|
after: decision.ruleText,
|
|
1278
1500
|
now,
|
|
1279
1501
|
staged,
|
|
1280
1502
|
reason: band.reason,
|
|
1281
|
-
roots,
|
|
1282
1503
|
});
|
|
1283
1504
|
return;
|
|
1284
1505
|
}
|
|
1285
1506
|
const seed = planSeed(ctx, decision);
|
|
1286
|
-
const existing =
|
|
1507
|
+
const existing = ctx.read(seed.target);
|
|
1287
1508
|
if (existing !== null) {
|
|
1288
1509
|
// The reviewed snapshot is the seed body without its provenance line, so
|
|
1289
1510
|
// the ledger row and the file never disagree about what was reviewed.
|
|
@@ -1293,7 +1514,7 @@ function stageNewRule({ decision, ctx, prior, ledgerPath, now, staged, rulesDir,
|
|
|
1293
1514
|
const reviewed = stripProvenanceFooter(existing);
|
|
1294
1515
|
appendStage({
|
|
1295
1516
|
prior,
|
|
1296
|
-
|
|
1517
|
+
appendLedger,
|
|
1297
1518
|
decision,
|
|
1298
1519
|
action: 'seed-staged',
|
|
1299
1520
|
target: seed.target,
|
|
@@ -1302,7 +1523,6 @@ function stageNewRule({ decision, ctx, prior, ledgerPath, now, staged, rulesDir,
|
|
|
1302
1523
|
now,
|
|
1303
1524
|
staged,
|
|
1304
1525
|
reason: 'seed already staged',
|
|
1305
|
-
roots,
|
|
1306
1526
|
});
|
|
1307
1527
|
return;
|
|
1308
1528
|
}
|
|
@@ -1316,7 +1536,7 @@ function stageNewRule({ decision, ctx, prior, ledgerPath, now, staged, rulesDir,
|
|
|
1316
1536
|
sourceSlug: sourceSlugFor(decision),
|
|
1317
1537
|
});
|
|
1318
1538
|
const entry = ledgerEntry(decision, 'seed-staged', seed.target, null, seed.content, now);
|
|
1319
|
-
|
|
1539
|
+
writeTarget({ target: seed.target, content: seedFile, entry });
|
|
1320
1540
|
prior.push(entry);
|
|
1321
1541
|
staged.push({ signature: decision.signature, target: seed.target, reason });
|
|
1322
1542
|
}
|
|
@@ -1406,13 +1626,55 @@ function applyDecisionsUnderLock({
|
|
|
1406
1626
|
canonical,
|
|
1407
1627
|
rootDir,
|
|
1408
1628
|
roots,
|
|
1629
|
+
dryRun = false,
|
|
1630
|
+
writes = [],
|
|
1409
1631
|
}) {
|
|
1410
|
-
|
|
1632
|
+
// A dry run takes no lock and creates no `.lock`/`.intent` file: it must be
|
|
1633
|
+
// byte-free on disk. The decision path below is the one the real run uses.
|
|
1634
|
+
const token = dryRun ? null : acquireLock(lockPath);
|
|
1635
|
+
const virtual = new Map();
|
|
1636
|
+
const read = dryRun
|
|
1637
|
+
? (target) => (virtual.has(target) ? virtual.get(target) : readExisting(target))
|
|
1638
|
+
: readExisting;
|
|
1639
|
+
const recordLedger = (entry) => {
|
|
1640
|
+
if (!dryRun) {
|
|
1641
|
+
appendLedgerLine(ledgerPath, entry, roots);
|
|
1642
|
+
return;
|
|
1643
|
+
}
|
|
1644
|
+
let target = entry.target;
|
|
1645
|
+
if (typeof target === 'string') {
|
|
1646
|
+
try { target = toLedgerTarget(target, roots.repo); } catch { /* keep the raw target in the preview */ }
|
|
1647
|
+
}
|
|
1648
|
+
writes.push({ kind: 'ledger', entry: { ...entry, target } });
|
|
1649
|
+
};
|
|
1650
|
+
const writeTarget = ({ target, content, entry }) => {
|
|
1651
|
+
if (!dryRun) {
|
|
1652
|
+
commitWrite({ ledgerPath, target, content, entry, roots });
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
writes.push({ kind: 'file', target, before: read(target), after: content ?? null, entry });
|
|
1656
|
+
recordLedger(entry);
|
|
1657
|
+
virtual.set(target, content ?? null);
|
|
1658
|
+
};
|
|
1659
|
+
const appendLedger = (entry) => recordLedger(entry);
|
|
1411
1660
|
try {
|
|
1412
|
-
const recovery = recoverPendingCommit(ledgerPath, roots);
|
|
1661
|
+
const recovery = dryRun ? null : recoverPendingCommit(ledgerPath, roots);
|
|
1413
1662
|
let envelope = countRulesAndLines({ rulesDir, conventionsPath });
|
|
1414
1663
|
const prior = parseLedger(readExisting(ledgerPath) ?? '');
|
|
1415
|
-
const ctx = { rulesDir, libraryDir, conventionsPath, envelope };
|
|
1664
|
+
const ctx = { rulesDir, libraryDir, conventionsPath, envelope, read };
|
|
1665
|
+
const refreshEnvelope = () => {
|
|
1666
|
+
if (!dryRun) {
|
|
1667
|
+
envelope = countRulesAndLines({ rulesDir, conventionsPath });
|
|
1668
|
+
ctx.envelope = envelope;
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
// Seeds live in `rules-library/`, so only the machine store moves here.
|
|
1672
|
+
envelope = {
|
|
1673
|
+
...envelope,
|
|
1674
|
+
conventions: { lineCount: countNonEmptyLines(read(conventionsPath) ?? '') },
|
|
1675
|
+
};
|
|
1676
|
+
ctx.envelope = envelope;
|
|
1677
|
+
};
|
|
1416
1678
|
const applied = [];
|
|
1417
1679
|
const staged = [];
|
|
1418
1680
|
const skipped = [];
|
|
@@ -1443,7 +1705,7 @@ function applyDecisionsUnderLock({
|
|
|
1443
1705
|
slugs: Array.isArray(item.decision.slugs) ? [...item.decision.slugs] : [],
|
|
1444
1706
|
reason: item.reason,
|
|
1445
1707
|
};
|
|
1446
|
-
|
|
1708
|
+
appendLedger(entry);
|
|
1447
1709
|
prior.push(entry);
|
|
1448
1710
|
staged.push({
|
|
1449
1711
|
signature: item.decision.signature,
|
|
@@ -1456,24 +1718,24 @@ function applyDecisionsUnderLock({
|
|
|
1456
1718
|
prior,
|
|
1457
1719
|
signalsPath,
|
|
1458
1720
|
conventionsPath,
|
|
1459
|
-
ledgerPath,
|
|
1460
1721
|
now,
|
|
1461
1722
|
canonical,
|
|
1462
1723
|
roots,
|
|
1724
|
+
read,
|
|
1725
|
+
writeTarget,
|
|
1726
|
+
dryRun,
|
|
1463
1727
|
});
|
|
1464
1728
|
let decisionsToApply = decisions;
|
|
1465
1729
|
if (unloadedOne) {
|
|
1466
1730
|
unloaded.push(unloadedOne);
|
|
1467
|
-
|
|
1468
|
-
ctx.envelope = envelope;
|
|
1731
|
+
refreshEnvelope();
|
|
1469
1732
|
decisionsToApply = decisions.filter((item) => item.signature !== unloadedOne.signature);
|
|
1470
1733
|
}
|
|
1471
1734
|
|
|
1472
1735
|
for (const decision of decisionsToApply) {
|
|
1473
1736
|
if (decision.tier.action === 'create') {
|
|
1474
|
-
stageNewRule({ decision, ctx, prior,
|
|
1475
|
-
|
|
1476
|
-
ctx.envelope = envelope;
|
|
1737
|
+
stageNewRule({ decision, ctx, prior, now, staged, rulesDir, appendLedger, writeTarget });
|
|
1738
|
+
refreshEnvelope();
|
|
1477
1739
|
continue;
|
|
1478
1740
|
}
|
|
1479
1741
|
if (decision.tier.action !== 'edit') continue;
|
|
@@ -1490,7 +1752,7 @@ function applyDecisionsUnderLock({
|
|
|
1490
1752
|
const proposed = proposedConventions(ctx, decision);
|
|
1491
1753
|
appendStage({
|
|
1492
1754
|
prior,
|
|
1493
|
-
|
|
1755
|
+
appendLedger,
|
|
1494
1756
|
decision,
|
|
1495
1757
|
action: 'staged',
|
|
1496
1758
|
target: conventionsPath,
|
|
@@ -1499,7 +1761,6 @@ function applyDecisionsUnderLock({
|
|
|
1499
1761
|
now,
|
|
1500
1762
|
staged,
|
|
1501
1763
|
reason: decision.compliance ?? band.reason,
|
|
1502
|
-
roots,
|
|
1503
1764
|
});
|
|
1504
1765
|
continue;
|
|
1505
1766
|
}
|
|
@@ -1513,7 +1774,7 @@ function applyDecisionsUnderLock({
|
|
|
1513
1774
|
const proposed = proposedConventions(ctx, decision);
|
|
1514
1775
|
appendStage({
|
|
1515
1776
|
prior,
|
|
1516
|
-
|
|
1777
|
+
appendLedger,
|
|
1517
1778
|
decision,
|
|
1518
1779
|
action: 'staged',
|
|
1519
1780
|
target: plan.target,
|
|
@@ -1522,7 +1783,6 @@ function applyDecisionsUnderLock({
|
|
|
1522
1783
|
now,
|
|
1523
1784
|
staged,
|
|
1524
1785
|
reason: plan.reason,
|
|
1525
|
-
roots,
|
|
1526
1786
|
});
|
|
1527
1787
|
continue;
|
|
1528
1788
|
}
|
|
@@ -1535,7 +1795,7 @@ function applyDecisionsUnderLock({
|
|
|
1535
1795
|
const proposed = proposedConventions(ctx, decision);
|
|
1536
1796
|
appendStage({
|
|
1537
1797
|
prior,
|
|
1538
|
-
|
|
1798
|
+
appendLedger,
|
|
1539
1799
|
decision,
|
|
1540
1800
|
action: 'staged',
|
|
1541
1801
|
target: plan.target,
|
|
@@ -1546,36 +1806,36 @@ function applyDecisionsUnderLock({
|
|
|
1546
1806
|
reason: decisionSlugs.length === 0
|
|
1547
1807
|
? 'rate limit: no contributing slug to spend'
|
|
1548
1808
|
: 'rate limit: ≤1 applied rule per task',
|
|
1549
|
-
roots,
|
|
1550
1809
|
});
|
|
1551
1810
|
continue;
|
|
1552
1811
|
}
|
|
1553
1812
|
|
|
1554
|
-
const before =
|
|
1813
|
+
const before = read(plan.target);
|
|
1555
1814
|
const after = addConventionsEntry(before ?? '', plan.section, plan.entry);
|
|
1556
1815
|
const entry = ledgerEntry(decision, plan.action, plan.target, before, after, now);
|
|
1557
|
-
|
|
1816
|
+
writeTarget({ target: plan.target, content: after, entry });
|
|
1558
1817
|
prior.push(entry);
|
|
1559
1818
|
for (const slug of decisionSlugs) appliedSlugs.add(slug);
|
|
1560
1819
|
applied.push({ signature: decision.signature, target: plan.target });
|
|
1561
|
-
|
|
1562
|
-
ctx.envelope = envelope;
|
|
1820
|
+
refreshEnvelope();
|
|
1563
1821
|
}
|
|
1564
1822
|
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1823
|
+
if (!dryRun) {
|
|
1824
|
+
for (const item of applied) {
|
|
1825
|
+
try {
|
|
1826
|
+
markSignalsApplied(item.signature, { signalsPath, canonical });
|
|
1827
|
+
} catch (error) {
|
|
1828
|
+
errors.push({
|
|
1829
|
+
signature: item.signature,
|
|
1830
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1573
1833
|
}
|
|
1574
1834
|
}
|
|
1575
1835
|
|
|
1576
|
-
return { applied, staged, skipped, unloaded, envelope, errors };
|
|
1836
|
+
return { applied, staged, skipped, unloaded, envelope, errors, writes };
|
|
1577
1837
|
} finally {
|
|
1578
|
-
releaseLock(lockPath, token);
|
|
1838
|
+
if (!dryRun) releaseLock(lockPath, token);
|
|
1579
1839
|
}
|
|
1580
1840
|
}
|
|
1581
1841
|
|
|
@@ -1598,6 +1858,7 @@ export function runImprover(options = {}) {
|
|
|
1598
1858
|
const aliasPath = options.aliasPath ?? path.join(baseDir, 'signature-aliases.json');
|
|
1599
1859
|
const catalogPath = options.catalogPath ?? path.join(baseDir, 'signature-catalog.json');
|
|
1600
1860
|
const now = options.now ?? (() => new Date().toISOString());
|
|
1861
|
+
const dryRun = options.dryRun === true;
|
|
1601
1862
|
|
|
1602
1863
|
const errors = [];
|
|
1603
1864
|
const aliasLoad = loadAliasMap(aliasPath);
|
|
@@ -1668,9 +1929,10 @@ export function runImprover(options = {}) {
|
|
|
1668
1929
|
let applySkipped = [];
|
|
1669
1930
|
let unloaded = [];
|
|
1670
1931
|
let applyErrors = [];
|
|
1932
|
+
let writes = [];
|
|
1671
1933
|
let envelope = { rules: { ruleCount: 0, lineCount: 0 }, conventions: { lineCount: 0 } };
|
|
1672
1934
|
try {
|
|
1673
|
-
({ applied, staged, skipped: applySkipped, unloaded, envelope, errors: applyErrors } = applyDecisionsUnderLock({
|
|
1935
|
+
({ applied, staged, skipped: applySkipped, unloaded, envelope, errors: applyErrors, writes } = applyDecisionsUnderLock({
|
|
1674
1936
|
decisions,
|
|
1675
1937
|
visiblePending,
|
|
1676
1938
|
rulesDir,
|
|
@@ -1683,6 +1945,7 @@ export function runImprover(options = {}) {
|
|
|
1683
1945
|
canonical,
|
|
1684
1946
|
rootDir,
|
|
1685
1947
|
roots,
|
|
1948
|
+
dryRun,
|
|
1686
1949
|
}));
|
|
1687
1950
|
errors.push(...applyErrors);
|
|
1688
1951
|
} catch (error) {
|
|
@@ -1701,6 +1964,14 @@ export function runImprover(options = {}) {
|
|
|
1701
1964
|
rejected,
|
|
1702
1965
|
errors,
|
|
1703
1966
|
envelope,
|
|
1967
|
+
dryRun,
|
|
1968
|
+
writes,
|
|
1969
|
+
decisions: decisions.map((decision) => ({
|
|
1970
|
+
signature: decision.signature,
|
|
1971
|
+
tier: decision.tier,
|
|
1972
|
+
target: null,
|
|
1973
|
+
action: decision.tier.action,
|
|
1974
|
+
})),
|
|
1704
1975
|
report: renderReport({
|
|
1705
1976
|
applied,
|
|
1706
1977
|
staged,
|
|
@@ -1715,6 +1986,15 @@ export function runImprover(options = {}) {
|
|
|
1715
1986
|
};
|
|
1716
1987
|
}
|
|
1717
1988
|
|
|
1989
|
+
/**
|
|
1990
|
+
* The read-only twin of `runImprover`: the same decision path, no lock, no
|
|
1991
|
+
* intent, no file, no signal mutation, no ledger row. `writes` is the exact
|
|
1992
|
+
* ordered write plan (`file` and `ledger` operations) the real run would use.
|
|
1993
|
+
*/
|
|
1994
|
+
export function previewImprover(options = {}) {
|
|
1995
|
+
return runImprover({ ...options, dryRun: true });
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1718
1998
|
function renderTarget(target, roots) {
|
|
1719
1999
|
if (target == null) return '(no file)';
|
|
1720
2000
|
if (typeof target !== 'string') return String(target);
|
|
@@ -1898,9 +2178,22 @@ function renderReport({ applied, staged, skipped, rejected, unloaded = [], error
|
|
|
1898
2178
|
|
|
1899
2179
|
// CLI entry: run directly (node rule-improver.mjs) and print the report.
|
|
1900
2180
|
// `--promote <stem>` promotes a staged seed into `.ai.local/rules/`.
|
|
2181
|
+
// `--rollback <ref>` undoes one ledger entry; ref = target path, signature, or
|
|
2182
|
+
// ISO timestamp. Fail-closed: a changed target or an ambiguous ref writes nothing.
|
|
1901
2183
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
2184
|
+
const rollbackIndex = process.argv.indexOf('--rollback');
|
|
1902
2185
|
const promoteIndex = process.argv.indexOf('--promote');
|
|
1903
|
-
if (
|
|
2186
|
+
if (rollbackIndex !== -1) {
|
|
2187
|
+
const ref = process.argv[rollbackIndex + 1];
|
|
2188
|
+
if (!ref || ref.startsWith('-')) {
|
|
2189
|
+
process.stderr.write('Usage: node .ai.local/scripts/rule-improver.mjs --rollback <target|signature|ISO timestamp>\n');
|
|
2190
|
+
process.exitCode = 2;
|
|
2191
|
+
} else {
|
|
2192
|
+
const result = runRollback({ ref });
|
|
2193
|
+
process.stdout.write(result.report);
|
|
2194
|
+
if (!result.ok) process.exitCode = 1;
|
|
2195
|
+
}
|
|
2196
|
+
} else if (promoteIndex !== -1) {
|
|
1904
2197
|
const result = runPromotion({ stem: process.argv[promoteIndex + 1] });
|
|
1905
2198
|
process.stdout.write(result.report);
|
|
1906
2199
|
if (!result.ok) process.exitCode = 1;
|