1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Koda Core — the engine library for the Koda AI coding agent.
//!
//! This crate contains the pure engine logic with zero terminal dependencies.
//! It communicates exclusively through [`engine::EngineEvent`] (output) and
//! [`engine::EngineCommand`] (input) enums.
//!
//! ## Quick navigation
//!
//! | Module | What it does |
//! |---|---|
//! | [`agent`] | Agent construction and built-in agent catalog |
//! | [`tools`] | 19 built-in tools (file ops, shell, search, web, memory, agents) |
//! | [`providers`] | 14 LLM providers (Anthropic, OpenAI, Gemini, local, etc.) |
//! | [`inference`] | Streaming inference loop with tool execution |
//! | [`config`] | Configuration loading, agent discovery, settings |
//! | [`trust`] | Safety gates and approval modes |
//! | [`engine`] | Event/command protocol (client–engine boundary) |
//! | [`compact`] | Context compaction (summarize old messages) |
//! | [`memory`] | Persistent semantic memory (`MEMORY.md`) |
//! | [`skills`] | Expertise modules (prompt injection, zero LLM cost) |
//! | [`db`] | SQLite persistence (sessions, history, file ownership) |
//!
//! ## Architecture
//!
//! ```text
//! ┌───────────────┐ EngineEvent ┌─────────────────┐
//! │ koda-cli │ ←───────────── │ koda-core │
//! │ (TUI/REPL) │ ─────────────→ │ (engine lib) │
//! └───────────────┘ EngineCommand └────────┬────────┘
//! │
//! ┌──────┴──────┐
//! │ providers │
//! │ (LLM APIs) │
//! └─────────────┘
//! ```
//!
//! See `DESIGN.md` in the repository root for the full architectural rationale.
// koda-core requires a Unix-like OS (macOS or Linux).
// The built-in shell tool uses `sh` which does not exist on Windows.
// Windows users: install WSL2 — https://learn.microsoft.com/windows/wsl
/// Sub-agent configuration, discovery, and invocation.
/// Approval flow and user interaction during tool execution.
pub
/// Heuristic path-escape detection for bash commands.
/// Bash command safety classification — ReadOnly, LocalMutation, Destructive.
/// Background sub-agent registry — tracks and drains async agent results.
/// Context compaction — summarise old messages to reclaim token budget.
/// Global configuration: provider, model, model settings, CLI flags.
/// Context window management and token budgeting (engine-internal).
pub
/// Context analysis — per-tool token breakdown and duplicate detection.
/// SQLite persistence layer — sessions, messages, usage tracking.
/// Engine protocol: `EngineEvent` / `EngineCommand` enums.
/// File lifecycle tracker — tracks files created by Koda per session (#465).
/// Git context injection — branch, staged/unstaged diffs, recent commits.
/// The main inference loop — send messages, stream responses, dispatch tools.
/// Shared helpers used by the inference loop.
/// Credential storage — `keys.toml` with env var fallback.
/// Last-used provider recall (stored in SQLite KV store, not a config file).
/// Loop detection — catches runaway repeated tool calls.
/// MCP client — connect to external MCP servers (#662).
/// Project memory — `MEMORY.md` / `CLAUDE.md` read/write.
/// Microcompact — lightweight tool result aging without full compaction.
/// Hardcoded model aliases for stable, cross-provider model selection.
/// Hardcoded context-window lookup table (fallback when API doesn't report).
/// Context-scaled output caps for tool results.
/// `Persistence` trait — the database contract.
/// Diff preview generation for file mutations.
/// System prompt construction.
/// Static provider catalog: `ProviderType` + `ProviderMeta` lookup tables.
/// LLM provider abstraction — Anthropic, Gemini, OpenAI-compatible.
/// Thread-safe runtime environment — replaces `std::env::set_var`.
/// Process sandboxing for the Bash tool (macOS seatbelt / Linux bwrap).
/// Session lifecycle — create, resume, list, delete.
/// Skill-scoped tool filtering — hard enforcement of `allowed_tools`.
/// Skill discovery and activation (project, user, built-in).
/// Sub-agent result caching — zero-cost retries after compaction.
/// Sub-agent invocation and lifecycle management.
pub
/// Tool dispatch — routes tool calls from inference to the registry.
/// Tool name normalization — maps model-emitted variants to canonical PascalCase.
/// Tool registry, definitions, execution, and path safety.
/// Token-safe output truncation.
/// Unified trust mode — single permission knob replacing ApprovalMode × SandboxMode.
/// Undo stack for file mutations.
/// Version string and update-check helpers.