Session Management
Relevant source files
The following files were used as context for generating this wiki page:
Session Management is a core component of OpenCode, responsible for tracking the entire lifecycle of a user's interaction with the AI agent. It encapsulates conversational history, file modifications, and state changes within a structured and persistent format. A session can be thought of as a continuous conversation or task, which can be created, forked, reverted, and summarized.
The system is built around a set of REST API endpoints that manage session entities and their related data, such as messages and file diffs. A command-line interface (CLI) provides user-facing tools to interact with these sessions. The underlying architecture supports complex operations like forking a conversation at a specific point, reverting file changes made by the agent, and compacting session history for performance.
Session Data Model
The session data model is defined by several interconnected schemas. The central entity is the Session, which is linked to messages, diffs, and potentially a parent session.
This diagram illustrates the relationships between the main data entities involved in session management.
Sources: packages/sdk/stainless/openapi.json
Session Schema
The Session object contains metadata about the conversation.
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the session, prefixed with ses. |
projectID | string | The ID of the project this session belongs to. |
directory | string | The working directory of the session. |
parentID | string | The ID of the parent session, if this session is a fork. |
title | string | A user-defined or auto-generated title for the session. |
version | string | The version of OpenCode that created the session. |
time | object | Timestamps for creation, updates, and compaction. |
summary | object | Contains a summary of changes, including an array of FileDiff objects. |
share | object | Contains the share URL if the session is shared. |
revert | object | Contains information about a reverted state, including messageID and snapshot. |
Sources: packages/sdk/stainless/openapi.json
Session Lifecycle and API
The session lifecycle is managed through a series of REST API endpoints. These endpoints allow for creating, retrieving, updating, and deleting sessions and their associated data.
Core API Endpoints
| Method | Endpoint | Operation ID | Description |
|---|---|---|---|
GET | /session | session.list | List all sessions for the current project. |
POST | /session | session.create | Create a new session. |
GET | /session/{id} | session.get | Retrieve a specific session by its ID. |
PATCH | /session/{id} | session.update | Update a session's properties (e.g., its title). |
DELETE | /session/{id} | session.delete | Delete a session and all its data. |
POST | /session/{id}/fork | session.fork | Create a new session by forking an existing one at a specific message. |
POST | /session/{id}/abort | session.abort | Abort a running session. |
Sources: packages/sdk/stainless/openapi.json
Session Creation Flow
The following diagram shows the process of creating a new session.
Session Interaction
Users interact with a session primarily by sending prompts. Each prompt creates a new user message, which is then processed by the agent, resulting in an assistant message containing various parts like text, tool calls, and file patches.
Prompting Flow
This sequence diagram illustrates how a user prompt is processed.
The POST /session/{id}/message endpoint is central to this flow. It accepts a payload containing message parts (text, files) and model information. The server then orchestrates the interaction with the configured AI provider.
Sources: packages/sdk/stainless/openapi.json
State Management Features
Session management includes several advanced features for controlling the state of the project and conversation.
Reverting Changes
OpenCode allows users to revert the file system changes made during a specific message exchange. The revert operation creates a snapshot of the current state and applies a reverse diff to restore files to their previous state.
The revert state is stored in the session.json file.
The revert functionality is exposed via the POST /session/{id}/revert and POST /session/{id}/unrevert API endpoints.
Session Compaction
To manage the size and performance of long-running sessions, a compaction process is available. Compaction merges consecutive file patches into a single, more efficient representation. This is particularly useful for reducing the amount of data that needs to be loaded and processed. The compact function in compaction.ts is responsible for this logic. A compacting timestamp is stored in the session's time object to indicate when this process is running.
Sources: packages/opencode/src/session/compaction.ts, packages/sdk/stainless/openapi.json
Session Forking
A session can be "forked" from any message within it, creating a new, independent session that shares the history up to that point. This is useful for exploring alternative paths or solutions without altering the original conversation. The POST /session/{id}/fork endpoint takes a messageID and creates a new session with its parentID set to the original session's ID.
Sources: packages/sdk/stainless/openapi.json
Command-Line Interface (CLI)
A set of CLI commands are provided for direct interaction with session management. These commands are defined in packages/opencode/src/cli/cmd/session.ts.
| Command | Description |
|---|---|
opencode session list | Lists all available sessions. |
opencode session new | Creates a new session. |
opencode session rm <id> | Removes a session by its ID. |
opencode session export <id> | Exports a session's conversation history. |
opencode session compact <id> | Compacts a session to optimize its size. |
opencode session share <id> | Generates a shareable link for a session. |
opencode session unshare <id> | Revokes a shared link for a session. |
Sources: packages/opencode/src/cli/cmd/session.ts
UI Representation
In the user interface, each user-assistant interaction is rendered as a SessionTurn. This component displays the user's prompt and the agent's response, including summaries, file diffs, and detailed tool execution steps. It dynamically updates as the agent works, showing progress and final results. The component relies on session data, such as message.summary.diffs, to render file changes in an accordion view.
The SessionTurn component also handles showing and hiding details, displaying errors, and managing the visual state (e.g., typewriter effect for titles) based on whether the content has been seen before in the browser's sessionStorage.