MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / undo

Method undo

packages/agent-core/src/agent/context/index.ts:181–239  ·  view source on GitHub ↗
(count: number)

Source from the content-addressed store, hash-verified

179 }
180
181 undo(count: number): void {
182 if (count <= 0) return;
183 if (this._history.length === 0) return;
184
185 this.agent.records.logRecord({ type: 'context.undo', count });
186
187 let removedUserCount = 0;
188 const removedMessages = new Set<ContextMessage>();
189 let stoppedAtBoundary = false;
190 for (let i = this._history.length - 1; i >= 0; i--) {
191 const message = this._history[i];
192 if (message === undefined) continue;
193 if (message.origin?.kind === 'injection') continue;
194 if (message.origin?.kind === 'compaction_summary') {
195 stoppedAtBoundary = true;
196 break;
197 }
198
199 removedMessages.add(message);
200 this._history.splice(i, 1);
201 this.agent.injection.onContextMessageRemoved(i);
202
203 if (i < this.tokenCountCoveredMessageCount) {
204 this.tokenCountCoveredMessageCount--;
205 this._tokenCount -= estimateTokensForMessages([message]);
206 }
207
208 if (isRealUserInput(message)) {
209 removedUserCount++;
210 if (removedUserCount >= count) break;
211 }
212 }
213
214 this.agent.replayBuilder.removeLastMessages(removedMessages);
215
216 this.openSteps.clear();
217 this.pendingToolResultIds.clear();
218 this.deferredMessages = [];
219 this.agent.microCompaction.reset(this._history.length);
220 this.agent.emitStatusUpdated();
221
222 if (
223 !this.agent.records.restoring &&
224 (stoppedAtBoundary || removedUserCount < count)
225 ) {
226 throw new KimiError(
227 ErrorCodes.REQUEST_INVALID,
228 formatUndoUnavailableMessage(count, removedUserCount, stoppedAtBoundary),
229 {
230 details: {
231 reason: 'undo_limit',
232 requestedCount: count,
233 undoableCount: removedUserCount,
234 stoppedAtCompaction: stoppedAtBoundary,
235 },
236 },
237 );
238 }

Callers

nothing calls this directly

Calls 10

isRealUserInputFunction · 0.90
logRecordMethod · 0.80
removeLastMessagesMethod · 0.80
emitStatusUpdatedMethod · 0.80
addMethod · 0.65
clearMethod · 0.65
resetMethod · 0.65

Tested by

no test coverage detected