diff --git a/packages/core/src/services/history/manager.ts b/packages/core/src/services/history/manager.ts index 8c60eb74..8b5d2c71 100644 --- a/packages/core/src/services/history/manager.ts +++ b/packages/core/src/services/history/manager.ts @@ -412,8 +412,8 @@ export class HistoryManager implements IHistoryManager { const failedRecords: { record: PromptRecord; error: Error }[] = []; - // Import each record individually, capturing failures - for (const record of records) { + // addRecord prepends, so replay the exported order backwards without mutating it. + for (const record of [...records].reverse()) { try { // 保持原始ID,维护数据关联性(chainId、previousId等) await this.addRecord(record); diff --git a/packages/core/tests/unit/history/import-export.test.ts b/packages/core/tests/unit/history/import-export.test.ts index 039d30b0..dc2a05fb 100644 --- a/packages/core/tests/unit/history/import-export.test.ts +++ b/packages/core/tests/unit/history/import-export.test.ts @@ -356,6 +356,46 @@ describe('HistoryManager Import/Export', () => { }); describe('data integrity', () => { + const makeRecord = (index: number): PromptRecord => ({ + id: `record-${index}`, + originalPrompt: `Prompt ${index}`, + optimizedPrompt: `Response ${index}`, + type: 'optimize', + chainId: `chain-${index}`, + version: 1, + timestamp: 1000 + index, + modelKey: 'openai', + templateId: 'template-1' + }); + + it('should preserve record order through an export/import round trip', async () => { + for (let index = 1; index <= 3; index++) { + await historyManager.addRecord(makeRecord(index)); + } + const backup = JSON.parse(JSON.stringify(await historyManager.exportData())); + const originalIds = backup.map((record: PromptRecord) => record.id); + + await historyManager.importData(backup); + + expect((await historyManager.getRecords()).map(record => record.id)).toEqual(originalIds); + expect(backup.map((record: PromptRecord) => record.id)).toEqual(originalIds); + }); + + it('should evict the oldest record after restoring a full history', async () => { + for (let index = 1; index <= 50; index++) { + await historyManager.addRecord(makeRecord(index)); + } + const backup = JSON.parse(JSON.stringify(await historyManager.exportData())); + + await historyManager.importData(backup); + await historyManager.addRecord(makeRecord(51)); + + const records = await historyManager.getRecords(); + expect(records).toHaveLength(50); + expect(records.map(record => record.id)).toContain('record-50'); + expect(records.map(record => record.id)).not.toContain('record-1'); + }); + it('should maintain chain relationships after import', async () => { // 创建一个完整的对话链 const importData: PromptRecord[] = [