@zosmaai/pi-llm-wiki 0.10.7 → 0.11.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.de.md +35 -4
  3. package/README.es.md +260 -170
  4. package/README.fr.md +35 -4
  5. package/README.hi.md +35 -4
  6. package/README.ja.md +35 -4
  7. package/README.ko.md +35 -4
  8. package/README.md +38 -3
  9. package/README.pt.md +35 -4
  10. package/README.ru.md +35 -4
  11. package/README.zh.md +260 -170
  12. package/assets/demo.gif +0 -0
  13. package/dist/extensions/llm-wiki/lib/bootstrap.js +71 -0
  14. package/dist/extensions/llm-wiki/lib/embeddings.js +401 -0
  15. package/dist/extensions/llm-wiki/lib/guardrails.js +232 -0
  16. package/dist/extensions/llm-wiki/lib/indexing.js +78 -0
  17. package/dist/extensions/llm-wiki/lib/ingest-worker.js +310 -0
  18. package/dist/extensions/llm-wiki/lib/inject.js +65 -0
  19. package/dist/extensions/llm-wiki/lib/knowledge-document.js +442 -0
  20. package/dist/extensions/llm-wiki/lib/knowledge-links.js +206 -0
  21. package/dist/extensions/llm-wiki/lib/legacy-repair.js +443 -0
  22. package/dist/extensions/llm-wiki/lib/metadata.js +499 -0
  23. package/dist/extensions/llm-wiki/lib/model-command.js +86 -0
  24. package/dist/extensions/llm-wiki/lib/observation.js +283 -0
  25. package/dist/extensions/llm-wiki/lib/recall.js +875 -0
  26. package/dist/extensions/llm-wiki/lib/retro.js +158 -0
  27. package/dist/extensions/llm-wiki/lib/runtime.js +191 -0
  28. package/dist/extensions/llm-wiki/lib/source-extractors.js +426 -0
  29. package/dist/extensions/llm-wiki/lib/source-packet.js +229 -0
  30. package/dist/extensions/llm-wiki/lib/subagent.js +41 -0
  31. package/dist/extensions/llm-wiki/lib/task-config.js +172 -0
  32. package/dist/extensions/llm-wiki/lib/tools.js +1192 -0
  33. package/dist/extensions/llm-wiki/lib/trajectories-command.js +51 -0
  34. package/dist/extensions/llm-wiki/lib/trajectory.js +467 -0
  35. package/dist/extensions/llm-wiki/lib/utils.js +347 -0
  36. package/dist/extensions/llm-wiki/lib/vault-format.js +247 -0
  37. package/dist/extensions/llm-wiki/lib/visible-status.js +31 -0
  38. package/dist/extensions/llm-wiki/lib/wiki-service.js +128 -0
  39. package/dist/mcp/exec.js +121 -0
  40. package/dist/mcp/index.js +229 -0
  41. package/dist/mcp/operations.js +130 -0
  42. package/dist/package.json +1 -0
  43. package/docs/superpowers/plans/2026-08-02-okf-foundation.md +1579 -0
  44. package/docs/superpowers/plans/2026-08-03-okf-foundation-remediation.md +3005 -0
  45. package/docs/superpowers/plans/2026-08-06-okf-foundation-release-remediation.md +1174 -0
  46. package/docs/superpowers/specs/2026-08-02-okf-foundation-design.md +578 -0
  47. package/docs/superpowers/specs/2026-08-02-okf-v0.2-interoperability-design.md +538 -0
  48. package/extensions/llm-wiki/index.ts +22 -36
  49. package/extensions/llm-wiki/lib/bootstrap.ts +84 -0
  50. package/extensions/llm-wiki/lib/embeddings.ts +9 -3
  51. package/extensions/llm-wiki/lib/guardrails.ts +174 -29
  52. package/extensions/llm-wiki/lib/indexing.ts +2 -1
  53. package/extensions/llm-wiki/lib/ingest-worker.ts +170 -29
  54. package/extensions/llm-wiki/lib/knowledge-document.ts +661 -0
  55. package/extensions/llm-wiki/lib/knowledge-links.ts +282 -0
  56. package/extensions/llm-wiki/lib/legacy-repair.ts +572 -0
  57. package/extensions/llm-wiki/lib/metadata.ts +531 -116
  58. package/extensions/llm-wiki/lib/observation.ts +37 -43
  59. package/extensions/llm-wiki/lib/recall.ts +61 -33
  60. package/extensions/llm-wiki/lib/retro.ts +65 -41
  61. package/extensions/llm-wiki/lib/source-extractors.ts +12 -17
  62. package/extensions/llm-wiki/lib/source-packet.ts +44 -31
  63. package/extensions/llm-wiki/lib/tools.ts +406 -348
  64. package/extensions/llm-wiki/lib/trajectory.ts +15 -1
  65. package/extensions/llm-wiki/lib/utils.ts +121 -130
  66. package/extensions/llm-wiki/lib/vault-format.ts +363 -0
  67. package/extensions/llm-wiki/lib/wiki-service.ts +183 -0
  68. package/mcp/exec.ts +122 -0
  69. package/mcp/index.ts +60 -250
  70. package/mcp/operations.ts +176 -0
  71. package/package.json +8 -2
  72. package/scripts/migrate-llm-wiki.js +801 -0
  73. package/skills/llm-wiki/SKILL.md +8 -6
