From 4b2f645f161a78a7b02c9d5c0c51d2daa28c5ba1 Mon Sep 17 00:00:00 2001 From: sausheong Date: Fri, 8 May 2026 00:44:55 +0800 Subject: [PATCH] Document harness components and design notes in README --- README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f981ff0..15906a3 100644 --- a/README.md +++ b/README.md @@ -74,22 +74,66 @@ func main() { } ``` -## Design +## Components -The harness is intentionally minimal. Every agent gets: +A general agent harness has ~13 conceptual components: the loop, the +model invocation layer, the tool registry, permission gating, +context/compaction, the system prompt, sub-agents, hooks, MCP, skills, +session persistence, UI, and entrypoints. harness implements the +in-process, library-shaped subset of those. The rest is left to the +caller — you own the binary, you own the UI, you own the channel. -- A streaming think-act loop with parallel tool dispatch -- A pluggable LLM provider interface (Anthropic, OpenAI, Gemini, Qwen - built in; bring your own to add more) -- Append-only session storage with optional JSONL persistence -- Three-stage summarize-and-splice compaction with prompt-cache-stable - prefixes -- A token budget calibrator that learns the chars→tokens ratio per - session +| # | Component | Where it lives in harness | +|---|---|---| +| 1 | **Agent loop** | `runtime.Runtime.Run()` — single goroutine, streaming-first; `<-chan ChatEvent` | +| 2 | **Model invocation** | `llm.Provider` interface + `ChatStream`; non-streaming fallback retries the same `ChatRequest` byte-for-byte to preserve the prompt-cache prefix | +| 3 | **Tool registry & schemas** | `tool.Registry`; `ToolDefs()` is sorted by name so the request prefix is stable across turns (cache hit) | +| 4 | **Tool execution & permissions** | `tool.PermissionChecker` (`Check` + `FilterToolDefs`); concurrency-safe partitioning via `Tool.IsConcurrencySafe` + `LoopConfig.MaxToolConcurrency` (default 10) | +| 5 | **Context & compaction** | `compaction.Manager` — summarize-and-splice at a clean user-message boundary; tool-result pruning at request time | +| 6 | **System prompt assembly** | `llm.SystemPromptPart` — splits the static cacheable prefix from the per-turn dynamic suffix; provider-side `cache_control` placement is automatic | +| 7 | **Sub-agents / delegation** | `runtime.SubagentResolver` interface + `MaxAgentDepth=3` cap; subagents run as in-process goroutines with their own `Runtime` | +| 10 | **Skills** | `runtime.SkillProvider` (`FormatIndex` + `Get`); auto-registers `load_skill` so the model can pull a skill on demand. See `examples/support-agent/` for a `//go:embed kb/*.md` example | +| 11 | **Session persistence** | `session.Session` (in-memory, append-only) + optional `session.Store` for JSONL persistence and cross-process resume | -Everything else — skills, long-term memory, knowledge graphs, -permission policies, subagent dispatch, on-disk session stores — is an -optional plug-point. Pass `nil` and it disappears. +Deliberately not in scope (numbers from the same conceptual list): + +- **8 — Hooks**: no PreToolUse/PostToolUse/Stop hooks. If you need + them, wrap the event channel. +- **9 — MCP**: not built in. Register MCP-backed tools manually as + `tool.Tool` implementations. +- **12 — UI**: harness emits events; the caller renders. +- **13 — Entrypoints**: harness is a library. There is no `harness` + binary. The example agents in `examples/` show how a binary is + assembled. + +## Design notes + +- **Streaming-first.** The loop yields `EventTextDelta`, + `EventToolCallStart`, `EventToolResult`, `EventDone`, and friends + through a `<-chan ChatEvent`. There is no buffered "give me the + whole response" path — even the non-streaming fallback re-emits + through the same channel. +- **Prompt-cache discipline.** Tool definitions are sorted, system + prompt parts are split into cached/dynamic blocks, and + `buildMessageParams` is a pure function (proven by + `TestBuildMessageParamsIsPure` in + `providers/anthropic/anthropic_test.go`). The non-streaming retry + path depends on byte-identical params to keep the cache prefix + valid. +- **Compaction at a clean boundary.** `compaction.Split` walks + backward counting user messages and cuts at one — never inside a + tool-call/tool-result pair. The prompt itself (`compaction/prompt.go`) + is adapted from Claude Code's `BASE_COMPACT_PROMPT` and emits a + 9-section structured summary inside an `` + `` + envelope; the `` block is stripped before re-injection. +- **Plug-points are nil-friendly.** `MemoryProvider`, + `KnowledgeGraph`, `SkillProvider`, `SubagentResolver`, + `PermissionChecker`, and `session.Store` are all optional. Pass + `nil` and the corresponding feature disappears with no degraded + behaviour. +- **Four providers, one interface.** Anthropic, OpenAI (and + OpenAI-compatible / local Ollama), Gemini, and Qwen ship built-in. + Adding a fifth is implementing `llm.Provider` (~300 LOC). See [`developer.md`](./developer.md) for a step-by-step guide to building agents on top of harness.