Code Structure
Cargo workspace layout, crate responsibilities, and how to extend Codiv.
Workspace Layout
Codiv is a Cargo workspace with four crates:
codiv/
├── Cargo.toml # Workspace root
├── codiv/ # TUI client
├── codivd/ # Async daemon
├── codiv-tools/ # Tool implementations
└── codiv-common/ # Shared types
Crate Responsibilities
codiv (TUI Client)
The terminal interface. Key modules:
- main — entry point, argument parsing, daemon connection
- tui — ratatui application loop, rendering, event handling
- input — command classification (Execute, Interactive, AiQuery, etc.)
- command_index — PATH scanning and hash map for O(1) command lookup
- completion — tab completion (programmable, command, file)
- bash — persistent bash co-process management
- pty — pseudo-terminal handling for interactive passthrough
codivd (Daemon)
The AI backend. Key modules:
- main — daemon startup, Unix socket listener
- agent — AI agent implementation, tool calling loop
- session — session management, context timeline
- streaming — LLM response streaming to client
- worker — bash worker processes for agent tool execution
- daemon_shell —
DaemonShellimplementation for independent agent shells (pipe-basedbash -i) - agent/shell_backend —
ShellBackendrouting: directs orchestrator commands throughClientRelay(shared co-process) and independent agent commands throughDaemonShell - config — configuration loading and hot-reload
codiv-tools (Shared Library)
Tool implementations used by both client and daemon:
- bash — shell command execution
- read — file reading with line numbers
- write — file writing
- edit — find-and-replace editing
- glob — file pattern matching
- grep — regex search
Each tool module exports a struct implementing a common tool trait with execute(), help(), and agent_guide() methods.
codiv-common (Shared Types)
Types shared between client and daemon:
- ipc — message types (AgentRequest, StreamChunk, ExecuteCommand, etc.)
- config — configuration structs
- types — common enums and utility types
- shell — shell abstraction layer (ShellIO trait, ShellSession generic over IO, PtyIO for co-process, PipeIO for daemon shells)
How to Add a New Tool
- Create a new module in
codiv-tools/src/(e.g.,my_tool.rs) - Implement the tool trait with
execute(),help(), andagent_guide() - Register the tool in
codiv-tools/src/lib.rs - Add CLI subcommand in
codiv/src/main.rs - Register the tool with the agent in
codivd/src/agent.rs - Add tests in the tool module
How to Modify IPC Messages
- Edit the message types in
codiv-common/src/ipc.rs - Update the client handler in
codiv/src/tui/to handle the new/changed message - Update the daemon sender in
codivd/src/to produce the new/changed message - Both crates depend on
codiv-common, so the compiler enforces that all message variants are handled