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
//! 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 |
//! | [`approval`] | 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.
/// Sub-agent configuration, discovery, and invocation.
/// Tool approval modes, safety gates, and shared mode state.
/// 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.
/// Loop detection — catches runaway repeated tool calls.
/// 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.
/// Progress reporting helpers for long operations.
/// System prompt construction.
/// LLM provider abstraction — Anthropic, Gemini, OpenAI-compatible.
/// Thread-safe runtime environment — replaces `std::env::set_var`.
/// Session lifecycle — create, resume, list, delete.
/// Last-used provider persistence (`~/.config/koda/settings.toml`).
/// 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.
/// Undo stack for file mutations.
/// Version string and update-check helpers.
/// Git worktree provisioning for sub-agent isolation.