@okfit/core 0.7.4 → 0.8.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/Actor.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Schema } from "effect";
2
2
 
3
3
  //#region src/Actor.ts
4
- const ACTOR_RE = /^(?:[^\s/:]+\/[^\s]+|[A-Za-z][A-Za-z0-9_-]*:[^\s]+)$/;
4
+ const ACTOR_RE = /^(?:[^\s/:]+\/[^\s]+|[A-Za-z][A-Za-z0-9_-]*:[^\s]+)$/u;
5
5
  const isHuman = (actor) => actor.startsWith("human:");
6
6
  const form = (actor) => {
7
7
  const colon = actor.indexOf(":");
package/Diagnostic.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { lineCharacter } from "./internal/position.js";
2
+ import { frontmatterPathSpan } from "./internal/yamlPathSpan.js";
2
3
  import { Schema } from "effect";
3
4
 
4
5
  //#region src/Diagnostic.ts
@@ -64,6 +65,24 @@ var DiagnosticRange = class DiagnosticRange extends Schema.Class("DiagnosticRang
64
65
  character
65
66
  });
66
67
  };
68
+ /**
69
+ * Maps a frontmatter YAML path to the range of its own value inside `document.source`
70
+ * (decision 2 of the phase 4 plan): the whole frontmatter block for `path: []`, `undefined`
71
+ * only when the document has no frontmatter block at all, and the block again as a fallback
72
+ * when the leaf named by `path` cannot be found (an absent key). For a double-quoted scalar
73
+ * value, the returned range includes the delimiting quote characters, since the yaml kit
74
+ * reports a quoted scalar's offset and length inclusive of them.
75
+ *
76
+ * The public entry point for `@okfit/profiles` and `@okfit/engine`; `@okfit/core`'s own lint
77
+ * rules call the internal `frontmatterPathRange` (`internal/frontmatter.ts`) directly.
78
+ *
79
+ * @public
80
+ */
81
+ static forFrontmatterPath = (document, path) => {
82
+ if (document.frontmatter === void 0) return void 0;
83
+ const span = frontmatterPathSpan(document.source, document.frontmatter, path);
84
+ return DiagnosticRange.fromOffset(document.source, span.offset, span.length);
85
+ };
67
86
  };
68
87
  const CONFORMANCE_CODES = new Set(ConformanceCode.literals);
69
88
  /**
package/Generated.js CHANGED
@@ -3,7 +3,7 @@ import { Timestamp } from "./Timestamp.js";
3
3
  import { Schema } from "effect";
4
4
 
5
5
  //#region src/Generated.ts
6
- const SHA256_HEX_RE = /^[0-9a-f]{64}$/;
6
+ const SHA256_HEX_RE = /^[0-9a-f]{64}$/u;
7
7
  /**
8
8
  * A lowercase 64-character hex sha256 digest of a concept's body, as `@okfit/profiles`'
9
9
  * `Derivation.bodyDigest` computes it (issue #19). Optional: every concept stamped before
package/OkfitConfig.js CHANGED
@@ -27,7 +27,7 @@ const LintLevel = Schema.Literals([
27
27
  });
28
28
  const SHORT_DURATION_RE = /^(\d+)(h|d|w)$/;
29
29
  const LONG_DURATION_RE = /^\d+(?:\.\d+)?\s+(?:nanos?|micros?|millis?|seconds?|minutes?|hours?|days?|weeks?)$/;
30
- const STALE_AFTER_PATTERN_RE = /^(?:\d+[hdw]|\d+(?:\.\d+)?\s+(?:nanos?|micros?|millis?|seconds?|minutes?|hours?|days?|weeks?))$/;
30
+ const STALE_AFTER_PATTERN_RE = /^(?:\d+[hdw]|\d+(?:\.\d+)?\s+(?:nanos?|micros?|millis?|seconds?|minutes?|hours?|days?|weeks?))$/u;
31
31
  const HOUR_MILLIS = 36e5;
32
32
  const DAY_MILLIS = 864e5;
33
33
  const WEEK_MILLIS = 6048e5;
package/Timestamp.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Function, Schema, SchemaTransformation } from "effect";
2
2
 
3
3
  //#region src/Timestamp.ts
4
- const OFFSET_RE = /(?:Z|[+-]\d{2}:\d{2})$/;
4
+ const OFFSET_RE = /(?:Z|[+-]\d{2}:\d{2})$/u;
5
5
  const ZERO_MILLIS = /\.000(?=Z$)/;
6
6
  /**
7
7
  * ISO 8601 string with an explicit offset (`Z`, `+hh:mm` or `-hh:mm`) to `DateTime.Utc`.
package/index.d.ts CHANGED
@@ -227,6 +227,20 @@ declare const DiagnosticRange_base: Schema.Class<DiagnosticRange, Schema.Struct<
227
227
  export declare class DiagnosticRange extends DiagnosticRange_base {
228
228
  /** Core's own offset-to-line/character helper over the file text (D-14, `internal/position.ts`). */
