@equationalapplications/core-okf 9.20.1 → 9.21.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/README.md CHANGED
@@ -4,11 +4,11 @@
4
4
 
5
5
  ## Overview
6
6
 
7
- A zero-dependency library for parsing and producing [Open Knowledge Format (OKF) v0.1 and v0.2](https://github.com/GoogleCloudPlatform/knowledge-catalog/tree/main/okf) bundles. This package provides the raw primitives to work with OKF frontmatter, concept documents, and index/log files, completely decoupled from any specific database or data model.
7
+ A zero-dependency library for parsing and producing [Open Knowledge Format (OKF) v0.1, v0.2 and v0.3](https://github.com/open-knowledge-format/open-knowledge-format) bundles. This package provides the raw primitives to work with OKF frontmatter, concept documents, and index/log files, completely decoupled from any specific database or data model.
8
8
 
9
9
  For a ready-made `MemoryDump` ⇄ OKF bundle adapter, see the [OKF guide for `@equationalapplications/core-llm-wiki`](https://equationalapplications.github.io/expo-llm-wiki/guides/okf/).
10
10
 
11
- For the llm-wiki interoperability profiles (`llm-wiki/1` and `llm-wiki/2`), see [`docs/okf-profile.md`](https://github.com/equationalapplications/expo-llm-wiki/blob/main/docs/okf-profile.md). Conformance fixtures live in [`fixtures/`](./fixtures/).
11
+ For the llm-wiki interoperability profiles (`llm-wiki/1` through `llm-wiki/3`), see [`docs/okf-profile.md`](https://github.com/equationalapplications/expo-llm-wiki/blob/main/docs/okf-profile.md). Conformance fixtures live in [`fixtures/`](./fixtures/).
12
12
 
13
13
  > **GraphRAG compatibility:** core-okf is database-agnostic. The adapter that can populate `llm_wiki_edges` for `traverseGraph()` lives in `@equationalapplications/core-llm-wiki`. See [GraphRAG guide](https://equationalapplications.github.io/expo-llm-wiki/guides/graphrag/).
14
14
 
@@ -96,19 +96,30 @@ const links = extractMarkdownLinks('See [preferences](facts/fact_abc.md) for mor
96
96
  | [@equationalapplications/react-llm-wiki](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/react/README.md) | Persistent episodic memory for Web |
97
97
  | [@equationalapplications/prisma-outbox](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/prisma-outbox/README.md) | Sync SQLite outbox events to Prisma |
98
98
  | [@equationalapplications/core-llm-tools](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/core-llm-tools/README.md) | Gemini tool schemas and capability injector |
99
- | [**@equationalapplications/core-okf**](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/okf/README.md) | Zero-dependency Open Knowledge Format (OKF) v0.1 + v0.2 primitives — parse and produce interoperable knowledge bundles. |
99
+ | [**@equationalapplications/core-okf**](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/okf/README.md) | Zero-dependency Open Knowledge Format (OKF) v0.1–v0.3 primitives — parse and produce interoperable knowledge bundles. |
100
100
  | [@equationalapplications/schema-org-llm-wiki](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/schema-org/README.md) | Curated schema.org warm-agent ontology manifest |
101
101
  | [@equationalapplications/schema-software-org](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/schema-software-org/README.md) | Software-organization executive ontology manifest — 21 node types, 42 edges, warm-agent superset, data-only |
102
102
  | [@equationalapplications/mcp-server](https://github.com/equationalapplications/expo-llm-wiki/blob/main/packages/mcp-server/README.md) | MCP server exposing hosted cloud memory tools to coding agents |
103
103
 
104
- ## OKF v0.2 conformance
104
+ ## OKF v0.1–v0.3 conformance
105
105
 
106
- This package conforms to [OKF v0.2](https://github.com/GoogleCloudPlatform/knowledge-catalog/blob/main/okf/SPEC.md). New helpers for v0.2:
106
+ This package conforms to the [Open Knowledge Format specification](https://github.com/open-knowledge-format/open-knowledge-format/blob/main/SPEC.md) (v0.1 through v0.3) and ships conformance fixtures for each profile it reads. Helpers added for v0.2:
107
107
 
108
108
  - `deriveTrustTier(verified)` — derive `'unverified' | 'machine-confirmed' | 'human-reviewed'`.
109
- - `isStaleAfter(staleAfter, now)` — staleness check against an absolute `YYYY-MM-DD` cutoff.
109
+ - `isStaleAfter(staleAfter, now)` — staleness check against an absolute cutoff.
110
110
  - `parseVerifiedFlexible(value)` — accept either an array or a bare `{ by, at }` mapping.
111
111
  - `extractFootnotes(body)` / `serializeFootnotes(footnotes)` — preserve footnote attribution verbatim.
112
112
  - `parseFlowMapping(text)` / `parseFlowSequence(text)` — flow parsing with at most one level of mapping-value nesting (no anchors/aliases outside quoted strings).
113
113
 
114
+ Helpers added for v0.3:
115
+
116
+ - `parseOkfTimestamp(value)` — v0.3 §5 timestamps: RFC 3339 date-times with an explicit offset and date-only forms; returns epoch ms or `null` (an offset-less date-time is invalid).
117
+ - `deriveTrustTier(verified, generatedAt?)` — with the optional second parameter, only events at or after `generated.at` are current (§5.3); `team:` and unknown verifier schemes are non-human.
118
+ - `isStaleAfter` (§5.5) — an empty value is absent and an unparseable value is **stale**, never fresh.
119
+ - `parseSourcesFlexible(value)` — a bare mapping is one source; an entry without a `resource` is ignored and counted.
120
+ - `serializeFrontmatter(fm, { rawBlocks })` / `buildConceptDocument(fm, body, { rawBlocks })` — verbatim re-emission of the raw lines a parse handed back.
121
+ - `buildRootIndexMd` / `parseRootIndexMd` — bundle-root frontmatter metadata round-trip (§8.1), with the reserved `x-llm-wiki-raw` slot written only on `llm-wiki/3` and stripped on `llm-wiki/1` and `/2`.
122
+
123
+ The parser reads comments and one bounded level of block structure, and hands back what it cannot represent as `raw` lines plus `issues` entries; anchors and aliases are rejected wherever they appear at a node start.
124
+
114
125
  The `serializeFrontmatter` function emits plain objects as flow mappings and actor strings (containing `/` or `:`) via `serializeActorString`. Anchor/alias expansion is rejected; only **unquoted** `&` or `*` characters are treated as opaque (quoted spans are excluded from the scan, so URL query strings like `"https://x/a?p=1&q=2"` remain valid).
package/dist/index.d.mts CHANGED
@@ -9,6 +9,7 @@ interface OkfFrontmatter {
9
9
  resource?: string;
10
10
  tags?: string[];
11
11
  timestamp?: string;
12
+ superseded_by?: string;
12
13
  [key: string]: OkfFrontmatterValue | undefined;
13
14
  }
14
15
  interface OkfIndexEntry {
@@ -65,50 +66,118 @@ interface OkfFootnote {
65
66
  }
66
67
 
67
68
  declare function serializeActorString(value: string): string;
68
- declare function serializeFrontmatter(fm: OkfFrontmatter): string;
69
- /**
70
- * `depth` counts levels of **mapping-value nesting** only: it increments when a
71
- * flow MAPPING's key has a value that is itself a flow collection, and is
72
- * rejected once it would exceed 1 (spec §2.6's "at most one level of nesting").
73
- * It does NOT increment when a flow SEQUENCE's item is itself a mapping or
74
- * sequence — `sources: [ {...}, {...} ]` (a flat array of objects) is the base
75
- * shape v0.2 uses everywhere and is not "nesting" in the spec's sense. This is
76
- * what lets `sources: [ { usage_window: { from, to } } ]` parse (sequence item
77
- * is a mapping at depth 0; that mapping's `usage_window` value is a mapping at
78
- * depth 1 — one level, allowed) while `{ a: { b: { c: 1 } } }` is rejected
79
- * (`a`'s value nests at depth 1; `b`'s value would need depth 2 — rejected).
80
- */
69
+ /** Options for {@link serializeFrontmatter} (§A7). */
70
+ interface SerializeFrontmatterOptions {
71
+ /**
72
+ * Verbatim lines §A3(d) handed back for one key each, written after the
73
+ * serialized keys and before the closing `---`, unchanged. The serializer
74
+ * does not parse or validate them; callers pass only blocks they stored
75
+ * from a parse. Without the option the output is byte-identical to the
76
+ * pre-v0.3 serializer's.
77
+ */
78
+ rawBlocks?: string[];
79
+ }
80
+ declare function serializeFrontmatter(fm: OkfFrontmatter, options?: SerializeFrontmatterOptions): string;
81
81
  declare function parseFlowMapping(text: string, depth?: number): Record<string, OkfFrontmatterValue> | null;
82
82
  declare function parseFlowSequence(text: string, depth?: number): OkfFrontmatterValue[] | null;
83
+ /**
84
+ * A key the parser could not represent, reported by key and reason slug —
85
+ * never by content (spec §A3d). The reason is one of: `nested_too_deep`
86
+ * (block structure beyond the representable level), `block_scalar` (a value
87
+ * introduced by `|` or `>`), `multiline_scalar` (an inline scalar continued
88
+ * on indented lines), `malformed` (a flow collection left open or rejected
89
+ * for depth or syntax), or `anchor_or_alias` (a `&`/`*` at a node start).
90
+ */
91
+ interface FrontmatterIssue {
92
+ key: string;
93
+ reason: string;
94
+ }
83
95
  /**
84
96
  * Parses the subset of YAML frontmatter that {@link serializeFrontmatter} produces:
85
- * scalar string/number/boolean/null values, quoted strings, and block string-lists.
86
- * NOT a general YAML parser — flow collections (`[...]`/`{...}`), multi-line block
87
- * scalars (`|`/`>`), anchors, and aliases are not recognized. Lines that don't match
88
- * a recognized shape are silently skipped rather than throwing, so a foreign bundle
89
- * never crashes
90
- * the import — it just loses fidelity on the unrecognized line.
97
+ * scalar string/number/boolean/null values, quoted strings, block string-lists,
98
+ * and one bounded level of block structure (§A3c): a top-level key with no inline
99
+ * value opens a block mapping (indented `k: v` at one indentation) or a block
100
+ * list (`- ` items that are scalars, single-line flow collections, or `- k: v`
101
+ * with aligned continuations). NOT a general YAML parser. The grammar is the
102
+ * §A3 subset: full-line comments are skipped; a `#` after whitespace outside
103
+ * quotes starts a comment (a URL fragment and a `#` inside quotes are data); a
104
+ * line starting with whitespace is never a top-level key; inside a block a key
105
+ * is separated from its value by a colon + space or the end of the line;
106
+ * anchors/aliases (`&`/`*` at a node start) are rejected.
107
+ *
108
+ * Lines whose value the parser cannot represent are handed back unparsed in
109
+ * `raw` (the key's lines verbatim, key line included, trailing blanks excluded)
110
+ * with an entry in `issues`, and the key is absent from `frontmatter` — no key
111
+ * is ever stored as `null`. An anchor/alias key goes to `issues` only, never to
112
+ * `raw` (we must not become an emitter of anchors). An inexact top-level plain
113
+ * scalar (`1.10`, `1234567890123456789`, `True`, `~` — its serialized form
114
+ * differs from its source text) is parsed as today AND its line is placed in
115
+ * `raw` with no issue (§A3f; the check is a top-level one by rule). Block
116
+ * content the grammar cannot represent — a block scalar, a second indentation
117
+ * level, a block line whose flow value nests past the serializer's bound, a
118
+ * multi-line flow — comes back in `raw` with the §A3d reason slug
119
+ * (`block_scalar`, `nested_too_deep`, `malformed`, `multiline_scalar`).
120
+ * Unrecognized lines that match no key at all are still skipped rather than
121
+ * throwing, so a foreign bundle never crashes the import.
91
122
  */
92
123
  declare function parseFrontmatter(content: string): {
93
124
  frontmatter: OkfFrontmatter;
94
125
  rest: string;
126
+ raw: Record<string, string>;
127
+ issues: FrontmatterIssue[];
95
128
  };
96
129
 
97
- declare function buildConceptDocument(fm: OkfFrontmatter, body: string): string;
98
- /** Reverse of {@link buildConceptDocument}: strips the single leading blank-line separator. */
130
+ declare function buildConceptDocument(fm: OkfFrontmatter, body: string, options?: SerializeFrontmatterOptions): string;
131
+ /**
132
+ * Reverse of {@link buildConceptDocument}: strips the single leading blank-line
133
+ * separator. The raw lines (`raw`) and issue entries (`issues`) the §A3 parser
134
+ * handed back are passed through beside `frontmatter` — core decides what to
135
+ * do with each (keep the raw lines of an unmapped key, drop and report a
136
+ * mapped one, spec §A3d/§B8).
137
+ */
99
138
  declare function parseConcept(content: string): {
100
139
  frontmatter: OkfFrontmatter;
101
140
  body: string;
141
+ raw: Record<string, string>;
142
+ issues: ReturnType<typeof parseFrontmatter>['issues'];
102
143
  };
103
144
 
104
145
  declare function buildIndexMd(sections: OkfIndexSection[]): string;
105
- declare function buildRootIndexMd(okfVersion: string, sections: OkfIndexSection[], options?: {
146
+ /** Options for {@link buildRootIndexMd} (§A5). */
147
+ interface BuildRootIndexMdOptions {
106
148
  profile?: string;
107
- }): string;
108
- declare function parseRootIndexMd(content: string): {
149
+ /**
150
+ * Root-index metadata, emitted verbatim after `okf_version` and `profile`.
151
+ * Both of those keys are builder-owned and never taken from this record.
152
+ * The reserved `x-llm-wiki-raw` slot (a `Record<key, lines>`) is written
153
+ * only when `profile` is `llm-wiki/3` — its string entries go out verbatim
154
+ * after the metadata keys — and is stripped on `llm-wiki/1` and `/2`, so it
155
+ * can never serialize as a literal YAML key on older profiles (owner note 1).
156
+ */
157
+ bundleMetadata?: Record<string, OkfFrontmatterValue>;
158
+ /**
159
+ * Verbatim lines handed back by a parse (§A7), written after the metadata
160
+ * keys and before the closing `---`. Callers pass only blocks they stored
161
+ * from a parse.
162
+ */
163
+ rawBlocks?: string[];
164
+ }
165
+ declare function buildRootIndexMd(okfVersion: string, sections: OkfIndexSection[], options?: BuildRootIndexMdOptions): string;
166
+ /** Result of {@link parseRootIndexMd} (§A5, refinement 44). */
167
+ interface ParsedRootIndexMd {
109
168
  okf_version?: string;
110
169
  profile?: string;
111
- };
170
+ /** Every frontmatter key except `okf_version`/`profile` and the
171
+ * parser-synthesized empty `type`. A root key handed back unparsed keeps
172
+ * its verbatim lines under the reserved `x-llm-wiki-raw` slot; an anchor
173
+ * is dropped and reported in `issues` only. */
174
+ metadata: Record<string, OkfFrontmatterValue>;
175
+ /** The parse's raw handback record (§A3d/§A3f), key line included. */
176
+ raw: Record<string, string>;
177
+ /** What the parse dropped or could not read, by key and reason slug. */
178
+ issues: FrontmatterIssue[];
179
+ }
180
+ declare function parseRootIndexMd(content: string): ParsedRootIndexMd;
112
181
 
113
182
  declare function buildEntityIndexMd(options: {
114
183
  summary?: string;
@@ -197,9 +266,42 @@ declare function extractFootnotes(body: string): OkfFootnote[];
197
266
  declare function serializeFootnotes(footnotes: OkfFootnote[]): string;
198
267
 
199
268
  type TrustTier = 'unverified' | 'machine-confirmed' | 'human-reviewed';
200
- declare function deriveTrustTier(verified: OkfVerified | undefined): TrustTier;
269
+ /**
270
+ * OKF v0.3 §5.3 trust currency (spec okf-v03 §A2, ruling R2). Optional second
271
+ * parameter `generatedAt`: epoch ms or a parseable timestamp string. When
272
+ * provided, only events whose parsed `at >= generatedAt` are current (an
273
+ * event whose `at` does not parse is ignored for tiering, kept as history);
274
+ * a `generatedAt` that is present but unreadable means NO event is current.
275
+ * When no event is current the tier is 'unverified' — it can only go down.
276
+ * When `generatedAt` is absent, all events are current, which
277
+ * reproduces the v0.2 sticky rule exactly (R2). §11.2 rule 2: `verified` may
278
+ * be a bare mapping.
279
+ */
280
+ declare function deriveTrustTier(verified: OkfVerified | OkfVerifiedEntry | undefined, generatedAt?: number | string | null): TrustTier;
281
+ /**
282
+ * OKF v0.3 §5.5: a consumer MUST NOT treat a `stale_after` value that it
283
+ * cannot parse as fresh. `null`/`undefined`, an empty or whitespace-only
284
+ * string are absent — not stale (§11.2 rule 1). A finite number, or a string
285
+ * `parseOkfTimestamp` accepts, compares as `now >= cutoff`. Anything else —
286
+ * a string that does not parse, a non-finite number — is STALE.
287
+ */
201
288
  declare function isStaleAfter(staleAfter: string | number | null, now: number): boolean;
202
289
  declare function parseVerifiedFlexible(value: unknown): OkfVerified;
290
+ /**
291
+ * OKF v0.3 §A4 (spec okf-v03, refinement 26): mirror of
292
+ * `parseVerifiedFlexible`. §11.2 rule 2: `sources` may be a bare mapping —
293
+ * one item ⇒ a list with one item. An entry is kept only when it is a
294
+ * mapping whose REQUIRED `resource` is a non-empty string (§5.1, §11.2
295
+ * rule 3); every other entry is ignored and counted in `ignored` so a
296
+ * consumer can report the count (§B13 `sources_entry_invalid`). `null`,
297
+ * `undefined` and unrecognizable shapes are absent — nothing to keep,
298
+ * nothing to count. The kept entries are the caller's own objects,
299
+ * unmodified (extras ride along; refinement 47).
300
+ */
301
+ declare function parseSourcesFlexible(value: unknown): {
302
+ sources: OkfSource[];
303
+ ignored: number;
304
+ };
203
305
  declare function formatVerifiedJson(entries: OkfVerified): string;
204
306
  declare function formatSourcesJson(sources: OkfSource[], sharedWindow?: OkfSourceUsageWindow | null): string;
205
307
  declare function latestVerified(entries: OkfVerified | undefined, _now: number): {
@@ -208,5 +310,17 @@ declare function latestVerified(entries: OkfVerified | undefined, _now: number):
208
310
  } | null;
209
311
  declare function parseCitationsList(body: string): string[];
210
312
 
211
- export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, createEventIdDeriver, deriveEventId, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseRootIndexMd, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
212
- export type { DeriveEventIdInput, OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, TrustTier };
313
+ /**
314
+ * OKF v0.3 timestamp helpers (spec okf-v03 §A1; upstream §5, §13.2).
315
+ *
316
+ * v0.3 §5 accepts exactly two forms: an RFC 3339 date-time WITH an explicit
317
+ * offset, or a date-only `YYYY-MM-DD` (meaning start of that day, UTC). An
318
+ * offset-less date-time (`2026-01-01T00:00:00`) stays invalid — §13.2. Our
319
+ * YAML layer is 1.2 core schema, so quoted and native scalars both arrive
320
+ * here as strings; finite numbers (epoch ms, the stored column form) pass
321
+ * through. Returns epoch ms, or null when unparseable.
322
+ */
323
+ declare function parseOkfTimestamp(value: unknown): number | null;
324
+
325
+ export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, createEventIdDeriver, deriveEventId, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseOkfTimestamp, parseRootIndexMd, parseSourcesFlexible, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
326
+ export type { BuildRootIndexMdOptions, DeriveEventIdInput, FrontmatterIssue, OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, ParsedRootIndexMd, SerializeFrontmatterOptions, TrustTier };
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ interface OkfFrontmatter {
9
9
  resource?: string;
10
10
  tags?: string[];
11
11
  timestamp?: string;
12
+ superseded_by?: string;
12
13
  [key: string]: OkfFrontmatterValue | undefined;
13
14
  }
14
15
  interface OkfIndexEntry {
@@ -65,50 +66,118 @@ interface OkfFootnote {
65
66
  }
66
67
 
67
68
  declare function serializeActorString(value: string): string;
68
- declare function serializeFrontmatter(fm: OkfFrontmatter): string;
69
- /**
70
- * `depth` counts levels of **mapping-value nesting** only: it increments when a
71
- * flow MAPPING's key has a value that is itself a flow collection, and is
72
- * rejected once it would exceed 1 (spec §2.6's "at most one level of nesting").
73
- * It does NOT increment when a flow SEQUENCE's item is itself a mapping or
74
- * sequence — `sources: [ {...}, {...} ]` (a flat array of objects) is the base
75
- * shape v0.2 uses everywhere and is not "nesting" in the spec's sense. This is
76
- * what lets `sources: [ { usage_window: { from, to } } ]` parse (sequence item
77
- * is a mapping at depth 0; that mapping's `usage_window` value is a mapping at
78
- * depth 1 — one level, allowed) while `{ a: { b: { c: 1 } } }` is rejected
79
- * (`a`'s value nests at depth 1; `b`'s value would need depth 2 — rejected).
80
- */
69
+ /** Options for {@link serializeFrontmatter} (§A7). */
70
+ interface SerializeFrontmatterOptions {
71
+ /**
72
+ * Verbatim lines §A3(d) handed back for one key each, written after the
73
+ * serialized keys and before the closing `---`, unchanged. The serializer
74
+ * does not parse or validate them; callers pass only blocks they stored
75
+ * from a parse. Without the option the output is byte-identical to the
76
+ * pre-v0.3 serializer's.
77
+ */
78
+ rawBlocks?: string[];
79
+ }
80
+ declare function serializeFrontmatter(fm: OkfFrontmatter, options?: SerializeFrontmatterOptions): string;
81
81
  declare function parseFlowMapping(text: string, depth?: number): Record<string, OkfFrontmatterValue> | null;
82
82
  declare function parseFlowSequence(text: string, depth?: number): OkfFrontmatterValue[] | null;
83
+ /**
84
+ * A key the parser could not represent, reported by key and reason slug —
85
+ * never by content (spec §A3d). The reason is one of: `nested_too_deep`
86
+ * (block structure beyond the representable level), `block_scalar` (a value
87
+ * introduced by `|` or `>`), `multiline_scalar` (an inline scalar continued
88
+ * on indented lines), `malformed` (a flow collection left open or rejected
89
+ * for depth or syntax), or `anchor_or_alias` (a `&`/`*` at a node start).
90
+ */
91
+ interface FrontmatterIssue {
92
+ key: string;
93
+ reason: string;
94
+ }
83
95
  /**
84
96
  * Parses the subset of YAML frontmatter that {@link serializeFrontmatter} produces:
85
- * scalar string/number/boolean/null values, quoted strings, and block string-lists.
86
- * NOT a general YAML parser — flow collections (`[...]`/`{...}`), multi-line block
87
- * scalars (`|`/`>`), anchors, and aliases are not recognized. Lines that don't match
88
- * a recognized shape are silently skipped rather than throwing, so a foreign bundle
89
- * never crashes
90
- * the import — it just loses fidelity on the unrecognized line.
97
+ * scalar string/number/boolean/null values, quoted strings, block string-lists,
98
+ * and one bounded level of block structure (§A3c): a top-level key with no inline
99
+ * value opens a block mapping (indented `k: v` at one indentation) or a block
100
+ * list (`- ` items that are scalars, single-line flow collections, or `- k: v`
101
+ * with aligned continuations). NOT a general YAML parser. The grammar is the
102
+ * §A3 subset: full-line comments are skipped; a `#` after whitespace outside
103
+ * quotes starts a comment (a URL fragment and a `#` inside quotes are data); a
104
+ * line starting with whitespace is never a top-level key; inside a block a key
105
+ * is separated from its value by a colon + space or the end of the line;
106
+ * anchors/aliases (`&`/`*` at a node start) are rejected.
107
+ *
108
+ * Lines whose value the parser cannot represent are handed back unparsed in
109
+ * `raw` (the key's lines verbatim, key line included, trailing blanks excluded)
110
+ * with an entry in `issues`, and the key is absent from `frontmatter` — no key
111
+ * is ever stored as `null`. An anchor/alias key goes to `issues` only, never to
112
+ * `raw` (we must not become an emitter of anchors). An inexact top-level plain
113
+ * scalar (`1.10`, `1234567890123456789`, `True`, `~` — its serialized form
114
+ * differs from its source text) is parsed as today AND its line is placed in
115
+ * `raw` with no issue (§A3f; the check is a top-level one by rule). Block
116
+ * content the grammar cannot represent — a block scalar, a second indentation
117
+ * level, a block line whose flow value nests past the serializer's bound, a
118
+ * multi-line flow — comes back in `raw` with the §A3d reason slug
119
+ * (`block_scalar`, `nested_too_deep`, `malformed`, `multiline_scalar`).
120
+ * Unrecognized lines that match no key at all are still skipped rather than
121
+ * throwing, so a foreign bundle never crashes the import.
91
122
  */
92
123
  declare function parseFrontmatter(content: string): {
93
124
  frontmatter: OkfFrontmatter;
94
125
  rest: string;
126
+ raw: Record<string, string>;
127
+ issues: FrontmatterIssue[];
95
128
  };
96
129
 
97
- declare function buildConceptDocument(fm: OkfFrontmatter, body: string): string;
98
- /** Reverse of {@link buildConceptDocument}: strips the single leading blank-line separator. */
130
+ declare function buildConceptDocument(fm: OkfFrontmatter, body: string, options?: SerializeFrontmatterOptions): string;
131
+ /**
132
+ * Reverse of {@link buildConceptDocument}: strips the single leading blank-line
133
+ * separator. The raw lines (`raw`) and issue entries (`issues`) the §A3 parser
134
+ * handed back are passed through beside `frontmatter` — core decides what to
135
+ * do with each (keep the raw lines of an unmapped key, drop and report a
136
+ * mapped one, spec §A3d/§B8).
137
+ */
99
138
  declare function parseConcept(content: string): {
100
139
  frontmatter: OkfFrontmatter;
101
140
  body: string;
141
+ raw: Record<string, string>;
142
+ issues: ReturnType<typeof parseFrontmatter>['issues'];
102
143
  };
103
144
 
104
145
  declare function buildIndexMd(sections: OkfIndexSection[]): string;
105
- declare function buildRootIndexMd(okfVersion: string, sections: OkfIndexSection[], options?: {
146
+ /** Options for {@link buildRootIndexMd} (§A5). */
147
+ interface BuildRootIndexMdOptions {
106
148
  profile?: string;
107
- }): string;
108
- declare function parseRootIndexMd(content: string): {
149
+ /**
150
+ * Root-index metadata, emitted verbatim after `okf_version` and `profile`.
151
+ * Both of those keys are builder-owned and never taken from this record.
152
+ * The reserved `x-llm-wiki-raw` slot (a `Record<key, lines>`) is written
153
+ * only when `profile` is `llm-wiki/3` — its string entries go out verbatim
154
+ * after the metadata keys — and is stripped on `llm-wiki/1` and `/2`, so it
155
+ * can never serialize as a literal YAML key on older profiles (owner note 1).
156
+ */
157
+ bundleMetadata?: Record<string, OkfFrontmatterValue>;
158
+ /**
159
+ * Verbatim lines handed back by a parse (§A7), written after the metadata
160
+ * keys and before the closing `---`. Callers pass only blocks they stored
161
+ * from a parse.
162
+ */
163
+ rawBlocks?: string[];
164
+ }
165
+ declare function buildRootIndexMd(okfVersion: string, sections: OkfIndexSection[], options?: BuildRootIndexMdOptions): string;
166
+ /** Result of {@link parseRootIndexMd} (§A5, refinement 44). */
167
+ interface ParsedRootIndexMd {
109
168
  okf_version?: string;
110
169
  profile?: string;
111
- };
170
+ /** Every frontmatter key except `okf_version`/`profile` and the
171
+ * parser-synthesized empty `type`. A root key handed back unparsed keeps
172
+ * its verbatim lines under the reserved `x-llm-wiki-raw` slot; an anchor
173
+ * is dropped and reported in `issues` only. */
174
+ metadata: Record<string, OkfFrontmatterValue>;
175
+ /** The parse's raw handback record (§A3d/§A3f), key line included. */
176
+ raw: Record<string, string>;
177
+ /** What the parse dropped or could not read, by key and reason slug. */
178
+ issues: FrontmatterIssue[];
179
+ }
180
+ declare function parseRootIndexMd(content: string): ParsedRootIndexMd;
112
181
 
113
182
  declare function buildEntityIndexMd(options: {
114
183
  summary?: string;
@@ -197,9 +266,42 @@ declare function extractFootnotes(body: string): OkfFootnote[];
197
266
  declare function serializeFootnotes(footnotes: OkfFootnote[]): string;
198
267
 
199
268
  type TrustTier = 'unverified' | 'machine-confirmed' | 'human-reviewed';
200
- declare function deriveTrustTier(verified: OkfVerified | undefined): TrustTier;
269
+ /**
270
+ * OKF v0.3 §5.3 trust currency (spec okf-v03 §A2, ruling R2). Optional second
271
+ * parameter `generatedAt`: epoch ms or a parseable timestamp string. When
272
+ * provided, only events whose parsed `at >= generatedAt` are current (an
273
+ * event whose `at` does not parse is ignored for tiering, kept as history);
274
+ * a `generatedAt` that is present but unreadable means NO event is current.
275
+ * When no event is current the tier is 'unverified' — it can only go down.
276
+ * When `generatedAt` is absent, all events are current, which
277
+ * reproduces the v0.2 sticky rule exactly (R2). §11.2 rule 2: `verified` may
278
+ * be a bare mapping.
279
+ */
280
+ declare function deriveTrustTier(verified: OkfVerified | OkfVerifiedEntry | undefined, generatedAt?: number | string | null): TrustTier;
281
+ /**
282
+ * OKF v0.3 §5.5: a consumer MUST NOT treat a `stale_after` value that it
283
+ * cannot parse as fresh. `null`/`undefined`, an empty or whitespace-only
284
+ * string are absent — not stale (§11.2 rule 1). A finite number, or a string
285
+ * `parseOkfTimestamp` accepts, compares as `now >= cutoff`. Anything else —
286
+ * a string that does not parse, a non-finite number — is STALE.
287
+ */
201
288
  declare function isStaleAfter(staleAfter: string | number | null, now: number): boolean;
202
289
  declare function parseVerifiedFlexible(value: unknown): OkfVerified;
290
+ /**
291
+ * OKF v0.3 §A4 (spec okf-v03, refinement 26): mirror of
292
+ * `parseVerifiedFlexible`. §11.2 rule 2: `sources` may be a bare mapping —
293
+ * one item ⇒ a list with one item. An entry is kept only when it is a
294
+ * mapping whose REQUIRED `resource` is a non-empty string (§5.1, §11.2
295
+ * rule 3); every other entry is ignored and counted in `ignored` so a
296
+ * consumer can report the count (§B13 `sources_entry_invalid`). `null`,
297
+ * `undefined` and unrecognizable shapes are absent — nothing to keep,
298
+ * nothing to count. The kept entries are the caller's own objects,
299
+ * unmodified (extras ride along; refinement 47).
300
+ */
301
+ declare function parseSourcesFlexible(value: unknown): {
302
+ sources: OkfSource[];
303
+ ignored: number;
304
+ };
203
305
  declare function formatVerifiedJson(entries: OkfVerified): string;
204
306
  declare function formatSourcesJson(sources: OkfSource[], sharedWindow?: OkfSourceUsageWindow | null): string;
205
307
  declare function latestVerified(entries: OkfVerified | undefined, _now: number): {
@@ -208,5 +310,17 @@ declare function latestVerified(entries: OkfVerified | undefined, _now: number):
208
310
  } | null;
209
311
  declare function parseCitationsList(body: string): string[];
210
312
 
211
- export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, createEventIdDeriver, deriveEventId, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseRootIndexMd, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
212
- export type { DeriveEventIdInput, OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, TrustTier };
313
+ /**
314
+ * OKF v0.3 timestamp helpers (spec okf-v03 §A1; upstream §5, §13.2).
315
+ *
316
+ * v0.3 §5 accepts exactly two forms: an RFC 3339 date-time WITH an explicit
317
+ * offset, or a date-only `YYYY-MM-DD` (meaning start of that day, UTC). An
318
+ * offset-less date-time (`2026-01-01T00:00:00`) stays invalid — §13.2. Our
319
+ * YAML layer is 1.2 core schema, so quoted and native scalars both arrive
320
+ * here as strings; finite numbers (epoch ms, the stored column form) pass
321
+ * through. Returns epoch ms, or null when unparseable.
322
+ */
323
+ declare function parseOkfTimestamp(value: unknown): number | null;
324
+
325
+ export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, createEventIdDeriver, deriveEventId, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseOkfTimestamp, parseRootIndexMd, parseSourcesFlexible, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
326
+ export type { BuildRootIndexMdOptions, DeriveEventIdInput, FrontmatterIssue, OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, ParsedRootIndexMd, SerializeFrontmatterOptions, TrustTier };