# lark-ai-lab/specagent architecture

> Architecture diagram of the GitHub repository lark-ai-lab/specagent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lark-ai-lab/specagent
- Repository: https://github.com/lark-ai-lab/specagent

## Overview

SpecAgent is an OpenAI-compatible gateway for trajectory-level speculation: an agent sends requests through the gateway, the Weak model drafts steps, and the Strong model reviews a candidate prefix before the gateway releases accepted work or repairs a rejected step. The protocol modules provide context shaping, action gating, evidence handling, verdict parsing, and session state; separate resource modules estimate inference cost. The README also documents four benchmark integrations. Edges reflect the README and source-index references; benchmark internals and unsampled call details are not inferred.

Main entry points: [`src/specagent/runtime/gateway.py`](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/runtime/gateway.py) (Session gateway), [`src/specagent/server.py`](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/server.py) (HTTP endpoint), [`src/specagent/protocol/resource_estimate.py`](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/resource_estimate.py) (Resource estimates), [`src/specagent/protocol/accounting.py`](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/accounting.py) (Token accounting)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Gateway runtime"]
  node_server["HTTP endpoint<br/>[server.py]"]
  node_gateway["Session gateway<br/>[gateway.py]"]
  node_auth["Backend auth<br/>[http_auth.py]"]
end

subgraph group_protocol["Review protocol"]
  node_context["Context views<br/>[context.py]"]
  node_side_effects["Action gating<br/>[side_effects.py]"]
  node_evidence["Request evidence<br/>[evidence.py]"]
  node_evidence_review["Payload pruning<br/>[evidence_review.py]"]
  node_verifier["Verdict parser<br/>[verifier.py]"]
  node_review_note["Repair note<br/>[review_note.py]"]
  node_state["Prefix state<br/>[state.py]"]
  node_types["Protocol records<br/>[types.py]"]
end

subgraph group_resources["Resource estimates"]
  node_accounting["Token accounting<br/>[accounting.py]"]
  node_estimator_ids["Estimator labels<br/>[estimator_ids.py]"]
  node_flops["FLOPs estimator<br/>[flops.py]"]
  node_hbm["HBM estimator<br/>[hbm.py]"]
  node_prefix_reuse["Prefix reuse<br/>[prefix_reuse.py]"]
  node_cache_report["Cache report<br/>[cache_report.py]"]
  node_paper["Paper settings<br/>[paper.py]"]
  node_resource_estimate["Resource estimates"]
end

subgraph group_integrations["Benchmark integrations"]
  node_tau2["τ²-Bench adapter"]
  node_automation["AutomationBench adapter"]
  node_vita["VitaBench adapter"]
  node_atlas["MCP-Atlas adapter"]
end

node_agent(("Agent client"))
node_weak["Weak model"]
node_strong["Strong model"]
node_tool_client["Tool client"]

node_agent -->|"sends requests"| node_server
node_server -->|"routes sessions"| node_gateway
node_server -->|"configures auth"| node_auth
node_gateway -->|"drafts with"| node_weak
node_gateway -->|"reviews with"| node_strong
node_gateway -->|"returns tool steps"| node_tool_client
node_gateway -->|"builds views"| node_context
node_gateway -->|"classifies actions"| node_side_effects
node_gateway -->|"collects evidence"| node_evidence
node_gateway -->|"prunes payload"| node_evidence_review
node_gateway -->|"parses verdict"| node_verifier
node_gateway -->|"builds repair note"| node_review_note
node_gateway -.->|"commits prefix"| node_state
node_gateway -->|"uses records"| node_types
node_accounting -->|"tags estimates"| node_estimator_ids
node_flops -->|"estimates from usage"| node_accounting
node_flops -->|"uses labels"| node_estimator_ids
node_hbm -->|"estimates from usage"| node_accounting
node_hbm -->|"uses model data"| node_flops
node_hbm -->|"loads settings"| node_paper
node_cache_report -->|"reports ledger"| node_accounting
node_resource_estimate -->|"uses ledger"| node_accounting
node_resource_estimate -->|"uses cache report"| node_cache_report
node_resource_estimate -->|"computes FLOPs"| node_flops
node_resource_estimate -->|"computes HBM"| node_hbm
node_resource_estimate -->|"loads settings"| node_paper
node_resource_estimate -->|"uses records"| node_types
node_tau2 -.->|"uses endpoint"| node_server
node_automation -.->|"uses endpoint"| node_server
node_vita -.->|"uses endpoint"| node_server
node_atlas -.->|"uses endpoint"| node_server