229
229
  static readonly fromOffset: (text: string, offset: number, length: number) => DiagnosticRange;
230
+ /**
231
+ * Maps a frontmatter YAML path to the range of its own value inside `document.source`
232
+ * (decision 2 of the phase 4 plan): the whole frontmatter block for `path: []`, `undefined`
233
+ * only when the document has no frontmatter block at all, and the block again as a fallback
234
+ * when the leaf named by `path` cannot be found (an absent key). For a double-quoted scalar
235
+ * value, the returned range includes the delimiting quote characters, since the yaml kit
236
+ * reports a quoted scalar's offset and length inclusive of them.
237
+ *
238
+ * The public entry point for `@okfit/profiles` and `@okfit/engine`; `@okfit/core`'s own lint
239
+ * rules call the internal `frontmatterPathRange` (`internal/frontmatter.ts`) directly.
240
+ *
241
+ * @public
242
+ */
243
+ static readonly forFrontmatterPath: (document: MarkdownDocument, path: ReadonlyArray<string | number>) => DiagnosticRange | undefined;
230
244
  }
231
245
  declare const Diagnostic_base: Schema.Class<Diagnostic, Schema.Struct<{
232
246
  readonly file: Schema.String;
@@ -1,8 +1,7 @@
1
- import { toFileOffset } from "./position.js";
1
+ import { frontmatterPathSpan } from "./yamlPathSpan.js";
2
2
  import { Diagnostic, DiagnosticRange } from "../Diagnostic.js";
3
3
  import { decodeConcept as decodeConcept$1 } from "./conceptDecode.js";
4
- import { Effect, Option, Result } from "effect";
5
- import { YamlDocument } from "@effected/yaml";
4
+ import { Option } from "effect";
6
5
 
7
6
  //#region src/internal/frontmatter.ts
8
7
  /** D-34 default severities for a `FamilyIssue`'s lint code; `type-missing` (D-33) is always "error". */
@@ -11,21 +10,23 @@ const FAMILY_ISSUE_SEVERITY = {
11
10
  "legacy-timestamp": "info",
12
11
  "computation-runtime-missing": "error"
13
12
  };
14
- const blockRange = (context) => DiagnosticRange.fromOffset(context.text, context.node.position.start.offset, context.node.position.end.offset - context.node.position.start.offset);
13
+ const spanToRange = (text, path, node) => {
14
+ const span = frontmatterPathSpan(text, node, path);
15
+ return DiagnosticRange.fromOffset(text, span.offset, span.length);
16
+ };
15
17
  /**
16
- * Lazily parses `node.value` as YAML only when a `FamilyIssue.path` needs a range (D-14, D-15;
17
- * diagnostic-range-and-position-mapping.md section 5). Falls back to the whole frontmatter block
18
- * when `path` is `[]`, the leaf can't be found, or the YAML re-parse itself is fatal (it already
19
- * decoded once upstream, so a fatal re-parse should not happen; this is defense only).
18
+ * Maps a frontmatter YAML path to a precise range inside `document.source` (decision 2 of the
19
+ * phase 4 plan): the whole frontmatter block for `path: []`, `undefined` only when the document
20
+ * has no frontmatter block at all, and the block again as a fallback when the leaf named by
21
+ * `path` cannot be found (an absent key, or a fatal re-parse that should not happen once the
22
+ * document already decoded once upstream). For a double-quoted scalar value, the returned range
23
+ * includes the delimiting quote characters, since the yaml kit reports a quoted scalar's offset
24
+ * and length inclusive of them.
25
+ *
26
+ * @internal
20
27
  */
21
- const rangeFor = (context, path) => {
22
- if (path.length === 0) return blockRange(context);
23
- const parsed = Effect.runSync(Effect.result(YamlDocument.parse(context.node.value)));
24
- if (Result.isFailure(parsed) || parsed.success.contents === null) return blockRange(context);
25
- const hit = parsed.success.contents.find(path);
26
- if (Option.isNone(hit)) return blockRange(context);
27
- return DiagnosticRange.fromOffset(context.text, toFileOffset(context.text, hit.value.offset), hit.value.length);
28
- };
28
+ const frontmatterPathRange = (document, path) => document.frontmatter === void 0 ? void 0 : spanToRange(document.source, path, document.frontmatter);
29
+ const rangeFor = (context, path) => spanToRange(context.text, path, context.node);
29
30
  const toDiagnostic = (context, issue) => Diagnostic.make({
30
31
  file: context.file,
31
32
  code: issue.code,
@@ -59,4 +60,4 @@ const decodeConcept = (raw, context) => {
59
60
  };
60
61
 
61
62
  //#endregion
62
- export { decodeConcept };
63
+ export { decodeConcept, frontmatterPathRange };
@@ -1,5 +1,6 @@
1
1
  import { Actor } from "../Actor.js";
2
2
  import { Diagnostic, DiagnosticRange } from "../Diagnostic.js";
3
+ import { frontmatterPathRange } from "./frontmatter.js";
3
4
  import { Derive } from "../Derive.js";
4
5
  import { DateTime } from "effect";
5
6
 
@@ -7,12 +8,6 @@ import { DateTime } from "effect";
7
8
  const FOOTNOTE_RE = /\[\^([^\]\s]+)\]/g;
8
9
  /** A footnote definition: the label at a line start, followed by a colon. */
9
10
  const FOOTNOTE_DEFINITION_RE = /^\[\^([^\]\s]+)\]:/gm;
10
- const frontmatterRange = (concept) => {
11
- const node = concept.document.frontmatter;
12
- if (node === void 0) return;
13
- const start = node.position.start.offset;
14
- return DiagnosticRange.fromOffset(concept.document.source, start, node.position.end.offset - start);
15
- };
16
11
  const present = (raw, key) => Object.hasOwn(raw, key) && raw[key] !== null;
17
12
  /**
18
13
  * Source spans of every inline code span and code block, so a footnote-shaped
@@ -32,10 +27,19 @@ const rule = (code, body) => (context) => {
32
27
  const severity = context.severity(code);
33
28
  return severity === "off" ? [] : body(context, severity);
34
29
  };
35
- /** A rule that yields messages per concept, each carrying the frontmatter block range (decision 2). */
36
- const perConcept = (code, body) => rule(code, (context, severity) => [...context.bundle.concepts.values()].flatMap((concept) => body(concept, context).map((message) => diagnostic(concept.path, code, severity, message, frontmatterRange(concept)))));
30
+ /** A rule that yields messages per concept, each anchored at the frontmatter path that names the offending value (default `[]`, the frontmatter block) and falling back to that block when the path's leaf is absent (decision 2). */
31
+ const perConcept = (code, body) => rule(code, (context, severity) => [...context.bundle.concepts.values()].flatMap((concept) => body(concept, context).map((item) => {
32
+ const { message, path } = typeof item === "string" ? {
33
+ message: item,
34
+ path: []
35
+ } : item;
36
+ return diagnostic(concept.path, code, severity, message, frontmatterPathRange(concept.document, path));
37
+ })));
37
38
  const configUnknownKey = rule("config-unknown-key", (context, severity) => Object.keys(context.config.extensions).map((key) => diagnostic("", "config-unknown-key", severity, `Unknown config key "${key}"`, void 0)));
38
- const unknownType = perConcept("unknown-type", (concept, context) => Object.hasOwn(context.config.types ?? {}, concept.frontmatter.type) ? [] : [`Type "${concept.frontmatter.type}" is not declared in [types]`]);
39
+ const unknownType = perConcept("unknown-type", (concept, context) => Object.hasOwn(context.config.types ?? {}, concept.frontmatter.type) ? [] : [{
40
+ message: `Type "${concept.frontmatter.type}" is not declared in [types]`,
41
+ path: ["type"]
42
+ }]);
39
43
  const requiredKeyMissing = perConcept("required-key-missing", (concept, context) => {
40
44
  const declared = context.config.types?.[concept.frontmatter.type]?.required ?? [];
41
45
  return [.../* @__PURE__ */ new Set([...context.config.concepts?.required ?? [], ...declared])].filter((key) => !present(concept.frontmatter.raw, key)).map((key) => `Required key "${key}" is missing`);
@@ -46,17 +50,45 @@ const fieldValueUnknown = perConcept("field-value-unknown", (concept, context) =
46
50
  for (const [key, declaration] of Object.entries(fields)) {
47
51
  if (declaration.values === void 0 || !present(concept.frontmatter.raw, key)) continue;
48
52
  const value = concept.frontmatter.raw[key];
49
- if (typeof value !== "string" || !Object.hasOwn(declaration.values, value)) out.push(`Value ${JSON.stringify(value)} for "${key}" is not one of: ${Object.keys(declaration.values).join(", ")}`);
53
+ if (typeof value !== "string" || !Object.hasOwn(declaration.values, value)) out.push({
54
+ message: `Value ${JSON.stringify(value)} for "${key}" is not one of: ${Object.keys(declaration.values).join(", ")}`,
55
+ path: [key]
56
+ });
50
57
  }
51
58
  return out;
52
59
  });
53
- const requireVerifiedUnmet = perConcept("require-verified-unmet", (concept, context) => context.config.types?.[concept.frontmatter.type]?.require_verified === true && concept.frontmatter.status !== "draft" && (concept.frontmatter.verified ?? []).length === 0 ? [`Type "${concept.frontmatter.type}" requires a verified entry`] : []);
60
+ const requireVerifiedUnmet = perConcept("require-verified-unmet", (concept, context) => context.config.types?.[concept.frontmatter.type]?.require_verified === true && concept.frontmatter.status !== "draft" && (concept.frontmatter.verified ?? []).length === 0 ? [{
61
+ message: `Type "${concept.frontmatter.type}" requires a verified entry`,
62
+ path: ["status"]
63
+ }] : []);
54
64
  const statusMissing = perConcept("status-missing", (concept) => concept.frontmatter.status === void 0 && (concept.frontmatter.verified ?? []).length === 0 ? ["Concept has no status and no verified entry, so it reads as stable; set status: draft or stable explicitly"] : []);
55
65
  const generatedMissing = perConcept("generated-missing", (concept, context) => context.config.actors?.agent !== void 0 && !present(concept.frontmatter.raw, "generated") ? [`Concept has no generated block; okfit sync will create it from actors.agent (${context.config.actors.agent}) once the body is committed, or set generated.by by hand`] : []);
56
66
  const actorPrefixUnknown = perConcept("actor-prefix-unknown", (concept) => {
57
- return [concept.frontmatter.generated?.by, ...(concept.frontmatter.verified ?? []).map((v) => v.by)].filter((by) => by !== void 0 && Actor.form(by) === "other").map((by) => `Actor "${by}" uses an unknown prefix; expected human:<id>, process:<id>, or <producer>/<version>`);
67
+ const candidates = [{
68
+ by: concept.frontmatter.generated?.by,
69
+ path: ["generated", "by"]
70
+ }, ...(concept.frontmatter.verified ?? []).map((entry, index) => ({
71
+ by: entry.by,
72
+ path: [
73
+ "verified",
74
+ index,
75
+ "by"
76
+ ]
77
+ }))];
78
+ const out = [];
79
+ for (const candidate of candidates) {
80
+ if (candidate.by === void 0 || Actor.form(candidate.by) !== "other") continue;
81
+ out.push({
82
+ message: `Actor "${candidate.by}" uses an unknown prefix; expected human:<id>, process:<id>, or <producer>/<version>`,
83
+ path: candidate.path
84
+ });
85
+ }
86
+ return out;
58
87
  });
59
- const stale = perConcept("stale", (concept, context) => context.now !== void 0 && concept.frontmatter.stale_after !== void 0 && Derive.isStale(concept.frontmatter, context.now) ? [`Concept is stale since ${DateTime.formatIso(concept.frontmatter.stale_after)}`] : []);
88
+ const stale = perConcept("stale", (concept, context) => context.now !== void 0 && concept.frontmatter.stale_after !== void 0 && Derive.isStale(concept.frontmatter, context.now) ? [{
89
+ message: `Concept is stale since ${DateTime.formatIso(concept.frontmatter.stale_after)}`,
90
+ path: ["stale_after"]
91
+ }] : []);
60
92
  const footnoteSourceUnknown = rule("footnote-source-unknown", (context, severity) => {
61
93
  const out = [];
62
94
  for (const concept of context.bundle.concepts.values()) {
@@ -0,0 +1,48 @@
1
+ import { toFileOffset } from "./position.js";
2
+ import { Effect, Option, Result } from "effect";
3
+ import { YamlDocument } from "@effected/yaml";
4
+
5
+ //#region src/internal/yamlPathSpan.ts
6
+ const blockSpan = (node) => ({
7
+ offset: node.position.start.offset,
8
+ length: node.position.end.offset - node.position.start.offset
9
+ });
10
+ /**
11
+ * One concept's frontmatter is re-parsed at most once per process, keyed by the `Frontmatter`
12
+ * node's own identity (stable for the lifetime of a loaded document): a lint pass fires several
13
+ * rules per concept (`unknown-type`, `stale`, `actor-prefix-unknown`, ...), and each one that
14
+ * anchors a range would otherwise re-run this YAML parse over the same, unchanged text.
15
+ */
16
+ const parsedCache = /* @__PURE__ */ new WeakMap();
17
+ const parsedYamlOf = (node) => {
18
+ const cached = parsedCache.get(node);
19
+ if (cached !== void 0) return cached;
20
+ const parsed = Effect.runSync(Effect.result(YamlDocument.parse(node.value)));
21
+ parsedCache.set(node, parsed);
22
+ return parsed;
23
+ };
24
+ /**
25
+ * Lazily parses `node.value` as YAML only when `path` needs a span (D-14, D-15;
26
+ * diagnostic-range-and-position-mapping.md section 5), memoized per `node` (`parsedYamlOf`).
27
+ * Falls back to the whole frontmatter block's span when `path` is `[]`, the leaf can't be found,
28
+ * or the YAML re-parse itself is fatal (it already decoded once upstream, so a fatal re-parse
29
+ * should not happen; this is defense only). For a double-quoted scalar, the yaml kit reports the
30
+ * offset and length INCLUDING the delimiting quote characters, so the returned span covers them
31
+ * too.
32
+ *
33
+ * @internal
34
+ */
35
+ const frontmatterPathSpan = (text, node, path) => {
36
+ if (path.length === 0) return blockSpan(node);
37
+ const parsed = parsedYamlOf(node);
38
+ if (Result.isFailure(parsed) || parsed.success.contents === null) return blockSpan(node);
39
+ const hit = parsed.success.contents.find(path);
40
+ if (Option.isNone(hit)) return blockSpan(node);
41
+ return {
42
+ offset: toFileOffset(text, hit.value.offset),
43
+ length: hit.value.length
44
+ };
45
+ };
46
+
47
+ //#endregion
48
+ export { frontmatterPathSpan };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okfit/core",
3
- "version": "0.7.4",
3
+ "version": "0.8.1",
4
4
  "private": false,
5
5
  "description": "Spec-level Open Knowledge Format (OKF) v0.2 schemas, bundle loading, graph, derivation, and validation for Effect.",
6
6
  "keywords": [
@@ -37,15 +37,15 @@
37
37
  "./package.json": "./package.json"
38
38
  },
39
39
  "peerDependencies": {
40
- "@effected/config-file": "^0.12.0",
41
- "@effected/glob": "^0.8.0",
42
- "@effected/jsonc": "^0.13.0",
43
- "@effected/markdown": "^0.12.0",
44
- "@effected/schemastore": "^0.15.0",
45
- "@effected/toml": "^0.9.0",
46
- "@effected/walker": "^0.12.0",
47
- "@effected/yaml": "^0.17.0",
48
- "effect": "4.0.0-rc.117"
40
+ "@effected/config-file": "^0.13.0",
41
+ "@effected/glob": "^0.9.0",
42
+ "@effected/jsonc": "^0.14.0",
43
+ "@effected/markdown": "^0.14.0",
44
+ "@effected/schemastore": "^0.16.0",
45
+ "@effected/toml": "^0.10.0",
46
+ "@effected/walker": "^0.13.0",
47
+ "@effected/yaml": "^0.18.0",
48
+ "effect": "4.0.0-rc.118"
49
49
  },
50
50
  "engines": {
51
51
  "node": ">=24.11.0"