|
1 | 1 | # Native knowledge architecture |
2 | 2 |
|
3 | | -This reference describes the chat application's native knowledge module. The |
4 | | -user workflow lives in the [Memory manual](../internal/manual/chat/places.md#knowledge-from-saved-chats-inside-memory). |
| 3 | +This contributor reference describes the chat application's profile knowledge |
| 4 | +module. Product behavior belongs in the [Memory manual](../internal/manual/chat/places.md#knowledge-from-saved-chats-inside-memory). |
5 | 5 |
|
6 | | -## One profile database |
| 6 | +## Ownership and entry points |
7 | 7 |
|
8 | | -`internal/knowledge.Vault` owns the database at `knowledge/knowledge.sqlite` |
9 | | -beneath the resolved profile. `Open(profile)` returns a lightweight handle; |
10 | | -operations open bounded SQLite connections. WAL permits readers while the |
11 | | -compiler makes short transactions. Write transactions begin immediately so |
12 | | -concurrent source admission cannot fail while upgrading a read lock. |
| 8 | +| Responsibility | Implementation owner | |
| 9 | +| --- | --- | |
| 10 | +| Start, resume, and stop background work | `cmd/codeaf/chatv3_knowledge.go`, `internal/knowledge/build.go` | |
| 11 | +| Discover and read saved history | `internal/session/knowledge_history.go` | |
| 12 | +| Admit immutable sources and publish readable pages | `internal/knowledge/store.go` | |
| 13 | +| Compile evidence into maintained concept guides | `internal/knowledge/compiler.go` | |
| 14 | +| Preserve edits and deletion intent | `internal/knowledge/edit.go` | |
| 15 | +| Agent knowledge tools and local recall | `internal/session/knowledge_tools.go`, `internal/knowledge/compiler_recall.go` | |
| 16 | +| Memory rendering, navigation, and editor | `internal/tui3/knowledge*.go` | |
| 17 | + |
| 18 | +`knowledge.Open(profile)` returns a lightweight `Vault` handle. The vault owns |
| 19 | +`knowledge/knowledge.sqlite` beneath the resolved profile. Callers use vault |
| 20 | +operations rather than reading compiler state or writing database tables directly. |
| 21 | +The chat and resident surfaces have separate lifecycles. |
| 22 | + |
| 23 | +## Data flow |
| 24 | + |
| 25 | +```text |
| 26 | +saved history and new conversation events |
| 27 | + → immutable source admission |
| 28 | + → evidence extraction and concept organization |
| 29 | + → reviewed guides |
| 30 | + → atomic publication |
| 31 | + → Memory, indexed search, and agent recall |
| 32 | +``` |
| 33 | + |
| 34 | +The initial build organizes a finite admitted history snapshot. Later evidence |
| 35 | +updates affected guides or creates distinct concepts. Source eligibility and |
| 36 | +concept organization are separate responsibilities. Saved task reports can supply |
| 37 | +evidence; internal control traffic and deleted history remain excluded. |
| 38 | + |
| 39 | +## Storage authority |
13 | 40 |
|
14 | 41 | | Stored data | Authority | |
15 | 42 | | --- | --- | |
16 | | -| Immutable source events and journal cursors | Original admitted text, provenance, pending work, and exactly where import resumes | |
17 | | -| Compiler publications and checkpoints | Extraction, learning, topic registry, validated model receipts, and recovery after interrupted composition | |
18 | | -| Effective pages and FTS | Reader, indexed search, citations, links, and one committed publication revision | |
19 | | -| Human overrides | Revision-checked Markdown edits and durable deletion tombstones, separate from generated prose | |
20 | | -| Build state and history errors | First-build intent, background progress, partial imports, retries, and recorded cost | |
21 | | - |
22 | | -Compiler documents retain optional claim and learning metadata inside SQLite. |
23 | | -Per-page records and full-text indexes serve readers; a page lookup does not |
24 | | -load the compiler document. JSON files and experimental exports are not read |
25 | | -paths. Publication overlays the latest human changes, replaces effective pages |
26 | | -and search entries, and commits the visible revision in one transaction. |
27 | | - |
28 | | -## Background ownership |
29 | | - |
30 | | -`v3Process` starts and joins `Vault.Run` under its process context. A worker lock |
31 | | -admits one process per profile; another running process waits and takes ownership |
32 | | -when the OS releases that lock. A separate compiler lock prevents overlapping |
33 | | -manual flushes. These OS locks retain worker ownership during provider requests; |
34 | | -SQLite transactions end before network work begins. |
35 | | - |
36 | | -A successful empty inventory arms future capture and shows the waiting state. |
37 | | -Existing history waits for **Build knowledge base**. The action records durable |
38 | | -intent and returns; it does not run models on the UI loop. Initial history drains |
39 | | -in bounded batches. Future turns use the quiet/max-wait policy. Cancellation |
40 | | -leaves admitted sources and completed stages available for the next process. |
41 | | - |
42 | | -`internal/session.KnowledgeHistory` owns saved-journal discovery and parsing. |
43 | | -It visits all project buckets without a recent-chat limit, includes archives and |
44 | | -legacy flat journals, resolves sealed layouts through `Place.Transcript`, and |
45 | | -excludes deleted conversations and task journals. Only complete records advance |
46 | | -cursors. Original messages use stable session/byte-offset IDs. Admission and |
47 | | -cursor advancement commit together; retries compare immutable source content. |
48 | | - |
49 | | -## Compiler and evidence |
50 | | - |
51 | | -The compiler preserves the original extraction, exact-quote validation, |
52 | | -claim deduplication, qualified assistant reports, human-only newer corrections, |
53 | | -contradictions, stable note identity, subject discovery, reviewed explanations, |
54 | | -question resolution, topic reconciliation, and canonical mention links. |
55 | | - |
56 | | -Provider results enter through typed role-response checks. Claims and paragraphs |
57 | | -must reference admitted sources or known claims; quotes must occur in the supplied |
58 | | -context. Separate review gates generated explanations. Model receipts record the |
59 | | -actual returned model ID and reject substitutions. The configured knowledge model |
60 | | -applies to every role; credentials never enter receipts or visible errors. |
61 | | - |
62 | | -The reader and editor share the same effective pages used by agent recall. |
63 | | -Edited prose is explicitly human-authored, with original source history kept |
64 | | -separate. Tombstones suppress generated pages across rebuilds. Stale editor |
65 | | -revisions cannot overwrite later edits. Search entries change in the same |
66 | | -transaction as visible page content. |
67 | | - |
68 | | -## Responsiveness and bounds |
69 | | - |
70 | | -Extraction admits at most 64 spans and 24,000 source characters per call. A |
71 | | -journal read handles at most 256 records or 64 eligible sources, with a 16 MiB |
72 | | -record bound. Discovery processes at most 256 notes per group, and oversized |
73 | | -subjects retain a partition-needed state rather than truncating claims. |
74 | | - |
75 | | -The UI performs database work through asynchronous commands. Search uses FTS, |
76 | | -late filter results are discarded, and unchanged publication revisions reuse |
77 | | -cached reader content. Rendering and navigation do not call the provider. |
78 | | -Provider responses and timeouts are bounded; retries use the same selected model. |
79 | | - |
80 | | -## Verification |
81 | | - |
82 | | -Tests cross saved-journal-to-readable/searchable-page, human-edit/rebuild, and |
83 | | -Knowledge-screen seams. They cover history from multiple projects, first-chat |
84 | | -population, notes/compaction exclusion, source support, correction authority, |
85 | | -review rejection, duplicate replay, and navigation while a build is requested. |
86 | | - |
87 | | -`TestKnowledgeLiveNativeCompiler` is opt-in through `CODEAF_KNOWLEDGE_LIVE=1`. |
88 | | -It uses the existing OpenRouter credential, removes Python from the executable |
89 | | -search path, and checks the actual model recorded for every call. Ordinary tests |
90 | | -use a provider fixture and require no credential. |
| 43 | +| Source events and import cursors | Admitted text, provenance, and resumable intake | |
| 44 | +| Compiler checkpoints and validated receipts | Recoverable background compilation | |
| 45 | +| Effective pages and full-text index | One published view for readers and search | |
| 46 | +| Human overrides | Revision-checked edits and durable deletion intent | |
| 47 | +| Build state | Background intent, stage progress, and recorded usage | |
| 48 | + |
| 49 | +Generated prose does not replace source evidence. Page identity is independent of |
| 50 | +its current title. Concept guides, detailed evidence notes, and topic maps have |
| 51 | +different reader roles. A visible guide count and the total stored page count need |
| 52 | +not be equal. |
| 53 | + |
| 54 | +## Public module contracts |
| 55 | + |
| 56 | +`Capture` admits local source text without a model call. `StartBuild` records |
| 57 | +background intent. The process owns and joins `Run` through its context. `Flush` |
| 58 | +runs compilation outside the UI loop. `Snapshot`, `Read`, and `Search` expose |
| 59 | +published content. `EditPage` and `DeletePage` require the expected page revision. |
| 60 | +`Revisit` operates on explicitly named immutable sources. Use the owning APIs for |
| 61 | +optional intake rather than bypassing their concurrency checks. |
| 62 | + |
| 63 | +Memory and agent recall consume the same effective pages. An edit must remain |
| 64 | +human-authored across future builds. A deletion must remain suppressed. An older |
| 65 | +editor revision must not overwrite a newer change. Generated additions to an |
| 66 | +edited guide remain reviewable through its source evidence. |
| 67 | + |
| 68 | +## Concurrency and failure boundaries |
| 69 | + |
| 70 | +The worker and compiler have separate per-profile ownership locks. Provider work |
| 71 | +runs outside SQLite write transactions. Independent guide work can run |
| 72 | +concurrently; shared extraction decisions and visible publication remain ordered. |
| 73 | +A publication transaction replaces pages, search entries, and the visible revision |
| 74 | +together, after overlaying the latest human changes. |
| 75 | + |
| 76 | +Interrupted work retains admitted sources and completed checkpoints. Invalid |
| 77 | +provider output cannot become a valid cached result or a partial visible |
| 78 | +publication. Every role uses the selected knowledge model; a fallback is refused. |
| 79 | +Keep credentials and source content out of visible errors. |
| 80 | + |
| 81 | +## Reader responsiveness |
| 82 | + |
| 83 | +The UI performs database work through asynchronous commands. Search uses the |
| 84 | +published full-text index. Late filter results are discarded. Unchanged publication |
| 85 | +revisions reuse reader content. Rendering, browsing, and local search do not call a |
| 86 | +model. Import and source-processing progress describe their own completed items, |
| 87 | +not an overall completion estimate. |
| 88 | + |
| 89 | +## Change and verify the owning boundary |
| 90 | + |
| 91 | +Keep database representation, compiler checkpoints, and prompt composition inside |
| 92 | +`internal/knowledge`. History eligibility changes belong with the session history |
| 93 | +reader. User-facing changes also update the shipped Memory manual. Test through |
| 94 | +capture, compilation, published pages, edit conflicts, and the actual terminal UI. |
| 95 | + |
| 96 | +Ordinary tests use a provider fixture and require no credential. The opt-in |
| 97 | +`TestKnowledgeLiveNativeCompiler` checks the actual recorded model through the live |
| 98 | +provider. Run the repository's required checks before merging. Recheck cancellation, |
| 99 | +source citations, human edits, deletion intent, and search consistency when changing |
| 100 | +publication or concurrency. |
0 commit comments