Data: Session State and Storage
Relevant source files
The following files were used as context for generating this wiki page:- packages/opencode/src/storage/storage.ts
- packages/opencode/src/session/message-v2.ts
- packages/opencode/src/snapshot/index.ts
- packages/opencode/src/session/prompt/beast.txt
- packages/opencode/src/session/prompt/codex.txt
- packages/opencode/src/command/template/review.txt
- packages/console/app/src/routes/index.tsx
The OpenCode system manages the state of each interaction with an AI agent through a structured session model. This includes the conversation history, the agent's behavioral configuration, and mechanisms for persisting and sharing this data. The core components are designed to be modular, allowing for different agent personalities and ensuring that session data is handled safely and efficiently.
This document outlines the architecture of session state, the structure of agent configurations, and the storage mechanisms used for persistence and sharing. It covers the data models for messages, the operational workflows defined for agents, and the backend services that manage data I/O.
Session State and Message Structure
The fundamental unit of a session is the message. The system uses a versioned message format, MessageV2, to represent each turn in the conversation. This structure is central to how the agent processes history and formulates responses.
The MessageV2 Data Model
The MessageV2 schema, defined using zod, ensures that all messages within a session are well-formed. It includes the role of the message author, the content, and associated metadata.
The diagram below illustrates the entity relationship for the MessageV2 schema.
The key fields of the MessageV2 object are detailed in the table below.
| Field | Type | Description |
|---|---|---|
id | string | A unique identifier for the message, typically a nanoid. |
role | enum | The author of the message. Can be user, assistant, tool_code, tool_result, or system. |
content | string | The primary text content of the message. |
display | string (optional) | An alternative representation of the content for display purposes. |
tool | object (optional) | Contains information about a tool call, including id, type, call, and result. |
Sources: packages/opencode/src/session/message-v2.ts:9-25
Agent Configuration and Workflow
A significant part of a session's state is the agent's configuration, which is defined by a system prompt. These prompts establish the agent's persona, its core workflow, and its operational guidelines. Different prompt files (beast.txt, codex.txt, review.txt, etc.) allow for specialized agents tailored to specific tasks.
Standard Agent Workflow
Most agents follow a standard, iterative workflow to solve problems. This workflow ensures a systematic approach to understanding, planning, and executing tasks.
The following flowchart shows the typical workflow for an OpenCode agent.
Task-Specific Instructions
Agents can be configured with highly specific instructions for particular tasks. For example, the code-reviewer agent has a detailed set of rules for what to look for, what tools to use, and how to format its feedback.
Code Reviewer Agent Focus Areas:
- Bugs: Logic errors, edge cases, security issues, and broken error handling.
- Structure: Adherence to existing patterns and conventions.
- Performance: Obvious issues like N+1 queries or blocking I/O.
The reviewer is instructed to be certain before flagging an issue and to use tools like the Explore agent to gather context.
Sources: packages/opencode/src/command/template/review.txt:19-45
Data Persistence and Storage
OpenCode uses a dedicated Storage class to manage the persistence of session data and snapshots. This class provides a consistent API for reading from and writing to the file system.
The Storage Class
The Storage class abstracts all file system operations related to session management. It handles creating directories and interacting with session and snapshot files.
This class diagram shows the structure of the Storage class and its methods.
The Storage class is composed of two main sub-modules for handling sessions and snapshots separately.
| Method | Description |
|---|---|
storage.session.write(id, data) | Writes session data to a file. |
storage.session.read(id) | Reads and validates session data from a file. |
storage.session.list() | Lists all existing session IDs. |
storage.snapshot.write(id, data) | Writes snapshot data to a file. |
storage.snapshot.read(id) | Reads and validates snapshot data from a file. |
Sources: packages/opencode/src/storage/storage.ts:25-84
Session Snapshots
Snapshots provide a mechanism to capture and share a specific state of a session. This is useful for debugging, collaboration, or creating shareable links. The snapshot functionality is managed by functions that interact with the Storage class.
The core functions are create and get, which handle writing and reading snapshot data.
The sequence diagram below illustrates the process of creating and retrieving a snapshot.
A snapshot object contains the session messages, a version identifier, and creation metadata.
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the snapshot. |
version | string | The version of the snapshot format. |
createdAt | string | ISO 8601 timestamp of when the snapshot was created. |
messages | MessageV2[] | An array of MessageV2 objects representing the session history. |
Sources: packages/opencode/src/snapshot/index.ts:7-11
Frontend State Management
The web console for OpenCode also manages state, although it is distinct from the backend session state. The frontend handles UI state and fetches data from remote sources.
In packages/console/app/src/routes/index.tsx, state is managed for:
- UI Interactions: The
handleCopyClickfunction manages the visual state of a copy button, providing immediate feedback to the user by toggling adata-copiedattribute. - Remote Data Fetching: The
createAsyncutility from SolidJS is used to fetch data asynchronously. For example, it calls agithub()function to retrieve the latest release information and star count, which are then stored and rendered in the UI.
This demonstrates a separation of concerns, where the frontend manages its own presentation-layer state while the core session state is handled by the backend agent and storage system.
Sources: packages/console/app/src/routes/index.tsx:21-32, packages/console/app/src/routes/index.tsx:18-19
Conclusion
The data and state management system in OpenCode is robust and well-structured. It revolves around the MessageV2 data model for representing conversation history and uses system prompts to define agent behavior. The Storage class provides a clean abstraction for data persistence, while the snapshot feature enables session sharing. This architecture effectively separates concerns between session state, agent configuration, and data storage, creating a flexible and maintainable system for powering the AI coding agent.