huginn
Use when developing the Huginn coding-agent harness — an opinionated personal coding-agent CLI in Rust for Linux/Pi (Ollama / OpenAI-compatible endpoints). Provides architecture, module map, conventions, verify commands, and the enforced-verification gate.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- huginn
- author
- Hermes Agent
- license
- MIT
- version
- 1.1.0
- metadata
- { "hermes": { "tags": [ "rust", "cli", "agent", "ollama", "coding-agent", "huginn" ], "related_skills": [] } }
- description
- Use when developing the Huginn coding-agent harness — an opinionated personal coding-agent CLI in Rust for Linux/Pi (Ollama / OpenAI-compatible endpoints). Provides architecture, module map, conventions, verify commands, and the enforced-verification gate.
03 SKILL.md 正文
Huginn — Development Skill
Huginn is an opinionated, personal coding-agent harness written in Rust for
Linux and Raspberry Pi. It runs a streaming agent loop against a local (or
cloud) Ollama / OpenAI-compatible /v1/chat/completions endpoint, with tools,
plan mode, context compaction, and parallel sub-agents. This skill gives you
the full context to work on it correctly.
When to Use
- Implementing or fixing issues in the huginn repo
- Adding or modifying tools in
crates/huginn-tools/or confinement incrates/huginn-sandbox/ - Changing the agent loop, compaction, or context management
- Modifying the TUI, session persistence, or CLI
- Reviewing PRs to the repo
Don't use for: porting the Ravenwood palette to other apps — use the
ravenwood-theme porting skill instead.
Architecture
CLI (src/main.rs)
└─ Settings (huginn-config) ── named providers, context-window inference
└─ Agent (huginn-agent)
├─ system prompt (stable instructions + mode) and a following setup message
├─ streaming loop ── POST /v1/chat/completions (Ollama / OpenRouter / …)
├─ compaction (huginn-agent context) ── estimate tokens, summarize middle
├─ tool dispatch (huginn-tools) ── mutators serial; others spawn_blocking
└─ events (mpsc) ── TextDelta, ToolStart/End, Iteration, Compacted,
VerifyRequired, AskUser, PlanProgress, Done, Error
└─ TUI (huginn-tui) ── ratatui event loop, drains agent events
└─ run_parallel ── N independent Agent tasks on git worktrees
One binary, huginn. Dependencies point only downward. Do not add a crate. delegate_task runs in huginn-agent (tools_exec.rs), not in huginn-tools.
Module Layout
| Crate | Owns |
|---|---|
crates/huginn-proto |
ChatMessage, ToolCall, FunctionCall, AgentEvent, AgentError / ToolError, plan types, TodoItem, Goal. Depends on serde, thiserror, tokio, serde_json. |
crates/huginn-tokenizer |
Estimator, usage meters, pricing. Depends on proto. |
crates/huginn-config |
Settings, named providers, onboarding, Grok auth, MCP server specs. Depends on proto. |
crates/huginn-persist |
Session JSONL (atomic writes), session-scoped goal/todos, memory. Depends on proto, config. |
crates/huginn-sandbox |
Path confinement, shell, Landlock, seccomp, rlimits. Depends on proto. landlock and seccompiler are Linux target-cfg deps of this crate only. |
crates/huginn-tools |
Definitions, dispatch, git, patch, documents, validate, web, skills, repomap, plugins, stdio MCP client. Depends on proto, config, persist, sandbox. |
crates/huginn-agent |
Streaming loop (core, stream, tools_exec, loop_control, parallel), compaction (context.rs). Depends on proto, tokenizer, config, persist, tools. Re-exports the MCP client and plugin specs the TUI and ACP call. |
crates/huginn-tui |
Ratatui UI, markdown, slash commands. Depends on proto, config, persist, agent. ratatui, crossterm, and pulldown-cmark stay here. |
crates/huginn-acp |
ACP v1 stdio adapter (huginn --acp). Depends on proto, config, persist, agent. |
crates/huginn-testutil |
outer_sandbox_restrictive, home_env_lock. Depends only on libc. |
root huginn |
src/main.rs (clap), src/runner.rs, src/update.rs, and src/lib.rs facade re-exports. |
Root src/<module>.rs files other than main.rs, runner.rs, and update.rs are pub use facades. Public paths that benches and integration tests use stay: huginn::agent::ChatMessage, huginn::tokenizer, huginn::context, huginn::repomap, huginn::testutil. Session store: repo → {workspace}/.huginn/sessions, system → ~/.huginn/system/sessions. Goal and todos: .huginn/sessions/<id>/state/; legacy .huginn/state/ is read-only.
Conventions
Rust
- Rust 2021 edition. Target MSRV: 1.97 (pinned in
rust-toolchain.toml). - Keep the binary small and dependency-light. Thin stdio MCP client only
(no marketplace, no HTTP/SSE). Agent Plugins
mcp.jsonis stdio-only. No telemetry. - Every public struct, enum, and fn should have a doc comment.
cargo doc --workspace --no-depsmust build with no warnings.cargo build --workspacemust build with no warnings.- Prefer
anyhowfor error handling in the binary;thiserroris available if a crate-level error type is needed. - DO NOT add comments unless asked — keep code self-documenting.
File Organization
- The root
src/lib.rsdeclares the facade modules. Implementation lives incrates/<name>/src. Benches stay at the repo root and call the facades. - Tests live in
#[cfg(test)]modules next to the code they cover (agent tests undercrates/huginn-agent/src/tests/), plus black-box tests intests/.
Build, Lint, Test
cargo build # debug build, must be warning-free
cargo build --release # LTO + strip
cargo test --workspace # every member, all offline (no Ollama needed)
cargo clippy --workspace --all-targets -- -D warnings # must be zero warnings
cargo fmt --all --check # formatting check
cargo doc --workspace --no-deps # docs must build with no warnings
cargo bench # Criterion benchmarks (tokenizer, context, repomap)
CI runs: cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings,
cargo build --workspace --all-targets, cargo test --workspace, cargo doc --workspace --no-deps, and
cargo audit. All of these must pass before a PR is mergeable.
The Enforced-Verify Gate
The agent has a built-in verification gate (default on, --no-verify to
disable). When the model edits files in a turn but does not call run_tests
before finishing, the harness injects a recovery reminder and re-runs the turn
(capped at 3 attempts). This is the project's core "verifiable changes" contract:
AgentEvent::VerifyRequiredis emitted when the gate re-runs.- The gate is skipped when the workspace has no detectable test runner
(
has_test_runner:Cargo.toml;package.jsonandnode_modules; Python only ifpytestis spawnable). - Credit is fail-closed:
run_testsorrun_shellwithis_verification_commandcounts, and only whenparse_exit_codeis0(do not usecontains("exit=0")—exit=10must fail). - Failed / sandbox-rejected edits must not arm the gate.
When implementing a fix, always run the real verification commands
(cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings,
cargo fmt --all) — do not rely on the agent's simulated run_tests tool.
Common Pitfalls
- Tools and sandbox are separate crates — definitions and dispatch live
in
crates/huginn-tools(definitions.rs,dispatch.rs). OS confinement (Landlock / seccomp / rlimits) lives incrates/huginn-sandbox(confinement.rs).landlockandseccompilerstay on that crate.huginn-tokenizerdoes not depend on the sandbox.delegate_taskstays inhuginn-agent'stools_exec.rs. - Tokenizer is a fast estimator, not exact — it over-estimates tiktoken counts by roughly 10–35% (mean ~28%), biased slightly high so compaction triggers early. It treats non-newline whitespace as free and applies a ~12% structural-overhead factor. Don't "fix" it to be exact without a real tokenizer to validate against.
- Compaction invariants —
messages[0]is always the system message; tool-call/tool-result pairs are never split. Don't break these. - Sandbox — all file paths are relative to the workspace root and confined
to it.
run_shellblocks dangerous patterns and strips known secret env vars. The blocklist is a denylist (best-effort), not an allowlist (issue #14). Landlock write roots are workspace + explicit extras +/dev— never the process temp dir.TMPDIRis pinned under.huginn/tmp. - Session IDs —
{iso}-{pid}-{counter}(issue #10 is fixed). Still don't invent a different scheme without updatinggenerate_session_id. - Repo map is cached per workspace path until
repomap::invalidate(called whenrepo_map_staleafter a successful file edit). Discovery prefersgit ls-files --exclude-standard, else anignore-crate walk; both honor.gitignoreand hardSKIP_DIRS. Candidates are path-scored before the extract budget (MAX_WALK_DEPTH/MAX_SOURCE_FILES_SCANNED). Cache key is the rawPath, not canonical — keepsettings.workspaceconsistent. - Session writes are atomic (
write_atomictemp+rename) for bothappend_messageandsave_all_messages. Do not reintroduce in-place truncate. - TUI
Agent::newis off-thread — never construct anAgenton the event-loop thread. Each turn owns its ownmpscpair (begin_agent_turn). Interrupt//stop//newmustabort_current_turn(abort handle and dropevent_rx) so a leftoverDonecannot join the next handle. Do not share one session-long channel. - Named providers —
Providerbundles endpoint + auth + default model. Resolution:--provider>HUGINN_PROVIDER> configprovider> builtinollama./providerwith no args lists names; unknown names must not silently become ollama. Session summary storesmodelonly — resume can pair a stored OpenRouter model with the default Ollama provider. - Headless plan revise — after
[r]evise+ approve, execute the revised plan and revised message list (run_plan_flow), not the first proposal. - Persistent state — todos/goal live under
.huginn/sessions/<id>/state/(crates/huginn-persist/src/state.rs); legacy.huginn/state/is read-only. Not astatic.todo_write/goal_setpersist; rebuild the system message at the start of every turn and after those tools. Nesteddelegate_taskis depth-1 (allow_delegate = falseon the child; no parent goal/todo overwrite).thinkis read-only and available in plan/chat. - Compaction thrashing —
Agent.compact_thrash_countpauses auto- compaction after 3 consecutive no-reduction compactions; retry every 4th iteration so a later prune can resume.
Verification Checklist
- [ ]
cargo buildsucceeds with no warnings - [ ]
cargo clippy --workspace --all-targets -- -D warningspasses (zero warnings) - [ ]
cargo fmt --all --checkpasses - [ ]
cargo test --workspacepasses (all offline) - [ ]
cargo doc --workspace --no-depsbuilds with no warnings - [ ] New public items have doc comments
- [ ] New logic has unit tests in the module's
#[cfg(test)]block - [ ] No new dependencies unless justified (keep the binary small)
Related Files
README.md— User-facing documentation, feature list, quick startROADMAP.md— Current state, planned work, tool countdocs/architecture.md— Detailed design walkthroughdocs/tools.md— Tool contracts and sandbox rulesdocs/testing.md— Test structure, coverage, mutation testingdocs/contributing.md— Build, style, and layout