artshelf 0.10.2 → 0.12.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/CHANGELOG.md +57 -0
- package/README.md +5 -0
- package/SPEC.md +169 -3
- package/dist/src/commands/index.js +4 -0
- package/dist/src/commands/put.js +1 -1
- package/dist/src/commands/reconcile.js +48 -0
- package/dist/src/commands/shared.js +17 -0
- package/dist/src/ledger.js +245 -188
- package/dist/src/locks.js +73 -0
- package/dist/src/provenance.js +142 -0
- package/dist/src/reconcile.js +332 -0
- package/dist/src/registry.js +3 -41
- package/dist/src/shared/help-text.js +26 -0
- package/docs/reference.html +26 -2
- package/package.json +1 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { existsSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
// Capture reconcile-safe provenance for an absolute artifact path. The matched root
|
|
4
|
+
// plus the relative path against it is what survives a `shelf` -> `artshelf` or
|
|
5
|
+
// `.shelf` -> `.artshelf` rename: a future reconcile can rebuild the current path
|
|
6
|
+
// from the current root without Artshelf watching the filesystem. This reads the
|
|
7
|
+
// filesystem to classify the node and fingerprint files; it never mutates anything.
|
|
8
|
+
export function computeProvenance(targetPath, context) {
|
|
9
|
+
const absolute = resolve(targetPath);
|
|
10
|
+
const ledgerRoot = resolveLedgerRoot(context.ledgerPath);
|
|
11
|
+
const repoRoot = findRepoRoot(ledgerRoot);
|
|
12
|
+
const node = classifyNode(absolute);
|
|
13
|
+
// Ledger-owned paths are the most specific root, so they win over the repo root:
|
|
14
|
+
// trash/, plans/, and receipts/ all live under the ledger directory.
|
|
15
|
+
if (isWithin(ledgerRoot, absolute)) {
|
|
16
|
+
return reconstructable("ledger", ledgerRoot, absolute, node);
|
|
17
|
+
}
|
|
18
|
+
if (repoRoot && isWithin(repoRoot, absolute)) {
|
|
19
|
+
return reconstructable("repo", repoRoot, absolute, node);
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
root: "external",
|
|
23
|
+
rootPath: null,
|
|
24
|
+
relativePath: null,
|
|
25
|
+
basename: basename(absolute),
|
|
26
|
+
pathKind: node.kind,
|
|
27
|
+
...(node.fingerprint ? { fingerprint: node.fingerprint } : {})
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const ROOT_KINDS = new Set(["repo", "ledger", "external"]);
|
|
31
|
+
const NODE_KINDS = new Set(["file", "directory", "other"]);
|
|
32
|
+
// Validate a provenance value carried on a record. Returns a list of problems
|
|
33
|
+
// (empty means well-formed). This is the line between a legacy row (no provenance
|
|
34
|
+
// field at all, which callers skip) and a malformed one: once provenance is present
|
|
35
|
+
// it must conform to the PathProvenance contract, including the rule that only
|
|
36
|
+
// `external` roots drop the reconstruct data (rootPath/relativePath).
|
|
37
|
+
export function validateProvenance(provenance) {
|
|
38
|
+
if (typeof provenance !== "object" || provenance === null) {
|
|
39
|
+
return ["provenance must be an object"];
|
|
40
|
+
}
|
|
41
|
+
const value = provenance;
|
|
42
|
+
const problems = [];
|
|
43
|
+
if (typeof value.root !== "string" || !ROOT_KINDS.has(value.root)) {
|
|
44
|
+
problems.push(`provenance.root is invalid: ${String(value.root)}`);
|
|
45
|
+
}
|
|
46
|
+
if (typeof value.basename !== "string" || value.basename.length === 0) {
|
|
47
|
+
problems.push("provenance.basename must be a non-empty string");
|
|
48
|
+
}
|
|
49
|
+
if (typeof value.pathKind !== "string" || !NODE_KINDS.has(value.pathKind)) {
|
|
50
|
+
problems.push(`provenance.pathKind is invalid: ${String(value.pathKind)}`);
|
|
51
|
+
}
|
|
52
|
+
if (value.rootPath !== null && typeof value.rootPath !== "string") {
|
|
53
|
+
problems.push("provenance.rootPath must be a string or null");
|
|
54
|
+
}
|
|
55
|
+
if (value.relativePath !== null && typeof value.relativePath !== "string") {
|
|
56
|
+
problems.push("provenance.relativePath must be a string or null");
|
|
57
|
+
}
|
|
58
|
+
// Reconstruct-data consistency: external paths cannot be rebuilt, so they carry
|
|
59
|
+
// null rootPath/relativePath; repo/ledger paths must carry both to be remappable.
|
|
60
|
+
if (value.root === "external") {
|
|
61
|
+
if (value.rootPath !== null || value.relativePath !== null) {
|
|
62
|
+
problems.push("provenance with external root must have null rootPath and relativePath");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (value.root === "repo" || value.root === "ledger") {
|
|
66
|
+
if (typeof value.rootPath !== "string" || typeof value.relativePath !== "string") {
|
|
67
|
+
problems.push(`provenance with ${value.root} root requires rootPath and relativePath`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (value.fingerprint !== undefined) {
|
|
71
|
+
const fingerprint = value.fingerprint;
|
|
72
|
+
if (typeof fingerprint !== "object" || fingerprint === null || typeof fingerprint.byteSize !== "number") {
|
|
73
|
+
problems.push("provenance.fingerprint must have a numeric byteSize");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return problems;
|
|
77
|
+
}
|
|
78
|
+
// The current ledger root: the directory that owns trash/, plans/, and receipts/.
|
|
79
|
+
// Provenance with a `ledger` root stores paths relative to this, so a reconcile can
|
|
80
|
+
// re-root them under the current ledger directory after a `.shelf` -> `.artshelf` move.
|
|
81
|
+
export function resolveLedgerRoot(ledgerPath) {
|
|
82
|
+
return resolve(dirname(ledgerPath));
|
|
83
|
+
}
|
|
84
|
+
// The current repo root for a ledger, using the same resolution as capture time:
|
|
85
|
+
// the enclosing git checkout, or the parent of a dotted ledger directory. Returns
|
|
86
|
+
// null when no repo root can be determined (e.g. a user-global ledger).
|
|
87
|
+
export function resolveRepoRoot(ledgerPath) {
|
|
88
|
+
return findRepoRoot(resolveLedgerRoot(ledgerPath));
|
|
89
|
+
}
|
|
90
|
+
function reconstructable(root, rootPath, absolute, node) {
|
|
91
|
+
return {
|
|
92
|
+
root,
|
|
93
|
+
rootPath,
|
|
94
|
+
relativePath: toPosix(relative(rootPath, absolute)),
|
|
95
|
+
basename: basename(absolute),
|
|
96
|
+
pathKind: node.kind,
|
|
97
|
+
...(node.fingerprint ? { fingerprint: node.fingerprint } : {})
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function findRepoRoot(ledgerRoot) {
|
|
101
|
+
const gitRoot = findGitRoot(ledgerRoot);
|
|
102
|
+
if (gitRoot)
|
|
103
|
+
return gitRoot;
|
|
104
|
+
// No git checkout: a dotted ledger directory (.artshelf / .shelf) sits directly
|
|
105
|
+
// inside its repo/folder, so the parent is the best repo-root candidate.
|
|
106
|
+
if (basename(ledgerRoot).startsWith(".")) {
|
|
107
|
+
const parent = dirname(ledgerRoot);
|
|
108
|
+
return parent === ledgerRoot ? null : parent;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
function findGitRoot(start) {
|
|
113
|
+
let current = resolve(start);
|
|
114
|
+
while (true) {
|
|
115
|
+
if (existsSync(join(current, ".git")))
|
|
116
|
+
return current;
|
|
117
|
+
const parent = dirname(current);
|
|
118
|
+
if (parent === current)
|
|
119
|
+
return null;
|
|
120
|
+
current = parent;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function classifyNode(absolute) {
|
|
124
|
+
try {
|
|
125
|
+
const stats = statSync(absolute);
|
|
126
|
+
if (stats.isFile())
|
|
127
|
+
return { kind: "file", fingerprint: { byteSize: stats.size } };
|
|
128
|
+
if (stats.isDirectory())
|
|
129
|
+
return { kind: "directory" };
|
|
130
|
+
return { kind: "other" };
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return { kind: "other" };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function isWithin(parent, child) {
|
|
137
|
+
const fromParent = relative(parent, child);
|
|
138
|
+
return fromParent === "" || (!fromParent.startsWith("..") && !isAbsolute(fromParent));
|
|
139
|
+
}
|
|
140
|
+
function toPosix(path) {
|
|
141
|
+
return sep === "/" ? path : path.split(sep).join("/");
|
|
142
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, sep } from "node:path";
|
|
4
|
+
import { assertSafeGeneratedId, readLedger, registerArtshelfArtifact, writeLedger } from "./ledger.js";
|
|
5
|
+
import { withPathLock } from "./locks.js";
|
|
6
|
+
import { computeProvenance, resolveLedgerRoot, resolveRepoRoot } from "./provenance.js";
|
|
7
|
+
import { now, toIso } from "./time.js";
|
|
8
|
+
const RECONCILE_CATEGORIES = new Set([
|
|
9
|
+
"remap",
|
|
10
|
+
"resolve-missing",
|
|
11
|
+
"resolve-stale-trash",
|
|
12
|
+
"registry-remap",
|
|
13
|
+
"blocked"
|
|
14
|
+
]);
|
|
15
|
+
// Classify path drift in a ledger into reconcile findings (NGX-437). This is the
|
|
16
|
+
// read-only engine the dry-run/execute workflow builds on: it never mutates the
|
|
17
|
+
// ledger or the filesystem, it only reads records and probes whether recorded paths
|
|
18
|
+
// still exist (and whether a renamed root can reconstruct them via provenance).
|
|
19
|
+
// Findings are returned in ledger order so downstream JSON output is deterministic.
|
|
20
|
+
export function classifyReconcileFindings(ledgerPath) {
|
|
21
|
+
const records = readLedger(ledgerPath);
|
|
22
|
+
const roots = {
|
|
23
|
+
ledgerRoot: resolveLedgerRoot(ledgerPath),
|
|
24
|
+
repoRoot: resolveRepoRoot(ledgerPath)
|
|
25
|
+
};
|
|
26
|
+
const findings = [];
|
|
27
|
+
for (const record of records) {
|
|
28
|
+
const finding = classifyRecord(record, roots);
|
|
29
|
+
if (finding)
|
|
30
|
+
findings.push(finding);
|
|
31
|
+
}
|
|
32
|
+
return findings;
|
|
33
|
+
}
|
|
34
|
+
// Build the reconcile plan without persisting anything (NGX-437 dry-run preview).
|
|
35
|
+
// This is fully read-only: it classifies drift and returns the plan a `--dry-run`
|
|
36
|
+
// would create, but never writes a plan file or touches the ledger. An empty plan
|
|
37
|
+
// (no actionable entries) collapses to the not-created shape so callers can render
|
|
38
|
+
// "nothing to reconcile" the same way cleanup does.
|
|
39
|
+
export function previewReconcilePlan(ledgerPath) {
|
|
40
|
+
const plan = buildReconcilePlan(ledgerPath);
|
|
41
|
+
return plan.entries.length === 0 ? noCreatedReconcilePlan(plan) : plan;
|
|
42
|
+
}
|
|
43
|
+
// Create (or reuse) a reviewed reconcile plan (NGX-437 dry-run). This is the only
|
|
44
|
+
// part of dry-run that writes: it persists the plan JSON and registers it as an
|
|
45
|
+
// artshelf-owned artifact so the plan file is tracked and a later `--execute` can
|
|
46
|
+
// bind to an exact reviewed plan id. When an earlier plan already covers the same
|
|
47
|
+
// findings it is reused verbatim (stable plan id), and when nothing is actionable
|
|
48
|
+
// no plan artifact is created at all, keeping dry-run side-effect-free in that case.
|
|
49
|
+
export function createReconcilePlan(ledgerPath) {
|
|
50
|
+
const plan = buildReconcilePlan(ledgerPath);
|
|
51
|
+
if (plan.entries.length === 0)
|
|
52
|
+
return noCreatedReconcilePlan(plan);
|
|
53
|
+
const existing = matchingExistingReconcilePlan(ledgerPath, plan);
|
|
54
|
+
const reviewed = existing ? { ...plan, planId: existing.planId, planPath: existing.planPath } : plan;
|
|
55
|
+
if (!reviewed.planPath)
|
|
56
|
+
throw new Error("reconcile plan path was not created");
|
|
57
|
+
writeReconcilePlanFile(reviewed.planPath, reviewed);
|
|
58
|
+
registerArtshelfArtifact(ledgerPath, reviewed.planPath, {
|
|
59
|
+
reason: `Artshelf reconcile dry-run plan ${reviewed.planId}`,
|
|
60
|
+
ttl: "14d",
|
|
61
|
+
kind: "run-artifact",
|
|
62
|
+
cleanup: "trash",
|
|
63
|
+
labels: ["artshelf", "reconcile-plan", reviewed.planId]
|
|
64
|
+
});
|
|
65
|
+
return reviewed;
|
|
66
|
+
}
|
|
67
|
+
// Apply a reviewed reconcile plan (NGX-437 `reconcile --execute`). This is the only
|
|
68
|
+
// mutating reconcile entrypoint and it is deliberately conservative:
|
|
69
|
+
// * It refuses up front when the plan id is missing, the plan file is absent, or the
|
|
70
|
+
// plan file's declared id/ledger does not match the scoped request (no fresh plan,
|
|
71
|
+
// no `--all`; the command layer enforces those, this binds to one exact plan id).
|
|
72
|
+
// * Before applying any entry it re-classifies the live ledger and only acts when the
|
|
73
|
+
// current finding still matches the reviewed entry, so a plan executed against a
|
|
74
|
+
// drifted ledger refuses the stale entries instead of mutating the wrong rows.
|
|
75
|
+
// Reconcile is ledger/registry housekeeping only: it rewrites paths and resolves rows
|
|
76
|
+
// and writes a receipt; it never creates or deletes filesystem artifacts.
|
|
77
|
+
export function executeReconcilePlan(ledgerPath, planId) {
|
|
78
|
+
if (!planId)
|
|
79
|
+
throw new Error("reconcile --execute requires --plan-id");
|
|
80
|
+
const planPath = reconcilePlanPath(ledgerPath, planId);
|
|
81
|
+
if (!existsSync(planPath))
|
|
82
|
+
throw new Error(`Reconcile plan not found: ${planId}`);
|
|
83
|
+
const plan = JSON.parse(readFileSync(planPath, "utf8"));
|
|
84
|
+
assertReconcilePlanExecutable(plan, planId, ledgerPath);
|
|
85
|
+
const receiptPath = reconcileReceiptPath(ledgerPath, planId);
|
|
86
|
+
return withPathLock(ledgerPath, () => {
|
|
87
|
+
const records = readLedger(ledgerPath);
|
|
88
|
+
const recordsById = new Map(records.map((record) => [record.id, record]));
|
|
89
|
+
const liveById = new Map(classifyReconcileFindings(ledgerPath).map((finding) => [finding.id, finding]));
|
|
90
|
+
const executedAt = toIso(now());
|
|
91
|
+
const audit = { reconcilePlanId: planId, reconcileReceiptPath: receiptPath, reconciledAt: executedAt };
|
|
92
|
+
const results = [];
|
|
93
|
+
for (const entry of plan.entries) {
|
|
94
|
+
const record = recordsById.get(entry.id);
|
|
95
|
+
const live = liveById.get(entry.id);
|
|
96
|
+
if (!record || !live || !sameReconcileTarget(live, entry)) {
|
|
97
|
+
results.push(skippedResult(entry));
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const applied = applyReconcileEntry(record, entry, audit, ledgerPath);
|
|
101
|
+
recordsById.set(entry.id, applied);
|
|
102
|
+
results.push(appliedResult(entry, applied));
|
|
103
|
+
}
|
|
104
|
+
writeReconcileReceipt(receiptPath, { planId, ledgerPath, executedAt, results });
|
|
105
|
+
writeLedger(ledgerPath, records.map((record) => recordsById.get(record.id) ?? record));
|
|
106
|
+
registerArtshelfArtifact(ledgerPath, receiptPath, {
|
|
107
|
+
reason: `Artshelf reconcile receipt for plan ${planId}`,
|
|
108
|
+
ttl: "30d",
|
|
109
|
+
kind: "run-artifact",
|
|
110
|
+
cleanup: "review",
|
|
111
|
+
labels: ["artshelf", "reconcile-receipt", planId]
|
|
112
|
+
});
|
|
113
|
+
return { planId, receiptPath, executedAt, results };
|
|
114
|
+
}, "Artshelf ledger");
|
|
115
|
+
}
|
|
116
|
+
// Produce the mutated record for one applicable entry. A remap rewrites the path and
|
|
117
|
+
// recomputes provenance against the new location (so the row is reconcile-healthy
|
|
118
|
+
// afterwards) while keeping the row's status; every resolve category archives the row
|
|
119
|
+
// ledger-only as `resolved`. previousPath always preserves the pre-action path.
|
|
120
|
+
function applyReconcileEntry(record, entry, audit, ledgerPath) {
|
|
121
|
+
if (entry.category === "remap" && entry.proposedPath) {
|
|
122
|
+
return {
|
|
123
|
+
...record,
|
|
124
|
+
path: entry.proposedPath,
|
|
125
|
+
provenance: computeProvenance(entry.proposedPath, { ledgerPath }),
|
|
126
|
+
previousPath: entry.currentPath,
|
|
127
|
+
...audit,
|
|
128
|
+
reconcileReason: entry.reason
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
...record,
|
|
133
|
+
status: "resolved",
|
|
134
|
+
resolvedAt: audit.reconciledAt,
|
|
135
|
+
resolutionReason: entry.reason,
|
|
136
|
+
previousPath: entry.currentPath,
|
|
137
|
+
...audit,
|
|
138
|
+
reconcileReason: entry.reason
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function appliedResult(entry, applied) {
|
|
142
|
+
return {
|
|
143
|
+
id: entry.id,
|
|
144
|
+
category: entry.category,
|
|
145
|
+
field: entry.field,
|
|
146
|
+
status: applied.status === "resolved" ? "resolved" : "remapped",
|
|
147
|
+
previousPath: entry.currentPath,
|
|
148
|
+
newPath: entry.category === "remap" ? entry.proposedPath : null,
|
|
149
|
+
reason: entry.reason
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function skippedResult(entry) {
|
|
153
|
+
return {
|
|
154
|
+
id: entry.id,
|
|
155
|
+
category: entry.category,
|
|
156
|
+
field: entry.field,
|
|
157
|
+
status: "skipped",
|
|
158
|
+
previousPath: entry.currentPath,
|
|
159
|
+
newPath: null,
|
|
160
|
+
reason: "live ledger state no longer matches the reviewed plan"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
// Two findings describe the same drift only when every structural field agrees; this
|
|
164
|
+
// is the execute-time safety check that refuses entries whose live state has moved on.
|
|
165
|
+
function sameReconcileTarget(live, entry) {
|
|
166
|
+
return (live.category === entry.category &&
|
|
167
|
+
live.field === entry.field &&
|
|
168
|
+
live.status === entry.status &&
|
|
169
|
+
live.currentPath === entry.currentPath &&
|
|
170
|
+
live.proposedPath === entry.proposedPath);
|
|
171
|
+
}
|
|
172
|
+
// Bind a loaded reconcile plan to the request before any ledger mutation, mirroring
|
|
173
|
+
// cleanup's assertCleanupPlanExecutable: the plan must declare the requested id, belong
|
|
174
|
+
// to the executing ledger, and carry well-formed entries.
|
|
175
|
+
function assertReconcilePlanExecutable(plan, planId, ledgerPath) {
|
|
176
|
+
if (plan.planId !== planId) {
|
|
177
|
+
throw new Error(`Reconcile plan id mismatch: plan file declares ${plan.planId}, requested ${planId}`);
|
|
178
|
+
}
|
|
179
|
+
if (plan.ledgerPath !== ledgerPath) {
|
|
180
|
+
throw new Error(`Reconcile plan ledger mismatch: plan was created for ${plan.ledgerPath}, executing ${ledgerPath}`);
|
|
181
|
+
}
|
|
182
|
+
if (!Array.isArray(plan.entries)) {
|
|
183
|
+
throw new Error(`Reconcile plan entries are malformed: ${planId}`);
|
|
184
|
+
}
|
|
185
|
+
for (const entry of plan.entries) {
|
|
186
|
+
if (!entry || typeof entry.id !== "string" || typeof entry.currentPath !== "string" || !RECONCILE_CATEGORIES.has(entry.category)) {
|
|
187
|
+
throw new Error(`Reconcile plan entries are malformed: ${planId}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function reconcileReceiptPath(ledgerPath, planId) {
|
|
192
|
+
assertSafeGeneratedId(planId, "reconcile plan id");
|
|
193
|
+
return join(dirname(ledgerPath), "reconcile-receipts", `${planId}.json`);
|
|
194
|
+
}
|
|
195
|
+
function writeReconcileReceipt(receiptPath, value) {
|
|
196
|
+
mkdirSync(dirname(receiptPath), { recursive: true });
|
|
197
|
+
writeFileSync(receiptPath, `${JSON.stringify(value, null, 2)}\n`);
|
|
198
|
+
}
|
|
199
|
+
function classifyRecord(record, roots) {
|
|
200
|
+
// A trashed row's original path is expected to be empty (it was moved to trash),
|
|
201
|
+
// so the only path that matters is the trash target.
|
|
202
|
+
if (record.status === "trashed")
|
|
203
|
+
return classifyTrashTarget(record);
|
|
204
|
+
// Live rows are the ones whose recorded artifact path should still exist. This
|
|
205
|
+
// mirrors validateLedger's "recorded path is missing" warning surface.
|
|
206
|
+
if (record.status === "active" || record.status === "review-required") {
|
|
207
|
+
return classifyActivePath(record, roots);
|
|
208
|
+
}
|
|
209
|
+
// resolved / cleanup-refused rows are terminal for reconcile purposes.
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
function classifyActivePath(record, roots) {
|
|
213
|
+
if (!record.path || existsSync(record.path))
|
|
214
|
+
return null;
|
|
215
|
+
const provenance = record.provenance;
|
|
216
|
+
const candidate = reconstructPath(provenance, roots);
|
|
217
|
+
if (provenance && candidate && existsSync(candidate)) {
|
|
218
|
+
if (isSafeMatch(provenance, candidate)) {
|
|
219
|
+
return finding(record, "remap", "path", record.path, candidate, `recorded path is missing; reconstructed at ${candidate}`);
|
|
220
|
+
}
|
|
221
|
+
return finding(record, "blocked", "path", record.path, null, `a candidate exists at ${candidate} but its name or fingerprint does not match the recorded artifact`);
|
|
222
|
+
}
|
|
223
|
+
return finding(record, "resolve-missing", "path", record.path, null, "recorded path is missing and no safe remap target was found");
|
|
224
|
+
}
|
|
225
|
+
function classifyTrashTarget(record) {
|
|
226
|
+
// Missing cleanup metadata on a trashed row is validateLedger's concern, not ours.
|
|
227
|
+
if (!record.targetPath || existsSync(record.targetPath))
|
|
228
|
+
return null;
|
|
229
|
+
return finding(record, "resolve-stale-trash", "targetPath", record.targetPath, null, "trashed target is missing; resolve the ledger row without touching the filesystem");
|
|
230
|
+
}
|
|
231
|
+
// Re-root a provenance-relative path under the current ledger/repo root. Only
|
|
232
|
+
// reconstructable roots (repo/ledger) with a stored relative path can be rebuilt;
|
|
233
|
+
// external paths and legacy rows without provenance return null.
|
|
234
|
+
function reconstructPath(provenance, roots) {
|
|
235
|
+
if (!provenance || provenance.relativePath === null)
|
|
236
|
+
return null;
|
|
237
|
+
if (provenance.root === "repo") {
|
|
238
|
+
return roots.repoRoot ? join(roots.repoRoot, fromPosix(provenance.relativePath)) : null;
|
|
239
|
+
}
|
|
240
|
+
if (provenance.root === "ledger") {
|
|
241
|
+
return join(roots.ledgerRoot, fromPosix(provenance.relativePath));
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
// A reconstructed candidate is only trusted when its basename matches and, for
|
|
246
|
+
// files with a captured fingerprint, its byte size matches too. Directories and
|
|
247
|
+
// fingerprint-less rows fall back to name plus existence as the evidence.
|
|
248
|
+
function isSafeMatch(provenance, candidate) {
|
|
249
|
+
if (basename(candidate) !== provenance.basename)
|
|
250
|
+
return false;
|
|
251
|
+
if (provenance.pathKind === "file" && provenance.fingerprint) {
|
|
252
|
+
try {
|
|
253
|
+
return statSync(candidate).size === provenance.fingerprint.byteSize;
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
function finding(record, category, field, currentPath, proposedPath, reason) {
|
|
262
|
+
return { id: record.id, category, field, status: record.status, currentPath, proposedPath, reason };
|
|
263
|
+
}
|
|
264
|
+
function fromPosix(path) {
|
|
265
|
+
return sep === "/" ? path : path.split("/").join(sep);
|
|
266
|
+
}
|
|
267
|
+
// Split classified findings into a plan: actionable entries (everything a scoped
|
|
268
|
+
// `--execute` may apply) versus blocked findings (surfaced for review only). The
|
|
269
|
+
// plan id/path are computed up front so a dry-run can persist deterministically.
|
|
270
|
+
function buildReconcilePlan(ledgerPath) {
|
|
271
|
+
const generatedAt = now();
|
|
272
|
+
const findings = classifyReconcileFindings(ledgerPath);
|
|
273
|
+
const entries = findings.filter((finding) => finding.category !== "blocked");
|
|
274
|
+
const blocked = findings.filter((finding) => finding.category === "blocked");
|
|
275
|
+
const planId = makeReconcilePlanId(generatedAt);
|
|
276
|
+
return {
|
|
277
|
+
planId,
|
|
278
|
+
generatedAt: toIso(generatedAt),
|
|
279
|
+
ledgerPath,
|
|
280
|
+
entries,
|
|
281
|
+
blocked,
|
|
282
|
+
planPath: reconcilePlanPath(ledgerPath, planId)
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function noCreatedReconcilePlan(plan) {
|
|
286
|
+
return { ...plan, planId: "not-created", planPath: null };
|
|
287
|
+
}
|
|
288
|
+
// Reuse an earlier plan whose actionable entries match this one's, so repeated
|
|
289
|
+
// dry-runs converge on a single stable plan id (mirrors cleanup plan reuse). Only
|
|
290
|
+
// the structural entry fields are fingerprinted; volatile fields (generatedAt) and
|
|
291
|
+
// the review-only blocked list do not affect reuse.
|
|
292
|
+
function matchingExistingReconcilePlan(ledgerPath, plan) {
|
|
293
|
+
const plansDir = join(dirname(ledgerPath), "reconcile-plans");
|
|
294
|
+
if (!existsSync(plansDir))
|
|
295
|
+
return null;
|
|
296
|
+
const filenames = readdirSync(plansDir).filter((name) => name.endsWith(".json")).sort().reverse();
|
|
297
|
+
for (const filename of filenames) {
|
|
298
|
+
const planPath = join(plansDir, filename);
|
|
299
|
+
try {
|
|
300
|
+
const candidate = JSON.parse(readFileSync(planPath, "utf8"));
|
|
301
|
+
if (candidate.ledgerPath !== ledgerPath)
|
|
302
|
+
continue;
|
|
303
|
+
if (reconcilePlanFingerprint(candidate) !== reconcilePlanFingerprint(plan))
|
|
304
|
+
continue;
|
|
305
|
+
return { ...candidate, planPath };
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
function reconcilePlanFingerprint(plan) {
|
|
314
|
+
return JSON.stringify(plan.entries.map((entry) => ({
|
|
315
|
+
id: entry.id,
|
|
316
|
+
category: entry.category,
|
|
317
|
+
field: entry.field,
|
|
318
|
+
currentPath: entry.currentPath,
|
|
319
|
+
proposedPath: entry.proposedPath
|
|
320
|
+
})));
|
|
321
|
+
}
|
|
322
|
+
function writeReconcilePlanFile(planPath, plan) {
|
|
323
|
+
mkdirSync(dirname(planPath), { recursive: true });
|
|
324
|
+
writeFileSync(planPath, `${JSON.stringify(plan, null, 2)}\n`);
|
|
325
|
+
}
|
|
326
|
+
function makeReconcilePlanId(date) {
|
|
327
|
+
return `reconcile_${toIso(date).replace(/[-:]/g, "").replace("T", "_").replace("Z", "")}_${randomBytes(2).toString("hex")}`;
|
|
328
|
+
}
|
|
329
|
+
function reconcilePlanPath(ledgerPath, planId) {
|
|
330
|
+
assertSafeGeneratedId(planId, "reconcile plan id");
|
|
331
|
+
return join(dirname(ledgerPath), "reconcile-plans", `${planId}.json`);
|
|
332
|
+
}
|
package/dist/src/registry.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, renameSync,
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { withPathLock } from "./locks.js";
|
|
4
5
|
import { now, toIso } from "./time.js";
|
|
5
6
|
export function defaultRegistryPath() {
|
|
6
7
|
return process.env.ARTSHELF_REGISTRY ?? process.env.SHELF_REGISTRY ?? join(homedir(), ".artshelf", "ledgers.json");
|
|
@@ -57,46 +58,7 @@ function writeRegistry(registryPath, registry) {
|
|
|
57
58
|
renameSync(tmpPath, registryPath);
|
|
58
59
|
}
|
|
59
60
|
function withRegistryLock(registryPath, fn) {
|
|
60
|
-
|
|
61
|
-
const lockPath = `${registryPath}.lock`;
|
|
62
|
-
const deadline = Date.now() + 5000;
|
|
63
|
-
const staleAfterMs = 30_000;
|
|
64
|
-
while (true) {
|
|
65
|
-
try {
|
|
66
|
-
mkdirSync(lockPath);
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
if (error.code !== "EEXIST")
|
|
71
|
-
throw error;
|
|
72
|
-
if (isStaleLock(lockPath, staleAfterMs)) {
|
|
73
|
-
rmSync(lockPath, { recursive: true, force: true });
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
if (Date.now() > deadline)
|
|
77
|
-
throw new Error(`Timed out waiting for Artshelf ledger registry lock: ${registryPath}`);
|
|
78
|
-
sleep(25);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
try {
|
|
82
|
-
return fn();
|
|
83
|
-
}
|
|
84
|
-
finally {
|
|
85
|
-
rmSync(lockPath, { recursive: true, force: true });
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function sleep(ms) {
|
|
89
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
90
|
-
}
|
|
91
|
-
function isStaleLock(lockPath, staleAfterMs) {
|
|
92
|
-
try {
|
|
93
|
-
return Date.now() - statSync(lockPath).mtimeMs > staleAfterMs;
|
|
94
|
-
}
|
|
95
|
-
catch (error) {
|
|
96
|
-
if (error.code === "ENOENT")
|
|
97
|
-
return false;
|
|
98
|
-
throw error;
|
|
99
|
-
}
|
|
61
|
+
return withPathLock(registryPath, fn, "Artshelf ledger registry");
|
|
100
62
|
}
|
|
101
63
|
function normalizeEntry(entry) {
|
|
102
64
|
if (!entry.name || !entry.path || !entry.scope || !entry.createdAt || !entry.updatedAt) {
|
|
@@ -52,6 +52,7 @@ const COMMAND_GROUPS = [
|
|
|
52
52
|
group: "Clean",
|
|
53
53
|
commands: [
|
|
54
54
|
{ name: "cleanup", summary: "Plan and execute approved cleanups" },
|
|
55
|
+
{ name: "reconcile", summary: "Reconcile drifted ledger paths via approval-gated plans" },
|
|
55
56
|
{ name: "trash", summary: "Inspect and purge Artshelf trash" },
|
|
56
57
|
{ name: "resolve", summary: "Mark a record manually resolved" }
|
|
57
58
|
]
|
|
@@ -107,6 +108,31 @@ Dry-run writes and registers a plan only when executable cleanup entries exist;
|
|
|
107
108
|
Matching dry-runs reuse the existing plan id and refresh its Artshelf-owned plan artifact.
|
|
108
109
|
Execute writes and registers an Artshelf-owned receipt artifact.
|
|
109
110
|
Global --all mode is dry-run only.
|
|
111
|
+
`;
|
|
112
|
+
}
|
|
113
|
+
if (command === "reconcile") {
|
|
114
|
+
return `Usage:
|
|
115
|
+
artshelf reconcile --dry-run [--ledger <path>] [--json]
|
|
116
|
+
artshelf reconcile --dry-run --all [--registry <path>] [--json]
|
|
117
|
+
artshelf reconcile --execute --plan-id <id> --ledger <path> [--json]
|
|
118
|
+
|
|
119
|
+
Reconcile is approval-gated ledger/registry housekeeping, not cleanup: it never
|
|
120
|
+
creates, moves, or deletes files. It rewrites drifted ledger paths and resolves
|
|
121
|
+
rows that can no longer be acted on, always through one reviewed plan id.
|
|
122
|
+
|
|
123
|
+
Dry-run classifies path drift into a reviewed plan:
|
|
124
|
+
remap a safe moved/renamed path is rewritten to its current location
|
|
125
|
+
resolve-missing an active path is gone with no safe target; resolve after review
|
|
126
|
+
resolve-stale-trash a trashed target is gone; resolve the ledger row, files untouched
|
|
127
|
+
blocked ambiguous or unsafe findings surfaced for review, never auto-applied
|
|
128
|
+
|
|
129
|
+
Execute applies one reviewed plan id against one explicit --ledger and refuses
|
|
130
|
+
missing, unknown, or mismatched plan ids and entries whose live ledger state has
|
|
131
|
+
drifted since review. There is no reconcile --execute --all and no fresh-plan-then-execute.
|
|
132
|
+
Dry-run writes and registers a plan only when actionable entries exist; no-op dry-runs report not-created.
|
|
133
|
+
Matching dry-runs reuse the existing plan id and refresh its Artshelf-owned plan artifact.
|
|
134
|
+
Execute writes and registers an Artshelf-owned reconcile receipt artifact.
|
|
135
|
+
Global --all mode is dry-run only.
|
|
110
136
|
`;
|
|
111
137
|
}
|
|
112
138
|
if (command === "trash")
|
package/docs/reference.html
CHANGED
|
@@ -203,6 +203,30 @@ artshelf trash purge --execute --plan-id <id> [--ledger <path>] [--j
|
|
|
203
203
|
<p>Mark a handled, missing, or no-longer-needed record as manually resolved. Updates the ledger only; never moves or deletes files.</p>
|
|
204
204
|
</section>
|
|
205
205
|
|
|
206
|
+
<section class="cmd">
|
|
207
|
+
<div class="cmd-head"><h2>artshelf reconcile</h2><span class="cmd-flag approval">approval-gated</span></div>
|
|
208
|
+
<pre><code><span class="c"># classify path drift into a reviewed plan</span>
|
|
209
|
+
artshelf reconcile --dry-run [--all] [--ledger <path>] [--json]
|
|
210
|
+
|
|
211
|
+
<span class="c"># apply exactly one reviewed plan id for one explicit ledger</span>
|
|
212
|
+
artshelf reconcile --execute --plan-id <id> --ledger <path> [--json]</code></pre>
|
|
213
|
+
<p>
|
|
214
|
+
Approval-gated ledger housekeeping for drifted recorded paths, not cleanup: it never
|
|
215
|
+
creates, moves, or deletes files. <code>--dry-run</code> classifies each drifted record as
|
|
216
|
+
<code>remap</code> (a moved path safely rewritten from provenance), <code>resolve-missing</code>,
|
|
217
|
+
<code>resolve-stale-trash</code>, or <code>blocked</code>, and registers a reviewed plan when
|
|
218
|
+
actionable entries exist. <code>--execute</code> applies one reviewed plan id, refuses
|
|
219
|
+
missing/unknown/mismatched plans and entries whose live state drifted, and stamps the
|
|
220
|
+
reconcile audit trail (<code>previousPath</code>, <code>reconcilePlanId</code>,
|
|
221
|
+
<code>reconciledAt</code>) on every touched row.
|
|
222
|
+
</p>
|
|
223
|
+
<div class="callout" data-kind="boundary">
|
|
224
|
+
<span class="callout-label">Hard boundary</span>
|
|
225
|
+
<p>No file deletion, no auto-execute, and no global execute.
|
|
226
|
+
<code>reconcile --execute --all</code> does not exist, and a fresh plan cannot be executed in one command.</p>
|
|
227
|
+
</div>
|
|
228
|
+
</section>
|
|
229
|
+
|
|
206
230
|
<section>
|
|
207
231
|
<h2>Global flags</h2>
|
|
208
232
|
<p>Only these apply to every command.</p>
|
|
@@ -252,7 +276,7 @@ artshelf trash purge --execute --plan-id <id> [--ledger <path>] [--j
|
|
|
252
276
|
<tr><th>option</th><th>meaning</th></tr>
|
|
253
277
|
<tr><td>--ledger <path></td><td>target an explicit JSONL ledger</td></tr>
|
|
254
278
|
<tr><td>--registry <path></td><td>target an explicit ledger registry</td></tr>
|
|
255
|
-
<tr><td>--all</td><td>read every registered ledger on commands that support discovery (<code>list</code>, <code>find</code>, <code>get</code>, <code>due</code>, <code>validate</code>, <code>review</code>, <code>status</code>, <code>cleanup --dry-run</code>, <code>trash list</code>)</td></tr>
|
|
279
|
+
<tr><td>--all</td><td>read every registered ledger on commands that support discovery (<code>list</code>, <code>find</code>, <code>get</code>, <code>due</code>, <code>validate</code>, <code>review</code>, <code>status</code>, <code>cleanup --dry-run</code>, <code>reconcile --dry-run</code>, <code>trash list</code>)</td></tr>
|
|
256
280
|
</table>
|
|
257
281
|
</section>
|
|
258
282
|
|
|
@@ -292,7 +316,7 @@ artshelf trash purge --execute --plan-id <id> [--ledger <path>] [--j
|
|
|
292
316
|
Inside a git repo, Artshelf defaults to <code>.artshelf/ledger.jsonl</code>. Outside a
|
|
293
317
|
repo it defaults to <code>~/.artshelf/ledger.jsonl</code>. A user-level registry at
|
|
294
318
|
<code>~/.artshelf/ledgers.json</code> is the discovery index for <code>--all</code>
|
|
295
|
-
review, status, cleanup dry-run, and trash-list; project records stay in their own
|
|
319
|
+
review, status, cleanup dry-run, reconcile dry-run, and trash-list; project records stay in their own
|
|
296
320
|
repo-local ledgers. Automatic update checks cache their last npm result at
|
|
297
321
|
<code>~/.artshelf/update-check.json</code> by default, with a long TTL
|
|
298
322
|
for update-available results and a shorter TTL for no-update or failed
|