@@ -0,0 +1,578 @@
1
+ # OKF Foundation Design
2
+
3
+ **Status:** Ready for fresh-context spec review
4
+
5
+ **Date:** 2026-08-02
6
+
7
+ **Parent:** [OKF v0.2 Interoperability Design](./2026-08-02-okf-v0.2-interoperability-design.md)
8
+
9
+ ## Purpose
10
+
11
+ This child spec defines the first implementation phase of OKF support in `@zosmaai/pi-llm-wiki`: one shared document model, safe YAML parsing, dual legacy/OKF reading, OKF-canonical new documents, explicit vault mode, standard Markdown links, and deterministic OKF indexes and logs.
12
+
13
+ This spec is normative for Foundation. The parent design is non-normative.
14
+
15
+ ## Scope
16
+
17
+ Foundation includes:
18
+
19
+ - a shared parser and serializer for legacy and OKF v0.2 frontmatter
20
+ - preservation of unknown producer fields
21
+ - explicit vault format mode
22
+ - dual-read behavior in every vault mode
23
+ - OKF-canonical output from all page producers
24
+ - support for Markdown links and legacy wikilinks
25
+ - deterministic `wiki/index.md` and `wiki/log.md` in OKF mode
26
+ - continued `meta/index.md` and `meta/log.md` generation
27
+ - integration with registry, backlinks, embeddings, recall readers, Pi tools, and MCP readers
28
+ - conformance fixtures for the shared format layer
29
+
30
+ Foundation excludes:
31
+
32
+ - migration commands or mode changes for existing vaults
33
+ - OKF import, review staging, transactions, and export
34
+ - trust-based recall scoring
35
+ - expanded OKF lint rules beyond parser/projection diagnostics needed by Foundation
36
+ - execution of Attested Computation documents
37
+
38
+ Those requirements belong to later child specs.
39
+
40
+ ## Design Principles
41
+
42
+ 1. **One parser, not legacy and OKF parser chains.** Legacy and OKF pages share Markdown-plus-YAML syntax. The shared parser represents both; compatibility classification happens after parsing.
43
+ 2. **Mode controls bundle behavior, not readability.** Every mode reads both legacy and OKF-shaped pages. Mode controls reserved files and generated projections.
44
+ 3. **Missing metadata does not become invented metadata.** Foundation never fabricates authorship, verification, provenance, or timestamps it cannot know.
45
+ 4. **Unknown data survives rewrites.** Unknown ordinary YAML fields remain semantically equivalent after parse and serialize.
46
+ 5. **Generated files are projections.** Registry, backlinks, indexes, and logs derive from authoritative pages and events.
47
+ 6. **Invalid explicit configuration fails closed.** Unknown mode and OKF version values never silently downgrade to legacy behavior.
48
+
49
+ ## Vault Mode
50
+
51
+ `config.json` gains one field:
52
+
53
+ ```json
54
+ {
55
+ "knowledge_format": "legacy"
56
+ }
57
+ ```
58
+
59
+ Allowed values:
60
+
61
+ - `legacy`
62
+ - `okf-0.2`
63
+
64
+ ### Resolution rules
65
+
66
+ | Vault state | Effective mode | Behavior |
67
+ |---|---|---|
68
+ | Existing config lacks `knowledge_format` | `legacy` | Preserve current reserved-file behavior |
69
+ | New vault bootstrap | `okf-0.2` | Create OKF root index and log |
70
+ | Config explicitly says `legacy` | `legacy` | Dual-read; no generated files under `wiki/` |
71
+ | Config explicitly says `okf-0.2` | `okf-0.2` | Dual-read; generate OKF reserved files |
72
+ | Config contains another value or non-string | Error | Do not rebuild or write wiki metadata |
73
+
74
+ Missing mode means legacy only for backward compatibility with an existing vault. New bootstrap always persists `okf-0.2`; it never relies on the missing-field fallback.
75
+
76
+ Foundation does not change an existing vault's mode. The later Interchange migration spec owns mode activation and the `legacy` → `okf-0.2` transition.
77
+
78
+ ### Compatibility promise
79
+
80
+ In both modes, every newly created concept path uses the canonical OKF shape defined here. Ordinary tool updates to an existing legacy document preserve its existing frontmatter shapes, including scalar or string-list `sources`; they parse, patch only the requested values, and serialize with legacy-shape preservation enabled. Foundation never converts an existing legacy document merely because a tool touched it. Canonical conversion of existing pages belongs exclusively to explicit migration in the later Interchange spec.
81
+
82
+ A user or model may intentionally replace an entire file outside these service operations. The next rebuild parses the resulting file as written but does not infer that a migration occurred.
83
+
84
+ Legacy mode preserves upgraded pi-llm-wiki behavior, not downgrade compatibility with older package versions whose parser cannot understand nested OKF v0.2 metadata.
85
+
86
+ In legacy mode:
87
+
88
+ - `meta/index.md`, `meta/log.md`, registry, backlinks, and embeddings continue to update.
89
+ - `wiki/index.md` and `wiki/log.md` are not created or rewritten.
90
+ - Existing user-owned files named `wiki/index.md` or `wiki/log.md` are not treated as generated Foundation projections.
91
+
92
+ In OKF mode:
93
+
94
+ - `wiki/index.md` and `wiki/log.md` are generated projections.
95
+ - Tool-call guardrails reject direct model edits to those reserved generated files.
96
+ - `meta/**` projections continue to exist for efficient internal lookup and backward-compatible tooling.
97
+
98
+ ## Shared Knowledge Document Model
99
+
100
+ The shared model represents:
101
+
102
+ - canonical concept ID
103
+ - source file path
104
+ - standard OKF fields
105
+ - pi-llm-wiki extension fields
106
+ - unknown producer fields
107
+ - compatibility diagnostics
108
+ - Markdown body
109
+
110
+ Conceptual shape:
111
+
112
+ ```text
113
+ KnowledgeDocument
114
+ id: bundle-relative path without .md
115
+ path: bundle-relative .md path
116
+ frontmatter:
117
+ type
118
+ title?
119
+ description?
120
+ resource?
121
+ tags?
122
+ sources?
123
+ generated?
124
+ verified?
125
+ status?
126
+ stale_after?
127
+ category?
128
+ domain?
129
+ aliases?
130
+ recall_triggers?
131
+ extensions: unknown fields
132
+ body
133
+ compatibility: legacy shapes detected during parsing
134
+ ```
135
+
136
+ The implementation may use smaller nested types. It must not expose YAML-library objects to consumers.
137
+
138
+ ### Concept identity
139
+
140
+ - A concept ID is the NFC-normalized, bundle-relative file path without the final `.md` suffix.
141
+ - `/` is the internal path separator on every platform.
142
+ - `index.md` and `log.md`, compared case-insensitively, are reserved and never concept documents.
143
+ - Display title never determines identity.
144
+ - New concept paths use existing Unicode-aware slug generation and must not produce reserved names.
145
+ - A scan that finds two paths colliding after NFC normalization or target-filesystem case folding returns a diagnostic and does not replace generated metadata with a partial registry.
146
+
147
+ Foundation does not rename existing paths. Import and migration path changes belong to Interchange.
148
+
149
+ ## YAML Parsing and Serialization
150
+
151
+ The current scalar/list parser cannot represent OKF v0.2 nested provenance and trust fields. Foundation replaces it with a maintained YAML parser behind the shared document API.
152
+
153
+ ### Parse requirements
154
+
155
+ - Require one frontmatter mapping delimited by `---` at the start of a concept document.
156
+ - Accept LF and CRLF input.
157
+ - Reject duplicate mapping keys.
158
+ - Reject aliases and alias expansion.
159
+ - Reject custom tags.
160
+ - Reject multiple YAML documents.
161
+ - Reject nesting deeper than 32 mappings/sequences.
162
+ - Reject frontmatter larger than 128 KiB measured as UTF-8 bytes.
163
+ - Return structured diagnostics; never throw a YAML-library error through tool boundaries.
164
+ - Accept unknown ordinary scalar, sequence, and mapping values.
165
+ - Parse YAML timestamps as preserved scalar values rather than converting them into host-language date objects implicitly.
166
+
167
+ The selected parser configuration and tests must prove these restrictions. A new dependency is acceptable because implementing general nested YAML safely in-house is out of scope.
168
+
169
+ ### Serialization requirements
170
+
171
+ - Emit UTF-8 and LF line endings.
172
+ - Emit the opening fence as `---\n`, YAML ending in one newline, and the closing fence as `---\n`.
173
+ - For a non-empty body, emit exactly one blank separator line after the closing fence, then the body with exactly one final newline.
174
+ - For an empty body, emit no blank separator line after the closing fence; the file ends with the closing fence's newline.
175
+ - The parser removes one optional blank separator line from the model body; additional leading blank lines belong to the body.
176
+ - Preserve unknown fields semantically.
177
+ - Preserve explicit empty lists and mappings.
178
+ - Do not promise preservation of comments, quote style, key order, scalar style, or blank-line formatting inside frontmatter.
179
+ - Never emit aliases, custom tags, or multiple YAML documents.
180
+ - Keep the Markdown body byte-equivalent except for normalizing the separator immediately after frontmatter and the final newline when the caller requested serialization.
181
+
182
+ A parse → serialize → parse round trip must preserve known and unknown YAML values semantically and preserve body text under the documented newline normalization.
183
+
184
+ ## Compatibility Classification
185
+
186
+ There is no fallback parser. The shared parser parses the YAML document once and records legacy shapes that later migration may convert.
187
+
188
+ Foundation recognizes, without rejecting:
189
+
190
+ - scalar or string-list legacy `sources`
191
+ - `created` and `updated`
192
+ - `summary`
193
+ - `raw_path` and `source_id`
194
+ - existing lowercase pi-llm-wiki type values
195
+ - legacy wikilinks in the body
196
+
197
+ Legacy shapes produce compatibility metadata, not conformance errors during ordinary reads. Foundation consumers can index them. The later migration and lint specs decide how to report or convert each shape.
198
+
199
+ A malformed frontmatter block is a parse error. It is never retried as plain Markdown or silently interpreted as legacy.
200
+
201
+ ## OKF Version Resolution
202
+
203
+ Only the root `wiki/index.md` may declare bundle version metadata.
204
+
205
+ In `okf-0.2` mode:
206
+
207
+ - generated root index declares `okf_version: "0.2"`
208
+ - missing generated root index is repairable by metadata rebuild
209
+ - an existing root index declaring another version blocks projection writes until explicitly handled
210
+ - malformed version frontmatter blocks projection writes
211
+ - version mismatch does not prevent best-effort ordinary reads of parseable concept files; readers surface the version diagnostic alongside results
212
+
213
+ In legacy mode, root index version metadata does not activate OKF behavior. Explicit mode remains source of truth. If a legacy-mode root index declares an unsupported version, tools report the mismatch but do not change mode or rewrite that file.
214
+
215
+ ## Canonical New Document Output
216
+
217
+ All page-producing flows use the shared serializer:
218
+
219
+ - source capture and ingestion
220
+ - entity/concept/synthesis/analysis creation
221
+ - observations and retrospectives
222
+ - requirements
223
+ - trajectories, cases, and skills when enabled
224
+ - `wiki_ensure_page`
225
+
226
+ Required output:
227
+
228
+ - non-empty `type`
229
+ - standard YAML frontmatter
230
+ - standard Markdown body
231
+
232
+ Recommended fields are emitted only when known. Foundation does not invent `generated`, `verified`, `sources`, `resource`, or `stale_after`.
233
+
234
+ For a new document, `sources` uses the OKF v0.2 sequence-of-mappings shape. For an existing legacy document, scalar and string-list `sources` values remain unchanged during ordinary field patches. The shared model records those values separately from canonical structured sources so no caller can accidentally reinterpret a string as a structured provenance entry.
235
+
236
+ Existing pi-llm-wiki extensions remain optional top-level fields:
237
+
238
+ - `category`
239
+ - `domain`
240
+ - `aliases`
241
+ - `recall_triggers`
242
+ - `created`
243
+ - `updated`
244
+ - source-packet identifiers needed by existing workflows
245
+
246
+ For new synthesized documents, `description` is the canonical one-sentence preview field. Existing `summary` may remain when it carries longer content or compatibility value.
247
+
248
+ ## Link Support
249
+
250
+ Foundation recognizes two internal-link syntaxes:
251
+
252
+ 1. standard Markdown links
253
+ 2. existing folder-qualified wikilinks
254
+
255
+ ### Markdown links
256
+
257
+ Link extraction follows CommonMark 0.31.2 parsing rules. A CommonMark-compatible AST parser, not a regular expression over raw Markdown, identifies links.
258
+
259
+ Backlink-producing nodes include inline links and resolved full, collapsed, and shortcut reference links. Images, image references, autolinks, raw HTML links, footnote references, and bare URLs do not produce backlinks. Link-like text inside inline code, fenced code blocks, indented code blocks, or escaped Markdown remains text. A reference definition produces an edge only when a link node uses it. GFM tables and task lists may be accepted as body syntax but add no separate link semantics.
260
+
261
+ Supported concept targets:
262
+
263
+ - bundle-root-relative: `/concepts/retrieval.md`
264
+ - file-relative: `../concepts/retrieval.md`
265
+ - same-directory relative: `other.md` or `./other.md`
266
+
267
+ Resolution:
268
+
269
+ - strip query and fragment before concept resolution
270
+ - percent-decode each path segment once
271
+ - normalize separators and dot segments
272
+ - reject a target that escapes bundle root
273
+ - normalize target identity to NFC
274
+ - ignore external URI schemes for backlink purposes
275
+
276
+ New generated knowledge uses Markdown links. Bundle-root-relative links are preferred in concept bodies. Generated directory indexes use links relative to their directory.
277
+
278
+ ### Wikilinks
279
+
280
+ Existing forms remain readable:
281
+
282
+ ```text
283
+ [[concepts/retrieval]]
284
+ [[concepts/retrieval|RAG]]
285
+ ```
286
+
287
+ Wikilinks continue to participate in registry backlinks and recall metadata. Foundation does not rewrite them; migration owns conversion.
288
+
289
+ ### Backlinks
290
+
291
+ Backlinks combine resolved Markdown links and resolved wikilinks, deduplicated by `(source concept ID, target concept ID)`. Unresolved links produce diagnostics but do not abort ordinary page reading. Generated metadata stores only links whose targets resolve to a known concept.
292
+
293
+ ## Deterministic Directory Indexes
294
+
295
+ Directory indexes exist only in `okf-0.2` mode. The root and every directory containing a concept directly or transitively receive `index.md`.
296
+
297
+ ### Root template
298
+
299
+ ```markdown
300
+ ---
301
+ okf_version: "0.2"
302
+ ---
303
+
304
+ # <vault name>
305
+
306
+ ## Directories
307
+
308
+ - [concepts/](concepts/)
309
+
310
+ ## Concepts
311
+
312
+ - [Welcome](welcome.md) — Entry point for the knowledge bundle.
313
+ ```
314
+
315
+ ### Subdirectory template
316
+
317
+ ```markdown
318
+ # concepts
319
+
320
+ ## Directories
321
+
322
+ - [architecture/](architecture/)
323
+
324
+ ## Concepts
325
+
326
+ - [Retrieval-Augmented Generation](retrieval-augmented-generation.md) — Grounds generation using retrieved evidence.
327
+ ```
328
+
329
+ ### Generation rules
330
+
331
+ - Root index frontmatter contains only `okf_version: "0.2"`. Foundation does not emit `profile` or another producer key there because OKF v0.2 is ambiguous about additional root-index frontmatter keys. The pi-llm-wiki profile remains documentation, not a bundle-level conformance claim, until OKF defines a portable profile-discovery mechanism.
332
+ - Root H1 is `config.name`, falling back to `Wiki` when absent or empty.
333
+ - Subdirectory H1 is the literal final directory segment.
334
+ - Omit `## Directories` when there are no immediate child directories.
335
+ - Omit `## Concepts` when there are no direct concepts.
336
+ - List immediate child directories before direct concepts.
337
+ - Sort both groups by NFC-normalized relative path using Unicode code-point order, independent of process locale.
338
+ - Concept title is `title` when non-empty, otherwise the final concept-ID segment.
339
+ - Include ` — <description>` only when `description` is a non-empty single-line string.
340
+ - Collapse description line breaks and repeated whitespace to one space.
341
+ - Escape Markdown link labels for backslash, `[` and `]`.
342
+ - Encode each link path segment for use as a relative URI while preserving `/` separators.
343
+ - Emit LF line endings and exactly one final newline.
344
+ - Include no generated timestamp, page count, or other nondeterministic value.
345
+ - Prune generated subdirectory indexes whose directories no longer contain concepts directly or transitively.
346
+
347
+ An index is a projection. Users and importers cannot use it to hide a concept from scanning; concept discovery scans eligible files directly.
348
+
349
+ ## Deterministic Root Log
350
+
351
+ `meta/events.jsonl` remains the authoritative append-only event source in both modes. `wiki/log.md` is a generated OKF projection only in `okf-0.2` mode. Foundation generates no per-directory logs.
352
+
353
+ ### Authoritative event shape
354
+
355
+ Each valid JSONL line must contain:
356
+
357
+ ```json
358
+ {
359
+ "timestamp": "2026-08-02T10:00:00.000Z",
360
+ "kind": "capture"
361
+ }
362
+ ```
363
+
364
+ Additional JSON-compatible fields are allowed. Event production is mode-independent: the same successful capture, creation, update, retro, observation, ingestion, and other tool-owned mutations append the same event in legacy and OKF modes. Event writers append only after the associated authoritative wiki mutation succeeds. A projection rebuild itself does not append an event. Manual file edits do not fabricate events because the extension cannot infer actor or intent safely.
365
+
366
+ ### Log template
367
+
368
+ ```markdown
369
+ # Wiki Update Log
370
+
371
+ ## 2026-08-02
372
+
373
+ - **capture**: {"source_id":"SRC-2026-08-02-001"}
374
+ - **bootstrap**: {"mode":"personal","topic":"AI Engineering"}
375
+ ```
376
+
377
+ ### Generation rules
378
+
379
+ - Parse events in source line order and retain each line number as sequence.
380
+ - A malformed JSON line, missing timestamp, invalid timestamp, or empty kind is a diagnostic and is omitted from the projection.
381
+ - Group valid events by UTC date derived from timestamp.
382
+ - Sort date groups newest first.
383
+ - Within a date, sort timestamp newest first; ties use source sequence newest first.
384
+ - Render `kind` as plain text inside `**...**`, escaping backslash, `*`, `_`, `[`, `]`, and collapsing line breaks to one space.
385
+ - Render all fields except `timestamp` and `kind` as canonical JSON with recursively sorted object keys, no insignificant whitespace, and JSON array order preserved.
386
+ - Omit the colon and details when no additional fields exist.
387
+ - Emit LF line endings and exactly one final newline.
388
+
389
+ `meta/log.md` may retain its existing richer internal rendering for backward compatibility. Both logs derive from the same events and neither is authoritative.
390
+
391
+ ## Projection Rebuild Semantics
392
+
393
+ A rebuild computes all outputs in memory before replacing any generated file.
394
+
395
+ If any concept has malformed frontmatter, normalized identity collision, or unsupported explicit mode/version:
396
+
397
+ - return a blocking diagnostic
398
+ - preserve the previous registry, backlinks, indexes, and logs
399
+ - do not publish a partial metadata generation
400
+
401
+ Unresolved links and malformed event lines are non-blocking projection diagnostics: valid concepts may still be indexed, and the previous semantics for reporting gaps remain available. The rebuild result reports every diagnostic.
402
+
403
+ On successful rebuild:
404
+
405
+ 1. write temporary projection files
406
+ 2. atomically rename each individual file
407
+ 3. update registry/backlinks before optional embedding refresh
408
+ 4. schedule embedding refresh only after metadata succeeds
409
+
410
+ Multi-file crash recovery is not introduced in Foundation because these outputs are derived and fully rebuildable. Interchange transactions apply only to authoritative concept changes.
411
+
412
+ ## Guardrails
413
+
414
+ In `okf-0.2` mode, direct model edits are blocked for:
415
+
416
+ - `.llm-wiki/wiki/index.md`
417
+ - every generated `.llm-wiki/wiki/**/index.md`
418
+ - `.llm-wiki/wiki/log.md`
419
+
420
+ The block message directs callers to `wiki_rebuild_meta` or the page-producing tool that owns the source mutation.
421
+
422
+ In legacy mode, Foundation does not newly claim ownership of `wiki/index.md` or `wiki/log.md` because they may be user files.
423
+
424
+ Existing protection for `raw/**` and `meta/**` remains unchanged.
425
+
426
+ ## Pi and MCP Integration
427
+
428
+ Foundation adds no separate business logic to command handlers or MCP handlers.
429
+
430
+ Shared service functions own:
431
+
432
+ - mode resolution
433
+ - parsing and serialization
434
+ - page discovery
435
+ - link resolution
436
+ - registry/backlink construction
437
+ - index/log projection
438
+
439
+ The current MCP surface has five operations; Foundation maps them exactly:
440
+
441
+ | MCP operation | Shared behavior required |
442
+ |---|---|
443
+ | `wiki_recall` | Uses shared mode resolution, registry entries, concept IDs, titles, descriptions, and page parsing. Interface-specific vault selection and rendering may differ from Pi. |
444
+ | `wiki_search` | Uses the same generated registry schema and type/title/extension metadata as Pi `wiki_search`. |
445
+ | `wiki_status` | Reports resolved `knowledge_format`, page counts, and blocking projection diagnostics from shared status data. |
446
+ | `wiki_retro` | Creates its source page through the same canonical new-document serializer as Pi `wiki_retro`. |
447
+ | `wiki_capture_source` | Creates its skeleton source page through the same canonical new-document serializer as the Pi capture tool. |
448
+
449
+ Foundation adds no MCP read operation. Behavioral parity means that, after selecting the same vault and invoking equivalent operations, Pi and MCP expose the same concept identity and parsed metadata and route writes through the same services. UI text, automatic context injection, and vault-selection transport remain interface-specific.
450
+
451
+ Parity is tested through shared service fixtures plus thin interface tests; MCP does not reimplement parsing, registry scoring fields, or page serialization.
452
+
453
+ ## Operation Table
454
+
455
+ | Operation | Mode/input | Authoritative state change | Generated result |
456
+ |---|---|---|---|
457
+ | Bootstrap new vault | No config | Persist `knowledge_format: okf-0.2`; create normal scaffolding | Build meta projections plus `wiki/index.md` and `wiki/log.md` |
458
+ | Open old vault | Mode field absent | None | Resolve as legacy |
459
+ | Create page in old vault | Legacy | Write one OKF-canonical concept and append the same mode-independent event after success | Rebuild only `meta/**`; do not create wiki reserved files |
460
+ | Create page in new vault | OKF 0.2 | Write one OKF-canonical concept and append the same mode-independent event after success | Rebuild `meta/**`, directory indexes, and root log |
461
+ | Manual valid page edit | Either | Existing file edit | End-of-turn rebuild appropriate to mode; no fabricated event |
462
+ | Rebuild with malformed page | Either | None | Return diagnostics; retain previous projections |
463
+ | Rebuild with unresolved link | Either | None | Publish valid projections and report unresolved link |
464
+ | Config has unknown mode | Invalid | None | Blocking mode diagnostic; no writes |
465
+ | OKF root declares unsupported version | OKF 0.2 | None | Blocking version diagnostic; no writes |
466
+ | Read unknown concept type | Either | None | Treat as generic concept and preserve type |
467
+
468
+ ## Diagnostics
469
+
470
+ Foundation uses stable codes:
471
+
472
+ - `config_invalid_knowledge_format`
473
+ - `frontmatter_missing`
474
+ - `frontmatter_parse_error`
475
+ - `frontmatter_duplicate_key`
476
+ - `frontmatter_alias_forbidden`
477
+ - `frontmatter_custom_tag_forbidden`
478
+ - `frontmatter_multiple_documents`
479
+ - `frontmatter_limit_bytes`
480
+ - `frontmatter_limit_depth`
481
+ - `concept_missing_type`
482
+ - `concept_identity_collision`
483
+ - `concept_reserved_name`
484
+ - `okf_version_mismatch`
485
+ - `link_path_escape`
486
+ - `link_unresolved`
487
+ - `event_invalid_json`
488
+ - `event_invalid_timestamp`
489
+ - `event_missing_kind`
490
+
491
+ Each diagnostic includes severity, code, file path, and message. Frontmatter diagnostics include line/column when provided safely by the parser.
492
+
493
+ ## Testing
494
+
495
+ ### Parser and serializer
496
+
497
+ - nested `sources`, `generated`, `verified`, and Attested Computation fields
498
+ - unknown nested producer fields
499
+ - CRLF input and LF output
500
+ - duplicate key, alias, custom tag, multiple document, byte-limit, and depth-limit rejection
501
+ - scalar timestamps remain scalar values
502
+ - parse/serialize semantic round trip
503
+ - exact non-empty-body separator and final newline
504
+ - exact empty-body output with no separator blank line
505
+ - documented handling of additional leading body blank lines
506
+
507
+ ### Mode behavior
508
+
509
+ - missing field resolves old vault to legacy
510
+ - new bootstrap persists OKF mode
511
+ - unknown or malformed explicit mode fails closed
512
+ - legacy mode never generates reserved wiki files
513
+ - OKF mode generates and protects reserved files
514
+ - OKF-canonical pages remain readable in legacy mode
515
+ - unsupported root version blocks OKF-mode rebuild
516
+
517
+ ### Identity and links
518
+
519
+ - Unicode NFC identity normalization
520
+ - case-fold and normalization collision detection
521
+ - reserved filename detection
522
+ - CommonMark inline and full/collapsed/shortcut reference links
523
+ - images, autolinks, HTML, footnotes, code spans/blocks, escapes, and unused reference definitions excluded from backlinks
524
+ - root-relative, file-relative, fragment, query, and percent-encoded Markdown links
525
+ - bundle escape rejection
526
+ - wikilink compatibility
527
+ - mixed-link backlink deduplication
528
+
529
+ ### Index fixtures
530
+
531
+ Golden fixtures cover:
532
+
533
+ - empty bundle
534
+ - root concepts only
535
+ - nested directories
536
+ - title/description fallback
537
+ - Unicode paths and labels
538
+ - Markdown escaping and URI encoding
539
+ - deterministic order independent of file enumeration order and locale
540
+ - pruning obsolete generated indexes
541
+
542
+ ### Log fixtures
543
+
544
+ Golden fixtures cover:
545
+
546
+ - empty event stream
547
+ - multiple UTC dates
548
+ - equal timestamps with sequence tie-break
549
+ - recursively sorted detail keys
550
+ - arrays preserving order
551
+ - malformed lines omitted with diagnostics
552
+ - deterministic output independent of object insertion order
553
+
554
+ ### Integration
555
+
556
+ - every page producer routes through shared serialization
557
+ - registry, backlinks, recall readers, embeddings, Pi tools, and MCP consume shared documents
558
+ - malformed authoritative input preserves previous projections
559
+ - unresolved links do not prevent valid projection publication
560
+ - existing tests remain green
561
+
562
+ ## Acceptance Criteria
563
+
564
+ Foundation is complete when:
565
+
566
+ 1. Every existing and new page producer uses the shared document API.
567
+ 2. Existing vaults open in legacy mode without creating `wiki/index.md` or `wiki/log.md`.
568
+ 3. New vaults declare `okf-0.2` and contain conformant deterministic reserved files.
569
+ 4. Nested OKF v0.2 metadata and unknown fields parse and round-trip semantically.
570
+ 5. Markdown links and legacy wikilinks both produce correct backlinks.
571
+ 6. Projection output matches golden fixtures byte-for-byte.
572
+ 7. Invalid explicit mode/version or malformed concepts cannot replace a known-good registry with partial metadata.
573
+ 8. Pi and MCP readers return equivalent shared-model results.
574
+ 9. Package tests, typecheck, lint, and coverage gates pass.
575
+
576
+ ## Planning Boundary
577
+
578
+ The implementation plan for Foundation must stop at these acceptance criteria. It must not add import, export, migration, transaction journals, trust weighting, graph UI, git snapshots, or expanded extraction adapters.