click node_server "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/server.py"
click node_gateway "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/runtime/gateway.py"
click node_auth "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/http_auth.py"
click node_context "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/context.py"
click node_side_effects "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/side_effects.py"
click node_evidence "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/evidence.py"
click node_evidence_review "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/evidence_review.py"
click node_verifier "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/verifier.py"
click node_review_note "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/runtime/review_note.py"
click node_state "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/state.py"
click node_types "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/types.py"
click node_accounting "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/accounting.py"
click node_estimator_ids "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/estimator_ids.py"
click node_flops "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/flops.py"
click node_hbm "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/hbm.py"
click node_prefix_reuse "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/prefix_reuse.py"
click node_cache_report "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/cache_report.py"
click node_paper "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/paper.py"
click node_resource_estimate "https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/resource_estimate.py"
click node_tau2 "https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/tau2"
click node_automation "https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/automationbench"
click node_vita "https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/vitabench"
click node_atlas "https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/mcp_atlas"

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_server,node_gateway,node_auth,node_agent,node_tool_client toneBlue
class node_context,node_side_effects,node_evidence,node_evidence_review,node_verifier,node_review_note,node_state,node_types toneAmber
class node_accounting,node_estimator_ids,node_flops,node_hbm,node_prefix_reuse,node_cache_report,node_paper,node_resource_estimate toneMint
class node_tau2,node_automation,node_vita,node_atlas toneRose
class node_weak,node_strong toneIndigo
```

## Components

### Gateway runtime

- [HTTP endpoint](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/server.py): `src/specagent/server.py`
- [Session gateway](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/runtime/gateway.py): `src/specagent/runtime/gateway.py`
- [Backend auth](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/http_auth.py): `src/specagent/http_auth.py`

### Review protocol

- [Context views](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/context.py): `src/specagent/protocol/context.py`
- [Action gating](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/side_effects.py): `src/specagent/protocol/side_effects.py`
- [Request evidence](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/evidence.py): `src/specagent/protocol/evidence.py`
- [Payload pruning](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/evidence_review.py): `src/specagent/protocol/evidence_review.py`
- [Verdict parser](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/verifier.py): `src/specagent/protocol/verifier.py`
- [Repair note](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/runtime/review_note.py): `src/specagent/runtime/review_note.py`
- [Prefix state](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/state.py): `src/specagent/protocol/state.py`
- [Protocol records](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/types.py): `src/specagent/protocol/types.py`

### Resource estimates

- [Token accounting](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/accounting.py): `src/specagent/protocol/accounting.py`
- [Estimator labels](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/estimator_ids.py): `src/specagent/protocol/estimator_ids.py`
- [FLOPs estimator](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/flops.py): `src/specagent/protocol/flops.py`
- [HBM estimator](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/hbm.py): `src/specagent/protocol/hbm.py`
- [Prefix reuse](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/prefix_reuse.py): `src/specagent/protocol/prefix_reuse.py`
- [Cache report](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/cache_report.py): `src/specagent/protocol/cache_report.py`
- [Paper settings](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/paper.py): `src/specagent/protocol/paper.py`
- [Resource estimates](https://github.com/lark-ai-lab/specagent/blob/main/src/specagent/protocol/resource_estimate.py): `src/specagent/protocol/resource_estimate.py`

### Benchmark integrations

- [τ²-Bench adapter](https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/tau2): `benchmarks/tau2`
- [AutomationBench adapter](https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/automationbench): `benchmarks/automationbench`
- [VitaBench adapter](https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/vitabench): `benchmarks/vitabench`
- [MCP-Atlas adapter](https://github.com/lark-ai-lab/specagent/tree/main/benchmarks/mcp_atlas): `benchmarks/mcp_atlas`

### Other components

- **Agent client**
- **Weak model**
- **Strong model**
- **Tool client**

## Connections

- Agent client → HTTP endpoint: sends requests (evidence: `README.md`)
- HTTP endpoint → Session gateway: routes sessions (evidence: `src/specagent/server.py`)
- HTTP endpoint → Backend auth: configures auth (evidence: `src/specagent/server.py`)
- Session gateway → Weak model: drafts with (evidence: `README.md`)
- Session gateway → Strong model: reviews with (evidence: `README.md`)
- Session gateway → Tool client: returns tool steps (evidence: `README.md`)
- Session gateway → Context views: builds views (evidence: `src/specagent/runtime/gateway.py`)
- Session gateway → Action gating: classifies actions (evidence: `src/specagent/runtime/gateway.py`)
- Session gateway → Request evidence: collects evidence (evidence: `src/specagent/runtime/gateway.py`)
- Session gateway → Payload pruning: prunes payload (evidence: `src/specagent/runtime/gateway.py`)
- Session gateway → Verdict parser: parses verdict (evidence: `src/specagent/runtime/gateway.py`)
- Session gateway → Repair note: builds repair note (evidence: `src/specagent/runtime/gateway.py`)
- Session gateway → Prefix state: commits prefix
- Session gateway → Protocol records: uses records (evidence: `src/specagent/runtime/gateway.py`)
- Token accounting → Estimator labels: tags estimates (evidence: `src/specagent/protocol/accounting.py`)
- FLOPs estimator → Token accounting: estimates from usage (evidence: `src/specagent/protocol/flops.py`)
- FLOPs estimator → Estimator labels: uses labels (evidence: `src/specagent/protocol/flops.py`)
- HBM estimator → Token accounting: estimates from usage (evidence: `src/specagent/protocol/hbm.py`)
- HBM estimator → FLOPs estimator: uses model data (evidence: `src/specagent/protocol/hbm.py`)
- HBM estimator → Paper settings: loads settings (evidence: `src/specagent/protocol/hbm.py`)
- Cache report → Token accounting: reports ledger (evidence: `src/specagent/protocol/cache_report.py`)
- Resource estimates → Token accounting: uses ledger (evidence: `src/specagent/protocol/resource_estimate.py`)
- Resource estimates → Cache report: uses cache report (evidence: `src/specagent/protocol/resource_estimate.py`)
- Resource estimates → FLOPs estimator: computes FLOPs (evidence: `src/specagent/protocol/resource_estimate.py`)
- Resource estimates → HBM estimator: computes HBM (evidence: `src/specagent/protocol/resource_estimate.py`)
- Resource estimates → Paper settings: loads settings (evidence: `src/specagent/protocol/resource_estimate.py`)
- Resource estimates → Protocol records: uses records (evidence: `src/specagent/protocol/resource_estimate.py`)
- τ²-Bench adapter → HTTP endpoint: uses endpoint
- AutomationBench adapter → HTTP endpoint: uses endpoint
- VitaBench adapter → HTTP endpoint: uses endpoint
- MCP-Atlas adapter → HTTP endpoint: uses endpoint
