Two-Stage Compaction Pipeline
The compaction pipeline follows a Preview → Apply pattern. This ensures that the operator can review what information will be summarized or discarded before the session’s durable record is modified.1. Compaction Preview
The system analyzes the session transcript to identify “review candidates”—specific items like tool outputs or assistant turns that are eligible for summarization.- Trigger: Initiated via
/compact previewin theChatComposerapps/web/src/chat/chatShared.tsx#145-151 or the Sessions section apps/web/src/console/sections/SessionsSection.tsx#156-183. - Logic: The
previewSessionCompactionAPI returns aChatCompactionPreviewcontaining atoken_delta(estimated savings) and a list of candidates apps/web/src/chat/chatPhase4Actions.ts#58-61. - UI State: A “meta” entry is appended to the local transcript, and the
DetailPanelStateis updated to show the preview details apps/web/src/chat/chatPhase4Actions.ts#64-113.
2. Compaction Apply
Once reviewed, the compaction is committed to the database.- Logic:
applySessionCompactionis called with optionalaccept_candidate_idsorreject_candidate_idsapps/web/src/chat/chatPhase4Actions.ts#70-74. - Artifacts: Upon success, a
ChatCompactionArtifactRecordis created, representing the summarized state apps/web/src/chat/chatPhase4Actions.ts#44-49.
Compaction Flow Data Path
The following diagram illustrates the flow from the user command to the backend and back to the UI state machine. Title: Compaction Execution Flow Sources: apps/web/src/chat/chatPhase4Actions.ts#33-120, apps/web/src/chat/ChatConsolePanel.tsx#17-22, apps/web/src/chat/chatShared.tsx#145-151Checkpoint Creation & Restore
Checkpoints are immutable snapshots of a session’s state, including its transcript, workspace documents, and compaction history.Creation
Checkpoints are created viaapi.createSessionCheckpoint apps/web/src/console/sections/SessionsSection.tsx#142-146. They capture the branch_state and can be tagged for easier discovery apps/web/src/chat/chatConsoleUtils.ts#81-100.
Restore
Restoring a checkpoint is a branching operation. It does not overwrite the current session; instead, it creates a new session based on the checkpoint’s state.- Function:
restoreCheckpointActionapps/web/src/chat/chatPhase4Actions.ts#146-169. - Side Effects:
- Calls
api.restoreSessionCheckpointapps/web/src/chat/chatPhase4Actions.ts#158. - Clears the local transcript state apps/web/src/chat/chatPhase4Actions.ts#165.
- Refreshes the session list and selects the new session apps/web/src/chat/chatPhase4Actions.ts#163-167.
- Calls
Checkpoint and Compaction Entity Relationship
Title: Continuity Entity Mapping Sources: apps/web/src/chat/chatConsoleUtils.ts#50-100, apps/web/src/console/sections/SessionsSection.tsx#44-46, apps/web/src/chat/ChatInspectorColumn.tsx#3-15The Detail Panel State Machine
TheDetailPanelState is the central mechanism for inspecting Phase 4 artifacts without cluttering the main chat transcript.
Implementation
The state is managed inChatConsolePanel.tsx apps/web/src/chat/ChatConsolePanel.tsx#113 and passed to the ChatInspectorColumn apps/web/src/chat/ChatInspectorColumn.tsx#93.
Key Transformation Functions
ThechatConsoleUtils.ts file provides “builders” that map raw API records to the DetailPanelState interface:
buildDetailFromCompactionArtifact: Formats compaction summaries and links related checkpoints apps/web/src/chat/chatConsoleUtils.ts#50-79.buildDetailFromCheckpointRecord: Displays checkpoint notes and workspace paths apps/web/src/chat/chatConsoleUtils.ts#81-100.buildDetailFromLiveEntry: Used for inspecting payloads of active tool calls apps/web/src/chat/chatConsoleUtils.ts#22-30.
Action Integration
TheDetailPanelState includes an actions array apps/web/src/chat/ChatInspectorColumn.tsx#50-55. This allows the UI to render context-specific buttons (e.g., “Restore Checkpoint”, “Accept Candidates”) directly within the inspection view.
| Source Record | Builder Function | Key Actions |
|---|---|---|
ChatCompactionPreview | buildCompactionPreviewDetail | Accept All, Reject All |
ChatCompactionArtifactRecord | buildDetailFromCompactionArtifact | View Source Records |
ChatCheckpointRecord | buildDetailFromCheckpointRecord | Restore Checkpoint |
ChatBackgroundTaskRecord | buildDetailFromBackgroundTask | Pause, Resume, Retry, Cancel |
Token Delta Calculation
The system provides real-time feedback on context usage through theuseChatContextBudget hook and Phase 4 status entries.
- Baseline vs. Draft: The budget differentiates between
baseline_tokens(persisted history) anddraft_tokens(unsent text in composer) apps/web/src/chat/chatShared.tsx#54-65. - Compaction Savings: When a compaction preview is generated, the
token_deltarepresents the difference between the current baseline and the projected summarized baseline apps/web/src/chat/chatPhase4Actions.ts#117. - Visual Indicators: The
ContextBudgetSummaryassigns atone(“default”, “warning”, “danger”) based on thresholds likeCONTEXT_BUDGET_SOFT_LIMIT(12,000 tokens) apps/web/src/chat/chatShared.tsx#22-23.