# buildwithyj/quota-cistern architecture

> Architecture diagram of the GitHub repository buildwithyj/quota-cistern, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/buildwithyj/quota-cistern
- Repository: https://github.com/buildwithyj/quota-cistern

## Overview

#### Purpose and Executables
`quota-cistern` is a Rust workspace for unattended, budget-bounded coding-agent execution. It queues delegated tasks, runs them in isolated Git branches/worktrees, enforces time and agent-usage limits, and leaves results for human review. User entry points are the `cistern` CLI (`crates/cistern/src/main.rs`, `crates/cistern/src/cli.rs`) and the long-running local daemon `cisternd` (`crates/cisternd/src/main.rs`).

#### Client/Daemon Boundary
The CLI owns command parsing, output formatting, local configuration access, and daemon session interaction (`crates/cistern/src/task.rs`, `crates/cistern/src/session.rs`, `crates/cistern/src/config.rs`). It maps commands such as task registration, `run`, interruption, review, diff, and apply onto daemon-facing contracts. Shared request/value definitions live in `crates/cistern-contract/src/lib.rs`, with address and exchange/code protocol pieces in `crates/cistern-contract/src/address.rs` and `crates/cistern-contract/src/exchange.rs`.

#### Hexagonal Daemon Core
`cisternd` separates policy from operating-system and vendor details. Domain state models configuration, tasks, sessions, consumption, and supervision in `crates/cisternd/src/core/domain/task.rs`, `crates/cisternd/src/core/domain/session.rs`, and `crates/cisternd/src/core/domain/supervision.rs`. Inbound and outbound interfaces define the core’s callable use cases and required infrastructure capabilities under `crates/cisternd/src/core/port/inbound/` and `crates/cisternd/src/core/port/outbound/`.

#### Execution and Budget Control
Execution orchestration is centered on `crates/cisternd/src/core/service/execution.rs`. A session receives a usage and time budget, starts eligible backlog tasks, observes actual per-task consumption, and admits further work only when remaining budget permits. Supervision tracks running work and termination reasons; interruption or exhausted budget stops active tasks while retaining their worktree/branch state. Backlog and configuration policies are separate services in `crates/cisternd/src/core/service/backlog.rs` and `crates/cisternd/src/core/service/configuration.rs`.

#### Adapters and Persistence/Process Effects
Inbound adapters expose backlog, configuration, and execution operations to the local client protocol (`crates/cisternd/src/adapter/inbound/backlog.rs`, `crates/cisternd/src/adapter/inbound/execution.rs`). Outbound adapters implement core ports against local machine resources: repository and worktree operations, session/backlog storage, clock/limits, and agent invocation (`crates/cisternd/src/adapter/outbound/repository.rs`, `crates/cisternd/src/adapter/outbound/worktree.rs`, `crates/cisternd/src/adapter/outbound/agent.rs`). This is the critical boundary that creates immutable per-task result branches and launches external agent processes.

#### Agent and Git Workflow
Version 0.1.0 targets the Claude vendor on one machine. The Claude adapter carries prompt/response and limit-related assets (`crates/cisternd/src/adapter/outbound/claude.json`, `crates/cisternd/src/adapter/outbound/claude-goal.txt`, `crates/cisternd/src/adapter/outbound/claude-answer.json`). Each assigned task gets a dedicated worktree and result branch; dependent tasks can use a predecessor branch as a base. Result branches are not subsequently modified, merged, or pushed by the system; apply/discard are review dispositions performed through the CLI.

#### Runtime Lifecycle
The daemon platform layer owns serving, signal handling, and background work coordination (`crates/cisternd/src/platform/serve.rs`, `crates/cisternd/src/platform/signal.rs`, `crates/cisternd/src/platform/work.rs`). The system is single-host and permits one active session at a time. Its material external dependencies are the local Git repository/worktree environment and the Claude agent runtime; no network service or web application is indicated.

