From ae8f61cefd383417bc0f80447d7ab1bfdfe0726d Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Mon, 22 Jun 2026 01:22:21 +0900 Subject: fix: compaction keeps original ID, forks old history to archive, chains via compactedFrom MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reworked compaction to match the confirmed design: - The compacted conversation KEEPS its original ID (messaging between agents is unaffected — the ID never changes) - The old full history is forked to a new archive conversation (new UUID) - The archive inherits the source's compactedFrom, creating a chain: A → Y → X (walk compactedFrom backward) - A's history is replaced with [summary + recent N] - A.compactedFrom = archive ID forkHistory: inherit compactedFrom from source (not set to sourceId), so archives chain backward to previous archives. FE: no tab switching needed — the ID doesn't change. Just reload history. --- packages/conversation-store/src/store.ts | 14 +++++++++----- packages/session-orchestrator/src/orchestrator.ts | 13 ++++++++----- packages/transport-contract/src/index.ts | 4 ++-- packages/transport-http/src/app.ts | 2 +- packages/transport-ws/src/extension.ts | 4 ++-- packages/wire/src/index.ts | 4 ++-- 6 files changed, 24 insertions(+), 17 deletions(-) (limited to 'packages') diff --git a/packages/conversation-store/src/store.ts b/packages/conversation-store/src/store.ts index d713de3..700be1e 100644 --- a/packages/conversation-store/src/store.ts +++ b/packages/conversation-store/src/store.ts @@ -113,7 +113,7 @@ export interface ConversationStore { * Set the `compactedFrom` field on a conversation's metadata, pointing to * the archive conversation that holds the pre-compaction history. */ - readonly setCompactedFrom: (conversationId: string, archiveId: string) => Promise; + readonly setCompactedFrom: (conversationId: string, newConversationId: string) => Promise; } export const conversationStoreHandle = defineService("conversation-store/store"); @@ -592,7 +592,9 @@ export function createConversationStore( } await storage.set(seqKey(targetId), String(Math.max(seq - 1, 0))); - // Copy metadata with archive title + closed status + compactedFrom. + // Copy metadata with archive title + closed status. + // Inherit compactedFrom from the source so archives chain: + // A → Y → X (each archive points to the previous one). const metaRaw = await storage.get(metaKey(sourceId)); if (metaRaw !== null) { const existing = parseMetaRow(metaRaw); @@ -602,7 +604,9 @@ export function createConversationStore( lastActivityAt: existing.lastActivityAt, title: `Archive: ${existing.title}`, status: "closed", - compactedFrom: sourceId, + ...(existing.compactedFrom !== undefined + ? { compactedFrom: existing.compactedFrom } + : {}), }; await storage.set(metaKey(targetId), JSON.stringify(row)); } @@ -630,7 +634,7 @@ export function createConversationStore( } }, - async setCompactedFrom(conversationId, archiveId) { + async setCompactedFrom(conversationId, newConversationId) { const raw = await storage.get(metaKey(conversationId)); const existing = raw !== null ? parseMetaRow(raw) : null; const ts = now(); @@ -642,7 +646,7 @@ export function createConversationStore( }; await storage.set( metaKey(conversationId), - JSON.stringify({ ...row, compactedFrom: archiveId }), + JSON.stringify({ ...row, compactedFrom: newConversationId }), ); }, }; diff --git a/packages/session-orchestrator/src/orchestrator.ts b/packages/session-orchestrator/src/orchestrator.ts index 21c068c..b46ecc1 100644 --- a/packages/session-orchestrator/src/orchestrator.ts +++ b/packages/session-orchestrator/src/orchestrator.ts @@ -132,7 +132,7 @@ export const conversationStatusChanged: EventHookDescriptor { + ({ conversationId, newConversationId, messagesSummarized, messagesKept }) => { broadcast({ type: "conversation.compacted", conversationId, - archiveId, + newConversationId, messagesSummarized, messagesKept, }); diff --git a/packages/wire/src/index.ts b/packages/wire/src/index.ts index 8c85f89..4ab8825 100644 --- a/packages/wire/src/index.ts +++ b/packages/wire/src/index.ts @@ -535,12 +535,12 @@ export interface ConversationMeta { * Result of a compaction operation. `summary` is the text the model produced; * `messagesKept` is how many recent messages were retained after the summary; * `messagesSummarized` is how many old messages were replaced by the summary. - * `archiveId` is the ID of the new conversation that holds the full + * `newConversationId` is the ID of the new conversation that holds the full * pre-compaction history (non-destructive — the original history is preserved). */ export interface CompactionResult { readonly summary: string; - readonly archiveId: string; + readonly newConversationId: string; readonly messagesSummarized: number; readonly messagesKept: number; } -- cgit v1.2.3