Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/session/optimized-context-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ describe("loadRecentTurns", () => {

await expect(store.load()).rejects.toThrow(TURNS_FILE);
});

test("reactor's load() stays strict and names an unrecoverable extra segment", async () => {
const dir = tempDir();
const store = await createOptimizedContextStore(dir);
const segmentName = segmentFileName(TURNS_FILE, 1);

fs.writeFileSync(path.join(dir, TURNS_FILE), jsonl([turn("a")]));
// Mid-file garbage that is neither null padding nor a torn tail — unrecoverable.
fs.writeFileSync(
path.join(dir, segmentName),
jsonl([turn("b")]) + "THIS IS NOT JSON\n" + jsonl([turn("c")]),
);

await expect(store.load()).rejects.toThrow(segmentName);
});
});

describe("createOptimizedContextStore checkpoint", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/session/optimized-context-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,11 @@ export async function createOptimizedContextStore(dir: string): Promise<ContextS
if (extraTexts.length === 0) return baseTurns;

const parsedExtras = extraTexts.map((text, index) =>
parseSegmentTurns(text, index === extraTexts.length - 1),
parseSegmentTurns(
text,
index === extraTexts.length - 1,
segmentFileName(TURNS_FILE, index + 1),
),
);
const keepExtras = longestWellFormedExtraCount(baseTurns, parsedExtras);

Expand Down Expand Up @@ -466,7 +470,7 @@ export async function createOptimizedContextStore(dir: string): Promise<ContextS
const parsedExtras: ConversationTurn[][] = [];
for (const name of extraNames) {
const text = await runGit(dir, ["show", `${hash}:${name}`]);
parsedExtras.push(parseSegmentTurns(text, false));
parsedExtras.push(parseSegmentTurns(text, false, name));
}
const keepExtras = longestWellFormedExtraCount(baseTurns, parsedExtras);
if (keepExtras === 0) return baseTurns;
Expand Down
Loading