{"record":{"id":"1b8e1e0c26652497","repo":"ruvnet/ruflo","slug":"delegation-depth-exhausted","errorCode":null,"errorMessage":"delegation-depth-exhausted","messagePattern":"delegation-depth-exhausted","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/security/src/policy/envelope.ts","lineNumber":93,"sourceCode":"    [child.delegationDepth, parent.delegationDepth],\n    [child.expiresAt, parent.expiresAt],\n  ];\n  return listChecks.every(Boolean)\n    && numericChecks.every(([next, current]) => (\n      current === undefined\n        ? true\n        : next !== undefined && next <= current\n    ))\n    && !(child.network === true && parent.network !== true)\n    && !(child.destructive === true && parent.destructive !== true);\n}\n\nexport function delegateEnvelope(\n  parent: CapabilityEnvelope,\n  child: CapabilityEnvelope,\n): CapabilityEnvelope {\n  if ((parent.delegationDepth ?? 0) <= 0) {\n    throw new Error('delegation-depth-exhausted');\n  }\n  const reduced = {\n    ...parent,\n    ...child,\n    delegationDepth: Math.min(\n      child.delegationDepth ?? Number.MAX_SAFE_INTEGER,\n      (parent.delegationDepth ?? 0) - 1,\n    ),\n    expiresAt: Math.min(\n      child.expiresAt ?? Number.MAX_SAFE_INTEGER,\n      parent.expiresAt ?? Number.MAX_SAFE_INTEGER,\n    ),\n  };\n  if (!isEnvelopeReduction(parent, reduced)) throw new Error('capability-envelope-cannot-grow');\n  return reduced;\n}\n","sourceCodeStart":75,"sourceCodeEnd":110,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/security/src/policy/envelope.ts#L75-L110","documentation":"delegateEnvelope() implements bounded delegation: a parent envelope may spawn children only while parent.delegationDepth (defaulting to 0) is > 0. Each delegation decrements the depth, so attempting to delegate from an envelope at depth 0 — or one that never set the field — throws Error('delegation-depth-exhausted').","triggerScenarios":"Creating a root envelope without delegationDepth and immediately calling delegateEnvelope(parent, child); an agent chain longer than the root's initial depth (grandchild of a depth-1 envelope); re-delegating an already-exhausted envelope in retry logic.","commonSituations":"Templates for capability envelopes that omit delegationDepth; adding a second tier of sub-agents to a hierarchy whose envelopes were minted with depth for only one tier; defensive envelopes intentionally issued with depth 0 being reused for delegation.","solutions":["Set delegationDepth on the root envelope to the maximum chain length you need (e.g. 3 for root -> agent -> sub-agent -> sub-sub-agent).","Check (parent.delegationDepth ?? 0) > 0 before attempting to delegate, and treat 0 as 'leaf only'.","When a hierarchy deepens, mint a fresh root envelope with a higher depth rather than delegating past the cap."],"exampleFix":"// before\nconst root = mintEnvelope({ principal: 'orchestrator', tools: [...] }); // delegationDepth undefined -> 0\nconst child = delegateEnvelope(root, { principal: 'worker', ... }); // throws\n\n// after\nconst root = mintEnvelope({ principal: 'orchestrator', delegationDepth: 3, tools: [...] });\nconst child = delegateEnvelope(root, { principal: 'worker', ... });","handlingStrategy":"validation","validationCode":"if ((parent.delegationDepth ?? 0) <= 0) {\n  throw new Error(\n    `envelope ${parent.id} is a leaf (delegationDepth 0) — cannot delegate`\n  );\n}\nconst child = delegateEnvelope(parent, childSpec);","typeGuard":"function canDelegate(parent: CapabilityEnvelope): boolean {\n  return (parent.delegationDepth ?? 0) > 0;\n}","tryCatchPattern":"try {\n  return delegateEnvelope(parent, child);\n} catch (err) {\n  if (err instanceof Error && err.message === 'delegation-depth-exhausted') {\n    return requestFreshRootEnvelope(parent.id); // mint from the authority instead\n  }\n  throw err;\n}","preventionTips":["Set delegationDepth on root envelopes to your planned hierarchy depth plus one.","Treat depth 0/undefined as 'leaf-only' and short-circuit before calling delegateEnvelope.","When adding tiers of sub-agents, re-mint the root with higher depth rather than exceeding the cap."],"tags":["capability","delegation","security","authorization"],"backgroundTag":"delegation-depth-exceeded","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}