@okfit/engine 0.15.0 → 0.16.1
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/config/anchor.js +1 -1
- package/config/layer.js +1 -1
- package/errors.js +81 -6
- package/index.d.ts +288 -7
- package/index.js +7 -3
- package/move/references.js +267 -0
- package/move/run.js +212 -0
- package/package.json +18 -19
- package/render/move.js +52 -0
- package/sync/index.js +4 -3
- package/targets/inside.js +15 -0
- package/targets/resolve.js +146 -0
- package/verify/run.js +63 -27
- package/version.js +1 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { TargetResolutionError } from "../errors.js";
|
|
2
|
+
import { relativeInside } from "./inside.js";
|
|
3
|
+
import { ConceptId } from "@okfit/core";
|
|
4
|
+
import { Effect, FileSystem, Option, Path } from "effect";
|
|
5
|
+
import { posix } from "node:path";
|
|
6
|
+
|
|
7
|
+
//#region src/targets/resolve.ts
|
|
8
|
+
const levenshtein = (a, b) => {
|
|
9
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
10
|
+
for (let i = 1; i <= a.length; i++) {
|
|
11
|
+
let previous = row[0] ?? 0;
|
|
12
|
+
row[0] = i;
|
|
13
|
+
for (let j = 1; j <= b.length; j++) {
|
|
14
|
+
const current = row[j] ?? 0;
|
|
15
|
+
row[j] = Math.min(current + 1, (row[j - 1] ?? 0) + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
16
|
+
previous = current;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return row[b.length] ?? 0;
|
|
20
|
+
};
|
|
21
|
+
const suggest = (arg, bundle) => {
|
|
22
|
+
const wanted = Option.getOrUndefined(ConceptId.normalize(arg));
|
|
23
|
+
if (wanted === void 0) return void 0;
|
|
24
|
+
const ids = [...bundle.concepts.keys()].sort();
|
|
25
|
+
const base = posix.basename(wanted);
|
|
26
|
+
const byBase = ids.filter((id) => posix.basename(id) === base);
|
|
27
|
+
if (byBase.length === 1) return byBase[0];
|
|
28
|
+
const limit = Math.max(2, Math.floor(wanted.length / 3));
|
|
29
|
+
let best;
|
|
30
|
+
for (const id of ids) {
|
|
31
|
+
const distance = levenshtein(wanted, id);
|
|
32
|
+
if (distance <= limit && (best === void 0 || distance < best.distance)) best = {
|
|
33
|
+
id,
|
|
34
|
+
distance
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return best?.id;
|
|
38
|
+
};
|
|
39
|
+
const withSuggestion = (arg, bundle) => {
|
|
40
|
+
const suggestion = suggest(arg, bundle);
|
|
41
|
+
return suggestion === void 0 ? {
|
|
42
|
+
arg,
|
|
43
|
+
reason: "not-found"
|
|
44
|
+
} : {
|
|
45
|
+
arg,
|
|
46
|
+
reason: "not-found",
|
|
47
|
+
suggestion
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
/** Bundle-relative posix path of `absolute`, or `undefined` when it lies outside `root`. */
|
|
51
|
+
const inside = (path, root, absolute) => {
|
|
52
|
+
const relative = relativeInside(path, root, absolute);
|
|
53
|
+
return relative === "" ? void 0 : relative;
|
|
54
|
+
};
|
|
55
|
+
const resolve = Effect.fn("okfit/targets/resolve")(function* (args, bundle, cwd, options) {
|
|
56
|
+
const fs = yield* FileSystem.FileSystem;
|
|
57
|
+
const path = yield* Path.Path;
|
|
58
|
+
const files = new Set(bundle.files);
|
|
59
|
+
const acceptFiles = options?.files === true;
|
|
60
|
+
const realOr = (absolute) => fs.realPath(absolute).pipe(Effect.orElseSucceed(() => absolute));
|
|
61
|
+
const root = yield* realOr(bundle.root);
|
|
62
|
+
/** A bundle-relative file path that names a target, or why it does not. */
|
|
63
|
+
const classify = (relative) => {
|
|
64
|
+
if (!files.has(relative)) return void 0;
|
|
65
|
+
if (acceptFiles) return { path: relative };
|
|
66
|
+
if (ConceptId.isReservedFile(relative)) return { failure: "reserved" };
|
|
67
|
+
const id = Option.getOrUndefined(ConceptId.fromPath(relative));
|
|
68
|
+
return id !== void 0 && bundle.concepts.has(id) ? { path: relative } : { failure: "not-a-concept" };
|
|
69
|
+
};
|
|
70
|
+
/** A filesystem candidate (absolute or cwd-relative): only an existing file inside the bundle counts. */
|
|
71
|
+
const onDisk = (absolute) => Effect.gen(function* () {
|
|
72
|
+
if (!(yield* fs.exists(absolute).pipe(Effect.orElseSucceed(() => false)))) return {
|
|
73
|
+
outside: false,
|
|
74
|
+
hit: void 0
|
|
75
|
+
};
|
|
76
|
+
const relative = inside(path, root, yield* realOr(absolute));
|
|
77
|
+
return relative === void 0 ? {
|
|
78
|
+
outside: true,
|
|
79
|
+
hit: void 0
|
|
80
|
+
} : {
|
|
81
|
+
outside: false,
|
|
82
|
+
hit: classify(relative)
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
const resolved = [];
|
|
86
|
+
const failures = [];
|
|
87
|
+
const seen = /* @__PURE__ */ new Set();
|
|
88
|
+
const accept = (arg, relative) => {
|
|
89
|
+
if (seen.has(relative)) return;
|
|
90
|
+
seen.add(relative);
|
|
91
|
+
const id = Option.getOrUndefined(ConceptId.fromPath(relative));
|
|
92
|
+
resolved.push({
|
|
93
|
+
arg,
|
|
94
|
+
path: relative,
|
|
95
|
+
id: id !== void 0 && bundle.concepts.has(id) ? id : void 0
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
for (const arg of args) {
|
|
99
|
+
const fromDisk = yield* onDisk(path.isAbsolute(arg) ? arg : path.resolve(cwd, arg));
|
|
100
|
+
const id = Option.getOrUndefined(ConceptId.normalize(arg.replace(/^\/+/, "")));
|
|
101
|
+
const byId = id === void 0 ? void 0 : classify(ConceptId.toPath(id));
|
|
102
|
+
const diskPath = fromDisk.hit !== void 0 && "path" in fromDisk.hit ? fromDisk.hit.path : void 0;
|
|
103
|
+
const idPath = byId !== void 0 && "path" in byId ? byId.path : void 0;
|
|
104
|
+
if (diskPath !== void 0 && idPath !== void 0 && diskPath !== idPath && !path.isAbsolute(arg)) failures.push({
|
|
105
|
+
arg,
|
|
106
|
+
reason: "ambiguous",
|
|
107
|
+
candidates: [diskPath, idPath]
|
|
108
|
+
});
|
|
109
|
+
else if (diskPath !== void 0) accept(arg, diskPath);
|
|
110
|
+
else if (idPath !== void 0) accept(arg, idPath);
|
|
111
|
+
else if (fromDisk.hit !== void 0 && "failure" in fromDisk.hit) failures.push({
|
|
112
|
+
arg,
|
|
113
|
+
reason: fromDisk.hit.failure
|
|
114
|
+
});
|
|
115
|
+
else if (byId !== void 0 && "failure" in byId) failures.push({
|
|
116
|
+
arg,
|
|
117
|
+
reason: byId.failure
|
|
118
|
+
});
|
|
119
|
+
else failures.push(fromDisk.outside ? {
|
|
120
|
+
arg,
|
|
121
|
+
reason: "outside-bundle"
|
|
122
|
+
} : withSuggestion(arg, bundle));
|
|
123
|
+
}
|
|
124
|
+
if (failures.length > 0) return yield* new TargetResolutionError({ failures });
|
|
125
|
+
return resolved;
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* Concept targets on the CLI (spec section 13): the shared resolver every
|
|
129
|
+
* concept command calls before it reads or writes anything. Resolution order
|
|
130
|
+
* is absolute path, cwd-relative path, then bundle-relative id; a candidate
|
|
131
|
+
* outside the bundle is discarded, never matched. With `files: true` any
|
|
132
|
+
* walked bundle file is accepted, so a reserved or undecodable file can still
|
|
133
|
+
* be validated.
|
|
134
|
+
*
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
var Targets = class {
|
|
138
|
+
constructor() {}
|
|
139
|
+
/** Resolve every argument or fail with every bad one; duplicates collapse to the first spelling. */
|
|
140
|
+
static resolve = resolve;
|
|
141
|
+
/** The nearest concept id to `arg`: a unique basename match first, else the closest by edit distance. */
|
|
142
|
+
static suggest = suggest;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { Targets };
|
package/verify/run.js
CHANGED
|
@@ -202,25 +202,8 @@ const inspectDrift = Effect.fn("okfit/verify/inspectDrift")(function* (options)
|
|
|
202
202
|
...boundAt === void 0 ? {} : { boundAt }
|
|
203
203
|
};
|
|
204
204
|
});
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
* human actor (the same git identity `verify` records) and, when
|
|
208
|
-
* `integrity.verified` is bound to that actor, that slot too, deleting the
|
|
209
|
-
* whole `integrity` block only when nothing else is left in it. Every cut is a
|
|
210
|
-
* byte-precise splice; an unsupported `verified` or `integrity` shape fails
|
|
211
|
-
* closed with the file untouched, and an actor with no entry fails
|
|
212
|
-
* `VerifyNothingToRevokeError`. Another actor's entries and binding are never
|
|
213
|
-
* touched, so they then read as `entry-superseded` or unbound by themselves.
|
|
214
|
-
*
|
|
215
|
-
* @internal
|
|
216
|
-
*/
|
|
217
|
-
const runRevoke = Effect.fn("okfit/verify/runRevoke")(function* (options) {
|
|
218
|
-
const bundle = yield* Bundle.load({ root: options.bundleRoot });
|
|
219
|
-
const { id, concept } = yield* resolveConcept(bundle, options.id);
|
|
220
|
-
const actor = yield* Derivation.verifier({
|
|
221
|
-
cwd: options.projectRoot,
|
|
222
|
-
config: options.config
|
|
223
|
-
});
|
|
205
|
+
const prepareRevoke = Effect.fn("okfit/verify/prepareRevoke")(function* (bundle, rawId, actor) {
|
|
206
|
+
const { id, concept } = yield* resolveConcept(bundle, rawId);
|
|
224
207
|
const fs = yield* FileSystem.FileSystem;
|
|
225
208
|
const absolutePath = (yield* Path.Path).join(bundle.root, concept.path);
|
|
226
209
|
const source = new TextDecoder("utf-8", { ignoreBOM: true }).decode(yield* fs.readFile(absolutePath));
|
|
@@ -254,18 +237,71 @@ const runRevoke = Effect.fn("okfit/verify/runRevoke")(function* (options) {
|
|
|
254
237
|
})), ...unbind._tag === "edit" ? [unbind.edit] : []].toSorted((a, b) => a.offset - b.offset);
|
|
255
238
|
const finalText = bom + MarkdownEdit.applyAll(text, edits);
|
|
256
239
|
const removed = (concept.frontmatter.verified ?? []).filter((entry) => entry.by === actor).map((entry) => Schema.encodeSync(Timestamp)(entry.at));
|
|
257
|
-
if (!options.dryRun) yield* writeVerified(absolutePath, finalText);
|
|
258
240
|
return {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
241
|
+
absolutePath,
|
|
242
|
+
finalText,
|
|
243
|
+
result: {
|
|
244
|
+
id,
|
|
245
|
+
bundleRoot: bundle.root,
|
|
246
|
+
conceptPath: concept.path,
|
|
247
|
+
by: actor,
|
|
248
|
+
removed,
|
|
249
|
+
bindingCleared: unbind._tag === "edit",
|
|
250
|
+
dryRun: false
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
/**
|
|
255
|
+
* `okfit verify --revoke`: removes every `verified` entry by the resolved
|
|
256
|
+
* human actor (the same git identity `verify` records) and, when
|
|
257
|
+
* `integrity.verified` is bound to that actor, that slot too, deleting the
|
|
258
|
+
* whole `integrity` block only when nothing else is left in it. Every cut is a
|
|
259
|
+
* byte-precise splice; an unsupported `verified` or `integrity` shape fails
|
|
260
|
+
* closed with the file untouched, and an actor with no entry fails
|
|
261
|
+
* `VerifyNothingToRevokeError`. Another actor's entries and binding are never
|
|
262
|
+
* touched, so they then read as `entry-superseded` or unbound by themselves.
|
|
263
|
+
*
|
|
264
|
+
* @internal
|
|
265
|
+
*/
|
|
266
|
+
const runRevoke = Effect.fn("okfit/verify/runRevoke")(function* (options) {
|
|
267
|
+
const bundle = yield* Bundle.load({ root: options.bundleRoot });
|
|
268
|
+
const actor = yield* Derivation.verifier({
|
|
269
|
+
cwd: options.projectRoot,
|
|
270
|
+
config: options.config
|
|
271
|
+
});
|
|
272
|
+
const prepared = yield* prepareRevoke(bundle, options.id, actor);
|
|
273
|
+
if (!options.dryRun) yield* writeVerified(prepared.absolutePath, prepared.finalText);
|
|
274
|
+
return {
|
|
275
|
+
...prepared.result,
|
|
265
276
|
dryRun: options.dryRun
|
|
266
277
|
};
|
|
267
278
|
});
|
|
268
279
|
/**
|
|
280
|
+
* {@link runRevoke} for several concepts, all or nothing: every revoke is
|
|
281
|
+
* prepared before the first write, so one concept with nothing to revoke (or an
|
|
282
|
+
* unsupported shape, or an unknown id) fails the call with the tree untouched.
|
|
283
|
+
* Duplicate ids collapse to one. Results come back in the order given.
|
|
284
|
+
*
|
|
285
|
+
* @internal
|
|
286
|
+
*/
|
|
287
|
+
const runRevokeIds = Effect.fn("okfit/verify/runRevokeIds")(function* (options) {
|
|
288
|
+
const bundle = yield* Bundle.load({ root: options.bundleRoot });
|
|
289
|
+
const actor = yield* Derivation.verifier({
|
|
290
|
+
cwd: options.projectRoot,
|
|
291
|
+
config: options.config
|
|
292
|
+
});
|
|
293
|
+
const prepared = /* @__PURE__ */ new Map();
|
|
294
|
+
for (const rawId of options.ids) {
|
|
295
|
+
const entry = yield* prepareRevoke(bundle, rawId, actor);
|
|
296
|
+
if (!prepared.has(entry.result.id)) prepared.set(entry.result.id, entry);
|
|
297
|
+
}
|
|
298
|
+
if (!options.dryRun) for (const entry of prepared.values()) yield* writeVerified(entry.absolutePath, entry.finalText);
|
|
299
|
+
return [...prepared.values()].map((entry) => ({
|
|
300
|
+
...entry.result,
|
|
301
|
+
dryRun: options.dryRun
|
|
302
|
+
}));
|
|
303
|
+
});
|
|
304
|
+
/**
|
|
269
305
|
* Issue #138. Attests every concept selected by `options.types` (or, when
|
|
270
306
|
* empty, every type whose declaration sets `require_verified = true`) that
|
|
271
307
|
* is not a draft, is not deprecated (#143) and whose `verified` entry by the
|
|
@@ -429,4 +465,4 @@ const loadPreviews = Effect.fn("okfit/verify/loadPreviews")(function* (candidate
|
|
|
429
465
|
});
|
|
430
466
|
|
|
431
467
|
//#endregion
|
|
432
|
-
export { inspectDrift, loadPickerCandidates, loadPreviews, loadStaleCandidates, runRevoke, runVerify, runVerifyBatch, runVerifyIds };
|
|
468
|
+
export { inspectDrift, loadPickerCandidates, loadPreviews, loadStaleCandidates, runRevoke, runRevokeIds, runVerify, runVerifyBatch, runVerifyIds };
|