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: Mark a Failed Compaction So Its Rerun Controls Stay Withheld (#15883)
Marco Beretta91e00bbe6e33
diff --git a/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js b/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.jsindex a7594950bb..d778f6b2e5 100644--- a/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js+++ b/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js@@ -283,6 +283,7 @@ jest.mock('@librechat/api', () => ({jest.requireActual('@librechat/api').shouldPersistCodeWorkspaceInitializationError,getSafeErrorMetadata: jest.requireActual('@librechat/api').getSafeErrorMetadata,getSafeErrorText: jest.requireActual('@librechat/api').getSafeErrorText,+ resolveFailedTurnContent: jest.requireActual('@librechat/api').resolveFailedTurnContent,getFailedTurnTraceFields: (...args) => mockGetFailedTurnTraceFields(...args),GenerationJobManager: mockGenerationJobManager,getReferencedQuotes: jest.fn((quotes) => {diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.jsindex 9e7e4d5017..c1fd96a5b9 100644--- a/api/server/controllers/agents/client.js+++ b/api/server/controllers/agents/client.js@@ -173,6 +173,8 @@ const {resolveToolRoleGrants,createTerminalRunErrorObserver,isAgentRunCancellation,+ getSummaryPartText,+ markCompactionOutcome,} = require('@librechat/api');const {Run,@@ -333,41 +335,6 @@ function captureRunContextMeta(client) {});}-/** Text of a summary content part; empty for anything else. */-function getSummaryPartText(part) {- if (part?.type !== ContentTypes.SUMMARY || !Array.isArray(part.content)) {- return '';- }- return part.content- .map((block) => (typeof block?.text === 'string' ? block.text : ''))- .join('')- .trim();-}--/**- * A compaction turn's response is its summary. The run emits no text, so a- * completion without a usable summary part means the summarizer produced- * nothing. A run that already recorded why (an error part, e.g. a skipped- * compaction) persists with that explanation; one that ended with neither- * fails as a typed error instead of persisting an empty assistant message.- * @param {Array<import('librechat-data-provider').TMessageContentParts>} contentParts- */-function markCompactionSummary(contentParts) {- const summary = contentParts.find(- (part) => part?.failed !== true && getSummaryPartText(part).length > 0,- );- if (summary != null) {- summary.initiatedBy = 'user';- return;- }- if (contentParts.some((part) => part?.type === ContentTypes.ERROR)) {- return;- }- throw Object.assign(new Error(JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED })), {- code: 'COMPACTION_FAILED',- });-}-function getLatestEventActorSummary(contentParts) {if (!Array.isArray(contentParts)) {return undefined;@@ -3570,7 +3537,9 @@ class AgentClient extends BaseClient {const completion = filterMalformedContentParts(this.contentParts);if (this.isCompactionTurn()) {- markCompactionSummary(completion);+ markCompactionOutcome(completion, {+ aborted: this.abortController?.signal?.aborted === true,+ });}const metadata = this.buildResponseMetadata();return metadata ? { completion, metadata } : { completion };diff --git a/api/server/controllers/agents/client.test.js b/api/server/controllers/agents/client.test.jsindex 8c1065a72e..8e63b061b1 100644--- a/api/server/controllers/agents/client.test.js+++ b/api/server/controllers/agents/client.test.js@@ -21,7 +21,7 @@ const mockStripActivityLabelParts = jest.fn((payload) =>);const { Providers } = require('@librechat/agents');-const { Constants, ContentTypes, EModelEndpoint } = require('librechat-data-provider');+const { Constants, ContentTypes, EModelEndpoint, ErrorTypes } = require('librechat-data-provider');const {GenerationJobManager,createStreamServices,@@ -3122,6 +3122,102 @@ describe('AgentClient - startup telemetry', () => {errorSpy.mockRestore();});+ /** A compaction's only record of having been one is the marker on the part it+ * produced, and Compact runs on whatever leaf the branch ends with. Without+ * the marker on the failure, a compaction that failed on a user leaf keeps a+ * Regenerate that answers that user message instead of redoing the run. */+ it('marks the failure a compaction turn persists instead of a summary', async () => {+ jest.clearAllMocks();+ mockCreateRun.mockImplementation(async () => ({+ Graph: null,+ processStream: jest.fn(async () => {+ throw new Error('summarizer unavailable');+ }),+ getCalibrationRatio: jest.fn(() => 0),+ }));+ mockIsHITLEnabled.mockReturnValue(false);+ const client = new AgentClient({+ req: {+ user: { id: 'user-123' },+ body: { compact: true },+ config: { endpoints: { [EModelEndpoint.agents]: {} } },+ _resumableStreamId: 'conversation-compaction-failure',+ },+ res: {},+ agent: {+ id: 'agent-123',+ endpoint: EModelEndpoint.openAI,+ provider: EModelEndpoint.openAI,+ model_parameters: { model: 'gpt-4' },+ hide_sequential_outputs: false,+ },+ endpointTokenConfig: {},+ eventHandlers: {},+ contentParts: [],+ collectedUsage: [],+ artifactPromises: [],+ });+ client.conversationId = 'conversation-compaction-failure';+ client.responseMessageId = 'response-compaction-failure';+ client.parentMessageId = 'parent-compaction-failure';+ client.recordCollectedUsage = jest.fn().mockResolvedValue();++ const { completion } = await client.sendCompletion([]);++ expect(completion).toEqual([+ expect.objectContaining({ type: ContentTypes.ERROR, initiatedBy: 'user' }),+ ]);+ });++ /** A summarizer that returns nothing emits no content at all, so the run has+ * neither a summary nor an explanation. The turn records the typed failure+ * itself instead of being saved as a bare error row the client cannot tell+ * apart from an answer to the message it hangs off. */+ it('records a marked typed failure when a compaction run produces nothing', async () => {+ jest.clearAllMocks();+ mockCreateRun.mockImplementation(async () => ({+ Graph: null,+ processStream: jest.fn(async () => {}),+ getCalibrationRatio: jest.fn(() => 0),+ }));+ mockIsHITLEnabled.mockReturnValue(false);+ const client = new AgentClient({+ req: {+ user: { id: 'user-123' },+ body: { compact: true },+ config: { endpoints: { [EModelEndpoint.agents]: {} } },+ _resumableStreamId: 'conversation-compaction-empty',+ },+ res: {},+ agent: {+ id: 'agent-123',+ endpoint: EModelEndpoint.openAI,+ provider: EModelEndpoint.openAI,+ model_parameters: { model: 'gpt-4' },+ hide_sequential_outputs: false,+ },+ endpointTokenConfig: {},+ eventHandlers: {},+ contentParts: [],+ collectedUsage: [],+ artifactPromises: [],+ });+ client.conversationId = 'conversation-compaction-empty';+ client.responseMessageId = 'response-compaction-empty';+ client.parentMessageId = 'parent-compaction-empty';+ client.recordCollectedUsage = jest.fn().mockResolvedValue();++ const { completion } = await client.sendCompletion([]);++ expect(completion).toEqual([+ {+ type: ContentTypes.ERROR,+ error: JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),+ initiatedBy: 'user',+ },+ ]);+ });+it('keeps a later non-provider run failure on the generic error path', async () => {jest.clearAllMocks();const { logger } = require('@librechat/data-schemas');diff --git a/api/server/controllers/agents/request.js b/api/server/controllers/agents/request.jsindex 65b92bfec5..9f9b5935d7 100644--- a/api/server/controllers/agents/request.js+++ b/api/server/controllers/agents/request.js@@ -46,6 +46,7 @@ const {getCodeWorkspaceSelectionErrorDetails,shouldPersistCodeWorkspaceInitializationError,getFailedTurnTraceFields,+ resolveFailedTurnContent,} = require('@librechat/api');const { disposeClient } = require('~/server/cleanup');const {@@ -490,6 +491,7 @@ async function saveErrorTurn(error: true,unfinished: false,isCreatedByUser: false,+ ...resolveFailedTurnContent(req.body, errorText),},{ context },);diff --git a/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx b/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsxindex 54cbb01bb8..6ea8f3d11a 100644--- a/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx+++ b/client/src/components/Chat/Messages/__tests__/HoverButtons.spec.tsx@@ -322,6 +322,32 @@ describe('HoverButtons edit affordance', () => {expect(screen.queryByTestId('continue-generation-button')).toBeNull();});+ /** The failure branch of the same shape: a compaction that produced no summary+ * marks its error part, which is the only thing distinguishing it from a+ * response to the user message it hangs off. */+ it('withholds rerun controls on a marked compaction failure hanging off a user message', () => {+ const failedCompaction = {+ ...userMessage,+ messageId: 'compaction-1',+ parentMessageId: userMessage.messageId,+ isCreatedByUser: false,+ text: '',+ content: [{ type: ContentTypes.ERROR, error: 'Summarization failed', initiatedBy: 'user' }],+ } as TMessage;++ const container = renderHoverButtons({+ isSubmitting: false,+ message: failedCompaction,+ isLast: true,+ latestMessageId: failedCompaction.messageId,+ thread: [userMessage, failedCompaction],+ });++ expect(container.querySelector(`#edit-${failedCompaction.messageId}`)).toBeNull();+ expect(screen.queryByTestId('regenerate-generation-button')).toBeNull();+ expect(screen.queryByTestId('continue-generation-button')).toBeNull();+ });+/** An ordinary turn that auto-summarized and was cancelled before its first* answer token persists the same summary-only content, but it hangs off the* user's message and is exactly the turn a user needs to rerun. */diff --git a/e2e/setup/fake-model.js b/e2e/setup/fake-model.jsindex 9f7b53440e..33df00e837 100644--- a/e2e/setup/fake-model.js+++ b/e2e/setup/fake-model.js@@ -39,6 +39,9 @@ const COUNTED_REPLY_MARKER = 'E2E_COUNTED_REPLY:';const ORDERED_REPLY_MARKER = 'E2E_ORDERED_REPLY:';const SLOW_REPLY_MARKER = 'E2E_SLOW_REPLY:';const EMPTY_SLOW_REPLY_MARKER = 'E2E_EMPTY_SLOW_REPLY:';+/** A run that completes having produced no content at all: the shape a+ * summarizer takes when it returns nothing for a manual compaction. */+const EMPTY_REPLY_MARKER = 'E2E_EMPTY_REPLY:';const SLOW_COUNTED_REPLY_MARKER = 'E2E_SLOW_COUNTED_REPLY:';const STEER_TOOL_REPLY_MARKER = 'E2E_STEER_TOOL_REPLY:';const STEER_SPLIT_REPLY_MARKER = 'E2E_STEER_SPLIT_REPLY:';@@ -583,6 +586,11 @@ function replyResponses(text) {};}+ const emptyName = getMarkerValue(text, EMPTY_REPLY_MARKER);+ if (emptyName) {+ return { responses: [''] };+ }+const slowCountedName = getMarkerValue(text, SLOW_COUNTED_REPLY_MARKER);if (slowCountedName) {const count = (slowCountedReplies.get(slowCountedName) ?? 0) + 1;diff --git a/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts b/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.tsindex 49fb188f8d..ef0bb10477 100644--- a/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts+++ b/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts@@ -1,5 +1,5 @@import { expect, test } from '@playwright/test';-import type { Page } from '@playwright/test';+import type { APIRequestContext, Page } from '@playwright/test';import { randomUUID } from 'node:crypto';import { getE2EUser } from '../../../setup/user';import {@@ -7,9 +7,10 @@ import {deleteMessagesByConversation,seedConversations,seedMessages,+ withMongo,} from '../db';import type { SeedMessage } from '../db';-import { messagesView } from '../helpers';+import { messagesView, sendMessageAndWaitForCompletion } from '../helpers';const userEmail = getE2EUser().email;const ROOT_PARENT = '00000000-0000-0000-0000-000000000000';@@ -77,7 +78,55 @@ function precedingTurns(label: string) {};}+/** The summarizer the mock deployment's endpoints call. */+const LABEL_SERVER = `http://127.0.0.1:${process.env.E2E_LABEL_PORT || '8889'}`;++/**+ * A real conversation whose manual compaction produces nothing: the summarizer+ * is switched to blank output for the compaction run, which is what a model+ * returning no checkpoint looks like. Seeding cannot stand in for this — the+ * point is what a compaction run records when it has neither a summary nor an+ * explanation to carry, and only a real run reaches that path.+ *+ * Returns the conversation and the turn the compaction produced.+ */+async function compactWithEmptySummarizer(page: Page, request: APIRequestContext, label: string) {+ await page.goto('/c/new');+ await sendMessageAndWaitForCompletion(page, `tell me about ${label}`);+ const conversationId = new URL(page.url()).pathname.replace('/c/', '');+ expect(conversationId).not.toBe('new');++ const behavior = await request.post(`${LABEL_SERVER}/__e2e/behavior`, {+ data: { mode: 'blank' },+ });+ expect(behavior.ok()).toBeTruthy();++ await page.getByTestId('token-usage').click();+ await page.getByRole('button', { name: 'Compact context' }).click();+ await expect(+ messagesView(page).getByText('Could not compact the context', { exact: false }),+ ).toBeVisible({ timeout: 60_000 });++ const compactionId = await withMongo(async (db) => {+ const row = await db+ .collection('messages')+ .findOne({ conversationId, 'content.type': 'error' });+ return row?.messageId as string | undefined;+ });+ expect(compactionId).toBeTruthy();+ return { conversationId, compactionId: compactionId as string };+}+test.describe('compaction rerun controls', () => {+ /** `compactWithEmptySummarizer` switches the shared fixture summarizer to+ * blank output before it returns, so a failure inside it would leave every+ * later mock test summarizing to nothing. Restoring here runs whether the+ * helper returned, failed, or the test was retried. */+ test.afterEach(async ({ request }) => {+ const response = await request.post(`${LABEL_SERVER}/__e2e/reset`);+ expect(response.ok()).toBeTruthy();+ });+test('a finished compaction offers no rerun controls @scenario:compaction-turn-offers-no-rerun-controls', async ({page,}) => {@@ -175,6 +224,97 @@ test.describe('compaction rerun controls', () => {}});+ test('a failed compaction hanging off a user turn offers no rerun controls @scenario:failed-compaction-on-user-turn-offers-no-rerun-controls', async ({+ page,+ }) => {+ const userMessageId = randomUUID();+ const compactionId = randomUUID();+ const conversationId = await seedBranch([+ {+ messageId: userMessageId,+ parentMessageId: ROOT_PARENT,+ text: 'Compact this before I continue',+ isCreatedByUser: true,+ sender: 'User',+ },+ {+ messageId: compactionId,+ parentMessageId: userMessageId,+ text: '',+ isCreatedByUser: false,+ sender: 'OpenAI',+ content: [+ {+ type: 'error',+ error: JSON.stringify({ type: 'compaction_skipped', reason: 'nothing_to_summarize' }),+ initiatedBy: 'user',+ },+ ],+ },+ ]);+ try {+ const row = await openRow(page, conversationId, compactionId);++ await expect(row.getByText('Nothing to compact', { exact: false })).toBeVisible();+ /* The compaction marks its failure, so the turn is still a compaction:+ replaying the user message behind it would answer it instead. */+ await expect(page.locator(`[id="edit-${compactionId}"]`)).toHaveCount(0);+ await expect(page.getByTestId('regenerate-generation-button')).toHaveCount(0);+ await expect(page.getByTestId('continue-generation-button')).toHaveCount(0);+ } finally {+ await cleanup(conversationId);+ }+ });++ /* A real compaction run, not a seeded row: the summarizer returns nothing, so+ the turn has no summary and no explanation of its own and must record the+ marked failure itself. Without that the row is indistinguishable from an+ answer to the message it hangs off. */+ test('a compaction whose summarizer returns nothing offers no rerun controls @scenario:empty-compaction-run-offers-no-rerun-controls', async ({+ page,+ request,+ }) => {+ const { conversationId, compactionId } = await compactWithEmptySummarizer(+ page,+ request,+ 'empty-compaction',+ );+ try {+ await page.locator(`[id="${compactionId}"]`).hover();+ await expect(page.locator(`[id="edit-${compactionId}"]`)).toHaveCount(0);+ await expect(page.getByTestId('regenerate-generation-button')).toHaveCount(0);+ await expect(page.getByTestId('continue-generation-button')).toHaveCount(0);+ /* The redo path a compaction keeps is the indicator's own action. */+ await page.getByTestId('token-usage').click();+ await expect(page.getByRole('button', { name: 'Compact context' })).toBeEnabled();+ } finally {+ await cleanup(conversationId);+ }+ });++ /* The same turn read back from storage: a reload rebuilds the row from what+ the server persisted, so the marker has to survive the round trip. */+ test('a compaction that produced nothing stays free of rerun controls after a reload @scenario:empty-compaction-run-survives-reload-without-rerun-controls', async ({+ page,+ request,+ }) => {+ const { conversationId, compactionId } = await compactWithEmptySummarizer(+ page,+ request,+ 'reloaded-compaction',+ );+ try {+ const row = await openRow(page, conversationId, compactionId);++ await expect(row.getByText('Could not compact the context', { exact: false })).toBeVisible();+ await expect(page.locator(`[id="edit-${compactionId}"]`)).toHaveCount(0);+ await expect(page.getByTestId('regenerate-generation-button')).toHaveCount(0);+ await expect(page.getByTestId('continue-generation-button')).toHaveCount(0);+ } finally {+ await cleanup(conversationId);+ }+ });+test('a turn that only auto-summarized keeps its rerun controls @scenario:auto-summarized-turn-keeps-rerun-controls', async ({page,}) => {@@ -228,7 +368,7 @@ test.describe('compaction rerun controls', () => {},]);try {- const row = await openRow(page, conversationId, chainedId);+ await openRow(page, conversationId, chainedId);/* No user turn to replay, so the rerun shapes go... */await expect(page.getByTestId('regenerate-generation-button')).toHaveCount(0);diff --git a/packages/api/src/agents/compaction.spec.ts b/packages/api/src/agents/compaction.spec.tsindex afe5b59f5a..c4cc6a94fe 100644--- a/packages/api/src/agents/compaction.spec.ts+++ b/packages/api/src/agents/compaction.spec.ts@@ -1,3 +1,4 @@+import { ContentTypes, ErrorTypes } from 'librechat-data-provider';import { COMPACTION_SEMANTIC_INDEX_LIMITS } from '@librechat/agents';import {MAX_COMPACTION_SEMANTIC_INDEX_ENTRIES,@@ -6,9 +7,12 @@ import {MAX_COMPACTION_SEMANTIC_INDEX_TEXT_LENGTH,} from '@librechat/data-schemas';import type { CompactionSemanticIndex, CompactionSemanticIndexSnapshot } from '@librechat/agents';+import type { SummaryContentPart, TMessageContentParts } from 'librechat-data-provider';import type { ICompactionSemanticIndexProjection } from '@librechat/data-schemas';import {createCompactionSemanticIndexProjection,+ markCompactionOutcome,+ resolveFailedTurnContent,restoreCompactionSemanticIndex,restoreCompactionSemanticIndexSnapshot,} from './compaction';@@ -137,3 +141,97 @@ describe('compaction semantic index continuation projection', () => {});});});++describe('markCompactionOutcome', () => {+ const summary = (+ text: string,+ overrides: Partial<SummaryContentPart> = {},+ ): TMessageContentParts => ({+ type: ContentTypes.SUMMARY,+ content: [{ type: ContentTypes.TEXT, text }],+ ...overrides,+ });+ const failure = (error: string): TMessageContentParts => ({ type: ContentTypes.ERROR, error });++ it('marks the summary a compaction produced', () => {+ const parts = [summary('Earlier turns, compacted.')];++ markCompactionOutcome(parts);++ expect(parts[0]).toMatchObject({ initiatedBy: 'user' });+ });++ /** The turn has no other record of having been a compaction: without the+ * marker a failure hanging off a user message keeps a Regenerate that+ * answers that message instead of redoing the compaction. */+ it('marks the failure a compaction recorded instead of a summary', () => {+ const parts = [failure('Nothing to summarize')];++ markCompactionOutcome(parts);++ expect(parts[0]).toMatchObject({ initiatedBy: 'user' });+ });++ it('marks the failure when only a partial summary streamed before it', () => {+ const parts = [summary('Half a checkpoint', { failed: true }), failure('Summarization failed')];++ markCompactionOutcome(parts);++ expect(parts[0]).not.toHaveProperty('initiatedBy');+ expect(parts[1]).toMatchObject({ initiatedBy: 'user' });+ });++ /** The fallback the reviewed head threw on: a run that produced neither a+ * summary nor an explanation now records the typed failure itself, so the+ * turn carries the marker on the stream and in storage instead of being+ * saved as a bare error row with no content. */+ it.each([+ ['nothing at all', []],+ ['an empty summary', [summary(' ')]],+ [+ 'a partial summary with no recorded failure',+ [summary('Half a checkpoint', { failed: true })],+ ],+ ])('records a marked typed failure for a run that produced %s', (_label, parts) => {+ markCompactionOutcome(parts);++ /** The typed failure is the turn's whole outcome: a truncated summary left+ * beside it would be read back as the conversation's checkpoint. */+ expect(parts).toEqual([+ {+ type: ContentTypes.ERROR,+ error: JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),+ initiatedBy: 'user',+ },+ ]);+ });++ /** A cancelled compaction stopped early rather than failing, and the abort+ * path owns that turn: it must not be turned into a failure row. */+ it('fails as a typed error when the run was cancelled', () => {+ const parts: TMessageContentParts[] = [];++ expect(() => markCompactionOutcome(parts, { aborted: true })).toThrow(+ JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),+ );+ expect(parts).toHaveLength(0);+ });+});++describe('resolveFailedTurnContent', () => {+ /** A thrown failure leaves the turn with no content of its own, so the row a+ * compaction persists carries the marked failure instead. */+ it('gives a failed compaction turn its marked failure content', () => {+ expect(resolveFailedTurnContent({ compact: true }, 'Summarization failed')).toEqual({+ content: [{ type: ContentTypes.ERROR, error: 'Summarization failed', initiatedBy: 'user' }],+ });+ });++ it.each([+ ['an ordinary turn', { compact: false }],+ ['a turn that never asked to compact', {}],+ ['a request with no body', undefined],+ ])('leaves %s with its text-only shape', (_label, requestBody) => {+ expect(resolveFailedTurnContent(requestBody, 'Something failed')).toEqual({});+ });+});diff --git a/packages/api/src/agents/compaction.ts b/packages/api/src/agents/compaction.tsindex 0783c122cf..2bedcda3a7 100644--- a/packages/api/src/agents/compaction.ts+++ b/packages/api/src/agents/compaction.ts@@ -1,3 +1,4 @@+import { ContentTypes, ErrorTypes } from 'librechat-data-provider';import {COMPACTION_SEMANTIC_INDEX_PROJECTION_VERSION,MAX_COMPACTION_SEMANTIC_INDEX_ENTRIES,@@ -15,6 +16,102 @@ import type {ICompactionSemanticIndexProjection,TCompactionSemanticIndexEntry,} from '@librechat/data-schemas';+import type { SummaryContentPart, TMessageContentParts } from 'librechat-data-provider';++/** Text of a summary content part; empty for anything else. */+export function getSummaryPartText(part: TMessageContentParts | null | undefined): string {+ if (part?.type !== ContentTypes.SUMMARY || !Array.isArray(part.content)) {+ return '';+ }+ return part.content+ .map((block) => (typeof block?.text === 'string' ? block.text : ''))+ .join('')+ .trim();+}++/** The typed failure a manual compaction reports when it produced no summary. */+const COMPACTION_FAILED_ERROR = JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED });++/**+ * The content a failed manual compaction persists: the typed failure, marked as+ * the compaction's own outcome. A turn saved from a thrown failure has no+ * content of its own, so without this the row is indistinguishable from an+ * answer to the message it hangs off and keeps that message's rerun controls.+ */+function compactionFailureContent(+ errorText: string = COMPACTION_FAILED_ERROR,+): TMessageContentParts[] {+ return [{ type: ContentTypes.ERROR, error: errorText, initiatedBy: 'user' }];+}++/**+ * The content fields a failed turn is persisted with. A manual compaction owns+ * its identity through content, so its row carries the marked failure; every+ * other failed turn contributes nothing and keeps its text-only shape. Callers+ * spread the result rather than deciding which turns are compactions.+ */+export function resolveFailedTurnContent(+ requestBody: { compact?: boolean } | null | undefined,+ errorText: string,+): { content?: TMessageContentParts[] } {+ if (requestBody?.compact !== true) {+ return {};+ }+ return { content: compactionFailureContent(errorText) };+}++/**+ * Stamps `initiatedBy: 'user'` on the part that carries a manual compaction's+ * outcome, which is the turn's only record of having been one: the run emits no+ * text of its own, and a compaction hangs off whatever leaf the branch ends+ * with, so a reader cannot infer it from the turn's shape or its parent.+ *+ * Every outcome is marked. A run that produced a summary marks it; a run that+ * recorded why it could not (an error part, e.g. a skipped compaction) marks+ * that instead; a run that produced neither records the typed failure here, so+ * it persists and streams like any other failed compaction rather than as an+ * empty assistant message. A cancelled run keeps failing as a typed error: the+ * turn stopped early rather than failing, and the abort path owns it.+ */+export function markCompactionOutcome(+ contentParts: TMessageContentParts[],+ { aborted = false }: { aborted?: boolean } = {},+): void {+ const summary = contentParts.find(+ (part): part is SummaryContentPart =>+ part?.type === ContentTypes.SUMMARY &&+ part.failed !== true &&+ getSummaryPartText(part).length > 0,+ );+ if (summary != null) {+ summary.initiatedBy = 'user';+ return;+ }+ let markedFailure = false;+ for (const part of contentParts) {+ if (part?.type === ContentTypes.ERROR) {+ part.initiatedBy = 'user';+ markedFailure = true;+ }+ }+ if (markedFailure) {+ return;+ }+ if (aborted) {+ throw Object.assign(new Error(COMPACTION_FAILED_ERROR), { code: 'COMPACTION_FAILED' });+ }+ /** A failed round keeps whatever deltas it streamed, and history loading+ * accepts any nonempty summary as the conversation's checkpoint+ * (`BaseClient.findSummaryContentBlock`). Persisting a truncated one would+ * replace the history it failed to summarize, so the unusable summary goes+ * and the typed failure is the turn's whole outcome. */+ for (let index = contentParts.length - 1; index >= 0; index -= 1) {+ if (contentParts[index]?.type === ContentTypes.SUMMARY) {+ contentParts.splice(index, 1);+ }+ }+ contentParts.push(...compactionFailureContent());+}function snapshotEntry(entry: CompactionSemanticIndexEntry,diff --git a/packages/data-provider/src/messages.spec.ts b/packages/data-provider/src/messages.spec.tsindex 67d865893c..6dc333301c 100644--- a/packages/data-provider/src/messages.spec.ts+++ b/packages/data-provider/src/messages.spec.ts@@ -1,3 +1,4 @@+import type { SummaryContentPart } from './types/assistants';import type { ParentMessage } from './messages';import type { TFile } from './types/files';import type { TMessage } from './types';@@ -251,7 +252,7 @@ describe('findMessageById', () => {});describe('isUserInitiatedCompaction', () => {- const summary = (overrides: Record<string, unknown> = {}) => ({+ const summary = (overrides: Partial<SummaryContentPart> = {}): SummaryContentPart => ({type: ContentTypes.SUMMARY,content: [{ type: ContentTypes.TEXT, text: 'checkpoint' }],...overrides,@@ -263,10 +264,21 @@ describe('isUserInitiatedCompaction', () => {).toBe(true);});+ /** A compaction that produced no summary marks its error part instead: that+ * turn is still a compaction, whatever it hangs off. */+ it('is true for the failure a compaction recorded instead of a summary', () => {+ expect(+ isUserInitiatedCompaction({+ content: [{ type: ContentTypes.ERROR, error: 'failed', initiatedBy: 'user' }],+ } as TMessage),+ ).toBe(true);+ });+/** An automatic summary detour carries no marker: that turn answers a user* message and stays rerunnable. */it.each([['an unmarked summary', [summary()]],+ ['an unmarked error part', [{ type: ContentTypes.ERROR, error: 'failed' }]],['a plain answer', [{ type: ContentTypes.TEXT, text: 'reply' }]],['no content', undefined],])('is false for %s', (_label, content) => {diff --git a/packages/data-provider/src/messages.ts b/packages/data-provider/src/messages.tsindex 14465bd803..09bd24c76e 100644--- a/packages/data-provider/src/messages.ts+++ b/packages/data-provider/src/messages.ts@@ -188,19 +188,28 @@ export function findMessageById(/*** True when a turn carries the marker the server stamps on a manual compaction:- * `markCompactionSummary` sets `initiatedBy: 'user'` on the summary a Compact- * action produced, and nothing else writes it, so an automatic summary detour is- * not one. This is the compaction's own identity, independent of where it hangs:- * Compact runs on whatever leaf the branch ends with, so its response can parent- * onto a user message as easily as onto the answer it summarized. Redoing one is- * the context indicator's Compact action, never a rerun of the turn behind it.+ * `markCompactionOutcome` sets `initiatedBy: 'user'` on whichever part carries+ * the outcome — the summary a Compact action produced, or the error part a run+ * that produced none recorded instead — and nothing else writes it, so an+ * automatic summary detour is not one. This is the compaction's own identity,+ * independent of where it hangs: Compact runs on whatever leaf the branch ends+ * with, so its response can parent onto a user message as easily as onto the+ * answer it summarized. Redoing one is the context indicator's Compact action,+ * never a rerun of the turn behind it.+ *+ * Compactions stored before the marker existed carry none, so callers keep+ * their own ancestry test as the fallback.*/export function isUserInitiatedCompaction(message?: Pick<TMessage, 'content'> | null): boolean {const content = message?.content;if (!Array.isArray(content)) {return false;}- return content.some((part) => part?.type === ContentTypes.SUMMARY && part.initiatedBy === 'user');+ return content.some(+ (part) =>+ (part?.type === ContentTypes.SUMMARY || part?.type === ContentTypes.ERROR) &&+ part.initiatedBy === 'user',+ );}/**diff --git a/packages/data-provider/src/types/assistants.ts b/packages/data-provider/src/types/assistants.tsindex 4ddff9334d..38b60f4e0c 100644--- a/packages/data-provider/src/types/assistants.ts+++ b/packages/data-provider/src/types/assistants.ts@@ -766,6 +766,10 @@ export type TMessageContentParts =type: ContentTypes.ERROR;text?: string | TextData;error?: string;+ /** Set when this failure is what a manual compaction produced instead of a+ * summary. The turn has no other record of having been one, so the rerun+ * controls read it the same way they read a summary's marker. */+ initiatedBy?: 'user';} & ContentMetadata)| ({type: ContentTypes.THINK;