Skip to content

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

📴 fix: Persist Cancelled Compactions With Their Identity (#16436)

Marco Berettaedc96d549e67
diff --git a/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js b/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js
index 8955e99e7d..2350045fe4 100644
--- a/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js
+++ b/api/server/controllers/agents/__tests__/request.partialDisconnect.spec.js
@@ -55,6 +55,8 @@ jest.mock('@librechat/api', () => ({
jest.requireActual('@librechat/api').getAgentErrorMetadata(...args),
applyForcedTemporaryRequest: jest.fn(),
resolveResumableRetention: jest.requireActual('@librechat/api').resolveResumableRetention,
+ markAbortedCompactionContent: (...args) =>
+ jest.requireActual('@librechat/api').markAbortedCompactionContent(...args),
sendEvent: jest.fn(),
persistedReasoningOverrideFields:
jest.requireActual('@librechat/api').persistedReasoningOverrideFields,
@@ -175,6 +177,7 @@ describe('ResumableAgentController tenant context', () => {
const firePartialDisconnect = async (
user,
jobRecord = { createdAt: 1000, contextMeta: partialContextMeta },
+ { body = {}, aggregatedContent = [{ type: 'text', text: 'Partial response' }] } = {},
) => {
mockGetConvo.mockResolvedValue({
conversationId: 'conversation-123',
@@ -228,6 +231,7 @@ describe('ResumableAgentController tenant context', () => {
endpoint: 'agents',
modelOptions: { model: 'gpt-4.1' },
},
+ ...body,
},
config: {},
};
@@ -240,7 +244,7 @@ describe('ResumableAgentController tenant context', () => {
await AgentController(req, res, jest.fn(), initializeClient, null);
expect(allSubscribersLeftHandler).toEqual(expect.any(Function));
- await allSubscribersLeftHandler([{ type: 'text', text: 'Partial response' }]);
+ await allSubscribersLeftHandler(aggregatedContent);
return tenantSeenBySave;
};
@@ -286,4 +290,56 @@ describe('ResumableAgentController tenant context', () => {
expect(tenantSeenBySave).toBeUndefined();
expect(mockSaveMessage).toHaveBeenCalledTimes(1);
});
+
+ /** A cancelled compaction's partial row is built here, not by sendCompletion,
+ * so it carries no marker unless the disconnect path stamps one: without it
+ * the row reads as an answer to the message it hangs off and keeps that
+ * message's rerun controls. */
+ it('stamps a partial response saved on disconnect with the compaction identity', async () => {
+ await firePartialDisconnect(
+ { id: 'user-123' },
+ { createdAt: 1000 },
+ {
+ body: { compact: true },
+ aggregatedContent: [
+ {
+ type: 'summary',
+ content: [{ type: 'text', text: 'Half a summary' }],
+ summarizing: true,
+ },
+ ],
+ },
+ );
+
+ const [, savedMessage] = mockSaveMessage.mock.calls[0];
+ expect(savedMessage).toMatchObject({
+ messageId: 'response-message',
+ unfinished: true,
+ error: false,
+ content: [{ type: 'summary', summarizing: true, initiatedBy: 'user' }],
+ });
+ });
+
+ /** The disconnect save runs while the generation is still live and the
+ * completing run overwrites the row, so it must not report a failure that
+ * has not happened: no typed failure is invented for a compaction whose
+ * snapshot carries no summary or error part. */
+ it('saves a non-outcome compaction partial on disconnect without a synthesized failure', async () => {
+ await firePartialDisconnect(
+ { id: 'user-123' },
+ { createdAt: 1000 },
+ {
+ body: { compact: true },
+ aggregatedContent: [{ type: 'think', think: 'Picking what to summarize' }],
+ },
+ );
+
+ const [, savedMessage] = mockSaveMessage.mock.calls[0];
+ expect(savedMessage).toMatchObject({
+ unfinished: true,
+ error: false,
+ content: [{ type: 'think', think: 'Picking what to summarize' }],
+ });
+ expect(savedMessage.content).toHaveLength(1);
+ });
});
diff --git a/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js b/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js
index cef45b6f7c..6125e4aa7e 100644
--- a/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js
+++ b/api/server/controllers/agents/__tests__/request.resumeMetadata.spec.js
@@ -267,6 +267,8 @@ jest.mock('@librechat/api', () => ({
jest.requireActual('@librechat/api').getAgentErrorMetadata(...args),
applyForcedTemporaryRequest: jest.fn(),
resolveResumableRetention: jest.requireActual('@librechat/api').resolveResumableRetention,
+ markAbortedCompactionContent: (...args) =>
+ jest.requireActual('@librechat/api').markAbortedCompactionContent(...args),
sendEvent: jest.fn(),
/** Real, because whether a skipped-persistence turn may raise an indicator is under test. */
isAnnounceableReply: jest.requireActual('@librechat/api').isAnnounceableReply,
diff --git a/api/server/controllers/agents/request.js b/api/server/controllers/agents/request.js
index 9801525b40..b75182e19b 100644
--- a/api/server/controllers/agents/request.js
+++ b/api/server/controllers/agents/request.js
@@ -63,6 +63,7 @@ const {
stampPreliminaryPrivateTextMessage,
announceReply,
announceErrorTurn,
+ markAbortedCompactionContent,
} = require('@librechat/api');
const { disposeClient } = require('~/server/cleanup');
const {
@@ -1712,6 +1713,9 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
* no user message of its own, the response parented onto an
* existing message. A reconnecting client rebuilds it that way. */
...((isRegenerate || isCompaction) && { isRegenerate: true }),
+ /** The job record is where the abort paths learn the turn was a
+ * compaction: they run after the request that created the job. */
+ ...(isCompaction && { compact: true }),
...(scheduleId
? {
scheduleId,
@@ -1902,7 +1906,14 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
return;
}
- const persistableContent = filterPersistableAbortContent(aggregatedContent);
+ /** The run is still live here: mark what streamed, but leave the outcome
+ * to whichever path settles the turn (the terminal abort synthesizes
+ * the typed failure a stopped compaction with no summary needs). */
+ const persistableContent = markAbortedCompactionContent(
+ filterPersistableAbortContent(aggregatedContent),
+ isCompaction,
+ { synthesizeFailure: false },
+ );
if (persistableContent.length === 0) {
logger.debug('[ResumableAgentController] No persistable content to save partial response');
return;
diff --git a/api/server/routes/agents/__tests__/abort.spec.js b/api/server/routes/agents/__tests__/abort.spec.js
index 93d3d4e04e..1656bb0a44 100644
--- a/api/server/routes/agents/__tests__/abort.spec.js
+++ b/api/server/routes/agents/__tests__/abort.spec.js
@@ -380,6 +380,69 @@ describe('Agent Abort Endpoint', () => {
}),
).resolves.toBe(false);
});
+
+ /** A compaction's `userMessage` is the persisted leaf projected for
+ * identity only (`projectCompactionAnchor`), so upserting it would
+ * erase a user leaf's text or turn an assistant leaf into an empty
+ * user row: Stop writes only the aborted response. */
+ it('skips the anchor prerequisite when an aborted compaction persists its row', async () => {
+ const jobStreamId = 'test-stream-compact';
+ const anchorId = 'persisted-leaf-1';
+ const compactionRowId = 'compaction-response-1';
+
+ mockGenerationJobManager.getJob.mockResolvedValue({
+ metadata: { userId: 'test-user-123', generationProtocolVersion: 2 },
+ });
+
+ const abortResult = {
+ success: true,
+ jobData: {
+ compact: true,
+ createdEventEmitted: true,
+ userMessage: {
+ messageId: anchorId,
+ parentMessageId: 'older-response',
+ conversationId: jobStreamId,
+ text: '',
+ },
+ responseMessageId: compactionRowId,
+ conversationId: jobStreamId,
+ endpoint: 'agents',
+ sender: 'TestAgent',
+ model: 'agent-1',
+ },
+ content: [
+ {
+ type: 'error',
+ error: JSON.stringify({ type: 'compaction_failed' }),
+ initiatedBy: 'user',
+ },
+ ],
+ text: '',
+ };
+ mockGenerationJobManager.abortJob.mockImplementation(async (_streamId, options) => {
+ await options.beforePublish(abortResult);
+ return abortResult;
+ });
+
+ const response = await request(app)
+ .post('/api/agents/chat/abort')
+ .set('X-LibreChat-Generation-Protocol', '2')
+ .send({ conversationId: jobStreamId, generationProtocolVersion: 2 });
+
+ expect(response.status).toBe(200);
+ expect(mockSaveMessage).toHaveBeenCalledTimes(1);
+ expect(mockSaveMessage).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.objectContaining({
+ messageId: compactionRowId,
+ parentMessageId: anchorId,
+ unfinished: true,
+ isCreatedByUser: false,
+ }),
+ expect.objectContaining({ context: expect.stringContaining('abort endpoint') }),
+ );
+ });
});
describe('Partial Response Saving', () => {
diff --git a/api/server/routes/agents/index.js b/api/server/routes/agents/index.js
index 8c6308fa78..b2579f3dc8 100644
--- a/api/server/routes/agents/index.js
+++ b/api/server/routes/agents/index.js
@@ -789,6 +789,10 @@ router.post('/chat/abort', chatConfigMiddleware, async (req, res, next) => {
* its parent and the preliminary-parent fence correctly rejects it. */
const shouldPersistAbortedTurn =
hasPersistableAbortContent(content) || jobData?.createdEventEmitted === true;
+ /** A compaction's `userMessage` is the already-persisted leaf
+ * projected for identity only; upserting it would erase a user
+ * leaf's text or turn an assistant leaf into an empty user row. */
+ const shouldPersistAnchor = jobData?.compact !== true;
if (
jobData?.userMessage?.messageId &&
@@ -852,23 +856,26 @@ router.post('/chat/abort', chatConfigMiddleware, async (req, res, next) => {
* with neither row stored. Both writes are idempotent upserts;
* await the user prerequisite first, but still attempt the child
* write and checkpoint cleanup so every independently useful
- * operation gets a chance to succeed. */
+ * operation gets a chance to succeed. A compaction skips the
+ * prerequisite: its anchor is the persisted leaf itself. */
let persistedRequestId;
- try {
- const persistedRequest = await saveAbortedUserMessage(
- { saveMessage, getPersistedPrivateTextId, getPrivateMessageTexts },
- messageContext,
- requestMessage,
- { context: 'api/server/routes/agents/index.js - abort user prerequisite' },
- req.user?.tenantId,
- pendingAbortResult.finalEvent,
- );
- if (!persistedRequest) {
- throw new Error('Abort user prerequisite was not persisted');
+ if (shouldPersistAnchor) {
+ try {
+ const persistedRequest = await saveAbortedUserMessage(
+ { saveMessage, getPersistedPrivateTextId, getPrivateMessageTexts },
+ messageContext,
+ requestMessage,
+ { context: 'api/server/routes/agents/index.js - abort user prerequisite' },
+ req.user?.tenantId,
+ pendingAbortResult.finalEvent,
+ );
+ if (!persistedRequest) {
+ throw new Error('Abort user prerequisite was not persisted');
+ }
+ persistedRequestId = persistedRequest._id;
+ } catch (error) {
+ persistenceErrors.push(error);
}
- persistedRequestId = persistedRequest._id;
- } catch (error) {
- persistenceErrors.push(error);
}
try {
diff --git a/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts b/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts
index ef0bb10477..d66c18b697 100644
--- a/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts
+++ b/e2e/specs/mock/scenarios/compaction-rerun-controls.spec.ts
@@ -117,6 +117,78 @@ async function compactWithEmptySummarizer(page: Page, request: APIRequestContext
return { conversationId, compactionId: compactionId as string };
}
+/**
+ * A real cancelled compaction: the fixture summarizer is held silent so the run
+ * is still summarizing when the composer's Stop fires, and the branch ends in
+ * a user message, the shape whose Regenerate would otherwise answer that user
+ * turn instead of redoing the compaction. Returns the conversation and the
+ * turn the cancelled compaction persisted.
+ *
+ * The conversation is created through the composer (a seeded one carries no
+ * model, so its compact submission never starts) and the user leaf is seeded
+ * onto the answer: seeded rows are newer, so the leaf reads as latest.
+ */
+async function cancelCompactionOnUserLeaf(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 answerId = await withMongo(async (db) => {
+ const row = await db
+ .collection('messages')
+ .findOne({ conversationId, isCreatedByUser: false }, { sort: { createdAt: -1 } });
+ return row?.messageId as string | undefined;
+ });
+ expect(answerId).toBeTruthy();
+
+ const leafUserId = randomUUID();
+ await seedMessages(userEmail, conversationId, [
+ {
+ messageId: leafUserId,
+ parentMessageId: answerId as string,
+ text: `Compact this before answering ${label}`,
+ isCreatedByUser: true,
+ sender: 'User',
+ },
+ ]);
+
+ const behavior = await request.post(`${LABEL_SERVER}/__e2e/behavior`, {
+ data: { mode: 'ok', delayMs: 60_000 },
+ });
+ expect(behavior.ok()).toBeTruthy();
+
+ await page.goto(`/c/${conversationId}`);
+ await expect(
+ messagesView(page).getByText(`Compact this before answering ${label}`),
+ ).toBeVisible();
+ await page.getByTestId('token-usage').click();
+ await page.getByRole('button', { name: 'Compact context' }).click();
+ const stop = page.getByTestId('stop-generation-button');
+ await expect(stop).toBeVisible({ timeout: 20_000 });
+ await stop.click();
+
+ /** The Stop route persists the aborted turn before it publishes the final
+ * event, so the row is in storage once the stop settles. */
+ let compactionId: string | undefined;
+ await expect
+ .poll(
+ () =>
+ withMongo(async (db) => {
+ const row = await db.collection('messages').findOne({
+ conversationId,
+ parentMessageId: leafUserId,
+ isCreatedByUser: false,
+ });
+ compactionId = row?.messageId as string | undefined;
+ return compactionId != null;
+ }),
+ { timeout: 20_000 },
+ )
+ .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
@@ -315,6 +387,61 @@ test.describe('compaction rerun controls', () => {
}
});
+ /* A real cancelled run, not a seeded row: the abort path owns the stopped
+ turn, so the marker has to be stamped where the aborted content is
+ assembled or the row keeps the user turn's rerun controls live. */
+ test('a cancelled compaction on a user leaf offers no rerun controls @scenario:cancelled-compaction-on-user-turn-offers-no-rerun-controls', async ({
+ page,
+ request,
+ }) => {
+ const { conversationId, compactionId } = await cancelCompactionOnUserLeaf(
+ page,
+ request,
+ 'cancelled-live',
+ );
+ try {
+ const row = page.locator(`[id="${compactionId}"]`);
+ await expect(row).toBeVisible();
+ await row.hover();
+
+ /* Replaying the user turn behind it would answer that message again rather
+ than redo the compaction, so the marker withholds the controls here too.
+ The stored outcome text is the reload scenario's to check: the live row
+ renders the turn it streamed, which stopped before its first delta. */
+ 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 cancelled turn read back from storage: the row the abort path
+ persisted has to carry the marker a reload rebuilds it from. */
+ test('a cancelled compaction on a user leaf stays free of rerun controls after a reload @scenario:cancelled-compaction-on-user-turn-survives-reload-without-rerun-controls', async ({
+ page,
+ request,
+ }) => {
+ const { conversationId, compactionId } = await cancelCompactionOnUserLeaf(
+ page,
+ request,
+ 'cancelled-reload',
+ );
+ 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,
}) => {
diff --git a/packages/api/src/agents/compaction.spec.ts b/packages/api/src/agents/compaction.spec.ts
index a8910eeb3e..5e6c2b924a 100644
--- a/packages/api/src/agents/compaction.spec.ts
+++ b/packages/api/src/agents/compaction.spec.ts
@@ -15,6 +15,7 @@ import {
dropUnusableSummaryParts,
findCheckpointSummaryPart,
getSummaryPartText,
+ markAbortedCompactionContent,
markCompactionOutcome,
resolveFailedTurnContent,
resolveCheckpointMessage,
@@ -317,6 +318,120 @@ describe('resolveCheckpointMessage', () => {
});
});
+describe('markAbortedCompactionContent', () => {
+ const partialSummary = (text: string): TMessageContentParts => ({
+ type: ContentTypes.SUMMARY,
+ /** Streamed deltas never carry a boundary; a stopped round keeps them. */
+ content: [{ type: ContentTypes.TEXT, text }],
+ summarizing: true,
+ });
+
+ /** The summarizer opens the part when its round starts, so a stop can land
+ * between that and the first delta. */
+ const emptySummaryPlaceholder = (): TMessageContentParts => ({
+ type: ContentTypes.SUMMARY,
+ content: [],
+ summarizing: true,
+ });
+
+ const completedSummary = (text: string): TMessageContentParts => ({
+ type: ContentTypes.SUMMARY,
+ content: [{ type: ContentTypes.TEXT, text }],
+ boundary: completedBoundary,
+ });
+
+ /** The abort path owns a cancelled run's row: its partial summary must still
+ * carry the marker, or on a branch ending in a user message the row keeps a
+ * Regenerate that answers that user turn instead of redoing the compaction.
+ * The truncated prefix is kept but marked failed, or its label presents it
+ * as a finished checkpoint. */
+ it('marks the partial summary a stopped compaction had streamed as failed', () => {
+ const parts = [partialSummary('Half a summary')];
+
+ markAbortedCompactionContent(parts, true);
+
+ expect(parts).toHaveLength(1);
+ expect(parts[0]).toMatchObject({ initiatedBy: 'user', failed: true, summarizing: true });
+ });
+
+ /** A round that finished before the Stop landed is a real checkpoint: the
+ * race is not a failure. */
+ it('marks a summary that completed before the stop without failing it', () => {
+ const parts = [completedSummary('Finished before the stop.')];
+
+ markAbortedCompactionContent(parts, true);
+
+ expect(parts).toHaveLength(1);
+ expect(parts[0]).toMatchObject({ initiatedBy: 'user' });
+ expect(parts[0]).not.toHaveProperty('failed');
+ });
+
+ /** Every part that can carry the marker gets it: the row's identity must not
+ * depend on which of its parts a reader inspects first. */
+ it('marks an error part the stopped run had already recorded', () => {
+ const parts: TMessageContentParts[] = [
+ partialSummary('Half a summary'),
+ { type: ContentTypes.ERROR, error: 'Something else failed first' },
+ ];
+
+ markAbortedCompactionContent(parts, true);
+
+ expect(parts[0]).toMatchObject({ initiatedBy: 'user', failed: true });
+ expect(parts[1]).toMatchObject({ initiatedBy: 'user' });
+ });
+
+ /** A summary placeholder with no text is not an outcome: nothing of the
+ * round survived to show, so the typed failure is the row's whole
+ * outcome. */
+ it('replaces a summary placeholder that streamed nothing with the typed failure', () => {
+ const parts = [emptySummaryPlaceholder()];
+
+ markAbortedCompactionContent(parts, true);
+
+ expect(parts).toEqual([
+ {
+ type: ContentTypes.ERROR,
+ error: JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),
+ initiatedBy: 'user',
+ },
+ ]);
+ });
+
+ /** A run stopped before any part streamed still needs an identifiable row:
+ * an empty one reads as an answer to the message it hangs off. */
+ it('records the typed failure when nothing streamed before the stop', () => {
+ const parts: TMessageContentParts[] = [];
+
+ markAbortedCompactionContent(parts, true);
+
+ expect(parts).toEqual([
+ {
+ type: ContentTypes.ERROR,
+ error: JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED }),
+ initiatedBy: 'user',
+ },
+ ]);
+ });
+
+ /** The disconnect save runs while the generation is still live and the
+ * completion path overwrites the row: it stamps identity and rewrites
+ * nothing else, not even the failure flag of a still-streaming part. */
+ it('marks a non-terminal snapshot without failing or synthesizing anything', () => {
+ const parts: TMessageContentParts[] = [partialSummary('Half a summary')];
+
+ markAbortedCompactionContent(parts, true, { synthesizeFailure: false });
+
+ expect(parts).toEqual([{ ...partialSummary('Half a summary'), initiatedBy: 'user' }]);
+ });
+
+ it('returns content from a turn that was not a compaction unchanged', () => {
+ const parts = [partialSummary('An automatic detour partial')];
+
+ expect(markAbortedCompactionContent(parts, false)).toBe(parts);
+ expect(parts[0]).not.toHaveProperty('initiatedBy');
+ });
+});
+
describe('findCheckpointSummaryPart', () => {
const legacySummary = { type: ContentTypes.SUMMARY, text: 'Summary of conversation' };
diff --git a/packages/api/src/agents/compaction.ts b/packages/api/src/agents/compaction.ts
index 241c394965..063870965a 100644
--- a/packages/api/src/agents/compaction.ts
+++ b/packages/api/src/agents/compaction.ts
@@ -196,6 +196,73 @@ export function resolveFailedTurnContent(
return { content: compactionFailureContent(errorText) };
}
+/**
+ * The content an aborted compaction persists: the run's stream-aggregated
+ * parts, carrying the marker that keeps the turn identifiable as a compaction.
+ * The abort path owns a cancelled run's row (a stopped turn is unfinished, not
+ * failed) and nothing else on that path knows the request was a compaction, so
+ * without this the row reads as an answer to the message it hangs off and keeps
+ * that message's rerun controls: on a branch ending in a user message,
+ * Regenerate would answer the user turn behind the compaction instead of
+ * redoing it.
+ *
+ * A terminal abort (Stop) settles the turn, so it applies the completed run's
+ * outcome rules: a usable summary is marked as the outcome; a partial one
+ * keeps its text but is marked `failed`, or its label would present the
+ * truncated prefix as a finished checkpoint; a placeholder that never streamed
+ * 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.
+ */
+export function markAbortedCompactionContent(
+ contentParts: TMessageContentParts[],
+ isCompaction: boolean,
+ { synthesizeFailure = true }: { synthesizeFailure?: boolean } = {},
+): TMessageContentParts[] {
+ if (!isCompaction) {
+ return contentParts;
+ }
+ let hasOutcome = false;
+ for (let index = contentParts.length - 1; index >= 0; index -= 1) {
+ const part = contentParts[index];
+ if (part == null) {
+ continue;
+ }
+ if (part.type === ContentTypes.ERROR) {
+ part.initiatedBy = 'user';
+ hasOutcome = true;
+ continue;
+ }
+ if (part.type !== ContentTypes.SUMMARY) {
+ 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';
+ hasOutcome = true;
+ continue;
+ }
+ if (!synthesizeFailure) {
+ summary.initiatedBy = 'user';
+ continue;
+ }
+ if (isSummaryPartWithText(summary)) {
+ summary.initiatedBy = 'user';
+ summary.failed = true;
+ hasOutcome = true;
+ continue;
+ }
+ contentParts.splice(index, 1);
+ }
+ if (!hasOutcome && synthesizeFailure) {
+ contentParts.push(...compactionFailureContent());
+ }
+ return contentParts;
+}
+
/**
* 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
diff --git a/packages/api/src/stream/GenerationJobManager.ts b/packages/api/src/stream/GenerationJobManager.ts
index 4284330a15..9f721c2a3d 100644
--- a/packages/api/src/stream/GenerationJobManager.ts
+++ b/packages/api/src/stream/GenerationJobManager.ts
@@ -92,6 +92,7 @@ import {
} from './internal/timing';
import { filterPersistableAbortContent } from './abortContent';
import { toClientPendingAction } from '~/agents/hitl/policy';
+import { markAbortedCompactionContent } from '~/agents/compaction';
import { ApprovalLifecycle, pausePersistenceActionId } from './ApprovalLifecycle';
import { projectPendingMCPOAuthPrompts } from '~/mcp/oauth/resume';
import { sanitizeJobMetadata } from './metadata';
@@ -4903,6 +4904,13 @@ class GenerationJobManagerClass {
// Filter only after the transform so sparse/empty/OAuth parts cannot
// shift a retained ID-less ask answer onto a different tool call.
abortContent = filterPersistableAbortContent(content);
+ // A stopped compaction is unfinished rather than failed, so the row keeps
+ // the abort shape, plus the marker that keeps it identifiable as the
+ // compaction's own turn instead of an answer to its parent.
+ abortContent = markAbortedCompactionContent(
+ abortContent as TMessageContentParts[],
+ jobData.compact === true,
+ );
shouldPersistAbortContent = abortContent.length > 0;
text = shouldPersistAbortContent
? parseTextParts(abortContent as TMessageContentParts[], false, {
diff --git a/packages/api/src/stream/__tests__/RedisJobStore.spec.ts b/packages/api/src/stream/__tests__/RedisJobStore.spec.ts
index afd49564a4..e8a7a63790 100644
--- a/packages/api/src/stream/__tests__/RedisJobStore.spec.ts
+++ b/packages/api/src/stream/__tests__/RedisJobStore.spec.ts
@@ -335,6 +335,7 @@ describe('RedisJobStore', () => {
iconURL: 'https://example.com/icon.png',
model: 'test-model',
agent_id: 'agent-1',
+ compact: true,
isTemporary: false,
retentionExpiresAt: '2030-01-01T00:00:00.000Z',
agentEventDeliveryKey: 'completion-delivery-1',
@@ -408,6 +409,9 @@ describe('RedisJobStore', () => {
* degrading to ordinary steering in every Redis deployment.
*/
expect(job.preemptCapable).toBe(true);
+ /** The abort paths read this flag from a reloaded job, so it has to
+ * survive the Redis round trip for a stopped compaction to be stamped. */
+ expect(job.compact).toBe(true);
expect(job.steerQuotesExecutionId).toBe('exec-1');
expect(job.generationProtocolVersion).toBe(2);
expect(job.checkpointNamespace).toEqual(expect.any(String));
diff --git a/packages/api/src/stream/__tests__/abortCompactionIdentity.spec.ts b/packages/api/src/stream/__tests__/abortCompactionIdentity.spec.ts
new file mode 100644
index 0000000000..12da6035a7
--- /dev/null
+++ b/packages/api/src/stream/__tests__/abortCompactionIdentity.spec.ts
@@ -0,0 +1,148 @@
+/**
+ * A Stop persists the aborted turn from job data alone, so the row only knows
+ * the run was a compaction through the job's `compact` metadata. Without the
+ * marker the stopped row reads as an answer to the message it hangs off and
+ * keeps that message's rerun controls: on a branch ending in a user message,
+ * Regenerate would answer the user turn behind the compaction.
+ */
+import { ContentTypes, ErrorTypes } from 'librechat-data-provider';
+import type { Agents } from 'librechat-data-provider';
+
+/** Suppress winston Console transport output (survives jest.resetModules) */
+jest.spyOn(console, 'log').mockImplementation();
+
+const COMPACTION_FAILED_ERROR = JSON.stringify({ type: ErrorTypes.COMPACTION_FAILED });
+
+/** Streamed deltas never carry a boundary; a stopped round keeps them. */
+const partialSummary: Agents.MessageContentComplex = {
+ type: ContentTypes.SUMMARY,
+ content: [{ type: ContentTypes.TEXT, text: 'Half a summary' }],
+ summarizing: true,
+};
+
+const partialText: Agents.MessageContentComplex = { type: ContentTypes.TEXT, text: 'Partial' };
+
+/** The abort result's `finalEvent` is `unknown` to the interface; the fields
+ * these tests read are the ones the abort FINAL always carries. */
+type AbortFinalEvent = {
+ responseMessage?: { unfinished?: boolean; error?: boolean } | null;
+ earlyAbort?: boolean;
+};
+
+async function configureManager() {
+ const { GenerationJobManager } = await import('../GenerationJobManager');
+ const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore');
+ const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport');
+
+ const jobStore = new InMemoryJobStore();
+ GenerationJobManager.configure({
+ jobStore,
+ eventTransport: new InMemoryEventTransport(),
+ isRedis: false,
+ cleanupOnComplete: false,
+ });
+ GenerationJobManager.initialize();
+ return { manager: GenerationJobManager, jobStore };
+}
+
+describe('abortJob compaction identity', () => {
+ beforeEach(() => {
+ jest.resetModules();
+ });
+
+ it('stamps the partial summary a stopped compaction had streamed as failed', async () => {
+ const { manager, jobStore } = await configureManager();
+ const streamId = 'abort-compaction-partial';
+ const job = await manager.createJob(streamId, 'user-1', 'conversation-1', {
+ initialMetadata: { compact: true },
+ });
+ jobStore.setContentParts(streamId, [partialSummary], job.createdAt);
+
+ const result = await manager.abortJob(streamId);
+ const finalEvent = result.finalEvent as AbortFinalEvent;
+
+ expect(result.success).toBe(true);
+ /** The row keeps its unfinished shape; the partial summary keeps its text
+ * but reads as failed, or its label presents the truncated prefix as a
+ * finished checkpoint. */
+ expect(finalEvent.responseMessage).toMatchObject({ unfinished: true, error: false });
+ expect(result.content).toEqual([
+ { ...partialSummary, initiatedBy: 'user', failed: true } as Agents.MessageContentComplex,
+ ]);
+
+ await manager.destroy();
+ });
+
+ /** A run stopped before any part streamed still needs an identifiable row:
+ * an empty one reads as an answer to the message it hangs off. */
+ it('records the typed failure when a stopped compaction streamed nothing', async () => {
+ const { manager } = await configureManager();
+ const streamId = 'abort-compaction-empty';
+ await manager.createJob(streamId, 'user-1', 'conversation-1', {
+ initialMetadata: { compact: true },
+ });
+
+ const result = await manager.abortJob(streamId);
+ const finalEvent = result.finalEvent as AbortFinalEvent;
+ const expectedContent: Agents.MessageContentComplex[] = [
+ {
+ type: ContentTypes.ERROR,
+ error: COMPACTION_FAILED_ERROR,
+ initiatedBy: 'user',
+ },
+ ];
+
+ expect(result.success).toBe(true);
+ expect(result.content).toEqual(expectedContent);
+ /** The typed failure makes the row persistable, so this is not an early
+ * abort: the compaction's turn exists and must reach storage. */
+ expect(finalEvent.responseMessage).not.toBeNull();
+ expect(finalEvent.earlyAbort).not.toBe(true);
+
+ await manager.destroy();
+ });
+
+ /** A placeholder the summarizer opened but never streamed text into carries
+ * nothing to show: the typed failure replaces it as the row's outcome. */
+ it('replaces an empty summary placeholder with the typed failure', async () => {
+ const { manager, jobStore } = await configureManager();
+ const streamId = 'abort-compaction-placeholder';
+ const placeholder: Agents.MessageContentComplex = {
+ type: ContentTypes.SUMMARY,
+ content: [],
+ summarizing: true,
+ };
+ const job = await manager.createJob(streamId, 'user-1', 'conversation-1', {
+ initialMetadata: { compact: true },
+ });
+ jobStore.setContentParts(streamId, [placeholder], job.createdAt);
+
+ const result = await manager.abortJob(streamId);
+
+ expect(result.success).toBe(true);
+ expect(result.content).toEqual([
+ {
+ type: ContentTypes.ERROR,
+ error: COMPACTION_FAILED_ERROR,
+ initiatedBy: 'user',
+ },
+ ]);
+
+ await manager.destroy();
+ });
+
+ it('leaves a stopped ordinary turn without the marker', async () => {
+ const { manager, jobStore } = await configureManager();
+ const streamId = 'abort-ordinary-turn';
+ const job = await manager.createJob(streamId, 'user-1', 'conversation-1');
+ jobStore.setContentParts(streamId, [partialText], job.createdAt);
+
+ const result = await manager.abortJob(streamId);
+
+ expect(result.success).toBe(true);
+ expect(result.content).toEqual([partialText]);
+ expect(result.content[0]).not.toHaveProperty('initiatedBy');
+
+ await manager.destroy();
+ });
+});
diff --git a/packages/api/src/stream/implementations/RedisJobStore.ts b/packages/api/src/stream/implementations/RedisJobStore.ts
index 09e2688d52..dcf31dc71a 100644
--- a/packages/api/src/stream/implementations/RedisJobStore.ts
+++ b/packages/api/src/stream/implementations/RedisJobStore.ts
@@ -5647,6 +5647,7 @@ export class RedisJobStore implements IJobStoreV2 {
userMessage: data.userMessage ? JSON.parse(data.userMessage) : undefined,
responseMessageId: data.responseMessageId || undefined,
isRegenerate: data.isRegenerate != null ? data.isRegenerate === '1' : undefined,
+ compact: data.compact != null ? data.compact === '1' : undefined,
mcpRequestBody: data.mcpRequestBody ? JSON.parse(data.mcpRequestBody) : undefined,
userSubmittedPaths: data.userSubmittedPaths ? JSON.parse(data.userSubmittedPaths) : undefined,
userSubmittedMessageFieldPaths: data.userSubmittedMessageFieldPaths
diff --git a/packages/api/src/stream/interfaces/IJobStore.ts b/packages/api/src/stream/interfaces/IJobStore.ts
index bd169c7299..d160933229 100644
--- a/packages/api/src/stream/interfaces/IJobStore.ts
+++ b/packages/api/src/stream/interfaces/IJobStore.ts
@@ -172,6 +172,9 @@ export interface SerializableJobData {
/** Whether this generation replaces an existing assistant branch. */
isRegenerate?: boolean;
+ /** Whether this generation is a manual context compaction; the abort paths
+ * read it to stamp the stopped row with the compaction's identity. */
+ compact?: boolean;
/** Exact normalized MCP placeholder identity for this turn. */
mcpRequestBody?: MCPRuntimeRequestBody;
/** Exact assistant-message fields authored by the user during this running job. */
@@ -462,6 +465,7 @@ export type JobMetadataPatch = Partial<
SerializableJobData,
| 'responseMessageId'
| 'isRegenerate'
+ | 'compact'
| 'mcpRequestBody'
| 'userSubmittedPaths'
| 'userSubmittedMessageFieldPaths'
diff --git a/packages/api/src/stream/metadata.ts b/packages/api/src/stream/metadata.ts
index 2b2d27eb64..61455d2161 100644
--- a/packages/api/src/stream/metadata.ts
+++ b/packages/api/src/stream/metadata.ts
@@ -14,6 +14,9 @@ export function sanitizeJobMetadata(metadata: Partial<GenerationJobMetadata>): J
if (metadata.isRegenerate !== undefined) {
patch.isRegenerate = metadata.isRegenerate;
}
+ if (metadata.compact !== undefined) {
+ patch.compact = metadata.compact;
+ }
if (metadata.mcpRequestBody) {
patch.mcpRequestBody = metadata.mcpRequestBody;
}
diff --git a/packages/api/src/types/stream.ts b/packages/api/src/types/stream.ts
index 4802d79d7b..f478ee3052 100644
--- a/packages/api/src/types/stream.ts
+++ b/packages/api/src/types/stream.ts
@@ -32,6 +32,9 @@ export interface GenerationJobMetadata {
responseMessageId?: string;
/** Whether this generation replaces an existing assistant branch. */
isRegenerate?: boolean;
+ /** Whether this generation is a manual context compaction; the abort paths
+ * read it to stamp the stopped row with the compaction's identity. */
+ compact?: boolean;
/** Exact normalized MCP placeholder identity for this turn. Persisted so HITL
* resume does not reconstruct a different parent or overridden conversation. */
mcpRequestBody?: MCPRuntimeRequestBody;