Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🪢 fix: Settle Compaction Rows Past Anchor Collisions and Legacy Parts (#16552)
Marco Beretta6e6dc1be115d
diff --git a/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js b/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.jsindex 85d5ec045e..8f7662158c 100644--- a/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js+++ b/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js@@ -346,16 +346,34 @@ describe('ResumableAgentController tenant context', () => {});expect(savedMessage.content).toHaveLength(1);});- /** The settling path (completion, error, abort) owns the final row: a- * disconnect snapshot landing after it would reopen the settled turn as- * an unfinished response. */- it('skips the partial save when the job record has settled', async () => {+ /** The settling path (completion, error, abort) owns the compaction's final+ * row: a disconnect snapshot landing after it would reopen the settled+ * turn as an unfinished response. */+ it('skips the compaction partial save when the job record has settled', async () => {await firePartialDisconnect({ id: 'user-123' },{ createdAt: 1000, status: 'error' },- { aggregatedContent: [{ type: 'text', text: 'Partial response' }] },+ {+ body: { compact: true },+ aggregatedContent: [{ type: 'text', text: 'Partial response' }],+ },);expect(mockSaveMessage).not.toHaveBeenCalled();});++ /** Ordinary turns keep the pre-change behavior exactly: their snapshot is+ * the fallback row even when the job record has settled, because the+ * terminal row write may still fail. */+ it('still persists an ordinary partial save when the job record has settled', async () => {+ await firePartialDisconnect(+ { id: 'user-123' },+ { createdAt: 1000, status: 'error' },+ { aggregatedContent: [{ type: 'text', text: 'Partial response' }] },+ );++ expect(mockSaveMessage).toHaveBeenCalledTimes(1);+ const [, savedMessage] = mockSaveMessage.mock.calls[0];+ expect(savedMessage).toMatchObject({ unfinished: true, error: false });+ });});diff --git a/api/server/controllers/agents/request.js b/api/server/controllers/agents/request.jsindex 8eb3bbd2c9..51fa58b533 100644--- a/api/server/controllers/agents/request.js+++ b/api/server/controllers/agents/request.js@@ -473,7 +473,7 @@ async function saveErrorTurn(/** The existing-row settlement (which row a failed turn settles, and* whether its error row may be written at all) lives in @librechat/api;* this supplies the caller's reads and write. */- const coveredByExistingRow = await settleExistingRowsBeforeErrorTurn(req.body, {+ const settlement = await settleExistingRowsBeforeErrorTurn(req.body, {userId,conversationId,errorMessageId,@@ -495,9 +495,12 @@ async function saveErrorTurn(},),});- if (coveredByExistingRow) {+ if (settlement.covered) {return;}+ /** The settlement names the error row's id, so an anchor-shaped+ * collision can never overwrite the anchor. */+ errorMessageId = settlement.errorRowMessageId;const context = 'api/server/controllers/agents/request.js - failed turn';const endpoint = endpointOption?.endpoint;@@ -610,13 +613,6 @@ async function saveErrorTurn(}}-/**- * The disconnect save is marker-only while the run is still live; a failed- * turn is what settles it, so a compaction's partial row is finalized here- * with the terminal outcome instead of keeping the snapshot's live-run- * marking. The decision lives in @librechat/api; this is the wiring, reusing- * the row the caller already loaded.- */function classifyScheduledFailure(error, aborted = false) {if (aborted || error?.code === 'SCHEDULE_NO_LONGER_ACTIVE') {return { status: 'interrupted', error: error?.message };@@ -1958,8 +1954,10 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit* record is the source, since the client-facing resume snapshot never* carries server-private state. */const contextMeta = jobRecord?.createdAt === jobCreatedAt ? jobRecord.contextMeta : undefined;- if (resolveDisconnectSnapshotMode(jobRecord, jobCreatedAt) === 'skip') {- logger.debug('[ResumableAgentController] Skipping partial response save for a settled job');+ if (resolveDisconnectSnapshotMode(isCompaction, jobRecord, jobCreatedAt) === 'skip') {+ logger.debug(+ '[ResumableAgentController] Skipping compaction partial save for a settled job',+ );return;}diff --git a/e2e/specs/mock/scenarios/compaction-collision-settlement.spec.ts b/e2e/specs/mock/scenarios/compaction-collision-settlement.spec.tsnew file mode 100644index 0000000000..b976a8177b--- /dev/null+++ b/e2e/specs/mock/scenarios/compaction-collision-settlement.spec.ts@@ -0,0 +1,163 @@+import { expect, test } from '@playwright/test';+import { randomUUID } from 'node:crypto';+import { getE2EUser } from '../../../setup/user';+import { deleteConversations, deleteMessagesByConversation, seedMessages, withMongo } from '../db';+import {+ MOCK_ENDPOINTS,+ NEW_CHAT_PATH,+ messagesView,+ sendMessage,+ selectMockEndpoint,+ sendMessageAndWaitForCompletion,+} from '../helpers';++const userEmail = getE2EUser().email;+const LABEL_SERVER = `http://127.0.0.1:${process.env.E2E_LABEL_PORT || '8889'}`;++type Row = Record<string, unknown>;++async function cleanup(conversationId: string) {+ await deleteMessagesByConversation([conversationId]);+ await deleteConversations([conversationId]);+}++function findRow(filter: Row): Promise<Row | null> {+ return withMongo((db) => db.collection('messages').findOne(filter));+}++test.describe('compaction collision settlement', () => {+ test.afterEach(async ({ request }) => {+ const response = await request.post(`${LABEL_SERVER}/__e2e/reset`);+ expect(response.ok()).toBeTruthy();+ });++ /* A leaf in the preliminary-response shape (an id ending in `_`) is the id+ a failed turn's error row would take, so a failed compaction on it must+ record its failure without overwriting the leaf. The fixture summarizer+ returns blank output, which is how this harness fails a compaction. */+ test('a failed compaction on an underscore-shaped leaf keeps the leaf and shows the failure @scenario:failed-compaction-on-underscore-leaf-keeps-the-leaf', async ({+ page,+ request,+ }) => {+ test.setTimeout(120_000);+ await page.goto('/c/new');+ await sendMessageAndWaitForCompletion(page, 'tell me about collisions');+ const conversationId = new URL(page.url()).pathname.replace('/c/', '');+ expect(conversationId).not.toBe('new');++ try {+ const answer = await withMongo((db) =>+ db+ .collection('messages')+ .findOne({ conversationId, isCreatedByUser: false }, { sort: { createdAt: -1 } }),+ );+ expect(answer?.messageId).toBeTruthy();++ const leafId = `${randomUUID()}_`;+ const leafText = 'Compact this underscore-shaped leaf';+ await seedMessages(userEmail, conversationId, [+ {+ messageId: leafId,+ parentMessageId: answer?.messageId as string,+ text: leafText,+ isCreatedByUser: true,+ sender: 'User',+ },+ ]);++ const behavior = await request.post(`${LABEL_SERVER}/__e2e/behavior`, {+ data: { mode: 'blank' },+ });+ expect(behavior.ok()).toBeTruthy();++ await page.goto(`/c/${conversationId}`);+ await expect(messagesView(page).getByText(leafText)).toBeVisible();+ await page.getByTestId('token-usage').click();+ await page.getByRole('button', { name: 'Compact context' }).click();++ let failure: Row | null = null;+ await expect+ .poll(+ async () => {+ failure = await findRow({+ conversationId,+ parentMessageId: leafId,+ isCreatedByUser: false,+ });+ return failure != null;+ },+ { timeout: 60_000 },+ )+ .toBeTruthy();+ expect((failure as Row | null)?.messageId).not.toBe(leafId);+ await expect(+ messagesView(page).getByText('Could not compact the context', { exact: false }),+ ).toBeVisible({ timeout: 20_000 });+ await expect(page.getByTestId('stop-generation-button')).toBeHidden({ timeout: 20_000 });++ /* The leaf keeps its author and text: the failure went to its own row. */+ const leaf = await findRow({ conversationId, messageId: leafId });+ expect(leaf?.isCreatedByUser).toBe(true);+ expect(leaf?.text).toBe(leafText);++ await page.reload();+ await expect(messagesView(page).getByText(leafText)).toBeVisible({ timeout: 20_000 });+ const row = page.locator(`[id="${(failure as Row | null)?.messageId as string}"]`);+ await expect(row).toBeVisible({ timeout: 20_000 });+ await expect(row.getByText('Could not compact the context', { exact: false })).toBeVisible();+ await expect(row.getByText('Summarizing...')).toHaveCount(0);+ } finally {+ await cleanup(conversationId);+ }+ });++ /* The settled-job guard only withholds compaction snapshots: an ordinary+ reply whose viewer left mid-stream still ends with its full answer. */+ test('an ordinary reply whose viewer leaves mid-stream still saves its full answer @scenario:abandoned-ordinary-reply-saves-its-full-answer', async ({+ page,+ }) => {+ test.setTimeout(150_000);+ const label = `abandoned-reply-${randomUUID().slice(0, 8)}`;+ const prompt = `E2E_SLOW_REPLY:${label}`;++ await page.goto(NEW_CHAT_PATH);+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const run = await sendMessage(page, prompt);+ expect(run.ok()).toBeTruthy();+ await expect(messagesView(page).getByText('chunk-010')).toBeVisible({ timeout: 15_000 });+ await expect(page).toHaveURL(/\/c\/[0-9a-fA-F-]{36}(\?|$)/, { timeout: 15_000 });+ const conversationId = new URL(page.url()).pathname.replace('/c/', '');++ /* Closing the only viewer drops every subscriber while the run streams. */+ await page.close();++ try {+ let reply: Row | null = null;+ await expect+ .poll(+ async () => {+ const user = await findRow({ conversationId, isCreatedByUser: true });+ if (!user) {+ return false;+ }+ reply = await findRow({+ conversationId,+ parentMessageId: user.messageId,+ isCreatedByUser: false,+ });+ return reply != null && reply.unfinished !== true;+ },+ { timeout: 90_000, intervals: [2_000] },+ )+ .toBeTruthy();++ const context = page.context();+ const viewer = context.pages().length > 0 ? context.pages()[0] : await context.newPage();+ await viewer.goto(`/c/${conversationId}`);+ await expect(messagesView(viewer).getByText(prompt)).toBeVisible({ timeout: 20_000 });+ await expect(messagesView(viewer).getByText('chunk-159')).toBeVisible({ timeout: 20_000 });+ } finally {+ await cleanup(conversationId);+ }+ });+});diff --git a/packages/api/src/agents/compaction.spec.ts b/packages/api/src/agents/compaction.spec.tsindex fa5bc41c02..f67c70e75c 100644--- a/packages/api/src/agents/compaction.spec.ts+++ b/packages/api/src/agents/compaction.spec.ts@@ -356,10 +356,12 @@ describe('markAbortedCompactionContent', () => {it('marks the partial summary a stopped compaction had streamed as failed', () => {const parts = [partialSummary('Half a summary')];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts).toHaveLength(1);- expect(parts[0]).toMatchObject({ initiatedBy: 'user', failed: true, summarizing: true });+ expect(marked).toHaveLength(1);+ expect(marked[0]).toMatchObject({ initiatedBy: 'user', failed: true, summarizing: true });+ /** The aggregated parts belong to the live run: the input is untouched. */+ expect(parts[0]).not.toHaveProperty('initiatedBy');});/** A round that finished before the Stop landed is a real checkpoint: the@@ -367,11 +369,11 @@ describe('markAbortedCompactionContent', () => {it('marks a summary that completed before the stop without failing it', () => {const parts = [completedSummary('Finished before the stop.')];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts).toHaveLength(1);- expect(parts[0]).toMatchObject({ initiatedBy: 'user' });- expect(parts[0]).not.toHaveProperty('failed');+ expect(marked).toHaveLength(1);+ expect(marked[0]).toMatchObject({ initiatedBy: 'user' });+ expect(marked[0]).not.toHaveProperty('failed');});/** Every part that can carry the marker gets it: the row's identity must not@@ -382,10 +384,10 @@ describe('markAbortedCompactionContent', () => {{ type: ContentTypes.ERROR, error: 'Something else failed first' },];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts[0]).toMatchObject({ initiatedBy: 'user', failed: true });- expect(parts[1]).toMatchObject({ initiatedBy: 'user' });+ expect(marked[0]).toMatchObject({ initiatedBy: 'user', failed: true });+ expect(marked[1]).toMatchObject({ initiatedBy: 'user' });});/** A summary placeholder with no text is not an outcome: nothing of the@@ -394,9 +396,9 @@ describe('markAbortedCompactionContent', () => {it('replaces a summary placeholder that streamed nothing with the typed failure', () => {const parts = [emptySummaryPlaceholder()];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts).toEqual([+ expect(marked).toEqual([{type: ContentTypes.ERROR,error: JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),@@ -411,9 +413,9 @@ describe('markAbortedCompactionContent', () => {it('records the typed failure beside an earlier checkpoint when the current round streamed nothing', () => {const parts = [completedSummary('An earlier checkpoint.'), emptySummaryPlaceholder()];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts).toEqual([+ expect(marked).toEqual([expect.objectContaining({type: ContentTypes.SUMMARY,initiatedBy: 'user',@@ -431,9 +433,9 @@ describe('markAbortedCompactionContent', () => {it('synthesizes no failure when a later round completed after an empty one', () => {const parts = [emptySummaryPlaceholder(), completedSummary('The later checkpoint.')];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts).toEqual([+ expect(marked).toEqual([expect.objectContaining({type: ContentTypes.SUMMARY,initiatedBy: 'user',@@ -446,9 +448,9 @@ describe('markAbortedCompactionContent', () => {it('records the typed failure when nothing streamed before the stop', () => {const parts: TMessageContentParts[] = [];- markAbortedCompactionContent(parts, true);+ const marked = markAbortedCompactionContent(parts, true);- expect(parts).toEqual([+ expect(marked).toEqual([{type: ContentTypes.ERROR,error: JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),@@ -463,9 +465,10 @@ describe('markAbortedCompactionContent', () => {it('marks a non-terminal snapshot without failing or synthesizing anything', () => {const parts: TMessageContentParts[] = [partialSummary('Half a summary')];- markAbortedCompactionContent(parts, true, { synthesizeFailure: false });+ const marked = markAbortedCompactionContent(parts, true, { synthesizeFailure: false });- expect(parts).toEqual([{ ...partialSummary('Half a summary'), initiatedBy: 'user' }]);+ expect(marked).toEqual([{ ...partialSummary('Half a summary'), initiatedBy: 'user' }]);+ expect(parts[0]).not.toHaveProperty('initiatedBy');});it('returns content from a turn that was not a compaction unchanged', () => {@@ -575,7 +578,7 @@ describe('persistFinalizedCompactionTurn', () => {]);});- it('writes only the envelope when the parts already carry the failure', async () => {+ it('writes the envelope with the marking reapplied when the parts already carry the failure', async () => {const saved: Record<string, unknown>[] = [];const partialRow = {content: [{ type: ContentTypes.ERROR, error: 'Summarization failed', initiatedBy: 'user' }],@@ -600,6 +603,7 @@ describe('persistFinalizedCompactionTurn', () => {conversationId: 'conversation-1',unfinished: false,error: true,+ content: [{ type: ContentTypes.ERROR, error: 'Summarization failed', initiatedBy: 'user' }],});});@@ -692,9 +696,9 @@ describe('resolveFinalizedCompactionTurn', () => {});/** The parts already carry the failure, but the snapshot's live-run flags- * are still unsettled: the write settles the envelope without touching- * content. */- it('settles only the envelope of a row whose parts already carry the failure', () => {+ * are still unsettled: the write settles the envelope with the marking+ * reapplied (idempotent on marked parts). */+ it('settles a row whose parts already carry the failure', () => {const failedSummary = {content: [{@@ -712,9 +716,26 @@ describe('resolveFinalizedCompactionTurn', () => {expect(resolveFinalizedCompactionTurn(failedSummary, { compact: true })).toEqual({write: true,+ content: failedSummary.content,});expect(resolveFinalizedCompactionTurn(recordedFailure, { compact: true })).toEqual({write: true,+ content: recordedFailure.content,+ });+ });++ /** Legacy and imported rows can carry failure parts that predate the+ * identity marker: the settle write stamps them, or the restored turn+ * keeps the wrong rerun target. */+ it('stamps legacy failure parts that never carried the marker', () => {+ const legacyRow = {+ unfinished: true,+ content: [{ type: ContentTypes.ERROR, error: 'Summarization failed' }],+ };++ expect(resolveFinalizedCompactionTurn(legacyRow, { compact: true })).toEqual({+ write: true,+ content: [{ type: ContentTypes.ERROR, error: 'Summarization failed', initiatedBy: 'user' }],});});@@ -733,7 +754,17 @@ describe('resolveFinalizedCompactionTurn', () => {],};- expect(resolveFinalizedCompactionTurn(snapshot, { compact: true })).toEqual({ write: true });+ expect(resolveFinalizedCompactionTurn(snapshot, { compact: true })).toEqual({+ write: true,+ content: [+ {+ type: ContentTypes.SUMMARY,+ content: [{ type: ContentTypes.TEXT, text: 'A finished checkpoint.' }],+ boundary: completedBoundary,+ initiatedBy: 'user',+ },+ ],+ });});/** A stopped compaction is written settled by the abort route, and an@@ -1222,7 +1253,9 @@ describe('settleExistingRowsBeforeErrorTurn', () => {it('settles a compaction snapshot under its live id and blocks the error row', async () => {const { saved, deps: d } = deps({ 'live-response': [partialSummaryRow()] });- await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toBe(true);+ await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toEqual({+ covered: true,+ });expect(saved).toHaveLength(1);expect(saved[0]).toMatchObject({@@ -1262,19 +1295,53 @@ describe('settleExistingRowsBeforeErrorTurn', () => {'live-response': [partialSummaryRow()],});- await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toBe(true);+ await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toEqual({+ covered: true,+ });expect(saved).toHaveLength(1);expect(saved[0]).toMatchObject({ messageId: 'live-response' });});+ /** The collision with no live row saved must not swallow the failure: the+ * error row proceeds under the failed run's own response id, where it can+ * never overwrite the anchor. */+ it('redirects the error row to the live id when only the anchor matched', async () => {+ const { saved, deps: d } = deps({+ 'error-target': [{ messageId: 'error-target', _id: 'anchor-shaped-match' }],+ });++ await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toEqual({+ covered: false,+ errorRowMessageId: 'live-response',+ });++ expect(saved).toHaveLength(0);+ });++ /** A failure before the response id was allocated leaves nowhere safe to+ * write the error row: writing it under the error id would overwrite the+ * anchor, so it is withheld instead. */+ it('withholds the error row when the collision has no live id to redirect to', async () => {+ const { saved, deps: d } = deps({+ 'error-target': [{ messageId: 'error-target', _id: 'anchor-shaped-match' }],+ });+ const { liveResponseMessageId: _omitted, ...dWithoutLiveId } = d;++ await expect(+ settleExistingRowsBeforeErrorTurn({ compact: true }, dWithoutLiveId),+ ).resolves.toEqual({ covered: true });++ expect(saved).toHaveLength(0);+ });+it('blocks the error row for an ordinary turn with an existing row, writing nothing', async () => {const { saved, deps: d } = deps({'error-target': [{ messageId: 'error-target', _id: 'existing' }],'live-response': [{ messageId: 'live-response', _id: 'partial' }],});- await expect(settleExistingRowsBeforeErrorTurn({}, d)).resolves.toBe(true);+ await expect(settleExistingRowsBeforeErrorTurn({}, d)).resolves.toEqual({ covered: true });expect(saved).toHaveLength(0);// The ordinary early return never reads the live row.@@ -1284,7 +1351,10 @@ describe('settleExistingRowsBeforeErrorTurn', () => {it('lets the error row through when no row covers the turn', async () => {const { saved, deps: d } = deps({});- await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toBe(false);+ await expect(settleExistingRowsBeforeErrorTurn({ compact: true }, d)).resolves.toEqual({+ covered: false,+ errorRowMessageId: 'error-target',+ });expect(saved).toHaveLength(0);});@@ -1295,6 +1365,15 @@ describe('isSettledJobRecord', () => {expect(isSettledJobRecord({ createdAt: 1000, status })).toBe(true);});+ /** The terminal claim precedes its row write, so the status alone does not+ * prove the row is durable: the snapshot stays the fallback until the+ * pending marker clears. */+ it('treats a record with terminal persistence still pending as unsettled', () => {+ expect(+ isSettledJobRecord({ createdAt: 1000, status: 'error', terminalPersistencePending: true }),+ ).toBe(false);+ });+it('leaves live and missing records unsettled', () => {expect(isSettledJobRecord({ createdAt: 1000, status: 'running' })).toBe(false);expect(isSettledJobRecord({ createdAt: 1000, status: 'requires_action' })).toBe(false);@@ -1310,15 +1389,28 @@ describe('isSettledJobRecord', () => {});describe('resolveDisconnectSnapshotMode', () => {- it.each(['complete', 'error', 'aborted'])('withholds the snapshot of a %s job', (status) => {- expect(resolveDisconnectSnapshotMode({ createdAt: 1000, status }, 1000)).toBe('skip');- });+ it.each(['complete', 'error', 'aborted'])(+ 'withholds the snapshot of a %s compaction job',+ (status) => {+ expect(resolveDisconnectSnapshotMode(true, { createdAt: 1000, status }, 1000)).toBe('skip');+ },+ );it('writes the snapshot for a live, missing, or other-epoch record', () => {- expect(resolveDisconnectSnapshotMode({ createdAt: 1000, status: 'running' }, 1000)).toBe(+ expect(resolveDisconnectSnapshotMode(true, { createdAt: 1000, status: 'running' }, 1000)).toBe(+ 'live',+ );+ expect(resolveDisconnectSnapshotMode(true, null, 1000)).toBe('live');+ expect(resolveDisconnectSnapshotMode(true, { createdAt: 2000, status: 'error' }, 1000)).toBe('live',);- expect(resolveDisconnectSnapshotMode(null, 1000)).toBe('live');- expect(resolveDisconnectSnapshotMode({ createdAt: 2000, status: 'error' }, 1000)).toBe('live');+ });++ /** An ordinary turn's snapshot is the fallback row its terminal write may+ * still need, so it is written whatever the record says. */+ it('writes the snapshot of a settled ordinary turn', () => {+ expect(+ resolveDisconnectSnapshotMode(false, { createdAt: 1000, status: 'complete' }, 1000),+ ).toBe('live');});});diff --git a/packages/api/src/agents/compaction.ts b/packages/api/src/agents/compaction.tsindex 529b49754d..1a50151e4a 100644--- a/packages/api/src/agents/compaction.ts+++ b/packages/api/src/agents/compaction.ts@@ -213,7 +213,9 @@ export function resolveFailedTurnContent(* text goes, leaving the typed failure as the row's outcome. A non-terminal* snapshot (`synthesizeFailure: false`, the disconnect save the run may still* complete and overwrite) marks what is there and rewrites nothing else.- * Content from a turn that was not a compaction is returned unchanged.+ * Content from a turn that was not a compaction is returned unchanged, and+ * the parts are never edited in place: the aggregated parts belong to the+ * still-live run on the disconnect path, so every stamped part is a copy.*/export function markAbortedCompactionContent(contentParts: TMessageContentParts[],@@ -223,54 +225,56 @@ export function markAbortedCompactionContent(if (!isCompaction) {return contentParts;}+ const marked: TMessageContentParts[] = [];let hasOutcome = false;let removedUnfinishedRound = false;- for (let index = contentParts.length - 1; index >= 0; index -= 1) {- const part = contentParts[index];+ for (const part of contentParts) {if (part == null) {+ marked.push(part);continue;}if (part.type === ContentTypes.ERROR) {- part.initiatedBy = 'user';+ marked.push({ ...part, initiatedBy: 'user' as const });hasOutcome = true;+ removedUnfinishedRound = false;continue;}if (part.type !== ContentTypes.SUMMARY) {+ marked.push(part);continue;}/** The usability predicate's false side narrows the part's type away, so* the reference is taken before it runs. */const summary = part;if (isUsableSummaryPart(part)) {- summary.initiatedBy = 'user';+ marked.push({ ...summary, initiatedBy: 'user' as const });hasOutcome = true;+ removedUnfinishedRound = false;continue;}if (!synthesizeFailure) {- summary.initiatedBy = 'user';+ marked.push({ ...summary, initiatedBy: 'user' as const });continue;}if (isSummaryPartWithText(summary)) {- summary.initiatedBy = 'user';- summary.failed = true;+ marked.push({ ...summary, initiatedBy: 'user' as const, failed: true });hasOutcome = true;+ removedUnfinishedRound = false;continue;}- contentParts.splice(index, 1);- /** Walking backwards, an outcome already seen belongs to a later round:- * this placeholder is an earlier round the later one superseded. */- if (!hasOutcome) {- removedUnfinishedRound = true;- }+ /** A later outcome supersedes this placeholder's round; the flag is+ * cleared whenever an outcome follows, so only a round opened after the+ * latest outcome synthesizes the failure. */+ removedUnfinishedRound = true;}/** An earlier round's checkpoint is not this round's outcome: a round the* run opened but never finished still records the typed failure beside it,* or the stopped turn reads as the successful compaction the checkpoint* describes. */if ((!hasOutcome || removedUnfinishedRound) && synthesizeFailure) {- contentParts.push(...compactionFailureContent());+ marked.push(...compactionFailureContent());}- return contentParts;+ return marked;}/** Whether a job record has reached a status whose path owns the turn's final@@ -278,12 +282,25 @@ export function markAbortedCompactionContent(* written over it, or the settled row reopens as an unfinished response.* Only a same-epoch record is trusted. */export function isSettledJobRecord(- jobRecord: { createdAt?: number; status?: string } | null | undefined,+ jobRecord:+ | {+ createdAt?: number;+ status?: string;+ terminalPersistencePending?: boolean;+ }+ | null+ | undefined,jobCreatedAt?: number,): boolean {if (jobRecord == null || (jobCreatedAt != null && jobRecord.createdAt !== jobCreatedAt)) {return false;}+ if (jobRecord.terminalPersistencePending === true) {+ /** The terminal claim precedes its row write: the status alone does not+ * prove the row is durable, and the snapshot is still the fallback if+ * that write fails. */+ return false;+ }return (jobRecord.status === 'complete' ||jobRecord.status === 'error' ||@@ -301,14 +318,20 @@ export type DisconnectSnapshotMode =/*** How the last-subscriber disconnect may persist this turn's snapshot, read- * from the same-epoch job record the caller already loaded. The guard reads- * the record the settling path writes, so the remaining window is that- * path's own commit span.+ * from the same-epoch job record the caller already loaded. A compaction+ * whose settling path owns the final row withholds the snapshot; the guard+ * reads the record that path writes, so the remaining window is the path's+ * own commit span. Ordinary turns keep writing their fallback row, settled+ * or not.*/export function resolveDisconnectSnapshotMode(+ isCompaction: boolean,jobRecord: { createdAt?: number; status?: string } | null | undefined,jobCreatedAt?: number,): DisconnectSnapshotMode {+ if (!isCompaction) {+ return 'live';+ }return isSettledJobRecord(jobRecord, jobCreatedAt) ? 'skip' : 'live';}@@ -449,17 +472,27 @@ export type ReadableMessageRow = {unfinished?: boolean;};+/** How a failed generation's fresh error row proceeds after its existing rows+ * are settled. */+export type ErrorTurnSettlement =+ /** An existing row covers the turn; the caller skips the error row. */+ | { covered: true }+ /** The error row is written under `errorRowMessageId`: the error id+ * itself, or the live response id when the anchor-shaped collision makes+ * the error id unusable for it. */+ | { covered: false; errorRowMessageId: string };+/*** Settles the rows a failed generation already persisted before its error row- * is written, through the caller's injected reads and write. Returns whether- * an existing row covers the turn, in which case the caller skips the fresh- * error row entirely.+ * is written, through the caller's injected reads and write.** The error id can normalize back to the compaction anchor itself when the* anchor ends in `_`: a match there never receives the error row, and the- * failed run settles its own distinct live response row instead. Ordinary- * turns keep their existing behavior: a found partial row is preserved as it- * stands and blocks the error row.+ * failed run settles its own distinct live response row instead. When no live+ * row exists either, the error row is still written, redirected to the live+ * response id so it can never overwrite the anchor. Ordinary turns keep their+ * existing behavior: a found partial row is preserved as it stands and blocks+ * the error row.*/export async function settleExistingRowsBeforeErrorTurn(requestBody: { compact?: boolean } | null | undefined,@@ -485,7 +518,7 @@ export async function settleExistingRowsBeforeErrorTurn(* path does, so other devices learn the persisted turn ended. */announceSettledTurn?: (messageId: string) => Promise<unknown>;},-): Promise<boolean> {+): Promise<ErrorTurnSettlement> {const isCompaction = requestBody?.compact === true;const settleLiveRow = async (): Promise<boolean> => {if (liveResponseMessageId == null || liveResponseMessageId === errorMessageId) {@@ -515,12 +548,25 @@ export async function settleExistingRowsBeforeErrorTurn('_id',);if (existing.length > 0) {- if (isCompaction) {- await settleLiveRow();+ if (!isCompaction) {+ return { covered: true };}- return true;+ if (await settleLiveRow()) {+ return { covered: true };+ }+ /** The match is the anchor itself: the error row goes to the failed+ * run's own response id when one exists, and is withheld entirely when+ * none does (a failure before the id was allocated), because writing it+ * under the error id would overwrite the anchor. */+ if (liveResponseMessageId != null && liveResponseMessageId !== errorMessageId) {+ return { covered: false, errorRowMessageId: liveResponseMessageId };+ }+ return { covered: true };}- return settleLiveRow();+ if (await settleLiveRow()) {+ return { covered: true };+ }+ return { covered: false, errorRowMessageId: errorMessageId };}/**@@ -567,12 +613,11 @@ export async function persistFinalizedCompactionTurn(/** What a failed compaction does with its already-persisted partial row. */export type FinalizedCompactionTurn =/** Not the failed run's row, or one holding nothing but a completed- * checkpoint worth keeping exactly as it stands. */+ * checkpoint on a row that was already settled. */| { write: false }- /** The parts already carry the failure (an error part, a failed summary);- * only the snapshot's live-run flags remain to settle. */- | { write: true; content?: undefined }- /** The parts need the terminal marking applied. */+ /** The terminal marking is applied to the parts (a legacy or snapshot row+ * may carry failure parts that never got the identity marker) and the row+ * settles with the terminal envelope. */| { write: true; content: TMessageContentParts[] };/**@@ -580,11 +625,13 @@ export type FinalizedCompactionTurn =* fires, so when the run then fails that snapshot is the row that stays: a* partial summary is marked failed beside its text, a snapshot with no* summary or error part gets the typed failure, and a snapshot whose parts- * already carry the failure still settles its live-run flags. A completed- * checkpoint is preserved as content, but a snapshot still flagged- * `unfinished` settles its envelope even then, or the restored conversation- * keeps treating the terminal job as live; a row that was already settled is- * left alone. Rows of turns that were not compactions are never written.+ * already carry the failure has the terminal marking reapplied (idempotent+ * for marked parts, stamping legacy parts that predate the marker) beside+ * its settled envelope. A completed checkpoint is preserved as content, but a+ * snapshot still flagged `unfinished` settles its envelope even then, or the+ * restored conversation keeps treating the terminal job as live; a row that+ * was already settled is left alone. Rows of turns that were not compactions+ * are never written.*/export function resolveFinalizedCompactionTurn(partialRow: { content?: unknown; unfinished?: boolean } | null | undefined,@@ -620,14 +667,13 @@ export function resolveFinalizedCompactionTurn(sawFailure = true;}}- if (unfinishedSummary) {+ if (unfinishedSummary || sawFailure) {return { write: true, content: markAbortedCompactionContent(content, true) };}- if (sawFailure) {- return { write: true };- }if (sawCheckpoint) {- return partialRow?.unfinished === true ? { write: true } : { write: false };+ return partialRow?.unfinished === true+ ? { write: true, content: markAbortedCompactionContent(content, true) }+ : { write: false };}return { write: true, content: markAbortedCompactionContent(content, true) };}