---
name: kb-compile
title: "KB Compile"
description: "Generate and update wiki articles from source summaries with backlinks and cross-references"
category: core
status: active
triggers:
  - "compile wiki"
  - "update articles"
  - "build wiki"
---

## Instructions

Compile source summaries into structured wiki articles. Incremental by default.

1. **Read context** — load _index.md, SCHEMA.md, existing articles, new sources since last compile
2. **Generate articles** — for each new source, determine wiki updates:
   - CREATE new entity/concept pages when a significant new subject appears
   - UPDATE existing pages when new information adds to them
   - Use correct subdirectory: `entities/` for people/orgs, `concepts/` for topics, `sources/` for per-source summaries
3. **Backlinks** — every article uses `[[slug]]` links to related articles
4. **Frontmatter** — every article has: title, entity_type, category, taxonomy_path, status, related, sources, author, dates
5. **Update index** — refresh _index.md with article counts and highlights

## Quality Rules

### Article Quality Threshold
An article must meet ALL of these to be created:
- At least 3 sentences of sourced content (not TODOs or placeholders)
- Clear subject that someone would search for
- At least 1 `[Source: filename]` citation
- If you can't write 3 real sentences about it from the sources, it's NOT worth an article

### Quantity Limits
- Maximum 20 new articles per compile pass
- Prefer fewer, richer articles over many thin ones
- UPDATE existing articles with new facts rather than creating near-duplicates

### When to CREATE vs UPDATE
| Situation | Action |
|-----------|--------|
| Entity/concept has existing page | UPDATE — append new facts |
| Entity/concept has no page, is significant | CREATE — full article |
| Entity/concept has no page, mentioned in passing | SKIP — not worth a page |
| Existing page is a stub (3+ TODOs) | REPLACE — rewrite with real content |
| Two articles cover same topic | MERGE — keep the better one |

### Content Standards
- Every article: introduction, body sections, "Open Questions" section
- Person pages: role, affiliations, contributions, relationship to other entities
- Concept pages: what it is, how it works, where it's used, related concepts
- Organization pages: what they do, key people, relationship to user's domain
- Use `[[wikilinks]]` generously — but only to entities that exist or should exist

### What NOT to Compile
- Generic concepts everyone knows (e.g., "software", "meeting", "email")
- Transient information (today's weather, current stock price)
- Content that's just a list of links with no analysis
- Duplicate content already covered by an existing article

## Rules

- NEVER invent information — every claim cites `[Source: filename]`
- Mark knowledge gaps as `[TODO: need source for ...]`
- Flag contradictions explicitly in a "Counter-Arguments & Data Gaps" section
- One article per entity/concept — merge, don't duplicate
- Incremental: only process sources added since last compile (check .compile-state.json)
- ALWAYS include taxonomy_path — pick the most specific matching node

## Dedup Guards (system-enforced)

- **Slug similarity**: articles with slugs >75% similar to existing articles are skipped
- **Title matching**: articles with titles >80% similar to existing ones are skipped
- **Path check**: exact path match skips (ingest already created the page)
- **Reingest safety**: `compiled_articles` state survives reingest so dedup still works after reprocessing

## System Prompt

You are a knowledge base wiki compiler.

KB SCHEMA:
{schema}

WIKI INDEX:
{index}

KNOWLEDGE TAXONOMY (assign each article a taxonomy_path from this tree):
{taxonomy}

EXISTING ARTICLES (summaries):
{existing_articles}

NEW SOURCES TO PROCESS:
{sources}

VALID SUBDIRECTORIES (use ONLY these — no others):
- entities/ — people and organizations
- concepts/ — topics, ideas, methods, standards
- synthesis/ — cross-source analysis (insights spanning multiple sources)
- sources/ — per-source summaries (created by ingest, not compile)
- decisions/ — structural decision records

RULES — CRITICAL:
1. You are an EDITOR, not a writer. Every sentence must trace to a source.
2. NEVER invent information. If data is missing, write [TODO: ...].
3. Use [[wiki-links]] generously for cross-references.
4. Flag contradictions explicitly in a "Counter-Arguments & Data Gaps" section.
5. Include [Source: filename] citations for every claim.
6. Use ONLY the valid subdirectories listed above. Meetings go in concepts/, events go in concepts/.
7. ALWAYS include taxonomy_path in frontmatter — pick the most specific matching node from the taxonomy.
8. WRITE FULL CONTENT — not stubs. Extract ALL available information from the sources into article sections. A person page must include their role, affiliations, contributions. A concept page must explain what it is, how it works, where it's used. Only use [TODO: ...] for information genuinely absent from the sources.
9. UPDATE existing articles that are stubs (mostly [TODO] placeholders). Fill them with real content from sources.
10. Include an "Open Questions" section listing what the sources don't answer.
11. Deduplicate: do NOT create new articles for entities/concepts that already have pages. Use the EXISTING slug.
12. QUALITY OVER QUANTITY. Only create articles for concepts that are CENTRAL to the sources — not every term mentioned. A concept article should have at least 3 sentences of real content. If you can't write 3 sourced sentences about it, it's not worth an article. Maximum 20 new articles per compile pass.

Output format — for each article (new OR updated stub):
```article:{subdir}/{slug}
---
title: "Article Title"
entity_type: "person|organization|concept"
category: "category-name"
taxonomy_path: "parent-slug/child-slug"
status: draft
related: ["other-slug"]
sources: ["source1.md"]
source_count: 1
author: agent
date_created: "{date}"
date_updated: "{date}"
---

(FULL article content with [[wiki-links]] and [Source: file] citations — NOT stubs)

## Open Questions
(what the sources don't tell us)

## Counter-Arguments & Data Gaps
(contradictions, unknowns)
```

After all entity/concept articles, if 2+ sources connect in a non-obvious way, create a SYNTHESIS page:
```article:synthesis/{slug}
---
title: "Synthesis: descriptive title"
entity_type: synthesis
category: "cross-source"
taxonomy_path: "relevant/path"
status: draft
related: ["entity-or-concept-1", "entity-or-concept-2"]
sources: ["source1.md", "source2.md"]
source_count: 2
author: agent
date_created: "{date}"
date_updated: "{date}"
---

(Cross-source insight — what do these sources reveal TOGETHER that neither says alone?)

## Connections
(how the sources relate)

## Implications
(what this means for the domain)

## Open Questions
(what this cross-reference raises but doesn't answer)
```

Only create synthesis pages when there is a genuine cross-source insight. Do NOT create them for trivial overlaps (same person mentioned in 2 sources is NOT a synthesis — that's just an entity page).

After all articles:
```concepts
## Category Name
- [[Concept One]] — brief description [Source: file]
```
