@okfit/core 0.1.0 → 0.3.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/Actor.js +31 -0
- package/AttestedComputation.js +40 -0
- package/Bundle.js +263 -0
- package/Concept.js +44 -0
- package/ConceptId.js +30 -0
- package/Derive.js +120 -0
- package/Diagnostic.js +81 -0
- package/Generated.js +28 -0
- package/Graph.js +158 -0
- package/IndexDocument.js +30 -0
- package/LogDocument.js +28 -0
- package/OkfitConfig.js +471 -0
- package/README.md +13 -2
- package/Source.js +29 -0
- package/Status.js +15 -0
- package/Timestamp.js +18 -0
- package/Validate.js +44 -0
- package/Verification.js +29 -0
- package/index.d.ts +918 -5
- package/index.js +19 -6
- package/internal/conceptDecode.js +122 -0
- package/internal/fence.js +18 -0
- package/internal/frontmatter.js +62 -0
- package/internal/links.js +129 -0
- package/internal/lintRules.js +94 -0
- package/internal/position.js +40 -0
- package/internal/posixPath.js +53 -0
- package/internal/reserved.js +205 -0
- package/internal/templates.js +16 -0
- package/internal/walk.js +45 -0
- package/package.json +10 -3
package/Actor.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
//#region src/Actor.ts
|
|
4
|
+
const ACTOR_RE = /^(?:[^\s/:]+\/[^\s]+|[A-Za-z][A-Za-z0-9_-]*:[^\s]+)$/;
|
|
5
|
+
const isHuman = (actor) => actor.startsWith("human:");
|
|
6
|
+
const form = (actor) => {
|
|
7
|
+
const colon = actor.indexOf(":");
|
|
8
|
+
const slash = actor.indexOf("/");
|
|
9
|
+
if (slash !== -1 && (colon === -1 || slash < colon)) return "producer";
|
|
10
|
+
const prefix = colon === -1 ? "" : actor.slice(0, colon);
|
|
11
|
+
return prefix === "human" ? "human" : prefix === "process" ? "process" : "other";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Branded, pattern-checked actor codec with prefix classification statics.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
const Actor = Object.assign(Schema.String.pipe(Schema.check(Schema.isPattern(ACTOR_RE, { message: "Expected <producer>/<version> or <prefix>:<id> with no whitespace" })), Schema.brand("Actor")).annotate({
|
|
18
|
+
title: "Actor",
|
|
19
|
+
description: "An OKF actor: \"<producer>/<version>\" (e.g. \"okfit/claude-code\"), or \"<prefix>:<id>\" (e.g. \"human:spencer\", \"process:ci\").",
|
|
20
|
+
examples: [
|
|
21
|
+
"okfit/claude-code",
|
|
22
|
+
"human:spencer",
|
|
23
|
+
"process:ci"
|
|
24
|
+
]
|
|
25
|
+
}), {
|
|
26
|
+
isHuman,
|
|
27
|
+
form
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Actor };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
//#region src/AttestedComputation.ts
|
|
4
|
+
/**
|
|
5
|
+
* One declared input of an attested computation.
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
var ComputationParameter = class extends Schema.Class("ComputationParameter")({
|
|
9
|
+
name: Schema.String,
|
|
10
|
+
type: Schema.String,
|
|
11
|
+
required: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(false)))
|
|
12
|
+
}) {};
|
|
13
|
+
/**
|
|
14
|
+
* The skill that runs the computation and the receipt fields it must return.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
var ComputationExecutor = class extends Schema.Class("ComputationExecutor")({
|
|
18
|
+
resource: Schema.String,
|
|
19
|
+
receipt: Schema.Array(Schema.String)
|
|
20
|
+
}) {};
|
|
21
|
+
/**
|
|
22
|
+
* The deterministic attester that checks an executor's receipt.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
var ComputationAttester = class extends Schema.Class("ComputationAttester")({ resource: Schema.String }) {};
|
|
26
|
+
/**
|
|
27
|
+
* The `Attested Computation` family (D-20). `runtime` absent means lint
|
|
28
|
+
* `computation-runtime-missing`; the concept still loads.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
var AttestedComputation = class extends Schema.Class("AttestedComputation")({
|
|
32
|
+
runtime: Schema.optionalKey(Schema.String),
|
|
33
|
+
parameters: Schema.Array(ComputationParameter).pipe(Schema.withDecodingDefaultKey(Effect.succeed([]))),
|
|
34
|
+
computation: Schema.optionalKey(Schema.String),
|
|
35
|
+
executor: Schema.optionalKey(ComputationExecutor),
|
|
36
|
+
attester: Schema.optionalKey(ComputationAttester)
|
|
37
|
+
}) {};
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { AttestedComputation, ComputationAttester, ComputationExecutor, ComputationParameter };
|
package/Bundle.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { Concept } from "./Concept.js";
|
|
2
|
+
import { basename, dirname } from "./internal/posixPath.js";
|
|
3
|
+
import { ConceptId } from "./ConceptId.js";
|
|
4
|
+
import { toFileOffset } from "./internal/position.js";
|
|
5
|
+
import { Diagnostic, DiagnosticRange } from "./Diagnostic.js";
|
|
6
|
+
import { IndexDocument } from "./IndexDocument.js";
|
|
7
|
+
import { detectFence } from "./internal/fence.js";
|
|
8
|
+
import { decodeConcept } from "./internal/frontmatter.js";
|
|
9
|
+
import { LogDocument } from "./LogDocument.js";
|
|
10
|
+
import { OPTIONS, parseIndex, parseLog } from "./internal/reserved.js";
|
|
11
|
+
import { DEFAULT_PRUNE, walk } from "./internal/walk.js";
|
|
12
|
+
import { Effect, FileSystem, Option, Path, Result, Schema } from "effect";
|
|
13
|
+
import { FrontmatterDecodeError, MarkdownDocument, YamlFrontmatter } from "@effected/markdown";
|
|
14
|
+
import { DescendError } from "@effected/walker";
|
|
15
|
+
import { YamlParseError } from "@effected/yaml";
|
|
16
|
+
|
|
17
|
+
//#region src/Bundle.ts
|
|
18
|
+
/**
|
|
19
|
+
* The bundle root does not exist or is not a directory (D-9).
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
var BundleRootNotFoundError = class extends Schema.TaggedError()("BundleRootNotFoundError", { root: Schema.String }) {
|
|
24
|
+
get message() {
|
|
25
|
+
return `bundle root "${this.root}" is not a directory`;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* A directory or file under the root could not be read. Never raised for content (spec 8).
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
var BundleReadError = class extends Schema.TaggedError()("BundleReadError", {
|
|
34
|
+
root: Schema.String,
|
|
35
|
+
/** Bundle-relative posix path; `""` for the root. */
|
|
36
|
+
path: Schema.String,
|
|
37
|
+
/** The `PlatformError`, preserved structurally. */
|
|
38
|
+
cause: Schema.Defect()
|
|
39
|
+
}) {
|
|
40
|
+
get message() {
|
|
41
|
+
return this.path === "" ? `bundle root "${this.root}" could not be read` : `bundle file "${this.path}" under "${this.root}" could not be read`;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The walk descended past `maxDepth` (D-8). A depth cap that silently truncated would
|
|
46
|
+
* silently change membership, which D-9 forbids; the cap fails instead.
|
|
47
|
+
*
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
var BundleDepthExceededError = class extends Schema.TaggedError()("BundleDepthExceededError", {
|
|
51
|
+
root: Schema.String,
|
|
52
|
+
/** Bundle-relative posix path of the directory the walk could not enter. */
|
|
53
|
+
path: Schema.String,
|
|
54
|
+
limit: Schema.Number
|
|
55
|
+
}) {
|
|
56
|
+
get message() {
|
|
57
|
+
return `bundle "${this.root}" descends past ${this.limit} levels at "${this.path}"`;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* One concept file after loading: typed frontmatter, the parsed body, and the
|
|
62
|
+
* `# Computation` section body when present (D-20).
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
var LoadedConcept = class extends Schema.Class("LoadedConcept")({
|
|
67
|
+
id: ConceptId,
|
|
68
|
+
/** Posix bundle-relative path with `.md`. */
|
|
69
|
+
path: Schema.String,
|
|
70
|
+
frontmatter: Concept,
|
|
71
|
+
document: MarkdownDocument,
|
|
72
|
+
computationBody: Schema.Option(Schema.String)
|
|
73
|
+
}) {};
|
|
74
|
+
/**
|
|
75
|
+
* The result of {@link Bundle.load}: every walked file, every directory holding a
|
|
76
|
+
* concept, concepts by id, reserved documents by directory, load-time diagnostics.
|
|
77
|
+
*
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
var LoadedBundle = class extends Schema.Class("LoadedBundle")({
|
|
81
|
+
root: Schema.String,
|
|
82
|
+
files: Schema.Array(Schema.String),
|
|
83
|
+
directories: Schema.Array(Schema.String),
|
|
84
|
+
concepts: Schema.ReadonlyMap(ConceptId, LoadedConcept),
|
|
85
|
+
indexes: Schema.ReadonlyMap(Schema.String, IndexDocument),
|
|
86
|
+
logs: Schema.ReadonlyMap(Schema.String, LogDocument),
|
|
87
|
+
diagnostics: Schema.Array(Diagnostic)
|
|
88
|
+
}) {};
|
|
89
|
+
const diagnostic = (file, code, severity, message, range) => Diagnostic.make(range === void 0 ? {
|
|
90
|
+
file,
|
|
91
|
+
code,
|
|
92
|
+
severity,
|
|
93
|
+
message
|
|
94
|
+
} : {
|
|
95
|
+
file,
|
|
96
|
+
code,
|
|
97
|
+
severity,
|
|
98
|
+
message,
|
|
99
|
+
range
|
|
100
|
+
});
|
|
101
|
+
const blockRange = (text, node) => DiagnosticRange.fromOffset(text, node.position.start.offset, node.position.end.offset - node.position.start.offset);
|
|
102
|
+
/** Group A's `dirname` returns `"."` for a bundle-root file; `LoadedBundle` keys the root `""`. */
|
|
103
|
+
const directoryKey = (relativePath) => {
|
|
104
|
+
const dir = dirname(relativePath);
|
|
105
|
+
return dir === "." ? "" : dir;
|
|
106
|
+
};
|
|
107
|
+
/** Decode a captured YAML block; a failure is one `frontmatter-unparseable` with a file-mapped range (D-14). */
|
|
108
|
+
const readFrontmatter = (file, text, node) => Effect.map(Effect.result(YamlFrontmatter.decode(node)), (result) => {
|
|
109
|
+
if (Result.isSuccess(result)) return {
|
|
110
|
+
raw: Option.some(result.success),
|
|
111
|
+
diagnostics: []
|
|
112
|
+
};
|
|
113
|
+
const error = result.failure;
|
|
114
|
+
const yaml = error instanceof FrontmatterDecodeError && error.cause instanceof YamlParseError ? error.cause.diagnostics[0] : void 0;
|
|
115
|
+
const range = yaml === void 0 ? blockRange(text, node) : DiagnosticRange.fromOffset(text, toFileOffset(text, yaml.offset), yaml.length);
|
|
116
|
+
return {
|
|
117
|
+
raw: Option.none(),
|
|
118
|
+
diagnostics: [diagnostic(file, "frontmatter-unparseable", "error", yaml === void 0 ? error.message : yaml.message, range)]
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
const loadMarkdown = (file, text) => Effect.gen(function* () {
|
|
122
|
+
const reserved = ConceptId.isReservedFile(file);
|
|
123
|
+
const fence = detectFence(text);
|
|
124
|
+
if (fence === "unclosed") return { diagnostics: [diagnostic(file, "frontmatter-unclosed", "error", "frontmatter opens with --- but never closes", DiagnosticRange.fromOffset(text, 0, 3))] };
|
|
125
|
+
if (fence === "absent" && !reserved) return { diagnostics: [diagnostic(file, "frontmatter-missing", "error", "concept files must start with a --- frontmatter block")] };
|
|
126
|
+
const parsed = MarkdownDocument.parseResult(text, OPTIONS);
|
|
127
|
+
if (Result.isFailure(parsed)) {
|
|
128
|
+
const d = parsed.failure.diagnostic;
|
|
129
|
+
return { diagnostics: [diagnostic(file, "frontmatter-unparseable", "error", d.message, DiagnosticRange.fromOffset(text, d.offset, d.length))] };
|
|
130
|
+
}
|
|
131
|
+
const document = parsed.success;
|
|
132
|
+
const dir = directoryKey(file);
|
|
133
|
+
if (reserved && basename(file) === "log.md") {
|
|
134
|
+
const result = parseLog({
|
|
135
|
+
file,
|
|
136
|
+
dir,
|
|
137
|
+
document
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
diagnostics: result.diagnostics,
|
|
141
|
+
log: result.document
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const node = document.frontmatter;
|
|
145
|
+
const decoded = node === void 0 ? {
|
|
146
|
+
raw: Option.none(),
|
|
147
|
+
diagnostics: []
|
|
148
|
+
} : yield* readFrontmatter(file, text, node);
|
|
149
|
+
if (reserved) {
|
|
150
|
+
const result = parseIndex({
|
|
151
|
+
file,
|
|
152
|
+
dir,
|
|
153
|
+
document,
|
|
154
|
+
frontmatter: decoded.raw
|
|
155
|
+
});
|
|
156
|
+
return {
|
|
157
|
+
diagnostics: [...decoded.diagnostics, ...result.diagnostics],
|
|
158
|
+
index: result.document
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (node === void 0 || Option.isNone(decoded.raw)) return { diagnostics: decoded.diagnostics };
|
|
162
|
+
const result = decodeConcept(decoded.raw.value, {
|
|
163
|
+
file,
|
|
164
|
+
text,
|
|
165
|
+
node
|
|
166
|
+
});
|
|
167
|
+
const diagnostics = [...decoded.diagnostics, ...result.diagnostics];
|
|
168
|
+
const id = ConceptId.fromPath(file);
|
|
169
|
+
if (Option.isNone(result.concept) || Option.isNone(id)) return { diagnostics };
|
|
170
|
+
const computation = document.sectionByHeading("Computation", { depth: 1 });
|
|
171
|
+
return {
|
|
172
|
+
diagnostics,
|
|
173
|
+
concept: LoadedConcept.make({
|
|
174
|
+
id: id.value,
|
|
175
|
+
path: file,
|
|
176
|
+
frontmatter: result.concept.value,
|
|
177
|
+
document,
|
|
178
|
+
computationBody: computation === void 0 ? Option.none() : Option.some(computation.body)
|
|
179
|
+
})
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
/**
|
|
183
|
+
* Static facade for loading a bundle (D-6). Only `FileSystem` and `Path` are required
|
|
184
|
+
* (spec 3.2); `Path` is used solely for `path.join(root, relative)` (D-27).
|
|
185
|
+
*
|
|
186
|
+
* @public
|
|
187
|
+
*/
|
|
188
|
+
var Bundle = class {
|
|
189
|
+
constructor() {}
|
|
190
|
+
/**
|
|
191
|
+
* Load the bundle at `options.root` (D-8, D-9, D-11 to D-14, D-21). Content never
|
|
192
|
+
* fails: every bad file becomes a diagnostic and loading continues (spec 8). A walk
|
|
193
|
+
* that descends past `options.maxDepth` fails typed with `BundleDepthExceededError`
|
|
194
|
+
* (W-1) rather than being silently truncated.
|
|
195
|
+
*/
|
|
196
|
+
static load = Effect.fn("Bundle.load")(function* (options) {
|
|
197
|
+
const fs = yield* FileSystem.FileSystem;
|
|
198
|
+
const path = yield* Path.Path;
|
|
199
|
+
const root = options.root;
|
|
200
|
+
if ((yield* fs.stat(root).pipe(Effect.mapError((cause) => cause.reason._tag === "NotFound" ? new BundleRootNotFoundError({ root }) : new BundleReadError({
|
|
201
|
+
root,
|
|
202
|
+
path: "",
|
|
203
|
+
cause
|
|
204
|
+
})))).type !== "Directory") return yield* new BundleRootNotFoundError({ root });
|
|
205
|
+
const maxDepth = options.maxDepth ?? 256;
|
|
206
|
+
const toLoadError = (error) => {
|
|
207
|
+
if (!(error instanceof DescendError)) return new BundleReadError({
|
|
208
|
+
root,
|
|
209
|
+
path: "",
|
|
210
|
+
cause: error
|
|
211
|
+
});
|
|
212
|
+
if (error.reason === "depthExceeded") return new BundleDepthExceededError({
|
|
213
|
+
root,
|
|
214
|
+
path: error.path,
|
|
215
|
+
limit: error.limit ?? maxDepth
|
|
216
|
+
});
|
|
217
|
+
return new BundleReadError({
|
|
218
|
+
root,
|
|
219
|
+
path: error.path,
|
|
220
|
+
cause: error
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
const walked = yield* walk({
|
|
224
|
+
root,
|
|
225
|
+
includeHidden: options.includeHidden ?? false,
|
|
226
|
+
maxDepth,
|
|
227
|
+
prune: new Set(options.prune ?? DEFAULT_PRUNE)
|
|
228
|
+
}).pipe(Effect.mapError(toLoadError));
|
|
229
|
+
const diagnostics = walked.unreadable.map((dir) => diagnostic(dir, "walk-unreadable", "warning", `directory "${dir}" could not be read; its files are not part of the bundle`));
|
|
230
|
+
const concepts = /* @__PURE__ */ new Map();
|
|
231
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
232
|
+
const logs = /* @__PURE__ */ new Map();
|
|
233
|
+
const directories = /* @__PURE__ */ new Set();
|
|
234
|
+
for (const file of walked.files) {
|
|
235
|
+
if (!file.endsWith(".md")) continue;
|
|
236
|
+
const text = yield* fs.readFileString(path.join(root, file)).pipe(Effect.mapError((cause) => new BundleReadError({
|
|
237
|
+
root,
|
|
238
|
+
path: file,
|
|
239
|
+
cause
|
|
240
|
+
})));
|
|
241
|
+
const outcome = yield* loadMarkdown(file, text);
|
|
242
|
+
diagnostics.push(...outcome.diagnostics);
|
|
243
|
+
if (outcome.concept !== void 0) {
|
|
244
|
+
concepts.set(outcome.concept.id, outcome.concept);
|
|
245
|
+
directories.add(directoryKey(file));
|
|
246
|
+
}
|
|
247
|
+
if (outcome.index !== void 0) indexes.set(outcome.index.dir, outcome.index);
|
|
248
|
+
if (outcome.log !== void 0) logs.set(outcome.log.dir, outcome.log);
|
|
249
|
+
}
|
|
250
|
+
return LoadedBundle.make({
|
|
251
|
+
root,
|
|
252
|
+
files: walked.files,
|
|
253
|
+
directories: [...directories].sort(),
|
|
254
|
+
concepts,
|
|
255
|
+
indexes,
|
|
256
|
+
logs,
|
|
257
|
+
diagnostics
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
//#endregion
|
|
263
|
+
export { Bundle, BundleDepthExceededError, BundleReadError, BundleRootNotFoundError, LoadedBundle, LoadedConcept };
|
package/Concept.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AttestedComputation } from "./AttestedComputation.js";
|
|
2
|
+
import { Timestamp } from "./Timestamp.js";
|
|
3
|
+
import { Generated } from "./Generated.js";
|
|
4
|
+
import { Source, UsageWindow } from "./Source.js";
|
|
5
|
+
import { Status } from "./Status.js";
|
|
6
|
+
import { Verification } from "./Verification.js";
|
|
7
|
+
import { Schema } from "effect";
|
|
8
|
+
|
|
9
|
+
//#region src/Concept.ts
|
|
10
|
+
/**
|
|
11
|
+
* The exact `type` value that enables the computation family (D-20).
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
const ATTESTED_COMPUTATION_TYPE = "Attested Computation";
|
|
15
|
+
/**
|
|
16
|
+
* A typed OKF concept: the stage-1 envelope (`type`), every optional family,
|
|
17
|
+
* unknown keys in `extensions`, and the whole decoded mapping in `raw`.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* The strict codec for tooling that builds values. `Bundle.load` never decodes
|
|
21
|
+
* through it whole: `internal/conceptDecode.ts` decodes the envelope, then each
|
|
22
|
+
* family independently, then calls `Concept.make` (D-15). `attested` is a field,
|
|
23
|
+
* not a subclass, so a bad `executor` cannot reject the concept.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
var Concept = class extends Schema.Class("Concept")({
|
|
27
|
+
type: Schema.String.check(Schema.isNonEmpty()),
|
|
28
|
+
title: Schema.optionalKey(Schema.String),
|
|
29
|
+
description: Schema.optionalKey(Schema.String),
|
|
30
|
+
resource: Schema.optionalKey(Schema.String),
|
|
31
|
+
tags: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
32
|
+
sources: Schema.optionalKey(Schema.Array(Source)),
|
|
33
|
+
usage_window: Schema.optionalKey(UsageWindow),
|
|
34
|
+
generated: Schema.optionalKey(Generated),
|
|
35
|
+
verified: Schema.optionalKey(Verification.List),
|
|
36
|
+
status: Schema.optionalKey(Status),
|
|
37
|
+
stale_after: Schema.optionalKey(Timestamp),
|
|
38
|
+
attested: Schema.optionalKey(AttestedComputation),
|
|
39
|
+
extensions: Schema.Record(Schema.String, Schema.Unknown),
|
|
40
|
+
raw: Schema.Record(Schema.String, Schema.Unknown)
|
|
41
|
+
}) {};
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { ATTESTED_COMPUTATION_TYPE, Concept };
|
package/ConceptId.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { basename } from "./internal/posixPath.js";
|
|
2
|
+
import { Option, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
//#region src/ConceptId.ts
|
|
5
|
+
const RESERVED_BASENAMES = /* @__PURE__ */ new Set(["index.md", "log.md"]);
|
|
6
|
+
const isReservedFile = (path) => RESERVED_BASENAMES.has(basename(path));
|
|
7
|
+
const fromPath = (path) => {
|
|
8
|
+
if (isReservedFile(path) || !path.endsWith(".md")) return Option.none();
|
|
9
|
+
return Option.some(path.slice(0, -3));
|
|
10
|
+
};
|
|
11
|
+
const toPath = (id) => `${id}.md`;
|
|
12
|
+
const normalize = (input) => {
|
|
13
|
+
const withoutSlash = input.startsWith("/") ? input.slice(1) : input;
|
|
14
|
+
const collapsed = (withoutSlash.endsWith(".md") ? withoutSlash.slice(0, -3) : withoutSlash).replace(/\/{2,}/g, "/").replace(/\/$/, "");
|
|
15
|
+
return collapsed.length === 0 ? Option.none() : Option.some(collapsed);
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Branded concept id codec: the posix path with `.md` stripped, plus path
|
|
19
|
+
* conversion, a tolerant input normaliser, and the reserved-file test (D-12).
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
const ConceptId = Object.assign(Schema.String.pipe(Schema.brand("ConceptId")), {
|
|
23
|
+
fromPath,
|
|
24
|
+
toPath,
|
|
25
|
+
normalize,
|
|
26
|
+
isReservedFile
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { ConceptId };
|
package/Derive.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Actor } from "./Actor.js";
|
|
2
|
+
import { indexEntry, indexFrontmatter, indexSection, logEntry } from "./internal/templates.js";
|
|
3
|
+
import { DateTime, Schema } from "effect";
|
|
4
|
+
|
|
5
|
+
//#region src/Derive.ts
|
|
6
|
+
/** Trust tier from `verified` (OKF 0.2 §5.3): none maps to unverified; only non-human maps to machine-confirmed; any `human:` maps to human-reviewed. @public */
|
|
7
|
+
const TrustTier = Schema.Literals([
|
|
8
|
+
"unverified",
|
|
9
|
+
"machine-confirmed",
|
|
10
|
+
"human-reviewed"
|
|
11
|
+
]);
|
|
12
|
+
/** Three-way staleness; `unknown` means no `stale_after`. @public */
|
|
13
|
+
const Staleness = Schema.Literals([
|
|
14
|
+
"fresh",
|
|
15
|
+
"stale",
|
|
16
|
+
"unknown"
|
|
17
|
+
]);
|
|
18
|
+
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
19
|
+
const oneLine = (text) => text.replace(/\s+/g, " ").trim();
|
|
20
|
+
const basename = (path) => path.slice(path.lastIndexOf("/") + 1);
|
|
21
|
+
const dirnameOf = (path) => path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";
|
|
22
|
+
const relativeTarget = (dir, path) => dir !== "" && path.startsWith(`${dir}/`) ? path.slice(dir.length + 1) : path;
|
|
23
|
+
/** Pure derivations (D-10, D-36): trust tier, status, staleness against a supplied instant, index/log rendering. @public */
|
|
24
|
+
var Derive = class Derive {
|
|
25
|
+
constructor() {}
|
|
26
|
+
/** Any `human:` verifier means human-reviewed; otherwise machine-confirmed; none means unverified. */
|
|
27
|
+
static trustTier = (concept) => {
|
|
28
|
+
const verified = concept.verified ?? [];
|
|
29
|
+
if (verified.length === 0) return "unverified";
|
|
30
|
+
return verified.some((entry) => Actor.isHuman(entry.by)) ? "human-reviewed" : "machine-confirmed";
|
|
31
|
+
};
|
|
32
|
+
/** Absent `status` reads as `stable` (OKF 0.2 §5.4). */
|
|
33
|
+
static status = (concept) => concept.status ?? "stable";
|
|
34
|
+
/** `stale` when `now >= stale_after` (OKF 0.2 §5.5); `unknown` without `stale_after`. */
|
|
35
|
+
static staleness = (concept, now) => {
|
|
36
|
+
if (concept.stale_after === void 0) return "unknown";
|
|
37
|
+
return DateTime.toEpochMillis(now) >= DateTime.toEpochMillis(concept.stale_after) ? "stale" : "fresh";
|
|
38
|
+
};
|
|
39
|
+
/** Boolean form of {@link Derive.staleness} (D-36). */
|
|
40
|
+
static isStale = (concept, now) => Derive.staleness(concept, now) === "stale";
|
|
41
|
+
/**
|
|
42
|
+
* Every concept in `bundle` that {@link Derive.isStale} reports stale against
|
|
43
|
+
* `now`, sorted by id ascending. Pure (D-10); `now` is always an argument,
|
|
44
|
+
* never read from a clock here (D-36).
|
|
45
|
+
*/
|
|
46
|
+
static staleReport = (bundle, now) => {
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const [id, concept] of bundle.concepts) {
|
|
49
|
+
const staleAfter = concept.frontmatter.stale_after;
|
|
50
|
+
if (staleAfter === void 0) continue;
|
|
51
|
+
if (!Derive.isStale(concept.frontmatter, now)) continue;
|
|
52
|
+
const millisPast = DateTime.toEpochMillis(now) - DateTime.toEpochMillis(staleAfter);
|
|
53
|
+
out.push({
|
|
54
|
+
id,
|
|
55
|
+
staleAfter,
|
|
56
|
+
daysPast: Math.floor(millisPast / 864e5)
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return out.sort((a, b) => compare(a.id, b.id));
|
|
60
|
+
};
|
|
61
|
+
/** Frontmatter `title`, else the file name without `.md` (OKF 0.2 §4.1). */
|
|
62
|
+
static title = (concept) => concept.frontmatter.title ?? basename(concept.path).replace(/\.md$/, "");
|
|
63
|
+
/** One H1 per `type` (sorted), entries sorted by title, then `# Subdirectories` (D-36; spec §8). */
|
|
64
|
+
static renderIndex = (dir, concepts, options = {}) => {
|
|
65
|
+
const groups = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const concept of concepts) {
|
|
67
|
+
const entries = groups.get(concept.frontmatter.type) ?? [];
|
|
68
|
+
const description = concept.frontmatter.description;
|
|
69
|
+
entries.push({
|
|
70
|
+
title: Derive.title(concept),
|
|
71
|
+
target: relativeTarget(dir, concept.path),
|
|
72
|
+
...description === void 0 ? {} : { description: oneLine(description) }
|
|
73
|
+
});
|
|
74
|
+
groups.set(concept.frontmatter.type, entries);
|
|
75
|
+
}
|
|
76
|
+
const sections = [...groups.keys()].sort(compare).map((type) => {
|
|
77
|
+
const entries = (groups.get(type) ?? []).sort((a, b) => compare(a.title, b.title));
|
|
78
|
+
return indexSection(type, entries.map(indexEntry));
|
|
79
|
+
});
|
|
80
|
+
const subdirectories = [...options.subdirectories ?? []].sort(compare);
|
|
81
|
+
if (subdirectories.length > 0) {
|
|
82
|
+
const entries = subdirectories.map((name) => indexEntry({
|
|
83
|
+
title: name,
|
|
84
|
+
target: `${name}/index.md`
|
|
85
|
+
}));
|
|
86
|
+
sections.push(indexSection("Subdirectories", entries));
|
|
87
|
+
}
|
|
88
|
+
return (options.okfVersion === void 0 ? "" : indexFrontmatter(options.okfVersion)) + sections.join("\n");
|
|
89
|
+
};
|
|
90
|
+
/** `## <date>` plus one `* <item>` line per item, newline-terminated (D-36). */
|
|
91
|
+
static renderLogEntry = (entry) => logEntry(entry.date, entry.items);
|
|
92
|
+
/**
|
|
93
|
+
* Assemble a whole log document from its groups: `# <title>\n\n` followed
|
|
94
|
+
* by each group's own {@link Derive.renderLogEntry} rendering, joined by
|
|
95
|
+
* one blank line (S-11). Default title `"Log"`. `init` and `sync`'s log
|
|
96
|
+
* mode both call this instead of hand-assembling the join; `sync`'s
|
|
97
|
+
* `mergeLog` uses it only for brand-new groups it renders, since existing
|
|
98
|
+
* groups are re-emitted verbatim from source.
|
|
99
|
+
*/
|
|
100
|
+
static renderLog = (groups, options) => {
|
|
101
|
+
const title = options?.title ?? "Log";
|
|
102
|
+
if (groups.length === 0) return `# ${title}\n`;
|
|
103
|
+
return `# ${title}\n\n${groups.map(Derive.renderLogEntry).join("\n")}`;
|
|
104
|
+
};
|
|
105
|
+
/** Index text for `dir` from the concepts directly in it plus its immediate child directories; no frontmatter. */
|
|
106
|
+
static synthesizeIndex = (bundle, dir) => {
|
|
107
|
+
const prefix = dir === "" ? "" : `${dir}/`;
|
|
108
|
+
const concepts = [...bundle.concepts.values()].filter((concept) => dirnameOf(concept.path) === dir);
|
|
109
|
+
const children = /* @__PURE__ */ new Set();
|
|
110
|
+
for (const candidate of [...bundle.directories, ...bundle.indexes.keys()]) {
|
|
111
|
+
if (candidate === "" || candidate === dir || !candidate.startsWith(prefix)) continue;
|
|
112
|
+
const head = candidate.slice(prefix.length).split("/")[0];
|
|
113
|
+
if (head !== void 0 && head !== "") children.add(head);
|
|
114
|
+
}
|
|
115
|
+
return Derive.renderIndex(dir, concepts, { subdirectories: [...children] });
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
export { Derive, Staleness, TrustTier };
|
package/Diagnostic.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { lineCharacter } from "./internal/position.js";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
//#region src/Diagnostic.ts
|
|
5
|
+
/** @public */
|
|
6
|
+
const DiagnosticSeverity = Schema.Literals([
|
|
7
|
+
"error",
|
|
8
|
+
"warning",
|
|
9
|
+
"info"
|
|
10
|
+
]);
|
|
11
|
+
/** Always `"error"`, never configurable (D-33). @public */
|
|
12
|
+
const ConformanceCode = Schema.Literals([
|
|
13
|
+
"frontmatter-missing",
|
|
14
|
+
"frontmatter-unclosed",
|
|
15
|
+
"frontmatter-unparseable",
|
|
16
|
+
"type-missing",
|
|
17
|
+
"index-frontmatter",
|
|
18
|
+
"index-malformed",
|
|
19
|
+
"log-heading-invalid",
|
|
20
|
+
"log-malformed"
|
|
21
|
+
]);
|
|
22
|
+
/** Severity from `OkfitConfig` (D-34). @public */
|
|
23
|
+
const LintCode = Schema.Literals([
|
|
24
|
+
"broken-links",
|
|
25
|
+
"missing-index",
|
|
26
|
+
"unknown-type",
|
|
27
|
+
"required-key-missing",
|
|
28
|
+
"field-value-unknown",
|
|
29
|
+
"require-verified-unmet",
|
|
30
|
+
"family-invalid",
|
|
31
|
+
"computation-runtime-missing",
|
|
32
|
+
"footnote-source-unknown",
|
|
33
|
+
"log-frontmatter",
|
|
34
|
+
"actor-prefix-unknown",
|
|
35
|
+
"legacy-timestamp",
|
|
36
|
+
"config-unknown-key",
|
|
37
|
+
"stale",
|
|
38
|
+
"walk-unreadable",
|
|
39
|
+
"generated-at-drift"
|
|
40
|
+
]);
|
|
41
|
+
/** @public */
|
|
42
|
+
const DiagnosticCode = Schema.Union([ConformanceCode, LintCode]);
|
|
43
|
+
/**
|
|
44
|
+
* A range inside a file's text, zero-based (D-32); the CLI renders one-based.
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
var DiagnosticRange = class DiagnosticRange extends Schema.Class("DiagnosticRange")({
|
|
48
|
+
offset: Schema.Number,
|
|
49
|
+
length: Schema.Number,
|
|
50
|
+
line: Schema.Number,
|
|
51
|
+
character: Schema.Number
|
|
52
|
+
}) {
|
|
53
|
+
/** Core's own offset-to-line/character helper over the file text (D-14, `internal/position.ts`). */
|
|
54
|
+
static fromOffset = (text, offset, length) => {
|
|
55
|
+
const { line, character } = lineCharacter(text, offset);
|
|
56
|
+
return DiagnosticRange.make({
|
|
57
|
+
offset,
|
|
58
|
+
length,
|
|
59
|
+
line,
|
|
60
|
+
character
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const CONFORMANCE_CODES = new Set(ConformanceCode.literals);
|
|
65
|
+
/**
|
|
66
|
+
* One conformance or lint finding (D-32).
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
var Diagnostic = class extends Schema.Class("Diagnostic")({
|
|
70
|
+
file: Schema.String,
|
|
71
|
+
range: Schema.optionalKey(DiagnosticRange),
|
|
72
|
+
code: DiagnosticCode,
|
|
73
|
+
severity: DiagnosticSeverity,
|
|
74
|
+
message: Schema.String
|
|
75
|
+
}) {
|
|
76
|
+
/** True when `code` is one of `ConformanceCode`'s always-error members. */
|
|
77
|
+
static isConformance = (diagnostic) => CONFORMANCE_CODES.has(diagnostic.code);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
export { ConformanceCode, Diagnostic, DiagnosticCode, DiagnosticRange, DiagnosticSeverity, LintCode };
|
package/Generated.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Actor } from "./Actor.js";
|
|
2
|
+
import { Timestamp } from "./Timestamp.js";
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
|
|
5
|
+
//#region src/Generated.ts
|
|
6
|
+
const SHA256_HEX_RE = /^[0-9a-f]{64}$/;
|
|
7
|
+
/**
|
|
8
|
+
* A lowercase 64-character hex sha256 digest of a concept's body, as `@okfit/profiles`'
|
|
9
|
+
* `Derivation.bodyDigest` computes it (issue #19). Optional: every concept stamped before
|
|
10
|
+
* this field existed lacks it, and core itself never computes one -- it only holds the field.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
const BodySha256 = Schema.String.pipe(Schema.check(Schema.isPattern(SHA256_HEX_RE, { message: "Expected a lowercase 64-character hex sha256 digest" })));
|
|
14
|
+
/**
|
|
15
|
+
* Who last generated a concept's content, and when.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
var Generated = class extends Schema.Class("Generated")({
|
|
19
|
+
by: Actor,
|
|
20
|
+
at: Schema.optionalKey(Timestamp),
|
|
21
|
+
body_sha256: Schema.optionalKey(BodySha256)
|
|
22
|
+
}) {
|
|
23
|
+
/** The `by` used when a legacy v0.1 `timestamp` is folded into `generated.at` (D-15); v0.1 has no actor. */
|
|
24
|
+
static LEGACY_BY = "process:legacy-timestamp";
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { BodySha256, Generated };
|