{"record":{"id":"a05e7fde66b84066","repo":"bytedance/deer-flow","slug":"failed-to-compact-context","errorCode":null,"errorMessage":"Failed to compact context.","messagePattern":"Failed to compact context\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/src/core/threads/api.ts","lineNumber":158,"sourceCode":"): Promise<ThreadCompactResponse> {\n  const response = await fetchWithAuth(\n    `${getBackendBaseURL()}/api/threads/${encodeURIComponent(threadId)}/compact`,\n    {\n      method: \"POST\",\n      headers: {\n        \"Content-Type\": \"application/json\",\n      },\n      body: JSON.stringify({\n        force: true,\n        ...(options.agentName ? { agent_name: options.agentName } : {}),\n        ...(options.modelName ? { model_name: options.modelName } : {}),\n      }),\n      signal: options.signal,\n    },\n  );\n\n  if (!response.ok) {\n    throw new Error(\n      await readThreadAPIError(response, \"Failed to compact context.\"),\n    );\n  }\n\n  return (await response.json()) as ThreadCompactResponse;\n}\n","sourceCodeStart":140,"sourceCodeEnd":165,"githubUrl":"https://github.com/bytedance/deer-flow/blob/1dd6ba1acb03700589994b0366c5d1c7d05e2eff/frontend/src/core/threads/api.ts#L140-L165","documentation":"Thrown when POST .../compact (context compaction) returns non-2xx. The client always sends force:true plus optional agent/model overrides and forwards options.signal so user cancellation aborts the fetch. Backend failures include 404 (unknown thread), 409 (compaction not possible mid-run), 422 (invalid agent_name/model_name), and 504 when compaction of a very long thread exceeds a gateway timeout.","triggerScenarios":"Compacting while a run is active on the thread; passing an agent_name or model_name that doesn't exist in config.yaml (422); compacting a thread whose history was already compacted and pruned; LLM backend failure during summarization (500).","commonSituations":"User clicks 'compact context' on a hot thread; model was renamed in config but the UI still offers the old name; provider API outage during the summarization call.","solutions":["Check the wrapped detail: 422 names the invalid agent/model field — pick a valid one from the model list","Retry after the current run finishes if the conflict is a live run","On 5xx, verify the configured summarization model works (test a normal chat turn) before retrying compaction","Use options.signal only for user cancel, and distinguish AbortError from this HTTP error"],"exampleFix":"// before\nawait compactThreadContext(threadId, {agentName});\n\n// after\ntry {\n  await compactThreadContext(threadId, {agentName});\n} catch (e) {\n  if (e instanceof Error && /invalid|unknown/i.test(e.message)) {\n    return compactThreadContext(threadId, {}); // drop bad override\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"async function compactionOptionsValid(options: CompactThreadContextOptions): Promise<boolean> {\n  if (!options.agentName && !options.modelName) return true;\n  const models = await loadModelList();\n  return (!options.agentName || models.agents.includes(options.agentName)) &&\n         (!options.modelName || models.names.includes(options.modelName));\n}","typeGuard":"export function isCompactError(e: unknown): e is Error {\n  return e instanceof Error && e.message.includes('Failed to compact context.');\n}","tryCatchPattern":"try {\n  return await compactThreadContext(threadId, options);\n} catch (e) {\n  if (isCompactError(e) && /invalid|unknown/i.test(e.message)) {\n    return compactThreadContext(threadId, {}); // drop bad overrides\n  }\n  if (isCompactError(e) && /conflict|409/i.test(e.message)) {\n    return retryAfterRunCompletes(threadId, options);\n  }\n  throw e;\n}","preventionTips":["Offer only agent/model names from the live config in the UI","Disable compact while a run is active","Keep options.signal wired to the cancel button so aborts don't surface as HTTP errors"],"tags":["threads","compaction","http","frontend"],"backgroundTag":null,"analyzedSha":"1dd6ba1acb03700589994b0366c5d1c7d05e2eff","analyzedAt":"2026-08-14T21:20:34.804Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}