Main entry points: [`crates/cisternd/src/core/service/execution.rs`](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/service/execution.rs) (Execution service), [`crates/cisternd/src/core/service/backlog.rs`](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/service/backlog.rs) (Backlog service), [`crates/cisternd/src/adapter/inbound/execution.rs`](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/inbound/execution.rs) (Local protocol adapters), [`crates/cistern/src/task.rs`](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern/src/task.rs) (CLI task/session layer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Local Client"]
  node_cli["cistern CLI<br/>Rust executable<br/>[main.rs]"]
  node_cli_commands["CLI task/session layer<br/>command mapping<br/>[task.rs]"]
  node_contracts["Shared contracts<br/>protocol types<br/>[lib.rs]"]
end

subgraph group_daemon["Daemon Core"]
  node_daemon["cisternd<br/>Rust daemon<br/>[main.rs]"]
  node_inbound_ports["Inbound ports<br/>use-case interfaces"]
  node_execution{{"Execution service<br/>orchestrator<br/>[execution.rs]"}}
  node_backlog["Backlog service<br/>policy service<br/>[backlog.rs]"]
  node_domain["Task/session supervision domain<br/>domain state<br/>[supervision.rs]"]
  node_outbound_ports["Outbound ports<br/>capability interfaces"]
end

subgraph group_adapters["Adapters &amp; Platform"]
  node_protocol_adapters["Local protocol adapters<br/>inbound adapters<br/>[execution.rs]"]
  node_platform["Daemon platform<br/>runtime lifecycle<br/>[serve.rs]"]
  node_git_adapter["Repository &amp; worktree adapter<br/>outbound adapter<br/>[worktree.rs]"]
  node_agent_adapter["Claude adapter<br/>outbound adapter<br/>[agent.rs]"]
end

subgraph group_external["Local External Systems"]
  node_git[("Local Git repository<br/>Git/worktree environment")]
  node_claude["Claude agent runtime<br/>external process"]
end

node_cli -->|"dispatches commands"| node_cli_commands
node_cli_commands -->|"uses protocol"| node_contracts
node_cli_commands -->|"local daemon session"| node_protocol_adapters
node_contracts -->|"defines exchange"| node_protocol_adapters
node_daemon -->|"starts lifecycle"| node_platform
node_platform -->|"serves"| node_protocol_adapters
node_protocol_adapters -->|"invokes use cases"| node_inbound_ports
node_inbound_ports -->|"execution operations"| node_execution
node_inbound_ports -->|"backlog operations"| node_backlog
node_execution -->|"selects eligible tasks"| node_backlog
node_execution -->|"tracks budgets and supervision"| node_domain
node_backlog -->|"manages task state"| node_domain
node_execution -->|"requests effects"| node_outbound_ports
node_backlog -->|"persists backlog"| node_outbound_ports
node_outbound_ports -->|"repository/worktree capability"| node_git_adapter
node_outbound_ports -->|"agent capability"| node_agent_adapter
node_git_adapter -->|"creates worktrees and branches"| node_git
node_agent_adapter -->|"launches and observes"| node_claude
node_agent_adapter -->|"runs task in worktree"| node_git_adapter

click node_cli "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern/src/main.rs"
click node_cli_commands "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern/src/task.rs"
click node_contracts "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern-contract/src/lib.rs"
click node_daemon "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/main.rs"
click node_execution "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/service/execution.rs"
click node_backlog "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/service/backlog.rs"
click node_domain "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/domain/supervision.rs"
click node_protocol_adapters "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/inbound/execution.rs"
click node_platform "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/platform/serve.rs"
click node_git_adapter "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/outbound/worktree.rs"
click node_agent_adapter "https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/outbound/agent.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_cli_commands,node_contracts toneBlue
class node_daemon,node_inbound_ports,node_execution,node_backlog,node_domain,node_outbound_ports toneAmber
class node_protocol_adapters,node_platform,node_git_adapter,node_agent_adapter toneMint
class node_git,node_claude toneRose
```

## Components

### Local Client

- [cistern CLI](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern/src/main.rs): Rust executable · Commands, local config, and human-facing output. · `crates/cistern/src/main.rs`
- [CLI task/session layer](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern/src/task.rs): command mapping · `crates/cistern/src/task.rs`
- [Shared contracts](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cistern-contract/src/lib.rs): protocol types · Daemon request, address, and exchange definitions. · `crates/cistern-contract/src/lib.rs`

### Daemon Core

- [cisternd](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/main.rs): Rust daemon · Single-host runtime with one active session. · `crates/cisternd/src/main.rs`
- **Inbound ports**: use-case interfaces
- [Execution service](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/service/execution.rs): orchestrator · Schedules eligible tasks within time and usage budgets. · `crates/cisternd/src/core/service/execution.rs`
- [Backlog service](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/service/backlog.rs): policy service · `crates/cisternd/src/core/service/backlog.rs`
- [Task/session supervision domain](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/core/domain/supervision.rs): domain state · Tasks, sessions, consumption, and termination reasons. · `crates/cisternd/src/core/domain/supervision.rs`
- **Outbound ports**: capability interfaces

### Adapters & Platform

- [Local protocol adapters](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/inbound/execution.rs): inbound adapters · Exposes backlog and execution operations to the client protocol. · `crates/cisternd/src/adapter/inbound/execution.rs`
- [Daemon platform](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/platform/serve.rs): runtime lifecycle · Serving, signals, and background-work coordination. · `crates/cisternd/src/platform/serve.rs`
- [Repository & worktree adapter](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/outbound/worktree.rs): outbound adapter · Creates isolated task worktrees and immutable result branches. · `crates/cisternd/src/adapter/outbound/worktree.rs`
- [Claude adapter](https://github.com/buildwithyj/quota-cistern/blob/main/crates/cisternd/src/adapter/outbound/agent.rs): outbound adapter · Launches the vendor agent with prompt and limit assets. · `crates/cisternd/src/adapter/outbound/agent.rs`

### Local External Systems

- **Local Git repository**: Git/worktree environment · Holds task worktrees and reviewable result branches.
- **Claude agent runtime**: external process

## Connections

- cistern CLI → CLI task/session layer: dispatches commands
- CLI task/session layer → Shared contracts: uses protocol
- CLI task/session layer → Local protocol adapters: local daemon session
- Shared contracts → Local protocol adapters: defines exchange
- cisternd → Daemon platform: starts lifecycle
- Daemon platform → Local protocol adapters: serves
- Local protocol adapters → Inbound ports: invokes use cases
- Inbound ports → Execution service: execution operations
- Inbound ports → Backlog service: backlog operations
- Execution service → Backlog service: selects eligible tasks
- Execution service → Task/session supervision domain: tracks budgets and supervision
- Backlog service → Task/session supervision domain: manages task state
- Execution service → Outbound ports: requests effects
- Backlog service → Outbound ports: persists backlog
- Outbound ports → Repository & worktree adapter: repository/worktree capability
- Outbound ports → Claude adapter: agent capability
- Repository & worktree adapter → Local Git repository: creates worktrees and branches
- Claude adapter → Claude agent runtime: launches and observes
- Claude adapter → Repository & worktree adapter: runs task in worktree
