Status: initial runtime slice implemented; selector UI and ZCode app-server projection still planned. Date: 2026-05-11.
This document turns the existing rewind/compact/checkpoint contract into an
implementation plan. It should be read with docs/design/v2/rewind-compact.md
and docs/design/v2/compact/README.md.
ZCode rewind should let users return to a previous user-authored point without making compact, checkpoint, or resume state ambiguous.
The product behavior is:
/rewind opens a message selector in interactive clients./rewind <messageId> rewinds conversation to before that user message when
the message is still in the active chain./rewind code <messageId> or an equivalent picker option restores only the
workspace checkpoint for that message./rewind both <messageId> restores both conversation and workspace when the
target is in the active chain and a workspace checkpoint exists./fork <checkpointId> path. File-only rewind may still
run if the checkpoint artifact exists.This keeps three concepts separate:
The rewind UX is defined below; state ownership stays in session/runtime contracts rather than TUI-local state:
/rewind is a local command that opens a message selector instead of mutating
state directly.fileHistoryTrackEdit() before mutation and the query loop creates a
snapshot for each accepted user message.ZCode implements this UX through session/runtime contracts instead of TUI-owned arrays. TUI and ZCode app-server should consume the same target evaluation results.
Already implemented:
CheckpointCreatedPayload, RewindTriggeredPayload,
WorkspaceCheckpointArtifact, and evaluateRewindTarget().messageId values are anchored to the user message that
opened the turn, while toolMessageId preserves the assistant message that
owns the tool part. Older checkpoint events may still only have the assistant
message id./rewind latest and /rewind <checkpointId> restore workspace files./fork latest and /fork <checkpointId> create a child session from a
workspace checkpoint.CompactBoundary payloads and replaces the active
provider history.Initial slice implemented on 2026-05-11:
/rewind message <messageId> and /rewind conversation <messageId> rewind
the active provider-visible conversation to before an active user message./rewind code <messageId> resolves the latest workspace checkpoint for that
message and restores it./rewind both <messageId> composes workspace restore and active conversation
rewind when the target is still in the active chain.Still missing for full conversation rewind UX:
RewindTargetKind:
message: user-authored message id.checkpoint: workspace checkpoint id.latest: latest workspace checkpoint or latest selectable message depending
on command mode.RewindMode:
conversation: restore provider-visible message chain only.workspace: restore files only.both: restore conversation and workspace in one audited turn.fork: create a child session when conversation rewind crosses compact.summarize: optional later mode that performs partial compact around the
selected message instead of restoring.RewindTargetSummary:
messageIdcheckpointId?createdAtpreviewmodeAvailabilitystrategycompactBoundaryId?coveredByCompactfileCount?diffStats?disabledReason?| Target | Requested mode | Checkpoint | Compact status | Strategy |
|---|---|---|---|---|
| active message | conversation | any | active chain | active_chain |
| active message | workspace | yes | active chain | active_chain |
| active message | both | yes | active chain | active_chain |
| active message | workspace/both | no | active chain | unavailable |
| compact-covered message | conversation/both | any | covered | fork_required |
| compact-covered message | workspace | yes | covered | file_only |
| missing message | any | any | missing | unavailable |
| checkpoint id only | workspace | yes | any | active_chain or file_only |
Important: both is not two unrelated operations. It is one runtime turn that
emits one rewind_triggered event with scope=both, records the selected
message/checkpoint, restores files, then replaces active conversation state. If
the file restore fails, conversation state must not be truncated.
Add a reusable pure result type in packages/contracts/src/rewind:
export interface RewindTargetSummary {
checkpointId?: string;
compactBoundaryId?: string;
coveredByCompact: boolean;
createdAt: number;
disabledReason?: string;
fileCount?: number;
messageId: MessageId;
modeAvailability: Record<RewindMode, boolean>;
preview?: string;
strategy: RewindStrategy;
}
The runtime should return these summaries to TUI/ZCode app-server instead of making clients parse raw messages, compact parts, or checkpoint events.
Extend RewindTriggeredPayload only if needed. Prefer using the existing
fields:
scope=conversation | bothstrategy=active_chain | fork_required | unavailabletargetMessageIdtargetCheckpointId?compactBoundaryId?restoredSnapshotRef?createdMessageId?reasonIf post-rewind hidden history needs durable metadata, add
conversationCutMessageId or removedMessageCount rather than storing removed
message bodies in the event.
Do not hard-delete messages for conversation rewind in P0. Add a contract that lets runtime persist a logical active-chain override:
interface SessionConversationRewind {
rewindId: string;
targetMessageId: MessageId;
keepBeforeMessageId: MessageId;
removedMessageCount: number;
compactBoundaryId?: string;
createdAt: number;
}
Implementation options:
revert/rewind metadata plus a
rewind_triggered event; message hydrator applies the latest rewind cut when
reconstructing active messages.This preserves audit history and avoids deleting attachments, tool artifacts, or compact summaries.
Add listRewindTargets({ limit, includeCoveredByCompact }) to runtime and
bootstrap app facade.
Rules:
Tests:
workspace and both.workspace only if the checkpoint exists
and conversation as fork-required.Current workspace checkpoint selection is checkpoint-id first, and checkpoint events point at assistant/tool messages. Conversation rewind needs a user-message view. Add explicit turn ownership to checkpoint metadata:
targetMessageId or userMessageId: the user-authored message the restore
target should appear under.toolMessageId or assistantMessageId: the assistant message that owns the
tool part and artifact.Then add message-id lookup:
selectCheckpointForMessage(events, messageId)This is the biggest difference from the current P0, where each successful file mutation creates a one-file checkpoint anchored to the assistant/tool message. Conversation rewind needs a message-level checkpoint view keyed by the user's turn.
Tests:
Add rewindConversationToMessage({ targetMessageId }).
Algorithm:
SessionStorePort.messages().messageHistory from kept chain.rewind_triggered with scope=conversation.The selected user prompt should be returned in the command result as
restoreInput for TUI/ZCode protocol clients. Runtime should not directly mutate TUI input
state.
Tests:
Add rewindToMessage({ targetMessageId, mode: "both" }).
Order:
FileSystemPort.Failure rule:
Tests:
For targets before the latest compact boundary:
conversation and both return fork_required.forkConversationFromMessage() later, but P0 should reuse
checkpoint fork where possible.workspace remains available if checkpoint artifacts exist and emits
strategy=file_only.Tests:
TUI:
/rewind should open a message selector, not the checkpoint selector./rewind latest, /rewind <checkpointId>, and /fork <checkpointId> as
scriptable compatibility paths until message-id syntax is finalized.ZCode app-server:
CLI non-interactive:
Keep current compatibility:
/rewind opens the selector in TUI or shows status in non-interactive clients./rewind latest restores latest workspace checkpoint for now./rewind <checkpointId> restores that workspace checkpoint./fork <checkpointId> forks from a checkpoint.Introduce message-id syntax behind tests:
/rewind message <messageId>/rewind code <messageId>/rewind both <messageId>/rewind fork <messageId>Do not overload bare /rewind <id> until checkpoint ids and message ids have
unambiguous prefixes in all persisted data.
Emit structured logs/events for:
rewind.targets.listedrewind.target.evaluatedrewind.startedrewind.workspace.restore.startedrewind.workspace.restore.completedrewind.conversation.apply.startedrewind.conversation.apply.completedrewind.failedEvery operation must carry the existing traceId, turnId, and child span
context. Errors should use stable reasons, not message text:
target_message_not_foundtarget_not_user_authoredtarget_covered_by_compact_requires_forkcheckpoint_required_for_workspace_rewindcheckpoint_snapshot_unavailableconversation_rebuild_failedsession_store_not_configuredfile_system_port_not_configuredContracts:
packages/contracts/tests/rewind.test.ts for message target
evaluation, mode availability, and compact-covered fork-required cases.Core runtime:
CLI/TUI:
/rewind opens message selector when available.ZCode app-server/bootstrap:
restoreInput instead of relying on terminal-only state.Each step should be a separate feature-level commit with focused tests.
/rewind latest eventually mean latest user message, or remain latest
workspace checkpoint for backwards compatibility?