Skip to content

refactor(agent-core-v2): make undo domain-owned - #2055

Merged
7Sageer merged 19 commits into
mainfrom
refactor-undo
Jul 27, 2026
Merged

refactor(agent-core-v2): make undo domain-owned#2055
7Sageer merged 19 commits into
mainfrom
refactor-undo

Conversation

@7Sageer

@7Sageer 7Sageer commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

Undo in agent-core-v2 only compensated conversation context, so other conversation-time state could drift from the surviving history. Todo changes, plan mode, and task-notification delivery from an undone turn remained applied. Undo also lacked one domain-owned entry point for validation, idle admission, reconciliation, telemetry, and error handling.

A generic wire-level journal cut was considered, but it made infrastructure own business-time semantics and required every model to declare whether it should travel through conversation time.

What changed

  • Adds an Agent-scoped conversation undo service that serializes undo requests, validates the requested count, and acquires loop quiescence only when no turn or request is active or queued.
  • Returns a structured busy error for active loop work or an already-running compaction without cancelling either operation.
  • Keeps context.undo as the only persisted undo fact. Context memory owns the history cut and token-measurement rebase.
  • Defines Todo, Plan, and task-notification delivery as checkpointed conversation-time models while leaving world-time state such as turn counters and task registries untouched.
  • Reconciles state and projections after the context cut, including task-notification eligibility, Todo state, Plan status, session metadata, telemetry, and the context.undone event.
  • Rebuilds the post-undo transcript from the stable journal projection and realigns restored context with the last surviving turn, preventing restored task notifications from producing an empty phantom turn.
  • Uses loop quiescence as the only admission guard; no separate prompt or compaction launch-pausing API is introduced.
  • Removes the generic wire time-travel design and keeps REST :undo, RPC undoHistory, and the debug surface aligned.

User-visible: /undo now restores conversation history, todo lists, plan mode, and background-task notification state consistently. When a turn is active or queued, or compaction is already running, it returns a busy error and leaves that work untouched.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4800dbf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Replace the compensating context.undo op with a wire-layer rewind
primitive: a log.cut control record with a persisted target, applied
uniformly by the wire during fold. Turn boundaries become first-class
(TurnIndexModel indexing turn.prompt record positions), models declare
a temporal classification (rewindable), and a single
IAgentRewindService owns the undo pipeline (quiesce -> precheck ->
cut -> reconcile) with all entry points converged.

- wire: log.cut record, rewindable model flag, re-fold rebuild;
  OpApplyContext.recordIndex for position-aware reducers
- rewind service: aborts the active turn, cancels in-flight
  compaction, preserves the pending queue, rebases measured tokens,
  reconciles lastPrompt, tracks conversation_undo
- todo list, plan mode, task-notification delivery and the turn index
  now rewind together with the undone turns
- transcript reducer applies cut ranges so snapshot/messages surfaces
  stay consistent with the model context
- REST/RPC/debug undo entry points converge on the rewind service;
  TUI parses the v2 undo-unavailable error shape
- legacy context.undo records keep replaying for old journals
@7Sageer 7Sageer changed the title refactor(agent-core-v2): rebuild undo as wire-level journal rewind refactor(agent-core-v2): make undo domain-owned Jul 22, 2026
7Sageer added 8 commits July 23, 2026 12:07
…luding todo list rollback and improved event handling
Undo now rejects with session.busy while compaction runs instead of
cancelling it, so the awaitable cancel() added for the earlier rewind
semantics has no callers left. Remove it from the interface and
implementation; the RPC cancel path keeps using the task abort
controller directly.
@7Sageer
7Sageer marked this pull request as ready for review July 24, 2026 06:18
# Conflicts:
#	packages/agent-core-v2/src/agent/plan/planOps.ts
#	packages/agent-core-v2/src/agent/plan/planService.ts
#	packages/agent-core-v2/src/agent/task/taskService.ts
#	packages/agent-core-v2/test/harness/agent.ts
#	packages/kap-server/src/services/transcript/coreBinding.ts
#	packages/kap-server/src/services/transcript/coreEventMap.ts
#	packages/kap-server/src/services/transcript/transcriptService.ts
#	packages/transcript/src/history/groupTurns.ts
@pkg-pr-new

pkg-pr-new Bot commented Jul 24, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@4800dbf
npx https://pkg.pr.new/@moonshot-ai/kimi-code@4800dbf

commit: 4800dbf

7Sageer and others added 8 commits July 24, 2026 14:59
…te checkpoint depth

- Rename IAgentConversationUndoReconciliationRegistry to
  IAgentConversationUndoParticipantRegistry (conversationUndoParticipants).
- Return the limiting model from checkpointDepth and include it in the
  SESSION_UNDO_UNAVAILABLE details; report checkpoint_lost instead of
  compaction_boundary when no compaction explains the missing depth.
- Add a registry invariant test: every model reacting to context.* ops
  must be registered via defineCheckpointedModel or explicitly exempt.
# Conflicts:
#	packages/agent-core-v2/src/agent/task/taskService.ts
#	packages/agent-core-v2/src/agent/toolSelect/toolSelectService.ts
Signed-off-by: 7Sageer <sag77r@hotmail.com>
@7Sageer
7Sageer merged commit d40d0d3 into main Jul 27, 2026
14 checks passed
@7Sageer
7Sageer deleted the refactor-undo branch July 27, 2026 03:09
@github-actions github-actions Bot mentioned this pull request Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant