# Architecture

Gojent is an intentionally small Go agent runtime composed of four standard-library-only core packages (`core/agent`, `core/llm`, `core/tool`, `core/session`), ready-made `tools`, and optional child modules for providers, durable stores, and MCP. Rather than shipping a workflow engine or hidden global state, an agent is an immutable Go value, a run is a method call, and composition is ordinary Go code.

## Interfaces only at the seams

The core exposes four small extension points and keeps the execution loop itself private:

- [`llm.Model`](https://pkg.go.dev/github.com/ijaidev/gojent/core/llm#Model) (optionally [`llm.Streamer`](https://pkg.go.dev/github.com/ijaidev/gojent/core/llm#Streamer)), with `Info()` mandatory so the runner checks declared capabilities instead of probing.
- [`tool.Tool`](https://pkg.go.dev/github.com/ijaidev/gojent/core/tool#Tool) for typed, schema-validated capabilities.
- [`session.Store`](https://pkg.go.dev/github.com/ijaidev/gojent/core/session#Store) for loading and atomically appending transcripts.
- [`agent.Policy`](https://pkg.go.dev/github.com/ijaidev/gojent/core/agent#Policy), [`agent.Approver`](https://pkg.go.dev/github.com/ijaidev/gojent/core/agent#Approver), and [`agent.Sink`](https://pkg.go.dev/github.com/ijaidev/gojent/core/agent#Sink) for runtime gating and observation.

`Generate` and `Stream` are public adapters over the private loop. Cross-cutting concerns such as tracing, caching, metrics, or redaction belong in wrappers around these seams rather than new core hooks. See [Extending Gojent](/docs/guides/extending) for seam middleware examples.

## Immutable agents, per-run configuration

An `agent.Agent` represents reusable behavior: instructions, a model, sorted tools, and optional defaults. Once constructed with `agent.New`, a run cannot mutate the agent, making a single `*agent.Agent` safe for concurrent executions.

Per-run configuration lives in `agent.RunOptions`, the single public per-run config type:

- `Generate` and `Stream` accept functional `agent.Option` values (such as `agent.WithSession` or `agent.WithMaxSteps`).
- `GenerateWith`, `StreamWith`, `GenerateMessagesWith`, and `StreamMessagesWith` accept a complete `agent.RunOptions` struct directly.
- `(*Agent).ApplyAgentOptions` resolves precedence (`package defaults < agent.Config < RunOptions`) with field-by-field overrides for `Budget` and `RetryPolicy`.

## Fail closed

Gojent rejects ambiguous or unsupported states explicitly instead of silently degrading:

- **Policy gates:** Without a policy, tools execute directly. Once a policy is attached, `agent.DecisionUnknown` (zero value) and any out-of-range `Decision` resolve to `agent.Deny`, and `agent.Ask` without an `Approver` is denied. See [Policies & Approvals](/docs/core/policies).
- **Provider capabilities:** Adapters declare capabilities in `llm.Info.Features` and reject unsupported options (such as stop sequences or strict JSON Schema on unsupported endpoints) with `llm.ErrUnsupported` rather than dropping fields.
- **Sealed message parts:** `llm.Message` uses sealed content parts with a format version; unknown JSON part types fail closed with `llm.ErrUnknownPartType`.

## Atomic persistence

A `session.Store` implements exactly two methods: `Load` and `Append`. During each step, the runner persists the assistant tool-call message and all of its corresponding tool results in one atomic `Append` call, ensuring a process crash cannot leave a transcript with dangling unanswered tool calls. See [Sessions & Stores](/docs/core/sessions).

## Package layout and one-way boundaries

Dependency direction is strictly one-way and enforced by `just arch-check`:

| Package / Module | Responsibility | Dependency Posture |
| --- | --- | --- |
| `core/agent` | Immutable `Agent`, execution loop, policies, events | Root module (stdlib-only) |
| `core/llm` | `Model`, canonical `Message`/`Request`/`Response`, streams, errors | Root module (stdlib-only) |
| `core/tool` | `Tool`, typed `Config`, JSON Schema generation and validation | Root module (stdlib-only) |
| `core/session` | `Session` identity, two-method `Store`, `Memory` | Root module (stdlib-only) |
| `tools` | Ready-made filesystem, exec, web, and task tools | Root module (stdlib-only) |
| `providers/mockllm` | Deterministic in-memory model for tests and examples | Root module (stdlib-only) |
| `providers/anthropic`, `google`, `openai` | Provider SDK adapters behind `llm.Model` | Separate `go.mod` modules |
| `stores/sqlite`, `postgres` | Durable `session.Store` backends | Separate `go.mod` modules |
| `mcp` | Client adapter for stdio and Streamable HTTP MCP servers | Separate `go.mod` module |
| `codjent` | Sample ACP v2 coding agent and provider manager TUI | Separate `go.mod` module |

Root packages never import child modules, and sibling provider or store modules never import one another.

## Non-goals

The core runtime deliberately excludes:

- a workflow engine, graph DSL, or node editor;
- a UI or hosted service (`codjent` is an optional sample outside the core);
- a vector database or retrieval framework;
- provider-specific concepts in core contracts;
- third-party dependencies in the root `go.mod`.

Optional integrations live in separate modules fetched with `go get`. Browse the complete root package index on [pkg.go.dev](https://pkg.go.dev/github.com/ijaidev/gojent).
