> For the complete documentation index, see [llms.txt](https://www.opencodebook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opencodebook.xyz/en/chapter_04_session_system/4.6_session_state_management.md). Agents can also search and read this documentation through its [MCP server](https://www.opencodebook.xyz/~gitbook/mcp).

# 4.6 Session State Management

> **Model**: claude-opus-4-6 (anthropic/claude-opus-4-6) **Generation Date**: 2025-02-17

***

## 4.6.1 SessionStatus: Session States

`SessionStatus` (`session/status.ts`) manages the real-time state of each session. There are three primary states:

```
idle ---> busy ---> idle
               +---> pending (waiting for user input, e.g., permission confirmation)
```

* **`idle`**: Idle. The session has no active LLM requests.
* **`busy`**: Busy. The LLM is generating a response or a tool is executing.
* **`pending`**: Waiting. The Agent requires user input (such as a permission confirmation or an interactive question).

State change triggers:

```
User sends a message       -> busy
LLM starts streaming       -> busy
Permission request pops up -> pending
User confirms permission   -> busy (execution continues)
LLM completes response     -> idle
User aborts the session    -> idle
An error occurs            -> idle (with error information)
```

These states are broadcast to the frontend via the event bus, driving UI elements such as loading indicators and input field disabling.

## 4.6.2 SessionSummary: Diff Summary Generation

`SessionSummary` (`session/summary.ts`) asynchronously generates a file change summary after each Step completes:

```typescript
SessionSummary.summarize({
  sessionID: input.sessionID,
  messageID: input.assistantMessage.parentID,
})
```

Summary contents:

* `additions`: Number of lines added
* `deletions`: Number of lines deleted
* `files`: Number of files changed
* `diffs`: Diff details for each file

These summaries are stored in the `summary` field of the User message, used to quickly display "which files were modified in this conversation" in the session list.

## 4.6.3 The Session Revert Mechanism

`SessionRevert` (`session/revert.ts`) provides file rollback capability -- restoring the working directory to the state at a specific point in the session.

**Revert Flow**:

```
1. User selects the message/step to revert to
       |
2. Find the Snapshot corresponding to that step
       |
3. Snapshot.restore(snapshotHash)  -> Restore files
       |
4. Record revert information in Session.Info
   session.revert = {
     messageID: "...",
     partID: "...",
     snapshot: "...",
     diff: "..."
   }
       |
5. Subsequent messages can continue based on the reverted state
```

**Unrevert**: If the user wants to undo the revert operation, the `unrevert` API can restore the state to what it was before the revert.

**Cleanup**: Before processing each new message, `SessionRevert.cleanup()` clears expired revert states to ensure they do not interfere with new operations.

## 4.6.4 The Complete Session Lifecycle

Connecting all the topics in this chapter, the complete lifecycle of a Session is as follows:

```
Creation (Session.create / Session.createNext)
|   Sets id, slug, projectID, directory, title
|   Publishes Session.Event.Created
|
v
Active Phase
|   +--- User sends message -> SessionPrompt.prompt()
|   |       +-- Creates User Message
|   |       +-- Enters Agentic Loop
|   |             +-- LLM.stream() -> Streaming generation
|   |             +-- Tool call -> Execute -> Feed back results
|   |             +-- Doom Loop detection
|   |             +-- Snapshot recording
|   |             +-- Compaction check
|   |
|   +--- Token accumulation -> Prune -> Compaction
|   +--- User reverts -> Revert
|   +--- User forks -> Fork
|   +--- Loop...
|
v
Sharing (optional)
|   Session.share() -> Generate a sharing URL
|
v
Archiving
|   session.time.archived = Date.now()
|
v
Deletion
    Session.delete() -> Clean up data
    Publishes Session.Event.Deleted
```

***

With this, the 6 core aspects of the Session system -- data model, message model, entry flow, Agentic Loop, Compaction, and state management -- have been fully covered. The Session is OpenCode's most central module; the Tool, Agent, and Provider systems discussed in subsequent chapters are all "components" that serve the Session.
