Agentic โฑ 8 min read ๐Ÿ“ examples/13_knowledge_corpus.mjs

Lesson 34: A corpus that becomes a wiki, and survives an edit

one corpus โ†’ surfaces โ†’ wiki projection โ†’ adopted ontology โ†’ a cache that survives an edit

1. Why This Feature Exists

A documentation folder is the cheapest knowledge base a team already has. This example turns five markdown pages, one memory fact and one todo into a single graph, renders the wiki a human reads, and then edits the world under it โ€” twice โ€” to show what is derived, what is adopted, and what survives:

2. What the Example Demonstrates

A โ€” SURFACES
wikiSurface() imports the folder as section nodes plus references edges (a dangling [[link]] is data, not an error), and the read-only memory / todos adapters merge in through surfaceFragment() + mergeFragment(). Nothing is written back.
B โ€” RENDER
renderWiki() writes one page per node; the index carries graph-sha256, which is how staleness is proved.
C โ€” ROUTE
a model, given only the rendered map, routes a 429 question to one page and answers from it.
D โ€” STALE
any edit after a render is a wiki-stale finding; only a re-render clears it.
E โ€” ONTOLOGY
profileSources() measures the corpus, proposeOntology() proposes kinds and relations with the evidence for each, adoption is versioned and compare-and-swapped, and the graph is rebuilt inside it.
F โ€” CACHE
syncCorpus() writes manifest.json with an explicit buildVersion; an unchanged corpus costs nothing, and an edited file rebuilds only the mechanical layer while the model's nodes are re-anchored by content key.
G โ€” ORPHAN
remove the paragraph a node was derived from and the node is dropped โ€” provenance that no longer resolves is not kept. What this shows: - one store, two projections: the wiki is a view, not a second database; - read-only surfaces: memory and todos are indexed without being written; - an ontology with evidence, adopted as a versioned, swappable act; - a delta rebuild that treats the agent's work as data. Run: node examples/13_knowledge_corpus.mjs Needs: PROVIDER plus its credentials in secret.env. @version 0.27.0
imported from 'merci-sdk'
defaultDisplay, initializeClient, loadCredentials, log

3. How the Core Call Works

The entry point, condensed. The full runnable file is linked below.

import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { defaultDisplay, initializeClient, loadCredentials, log } from 'merci-sdk';
import {
    adoptOntology, adoptedProfile, applyPatch, buildGraph, corpusKey, fingerprintSources, graphSha,
    loadCorpus, memorySurface, mergeFragment, ontologySha, profileSources, proposeOntology, readSources,
    readWikiSha, renderKnowledgeBlock, renderWiki, saveGraph, sourceDelta, surfaceFragment, syncCorpus,
    todosSurface, validateGraph, wikiSurface,
} from 'merci-sdk/knowledge';
import { extractAndParseJson } from 'merci-sdk/json';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';

const STATE_DIR = join(SCRATCH_DIR, '13-knowledge-corpus');
const CORPUS_DIR = join(STATE_DIR, 'corpus');
const STORE_DIR = join(STATE_DIR, 'store');
const CACHE_DIR = join(STATE_DIR, 'cache');

/** @type {Record<string, string>} */
const PAGES = {
    'getting_started.md': `# Getting started with Relay

Read when: you are wiring a new service into Relay for the first time.
Not for: production incidents; start at [[incidents]].

## Provisioning

You must request a project id before the first call.
The platform shall issue credentials within one working day.
If the project is new, the request must include a data classification.
Read [[retries]] before you send production traffic.
`,
    'retries.md': `# Retries and idempotency

Read when: a request failed and you are deciding whether to send it again.
Not for: quota planning; see [[rate_limits]].

## Safe retries

A retry is safe only if the request carries an idempotency key.
You must not retry a write that has no idempotency key.
The client shall back off for at least 200 ms before the second attempt.

Read the full example: examples/13_knowledge_corpus.mjs โ†’

4. Behavior the Code Fixes

one corpus โ†’ surfaces โ†’ wiki projection โ†’ adopted ontology โ†’ a cache that survives an edit

5. Run the Example

Execute the runnable example from the repository root:

node examples/13_knowledge_corpus.mjs
What it does: one corpus โ†’ surfaces โ†’ wiki projection โ†’ adopted ontology โ†’ a cache that survives an edit