-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix(server): reject checkpoint reverts before mutating the workspace #9069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,12 @@ import { isTemporaryWorktreeBranch } from "@t3tools/shared/git"; | |
|
|
||
| import { parseTurnDiffFilesFromUnifiedDiff } from "../../checkpointing/Diffs.ts"; | ||
| import { | ||
| checkpointRefForRevertBase, | ||
| checkpointRefForThreadTurn, | ||
| resolveThreadWorkspaceCwd, | ||
| } from "../../checkpointing/Utils.ts"; | ||
| import * as CheckpointStore from "../../checkpointing/CheckpointStore.ts"; | ||
| import type { ProviderServiceError } from "../../provider/Errors.ts"; | ||
| import { ProviderService } from "../../provider/Services/ProviderService.ts"; | ||
| import { CheckpointReactor, type CheckpointReactorShape } from "../Services/CheckpointReactor.ts"; | ||
| import { forkParked } from "../../serverActivation.ts"; | ||
|
|
@@ -755,6 +757,15 @@ const make = Effect.gen(function* () { | |
| return; | ||
| } | ||
|
|
||
| // Snapshot the current worktree before touching anything so a failed | ||
| // provider rollback can be compensated back to the exact pre-revert | ||
| // state — prior checkpoints can miss uncommitted local edits. | ||
| const revertBaseRef = checkpointRefForRevertBase(event.payload.threadId); | ||
| yield* checkpointStore.captureCheckpoint({ | ||
| cwd: sessionRuntime.value.cwd, | ||
| checkpointRef: revertBaseRef, | ||
| }); | ||
|
|
||
| const restored = yield* checkpointStore.restoreCheckpoint({ | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| cwd: sessionRuntime.value.cwd, | ||
| checkpointRef: targetCheckpointRef, | ||
|
|
@@ -776,13 +787,40 @@ const make = Effect.gen(function* () { | |
|
|
||
| const rolledBackTurns = Math.max(0, currentTurnCount - event.payload.turnCount); | ||
| if (rolledBackTurns > 0) { | ||
| yield* providerService.rollbackConversation({ | ||
| threadId: sessionRuntime.value.threadId, | ||
| numTurns: rolledBackTurns, | ||
| }); | ||
| const rollbackFailure = yield* providerService | ||
| .rollbackConversation({ | ||
| threadId: sessionRuntime.value.threadId, | ||
| numTurns: rolledBackTurns, | ||
| }) | ||
| .pipe( | ||
| Effect.map(() => Option.none<ProviderServiceError>()), | ||
| Effect.catch((error) => Effect.succeed(Option.some(error))), | ||
| ); | ||
| if (Option.isSome(rollbackFailure)) { | ||
| // Compensate: the workspace moved but the conversation did not. | ||
| // Restore the pre-revert snapshot so both sides stay in their | ||
| // original state and retrying the revert starts from a clean slate. | ||
| yield* checkpointStore | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium When provider rollback fails, the compensation restores file contents but leaves any pre-existing staged edits unstaged, so it does not restore the claimed exact pre-revert state. 🤖 Copy this AI Prompt to have your agent fix this: |
||
| .restoreCheckpoint({ | ||
| cwd: sessionRuntime.value.cwd, | ||
| checkpointRef: revertBaseRef, | ||
| fallbackToHead: false, | ||
| }) | ||
| .pipe( | ||
| Effect.andThen(workspaceEntries.refresh(sessionRuntime.value.cwd)), | ||
| Effect.catch(() => Effect.void), | ||
| ); | ||
| yield* appendRevertFailureActivity({ | ||
| threadId: event.payload.threadId, | ||
| turnCount: event.payload.turnCount, | ||
| detail: rollbackFailure.value.message, | ||
| createdAt: now, | ||
| }).pipe(Effect.catch(() => Effect.void)); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const staleCheckpointRefs: Array<CheckpointRef> = []; | ||
| const staleCheckpointRefs: Array<CheckpointRef> = [revertBaseRef]; | ||
| for (const checkpoint of thread.checkpoints) { | ||
| if (checkpoint.checkpointTurnCount > event.payload.turnCount) { | ||
| staleCheckpointRefs.push(checkpoint.checkpointRef); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.