-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: preserve full conversation history when restoring checkpoints before context compression #9311
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
Open
roomote
wants to merge
1
commit into
main
Choose a base branch
from
fix/context-compression-checkpoint-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
src/services/checkpoints/__tests__/api-snapshots.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" | ||
| import * as fs from "fs/promises" | ||
| import * as path from "path" | ||
| import { ShadowCheckpointService } from "../ShadowCheckpointService" | ||
| import { fileExistsAtPath } from "../../../utils/fs" | ||
| import { safeWriteJson } from "../../../utils/safeWriteJson" | ||
| import type { ApiMessage } from "../../../core/task-persistence" | ||
|
|
||
| vi.mock("fs/promises") | ||
| vi.mock("../../../utils/fs") | ||
| vi.mock("../../../utils/safeWriteJson") | ||
|
|
||
| describe("API History Snapshots", () => { | ||
| let service: ShadowCheckpointService | ||
| const mockTaskId = "test-task-123" | ||
| const mockCheckpointsDir = "/mock/checkpoints" | ||
| const mockWorkspaceDir = "/mock/workspace" | ||
| const mockLog = vi.fn() | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| // Create a concrete subclass for testing | ||
| class TestCheckpointService extends ShadowCheckpointService { | ||
| constructor() { | ||
| super(mockTaskId, mockCheckpointsDir, mockWorkspaceDir, mockLog) | ||
| } | ||
| } | ||
| service = new TestCheckpointService() | ||
| }) | ||
|
|
||
| describe("saveApiHistorySnapshot", () => { | ||
| it("should save API history snapshot to the correct path", async () => { | ||
| const commitHash = "abc123" | ||
| const apiHistory: ApiMessage[] = [ | ||
| { | ||
| role: "user", | ||
| content: "Test message 1", | ||
| ts: 1000, | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: "Test response 1", | ||
| ts: 2000, | ||
| }, | ||
| ] | ||
|
|
||
| await service.saveApiHistorySnapshot(commitHash, apiHistory) | ||
|
|
||
| const expectedPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| expect(safeWriteJson).toHaveBeenCalledWith(expectedPath, apiHistory) | ||
| }) | ||
|
|
||
| it("should handle empty API history", async () => { | ||
| const commitHash = "def456" | ||
| const apiHistory: ApiMessage[] = [] | ||
|
|
||
| await service.saveApiHistorySnapshot(commitHash, apiHistory) | ||
|
|
||
| const expectedPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| expect(safeWriteJson).toHaveBeenCalledWith(expectedPath, apiHistory) | ||
| }) | ||
|
|
||
| it("should handle API history with context compression markers", async () => { | ||
| const commitHash = "ghi789" | ||
| const apiHistory: ApiMessage[] = [ | ||
| { | ||
| role: "user", | ||
| content: "Initial message", | ||
| ts: 1000, | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: "Response", | ||
| ts: 2000, | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: "Compressed context summary", | ||
| ts: 3000, | ||
| isSummary: true, | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: "After compression", | ||
| ts: 4000, | ||
| }, | ||
| ] | ||
|
|
||
| await service.saveApiHistorySnapshot(commitHash, apiHistory) | ||
|
|
||
| const expectedPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| expect(safeWriteJson).toHaveBeenCalledWith(expectedPath, apiHistory) | ||
| }) | ||
| }) | ||
|
|
||
| describe("restoreApiHistorySnapshot", () => { | ||
| it("should restore API history snapshot when file exists", async () => { | ||
| const commitHash = "abc123" | ||
| const expectedHistory: ApiMessage[] = [ | ||
| { | ||
| role: "user", | ||
| content: "Test message 1", | ||
| ts: 1000, | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: "Test response 1", | ||
| ts: 2000, | ||
| }, | ||
| ] | ||
|
|
||
| const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| vi.mocked(fileExistsAtPath).mockResolvedValue(true) | ||
| vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(expectedHistory)) | ||
|
|
||
| const result = await service.restoreApiHistorySnapshot(commitHash) | ||
|
|
||
| expect(result).toEqual(expectedHistory) | ||
| expect(fileExistsAtPath).toHaveBeenCalledWith(snapshotPath) | ||
| expect(fs.readFile).toHaveBeenCalledWith(snapshotPath, "utf8") | ||
| }) | ||
|
|
||
| it("should return null when snapshot file does not exist", async () => { | ||
| const commitHash = "nonexistent" | ||
| const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| vi.mocked(fileExistsAtPath).mockResolvedValue(false) | ||
|
|
||
| const result = await service.restoreApiHistorySnapshot(commitHash) | ||
|
|
||
| expect(result).toBeNull() | ||
| expect(fileExistsAtPath).toHaveBeenCalledWith(snapshotPath) | ||
| expect(fs.readFile).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should handle invalid JSON gracefully", async () => { | ||
| const commitHash = "invalid" | ||
| const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| vi.mocked(fileExistsAtPath).mockResolvedValue(true) | ||
| vi.mocked(fs.readFile).mockResolvedValue("{ invalid json") | ||
|
|
||
| const result = await service.restoreApiHistorySnapshot(commitHash) | ||
|
|
||
| expect(result).toBeNull() | ||
| expect(mockLog).toHaveBeenCalledWith(expect.stringContaining("Failed to restore snapshot")) | ||
| }) | ||
|
|
||
| it("should handle file read errors gracefully", async () => { | ||
| const commitHash = "error" | ||
| const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| vi.mocked(fileExistsAtPath).mockResolvedValue(true) | ||
| vi.mocked(fs.readFile).mockRejectedValue(new Error("File read error")) | ||
|
|
||
| const result = await service.restoreApiHistorySnapshot(commitHash) | ||
|
|
||
| expect(result).toBeNull() | ||
| expect(mockLog).toHaveBeenCalledWith(expect.stringContaining("Failed to restore snapshot")) | ||
| }) | ||
|
|
||
| it("should restore API history with preserved context compression state", async () => { | ||
| const commitHash = "with-compression" | ||
| const expectedHistory: ApiMessage[] = [ | ||
| { | ||
| role: "user", | ||
| content: "Original message 1", | ||
| ts: 1000, | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: "Original response 1", | ||
| ts: 2000, | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: "Original message 2", | ||
| ts: 3000, | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: "Original response 2", | ||
| ts: 4000, | ||
| }, | ||
| // No compression markers - full history preserved | ||
| ] | ||
|
|
||
| const snapshotPath = path.join(mockCheckpointsDir, "api_snapshots", `${commitHash}.json`) | ||
| vi.mocked(fileExistsAtPath).mockResolvedValue(true) | ||
| vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(expectedHistory)) | ||
|
|
||
| const result = await service.restoreApiHistorySnapshot(commitHash) | ||
|
|
||
| expect(result).toEqual(expectedHistory) | ||
| expect(result).toHaveLength(4) | ||
| expect(result?.every((msg) => !msg.isSummary)).toBe(true) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API history snapshot is saved after awaiting the checkpoint result, but if
checkpointResultis undefined (whensaveCheckpointreturns undefined for no changes), the snapshot save will be skipped. This could lead to inconsistent state where some checkpoints have snapshots and others don't, even when the API history should be preserved. Consider saving the snapshot before returning, or ensure it's saved even when no file changes occurred but API history exists.Fix it with Roo Code or mention @roomote and request a fix.