Let the LLM incrementally build and maintain a persistent Markdown knowledge base, based on Karpathy's LLM Wiki pattern。
Step 1: Locate the wiki root (do this first on every trigger)
Use the following rulestop-downto decide, stopping as soon as one matches:
| Condition | wiki root | Mode |
|---|---|---|
CWD or any parent directory contains SCHEMA.md + index.md |
that directory | code-repo |
CWD is ~/wiki/ or a subdirectory of it |
~/wiki/ |
personal |
CWD contains CLAUDE.md + index.md(and it is not the project source code's CLAUDE.md) |
CWD | personal |
| None of the above match, but the user is talking about a wiki operation | ~/wiki/(default) |
personal |
| The user explicitly says "create a new wiki" | Go to Step 4: New wiki onboarding | — |
Essential differences between the modes:
- personal: Tracks reading notes from diverse sources (articles / videos / PDFs), with five directory levels (
raw/sources/entities/concepts/syntheses/), and has reliability levels - code-repo: An architecture wiki that tracks the source code of a single open-source project, with three directory levels (
concepts/entities/changelog/, optionalguide/), and conclusions must include文件:行号
Step 2: Read the schema to get all rules (once)
On the first wiki operation triggered in each new session,first readthe schema file of the current wiki root:
- personal mode → read
<wiki-root>/CLAUDE.md - code-repo mode → read
<wiki-root>/SCHEMA.md
The schema file is the sole authority— all page formats, tag systems, frontmatter fields, ingest steps, and lint check items are in it. This SKILL.md does not repeat the schema content, and only does routing.
If the schema file is missing, it means this wiki has not been correctly initialized → go to Step 4。
Step 3: Session startup protocol (execute on first trigger)
In order:
- Read the schema (see Step 2)
- Read
<wiki-root>/index.md— understand the overall wiki (do not read the full text, just understand what pages there are) - Read
<wiki-root>/log.mdthe last 10 entries — understand what was recently done - Read
<wiki-root>/inbox.md(if it exists) — see whether there are pending items - Report to the user in one sentence: "wiki status: N pages, most recent operation X, Y pending items"
For subsequent operations, as long as the wiki root does not change, do not read these again.
Step 4: New wiki onboarding
When the user, in adirectory with no existing wiki marker, says "create a wiki", "make an architecture wiki for X", or "make a new knowledge base":
4.1 Clarify the type
你这个 wiki 想做什么?
A) 个人知识库 —— 沉淀阅读笔记、跨领域观察(personal 模式)
B) 代码仓库 wiki —— 跟踪一个开源项目的源码架构(code-repo 模式)
4.2 Initialize from a template
This skill repository (by default at ~/code/lanshu-wiki-skill/) has two templates in the schema/ directory:
| Type | Template source | Target |
|---|---|---|
| personal | schema/wiki-personal-CLAUDE.md |
<wiki-root>/CLAUDE.md |
| code-repo | schema/wiki-code-repo-SCHEMA.md |
<wiki-root>/SCHEMA.md |
Execute (using personal as an example):
SKILL_ROOT="${SKILL_ROOT:-$HOME/code/lanshu-wiki-skill}"
WIKI_ROOT="$HOME/wiki" # 或用户指定的路径
# 建立五层目录骨架
mkdir -p "$WIKI_ROOT"/{raw,sources,entities,concepts,syntheses,reports,assets}
# 复制权威 schema
cp "$SKILL_ROOT/schema/wiki-personal-CLAUDE.md" "$WIKI_ROOT/CLAUDE.md"
# 初始化必备文件
cd "$WIKI_ROOT"
echo "# Wiki Index" > index.md
echo "# Wiki Log" > log.md
echo "# Wiki Inbox" > inbox.md
The same applies to code-repo mode: create the directory {concepts,entities,changelog} Three layers, schema replication wiki-code-repo-SCHEMA.md to <wiki-root>/SCHEMA.md。
4.3 Guide the user to fill in the ⚠️ fields in the template
After reading the template, search for the ⚠️ marker positions and ask the user item by item:
- personal: The "Tag Taxonomy" section — what are your domain tags? (provide 5 scenario templates to choose from)
- code-repo: The "Domain" and "Tag Taxonomy" sections — project name / commit hash / subsystem list / project-related tags
4.4 Prompt the user for the long-term workflow
- Subsequent
/wiki ingest <来源>to start filling in - For a code-repo wiki, it is recommended to do the 5 most core modules first before expanding
- Recommend the wiki repository
git initand push backups regularly
Command routing table
For all commands, thespecific steps are in the schema. This table only does the mapping.
| Command | Natural language trigger | What it does | Where to read the detailed rules |
|---|---|---|---|
/wiki add <来源> |
"add to wiki", "save" | Append to inbox.md, do not process immediately | The Ingest section of the schema |
/wiki ingest <来源> |
"ingest", "import" | Full ingest workflow | The Ingest section of the schema |
/wiki inbox |
"view to-dos" | List the contents of inbox.md, ask whether to ingest them one by one | — |
/wiki query <问题> |
"is there X in the wiki" | Read index → read relevant pages → synthesize answer → possibly archive as synthesis | The Query section of the schema |
/wiki lint |
"lint it" | Scan according to the schema Lint section, write reports/lint-report.md |
The Lint section of the schema |
/wiki status |
"wiki status" | See the status template below | — |
/wiki deprecate <页> |
"deprecate this page" | Mark archived + add deprecation note + remove from the normal list in index | The Deprecate section of the schema |
/wiki retract <来源> |
"retract this source" | Mark archived + scan all referencing pages to lower confidence | The Retract section of the schema |
/wiki merge <A> <B> |
"merge these two pages" | Merge content + globally replace links + archive the secondary page | The Merge section of the schema |
status command template
# 以已定位的 $WIKI_ROOT 为准(Step 1 决定)
echo "=== 页面统计 ==="
find "$WIKI_ROOT" -name "*.md" -not -path "*/\.*" -not -path "*/raw/*" -not -path "*/_archive/*" | wc -l
echo "=== 最近操作 ==="
grep "^## \[" "$WIKI_ROOT/log.md" 2>/dev/null | tail -3
echo "=== 待处理 ==="
[ -f "$WIKI_ROOT/inbox.md" ] && grep -c "^- \[ \]" "$WIKI_ROOT/inbox.md" || echo "(无 inbox)"
Hard rules that do not change across modes
Whether personal or code-repo, the following rules hold (they are also reiterated in the schema):
- The schema file is authoritative: when the schema conflicts with this SKILL.md, the schema prevails
- Conclusions must be traceable: personal uses source-ids, code-repo uses
文件:行号; if this cannot be done, it is better not to write it - Read before writing: before updating a page, you must read the full text; it is forbidden to modify after only looking at the index summary
- Deduplication first: before creating a new page, you must search index.md for whether the same concept already exists
- Append rather than overwrite:
raw/is immutable,log.mdis append-only, contradictions should be noted rather than resolved - Chinese content: the body text of wiki pages uses Chinese, frontmatter field names remain in English
- No cross-directory pollution: all paths are based on the wiki root located in Step 1, unrelated to CWD
For developers: extend more modes
schema/ The directory can contain more templates as needed (such as wiki-paper-research-SCHEMA.md for literature reviews,wiki-product-research-SCHEMA.md for product research). New templates require:
- Filename format
wiki-<scenario>-SCHEMA.mdorwiki-<scenario>-CLAUDE.md - Must have a "usage" description at the top
- Self-contained (does not depend on other schemas) — the agent must be able to get to work from a single read
After adding a new template, in Step 4.1 just add the corresponding option.