{"record":{"id":"5d50a0a69bcb33af","repo":"danny-avila/LibreChat","slug":"subagent-agentid-changed-before-it-could-be-ini","errorCode":null,"errorMessage":"Subagent ${agentId} changed before it could be initialized.","messagePattern":"Subagent (.+?) changed before it could be initialized\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"api/server/services/Endpoints/agents/initialize.js","lineNumber":836,"sourceCode":"      }\n      logger.error(`[initializeClient] Error loading subagent metadata ${agentId}:`, error);\n      skippedAgentIds.add(agentId);\n      return null;\n    }\n  };\n\n  /**\n   * Resolves the selected descriptor inside the foreground request. The\n   * legacy initializer requires request/response objects for tool and MCP\n   * setup, so this intentionally remains request-scoped until AI-1597 gives\n   * child execution a durable runtime context.\n   */\n  const initializeLazySubagent = async ({ agentId, configId, context, lazyChildren }) => {\n    throwIfAborted(context.signal);\n    const agent = await waitForAbort(db.getAgentWithVersionCount({ id: agentId }), context.signal);\n    throwIfAborted(context.signal);\n    if (!agent || getLazySubagentConfigId(agent) !== configId) {\n      throw new Error(`Subagent ${agentId} changed before it could be initialized.`);\n    }\n    if (!(await hasSubagentViewAccess(agent, agentId, context.signal))) {\n      throw new Error(`You no longer have access to subagent ${agentId}.`);\n    }\n    const validation = await waitForAbort(\n      validateAgentModel({ req, res, agent, modelsConfig, logViolation }),\n      context.signal,\n    );\n    throwIfAborted(context.signal);\n    if (!validation.isValid) {\n      throw new Error(validation.error?.message ?? `Subagent ${agentId} failed model validation.`);\n    }\n    const scopedSkillIds = resolveAgentScopedSkillIds({\n      agent,\n      accessibleSkillIds,\n      skillsCapabilityEnabled,\n      ephemeralSkillsToggle,\n    });","sourceCodeStart":818,"sourceCodeEnd":854,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/server/services/Endpoints/agents/initialize.js#L818-L854","documentation":"Thrown during lazy subagent initialization when the freshly fetched agent is missing or its current config id no longer matches the config id captured when the graph was planned. This is a TOCTOU (time-of-check/time-of-use) guard: the agent changed between planning and resolution.","triggerScenarios":"The subagent's definition was edited, re-versioned, or deleted between graph planning and lazy initialization within the same request; an abort/version mismatch on getAgentWithVersionCount.","commonSituations":"Concurrent edits to an agent during a long-running request; versioned agents where the version bumped mid-request; deletion of a subagent referenced by an in-flight primary agent.","solutions":["Retry the request after the concurrent edit/deletion settles.","Avoid editing agents that are currently being initialized/invoked.","Use stable agent versions/config ids for production graphs.","Re-plan the subagent graph so it reflects the current agent definitions."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"const agent = await db.getAgentWithVersionCount({ id: agentId });\nif (!agent || getLazySubagentConfigId(agent) !== configId) {\n  throw new Error(`Agent ${agentId} changed; re-plan graph`);\n}","typeGuard":null,"tryCatchPattern":"try { await initializeLazySubagent({ agentId, configId, context }); }\ncatch (e) { if (/changed before it could be initialized/.test(e.message)) { await rePlanGraph(); return; } throw e; }","preventionTips":["Avoid editing agents that have in-flight requests.","Pin stable config ids for production graphs.","Re-plan the graph on a stale-config error rather than retrying blindly."],"tags":["agents","subagents","race-condition","versioning"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}