nexus-agents 2.70.1 → 2.72.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/dist/{adaptive-memory-MKSYEBST.js → adaptive-memory-UPE76IP6.js} +5 -5
  2. package/dist/{chunk-DWLATKBK.js → child-mcp-config-5HRJGLCR.js} +6 -4
  3. package/dist/child-mcp-config-5HRJGLCR.js.map +1 -0
  4. package/dist/{chunk-TTZODPLI.js → chunk-2JMUX5UA.js} +25 -12
  5. package/dist/{chunk-TTZODPLI.js.map → chunk-2JMUX5UA.js.map} +1 -1
  6. package/dist/{chunk-ZPPX2K57.js → chunk-2KB63QGE.js} +2 -2
  7. package/dist/{chunk-L2LQ3TSV.js → chunk-2MD5MWCK.js} +2 -2
  8. package/dist/{chunk-ANC3HU6F.js → chunk-345KMHWH.js} +6 -6
  9. package/dist/chunk-345KMHWH.js.map +1 -0
  10. package/dist/{chunk-7ZPYV4HO.js → chunk-3FIDMWFC.js} +2 -2
  11. package/dist/chunk-3FIDMWFC.js.map +1 -0
  12. package/dist/{chunk-H5RKTUDD.js → chunk-3HR6UJ2E.js} +3243 -7299
  13. package/dist/chunk-3HR6UJ2E.js.map +1 -0
  14. package/dist/{chunk-UMLBVSW4.js → chunk-53K3KEKT.js} +51 -707
  15. package/dist/chunk-53K3KEKT.js.map +1 -0
  16. package/dist/chunk-5MHIWRKB.js +691 -0
  17. package/dist/chunk-5MHIWRKB.js.map +1 -0
  18. package/dist/{chunk-VGZJIR22.js → chunk-5WQ3SRSE.js} +2 -2
  19. package/dist/{chunk-TOYPY5XA.js → chunk-A35XORXU.js} +73 -10
  20. package/dist/chunk-A35XORXU.js.map +1 -0
  21. package/dist/chunk-BVETPIOQ.js +556 -0
  22. package/dist/chunk-BVETPIOQ.js.map +1 -0
  23. package/dist/{chunk-OF7CYMMA.js → chunk-DA5UDQYW.js} +2 -2
  24. package/dist/{chunk-XATH462F.js → chunk-ES6GFP35.js} +186 -34
  25. package/dist/chunk-ES6GFP35.js.map +1 -0
  26. package/dist/chunk-GOT7OAL5.js +59 -0
  27. package/dist/chunk-GOT7OAL5.js.map +1 -0
  28. package/dist/{chunk-LJT65EA7.js → chunk-I7ORMAO7.js} +2 -2
  29. package/dist/{chunk-LMRKHQG5.js → chunk-L6N2S3UB.js} +2 -2
  30. package/dist/{chunk-7OBFO4GF.js → chunk-O4KUCF5S.js} +125 -40
  31. package/dist/chunk-O4KUCF5S.js.map +1 -0
  32. package/dist/chunk-P5OFZWDW.js +303 -0
  33. package/dist/chunk-P5OFZWDW.js.map +1 -0
  34. package/dist/{chunk-MJHOSM5U.js → chunk-QECRZ3YA.js} +2 -2
  35. package/dist/{chunk-WYSHXPKK.js → chunk-QL4HCYRD.js} +4 -44
  36. package/dist/chunk-QL4HCYRD.js.map +1 -0
  37. package/dist/{chunk-E66KFRSJ.js → chunk-TF3GROMO.js} +2 -2
  38. package/dist/{chunk-U3HZQTUF.js → chunk-TQFRPFMG.js} +2 -2
  39. package/dist/{chunk-7QWNOE23.js → chunk-V7ATY4BG.js} +3 -3
  40. package/dist/{chunk-32RIOULO.js → chunk-VPC3YNFR.js} +2 -2
  41. package/dist/{chunk-L4XSIHF5.js → chunk-VTVKC4FS.js} +4 -4
  42. package/dist/{chunk-6ZLFULSQ.js → chunk-XHVDKY3X.js} +315 -31
  43. package/dist/chunk-XHVDKY3X.js.map +1 -0
  44. package/dist/cli-circuit-breaker-GFF2RLBZ.js +14 -0
  45. package/dist/cli.d.ts +3 -1
  46. package/dist/cli.js +1167 -2245
  47. package/dist/cli.js.map +1 -1
  48. package/dist/{composite-router-AYVJPIOS.js → composite-router-33F3F74I.js} +4 -4
  49. package/dist/{consensus-vote-WUGHRBYE.js → consensus-vote-5V4KVHBE.js} +12 -11
  50. package/dist/doctor-deep-AHDTNURD.js +13 -0
  51. package/dist/expert-bridge-DMDHHDEU.js +11 -0
  52. package/dist/factory-FVD7PZ6S.js +15 -0
  53. package/dist/{factory-KMBWFIX2.js → factory-VQS3HJ7V.js} +6 -6
  54. package/dist/index.d.ts +359 -3358
  55. package/dist/index.js +70 -807
  56. package/dist/index.js.map +1 -1
  57. package/dist/init-opencode-EIOIPVWL.js +158 -0
  58. package/dist/init-opencode-EIOIPVWL.js.map +1 -0
  59. package/dist/issue-triage-HJUJWGAD.js +16 -0
  60. package/dist/{learning-persistence-FILWP3IR.js → learning-persistence-N6ILD2HX.js} +3 -3
  61. package/dist/{mobimem-77W5ED4Z.js → mobimem-BOJFXQ7B.js} +4 -4
  62. package/dist/{nexus-data-dir-M6DYKIHJ.js → nexus-data-dir-77UO7N6J.js} +2 -2
  63. package/dist/{registry-command-BBLIXULQ.js → registry-command-NCWUJKAF.js} +4 -4
  64. package/dist/{repo-security-plan-7SNM7JQN.js → repo-security-plan-3J45VAD6.js} +5 -5
  65. package/dist/research-helpers-synthesize-UGQHZZJN.js +12 -0
  66. package/dist/{routing-memory-DCIZEEVC.js → routing-memory-NO7QEH7T.js} +4 -4
  67. package/dist/{session-memory-5TSAASQW.js → session-memory-DOXLEWEU.js} +5 -5
  68. package/dist/{setup-command-PU636ZJH.js → setup-command-DVEBFKR2.js} +10 -10
  69. package/dist/setup-config-E3JZYSLR.js +11 -0
  70. package/dist/{setup-custom-api-IQX3GD2D.js → setup-custom-api-DHJ5DRH2.js} +6 -6
  71. package/dist/{weather-report-NETGWTJX.js → weather-report-FNN4OX3N.js} +4 -4
  72. package/package.json +1 -1
  73. package/dist/chunk-6ZLFULSQ.js.map +0 -1
  74. package/dist/chunk-7OBFO4GF.js.map +0 -1
  75. package/dist/chunk-7ZPYV4HO.js.map +0 -1
  76. package/dist/chunk-ANC3HU6F.js.map +0 -1
  77. package/dist/chunk-DWLATKBK.js.map +0 -1
  78. package/dist/chunk-FDNWRZNJ.js +0 -22
  79. package/dist/chunk-FDNWRZNJ.js.map +0 -1
  80. package/dist/chunk-H5RKTUDD.js.map +0 -1
  81. package/dist/chunk-TOYPY5XA.js.map +0 -1
  82. package/dist/chunk-UMLBVSW4.js.map +0 -1
  83. package/dist/chunk-WYSHXPKK.js.map +0 -1
  84. package/dist/chunk-XATH462F.js.map +0 -1
  85. package/dist/cli-circuit-breaker-2CJ6NV52.js +0 -14
  86. package/dist/doctor-deep-BJFDBGPO.js +0 -13
  87. package/dist/expert-bridge-75WNNWI4.js +0 -11
  88. package/dist/factory-BHHC6C7W.js +0 -15
  89. package/dist/issue-triage-TXQ7J6GG.js +0 -16
  90. package/dist/mcp-config-OCWIXE2Y.js +0 -13
  91. package/dist/research-helpers-synthesize-7CI2FJE5.js +0 -12
  92. package/dist/setup-config-EA5RDIO2.js +0 -11
  93. package/dist/weather-report-NETGWTJX.js.map +0 -1
  94. /package/dist/{adaptive-memory-MKSYEBST.js.map → adaptive-memory-UPE76IP6.js.map} +0 -0
  95. /package/dist/{chunk-ZPPX2K57.js.map → chunk-2KB63QGE.js.map} +0 -0
  96. /package/dist/{chunk-L2LQ3TSV.js.map → chunk-2MD5MWCK.js.map} +0 -0
  97. /package/dist/{chunk-VGZJIR22.js.map → chunk-5WQ3SRSE.js.map} +0 -0
  98. /package/dist/{chunk-OF7CYMMA.js.map → chunk-DA5UDQYW.js.map} +0 -0
  99. /package/dist/{chunk-LJT65EA7.js.map → chunk-I7ORMAO7.js.map} +0 -0
  100. /package/dist/{chunk-LMRKHQG5.js.map → chunk-L6N2S3UB.js.map} +0 -0
  101. /package/dist/{chunk-MJHOSM5U.js.map → chunk-QECRZ3YA.js.map} +0 -0
  102. /package/dist/{chunk-E66KFRSJ.js.map → chunk-TF3GROMO.js.map} +0 -0
  103. /package/dist/{chunk-U3HZQTUF.js.map → chunk-TQFRPFMG.js.map} +0 -0
  104. /package/dist/{chunk-7QWNOE23.js.map → chunk-V7ATY4BG.js.map} +0 -0
  105. /package/dist/{chunk-32RIOULO.js.map → chunk-VPC3YNFR.js.map} +0 -0
  106. /package/dist/{chunk-L4XSIHF5.js.map → chunk-VTVKC4FS.js.map} +0 -0
  107. /package/dist/{cli-circuit-breaker-2CJ6NV52.js.map → cli-circuit-breaker-GFF2RLBZ.js.map} +0 -0
  108. /package/dist/{composite-router-AYVJPIOS.js.map → composite-router-33F3F74I.js.map} +0 -0
  109. /package/dist/{consensus-vote-WUGHRBYE.js.map → consensus-vote-5V4KVHBE.js.map} +0 -0
  110. /package/dist/{doctor-deep-BJFDBGPO.js.map → doctor-deep-AHDTNURD.js.map} +0 -0
  111. /package/dist/{expert-bridge-75WNNWI4.js.map → expert-bridge-DMDHHDEU.js.map} +0 -0
  112. /package/dist/{factory-BHHC6C7W.js.map → factory-FVD7PZ6S.js.map} +0 -0
  113. /package/dist/{factory-KMBWFIX2.js.map → factory-VQS3HJ7V.js.map} +0 -0
  114. /package/dist/{issue-triage-TXQ7J6GG.js.map → issue-triage-HJUJWGAD.js.map} +0 -0
  115. /package/dist/{learning-persistence-FILWP3IR.js.map → learning-persistence-N6ILD2HX.js.map} +0 -0
  116. /package/dist/{mcp-config-OCWIXE2Y.js.map → mobimem-BOJFXQ7B.js.map} +0 -0
  117. /package/dist/{mobimem-77W5ED4Z.js.map → nexus-data-dir-77UO7N6J.js.map} +0 -0
  118. /package/dist/{registry-command-BBLIXULQ.js.map → registry-command-NCWUJKAF.js.map} +0 -0
  119. /package/dist/{nexus-data-dir-M6DYKIHJ.js.map → repo-security-plan-3J45VAD6.js.map} +0 -0
  120. /package/dist/{repo-security-plan-7SNM7JQN.js.map → research-helpers-synthesize-UGQHZZJN.js.map} +0 -0
  121. /package/dist/{research-helpers-synthesize-7CI2FJE5.js.map → routing-memory-NO7QEH7T.js.map} +0 -0
  122. /package/dist/{routing-memory-DCIZEEVC.js.map → session-memory-DOXLEWEU.js.map} +0 -0
  123. /package/dist/{session-memory-5TSAASQW.js.map → setup-command-DVEBFKR2.js.map} +0 -0
  124. /package/dist/{setup-command-PU636ZJH.js.map → setup-config-E3JZYSLR.js.map} +0 -0
  125. /package/dist/{setup-custom-api-IQX3GD2D.js.map → setup-custom-api-DHJ5DRH2.js.map} +0 -0
  126. /package/dist/{setup-config-EA5RDIO2.js.map → weather-report-FNN4OX3N.js.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mcp/tools/consensus-vote.ts","../src/mcp/mcp-notifier.ts","../src/mcp/middleware/rate-limiter.ts","../src/mcp/middleware/validation.ts","../src/mcp/middleware/policy-types.ts","../src/mcp/middleware/policy-helpers.ts","../src/mcp/middleware/policy-rules.ts","../src/mcp/middleware/policy.ts","../src/mcp/middleware/timeout-guard.ts","../src/mcp/middleware/request-context.ts","../src/security/access-constraint-deriver/denylist.ts","../src/security/access-constraint-deriver/tool-risk.ts","../src/security/access-constraint-deriver/enforcer.ts","../src/security/access-constraint-deriver/mcp-guard.ts","../src/security/access-constraint-deriver/chain-adapter.ts","../src/mcp/middleware/middleware-chain.ts","../src/mcp/middleware/tool-wrapper.ts","../src/audit/secure-handler-audit.ts","../src/mcp/middleware/tool-input-sanitizer.ts","../src/mcp/middleware/secure-handler.ts","../src/mcp/tools/tool-result.ts","../src/cli/vote-types.ts","../src/cli-adapters/cli-to-model-adapter.ts","../src/adapters/claude-adapter.ts","../src/adapters/claude-adapter-types.ts","../src/adapters/claude-adapter-helpers.ts","../src/context/token-counter-helpers.ts","../src/adapters/prompt-utils.ts","../src/adapters/sdk/sdk-adapter.ts","../src/adapters/auto-adapter.ts","../src/agents/collaboration/event-bus-helpers.ts","../src/agents/collaboration/circular-buffer.ts","../src/agents/collaboration/event-bus.ts","../src/agents/collaboration/event-bus-topics.ts","../src/adapters/resilient-adapter.ts","../src/adapters/unified-registry.ts","../src/cli/voter-prompts.ts","../src/cli/voter-response.ts","../src/cli/voter-execution.ts","../src/cli/voter-agents-deadline.ts","../src/cli/voter-agents.ts","../src/consensus/types-core.ts","../src/consensus/types-voting-protocol.ts","../src/consensus/types-weighted-voting.ts","../src/consensus/higher-order-types.ts","../src/consensus/higher-order-helpers.ts","../src/consensus/higher-order-voting.ts","../src/consensus/strategies.ts","../src/consensus/result-builder.ts","../src/consensus/helpers.ts","../src/consensus/incremental-quorum.ts","../src/consensus/engine.ts","../src/consensus/quorum-validator.ts","../src/consensus/voting-protocol-helpers.ts","../src/consensus/voting-protocol.ts","../src/agents/collaboration/byzantine-events.ts","../src/consensus/weighted-voting-helpers.ts","../src/consensus/weighted-voting.ts","../src/consensus/correlation-helpers.ts","../src/consensus/correlation-tracker.ts","../src/consensus/correlation-persistence.ts","../src/mcp/tools/consensus-vote-types.ts","../src/mcp/tools/tool-memory.ts","../src/context/belief-core-types.ts","../src/context/belief-update-types.ts","../src/context/belief-hindsight-types.ts","../src/context/belief-memory-interface.ts","../src/utils/id-utils.ts","../src/context/belief-memory-helpers.ts","../src/context/belief-memory-recall.ts","../src/context/belief-memory-audit.ts","../src/context/belief-memory-reflect.ts","../src/context/belief-memory.ts","../src/context/graph-memory-types.ts","../src/context/graph-memory-helpers.ts","../src/context/graph-memory.ts","../src/context/agentic-memory-types.ts","../src/context/agentic-memory-primitives.ts","../src/context/agentic-memory-extraction.ts","../src/context/agentic-memory-linking.ts","../src/context/agentic-memory-db-helpers.ts","../src/context/agentic-memory-operations.ts","../src/context/agentic-memory.ts","../src/context/belief-memory-persistence.ts","../src/context/belief-persistence-types.ts","../src/context/memory-types.ts","../src/context/memory-module-types.ts","../src/context/typed-memory-impl.ts","../src/context/typed-memory.ts","../src/mcp/tools/memory-promotion.ts","../src/mcp/tools/memory-decay.ts","../src/mcp/tools/tool-memory-query.ts","../src/orchestration/outcomes/outcome-store-persistence.ts","../src/orchestration/outcomes/learning-events.ts","../src/mcp/tools/consensus-vote-recording.ts","../src/mcp/tools/simulation-guard.ts"],"sourcesContent":["/* eslint-disable max-lines */ // Consensus voting — cohesive single module (governance: 400-600 OK)\n/**\n * nexus-agents/mcp - Consensus Vote Tool\n * @module mcp/tools/consensus-vote\n */\n\nimport { z } from 'zod';\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport type { ILogger } from '../../core/index.js';\nimport {\n createLogger,\n getErrorMessage,\n getTimeProvider,\n getRandomProvider,\n formatZodError,\n} from '../../core/index.js';\nimport type { IMcpNotifier } from '../mcp-notifier.js';\nimport { createMcpNotifier, NOOP_NOTIFIER, withProgressHeartbeat } from '../mcp-notifier.js';\nimport { wrapToolWithTimeout, toSdkCallback, getToolTimeout } from '../middleware/tool-wrapper.js';\nimport { createSecureHandler, type HandlerContext } from '../middleware/secure-handler.js';\nimport { toolError, toolSuccess, type ToolResult, type BaseMcpToolDeps } from './tool-result.js';\nimport type { ConsensusAlgorithm, Vote, ConsensusResult, Proposal } from '../../consensus/types.js';\nimport type { VoterRole, AgentVoteResult } from '../../cli/vote-types.js';\nimport { collectRealVotes } from '../../cli/voter-agents.js';\nimport { createConsensusEngine } from '../../consensus/engine.js';\nimport type {\n HigherOrderVotingResult,\n ICorrelationTracker,\n} from '../../consensus/higher-order-types.js';\nimport { HigherOrderVotingStrategy } from '../../consensus/index.js';\nimport {\n createPersistentCorrelationTracker,\n createPersistedProposal,\n saveCorrelationData,\n} from '../../consensus/correlation-persistence.js';\nimport {\n MAX_PROPOSAL_LENGTH,\n VotingStrategySchema,\n ConsensusVoteInputSchema,\n buildResponse,\n} from './consensus-vote-types.js';\nimport { recordVoteSuccess, recordVoteError } from './consensus-vote-recording.js';\nimport { warnIfSimulatedOutsideTests } from './simulation-guard.js';\nimport type {\n VotingStrategy,\n ConsensusVoteInput,\n ConsensusVoteResponse,\n ExtendedVotingResult,\n} from './consensus-vote-types.js';\n\nexport type {\n VotingStrategy,\n ConsensusVoteInput,\n ConsensusVoteResponse,\n AgentVoteSummary,\n VoteDecisionStatus,\n HigherOrderMetadata,\n ExtendedVotingResult,\n} from './consensus-vote-types.js';\nexport { VotingStrategySchema, ConsensusVoteInputSchema } from './consensus-vote-types.js';\n\n// --- Correlation Tracker Singleton ---\nlet persistentCorrelationTracker: ICorrelationTracker | undefined;\n\n/** Gets or creates the persistent CorrelationTracker (Issue #517). */\nfunction getOrCreateCorrelationTracker(): ICorrelationTracker {\n persistentCorrelationTracker ??= createPersistentCorrelationTracker();\n return persistentCorrelationTracker;\n}\n\n/** Resets the persistent CorrelationTracker. @internal */\nexport function resetCorrelationTracker(): void {\n persistentCorrelationTracker = undefined;\n}\n\n// --- Dependencies ---\nexport interface ConsensusVoteDeps extends BaseMcpToolDeps {\n /** MCP notifier for client-visible logging (Issue #974) */\n notifier?: IMcpNotifier | undefined;\n}\n\n// --- Strategy Resolution ---\nfunction resolveStrategy(input: ConsensusVoteInput): VotingStrategy {\n if (input.strategy !== undefined) return input.strategy;\n if (input.threshold !== undefined) {\n switch (input.threshold) {\n case 'majority':\n return 'simple_majority';\n case 'supermajority':\n return 'supermajority';\n case 'unanimous':\n return 'unanimous';\n }\n }\n return 'simple_majority';\n}\n\nfunction strategyToAlgorithm(strategy: VotingStrategy): ConsensusAlgorithm {\n if (strategy === 'higher_order') return 'higher_order';\n if (strategy === 'opinion_wise') return 'opinion_wise';\n return strategy;\n}\n\nfunction getVoterRoles(quickMode: boolean): readonly VoterRole[] {\n // Default panel expanded to 7 roles 2026-04-25 — scope_steward added to\n // catch build-vs-buy blind spots (#2185). QuickMode substitutes\n // scope_steward for pm so fast triage covers existence-justification.\n return quickMode\n ? ['architect', 'security', 'scope_steward']\n : ['architect', 'security', 'devex', 'ai_ml', 'pm', 'catfish', 'scope_steward'];\n}\n\n// --- Voting Execution ---\n/** Creates a synthetic ConsensusResult when all votes are errors (Issue #815). */\nfunction createEmptyConsensusResult(\n proposal: string,\n algorithm: ConsensusAlgorithm\n): ConsensusResult {\n const now = new Date().toISOString();\n return {\n proposalId: 'no-valid-votes',\n proposal: { title: 'MCP Consensus Vote', description: proposal, algorithm },\n outcome: 'rejected',\n votes: new Map<string, Vote>(),\n voteCounts: { approve: 0, reject: 0, abstain: 0, total: 0 },\n approvalPercentage: 0,\n quorumReached: false,\n startedAt: now,\n closedAt: now,\n durationMs: 0,\n };\n}\n\n/** Thresholds per algorithm for cascade detection. */\nconst CASCADE_THRESHOLDS: Record<string, number> = {\n majority: 0.5,\n supermajority: 0.67,\n unanimous: 1.0,\n};\n\n/** Detect if vote outcome is mathematically decided (#1765). */\nfunction detectEarlyCascade(\n algorithm: string,\n approvals: number,\n rejections: number,\n total: number\n): { decided: boolean; reason: string } {\n const threshold = CASCADE_THRESHOLDS[algorithm] ?? 0.5;\n if (total === 0) return { decided: false, reason: '' };\n\n // Unanimous: any rejection decides\n if (algorithm === 'unanimous' && rejections > 0) {\n return { decided: true, reason: `Unanimous rejected: ${String(rejections)} rejection(s)` };\n }\n // Approval locked: even if all remaining vote reject, approval holds\n if (approvals / total > threshold) {\n return {\n decided: true,\n reason: `Approval locked: ${String(approvals)}/${String(total)} > ${String(threshold)}`,\n };\n }\n // Rejection locked: even if all remaining vote approve, rejection holds\n const remaining = total - approvals - rejections;\n if ((approvals + remaining) / total < threshold) {\n return {\n decided: true,\n reason: `Rejection locked: max possible ${String(approvals + remaining)}/${String(total)} < ${String(threshold)}`,\n };\n }\n return { decided: false, reason: '' };\n}\n\nasync function processVotesThroughEngine(\n votes: readonly AgentVoteResult[],\n proposal: string,\n algorithm: ConsensusAlgorithm\n): Promise<ConsensusResult> {\n const validVotes = votes.filter((v) => v.source !== 'error');\n if (validVotes.length === 0) return createEmptyConsensusResult(proposal, algorithm);\n\n const engine = createConsensusEngine();\n const engineProposal: Proposal = {\n title: 'MCP Consensus Vote',\n description: proposal,\n algorithm,\n };\n const proposalResult = await engine.propose(engineProposal);\n if (!proposalResult.ok)\n throw new Error(`Failed to create proposal: ${proposalResult.error.message}`, {\n cause: proposalResult.error,\n });\n\n const proposalId = proposalResult.value;\n for (const { role, vote } of validVotes) await engine.vote(proposalId, role, vote);\n\n const resultRes = await engine.close(proposalId);\n if (!resultRes.ok)\n throw new Error(`Failed to close proposal: ${resultRes.error.message}`, {\n cause: resultRes.error,\n });\n return resultRes.value;\n}\n\nfunction runHigherOrderVoting(\n strategy: VotingStrategy,\n voteMap: Map<string, Vote>,\n logger: ILogger\n): HigherOrderVotingResult | undefined {\n if (strategy !== 'higher_order') return undefined;\n const hovStrategy = new HigherOrderVotingStrategy();\n const tracker = getOrCreateCorrelationTracker();\n const result = hovStrategy.aggregate(voteMap, tracker);\n logger.info('Higher-Order Voting complete', {\n method: result.method,\n decision: result.decision,\n posteriorApproval: result.posteriorApproval.toFixed(3),\n });\n return result;\n}\n\nfunction recordVotesToTracker(\n votes: readonly AgentVoteResult[],\n voteMap: Map<string, Vote>,\n outcome: 'approved' | 'rejected',\n logger: ILogger\n): void {\n const allVotesReal = votes.every((v) => v.source === 'llm');\n if (!allVotesReal) {\n logger.warn('Skipping correlation recording due to non-LLM votes', {\n count: votes.filter((v) => v.source !== 'llm').length,\n });\n return;\n }\n const tracker = getOrCreateCorrelationTracker();\n const id = `consensus-${String(getTimeProvider().now())}-${getRandomProvider().random().toString(36).slice(2, 9)}`;\n tracker.recordProposalVotes(id, voteMap, outcome);\n logger.debug('Recorded votes to tracker', { proposalId: id, outcome });\n\n try {\n const persisted = createPersistedProposal(id, voteMap, outcome);\n const saveResult = saveCorrelationData([persisted]);\n if (!saveResult.ok) {\n logger.warn('Failed to persist correlation data', { error: saveResult.error.message });\n }\n } catch (error: unknown) {\n const message = getErrorMessage(error);\n logger.warn('Error persisting correlation data', { error: message });\n }\n}\n\n/** Process votes with cascade detection — extracted for max-lines-per-function (#1765). */\nasync function processVotesWithCascade(\n votes: readonly AgentVoteResult[],\n opts: {\n totalRoles: number;\n proposal: string;\n algorithm: ConsensusAlgorithm;\n strategy: VotingStrategy;\n log: ILogger;\n }\n): Promise<{\n engineResult: ConsensusResult;\n voteMap: Map<string, Vote>;\n higherOrderResult: ReturnType<typeof runHigherOrderVoting>;\n outcome: 'approved' | 'rejected';\n cascaded: boolean;\n}> {\n const validVotes = votes.filter((v) => v.source !== 'error');\n const approvals = validVotes.filter((v) => v.vote.decision === 'approve').length;\n const rejections = validVotes.filter((v) => v.vote.decision === 'reject').length;\n const cascadeInfo = detectEarlyCascade(opts.algorithm, approvals, rejections, opts.totalRoles);\n\n if (cascadeInfo.decided) {\n opts.log.info('Vote cascade: outcome decided early', {\n approvals,\n rejections,\n total: opts.totalRoles,\n reason: cascadeInfo.reason,\n });\n }\n\n const engineResult = await processVotesThroughEngine(votes, opts.proposal, opts.algorithm);\n const voteMap = new Map<string, Vote>();\n for (const { role, vote, source } of votes) {\n if (source !== 'error') voteMap.set(role, vote);\n }\n\n const higherOrderResult = cascadeInfo.decided\n ? undefined\n : runHigherOrderVoting(opts.strategy, voteMap, opts.log);\n const outcome: 'approved' | 'rejected' =\n engineResult.outcome === 'approved' ? 'approved' : 'rejected';\n\n return { engineResult, voteMap, higherOrderResult, outcome, cascaded: cascadeInfo.decided };\n}\n\n/** Execute a consensus vote with full strategy support. Exported for pipeline DRY (#1694). */\n/** Confidence threshold above which a contrarian rejection triggers escalation (#1799). */\nconst CONTRARIAN_ESCALATION_THRESHOLD = 0.8;\n\n/** Run a single contrarian agent to check for YAGNI/MISALIGNED/SECURITY_RISK (#1799). */\nasync function runContrarianCheck(\n proposal: string,\n log: ILogger\n): Promise<{ shouldEscalate: boolean; reason: string; confidence: number }> {\n try {\n const { executeExpert } = await import('../../pipeline/expert-bridge.js');\n const prompt = [\n 'You are a contrarian analyst. Your job is to find reasons this proposal should be REJECTED.',\n 'Look for: YAGNI (not needed), MISALIGNED (wrong tech/architecture), SECURITY_RISK, SCOPE_CREEP.',\n '',\n `Proposal: ${proposal.slice(0, 2000)}`,\n '',\n 'If you find a strong reason to reject, respond with JSON:',\n '{\"decision\":\"reject\",\"confidence\":0.0-1.0,\"reasoning\":\"your concern\"}',\n 'If the proposal is sound, respond with:',\n '{\"decision\":\"approve\",\"confidence\":0.0-1.0,\"reasoning\":\"why it is acceptable\"}',\n ].join('\\n');\n\n const result = await executeExpert('architecture', prompt);\n if (!result.success) return { shouldEscalate: false, reason: '', confidence: 0 };\n\n const jsonMatch = result.text.match(/\\{[\\s\\S]*\\}/);\n if (jsonMatch === null) return { shouldEscalate: false, reason: '', confidence: 0 };\n\n const parsed = JSON.parse(jsonMatch[0]) as {\n decision?: string;\n confidence?: number;\n reasoning?: string;\n };\n\n const isRejection = parsed.decision === 'reject';\n const confidence = typeof parsed.confidence === 'number' ? parsed.confidence : 0;\n const reasoning = typeof parsed.reasoning === 'string' ? parsed.reasoning : '';\n\n if (isRejection && confidence >= CONTRARIAN_ESCALATION_THRESHOLD) {\n log.info('Contrarian rejected with high confidence', {\n confidence,\n reasoning: reasoning.slice(0, 200),\n });\n return { shouldEscalate: true, reason: reasoning, confidence };\n }\n\n return { shouldEscalate: false, reason: '', confidence };\n } catch {\n return { shouldEscalate: false, reason: '', confidence: 0 };\n }\n}\n\nexport async function executeVoting(\n input: ConsensusVoteInput,\n logger: ILogger\n): Promise<ExtendedVotingResult> {\n const strategy = resolveStrategy(input);\n const algorithm = strategyToAlgorithm(strategy);\n const roles = getVoterRoles(input.quickMode);\n const startTime = getTimeProvider().now();\n\n logger.info('Starting consensus vote', { strategy, algorithm, roleCount: roles.length });\n\n const votes = await collectRealVotes({\n roles,\n proposal: input.proposal,\n simulate: input.simulateVotes,\n });\n\n // Check for early cascade and process votes (#1765)\n const { engineResult, voteMap, higherOrderResult, outcome, cascaded } =\n await processVotesWithCascade(votes, {\n totalRoles: roles.length,\n proposal: input.proposal,\n algorithm,\n strategy,\n log: logger,\n });\n\n recordVotesToTracker(votes, voteMap, outcome, logger);\n\n // Contrarian check for quickMode approvals (#1799):\n // When quickMode approves, run a single contrarian agent to catch YAGNI/SECURITY_RISK.\n // If contrarian rejects with high confidence, escalate to full vote.\n if (input.quickMode && outcome === 'approved' && !input.simulateVotes) {\n const escalation = await runContrarianCheck(input.proposal, logger);\n if (escalation.shouldEscalate) {\n logger.warn('Contrarian escalation: re-running with full vote', {\n reason: escalation.reason,\n confidence: escalation.confidence,\n });\n return executeVoting({ ...input, quickMode: false }, logger);\n }\n }\n\n const totalTimeMs = getTimeProvider().now() - startTime;\n logger.info('Consensus vote completed', { strategy, outcome, durationMs: totalTimeMs, cascaded });\n\n const result: ExtendedVotingResult = {\n proposal: input.proposal,\n threshold: algorithm,\n result: engineResult,\n votes,\n totalTimeMs,\n simulateVotes: input.simulateVotes,\n strategy,\n };\n if (higherOrderResult !== undefined) result.higherOrderResult = higherOrderResult;\n return result;\n}\n\n// --- Handler & Registration ---\nasync function handleConsensusVote(\n deps: ConsensusVoteDeps,\n args: ConsensusVoteInput\n): Promise<{ ok: true; value: ConsensusVoteResponse } | { ok: false; error: string }> {\n const logger = deps.logger ?? createLogger({ tool: 'consensus_vote' });\n if (args.simulateVotes) warnIfSimulatedOutsideTests('consensus_vote', logger);\n try {\n const result = await executeVoting(args, logger);\n const strategy = args.strategy ?? 'simple_majority';\n\n // Detect all-error votes: return structured error instead of fake \"rejected\" (#1552)\n const errorVotes = result.votes.filter((v) => v.source === 'error');\n if (errorVotes.length === result.votes.length && result.votes.length > 0) {\n const failures = errorVotes.map((v) => `${v.role}: ${v.error ?? 'unknown error'}`).join('; ');\n logger.warn('All voters failed', { failureCount: errorVotes.length, failures });\n recordVoteError(args.proposal, `All ${String(errorVotes.length)} voters failed: ${failures}`);\n return {\n ok: false,\n error: `All ${String(errorVotes.length)} voters failed. Failures: ${failures}`,\n };\n }\n\n recordVoteSuccess(\n args.proposal,\n strategy,\n result.result.outcome,\n result.totalTimeMs,\n result.votes\n );\n return { ok: true, value: buildResponse(args, result) };\n } catch (error) {\n const message = getErrorMessage(error);\n const cause = error instanceof Error ? error : new Error(message);\n logger.error('Consensus vote failed', cause);\n recordVoteError(args.proposal, message);\n return { ok: false, error: `Voting failed: ${message}` };\n }\n}\n\ntype ConsensusVoteToolResponse = ToolResult;\n\nfunction createConsensusVoteHandler(deps: ConsensusVoteDeps) {\n const notifier = deps.notifier ?? NOOP_NOTIFIER;\n return async (args: unknown, ctx: HandlerContext): Promise<ConsensusVoteToolResponse> => {\n const validationResult = ConsensusVoteInputSchema.safeParse(args);\n if (!validationResult.success) {\n return toolError(`Validation error: ${formatZodError(validationResult.error)}`);\n }\n\n const strategy = validationResult.data.strategy ?? 'simple_majority';\n ctx.logger.debug('Starting consensus vote', {\n strategy,\n quickMode: validationResult.data.quickMode,\n });\n notifier.info('consensus_vote', {\n event: 'vote_start',\n proposalLength: validationResult.data.proposal.length,\n strategy,\n });\n\n const result = await withProgressHeartbeat('consensus_vote', notifier, () =>\n handleConsensusVote(deps, validationResult.data)\n );\n if (!result.ok) {\n return toolError(result.error);\n }\n\n for (const vote of result.value.votes) {\n notifier.debug('consensus_vote', {\n event: 'vote_collected',\n role: vote.role,\n decision: vote.decision,\n });\n }\n notifier.info('consensus_vote', {\n event: 'vote_complete',\n decision: result.value.decision,\n approvalPercentage: result.value.approvalPercentage,\n voteCount: result.value.votes.length,\n });\n const data = result.value as unknown as Record<string, unknown>;\n return {\n ...toolSuccess(JSON.stringify(result.value, null, 2)),\n structuredContent: data,\n };\n };\n}\n\n/** Output schema for consensus_vote tool (Issue #1117, #1246). */\nexport const CONSENSUS_VOTE_OUTPUT_SCHEMA = {\n proposal: z.string(),\n strategy: VotingStrategySchema,\n decision: z.enum(['approved', 'rejected', 'no_quorum']),\n approvalPercentage: z.number(),\n voteCounts: z.object({\n approve: z.number(),\n reject: z.number(),\n abstain: z.number(),\n error: z.number(),\n }),\n votes: z.array(\n z.object({\n role: z.string().max(100),\n decision: z.enum(['approve', 'reject', 'abstain']),\n confidence: z.number(),\n reasoning: z.string().max(4000),\n simulated: z.boolean(),\n error: z.boolean(),\n modelUsed: z.string().max(100).optional(),\n rejectionCategories: z\n .array(\n z.enum([\n 'YAGNI',\n 'DRY_VIOLATION',\n 'OVER_ENGINEERING',\n 'SCOPE_CREEP',\n 'SECURITY_RISK',\n 'MISALIGNED',\n 'INSUFFICIENT_EVIDENCE',\n ])\n )\n .optional(),\n })\n ),\n threshold: z.enum(['majority', 'supermajority', 'unanimous']).optional(),\n durationMs: z.number(),\n simulateVotes: z.boolean(),\n higherOrderMetadata: z\n .object({\n posteriorApproval: z.number(),\n posteriorRejection: z.number(),\n effectiveVoteCount: z.number(),\n method: z.enum(['ow', 'isp', 'simple']),\n usedCorrelationData: z.boolean(),\n improvementOverBaseline: z.number(),\n downweightedAgents: z.array(z.string().max(100)).max(10),\n reasoning: z.string().max(2000),\n })\n .optional(),\n};\n\n/**\n * Registers the consensus_vote tool with the MCP server.\n * Uses createSecureHandler (Issue #531) with timeout protection (Issue #271).\n * @category MCP\n */\nexport function registerConsensusVoteTool(server: McpServer, deps: ConsensusVoteDeps): void {\n const logger = deps.logger ?? createLogger({ tool: 'consensus_vote' });\n const notifier = deps.notifier ?? createMcpNotifier(server);\n const depsWithNotifier = { ...deps, notifier };\n const toolSchema = {\n proposal: z.string().min(1).max(MAX_PROPOSAL_LENGTH).describe('Proposal text to vote on'),\n threshold: z\n .enum(['majority', 'supermajority', 'unanimous'])\n .optional()\n .describe('Voting threshold (legacy). Use strategy instead.'),\n strategy: VotingStrategySchema.optional().describe(\n 'Voting strategy: simple_majority (default), supermajority, unanimous, proof_of_learning, or higher_order'\n ),\n quickMode: z.boolean().optional().default(false).describe('Use 3 agents instead of 7'),\n simulateVotes: z\n .boolean()\n .optional()\n .default(false)\n .describe('TESTS ONLY — random output, must not be used for real decisions (#2319)'),\n };\n\n const description =\n 'Execute multi-model consensus voting on a proposal. ' +\n 'Uses 7 specialized agent roles (architect, security, devex, ai_ml, pm, catfish, scope_steward) ' +\n 'to vote on proposals with configurable strategies. ' +\n 'Supports higher_order strategy for Bayesian-optimal aggregation with correlation awareness (Issue #514).';\n\n const secureHandler = createSecureHandler(createConsensusVoteHandler(depsWithNotifier), {\n toolName: 'consensus_vote',\n rateLimiter: deps.rateLimiter,\n logger,\n });\n\n const timeoutMs = getToolTimeout('consensus_vote', deps.security);\n const wrappedHandler = wrapToolWithTimeout('consensus_vote', secureHandler, {\n timeoutMs,\n logger,\n });\n\n server.registerTool(\n 'consensus_vote',\n { description, inputSchema: toolSchema, outputSchema: CONSENSUS_VOTE_OUTPUT_SCHEMA },\n toSdkCallback(wrappedHandler)\n );\n logger.info('Registered consensus_vote tool with secure handler and timeout protection');\n}\n","/**\n * nexus-agents/mcp - MCP Notification Helper\n *\n * Sends structured logging notifications to MCP clients via\n * the `notifications/message` protocol method.\n * Clients (e.g., Claude Code) can display these for real-time\n * observability of orchestration events.\n *\n * Also provides progress notification support via AsyncLocalStorage\n * for resetting client-side request timeouts (MCP SDK resetTimeoutOnProgress).\n *\n * @module mcp/mcp-notifier\n * (Source: Issue #973, #974 — Claude Code Observability)\n * (Source: Issue #1108 — Progress heartbeat timeout reset)\n * (Source: MCP Protocol 2025-11-25, Logging Specification)\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport { createLogger, getErrorMessage } from '../core/index.js';\n\n/**\n * Logging levels for MCP notifications (RFC 5424 syslog).\n */\nexport type McpLogLevel = 'debug' | 'info' | 'notice' | 'warning' | 'error';\n\n/**\n * MCP notifier for sending structured log events to clients.\n */\nexport interface IMcpNotifier {\n /** Send info-level notification (key orchestration events) */\n info(logger: string, data: Record<string, unknown>): void;\n /** Send debug-level notification (detailed execution steps) */\n debug(logger: string, data: Record<string, unknown>): void;\n /** Send warning-level notification */\n warn(logger: string, data: Record<string, unknown>): void;\n}\n\nconst internalLogger = createLogger({ component: 'mcp-notifier' });\n\n/**\n * Creates an MCP notifier that sends logging notifications to connected clients.\n *\n * Notifications are fire-and-forget — failures are logged but never\n * propagate to callers. This ensures observability never breaks tool execution.\n */\nexport function createMcpNotifier(server: McpServer): IMcpNotifier {\n function send(level: McpLogLevel, logger: string, data: Record<string, unknown>): void {\n try {\n server.sendLoggingMessage({ level, logger, data }).catch((error: unknown) => {\n internalLogger.debug('Failed to send MCP notification', {\n level,\n logger,\n error: getErrorMessage(error),\n });\n });\n } catch (error: unknown) {\n internalLogger.debug('Failed to send MCP notification', {\n level,\n logger,\n error: getErrorMessage(error),\n });\n }\n }\n\n return {\n info: (logger, data) => {\n send('info', logger, data);\n },\n debug: (logger, data) => {\n send('debug', logger, data);\n },\n warn: (logger, data) => {\n send('warning', logger, data);\n },\n };\n}\n\n/**\n * No-op notifier for when MCP server is not available.\n */\nexport const NOOP_NOTIFIER: IMcpNotifier = {\n info: () => undefined,\n debug: () => undefined,\n warn: () => undefined,\n};\n\n// ============================================================================\n// Progress Notification Support (MCP SDK resetTimeoutOnProgress)\n// ============================================================================\n\n/**\n * Callback to send a progress notification to the MCP client.\n * When the client sets resetTimeoutOnProgress=true, each notification\n * resets the client's 60s request timeout.\n */\nexport type ProgressSender = (progress: number, total?: number) => void;\n\n/**\n * Progress context stored via AsyncLocalStorage.\n * Set by toSdkCallbackWithProgress when a progressToken is available.\n */\nexport interface ProgressContext {\n readonly progressToken: string | number;\n readonly sendNotification: ProgressSender;\n}\n\n/**\n * AsyncLocalStorage for MCP progress context.\n * Allows withProgressHeartbeat to access the progress sender without\n * threading it through the entire middleware chain.\n */\nexport const progressContextStorage = new AsyncLocalStorage<ProgressContext>();\n\n// ============================================================================\n// Abort Signal Support (MCP SDK cancellation)\n// ============================================================================\n\n/**\n * AsyncLocalStorage for MCP abort signal.\n * Set by toSdkCallback when the SDK provides an AbortSignal.\n * Allows middleware (e.g., TimeoutGuard) to race client cancellation\n * alongside server-side timeouts.\n */\nexport const abortSignalStorage = new AsyncLocalStorage<AbortSignal>();\n\n/**\n * Wraps an async operation with periodic heartbeat notifications.\n *\n * When a progressToken is available (via AsyncLocalStorage from the MCP\n * request handler), sends real `notifications/progress` that reset the\n * client's request timeout (MCP SDK resetTimeoutOnProgress feature).\n *\n * Always sends logging notifications for observability regardless.\n *\n * @param toolName - Name of the tool for notification context\n * @param notifier - MCP notifier instance\n * @param operation - The async operation to wrap\n * @param intervalMs - Heartbeat interval (default: 15000ms)\n * @returns The operation result\n */\nexport async function withProgressHeartbeat<T>(\n toolName: string,\n notifier: IMcpNotifier,\n operation: () => Promise<T>,\n intervalMs = 15_000\n): Promise<T> {\n const startTime = Date.now();\n let beatCount = 0;\n const progressCtx = progressContextStorage.getStore();\n\n const timer = setInterval(() => {\n beatCount++;\n const elapsed = Math.round((Date.now() - startTime) / 1000);\n\n // Send real progress notification if client provided progressToken\n if (progressCtx !== undefined) {\n progressCtx.sendNotification(beatCount);\n }\n\n // Always send logging notification for observability\n notifier.debug(toolName, {\n event: 'heartbeat',\n elapsedSeconds: elapsed,\n beatCount,\n hasProgressToken: progressCtx !== undefined,\n });\n }, intervalMs);\n\n try {\n return await operation();\n } finally {\n clearInterval(timer);\n }\n}\n","/**\n * nexus-agents/mcp - Rate Limiter Middleware\n *\n * Token bucket implementation for rate limiting MCP tool calls.\n * Prevents abuse and ensures fair resource usage.\n *\n * (Source: Token Bucket Algorithm, RFC 6585)\n */\n\nimport { createLogger, type ILogger, getTimeProvider } from '../../core/index.js';\n\n/**\n * Configuration for the token bucket rate limiter.\n */\nexport interface RateLimiterConfig {\n /** Maximum number of tokens in the bucket */\n readonly capacity: number;\n /** Number of tokens added per interval */\n readonly refillRate: number;\n /** Interval in milliseconds between token refills (default: 1000ms) */\n readonly refillIntervalMs?: number;\n /** Optional logger instance */\n readonly logger?: ILogger;\n /** Optional identifier for logging */\n readonly name?: string;\n}\n\n/**\n * Current state of the rate limiter.\n */\nexport interface RateLimiterState {\n /** Current number of available tokens */\n readonly tokens: number;\n /** Capacity of the bucket */\n readonly capacity: number;\n /** Time until next token is available (0 if tokens available) */\n readonly nextTokenMs: number;\n}\n\n// Canonical source: config/timeouts.ts (Issue #1046)\nimport { CACHE_TIMEOUTS } from '../../config/timeouts.js';\n\nconst DEFAULT_REFILL_INTERVAL_MS = CACHE_TIMEOUTS.rateLimitRefillMs;\n\n/**\n * Token bucket rate limiter implementation.\n *\n * The token bucket algorithm allows for bursting up to the capacity,\n * while maintaining a steady-state rate equal to the refill rate.\n *\n * @example\n * ```typescript\n * const limiter = new RateLimiter({\n * capacity: 100,\n * refillRate: 10,\n * refillIntervalMs: 1000,\n * });\n *\n * if (limiter.tryAcquire()) {\n * // Proceed with operation\n * } else {\n * // Rate limited, reject or queue\n * }\n * ```\n */\nexport class RateLimiter {\n private tokens: number;\n private readonly capacity: number;\n private readonly refillRate: number;\n private readonly refillIntervalMs: number;\n private lastRefillTime: number;\n private readonly logger: ILogger;\n private readonly name: string;\n\n constructor(config: RateLimiterConfig) {\n this.capacity = config.capacity;\n this.refillRate = config.refillRate;\n this.refillIntervalMs = config.refillIntervalMs ?? DEFAULT_REFILL_INTERVAL_MS;\n this.tokens = this.capacity;\n this.lastRefillTime = getTimeProvider().now();\n this.name = config.name ?? 'rate-limiter';\n this.logger = config.logger ?? createLogger({ component: this.name });\n\n this.logger.debug('Rate limiter initialized', {\n capacity: this.capacity,\n refillRate: this.refillRate,\n refillIntervalMs: this.refillIntervalMs,\n });\n }\n\n /**\n * Refills tokens based on elapsed time.\n * Called automatically before each acquire attempt.\n */\n private refill(): void {\n const now = getTimeProvider().now();\n const elapsed = now - this.lastRefillTime;\n const intervals = Math.floor(elapsed / this.refillIntervalMs);\n\n if (intervals > 0) {\n const tokensToAdd = intervals * this.refillRate;\n this.tokens = Math.min(this.capacity, this.tokens + tokensToAdd);\n this.lastRefillTime = now - (elapsed % this.refillIntervalMs);\n\n if (tokensToAdd > 0) {\n this.logger.debug('Tokens refilled', {\n added: tokensToAdd,\n current: this.tokens,\n });\n }\n }\n }\n\n /**\n * Attempts to acquire a token.\n *\n * @param count - Number of tokens to acquire (default: 1)\n * @returns True if tokens were acquired, false if rate limited\n */\n tryAcquire(count = 1): boolean {\n this.refill();\n\n if (this.tokens >= count) {\n this.tokens -= count;\n this.logger.debug('Token acquired', {\n requested: count,\n remaining: this.tokens,\n });\n return true;\n }\n\n this.logger.warn('Rate limit exceeded', {\n requested: count,\n available: this.tokens,\n });\n return false;\n }\n\n /**\n * Gets the current state of the rate limiter.\n *\n * @returns The current rate limiter state\n */\n getState(): RateLimiterState {\n this.refill();\n\n const nextTokenMs =\n this.tokens > 0 ? 0 : this.refillIntervalMs - (getTimeProvider().now() - this.lastRefillTime);\n\n return {\n tokens: this.tokens,\n capacity: this.capacity,\n nextTokenMs: Math.max(0, nextTokenMs),\n };\n }\n\n /**\n * Resets the rate limiter to full capacity.\n * Useful for testing or after configuration changes.\n */\n reset(): void {\n this.tokens = this.capacity;\n this.lastRefillTime = getTimeProvider().now();\n this.logger.debug('Rate limiter reset', { tokens: this.tokens });\n }\n}\n\n/**\n * Creates a rate limiter with default settings suitable for MCP tools.\n *\n * Default configuration:\n * - Capacity: 100 tokens\n * - Refill rate: 10 tokens per second\n *\n * @param name - Optional name for the rate limiter\n * @param logger - Optional logger instance\n * @returns A configured RateLimiter instance\n */\nexport function createDefaultRateLimiter(name?: string, logger?: ILogger): RateLimiter {\n const config: RateLimiterConfig = {\n capacity: 100,\n refillRate: 10,\n refillIntervalMs: 1000,\n };\n if (name !== undefined) {\n (config as { name?: string }).name = name;\n }\n if (logger !== undefined) {\n (config as { logger?: ILogger }).logger = logger;\n }\n return new RateLimiter(config);\n}\n","/**\n * nexus-agents/mcp - Validation Middleware\n *\n * Input validation helper using Zod schemas.\n * All tool inputs must be validated at the boundary.\n *\n * (Source: MCP Protocol 2025-11-25, Zod Documentation)\n */\n\nimport type { ZodType } from 'zod';\n\nimport { type Result, ok, err, ValidationError, formatZodError } from '../../core/index.js';\n\n// Re-export isZodError for backward compatibility\nexport { isZodError } from '../../core/index.js';\n\n/**\n * Validates tool input against a Zod schema.\n *\n * This function should be called at the start of every tool handler\n * to validate incoming arguments before processing.\n *\n * @template T - The expected type after validation\n * @param schema - The Zod schema to validate against\n * @param args - The unknown input to validate\n * @returns Result containing validated data or a ValidationError\n *\n * @example\n * ```typescript\n * const InputSchema = z.object({\n * task: z.string().min(1),\n * context: z.record(z.string(), z.unknown()).optional(),\n * });\n *\n * server.tool('my_tool', InputSchema.shape, async (args) => {\n * const result = validateToolInput(InputSchema, args);\n * if (!result.ok) {\n * return { isError: true, content: [{ type: 'text', text: result.error.message }] };\n * }\n * const { task, context } = result.value;\n * // Process validated input...\n * });\n * ```\n */\nexport function validateToolInput<T>(\n schema: ZodType<T>,\n args: unknown\n): Result<T, ValidationError> {\n const parsed = schema.safeParse(args);\n\n if (parsed.success) {\n return ok(parsed.data);\n }\n\n const message = formatZodError(parsed.error);\n const validationError = new ValidationError(`Invalid tool input: ${message}`, {\n context: {\n issues: parsed.error.issues,\n receivedType: typeof args,\n },\n });\n\n return err(validationError);\n}\n\n/**\n * Creates a validation function bound to a specific schema.\n *\n * Useful for reusing the same schema across multiple tools.\n *\n * @template T - The expected type after validation\n * @param schema - The Zod schema to bind\n * @returns A validation function for the schema\n *\n * @example\n * ```typescript\n * const validateTask = createValidator(TaskSchema);\n *\n * // Later in tool handlers:\n * const result = validateTask(args);\n * ```\n */\nexport function createValidator<T>(\n schema: ZodType<T>\n): (args: unknown) => Result<T, ValidationError> {\n return (args: unknown) => validateToolInput(schema, args);\n}\n\n/**\n * Validates tool output against a Zod schema.\n *\n * This function should be called before returning results from a tool handler\n * to ensure outputs conform to the expected schema.\n *\n * Issue #547: MCP tools were missing output validation, allowing malformed responses.\n *\n * @template T - The expected output type after validation\n * @param schema - The Zod schema to validate against\n * @param output - The output to validate\n * @returns Result containing validated data or a ValidationError\n *\n * @example\n * ```typescript\n * const OutputSchema = z.object({\n * success: z.boolean(),\n * data: z.record(z.string(), z.unknown()),\n * });\n *\n * server.tool('my_tool', InputSchema.shape, async (args) => {\n * // ... process input ...\n * const result = { success: true, data: processedData };\n *\n * const validated = validateToolOutput(OutputSchema, result);\n * if (!validated.ok) {\n * return { isError: true, content: [{ type: 'text', text: validated.error.message }] };\n * }\n * return { content: [{ type: 'text', text: JSON.stringify(validated.value) }] };\n * });\n * ```\n */\nexport function validateToolOutput<T>(\n schema: ZodType<T>,\n output: unknown\n): Result<T, ValidationError> {\n const parsed = schema.safeParse(output);\n\n if (parsed.success) {\n return ok(parsed.data);\n }\n\n const message = formatZodError(parsed.error);\n const validationError = new ValidationError(`Invalid tool output: ${message}`, {\n context: {\n issues: parsed.error.issues,\n outputType: typeof output,\n },\n });\n\n return err(validationError);\n}\n\n/**\n * Creates an output validation function bound to a specific schema.\n *\n * Useful for reusing the same schema across multiple tools.\n *\n * @template T - The expected type after validation\n * @param schema - The Zod schema to bind\n * @returns A validation function for the schema\n *\n * @example\n * ```typescript\n * const validateOutput = createOutputValidator(OutputSchema);\n *\n * // Later in tool handlers:\n * const result = validateOutput(output);\n * ```\n */\nexport function createOutputValidator<T>(\n schema: ZodType<T>\n): (output: unknown) => Result<T, ValidationError> {\n return (output: unknown) => validateToolOutput(schema, output);\n}\n","/**\n * nexus-agents/mcp - Policy Firewall Types\n *\n * Type definitions for the authorization layer of MCP tool calls.\n *\n * (Source: OWASP ASVS 4.0, Authorization Controls)\n */\n\nimport { z } from 'zod';\n\nimport { SecurityError, type ILogger } from '../../core/index.js';\n\n// =============================================================================\n// Core Types\n// =============================================================================\n\n/**\n * Artifact type for policy context.\n * Artifacts are resources that can be referenced in policy decisions.\n */\nexport interface Artifact<T = unknown> {\n readonly id: string;\n readonly type: string;\n readonly value: T;\n readonly createdAt: Date;\n}\n\n/**\n * Execution mode for tool operations.\n * - 'read-only': Only read operations allowed (default)\n * - 'read-write': Both read and write operations allowed\n */\nexport type ExecutionMode = 'read-only' | 'read-write';\n\n/**\n * Policy enforcement mode.\n * - 'enforce': Block denied operations\n * - 'warn': Log denials but allow execution (for migration)\n */\nexport type PolicyMode = 'enforce' | 'warn';\n\n/**\n * Result of a policy evaluation.\n */\nexport interface PolicyDecision {\n readonly allowed: boolean;\n readonly reason: string;\n readonly requiredArtifact?: string;\n readonly ruleName?: string;\n}\n\n/**\n * Context provided to policy rules for evaluation.\n */\nexport interface PolicyContext {\n readonly toolName: string;\n readonly args: unknown;\n readonly mode: ExecutionMode;\n readonly artifacts?: Map<string, Artifact>;\n readonly workflowId?: string;\n readonly allowedPaths?: readonly string[];\n}\n\n/**\n * A single policy rule that can approve or deny operations.\n */\nexport interface PolicyRule {\n readonly name: string;\n readonly description: string;\n check(ctx: PolicyContext): PolicyDecision;\n}\n\n/**\n * Interface for the policy firewall.\n */\nexport interface IPolicyFirewall {\n evaluate(ctx: PolicyContext): PolicyDecision;\n addRule(rule: PolicyRule): void;\n removeRule(name: string): boolean;\n getRules(): readonly PolicyRule[];\n setMode(mode: PolicyMode): void;\n getMode(): PolicyMode;\n}\n\n/**\n * Configuration for the policy firewall.\n */\nexport interface PolicyFirewallConfig {\n /** Enforcement mode (default: 'enforce') */\n readonly mode?: PolicyMode;\n /** Logger instance */\n readonly logger?: ILogger;\n /** Initial rules to register */\n readonly rules?: readonly PolicyRule[];\n}\n\n// =============================================================================\n// Error Types\n// =============================================================================\n\n/**\n * Policy error for authorization failures.\n */\nexport class PolicyError extends SecurityError {\n readonly decision: PolicyDecision;\n\n constructor(message: string, decision: PolicyDecision) {\n super(message, {\n context: {\n allowed: decision.allowed,\n reason: decision.reason,\n ruleName: decision.ruleName,\n requiredArtifact: decision.requiredArtifact,\n },\n });\n this.name = 'PolicyError';\n this.decision = decision;\n }\n}\n\n// =============================================================================\n// Zod Schemas for Configuration\n// =============================================================================\n\n/**\n * Schema for policy configuration.\n */\nexport const PolicyConfigSchema = z.object({\n defaultMode: z.enum(['read-only', 'read-write']).default('read-only'),\n policyMode: z.enum(['enforce', 'warn']).default('enforce'),\n allowedPaths: z.array(z.string()).default(['./']),\n});\n\nexport type PolicyConfig = z.infer<typeof PolicyConfigSchema>;\n","/**\n * nexus-agents/mcp - Policy Firewall Helpers\n *\n * Utility functions for path validation and argument extraction.\n *\n * (Source: OWASP ASVS 4.0, Authorization Controls)\n */\n\n// =============================================================================\n// Path Utility Functions\n// =============================================================================\n\n/**\n * Validates a path against allowed roots.\n *\n * @param targetPath - The path to validate\n * @param allowedPaths - Array of allowed root paths\n * @returns True if the path is within an allowed root\n */\nexport function isPathSafe(targetPath: string, allowedPaths: readonly string[]): boolean {\n // Normalize the target path\n const normalizedTarget = normalizePath(targetPath);\n\n // Check if any allowed path is a prefix of the target\n for (const allowed of allowedPaths) {\n const normalizedAllowed = normalizePath(allowed);\n if (normalizedTarget.startsWith(normalizedAllowed)) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Normalizes a path by removing trailing slashes and handling relative paths.\n */\nexport function normalizePath(p: string): string {\n // Remove trailing slashes\n let normalized = p.replace(/\\/{1,100}$/, '');\n\n // Handle relative paths\n if (normalized === '.') {\n normalized = '';\n } else if (normalized.startsWith('./')) {\n normalized = normalized.slice(2);\n }\n\n // Ensure absolute-like comparison\n if (!normalized.startsWith('/')) {\n normalized = '/' + normalized;\n }\n\n return normalized;\n}\n\n/**\n * Extracts path from tool arguments if present.\n */\nexport function extractPathFromArgs(args: unknown): string | undefined {\n if (args === null || typeof args !== 'object') {\n return undefined;\n }\n\n const argsObj = args as Record<string, unknown>;\n\n // Common path field names\n const pathFields = ['path', 'filePath', 'file_path', 'directory', 'dir', 'target'];\n\n for (const field of pathFields) {\n const value = argsObj[field];\n if (typeof value === 'string') {\n return value;\n }\n }\n\n return undefined;\n}\n","/**\n * nexus-agents/mcp - Policy Firewall Rules\n *\n * Default policy rules and constants for the authorization layer.\n *\n * (Source: OWASP ASVS 4.0, Authorization Controls)\n */\n\nimport type { PolicyContext, PolicyDecision, PolicyRule } from './policy-types.js';\nimport { isPathSafe, extractPathFromArgs } from './policy-helpers.js';\n\n// =============================================================================\n// Tool Classification Constants\n// =============================================================================\n\n/**\n * Tools that are considered write/mutation operations.\n */\nexport const MUTATION_TOOLS = new Set([\n 'write_file',\n 'edit_file',\n 'delete_file',\n 'create_directory',\n 'remove_directory',\n 'execute_command',\n 'run_shell',\n 'bash',\n 'create_expert',\n 'run_workflow',\n]);\n\n/**\n * Tools that are considered read-only operations.\n */\nexport const READ_ONLY_TOOLS = new Set([\n 'read_file',\n 'list_directory',\n 'search_files',\n 'get_status',\n 'orchestrate',\n 'delegate_to_model',\n]);\n\n// =============================================================================\n// Tool Classification Functions\n// =============================================================================\n\n/**\n * Checks if a tool is a mutation operation.\n */\nexport function isMutationTool(toolName: string): boolean {\n // Check explicit mutation tools\n if (MUTATION_TOOLS.has(toolName)) {\n return true;\n }\n\n // Check explicit read-only tools\n if (READ_ONLY_TOOLS.has(toolName)) {\n return false;\n }\n\n // Default to treating unknown tools as mutations (safe default)\n return true;\n}\n\n// =============================================================================\n// Default Policy Rules\n// =============================================================================\n\n/**\n * Policy rule that denies mutation operations when mode is 'read-only'.\n *\n * This ensures that write operations are only allowed when explicitly\n * enabled via the 'read-write' mode.\n */\nexport const denyMutationsWithoutModeRule: PolicyRule = {\n name: 'deny-mutations-without-mode',\n description: 'Blocks write operations unless mode is read-write',\n check(ctx: PolicyContext): PolicyDecision {\n // If mode is read-write, allow all operations\n if (ctx.mode === 'read-write') {\n return { allowed: true, reason: 'Read-write mode enabled' };\n }\n\n // Check if this is a mutation tool\n if (isMutationTool(ctx.toolName)) {\n return {\n allowed: false,\n reason: `Tool '${ctx.toolName}' is a mutation operation but mode is '${ctx.mode}'. Set mode to 'read-write' to enable.`,\n };\n }\n\n // Read-only tool in read-only mode is allowed\n return { allowed: true, reason: 'Read-only operation allowed' };\n },\n};\n\n/**\n * Policy rule that validates paths against allowed roots.\n *\n * Prevents path traversal attacks by ensuring all file operations\n * target paths within configured allowed directories.\n */\nexport const safePathsRule: PolicyRule = {\n name: 'safe-paths',\n description: 'Validates paths against allowed root directories',\n check(ctx: PolicyContext): PolicyDecision {\n // Extract path from arguments\n const targetPath = extractPathFromArgs(ctx.args);\n\n // If no path in args, allow (not a file operation)\n if (targetPath === undefined) {\n return { allowed: true, reason: 'No path argument found' };\n }\n\n // Check for obvious path traversal attempts\n if (targetPath.includes('..')) {\n return {\n allowed: false,\n reason: `Path contains '..' which may indicate path traversal: ${targetPath}`,\n };\n }\n\n // Get allowed paths from context or use default\n const allowedPaths = ctx.allowedPaths ?? ['./'];\n\n // Validate path is within allowed roots\n if (!isPathSafe(targetPath, allowedPaths)) {\n return {\n allowed: false,\n reason: `Path '${targetPath}' is outside allowed directories: ${allowedPaths.join(', ')}`,\n };\n }\n\n return { allowed: true, reason: 'Path is within allowed directories' };\n },\n};\n","/**\n * nexus-agents/mcp - Policy Firewall Middleware\n *\n * Authorization layer for MCP tool calls. Evaluates policy rules\n * to determine whether operations should be allowed or denied.\n *\n * This is separate from validation - validation checks if input is well-formed,\n * policy checks if the operation is authorized.\n *\n * (Source: OWASP ASVS 4.0, Authorization Controls)\n */\n\nimport { createLogger, type ILogger, type Result, ok, err } from '../../core/index.js';\n\nimport type {\n Artifact,\n ExecutionMode,\n PolicyMode,\n PolicyDecision,\n PolicyContext,\n PolicyRule,\n IPolicyFirewall,\n PolicyFirewallConfig,\n} from './policy-types.js';\nimport { PolicyError } from './policy-types.js';\nimport { denyMutationsWithoutModeRule, safePathsRule } from './policy-rules.js';\n\n// =============================================================================\n// PolicyFirewall Implementation\n// =============================================================================\n\n/**\n * Policy firewall that evaluates rules to authorize or deny operations.\n *\n * Rules are evaluated in order. The first rule that denies the operation\n * stops evaluation and returns the denial. If all rules pass, the operation\n * is allowed.\n *\n * @example\n * ```typescript\n * const firewall = new PolicyFirewall({ mode: 'enforce' });\n *\n * // Add rules\n * firewall.addRule(denyMutationsWithoutModeRule);\n * firewall.addRule(safePathsRule);\n *\n * // Evaluate\n * const decision = firewall.evaluate({\n * toolName: 'write_file',\n * args: { path: '/etc/passwd' },\n * mode: 'read-only',\n * });\n *\n * if (!decision.allowed) {\n * console.error(`Denied: ${decision.reason}`);\n * }\n * ```\n */\nexport class PolicyFirewall implements IPolicyFirewall {\n private readonly rules: PolicyRule[] = [];\n private mode: PolicyMode;\n private readonly logger: ILogger;\n\n constructor(config?: PolicyFirewallConfig) {\n this.mode = config?.mode ?? 'enforce';\n this.logger = config?.logger ?? createLogger({ component: 'policy-firewall' });\n\n // Register initial rules if provided\n if (config?.rules) {\n for (const rule of config.rules) {\n this.rules.push(rule);\n }\n }\n\n this.logger.debug('Policy firewall initialized', {\n mode: this.mode,\n ruleCount: this.rules.length,\n });\n }\n\n /**\n * Evaluates all policy rules against the given context.\n *\n * Rules are evaluated in order. The first rule that denies stops\n * evaluation and returns the denial decision.\n *\n * @param ctx - The policy context to evaluate\n * @returns The policy decision\n */\n evaluate(ctx: PolicyContext): PolicyDecision {\n this.logger.debug('Evaluating policy', {\n toolName: ctx.toolName,\n mode: ctx.mode,\n ruleCount: this.rules.length,\n });\n\n // If no rules, allow by default\n if (this.rules.length === 0) {\n return this.allowWithReason(ctx, 'No policy rules configured');\n }\n\n // Evaluate each rule in order\n for (const rule of this.rules) {\n const decision = rule.check(ctx);\n\n if (!decision.allowed) {\n return this.handleDenial(ctx, rule, decision);\n }\n }\n\n // All rules passed\n return this.allowWithReason(ctx, 'All policy rules passed');\n }\n\n /**\n * Creates an allow decision with the given reason and logs it.\n */\n private allowWithReason(ctx: PolicyContext, reason: string): PolicyDecision {\n const decision: PolicyDecision = { allowed: true, reason };\n this.logDecision(ctx, decision);\n return decision;\n }\n\n /**\n * Handles a rule denial, respecting warn mode if configured.\n */\n private handleDenial(\n ctx: PolicyContext,\n rule: PolicyRule,\n decision: PolicyDecision\n ): PolicyDecision {\n const denialDecision: PolicyDecision = {\n ...decision,\n ruleName: rule.name,\n };\n\n this.logDecision(ctx, denialDecision);\n\n // In warn mode, log but still allow\n if (this.mode === 'warn') {\n this.logger.warn('Policy denial overridden by warn mode', {\n toolName: ctx.toolName,\n ruleName: rule.name,\n reason: decision.reason,\n });\n return {\n allowed: true,\n reason: `[WARN MODE] Would be denied: ${decision.reason}`,\n ruleName: rule.name,\n };\n }\n\n return denialDecision;\n }\n\n /**\n * Adds a policy rule to the firewall.\n *\n * @param rule - The rule to add\n */\n addRule(rule: PolicyRule): void {\n // Prevent duplicate rules\n const existingIndex = this.rules.findIndex((r) => r.name === rule.name);\n if (existingIndex >= 0) {\n this.logger.warn('Replacing existing policy rule', { ruleName: rule.name });\n this.rules[existingIndex] = rule;\n } else {\n this.rules.push(rule);\n this.logger.debug('Policy rule added', { ruleName: rule.name });\n }\n }\n\n /**\n * Removes a policy rule by name.\n *\n * @param name - The name of the rule to remove\n * @returns True if the rule was found and removed\n */\n removeRule(name: string): boolean {\n const index = this.rules.findIndex((r) => r.name === name);\n if (index >= 0) {\n this.rules.splice(index, 1);\n this.logger.debug('Policy rule removed', { ruleName: name });\n return true;\n }\n return false;\n }\n\n /**\n * Gets all registered policy rules.\n *\n * @returns A readonly array of policy rules\n */\n getRules(): readonly PolicyRule[] {\n return [...this.rules];\n }\n\n /**\n * Sets the policy enforcement mode.\n *\n * @param mode - The new enforcement mode\n */\n setMode(mode: PolicyMode): void {\n const previousMode = this.mode;\n this.mode = mode;\n this.logger.info('Policy mode changed', { from: previousMode, to: mode });\n }\n\n /**\n * Gets the current policy enforcement mode.\n *\n * @returns The current mode\n */\n getMode(): PolicyMode {\n return this.mode;\n }\n\n /**\n * Logs a policy decision for audit purposes.\n */\n private logDecision(ctx: PolicyContext, decision: PolicyDecision): void {\n const logData = {\n toolName: ctx.toolName,\n mode: ctx.mode,\n workflowId: ctx.workflowId,\n allowed: decision.allowed,\n reason: decision.reason,\n ruleName: decision.ruleName,\n };\n\n if (decision.allowed) {\n this.logger.debug('Policy decision: ALLOWED', logData);\n } else {\n this.logger.warn('Policy decision: DENIED', logData);\n }\n }\n}\n\n// =============================================================================\n// Factory Functions\n// =============================================================================\n\n/**\n * Creates a policy firewall with default rules.\n *\n * Default rules included:\n * - deny-mutations-without-mode\n * - safe-paths\n *\n * @param config - Optional configuration\n * @returns A configured PolicyFirewall instance\n */\nexport function createDefaultPolicyFirewall(config?: PolicyFirewallConfig): PolicyFirewall {\n const firewall = new PolicyFirewall(config);\n\n // Add default rules\n firewall.addRule(denyMutationsWithoutModeRule);\n firewall.addRule(safePathsRule);\n\n return firewall;\n}\n\n/**\n * Evaluates a policy context and returns a Result.\n *\n * This is a convenience function that wraps the firewall evaluation\n * in a Result type for easier error handling.\n *\n * @param firewall - The policy firewall to use\n * @param ctx - The policy context to evaluate\n * @returns Result containing void on success or PolicyError on denial\n */\nexport function evaluatePolicy(\n firewall: IPolicyFirewall,\n ctx: PolicyContext\n): Result<void, PolicyError> {\n const decision = firewall.evaluate(ctx);\n\n if (decision.allowed) {\n return ok(undefined);\n }\n\n return err(new PolicyError(`Policy denied: ${decision.reason}`, decision));\n}\n\n/**\n * Creates a policy context from tool invocation parameters.\n *\n * @param toolName - Name of the tool being invoked\n * @param args - Tool arguments\n * @param options - Additional context options\n * @returns A PolicyContext object\n */\nexport function createPolicyContext(\n toolName: string,\n args: unknown,\n options?: {\n mode?: ExecutionMode;\n artifacts?: Map<string, Artifact>;\n workflowId?: string;\n allowedPaths?: readonly string[];\n }\n): PolicyContext {\n // Build base context with required properties\n const base = {\n toolName,\n args,\n mode: options?.mode ?? 'read-only',\n };\n\n // Use Object.assign to build result, only adding optional properties\n // when they are actually defined (to satisfy exactOptionalPropertyTypes)\n const result: Record<string, unknown> = { ...base };\n\n if (options?.artifacts !== undefined) {\n result['artifacts'] = options.artifacts;\n }\n if (options?.workflowId !== undefined) {\n result['workflowId'] = options.workflowId;\n }\n if (options?.allowedPaths !== undefined) {\n result['allowedPaths'] = options.allowedPaths;\n }\n\n return result as unknown as PolicyContext;\n}\n\n// =============================================================================\n// Re-exports for backward compatibility\n// =============================================================================\n\nexport * from './policy-types.js';\nexport * from './policy-rules.js';\nexport * from './policy-helpers.js';\n","/**\n * nexus-agents/mcp - Timeout Guard Middleware\n *\n * Provides timeout protection for MCP operations to mitigate ReDoS and\n * other denial-of-service vectors. Implements configurable timeouts with\n * proper cleanup and error handling.\n *\n * (Source: CVE-2026-0621, GHSA-8r9q-7v3j-jr4g)\n * (Source: Issue #107)\n *\n * @module mcp/middleware/timeout-guard\n */\n\nimport {\n getErrorMessage,\n createLogger,\n type ILogger,\n type Result,\n ok,\n err,\n getTimeProvider,\n} from '../../core/index.js';\n\n/**\n * Error codes for timeout-related failures.\n */\nexport type TimeoutErrorCode =\n | 'OPERATION_TIMEOUT'\n | 'OPERATION_CANCELLED'\n | 'INVALID_TIMEOUT'\n | 'GUARD_ERROR';\n\n/**\n * Timeout guard error.\n */\nexport interface TimeoutError {\n readonly code: TimeoutErrorCode;\n readonly message: string;\n readonly operation?: string;\n readonly timeoutMs?: number;\n readonly cause?: Error;\n}\n\n/**\n * Configuration for timeout guard.\n */\nexport interface TimeoutGuardConfig {\n /** Default timeout in milliseconds (default: 30000) */\n readonly defaultTimeoutMs?: number;\n /** Maximum allowed timeout in milliseconds (default: 300000) */\n readonly maxTimeoutMs?: number;\n /** Whether to log timeout events (default: true) */\n readonly enableLogging?: boolean;\n /** Logger instance */\n readonly logger?: ILogger;\n}\n\n/**\n * Result of a guarded operation.\n */\nexport interface GuardedResult<T> {\n /** The operation result */\n readonly value: T;\n /** Execution duration in milliseconds */\n readonly durationMs: number;\n /** Whether the operation was near timeout */\n readonly nearTimeout: boolean;\n}\n\n/** Execution options for guarded operations. */\nexport interface ExecuteOptions {\n /** Custom timeout for this operation */\n readonly timeoutMs?: number;\n /** Name for logging/debugging */\n readonly operationName?: string;\n /** Cleanup function to call on timeout */\n readonly onTimeout?: () => void;\n /** AbortSignal for client-initiated cancellation */\n readonly signal?: AbortSignal;\n}\n\n// Canonical source: config/timeouts.ts (Issue #1046)\nimport { TIMEOUT_GUARD } from '../../config/timeouts.js';\n\nconst DEFAULT_TIMEOUT_MS = TIMEOUT_GUARD.defaultMs;\nconst MAX_TIMEOUT_MS = TIMEOUT_GUARD.maxMs;\nconst NEAR_TIMEOUT_THRESHOLD = TIMEOUT_GUARD.nearTimeoutThreshold;\n\n/** Internal state for tracking timeout. */\ninterface TimeoutState {\n timeoutId: ReturnType<typeof setTimeout> | undefined;\n timedOut: boolean;\n}\n\n/**\n * Creates a timeout error for operation timeout.\n */\nfunction createTimeoutError(operationName: string, timeoutMs: number): TimeoutError {\n return {\n code: 'OPERATION_TIMEOUT',\n message: `Operation '${operationName}' timed out after ${String(timeoutMs)}ms`,\n operation: operationName,\n timeoutMs,\n };\n}\n\n/**\n * Creates a guard error from a caught exception.\n */\nfunction createGuardError(error: unknown, operationName: string): TimeoutError {\n const guardError: TimeoutError = {\n code: 'GUARD_ERROR',\n message: getErrorMessage(error),\n operation: operationName,\n };\n if (error instanceof Error) {\n return { ...guardError, cause: error };\n }\n return guardError;\n}\n\n/**\n * Timeout guard for protecting async operations from hanging.\n *\n * Provides protection against:\n * - ReDoS attacks (CVE-2026-0621)\n * - Slow/hanging external services\n * - Resource exhaustion\n *\n * @example\n * ```typescript\n * const guard = new TimeoutGuard({ defaultTimeoutMs: 5000 });\n *\n * const result = await guard.execute(\n * () => someAsyncOperation(),\n * { operationName: 'process-uri' }\n * );\n *\n * if (result.ok) {\n * console.log('Completed in', result.value.durationMs, 'ms');\n * }\n * ```\n */\nexport class TimeoutGuard {\n private readonly defaultTimeoutMs: number;\n private readonly maxTimeoutMs: number;\n private readonly enableLogging: boolean;\n private readonly logger: ILogger;\n\n constructor(config?: TimeoutGuardConfig) {\n this.defaultTimeoutMs = config?.defaultTimeoutMs ?? DEFAULT_TIMEOUT_MS;\n this.maxTimeoutMs = config?.maxTimeoutMs ?? MAX_TIMEOUT_MS;\n this.enableLogging = config?.enableLogging ?? true;\n this.logger = config?.logger ?? createLogger({ component: 'timeout-guard' });\n }\n\n /** Resolves effective timeout and operation name from options. */\n private resolveOptions(options?: ExecuteOptions): {\n timeoutMs: number;\n operationName: string;\n } {\n return {\n timeoutMs: Math.min(options?.timeoutMs ?? this.defaultTimeoutMs, this.maxTimeoutMs),\n operationName: options?.operationName ?? 'unknown',\n };\n }\n\n /**\n * Executes an async operation with timeout protection.\n */\n async execute<T>(\n operation: () => Promise<T>,\n options?: ExecuteOptions\n ): Promise<Result<GuardedResult<T>, TimeoutError>> {\n const { timeoutMs, operationName } = this.resolveOptions(options);\n\n const validationError = this.validateTimeout(timeoutMs, operationName);\n if (validationError !== null) {\n return err(validationError);\n }\n\n return this.runGuarded(operation, timeoutMs, operationName, options);\n }\n\n /** Runs the operation with timeout and optional abort signal. */\n private async runGuarded<T>(\n operation: () => Promise<T>,\n timeoutMs: number,\n operationName: string,\n options?: ExecuteOptions\n ): Promise<Result<GuardedResult<T>, TimeoutError>> {\n this.logStart(operationName, timeoutMs);\n const startTime = getTimeProvider().now();\n const state: TimeoutState = { timeoutId: undefined, timedOut: false };\n\n try {\n const result = await this.runWithTimeout(\n operation,\n timeoutMs,\n state,\n options?.onTimeout,\n options?.signal\n );\n return this.handleSuccess(result, startTime, timeoutMs, operationName);\n } catch {\n const cancelled = options?.signal?.aborted === true && !state.timedOut;\n return err(\n this.handleFailure(state.timedOut, operationName, timeoutMs, startTime, cancelled)\n );\n } finally {\n if (state.timeoutId !== undefined) {\n clearTimeout(state.timeoutId);\n }\n }\n }\n\n private validateTimeout(timeoutMs: number, operationName: string): TimeoutError | null {\n if (timeoutMs <= 0) {\n return {\n code: 'INVALID_TIMEOUT',\n message: `Invalid timeout: ${String(timeoutMs)}ms`,\n operation: operationName,\n };\n }\n return null;\n }\n\n private logStart(operationName: string, timeoutMs: number): void {\n if (this.enableLogging) {\n this.logger.debug('Starting guarded operation', { operation: operationName, timeoutMs });\n }\n }\n\n private async runWithTimeout<T>(\n operation: () => Promise<T>,\n timeoutMs: number,\n state: TimeoutState,\n onTimeout?: () => void,\n signal?: AbortSignal\n ): Promise<T> {\n const timeoutPromise = new Promise<never>((_resolve, reject) => {\n state.timeoutId = setTimeout(() => {\n state.timedOut = true;\n onTimeout?.();\n reject(new Error(`Operation timed out after ${String(timeoutMs)}ms`));\n }, timeoutMs);\n });\n\n const promises: Array<Promise<T>> = [operation(), timeoutPromise];\n\n // Race against client AbortSignal if provided\n if (signal !== undefined && !signal.aborted) {\n const abortPromise = new Promise<never>((_resolve, reject) => {\n signal.addEventListener(\n 'abort',\n () => {\n reject(new Error('Operation cancelled by client'));\n },\n { once: true }\n );\n });\n promises.push(abortPromise);\n }\n\n return Promise.race(promises);\n }\n\n private handleSuccess<T>(\n result: T,\n startTime: number,\n timeoutMs: number,\n operationName: string\n ): Result<GuardedResult<T>, TimeoutError> {\n const durationMs = getTimeProvider().now() - startTime;\n const nearTimeout = durationMs > timeoutMs * NEAR_TIMEOUT_THRESHOLD;\n\n if (nearTimeout && this.enableLogging) {\n this.logger.warn('Operation completed near timeout threshold', {\n operation: operationName,\n durationMs,\n timeoutMs,\n thresholdPercent: Math.round((durationMs / timeoutMs) * 100),\n });\n }\n\n if (this.enableLogging) {\n this.logger.debug('Guarded operation completed', { operation: operationName, durationMs });\n }\n\n return ok({ value: result, durationMs, nearTimeout });\n }\n\n private handleFailure(\n timedOut: boolean,\n operationName: string,\n timeoutMs: number,\n startTime: number,\n cancelled = false\n ): TimeoutError {\n const durationMs = getTimeProvider().now() - startTime;\n\n if (cancelled) {\n this.logger.info('Operation cancelled by client', {\n operation: operationName,\n durationMs,\n });\n return {\n code: 'OPERATION_CANCELLED',\n message: `Operation '${operationName}' cancelled by client`,\n operation: operationName,\n };\n }\n\n if (timedOut) {\n this.logger.error('Operation timed out', undefined, {\n operation: operationName,\n timeoutMs,\n durationMs,\n });\n return createTimeoutError(operationName, timeoutMs);\n }\n\n return createGuardError(new Error('Unknown error'), operationName);\n }\n\n /**\n * Creates a guarded version of an async function.\n */\n guard<TArgs extends unknown[], TResult>(\n fn: (...args: TArgs) => Promise<TResult>,\n options?: { readonly timeoutMs?: number; readonly operationName?: string }\n ): (...args: TArgs) => Promise<Result<GuardedResult<TResult>, TimeoutError>> {\n return async (...args: TArgs): Promise<Result<GuardedResult<TResult>, TimeoutError>> => {\n return this.execute(() => fn(...args), options);\n };\n }\n}\n\n/**\n * URI validation utilities to complement timeout protection.\n */\nexport const UriValidation = {\n MAX_URI_LENGTH: 8192,\n MAX_TEMPLATE_DEPTH: 3,\n SUSPICIOUS_PATTERN: /\\{[+#./;?&]?[^}]*\\*\\}.*\\{[+#./;?&]?[^}]*\\*\\}|\\{(?:[^{}]*\\{){3,}/,\n\n validate(uri: string): Result<string, TimeoutError> {\n if (uri.length > this.MAX_URI_LENGTH) {\n return err({\n code: 'GUARD_ERROR',\n message: `URI exceeds maximum length: ${String(uri.length)} > ${String(this.MAX_URI_LENGTH)}`,\n operation: 'uri-validation',\n });\n }\n\n if (this.SUSPICIOUS_PATTERN.test(uri)) {\n return err({\n code: 'GUARD_ERROR',\n message: 'URI contains suspicious patterns that may cause performance issues',\n operation: 'uri-validation',\n });\n }\n\n return ok(uri);\n },\n\n sanitize(uri: string): string {\n const sanitized = uri.slice(0, this.MAX_URI_LENGTH);\n let depth = 0;\n let result = '';\n\n for (const char of sanitized) {\n if (char === '{') {\n depth++;\n if (depth > this.MAX_TEMPLATE_DEPTH) continue;\n } else if (char === '}') {\n if (depth > this.MAX_TEMPLATE_DEPTH) {\n depth--;\n continue;\n }\n depth--;\n }\n result += char;\n }\n\n return result;\n },\n};\n\n/**\n * Creates a timeout guard with default MCP-appropriate settings.\n */\nexport function createDefaultTimeoutGuard(logger?: ILogger): TimeoutGuard {\n const config: TimeoutGuardConfig = {\n defaultTimeoutMs: DEFAULT_TIMEOUT_MS,\n maxTimeoutMs: MAX_TIMEOUT_MS,\n enableLogging: true,\n };\n if (logger !== undefined) {\n return new TimeoutGuard({ ...config, logger });\n }\n return new TimeoutGuard(config);\n}\n","/**\n * nexus-agents/mcp - Request Context Middleware\n *\n * Provides request ID generation and caller context tracking for MCP tools.\n * (Source: Issue #185 Phase 1 - Request context & PolicyFirewall integration)\n *\n * @module mcp/middleware/request-context\n */\n\nimport { randomBytes } from 'node:crypto';\nimport { getTimeProvider } from '../../core/index.js';\nimport type { TrustTier } from '../../security/trust-types.js';\n\n/**\n * Authenticated user information.\n * (Source: Issue #739 - MCP authentication)\n */\nexport interface AuthenticatedUser {\n /** Unique user/client identifier */\n readonly id: string;\n /** Human-readable name (optional) */\n readonly name?: string;\n /** Granted permissions/scopes (optional) */\n readonly permissions?: readonly string[];\n}\n\n/**\n * Caller identification for audit trails.\n */\nexport interface CallerInfo {\n /** Client identifier (e.g., 'claude-cli', 'gemini-cli') */\n readonly clientId?: string;\n /** User agent string if available */\n readonly userAgent?: string;\n /** Session ID for request correlation */\n readonly sessionId?: string;\n /** IP address or transport identifier */\n readonly transport?: string;\n /** Whether the request is authenticated (Issue #739) */\n readonly authenticated?: boolean;\n /** Authenticated user information (Issue #739) */\n readonly authenticatedUser?: AuthenticatedUser;\n}\n\n/**\n * Request context for MCP tool invocations.\n * Immutable once created.\n */\nexport interface RequestContext {\n /** Unique request identifier (format: req_<16 hex chars>) */\n readonly requestId: string;\n /** Timestamp when request was received (ISO 8601, ET) */\n readonly timestamp: string;\n /** Tool being invoked */\n readonly toolName: string;\n /** Caller information for audit */\n readonly caller: CallerInfo;\n /**\n * Trust tier for this request (Issue #828).\n * Derived from caller authentication state:\n * - '1' = Authenticated + known client, or stdio (local-only)\n * - '2' = Authenticated via network\n * - '3' = Unauthenticated network request\n * - '4' = Request with detected injection patterns (set by sanitizer)\n */\n readonly trustTier: TrustTier;\n /** Trace ID for distributed tracing correlation */\n readonly traceId?: string;\n /** Parent span ID if part of a larger trace */\n readonly parentSpanId?: string;\n}\n\n/**\n * Options for creating a request context.\n */\nexport interface CreateContextOptions {\n /** Tool name being invoked */\n toolName: string;\n /** Optional caller information */\n caller?: CallerInfo;\n /** Optional explicit trust tier override (defaults to derived from caller) */\n trustTier?: TrustTier;\n /** Optional trace ID for correlation */\n traceId?: string;\n /** Optional parent span ID */\n parentSpanId?: string;\n}\n\n/**\n * Generates a cryptographically secure request ID.\n * Format: req_<16 hex characters>\n *\n * @returns Unique request identifier\n */\nexport function generateRequestId(): string {\n const bytes = randomBytes(8);\n return `req_${bytes.toString('hex')}`;\n}\n\n/**\n * Generates a session ID for request correlation.\n * Format: sess_<12 hex characters>\n *\n * @returns Unique session identifier\n */\nexport function generateSessionId(): string {\n const bytes = randomBytes(6);\n return `sess_${bytes.toString('hex')}`;\n}\n\n/**\n * Formats timestamp in ISO 8601 format with ET timezone.\n * (Source: CLAUDE.md - Time Authority section)\n */\nfunction formatTimestamp(): string {\n const now = new Date(getTimeProvider().now());\n const formatter = new Intl.DateTimeFormat('en-US', {\n timeZone: 'America/New_York',\n timeZoneName: 'shortOffset',\n });\n const parts = formatter.formatToParts(now);\n const offset = parts.find((p) => p.type === 'timeZoneName')?.value ?? '-05:00';\n const base = now.toLocaleString('sv-SE', {\n timeZone: 'America/New_York',\n hour12: false,\n });\n return base.replace(' ', 'T') + offset.replace('GMT', '');\n}\n\n/**\n * Derives trust tier from caller authentication state (Issue #828).\n *\n * - Authenticated + known CLI client (claude, gemini, codex) → Tier 1\n * - stdio transport (local-only, no network) → Tier 1\n * - Authenticated via network → Tier 2\n * - Unauthenticated → Tier 3\n */\nexport function deriveTrustTier(caller: CallerInfo): TrustTier {\n const knownClients = ['claude-cli', 'gemini-cli', 'codex-cli'];\n\n if (caller.transport === 'stdio') return '1';\n\n if (caller.authenticated === true) {\n if (caller.clientId !== undefined && knownClients.includes(caller.clientId)) {\n return '1';\n }\n return '2';\n }\n\n return '3';\n}\n\n/**\n * Creates an immutable request context for an MCP tool invocation.\n *\n * @param options - Context creation options\n * @returns Immutable request context\n */\nexport function createRequestContext(options: CreateContextOptions): RequestContext {\n const caller = options.caller ?? {};\n const context: RequestContext = {\n requestId: generateRequestId(),\n timestamp: formatTimestamp(),\n toolName: options.toolName,\n caller,\n trustTier: options.trustTier ?? deriveTrustTier(caller),\n ...(options.traceId !== undefined && { traceId: options.traceId }),\n ...(options.parentSpanId !== undefined && { parentSpanId: options.parentSpanId }),\n };\n\n // Freeze to ensure immutability\n return Object.freeze(context);\n}\n\n/**\n * Extracts caller info from MCP transport metadata.\n * Currently supports extracting from request headers or environment.\n *\n * @param metadata - Optional transport metadata\n * @returns Caller information\n */\nexport function extractCallerInfo(metadata?: Record<string, unknown>): CallerInfo {\n const caller: CallerInfo = {};\n\n if (metadata !== undefined) {\n // Extract all available fields from metadata\n const extracted: CallerInfo = {\n ...caller,\n ...(typeof metadata['clientId'] === 'string' ? { clientId: metadata['clientId'] } : {}),\n ...(typeof metadata['userAgent'] === 'string' ? { userAgent: metadata['userAgent'] } : {}),\n ...(typeof metadata['sessionId'] === 'string' ? { sessionId: metadata['sessionId'] } : {}),\n };\n\n // If any metadata was extracted, return it directly\n if (\n typeof metadata['clientId'] === 'string' ||\n typeof metadata['userAgent'] === 'string' ||\n typeof metadata['sessionId'] === 'string'\n ) {\n return extracted;\n }\n }\n\n // Fallback to environment variables for known CLI tools\n const claudeSession = process.env['CLAUDE_SESSION_ID'];\n if (claudeSession !== undefined) {\n return { ...caller, clientId: 'claude-cli', sessionId: claudeSession };\n }\n\n const geminiSession = process.env['GEMINI_SESSION_ID'];\n if (geminiSession !== undefined) {\n return { ...caller, clientId: 'gemini-cli', sessionId: geminiSession };\n }\n\n return caller;\n}\n\n/**\n * Formats request context for logging.\n * Extracts essential fields for log context.\n *\n * @param ctx - Request context\n * @returns Log-friendly context object\n */\nexport function contextForLogging(ctx: RequestContext): Record<string, unknown> {\n return {\n requestId: ctx.requestId,\n toolName: ctx.toolName,\n trustTier: ctx.trustTier,\n ...(ctx.caller.clientId !== undefined && { clientId: ctx.caller.clientId }),\n ...(ctx.traceId !== undefined && { traceId: ctx.traceId }),\n };\n}\n\n/**\n * Type guard to check if a value is a valid RequestContext.\n */\nexport function isRequestContext(value: unknown): value is RequestContext {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n const obj = value as Record<string, unknown>;\n return (\n typeof obj['requestId'] === 'string' &&\n obj['requestId'].startsWith('req_') &&\n typeof obj['timestamp'] === 'string' &&\n typeof obj['toolName'] === 'string' &&\n typeof obj['caller'] === 'object' &&\n typeof obj['trustTier'] === 'string' &&\n ['1', '2', '3', '4'].includes(obj['trustTier'])\n );\n}\n","/**\n * Access Constraint Deriver — Unbypassable denylist (#1977 condition 3).\n *\n * Hardcoded patterns that no LLM-derived policy may override. Applied FIRST\n * in the enforcer, before checking the per-task policy. If the LLM (or a\n * malicious user objective) produces a policy that would allow access to\n * `.env`, SSH keys, cloud credentials, or similar, the denylist still wins.\n *\n * This exists because the LLM deriver is the weakest link — a user objective\n * that says \"I need to view my AWS credentials to debug\" would otherwise\n * produce a policy allowing `~/.aws/**`. The denylist refuses regardless.\n *\n * @module security/access-constraint-deriver/denylist\n */\n\n/**\n * File-path patterns that are unconditionally denied. Glob-style wildcards.\n * All matching is case-insensitive to catch `~/.SSH` and similar.\n */\nexport const UNBYPASSABLE_PATH_PATTERNS: readonly string[] = [\n // Environment files\n '.env',\n '.env.*',\n '**/.env',\n '**/.env.*',\n\n // SSH credentials\n '~/.ssh/**',\n '**/ssh/id_*',\n '**/*_rsa',\n '**/*_ed25519',\n '**/*.pem',\n\n // Cloud credentials\n '~/.aws/**',\n '~/.azure/**',\n '~/.gcp/**',\n '~/.config/gcloud/**',\n '~/.kube/config',\n\n // Unix secret files\n '/etc/shadow',\n '/etc/sudoers',\n '/etc/sudoers.d/**',\n\n // Common secret file patterns\n '**/secrets.*',\n '**/credentials.*',\n '**/private_key.*',\n '**/id_rsa*',\n];\n\n/**\n * Tool names that are unconditionally denied regardless of derived policy.\n * These are tools that should never be callable during automated agent\n * dispatch — they require explicit human action.\n */\nexport const UNBYPASSABLE_TOOL_NAMES: readonly string[] = [\n // Destructive git operations\n 'git_push_force',\n 'git_reset_hard',\n 'git_branch_delete_force',\n 'git_clean_force',\n\n // Destructive filesystem\n 'rm_recursive_force',\n 'chmod_recursive',\n\n // Identity / auth mutations\n 'ssh_add_key',\n 'gpg_add_key',\n 'npm_publish_force',\n\n // Remote destruction\n 'github_repo_delete',\n 'github_org_transfer',\n 'aws_account_close',\n];\n\n/**\n * Compiles a glob pattern to a regex at module-load time.\n * Supports: `**` (any path segments), `*` (any non-separator chars),\n * `~/` (home-anchor prefix). All other regex metachars escaped.\n *\n * Regexes built here come from the hardcoded `UNBYPASSABLE_PATH_PATTERNS`\n * constant — never from user input — so they are ReDoS-safe by construction.\n */\nfunction compileGlobToRegex(pattern: string): RegExp {\n const pat = pattern.toLowerCase();\n // Escape regex metacharacters INCLUDING backslash (per CodeQL — without\n // backslash in the class, a `\\` in input would leak into the regex output\n // unescaped). Do NOT escape `*` here — it's a glob wildcard that the\n // subsequent replaces expand into regex wildcards.\n const escaped = pat\n .replace(/[\\\\.+^$()|[\\]{}]/g, '\\\\$&')\n .replace(/\\*\\*/g, '__DOUBLESTAR__')\n .replace(/\\*/g, '[^/]*')\n .replace(/__DOUBLESTAR__/g, '.*');\n const anchored = escaped.startsWith('~/')\n ? `(^|/)${escaped.slice(2)}$`\n : escaped.startsWith('/')\n ? `^${escaped}$`\n : `(^|/)${escaped}$`;\n return new RegExp(anchored);\n}\n\n/**\n * Precompiled regexes for every unbypassable path pattern, computed once\n * at module load. Static inputs → no ReDoS surface.\n */\nconst COMPILED_PATH_PATTERNS: ReadonlyArray<{\n readonly pattern: string;\n readonly regex: RegExp;\n}> = UNBYPASSABLE_PATH_PATTERNS.map((pattern) => ({\n pattern,\n regex: compileGlobToRegex(pattern),\n}));\n\n/**\n * Returns true if a lowercased file path matches the given pattern.\n * Exported for tests; production code should use `isPathDenied`.\n */\nexport function matchDenyPattern(path: string, pattern: string): boolean {\n const normalized = path.toLowerCase();\n const compiled = COMPILED_PATH_PATTERNS.find((c) => c.pattern === pattern);\n if (compiled !== undefined) return compiled.regex.test(normalized);\n // Fallback for ad-hoc test patterns not in the static list.\n return compileGlobToRegex(pattern).test(normalized);\n}\n\n/** Returns true if the path hits any unbypassable pattern. */\nexport function isPathDenied(path: string): boolean {\n const normalized = path.toLowerCase();\n return COMPILED_PATH_PATTERNS.some((c) => c.regex.test(normalized));\n}\n\n/** Returns true if the tool name is unconditionally denied. */\nexport function isToolDenied(toolName: string): boolean {\n return UNBYPASSABLE_TOOL_NAMES.includes(toolName);\n}\n","/**\n * Tool risk classification for `confirm_risky` access-policy mode (#2279).\n *\n * Classifies each registered MCP tool as `read-only` (safe — log-and-allow\n * under confirm_risky) or `risky` (write/exec/network — block under\n * confirm_risky). Used by the policy enforcer to differentiate which\n * violations a human would have wanted to review.\n *\n * Classification policy:\n *\n * - **Read-only**: tool exclusively reads existing state — no file writes,\n * no subprocess execution, no network requests beyond a single bounded\n * read of project-internal data. `query_*`, `list_*`, `search_*`,\n * `*_query`, `*_analyze` patterns by default.\n * - **Risky**: tool writes files, runs subprocesses, posts to external\n * services, calls LLM APIs, or has any side effect that a human reviewer\n * would want to see before approving in confirm_risky mode.\n *\n * If a tool is unknown, it is treated as risky (default-deny) — same\n * principle as the unbypassable denylist: the security layer fails closed\n * on a misclassification.\n *\n * Update path: when adding a new MCP tool, classify it explicitly here.\n * The `inject-governance.ts check` CI gate ensures the registry list is\n * still the source of truth; this file is the risk overlay.\n *\n * @module security/access-constraint-deriver/tool-risk\n */\n\n/**\n * Tools that exclusively read state. Violations on these are log-and-allow\n * under `confirm_risky` mode — same as `audit` mode.\n */\nexport const READ_ONLY_TOOLS: ReadonlySet<string> = new Set([\n // Discovery / listing\n 'list_experts',\n 'list_workflows',\n // Research reads\n 'research_query',\n 'research_analyze',\n 'research_catalog_review',\n 'research_synthesize',\n // Memory reads\n 'memory_query',\n 'memory_stats',\n // Observability\n 'weather_report',\n 'query_trace',\n 'query_task_state',\n // Codebase intelligence (read-only over local files)\n 'search_codebase',\n 'extract_symbols',\n // Repo analysis (read-only)\n 'repo_analyze',\n 'repo_security_plan',\n // Routing recommendation (no side effects — returns recommendation)\n 'delegate_to_model',\n // Registry import (returns a draft template — does not write)\n 'registry_import',\n]);\n\n/**\n * Returns true if the tool is risky under confirm_risky mode (default-deny\n * for unknown tools — security layer fails closed).\n */\nexport function isRiskyTool(toolName: string): boolean {\n return !READ_ONLY_TOOLS.has(toolName);\n}\n","/**\n * Access Constraint Deriver — Policy enforcement (#1977, #2279).\n *\n * Pure function that checks a proposed tool call against a derived policy\n * and returns an AccessDecision. Enforcement depends on the policy's mode:\n * - `off` and `audit` never block\n * - `confirm_risky` (#2279) blocks violations on risky tools (write/exec/\n * network) and log-and-allows violations on read-only tools\n * - `enforce` blocks every violation regardless of risk classification\n *\n * @module security/access-constraint-deriver/enforcer\n */\n\nimport { isPathDenied, isToolDenied } from './denylist.js';\nimport { isRiskyTool } from './tool-risk.js';\nimport type { AccessDecision, TaskAccessPolicy } from './types.js';\n\n/**\n * Checks a proposed tool call against the unbypassable denylist AND the\n * task's derived access policy.\n *\n * Order of operations (important — the denylist is FIRST and unbypassable):\n * 1. If the tool is on the hardcoded deny-tool list → deny regardless\n * of policy. This is unbypassable even in `off` mode.\n * 2. If a file-path argument is provided and matches an unbypassable path\n * pattern (e.g. `~/.ssh/**`, `/etc/shadow`) → deny regardless.\n * 3. Otherwise, fall back to the per-task policy (bypass in skeleton).\n *\n * The denylist check runs before the policy check so a malicious LLM-derived\n * policy cannot grant access to secrets/credentials by listing the tool in\n * `allowedTools`.\n */\nexport function checkAccess(\n toolName: string,\n policy: TaskAccessPolicy,\n args?: { readonly path?: string }\n): AccessDecision {\n // 1. Unbypassable tool denylist — applies in all modes.\n if (isToolDenied(toolName)) {\n return {\n decision: 'deny',\n reason: `tool \"${toolName}\" is on the unbypassable deny-tool list`,\n matchedRule: 'unbypassable:tool',\n };\n }\n\n // 2. Unbypassable path denylist — applies when a path argument is given.\n if (typeof args?.path === 'string' && args.path.length > 0 && isPathDenied(args.path)) {\n return {\n decision: 'deny',\n reason: `path \"${args.path}\" is on the unbypassable deny-path list`,\n matchedRule: 'unbypassable:path',\n };\n }\n\n // 3. Per-task policy check.\n if (policy.allowedTools === '*') return { decision: 'allow' };\n\n if (policy.allowedTools.includes(toolName)) return { decision: 'allow' };\n\n return decideOnViolation(toolName, policy.mode);\n}\n\n/**\n * Mode-specific behavior when a tool is not in the per-task allowlist.\n * Extracted so checkAccess stays under the complexity-10 cap.\n */\nfunction decideOnViolation(toolName: string, mode: TaskAccessPolicy['mode']): AccessDecision {\n if (mode === 'audit') {\n return {\n decision: 'log-and-allow',\n warning: `tool \"${toolName}\" not in derived policy (audit mode)`,\n };\n }\n // confirm_risky: split by tool risk classification (#2279). Read-only\n // violations are log-and-allow (audit-like); risky violations are denied\n // with a structured reason that surfaces \"would-have-required-approval\"\n // semantics. Operators add the tool to the allowlist after review, or\n // graduate to `enforce` once the violation rate is acceptable.\n if (mode === 'confirm_risky') {\n if (!isRiskyTool(toolName)) {\n return {\n decision: 'log-and-allow',\n warning: `tool \"${toolName}\" not in derived policy (confirm_risky mode, read-only — would have required human approval, allowed because read-only)`,\n };\n }\n return {\n decision: 'deny',\n reason: `tool \"${toolName}\" not in derived policy (confirm_risky mode, risky — would have required human approval; denied for now, add to allowedTools or run in audit mode to allow)`,\n matchedRule: 'allowedTools:confirm_risky',\n };\n }\n return {\n decision: 'deny',\n reason: `tool \"${toolName}\" not in derived policy`,\n matchedRule: 'allowedTools',\n };\n}\n","/**\n * Access Constraint Deriver — MCP tool dispatch guard (#1977 final wiring).\n *\n * Per-call helper and middleware factory that plugs the access-constraint\n * enforcer into the MCP tool dispatch path. Runs AFTER policy derivation\n * (deriver.ts owns that) and BEFORE the tool handler executes.\n *\n * Behavior matrix:\n * - mode=off → no-op, allows every call (this is the default; runtime\n * behavior is unchanged from pre-#1977)\n * - mode=audit → checks against the policy; on violation, logs a warning\n * and still forwards to the handler\n * - mode=enforce → checks against the policy; on violation, returns an\n * MCP-format isError result without invoking the handler\n *\n * The enforcer's hardcoded denylist (denylist.ts) runs FIRST regardless\n * of mode. Even `off` mode denies destructive tools and secret paths.\n *\n * @module security/access-constraint-deriver/mcp-guard\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport { checkAccess } from './enforcer.js';\nimport type { AccessDecision, TaskAccessPolicy } from './types.js';\n\n/** AsyncLocalStorage holding the active task's access policy. */\nconst accessPolicyStorage = new AsyncLocalStorage<TaskAccessPolicy>();\n\n/**\n * Run `fn` with `policy` available to any nested MCP tool dispatch.\n *\n * Orchestrators (orchestrate, execute_expert, etc.) derive a policy at\n * task start and wrap downstream work in this helper. Tool handlers that\n * use `guardMcpToolCall` will read the policy from ALS.\n */\nexport function withAccessPolicy<T>(policy: TaskAccessPolicy, fn: () => Promise<T>): Promise<T> {\n return accessPolicyStorage.run(policy, fn);\n}\n\n/**\n * Returns the active access policy, or undefined if no wrapping `withAccessPolicy`\n * is in scope. When undefined, `guardMcpToolCall` treats the request as\n * mode=off (permissive) since no task context has been established.\n */\nexport function getActivePolicy(): TaskAccessPolicy | undefined {\n return accessPolicyStorage.getStore();\n}\n\n/**\n * Input-hints a tool handler can surface so the guard can reason about\n * file-path arguments. Tools with a `path` arg should pass it; tools\n * without can omit.\n */\nexport interface GuardArgs {\n readonly path?: string;\n}\n\n/**\n * Check a proposed MCP tool call against the active access policy.\n *\n * Pure function — does not mutate the policy or the storage. Returns an\n * AccessDecision the caller interprets:\n *\n * - `allow`: invoke the handler\n * - `deny`: do not invoke; return a RefuseAction / isError result upstream\n * - `log-and-allow`: log a warning, then invoke the handler (audit mode)\n *\n * When no policy is in ALS (no wrapping `withAccessPolicy`), this returns\n * `allow` — the guard is opt-in at the orchestrator layer.\n */\nexport function guardMcpToolCall(toolName: string, args?: GuardArgs): AccessDecision {\n const policy = getActivePolicy();\n if (policy === undefined) return { decision: 'allow' };\n return checkAccess(toolName, policy, args);\n}\n\n/**\n * Shape of the logger the middleware uses. Minimal to avoid coupling.\n */\ninterface MiddlewareLogger {\n warn: (message: string, context?: Record<string, unknown>) => void;\n info: (message: string, context?: Record<string, unknown>) => void;\n}\n\n/**\n * Formats a deny decision as an MCP-compliant isError ToolResult shape.\n * The MCP server's middleware chain recognizes `{ isError, content }` and\n * surfaces it as a tool error to the caller.\n */\nexport function denyToToolResult(\n decision: Extract<AccessDecision, { decision: 'deny' }>,\n requestId: string\n): { isError: true; content: Array<{ type: 'text'; text: string }> } {\n return {\n isError: true,\n content: [\n {\n type: 'text',\n text: `access denied: ${decision.reason} (rule: ${decision.matchedRule}, request: ${requestId})`,\n },\n ],\n };\n}\n\n/**\n * Create an access-policy middleware that plugs into the MCP middleware\n * chain. Reads the active policy from ALS. When no policy is active OR\n * the policy is in `off` mode, this middleware is a no-op pass-through.\n */\nexport function createAccessPolicyMiddleware(config: {\n readonly toolName: string;\n readonly logger: MiddlewareLogger;\n}): (\n args: unknown,\n ctx: { readonly requestContext: { readonly requestId: string } },\n next: (args: unknown, ctx: unknown) => Promise<unknown>\n) => Promise<unknown> {\n return async (args, ctx, next) => {\n const policy = getActivePolicy();\n if (policy === undefined || policy.mode === 'off') {\n return next(args, ctx);\n }\n\n const guardArgs = toGuardArgs(args);\n const decision = checkAccess(config.toolName, policy, guardArgs);\n\n if (decision.decision === 'allow') {\n return next(args, ctx);\n }\n if (decision.decision === 'log-and-allow') {\n config.logger.warn('access-policy: audit violation', {\n tool: config.toolName,\n warning: decision.warning,\n policySource: policy.source,\n requestId: ctx.requestContext.requestId,\n });\n return next(args, ctx);\n }\n // decision.decision === 'deny'\n config.logger.info('access-policy: tool call denied', {\n tool: config.toolName,\n reason: decision.reason,\n matchedRule: decision.matchedRule,\n policySource: policy.source,\n mode: policy.mode,\n requestId: ctx.requestContext.requestId,\n });\n return denyToToolResult(decision, ctx.requestContext.requestId);\n };\n}\n\n/** Extract path from a typed tool-arg record, if present and a string. */\nfunction toGuardArgs(args: unknown): GuardArgs | undefined {\n if (typeof args !== 'object' || args === null) return undefined;\n const path = (args as Record<string, unknown>)['path'];\n return typeof path === 'string' && path.length > 0 ? { path } : undefined;\n}\n","/**\n * Access Constraint Deriver — MCP middleware-chain adapter (#1977 activation).\n *\n * Bridges `createAccessPolicyMiddleware` (which returns a generic\n * `Promise<unknown>`-shaped middleware) to the strongly-typed `Middleware`\n * contract used by `mcp/middleware/middleware-chain.ts`.\n *\n * Mounted into the standard middleware stack so every tool call passes\n * through the ClawGuard enforcer. When no orchestrator has wrapped the\n * call with `withAccessPolicy(...)`, this adapter is a no-op pass-through\n * — runtime behavior is unchanged for callers that don't set up a policy.\n *\n * @module security/access-constraint-deriver/chain-adapter\n */\n\nimport { checkAccess } from './enforcer.js';\nimport { denyToToolResult, getActivePolicy } from './mcp-guard.js';\nimport type { GuardArgs } from './mcp-guard.js';\n// Type-only import: no runtime cycle with mcp/middleware/middleware-chain.ts.\nimport type { Middleware } from '../../mcp/middleware/middleware-chain.js';\n\nfunction toGuardArgs(args: unknown): GuardArgs | undefined {\n if (typeof args !== 'object' || args === null) return undefined;\n const path = (args as Record<string, unknown>)['path'];\n return typeof path === 'string' && path.length > 0 ? { path } : undefined;\n}\n\n/**\n * Builds a middleware-chain-compatible access-policy middleware for\n * `toolName`. Reads the active `TaskAccessPolicy` from `AsyncLocalStorage`\n * (populated by `withAccessPolicy`). When no policy is active OR the\n * policy is in `off` mode, the middleware is a no-op pass-through.\n */\nexport function createAccessPolicyChainMiddleware(toolName: string): Middleware {\n return async (args, ctx, next) => {\n const policy = getActivePolicy();\n if (policy === undefined || policy.mode === 'off') {\n return next(args, ctx);\n }\n\n const decision = checkAccess(toolName, policy, toGuardArgs(args));\n\n if (decision.decision === 'allow') {\n return next(args, ctx);\n }\n if (decision.decision === 'log-and-allow') {\n ctx.logger.warn('access-policy: audit violation', {\n tool: toolName,\n warning: decision.warning,\n policySource: policy.source,\n requestId: ctx.requestContext.requestId,\n });\n return next(args, ctx);\n }\n ctx.logger.info('access-policy: tool call denied', {\n tool: toolName,\n reason: decision.reason,\n matchedRule: decision.matchedRule,\n policySource: policy.source,\n mode: policy.mode,\n requestId: ctx.requestContext.requestId,\n });\n return denyToToolResult(decision, ctx.requestContext.requestId);\n };\n}\n","/**\n * nexus-agents/mcp - Centralized Middleware Chain\n *\n * Provides a composable middleware chain for MCP tools with guaranteed\n * execution order: auth → validation → policy → rate-limit → timeout → audit\n *\n * @module mcp/middleware/middleware-chain\n * (Source: Issue #189 - Centralized MCP middleware chain)\n */\n\nimport type { z } from 'zod';\nimport type { ILogger } from '../../core/index.js';\nimport { createLogger, getTimeProvider } from '../../core/index.js';\nimport { validateToolInput } from './validation.js';\nimport { RateLimiter, type RateLimiterConfig } from './rate-limiter.js';\nimport { type IPolicyFirewall, type ExecutionMode, createPolicyContext } from './policy.js';\nimport { TimeoutGuard, type TimeoutGuardConfig } from './timeout-guard.js';\nimport { createRequestContext, contextForLogging, type RequestContext } from './request-context.js';\nimport { createMetricsMiddleware } from './tool-metrics.js';\nimport { abortSignalStorage } from '../mcp-notifier.js';\nimport { createAccessPolicyChainMiddleware } from '../../security/access-constraint-deriver/chain-adapter.js';\n\n/**\n * MCP tool result type.\n *\n * This interface is structurally compatible with the MCP SDK's CallToolResult.\n * The content array accepts text content for simplicity, while remaining\n * compatible with the SDK's broader ContentBlock type at runtime.\n */\nexport interface ToolResult {\n /** Content blocks returned by the tool (text content) */\n content: Array<{ type: 'text'; text: string }>;\n /** Whether this represents an error result */\n isError?: boolean;\n /** Structured output for SDK outputSchema validation (Issue #1117) */\n structuredContent?: Record<string, unknown>;\n}\n\n/**\n * Middleware context passed through the chain.\n */\nexport interface MiddlewareContext {\n /** Unique request ID for tracing */\n readonly requestContext: RequestContext;\n /** Logger with request context */\n readonly logger: ILogger;\n /** Validated arguments (set after validation middleware) */\n validatedArgs?: unknown;\n}\n\n/**\n * Middleware function signature.\n * Each middleware receives the context and a next function to call.\n */\nexport type Middleware = (\n args: unknown,\n ctx: MiddlewareContext,\n next: (args: unknown, ctx: MiddlewareContext) => Promise<ToolResult>\n) => Promise<ToolResult>;\n\n/**\n * Configuration for the middleware chain.\n */\nexport interface MiddlewareChainConfig {\n /** Tool name for logging and policy evaluation */\n toolName: string;\n /** Zod schema for input validation (optional) */\n schema?: z.ZodType;\n /** Policy firewall instance (optional) */\n policyFirewall?: IPolicyFirewall | undefined;\n /** Execution mode for policy evaluation */\n executionMode?: ExecutionMode | undefined;\n /** Allowed paths for file operations */\n allowedPaths?: readonly string[] | undefined;\n /** Rate limiter configuration (optional) */\n rateLimiter?: RateLimiterConfig | RateLimiter | undefined;\n /** Timeout configuration (optional) */\n timeout?: TimeoutGuardConfig | undefined;\n /** Logger instance (optional) */\n logger?: ILogger | undefined;\n /** Skip specific middleware steps */\n skip?: MiddlewareSkipConfig | undefined;\n}\n\n/**\n * Configuration for skipping middleware steps.\n */\nexport interface MiddlewareSkipConfig {\n validation?: boolean | undefined;\n policy?: boolean | undefined;\n rateLimit?: boolean | undefined;\n timeout?: boolean | undefined;\n audit?: boolean | undefined;\n /** Skip the ClawGuard access-policy middleware (#1977). */\n accessPolicy?: boolean | undefined;\n}\n\n/**\n * Tool handler function signature.\n */\nexport type ToolHandler = (args: unknown) => Promise<ToolResult>;\n\n/**\n * Context-aware handler that receives middleware context.\n */\nexport type ContextAwareToolHandler = (\n args: unknown,\n ctx: MiddlewareContext\n) => Promise<ToolResult>;\n\n/**\n * Creates an error result with MCP format.\n */\nfunction errorResult(message: string, requestId: string): ToolResult {\n return {\n isError: true,\n content: [{ type: 'text', text: `${message} (request: ${requestId})` }],\n };\n}\n\n/**\n * Creates validation middleware.\n */\nfunction createValidationMiddleware(schema: z.ZodType): Middleware {\n return async (args, ctx, next) => {\n const result = validateToolInput(schema, args);\n if (!result.ok) {\n ctx.logger.warn('Validation failed', {\n error: result.error.message,\n });\n return errorResult(`Validation error: ${result.error.message}`, ctx.requestContext.requestId);\n }\n ctx.validatedArgs = result.value;\n return next(result.value, ctx);\n };\n}\n\n/**\n * Creates policy middleware.\n */\nfunction createPolicyMiddleware(\n firewall: IPolicyFirewall,\n toolName: string,\n mode: ExecutionMode,\n allowedPaths?: readonly string[]\n): Middleware {\n return async (args, ctx, next) => {\n const policyCtx = createPolicyContext(toolName, args, {\n mode,\n ...(allowedPaths !== undefined && { allowedPaths }),\n });\n const decision = firewall.evaluate(policyCtx);\n\n if (!decision.allowed) {\n ctx.logger.warn('Policy denied', {\n reason: decision.reason,\n ruleName: decision.ruleName,\n });\n return errorResult(`Policy denied: ${decision.reason}`, ctx.requestContext.requestId);\n }\n ctx.logger.debug('Policy check passed', { reason: decision.reason });\n return next(args, ctx);\n };\n}\n\n/**\n * Creates rate limit middleware.\n */\nfunction createRateLimitMiddleware(limiter: RateLimiter): Middleware {\n return async (args, ctx, next) => {\n const acquired = limiter.tryAcquire();\n if (!acquired) {\n const state = limiter.getState();\n ctx.logger.warn('Rate limit exceeded', {\n nextTokenMs: state.nextTokenMs,\n });\n return errorResult(\n `Rate limit exceeded. Try again in ${String(state.nextTokenMs)}ms`,\n ctx.requestContext.requestId\n );\n }\n return next(args, ctx);\n };\n}\n\n/**\n * Creates timeout middleware.\n * Reads AbortSignal from AsyncLocalStorage for client cancellation support.\n */\nfunction createTimeoutMiddleware(guard: TimeoutGuard, toolName: string): Middleware {\n return async (args, ctx, next) => {\n const signal = abortSignalStorage.getStore();\n const result = await guard.execute(() => next(args, ctx), {\n operationName: toolName,\n ...(signal !== undefined ? { signal } : {}),\n });\n\n if (!result.ok) {\n ctx.logger.error('Operation timed out', undefined, {\n code: result.error.code,\n timeoutMs: result.error.timeoutMs,\n });\n return errorResult(result.error.message, ctx.requestContext.requestId);\n }\n\n if (result.value.nearTimeout) {\n ctx.logger.warn('Operation completed near timeout threshold', {\n durationMs: result.value.durationMs,\n });\n }\n\n return result.value.value;\n };\n}\n\n/**\n * Creates audit middleware that logs start/end of request.\n */\nfunction createAuditMiddleware(): Middleware {\n return async (args, ctx, next) => {\n const startTime = getTimeProvider().now();\n ctx.logger.info('Tool invocation started');\n\n try {\n const result = await next(args, ctx);\n const durationMs = getTimeProvider().now() - startTime;\n\n if (result.isError === true) {\n ctx.logger.warn('Tool execution completed with error', { durationMs });\n } else {\n ctx.logger.info('Tool execution completed', { durationMs });\n }\n return result;\n } catch (error) {\n const durationMs = getTimeProvider().now() - startTime;\n const message = error instanceof Error ? error.message : 'Unknown error';\n ctx.logger.error('Tool execution failed', error instanceof Error ? error : undefined, {\n durationMs,\n });\n return errorResult(`Internal error: ${message}`, ctx.requestContext.requestId);\n }\n };\n}\n\n/**\n * Composes multiple middleware functions into a single chain.\n */\nfunction composeMiddleware(middlewares: Middleware[]): Middleware {\n return (args, ctx, finalHandler) => {\n const dispatch = (index: number, currentArgs: unknown): Promise<ToolResult> => {\n if (index >= middlewares.length) {\n return finalHandler(currentArgs, ctx);\n }\n const middleware = middlewares[index];\n if (middleware === undefined) {\n return finalHandler(currentArgs, ctx);\n }\n return middleware(currentArgs, ctx, (nextArgs) => dispatch(index + 1, nextArgs));\n };\n return dispatch(0, args);\n };\n}\n\n/** Helper: adds audit middleware if not skipped */\nfunction addAuditMiddleware(middlewares: Middleware[], skip: MiddlewareSkipConfig): void {\n if (skip.audit !== true) {\n middlewares.push(createAuditMiddleware());\n }\n}\n\n/** Helper: adds rate limit middleware if configured */\nfunction addRateLimitMiddleware(\n middlewares: Middleware[],\n config: MiddlewareChainConfig,\n skip: MiddlewareSkipConfig\n): void {\n if (skip.rateLimit !== true && config.rateLimiter !== undefined) {\n const limiter =\n config.rateLimiter instanceof RateLimiter\n ? config.rateLimiter\n : new RateLimiter(config.rateLimiter);\n middlewares.push(createRateLimitMiddleware(limiter));\n }\n}\n\n/** Helper: adds validation middleware if schema provided */\nfunction addValidationMiddleware(\n middlewares: Middleware[],\n config: MiddlewareChainConfig,\n skip: MiddlewareSkipConfig\n): void {\n if (skip.validation !== true && config.schema !== undefined) {\n middlewares.push(createValidationMiddleware(config.schema));\n }\n}\n\n/** Helper: adds policy middleware if configured */\nfunction addPolicyMiddleware(\n middlewares: Middleware[],\n config: MiddlewareChainConfig,\n skip: MiddlewareSkipConfig\n): void {\n if (skip.policy !== true && config.policyFirewall !== undefined) {\n const mode = config.executionMode ?? 'read-only';\n middlewares.push(\n createPolicyMiddleware(config.policyFirewall, config.toolName, mode, config.allowedPaths)\n );\n }\n}\n\n/** Helper: adds timeout middleware if configured */\nfunction addTimeoutMiddleware(\n middlewares: Middleware[],\n config: MiddlewareChainConfig,\n skip: MiddlewareSkipConfig\n): void {\n if (skip.timeout !== true && config.timeout !== undefined) {\n const guard = new TimeoutGuard(config.timeout);\n middlewares.push(createTimeoutMiddleware(guard, config.toolName));\n }\n}\n\n/**\n * Helper: adds the ClawGuard access-policy middleware (#1977).\n *\n * Always added unless explicitly skipped. The middleware is ALS-backed\n * and a no-op pass-through when no orchestrator has called\n * `withAccessPolicy(...)` — so runtime behavior is unchanged for callers\n * that haven't opted in.\n */\nfunction addAccessPolicyMiddleware(\n middlewares: Middleware[],\n config: MiddlewareChainConfig,\n skip: MiddlewareSkipConfig\n): void {\n if (skip.accessPolicy !== true) {\n middlewares.push(createAccessPolicyChainMiddleware(config.toolName));\n }\n}\n\n/** Helper: builds the middleware stack */\nfunction buildMiddlewareStack(config: MiddlewareChainConfig): Middleware[] {\n const skip = config.skip ?? {};\n const middlewares: Middleware[] = [];\n\n middlewares.push(createMetricsMiddleware()); // Tool usage analytics (#1022)\n addAuditMiddleware(middlewares, skip);\n addRateLimitMiddleware(middlewares, config, skip);\n addValidationMiddleware(middlewares, config, skip);\n addPolicyMiddleware(middlewares, config, skip);\n addAccessPolicyMiddleware(middlewares, config, skip); // #1977 ClawGuard\n addTimeoutMiddleware(middlewares, config, skip);\n\n return middlewares;\n}\n\n/**\n * Creates a middleware chain with the standard execution order.\n *\n * Order: audit → rate-limit → validation → policy → timeout → handler\n *\n * Audit wraps everything to capture timing. Rate limit is checked early\n * to reject requests before expensive validation. Timeout wraps the\n * actual handler execution.\n *\n * @param config - Chain configuration\n * @returns A function that wraps handlers with the middleware chain\n */\nexport function createMiddlewareChain(\n config: MiddlewareChainConfig\n): (handler: ContextAwareToolHandler) => ToolHandler {\n const logger = config.logger ?? createLogger({ tool: config.toolName });\n const middlewares = buildMiddlewareStack(config);\n const composed = composeMiddleware(middlewares);\n\n return (handler: ContextAwareToolHandler): ToolHandler => {\n return async (args: unknown): Promise<ToolResult> => {\n const requestContext = createRequestContext({ toolName: config.toolName });\n const requestLogger = logger.child(contextForLogging(requestContext));\n const ctx: MiddlewareContext = { requestContext, logger: requestLogger };\n return composed(args, ctx, (finalArgs, finalCtx) => handler(finalArgs, finalCtx));\n };\n };\n}\n\n/**\n * Convenience function to wrap a handler with default middleware.\n *\n * @param toolName - Name of the tool\n * @param handler - The tool handler\n * @param options - Optional middleware configuration\n * @returns Wrapped handler with middleware\n */\nexport function withMiddleware(\n toolName: string,\n handler: ContextAwareToolHandler | ToolHandler,\n options?: Partial<Omit<MiddlewareChainConfig, 'toolName'>>\n): ToolHandler {\n // Note: Policy firewall is NOT added by default - must be explicitly configured\n // This follows the principle of minimal defaults with explicit opt-in for security\n const config: MiddlewareChainConfig = {\n toolName,\n ...options,\n };\n\n const chain = createMiddlewareChain(config);\n\n // Wrap handler to support both signatures\n const wrappedHandler: ContextAwareToolHandler = (args, ctx) => {\n // Check if handler expects context (2 params)\n if (handler.length >= 2) {\n return handler(args, ctx);\n }\n return (handler as ToolHandler)(args);\n };\n\n return chain(wrappedHandler);\n}\n\n/**\n * Creates a middleware chain factory with shared configuration.\n *\n * @param sharedConfig - Configuration shared across all tools\n * @returns Factory function for creating wrapped handlers\n */\nexport function createMiddlewareFactory(\n sharedConfig: Omit<MiddlewareChainConfig, 'toolName' | 'schema'>\n): (\n toolName: string,\n handler: ContextAwareToolHandler | ToolHandler,\n schema?: z.ZodType\n) => ToolHandler {\n return (toolName, handler, schema) => {\n const options = schema !== undefined ? { ...sharedConfig, schema } : sharedConfig;\n return withMiddleware(toolName, handler, options);\n };\n}\n","/**\n * nexus-agents/mcp - Tool Wrapper Helper\n *\n * Provides a convenient wrapper for MCP tools that automatically applies\n * the middleware chain with timeout protection (CVE-2026-0621 mitigation).\n *\n * @module mcp/middleware/tool-wrapper\n * (Source: Issue #271, CVE-2026-0621 mitigation)\n */\n\nimport type { ILogger } from '../../core/index.js';\nimport type { TimeoutConfig, SecurityConfig } from '../../config/schemas.js';\nimport type { IPolicyFirewall, ExecutionMode } from './policy.js';\nimport type { RateLimiterConfig } from './rate-limiter.js';\nimport { RateLimiter } from './rate-limiter.js';\nimport {\n withMiddleware,\n createMiddlewareFactory,\n type ToolHandler,\n type ContextAwareToolHandler,\n type MiddlewareChainConfig,\n} from './middleware-chain.js';\nimport { MCP_TIMEOUTS } from '../../config/timeouts.js';\nimport {\n progressContextStorage,\n abortSignalStorage,\n type ProgressContext,\n} from '../mcp-notifier.js';\nimport { createLogger as createInternalLogger, getErrorMessage } from '../../core/index.js';\n\n/**\n * Default timeout configuration.\n * Values sourced from config/timeouts.ts (Issue #984).\n */\nexport const DEFAULT_TIMEOUT_CONFIG: TimeoutConfig = {\n defaultTimeoutMs: MCP_TIMEOUTS.defaultMs,\n maxTimeoutMs: MCP_TIMEOUTS.maxMs,\n enableLogging: true,\n uriValidation: true,\n};\n\n/**\n * Default per-tool timeout overrides.\n * Sourced from config/timeouts.ts (Issue #984).\n */\nexport const DEFAULT_TOOL_TIMEOUTS: Record<string, number> = {\n ...MCP_TIMEOUTS.perTool,\n};\n\n/**\n * Resolves the timeout for a specific tool.\n * Priority: explicit override > security config perToolTimeout > DEFAULT_TOOL_TIMEOUTS > global default.\n * (Issue #657 - Per-tool timeout configuration)\n */\nexport function getToolTimeout(\n toolName: string,\n security?: SecurityConfig,\n explicitMs?: number\n): number {\n // Explicit override takes highest priority\n if (explicitMs !== undefined) {\n return explicitMs;\n }\n // Check security config per-tool overrides\n const perToolConfig = security?.timeout?.perToolTimeout;\n const perToolMs = perToolConfig?.[toolName];\n if (perToolMs !== undefined) {\n return perToolMs;\n }\n // Check built-in per-tool defaults\n const builtInDefault = DEFAULT_TOOL_TIMEOUTS[toolName];\n if (builtInDefault !== undefined) {\n return builtInDefault;\n }\n // Fall back to global default\n return security?.timeout?.defaultTimeoutMs ?? DEFAULT_TIMEOUT_CONFIG.defaultTimeoutMs;\n}\n\n/**\n * Configuration for creating a tool factory.\n */\nexport interface ToolFactoryConfig {\n /** Logger instance */\n logger?: ILogger | undefined;\n /** Security configuration (includes timeout config) */\n security?: SecurityConfig | undefined;\n /** Policy firewall instance */\n policyFirewall?: IPolicyFirewall | undefined;\n /** Rate limiter configuration */\n rateLimiter?: RateLimiterConfig | RateLimiter | undefined;\n /** Allowed paths for file operations */\n allowedPaths?: readonly string[] | undefined;\n}\n\n/**\n * Per-tool configuration options.\n */\nexport interface ToolWrapperOptions {\n /** Execution mode for policy evaluation (default: 'read-only') */\n executionMode?: ExecutionMode | undefined;\n /** Custom timeout in ms (overrides default) */\n timeoutMs?: number | undefined;\n /** Skip timeout protection (use sparingly) */\n skipTimeout?: boolean | undefined;\n /** Skip rate limiting */\n skipRateLimit?: boolean | undefined;\n}\n\n/**\n * Gets timeout configuration from security config or uses defaults.\n */\nfunction getTimeoutConfig(\n security?: SecurityConfig,\n overrideMs?: number\n): MiddlewareChainConfig['timeout'] {\n const timeoutConfig = security?.timeout ?? DEFAULT_TIMEOUT_CONFIG;\n\n return {\n defaultTimeoutMs: overrideMs ?? timeoutConfig.defaultTimeoutMs,\n maxTimeoutMs: timeoutConfig.maxTimeoutMs,\n enableLogging: timeoutConfig.enableLogging,\n };\n}\n\n/**\n * Creates a tool factory with shared configuration.\n *\n * This factory produces wrapped handlers that include timeout protection,\n * rate limiting, and other middleware as configured.\n *\n * @example\n * ```typescript\n * const wrapTool = createToolFactory({\n * security: appConfig.security,\n * rateLimiter: { capacity: 100, refillRate: 10 },\n * });\n *\n * const handler = wrapTool('my_tool', async (args) => {\n * // Your tool logic here\n * return { content: [{ type: 'text', text: 'Done' }] };\n * });\n * ```\n */\nexport function createToolFactory(\n config: ToolFactoryConfig\n): (\n toolName: string,\n handler: ContextAwareToolHandler | ToolHandler,\n options?: ToolWrapperOptions\n) => ToolHandler {\n const { security, policyFirewall, rateLimiter, allowedPaths, logger } = config;\n\n return (toolName, handler, options) => {\n const skip = {\n timeout: options?.skipTimeout,\n rateLimit: options?.skipRateLimit,\n };\n\n const chainConfig: Omit<MiddlewareChainConfig, 'toolName'> = {\n logger,\n policyFirewall,\n executionMode: options?.executionMode ?? 'read-only',\n allowedPaths,\n rateLimiter,\n timeout: skip.timeout === true ? undefined : getTimeoutConfig(security, options?.timeoutMs),\n skip,\n };\n\n return withMiddleware(toolName, handler, chainConfig);\n };\n}\n\n/**\n * Wraps a single tool handler with timeout protection.\n *\n * This is a convenience function for simple cases where you don't need\n * the full factory setup.\n *\n * @example\n * ```typescript\n * const handler = wrapToolWithTimeout('my_tool', async (args) => {\n * return { content: [{ type: 'text', text: 'Done' }] };\n * });\n * ```\n */\nexport function wrapToolWithTimeout(\n toolName: string,\n handler: ContextAwareToolHandler | ToolHandler,\n options?: {\n timeoutMs?: number;\n logger?: ILogger;\n }\n): ToolHandler {\n return withMiddleware(toolName, handler, {\n timeout: getTimeoutConfig(undefined, options?.timeoutMs),\n logger: options?.logger,\n });\n}\n\n/** Shape of the MCP SDK's extra._meta for progress tokens. */\ninterface SdkMeta {\n readonly progressToken?: string | number;\n}\n\n/** Shape of the MCP SDK's extra object passed to tool handlers. */\ninterface SdkExtra {\n readonly _meta?: SdkMeta;\n readonly signal?: AbortSignal;\n readonly sendNotification?: (notification: {\n method: string;\n params?: Record<string, unknown>;\n }) => Promise<void>;\n}\n\nconst wrapperLogger = createInternalLogger({ component: 'tool-wrapper' });\n\n/** Extract progress context from MCP SDK extra if progressToken present. */\nfunction extractProgressContext(extra: unknown): ProgressContext | undefined {\n const sdk = extra as SdkExtra | undefined;\n const token = sdk?._meta?.progressToken;\n const sendFn = sdk?.sendNotification;\n if (token === undefined || sendFn === undefined) return undefined;\n\n return {\n progressToken: token,\n sendNotification: (progress: number, total?: number) => {\n const params: Record<string, unknown> = {\n progressToken: token,\n progress,\n };\n if (total !== undefined) params['total'] = total;\n sendFn({ method: 'notifications/progress', params }).catch((err: unknown) => {\n wrapperLogger.debug('Failed to send progress notification', {\n error: getErrorMessage(err),\n });\n });\n },\n };\n}\n\n/**\n * Runs handler within nested AsyncLocalStorage contexts for progress + abort.\n */\n/** SDK-compatible tool result with optional structuredContent (Issue #1117). */\ntype SdkToolResult = {\n content: Array<{ type: 'text'; text: string }>;\n isError?: boolean;\n structuredContent?: Record<string, unknown>;\n};\n\nfunction runWithContexts(\n handler: ToolHandler,\n args: unknown,\n progressCtx: ProgressContext | undefined,\n signal: AbortSignal | undefined\n): Promise<SdkToolResult> {\n const run = (): Promise<SdkToolResult> => handler(args);\n\n // Nest contexts: abort signal outer, progress inner\n if (signal !== undefined && progressCtx !== undefined) {\n return abortSignalStorage.run(signal, () => progressContextStorage.run(progressCtx, run));\n }\n if (signal !== undefined) {\n return abortSignalStorage.run(signal, run);\n }\n if (progressCtx !== undefined) {\n return progressContextStorage.run(progressCtx, run);\n }\n return run();\n}\n\n/**\n * Adapts a ToolHandler to the MCP SDK's expected callback signature.\n *\n * Extracts progressToken and AbortSignal from extra, runs the handler\n * within AsyncLocalStorage contexts so middleware can access them.\n *\n * @param handler - Our internal ToolHandler\n * @returns SDK-compatible callback function\n */\nexport function toSdkCallback(\n handler: ToolHandler\n): (args: unknown, extra: unknown) => Promise<SdkToolResult> {\n return (args: unknown, extra: unknown) => {\n const progressCtx = extractProgressContext(extra);\n const signal = (extra as SdkExtra | undefined)?.signal;\n return runWithContexts(handler, args, progressCtx, signal);\n };\n}\n\n/**\n * Re-export middleware factory for advanced use cases.\n */\nexport { createMiddlewareFactory, withMiddleware };\n","/**\n * nexus-agents/audit - SecureHandler Audit Integration\n *\n * Integration helper to add audit logging to SecureHandler middleware.\n *\n * (Source: Issue #193 - Phase 3 structured audit logging)\n *\n * @module audit/secure-handler-audit\n */\n\nimport type { IAuditLogger, AuditActor, AuditOutcome } from './audit-types.js';\nimport type { RequestContext } from '../mcp/middleware/request-context.js';\n\n/**\n * Configuration for audit-enabled secure handler.\n */\nexport interface AuditHandlerConfig {\n /** Audit logger instance */\n auditLogger: IAuditLogger;\n /** Default actor for requests without caller info */\n defaultActor?: AuditActor | undefined;\n}\n\n/**\n * Creates an AuditActor from RequestContext.\n */\nexport function actorFromContext(ctx: RequestContext, fallback?: AuditActor): AuditActor {\n const caller = ctx.caller;\n const clientId = caller.clientId;\n if (clientId !== undefined && clientId.length > 0) {\n return {\n type: clientId.includes('api') ? 'external' : clientId.includes('cli') ? 'agent' : 'user',\n id: clientId,\n name: caller.userAgent,\n };\n }\n return fallback ?? { type: 'system', id: 'unknown', name: 'Unknown Caller' };\n}\n\n/**\n * Maps tool result to audit outcome.\n */\nexport function resultToOutcome(\n isError: boolean | undefined,\n isPolicyDenied: boolean\n): AuditOutcome {\n if (isPolicyDenied) return 'denied';\n if (isError === true) return 'failure';\n return 'success';\n}\n\n/** Options for logging tool invocation audit */\nexport interface LogToolInvocationOpts {\n auditLogger: IAuditLogger;\n toolName: string;\n outcome: AuditOutcome;\n actor: AuditActor;\n requestId: string;\n durationMs?: number | undefined;\n errorMessage?: string | undefined;\n}\n\n/**\n * Logs tool invocation to audit logger.\n */\nexport function logToolInvocationAudit(opts: LogToolInvocationOpts): void {\n opts.auditLogger.logToolInvocation({\n toolName: opts.toolName,\n outcome: opts.outcome,\n actor: opts.actor,\n requestId: opts.requestId,\n durationMs: opts.durationMs,\n errorMessage: opts.errorMessage,\n });\n}\n\n/** Options for logging policy audit */\nexport interface LogPolicyAuditOpts {\n auditLogger: IAuditLogger;\n policyName: string;\n decision: 'allow' | 'deny';\n reason: string;\n toolName: string;\n actor: AuditActor;\n requestId: string;\n}\n\n/**\n * Logs policy decision to audit logger.\n */\nexport function logPolicyAudit(opts: LogPolicyAuditOpts): void {\n opts.auditLogger.logPolicyDecision({\n policyName: opts.policyName,\n decision: opts.decision,\n reason: opts.reason,\n toolName: opts.toolName,\n actor: opts.actor,\n requestId: opts.requestId,\n });\n}\n\n/** Options for logging rate limit audit */\nexport interface LogRateLimitAuditOpts {\n auditLogger: IAuditLogger;\n toolName: string;\n actor: AuditActor;\n currentRate: number;\n limitRate: number;\n requestId: string;\n}\n\n/**\n * Logs rate limit violation to audit logger.\n */\nexport function logRateLimitAudit(opts: LogRateLimitAuditOpts): void {\n opts.auditLogger.logRateLimitViolation({\n toolName: opts.toolName,\n actor: opts.actor,\n currentRate: opts.currentRate,\n limitRate: opts.limitRate,\n requestId: opts.requestId,\n });\n}\n","/**\n * nexus-agents/mcp - Tool Input Sanitizer Middleware\n *\n * Lightweight sanitization for MCP tool arguments. Strips XML-like\n * conversation injection tags and detects prompt injection patterns\n * in all string values within tool arguments.\n *\n * Defense-in-depth layer that protects against prompt injection\n * through tool arguments containing external content.\n *\n * @module mcp/middleware/tool-input-sanitizer\n * (Source: Issue #828 — Wire security modules into production pipeline)\n */\n\nimport type { ILogger } from '../../core/index.js';\n\n/**\n * Result of sanitizing tool input.\n */\nexport interface SanitizeToolInputResult {\n /** Sanitized arguments (XML tags stripped from string values) */\n readonly sanitized: unknown;\n /** Whether any modification was made */\n readonly wasModified: boolean;\n /** Count of strings that were modified */\n readonly modifiedCount: number;\n /** Injection patterns detected (for logging) */\n readonly detectedPatterns: readonly string[];\n}\n\n/**\n * XML-like tags that mimic conversation structure or system prompts.\n * Stripping these prevents prompt injection through tool arguments.\n */\nconst XML_INJECTION_PATTERN =\n /<\\/?(system|human|assistant|instructions|user|prompt|context|tool_use|tool_result)\\b[^>]*>/gi;\n\n/**\n * Patterns that indicate attempted prompt injection.\n * These are logged but not necessarily stripped (detection only).\n */\nconst INJECTION_DETECTORS: ReadonlyArray<{ name: string; pattern: RegExp }> = [\n { name: 'system_prompt_override', pattern: /ignore (?:all )?previous (?:instructions|rules)/i },\n { name: 'role_impersonation', pattern: /i(?:'m| am) the (?:repo |project )?(?:owner|admin)/i },\n { name: 'hidden_instruction', pattern: /<!--[\\s\\S]*?(?:execute|delete|merge|apply)[\\s\\S]*?-->/i },\n];\n\n/**\n * Sanitizes a single string value by stripping XML injection tags.\n * Returns the cleaned string and whether it was modified.\n */\nfunction sanitizeString(value: string): { cleaned: string; modified: boolean } {\n XML_INJECTION_PATTERN.lastIndex = 0;\n const cleaned = value.replace(XML_INJECTION_PATTERN, '');\n return { cleaned, modified: cleaned !== value };\n}\n\n/**\n * Detects injection patterns in a string without modifying it.\n */\nfunction detectPatterns(value: string): string[] {\n const detected: string[] = [];\n for (const { name, pattern } of INJECTION_DETECTORS) {\n pattern.lastIndex = 0;\n if (pattern.test(value)) {\n detected.push(name);\n }\n }\n return detected;\n}\n\n/**\n * Recursively sanitizes all string values in an object/array.\n * Returns a deep copy with XML injection tags stripped from strings.\n */\nfunction sanitizeValue(value: unknown, stats: { count: number; patterns: string[] }): unknown {\n if (typeof value === 'string') {\n const patterns = detectPatterns(value);\n if (patterns.length > 0) {\n stats.patterns.push(...patterns);\n }\n const { cleaned, modified } = sanitizeString(value);\n if (modified) stats.count++;\n return cleaned;\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => sanitizeValue(item, stats));\n }\n\n if (value !== null && typeof value === 'object') {\n const result: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(value as Record<string, unknown>)) {\n result[key] = sanitizeValue(val, stats);\n }\n return result;\n }\n\n return value;\n}\n\n/**\n * Sanitizes MCP tool arguments by stripping XML injection tags\n * from all string values and detecting injection patterns.\n *\n * @param args - Tool arguments to sanitize\n * @returns Sanitized result with modification tracking\n */\nexport function sanitizeToolInput(args: unknown): SanitizeToolInputResult {\n if (args === undefined || args === null) {\n return { sanitized: args, wasModified: false, modifiedCount: 0, detectedPatterns: [] };\n }\n\n const stats = { count: 0, patterns: [] as string[] };\n const sanitized = sanitizeValue(args, stats);\n const uniquePatterns = [...new Set(stats.patterns)];\n\n return {\n sanitized,\n wasModified: stats.count > 0,\n modifiedCount: stats.count,\n detectedPatterns: uniquePatterns,\n };\n}\n\n/**\n * Logs sanitization results when modifications or detections occur.\n */\nexport function logSanitizationResult(\n result: SanitizeToolInputResult,\n logger: ILogger,\n toolName: string\n): void {\n if (result.wasModified) {\n logger.warn('Tool input sanitized — XML injection tags stripped', {\n tool: toolName,\n modifiedFields: result.modifiedCount,\n });\n }\n if (result.detectedPatterns.length > 0) {\n logger.warn('Injection patterns detected in tool input', {\n tool: toolName,\n patterns: result.detectedPatterns,\n });\n }\n}\n","/**\n * nexus-agents/mcp - Secure Handler Middleware\n *\n * Higher-order function that wraps MCP tool handlers with security middleware:\n * - RequestContext creation and tracking\n * - PolicyFirewall evaluation\n * - Logging with request context\n *\n * (Source: Issue #185 Phase 1 - PolicyFirewall integration)\n *\n * @module mcp/middleware/secure-handler\n */\n\nimport type { ILogger } from '../../core/index.js';\nimport { createLogger, getTimeProvider } from '../../core/index.js';\nimport {\n createRequestContext,\n contextForLogging,\n type RequestContext,\n type CallerInfo,\n} from './request-context.js';\nimport { type IPolicyFirewall, type ExecutionMode, createPolicyContext } from './policy.js';\nimport type { RateLimiter } from './rate-limiter.js';\nimport type { IAuditLogger } from '../../audit/audit-types.js';\nimport { actorFromContext, resultToOutcome } from '../../audit/secure-handler-audit.js';\nimport {\n sanitizeToolInput,\n logSanitizationResult,\n type SanitizeToolInputResult,\n} from './tool-input-sanitizer.js';\nimport type { ToolResult } from '../tools/tool-result.js';\n\nexport type { ToolResult };\n\n/**\n * Tool handler function signature.\n */\nexport type ToolHandler = (args: unknown) => Promise<ToolResult>;\n\n/**\n * Security tier for MCP tools. Controls input validation strictness.\n *\n * - 'standard': Default. XML injection tag stripping only (existing behavior).\n * - 'user-facing': Accepts user task descriptions. Rejects known injection patterns.\n * - 'external': Processes external URLs/content. Strictest validation.\n *\n * @see Issue #1586 — Tiered security validation\n */\nexport type SecurityTier = 'standard' | 'user-facing' | 'external';\n\n/**\n * Configuration for the secure handler wrapper.\n */\nexport interface SecureHandlerConfig {\n /** Tool name for logging and policy evaluation */\n toolName: string;\n /** Security tier controlling input validation strictness (default: 'standard') */\n securityTier?: SecurityTier;\n /** Policy firewall instance (optional - if not provided, policy checks are skipped) */\n policyFirewall?: IPolicyFirewall;\n /** Execution mode for policy evaluation */\n executionMode?: ExecutionMode;\n /** Allowed paths for file operations */\n allowedPaths?: readonly string[];\n /** Rate limiter instance (optional) */\n rateLimiter?: RateLimiter;\n /** Logger instance (optional - creates default if not provided) */\n logger?: ILogger;\n /** Caller information extractor (optional) */\n callerInfo?: CallerInfo;\n /** Audit logger for structured audit trail (Issue #740 Phase 2) */\n auditLogger?: IAuditLogger;\n}\n\n/**\n * Extended handler context passed to the wrapped handler.\n */\nexport interface HandlerContext {\n /** Request context for this invocation */\n requestContext: RequestContext;\n /** Logger with request context attached */\n logger: ILogger;\n}\n\n/**\n * Tool handler with context signature.\n */\nexport type ContextAwareHandler = (args: unknown, ctx: HandlerContext) => Promise<ToolResult>;\n\n/**\n * Creates a rate limit error response.\n */\nfunction rateLimitError(nextTokenMs: number): ToolResult {\n return {\n isError: true,\n content: [\n {\n type: 'text',\n text: `Rate limit exceeded. Try again in ${String(nextTokenMs)}ms.`,\n },\n ],\n };\n}\n\n/**\n * Creates a policy denial error response.\n */\nfunction policyDeniedError(reason: string, requestId: string): ToolResult {\n return {\n isError: true,\n content: [{ type: 'text', text: `Policy denied: ${reason} (request: ${requestId})` }],\n };\n}\n\n/**\n * Creates an internal error response.\n */\nfunction internalError(message: string, requestId: string): ToolResult {\n return {\n isError: true,\n content: [{ type: 'text', text: `Internal error: ${message} (request: ${requestId})` }],\n };\n}\n\n/**\n * Maximum input size for tool arguments (10MB).\n * Prevents memory exhaustion from oversized payloads.\n * (Source: Issue #740 - MCP security hardening)\n */\nconst MAX_INPUT_SIZE_BYTES = 10 * 1024 * 1024;\n\n/**\n * Patterns that indicate leaked secrets in tool output.\n * Each pattern is tested against tool response text.\n */\nconst SECRET_PATTERNS: readonly RegExp[] = [\n // API keys with common prefixes\n /\\b(sk-[a-zA-Z0-9]{20,})\\b/,\n /\\b(pk-[a-zA-Z0-9]{20,})\\b/,\n // AWS-style keys\n /\\b(AKIA[A-Z0-9]{16})\\b/,\n // Bearer tokens in output\n /Bearer\\s+[a-zA-Z0-9_\\-.~+/]+=*/,\n // Generic long hex secrets (40+ chars)\n /\\b[0-9a-f]{40,}\\b/i,\n // password= or token= in output\n /(?:password|token|secret|apikey|api_key)\\s*[=:]\\s*\\S{8,}/i,\n];\n\n/** Redact detected secrets from tool output text. */\nfunction sanitizeOutput(text: string, logger: ILogger): string {\n let sanitized = text;\n for (const pattern of SECRET_PATTERNS) {\n if (pattern.test(sanitized)) {\n logger.warn('Potential secret detected in tool output, redacting', {\n pattern: pattern.source.slice(0, 30),\n });\n sanitized = sanitized.replace(pattern, '[REDACTED]');\n }\n }\n return sanitized;\n}\n\n/** Sanitize all text content in a tool result (Issue #740). */\nfunction sanitizeToolResult(result: ToolResult, logger: ILogger): void {\n for (const item of result.content) {\n item.text = sanitizeOutput(item.text, logger);\n }\n}\n\n/** Validates input size and returns error if too large. */\nfunction checkInputSize(args: unknown, logger: ILogger, requestId: string): ToolResult | null {\n if (args === undefined) return null;\n const inputSize = JSON.stringify(args).length;\n if (inputSize > MAX_INPUT_SIZE_BYTES) {\n logger.warn('Input size exceeds limit', { inputSize, limit: MAX_INPUT_SIZE_BYTES });\n return internalError('Input too large', requestId);\n }\n return null;\n}\n\n/**\n * Checks rate limiter and returns error if exceeded.\n */\nfunction checkRateLimit(rateLimiter: RateLimiter, logger: ILogger): ToolResult | null {\n const acquired = rateLimiter.tryAcquire();\n if (!acquired) {\n const state = rateLimiter.getState();\n logger.warn('Rate limit exceeded');\n return rateLimitError(state.nextTokenMs);\n }\n return null;\n}\n\n/** Options for policy check */\ninterface PolicyCheckOptions {\n firewall: IPolicyFirewall;\n toolName: string;\n args: unknown;\n mode: ExecutionMode;\n allowedPaths?: readonly string[] | undefined;\n logger: ILogger;\n requestId: string;\n}\n\n/**\n * Evaluates policy firewall and returns error if denied.\n */\nfunction checkPolicy(opts: PolicyCheckOptions): ToolResult | null {\n const ctxOpts = {\n mode: opts.mode,\n ...(opts.allowedPaths && { allowedPaths: opts.allowedPaths }),\n };\n const decision = opts.firewall.evaluate(createPolicyContext(opts.toolName, opts.args, ctxOpts));\n\n if (!decision.allowed) {\n opts.logger.warn('Policy denied tool execution', {\n reason: decision.reason,\n ruleName: decision.ruleName,\n });\n return policyDeniedError(decision.reason, opts.requestId);\n }\n opts.logger.debug('Policy check passed', { reason: decision.reason });\n return null;\n}\n\n/**\n * Executes handler and logs result.\n */\nasync function executeHandler(\n handler: ToolHandler | ContextAwareHandler,\n args: unknown,\n ctx: HandlerContext,\n logger: ILogger\n): Promise<ToolResult> {\n const startTime = getTimeProvider().now();\n const result =\n handler.length >= 2 ? await handler(args, ctx) : await (handler as ToolHandler)(args);\n\n const durationMs = getTimeProvider().now() - startTime;\n if (result.isError === true) {\n logger.warn('Tool execution completed with error', { durationMs });\n } else {\n logger.info('Tool execution completed', { durationMs });\n }\n return result;\n}\n\n/** Emits an audit event for a completed tool invocation. */\nfunction emitToolAudit(\n auditLogger: IAuditLogger,\n toolName: string,\n ctx: RequestContext,\n result: ToolResult,\n durationMs: number\n): void {\n const actor = actorFromContext(ctx);\n const outcome = resultToOutcome(result.isError, false);\n auditLogger.logToolInvocation({\n toolName,\n outcome,\n actor,\n requestId: ctx.requestId,\n durationMs,\n });\n}\n\n/**\n * Emits an audit event when a tool handler throws (or returns a rejected\n * Promise) — closes the audit-trail gap where unexpected exceptions left\n * no auditor record (security-review fallout from #2191).\n */\nfunction emitToolAuditException(\n auditLogger: IAuditLogger,\n toolName: string,\n ctx: RequestContext,\n durationMs: number\n): void {\n const actor = actorFromContext(ctx);\n auditLogger.logToolInvocation({\n toolName,\n outcome: 'error',\n actor,\n requestId: ctx.requestId,\n durationMs,\n });\n}\n\n/** Emits an audit event for a policy denial. */\nfunction emitPolicyAudit(\n auditLogger: IAuditLogger,\n toolName: string,\n ctx: RequestContext,\n reason: string\n): void {\n const actor = actorFromContext(ctx);\n auditLogger.logPolicyDecision({\n policyName: 'default',\n decision: 'deny',\n reason,\n toolName,\n actor,\n requestId: ctx.requestId,\n });\n}\n\n/** Emits an audit event for a rate limit violation. */\nfunction emitRateLimitAudit(\n auditLogger: IAuditLogger,\n toolName: string,\n ctx: RequestContext\n): void {\n const actor = actorFromContext(ctx);\n auditLogger.logRateLimitViolation({\n toolName,\n actor,\n currentRate: 0,\n limitRate: 0,\n requestId: ctx.requestId,\n });\n}\n\n/** Reject inputs with detected injection patterns for elevated security tiers. */\nfunction checkSecurityTier(\n config: SecureHandlerConfig,\n sanitizeResult: SanitizeToolInputResult,\n logger: ILogger\n): ToolResult | null {\n const tier = config.securityTier ?? 'standard';\n if (tier === 'standard' || sanitizeResult.detectedPatterns.length === 0) {\n return null;\n }\n logger.warn('Input rejected by security tier validation', {\n tier,\n patterns: sanitizeResult.detectedPatterns,\n });\n return {\n isError: true,\n content: [\n {\n type: 'text',\n text:\n `Input validation failed: detected patterns [${sanitizeResult.detectedPatterns.join(', ')}]. ` +\n 'Remove prompt injection patterns and retry.',\n },\n ],\n };\n}\n\n/** Pre-execution checks: input size, input sanitization, rate limit, policy. */\nfunction runPreChecks(\n config: SecureHandlerConfig,\n args: unknown,\n mode: ExecutionMode,\n requestContext: RequestContext,\n logger: ILogger\n): { error: ToolResult | null; sanitizedArgs: unknown } {\n const sizeResult = checkInputSize(args, logger, requestContext.requestId);\n if (sizeResult) return { error: sizeResult, sanitizedArgs: args };\n\n // Sanitize tool input: strip XML injection tags, detect injection patterns (Issue #828)\n const sanitizeResult = sanitizeToolInput(args);\n logSanitizationResult(sanitizeResult, logger, config.toolName);\n const sanitizedArgs = sanitizeResult.wasModified ? sanitizeResult.sanitized : args;\n\n // Tiered validation: reject (not strip) for user-facing/external tools (Issue #1586)\n const tierError = checkSecurityTier(config, sanitizeResult, logger);\n if (tierError !== null) return { error: tierError, sanitizedArgs };\n\n if (config.rateLimiter) {\n const rlResult = checkRateLimit(config.rateLimiter, logger);\n if (rlResult) {\n if (config.auditLogger)\n emitRateLimitAudit(config.auditLogger, config.toolName, requestContext);\n return { error: rlResult, sanitizedArgs };\n }\n }\n\n if (config.policyFirewall) {\n const pResult = checkPolicy({\n firewall: config.policyFirewall,\n toolName: config.toolName,\n args: sanitizedArgs,\n mode,\n allowedPaths: config.allowedPaths,\n logger,\n requestId: requestContext.requestId,\n });\n if (pResult) {\n if (config.auditLogger)\n emitPolicyAudit(config.auditLogger, config.toolName, requestContext, 'policy denied');\n return { error: pResult, sanitizedArgs };\n }\n }\n\n return { error: null, sanitizedArgs };\n}\n\n/**\n * Wraps a tool handler with security middleware.\n *\n * @param handler - The original tool handler or context-aware handler\n * @param config - Security configuration\n * @returns Wrapped handler with security middleware\n */\nexport function createSecureHandler(\n handler: ToolHandler | ContextAwareHandler,\n config: SecureHandlerConfig\n): ToolHandler {\n const logger = config.logger ?? createLogger({ tool: config.toolName });\n const mode = config.executionMode ?? 'read-only';\n\n return async (args: unknown): Promise<ToolResult> => {\n const ctxOpts = {\n toolName: config.toolName,\n ...(config.callerInfo && { caller: config.callerInfo }),\n };\n const requestContext = createRequestContext(ctxOpts);\n const requestLogger = logger.child(contextForLogging(requestContext));\n requestLogger.info('Tool invocation started');\n\n const { error: preCheckError, sanitizedArgs } = runPreChecks(\n config,\n args,\n mode,\n requestContext,\n requestLogger\n );\n if (preCheckError) return preCheckError;\n\n return executeAndAudit(handler, sanitizedArgs, requestContext, requestLogger, config);\n };\n}\n\n/**\n * Executes the wrapped handler with audit emission on both the success and\n * exception paths. Extracted from `createSecureHandler` to keep that\n * function within the 50-line budget.\n */\nasync function executeAndAudit(\n handler: ToolHandler | ContextAwareHandler,\n sanitizedArgs: unknown,\n requestContext: RequestContext,\n requestLogger: ILogger,\n config: SecureHandlerConfig\n): Promise<ToolResult> {\n const execStartTime = getTimeProvider().now();\n try {\n const result = await executeHandler(\n handler,\n sanitizedArgs,\n { requestContext, logger: requestLogger },\n requestLogger\n );\n sanitizeToolResult(result, requestLogger);\n if (config.auditLogger) {\n emitToolAudit(\n config.auditLogger,\n config.toolName,\n requestContext,\n result,\n getTimeProvider().now() - execStartTime\n );\n }\n return result;\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error';\n requestLogger.error('Tool execution failed', error instanceof Error ? error : undefined);\n if (config.auditLogger) {\n emitToolAuditException(\n config.auditLogger,\n config.toolName,\n requestContext,\n getTimeProvider().now() - execStartTime\n );\n }\n return internalError(message, requestContext.requestId);\n }\n}\n\n/**\n * Creates a secure handler factory with shared configuration.\n * Useful for registering multiple tools with the same security settings.\n *\n * @param sharedConfig - Shared security configuration\n * @returns Factory function for creating secure handlers\n */\nexport function createSecureHandlerFactory(\n sharedConfig: Omit<SecureHandlerConfig, 'toolName'>\n): (toolName: string, handler: ToolHandler | ContextAwareHandler) => ToolHandler {\n return (toolName: string, handler: ToolHandler | ContextAwareHandler) =>\n createSecureHandler(handler, { ...sharedConfig, toolName });\n}\n","/**\n * nexus-agents/mcp - Tool Result Helpers\n *\n * Canonical type and factory functions for MCP tool results.\n * Extracted from index.ts to allow tool implementations to import\n * without circular dependencies.\n *\n * @module mcp/tools/tool-result\n */\n\nimport type { ILogger } from '../../core/index.js';\nimport type { RateLimiter } from '../middleware/rate-limiter.js';\nimport type { SecurityConfig } from '../../config/schemas.js';\n\n// ============================================================================\n// Base Dependencies\n// ============================================================================\n\n/**\n * Common dependency interface shared by all MCP tool handlers.\n *\n * Tool-specific deps interfaces should extend this base.\n * (Source: Issue #1439 — DRY extraction of 25 duplicated Deps interfaces)\n */\nexport interface BaseMcpToolDeps {\n /** Optional logger */\n logger?: ILogger;\n /** Rate limiter for throttling tool calls (required) */\n rateLimiter: RateLimiter;\n /** Security configuration (includes timeout settings) */\n security?: SecurityConfig | undefined;\n}\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * MCP tool content types.\n */\nexport interface TextContent {\n type: 'text';\n text: string;\n}\n\n/**\n * MCP tool result.\n *\n * Uses mutable properties for compatibility with secure-handler\n * sanitization (which rewrites `text` in-place).\n */\nexport interface ToolResult {\n content: Array<TextContent>;\n isError?: boolean;\n /** Structured output for SDK outputSchema validation (Issue #1117) */\n structuredContent?: Record<string, unknown>;\n}\n\n// ============================================================================\n// Factory Functions\n// ============================================================================\n\n/**\n * Creates a successful tool result.\n *\n * @param text - The result text\n * @returns A ToolResult with the text content\n *\n * @example\n * ```typescript\n * return toolSuccess(JSON.stringify({ status: 'ok', data: result }));\n * ```\n */\nexport function toolSuccess(text: string): ToolResult {\n return {\n content: [{ type: 'text', text }],\n };\n}\n\n/**\n * Creates a successful tool result with structured content for outputSchema validation.\n *\n * When a tool is registered with outputSchema, the SDK validates structuredContent\n * against the schema. This helper returns both text (for display) and structured data.\n *\n * @param data - The structured result data (must match the tool's outputSchema)\n * @returns A ToolResult with both text content and structuredContent\n *\n * @example\n * ```typescript\n * return toolSuccessStructured({ experts: [...], count: 10 });\n * ```\n */\nexport function toolSuccessStructured(data: Record<string, unknown>): ToolResult {\n return {\n content: [{ type: 'text', text: JSON.stringify(data, null, 2) }],\n structuredContent: data,\n };\n}\n\n/**\n * Creates an error tool result.\n *\n * @param message - The error message\n * @returns A ToolResult with isError set to true\n *\n * @example\n * ```typescript\n * if (!input.ok) {\n * return toolError(`Validation failed: ${input.error}`);\n * }\n * ```\n */\nexport function toolError(message: string): ToolResult {\n return {\n isError: true,\n content: [{ type: 'text', text: message }],\n };\n}\n","/**\n * nexus-agents vote command types\n *\n * Type definitions for the consensus voting CLI command.\n *\n * (Source: Issue #212, Process Automation Epic #209)\n */\n\nimport type { ConsensusAlgorithm, Vote, ConsensusResult } from '../consensus/types.js';\n\n/**\n * Options for the vote command.\n */\nexport interface VoteCommandOptions {\n readonly proposal: string;\n readonly threshold?: 'majority' | 'supermajority' | 'unanimous';\n /** Use simulated votes instead of LLM execution (maps from --dry-run CLI flag) */\n readonly dryRun?: boolean;\n readonly quick?: boolean;\n readonly verbose?: boolean;\n readonly createIssue?: boolean;\n readonly issueNumber?: number;\n /** Timeout per vote in milliseconds (default: 90000 per Issue #607) */\n readonly timeoutMs?: number;\n}\n\n/**\n * Voter agent role definitions.\n *\n * `scope_steward` (#2185) was added 2026-04-25 to address a build-vs-buy\n * blind spot in the original 6-role panel: the panel approved a proposal\n * to build a USB-flasher CLI without flagging that Rufus already solves\n * the problem. The scope-steward role explicitly checks for existing tools\n * + biases toward \"don't build.\"\n */\nexport type VoterRole =\n | 'architect'\n | 'security'\n | 'devex'\n | 'ai_ml'\n | 'pm'\n | 'catfish'\n | 'scope_steward';\n\n/**\n * Maps threshold names to consensus algorithms.\n */\nexport const THRESHOLD_MAP: Record<string, ConsensusAlgorithm> = {\n majority: 'simple_majority',\n supermajority: 'supermajority',\n unanimous: 'unanimous',\n};\n\n/**\n * Agent role descriptions for prompt generation.\n */\nexport const VOTER_ROLES: Record<VoterRole, string> = {\n architect: 'Software Architect - evaluates technical design, scalability, and maintainability',\n security:\n 'Security Engineer - evaluates security implications, vulnerabilities, and attack vectors',\n devex: 'Developer Experience - evaluates usability, documentation, and developer workflow',\n ai_ml: 'AI/ML Engineer - evaluates AI/ML aspects, model selection, and learning capabilities',\n pm: 'Product Manager - evaluates business value, user impact, and resource allocation',\n catfish:\n 'Contrarian Analyst - deliberately challenges proposals to prevent agreement bias (arXiv:2505.21503)',\n scope_steward:\n 'Scope Steward - asks whether to build at all; checks existing tools, biases toward kill-the-feature (#2185)',\n};\n\n/**\n * Individual agent vote with metadata.\n */\nexport interface AgentVoteResult {\n readonly role: VoterRole;\n readonly vote: Vote;\n readonly processingTimeMs: number;\n /**\n * Source of the vote:\n * - 'llm': Real LLM execution\n * - 'simulation': Fallback simulation (opt-in only)\n * - 'error': Error during execution (Issue #523)\n */\n readonly source: 'llm' | 'simulation' | 'error';\n /** CLI that executed this vote (for adaptive routing feedback). */\n readonly cli?: string | undefined;\n /** Error message if vote fell back to simulation or encountered an error */\n readonly error?: string;\n}\n\n/**\n * Full voting result.\n */\nexport interface VotingResult {\n readonly proposal: string;\n readonly threshold: ConsensusAlgorithm;\n readonly result: ConsensusResult;\n readonly votes: readonly AgentVoteResult[];\n readonly totalTimeMs: number;\n /** Whether simulated votes were used instead of LLM execution */\n readonly simulateVotes: boolean;\n}\n\n/**\n * Vote verification hash for audit trail.\n */\nexport interface VoteHash {\n readonly role: VoterRole;\n readonly hash: string;\n readonly timestamp: string;\n}\n","/**\n * CLI-to-Model Adapter Bridge\n *\n * Wraps an ICliAdapter to implement IModelAdapter, enabling CLI tools\n * (claude, gemini, codex) to be used where model adapters are expected.\n *\n * @module cli-adapters/cli-to-model-adapter\n */\n\nimport type {\n Result,\n IModelAdapter,\n CompletionRequest,\n CompletionResponse,\n ModelCapability,\n} from '../core/index.js';\nimport { ModelCapability as MC, ok, err, ModelError, ConfigError } from '../core/index.js';\nimport { estimateTokens } from '../core/token-estimator.js';\nimport type { ICliAdapter, CliTask, CliResponse, CliError, ExecutionOptions } from './types.js';\nimport type { StreamChunk } from '../core/types/model.js';\n\n/** Configuration for CliToModelAdapter. */\nexport interface CliToModelAdapterConfig {\n /** Default timeout for CLI calls (ms). Overrides auto-detection. */\n readonly defaultTimeoutMs?: number;\n}\n\n/**\n * Bridge adapter that wraps ICliAdapter to implement IModelAdapter.\n *\n * This enables using CLI tools (claude, gemini, codex) in contexts\n * that expect IModelAdapter.\n *\n * @example\n * ```typescript\n * const cliAdapter = createCliAdapter({ cli: 'claude' });\n * const modelAdapter = new CliToModelAdapter(cliAdapter);\n *\n * // Now use modelAdapter where IModelAdapter is expected\n * const result = await modelAdapter.complete({\n * messages: [{ role: 'user', content: 'Hello!' }],\n * });\n * ```\n */\nexport class CliToModelAdapter implements IModelAdapter {\n readonly providerId: string;\n readonly modelId: string;\n readonly capabilities: readonly ModelCapability[];\n\n private readonly cliAdapter: ICliAdapter;\n private readonly defaultTimeoutMs: number | undefined;\n\n /**\n * Creates a bridge from CLI adapter to model adapter.\n *\n * @param cliAdapter - The CLI adapter to wrap\n * @param config - Optional configuration (e.g. timeout override)\n */\n constructor(cliAdapter: ICliAdapter, config?: CliToModelAdapterConfig) {\n this.cliAdapter = cliAdapter;\n this.defaultTimeoutMs = config?.defaultTimeoutMs;\n this.providerId = `cli-${cliAdapter.name}`;\n this.modelId = cliAdapter.getModelInfo().id;\n this.capabilities = this.deriveCapabilities();\n }\n\n /**\n * Derives ModelCapability from CLI capabilities.\n */\n private deriveCapabilities(): readonly ModelCapability[] {\n const caps: ModelCapability[] = [MC.COMPLETION, MC.TOOL_USE];\n\n // Claude CLI has extended thinking capability\n if (this.cliAdapter.name === 'claude') {\n caps.push(MC.EXTENDED_THINKING);\n }\n\n return caps;\n }\n\n /**\n * Converts CompletionRequest to CliTask.\n */\n private toCliTask(request: CompletionRequest): CliTask {\n // Build content from messages\n const content = request.messages\n .map((msg) => {\n const text =\n typeof msg.content === 'string'\n ? msg.content\n : msg.content\n .filter((b): b is { type: 'text'; text: string } => b.type === 'text')\n .map((b) => b.text)\n .join('\\n');\n return `[${msg.role}]: ${text}`;\n })\n .join('\\n\\n');\n\n // Build task with conditional optional properties\n const task: CliTask = { content };\n\n if (request.systemPrompt !== undefined) {\n (task as { systemPrompt: string }).systemPrompt = request.systemPrompt;\n }\n if (request.maxTokens !== undefined) {\n (task as { maxTokens: number }).maxTokens = request.maxTokens;\n }\n\n return task;\n }\n\n /**\n * Converts CliResponse to CompletionResponse.\n */\n private toCompletionResponse(response: CliResponse): CompletionResponse {\n return {\n content: [{ type: 'text', text: response.text }],\n usage: {\n inputTokens: response.usage?.inputTokens ?? 0,\n outputTokens: response.usage?.outputTokens ?? 0,\n totalTokens: response.usage?.totalTokens ?? 0,\n },\n stopReason: 'end_turn',\n model: response.model ?? this.modelId,\n };\n }\n\n /**\n * Converts CliError to ModelError.\n */\n private toModelError(cliError: CliError): ModelError {\n const options = cliError.cause !== undefined ? { cause: cliError.cause } : {};\n return new ModelError(cliError.message, options);\n }\n\n /**\n * Send a completion request via CLI.\n */\n async complete(request: CompletionRequest): Promise<Result<CompletionResponse, ModelError>> {\n const task = this.toCliTask(request);\n const opts: ExecutionOptions | undefined =\n this.defaultTimeoutMs !== undefined ? { timeoutMs: this.defaultTimeoutMs } : undefined;\n const result = await this.cliAdapter.execute(task, opts);\n\n if (!result.ok) {\n return err(this.toModelError(result.error));\n }\n\n return ok(this.toCompletionResponse(result.value));\n }\n\n /**\n * Streaming is not supported via CLI adapters.\n * Falls back to non-streaming and yields single chunk.\n */\n async *stream(request: CompletionRequest): AsyncIterable<StreamChunk> {\n const result = await this.complete(request);\n\n if (!result.ok) {\n throw result.error;\n }\n\n const response = result.value;\n\n yield { type: 'message_start', message: { model: response.model } };\n\n yield {\n type: 'content_block_start',\n index: 0,\n contentBlock: response.content[0] ?? { type: 'text', text: '' },\n };\n\n const firstBlock = response.content[0];\n const text = firstBlock?.type === 'text' ? firstBlock.text : '';\n yield { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text } };\n\n yield { type: 'content_block_stop', index: 0 };\n\n yield {\n type: 'message_delta',\n delta: { stop_reason: response.stopReason },\n usage: response.usage,\n };\n\n yield { type: 'message_stop' };\n }\n\n /**\n * Token count via canonical estimator (DRY consolidation Issue #1596).\n */\n countTokens(text: string): Promise<number> {\n return Promise.resolve(estimateTokens(text));\n }\n\n /**\n * Validate configuration by running health check.\n */\n validateConfig(): Result<void, ConfigError> {\n // CLI adapters validate via health check\n return ok(undefined);\n }\n\n /**\n * Initialize the underlying CLI adapter.\n */\n async initialize(): Promise<void> {\n await this.cliAdapter.initialize();\n }\n\n /**\n * Dispose the underlying CLI adapter.\n */\n async dispose(): Promise<void> {\n await this.cliAdapter.dispose();\n }\n}\n\n/**\n * Creates a model adapter from a CLI adapter.\n *\n * @param cliAdapter - The CLI adapter to wrap\n * @returns IModelAdapter implementation\n */\nexport function createCliToModelAdapter(\n cliAdapter: ICliAdapter,\n config?: CliToModelAdapterConfig\n): CliToModelAdapter {\n return new CliToModelAdapter(cliAdapter, config);\n}\n","/**\n * nexus-agents/adapters - Claude/Anthropic Model Adapter\n *\n * Adapter for Anthropic's Claude models (claude-opus-4, claude-sonnet-4, claude-haiku-3).\n * Implements the IModelAdapter interface with streaming support, rate limiting,\n * and proper error handling.\n *\n * Verified 2026-01-03: @anthropic-ai/sdk@0.71.2 is current stable\n * (Source: npm registry)\n */\n\nimport Anthropic from '@anthropic-ai/sdk';\nimport type { MessageStreamEvent } from '@anthropic-ai/sdk/resources/messages';\nimport type {\n Result,\n CompletionRequest,\n CompletionResponse,\n StreamChunk,\n ContentBlock,\n TokenUsage,\n} from '../core/index.js';\nimport { ok, err, ModelError, ConfigError, getTokenEstimator } from '../core/index.js';\nimport {\n BaseAdapter,\n type BaseAdapterConfig,\n requireApiKey,\n validateApiKeyPresence,\n} from './base-adapter.js';\nimport { createStream } from './streaming.js';\nimport type { ClaudeAdapterConfig } from './claude-adapter-types.js';\nimport { DEFAULT_MAX_TOKENS } from './claude-adapter-types.js';\nimport {\n mapStopReason,\n mapContentBlock,\n mapMessage,\n mapTool,\n resolveModelId,\n getModelCapabilities,\n} from './claude-adapter-helpers.js';\nimport { extractRequestSystemPrompt } from './prompt-utils.js';\n\n// Re-export types and constants for backward compatibility\nexport type { ClaudeAdapterConfig } from './claude-adapter-types.js';\nexport { CLAUDE_MODELS, CLAUDE_MODEL_ALIASES } from './claude-adapter-types.js';\n\n/**\n * Claude/Anthropic model adapter.\n *\n * Provides a unified interface for interacting with Anthropic's Claude models.\n * Supports completion, streaming, tool use, and vision capabilities.\n *\n * @example\n * ```typescript\n * const adapter = new ClaudeAdapter({\n * modelId: 'claude-sonnet-4',\n * apiKey: process.env.ANTHROPIC_API_KEY,\n * });\n *\n * const result = await adapter.complete({\n * messages: [{ role: 'user', content: 'Hello!' }],\n * maxTokens: 1024,\n * });\n *\n * if (result.ok) {\n * console.log(result.value.content);\n * }\n * ```\n */\nexport class ClaudeAdapter extends BaseAdapter {\n private readonly client: Anthropic;\n private readonly resolvedModelId: string;\n\n /**\n * Creates a new ClaudeAdapter instance.\n *\n * @param config - Claude adapter configuration\n * @throws {ConfigError} If API key is missing\n */\n constructor(config: ClaudeAdapterConfig) {\n const resolvedModelId = resolveModelId(config.modelId);\n\n // Build baseConfig conditionally to satisfy exactOptionalPropertyTypes\n const baseConfig: BaseAdapterConfig = {\n providerId: 'anthropic',\n modelId: resolvedModelId,\n capabilities: getModelCapabilities(config.modelId),\n apiKey: config.apiKey,\n };\n\n // Only set optional properties if defined\n if (config.baseUrl !== undefined) {\n baseConfig.baseUrl = config.baseUrl;\n }\n if (config.timeout !== undefined) {\n baseConfig.timeout = config.timeout;\n }\n if (config.maxRetries !== undefined) {\n baseConfig.maxRetries = config.maxRetries;\n }\n\n super(baseConfig);\n\n this.resolvedModelId = resolvedModelId;\n\n // Validate API key presence\n requireApiKey(config.apiKey, 'Anthropic', config.modelId);\n\n // Create Anthropic client\n this.client = new Anthropic({\n apiKey: config.apiKey,\n baseURL: config.baseUrl,\n timeout: config.timeout,\n maxRetries: config.maxRetries ?? 2,\n });\n }\n\n /**\n * Validates adapter configuration.\n * Extends base validation with Claude-specific checks.\n */\n override validateConfig(): Result<void, ConfigError> {\n const baseResult = super.validateConfig();\n if (!baseResult.ok) {\n return baseResult;\n }\n\n // Validate API key is present\n const keyResult = validateApiKeyPresence(this.config.apiKey, this.providerId, this.modelId);\n if (!keyResult.ok) return keyResult;\n\n return ok(undefined);\n }\n\n /**\n * Send a completion request to Claude.\n *\n * @param request - The completion request\n * @returns Result with response or ModelError\n */\n async complete(request: CompletionRequest): Promise<Result<CompletionResponse, ModelError>> {\n this.logRequest(request);\n\n try {\n const response = await this.executeCompletion(request);\n this.logResponse(response);\n return ok(response);\n } catch (error) {\n return err(this.transformError(error));\n }\n }\n\n /**\n * Stream a completion request from Claude.\n *\n * @param request - The completion request\n * @yields StreamChunk objects as they arrive\n */\n async *stream(request: CompletionRequest): AsyncIterable<StreamChunk> {\n this.logRequest(request);\n\n const [controller, iterable] = createStream<StreamChunk>();\n\n // Start streaming in the background\n this.executeStream(request, controller).catch((error: unknown) => {\n const modelError = this.transformError(error);\n controller.error(modelError);\n });\n\n yield* iterable;\n }\n\n /**\n * Count tokens in text using Claude-specific estimation.\n *\n * Claude uses a custom tokenizer. This provides a more accurate estimate\n * than the base adapter's generic calculation.\n *\n * @param text - Text to count tokens for\n * @returns Approximate token count\n */\n override countTokens(text: string): Promise<number> {\n // Use unified TokenEstimator with Claude-specific ratio (~3.5 chars/token)\n return Promise.resolve(getTokenEstimator().estimateText(text, 'claude'));\n }\n\n /**\n * Executes the completion request against the Anthropic API.\n */\n private async executeCompletion(request: CompletionRequest): Promise<CompletionResponse> {\n const params = this.buildRequestParams(request);\n const response = await this.client.messages.create(params);\n\n return this.mapResponse(response);\n }\n\n /**\n * Executes streaming completion and pushes chunks to the controller.\n */\n private async executeStream(\n request: CompletionRequest,\n controller: {\n push: (chunk: StreamChunk) => Result<void, Error>;\n complete: () => void;\n error: (error: Error) => void;\n }\n ): Promise<void> {\n try {\n const params = this.buildRequestParams(request);\n const stream = this.client.messages.stream(params);\n\n for await (const event of stream) {\n const chunk = this.mapStreamEvent(event);\n if (chunk) {\n controller.push(chunk);\n }\n }\n\n controller.complete();\n } catch (error) {\n const modelError = this.transformError(error);\n controller.error(modelError);\n }\n }\n\n /**\n * Builds Anthropic API request parameters from our CompletionRequest.\n */\n private buildRequestParams(\n request: CompletionRequest\n ): Anthropic.MessageCreateParamsNonStreaming {\n // Filter out system messages and map the rest\n const messages = request.messages.filter((m) => m.role !== 'system').map(mapMessage);\n\n const params: Anthropic.MessageCreateParamsNonStreaming = {\n model: this.resolvedModelId,\n messages,\n max_tokens: request.maxTokens ?? DEFAULT_MAX_TOKENS,\n };\n\n // Add system prompt if provided\n const systemPrompt = extractRequestSystemPrompt(request);\n if (systemPrompt !== undefined) {\n params.system = systemPrompt;\n }\n\n // Apply optional parameters\n this.applyOptionalParams(params, request);\n\n return params;\n }\n\n /**\n * Applies optional parameters to the request params.\n */\n private applyOptionalParams(\n params: Anthropic.MessageCreateParamsNonStreaming,\n request: CompletionRequest\n ): void {\n if (request.temperature !== undefined) {\n // eslint-disable-next-line @typescript-eslint/no-deprecated -- SDK 0.88 deprecated `temperature` for post-Opus-4.6 models; kept for backward compat with older Anthropic models and value 1.0\n params.temperature = request.temperature;\n }\n\n if (request.stop !== undefined && request.stop.length > 0) {\n params.stop_sequences = request.stop;\n }\n\n if (request.tools !== undefined && request.tools.length > 0) {\n params.tools = request.tools.map(mapTool);\n }\n\n // Issue #470: Log warning when responseFormat is requested but not supported\n if (request.responseFormat !== undefined && request.responseFormat.type !== 'text') {\n this.logger.warn('responseFormat is not supported by Claude adapter', {\n requestedFormat: request.responseFormat.type,\n suggestion: 'Use tool use or prompt engineering for structured output',\n });\n }\n }\n\n /**\n * Maps Anthropic API response to our CompletionResponse format.\n */\n private mapResponse(response: Anthropic.Message): CompletionResponse {\n const content: ContentBlock[] = response.content.map(mapContentBlock);\n\n const usage: TokenUsage = {\n inputTokens: response.usage.input_tokens,\n outputTokens: response.usage.output_tokens,\n totalTokens: response.usage.input_tokens + response.usage.output_tokens,\n };\n\n return {\n content,\n usage,\n stopReason: mapStopReason(response.stop_reason),\n model: response.model,\n };\n }\n\n /**\n * Maps Anthropic stream events to our StreamChunk format.\n */\n private mapStreamEvent(event: MessageStreamEvent): StreamChunk | null {\n switch (event.type) {\n case 'message_start':\n return {\n type: 'message_start',\n message: { model: event.message.model },\n };\n\n case 'content_block_start':\n return {\n type: 'content_block_start',\n index: event.index,\n contentBlock: mapContentBlock(event.content_block),\n };\n\n case 'content_block_delta':\n if (event.delta.type === 'text_delta') {\n return {\n type: 'content_block_delta',\n index: event.index,\n delta: { type: 'text_delta', text: event.delta.text },\n };\n }\n return null;\n\n case 'content_block_stop':\n return {\n type: 'content_block_stop',\n index: event.index,\n };\n\n case 'message_delta':\n return {\n type: 'message_delta',\n delta: { stop_reason: mapStopReason(event.delta.stop_reason ?? null) },\n usage: {\n inputTokens: 0, // Not available in delta\n outputTokens: event.usage.output_tokens,\n totalTokens: event.usage.output_tokens,\n },\n };\n\n case 'message_stop':\n return { type: 'message_stop' };\n\n default:\n return null;\n }\n }\n}\n\n/**\n * Creates a ClaudeAdapter with the specified configuration.\n * Factory function for cleaner API.\n *\n * @param config - Claude adapter configuration\n * @returns A configured ClaudeAdapter instance\n *\n * @example\n * ```typescript\n * const adapter = createClaudeAdapter({\n * modelId: 'claude-sonnet-4',\n * apiKey: process.env.ANTHROPIC_API_KEY!,\n * });\n * ```\n */\nexport function createClaudeAdapter(config: ClaudeAdapterConfig): ClaudeAdapter {\n return new ClaudeAdapter(config);\n}\n","/**\n * nexus-agents/adapters - Claude Adapter Types\n *\n * Type definitions and constants for the Claude/Anthropic adapter.\n *\n * @module adapters/claude-adapter-types\n */\n\nimport { getCliModelName } from '../config/model-config-helpers.js';\n\n/**\n * Supported Claude model identifiers.\n *\n * Derived from `config/model-capabilities.ts` (single source of truth — issue\n * #2186). Do not hardcode model-version strings here; update the registry.\n */\nexport const CLAUDE_MODELS = {\n OPUS_4: getCliModelName('claude-opus'),\n SONNET_4: getCliModelName('claude-sonnet'),\n HAIKU_4: getCliModelName('claude-haiku'),\n} as const;\n\n/**\n * Legacy version-suffix aliases mapped to the current registry cliModelName.\n *\n * Values come from `CLAUDE_MODELS` so they stay in sync with the canonical\n * registry. Add legacy entries here, never the version strings themselves.\n */\nexport const CLAUDE_MODEL_ALIASES: Record<string, string> = {\n 'claude-opus-4': CLAUDE_MODELS.OPUS_4,\n 'claude-sonnet-4': CLAUDE_MODELS.SONNET_4,\n 'claude-haiku-4': CLAUDE_MODELS.HAIKU_4,\n // Legacy alias — pre-4.x users routed to the current haiku.\n 'claude-haiku-3': CLAUDE_MODELS.HAIKU_4,\n} as const;\n\n/**\n * Configuration specific to ClaudeAdapter.\n */\nexport interface ClaudeAdapterConfig {\n /** Model ID (e.g., 'claude-sonnet-4' or full model identifier) */\n modelId: string;\n /** API key for Anthropic API (required) */\n apiKey: string;\n /** Base URL for API (optional, defaults to Anthropic's API) */\n baseUrl?: string;\n /** Request timeout in milliseconds (optional) */\n timeout?: number;\n /** Maximum retries for failed requests (optional) */\n maxRetries?: number;\n}\n\n// Note: Token estimation moved to core/token-estimator.ts (unified TokenEstimator)\n\n/**\n * Default maximum tokens for Claude models.\n */\nexport const DEFAULT_MAX_TOKENS = 4096;\n","/**\n * nexus-agents/adapters - Claude Adapter Helpers\n *\n * Helper functions for mapping between nexus-agents types and Anthropic SDK types.\n *\n * @module adapters/claude-adapter-helpers\n */\n\nimport Anthropic from '@anthropic-ai/sdk';\nimport type {\n MessageParam,\n ContentBlock as AnthropicContentBlock,\n} from '@anthropic-ai/sdk/resources/messages';\nimport type { ContentBlock, Message, ToolDefinition, StopReason } from '../core/index.js';\nimport { ModelCapability } from '../core/index.js';\nimport { getCliModelName, resolveCliAlias } from '../config/model-config-helpers.js';\n\n/**\n * Maps Anthropic stop reasons to our StopReason type.\n */\nexport function mapStopReason(anthropicReason: string | null): StopReason {\n switch (anthropicReason) {\n case 'end_turn':\n return 'end_turn';\n case 'max_tokens':\n return 'max_tokens';\n case 'stop_sequence':\n return 'stop_sequence';\n case 'tool_use':\n return 'tool_use';\n default:\n return 'end_turn';\n }\n}\n\n/**\n * Maps Anthropic content blocks to our ContentBlock type.\n */\nexport function mapContentBlock(block: AnthropicContentBlock): ContentBlock {\n if (block.type === 'text') {\n return { type: 'text', text: block.text };\n }\n if (block.type === 'tool_use') {\n const toolBlock = block;\n return {\n type: 'tool_use',\n id: toolBlock.id,\n name: toolBlock.name,\n input: toolBlock.input,\n };\n }\n // Handle unexpected block types gracefully\n return { type: 'text', text: '' };\n}\n\n/**\n * Maps our Message format to Anthropic's MessageParam format.\n */\nexport function mapMessage(message: Message): MessageParam {\n const role = message.role === 'user' ? 'user' : 'assistant';\n\n if (typeof message.content === 'string') {\n return { role, content: message.content };\n }\n\n // Map content blocks\n const content = message.content.map((block) => {\n if (block.type === 'text') {\n return { type: 'text' as const, text: block.text };\n }\n if (block.type === 'tool_use') {\n return {\n type: 'tool_use' as const,\n id: block.id,\n name: block.name,\n input: block.input,\n };\n }\n if (block.type === 'tool_result') {\n const toolResult: {\n type: 'tool_result';\n tool_use_id: string;\n content: string;\n is_error?: boolean;\n } = {\n type: 'tool_result' as const,\n tool_use_id: block.tool_use_id,\n content: block.content,\n };\n // Only set is_error if explicitly defined (exactOptionalPropertyTypes)\n if (block.is_error !== undefined) {\n toolResult.is_error = block.is_error;\n }\n return toolResult;\n }\n // Image type is the remaining possibility\n // Cast source to match Anthropic's expected type\n return {\n type: 'image' as const,\n source: block.source as Anthropic.ImageBlockParam['source'],\n };\n });\n\n return { role, content };\n}\n\n/**\n * Maps our ToolDefinition to Anthropic's tool format.\n */\nexport function mapTool(tool: ToolDefinition): Anthropic.Tool {\n return {\n name: tool.name,\n description: tool.description,\n input_schema: tool.inputSchema as Anthropic.Tool.InputSchema,\n };\n}\n\n/**\n * Resolves a Claude model alias to the full identifier the SDK expects.\n *\n * `resolveCliAlias` consults the canonical registry — both the cliAlias /\n * id columns AND the `aliases` array (#2199 Child 5 migration). Unknown\n * ids pass through (e.g., custom Bedrock identifiers).\n *\n * The canonical model strings live in `config/model-capabilities.ts`; this\n * function never holds them directly (issue #2186 Child 1).\n */\nexport function resolveModelId(modelId: string): string {\n const registryId = resolveCliAlias(modelId);\n if (registryId !== undefined) return getCliModelName(registryId);\n return modelId;\n}\n\n/**\n * Determines capabilities based on model ID.\n */\nexport function getModelCapabilities(modelId: string): readonly ModelCapability[] {\n const capabilities: ModelCapability[] = [\n ModelCapability.COMPLETION,\n ModelCapability.STREAMING,\n ModelCapability.TOOL_USE,\n ModelCapability.VISION,\n ];\n\n // Extended thinking is available on Opus and Sonnet 4\n const resolvedId = resolveModelId(modelId);\n if (resolvedId.includes('opus') || resolvedId.includes('sonnet-4')) {\n capabilities.push(ModelCapability.EXTENDED_THINKING);\n }\n\n return capabilities;\n}\n","/**\n * nexus-agents/context - Token Counter Helpers\n *\n * Internal helper functions for token counting.\n *\n * @module context/token-counter-helpers\n */\n\nimport Anthropic from '@anthropic-ai/sdk';\nimport type { Message } from '../core/index.js';\nimport type { TokenCounterProvider } from './token-counter-types.js';\n\n/**\n * Generates a cache key from content.\n */\nexport function generateCacheKey(\n content: string | Message[],\n provider: TokenCounterProvider | 'estimate',\n model?: string\n): string {\n const contentStr = typeof content === 'string' ? content : JSON.stringify(content);\n return `${provider}:${model ?? 'default'}:${contentStr}`;\n}\n\n/**\n * Converts Message[] to Anthropic MessageParam[] format.\n */\nexport function messagesToAnthropicFormat(messages: Message[]): Anthropic.MessageParam[] {\n return messages\n .filter((m) => m.role !== 'system')\n .map((m) => {\n const role = m.role === 'user' ? 'user' : 'assistant';\n if (typeof m.content === 'string') {\n return { role, content: m.content };\n }\n // Map content blocks\n const content = m.content.map((block) => {\n if (block.type === 'text') {\n return { type: 'text' as const, text: block.text };\n }\n if (block.type === 'tool_use') {\n return {\n type: 'tool_use' as const,\n id: block.id,\n name: block.name,\n input: block.input,\n };\n }\n if (block.type === 'tool_result') {\n return {\n type: 'tool_result' as const,\n tool_use_id: block.tool_use_id,\n content: block.content,\n };\n }\n // Image type - handle source type properly\n return {\n type: 'image' as const,\n source: block.source as Anthropic.ImageBlockParam['source'],\n };\n });\n return { role, content };\n });\n}\n\n/**\n * Extracts system prompt from messages if present.\n */\nexport function extractSystemPrompt(messages: Message[]): string | undefined {\n const systemMsg = messages.find((m) => m.role === 'system');\n if (systemMsg === undefined) {\n return undefined;\n }\n if (typeof systemMsg.content === 'string') {\n return systemMsg.content;\n }\n return systemMsg.content\n .filter((b): b is { type: 'text'; text: string } => b.type === 'text')\n .map((b) => b.text)\n .join('\\n');\n}\n","/**\n * Shared prompt extraction utilities for model adapters.\n *\n * Consolidates the duplicated extractSystemPrompt logic from\n * claude-adapter, gemini-adapter, and ollama-adapter into a single\n * implementation.\n *\n * @module adapters/prompt-utils\n * (Source: Issue #1596 — DRY adapter standardization)\n */\n\nimport type { CompletionRequest } from '../core/index.js';\nimport { extractSystemPrompt } from '../context/token-counter-helpers.js';\n\n/**\n * Extracts the system prompt from a CompletionRequest.\n *\n * Checks the explicit `systemPrompt` field first, then falls back to\n * searching the messages array for a system-role message.\n */\nexport function extractRequestSystemPrompt(request: CompletionRequest): string | undefined {\n if (request.systemPrompt !== undefined && request.systemPrompt !== '') {\n return request.systemPrompt;\n }\n return extractSystemPrompt(request.messages);\n}\n","/**\n * nexus-agents/adapters/sdk - Base SDK Adapter\n *\n * Implements IModelAdapter using the Vercel AI SDK's generateText/streamText\n * APIs. Provides a unified adapter for any AI SDK-supported provider.\n *\n * @module adapters/sdk/sdk-adapter\n * (Source: Issue #1123 — AI SDK provider layer)\n */\n\nimport type {\n CompletionRequest,\n CompletionResponse,\n ContentBlock,\n StreamChunk,\n Result,\n ILogger,\n} from '../../core/index.js';\nimport {\n ok,\n ModelError,\n ModelCapability,\n createLogger,\n getErrorMessage,\n} from '../../core/index.js';\nimport { BaseAdapter, AdapterModelError } from '../base-adapter.js';\nimport { ErrorCode } from '../../core/index.js';\nimport { isRateLimitLikeError } from '../rate-limit-detector.js';\nimport type { SdkAdapterConfig, SdkProviderId } from './types.js';\nimport { PROVIDER_ENV_KEYS, CUSTOM_API_BASE_URL_ENV } from './types.js';\nimport { validateCustomApiBaseUrl } from './custom-api-validation.js';\n\n/** Minimal AI SDK model interface (duck-typed for optional dependency). */\ninterface AiSdkModel {\n readonly modelId: string;\n}\n\n/** AI SDK generateText result shape (duck-typed). */\ninterface GenerateTextResult {\n text: string;\n finishReason: string;\n usage: {\n inputTokens: number | undefined;\n outputTokens: number | undefined;\n totalTokens: number | undefined;\n };\n response: { modelId: string };\n}\n\n/** AI SDK streamText result shape (duck-typed). */\ninterface StreamTextResult {\n textStream: AsyncIterable<string>;\n}\n\n/** Function signatures for AI SDK entry points (loaded dynamically). */\ninterface AiSdkFunctions {\n generateText: (options: Record<string, unknown>) => Promise<GenerateTextResult>;\n streamText: (options: Record<string, unknown>) => StreamTextResult;\n}\n\n/** AI SDK provider factory: creates a provider instance that is callable as a model factory. */\ntype ProviderFactory = (opts: Record<string, unknown>) => ProviderInstance;\n\n/** AI SDK provider instance: callable to create a model. */\ntype ProviderInstance = (id: string) => AiSdkModel;\n\n/**\n * Extracts a named provider factory from a dynamically-imported AI SDK module.\n *\n * AI SDK provider modules export factory functions (e.g., createAnthropic, createOpenAI)\n * that return callable provider instances. Since these are optional peer dependencies\n * loaded via dynamic import, we validate the shape at runtime rather than relying on\n * compile-time types.\n */\nfunction extractProviderFactory(\n mod: Record<string, unknown>,\n factoryName: string\n): ProviderFactory {\n const factory = mod[factoryName];\n if (typeof factory !== 'function') {\n throw new Error(`AI SDK module missing expected export: ${factoryName}`);\n }\n return factory as ProviderFactory;\n}\n\n/**\n * Validates a dynamically-imported AI SDK module has the expected generateText/streamText exports.\n *\n * The 'ai' package is an optional peer dependency loaded via dynamic import.\n * We validate the shape at runtime to avoid unsafe casts.\n */\nfunction extractAiSdkFunctions(mod: Record<string, unknown>): AiSdkFunctions {\n const generateText = mod['generateText'];\n const streamText = mod['streamText'];\n if (typeof generateText !== 'function') {\n throw new Error(\"AI SDK module missing expected export: 'generateText'\");\n }\n if (typeof streamText !== 'function') {\n throw new Error(\"AI SDK module missing expected export: 'streamText'\");\n }\n return {\n generateText: generateText as AiSdkFunctions['generateText'],\n streamText: streamText as AiSdkFunctions['streamText'],\n };\n}\n\n/**\n * Resolves the API key for a given provider.\n * Priority: explicit config > environment variable.\n */\nfunction resolveApiKey(providerId: SdkProviderId, configKey?: string): string | undefined {\n if (configKey !== undefined) return configKey;\n const envVar = PROVIDER_ENV_KEYS[providerId];\n return process.env[envVar];\n}\n\n/**\n * For the `custom-openai` provider only: resolve the base URL (config >\n * env) and run it through the SSRF guard. Returns `undefined` for every\n * other provider (the AI SDK's built-in factories handle their own\n * endpoints). Throws `ConfigError` at construction time for invalid\n * custom-openai setups — catching misconfiguration immediately rather\n * than on the first request.\n */\nfunction resolveAndValidateCustomBaseUrl(config: SdkAdapterConfig): string | undefined {\n if (config.providerId !== 'custom-openai') return undefined;\n const raw = config.baseUrl ?? process.env[CUSTOM_API_BASE_URL_ENV];\n const validated = validateCustomApiBaseUrl(raw);\n if (!validated.ok) throw validated.error;\n return validated.value.toString();\n}\n\n/**\n * Maps AI SDK finish reasons to our StopReason type.\n */\nfunction mapFinishReason(reason: string): CompletionResponse['stopReason'] {\n switch (reason) {\n case 'stop':\n case 'end-turn':\n return 'end_turn';\n case 'length':\n return 'max_tokens';\n case 'tool-calls':\n return 'tool_use';\n default:\n return 'end_turn';\n }\n}\n\n/**\n * Categorizes an error into an ErrorCode for the circuit breaker.\n */\nfunction categorizeError(error: unknown): ErrorCode {\n if (isRateLimitLikeError(error)) {\n return ErrorCode.MODEL_RATE_LIMITED;\n }\n const message = getErrorMessage(error).toLowerCase();\n if (message.includes('timeout') || message.includes('timed out')) {\n return ErrorCode.MODEL_TIMEOUT;\n }\n if (message.includes('401') || message.includes('unauthorized') || message.includes('api key')) {\n return ErrorCode.CONFIG_INVALID;\n }\n return ErrorCode.MODEL_ERROR;\n}\n\n/**\n * AI SDK adapter implementing IModelAdapter.\n *\n * Uses Vercel AI SDK (npm: ai) for model interaction instead of\n * CLI subprocess spawning. Supports any provider that has an\n * `@ai-sdk/*` package.\n */\nexport class SdkAdapter extends BaseAdapter {\n private readonly sdkProviderId: SdkProviderId;\n private model: AiSdkModel | undefined;\n private sdkFunctions: AiSdkFunctions | undefined;\n private readonly sdkConfig: SdkAdapterConfig;\n /** Validated base URL for custom-openai provider; undefined for built-ins. */\n private readonly customBaseUrl: string | undefined;\n /** Inflight init promise for coalescing concurrent calls (Issue #1438). */\n private initPromise: Promise<void> | undefined;\n\n constructor(config: SdkAdapterConfig, logger?: ILogger) {\n const apiKey = resolveApiKey(config.providerId, config.apiKey);\n super({\n providerId: `sdk-${config.providerId}`,\n modelId: config.modelId,\n capabilities: [ModelCapability.COMPLETION, ModelCapability.STREAMING],\n logger: logger ?? createLogger({ adapter: `sdk-${config.providerId}` }),\n ...(apiKey !== undefined ? { apiKey } : {}),\n ...(config.timeout !== undefined ? { timeout: config.timeout } : {}),\n ...(config.maxRetries !== undefined ? { maxRetries: config.maxRetries } : {}),\n });\n this.sdkProviderId = config.providerId;\n this.sdkConfig = config;\n this.customBaseUrl = resolveAndValidateCustomBaseUrl(config);\n }\n\n /**\n * Lazily initialize the AI SDK model and functions.\n * This allows the adapter to be created without the AI SDK installed,\n * failing only when actually used.\n */\n private async ensureInitialized(): Promise<void> {\n if (this.model !== undefined) return;\n // Coalesce concurrent init calls into a single load (Issue #1438)\n if (this.initPromise !== undefined) {\n await this.initPromise;\n return;\n }\n this.initPromise = this.doInitialize();\n try {\n await this.initPromise;\n } finally {\n this.initPromise = undefined;\n }\n }\n\n private async doInitialize(): Promise<void> {\n const apiKey = resolveApiKey(this.sdkProviderId, this.sdkConfig.apiKey);\n if (apiKey === undefined) {\n throw new AdapterModelError(`No API key for ${this.sdkProviderId}`, {\n code: ErrorCode.CONFIG_INVALID,\n });\n }\n\n // Dynamic import — AI SDK is an optional peer dependency\n const providerModule = await this.loadProvider(apiKey);\n this.model = providerModule.model;\n\n // AI SDK is an optional peer dependency — validate shape at runtime\n const aiModule = await import('ai');\n this.sdkFunctions = extractAiSdkFunctions(aiModule);\n }\n\n /**\n * Loads the provider-specific AI SDK module.\n *\n * Each @ai-sdk/* package exports a factory function (e.g., createAnthropic)\n * that returns a callable provider instance. We use extractProviderFactory()\n * to validate the export exists at runtime, since these are optional peer deps.\n */\n private async loadProvider(apiKey: string): Promise<{ model: AiSdkModel }> {\n switch (this.sdkProviderId) {\n case 'anthropic': {\n const mod = await import('@ai-sdk/anthropic');\n const factory = extractProviderFactory(mod, 'createAnthropic');\n const provider = factory({ apiKey });\n return { model: provider(this.modelId) };\n }\n case 'openai': {\n const mod = await import('@ai-sdk/openai');\n const factory = extractProviderFactory(mod, 'createOpenAI');\n const provider = factory({ apiKey });\n return { model: provider(this.modelId) };\n }\n case 'google': {\n const mod = await import('@ai-sdk/google');\n const factory = extractProviderFactory(mod, 'createGoogleGenerativeAI');\n const provider = factory({ apiKey });\n return { model: provider(this.modelId) };\n }\n case 'custom-openai': {\n // OpenAI-compatible gateway (multi-vendor proxies, self-hosted servers,\n // corporate LLM gateways). Reuses @ai-sdk/openai with a configurable\n // baseURL. See custom-api-validation.ts for the SSRF guard; the\n // adapter constructor validates before this method is reached.\n const mod = await import('@ai-sdk/openai');\n const factory = extractProviderFactory(mod, 'createOpenAI');\n const opts: Record<string, unknown> = { apiKey };\n if (this.customBaseUrl !== undefined) opts['baseURL'] = this.customBaseUrl;\n const provider = factory(opts);\n return { model: provider(this.modelId) };\n }\n }\n }\n\n /**\n * Maps our CompletionRequest to AI SDK generateText options.\n */\n private buildSdkOptions(request: CompletionRequest): Record<string, unknown> {\n const options: Record<string, unknown> = {\n model: this.model,\n messages: request.messages.map((m) => ({\n role: m.role === 'system' ? 'system' : m.role,\n content:\n typeof m.content === 'string'\n ? m.content\n : m.content.map((c: ContentBlock) => {\n if (c.type === 'text') return { type: 'text' as const, text: c.text };\n return c;\n }),\n })),\n };\n\n if (request.systemPrompt !== undefined) {\n options['system'] = request.systemPrompt;\n }\n if (request.temperature !== undefined) {\n options['temperature'] = request.temperature;\n }\n if (request.maxTokens !== undefined) {\n options['maxTokens'] = request.maxTokens;\n }\n if (request.stop !== undefined) {\n options['stopSequences'] = request.stop;\n }\n\n return options;\n }\n\n async complete(request: CompletionRequest): Promise<Result<CompletionResponse, ModelError>> {\n try {\n await this.ensureInitialized();\n this.logRequest(request);\n\n const sdk = this.sdkFunctions;\n if (sdk === undefined) {\n throw new Error(\n `SDK not initialized for model '${this.sdkConfig.modelId}' (provider: ${this.sdkProviderId}). ` +\n 'Ensure ensureInitialized() completes before calling complete().'\n );\n }\n const options = this.buildSdkOptions(request);\n const result = await sdk.generateText(options);\n\n const response: CompletionResponse = {\n content: [{ type: 'text', text: result.text }],\n usage: {\n inputTokens: result.usage.inputTokens ?? 0,\n outputTokens: result.usage.outputTokens ?? 0,\n totalTokens: result.usage.totalTokens ?? 0,\n },\n stopReason: mapFinishReason(result.finishReason),\n model: result.response.modelId,\n };\n\n this.logResponse(response);\n return ok(response);\n } catch (error: unknown) {\n const code = categorizeError(error);\n return this.toErrorResult(error, code);\n }\n }\n\n async *stream(request: CompletionRequest): AsyncIterable<StreamChunk> {\n // Ensure initialization and SDK readiness before entering the generator body.\n // Errors thrown before the first yield in an async generator bypass for-await-of\n // try/catch in callers, so we validate eagerly and wrap the body in try/catch.\n await this.ensureInitialized();\n this.logRequest(request);\n\n const sdk = this.sdkFunctions;\n if (sdk === undefined) {\n throw new AdapterModelError('SDK not initialized after ensureInitialized()', {\n code: ErrorCode.CONFIG_INVALID,\n });\n }\n\n const options = this.buildSdkOptions(request);\n\n // First yield establishes the generator — errors after this point are\n // properly caught by callers using for-await-of with try/catch.\n yield { type: 'message_start', message: { model: this.modelId } };\n\n const result = sdk.streamText(options);\n let index = 0;\n yield { type: 'content_block_start', index, contentBlock: { type: 'text', text: '' } };\n\n for await (const text of result.textStream) {\n yield {\n type: 'content_block_delta',\n index,\n delta: { type: 'text_delta', text },\n };\n }\n\n yield { type: 'content_block_stop', index };\n index++;\n yield {\n type: 'message_delta',\n delta: { stop_reason: 'end_turn' },\n usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },\n };\n yield { type: 'message_stop' };\n }\n\n /**\n * Converts a caught error into a Result error with categorized ErrorCode.\n */\n private toErrorResult(error: unknown, code: ErrorCode): Result<CompletionResponse, ModelError> {\n const message = getErrorMessage(error);\n const errorObj = error instanceof Error ? error : new Error(message);\n this.logger.error(`SDK adapter error (${this.sdkProviderId})`, errorObj);\n // AdapterModelError extends ModelError — no cast needed\n const modelError = new AdapterModelError(`${this.sdkProviderId} SDK error: ${message}`, {\n code,\n });\n return { ok: false, error: modelError };\n }\n}\n","/**\n * Auto-Selecting Model Adapter Factory\n *\n * Automatically selects the best available adapter:\n * 1. CLI adapters (claude/gemini/codex) - OAuth, no API keys needed\n * The CLIs handle model selection internally.\n * 2. API adapters (Anthropic/OpenAI) - requires API keys (fallback)\n *\n * Supports optional caching to avoid repeated CLI health checks.\n *\n * @module adapters/auto-adapter\n * (Source: Issue #165 - CLI detection cache)\n */\n\nimport type { IModelAdapter, ILogger } from '../core/index.js';\nimport { createLogger } from '../core/index.js';\nimport { createCliAdapter, isCliAvailable, getAvailableClis } from '../cli-adapters/factory.js';\nimport { createCliToModelAdapter } from '../cli-adapters/cli-to-model-adapter.js';\nimport { createClaudeAdapter } from './claude-adapter.js';\nimport { SdkAdapter } from './sdk/index.js';\nimport type { CliName } from '../cli-adapters/types.js';\nimport type { ICliDetectionCache } from '../cli-adapters/cli-detection-cache.js';\nimport { createCliDetectionCache } from '../cli-adapters/cli-detection-cache.js';\nimport { CUSTOM_API_DEFAULT_MODEL } from '../config/defaults.js';\nimport { getCliModelName } from '../config/model-config-helpers.js';\nimport { DEFAULT_MODEL_PER_CLI } from '../config/model-capabilities.js';\n\n/**\n * Adapter selection priority.\n */\nexport type AdapterPriority = 'cli-first' | 'api-first' | 'cli-only' | 'api-only';\n\n/**\n * Configuration for auto-selecting adapters.\n */\nexport interface AutoAdapterConfig {\n /** Selection priority (default: 'cli-first') */\n readonly priority?: AdapterPriority;\n /** Preferred CLI if multiple available (optional) */\n readonly preferredCli?: CliName;\n /** API key for Anthropic (optional, for fallback) */\n readonly anthropicApiKey?: string;\n /** API key for OpenAI (optional, for fallback via AI SDK) */\n readonly openaiApiKey?: string;\n /** API key for Google AI (optional, for fallback via AI SDK) */\n readonly googleApiKey?: string;\n /** Logger instance */\n readonly logger?: ILogger;\n /** CLI detection cache (optional, creates new if not provided) */\n readonly cache?: ICliDetectionCache;\n /** Whether to create and use cache if not provided (default: true) */\n readonly enableCache?: boolean;\n /** Default timeout for CLI subprocess calls (ms). Overrides auto-detection. */\n readonly defaultCliTimeoutMs?: number;\n}\n\n/**\n * Result of adapter selection.\n */\nexport interface AdapterSelection {\n /** The selected adapter */\n readonly adapter: IModelAdapter;\n /** Source of the adapter */\n readonly source: 'cli' | 'api';\n /** Which CLI or API was selected */\n readonly name: string;\n /** Why this adapter was selected */\n readonly reason: string;\n /** The cache used for CLI detection (for reuse) */\n readonly cache?: ICliDetectionCache | undefined;\n}\n\nconst defaultLogger = createLogger({ component: 'auto-adapter' });\n\n/**\n * Resolves the cache to use based on configuration.\n */\nfunction resolveCache(config: AutoAdapterConfig, logger: ILogger): ICliDetectionCache | undefined {\n if (config.cache !== undefined) {\n return config.cache;\n }\n const enableCache = config.enableCache ?? true;\n return enableCache ? createCliDetectionCache({ logger }) : undefined;\n}\n\n/**\n * Attempts to create a CLI-based model adapter.\n * The CLI tools (claude, gemini, codex) handle their own model selection.\n * Uses cache to avoid repeated health checks.\n */\nasync function tryCliAdapter(\n config: AutoAdapterConfig,\n logger: ILogger,\n cache?: ICliDetectionCache\n): Promise<AdapterSelection | null> {\n const preferredCli = config.preferredCli;\n\n const bridgeConfig =\n config.defaultCliTimeoutMs !== undefined\n ? { defaultTimeoutMs: config.defaultCliTimeoutMs }\n : undefined;\n\n // If preferred CLI specified, try that first\n if (preferredCli !== undefined && (await isCliAvailable(preferredCli, cache))) {\n logger.info('Using preferred CLI', { cli: preferredCli });\n const cliAdapter = createCliAdapter({ cli: preferredCli, logger });\n await cliAdapter.initialize();\n return {\n adapter: createCliToModelAdapter(cliAdapter, bridgeConfig),\n source: 'cli',\n name: preferredCli,\n reason: `Preferred CLI '${preferredCli}' is available (model selection handled by CLI)`,\n cache,\n };\n }\n\n // Otherwise, get all available CLIs and use the first one found\n const availableClis = await getAvailableClis(cache);\n\n if (availableClis.length === 0) {\n logger.info('No CLI adapters available');\n return null;\n }\n\n // Use first available CLI - each CLI handles its own model selection\n const selectedCli = availableClis[0];\n\n if (selectedCli === undefined) {\n return null;\n }\n\n logger.info('Auto-selected CLI', { cli: selectedCli, available: availableClis });\n const cliAdapter = createCliAdapter({ cli: selectedCli, logger });\n await cliAdapter.initialize();\n\n return {\n adapter: createCliToModelAdapter(cliAdapter, bridgeConfig),\n source: 'cli',\n name: selectedCli,\n reason: `Using '${selectedCli}' CLI (model selection handled by CLI)`,\n cache,\n };\n}\n\n/**\n * Resolves an API key from config or environment variable.\n */\nfunction resolveApiKeyFromEnv(configKey: string | undefined, envVar: string): string | undefined {\n const key = configKey ?? process.env[envVar];\n return key !== undefined && key.length > 0 ? key : undefined;\n}\n\n/**\n * Attempts to create an API-based model adapter.\n * Tries providers in order: Anthropic (native), OpenAI (SDK), Google (SDK).\n * This is a fallback when no CLIs are available.\n */\nfunction tryApiAdapter(config: AutoAdapterConfig, logger: ILogger): AdapterSelection | null {\n // Derive default model IDs from canonical registry instead of hardcoding\n const claudeModelId = getCliModelName(DEFAULT_MODEL_PER_CLI['claude']);\n const codexModelId = getCliModelName(DEFAULT_MODEL_PER_CLI['codex']);\n const geminiModelId = getCliModelName(DEFAULT_MODEL_PER_CLI['gemini']);\n\n // 1. Anthropic — use native ClaudeAdapter (battle-tested)\n const anthropicKey = resolveApiKeyFromEnv(config.anthropicApiKey, 'ANTHROPIC_API_KEY');\n if (anthropicKey !== undefined) {\n logger.info('Using Anthropic API adapter', { model: claudeModelId });\n return {\n adapter: createClaudeAdapter({ modelId: claudeModelId, apiKey: anthropicKey }),\n source: 'api',\n name: 'anthropic',\n reason: `Using Anthropic API (native adapter, model: ${claudeModelId})`,\n };\n }\n\n // 2. OpenAI — use AI SDK adapter\n const openaiKey = resolveApiKeyFromEnv(config.openaiApiKey, 'OPENAI_API_KEY');\n if (openaiKey !== undefined) {\n logger.info('Using OpenAI API adapter (AI SDK)', { model: codexModelId });\n return {\n adapter: new SdkAdapter({ providerId: 'openai', modelId: codexModelId, apiKey: openaiKey }),\n source: 'api',\n name: 'openai',\n reason: `Using OpenAI API via AI SDK (model: ${codexModelId})`,\n };\n }\n\n // 3. Google — use AI SDK adapter\n const googleKey = resolveApiKeyFromEnv(config.googleApiKey, 'GOOGLE_AI_API_KEY');\n if (googleKey !== undefined) {\n logger.info('Using Google AI API adapter (AI SDK)', { model: geminiModelId });\n return {\n adapter: new SdkAdapter({\n providerId: 'google',\n modelId: geminiModelId,\n apiKey: googleKey,\n }),\n source: 'api',\n name: 'google',\n reason: `Using Google AI API via AI SDK (model: ${geminiModelId})`,\n };\n }\n\n // 4. Custom OpenAI-compatible gateway (multi-vendor proxies, self-hosted\n // LLM servers, corporate gateways). Extracted for line limit.\n const custom = tryCustomOpenAiAdapter(logger);\n if (custom !== null) return custom;\n\n logger.info('No API keys available for any provider');\n return null;\n}\n\n/**\n * Tries the custom-openai SDK adapter if `NEXUS_CUSTOM_API_KEY` and\n * `NEXUS_CUSTOM_API_BASE_URL` are both set. The adapter constructor\n * runs the base URL through an SSRF guard (see\n * adapters/sdk/custom-api-validation.ts). Epic #2119.\n */\nfunction tryCustomOpenAiAdapter(logger: ILogger): AdapterSelection | null {\n const customKey = resolveApiKeyFromEnv(undefined, 'NEXUS_CUSTOM_API_KEY');\n const customBaseUrl = process.env['NEXUS_CUSTOM_API_BASE_URL'];\n if (customKey === undefined || customBaseUrl === undefined || customBaseUrl === '') {\n return null;\n }\n const customModelId = process.env['NEXUS_CUSTOM_MODEL'] ?? CUSTOM_API_DEFAULT_MODEL;\n logger.info('Using custom-openai SDK adapter', {\n model: customModelId,\n baseUrl: customBaseUrl,\n });\n return {\n adapter: new SdkAdapter({\n providerId: 'custom-openai',\n modelId: customModelId,\n apiKey: customKey,\n baseUrl: customBaseUrl,\n }),\n source: 'api',\n name: 'custom-openai',\n reason: `Using custom OpenAI-compatible gateway at ${customBaseUrl} (model: ${customModelId})`,\n };\n}\n\n/** Try CLI first, then API as fallback. */\nasync function selectCliFirst(\n config: AutoAdapterConfig,\n logger: ILogger,\n cache?: ICliDetectionCache\n): Promise<AdapterSelection> {\n const cliResult = await tryCliAdapter(config, logger, cache);\n if (cliResult !== null) return cliResult;\n const apiResult = tryApiAdapter(config, logger);\n if (apiResult !== null) return apiResult;\n throw new Error(\n 'No adapters available. Install a CLI (claude/gemini/codex) or set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GOOGLE_AI_API_KEY.'\n );\n}\n\n/** Try API first, then CLI as fallback. */\nasync function selectApiFirst(\n config: AutoAdapterConfig,\n logger: ILogger,\n cache?: ICliDetectionCache\n): Promise<AdapterSelection> {\n const apiResult = tryApiAdapter(config, logger);\n if (apiResult !== null) return apiResult;\n const cliResult = await tryCliAdapter(config, logger, cache);\n if (cliResult !== null) return cliResult;\n throw new Error(\n 'No adapters available. Set ANTHROPIC_API_KEY/OPENAI_API_KEY/GOOGLE_AI_API_KEY or install a CLI (claude/gemini/codex).'\n );\n}\n\n/** CLI only - no API fallback. */\nasync function selectCliOnly(\n config: AutoAdapterConfig,\n logger: ILogger,\n cache?: ICliDetectionCache\n): Promise<AdapterSelection> {\n const cliResult = await tryCliAdapter(config, logger, cache);\n if (cliResult !== null) return cliResult;\n throw new Error(\n 'No CLI adapters available. Install and authenticate claude, gemini, or codex CLI.'\n );\n}\n\n/** API only - no CLI fallback. */\nfunction selectApiOnly(config: AutoAdapterConfig, logger: ILogger): AdapterSelection {\n const apiResult = tryApiAdapter(config, logger);\n if (apiResult !== null) return apiResult;\n throw new Error(\n 'No API key available. Set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GOOGLE_AI_API_KEY.'\n );\n}\n\n/**\n * Auto-selects the best available model adapter.\n * Uses caching to avoid repeated CLI health checks.\n *\n * @param config - Configuration options\n * @returns Selected adapter or throws if none available\n *\n * @example\n * ```typescript\n * // Use CLI if available, fall back to API\n * const { adapter, source, reason, cache } = await createAutoAdapter();\n * console.log(`Using ${source} adapter: ${reason}`);\n *\n * // Reuse cache for subsequent selections\n * const { adapter: adapter2 } = await createAutoAdapter({ cache });\n *\n * // Force CLI only\n * const { adapter } = await createAutoAdapter({ priority: 'cli-only' });\n * ```\n */\nexport async function createAutoAdapter(config: AutoAdapterConfig = {}): Promise<AdapterSelection> {\n const logger = config.logger ?? defaultLogger;\n const priority = config.priority ?? 'cli-first';\n const cache = resolveCache(config, logger);\n\n logger.info('Auto-selecting adapter', { priority, cacheEnabled: cache !== undefined });\n\n switch (priority) {\n case 'cli-first':\n return selectCliFirst(config, logger, cache);\n case 'api-first':\n return selectApiFirst(config, logger, cache);\n case 'cli-only':\n return selectCliOnly(config, logger, cache);\n case 'api-only':\n return selectApiOnly(config, logger);\n default: {\n const exhaustive: never = priority;\n throw new Error(`Unknown priority: ${String(exhaustive)}`);\n }\n }\n}\n\n/**\n * Checks which adapters are available without creating them.\n * Uses caching to avoid repeated CLI health checks.\n *\n * @param cache - Optional cache to use\n * @returns Available CLIs and which API keys are set\n */\nexport async function getAvailableAdapters(cache?: ICliDetectionCache): Promise<{\n clis: CliName[];\n hasAnthropicKey: boolean;\n hasOpenaiKey: boolean;\n hasGoogleKey: boolean;\n cache?: ICliDetectionCache;\n}> {\n const effectiveCache = cache ?? createCliDetectionCache();\n const clis = await getAvailableClis(effectiveCache);\n\n return {\n clis,\n hasAnthropicKey: resolveApiKeyFromEnv(undefined, 'ANTHROPIC_API_KEY') !== undefined,\n hasOpenaiKey: resolveApiKeyFromEnv(undefined, 'OPENAI_API_KEY') !== undefined,\n hasGoogleKey: resolveApiKeyFromEnv(undefined, 'GOOGLE_AI_API_KEY') !== undefined,\n cache: effectiveCache,\n };\n}\n","/**\n * Event Bus Helper Functions\n *\n * Pure utility functions for event bus operations.\n * Extracted from event-bus.ts to maintain file size limits.\n *\n * @module agents/collaboration/event-bus-helpers\n * (Source: Issue #182, ARCHITECTURE.md Hybrid Architecture)\n */\n\nimport { randomUUID } from 'node:crypto';\nimport { getTimeProvider } from '../../core/index.js';\nimport type { SubscriptionId, TopicPattern, DomainEvent, EventFilter } from './event-bus-types.js';\n\n/** Default maximum history size */\nexport const DEFAULT_MAX_HISTORY_SIZE = 1000;\n\n/** Maximum number of subscriptions per bus */\nexport const MAX_SUBSCRIPTIONS = 500;\n\n/**\n * Internal subscription record.\n */\nexport interface SubscriptionRecord {\n readonly id: SubscriptionId;\n readonly pattern: TopicPattern;\n readonly regex: RegExp;\n readonly listener: (event: DomainEvent) => void | Promise<void>;\n}\n\n/** Maximum allowed topic pattern length */\nconst MAX_TOPIC_PATTERN_LENGTH = 200;\n\n/**\n * Convert a topic pattern to a regex for matching.\n * Supports wildcard patterns:\n * - 'session.created' -> exact match\n * - 'session.*' -> matches session.anything\n * - '*' -> matches everything\n *\n * Security Note (Issue #341): This function is safe from ReDoS because:\n * 1. The input is fully escaped before regex construction\n * 2. Only `*` wildcards are converted to `[^.]+` (bounded, non-greedy)\n * 3. Pattern length is bounded to prevent memory issues\n *\n * @throws Error if pattern exceeds MAX_TOPIC_PATTERN_LENGTH\n */\nexport function patternToRegex(pattern: TopicPattern): RegExp {\n // Validate length to prevent memory issues (Issue #341)\n if (pattern.length > MAX_TOPIC_PATTERN_LENGTH) {\n throw new Error(`Topic pattern exceeds maximum length of ${String(MAX_TOPIC_PATTERN_LENGTH)}`);\n }\n\n if (pattern === '*') {\n return /^.+$/;\n }\n // Use placeholder for * before escaping, then restore as regex wildcard\n // WILDCARD_PLACEHOLDER is a static constant, safe for RegExp (Issue #341)\n const WILDCARD_PLACEHOLDER = '\\x00WILDCARD\\x00';\n const withPlaceholder = pattern.replace(/\\*/g, WILDCARD_PLACEHOLDER);\n // Escape all regex special characters - this makes the pattern safe\n const escaped = withPlaceholder.replace(/[.+?^${}()|[\\]\\\\]/g, '\\\\$&');\n // Replace placeholder with bounded wildcard - safe since input was escaped\n const withWildcards = escaped.replace(new RegExp(WILDCARD_PLACEHOLDER, 'g'), '[^.]+');\n return new RegExp(`^${withWildcards}$`);\n}\n\n/**\n * Check if a topic matches a pattern.\n */\nexport function topicMatchesPattern(topic: string, regex: RegExp): boolean {\n return regex.test(topic);\n}\n\n/**\n * Generate a unique event ID.\n */\nexport function generateEventId(): string {\n return `evt-${String(getTimeProvider().now())}-${randomUUID().slice(0, 8)}`;\n}\n\n/**\n * Generate a unique subscription ID.\n */\nexport function generateSubscriptionId(): SubscriptionId {\n return `sub-${String(getTimeProvider().now())}-${randomUUID().slice(0, 8)}`;\n}\n\n/**\n * Generate a unique correlation ID for request tracing.\n * (Source: Issue #224, Sprint #228)\n *\n * @example\n * ```typescript\n * const correlationId = generateCorrelationId();\n * // -> 'cor_a1b2c3d4'\n * ```\n */\nexport function generateCorrelationId(): string {\n return `cor_${randomUUID().slice(0, 8)}`;\n}\n\n/**\n * Create a child correlation ID that chains to a parent.\n * Enables hierarchical tracing of subtasks.\n * (Source: Issue #224, Sprint #228)\n *\n * @param parentCorrelationId - The parent correlation ID to chain from\n * @returns A new correlation ID in format 'parentId.child_xxxxxxxx'\n *\n * @example\n * ```typescript\n * const parentId = generateCorrelationId();\n * // -> 'cor_a1b2c3d4'\n *\n * const childId = createChildCorrelationId(parentId);\n * // -> 'cor_a1b2c3d4.child_e5f6g7h8'\n * ```\n */\nexport function createChildCorrelationId(parentCorrelationId: string): string {\n return `${parentCorrelationId}.child_${randomUUID().slice(0, 8)}`;\n}\n\n/**\n * Apply topic, session, correlation, and timestamp filters to events.\n */\nexport function applyHistoryFilters(history: DomainEvent[], filter: EventFilter): DomainEvent[] {\n let result: DomainEvent[] = history;\n\n if (filter.topic !== undefined && filter.topic !== '') {\n const regex = patternToRegex(filter.topic);\n result = result.filter((e) => topicMatchesPattern(e.topic, regex));\n }\n\n if (filter.sessionId !== undefined && filter.sessionId !== '') {\n result = result.filter((e) => e.sessionId === filter.sessionId);\n }\n\n if (filter.correlationId !== undefined && filter.correlationId !== '') {\n result = result.filter((e) => e.correlationId === filter.correlationId);\n }\n\n return applyTimestampFilters(result, filter);\n}\n\n/**\n * Apply timestamp-based filters to events.\n */\nexport function applyTimestampFilters(result: DomainEvent[], filter: EventFilter): DomainEvent[] {\n let filtered = result;\n\n if (filter.after !== undefined && filter.after !== '') {\n const afterTimestamp = filter.after;\n filtered = filtered.filter((e) => e.timestamp > afterTimestamp);\n }\n\n if (filter.before !== undefined && filter.before !== '') {\n const beforeTimestamp = filter.before;\n filtered = filtered.filter((e) => e.timestamp < beforeTimestamp);\n }\n\n return filtered;\n}\n\n/**\n * Apply limit to history results.\n */\nexport function applyHistoryLimit(result: DomainEvent[], filter: EventFilter): DomainEvent[] {\n if (filter.limit !== undefined && filter.limit > 0) {\n return result.slice(-filter.limit);\n }\n return result;\n}\n\n/**\n * Enrich event with generated fields if missing.\n */\nexport function enrichEvent(event: DomainEvent): DomainEvent {\n return {\n ...event,\n eventId: event.eventId || generateEventId(),\n timestamp: event.timestamp || getTimeProvider().nowIso(),\n };\n}\n\n/**\n * Count subscribers matching a topic from a subscription map.\n */\nexport function countMatchingSubscribers(\n topic: string,\n subscriptions: Map<SubscriptionId, SubscriptionRecord>\n): number {\n let count = 0;\n for (const record of subscriptions.values()) {\n if (topicMatchesPattern(topic, record.regex)) {\n count++;\n }\n }\n return count;\n}\n","/**\n * Circular Buffer\n *\n * O(1) push and eviction for bounded collections.\n * Used by EventBus history to avoid O(n) array.shift() operations.\n *\n * @module agents/collaboration/circular-buffer\n * @see Issue #407\n */\n\n/**\n * A fixed-capacity circular buffer with O(1) push and O(1) oldest-eviction.\n *\n * When the buffer is full, new items overwrite the oldest items.\n * Items are always returned in insertion order (oldest to newest).\n */\nexport class CircularBuffer<T> {\n private readonly buffer: (T | undefined)[];\n private head = 0; // Points to oldest item (or next write position when empty)\n private tail = 0; // Points to next write position\n private _size = 0;\n\n /**\n * Creates a new circular buffer with the specified capacity.\n *\n * @param capacity - Maximum number of items to store\n * @throws Error if capacity is less than 1\n */\n constructor(private readonly capacity: number) {\n if (capacity < 1) {\n throw new Error('CircularBuffer capacity must be at least 1');\n }\n this.buffer = new Array<T | undefined>(capacity);\n }\n\n /**\n * Returns the number of items currently in the buffer.\n */\n get size(): number {\n return this._size;\n }\n\n /**\n * Returns true if the buffer is empty.\n */\n get isEmpty(): boolean {\n return this._size === 0;\n }\n\n /**\n * Returns true if the buffer is at capacity.\n */\n get isFull(): boolean {\n return this._size === this.capacity;\n }\n\n /**\n * Adds an item to the buffer.\n *\n * If the buffer is full, the oldest item is overwritten.\n * This operation is O(1).\n *\n * @param item - Item to add\n */\n push(item: T): void {\n this.buffer[this.tail] = item;\n this.tail = (this.tail + 1) % this.capacity;\n\n if (this._size < this.capacity) {\n this._size++;\n } else {\n // Buffer was full, oldest item was overwritten\n this.head = (this.head + 1) % this.capacity;\n }\n }\n\n /**\n * Removes all items from the buffer.\n * This operation is O(n) to allow garbage collection of items.\n */\n clear(): void {\n // Clear references to allow GC\n for (let i = 0; i < this.capacity; i++) {\n this.buffer[i] = undefined;\n }\n this.head = 0;\n this.tail = 0;\n this._size = 0;\n }\n\n /**\n * Returns all items in insertion order (oldest to newest).\n * This operation is O(n) where n is the number of items.\n *\n * @returns Array of items in insertion order\n */\n toArray(): T[] {\n if (this._size === 0) {\n return [];\n }\n\n const result: T[] = [];\n let readIndex = this.head;\n\n for (let i = 0; i < this._size; i++) {\n result.push(this.buffer[readIndex] as T);\n readIndex = (readIndex + 1) % this.capacity;\n }\n\n return result;\n }\n\n /**\n * Iterates over items in insertion order (oldest to newest).\n */\n *[Symbol.iterator](): Iterator<T> {\n let readIndex = this.head;\n for (let i = 0; i < this._size; i++) {\n yield this.buffer[readIndex] as T;\n readIndex = (readIndex + 1) % this.capacity;\n }\n }\n\n /**\n * Returns the most recently added item, or undefined if empty.\n * This operation is O(1).\n */\n peekNewest(): T | undefined {\n if (this._size === 0) {\n return undefined;\n }\n const newestIndex = (this.tail - 1 + this.capacity) % this.capacity;\n return this.buffer[newestIndex];\n }\n\n /**\n * Returns the oldest item in the buffer, or undefined if empty.\n * This operation is O(1).\n */\n peekOldest(): T | undefined {\n if (this._size === 0) {\n return undefined;\n }\n return this.buffer[this.head];\n }\n}\n","/**\n * Event Bus Implementation\n *\n * Async message passing for agent-to-agent communication.\n * Supports topic-based routing, wildcard patterns, and event history.\n *\n * @module agents/collaboration/event-bus\n * (Source: Issue #182, ARCHITECTURE.md Hybrid Architecture)\n */\n\nimport { getTimeProvider } from '../../core/index.js';\nimport type {\n IEventBus,\n DomainEvent,\n EventListener,\n Subscription,\n SubscriptionId,\n TopicPattern,\n EventFilter,\n EventBusOptions,\n EventBusStats,\n} from './event-bus-types.js';\n\nimport {\n DEFAULT_MAX_HISTORY_SIZE,\n MAX_SUBSCRIPTIONS,\n type SubscriptionRecord,\n patternToRegex,\n topicMatchesPattern,\n generateEventId,\n generateSubscriptionId,\n applyHistoryFilters,\n applyHistoryLimit,\n enrichEvent,\n countMatchingSubscribers,\n} from './event-bus-helpers.js';\n\nimport { CircularBuffer } from './circular-buffer.js';\n\n// Re-export correlation ID helpers for public API\nexport { generateCorrelationId, createChildCorrelationId } from './event-bus-helpers.js';\n\n/**\n * Event Bus for agent-to-agent communication.\n *\n * Features:\n * - Topic-based pub/sub with wildcard support\n * - Async and sync event emission\n * - Event history with filtering\n * - Error isolation (one listener failure doesn't block others)\n *\n * @example\n * ```typescript\n * const bus = new EventBus();\n *\n * // Subscribe to all session events\n * const sub = bus.subscribe('session.*', (event) => {\n * console.log('Session event:', event.topic);\n * });\n *\n * // Emit an event\n * bus.emit(createSessionCreatedEvent({ sessionId: '123', ... }));\n *\n * // Cleanup\n * sub.unsubscribe();\n * ```\n */\nexport class EventBus implements IEventBus {\n private readonly subscriptions: Map<SubscriptionId, SubscriptionRecord> = new Map();\n private readonly history: CircularBuffer<DomainEvent>;\n private readonly asyncHandling: boolean;\n private readonly logger?: EventBusOptions['logger'];\n\n private stats: EventBusStats = {\n eventsEmitted: 0,\n subscriptionsCreated: 0,\n activeSubscriptions: 0,\n historySize: 0,\n errorCount: 0,\n };\n\n constructor(options: EventBusOptions = {}) {\n const maxHistorySize = options.maxHistorySize ?? DEFAULT_MAX_HISTORY_SIZE;\n this.history = new CircularBuffer<DomainEvent>(maxHistorySize);\n this.asyncHandling = options.asyncHandling ?? false;\n this.logger = options.logger;\n }\n\n /**\n * Emit an event synchronously to all matching subscribers.\n */\n emit(event: DomainEvent): void {\n const enrichedEvent = enrichEvent(event);\n this.addToHistory(enrichedEvent);\n this.stats.eventsEmitted++;\n\n this.logger?.debug('Event emitted', {\n topic: enrichedEvent.topic,\n eventId: enrichedEvent.eventId,\n subscriberCount: countMatchingSubscribers(enrichedEvent.topic, this.subscriptions),\n });\n\n for (const record of this.subscriptions.values()) {\n if (topicMatchesPattern(enrichedEvent.topic, record.regex)) {\n this.invokeListener(record, enrichedEvent);\n }\n }\n }\n\n /**\n * Emit an event and wait for all async handlers to complete.\n */\n async emitAsync(event: DomainEvent): Promise<void> {\n const enrichedEvent = enrichEvent(event);\n this.addToHistory(enrichedEvent);\n this.stats.eventsEmitted++;\n\n this.logger?.debug('Async event emitted', {\n topic: enrichedEvent.topic,\n eventId: enrichedEvent.eventId,\n });\n\n const promises: Promise<void>[] = [];\n\n for (const record of this.subscriptions.values()) {\n if (topicMatchesPattern(enrichedEvent.topic, record.regex)) {\n promises.push(this.invokeListenerAsync(record, enrichedEvent));\n }\n }\n\n await Promise.all(promises);\n }\n\n /**\n * Subscribe to events matching a topic pattern.\n */\n subscribe<T extends DomainEvent = DomainEvent>(\n pattern: TopicPattern,\n listener: EventListener<T>\n ): Subscription {\n if (this.subscriptions.size >= MAX_SUBSCRIPTIONS) {\n throw new Error(\n `Maximum subscription limit reached (${String(MAX_SUBSCRIPTIONS)}). ` +\n 'Unsubscribe from unused patterns before adding new ones.'\n );\n }\n\n const id = generateSubscriptionId();\n const regex = patternToRegex(pattern);\n\n const record: SubscriptionRecord = {\n id,\n pattern,\n regex,\n listener: listener as EventListener,\n };\n\n this.subscriptions.set(id, record);\n this.stats.subscriptionsCreated++;\n this.stats.activeSubscriptions = this.subscriptions.size;\n\n this.logger?.debug('Subscription created', { id, pattern });\n\n return {\n id,\n pattern,\n unsubscribe: () => {\n this.unsubscribe(id);\n },\n };\n }\n\n /**\n * Unsubscribe from events.\n */\n unsubscribe(subscriptionId: SubscriptionId): void {\n const existed = this.subscriptions.delete(subscriptionId);\n if (existed) {\n this.stats.activeSubscriptions = this.subscriptions.size;\n this.logger?.debug('Subscription removed', { subscriptionId });\n }\n }\n\n /**\n * Get event history matching filter criteria.\n */\n getHistory(filter?: EventFilter): readonly DomainEvent[] {\n const historyArray = this.history.toArray();\n if (filter === undefined) {\n return historyArray;\n }\n\n const filtered = applyHistoryFilters(historyArray, filter);\n return applyHistoryLimit(filtered, filter);\n }\n\n /**\n * Clear event history.\n */\n clearHistory(): void {\n this.history.clear();\n this.stats.historySize = 0;\n this.logger?.debug('History cleared');\n }\n\n /**\n * Get event bus statistics.\n */\n getStats(): EventBusStats {\n return { ...this.stats };\n }\n\n /**\n * Check if a topic pattern has any subscribers.\n */\n hasSubscribers(pattern: TopicPattern): boolean {\n const regex = patternToRegex(pattern);\n for (const record of this.subscriptions.values()) {\n // Check if the patterns could overlap\n if (\n topicMatchesPattern(pattern, record.regex) ||\n topicMatchesPattern(record.pattern, regex)\n ) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * Add event to history, maintaining size limit via O(1) circular buffer.\n * @see Issue #407 - Performance optimization\n */\n private addToHistory(event: DomainEvent): void {\n this.history.push(event);\n this.stats.historySize = this.history.size;\n }\n\n /**\n * Invoke a listener synchronously with error isolation.\n */\n private invokeListener(record: SubscriptionRecord, event: DomainEvent): void {\n try {\n const result = record.listener(event);\n // If async handling is enabled and listener returns a promise, don't wait\n if (this.asyncHandling && result instanceof Promise) {\n result.catch((error: unknown) => {\n this.handleListenerError(record, event, error);\n });\n }\n } catch (error) {\n this.handleListenerError(record, event, error);\n }\n }\n\n /**\n * Invoke a listener asynchronously.\n */\n private async invokeListenerAsync(record: SubscriptionRecord, event: DomainEvent): Promise<void> {\n try {\n await record.listener(event);\n } catch (error) {\n this.handleListenerError(record, event, error);\n }\n }\n\n /**\n * Handle listener errors without disrupting other listeners.\n */\n private handleListenerError(\n record: SubscriptionRecord,\n event: DomainEvent,\n error: unknown\n ): void {\n this.stats.errorCount++;\n const errorObj = error instanceof Error ? error : new Error(String(error));\n this.logger?.error('Event listener error', errorObj, {\n subscriptionId: record.id,\n pattern: record.pattern,\n eventTopic: event.topic,\n eventId: event.eventId,\n });\n }\n}\n\n/**\n * Create a singleton event bus instance for global coordination.\n * Use with caution - prefer dependency injection for testability.\n */\nlet globalBus: EventBus | null = null;\n\nexport function getGlobalEventBus(options?: EventBusOptions): EventBus {\n globalBus ??= new EventBus(options);\n return globalBus;\n}\n\n/**\n * Reset the global event bus (for testing).\n */\nexport function resetGlobalEventBus(): void {\n globalBus = null;\n}\n\n/**\n * Factory functions for creating typed events.\n */\nexport function createEvent<T extends DomainEvent>(\n topic: T['topic'],\n payload: T['payload'],\n options?: {\n sessionId?: string;\n correlationId?: string;\n }\n): T {\n return {\n eventId: generateEventId(),\n timestamp: getTimeProvider().nowIso(),\n topic,\n payload,\n sessionId: options?.sessionId,\n correlationId: options?.correlationId,\n } as T;\n}\n","/**\n * Event Bus Topic Constants\n *\n * Type-safe topic constants for event subscription.\n *\n * @module agents/collaboration/event-bus-topics\n * (Source: Issue #182, ARCHITECTURE.md Hybrid Architecture)\n */\n\n/**\n * Topic constants for type-safe subscription.\n */\nexport const EventTopics = {\n // Session events\n SESSION_CREATED: 'session.created',\n SESSION_STATUS_CHANGED: 'session.status_changed',\n SESSION_PARTICIPANT_JOINED: 'session.participant_joined',\n SESSION_RESULT_SUBMITTED: 'session.result_submitted',\n SESSION_FINALIZED: 'session.finalized',\n SESSION_ALL: 'session.*',\n\n // Message events\n MESSAGE_SENT: 'message.sent',\n MESSAGE_RECEIVED: 'message.received',\n MESSAGE_ALL: 'message.*',\n\n // Agent events\n AGENT_TASK_DELEGATED: 'agent.task_delegated',\n AGENT_RESULT_BROADCAST: 'agent.result_broadcast',\n AGENT_ALL: 'agent.*',\n\n // Consensus events\n CONSENSUS_VOTE_REQUESTED: 'consensus.vote_requested',\n CONSENSUS_VOTE_CAST: 'consensus.vote_cast',\n CONSENSUS_REACHED: 'consensus.reached',\n CONSENSUS_ALL: 'consensus.*',\n\n // Protocol events\n PROTOCOL_STARTED: 'protocol.started',\n PROTOCOL_ITERATION: 'protocol.iteration',\n PROTOCOL_COMPLETED: 'protocol.completed',\n PROTOCOL_ALL: 'protocol.*',\n\n // Aegean phase events (Issue #216)\n AEGEAN_ROUND_STARTED: 'protocol.aegean.round_started',\n AEGEAN_VOTE_COLLECTED: 'protocol.aegean.vote_collected',\n AEGEAN_QUORUM_DETECTED: 'protocol.aegean.quorum_detected',\n AEGEAN_ALL: 'protocol.aegean.*',\n\n // Reflexion phase events (Issue #216)\n REFLEXION_CRITIQUE_STARTED: 'protocol.reflexion.critique_started',\n REFLEXION_CRITIQUE_COMPLETED: 'protocol.reflexion.critique_completed',\n REFLEXION_SYNTHESIS: 'protocol.reflexion.synthesis',\n REFLEXION_ALL: 'protocol.reflexion.*',\n\n // Trinity phase events (Issue #216)\n TRINITY_PHASE_STARTED: 'protocol.trinity.phase_started',\n TRINITY_PHASE_COMPLETED: 'protocol.trinity.phase_completed',\n TRINITY_ALL: 'protocol.trinity.*',\n\n // Byzantine detection events (Issue #218)\n BYZANTINE_WEIGHT_UPDATED: 'byzantine.weight_updated',\n BYZANTINE_PATTERN_DETECTED: 'byzantine.pattern_detected',\n BYZANTINE_AGENT_FLAGGED: 'byzantine.agent_flagged',\n BYZANTINE_COLLUSION_SUSPECTED: 'byzantine.collusion_suspected',\n BYZANTINE_ALL: 'byzantine.*',\n\n // Wildcard\n ALL: '*',\n} as const;\n","/**\n * Resilient Model Adapter\n *\n * Transparent proxy implementing IModelAdapter with lazy detection,\n * automatic failover on circuit breaker events, and observable health.\n *\n * Key design: ResilientAdapter IS an IModelAdapter. Every existing consumer\n * works unchanged. The health/failover API lives on IResilientAdapter for\n * dashboard and monitoring consumers only.\n *\n * @module adapters/resilient-adapter\n * (Source: Issue #811 - Resilient model adapter architecture)\n */\n\nimport type {\n IModelAdapter,\n CompletionRequest,\n CompletionResponse,\n StreamChunk,\n ModelCapability,\n} from '../core/types/model.js';\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { ModelError, ConfigError } from '../core/errors.js';\nimport type { ILogger } from '../core/index.js';\nimport { getTimeProvider, getRandomProvider } from '../core/index.js';\nimport { getErrorMessage, createLogger } from '../core/index.js';\n\nimport { createAutoAdapter, type AdapterSelection } from './auto-adapter.js';\nimport {\n isRateLimitLikeError,\n toRateLimitError,\n recordRateLimitEvent,\n} from './rate-limit-detector.js';\nimport type { CliName } from '../cli-adapters/types.js';\nimport type {\n IResilientAdapter,\n AdapterHealthInfo,\n ResilientAdapterConfig,\n} from './resilient-adapter-types.js';\nimport type { CircuitStateChangeEvent } from '../cli-adapters/circuit-breaker-types.js';\nimport type { CircuitBreakerRegistry } from '../cli-adapters/circuit-breaker.js';\nimport { getGlobalEventBus } from '../core/event-bus.js';\n\n// ============================================================================\n// Factory\n// ============================================================================\n\n/**\n * Creates a resilient adapter with lazy detection and automatic failover.\n *\n * @example\n * ```typescript\n * const adapter = createResilientAdapter({ logger });\n * // No detection at creation time — detection happens on first use\n * const result = await adapter.complete(request); // triggers detection\n * ```\n */\nexport function createResilientAdapter(config?: ResilientAdapterConfig): IResilientAdapter {\n return new ResilientAdapter(config);\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Resilient adapter proxy. Implements IModelAdapter transparently.\n */\nexport class ResilientAdapter implements IResilientAdapter {\n private readonly logger: ILogger;\n private currentAdapter: IModelAdapter | undefined;\n private currentSelection: AdapterSelection | undefined;\n private hasEverDetected = false;\n private health: AdapterHealthInfo | undefined;\n private preferredCli: CliName | undefined;\n private readonly failoverCallbacks = new Set<(info: AdapterHealthInfo) => void>();\n private circuitBreakerRegistry: CircuitBreakerRegistry | undefined;\n private circuitListener: ((event: CircuitStateChangeEvent) => void) | undefined;\n private disposed = false;\n private readonly defaultCliTimeoutMs: number | undefined;\n /** Inflight detection promise for coalescing concurrent calls (Issue #1423). */\n private detectionPromise: Promise<IModelAdapter | undefined> | undefined;\n\n constructor(config?: ResilientAdapterConfig) {\n this.logger = config?.logger ?? createLogger({ component: 'resilient-adapter' });\n this.preferredCli = config?.preferredCli;\n this.defaultCliTimeoutMs = config?.defaultCliTimeoutMs;\n }\n\n // --- IModelAdapter properties (forwarded) ---\n\n get providerId(): string {\n return this.currentAdapter?.providerId ?? 'resilient-proxy';\n }\n\n get modelId(): string {\n return this.currentAdapter?.modelId ?? 'pending-detection';\n }\n\n get capabilities(): readonly ModelCapability[] {\n return this.currentAdapter?.capabilities ?? [];\n }\n\n // --- IModelAdapter methods ---\n\n async complete(request: CompletionRequest): Promise<Result<CompletionResponse, ModelError>> {\n const adapter = await this.ensureAdapter();\n if (adapter === undefined) {\n return err(new ModelError('No model adapter available'));\n }\n const result = await adapter.complete(request);\n if (!result.ok && isRateLimitLikeError(result.error)) {\n const rlError = toRateLimitError(result.error, adapter.providerId);\n recordRateLimitEvent({\n provider: adapter.providerId,\n timestamp: getTimeProvider().now(),\n retryAfterMs: rlError.retryAfterMs,\n });\n this.logger.warn('Rate limit detected', {\n provider: adapter.providerId,\n retryAfterMs: rlError.retryAfterMs,\n });\n }\n return result;\n }\n\n async *stream(request: CompletionRequest): AsyncIterable<StreamChunk> {\n const adapter = await this.ensureAdapter();\n if (adapter === undefined) {\n return;\n }\n yield* adapter.stream(request);\n }\n\n async countTokens(text: string): Promise<number> {\n const adapter = await this.ensureAdapter();\n if (adapter === undefined) {\n return 0;\n }\n return adapter.countTokens(text);\n }\n\n validateConfig(): Result<void, ConfigError> {\n if (this.currentAdapter !== undefined) {\n return this.currentAdapter.validateConfig();\n }\n // Before first use, config is trivially valid\n return ok(undefined);\n }\n\n // --- IResilientAdapter methods ---\n\n getHealth(): AdapterHealthInfo | undefined {\n return this.health;\n }\n\n async refresh(): Promise<void> {\n this.currentAdapter = undefined;\n this.currentSelection = undefined;\n this.detectionPromise = undefined;\n await this.ensureAdapter();\n }\n\n setPreferredCli(cli: CliName | undefined): void {\n this.preferredCli = cli;\n this.currentAdapter = undefined;\n this.currentSelection = undefined;\n }\n\n onFailover(cb: (info: AdapterHealthInfo) => void): () => void {\n this.failoverCallbacks.add(cb);\n return () => {\n this.failoverCallbacks.delete(cb);\n };\n }\n\n /**\n * Attach a circuit breaker registry for automatic failover.\n * When the current adapter's circuit opens, the cached adapter\n * is cleared so the next call triggers re-detection.\n */\n attachCircuitBreakerRegistry(registry: CircuitBreakerRegistry): void {\n this.detachCircuitBreakerRegistry();\n this.circuitBreakerRegistry = registry;\n this.circuitListener = (event: CircuitStateChangeEvent) => {\n this.handleCircuitStateChange(event);\n };\n registry.addGlobalStateChangeListener(this.circuitListener);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n this.detachCircuitBreakerRegistry();\n this.failoverCallbacks.clear();\n }\n\n // --- Private methods ---\n\n private async ensureAdapter(): Promise<IModelAdapter | undefined> {\n if (this.currentAdapter !== undefined) {\n return this.currentAdapter;\n }\n // Coalesce concurrent detection calls into a single probe (Issue #1423)\n this.detectionPromise ??= this.detectAdapter().finally(() => {\n this.detectionPromise = undefined;\n });\n return this.detectionPromise;\n }\n\n private async detectAdapter(): Promise<IModelAdapter | undefined> {\n try {\n this.logger.info('Detecting model adapter (lazy)');\n const config = {\n logger: this.logger,\n ...(this.preferredCli !== undefined && { preferredCli: this.preferredCli }),\n ...(this.defaultCliTimeoutMs !== undefined && {\n defaultCliTimeoutMs: this.defaultCliTimeoutMs,\n }),\n };\n const selection = await createAutoAdapter(config);\n this.applySelection(selection);\n return this.currentAdapter;\n } catch (error) {\n const message = getErrorMessage(error);\n this.logger.warn('No model adapter available', { error: message });\n this.health = {\n source: 'api',\n state: 'unavailable',\n selectedAt: new Date(),\n failoverCount: this.health?.failoverCount ?? 0,\n lastError: message,\n };\n return undefined;\n }\n }\n\n private applySelection(selection: AdapterSelection): void {\n const isFailover = this.hasEverDetected;\n this.currentAdapter = selection.adapter;\n this.currentSelection = selection;\n this.hasEverDetected = true;\n\n const source = mapSelectionSource(selection);\n const failoverCount = (this.health?.failoverCount ?? 0) + (isFailover ? 1 : 0);\n\n this.health = {\n source,\n state: 'healthy',\n selectedAt: new Date(),\n failoverCount,\n };\n\n this.logger.info('Adapter selected', {\n source: selection.source,\n name: selection.name,\n model: selection.adapter.modelId,\n provider: selection.adapter.providerId,\n failover: isFailover,\n });\n\n if (isFailover) {\n this.emitFailover();\n }\n }\n\n private handleCircuitStateChange(event: CircuitStateChangeEvent): void {\n if (event.newState !== 'open') return;\n\n const currentSource = this.currentSelection?.name;\n if (currentSource === undefined || currentSource !== event.cliName) {\n return; // Not our current adapter\n }\n\n this.logger.warn('Current adapter circuit opened, clearing cache for re-detection', {\n cli: event.cliName,\n reason: event.reason,\n });\n\n this.currentAdapter = undefined;\n this.currentSelection = undefined;\n\n if (this.health !== undefined) {\n this.health = {\n ...this.health,\n state: 'degraded',\n lastError: event.reason,\n };\n }\n }\n\n private emitFailover(): void {\n const info = this.health;\n if (info === undefined) return;\n\n for (const cb of this.failoverCallbacks) {\n try {\n cb(info);\n } catch (error: unknown) {\n this.logger.debug('Failover callback error', { error: getErrorMessage(error) });\n }\n }\n\n try {\n const eventBus = getGlobalEventBus();\n eventBus.emit({\n eventId: `failover-${getTimeProvider().now().toString(36)}-${getRandomProvider().random().toString(36).slice(2, 8)}`,\n timestamp: new Date().toISOString(),\n topic: 'adapter.failover',\n payload: info,\n });\n } catch (error: unknown) {\n this.logger.debug('EventBus emit failed during failover', {\n error: getErrorMessage(error),\n });\n }\n }\n\n private detachCircuitBreakerRegistry(): void {\n if (this.circuitBreakerRegistry !== undefined && this.circuitListener !== undefined) {\n this.circuitBreakerRegistry.removeGlobalStateChangeListener(this.circuitListener);\n }\n this.circuitBreakerRegistry = undefined;\n this.circuitListener = undefined;\n }\n}\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\nfunction mapSelectionSource(selection: AdapterSelection): CliName | 'api' {\n if (selection.source === 'cli') {\n return selection.name as CliName;\n }\n return 'api';\n}\n","/**\n * Unified Adapter Registry — single entry point for all model adapter access.\n *\n * Pre-computes task-to-CLI routing from the canonical model registry and\n * task specialization matrix at creation time. All consumers get adapters\n * through this registry instead of calling createAutoAdapter/createResilientAdapter\n * directly.\n *\n * Design:\n * - One IResilientAdapter per CLI (claude/gemini/codex), created lazily on first access\n * - One \"default\" adapter for unscoped requests (uses createAutoAdapter priority)\n * - Task routing is deterministic: category → primary CLI → cached adapter\n * - Session-scoped: create once at MCP startup, reuse for the session lifetime\n *\n * @module adapters/unified-registry\n * (Source: Issue #1149 — Unified Adapter Registry)\n * (Source: Issue #1151 — Single adapter entry point)\n */\n\nimport type { ILogger } from '../core/index.js';\nimport { createLogger } from '../core/index.js';\nimport { createResilientAdapter } from './resilient-adapter.js';\nimport type { IResilientAdapter } from './resilient-adapter-types.js';\nimport type { CliName } from '../cli-adapters/types.js';\nimport { TASK_SPECIALIZATION_MATRIX, detectTaskCategory } from '../config/task-specialization.js';\nimport type { TaskCategory } from '../config/task-specialization-types.js';\nimport { DEFAULT_MODEL_CAPABILITIES, DEFAULT_MODEL_PER_CLI } from '../config/model-capabilities.js';\nimport type { CliNameLiteral } from '../config/model-capabilities-types.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/** Configuration for the unified registry. */\nexport interface UnifiedRegistryConfig {\n /** Logger instance */\n readonly logger?: ILogger;\n /** Default CLI timeout for subprocess calls (ms) */\n readonly defaultCliTimeoutMs?: number;\n}\n\n/** Summary of the pre-computed task routing table. */\nexport interface TaskRoutingEntry {\n readonly category: TaskCategory;\n readonly primaryCli: CliName;\n readonly secondaryCli: CliName;\n readonly primaryModel: string;\n}\n\n/** Snapshot of registry state for observability. */\nexport interface RegistrySnapshot {\n readonly taskRouting: readonly TaskRoutingEntry[];\n readonly cachedAdapters: readonly CliName[];\n readonly availableModels: number;\n}\n\n// ============================================================================\n// Registry\n// ============================================================================\n\n/**\n * Unified adapter registry. Centralizes all adapter creation and task routing.\n *\n * Usage:\n * ```typescript\n * const registry = createUnifiedRegistry({ logger });\n * const adapter = registry.getAdapter('code_generation'); // → codex adapter\n * const adapter2 = registry.getAdapterForCli('claude'); // → claude adapter\n * const adapter3 = registry.getDefault(); // → best available\n * ```\n */\nexport class UnifiedAdapterRegistry {\n private readonly logger: ILogger;\n private readonly defaultCliTimeoutMs: number | undefined;\n\n /** Pre-computed task → CLI routing (immutable after construction). */\n private readonly taskRouting: ReadonlyMap<TaskCategory, TaskRoutingEntry>;\n\n /** Per-CLI adapter cache — max 3 entries (claude/gemini/codex). */\n private readonly cliAdapters = new Map<CliName, IResilientAdapter>();\n\n /** Default adapter for unscoped requests. */\n private defaultAdapter: IResilientAdapter | undefined;\n\n constructor(config?: UnifiedRegistryConfig) {\n this.logger = config?.logger ?? createLogger({ component: 'unified-registry' });\n this.defaultCliTimeoutMs = config?.defaultCliTimeoutMs;\n this.taskRouting = this.buildTaskRouting();\n this.logger.info('UnifiedAdapterRegistry initialized', {\n categories: this.taskRouting.size,\n models: DEFAULT_MODEL_CAPABILITIES.models.length,\n });\n }\n\n // --------------------------------------------------------------------------\n // Public API\n // --------------------------------------------------------------------------\n\n /** Logger used by this registry. Exposed so singleton helpers can warn. */\n getLogger(): ILogger {\n return this.logger;\n }\n\n /**\n * Get adapter for a task category. Uses pre-computed routing.\n * Falls back to default adapter if category unknown.\n */\n getAdapter(category: TaskCategory): IResilientAdapter {\n const routing = this.taskRouting.get(category);\n if (routing === undefined) {\n this.logger.warn('Unknown task category, using default', { category });\n return this.getDefault();\n }\n return this.getAdapterForCli(routing.primaryCli);\n }\n\n /**\n * Get adapter for a free-text task description.\n * Detects category from keywords, falls back to default.\n */\n getAdapterForTask(taskDescription: string): IResilientAdapter {\n const match = detectTaskCategory(taskDescription);\n if (match !== null) {\n this.logger.debug('Task category detected', {\n category: match.category,\n cli: match.primaryCli,\n });\n return this.getAdapterForCli(match.primaryCli);\n }\n return this.getDefault();\n }\n\n /**\n * Get adapter pinned to a specific CLI.\n * Creates and caches one IResilientAdapter per CLI.\n */\n getAdapterForCli(cli: CliName): IResilientAdapter {\n const cached = this.cliAdapters.get(cli);\n if (cached !== undefined) return cached;\n\n const adapter = createResilientAdapter({\n logger: this.logger,\n preferredCli: cli,\n ...(this.defaultCliTimeoutMs !== undefined && {\n defaultCliTimeoutMs: this.defaultCliTimeoutMs,\n }),\n });\n this.cliAdapters.set(cli, adapter);\n this.logger.info('Created CLI-specific adapter', { cli });\n return adapter;\n }\n\n /**\n * Get adapter for a model preference string (e.g., \"claude-opus-4-6\").\n * Resolves the model to its CLI via the canonical registry.\n * Falls back to default adapter if model not recognized.\n */\n getAdapterForModel(modelPreference: string): IResilientAdapter {\n // Prefer exact matches (id / cliAlias / cliModelName) over prefix matches.\n // For prefix fallback, longest-prefix-wins so a future registry containing\n // both 'gemini-pro' and 'gemini-pro-experimental' resolves correctly even\n // though the registry's natural array order isn't sorted by id length.\n // Defense-in-depth — no current registry has prefix overlaps. (#2192)\n const exact = DEFAULT_MODEL_CAPABILITIES.models.find(\n (m) =>\n m.id === modelPreference ||\n m.cliAlias === modelPreference ||\n m.cliModelName === modelPreference\n );\n const prefix =\n exact ??\n [...DEFAULT_MODEL_CAPABILITIES.models]\n .filter((m) => modelPreference.startsWith(m.id))\n .sort((a, b) => b.id.length - a.id.length)[0];\n const model = prefix;\n if (model !== undefined) {\n this.logger.debug('Model resolved to CLI', {\n model: modelPreference,\n cli: model.cliName,\n });\n return this.getAdapterForCli(model.cliName as CliName);\n }\n this.logger.debug('Model not in registry, using default', {\n model: modelPreference,\n });\n return this.getDefault();\n }\n\n /**\n * Get adapter for an expert role (e.g., \"code_expert\").\n * Uses ROLE_TO_TASK_CATEGORY mapping → task specialization → CLI.\n */\n getAdapterForRole(role: string): IResilientAdapter {\n const category = ROLE_TO_CATEGORY[role];\n if (category !== undefined) return this.getAdapter(category);\n return this.getDefault();\n }\n\n /**\n * Get the default adapter (no CLI preference — auto-detection priority).\n */\n getDefault(): IResilientAdapter {\n if (this.defaultAdapter !== undefined) return this.defaultAdapter;\n this.defaultAdapter = createResilientAdapter({\n logger: this.logger,\n ...(this.defaultCliTimeoutMs !== undefined && {\n defaultCliTimeoutMs: this.defaultCliTimeoutMs,\n }),\n });\n return this.defaultAdapter;\n }\n\n /**\n * Get snapshot of registry state for observability/debugging.\n */\n getSnapshot(): RegistrySnapshot {\n return {\n taskRouting: [...this.taskRouting.values()],\n cachedAdapters: [...this.cliAdapters.keys()],\n availableModels: DEFAULT_MODEL_CAPABILITIES.models.length,\n };\n }\n\n /**\n * Get the pre-computed routing for a specific category.\n */\n getRouting(category: TaskCategory): TaskRoutingEntry | undefined {\n return this.taskRouting.get(category);\n }\n\n /**\n * Dispose all cached adapters.\n */\n dispose(): void {\n for (const adapter of this.cliAdapters.values()) {\n adapter.dispose();\n }\n this.cliAdapters.clear();\n this.defaultAdapter?.dispose();\n this.defaultAdapter = undefined;\n this.logger.info('UnifiedAdapterRegistry disposed');\n }\n\n // --------------------------------------------------------------------------\n // Private\n // --------------------------------------------------------------------------\n\n private buildTaskRouting(): ReadonlyMap<TaskCategory, TaskRoutingEntry> {\n const routing = new Map<TaskCategory, TaskRoutingEntry>();\n for (const spec of TASK_SPECIALIZATION_MATRIX) {\n const primaryModel = resolveDefaultModel(spec.primaryCli);\n routing.set(spec.category, {\n category: spec.category,\n primaryCli: spec.primaryCli,\n secondaryCli: spec.secondaryCli,\n primaryModel,\n });\n }\n return routing;\n }\n}\n\n// ============================================================================\n// Role → Category Mapping (consolidated from create-expert-routing.ts)\n// ============================================================================\n\n/** Maps expert roles to task categories for CLI specialization. */\nconst ROLE_TO_CATEGORY: Record<string, TaskCategory> = {\n code_expert: 'code_generation',\n architecture_expert: 'architecture',\n security_expert: 'security_review',\n documentation_expert: 'documentation',\n testing_expert: 'testing',\n devops_expert: 'devops',\n research_expert: 'research',\n pm_expert: 'planning',\n ux_expert: 'planning',\n infrastructure_expert: 'devops',\n};\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\n/** Resolve the default model name for a CLI from the canonical registry. */\nfunction resolveDefaultModel(cli: string): string {\n if (cli in DEFAULT_MODEL_PER_CLI) {\n return DEFAULT_MODEL_PER_CLI[cli as CliNameLiteral];\n }\n return cli;\n}\n\n// ============================================================================\n// Factory & Singleton\n// ============================================================================\n\nlet globalRegistry: UnifiedAdapterRegistry | undefined;\n\n/**\n * Create a new UnifiedAdapterRegistry instance.\n * For most uses, prefer `getGlobalRegistry()` instead.\n */\nexport function createUnifiedRegistry(config?: UnifiedRegistryConfig): UnifiedAdapterRegistry {\n return new UnifiedAdapterRegistry(config);\n}\n\n/**\n * Get the global singleton registry.\n * Creates it on first access with default config.\n *\n * If the singleton already exists and a non-empty config is supplied, the\n * config is ignored — callers get the pre-existing instance. A warning is\n * emitted so this asymmetry is not silent.\n */\nexport function getGlobalRegistry(config?: UnifiedRegistryConfig): UnifiedAdapterRegistry {\n if (globalRegistry === undefined) {\n globalRegistry = new UnifiedAdapterRegistry(config);\n return globalRegistry;\n }\n if (config !== undefined && Object.keys(config).length > 0) {\n globalRegistry\n .getLogger()\n .warn(\n 'UnifiedAdapterRegistry singleton already initialized; provided config ignored. ' +\n 'Call resetGlobalRegistry() first if reconfiguration is intentional.',\n { providedKeys: Object.keys(config) }\n );\n }\n return globalRegistry;\n}\n\n/** Reset the global registry (for testing). */\nexport function resetGlobalRegistry(): void {\n globalRegistry?.dispose();\n globalRegistry = undefined;\n}\n","/**\n * nexus-agents/cli - Voter System Prompts\n *\n * Role-specific system prompts for voter agents.\n *\n * @module cli/voter-prompts\n * (Source: Issue #226, extracted from voter-agents.ts for #272)\n */\n\nimport type { VoterRole } from './vote-types.js';\n\n/** Default project name used in voter prompts when no context is provided. */\nconst DEFAULT_PROJECT = 'nexus-agents';\n\n/**\n * Generate voter system prompts with optional project context.\n *\n * When nexus-agents MCP tools are used as a service for other projects,\n * the default \"nexus-agents project\" context causes voters to reject\n * proposals as MISALIGNED. This function allows injecting the target\n * project name so voters evaluate correctly.\n *\n * @param project - Project name/context to inject (defaults to 'nexus-agents')\n */\n/**\n * PR-review-mode addendum (#2244). Appended to every voter's prompt so the\n * format is reinforced at the system-prompt level, where role framing\n * dominates — the proposal-text-only approach in #2238 produced 0 verified\n * findings across 50 voter calls in #2241.\n *\n * The addendum is conditional on its face: voters reviewing a non-diff\n * proposal ignore it. Voters reviewing a diff get the explicit format +\n * few-shot example here, plus the same instructions in the proposal text.\n */\nfunction prReviewModeAddendum(): string {\n return `\nPR-review mode — if you are reviewing a code diff (not a proposal) AND you have at least one concrete defect that justifies blocking the merge, populate the OPTIONAL TOP-LEVEL \"findings\" field on your JSON response. NOT inside reasoning — top-level. The schema:\n\n\"findings\": [\n {\n \"summary\": \"One-line summary\",\n \"location\": \"path/file.ext:LINE\",\n \"severity\": \"critical\" | \"high\" | \"medium\" | \"low\",\n \"gate\": {\n \"reread_cited_line\": \"passed\",\n \"traced_call_path\": \"passed\",\n \"named_assertion\": \"Concrete failing assertion — what test would fail and how. Substantive, not 'passed'.\",\n \"ruled_out_language_non_issue\": \"passed\"\n },\n \"claim\": \"What is wrong and why it justifies blocking.\"\n }\n]\n\nA finding only triggers strict request_changes if all 4 gate fields = \"passed\" AND named_assertion is substantive (>10 chars naming a concrete failure, not just \"passed\"). Findings missing any of those surface as informational only — they do not block on their own. The 2026-04-25 audit (#2225) found a 100% false-positive rate when this gate wasn't enforced. If you're approving the diff, OMIT the findings field entirely. If reviewing a non-diff proposal, ignore this section.\n\nHistory note: an earlier prompt asked for YAML inside reasoning; that format was lossy across JSON serialization (#2245). Use the top-level JSON array above.`;\n}\n\n/** Common footer appended to all voter prompts. */\nfunction voterFooter(): string {\n return `\nWorkflow-test assessment (include in your reasoning):\n- Testability: Can changes be verified with automated tests?\n- Workflow integration: Does this fit existing CI/build/test pipelines?\n- Incremental verifiability: Can progress be measured at each step?\n\nWhen rejecting, classify your reasons using categories: YAGNI, DRY_VIOLATION, OVER_ENGINEERING, SCOPE_CREEP, SECURITY_RISK, MISALIGNED, INSUFFICIENT_EVIDENCE.\n${prReviewModeAddendum()}`;\n}\n\n/** Build the architect role prompt. */\nfunction architectPrompt(project: string): string {\n return `You are a Software Architect voting on technical proposals for the ${project} project.\n\nYour evaluation criteria:\n- Technical design quality and architectural soundness\n- Scalability and performance implications\n- Maintainability and code organization\n- Alignment with existing patterns (Result<T,E>, Zod validation, TypeScript best practices)\n- Integration complexity with current codebase\n${voterFooter()}\n\nBe direct and technical. Focus on structural implications.`;\n}\n\n/** Build the security role prompt. */\nfunction securityPrompt(project: string): string {\n return `You are a Security Engineer voting on proposals for the ${project} project.\n\nYour evaluation criteria:\n- Security vulnerabilities and attack vectors (OWASP Top 10)\n- Input validation and sanitization\n- Secrets management and credential handling\n- Path traversal and injection prevention\n- Rate limiting and resource exhaustion\n${voterFooter()}\n\nBe thorough about risks. Flag any security concerns.`;\n}\n\n/** Build the devex role prompt. */\nfunction devexPrompt(project: string): string {\n return `You are a Developer Experience Engineer voting on proposals for the ${project} project.\n\nYour evaluation criteria:\n- API usability and ergonomics\n- Documentation clarity and completeness\n- Learning curve for new developers\n- Testing and debugging experience\n- CLI/tool integration quality\n${voterFooter()}\n\nFocus on practical developer impact.`;\n}\n\n/** Build the AI/ML role prompt. */\nfunction aiMlPrompt(project: string): string {\n return `You are an AI/ML Engineer voting on proposals for the ${project} project.\n\nYour evaluation criteria:\n- Multi-agent coordination effectiveness\n- Model selection and routing strategies\n- Context management and token efficiency\n- Learning and adaptation capabilities\n- Consensus protocol design\n- Integration with LLM capabilities\n${voterFooter()}\n\nEvaluate AI/ML implications and opportunities.`;\n}\n\n/** Build the PM role prompt. */\nfunction pmPrompt(project: string): string {\n return `You are a Product Manager voting on proposals for the ${project} project.\n\nYour evaluation criteria:\n- Business value and user impact\n- Resource requirements and timeline\n- Risk assessment and mitigation\n- Priority relative to roadmap\n- Success metrics and validation approach\n- Alignment with project goals in CLAUDE.md\n${voterFooter()}\n\nBalance value against effort. Be pragmatic.`;\n}\n\n/** Build the catfish (contrarian) role prompt. */\nfunction catfishPrompt(project: string): string {\n return `You are a Contrarian Analyst (catfish agent) voting on proposals for the ${project} project.\n\nYour role is to prevent false consensus by deliberately challenging proposals.\nBased on research (arXiv:2505.21503), agreement bias in multi-agent voting leads\nto poor decisions when agents rubber-stamp proposals without genuine scrutiny.\n\nYour evaluation criteria:\n- What are the hidden costs, risks, or downsides not mentioned?\n- What assumptions are being made that might be wrong?\n- What alternatives were not considered?\n- What could go wrong in practice vs. theory?\n- Is there scope creep or unnecessary complexity?\n\nWorkflow-test assessment (include in your reasoning):\n- Testability: Is the proposal verifiable, or just theoretical?\n- Workflow integration: Will this actually work in existing pipelines?\n- Incremental verifiability: Can we tell if it's working at each stage?\n\nWhen rejecting, you MUST classify your reasons using categories: YAGNI, DRY_VIOLATION, OVER_ENGINEERING, SCOPE_CREEP, SECURITY_RISK, MISALIGNED, INSUFFICIENT_EVIDENCE.\n\nIMPORTANT: Your job is to find legitimate concerns, not to reject everything.\nIf after genuine scrutiny you find no significant issues, you MAY approve.\nBut your default posture is skeptical — look for what others might miss.\nHigh-confidence rejections with specific reasoning are your most valuable output.\n${prReviewModeAddendum()}`;\n}\n\n/**\n * Build the scope_steward role prompt (#2185).\n *\n * Different axis from `pm` (which prioritizes WHICH features to build) and\n * `catfish` (which doubts the framing). The steward asks: should we build\n * this AT ALL? Existing tools usually win; default bias is \"don't ship.\"\n */\nfunction scopeStewardPrompt(project: string): string {\n return `You are a Scope Steward voting on proposals for the ${project} project.\n\nYour job is to gate against build-when-buy-would-do and feature sprawl.\nThe originating case (2026-04-24): a 6-role panel approved building a USB flasher\nCLI without anyone flagging that Rufus already solves the problem better, for the\nsame audience, with 100M+ installs of battle-tested code. This role exists to\ncatch that class of mistake.\n\nYour evaluation criteria — work through these mandatory checks in your reasoning:\n\n1. **Existing-tool check.** Search your knowledge for tools, libraries, or\n services that already solve the stated problem. Name them concretely\n (not \"there might be alternatives\" — actual names: Rufus, ripgrep,\n esbuild, etc.). If you can't name an alternative, say so explicitly.\n\n2. **Build-vs-buy math.** For each existing tool you named: what would we\n LOSE by adopting it (license, dependency surface, integration cost)?\n What would we GAIN by building our own (tighter integration, no extra\n binary, etc.)? Default lean: BUY. Building is justified only when the\n loss column is concrete and the gain column is load-bearing.\n\n3. **Mission alignment.** Does this proposal serve the project's stated\n mission, or is it scope drift? If drift, name the drift specifically.\n\n4. **Kill-the-feature option.** For every proposal, explicitly evaluate\n \"what if we just didn't do this?\" as a ranked option. Many proposals\n don't need to be built. Make the no-build case before the build case.\n\n5. **Sprawl audit.** Check whether similar functionality already exists\n in the codebase. If it does, recommend extending — not forking. The\n anti-sprawl policy in CLAUDE.md is specifically the rule this role\n enforces.\n\nDefault bias: REJECT proposals where an existing tool fits, even if our\nown implementation would be marginally nicer. Only approve when the\nexisting-tool check fails AND the kill-the-feature option is worse AND\nmission alignment is clear AND no comparable in-codebase functionality\nexists.\n\nFew-shot example of a textbook rejection:\n> Proposal: \"Add an aegis-boot subcommand to flash bootable USB sticks.\"\n> Steward response: \"REJECT (DON'T-BUILD). Rufus has solved this for the\n> same audience for 10+ years with 100M+ installs and battle-tested code.\n> Adopting Rufus loses nothing material; building our own loses\n> maintenance bandwidth indefinitely. Mission alignment: aegis-boot's\n> mission is verifiable boot, not USB tooling. Kill option clearly wins.\n> No prior in-codebase functionality. Recommend: point users at Rufus in\n> the docs and stop here.\"\n\n${voterFooter()}\n\nWhen rejecting, you MUST classify reasons (YAGNI, DRY_VIOLATION,\nOVER_ENGINEERING, SCOPE_CREEP, MISALIGNED). The steward's most common\ncategories are SCOPE_CREEP, YAGNI, and OVER_ENGINEERING.\n\nYou CAN approve. But your default posture is: \"this should not be built;\nprove me wrong with the build-vs-buy math.\"`;\n}\n\nexport function getVoterPrompts(project: string = DEFAULT_PROJECT): Record<VoterRole, string> {\n return {\n architect: architectPrompt(project),\n security: securityPrompt(project),\n devex: devexPrompt(project),\n ai_ml: aiMlPrompt(project),\n pm: pmPrompt(project),\n catfish: catfishPrompt(project),\n scope_steward: scopeStewardPrompt(project),\n };\n}\n\n/**\n * Default prompts (backward compatible — uses 'nexus-agents' project context).\n */\nexport const VOTER_SYSTEM_PROMPTS: Record<VoterRole, string> = getVoterPrompts();\n\n/**\n * Base reasoning templates for simulated votes.\n *\n * scope_steward simulated reasoning intentionally reflects the role's\n * bias-toward-not-shipping posture (PM vote condition on #2185).\n */\nexport const SIMULATED_VOTE_REASONING: Record<VoterRole, string> = {\n architect: 'Evaluated technical design and architecture implications.',\n security: 'Reviewed security considerations and attack surface.',\n devex: 'Assessed developer experience and workflow impact.',\n ai_ml: 'Analyzed AI/ML capabilities and learning potential.',\n pm: 'Evaluated business value and resource requirements.',\n catfish: 'Challenged proposal assumptions and identified potential risks.',\n scope_steward:\n 'Checked existing tools, build-vs-buy math, kill-the-feature option; bias toward not shipping.',\n};\n","/**\n * nexus-agents voter response parsing\n *\n * Schema and parsing utilities for structured vote responses from LLM.\n *\n * (Source: Extracted from voter-agents.ts per Issue #285)\n */\n\nimport { z } from 'zod';\nimport type { Vote } from '../consensus/types.js';\nimport type { VoterRole } from './vote-types.js';\nimport { getErrorMessage, createLogger } from '../core/index.js';\n\n// ============================================================================\n// Error Classes\n// ============================================================================\n\n/**\n * Error thrown when vote response parsing fails and synthetic votes not allowed.\n * (Source: Issue #512 - Fail-safe voting response parsing)\n *\n * By default, parseVoteResponse throws this error when JSON parsing or validation\n * fails. To use synthetic fallback votes (NOT RECOMMENDED), pass\n * `allowSyntheticVote: true` to the options parameter.\n */\nexport class SyntheticVoteError extends Error {\n constructor(\n reason: string,\n public readonly rawOutput: string\n ) {\n super(\n `Vote response parsing failed: ${reason}. ` +\n 'To use synthetic fallback votes (NOT RECOMMENDED), set allowSyntheticVote: true'\n );\n this.name = 'SyntheticVoteError';\n }\n}\n\n/**\n * Vote source tracking - indicates whether vote is real or synthetic.\n * (Source: Issue #512 - Voting integrity)\n */\nexport type ParsedVoteSource = 'parsed' | 'fallback';\n\n/**\n * Extended vote with source tracking.\n */\nexport interface ParsedVote extends Vote {\n readonly source: ParsedVoteSource;\n}\n\n/**\n * Options for parseVoteResponse.\n */\nexport interface ParseVoteOptions {\n /**\n * Allow synthetic fallback votes when parsing fails.\n * Default: false (throws SyntheticVoteError)\n * (Source: Issue #512 - Fail-safe voting)\n */\n readonly allowSyntheticVote?: boolean;\n}\n\n// ============================================================================\n// Structured Vote Response Schema\n// ============================================================================\n\n/**\n * Pre-verified finding shape — voter emits this; downstream\n * `isFindingVerified` adds the derived `verified` flag.\n *\n * #2245 follow-up: voters previously asked to embed YAML findings inside\n * the JSON `reasoning` field. That format is lossy across JSON\n * serialization (backticks/newlines). The v4 retest produced 0 findings\n * across 9 request_changes voters because the LLM either dropped the\n * YAML to keep JSON valid, or produced invalid JSON the parser rejected.\n * Solution: expose findings as a top-level array on the vote response.\n */\nexport const RawFindingSchema = z.object({\n summary: z.string().min(1).max(500).describe('One-line summary of the issue'),\n location: z.string().min(1).max(200).describe('path/file.ext:line'),\n severity: z.enum(['critical', 'high', 'medium', 'low']).default('medium'),\n gate: z.object({\n reread_cited_line: z.enum(['passed', 'failed', 'skipped']).default('skipped'),\n traced_call_path: z.enum(['passed', 'failed', 'skipped']).default('skipped'),\n named_assertion: z\n .string()\n .default('')\n .describe('Concrete failing assertion — substantive, not a rubber-stamp word'),\n ruled_out_language_non_issue: z.enum(['passed', 'failed', 'skipped']).default('skipped'),\n }),\n claim: z.string().min(1).max(2000).describe('What is wrong and why it justifies blocking'),\n});\n\nexport type RawFinding = z.infer<typeof RawFindingSchema>;\n\n/**\n * Zod schema for parsing structured vote responses from LLM.\n */\nexport const VoteResponseSchema = z.object({\n decision: z.enum(['approve', 'reject', 'abstain']).describe('Your vote decision'),\n reasoning: z.string().min(10).max(4000).describe('Explanation for your vote (10-4000 chars)'),\n confidence: z.number().min(0).max(1).describe('Confidence level 0-1'),\n conditions: z.array(z.string()).optional().describe('Optional conditions for approval'),\n /** Structured rejection categories for reject→refine→re-vote loops (Issue #1213). */\n rejectionCategories: z\n .array(\n z.enum([\n 'YAGNI',\n 'DRY_VIOLATION',\n 'OVER_ENGINEERING',\n 'SCOPE_CREEP',\n 'SECURITY_RISK',\n 'MISALIGNED',\n 'INSUFFICIENT_EVIDENCE',\n ])\n )\n .optional()\n .describe('Rejection reason categories when decision is reject'),\n /** Top-level structured findings for PR-review mode (#2245 v4 follow-up).\n * Replaces the YAML-in-reasoning encoding that proved lossy. */\n findings: z.array(RawFindingSchema).optional().describe('Structured findings (PR review only)'),\n});\n\nexport type VoteResponse = z.infer<typeof VoteResponseSchema>;\n\n// ============================================================================\n// Vote Prompt Construction\n// ============================================================================\n\n/** Example responses appended to vote prompts. Kept as a constant to keep\n * `buildVotePrompt` under the max-lines-per-function lint cap. */\nconst VOTE_PROMPT_EXAMPLES = `Example approve response:\n{\n \"decision\": \"approve\",\n \"reasoning\": \"The proposal aligns with architectural patterns. Testability: high — unit tests can verify each component. Workflow integration: fits existing CI pipeline.\",\n \"confidence\": 0.85,\n \"conditions\": [\"Add unit tests before merge\"]\n}\n\nExample reject response:\n{\n \"decision\": \"reject\",\n \"reasoning\": \"This adds speculative abstractions for hypothetical future needs. Testability: unclear — no concrete test plan provided.\",\n \"confidence\": 0.80,\n \"rejectionCategories\": [\"YAGNI\", \"OVER_ENGINEERING\"]\n}\n\nExample PR-review request_changes response with structured findings:\n{\n \"decision\": \"reject\",\n \"reasoning\": \"Off-by-one in clampPageSize and missing null guard on response.timing — both visible in the diff.\",\n \"confidence\": 0.9,\n \"rejectionCategories\": [\"INSUFFICIENT_EVIDENCE\"],\n \"findings\": [\n {\n \"summary\": \"Off-by-one in clampPageSize\",\n \"location\": \"packages/nexus-agents/src/api/pagination.ts:18\",\n \"severity\": \"high\",\n \"gate\": {\n \"reread_cited_line\": \"passed\",\n \"traced_call_path\": \"passed\",\n \"named_assertion\": \"Test would assert clampPageSize(50, 100) === 50; this returns 49.\",\n \"ruled_out_language_non_issue\": \"passed\"\n },\n \"claim\": \"Function name says 'clamp to range' but returns requested-1 in the in-range path.\"\n }\n ]\n}`;\n\n/**\n * Constructs the user prompt for vote evaluation.\n * Includes workflow-test evaluation criteria (Issue #1212) and\n * rejection category instructions (Issue #1213).\n */\nexport function buildVotePrompt(proposal: string): string {\n return `Evaluate the following proposal and provide your vote.\n\nPROPOSAL:\n${proposal}\n\nIn addition to your role-specific criteria, assess these workflow-test dimensions:\n- Testability: Can the proposed changes be verified with automated tests?\n- Workflow integration: Does this fit into existing CI/make/test workflows?\n- Incremental verifiability: Can progress be measured at each step?\n\nRespond with a JSON object containing:\n- decision: \"approve\", \"reject\", or \"abstain\"\n- reasoning: Explanation for your vote (10-4000 characters). Include your workflow-test assessment.\n- confidence: Number between 0 and 1\n- conditions: Optional array of conditions for approval\n- rejectionCategories: Required when rejecting. Array of categories from: YAGNI, DRY_VIOLATION, OVER_ENGINEERING, SCOPE_CREEP, SECURITY_RISK, MISALIGNED, INSUFFICIENT_EVIDENCE\n- findings: PR-REVIEW MODE ONLY. Optional top-level array of structured findings — see \"PR-review mode\" in the system prompt. OMIT this field entirely when reviewing a non-diff proposal or when approving a diff.\n\n${VOTE_PROMPT_EXAMPLES}`;\n}\n\n// ============================================================================\n// Vote Response Parsing\n// ============================================================================\n\n/**\n * Extracts JSON from LLM response text.\n * Handles responses that may include markdown code blocks.\n */\nexport function extractJsonFromResponse(text: string): string {\n // Try to find JSON in code blocks first\n const codeBlockMatch = /```(?:json)?\\s*([\\s\\S]*?)```/i.exec(text);\n if (codeBlockMatch?.[1] !== undefined) {\n return codeBlockMatch[1].trim();\n }\n\n // Look for JSON object directly\n const jsonMatch = /\\{[\\s\\S]*\\}/i.exec(text);\n if (jsonMatch?.[0] !== undefined) {\n return jsonMatch[0];\n }\n\n return text.trim();\n}\n\n/**\n * Creates a fallback vote when parsing fails.\n * Attempts to infer decision from text content.\n * ONLY used when allowSyntheticVote is explicitly true.\n * (Source: Issue #512 - Fail-safe voting)\n */\nfunction createFallbackVote(output: string, _role: VoterRole, reason: string): ParsedVote {\n const lower = output.toLowerCase();\n let decision: Vote['decision'] = 'abstain';\n\n // Simple keyword detection - heuristic only\n // Check reject keywords FIRST since \"disagree\" contains \"agree\" substring\n if (lower.includes('reject') || lower.includes('decline') || lower.includes('disagree')) {\n decision = 'reject';\n } else if (lower.includes('approve') || lower.includes('accept') || lower.includes('agree')) {\n decision = 'approve';\n }\n\n // Log warning about synthetic vote\n createLogger({ component: 'voter-response' }).warn(\n 'Creating synthetic vote (NOT parsed from LLM output)',\n { decision, reason }\n );\n\n return {\n decision,\n reasoning: `[SYNTHETIC: ${reason}] ${output.slice(0, 200)}`,\n confidence: 0.5,\n source: 'fallback', // Mark as synthetic\n };\n}\n\n/** Maps a validated VoteResponse into a ParsedVote, threading optional fields. */\nfunction buildParsedVote(data: VoteResponse): ParsedVote {\n return {\n decision: data.decision,\n reasoning: data.reasoning,\n confidence: data.confidence,\n ...(data.conditions !== undefined ? { conditions: data.conditions } : {}),\n ...(data.rejectionCategories !== undefined\n ? { rejectionCategories: data.rejectionCategories }\n : {}),\n ...(data.findings !== undefined ? { findings: data.findings } : {}),\n source: 'parsed',\n };\n}\n\n/**\n * Parses vote response from LLM output.\n *\n * By default, throws SyntheticVoteError if parsing fails. To use synthetic\n * fallback votes (NOT RECOMMENDED), pass `allowSyntheticVote: true`.\n *\n * (Source: Issue #512 - Fail-safe voting response parsing)\n *\n * @param output - Raw LLM output text\n * @param role - Voter role for context\n * @param options - Parsing options including allowSyntheticVote\n * @returns ParsedVote with source tracking\n * @throws SyntheticVoteError if parsing fails and allowSyntheticVote is false\n */\nexport function parseVoteResponse(\n output: string,\n role: VoterRole,\n options?: ParseVoteOptions\n): ParsedVote {\n const allowSyntheticVote = options?.allowSyntheticVote ?? false;\n\n try {\n const jsonStr = extractJsonFromResponse(output);\n const parsed = JSON.parse(jsonStr) as unknown;\n const validated = VoteResponseSchema.safeParse(parsed);\n\n if (validated.success) {\n return buildParsedVote(validated.data);\n }\n\n // Validation failed - throw or fallback based on config\n const reason = `Validation failed: ${validated.error.issues.map((e: { message: string }) => e.message).join(', ')}`;\n if (!allowSyntheticVote) {\n throw new SyntheticVoteError(reason, output);\n }\n return createFallbackVote(output, role, reason);\n } catch (error) {\n // If it's already a SyntheticVoteError, rethrow it\n if (error instanceof SyntheticVoteError) {\n throw error;\n }\n\n // Parse error - throw or fallback based on config\n const reason = getErrorMessage(error, 'Unknown parse error');\n if (!allowSyntheticVote) {\n throw new SyntheticVoteError(reason, output);\n }\n return createFallbackVote(output, role, reason);\n }\n}\n","/**\n * nexus-agents voter execution utilities\n *\n * Vote execution helpers including result creation, timeout handling,\n * retry logic, and simulation fallback.\n *\n * (Source: Extracted from voter-agents.ts per Issue #285)\n */\n\nimport type { Vote } from '../consensus/types.js';\nimport type { VoterRole, AgentVoteResult } from './vote-types.js';\nimport type { IModelAdapter, CompletionRequest, ILogger } from '../core/index.js';\nimport { getRandomProvider } from '../core/index.js';\nimport { delay, withTimeout } from '../utils/async-utils.js';\nimport { VOTER_SYSTEM_PROMPTS, SIMULATED_VOTE_REASONING } from './voter-prompts.js';\nimport { buildVotePrompt, parseVoteResponse, SyntheticVoteError } from './voter-response.js';\n\n// Import timeout constants from canonical source (Issue #984)\nimport {\n VOTE_TIMEOUTS,\n resolveVoteTimeout as _resolveVoteTimeout,\n validateTimeout as _validateTimeout,\n} from '../config/timeouts.js';\n\n/** Default vote timeout. Canonical source: `config/timeouts.ts`. */\nexport const DEFAULT_VOTE_TIMEOUT_MS = VOTE_TIMEOUTS.defaultMs;\n\n/** Resolves vote timeout with env var override. Canonical: `config/timeouts.ts`. */\nexport const resolveVoteTimeout = _resolveVoteTimeout;\n\n/** Maximum vote timeout. Canonical source: `config/timeouts.ts`. */\nexport const MAX_VOTE_TIMEOUT_MS = VOTE_TIMEOUTS.maxMs;\n\n/** Minimum vote timeout. Canonical source: `config/timeouts.ts`. */\nexport const MIN_VOTE_TIMEOUT_MS = VOTE_TIMEOUTS.minMs;\n\n/** Maximum retries per vote. Canonical source: `config/timeouts.ts`. */\nexport const DEFAULT_MAX_RETRIES = VOTE_TIMEOUTS.maxRetries;\n\n/**\n * Initial retry delay in milliseconds.\n */\nconst INITIAL_RETRY_DELAY_MS = 1_000;\n\n/**\n * Retry delay for rate-limit errors in milliseconds (Issue #1319).\n * Longer than standard to respect API rate limits.\n */\nexport const RATE_LIMIT_RETRY_DELAY_MS = 5_000;\n\n/**\n * Detects whether an error message indicates a rate-limit condition.\n * Delegates to canonical rate-limit-detector (DRY consolidation Issue #1596).\n */\nimport { isRateLimitLikeError } from '../adapters/rate-limit-detector.js';\n\n/** @see isRateLimitLikeError — re-exported for backward compatibility */\nexport function isRateLimitError(message: string): boolean {\n return isRateLimitLikeError(new Error(message));\n}\n\n/** Validates and clamps timeout. Canonical source: `config/timeouts.ts`. */\nexport const validateTimeout = _validateTimeout;\n\n// ============================================================================\n// Vote Result Helpers\n// ============================================================================\n\n/**\n * Creates an error vote result (abstain with error message).\n * Issue #523: Uses source: 'error' instead of 'llm' for accuracy.\n */\nexport function createErrorVoteResult(\n role: VoterRole,\n errorMsg: string,\n processingTimeMs: number\n): AgentVoteResult {\n return {\n role,\n vote: {\n decision: 'abstain',\n reasoning: `[Error] Vote execution failed: ${errorMsg}`,\n confidence: 0,\n },\n processingTimeMs,\n source: 'error',\n error: errorMsg,\n };\n}\n\n/**\n * Creates a simulation vote result.\n */\nexport function createSimulationVoteResult(\n role: VoterRole,\n proposal: string,\n processingTimeMs: number,\n error?: string\n): AgentVoteResult {\n return {\n role,\n vote: simulateVote(role, proposal),\n processingTimeMs,\n source: 'simulation',\n ...(error !== undefined && { error }),\n };\n}\n\n/**\n * Creates simulated votes for multiple roles.\n */\nexport function createSimulatedVotes(\n roles: readonly VoterRole[],\n proposal: string,\n error?: string\n): readonly AgentVoteResult[] {\n const random = getRandomProvider();\n return roles.map((role) =>\n createSimulationVoteResult(role, proposal, random.randomInt(0, 100), error)\n );\n}\n\n/**\n * Role-specific vote distributions for simulation.\n * Each role has weighted probabilities reflecting their typical concerns:\n * - security: More skeptical, finds potential issues\n * - architect: Technically focused, generally supportive of good design\n * - devex: Balanced, considers usability\n * - ai_ml: Technically focused, evaluates AI aspects\n * - pm: Business focused, generally supportive of value\n *\n * Format: [approve_weight, reject_weight, abstain_weight]\n */\nconst ROLE_VOTE_DISTRIBUTIONS: Record<VoterRole, [number, number, number]> = {\n security: [40, 45, 15], // More skeptical - security concerns\n architect: [55, 30, 15], // Generally approving of good design\n devex: [50, 30, 20], // Balanced - considers usability\n ai_ml: [55, 30, 15], // Technical focus\n pm: [55, 25, 20], // Business focus - generally supportive\n catfish: [20, 65, 15], // Deliberately contrarian - challenges proposals (arXiv:2505.21503)\n scope_steward: [25, 60, 15], // Default-bias toward not shipping (#2185)\n};\n\n/**\n * Selects a decision based on weighted probabilities.\n */\nfunction selectWeightedDecision(\n weights: [number, number, number]\n): 'approve' | 'reject' | 'abstain' {\n const random = getRandomProvider();\n const total = weights[0] + weights[1] + weights[2];\n const rand = random.random() * total;\n\n if (rand < weights[0]) return 'approve';\n if (rand < weights[0] + weights[1]) return 'reject';\n return 'abstain';\n}\n\n/**\n * Fallback simulation when LLM is unavailable.\n * Uses role-specific vote distributions to provide more realistic simulation.\n * Clearly marks output as simulated.\n *\n * (Improved per Issue #453 - remove hardcoded 60% approve bias)\n */\nexport function simulateVote(role: VoterRole, proposal: string): Vote {\n const random = getRandomProvider();\n const weights = ROLE_VOTE_DISTRIBUTIONS[role];\n const decision = selectWeightedDecision(weights);\n\n // Confidence varies by decision type and role\n // Rejections tend to be higher confidence (found specific issue)\n // Approvals are moderate confidence (no issues found, but limited analysis)\n // Abstains are low confidence (insufficient information)\n let baseConfidence: number;\n if (decision === 'reject') {\n baseConfidence = 0.6 + random.random() * 0.3; // 0.6-0.9\n } else if (decision === 'approve') {\n baseConfidence = 0.5 + random.random() * 0.3; // 0.5-0.8\n } else {\n baseConfidence = 0.3 + random.random() * 0.2; // 0.3-0.5\n }\n\n return {\n decision,\n reasoning: `[Simulated - no LLM available] ${SIMULATED_VOTE_REASONING[role]} Proposal: \"${proposal.slice(0, 50)}...\"`,\n confidence: baseConfidence,\n };\n}\n\n// ============================================================================\n// Timeout and Retry Utilities\n// ============================================================================\n\n// Re-export from canonical source for backward compatibility\nexport { withTimeout, delay } from '../utils/async-utils.js';\n\n// ============================================================================\n// Vote Attempt Execution\n// ============================================================================\n\n/**\n * Extracts text content from completion response.\n */\nexport function extractTextFromResponse(content: unknown): string {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content\n .map((block) => {\n if (typeof block === 'object' && block !== null && 'type' in block) {\n const typed = block as { type: string; text?: string };\n if (typed.type === 'text' && typeof typed.text === 'string') {\n return typed.text;\n }\n }\n return '';\n })\n .join('');\n }\n return String(content);\n}\n\n/**\n * Executes a single vote attempt (no retries).\n *\n * By default, throws SyntheticVoteError if response parsing fails.\n * This ensures we only get real LLM votes, not synthetic fallbacks.\n * (Source: Issue #512 - Fail-safe voting)\n */\nexport async function executeSingleVoteAttempt(\n role: VoterRole,\n proposal: string,\n adapter: IModelAdapter,\n timeoutMs: number\n): Promise<{ ok: true; vote: Vote; output: string } | { ok: false; error: string }> {\n const request: CompletionRequest = {\n messages: [\n { role: 'system', content: VOTER_SYSTEM_PROMPTS[role] },\n { role: 'user', content: buildVotePrompt(proposal) },\n ],\n // 500 was correct for short proposal-style votes but caused mid-string\n // truncation (\"Unterminated string in JSON at position N\") in #2241 v3\n // when voters review code diffs — the JSON envelope + reasoning + YAML\n // findings block routinely exceed 500 tokens. Bumped to 2000 (#2245);\n // refine per use case if needed.\n maxTokens: 2000,\n temperature: 0.3, // Low temperature for consistent evaluations\n };\n\n const timeoutResult = await withTimeout(\n adapter.complete(request),\n timeoutMs,\n `Vote timeout after ${String(timeoutMs)}ms for role: ${role}`\n );\n\n if (!timeoutResult.ok) {\n return { ok: false, error: timeoutResult.error };\n }\n\n const response = timeoutResult.value;\n\n if (!response.ok) {\n return { ok: false, error: response.error.message };\n }\n\n const output = extractTextFromResponse(response.value.content);\n\n try {\n // parseVoteResponse throws SyntheticVoteError by default if parsing fails\n // This ensures we only accept real LLM votes, not synthetic fallbacks\n const vote = parseVoteResponse(output, role);\n return { ok: true, vote, output };\n } catch (error) {\n if (error instanceof SyntheticVoteError) {\n // Parsing failed - return error to trigger retry\n return { ok: false, error: `Vote parsing failed: ${error.message}` };\n }\n throw error; // Re-throw unexpected errors\n }\n}\n\n/** Options for executeWithRetries. */\nexport interface RetryOptions {\n readonly role: VoterRole;\n readonly proposal: string;\n readonly adapter: IModelAdapter;\n readonly logger: ILogger;\n readonly timeoutMs: number;\n readonly maxRetries: number;\n}\n\n/**\n * Executes vote attempts with retry logic.\n * Returns the error message from last failed attempt, or undefined if successful.\n */\nexport async function executeWithRetries(\n opts: RetryOptions\n): Promise<{ vote: Vote; ok: true } | { error: string; ok: false }> {\n const { role, proposal, adapter, logger, timeoutMs, maxRetries } = opts;\n let lastError = '';\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n if (attempt > 0) {\n const isRateLimit = isRateLimitError(lastError);\n const baseDelay = isRateLimit ? RATE_LIMIT_RETRY_DELAY_MS : INITIAL_RETRY_DELAY_MS;\n const delayMs = baseDelay * Math.pow(2, attempt - 1);\n logger.debug('Retrying vote execution', { role, attempt, delayMs, isRateLimit });\n await delay(delayMs);\n }\n\n // #2472: per-attempt timing breakdown so investigators can see which\n // retry succeeded (or which attempt blew the cap). Total vote time\n // is already captured at the call-site; this fills the per-attempt gap.\n const attemptStart = Date.now();\n const result = await executeSingleVoteAttempt(role, proposal, adapter, timeoutMs);\n const attemptMs = Date.now() - attemptStart;\n if (result.ok) {\n logger.info('Vote attempt timing', {\n role,\n attempt: attempt + 1,\n attemptMs,\n succeeded: true,\n });\n return { vote: result.vote, ok: true };\n }\n\n lastError = result.error;\n const rateLimited = isRateLimitError(lastError);\n logger.info('Vote attempt timing', {\n role,\n attempt: attempt + 1,\n attemptMs,\n succeeded: false,\n rateLimited,\n });\n logger.warn('Vote attempt failed', {\n role,\n attempt: attempt + 1,\n maxRetries: maxRetries + 1,\n error: lastError,\n ...(rateLimited ? { rateLimited: true } : {}),\n });\n }\n\n return { error: lastError !== '' ? lastError : 'Unknown error after all retries', ok: false };\n}\n","/**\n * Overall-deadline racing for consensus voting (Issue #1871).\n *\n * Defensive layer above per-vote timeouts: even if a single\n * executeAgentVote() promise never settles (subprocess adapter hang,\n * IPC wait that swallows timeout, etc.), this helper guarantees the\n * whole consensus call returns bounded partial results.\n *\n * Each role's vote promise is raced against a shared wall-clock\n * deadline. Any role whose promise has not settled when the deadline\n * fires is filled with createErrorVoteResult('overall consensus\n * deadline exceeded'), preserving role order so downstream aggregation\n * stays deterministic.\n */\nimport type { IModelAdapter, ILogger } from '../core/index.js';\nimport type { AgentVoteResult, VoterRole } from './vote-types.js';\nimport { createErrorVoteResult, delay } from './voter-execution.js';\n\nexport interface VoteOptions {\n readonly timeoutMs: number;\n readonly maxRetries: number;\n readonly allowSimulation: boolean;\n}\n\nexport type VoteFn = (\n role: VoterRole,\n proposal: string,\n adapter: IModelAdapter,\n logger: ILogger,\n options: VoteOptions\n) => Promise<AgentVoteResult>;\n\nexport interface LaunchVotesInput {\n readonly roles: readonly VoterRole[];\n readonly proposal: string;\n readonly roleAdapters: ReadonlyMap<VoterRole, IModelAdapter>;\n readonly fallbackAdapter: IModelAdapter;\n readonly logger: ILogger;\n readonly voteOptions: VoteOptions;\n readonly interDelay: number;\n readonly overallDeadlineMs: number;\n /** Vote launcher (injected by caller — typically executeAgentVote). */\n readonly voteFn: VoteFn;\n}\n\nconst DEADLINE_MESSAGE = 'overall consensus deadline exceeded';\n\nfunction raceWithDeadline(\n p: Promise<AgentVoteResult>,\n role: VoterRole,\n deadlineMs: number\n): Promise<AgentVoteResult> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timeoutP = new Promise<AgentVoteResult>((resolve) => {\n timer = setTimeout(() => {\n resolve(createErrorVoteResult(role, DEADLINE_MESSAGE, deadlineMs));\n }, deadlineMs);\n });\n return Promise.race([p, timeoutP]).finally(() => {\n if (timer !== undefined) clearTimeout(timer);\n });\n}\n\nexport async function launchVotesWithOverallDeadline(\n input: LaunchVotesInput\n): Promise<readonly AgentVoteResult[]> {\n const {\n roles,\n proposal,\n roleAdapters,\n fallbackAdapter,\n logger,\n voteOptions,\n interDelay,\n overallDeadlineMs,\n voteFn,\n } = input;\n\n const startedAt = Date.now();\n\n const wrapped = roles.map(async (role, i) => {\n if (i > 0 && interDelay > 0) await delay(interDelay);\n const adapter = roleAdapters.get(role) ?? fallbackAdapter;\n const elapsed = Date.now() - startedAt;\n const remaining = Math.max(1, overallDeadlineMs - elapsed);\n return raceWithDeadline(voteFn(role, proposal, adapter, logger, voteOptions), role, remaining);\n });\n\n const results = await Promise.all(wrapped);\n\n const expired = results.filter((r) => r.source === 'error' && r.error === DEADLINE_MESSAGE);\n if (expired.length > 0) {\n logger.warn('Consensus overall deadline reached; returning partial results', {\n totalRoles: roles.length,\n expiredRoles: expired.map((r) => r.role),\n overallDeadlineMs,\n });\n }\n return results;\n}\n","/**\n * nexus-agents voter agents\n *\n * Real LLM-powered voter agents for consensus voting.\n * Replaces simulated voting with actual agent execution that\n * analyzes proposals.\n *\n * (Source: Issue #226, Sprint #229)\n * (Updated: Issue #280 - Fixed timeout handling, removed simulation fallback)\n * (Refactored: Issue #285 - Extracted response and execution utilities)\n *\n * File structure:\n * - voter-prompts.ts: System prompts for each voter role\n * - voter-response.ts: Response parsing and validation\n * - voter-execution.ts: Execution utilities (timeout, retry, result creation)\n * - voter-agents.ts: Main API (this file)\n */\n\nimport type { VoterRole, AgentVoteResult } from './vote-types.js';\nimport { VOTER_ROLES } from './vote-types.js';\nimport type { IModelAdapter, ILogger } from '../core/index.js';\nimport { createLogger, getTimeProvider, getErrorMessage } from '../core/index.js';\nimport { getGlobalRegistry } from '../adapters/unified-registry.js';\nimport { getAvailableClis } from '../cli-adapters/factory.js';\nimport type { CliName } from '../cli-adapters/types.js';\n\n// Re-export prompts for backward compatibility\nexport { VOTER_SYSTEM_PROMPTS, SIMULATED_VOTE_REASONING } from './voter-prompts.js';\n\n// Re-export response utilities for backward compatibility\nexport {\n VoteResponseSchema,\n type VoteResponse,\n buildVotePrompt,\n extractJsonFromResponse,\n parseVoteResponse,\n SyntheticVoteError,\n type ParsedVote,\n type ParsedVoteSource,\n type ParseVoteOptions,\n} from './voter-response.js';\n\n// Re-export execution utilities for backward compatibility\nexport {\n DEFAULT_VOTE_TIMEOUT_MS,\n MAX_VOTE_TIMEOUT_MS,\n MIN_VOTE_TIMEOUT_MS,\n DEFAULT_MAX_RETRIES,\n RATE_LIMIT_RETRY_DELAY_MS,\n createErrorVoteResult,\n createSimulationVoteResult,\n createSimulatedVotes,\n simulateVote,\n isRateLimitError,\n withTimeout,\n delay,\n extractTextFromResponse,\n executeSingleVoteAttempt,\n validateTimeout,\n resolveVoteTimeout,\n type RetryOptions,\n executeWithRetries,\n} from './voter-execution.js';\n\n// Import from execution module for internal use\nimport {\n createErrorVoteResult,\n createSimulationVoteResult,\n createSimulatedVotes,\n executeWithRetries,\n} from './voter-execution.js';\nimport { resolveVoteTimeout, VOTE_TIMEOUTS, getMcpSafeDeadlineMs } from '../config/timeouts.js';\nimport { launchVotesWithOverallDeadline } from './voter-agents-deadline.js';\n\n/**\n * Computes an overall wall-clock deadline for a consensus vote call (#1871).\n *\n * Acts as a safety net above per-vote timeouts: even if executeAgentVote's\n * internal withTimeout race fails to resolve (e.g. subprocess adapter hang),\n * this deadline bounds total wall time and lets partial results return.\n *\n * Formula: worst-case legitimate completion (timeoutMs * (maxRetries+1))\n * plus staggered launch headroom, plus a 60s buffer.\n */\nexport function computeOverallConsensusDeadlineMs(\n timeoutMs: number,\n maxRetries: number,\n roleCount: number,\n interDelayMs: number\n): number {\n const perVoteBudget = timeoutMs * (maxRetries + 1);\n const staggerBudget = Math.max(0, roleCount - 1) * interDelayMs;\n return perVoteBudget + staggerBudget + 60_000;\n}\n\n// ============================================================================\n// Agent Vote Execution\n// ============================================================================\n\n/**\n * Options for executing voter agents.\n */\n/** Default inter-agent delay to prevent rate limiting (ms). Raised from 1s to 2s (#1802). */\nexport const DEFAULT_INTER_AGENT_DELAY_MS = 2000;\n\nexport interface VoterAgentOptions {\n /** Logger instance */\n readonly logger?: ILogger;\n /** Model adapter to use (auto-selected if not provided) */\n readonly adapter?: IModelAdapter;\n /** Timeout per vote in milliseconds (default: 120000, override via NEXUS_VOTE_TIMEOUT_MS) */\n readonly timeoutMs?: number;\n /** Maximum retries per vote (default: 2) */\n readonly maxRetries?: number;\n /** Whether to allow simulation fallback (default: false per Issue #280) */\n readonly allowSimulation?: boolean;\n /** Delay between launching each agent vote to prevent rate limiting (default: 1000ms). Set to 0 to disable. */\n readonly interAgentDelayMs?: number;\n}\n\n// Re-export AgentVoteResult for convenience\nexport type { AgentVoteResult };\n\nconst defaultLogger = createLogger({ component: 'voter-agents' });\n\n/**\n * Executes a real LLM vote for a single role with timeout and retry support.\n *\n * Per Issue #280: No simulation fallback by default. Returns error result\n * instead of simulated vote when execution fails.\n */\nexport async function executeAgentVote(\n role: VoterRole,\n proposal: string,\n adapter: IModelAdapter,\n logger: ILogger,\n options?: { timeoutMs?: number; maxRetries?: number; allowSimulation?: boolean }\n): Promise<AgentVoteResult> {\n const start = getTimeProvider().now();\n const timeoutMs = options?.timeoutMs ?? resolveVoteTimeout();\n const maxRetries = options?.maxRetries ?? VOTE_TIMEOUTS.maxRetries;\n const allowSimulation = options?.allowSimulation ?? false;\n\n logger.info('Executing vote', { role, model: adapter.modelId, provider: adapter.providerId });\n\n const result = await executeWithRetries({\n role,\n proposal,\n adapter,\n logger,\n timeoutMs,\n maxRetries,\n });\n const processingTimeMs = getTimeProvider().now() - start;\n\n if (result.ok) {\n logger.info('Vote completed', { role, model: adapter.modelId, decision: result.vote.decision });\n return { role, vote: result.vote, processingTimeMs, source: 'llm', cli: adapter.providerId };\n }\n\n // All retries exhausted\n logger.error('Vote execution failed after all retries', undefined, {\n role,\n model: adapter.modelId,\n errorMessage: result.error,\n });\n\n if (allowSimulation) {\n logger.warn('Falling back to simulation (allowSimulation=true)', { role });\n return createSimulationVoteResult(role, proposal, processingTimeMs, result.error);\n }\n\n return createErrorVoteResult(role, result.error, processingTimeMs);\n}\n\n// ============================================================================\n// Batch Vote Collection\n// ============================================================================\n\n/**\n * Options for collecting votes from multiple agents.\n */\nexport interface CollectRealVotesOptions extends VoterAgentOptions {\n /** Voter roles to include */\n readonly roles: readonly VoterRole[];\n /** Proposal text */\n readonly proposal: string;\n /** Use simulation mode (explicit opt-in only) */\n readonly simulate?: boolean;\n}\n\n/**\n * Error thrown when no adapter is available and simulation is disabled.\n */\nexport class NoAdapterError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'NoAdapterError';\n }\n}\n\n/**\n * Resolves the model adapter, handling errors per Issue #280.\n */\nfunction resolveAdapter(\n options: CollectRealVotesOptions,\n logger: ILogger\n): { adapter: IModelAdapter } | { error: string } {\n try {\n if (options.adapter !== undefined) return { adapter: options.adapter };\n const registry = getGlobalRegistry({ logger });\n return { adapter: registry.getDefault() };\n } catch (error) {\n return { error: getErrorMessage(error) };\n }\n}\n\n/** Assigns a single adapter to all roles (fallback path). */\nfunction assignUniformAdapter(\n roles: readonly VoterRole[],\n adapter: IModelAdapter\n): Map<VoterRole, IModelAdapter> {\n const adapters = new Map<VoterRole, IModelAdapter>();\n for (const role of roles) adapters.set(role, adapter);\n return adapters;\n}\n\n/** Creates CLI-specific adapters for available CLIs via the unified registry. */\nfunction createCliAdapterMap(\n clis: readonly CliName[],\n logger: ILogger\n): Map<CliName, IModelAdapter> {\n const registry = getGlobalRegistry({ logger });\n const result = new Map<CliName, IModelAdapter>();\n for (const cli of clis) {\n result.set(cli, registry.getAdapterForCli(cli));\n }\n return result;\n}\n\n/**\n * Creates diverse per-role adapters using all available CLIs (Issue #845).\n * Distributes roles across CLIs in round-robin fashion for model diversity.\n * Falls back to single adapter if only one CLI is available.\n */\nasync function resolveDiverseAdapters(\n roles: readonly VoterRole[],\n logger: ILogger,\n fallbackAdapter: IModelAdapter\n): Promise<Map<VoterRole, IModelAdapter>> {\n let availableClis: CliName[];\n try {\n availableClis = await getAvailableClis();\n } catch (e: unknown) {\n logger.warn('Failed to resolve available CLIs; falling back to single adapter', {\n error: String(e),\n });\n availableClis = [];\n }\n\n if (availableClis.length <= 1) {\n logger.info('Using single adapter for all roles', { cliCount: availableClis.length });\n return assignUniformAdapter(roles, fallbackAdapter);\n }\n\n const cliAdapters = createCliAdapterMap(availableClis, logger);\n if (cliAdapters.size <= 1) return assignUniformAdapter(roles, fallbackAdapter);\n\n // Round-robin assign roles to diverse CLIs\n const cliList = [...cliAdapters.entries()];\n const adapters = new Map<VoterRole, IModelAdapter>();\n const assignments: Record<string, string> = {};\n for (let i = 0; i < roles.length; i++) {\n const role = roles[i];\n const entry = cliList[i % cliList.length];\n if (role === undefined || entry === undefined) continue;\n adapters.set(role, entry[1]);\n assignments[role] = entry[0];\n }\n\n logger.info('Diverse adapters assigned', {\n cliCount: cliAdapters.size,\n clis: [...cliAdapters.keys()],\n roleAssignments: assignments,\n });\n return adapters;\n}\n\n/** Options for staggered vote launching. */\ninterface StaggeredVoteInput {\n readonly roles: readonly VoterRole[];\n readonly proposal: string;\n readonly roleAdapters: Map<VoterRole, IModelAdapter>;\n readonly fallbackAdapter: IModelAdapter;\n readonly logger: ILogger;\n readonly voteOptions: { timeoutMs: number; maxRetries: number; allowSimulation: boolean };\n readonly interDelay: number;\n}\n\n/**\n * Launches votes with staggered delays to prevent rate limiting (Issue #1319)\n * and an overall wall-clock deadline to prevent indefinite hangs (Issue #1871).\n */\nasync function launchStaggeredVotes(\n input: StaggeredVoteInput\n): Promise<readonly AgentVoteResult[]> {\n const { roles, proposal, roleAdapters, fallbackAdapter, logger, voteOptions, interDelay } = input;\n // Raw \"worst legitimate completion\" estimate — retained unchanged so the\n // formula still answers \"how long could this vote take in principle?\".\n const computedDeadlineMs = computeOverallConsensusDeadlineMs(\n voteOptions.timeoutMs,\n voteOptions.maxRetries,\n roles.length,\n interDelay\n );\n // Clamp below the outer MCP tool-wrapper timeout. Without this, the\n // middleware kills the promise chain before launchVotesWithOverallDeadline\n // can produce structured partial results — clients see a naked timeout\n // error instead of a `source: 'error' / error: 'overall consensus deadline\n // exceeded'` vote per stuck role. (Issue #2105)\n const overallDeadlineMs = getMcpSafeDeadlineMs(computedDeadlineMs, 'consensus_vote');\n if (overallDeadlineMs < computedDeadlineMs) {\n logger.debug('Consensus deadline clamped to MCP wrapper timeout', {\n computedDeadlineMs,\n overallDeadlineMs,\n });\n }\n return launchVotesWithOverallDeadline({\n roles,\n proposal,\n roleAdapters,\n fallbackAdapter,\n logger,\n voteOptions,\n interDelay,\n overallDeadlineMs,\n voteFn: executeAgentVote,\n });\n}\n\n/**\n * Collects votes from multiple voter agents.\n *\n * Per Issue #280: No automatic simulation fallback. If no adapter is\n * available and simulation is not explicitly enabled, throws NoAdapterError.\n * Per Issue #845: Uses diverse CLIs when multiple are available.\n */\nexport async function collectRealVotes(\n options: CollectRealVotesOptions\n): Promise<readonly AgentVoteResult[]> {\n const logger = options.logger ?? defaultLogger;\n const { roles, proposal, simulate, allowSimulation } = options;\n const timeoutMs = options.timeoutMs ?? resolveVoteTimeout();\n const maxRetries = options.maxRetries ?? VOTE_TIMEOUTS.maxRetries;\n\n if (simulate === true) {\n logger.info('Using simulation mode (explicitly requested)');\n return createSimulatedVotes(roles, proposal);\n }\n\n const adapterResult = resolveAdapter(options, logger);\n\n if ('error' in adapterResult) {\n logger.error('No adapter available for voting', undefined, { error: adapterResult.error });\n\n if (allowSimulation === true) {\n logger.warn('Falling back to simulation (allowSimulation=true)');\n return createSimulatedVotes(roles, proposal, 'No adapter available');\n }\n\n throw new NoAdapterError(\n `No adapter available for voting: ${adapterResult.error}. ` +\n 'Install a CLI (claude/gemini/codex) or set ANTHROPIC_API_KEY.'\n );\n }\n\n // Per Issue #845: Use diverse adapters when no explicit adapter is provided\n const roleAdapters =\n options.adapter !== undefined\n ? assignUniformAdapter(roles, adapterResult.adapter)\n : await resolveDiverseAdapters(roles, logger, adapterResult.adapter);\n const voteOptions = { timeoutMs, maxRetries, allowSimulation: allowSimulation ?? false };\n const interDelay = options.interAgentDelayMs ?? DEFAULT_INTER_AGENT_DELAY_MS;\n\n return launchStaggeredVotes({\n roles,\n proposal,\n roleAdapters,\n fallbackAdapter: adapterResult.adapter,\n logger,\n voteOptions,\n interDelay,\n });\n}\n\n/**\n * Gets a description for a voter role.\n */\nexport function getRoleDescription(role: VoterRole): string {\n return VOTER_ROLES[role];\n}\n","/**\n * nexus-agents/consensus - Core Type Definitions\n *\n * Core type definitions and Zod schemas for the consensus engine.\n * Supports multiple voting strategies for multi-agent decisions.\n */\n\nimport { z } from 'zod';\n\n/**\n * Consensus algorithm types.\n * - simple_majority: >50% of votes required\n * - supermajority: >=67% of votes required\n * - unanimous: 100% approval required\n * - proof_of_learning: weighted voting based on agent performance\n * - opinion_wise: higher-order voting with correlation awareness (Issue #333)\n * - higher_order: alias for opinion_wise (Issue #514)\n */\nexport const ConsensusAlgorithmSchema = z.enum([\n 'simple_majority',\n 'supermajority',\n 'unanimous',\n 'proof_of_learning',\n 'opinion_wise',\n 'higher_order',\n]);\nexport type ConsensusAlgorithm = z.infer<typeof ConsensusAlgorithmSchema>;\n\n/**\n * Vote decision options.\n */\nexport const VoteDecisionSchema = z.enum(['approve', 'reject', 'abstain']);\nexport type VoteDecision = z.infer<typeof VoteDecisionSchema>;\n\n/**\n * Proposal status in the lifecycle.\n */\nexport const ProposalStatusSchema = z.enum([\n 'pending',\n 'voting',\n 'approved',\n 'rejected',\n 'timeout',\n 'closed',\n]);\nexport type ProposalStatus = z.infer<typeof ProposalStatusSchema>;\n\n/**\n * Structured rejection feedback categories (Issue #1213).\n * Enables reject→refine→re-vote feedback loops by classifying rejection reasons.\n */\nexport const RejectionCategorySchema = z.enum([\n 'YAGNI',\n 'DRY_VIOLATION',\n 'OVER_ENGINEERING',\n 'SCOPE_CREEP',\n 'SECURITY_RISK',\n 'MISALIGNED',\n 'INSUFFICIENT_EVIDENCE',\n]);\nexport type RejectionCategory = z.infer<typeof RejectionCategorySchema>;\n\n/**\n * All valid rejection category values, for runtime reference.\n */\nexport const REJECTION_CATEGORIES = RejectionCategorySchema.options;\n\n/**\n * Pre-verified finding emitted by a voter (#2245 v4 follow-up).\n * Mirrors `cli/voter-response.ts:RawFindingSchema` — kept inline here to\n * avoid a circular cli→consensus import. Downstream code in mcp/tools\n * adds the derived `verified` flag based on the gate fields.\n */\nconst FindingShapeSchema = z.object({\n summary: z.string().min(1).max(500),\n location: z.string().min(1).max(200),\n severity: z.enum(['critical', 'high', 'medium', 'low']).default('medium'),\n gate: z.object({\n reread_cited_line: z.enum(['passed', 'failed', 'skipped']).default('skipped'),\n traced_call_path: z.enum(['passed', 'failed', 'skipped']).default('skipped'),\n named_assertion: z.string().default(''),\n ruled_out_language_non_issue: z.enum(['passed', 'failed', 'skipped']).default('skipped'),\n }),\n claim: z.string().min(1).max(2000),\n});\n\n/**\n * A vote cast by an agent.\n */\nexport const VoteSchema = z.object({\n decision: VoteDecisionSchema,\n reasoning: z.string().min(1).describe('Explanation for the vote'),\n confidence: z.number().min(0).max(1).describe('Confidence level 0-1'),\n conditions: z.array(z.string()).optional().describe('Conditions for approval'),\n /** Structured rejection categories for reject→refine→re-vote loops (Issue #1213). */\n rejectionCategories: z\n .array(RejectionCategorySchema)\n .optional()\n .describe('Rejection reason categories when decision is reject'),\n /** Pre-verified PR-review findings (#2245 v4 follow-up). Optional;\n * populated only when the voter emits the structured top-level array. */\n findings: z.array(FindingShapeSchema).optional().describe('PR-review findings (pre-verified)'),\n timestamp: z.iso.datetime().optional(),\n});\nexport type Vote = z.infer<typeof VoteSchema>;\n\n/**\n * A proposal submitted for consensus.\n */\nexport const ProposalSchema = z.object({\n id: z.string().optional().describe('Auto-generated if not provided'),\n title: z.string().min(1).max(200).describe('Short proposal title'),\n description: z.string().min(1).describe('Detailed proposal description'),\n algorithm: ConsensusAlgorithmSchema,\n timeout: z.number().int().positive().optional().describe('Timeout in milliseconds'),\n requiredVoters: z.array(z.string()).optional().describe('Agent IDs that must vote'),\n metadata: z.record(z.string(), z.unknown()).optional().describe('Additional context'),\n createdAt: z.iso.datetime().optional(),\n});\nexport type Proposal = z.infer<typeof ProposalSchema>;\n\n/**\n * Unique identifier for a proposal.\n */\nexport type ProposalId = string;\n\n/**\n * Vote counts summary.\n */\nexport interface VoteCounts {\n approve: number;\n reject: number;\n abstain: number;\n total: number;\n}\n\n/**\n * Weighted vote counts for proof-of-learning.\n */\nexport interface WeightedVoteCounts {\n approve: number;\n reject: number;\n abstain: number;\n totalWeight: number;\n}\n\n/**\n * Result of a consensus decision.\n */\nexport interface ConsensusResult {\n proposalId: ProposalId;\n proposal: Proposal;\n outcome: ProposalStatus;\n votes: Map<string, Vote>;\n voteCounts: VoteCounts;\n weightedCounts?: WeightedVoteCounts | undefined;\n approvalPercentage: number;\n quorumReached: boolean;\n startedAt: string;\n closedAt: string;\n durationMs: number;\n}\n\n/**\n * Consensus result schema for validation.\n */\nexport const ConsensusResultSchema = z.object({\n proposalId: z.string(),\n proposal: ProposalSchema,\n outcome: ProposalStatusSchema,\n votes: z.map(z.string(), VoteSchema),\n voteCounts: z.object({\n approve: z.number().int().nonnegative(),\n reject: z.number().int().nonnegative(),\n abstain: z.number().int().nonnegative(),\n total: z.number().int().nonnegative(),\n }),\n weightedCounts: z\n .object({\n approve: z.number().nonnegative(),\n reject: z.number().nonnegative(),\n abstain: z.number().nonnegative(),\n totalWeight: z.number().nonnegative(),\n })\n .optional(),\n approvalPercentage: z.number().min(0).max(100),\n quorumReached: z.boolean(),\n startedAt: z.iso.datetime(),\n closedAt: z.iso.datetime(),\n durationMs: z.number().int().nonnegative(),\n});\n\n/**\n * Agent performance record for proof-of-learning.\n */\nexport interface AgentPerformance {\n agentId: string;\n totalVotes: number;\n correctVotes: number;\n successRate: number;\n lastUpdated: string;\n}\n\n/**\n * Agent performance schema.\n */\nexport const AgentPerformanceSchema = z.object({\n agentId: z.string(),\n totalVotes: z.number().int().nonnegative(),\n correctVotes: z.number().int().nonnegative(),\n successRate: z.number().min(0).max(1),\n lastUpdated: z.iso.datetime(),\n});\n\n/**\n * Proposal content caching configuration for determinism. (Issue #589)\n */\nexport interface ProposalCacheConfig {\n /** Enable content-based caching for repeated proposals */\n enabled: boolean;\n /** TTL in milliseconds (default: 1 hour) */\n ttlMs: number;\n /** Maximum cached entries (default: 500) */\n maxEntries: number;\n}\n\n/**\n * Incremental quorum configuration (Issue #1408).\n * When enabled, ambiguous votes trigger voter pool expansion.\n */\nexport interface IncrementalQuorumConfig {\n /** Enable incremental quorum expansion. Default: false */\n enabled: boolean;\n /** Maximum expansion rounds (5→7→9). Default: 2 */\n maxExpansionRounds: number;\n /** Voters to add per expansion round. Default: 2 */\n votersPerExpansion: number;\n /** Minimum average confidence to avoid expansion. Default: 0.6 */\n confidenceThreshold: number;\n /** Ambiguity band: if approval rate is within this of threshold, expand. Default: 0.15 */\n ambiguityBand: number;\n}\n\n/**\n * Default incremental quorum configuration.\n */\nexport const DEFAULT_INCREMENTAL_QUORUM_CONFIG: IncrementalQuorumConfig = {\n enabled: false,\n maxExpansionRounds: 2,\n votersPerExpansion: 2,\n confidenceThreshold: 0.6,\n ambiguityBand: 0.15,\n};\n\n/**\n * Callback to request additional voters for incremental quorum.\n * Returns the IDs of newly added voters.\n */\nexport type VoterExpansionCallback = (\n proposalId: ProposalId,\n currentVoterCount: number,\n requestedCount: number\n) => Promise<readonly string[]>;\n\n/**\n * Consensus engine configuration.\n */\nexport interface ConsensusEngineConfig {\n defaultTimeout: number;\n minVotersForQuorum: number;\n maxActiveProposals: number;\n enablePerformanceTracking: boolean;\n /** Maximum number of closed proposals to retain. Oldest are evicted when exceeded. (Issue #549) */\n maxClosedProposals: number;\n /** Content-based proposal caching for determinism (Issue #589) */\n proposalCache?: ProposalCacheConfig;\n /** Incremental quorum configuration (Issue #1408) */\n incrementalQuorum?: IncrementalQuorumConfig;\n}\n\n/**\n * Proposal cache configuration schema. (Issue #589)\n */\nexport const ProposalCacheConfigSchema = z.object({\n enabled: z.boolean().default(false),\n ttlMs: z.number().int().positive().default(3600000), // 1 hour\n maxEntries: z.number().int().positive().default(500),\n});\n\n/**\n * Consensus engine configuration schema.\n */\nexport const ConsensusEngineConfigSchema = z.object({\n defaultTimeout: z.number().int().positive().default(300000), // 5 minutes\n minVotersForQuorum: z.number().int().positive().default(2),\n maxActiveProposals: z.number().int().positive().default(100),\n enablePerformanceTracking: z.boolean().default(true),\n maxClosedProposals: z.number().int().positive().default(1000), // Issue #549\n proposalCache: ProposalCacheConfigSchema.optional(), // Issue #589\n});\n\n/**\n * Default configuration values.\n */\nexport const DEFAULT_CONSENSUS_CONFIG: ConsensusEngineConfig = {\n defaultTimeout: 300000, // 5 minutes\n minVotersForQuorum: 2,\n maxActiveProposals: 100,\n enablePerformanceTracking: true,\n maxClosedProposals: 1000, // Issue #549: Prevent unbounded memory growth\n};\n\n/**\n * Voting thresholds for each algorithm.\n */\nexport const VOTING_THRESHOLDS: Record<ConsensusAlgorithm, number> = {\n simple_majority: 0.5,\n supermajority: 0.67,\n unanimous: 1.0,\n proof_of_learning: 0.5, // Uses weighted voting\n opinion_wise: 0.5, // Uses correlation-aware Bayesian aggregation (Issue #333)\n higher_order: 0.5, // Alias for opinion_wise (Issue #514)\n};\n\n/**\n * Internal proposal state managed by the engine.\n */\nexport interface ProposalState {\n proposal: Proposal;\n status: ProposalStatus;\n votes: Map<string, Vote>;\n voteWeights: Map<string, number>;\n startedAt: Date;\n timeoutId?: ReturnType<typeof setTimeout>;\n /** Number of incremental quorum expansions applied (Issue #1408). */\n expansionRounds?: number;\n}\n\n/**\n * Consensus metrics for monitoring.\n */\nexport interface ConsensusMetrics {\n totalProposals: number;\n approvedProposals: number;\n rejectedProposals: number;\n timedOutProposals: number;\n averageDurationMs: number;\n averageVotesPerProposal: number;\n algorithmUsage: Record<ConsensusAlgorithm, number>;\n}\n\n/**\n * Consensus metrics schema.\n */\nexport const ConsensusMetricsSchema = z.object({\n totalProposals: z.number().int().nonnegative(),\n approvedProposals: z.number().int().nonnegative(),\n rejectedProposals: z.number().int().nonnegative(),\n timedOutProposals: z.number().int().nonnegative(),\n averageDurationMs: z.number().nonnegative(),\n averageVotesPerProposal: z.number().nonnegative(),\n algorithmUsage: z.record(ConsensusAlgorithmSchema, z.number().int().nonnegative()),\n});\n","/**\n * nexus-agents/consensus - Multi-Round Voting Protocol Types\n *\n * Multi-Round Voting Protocol Types (Issue #100)\n * Based on arXiv:2512.21352 - Multi-Agent Committees for Code Review\n */\n\nimport { z } from 'zod';\nimport type { Vote } from './types-core.js';\n\n/**\n * Voting round phases.\n * - analysis: Independent analysis (Round 1)\n * - deliberation: Share findings and discuss (Round 2)\n * - consensus: Final vote on recommendations (Round 3)\n */\nexport const VotingRoundPhaseSchema = z.enum(['analysis', 'deliberation', 'consensus']);\nexport type VotingRoundPhase = z.infer<typeof VotingRoundPhaseSchema>;\n\n/**\n * Voting round status.\n */\nexport const VotingRoundStatusSchema = z.enum([\n 'pending',\n 'in_progress',\n 'awaiting_votes',\n 'completed',\n 'aborted',\n]);\nexport type VotingRoundStatus = z.infer<typeof VotingRoundStatusSchema>;\n\n/**\n * A finding submitted by an agent during analysis.\n */\nexport const AgentFindingSchema = z.object({\n agentId: z.string(),\n category: z.enum(['bug', 'security', 'performance', 'style', 'design', 'documentation', 'other']),\n severity: z.enum(['critical', 'major', 'minor', 'suggestion']),\n description: z.string().min(1),\n location: z.string().optional().describe('File path and line range if applicable'),\n suggestion: z.string().optional().describe('Recommended fix'),\n confidence: z.number().min(0).max(1),\n timestamp: z.iso.datetime().optional(),\n});\nexport type AgentFinding = z.infer<typeof AgentFindingSchema>;\n\n/**\n * Finding vote during deliberation.\n */\nexport const FindingVoteSchema = z.object({\n agentId: z.string(),\n findingId: z.string(),\n agree: z.boolean(),\n reasoning: z.string().optional(),\n amendedSeverity: z.enum(['critical', 'major', 'minor', 'suggestion']).optional(),\n});\nexport type FindingVote = z.infer<typeof FindingVoteSchema>;\n\n/**\n * A single voting round in the protocol.\n */\nexport interface VotingRound {\n id: string;\n phase: VotingRoundPhase;\n status: VotingRoundStatus;\n findings: Map<string, AgentFinding>; // findingId -> finding\n findingVotes: Map<string, FindingVote[]>; // findingId -> votes\n finalVotes: Map<string, Vote>; // agentId -> final vote\n startedAt: string;\n completedAt?: string;\n roundNumber: number;\n}\n\n/**\n * Configuration for the voting protocol.\n */\nexport interface VotingProtocolConfig {\n /** Number of agents in the committee (default: 3) */\n committeeSize: number;\n /** Maximum rounds before forcing decision (default: 3) */\n maxRounds: number;\n /** Timeout per round in milliseconds (default: 60000) */\n roundTimeoutMs: number;\n /** Minimum agreement threshold (default: 0.67) */\n agreementThreshold: number;\n /** Enable anti-sycophancy detection (default: true) */\n enableAntiSycophancy: boolean;\n /** Similarity threshold for sycophancy detection (default: 0.8) */\n sycophancyThreshold: number;\n}\n\nexport const VotingProtocolConfigSchema = z.object({\n committeeSize: z.number().int().min(2).max(7).default(3),\n maxRounds: z.number().int().min(1).max(5).default(3),\n roundTimeoutMs: z.number().int().positive().default(60000),\n agreementThreshold: z.number().min(0.5).max(1).default(0.67),\n enableAntiSycophancy: z.boolean().default(true),\n sycophancyThreshold: z.number().min(0).max(1).default(0.8),\n});\n\nexport const DEFAULT_VOTING_PROTOCOL_CONFIG: VotingProtocolConfig = {\n committeeSize: 3,\n maxRounds: 3,\n roundTimeoutMs: 60000,\n agreementThreshold: 0.67,\n enableAntiSycophancy: true,\n sycophancyThreshold: 0.8,\n};\n\n/**\n * Session state for a voting protocol instance.\n */\nexport interface VotingSession {\n id: string;\n topic: string;\n committee: string[]; // Agent IDs\n rounds: VotingRound[];\n currentRound: number;\n config: VotingProtocolConfig;\n status: 'active' | 'completed' | 'aborted';\n createdAt: string;\n completedAt?: string;\n finalResult?: VotingProtocolResult;\n}\n\n/**\n * Final result of a voting protocol session.\n */\nexport interface VotingProtocolResult {\n sessionId: string;\n topic: string;\n outcome: 'approved' | 'rejected' | 'needs_revision' | 'no_consensus';\n consolidatedFindings: ConsolidatedFinding[];\n roundSummaries: RoundSummary[];\n agreementScore: number;\n sycophancyDetected: boolean;\n totalDurationMs: number;\n participatingAgents: string[];\n}\n\n/**\n * A consolidated finding after deliberation.\n */\nexport interface ConsolidatedFinding {\n id: string;\n category: AgentFinding['category'];\n severity: AgentFinding['severity'];\n description: string;\n location?: string;\n suggestion?: string;\n supportingAgents: string[];\n agreementRatio: number;\n originalFindings: AgentFinding[];\n}\n\n/**\n * Summary of a single round.\n */\nexport interface RoundSummary {\n roundNumber: number;\n phase: VotingRoundPhase;\n findingsCount: number;\n votesCount: number;\n agreementScore: number;\n durationMs: number;\n}\n\n/**\n * Interface for the multi-round voting protocol.\n * (Source: Issue #100, arXiv:2512.21352)\n */\nexport interface IVotingProtocol {\n /** Create a new voting session with a committee */\n createSession(\n topic: string,\n committee: string[],\n config?: Partial<VotingProtocolConfig>\n ): VotingSession;\n\n /** Start the analysis round (Round 1) */\n startAnalysisRound(sessionId: string): Promise<VotingRound>;\n\n /** Submit findings from an agent during analysis */\n submitFindings(sessionId: string, agentId: string, findings: AgentFinding[]): Promise<void>;\n\n /** Start the deliberation round (Round 2) */\n startDeliberationRound(sessionId: string): Promise<VotingRound>;\n\n /** Vote on findings during deliberation */\n voteOnFinding(sessionId: string, vote: FindingVote): Promise<void>;\n\n /** Start the consensus round (Round 3) */\n startConsensusRound(sessionId: string): Promise<VotingRound>;\n\n /** Submit final vote during consensus */\n submitFinalVote(sessionId: string, agentId: string, vote: Vote): Promise<void>;\n\n /** Get the final result (closes session if complete) */\n getResult(sessionId: string): Promise<VotingProtocolResult | null>;\n\n /** Check for sycophancy in the current round */\n detectSycophancy(sessionId: string): SycophancyReport;\n\n /** Get the current session state */\n getSession(sessionId: string): VotingSession | undefined;\n}\n\n/**\n * Report from sycophancy detection.\n */\nexport interface SycophancyReport {\n detected: boolean;\n confidenceScore: number;\n indicators: SycophancyIndicator[];\n affectedAgents: string[];\n recommendation: string;\n}\n\n/**\n * Individual sycophancy indicator.\n */\nexport interface SycophancyIndicator {\n type: 'premature_consensus' | 'opinion_convergence' | 'confidence_inflation' | 'echo_chamber';\n description: string;\n severity: 'low' | 'medium' | 'high';\n agents: string[];\n}\n","/**\n * nexus-agents/consensus - Weighted Byzantine Voting Types\n *\n * Weighted Byzantine Voting Types (Issue #103)\n * Based on CP-WBFT (arXiv:2511.10400)\n */\n\nimport { z } from 'zod';\nimport type { Vote } from './types-core.js';\n\n/**\n * Task outcome for tracking agent performance.\n */\nexport const TaskOutcomeSchema = z.enum(['success', 'failure', 'partial', 'unknown']);\nexport type TaskOutcome = z.infer<typeof TaskOutcomeSchema>;\n\n/**\n * Extended agent performance with Byzantine detection.\n */\nexport interface WeightedAgentRecord {\n readonly agentId: string;\n readonly totalTasks: number;\n readonly successfulTasks: number;\n readonly failedTasks: number;\n readonly partialTasks: number;\n readonly successRate: number;\n readonly weight: number;\n readonly trustScore: number;\n readonly byzantineFlags: number;\n readonly lastActive: Date;\n readonly createdAt: Date;\n}\n\nexport const WeightedAgentRecordSchema = z.object({\n agentId: z.string().min(1),\n totalTasks: z.number().int().nonnegative(),\n successfulTasks: z.number().int().nonnegative(),\n failedTasks: z.number().int().nonnegative(),\n partialTasks: z.number().int().nonnegative(),\n successRate: z.number().min(0).max(1),\n weight: z.number().min(0).max(1),\n trustScore: z.number().min(0).max(1),\n byzantineFlags: z.number().int().nonnegative(),\n lastActive: z.date(),\n createdAt: z.date(),\n});\n\n/**\n * Weighted consensus result.\n */\nexport interface WeightedConsensusResult {\n readonly decision: 'approve' | 'reject' | 'no_consensus';\n readonly weightedApproval: number;\n readonly weightedRejection: number;\n readonly totalWeight: number;\n readonly quorumReached: boolean;\n readonly byzantineDetected: boolean;\n readonly participatingAgents: readonly string[];\n readonly weightBreakdown: ReadonlyMap<string, number>;\n}\n\n/**\n * Configuration for weighted Byzantine voting.\n */\nexport interface WeightedVotingConfig {\n /** Minimum weight to participate in voting (default: 0.1) */\n readonly minWeight: number;\n /** Maximum Byzantine fault tolerance (default: 0.33) */\n readonly maxByzantineFraction: number;\n /** Weight decay factor per failed task (default: 0.9) */\n readonly weightDecayFactor: number;\n /** Weight recovery factor per successful task (default: 1.05) */\n readonly weightRecoveryFactor: number;\n /** Trust score required to vote (default: 0.3) */\n readonly minTrustScore: number;\n /** Byzantine flag threshold for exclusion (default: 3) */\n readonly byzantineFlagThreshold: number;\n /** Initial weight for new agents (default: 0.5) */\n readonly initialWeight: number;\n /** Quorum threshold for valid consensus (default: 0.67) */\n readonly quorumThreshold: number;\n}\n\nexport const WeightedVotingConfigSchema = z.object({\n minWeight: z.number().min(0).max(1).default(0.1),\n maxByzantineFraction: z.number().min(0).max(0.5).default(0.33),\n weightDecayFactor: z.number().min(0.5).max(1).default(0.9),\n weightRecoveryFactor: z.number().min(1).max(2).default(1.05),\n minTrustScore: z.number().min(0).max(1).default(0.3),\n byzantineFlagThreshold: z.number().int().positive().default(3),\n initialWeight: z.number().min(0).max(1).default(0.5),\n quorumThreshold: z.number().min(0.5).max(1).default(0.67),\n});\n\nexport const DEFAULT_WEIGHTED_VOTING_CONFIG: WeightedVotingConfig = {\n minWeight: 0.1,\n maxByzantineFraction: 0.33,\n weightDecayFactor: 0.9,\n weightRecoveryFactor: 1.05,\n minTrustScore: 0.3,\n byzantineFlagThreshold: 3,\n initialWeight: 0.5,\n quorumThreshold: 0.67,\n};\n\n/**\n * Interface for weighted Byzantine voting.\n * (Source: Issue #103, arXiv:2511.10400 - CP-WBFT)\n */\nexport interface IWeightedVoting {\n /** Calculate vote weight for an agent */\n calculateWeight(agentId: string): number;\n\n /** Update agent performance based on task outcome */\n updatePerformance(agentId: string, outcome: TaskOutcome): void;\n\n /** Run weighted consensus on votes */\n weightedConsensus(votes: ReadonlyMap<string, Vote>): WeightedConsensusResult;\n\n /** Register a new agent */\n registerAgent(agentId: string): void;\n\n /** Get agent performance record */\n getAgentRecord(agentId: string): WeightedAgentRecord | undefined;\n\n /** Flag agent for Byzantine behavior */\n flagByzantine(agentId: string, reason: string): void;\n\n /** Get all agent records */\n getAllRecords(): readonly WeightedAgentRecord[];\n\n /** Check if agent can vote */\n canVote(agentId: string): boolean;\n\n /** Recalibrate all weights based on global performance */\n recalibrateWeights(): void;\n}\n","/**\n * nexus-agents/consensus - Higher-Order Voting Types\n *\n * Type definitions for Opinion-Wise (OW) and Independent Subset Partition (ISP)\n * voting methods that account for correlations between agent opinions.\n *\n * Higher-order voting uses Bayesian-optimal aggregation that handles correlated\n * agents better than traditional independent voting assumptions.\n *\n * @module consensus/higher-order-types\n * (Source: Issue #333)\n */\n\nimport { z } from 'zod';\nimport type { Vote, VoteDecision } from './types-core.js';\n\n// ============================================================================\n// CORRELATION TYPES\n// ============================================================================\n\n/**\n * Pair of agent IDs for correlation tracking.\n * Stored as \"agentA:agentB\" where agentA < agentB lexicographically.\n */\nexport type AgentPairKey = `${string}:${string}`;\n\n/**\n * Creates a canonical agent pair key for correlation lookup.\n * Orders agents lexicographically to ensure consistent keys.\n */\nexport function createAgentPairKey(agentA: string, agentB: string): AgentPairKey {\n return agentA < agentB ? `${agentA}:${agentB}` : `${agentB}:${agentA}`;\n}\n\n/**\n * Extracts agent IDs from a pair key.\n */\nexport function parseAgentPairKey(key: AgentPairKey): [string, string] {\n const parts = key.split(':');\n if (parts.length !== 2 || parts[0] === undefined || parts[1] === undefined) {\n throw new Error(`Invalid agent pair key: ${key}`);\n }\n return [parts[0], parts[1]];\n}\n\n/**\n * Correlation coefficient between two agents' voting patterns.\n * Range: -1 (perfectly anti-correlated) to +1 (perfectly correlated).\n * 0 indicates independence.\n */\nexport const CorrelationCoefficientSchema = z.number().min(-1).max(1);\nexport type CorrelationCoefficient = z.infer<typeof CorrelationCoefficientSchema>;\n\n/**\n * Correlation matrix storing pairwise correlations between agents.\n */\nexport type CorrelationMatrix = Map<AgentPairKey, CorrelationCoefficient>;\n\n/**\n * A subset of agents that vote independently of each other.\n * Used in ISP (Independent Subset Partition) method.\n */\nexport interface IndependentSubset {\n /** Unique identifier for this subset */\n readonly id: string;\n /** Agent IDs in this independent subset */\n readonly agentIds: readonly string[];\n /** Average internal independence score (lower = more independent) */\n readonly independenceScore: number;\n /** Number of observations supporting this grouping */\n readonly observationCount: number;\n}\n\nexport const IndependentSubsetSchema = z.object({\n id: z.string(),\n agentIds: z.array(z.string()),\n independenceScore: z.number().min(0).max(1),\n observationCount: z.number().int().nonnegative(),\n});\n\n// ============================================================================\n// VOTING HISTORY TYPES\n// ============================================================================\n\n/**\n * Record of a single voting observation for correlation tracking.\n */\nexport interface VotingObservation {\n /** Unique proposal ID */\n readonly proposalId: string;\n /** Agent who cast the vote */\n readonly agentId: string;\n /** The vote decision */\n readonly decision: VoteDecision;\n /** Confidence level (0-1) */\n readonly confidence: number;\n /** Whether the vote aligned with the final outcome */\n readonly alignedWithOutcome: boolean;\n /** Timestamp of the vote */\n readonly timestamp: Date;\n}\n\nexport const VotingObservationSchema = z.object({\n proposalId: z.string(),\n agentId: z.string(),\n decision: z.enum(['approve', 'reject', 'abstain']),\n confidence: z.number().min(0).max(1),\n alignedWithOutcome: z.boolean(),\n timestamp: z.date(),\n});\n\n/**\n * Aggregated voting history for a pair of agents.\n */\nexport interface PairwiseVotingHistory {\n /** Agent pair key */\n readonly pairKey: AgentPairKey;\n /** Number of proposals where both agents voted */\n readonly jointObservations: number;\n /** Number of times both agents agreed */\n readonly agreements: number;\n /** Number of times agents disagreed */\n readonly disagreements: number;\n /** Computed correlation coefficient */\n readonly correlation: CorrelationCoefficient;\n /** Last update timestamp */\n readonly lastUpdated: Date;\n}\n\nexport const PairwiseVotingHistorySchema = z.object({\n pairKey: z.string() as z.ZodType<AgentPairKey>,\n jointObservations: z.number().int().nonnegative(),\n agreements: z.number().int().nonnegative(),\n disagreements: z.number().int().nonnegative(),\n correlation: CorrelationCoefficientSchema,\n lastUpdated: z.date(),\n});\n\n// ============================================================================\n// HIGHER-ORDER VOTING CONFIG\n// ============================================================================\n\n/**\n * Configuration for higher-order voting.\n */\nexport interface HigherOrderVotingConfig {\n /** Minimum observations before using correlation data (default: 10) */\n readonly minObservationsForCorrelation: number;\n /** Correlation threshold to consider agents correlated (default: 0.3) */\n readonly correlationThreshold: number;\n /** Maximum correlation age in milliseconds before recalculation (default: 24h) */\n readonly correlationMaxAgeMs: number;\n /** Independence threshold for ISP grouping (default: 0.2) */\n readonly independenceThreshold: number;\n /** Whether to fall back to simple voting when correlation data insufficient */\n readonly fallbackToSimpleVoting: boolean;\n /** Decay factor for old observations (0-1, default: 0.95) */\n readonly observationDecayFactor: number;\n /** Maximum observations to store per agent before FIFO eviction (default: 1000) */\n readonly maxObservationsPerAgent: number;\n /** Maximum total proposals to track before evicting oldest (default: 5000) */\n readonly maxProposals: number;\n /** Maximum pairwise history entries before LRU eviction (default: 100) */\n readonly maxTrackedPairs: number;\n}\n\nexport const HigherOrderVotingConfigSchema = z.object({\n minObservationsForCorrelation: z.number().int().positive().default(10),\n correlationThreshold: z.number().min(0).max(1).default(0.3),\n correlationMaxAgeMs: z.number().int().positive().default(86400000), // 24 hours\n independenceThreshold: z.number().min(0).max(1).default(0.2),\n fallbackToSimpleVoting: z.boolean().default(true),\n observationDecayFactor: z.number().min(0).max(1).default(0.95),\n maxObservationsPerAgent: z.number().int().positive().default(1000),\n maxProposals: z.number().int().positive().default(5000),\n maxTrackedPairs: z.number().int().positive().default(100),\n});\n\nexport const DEFAULT_HIGHER_ORDER_CONFIG: HigherOrderVotingConfig = {\n minObservationsForCorrelation: 10,\n correlationThreshold: 0.3,\n correlationMaxAgeMs: 86400000, // 24 hours\n independenceThreshold: 0.2,\n fallbackToSimpleVoting: true,\n observationDecayFactor: 0.95,\n maxObservationsPerAgent: 1000,\n maxProposals: 5000,\n maxTrackedPairs: 100,\n};\n\n// ============================================================================\n// HIGHER-ORDER VOTING RESULTS\n// ============================================================================\n\n/**\n * Result of Bayesian aggregation with correlation awareness.\n */\nexport interface HigherOrderVotingResult {\n /** Final decision */\n readonly decision: 'approve' | 'reject' | 'no_consensus';\n /** Posterior probability of approval */\n readonly posteriorApproval: number;\n /** Posterior probability of rejection */\n readonly posteriorRejection: number;\n /** Effective number of independent votes */\n readonly effectiveVoteCount: number;\n /** Whether correlation data was sufficient */\n readonly usedCorrelationData: boolean;\n /** Method used: 'ow' (opinion-wise), 'isp', or 'simple' (fallback) */\n readonly method: 'ow' | 'isp' | 'simple';\n /** Improvement over baseline majority voting (percentage points) */\n readonly improvementOverBaseline: number;\n /** Independent subsets used (if ISP method) */\n readonly independentSubsets?: readonly IndependentSubset[];\n /** Agents whose votes were down-weighted due to correlation */\n readonly downweightedAgents: readonly string[];\n /** Reasoning for the decision */\n readonly reasoning: string;\n}\n\nexport const HigherOrderVotingResultSchema = z.object({\n decision: z.enum(['approve', 'reject', 'no_consensus']),\n posteriorApproval: z.number().min(0).max(1),\n posteriorRejection: z.number().min(0).max(1),\n effectiveVoteCount: z.number().nonnegative(),\n usedCorrelationData: z.boolean(),\n method: z.enum(['ow', 'isp', 'simple']),\n improvementOverBaseline: z.number(),\n independentSubsets: z.array(IndependentSubsetSchema).optional(),\n downweightedAgents: z.array(z.string()),\n reasoning: z.string(),\n});\n\n// ============================================================================\n// CORRELATION TRACKER INTERFACE\n// ============================================================================\n\n/**\n * Statistics about correlation tracking.\n */\nexport interface CorrelationTrackerStats {\n /** Total agents being tracked */\n readonly totalAgents: number;\n /** Total agent pairs with correlation data */\n readonly trackedPairs: number;\n /** Total voting observations recorded */\n readonly totalObservations: number;\n /** Average correlation across all pairs */\n readonly averageCorrelation: number;\n /** Number of identified independent subsets */\n readonly independentSubsetCount: number;\n /** Pairs with sufficient data for correlation calculation */\n readonly pairsWithSufficientData: number;\n}\n\nexport const CorrelationTrackerStatsSchema = z.object({\n totalAgents: z.number().int().nonnegative(),\n trackedPairs: z.number().int().nonnegative(),\n totalObservations: z.number().int().nonnegative(),\n averageCorrelation: z.number(),\n independentSubsetCount: z.number().int().nonnegative(),\n pairsWithSufficientData: z.number().int().nonnegative(),\n});\n\n/**\n * Interface for correlation tracking between agents.\n */\nexport interface ICorrelationTracker {\n /**\n * Record a vote and its outcome for correlation tracking.\n */\n recordVote(agentId: string, vote: Vote, outcome: 'approved' | 'rejected'): void;\n\n /**\n * Record votes from multiple agents for the same proposal.\n */\n recordProposalVotes(\n proposalId: string,\n votes: ReadonlyMap<string, Vote>,\n outcome: 'approved' | 'rejected'\n ): void;\n\n /**\n * Compute the full correlation matrix for all tracked agents.\n */\n computeCorrelationMatrix(): CorrelationMatrix;\n\n /**\n * Get correlation between two specific agents.\n * Returns undefined if insufficient data.\n */\n getCorrelation(agentA: string, agentB: string): CorrelationCoefficient | undefined;\n\n /**\n * Identify groups of agents that vote independently.\n */\n identifyIndependentSubsets(): readonly IndependentSubset[];\n\n /**\n * Check if there is sufficient correlation data for a set of agents.\n */\n hasSufficientData(agentIds: readonly string[]): boolean;\n\n /**\n * Get statistics about the correlation tracker.\n */\n getStats(): CorrelationTrackerStats;\n\n /**\n * Clear all recorded data.\n */\n clear(): void;\n}\n\n// ============================================================================\n// OW VOTING INTERFACE\n// ============================================================================\n\n/**\n * Interface for Opinion-Wise higher-order voting.\n */\nexport interface IHigherOrderVoting {\n /**\n * Aggregate votes using Bayesian correlation-aware method.\n */\n aggregateWithCorrelation(\n votes: ReadonlyMap<string, Vote>,\n correlationMatrix: CorrelationMatrix\n ): HigherOrderVotingResult;\n\n /**\n * Estimate correlation matrix from voting history.\n */\n estimateCorrelation(tracker: ICorrelationTracker): CorrelationMatrix;\n\n /**\n * Compute result using Independent Subset Partition method.\n */\n computeISP(\n votes: ReadonlyMap<string, Vote>,\n independentSubsets: readonly IndependentSubset[]\n ): HigherOrderVotingResult;\n\n /**\n * Full pipeline: estimate correlation, compute result.\n */\n aggregate(\n votes: ReadonlyMap<string, Vote>,\n tracker: ICorrelationTracker\n ): HigherOrderVotingResult;\n\n /**\n * Get the current configuration.\n */\n getConfig(): HigherOrderVotingConfig;\n}\n","/**\n * nexus-agents/consensus - Higher-Order Voting Helpers\n *\n * Helper functions for Opinion-Wise (OW) and Independent Subset Partition (ISP)\n * voting methods. Extracted from higher-order-voting.ts to maintain file size limits.\n *\n * @module consensus/higher-order-helpers\n * (Source: Issue #333, #339)\n */\n\nimport type { Vote } from './types-core.js';\nimport type {\n CorrelationMatrix,\n HigherOrderVotingResult,\n IndependentSubset,\n} from './higher-order-types.js';\nimport { createAgentPairKey } from './higher-order-types.js';\n\n/**\n * Result of Bayesian aggregation.\n */\nexport interface BayesianAggregateResult {\n readonly posteriorApproval: number;\n readonly posteriorRejection: number;\n readonly effectiveVoteCount: number;\n readonly downweightedAgents: string[];\n}\n\n/**\n * Result of subset aggregation.\n */\nexport interface SubsetAggregationResult {\n readonly subsetResults: Array<{ approval: number; rejection: number; weight: number }>;\n readonly downweightedAgents: string[];\n}\n\n/**\n * Result of combined subset results.\n */\nexport interface CombinedSubsetResult {\n readonly posteriorApproval: number;\n readonly posteriorRejection: number;\n readonly totalWeight: number;\n}\n\n/**\n * Check if there is sufficient correlation data for analysis.\n */\nexport function hasSufficientCorrelationData(\n agentIds: readonly string[],\n correlationMatrix: CorrelationMatrix\n): boolean {\n if (agentIds.length < 2) return false;\n\n let pairsWithData = 0;\n const totalPairs = (agentIds.length * (agentIds.length - 1)) / 2;\n\n for (let i = 0; i < agentIds.length; i++) {\n for (let j = i + 1; j < agentIds.length; j++) {\n const agentA = agentIds[i];\n const agentB = agentIds[j];\n if (agentA !== undefined && agentB !== undefined) {\n const pairKey = createAgentPairKey(agentA, agentB);\n if (correlationMatrix.has(pairKey)) {\n pairsWithData++;\n }\n }\n }\n }\n\n return pairsWithData >= Math.ceil(totalPairs * 0.5);\n}\n\n/**\n * Compute effective weights for agents based on correlations.\n * Reduces weights for highly correlated agents.\n */\nexport function computeEffectiveWeights(\n agentIds: readonly string[],\n correlationMatrix: CorrelationMatrix,\n correlationThreshold: number\n): Map<string, number> {\n const weights = new Map<string, number>();\n\n // Start with equal weights\n for (const agentId of agentIds) {\n weights.set(agentId, 1.0);\n }\n\n // Reduce weights for highly correlated agents\n for (let i = 0; i < agentIds.length; i++) {\n for (let j = i + 1; j < agentIds.length; j++) {\n const agentA = agentIds[i];\n const agentB = agentIds[j];\n if (agentA === undefined || agentB === undefined) continue;\n\n const pairKey = createAgentPairKey(agentA, agentB);\n const correlation = correlationMatrix.get(pairKey);\n\n if (correlation !== undefined && correlation > correlationThreshold) {\n // Reduce weight proportionally to correlation\n const reduction = correlation * 0.5;\n const currentWeightA = weights.get(agentA) ?? 1.0;\n const currentWeightB = weights.get(agentB) ?? 1.0;\n\n // Apply reduction to both agents (but less to avoid over-penalizing)\n weights.set(agentA, Math.max(0.1, currentWeightA - reduction * 0.5));\n weights.set(agentB, Math.max(0.1, currentWeightB - reduction * 0.5));\n }\n }\n }\n\n return weights;\n}\n\n/**\n * Perform Bayesian aggregation of votes with effective weights.\n */\nexport function bayesianAggregate(\n votes: ReadonlyMap<string, Vote>,\n effectiveWeights: Map<string, number>\n): BayesianAggregateResult {\n let weightedApproval = 0;\n let weightedRejection = 0;\n let totalWeight = 0;\n const downweightedAgents: string[] = [];\n\n for (const [agentId, vote] of votes) {\n const weight = effectiveWeights.get(agentId) ?? 1.0;\n\n // Track downweighted agents\n if (weight < 0.8) {\n downweightedAgents.push(agentId);\n }\n\n // Weight by confidence and effective weight\n const effectiveWeight = weight * vote.confidence;\n totalWeight += effectiveWeight;\n\n if (vote.decision === 'approve') {\n weightedApproval += effectiveWeight;\n } else if (vote.decision === 'reject') {\n weightedRejection += effectiveWeight;\n }\n // Abstains contribute to total weight but not to either side\n }\n\n const posteriorApproval = totalWeight > 0 ? weightedApproval / totalWeight : 0.5;\n const posteriorRejection = totalWeight > 0 ? weightedRejection / totalWeight : 0.5;\n const effectiveVoteCount = totalWeight;\n\n return { posteriorApproval, posteriorRejection, effectiveVoteCount, downweightedAgents };\n}\n\n/**\n * Aggregate votes within independent subsets.\n */\nexport function aggregateSubsets(\n votes: ReadonlyMap<string, Vote>,\n independentSubsets: readonly IndependentSubset[]\n): SubsetAggregationResult {\n const subsetResults: Array<{ approval: number; rejection: number; weight: number }> = [];\n const downweightedAgents: string[] = [];\n\n for (const subset of independentSubsets) {\n const subsetVotes = new Map<string, Vote>();\n for (const agentId of subset.agentIds) {\n const vote = votes.get(agentId);\n if (vote !== undefined) subsetVotes.set(agentId, vote);\n }\n if (subsetVotes.size === 0) continue;\n\n const { approval, rejection } = countSubsetVotes(subsetVotes);\n const weight = subsetVotes.size * (1 - subset.independenceScore);\n subsetResults.push({ approval, rejection, weight });\n\n if (subset.agentIds.length === 1) downweightedAgents.push(...subset.agentIds);\n }\n return { subsetResults, downweightedAgents };\n}\n\n/**\n * Combine subset results into overall posterior probabilities.\n */\nexport function combineSubsetResults(\n subsetResults: readonly { approval: number; rejection: number; weight: number }[]\n): CombinedSubsetResult {\n let totalApproval = 0;\n let totalRejection = 0;\n let totalWeight = 0;\n for (const { approval, rejection, weight } of subsetResults) {\n totalApproval += approval * weight;\n totalRejection += rejection * weight;\n totalWeight += weight;\n }\n return {\n posteriorApproval: totalWeight > 0 ? totalApproval / totalWeight : 0.5,\n posteriorRejection: totalWeight > 0 ? totalRejection / totalWeight : 0.5,\n totalWeight,\n };\n}\n\n/**\n * Count votes within a subset, weighted by confidence.\n */\nexport function countSubsetVotes(votes: ReadonlyMap<string, Vote>): {\n approval: number;\n rejection: number;\n} {\n let approval = 0;\n let rejection = 0;\n\n for (const vote of votes.values()) {\n if (vote.decision === 'approve') {\n approval += vote.confidence;\n } else if (vote.decision === 'reject') {\n rejection += vote.confidence;\n }\n }\n\n const total = approval + rejection;\n return {\n approval: total > 0 ? approval / total : 0.5,\n rejection: total > 0 ? rejection / total : 0.5,\n };\n}\n\n/**\n * Determine decision based on posterior probabilities.\n */\nexport function determineHigherOrderDecision(\n posteriorApproval: number,\n posteriorRejection: number\n): 'approve' | 'reject' | 'no_consensus' {\n const diff = Math.abs(posteriorApproval - posteriorRejection);\n\n // Require significant margin for decision\n if (diff < 0.1) {\n return 'no_consensus';\n }\n\n return posteriorApproval > posteriorRejection ? 'approve' : 'reject';\n}\n\n/**\n * Perform simple majority aggregation (no correlation data).\n */\nexport function aggregateSimple(\n votes: ReadonlyMap<string, Vote>,\n buildReasoningFn: (\n decision: 'approve' | 'reject' | 'no_consensus',\n effectiveVotes: number,\n downweightedAgents: string[],\n method: 'ow' | 'isp' | 'simple'\n ) => string\n): HigherOrderVotingResult {\n let approve = 0;\n let reject = 0;\n let total = 0;\n\n for (const vote of votes.values()) {\n if (vote.decision === 'approve') {\n approve++;\n } else if (vote.decision === 'reject') {\n reject++;\n }\n if (vote.decision !== 'abstain') {\n total++;\n }\n }\n\n const posteriorApproval = total > 0 ? approve / total : 0.5;\n const posteriorRejection = total > 0 ? reject / total : 0.5;\n const decision = determineHigherOrderDecision(posteriorApproval, posteriorRejection);\n\n return {\n decision,\n posteriorApproval,\n posteriorRejection,\n effectiveVoteCount: total,\n usedCorrelationData: false,\n method: 'simple',\n improvementOverBaseline: 0,\n downweightedAgents: [],\n reasoning: buildReasoningFn(decision, total, [], 'simple'),\n };\n}\n\n/**\n * Calculate improvement over baseline.\n */\nexport function calculateImprovement(\n posteriorApproval: number,\n posteriorRejection: number,\n decision: 'approve' | 'reject' | 'no_consensus',\n baseline: HigherOrderVotingResult\n): number {\n // Improvement is measured by increased confidence in the same direction\n if (decision === 'no_consensus' || baseline.decision === 'no_consensus') {\n return 0;\n }\n\n const currentConfidence = decision === 'approve' ? posteriorApproval : posteriorRejection;\n const baselineConfidence =\n baseline.decision === 'approve' ? baseline.posteriorApproval : baseline.posteriorRejection;\n\n // Return improvement as percentage points\n return (currentConfidence - baselineConfidence) * 100;\n}\n\n/**\n * Build reasoning string for voting result.\n */\nexport function buildReasoning(\n decision: 'approve' | 'reject' | 'no_consensus',\n effectiveVotes: number,\n downweightedAgents: readonly string[],\n method: 'ow' | 'isp' | 'simple'\n): string {\n const methodName =\n method === 'ow'\n ? 'Opinion-Wise Bayesian aggregation'\n : method === 'isp'\n ? 'Independent Subset Partition'\n : 'simple majority voting';\n\n let reasoning = `Decision reached via ${methodName} with ${effectiveVotes.toFixed(1)} effective votes. `;\n\n if (downweightedAgents.length > 0) {\n reasoning += `${String(downweightedAgents.length)} agent(s) downweighted due to correlation. `;\n }\n\n if (decision === 'no_consensus') {\n reasoning += 'No consensus reached due to insufficient margin.';\n } else {\n reasoning += `Final decision: ${decision}.`;\n }\n\n return reasoning;\n}\n","/**\n * nexus-agents/consensus - Higher-Order Voting Implementation\n *\n * Implements Opinion-Wise (OW) and Independent Subset Partition (ISP) voting\n * methods that account for correlations between agent opinions.\n *\n * Traditional voting assumes independence between voters. Higher-order voting\n * uses Bayesian-optimal aggregation that handles correlated agents better,\n * resulting in more accurate consensus decisions.\n *\n * @module consensus/higher-order-voting\n * (Source: Issue #333)\n */\n\nimport { createLogger } from '../core/logger.js';\nimport type { Vote, VoteCounts } from './types-core.js';\nimport type {\n IHigherOrderVoting,\n ICorrelationTracker,\n CorrelationMatrix,\n HigherOrderVotingConfig,\n HigherOrderVotingResult,\n IndependentSubset,\n} from './higher-order-types.js';\nimport { DEFAULT_HIGHER_ORDER_CONFIG } from './higher-order-types.js';\nimport type { IVotingStrategy, VotingOutcome } from './strategies.js';\nimport type { ConsensusAlgorithm } from './types-core.js';\nimport {\n hasSufficientCorrelationData,\n computeEffectiveWeights,\n bayesianAggregate,\n aggregateSubsets,\n combineSubsetResults,\n determineHigherOrderDecision,\n aggregateSimple,\n calculateImprovement,\n buildReasoning,\n} from './higher-order-helpers.js';\n\nconst logger = createLogger({ component: 'higher-order-voting' });\n\n/** Options for creating OWVoting instance. */\nexport interface OWVotingOptions {\n readonly config?: Partial<HigherOrderVotingConfig>;\n}\n\n/**\n * Opinion-Wise higher-order voting implementation.\n * Uses Bayesian aggregation with correlation awareness.\n */\nexport class OWVoting implements IHigherOrderVoting, IVotingStrategy {\n readonly algorithm: ConsensusAlgorithm = 'simple_majority';\n private readonly config: HigherOrderVotingConfig;\n\n constructor(options: OWVotingOptions = {}) {\n this.config = { ...DEFAULT_HIGHER_ORDER_CONFIG, ...options.config };\n logger.info('OWVoting initialized', { config: this.config });\n }\n\n /** IVotingStrategy implementation for integration with ConsensusEngine. */\n calculateOutcome(votes: Map<string, Vote>, _weights?: Map<string, number>): VotingOutcome {\n const result = this.aggregateSimpleInternal(votes);\n return this.toVotingOutcome(votes, result);\n }\n\n aggregateWithCorrelation(\n votes: ReadonlyMap<string, Vote>,\n correlationMatrix: CorrelationMatrix\n ): HigherOrderVotingResult {\n const agentIds = Array.from(votes.keys());\n const hasSufficientData = hasSufficientCorrelationData(agentIds, correlationMatrix);\n\n if (!hasSufficientData && this.config.fallbackToSimpleVoting) {\n // Issue #525: Log at INFO level for visibility\n logger.info('Insufficient correlation data, falling back to simple voting', {\n agentCount: agentIds.length,\n reason: 'insufficient_correlation_data',\n });\n return this.aggregateSimpleInternal(votes);\n }\n\n const effectiveWeights = computeEffectiveWeights(\n agentIds,\n correlationMatrix,\n this.config.correlationThreshold\n );\n\n const { posteriorApproval, posteriorRejection, effectiveVoteCount, downweightedAgents } =\n bayesianAggregate(votes, effectiveWeights);\n\n const decision = determineHigherOrderDecision(posteriorApproval, posteriorRejection);\n const baselineResult = this.aggregateSimpleInternal(votes);\n const improvementOverBaseline = calculateImprovement(\n posteriorApproval,\n posteriorRejection,\n decision,\n baselineResult\n );\n\n const result: HigherOrderVotingResult = {\n decision,\n posteriorApproval,\n posteriorRejection,\n effectiveVoteCount,\n usedCorrelationData: hasSufficientData,\n method: 'ow',\n improvementOverBaseline,\n downweightedAgents,\n reasoning: buildReasoning(decision, effectiveVoteCount, downweightedAgents, 'ow'),\n };\n\n logger.info('OW aggregation complete', {\n decision,\n posteriorApproval: posteriorApproval.toFixed(3),\n effectiveVotes: effectiveVoteCount.toFixed(2),\n downweightedAgents: downweightedAgents.length,\n });\n\n return result;\n }\n\n estimateCorrelation(tracker: ICorrelationTracker): CorrelationMatrix {\n return tracker.computeCorrelationMatrix();\n }\n\n computeISP(\n votes: ReadonlyMap<string, Vote>,\n independentSubsets: readonly IndependentSubset[]\n ): HigherOrderVotingResult {\n if (independentSubsets.length === 0) {\n // Issue #525: Log at INFO level for visibility\n logger.info('No independent subsets, falling back to simple voting', {\n reason: 'no_independent_subsets',\n });\n return this.aggregateSimpleInternal(votes);\n }\n\n const { subsetResults, downweightedAgents } = aggregateSubsets(votes, independentSubsets);\n const { posteriorApproval, posteriorRejection } = combineSubsetResults(subsetResults);\n const effectiveVoteCount = independentSubsets.length;\n const decision = determineHigherOrderDecision(posteriorApproval, posteriorRejection);\n\n const baselineResult = this.aggregateSimpleInternal(votes);\n const improvementOverBaseline = calculateImprovement(\n posteriorApproval,\n posteriorRejection,\n decision,\n baselineResult\n );\n\n const result: HigherOrderVotingResult = {\n decision,\n posteriorApproval,\n posteriorRejection,\n effectiveVoteCount,\n usedCorrelationData: true,\n method: 'isp',\n improvementOverBaseline,\n independentSubsets,\n downweightedAgents,\n reasoning: buildReasoning(decision, effectiveVoteCount, downweightedAgents, 'isp'),\n };\n\n logger.info('ISP aggregation complete', {\n decision,\n subsetCount: independentSubsets.length,\n posteriorApproval: posteriorApproval.toFixed(3),\n });\n\n return result;\n }\n\n aggregate(\n votes: ReadonlyMap<string, Vote>,\n tracker: ICorrelationTracker\n ): HigherOrderVotingResult {\n const agentIds = Array.from(votes.keys());\n\n if (!tracker.hasSufficientData(agentIds)) {\n if (this.config.fallbackToSimpleVoting) {\n // Issue #525: Log at INFO level for visibility\n logger.info('Insufficient data for correlation analysis, using simple voting', {\n agentCount: agentIds.length,\n reason: 'insufficient_tracker_data',\n });\n return this.aggregateSimpleInternal(votes);\n }\n }\n\n const correlationMatrix = tracker.computeCorrelationMatrix();\n const owResult = this.aggregateWithCorrelation(votes, correlationMatrix);\n\n const independentSubsets = tracker.identifyIndependentSubsets();\n if (independentSubsets.length > 1) {\n const ispResult = this.computeISP(votes, independentSubsets);\n const owConfidence = Math.abs(owResult.posteriorApproval - 0.5) * 2;\n const ispConfidence = Math.abs(ispResult.posteriorApproval - 0.5) * 2;\n\n if (ispConfidence > owConfidence) {\n logger.debug('Using ISP result over OW due to higher confidence');\n return ispResult;\n }\n }\n\n return owResult;\n }\n\n getConfig(): HigherOrderVotingConfig {\n return { ...this.config };\n }\n\n private aggregateSimpleInternal(votes: ReadonlyMap<string, Vote>): HigherOrderVotingResult {\n return aggregateSimple(votes, buildReasoning);\n }\n\n private toVotingOutcome(\n votes: Map<string, Vote>,\n result: HigherOrderVotingResult\n ): VotingOutcome {\n let approve = 0;\n let reject = 0;\n let abstain = 0;\n\n for (const vote of votes.values()) {\n if (vote.decision === 'approve') approve++;\n else if (vote.decision === 'reject') reject++;\n else abstain++;\n }\n\n const voteCounts: VoteCounts = { approve, reject, abstain, total: votes.size };\n\n const rawPercentage = result.posteriorApproval * 100;\n const approvalPercentage = Number.isFinite(rawPercentage) ? rawPercentage : 0;\n\n return {\n approved: result.decision === 'approve',\n approvalPercentage,\n voteCounts,\n reason: result.reasoning,\n };\n }\n}\n\n/** Creates a new OWVoting instance. */\nexport function createOWVoting(options?: OWVotingOptions): IHigherOrderVoting {\n return new OWVoting(options);\n}\n\n/**\n * Higher-order voting strategy for integration with VotingStrategyFactory.\n * Wraps OWVoting to provide IVotingStrategy interface.\n */\nexport class HigherOrderVotingStrategy extends OWVoting implements IVotingStrategy {\n override readonly algorithm: ConsensusAlgorithm = 'opinion_wise';\n\n constructor(options: OWVotingOptions = {}) {\n super(options);\n }\n}\n\n/** Creates a higher-order voting strategy for use with ConsensusEngine. */\nexport function createHigherOrderVotingStrategy(\n options?: OWVotingOptions\n): HigherOrderVotingStrategy {\n return new HigherOrderVotingStrategy(options);\n}\n","/**\n * nexus-agents/consensus - Voting Strategies\n *\n * Implementation of different voting strategies for consensus engine.\n * Supports simple majority, supermajority, unanimous, and proof-of-learning.\n */\n\nimport type {\n ConsensusAlgorithm,\n Vote,\n VoteCounts,\n WeightedVoteCounts,\n AgentPerformance,\n} from './types.js';\nimport { VOTING_THRESHOLDS } from './types.js';\nimport { HigherOrderVotingStrategy } from './higher-order-voting.js';\n\n/**\n * Interface for voting strategy implementations.\n */\nexport interface IVotingStrategy {\n readonly algorithm: ConsensusAlgorithm;\n calculateOutcome(votes: Map<string, Vote>, weights?: Map<string, number>): VotingOutcome;\n}\n\n/**\n * Result of a voting strategy calculation.\n */\nexport interface VotingOutcome {\n approved: boolean;\n approvalPercentage: number;\n voteCounts: VoteCounts;\n weightedCounts?: WeightedVoteCounts;\n reason: string;\n}\n\n/**\n * Base voting strategy with common functionality.\n */\nabstract class BaseVotingStrategy implements IVotingStrategy {\n abstract readonly algorithm: ConsensusAlgorithm;\n\n abstract calculateOutcome(votes: Map<string, Vote>, weights?: Map<string, number>): VotingOutcome;\n\n /**\n * Count votes by decision type.\n */\n protected countVotes(votes: Map<string, Vote>): VoteCounts {\n let approve = 0;\n let reject = 0;\n let abstain = 0;\n\n for (const vote of votes.values()) {\n switch (vote.decision) {\n case 'approve':\n approve++;\n break;\n case 'reject':\n reject++;\n break;\n case 'abstain':\n abstain++;\n break;\n }\n }\n\n return { approve, reject, abstain, total: votes.size };\n }\n\n /**\n * Calculate weighted vote counts using agent performance weights.\n */\n protected countWeightedVotes(\n votes: Map<string, Vote>,\n weights: Map<string, number>\n ): WeightedVoteCounts {\n let approve = 0;\n let reject = 0;\n let abstain = 0;\n let totalWeight = 0;\n\n for (const [agentId, vote] of votes.entries()) {\n const weight = weights.get(agentId) ?? 1.0;\n totalWeight += weight;\n\n switch (vote.decision) {\n case 'approve':\n approve += weight;\n break;\n case 'reject':\n reject += weight;\n break;\n case 'abstain':\n abstain += weight;\n break;\n }\n }\n\n return { approve, reject, abstain, totalWeight };\n }\n}\n\n/**\n * Simple majority voting strategy (>50% approval).\n */\nexport class SimpleMajorityStrategy extends BaseVotingStrategy {\n readonly algorithm: ConsensusAlgorithm = 'simple_majority';\n\n calculateOutcome(votes: Map<string, Vote>): VotingOutcome {\n const counts = this.countVotes(votes);\n const votingVotes = counts.approve + counts.reject; // Abstains don't count\n const threshold = VOTING_THRESHOLDS.simple_majority;\n\n if (votingVotes === 0) {\n return {\n approved: false,\n approvalPercentage: 0,\n voteCounts: counts,\n reason: 'No votes cast (excluding abstentions)',\n };\n }\n\n const approvalPercentage = (counts.approve / votingVotes) * 100;\n const approved = counts.approve / votingVotes > threshold;\n\n return {\n approved,\n approvalPercentage,\n voteCounts: counts,\n reason: approved\n ? `Approved with ${approvalPercentage.toFixed(1)}% (>${String(threshold * 100)}% required)`\n : `Rejected with ${approvalPercentage.toFixed(1)}% (<=${String(threshold * 100)}% threshold)`,\n };\n }\n}\n\n/**\n * Supermajority voting strategy (>=67% approval).\n */\nexport class SupermajorityStrategy extends BaseVotingStrategy {\n readonly algorithm: ConsensusAlgorithm = 'supermajority';\n\n calculateOutcome(votes: Map<string, Vote>): VotingOutcome {\n const counts = this.countVotes(votes);\n const votingVotes = counts.approve + counts.reject;\n const threshold = VOTING_THRESHOLDS.supermajority;\n\n if (votingVotes === 0) {\n return {\n approved: false,\n approvalPercentage: 0,\n voteCounts: counts,\n reason: 'No votes cast (excluding abstentions)',\n };\n }\n\n const approvalPercentage = (counts.approve / votingVotes) * 100;\n const approved = counts.approve / votingVotes >= threshold;\n\n return {\n approved,\n approvalPercentage,\n voteCounts: counts,\n reason: approved\n ? `Approved with ${approvalPercentage.toFixed(1)}% (>=${String(threshold * 100)}% required)`\n : `Rejected with ${approvalPercentage.toFixed(1)}% (<${String(threshold * 100)}% threshold)`,\n };\n }\n}\n\n/**\n * Unanimous voting strategy (100% approval required).\n */\nexport class UnanimousStrategy extends BaseVotingStrategy {\n readonly algorithm: ConsensusAlgorithm = 'unanimous';\n\n calculateOutcome(votes: Map<string, Vote>): VotingOutcome {\n const counts = this.countVotes(votes);\n\n if (counts.total === 0) {\n return {\n approved: false,\n approvalPercentage: 0,\n voteCounts: counts,\n reason: 'No votes cast',\n };\n }\n\n // For unanimous, any rejection fails the proposal\n // Abstentions are allowed but don't count toward approval\n const approvalPercentage = counts.total > 0 ? (counts.approve / counts.total) * 100 : 0;\n\n if (counts.reject > 0) {\n return {\n approved: false,\n approvalPercentage,\n voteCounts: counts,\n reason: `Rejected: ${String(counts.reject)} rejection(s) cast (unanimous approval required)`,\n };\n }\n\n if (counts.approve === 0) {\n return {\n approved: false,\n approvalPercentage: 0,\n voteCounts: counts,\n reason: 'No approvals cast (at least one approval required)',\n };\n }\n\n return {\n approved: true,\n approvalPercentage,\n voteCounts: counts,\n reason: `Unanimously approved with ${String(counts.approve)} vote(s)`,\n };\n }\n}\n\n/**\n * Proof-of-learning weighted voting strategy.\n * Agents with better track records have more voting power.\n */\nexport class ProofOfLearningStrategy extends BaseVotingStrategy {\n readonly algorithm: ConsensusAlgorithm = 'proof_of_learning';\n\n calculateOutcome(votes: Map<string, Vote>, weights?: Map<string, number>): VotingOutcome {\n const counts = this.countVotes(votes);\n const effectiveWeights = weights ?? new Map<string, number>();\n const weightedCounts = this.countWeightedVotes(votes, effectiveWeights);\n const threshold = VOTING_THRESHOLDS.proof_of_learning;\n\n const votingWeight = weightedCounts.approve + weightedCounts.reject;\n\n if (votingWeight === 0) {\n return {\n approved: false,\n approvalPercentage: 0,\n voteCounts: counts,\n weightedCounts,\n reason: 'No weighted votes cast (excluding abstentions)',\n };\n }\n\n const approvalPercentage = (weightedCounts.approve / votingWeight) * 100;\n const approved = weightedCounts.approve / votingWeight > threshold;\n\n return {\n approved,\n approvalPercentage,\n voteCounts: counts,\n weightedCounts,\n reason: approved\n ? `Approved with ${approvalPercentage.toFixed(1)}% weighted approval`\n : `Rejected with ${approvalPercentage.toFixed(1)}% weighted approval`,\n };\n }\n}\n\n/**\n * Calculate vote weight for an agent based on their performance history.\n * Weight ranges from 0.5 (no history) to 1.0 (perfect track record).\n */\nexport function calculateVoteWeight(performance: AgentPerformance | undefined): number {\n if (performance === undefined || performance.totalVotes === 0) {\n return 1.0; // Default weight for new agents\n }\n\n // Weight = 0.5 + (successRate * 0.5)\n // This gives a range of 0.5 to 1.0 based on historical accuracy\n return 0.5 + performance.successRate * 0.5;\n}\n\n/**\n * Factory for creating voting strategies.\n */\nexport class VotingStrategyFactory {\n private readonly strategies: Map<ConsensusAlgorithm, IVotingStrategy>;\n\n constructor() {\n this.strategies = new Map<ConsensusAlgorithm, IVotingStrategy>([\n ['simple_majority', new SimpleMajorityStrategy()],\n ['supermajority', new SupermajorityStrategy()],\n ['unanimous', new UnanimousStrategy()],\n ['proof_of_learning', new ProofOfLearningStrategy()],\n ['opinion_wise', new HigherOrderVotingStrategy()],\n ['higher_order', new HigherOrderVotingStrategy()],\n ]);\n }\n\n /**\n * Get a voting strategy by algorithm type.\n */\n getStrategy(algorithm: ConsensusAlgorithm): IVotingStrategy {\n const strategy = this.strategies.get(algorithm);\n if (strategy === undefined) {\n throw new Error(`Unknown voting algorithm: ${algorithm}`);\n }\n return strategy;\n }\n\n /**\n * Register a custom voting strategy.\n */\n registerStrategy(strategy: IVotingStrategy): void {\n this.strategies.set(strategy.algorithm, strategy);\n }\n\n /**\n * Get all available algorithm types.\n */\n getAvailableAlgorithms(): ConsensusAlgorithm[] {\n return Array.from(this.strategies.keys());\n }\n}\n\n/**\n * Creates a voting strategy factory with default strategies.\n */\nexport function createStrategyFactory(): VotingStrategyFactory {\n return new VotingStrategyFactory();\n}\n","/**\n * nexus-agents/consensus - Result Builder\n *\n * Helper functions for building consensus results.\n */\n\nimport { getTimeProvider } from '../core/index.js';\nimport type {\n ProposalId,\n ProposalState,\n ConsensusResult,\n ProposalStatus,\n ConsensusEngineConfig,\n} from './types.js';\nimport type { VotingOutcome } from './strategies.js';\n\n/**\n * Build a pending result for an active proposal.\n */\nexport function buildPendingResult(\n state: ProposalState,\n proposalId: ProposalId,\n outcome: VotingOutcome,\n config: ConsensusEngineConfig\n): ConsensusResult {\n const now = new Date(getTimeProvider().now());\n return {\n proposalId,\n proposal: state.proposal,\n outcome: state.status === 'voting' ? 'pending' : state.status,\n votes: new Map(state.votes),\n voteCounts: outcome.voteCounts,\n weightedCounts: outcome.weightedCounts,\n approvalPercentage: outcome.approvalPercentage,\n quorumReached: state.votes.size >= config.minVotersForQuorum,\n startedAt: state.startedAt.toISOString(),\n closedAt: now.toISOString(),\n durationMs: now.getTime() - state.startedAt.getTime(),\n };\n}\n\n/**\n * Build a final result for a closed proposal.\n */\nexport function buildFinalResult(\n state: ProposalState,\n proposalId: ProposalId,\n outcome: VotingOutcome,\n config: ConsensusEngineConfig\n): ConsensusResult {\n const now = new Date(getTimeProvider().now());\n const quorumReached = state.votes.size >= config.minVotersForQuorum;\n const finalStatus = determineFinalStatus(quorumReached, outcome.approved);\n\n return {\n proposalId,\n proposal: state.proposal,\n outcome: finalStatus,\n votes: new Map(state.votes),\n voteCounts: outcome.voteCounts,\n weightedCounts: outcome.weightedCounts,\n approvalPercentage: outcome.approvalPercentage,\n quorumReached,\n startedAt: state.startedAt.toISOString(),\n closedAt: now.toISOString(),\n durationMs: now.getTime() - state.startedAt.getTime(),\n };\n}\n\n/**\n * Build a timeout result for an expired proposal.\n */\nexport function buildTimeoutResult(\n state: ProposalState,\n proposalId: ProposalId,\n outcome: VotingOutcome,\n config: ConsensusEngineConfig\n): ConsensusResult {\n const now = new Date(getTimeProvider().now());\n return {\n proposalId,\n proposal: state.proposal,\n outcome: 'timeout',\n votes: new Map(state.votes),\n voteCounts: outcome.voteCounts,\n weightedCounts: outcome.weightedCounts,\n approvalPercentage: outcome.approvalPercentage,\n quorumReached: state.votes.size >= config.minVotersForQuorum,\n startedAt: state.startedAt.toISOString(),\n closedAt: now.toISOString(),\n durationMs: now.getTime() - state.startedAt.getTime(),\n };\n}\n\n/**\n * Determine final status based on quorum and approval.\n */\nexport function determineFinalStatus(quorumReached: boolean, approved: boolean): ProposalStatus {\n if (!quorumReached || !approved) return 'rejected';\n return 'approved';\n}\n","/**\n * nexus-agents/consensus - Helper Functions\n *\n * Utility functions for the consensus engine.\n */\n\nimport type { ProposalId } from './types.js';\nimport { getTimeProvider, getRandomProvider } from '../core/index.js';\n\n/**\n * Generate a unique proposal ID.\n */\nexport function generateProposalId(): ProposalId {\n const timestamp = getTimeProvider().now().toString(36);\n const random = getRandomProvider().random().toString(36).substring(2, 8);\n return `prop_${timestamp}_${random}`;\n}\n","/**\n * nexus-agents/consensus - Incremental Quorum\n *\n * Detects ambiguous voting scenarios and triggers voter pool expansion.\n * Complements agreement-based cascading (early termination when certain)\n * with expansion when uncertain.\n *\n * (Source: Issue #1408 — Incremental Quorum for Consensus Robustness)\n *\n * @module consensus/incremental-quorum\n */\n\nimport type { Vote } from './types.js';\n\n/**\n * Parameters for ambiguity detection.\n */\nexport interface AmbiguityParams {\n /** Minimum average confidence to avoid expansion. */\n readonly confidenceThreshold: number;\n /** If approval rate is within this band of the threshold, consider ambiguous. */\n readonly ambiguityBand: number;\n}\n\n/**\n * Determines if the current voting state is ambiguous.\n *\n * Ambiguity is detected when:\n * 1. Approval rate is within the ambiguity band of the threshold, OR\n * 2. Average voter confidence is below the confidence threshold\n *\n * @param votes - Current vote map\n * @param totalExpected - Total expected voters\n * @param threshold - Algorithm-specific approval threshold (0-1)\n * @param params - Ambiguity detection parameters\n * @returns true if the voting state is ambiguous\n */\nexport function isVotingAmbiguous(\n votes: ReadonlyMap<string, Vote>,\n totalExpected: number,\n threshold: number,\n params: AmbiguityParams\n): boolean {\n if (votes.size === 0) return false;\n\n let approvals = 0;\n let totalConfidence = 0;\n for (const vote of votes.values()) {\n if (vote.decision === 'approve') approvals++;\n totalConfidence += vote.confidence;\n }\n\n const approvalRate = approvals / totalExpected;\n const avgConfidence = totalConfidence / votes.size;\n\n // Check if approval rate is within the ambiguity band of threshold\n const lowerBand = threshold - params.ambiguityBand;\n const upperBand = threshold + params.ambiguityBand;\n const rateAmbiguous = approvalRate >= lowerBand && approvalRate <= upperBand;\n\n // Check if confidence is too low\n const confidenceAmbiguous = avgConfidence < params.confidenceThreshold;\n\n return rateAmbiguous || confidenceAmbiguous;\n}\n","/* eslint-disable max-lines */\n// 426 lines — cohesive single-concern engine. Per governance, 400-600 OK if cohesive.\n\n/**\n * nexus-agents/consensus - Consensus Engine\n *\n * Core consensus engine implementation supporting multiple voting strategies.\n * Manages proposal lifecycle, vote collection, and outcome determination.\n */\n\nimport type { Result, ILogger } from '../core/index.js';\nimport { ok, err, AgentError, createLogger, getTimeProvider } from '../core/index.js';\nimport type {\n Proposal,\n ProposalId,\n Vote,\n ConsensusResult,\n ConsensusAlgorithm,\n AgentPerformance,\n ConsensusEngineConfig,\n ProposalState,\n ConsensusMetrics,\n ProposalCacheConfig,\n IncrementalQuorumConfig,\n VoterExpansionCallback,\n} from './types.js';\nimport {\n ProposalSchema,\n VoteSchema,\n DEFAULT_CONSENSUS_CONFIG,\n VOTING_THRESHOLDS,\n DEFAULT_INCREMENTAL_QUORUM_CONFIG,\n} from './types.js';\nimport { VotingStrategyFactory, calculateVoteWeight, type VotingOutcome } from './strategies.js';\nimport { buildFinalResult, buildTimeoutResult, buildPendingResult } from './result-builder.js';\nimport { generateProposalId } from './helpers.js';\nimport { isVotingAmbiguous } from './incremental-quorum.js';\n\n/**\n * Error class for consensus-related failures.\n */\nexport class ConsensusError extends AgentError {\n constructor(message: string, context?: Record<string, unknown>) {\n super(message, context !== undefined ? { context } : {});\n this.name = 'ConsensusError';\n }\n}\n\n/**\n * Interface for the consensus engine.\n */\nexport interface IConsensusEngine {\n propose(proposal: Proposal): Promise<Result<ProposalId, ConsensusError>>;\n vote(proposalId: ProposalId, agentId: string, vote: Vote): Promise<Result<void, ConsensusError>>;\n getResult(proposalId: ProposalId): Promise<Result<ConsensusResult, ConsensusError>>;\n close(proposalId: ProposalId): Promise<Result<ConsensusResult, ConsensusError>>;\n getMetrics(): ConsensusMetrics;\n}\n\ninterface InternalMetrics {\n totalProposals: number;\n approvedProposals: number;\n rejectedProposals: number;\n timedOutProposals: number;\n totalDurationMs: number;\n totalVotes: number;\n algorithmUsage: Record<ConsensusAlgorithm, number>;\n}\n\n/**\n * Cache entry for proposal content-based caching (Issue #589).\n */\ninterface ProposalCacheEntry {\n readonly proposalId: ProposalId;\n readonly result: ConsensusResult;\n readonly cachedAt: number;\n}\n\n/**\n * Default proposal cache configuration.\n * Enabled by default to improve determinism (fitness audit recommendation).\n */\nconst DEFAULT_PROPOSAL_CACHE_CONFIG: ProposalCacheConfig = {\n enabled: true,\n ttlMs: 3600000, // 1 hour\n maxEntries: 500,\n};\n\n/**\n * Consensus engine for multi-agent decision making.\n *\n * @example\n * ```typescript\n * const engine = new ConsensusEngine({ defaultTimeout: 30000 });\n * const proposalResult = await engine.propose({\n * title: 'Use microservices architecture',\n * description: 'Proposal to adopt microservices',\n * algorithm: 'supermajority',\n * });\n * if (proposalResult.ok) {\n * await engine.vote(proposalResult.value, 'agent-1', {\n * decision: 'approve',\n * confidence: 0.9,\n * reasoning: 'Good for scalability',\n * });\n * }\n * ```\n */\nexport class ConsensusEngine implements IConsensusEngine {\n private readonly proposals: Map<ProposalId, ProposalState> = new Map();\n private readonly closedProposals: Map<ProposalId, ConsensusResult> = new Map();\n private readonly agentPerformance: Map<string, AgentPerformance> = new Map();\n private readonly proposalContentCache: Map<string, ProposalCacheEntry> = new Map();\n private readonly strategyFactory: VotingStrategyFactory;\n private readonly config: ConsensusEngineConfig;\n private readonly cacheConfig: ProposalCacheConfig;\n private readonly quorumConfig: IncrementalQuorumConfig;\n private readonly logger: ILogger;\n private readonly metrics: InternalMetrics;\n private voterExpansionCallback?: VoterExpansionCallback;\n\n constructor(config?: Partial<ConsensusEngineConfig>, logger?: ILogger) {\n this.config = { ...DEFAULT_CONSENSUS_CONFIG, ...config };\n this.cacheConfig = { ...DEFAULT_PROPOSAL_CACHE_CONFIG, ...config?.proposalCache };\n this.quorumConfig = { ...DEFAULT_INCREMENTAL_QUORUM_CONFIG, ...config?.incrementalQuorum };\n this.logger = logger ?? createLogger({ component: 'ConsensusEngine' });\n this.strategyFactory = new VotingStrategyFactory();\n this.metrics = this.createInitialMetrics();\n }\n\n /**\n * Sets the callback for incremental quorum voter expansion (Issue #1408).\n * When ambiguous votes are detected, this callback requests additional voters.\n */\n setVoterExpansionCallback(callback: VoterExpansionCallback): void {\n this.voterExpansionCallback = callback;\n }\n\n propose(proposal: Proposal): Promise<Result<ProposalId, ConsensusError>> {\n const validation = ProposalSchema.safeParse(proposal);\n if (!validation.success) {\n return Promise.resolve(\n err(\n new ConsensusError(`Invalid proposal: ${validation.error.message}`, {\n errors: validation.error.issues,\n })\n )\n );\n }\n\n // Check cache for identical proposal content (Issue #589)\n if (this.cacheConfig.enabled) {\n const cachedEntry = this.getCachedResult(validation.data);\n if (cachedEntry !== undefined) {\n this.logger.debug('Returning cached proposal result', {\n cachedProposalId: cachedEntry.proposalId,\n cachedOutcome: cachedEntry.result.outcome,\n });\n return Promise.resolve(ok(cachedEntry.proposalId));\n }\n }\n\n if (this.proposals.size >= this.config.maxActiveProposals) {\n return Promise.resolve(\n err(\n new ConsensusError(\n `Maximum active proposals (${String(this.config.maxActiveProposals)}) reached`\n )\n )\n );\n }\n\n const proposalId = proposal.id ?? generateProposalId();\n const state = this.createProposalState(validation.data, proposalId);\n this.setupTimeout(state, proposalId, proposal.timeout);\n this.registerProposal(proposalId, state, proposal.algorithm);\n return Promise.resolve(ok(proposalId));\n }\n\n async vote(\n proposalId: ProposalId,\n agentId: string,\n vote: Vote\n ): Promise<Result<void, ConsensusError>> {\n const validationErr = this.validateVote(vote);\n if (validationErr !== undefined) return err(validationErr);\n\n const stateErr = this.validateProposalState(proposalId);\n if (stateErr !== undefined) return err(stateErr);\n\n const state = this.proposals.get(proposalId);\n if (state === undefined) {\n return err(new ConsensusError(`Proposal ${proposalId} not found`));\n }\n\n this.recordVote(state, agentId, vote);\n\n // Agreement-based cascade always takes priority\n if (this.canCascadeEarly(state)) {\n return this.closeInternal(proposalId).then((r) => (r.ok ? ok(undefined) : err(r.error)));\n }\n\n // All required voters voted — check for incremental quorum expansion\n if (this.allRequiredVotersVoted(state)) {\n const expanded = await this.tryExpandQuorum(proposalId, state);\n if (!expanded) {\n return this.closeInternal(proposalId).then((r) => (r.ok ? ok(undefined) : err(r.error)));\n }\n // Expansion succeeded — wait for new voters\n }\n\n return Promise.resolve(ok(undefined));\n }\n\n getResult(proposalId: ProposalId): Promise<Result<ConsensusResult, ConsensusError>> {\n const closedResult = this.closedProposals.get(proposalId);\n if (closedResult !== undefined) return Promise.resolve(ok(closedResult));\n\n const state = this.proposals.get(proposalId);\n if (state === undefined) {\n return Promise.resolve(err(new ConsensusError(`Proposal ${proposalId} not found`)));\n }\n\n const outcome = this.calculateOutcome(state);\n return Promise.resolve(ok(buildPendingResult(state, proposalId, outcome, this.config)));\n }\n\n close(proposalId: ProposalId): Promise<Result<ConsensusResult, ConsensusError>> {\n return this.closeInternal(proposalId);\n }\n\n getMetrics(): ConsensusMetrics {\n const completed =\n this.metrics.approvedProposals +\n this.metrics.rejectedProposals +\n this.metrics.timedOutProposals;\n return {\n totalProposals: this.metrics.totalProposals,\n approvedProposals: this.metrics.approvedProposals,\n rejectedProposals: this.metrics.rejectedProposals,\n timedOutProposals: this.metrics.timedOutProposals,\n averageDurationMs: completed > 0 ? this.metrics.totalDurationMs / completed : 0,\n averageVotesPerProposal: completed > 0 ? this.metrics.totalVotes / completed : 0,\n algorithmUsage: { ...this.metrics.algorithmUsage },\n };\n }\n\n updateAgentPerformance(agentId: string, wasCorrect: boolean): void {\n const existing = this.agentPerformance.get(agentId);\n const now = getTimeProvider().nowIso();\n\n if (existing === undefined) {\n this.agentPerformance.set(agentId, {\n agentId,\n totalVotes: 1,\n correctVotes: wasCorrect ? 1 : 0,\n successRate: wasCorrect ? 1.0 : 0.0,\n lastUpdated: now,\n });\n } else {\n const totalVotes = existing.totalVotes + 1;\n const correctVotes = existing.correctVotes + (wasCorrect ? 1 : 0);\n this.agentPerformance.set(agentId, {\n agentId,\n totalVotes,\n correctVotes,\n successRate: correctVotes / totalVotes,\n lastUpdated: now,\n });\n }\n }\n\n getAgentPerformance(agentId: string): AgentPerformance | undefined {\n return this.agentPerformance.get(agentId);\n }\n\n getActiveProposalCount(): number {\n return this.proposals.size;\n }\n\n private createProposalState(data: Proposal, proposalId: ProposalId): ProposalState {\n const now = new Date(getTimeProvider().now());\n return {\n proposal: { ...data, id: proposalId, createdAt: now.toISOString() },\n status: 'voting',\n votes: new Map(),\n voteWeights: new Map(),\n startedAt: now,\n };\n }\n\n private setupTimeout(state: ProposalState, proposalId: ProposalId, timeout?: number): void {\n const timeoutMs = timeout ?? this.config.defaultTimeout;\n state.timeoutId = setTimeout(() => {\n this.handleTimeout(proposalId);\n }, timeoutMs);\n }\n\n private registerProposal(\n proposalId: ProposalId,\n state: ProposalState,\n algorithm: ConsensusAlgorithm\n ): void {\n this.proposals.set(proposalId, state);\n this.metrics.totalProposals++;\n this.metrics.algorithmUsage[algorithm]++;\n this.logger.info('Proposal created', {\n proposalId,\n title: state.proposal.title,\n algorithm,\n timeout: this.config.defaultTimeout,\n });\n }\n\n private validateVote(vote: Vote): ConsensusError | undefined {\n const validation = VoteSchema.safeParse(vote);\n if (!validation.success) {\n return new ConsensusError(`Invalid vote: ${validation.error.message}`, {\n errors: validation.error.issues,\n });\n }\n return undefined;\n }\n\n private validateProposalState(proposalId: ProposalId): ConsensusError | undefined {\n const state = this.proposals.get(proposalId);\n if (state === undefined) {\n if (this.closedProposals.has(proposalId)) {\n return new ConsensusError(`Proposal ${proposalId} is already closed`);\n }\n return new ConsensusError(`Proposal ${proposalId} not found`);\n }\n if (state.status !== 'voting') {\n return new ConsensusError(`Proposal ${proposalId} is not accepting votes`, {\n status: state.status,\n });\n }\n return undefined;\n }\n\n private recordVote(state: ProposalState, agentId: string, vote: Vote): void {\n state.votes.set(agentId, {\n ...vote,\n timestamp: getTimeProvider().nowIso(),\n });\n if (state.proposal.algorithm === 'proof_of_learning') {\n const performance = this.agentPerformance.get(agentId);\n state.voteWeights.set(agentId, calculateVoteWeight(performance));\n }\n this.logger.debug('Vote recorded', {\n proposalId: state.proposal.id,\n agentId,\n decision: vote.decision,\n confidence: vote.confidence,\n });\n }\n\n private closeInternal(proposalId: ProposalId): Promise<Result<ConsensusResult, ConsensusError>> {\n const state = this.proposals.get(proposalId);\n if (state === undefined) {\n const closed = this.closedProposals.get(proposalId);\n if (closed !== undefined) return Promise.resolve(ok(closed));\n return Promise.resolve(err(new ConsensusError(`Proposal ${proposalId} not found`)));\n }\n\n if (state.timeoutId !== undefined) clearTimeout(state.timeoutId);\n\n const outcome = this.calculateOutcome(state);\n const result = buildFinalResult(state, proposalId, outcome, this.config);\n this.finalize(proposalId, result, state.votes.size);\n return Promise.resolve(ok(result));\n }\n\n private handleTimeout(proposalId: ProposalId): void {\n const state = this.proposals.get(proposalId);\n if (state?.status !== 'voting') return;\n\n this.logger.warn('Proposal timed out', { proposalId, voteCount: state.votes.size });\n const outcome = this.calculateOutcome(state);\n const result = buildTimeoutResult(state, proposalId, outcome, this.config);\n this.proposals.delete(proposalId);\n this.addClosedProposal(proposalId, result);\n this.metrics.timedOutProposals++;\n this.metrics.totalDurationMs += result.durationMs;\n this.metrics.totalVotes += state.votes.size;\n }\n\n private finalize(proposalId: ProposalId, result: ConsensusResult, voteCount: number): void {\n this.proposals.delete(proposalId);\n this.addClosedProposal(proposalId, result);\n this.updateMetrics(result);\n\n // Cache successful results for determinism (Issue #589)\n if (\n this.cacheConfig.enabled &&\n (result.outcome === 'approved' || result.outcome === 'rejected')\n ) {\n this.addToCache(result.proposal, proposalId, result);\n }\n\n this.logger.info('Proposal closed', {\n proposalId,\n outcome: result.outcome,\n approvalPercentage: result.approvalPercentage.toFixed(1),\n voteCount,\n quorumReached: result.quorumReached,\n durationMs: result.durationMs,\n });\n }\n\n /**\n * Adds a closed proposal and evicts oldest entries if over limit.\n * Issue #549: Prevent unbounded memory growth in closedProposals Map.\n */\n private addClosedProposal(proposalId: ProposalId, result: ConsensusResult): void {\n // Evict oldest entries if at capacity (Map maintains insertion order)\n while (this.closedProposals.size >= this.config.maxClosedProposals) {\n const firstKey = this.closedProposals.keys().next();\n if (firstKey.done === true) break;\n const oldestKey = firstKey.value;\n this.closedProposals.delete(oldestKey);\n this.logger.debug('Evicted oldest closed proposal', { evictedId: oldestKey });\n }\n this.closedProposals.set(proposalId, result);\n }\n\n private calculateOutcome(state: ProposalState): VotingOutcome {\n const strategy = this.strategyFactory.getStrategy(state.proposal.algorithm);\n const outcome: VotingOutcome = strategy.calculateOutcome(state.votes, state.voteWeights);\n return outcome;\n }\n\n /**\n * Agreement-based cascading: close early when outcome is mathematically determined.\n * If approvals already exceed the threshold even if all remaining voters reject,\n * or rejections make approval impossible, the proposal can be decided early.\n */\n private canCascadeEarly(state: ProposalState): boolean {\n const required = state.proposal.requiredVoters;\n if (required === undefined || required.length === 0) return false;\n\n const totalExpected = required.length;\n const votesCast = state.votes.size;\n const remaining = totalExpected - votesCast;\n if (remaining <= 0) return false; // All voted — handled by allRequiredVotersVoted\n\n const threshold = VOTING_THRESHOLDS[state.proposal.algorithm];\n\n let approvals = 0;\n let rejections = 0;\n for (const vote of state.votes.values()) {\n if (vote.decision === 'approve') approvals++;\n else if (vote.decision === 'reject') rejections++;\n }\n\n // Can approve even if all remaining reject?\n const minApprovalRate = approvals / totalExpected;\n if (minApprovalRate > threshold) {\n this.logger.info('Agreement cascade: early approval', {\n proposalId: state.proposal.id,\n approvals,\n totalExpected,\n threshold,\n remaining,\n });\n return true;\n }\n\n // Can never reach threshold even if all remaining approve?\n const maxPossibleApprovals = approvals + remaining;\n const maxApprovalRate = maxPossibleApprovals / totalExpected;\n if (maxApprovalRate < threshold) {\n this.logger.info('Agreement cascade: early rejection', {\n proposalId: state.proposal.id,\n rejections,\n totalExpected,\n threshold,\n remaining,\n });\n return true;\n }\n\n return false;\n }\n\n private allRequiredVotersVoted(state: ProposalState): boolean {\n const required = state.proposal.requiredVoters;\n if (required === undefined || required.length === 0) return false;\n return required.every((voterId) => state.votes.has(voterId));\n }\n\n /**\n * Attempts incremental quorum expansion when voting is ambiguous (Issue #1408).\n * Returns true if expansion occurred (wait for new voters), false to close immediately.\n */\n private async tryExpandQuorum(proposalId: ProposalId, state: ProposalState): Promise<boolean> {\n if (!this.quorumConfig.enabled) return false;\n if (this.voterExpansionCallback === undefined) return false;\n\n const rounds = state.expansionRounds ?? 0;\n if (rounds >= this.quorumConfig.maxExpansionRounds) return false;\n\n const required = state.proposal.requiredVoters;\n if (required === undefined) return false;\n\n const threshold = VOTING_THRESHOLDS[state.proposal.algorithm];\n const ambiguous = isVotingAmbiguous(state.votes, required.length, threshold, {\n confidenceThreshold: this.quorumConfig.confidenceThreshold,\n ambiguityBand: this.quorumConfig.ambiguityBand,\n });\n\n if (!ambiguous) return false;\n\n let newVoters: readonly string[];\n try {\n newVoters = await this.voterExpansionCallback(\n proposalId,\n required.length,\n this.quorumConfig.votersPerExpansion\n );\n } catch (err: unknown) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.logger.warn('Incremental quorum: expansion callback threw; closing as-is', {\n proposalId,\n errorMessage: error.message,\n });\n return false;\n }\n\n if (newVoters.length === 0) {\n this.logger.info('Incremental quorum: no additional voters available', { proposalId });\n return false;\n }\n\n // Expand the required voters list\n state.proposal.requiredVoters = [...required, ...newVoters];\n state.expansionRounds = rounds + 1;\n\n this.logger.info('Incremental quorum: expanded voter pool', {\n proposalId,\n round: rounds + 1,\n newVoters: newVoters.length,\n totalVoters: state.proposal.requiredVoters.length,\n });\n\n return true;\n }\n\n private updateMetrics(result: ConsensusResult): void {\n this.metrics.totalDurationMs += result.durationMs;\n this.metrics.totalVotes += result.voteCounts.total;\n if (result.outcome === 'approved') this.metrics.approvedProposals++;\n else if (result.outcome === 'rejected') this.metrics.rejectedProposals++;\n else if (result.outcome === 'timeout') this.metrics.timedOutProposals++;\n }\n\n private createInitialMetrics(): InternalMetrics {\n return {\n totalProposals: 0,\n approvedProposals: 0,\n rejectedProposals: 0,\n timedOutProposals: 0,\n totalDurationMs: 0,\n totalVotes: 0,\n algorithmUsage: {\n simple_majority: 0,\n supermajority: 0,\n unanimous: 0,\n proof_of_learning: 0,\n opinion_wise: 0,\n higher_order: 0,\n },\n };\n }\n\n // ============================================================================\n // Proposal Content Caching (Issue #589)\n // ============================================================================\n\n /**\n * Creates a content hash for a proposal to enable cache lookups.\n * Hash is based on title, description, and algorithm (deterministic content).\n */\n private hashProposalContent(proposal: Proposal): string {\n const content = [proposal.title, proposal.description, proposal.algorithm].join('|');\n // Simple FNV-1a hash for fast deterministic hashing\n let hash = 2166136261;\n for (let i = 0; i < content.length; i++) {\n hash ^= content.charCodeAt(i);\n hash = Math.imul(hash, 16777619);\n }\n return (hash >>> 0).toString(16);\n }\n\n /**\n * Gets cached result for a proposal if it exists and hasn't expired.\n */\n private getCachedResult(proposal: Proposal): ProposalCacheEntry | undefined {\n const hash = this.hashProposalContent(proposal);\n const cached = this.proposalContentCache.get(hash);\n if (cached === undefined) return undefined;\n\n const now = getTimeProvider().now();\n if (now - cached.cachedAt > this.cacheConfig.ttlMs) {\n this.proposalContentCache.delete(hash);\n this.logger.debug('Cache entry expired', { hash });\n return undefined;\n }\n\n return cached;\n }\n\n /**\n * Adds a proposal result to the cache, evicting oldest entries if needed.\n */\n private addToCache(proposal: Proposal, proposalId: ProposalId, result: ConsensusResult): void {\n const hash = this.hashProposalContent(proposal);\n\n // Evict oldest entries if at capacity (Map maintains insertion order)\n while (this.proposalContentCache.size >= this.cacheConfig.maxEntries) {\n const firstKey = this.proposalContentCache.keys().next();\n if (firstKey.done === true) break;\n const oldestKey = firstKey.value;\n this.proposalContentCache.delete(oldestKey);\n this.logger.debug('Evicted oldest cache entry', { hash: oldestKey });\n }\n\n this.proposalContentCache.set(hash, {\n proposalId,\n result,\n cachedAt: getTimeProvider().now(),\n });\n this.logger.debug('Added proposal to cache', { hash, proposalId });\n }\n\n /**\n * Gets the current cache size (for testing/monitoring).\n */\n getCacheSize(): number {\n return this.proposalContentCache.size;\n }\n\n /**\n * Clears the proposal content cache (for testing/reset).\n */\n clearCache(): void {\n this.proposalContentCache.clear();\n this.logger.debug('Proposal cache cleared');\n }\n}\n\n/**\n * Create a consensus engine with the given configuration.\n *\n * @example\n * ```typescript\n * const engine = createConsensusEngine({\n * defaultTimeout: 60000,\n * maxActiveProposals: 10,\n * });\n * ```\n */\nexport function createConsensusEngine(\n config?: Partial<ConsensusEngineConfig>,\n logger?: ILogger\n): ConsensusEngine {\n return new ConsensusEngine(config, logger);\n}\n","/**\n * Unified Quorum Validator\n *\n * Abstracts quorum validation across VotingProtocol, WeightedVoting, and ConsensusEngine.\n * Per redundancy-analysis.md Section 3.4 - consolidates three quorum implementations.\n *\n * @module consensus/quorum-validator\n * (Source: Issue #576, ADR-0003)\n */\n\nimport { createLogger, formatPercentage, type ILogger } from '../core/index.js';\nimport type { ConsensusAlgorithm, Vote, VoteCounts, WeightedVoteCounts } from './types-core.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Agent record for eligibility checks and Byzantine detection.\n */\nexport interface AgentRecord {\n readonly agentId: string;\n readonly weight: number;\n readonly trustScore: number;\n readonly byzantineFlags?: number;\n readonly successRate?: number;\n readonly totalTasks?: number;\n}\n\n/**\n * Quorum validation configuration.\n */\nexport interface QuorumValidationConfig {\n /** Algorithm type */\n readonly algorithm: ConsensusAlgorithm | 'weighted_byzantine';\n /** Numeric threshold (0-1 for percentage, 0-N for weighted sum) */\n readonly threshold: number;\n /** Minimum voters required */\n readonly minVoters: number;\n /** Enable Byzantine detection */\n readonly enableByzantineDetection?: boolean;\n /** Apply confidence multiplier to weights */\n readonly confidenceMultiplier?: boolean;\n /** Include abstentions in quorum calculation */\n readonly includeAbstentions?: boolean;\n}\n\n/**\n * Input for quorum validation.\n */\nexport interface QuorumValidationInput {\n /** Votes cast by agent ID */\n readonly votes: ReadonlyMap<string, Vote>;\n /** Optional: Pre-calculated agent weights */\n readonly agentWeights?: ReadonlyMap<string, number>;\n /** Configuration */\n readonly config: QuorumValidationConfig;\n /** Optional: Agent records for eligibility checks */\n readonly agentRecords?: ReadonlyMap<string, AgentRecord>;\n /** Optional: Required participant count (for ratio-based quorum) */\n readonly requiredParticipants?: number;\n}\n\n/**\n * Quorum validation result (discriminated union).\n */\nexport type QuorumValidationResult =\n | {\n readonly status: 'reached';\n readonly decision: 'approve' | 'reject';\n readonly confidence: number;\n readonly reasoning: string;\n }\n | {\n readonly status: 'not_reached';\n readonly reason: 'insufficient_votes' | 'insufficient_weight' | 'no_consensus';\n readonly details: string;\n }\n | { readonly status: 'invalid'; readonly error: string }\n | { readonly status: 'timeout'; readonly partial: boolean; readonly details: string };\n\n/**\n * Detailed quorum breakdown for observability.\n */\nexport interface QuorumBreakdown {\n readonly totalVotes: number;\n readonly voteCounts: VoteCounts;\n readonly totalWeight: number | undefined;\n readonly weightedCounts: WeightedVoteCounts | undefined;\n readonly threshold: number;\n readonly actualQuorum: number;\n readonly quorumReached: boolean;\n readonly eligibleAgents: readonly string[];\n readonly reasoning: string;\n}\n\n/**\n * Eligibility check result.\n */\nexport type EligibilityResult =\n | { readonly eligible: true; readonly weight: number }\n | {\n readonly eligible: false;\n readonly reason: 'insufficient_weight' | 'low_trust' | 'byzantine_flagged' | 'excluded';\n readonly weight: number;\n };\n\n/**\n * Unified quorum validator interface.\n */\nexport interface IQuorumValidator {\n validateQuorum(input: QuorumValidationInput): QuorumValidationResult;\n getQuorumBreakdown(input: QuorumValidationInput): QuorumBreakdown;\n isAgentEligible(\n agentId: string,\n record: AgentRecord | undefined,\n config: QuorumValidationConfig\n ): EligibilityResult;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Default quorum thresholds by algorithm type.\n * Used for reference when config.threshold is not specified.\n */\nexport const DEFAULT_QUORUM_THRESHOLDS: Readonly<\n Record<ConsensusAlgorithm | 'weighted_byzantine', number>\n> = {\n simple_majority: 0.5,\n supermajority: 0.67,\n unanimous: 1.0,\n proof_of_learning: 0.5,\n opinion_wise: 0.5,\n higher_order: 0.5,\n weighted_byzantine: 0.67,\n};\n\nconst DEFAULT_MIN_TRUST = 0.3;\nconst DEFAULT_MIN_WEIGHT = 0.1;\n\n/**\n * Unified quorum validator implementation.\n */\nexport class QuorumValidator implements IQuorumValidator {\n private readonly logger: ILogger;\n\n constructor(logger?: ILogger) {\n this.logger = logger ?? createLogger({ component: 'QuorumValidator' });\n }\n\n validateQuorum(input: QuorumValidationInput): QuorumValidationResult {\n const { votes, config } = input;\n\n // Validate input\n if (votes.size === 0) {\n return { status: 'invalid', error: 'No votes provided' };\n }\n\n // Calculate breakdown\n const breakdown = this.getQuorumBreakdown(input);\n\n // Check if quorum reached\n if (!breakdown.quorumReached) {\n return this.buildNotReachedResult(breakdown, config);\n }\n\n // Determine decision\n return this.buildReachedResult(breakdown);\n }\n\n getQuorumBreakdown(input: QuorumValidationInput): QuorumBreakdown {\n const { votes, agentWeights, config, agentRecords, requiredParticipants } = input;\n\n // Count votes\n const voteCounts = this.countVotes(votes);\n const eligibleAgents = this.getEligibleAgents(votes, agentRecords, config);\n\n // Calculate weights if applicable\n const { totalWeight, weightedCounts } = this.calculateWeights(\n votes,\n agentWeights,\n agentRecords,\n config\n );\n\n // Calculate quorum based on algorithm\n const { threshold, actualQuorum, quorumReached, reasoning } = this.calculateQuorumStatus(\n voteCounts,\n weightedCounts,\n totalWeight,\n config,\n requiredParticipants ?? votes.size\n );\n\n return {\n totalVotes: votes.size,\n voteCounts,\n totalWeight,\n weightedCounts,\n threshold,\n actualQuorum,\n quorumReached,\n eligibleAgents,\n reasoning,\n };\n }\n\n isAgentEligible(\n agentId: string,\n record: AgentRecord | undefined,\n config: QuorumValidationConfig\n ): EligibilityResult {\n if (record === undefined) {\n return { eligible: true, weight: 1.0 }; // Default eligibility\n }\n\n // Check Byzantine flags\n if (config.enableByzantineDetection === true && (record.byzantineFlags ?? 0) > 0) {\n this.logger.debug('Agent flagged as Byzantine', { agentId, flags: record.byzantineFlags });\n return { eligible: false, reason: 'byzantine_flagged', weight: record.weight };\n }\n\n // Check trust score\n if (record.trustScore < DEFAULT_MIN_TRUST) {\n this.logger.debug('Agent trust score below threshold', {\n agentId,\n trustScore: record.trustScore,\n });\n return { eligible: false, reason: 'low_trust', weight: record.weight };\n }\n\n // Check weight\n if (record.weight < DEFAULT_MIN_WEIGHT) {\n this.logger.debug('Agent weight below threshold', { agentId, weight: record.weight });\n return { eligible: false, reason: 'insufficient_weight', weight: record.weight };\n }\n\n return { eligible: true, weight: record.weight };\n }\n\n private countVotes(votes: ReadonlyMap<string, Vote>): VoteCounts {\n let approve = 0;\n let reject = 0;\n let abstain = 0;\n\n for (const vote of votes.values()) {\n switch (vote.decision) {\n case 'approve':\n approve++;\n break;\n case 'reject':\n reject++;\n break;\n case 'abstain':\n abstain++;\n break;\n }\n }\n\n return { approve, reject, abstain, total: votes.size };\n }\n\n private getEligibleAgents(\n votes: ReadonlyMap<string, Vote>,\n agentRecords: ReadonlyMap<string, AgentRecord> | undefined,\n config: QuorumValidationConfig\n ): readonly string[] {\n const eligible: string[] = [];\n\n for (const agentId of votes.keys()) {\n const record = agentRecords?.get(agentId);\n const result = this.isAgentEligible(agentId, record, config);\n if (result.eligible) {\n eligible.push(agentId);\n }\n }\n\n return eligible;\n }\n\n private calculateWeights(\n votes: ReadonlyMap<string, Vote>,\n agentWeights: ReadonlyMap<string, number> | undefined,\n agentRecords: ReadonlyMap<string, AgentRecord> | undefined,\n config: QuorumValidationConfig\n ): { totalWeight: number | undefined; weightedCounts: WeightedVoteCounts | undefined } {\n // Skip weight calculation for simple algorithms\n if (config.algorithm === 'simple_majority' || config.algorithm === 'unanimous') {\n return { totalWeight: undefined, weightedCounts: undefined };\n }\n\n const counts = { totalWeight: 0, approve: 0, reject: 0, abstain: 0 };\n\n for (const [agentId, vote] of votes.entries()) {\n const weight = this.getVoteWeight(agentId, vote, agentWeights, agentRecords, config);\n counts.totalWeight += weight;\n this.addWeightToDecision(counts, vote.decision, weight);\n }\n\n return {\n totalWeight: counts.totalWeight,\n weightedCounts: {\n approve: counts.approve,\n reject: counts.reject,\n abstain: counts.abstain,\n totalWeight: counts.totalWeight,\n },\n };\n }\n\n private getVoteWeight(\n agentId: string,\n vote: Vote,\n agentWeights: ReadonlyMap<string, number> | undefined,\n agentRecords: ReadonlyMap<string, AgentRecord> | undefined,\n config: QuorumValidationConfig\n ): number {\n let weight = agentWeights?.get(agentId) ?? agentRecords?.get(agentId)?.weight ?? 1.0;\n if (config.confidenceMultiplier === true) {\n weight *= vote.confidence;\n }\n return weight;\n }\n\n private addWeightToDecision(\n counts: { approve: number; reject: number; abstain: number },\n decision: 'approve' | 'reject' | 'abstain',\n weight: number\n ): void {\n counts[decision] += weight;\n }\n\n private calculateQuorumStatus(\n voteCounts: VoteCounts,\n weightedCounts: WeightedVoteCounts | undefined,\n totalWeight: number | undefined,\n config: QuorumValidationConfig,\n requiredParticipants: number\n ): { threshold: number; actualQuorum: number; quorumReached: boolean; reasoning: string } {\n const threshold = config.threshold;\n\n // For weighted algorithms, use weighted counts\n if (weightedCounts !== undefined && totalWeight !== undefined && totalWeight > 0) {\n return this.calculateWeightedQuorum(weightedCounts, totalWeight, threshold);\n }\n\n // For simple algorithms, use vote counts\n return this.calculateSimpleQuorum(voteCounts, requiredParticipants, threshold, config);\n }\n\n private calculateWeightedQuorum(\n weightedCounts: WeightedVoteCounts,\n totalWeight: number,\n threshold: number\n ): { threshold: number; actualQuorum: number; quorumReached: boolean; reasoning: string } {\n const quorumReached = totalWeight >= threshold;\n const approveRatio = weightedCounts.approve / totalWeight;\n const rejectRatio = weightedCounts.reject / totalWeight;\n\n const reasoning = this.buildWeightedReasoning(\n quorumReached,\n totalWeight,\n threshold,\n approveRatio,\n rejectRatio\n );\n\n return { threshold, actualQuorum: totalWeight, quorumReached, reasoning };\n }\n\n private buildWeightedReasoning(\n quorumReached: boolean,\n totalWeight: number,\n threshold: number,\n approveRatio: number,\n rejectRatio: number\n ): string {\n if (!quorumReached) {\n return `Weighted quorum not reached: ${totalWeight.toFixed(2)} < ${String(threshold)}`;\n }\n if (approveRatio > rejectRatio) {\n return `Approval wins with weighted ratio ${formatPercentage(approveRatio, 1)}`;\n }\n if (rejectRatio > approveRatio) {\n return `Rejection wins with weighted ratio ${formatPercentage(rejectRatio, 1)}`;\n }\n return `No clear winner: approve=${formatPercentage(approveRatio, 1)}, reject=${formatPercentage(rejectRatio, 1)}`;\n }\n\n private calculateSimpleQuorum(\n voteCounts: VoteCounts,\n requiredParticipants: number,\n threshold: number,\n config: QuorumValidationConfig\n ): { threshold: number; actualQuorum: number; quorumReached: boolean; reasoning: string } {\n const activeVotes =\n config.includeAbstentions === true\n ? voteCounts.total\n : voteCounts.approve + voteCounts.reject;\n\n const total = requiredParticipants > 0 ? requiredParticipants : activeVotes;\n const approveRatio = total > 0 ? voteCounts.approve / total : 0;\n const rejectRatio = total > 0 ? voteCounts.reject / total : 0;\n\n const hasMinVoters = activeVotes >= config.minVoters;\n const quorumReached = hasMinVoters && (approveRatio >= threshold || rejectRatio >= threshold);\n\n const reasoning = this.buildSimpleReasoning({\n hasMinVoters,\n activeVotes,\n minVoters: config.minVoters,\n approveRatio,\n rejectRatio,\n threshold,\n });\n\n return {\n threshold,\n actualQuorum: Math.max(approveRatio, rejectRatio),\n quorumReached,\n reasoning,\n };\n }\n\n private buildSimpleReasoning(params: {\n hasMinVoters: boolean;\n activeVotes: number;\n minVoters: number;\n approveRatio: number;\n rejectRatio: number;\n threshold: number;\n }): string {\n const { hasMinVoters, activeVotes, minVoters, approveRatio, rejectRatio, threshold } = params;\n if (!hasMinVoters) {\n return `Insufficient voters: ${String(activeVotes)} < ${String(minVoters)}`;\n }\n if (approveRatio >= threshold) {\n return `Approval reaches threshold: ${formatPercentage(approveRatio, 1)} >= ${formatPercentage(threshold, 1)}`;\n }\n if (rejectRatio >= threshold) {\n return `Rejection reaches threshold: ${formatPercentage(rejectRatio, 1)} >= ${formatPercentage(threshold, 1)}`;\n }\n return `No decision reached threshold: approve=${formatPercentage(approveRatio, 1)}, reject=${formatPercentage(rejectRatio, 1)}`;\n }\n\n private buildNotReachedResult(\n breakdown: QuorumBreakdown,\n config: QuorumValidationConfig\n ): QuorumValidationResult {\n const { voteCounts, weightedCounts, threshold } = breakdown;\n\n if (voteCounts.total < config.minVoters) {\n return {\n status: 'not_reached',\n reason: 'insufficient_votes',\n details: `Only ${String(voteCounts.total)} votes, need ${String(config.minVoters)}`,\n };\n }\n\n if (weightedCounts !== undefined && weightedCounts.totalWeight < threshold) {\n return {\n status: 'not_reached',\n reason: 'insufficient_weight',\n details: `Weight ${weightedCounts.totalWeight.toFixed(2)} < threshold ${String(threshold)}`,\n };\n }\n\n return {\n status: 'not_reached',\n reason: 'no_consensus',\n details: breakdown.reasoning,\n };\n }\n\n private buildReachedResult(breakdown: QuorumBreakdown): QuorumValidationResult {\n const { voteCounts, weightedCounts } = breakdown;\n\n // Determine winning decision\n const approves = weightedCounts?.approve ?? voteCounts.approve;\n const rejects = weightedCounts?.reject ?? voteCounts.reject;\n const total = weightedCounts?.totalWeight ?? voteCounts.total;\n\n const decision: 'approve' | 'reject' = approves >= rejects ? 'approve' : 'reject';\n const confidence = total > 0 ? Math.abs(approves - rejects) / total : 0;\n\n return {\n status: 'reached',\n decision,\n confidence,\n reasoning: breakdown.reasoning,\n };\n }\n}\n\n/**\n * Creates a quorum validator instance.\n */\nexport function createQuorumValidator(logger?: ILogger): IQuorumValidator {\n return new QuorumValidator(logger);\n}\n","/**\n * Helper functions for the multi-round voting protocol.\n *\n * @module consensus/voting-protocol-helpers\n * (Source: Issue #100, arXiv:2512.21352)\n */\n\nimport type {\n VotingSession,\n VotingRound,\n VotingRoundPhase,\n VotingProtocolConfig,\n VotingProtocolResult,\n AgentFinding,\n Vote,\n ConsolidatedFinding,\n RoundSummary,\n SycophancyReport,\n SycophancyIndicator,\n} from './types.js';\nimport { createQuorumValidator, type QuorumValidationConfig } from './quorum-validator.js';\nimport { getTimeProvider, getRandomProvider } from '../core/index.js';\n\n/**\n * Generate a unique session ID.\n */\nexport function generateSessionId(): string {\n const time = getTimeProvider();\n const random = getRandomProvider();\n return `session_${String(time.now())}_${random.randomString(7)}`;\n}\n\n/**\n * Generate a unique finding ID.\n */\nexport function generateFindingId(): string {\n const time = getTimeProvider();\n const random = getRandomProvider();\n return `finding_${String(time.now())}_${random.randomString(7)}`;\n}\n\n/**\n * Generate a unique round ID.\n */\nexport function generateRoundId(): string {\n const time = getTimeProvider();\n const random = getRandomProvider();\n return `round_${String(time.now())}_${random.randomString(7)}`;\n}\n\n/**\n * Create a new voting round.\n */\nexport function createRound(phase: VotingRoundPhase, roundNumber: number): VotingRound {\n const time = getTimeProvider();\n return {\n id: generateRoundId(),\n phase,\n status: 'in_progress',\n findings: new Map(),\n findingVotes: new Map(),\n finalVotes: new Map(),\n startedAt: time.nowIso(),\n roundNumber,\n };\n}\n\n/**\n * Find the most common element in an array.\n */\nexport function mostCommon<T>(items: T[]): T | undefined {\n if (items.length === 0) return undefined;\n\n const counts = new Map<T, number>();\n items.forEach((item) => {\n counts.set(item, (counts.get(item) ?? 0) + 1);\n });\n\n let maxCount = 0;\n let result: T | undefined;\n counts.forEach((count, item) => {\n if (count > maxCount) {\n maxCount = count;\n result = item;\n }\n });\n\n return result;\n}\n\n/**\n * Calculate agreement score from votes.\n */\nexport function calculateAgreementScore(votes: Vote[]): number {\n if (votes.length < 2) return 1;\n\n const decisions = votes.map((v) => v.decision);\n const mostCommonDecision = mostCommon(decisions);\n const agreementCount = decisions.filter((d) => d === mostCommonDecision).length;\n\n return agreementCount / votes.length;\n}\n\n/**\n * Determine outcome from final votes using unified QuorumValidator.\n * Per ADR-0003, this delegates to the consolidated quorum logic.\n */\nexport function determineOutcome(\n votes: Vote[],\n config: VotingProtocolConfig\n): VotingProtocolResult['outcome'] {\n if (votes.length === 0) return 'no_consensus';\n\n // Convert array to Map for QuorumValidator\n const voteMap = new Map<string, Vote>();\n votes.forEach((vote, index) => {\n voteMap.set(`voter_${String(index)}`, vote);\n });\n\n // Create quorum config from voting protocol config\n const quorumConfig: QuorumValidationConfig = {\n algorithm: 'simple_majority',\n threshold: config.agreementThreshold,\n minVoters: 2, // Minimum for valid consensus\n };\n\n // Use unified QuorumValidator\n const validator = createQuorumValidator();\n const result = validator.validateQuorum({\n votes: voteMap,\n config: quorumConfig,\n });\n\n // Map QuorumValidationResult to VotingProtocolResult outcome\n if (result.status === 'reached') {\n return result.decision === 'approve' ? 'approved' : 'rejected';\n }\n\n // Check for mixed votes (needs revision)\n const approvals = votes.filter((v) => v.decision === 'approve').length;\n const rejections = votes.filter((v) => v.decision === 'reject').length;\n if (approvals > 0 && rejections > 0) return 'needs_revision';\n\n return 'no_consensus';\n}\n\n/**\n * Consolidate findings from deliberation round.\n */\nexport function consolidateFindings(session: VotingSession): ConsolidatedFinding[] {\n const deliberationRound = session.rounds[1];\n if (!deliberationRound) return [];\n\n const consolidated: ConsolidatedFinding[] = [];\n\n deliberationRound.findings.forEach((finding, findingId) => {\n const votes = deliberationRound.findingVotes.get(findingId) ?? [];\n const agreeVotes = votes.filter((v) => v.agree);\n const agreementRatio = votes.length > 0 ? agreeVotes.length / votes.length : 0;\n\n // Only include findings with some agreement\n if (agreementRatio >= 0.5) {\n // Determine final severity (use most common amended severity or original)\n const severityVotes = agreeVotes\n .filter((v) => v.amendedSeverity)\n .map((v) => v.amendedSeverity);\n const finalSeverity =\n severityVotes.length > 0\n ? (mostCommon(severityVotes) ?? finding.severity)\n : finding.severity;\n\n const consolidatedFinding: ConsolidatedFinding = {\n id: findingId,\n category: finding.category,\n severity: finalSeverity,\n description: finding.description,\n supportingAgents: agreeVotes.map((v) => v.agentId),\n agreementRatio,\n originalFindings: [finding],\n };\n if (finding.location !== undefined) {\n consolidatedFinding.location = finding.location;\n }\n if (finding.suggestion !== undefined) {\n consolidatedFinding.suggestion = finding.suggestion;\n }\n consolidated.push(consolidatedFinding);\n }\n });\n\n // Sort by severity and agreement\n return consolidated.sort((a, b) => {\n const severityOrder = { critical: 0, major: 1, minor: 2, suggestion: 3 };\n const sevDiff = severityOrder[a.severity] - severityOrder[b.severity];\n if (sevDiff !== 0) return sevDiff;\n return b.agreementRatio - a.agreementRatio;\n });\n}\n\n/**\n * Build round summaries from session.\n */\nexport function buildRoundSummaries(session: VotingSession): RoundSummary[] {\n const time = getTimeProvider();\n return session.rounds.map((round, index) => {\n const startTime = new Date(round.startedAt).getTime();\n const endTime =\n round.completedAt !== undefined ? new Date(round.completedAt).getTime() : time.now();\n\n let votesCount = 0;\n let agreementScore = 0;\n\n if (round.phase === 'deliberation') {\n // Count finding votes\n round.findingVotes.forEach((votes) => {\n votesCount += votes.length;\n const agreeCount = votes.filter((v) => v.agree).length;\n agreementScore += votes.length > 0 ? agreeCount / votes.length : 0;\n });\n const findingsCount = round.findingVotes.size;\n if (findingsCount > 0) {\n agreementScore /= findingsCount;\n }\n } else if (round.phase === 'consensus') {\n // Count final votes\n votesCount = round.finalVotes.size;\n const votes = Array.from(round.finalVotes.values());\n agreementScore = calculateAgreementScore(votes);\n }\n\n return {\n roundNumber: index + 1,\n phase: round.phase,\n findingsCount: round.findings.size,\n votesCount,\n agreementScore,\n durationMs: endTime - startTime,\n };\n });\n}\n\n// ============================================================================\n// Sycophancy Detection\n// ============================================================================\n\n/**\n * Check for premature consensus pattern.\n */\nexport function checkPrematureConsensus(session: VotingSession): SycophancyIndicator | null {\n const analysisRound = session.rounds[0];\n if (!analysisRound) return null;\n\n const agentFindings = new Map<string, AgentFinding[]>();\n analysisRound.findings.forEach((finding) => {\n const existing = agentFindings.get(finding.agentId) ?? [];\n existing.push(finding);\n agentFindings.set(finding.agentId, existing);\n });\n\n const allConfidences: number[] = [];\n analysisRound.findings.forEach((finding) => {\n allConfidences.push(finding.confidence);\n });\n\n const avgConfidence =\n allConfidences.length > 0\n ? allConfidences.reduce((a, b) => a + b, 0) / allConfidences.length\n : 0;\n\n if (avgConfidence > 0.95 && agentFindings.size >= 2) {\n return {\n type: 'premature_consensus',\n description: 'All agents showing unusually high confidence',\n severity: 'medium',\n agents: Array.from(agentFindings.keys()),\n };\n }\n\n return null;\n}\n\n/**\n * Check for opinion convergence pattern.\n */\nexport function checkOpinionConvergence(session: VotingSession): SycophancyIndicator | null {\n const deliberationRound = session.rounds[1];\n if (!deliberationRound) return null;\n\n let totalVotes = 0;\n let agreeVotes = 0;\n\n deliberationRound.findingVotes.forEach((votes) => {\n totalVotes += votes.length;\n agreeVotes += votes.filter((v) => v.agree).length;\n });\n\n const agreeRatio = totalVotes > 0 ? agreeVotes / totalVotes : 0;\n\n if (agreeRatio > session.config.sycophancyThreshold && totalVotes > 3) {\n const affectedAgents: string[] = [];\n deliberationRound.findingVotes.forEach((votes) => {\n votes.forEach((v) => {\n if (v.agree && !affectedAgents.includes(v.agentId)) {\n affectedAgents.push(v.agentId);\n }\n });\n });\n\n return {\n type: 'opinion_convergence',\n description: `${String(Math.round(agreeRatio * 100))}% agreement rate suggests possible opinion convergence`,\n severity: 'high',\n agents: affectedAgents,\n };\n }\n\n return null;\n}\n\n/**\n * Check for confidence inflation pattern.\n */\nexport function checkConfidenceInflation(session: VotingSession): SycophancyIndicator | null {\n const consensusRound = session.rounds[2];\n if (!consensusRound) return null;\n\n const votes = Array.from(consensusRound.finalVotes.values());\n if (votes.length < 2) return null;\n\n const confidences = votes.map((v) => v.confidence);\n const avgConfidence = confidences.reduce((a, b) => a + b, 0) / confidences.length;\n const allHigh = confidences.every((c) => c > 0.9);\n\n if (allHigh && avgConfidence > 0.95) {\n return {\n type: 'confidence_inflation',\n description: 'All agents reporting very high confidence suggests possible sycophancy',\n severity: 'medium',\n agents: Array.from(consensusRound.finalVotes.keys()),\n };\n }\n\n return null;\n}\n\n/**\n * Run all sycophancy detection checks.\n */\nexport function detectSycophancyPatterns(session: VotingSession): SycophancyReport {\n const indicators: SycophancyIndicator[] = [];\n const affectedAgents: string[] = [];\n\n const prematureConsensus = checkPrematureConsensus(session);\n if (prematureConsensus) {\n indicators.push(prematureConsensus);\n affectedAgents.push(...prematureConsensus.agents);\n }\n\n const opinionConvergence = checkOpinionConvergence(session);\n if (opinionConvergence) {\n indicators.push(opinionConvergence);\n affectedAgents.push(...opinionConvergence.agents);\n }\n\n const confidenceInflation = checkConfidenceInflation(session);\n if (confidenceInflation) {\n indicators.push(confidenceInflation);\n affectedAgents.push(...confidenceInflation.agents);\n }\n\n const detected = indicators.length > 0;\n const confidenceScore = Math.min(1, indicators.length * 0.3);\n const uniqueAffected = [...new Set(affectedAgents)];\n\n return {\n detected,\n confidenceScore,\n indicators,\n affectedAgents: uniqueAffected,\n recommendation: detected\n ? 'Consider requesting independent re-analysis or adding more diverse agents'\n : 'No sycophancy detected',\n };\n}\n","/**\n * Multi-Round Voting Protocol Implementation\n *\n * Implements a structured 3-round voting protocol for multi-agent code review\n * based on research showing 91.7-100% success rates vs 78% single-agent baseline.\n *\n * @module consensus/voting-protocol\n * (Source: Issue #100, arXiv:2512.21352 - Multi-Agent Committees)\n * (Source: arXiv:2509.23055 - Sycophancy Prevention)\n */\n\nimport { createLogger } from '../core/logger.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { ILogger } from '../core/index.js';\nimport type {\n IVotingProtocol,\n VotingSession,\n VotingRound,\n VotingProtocolConfig,\n VotingProtocolResult,\n AgentFinding,\n FindingVote,\n Vote,\n SycophancyReport,\n} from './types.js';\nimport {\n AgentFindingSchema,\n FindingVoteSchema,\n VoteSchema,\n DEFAULT_VOTING_PROTOCOL_CONFIG,\n} from './types.js';\nimport {\n generateSessionId,\n generateFindingId,\n createRound,\n consolidateFindings,\n buildRoundSummaries,\n determineOutcome,\n calculateAgreementScore,\n detectSycophancyPatterns,\n} from './voting-protocol-helpers.js';\n\nconst logger: ILogger = createLogger({ component: 'voting-protocol' });\n\n/**\n * Multi-round voting protocol for code review.\n */\nexport class VotingProtocol implements IVotingProtocol {\n private readonly sessions: Map<string, VotingSession> = new Map();\n private readonly logger: ILogger;\n\n constructor(customLogger?: ILogger) {\n this.logger = customLogger ?? logger;\n }\n\n /**\n * Create a new voting session with a committee.\n */\n createSession(\n topic: string,\n committee: string[],\n config?: Partial<VotingProtocolConfig>\n ): VotingSession {\n const sessionConfig = { ...DEFAULT_VOTING_PROTOCOL_CONFIG, ...config };\n\n if (committee.length < 2) {\n throw new Error('Committee must have at least 2 members');\n }\n\n if (committee.length > sessionConfig.committeeSize) {\n throw new Error(`Committee size exceeds maximum (${String(sessionConfig.committeeSize)})`);\n }\n\n const session: VotingSession = {\n id: generateSessionId(),\n topic,\n committee,\n rounds: [],\n currentRound: 0,\n config: sessionConfig,\n status: 'active',\n createdAt: getTimeProvider().nowIso(),\n };\n\n this.sessions.set(session.id, session);\n this.logger.info('Voting session created', {\n sessionId: session.id,\n topic,\n committeeSize: committee.length,\n });\n\n return session;\n }\n\n /**\n * Start the analysis round (Round 1).\n */\n startAnalysisRound(sessionId: string): Promise<VotingRound> {\n const session = this.getSessionOrThrow(sessionId);\n this.validateSessionActive(session);\n\n if (session.currentRound !== 0) {\n return Promise.reject(new Error('Analysis round can only be started as Round 1'));\n }\n\n const round = createRound('analysis', 1);\n session.rounds.push(round);\n session.currentRound = 1;\n\n this.logger.info('Analysis round started', { sessionId, roundId: round.id });\n return Promise.resolve(round);\n }\n\n /**\n * Submit findings from an agent during analysis.\n */\n submitFindings(sessionId: string, agentId: string, findings: AgentFinding[]): Promise<void> {\n const session = this.getSessionOrThrow(sessionId);\n this.validateSessionActive(session);\n\n const currentRound = this.getCurrentRound(session);\n if (currentRound.phase !== 'analysis') {\n return Promise.reject(new Error('Findings can only be submitted during analysis round'));\n }\n\n if (!session.committee.includes(agentId)) {\n return Promise.reject(new Error(`Agent ${agentId} is not a committee member`));\n }\n\n for (const finding of findings) {\n const validation = AgentFindingSchema.safeParse(finding);\n if (!validation.success) {\n return Promise.reject(new Error(`Invalid finding: ${validation.error.message}`));\n }\n\n const findingId = generateFindingId();\n const timestampedFinding = {\n ...validation.data,\n agentId,\n timestamp: getTimeProvider().nowIso(),\n };\n currentRound.findings.set(findingId, timestampedFinding);\n }\n\n this.logger.debug('Findings submitted', { sessionId, agentId, findingsCount: findings.length });\n return Promise.resolve();\n }\n\n /**\n * Start the deliberation round (Round 2).\n */\n startDeliberationRound(sessionId: string): Promise<VotingRound> {\n const session = this.getSessionOrThrow(sessionId);\n this.validateSessionActive(session);\n\n if (session.currentRound !== 1) {\n return Promise.reject(new Error('Deliberation round can only be started after analysis'));\n }\n\n const analysisRound = session.rounds[0];\n if (analysisRound) {\n analysisRound.status = 'completed';\n analysisRound.completedAt = getTimeProvider().nowIso();\n }\n\n if (session.config.enableAntiSycophancy) {\n const report = this.detectSycophancy(sessionId);\n if (report.detected) {\n this.logger.warn('Sycophancy detected before deliberation', {\n sessionId,\n indicators: report.indicators.length,\n });\n }\n }\n\n const round = createRound('deliberation', 2);\n if (analysisRound) {\n analysisRound.findings.forEach((finding, id) => {\n round.findings.set(id, finding);\n round.findingVotes.set(id, []);\n });\n }\n\n session.rounds.push(round);\n session.currentRound = 2;\n\n this.logger.info('Deliberation round started', {\n sessionId,\n roundId: round.id,\n findingsCount: round.findings.size,\n });\n return Promise.resolve(round);\n }\n\n /**\n * Vote on findings during deliberation.\n */\n voteOnFinding(sessionId: string, vote: FindingVote): Promise<void> {\n const session = this.getSessionOrThrow(sessionId);\n this.validateSessionActive(session);\n\n const currentRound = this.getCurrentRound(session);\n if (currentRound.phase !== 'deliberation') {\n return Promise.reject(new Error('Finding votes can only be submitted during deliberation'));\n }\n\n const validation = FindingVoteSchema.safeParse(vote);\n if (!validation.success) {\n return Promise.reject(new Error(`Invalid vote: ${validation.error.message}`));\n }\n\n if (!session.committee.includes(vote.agentId)) {\n return Promise.reject(new Error(`Agent ${vote.agentId} is not a committee member`));\n }\n\n if (!currentRound.findings.has(vote.findingId)) {\n return Promise.reject(new Error(`Finding ${vote.findingId} not found`));\n }\n\n const existingVotes = currentRound.findingVotes.get(vote.findingId) ?? [];\n const filtered = existingVotes.filter((v) => v.agentId !== vote.agentId);\n filtered.push(validation.data);\n currentRound.findingVotes.set(vote.findingId, filtered);\n\n this.logger.debug('Finding vote recorded', {\n sessionId,\n agentId: vote.agentId,\n findingId: vote.findingId,\n agree: vote.agree,\n });\n return Promise.resolve();\n }\n\n /**\n * Start the consensus round (Round 3).\n */\n startConsensusRound(sessionId: string): Promise<VotingRound> {\n const session = this.getSessionOrThrow(sessionId);\n this.validateSessionActive(session);\n\n if (session.currentRound !== 2) {\n return Promise.reject(new Error('Consensus round can only be started after deliberation'));\n }\n\n const deliberationRound = session.rounds[1];\n if (deliberationRound) {\n deliberationRound.status = 'completed';\n deliberationRound.completedAt = getTimeProvider().nowIso();\n }\n\n const round = createRound('consensus', 3);\n session.rounds.push(round);\n session.currentRound = 3;\n\n this.logger.info('Consensus round started', { sessionId, roundId: round.id });\n return Promise.resolve(round);\n }\n\n /**\n * Submit final vote during consensus.\n */\n submitFinalVote(sessionId: string, agentId: string, vote: Vote): Promise<void> {\n const session = this.getSessionOrThrow(sessionId);\n this.validateSessionActive(session);\n\n const currentRound = this.getCurrentRound(session);\n if (currentRound.phase !== 'consensus') {\n return Promise.reject(new Error('Final votes can only be submitted during consensus'));\n }\n\n const validation = VoteSchema.safeParse(vote);\n if (!validation.success) {\n return Promise.reject(new Error(`Invalid vote: ${validation.error.message}`));\n }\n\n if (!session.committee.includes(agentId)) {\n return Promise.reject(new Error(`Agent ${agentId} is not a committee member`));\n }\n\n currentRound.finalVotes.set(agentId, {\n ...validation.data,\n timestamp: getTimeProvider().nowIso(),\n });\n\n this.logger.debug('Final vote recorded', {\n sessionId,\n agentId,\n decision: vote.decision,\n confidence: vote.confidence,\n });\n return Promise.resolve();\n }\n\n /**\n * Get the final result.\n */\n getResult(sessionId: string): Promise<VotingProtocolResult | null> {\n const session = this.sessions.get(sessionId);\n if (!session) return Promise.resolve(null);\n\n if (session.status === 'completed' && session.finalResult) {\n return Promise.resolve(session.finalResult);\n }\n\n if (session.currentRound < 3) return Promise.resolve(null);\n\n const consensusRound = session.rounds[2];\n if (!consensusRound) return Promise.resolve(null);\n\n const allVoted = session.committee.every((agentId) => consensusRound.finalVotes.has(agentId));\n if (!allVoted) return Promise.resolve(null);\n\n const result = this.buildFinalResult(session);\n session.finalResult = result;\n session.status = 'completed';\n session.completedAt = getTimeProvider().nowIso();\n\n consensusRound.status = 'completed';\n consensusRound.completedAt = getTimeProvider().nowIso();\n\n this.logger.info('Voting session completed', {\n sessionId,\n outcome: result.outcome,\n agreementScore: result.agreementScore,\n consolidatedFindings: result.consolidatedFindings.length,\n });\n\n return Promise.resolve(result);\n }\n\n /**\n * Detect sycophancy patterns.\n */\n detectSycophancy(sessionId: string): SycophancyReport {\n const session = this.getSessionOrThrow(sessionId);\n return detectSycophancyPatterns(session);\n }\n\n /**\n * Get the current session state.\n */\n getSession(sessionId: string): VotingSession | undefined {\n return this.sessions.get(sessionId);\n }\n\n private getSessionOrThrow(sessionId: string): VotingSession {\n const session = this.sessions.get(sessionId);\n if (!session) {\n throw new Error(`Session ${sessionId} not found`);\n }\n return session;\n }\n\n private validateSessionActive(session: VotingSession): void {\n if (session.status !== 'active') {\n throw new Error(`Session ${session.id} is ${session.status}`);\n }\n }\n\n private getCurrentRound(session: VotingSession): VotingRound {\n const round = session.rounds[session.currentRound - 1];\n if (!round) {\n throw new Error(`No round ${String(session.currentRound)} found`);\n }\n return round;\n }\n\n private buildFinalResult(session: VotingSession): VotingProtocolResult {\n const startTime = new Date(session.createdAt).getTime();\n const endTime = getTimeProvider().now();\n\n const consolidatedFindingsList = consolidateFindings(session);\n const roundSummaries = buildRoundSummaries(session);\n\n const consensusRound = session.rounds[2];\n const finalVotes = consensusRound ? Array.from(consensusRound.finalVotes.values()) : [];\n const outcome = determineOutcome(finalVotes, session.config);\n const agreementScore = calculateAgreementScore(finalVotes);\n const sycophancyReport = detectSycophancyPatterns(session);\n\n return {\n sessionId: session.id,\n topic: session.topic,\n outcome,\n consolidatedFindings: consolidatedFindingsList,\n roundSummaries,\n agreementScore,\n sycophancyDetected: sycophancyReport.detected,\n totalDurationMs: endTime - startTime,\n participatingAgents: session.committee,\n };\n }\n}\n\n/**\n * Create a voting protocol instance.\n */\nexport function createVotingProtocol(customLogger?: ILogger): VotingProtocol {\n return new VotingProtocol(customLogger);\n}\n","/**\n * Byzantine Detection EventBus Integration Helpers\n * (Source: Issue #218, Sprint #228)\n *\n * Provides helper functions for emitting Byzantine fault detection events.\n * Used by CP-WBFT weighted voting to report Byzantine behavior patterns.\n *\n * @module agents/collaboration/byzantine-events\n */\n\nimport type {\n IEventBus,\n ByzantineWeightUpdatedEvent,\n ByzantinePatternDetectedEvent,\n ByzantineAgentFlaggedEvent,\n ByzantineCollusionSuspectedEvent,\n} from './event-bus-types.js';\nimport { createEvent } from './event-bus.js';\n\n// =============================================================================\n// Weight Update Events\n// =============================================================================\n\n/** Parameters for emitting byzantine.weight_updated event. */\nexport interface WeightUpdatedParams {\n readonly agentId: string;\n readonly previousWeight: number;\n readonly newWeight: number;\n readonly reason: 'performance_update' | 'flag_penalty' | 'recalibration';\n readonly sessionId?: string | undefined;\n readonly correlationId?: string | undefined;\n}\n\n/** Emits byzantine.weight_updated event when an agent's weight changes. */\nexport function emitWeightUpdated(eventBus: IEventBus, params: WeightUpdatedParams): void {\n const event = createEvent<ByzantineWeightUpdatedEvent>(\n 'byzantine.weight_updated',\n {\n agentId: params.agentId,\n previousWeight: params.previousWeight,\n newWeight: params.newWeight,\n reason: params.reason,\n },\n {\n ...(params.sessionId !== undefined && { sessionId: params.sessionId }),\n ...(params.correlationId !== undefined && { correlationId: params.correlationId }),\n }\n );\n eventBus.emit(event);\n}\n\n// =============================================================================\n// Pattern Detection Events\n// =============================================================================\n\n/** Parameters for emitting byzantine.pattern_detected event. */\nexport interface PatternDetectedParams {\n readonly patternType: 'contrarian' | 'collusion';\n readonly agentIds: readonly string[];\n readonly confidence: number;\n readonly details: string;\n readonly sessionId?: string | undefined;\n readonly correlationId?: string | undefined;\n}\n\n/** Emits byzantine.pattern_detected event when Byzantine pattern is detected. */\nexport function emitPatternDetected(eventBus: IEventBus, params: PatternDetectedParams): void {\n const event = createEvent<ByzantinePatternDetectedEvent>(\n 'byzantine.pattern_detected',\n {\n patternType: params.patternType,\n agentIds: params.agentIds,\n confidence: params.confidence,\n details: params.details,\n },\n {\n ...(params.sessionId !== undefined && { sessionId: params.sessionId }),\n ...(params.correlationId !== undefined && { correlationId: params.correlationId }),\n }\n );\n eventBus.emit(event);\n}\n\n// =============================================================================\n// Agent Flagged Events\n// =============================================================================\n\n/** Parameters for emitting byzantine.agent_flagged event. */\nexport interface AgentFlaggedParams {\n readonly agentId: string;\n readonly reason: string;\n readonly previousWeight: number;\n readonly canVote: boolean;\n readonly sessionId?: string | undefined;\n readonly correlationId?: string | undefined;\n}\n\n/** Emits byzantine.agent_flagged event when an agent is flagged as Byzantine. */\nexport function emitAgentFlagged(eventBus: IEventBus, params: AgentFlaggedParams): void {\n const event = createEvent<ByzantineAgentFlaggedEvent>(\n 'byzantine.agent_flagged',\n {\n agentId: params.agentId,\n reason: params.reason,\n previousWeight: params.previousWeight,\n canVote: params.canVote,\n },\n {\n ...(params.sessionId !== undefined && { sessionId: params.sessionId }),\n ...(params.correlationId !== undefined && { correlationId: params.correlationId }),\n }\n );\n eventBus.emit(event);\n}\n\n// =============================================================================\n// Collusion Suspected Events\n// =============================================================================\n\n/** Parameters for emitting byzantine.collusion_suspected event. */\nexport interface CollusionSuspectedParams {\n readonly groupAgentIds: readonly string[];\n readonly groupSize: number;\n readonly votingBlock: number;\n readonly threshold: number;\n readonly sessionId?: string | undefined;\n readonly correlationId?: string | undefined;\n}\n\n/** Emits byzantine.collusion_suspected event when collusion pattern is suspected. */\nexport function emitCollusionSuspected(\n eventBus: IEventBus,\n params: CollusionSuspectedParams\n): void {\n const event = createEvent<ByzantineCollusionSuspectedEvent>(\n 'byzantine.collusion_suspected',\n {\n groupAgentIds: params.groupAgentIds,\n groupSize: params.groupSize,\n votingBlock: params.votingBlock,\n threshold: params.threshold,\n },\n {\n ...(params.sessionId !== undefined && { sessionId: params.sessionId }),\n ...(params.correlationId !== undefined && { correlationId: params.correlationId }),\n }\n );\n eventBus.emit(event);\n}\n","/**\n * nexus-agents/consensus - Weighted Byzantine Voting Helpers\n *\n * Pure helper functions extracted from WeightedVoting class.\n * These functions are stateless and operate on parameters only.\n *\n * @module consensus/weighted-voting-helpers\n * (Source: Issue #103, arXiv:2511.10400 - CP-WBFT)\n */\n\nimport { getTimeProvider } from '../core/index.js';\nimport { clamp01 } from '../utils/math-utils.js';\nimport type { WeightedAgentRecord, WeightedConsensusResult, Vote } from './types.js';\nimport type { IEventBus } from '../core/event-bus.js';\n\n/**\n * Mutable agent record for internal tracking.\n * Used internally by WeightedVoting class.\n */\nexport interface MutableAgentRecord {\n agentId: string;\n totalTasks: number;\n successfulTasks: number;\n failedTasks: number;\n partialTasks: number;\n successRate: number;\n weight: number;\n trustScore: number;\n byzantineFlags: number;\n byzantineReasons: string[];\n lastActive: Date;\n createdAt: Date;\n}\n\n/** Options for WeightedVoting constructor. */\nexport interface WeightedVotingOptions {\n /** Configuration for voting thresholds and weights. */\n config?: Partial<import('./types.js').WeightedVotingConfig>;\n /** Optional event bus for Byzantine detection events (Issue #218). */\n eventBus?: IEventBus;\n /** Whether to emit Byzantine detection events (default: true if eventBus provided). */\n emitEvents?: boolean;\n}\n\n/**\n * Check if a vote is a low-confidence contrarian vote.\n * Used for Byzantine pattern detection.\n */\nexport function isLowConfidenceContrarian(vote: Vote, majorityApprove: boolean): boolean {\n const isContrarian = majorityApprove ? vote.decision === 'reject' : vote.decision === 'approve';\n return isContrarian && vote.confidence < 0.3;\n}\n\n/**\n * Compute the majority vote direction based on weighted votes.\n * Returns true if majority approves, false if majority rejects.\n */\nexport function computeMajorityDirection(\n voteArray: ReadonlyArray<readonly [string, Vote]>,\n weights: ReadonlyMap<string, number>\n): boolean {\n let totalApprove = 0;\n let totalReject = 0;\n for (const [agentId, vote] of voteArray) {\n const w = weights.get(agentId) ?? 0;\n if (vote.decision === 'approve') totalApprove += w;\n if (vote.decision === 'reject') totalReject += w;\n }\n return totalApprove > totalReject;\n}\n\n/**\n * Determine consensus decision based on vote tallies.\n */\nexport function determineDecision(\n approval: number,\n rejection: number,\n total: number,\n quorumReached: boolean,\n quorumThreshold: number\n): WeightedConsensusResult['decision'] {\n if (!quorumReached || total === 0) return 'no_consensus';\n const approvalRatio = approval / total;\n const rejectionRatio = rejection / total;\n if (approvalRatio > rejectionRatio && approvalRatio >= quorumThreshold) return 'approve';\n if (rejectionRatio > approvalRatio && rejectionRatio >= quorumThreshold) return 'reject';\n return 'no_consensus';\n}\n\n/**\n * Update derived metrics (success rate, trust score) on a mutable record.\n */\nexport function updateDerivedMetrics(record: MutableAgentRecord): void {\n // Update success rate\n if (record.totalTasks > 0) {\n const weightedSuccess = record.successfulTasks + record.partialTasks * 0.5;\n record.successRate = weightedSuccess / record.totalTasks;\n }\n\n // Update trust score based on weight and Byzantine flags\n const byzantinePenalty = Math.pow(0.7, record.byzantineFlags);\n record.trustScore = Math.min(1, record.weight * byzantinePenalty);\n}\n\n/**\n * Convert a mutable internal record to an immutable public record.\n */\nexport function toImmutableRecord(record: MutableAgentRecord): WeightedAgentRecord {\n return {\n agentId: record.agentId,\n totalTasks: record.totalTasks,\n successfulTasks: record.successfulTasks,\n failedTasks: record.failedTasks,\n partialTasks: record.partialTasks,\n successRate: record.successRate,\n weight: record.weight,\n trustScore: record.trustScore,\n byzantineFlags: record.byzantineFlags,\n lastActive: record.lastActive,\n createdAt: record.createdAt,\n };\n}\n\n/**\n * Create a vote signature for collusion detection.\n * Combines decision and confidence into a unique key.\n */\nexport function createVoteSignature(vote: Vote): string {\n return `${vote.decision}:${vote.confidence.toFixed(2)}`;\n}\n\n/**\n * Group votes by their signature for collusion pattern detection.\n */\nexport function groupVotesBySignature(\n voteArray: ReadonlyArray<readonly [string, Vote]>\n): Map<string, string[]> {\n const signatures = new Map<string, string[]>();\n for (const [agentId, vote] of voteArray) {\n const sig = createVoteSignature(vote);\n const agents = signatures.get(sig) ?? [];\n agents.push(agentId);\n signatures.set(sig, agents);\n }\n return signatures;\n}\n\n/**\n * Create a new mutable agent record with initial values.\n */\nexport function createAgentRecord(agentId: string, initialWeight: number): MutableAgentRecord {\n const now = new Date(getTimeProvider().now());\n return {\n agentId,\n totalTasks: 0,\n successfulTasks: 0,\n failedTasks: 0,\n partialTasks: 0,\n successRate: 0,\n weight: initialWeight,\n trustScore: initialWeight,\n byzantineFlags: 0,\n byzantineReasons: [],\n lastActive: now,\n createdAt: now,\n };\n}\n\n/**\n * Compute global success statistics from all agent records.\n */\nexport function computeGlobalStats(records: Iterable<MutableAgentRecord>): {\n globalSuccessRate: number;\n totalTasks: number;\n} {\n let totalSuccess = 0;\n let totalTasks = 0;\n\n for (const record of records) {\n totalSuccess += record.successfulTasks;\n totalTasks += record.totalTasks;\n }\n\n const globalSuccessRate = totalTasks > 0 ? totalSuccess / totalTasks : 0.5;\n return { globalSuccessRate, totalTasks };\n}\n\n/**\n * Calculate calibrated weight based on relative performance.\n */\nexport function calculateCalibratedWeight(\n record: MutableAgentRecord,\n globalSuccessRate: number,\n initialWeight: number\n): number {\n const relativePerformance = record.successRate / Math.max(0.01, globalSuccessRate);\n const calibratedWeight = clamp01(initialWeight * relativePerformance);\n // Smooth transition (50% old weight, 50% calibrated)\n return (record.weight + calibratedWeight) / 2;\n}\n\n/**\n * Apply weight change based on task outcome.\n */\nexport function applyOutcomeWeight(\n currentWeight: number,\n outcome: import('./types.js').TaskOutcome,\n decayFactor: number,\n recoveryFactor: number\n): number {\n switch (outcome) {\n case 'success':\n return Math.min(1, currentWeight * recoveryFactor);\n case 'failure':\n return Math.max(0, currentWeight * decayFactor);\n case 'partial':\n return Math.max(0, currentWeight * ((decayFactor + 1) / 2));\n case 'unknown':\n return currentWeight;\n }\n}\n","/**\n * nexus-agents/consensus - Weighted Byzantine Voting Implementation\n *\n * Implements CP-WBFT (arXiv:2511.10400) for weighted Byzantine fault-tolerant voting.\n * Agent votes are weighted by historical reliability with automatic trust calibration.\n *\n * @module consensus/weighted-voting\n * (Source: Issue #103, arXiv:2511.10400 - CP-WBFT)\n */\n\nimport { createLogger } from '../core/logger.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type {\n IWeightedVoting,\n WeightedAgentRecord,\n WeightedConsensusResult,\n WeightedVotingConfig,\n TaskOutcome,\n Vote,\n} from './types.js';\nimport { DEFAULT_WEIGHTED_VOTING_CONFIG } from './types.js';\nimport type { IEventBus } from '../core/event-bus.js';\nimport {\n emitWeightUpdated,\n emitPatternDetected,\n emitAgentFlagged,\n emitCollusionSuspected,\n} from '../agents/collaboration/byzantine-events.js';\nimport {\n type MutableAgentRecord,\n type WeightedVotingOptions,\n isLowConfidenceContrarian,\n computeMajorityDirection,\n determineDecision,\n updateDerivedMetrics,\n toImmutableRecord,\n groupVotesBySignature,\n createAgentRecord,\n computeGlobalStats,\n calculateCalibratedWeight,\n applyOutcomeWeight,\n} from './weighted-voting-helpers.js';\n\nexport type { WeightedVotingOptions } from './weighted-voting-helpers.js';\n\nconst logger = createLogger({ component: 'weighted-voting' });\n\n/**\n * Weighted Byzantine voting implementation.\n * Implements CP-WBFT pattern for fault-tolerant multi-agent consensus.\n */\nexport class WeightedVoting implements IWeightedVoting {\n private readonly records: Map<string, MutableAgentRecord> = new Map();\n private readonly config: WeightedVotingConfig;\n private readonly eventBus: IEventBus | undefined;\n private readonly emitEvents: boolean;\n\n constructor(options: WeightedVotingOptions = {}) {\n this.config = { ...DEFAULT_WEIGHTED_VOTING_CONFIG, ...options.config };\n this.eventBus = options.eventBus ?? undefined;\n this.emitEvents = options.emitEvents ?? options.eventBus !== undefined;\n logger.info('WeightedVoting initialized', {\n config: this.config,\n eventsEnabled: this.emitEvents,\n });\n }\n\n calculateWeight(agentId: string): number {\n const record = this.records.get(agentId);\n if (record === undefined) return 0;\n return record.weight;\n }\n\n updatePerformance(agentId: string, outcome: TaskOutcome): void {\n let record = this.records.get(agentId);\n if (record === undefined) {\n this.registerAgent(agentId);\n record = this.records.get(agentId);\n if (record === undefined) return;\n }\n\n const previousWeight = record.weight;\n record.totalTasks += 1;\n record.lastActive = new Date(getTimeProvider().now());\n\n // Update task counts\n if (outcome === 'success') record.successfulTasks += 1;\n else if (outcome === 'failure') record.failedTasks += 1;\n else if (outcome === 'partial') record.partialTasks += 1;\n\n // Apply weight change\n record.weight = applyOutcomeWeight(\n record.weight,\n outcome,\n this.config.weightDecayFactor,\n this.config.weightRecoveryFactor\n );\n updateDerivedMetrics(record);\n this.emitWeightChange(agentId, previousWeight, record.weight, 'performance_update');\n\n logger.debug('Performance updated', {\n agentId,\n outcome,\n newWeight: record.weight,\n successRate: record.successRate,\n });\n }\n\n weightedConsensus(votes: ReadonlyMap<string, Vote>): WeightedConsensusResult {\n const { approval, rejection, total, agents, breakdown } = this.countVotes(votes);\n const byzantineDetected = this.detectByzantinePatterns(votes, breakdown);\n const quorumReached = total >= this.config.quorumThreshold;\n const decision = determineDecision(\n approval,\n rejection,\n total,\n quorumReached,\n this.config.quorumThreshold\n );\n\n const result: WeightedConsensusResult = {\n decision,\n weightedApproval: approval,\n weightedRejection: rejection,\n totalWeight: total,\n quorumReached,\n byzantineDetected,\n participatingAgents: agents,\n weightBreakdown: breakdown,\n };\n\n this.logConsensusResult(result);\n return result;\n }\n\n registerAgent(agentId: string): void {\n if (this.records.has(agentId)) {\n logger.debug('Agent already registered', { agentId });\n return;\n }\n const record = createAgentRecord(agentId, this.config.initialWeight);\n this.records.set(agentId, record);\n logger.info('Agent registered', { agentId, initialWeight: this.config.initialWeight });\n }\n\n getAgentRecord(agentId: string): WeightedAgentRecord | undefined {\n const record = this.records.get(agentId);\n if (record === undefined) return undefined;\n return toImmutableRecord(record);\n }\n\n flagByzantine(agentId: string, reason: string): void {\n const record = this.records.get(agentId);\n if (record === undefined) {\n logger.warn('Cannot flag unregistered agent', { agentId });\n return;\n }\n\n const previousWeight = record.weight;\n record.byzantineFlags += 1;\n record.byzantineReasons.push(reason);\n record.weight = Math.max(0, record.weight * 0.5);\n updateDerivedMetrics(record);\n\n const canStillVote = this.canVote(agentId);\n this.emitAgentFlaggedEvent(agentId, reason, previousWeight, canStillVote);\n this.emitWeightChange(agentId, previousWeight, record.weight, 'flag_penalty');\n\n logger.warn('Agent flagged for Byzantine behavior', {\n agentId,\n reason,\n totalFlags: record.byzantineFlags,\n newWeight: record.weight,\n });\n\n if (record.byzantineFlags >= this.config.byzantineFlagThreshold) {\n this.excludeAgent(record);\n }\n }\n\n getAllRecords(): readonly WeightedAgentRecord[] {\n return Array.from(this.records.values()).map((r) => toImmutableRecord(r));\n }\n\n canVote(agentId: string): boolean {\n const record = this.records.get(agentId);\n if (record === undefined) return false;\n return record.weight >= this.config.minWeight && record.trustScore >= this.config.minTrustScore;\n }\n\n recalibrateWeights(): void {\n const { globalSuccessRate, totalTasks } = computeGlobalStats(this.records.values());\n if (totalTasks === 0) return;\n\n for (const record of this.records.values()) {\n if (record.totalTasks < 3) continue;\n const previousWeight = record.weight;\n record.weight = calculateCalibratedWeight(\n record,\n globalSuccessRate,\n this.config.initialWeight\n );\n updateDerivedMetrics(record);\n this.emitWeightChange(record.agentId, previousWeight, record.weight, 'recalibration');\n }\n\n logger.info('Weights recalibrated', {\n agentCount: this.records.size,\n globalSuccessRate: globalSuccessRate.toFixed(3),\n });\n }\n\n // Private helpers\n\n private countVotes(votes: ReadonlyMap<string, Vote>): {\n approval: number;\n rejection: number;\n total: number;\n agents: string[];\n breakdown: Map<string, number>;\n } {\n let approval = 0;\n let rejection = 0;\n let total = 0;\n const agents: string[] = [];\n const breakdown = new Map<string, number>();\n\n for (const [agentId, vote] of votes) {\n if (!this.canVote(agentId)) {\n logger.warn('Agent cannot vote', { agentId, reason: 'insufficient weight or trust' });\n continue;\n }\n const weight = this.calculateWeight(agentId);\n breakdown.set(agentId, weight);\n agents.push(agentId);\n total += weight;\n const effectiveWeight = weight * vote.confidence;\n if (vote.decision === 'approve') approval += effectiveWeight;\n else if (vote.decision === 'reject') rejection += effectiveWeight;\n }\n return { approval, rejection, total, agents, breakdown };\n }\n\n private logConsensusResult(result: WeightedConsensusResult): void {\n logger.info('Weighted consensus calculated', {\n decision: result.decision,\n approval: result.weightedApproval.toFixed(3),\n rejection: result.weightedRejection.toFixed(3),\n totalWeight: result.totalWeight.toFixed(3),\n quorumReached: result.quorumReached,\n byzantineDetected: result.byzantineDetected,\n });\n }\n\n private emitWeightChange(\n agentId: string,\n prev: number,\n next: number,\n reason: 'performance_update' | 'flag_penalty' | 'recalibration'\n ): void {\n if (this.emitEvents && this.eventBus !== undefined && prev !== next) {\n emitWeightUpdated(this.eventBus, { agentId, previousWeight: prev, newWeight: next, reason });\n }\n }\n\n private emitAgentFlaggedEvent(\n agentId: string,\n reason: string,\n previousWeight: number,\n canVote: boolean\n ): void {\n if (this.emitEvents && this.eventBus !== undefined) {\n emitAgentFlagged(this.eventBus, { agentId, reason, previousWeight, canVote });\n }\n }\n\n private excludeAgent(record: MutableAgentRecord): void {\n const weightBefore = record.weight;\n record.trustScore = 0;\n record.weight = 0;\n this.emitWeightChange(record.agentId, weightBefore, 0, 'flag_penalty');\n logger.warn('Agent excluded from voting due to Byzantine behavior', {\n agentId: record.agentId,\n flags: record.byzantineFlags,\n });\n }\n\n private detectByzantinePatterns(\n votes: ReadonlyMap<string, Vote>,\n weights: Map<string, number>\n ): boolean {\n const voteArray = Array.from(votes.entries());\n if (voteArray.length < 3) return false;\n const majorityApprove = computeMajorityDirection(voteArray, weights);\n if (this.detectContrarianByzantine(voteArray, majorityApprove)) return true;\n return this.detectCollusionPattern(voteArray);\n }\n\n private detectContrarianByzantine(\n voteArray: Array<[string, Vote]>,\n majorityApprove: boolean\n ): boolean {\n const contrarianAgents: string[] = [];\n for (const [agentId, vote] of voteArray) {\n if (!isLowConfidenceContrarian(vote, majorityApprove)) continue;\n const record = this.records.get(agentId);\n if (record !== undefined && record.byzantineFlags >= 2) {\n contrarianAgents.push(agentId);\n }\n }\n if (contrarianAgents.length > 0) {\n this.emitContrarianPattern(contrarianAgents);\n return true;\n }\n return false;\n }\n\n private emitContrarianPattern(agents: string[]): void {\n if (this.emitEvents && this.eventBus !== undefined) {\n emitPatternDetected(this.eventBus, {\n patternType: 'contrarian',\n agentIds: agents,\n confidence: 0.8,\n details: `${String(agents.length)} agent(s) with Byzantine flags voting contrary to majority with low confidence`,\n });\n }\n }\n\n private detectCollusionPattern(voteArray: Array<[string, Vote]>): boolean {\n const voteSignatures = groupVotesBySignature(voteArray);\n const threshold = voteArray.length * 0.6;\n for (const [signature, agents] of voteSignatures.entries()) {\n if (agents.length >= 3 && agents.length > threshold) {\n this.emitCollusionEvents(agents, signature, voteArray.length);\n return true;\n }\n }\n return false;\n }\n\n private emitCollusionEvents(agents: string[], signature: string, totalVotes: number): void {\n if (this.emitEvents && this.eventBus !== undefined) {\n emitPatternDetected(this.eventBus, {\n patternType: 'collusion',\n agentIds: agents,\n confidence: Math.min(0.95, agents.length / totalVotes),\n details: `${String(agents.length)} agents voting identically: ${signature}`,\n });\n emitCollusionSuspected(this.eventBus, {\n groupAgentIds: agents,\n groupSize: agents.length,\n votingBlock: agents.length / totalVotes,\n threshold: 0.6,\n });\n }\n }\n}\n\n/** Create a weighted voting instance. */\nexport function createWeightedVoting(options?: WeightedVotingOptions): IWeightedVoting {\n return new WeightedVoting(options);\n}\n","/**\n * nexus-agents/consensus - Correlation Helpers\n *\n * Helper functions for correlation tracking and independent subset partitioning.\n * Extracted from correlation-tracker.ts to maintain file size limits.\n *\n * @module consensus/correlation-helpers\n * (Source: Issue #339)\n */\n\nimport type {\n CorrelationMatrix,\n CorrelationCoefficient,\n IndependentSubset,\n VotingObservation,\n HigherOrderVotingConfig,\n AgentPairKey,\n} from './higher-order-types.js';\nimport { createAgentPairKey } from './higher-order-types.js';\n\n// ============================================================================\n// Pairwise History Types\n// ============================================================================\n\n/**\n * Internal mutable representation of pairwise history.\n */\nexport interface MutablePairwiseHistory {\n pairKey: AgentPairKey;\n jointObservations: number;\n agreements: number;\n disagreements: number;\n correlation: CorrelationCoefficient;\n lastUpdated: Date;\n}\n\n// ============================================================================\n// Vote Agreement\n// ============================================================================\n\n/**\n * Check if two observations are comparable (neither is an abstain).\n * Abstain votes should be excluded from correlation tracking.\n *\n * @param obsA - First observation\n * @param obsB - Second observation\n * @returns True if both observations have non-abstain decisions\n */\nexport function isComparable(obsA: VotingObservation, obsB: VotingObservation): boolean {\n return obsA.decision !== 'abstain' && obsB.decision !== 'abstain';\n}\n\n/**\n * Check if two votes agree.\n * Only call this after verifying isComparable() returns true.\n * Abstains are treated as neutral - neither agree nor disagree.\n *\n * @param obsA - First observation\n * @param obsB - Second observation\n * @returns True if votes agree, null if either is an abstain (neutral)\n */\nexport function votesAgree(obsA: VotingObservation, obsB: VotingObservation): boolean | null {\n if (obsA.decision === 'abstain' || obsB.decision === 'abstain') {\n return null; // Neutral — skip this pair (Issue #763)\n }\n return obsA.decision === obsB.decision;\n}\n\n/**\n * Check if a decision aligned with the outcome.\n *\n * @param decision - The voting decision\n * @param outcome - The final outcome\n * @returns True if decision aligned with outcome\n */\nexport function didAlignWithOutcome(decision: string, outcome: 'approved' | 'rejected'): boolean {\n if (decision === 'abstain') return true;\n return (\n (decision === 'approve' && outcome === 'approved') ||\n (decision === 'reject' && outcome === 'rejected')\n );\n}\n\n// ============================================================================\n// Correlation Computation\n// ============================================================================\n\n/**\n * Compute correlation coefficient from pairwise history.\n *\n * Simple correlation: (agreements - disagreements) / total\n * Range: -1 (always disagree) to +1 (always agree)\n *\n * @param history - Pairwise history\n * @returns Correlation coefficient\n */\nexport function computeCorrelationCoefficient(\n history: MutablePairwiseHistory\n): CorrelationCoefficient {\n const n = history.jointObservations;\n if (n === 0) return 0;\n\n // Map agreement/disagreement rates to [-1, 1] correlation\n // 0.5 agreement rate = 0 correlation (random)\n // 1.0 agreement rate = +1 correlation (perfect agreement)\n // 0.0 agreement rate = -1 correlation (perfect disagreement)\n const agreementRate = history.agreements / n;\n const disagreementRate = history.disagreements / n;\n\n return agreementRate - disagreementRate;\n}\n\n// ============================================================================\n// Independent Subset Partitioning\n// ============================================================================\n\n/**\n * Check if an agent is independent from all members of a subset.\n *\n * @param agent - Agent to check\n * @param subset - Existing subset members\n * @param correlationMatrix - Correlation matrix\n * @param independenceThreshold - Maximum correlation for independence\n * @returns True if agent is independent from subset\n */\nexport function isIndependentFromSubset(\n agent: string,\n subset: readonly string[],\n correlationMatrix: CorrelationMatrix,\n independenceThreshold: number\n): boolean {\n for (const member of subset) {\n const pairKey = createAgentPairKey(agent, member);\n const correlation = correlationMatrix.get(pairKey);\n\n // If we don't have data, assume independent\n if (correlation === undefined) continue;\n\n // If correlation exceeds threshold, not independent\n if (Math.abs(correlation) > independenceThreshold) {\n return false;\n }\n }\n return true;\n}\n\n/**\n * Compute the average absolute correlation within a subset.\n *\n * @param subset - Agent IDs in the subset\n * @param correlationMatrix - Correlation matrix\n * @returns Average absolute correlation (0 if no pairs)\n */\nexport function computeSubsetIndependenceScore(\n subset: readonly string[],\n correlationMatrix: CorrelationMatrix\n): number {\n if (subset.length < 2) return 0;\n\n let totalCorrelation = 0;\n let pairs = 0;\n\n for (let i = 0; i < subset.length; i++) {\n for (let j = i + 1; j < subset.length; j++) {\n const agentA = subset[i];\n const agentB = subset[j];\n if (agentA !== undefined && agentB !== undefined) {\n const pairKey = createAgentPairKey(agentA, agentB);\n const correlation = correlationMatrix.get(pairKey);\n if (correlation !== undefined) {\n totalCorrelation += Math.abs(correlation);\n pairs++;\n }\n }\n }\n }\n\n return pairs > 0 ? totalCorrelation / pairs : 0;\n}\n\n/**\n * Compute the minimum observation count for pairs within a subset.\n *\n * @param subset - Agent IDs in the subset\n * @param pairwiseHistory - Map of pairwise histories\n * @returns Minimum observation count (0 if no history)\n */\nexport function computeSubsetObservationCount(\n subset: readonly string[],\n pairwiseHistory: ReadonlyMap<AgentPairKey, MutablePairwiseHistory>\n): number {\n let minObservations = Infinity;\n\n for (let i = 0; i < subset.length; i++) {\n for (let j = i + 1; j < subset.length; j++) {\n const agentA = subset[i];\n const agentB = subset[j];\n if (agentA !== undefined && agentB !== undefined) {\n const pairKey = createAgentPairKey(agentA, agentB);\n const history = pairwiseHistory.get(pairKey);\n if (history !== undefined) {\n minObservations = Math.min(minObservations, history.jointObservations);\n } else {\n minObservations = 0;\n }\n }\n }\n }\n\n return minObservations === Infinity ? 0 : minObservations;\n}\n\n/**\n * Partition agents into independent groups using greedy clustering.\n *\n * @param agents - List of agent IDs\n * @param correlationMatrix - Correlation matrix\n * @param pairwiseHistory - Map of pairwise histories\n * @param config - Higher-order voting configuration\n * @returns Array of independent subsets\n */\nexport function partitionIntoIndependentGroups(\n agents: readonly string[],\n correlationMatrix: CorrelationMatrix,\n pairwiseHistory: ReadonlyMap<AgentPairKey, MutablePairwiseHistory>,\n config: HigherOrderVotingConfig\n): IndependentSubset[] {\n if (agents.length === 0) return [];\n\n const subsets: IndependentSubset[] = [];\n const assigned = new Set<string>();\n let subsetId = 0;\n\n for (const agent of agents) {\n if (assigned.has(agent)) continue;\n\n const subset: string[] = [agent];\n assigned.add(agent);\n\n // Try to add other unassigned agents if they're independent\n for (const other of agents) {\n if (assigned.has(other)) continue;\n if (isIndependentFromSubset(other, subset, correlationMatrix, config.independenceThreshold)) {\n subset.push(other);\n assigned.add(other);\n }\n }\n\n const independenceScore = computeSubsetIndependenceScore(subset, correlationMatrix);\n const observationCount = computeSubsetObservationCount(subset, pairwiseHistory);\n\n subsets.push({\n id: `subset-${String(subsetId++)}`,\n agentIds: subset,\n independenceScore,\n observationCount,\n });\n }\n\n return subsets;\n}\n","/**\n * nexus-agents/consensus - Correlation Tracker\n *\n * Tracks voting history and computes pairwise correlations between agents.\n * Used by higher-order voting methods to account for agent dependencies.\n *\n * @module consensus/correlation-tracker\n * (Source: Issue #333)\n */\n\nimport { createLogger } from '../core/logger.js';\nimport { getTimeProvider, getRandomProvider } from '../core/index.js';\nimport type { Vote } from './types-core.js';\nimport type {\n ICorrelationTracker,\n CorrelationMatrix,\n CorrelationCoefficient,\n IndependentSubset,\n VotingObservation,\n HigherOrderVotingConfig,\n CorrelationTrackerStats,\n AgentPairKey,\n} from './higher-order-types.js';\nimport { createAgentPairKey, DEFAULT_HIGHER_ORDER_CONFIG } from './higher-order-types.js';\nimport {\n type MutablePairwiseHistory,\n isComparable,\n votesAgree,\n didAlignWithOutcome,\n computeCorrelationCoefficient,\n partitionIntoIndependentGroups,\n} from './correlation-helpers.js';\n\n// Re-export helper types and functions for convenience\nexport type { MutablePairwiseHistory } from './correlation-helpers.js';\nexport {\n isComparable,\n votesAgree,\n didAlignWithOutcome,\n computeCorrelationCoefficient,\n isIndependentFromSubset,\n computeSubsetIndependenceScore,\n computeSubsetObservationCount,\n partitionIntoIndependentGroups,\n} from './correlation-helpers.js';\n\nconst logger = createLogger({ component: 'correlation-tracker' });\n\n/**\n * Correlation tracker implementation.\n * Records voting history and computes pairwise agent correlations.\n *\n * Memory bounded: uses FIFO eviction when maxObservationsPerAgent or maxProposals limits reached.\n */\nexport class CorrelationTracker implements ICorrelationTracker {\n private readonly config: HigherOrderVotingConfig;\n private readonly observations: Map<string, VotingObservation[]> = new Map();\n private readonly pairwiseHistory: Map<AgentPairKey, MutablePairwiseHistory> = new Map();\n private readonly agentProposals: Map<string, Map<string, VotingObservation>> = new Map();\n /** Ordered list of proposal IDs for FIFO eviction */\n private readonly proposalOrder: string[] = [];\n private cachedSubsets: IndependentSubset[] | null = null;\n\n constructor(config?: Partial<HigherOrderVotingConfig>) {\n this.config = { ...DEFAULT_HIGHER_ORDER_CONFIG, ...config };\n logger.info('CorrelationTracker initialized', {\n maxObservationsPerAgent: this.config.maxObservationsPerAgent,\n maxProposals: this.config.maxProposals,\n });\n }\n\n recordVote(agentId: string, vote: Vote, outcome: 'approved' | 'rejected'): void {\n const proposalId = `proposal-${String(getTimeProvider().now())}-${getRandomProvider().random().toString(36).slice(2, 9)}`;\n const observation: VotingObservation = {\n proposalId,\n agentId,\n decision: vote.decision,\n confidence: vote.confidence,\n alignedWithOutcome: didAlignWithOutcome(vote.decision, outcome),\n timestamp: new Date(getTimeProvider().now()),\n };\n this.storeObservation(agentId, observation);\n this.invalidateCache();\n }\n\n recordProposalVotes(\n proposalId: string,\n votes: ReadonlyMap<string, Vote>,\n outcome: 'approved' | 'rejected'\n ): void {\n // FIFO eviction when proposal limit reached (Issue #521)\n this.evictOldProposalsIfNeeded();\n\n const proposalObservations: VotingObservation[] = [];\n\n for (const [agentId, vote] of votes) {\n const observation: VotingObservation = {\n proposalId,\n agentId,\n decision: vote.decision,\n confidence: vote.confidence,\n alignedWithOutcome: didAlignWithOutcome(vote.decision, outcome),\n timestamp: new Date(getTimeProvider().now()),\n };\n this.storeObservation(agentId, observation);\n this.storeAgentProposal(agentId, proposalId, observation);\n proposalObservations.push(observation);\n }\n\n // Track proposal order for FIFO eviction\n this.proposalOrder.push(proposalId);\n\n this.updatePairwiseCorrelations(proposalId, proposalObservations);\n this.invalidateCache();\n\n logger.debug('Recorded proposal votes', {\n proposalId,\n agentCount: votes.size,\n outcome,\n totalProposals: this.proposalOrder.length,\n });\n }\n\n computeCorrelationMatrix(): CorrelationMatrix {\n const matrix: CorrelationMatrix = new Map();\n\n for (const [pairKey, history] of this.pairwiseHistory) {\n if (history.jointObservations >= this.config.minObservationsForCorrelation) {\n matrix.set(pairKey, history.correlation);\n }\n }\n\n return matrix;\n }\n\n getCorrelation(agentA: string, agentB: string): CorrelationCoefficient | undefined {\n const pairKey = createAgentPairKey(agentA, agentB);\n const history = this.pairwiseHistory.get(pairKey);\n\n if (history === undefined) return undefined;\n if (history.jointObservations < this.config.minObservationsForCorrelation) return undefined;\n\n return history.correlation;\n }\n\n identifyIndependentSubsets(): readonly IndependentSubset[] {\n if (this.cachedSubsets !== null) return this.cachedSubsets;\n\n const agents = this.getTrackedAgents();\n if (agents.length === 0) {\n this.cachedSubsets = [];\n return this.cachedSubsets;\n }\n\n const correlationMatrix = this.computeCorrelationMatrix();\n const subsets = partitionIntoIndependentGroups(\n agents,\n correlationMatrix,\n this.pairwiseHistory,\n this.config\n );\n\n this.cachedSubsets = subsets;\n logger.debug('Identified independent subsets', {\n agentCount: agents.length,\n subsetCount: subsets.length,\n });\n\n return subsets;\n }\n\n hasSufficientData(agentIds: readonly string[]): boolean {\n if (agentIds.length < 2) return true;\n\n let pairsWithData = 0;\n const totalPairs = (agentIds.length * (agentIds.length - 1)) / 2;\n\n for (let i = 0; i < agentIds.length; i++) {\n for (let j = i + 1; j < agentIds.length; j++) {\n const agentA = agentIds[i];\n const agentB = agentIds[j];\n if (agentA !== undefined && agentB !== undefined) {\n const pairKey = createAgentPairKey(agentA, agentB);\n const history = this.pairwiseHistory.get(pairKey);\n if (\n history !== undefined &&\n history.jointObservations >= this.config.minObservationsForCorrelation\n ) {\n pairsWithData++;\n }\n }\n }\n }\n\n // Require at least 50% of pairs to have sufficient data\n return pairsWithData >= totalPairs * 0.5;\n }\n\n getStats(): CorrelationTrackerStats {\n const agents = this.getTrackedAgents();\n // Used by callers to understand full correlation state\n void this.computeCorrelationMatrix();\n const subsets = this.identifyIndependentSubsets();\n\n let totalCorrelation = 0;\n let pairsWithSufficientData = 0;\n\n for (const [, history] of this.pairwiseHistory) {\n if (history.jointObservations >= this.config.minObservationsForCorrelation) {\n totalCorrelation += history.correlation;\n pairsWithSufficientData++;\n }\n }\n\n let totalObservations = 0;\n for (const obs of this.observations.values()) {\n totalObservations += obs.length;\n }\n\n return {\n totalAgents: agents.length,\n trackedPairs: this.pairwiseHistory.size,\n totalObservations,\n averageCorrelation:\n pairsWithSufficientData > 0 ? totalCorrelation / pairsWithSufficientData : 0,\n independentSubsetCount: subsets.length,\n pairsWithSufficientData,\n };\n }\n\n clear(): void {\n this.observations.clear();\n this.pairwiseHistory.clear();\n this.agentProposals.clear();\n this.proposalOrder.length = 0;\n this.cachedSubsets = null;\n logger.info('CorrelationTracker cleared');\n }\n\n // ============================================================================\n // Private helpers\n // ============================================================================\n\n /**\n * Evict oldest proposals when maxProposals limit is reached.\n * Also cleans up agentProposals entries for evicted proposals.\n */\n private evictOldProposalsIfNeeded(): void {\n while (this.proposalOrder.length >= this.config.maxProposals) {\n const evictedProposalId = this.proposalOrder.shift();\n if (evictedProposalId === undefined) break;\n\n // Clean up agentProposals for the evicted proposal\n for (const [agentId, proposalMap] of this.agentProposals) {\n if (proposalMap.has(evictedProposalId)) {\n proposalMap.delete(evictedProposalId);\n // Clean up empty agent entries\n if (proposalMap.size === 0) {\n this.agentProposals.delete(agentId);\n }\n }\n }\n\n logger.debug('Evicted oldest proposal', {\n evictedProposalId,\n reason: 'maxProposals',\n remainingProposals: this.proposalOrder.length,\n });\n }\n }\n\n /**\n * Evict the oldest pairwise history entry (by lastUpdated) when\n * maxTrackedPairs limit is exceeded.\n */\n private evictOldestPair(): void {\n if (this.pairwiseHistory.size <= this.config.maxTrackedPairs) return;\n\n let oldestKey: AgentPairKey | undefined;\n let oldestTime = Infinity;\n for (const [key, history] of this.pairwiseHistory) {\n const time = history.lastUpdated.getTime();\n if (time < oldestTime) {\n oldestTime = time;\n oldestKey = key;\n }\n }\n if (oldestKey !== undefined) {\n this.pairwiseHistory.delete(oldestKey);\n logger.debug('Evicted oldest pairwise history entry', {\n evictedKey: oldestKey,\n reason: 'maxTrackedPairs',\n remainingPairs: this.pairwiseHistory.size,\n });\n }\n }\n\n private storeObservation(agentId: string, observation: VotingObservation): void {\n let agentObs = this.observations.get(agentId);\n if (agentObs === undefined) {\n agentObs = [];\n this.observations.set(agentId, agentObs);\n }\n\n // FIFO eviction when per-agent limit reached (Issue #521)\n while (agentObs.length >= this.config.maxObservationsPerAgent) {\n const evicted = agentObs.shift();\n if (evicted !== undefined) {\n logger.debug('Evicted oldest observation for agent', {\n agentId,\n evictedProposalId: evicted.proposalId,\n reason: 'maxObservationsPerAgent',\n });\n }\n }\n\n agentObs.push(observation);\n }\n\n private storeAgentProposal(\n agentId: string,\n proposalId: string,\n observation: VotingObservation\n ): void {\n let agentProposalMap = this.agentProposals.get(agentId);\n if (agentProposalMap === undefined) {\n agentProposalMap = new Map();\n this.agentProposals.set(agentId, agentProposalMap);\n }\n agentProposalMap.set(proposalId, observation);\n }\n\n private updatePairwiseCorrelations(proposalId: string, observations: VotingObservation[]): void {\n for (let i = 0; i < observations.length; i++) {\n for (let j = i + 1; j < observations.length; j++) {\n const obsA = observations[i];\n const obsB = observations[j];\n if (obsA === undefined || obsB === undefined) continue;\n\n const pairKey = createAgentPairKey(obsA.agentId, obsB.agentId);\n let history = this.pairwiseHistory.get(pairKey);\n\n if (history === undefined) {\n history = {\n pairKey,\n jointObservations: 0,\n agreements: 0,\n disagreements: 0,\n correlation: 0,\n lastUpdated: new Date(getTimeProvider().now()),\n };\n this.pairwiseHistory.set(pairKey, history);\n this.evictOldestPair();\n }\n\n // Skip abstain observations — they are neutral (Issue #763)\n if (!isComparable(obsA, obsB)) continue;\n\n history.jointObservations++;\n const agreed = votesAgree(obsA, obsB);\n if (agreed === true) {\n history.agreements++;\n } else {\n history.disagreements++;\n }\n\n history.correlation = computeCorrelationCoefficient(history);\n history.lastUpdated = new Date(getTimeProvider().now());\n }\n }\n }\n\n private getTrackedAgents(): string[] {\n return Array.from(this.observations.keys());\n }\n\n private invalidateCache(): void {\n this.cachedSubsets = null;\n }\n}\n\n/**\n * Creates a new correlation tracker instance.\n */\nexport function createCorrelationTracker(\n config?: Partial<HigherOrderVotingConfig>\n): ICorrelationTracker {\n return new CorrelationTracker(config);\n}\n","/**\n * nexus-agents/consensus - Correlation Persistence\n *\n * Disk persistence for CorrelationTracker voting history. Enables Higher-Order\n * Voting (HOV) to accumulate correlation data across process restarts,\n * activating Bayesian correlation awareness.\n *\n * Data: ~/.nexus-agents/voting/correlations.json (mode 0o600)\n *\n * @module consensus/correlation-persistence\n * (Source: Issue #514)\n */\n\nimport * as fs from 'node:fs';\nimport { nexusDataPath } from '../config/nexus-data-dir.js';\nimport { z } from 'zod';\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport type { ILogger } from '../core/logger.js';\nimport { createLogger } from '../core/logger.js';\nimport type { ICorrelationTracker, HigherOrderVotingConfig } from './higher-order-types.js';\nimport { DEFAULT_HIGHER_ORDER_CONFIG } from './higher-order-types.js';\nimport type { Vote } from './types-core.js';\nimport { createCorrelationTracker } from './correlation-tracker.js';\n\nconst logger: ILogger = createLogger({ component: 'correlation-persistence' });\n\n/** Subdirectory name under the resolved nexus data dir for voting data. */\nconst VOTING_SUBDIR = 'voting';\n\n/** Filename for persisted correlation data */\nconst CORRELATIONS_FILE = 'correlations.json';\n\n/** File permissions: user read/write only */\nconst FILE_MODE = 0o600;\n\n/** Directory permissions: user read/write/execute only */\nconst DIR_MODE = 0o700;\n\n/** Schema version for forward compatibility */\nconst SCHEMA_VERSION = 1;\n\n// ============================================================================\n// Persisted Data Types\n// ============================================================================\n\n/**\n * A single persisted vote within a proposal.\n */\nconst PersistedVoteSchema = z.object({\n agentId: z.string(),\n decision: z.enum(['approve', 'reject', 'abstain']),\n confidence: z.number().min(0).max(1),\n});\n\n/** Type for a persisted vote entry */\ntype PersistedVote = z.infer<typeof PersistedVoteSchema>;\n\n/**\n * A persisted proposal with its votes and outcome.\n * Stored as a replayable record so internal tracker state\n * is reconstructed through the public API.\n */\nconst PersistedProposalSchema = z.object({\n proposalId: z.string(),\n votes: z.array(PersistedVoteSchema),\n outcome: z.enum(['approved', 'rejected']),\n timestamp: z.iso.datetime(),\n});\n\n/** Type for a persisted proposal entry */\ntype PersistedProposal = z.infer<typeof PersistedProposalSchema>;\n\n/**\n * Top-level persisted correlation data structure.\n * Contains schema version for forward compatibility\n * and an array of proposals that can be replayed.\n */\nexport const PersistedCorrelationDataSchema = z.object({\n version: z.number().int().positive(),\n proposals: z.array(PersistedProposalSchema),\n savedAt: z.iso.datetime(),\n});\n\n/** Validated persisted correlation data */\nexport type PersistedCorrelationData = z.infer<typeof PersistedCorrelationDataSchema>;\n\n// ============================================================================\n// Path Helpers\n// ============================================================================\n\n/**\n * Returns the absolute path to the correlation data file.\n * Resolves under `$NEXUS_DATA_DIR/voting/correlations.json` (#2302).\n */\nexport function getCorrelationDataPath(): string {\n return nexusDataPath(VOTING_SUBDIR, CORRELATIONS_FILE);\n}\n\n/**\n * Ensures the voting data directory exists with appropriate permissions.\n */\nfunction ensureVotingDirectory(): Result<void, Error> {\n const dirPath = nexusDataPath(VOTING_SUBDIR);\n try {\n fs.mkdirSync(dirPath, { recursive: true, mode: DIR_MODE });\n return ok(undefined);\n } catch (cause: unknown) {\n const error = cause instanceof Error ? cause : new Error(String(cause));\n return err(new Error(`Failed to create voting directory at ${dirPath}: ${error.message}`));\n }\n}\n\n// ============================================================================\n// Save\n// ============================================================================\n\n/**\n * Merges existing and new proposals, deduplicating by proposalId.\n * Applies FIFO eviction when the merged set exceeds maxProposals.\n */\nfunction mergeProposals(\n existingProposals: PersistedProposal[],\n newProposals: PersistedProposal[],\n maxProposals: number\n): PersistedProposal[] {\n // Deduplicate by proposalId, preferring new entries\n const proposalMap = new Map<string, PersistedProposal>();\n for (const proposal of existingProposals) {\n proposalMap.set(proposal.proposalId, proposal);\n }\n for (const proposal of newProposals) {\n proposalMap.set(proposal.proposalId, proposal);\n }\n\n const merged = Array.from(proposalMap.values());\n\n // Sort by timestamp ascending for consistent replay order\n merged.sort((a, b) => a.timestamp.localeCompare(b.timestamp));\n\n // FIFO eviction to respect maxProposals\n if (merged.length > maxProposals) {\n return merged.slice(merged.length - maxProposals);\n }\n\n return merged;\n}\n\n/**\n * Saves correlation data to disk.\n *\n * Performs an atomic write (write to temp file, then rename) to prevent\n * corruption from interrupted writes. File permissions are set to 0o600.\n *\n * @param proposals - Array of proposals with their votes and outcomes to persist\n * @param config - Higher-order voting config (used for maxProposals eviction)\n * @returns Result indicating success or failure\n */\nexport function saveCorrelationData(\n proposals: PersistedProposal[],\n config: HigherOrderVotingConfig = DEFAULT_HIGHER_ORDER_CONFIG\n): Result<void, Error> {\n const dirResult = ensureVotingDirectory();\n if (!dirResult.ok) return dirResult;\n\n const filePath = getCorrelationDataPath();\n\n // Load existing proposals to merge\n let existingProposals: PersistedProposal[] = [];\n const loadResult = loadCorrelationData();\n if (loadResult.ok) {\n existingProposals = loadResult.value.proposals;\n }\n\n const merged = mergeProposals(existingProposals, proposals, config.maxProposals);\n\n const data: PersistedCorrelationData = {\n version: SCHEMA_VERSION,\n proposals: merged,\n savedAt: new Date().toISOString(),\n };\n\n const json = JSON.stringify(data, null, 2);\n const tempPath = `${filePath}.tmp.${String(process.pid)}`;\n\n try {\n // Atomic write: write to temp, then rename\n fs.writeFileSync(tempPath, json, { encoding: 'utf-8', mode: FILE_MODE });\n fs.renameSync(tempPath, filePath);\n\n logger.info('Correlation data saved', {\n path: filePath,\n proposalCount: merged.length,\n });\n\n return ok(undefined);\n } catch (cause: unknown) {\n // Clean up temp file on failure\n try {\n fs.unlinkSync(tempPath);\n } catch (cleanupErr: unknown) {\n const msg = cleanupErr instanceof Error ? cleanupErr.message : String(cleanupErr);\n logger.debug('Failed to clean up temp file during correlation save', {\n path: tempPath,\n error: msg,\n });\n }\n\n const error = cause instanceof Error ? cause : new Error(String(cause));\n return err(new Error(`Failed to save correlation data: ${error.message}`));\n }\n}\n\n// ============================================================================\n// Load\n// ============================================================================\n\n/** Read and parse JSON from disk. Returns parsed data or error. */\nfunction readAndParseFile(filePath: string): Result<unknown, Error> {\n if (!fs.existsSync(filePath)) {\n return err(new Error(`Correlation data file not found: ${filePath}`));\n }\n\n let rawContent: string;\n try {\n rawContent = fs.readFileSync(filePath, { encoding: 'utf-8' });\n } catch (cause: unknown) {\n const error = cause instanceof Error ? cause : new Error(String(cause));\n return err(new Error(`Failed to read correlation data: ${error.message}`));\n }\n\n try {\n return ok(JSON.parse(rawContent) as unknown);\n } catch (cause: unknown) {\n const error = cause instanceof Error ? cause : new Error(String(cause));\n logger.warn('Corrupt correlation data file, will start fresh', {\n path: filePath,\n error: error.message,\n });\n return err(new Error(`Corrupt correlation data (invalid JSON): ${error.message}`));\n }\n}\n\n/** Validate parsed data against schema and version. */\nfunction validateCorrelationData(\n parsed: unknown,\n filePath: string\n): Result<PersistedCorrelationData, Error> {\n const validation = PersistedCorrelationDataSchema.safeParse(parsed);\n if (!validation.success) {\n logger.warn('Invalid correlation data schema, will start fresh', {\n path: filePath,\n errors: validation.error.issues.map((issue) => issue.message),\n });\n return err(new Error(`Invalid correlation data schema: ${validation.error.message}`));\n }\n\n if (validation.data.version > SCHEMA_VERSION) {\n logger.warn('Correlation data from newer version, will start fresh', {\n fileVersion: validation.data.version,\n currentVersion: SCHEMA_VERSION,\n });\n return err(\n new Error(\n `Unsupported schema version ${String(validation.data.version)} (current: ${String(SCHEMA_VERSION)})`\n )\n );\n }\n\n return ok(validation.data);\n}\n\n/**\n * Loads and validates correlation data from disk.\n *\n * Handles corrupt files gracefully by logging a warning and returning\n * an error Result. Callers should start fresh on load failure.\n *\n * @returns Result containing validated data or an error\n */\nexport function loadCorrelationData(): Result<PersistedCorrelationData, Error> {\n const filePath = getCorrelationDataPath();\n\n const parseResult = readAndParseFile(filePath);\n if (!parseResult.ok) return parseResult;\n\n const validateResult = validateCorrelationData(parseResult.value, filePath);\n if (!validateResult.ok) return validateResult;\n\n logger.info('Correlation data loaded', {\n path: filePath,\n proposalCount: validateResult.value.proposals.length,\n savedAt: validateResult.value.savedAt,\n });\n\n return validateResult;\n}\n\n// ============================================================================\n// Persistent Tracker Factory\n// ============================================================================\n\n/**\n * Replays persisted proposals into a tracker via `recordProposalVotes()`,\n * reconstructing all internal state through the public API.\n */\nfunction replayProposals(\n tracker: ICorrelationTracker,\n proposals: readonly PersistedProposal[]\n): number {\n let replayed = 0;\n\n for (const proposal of proposals) {\n const votes = new Map<string, Vote>();\n\n for (const vote of proposal.votes) {\n votes.set(vote.agentId, {\n decision: vote.decision,\n reasoning: 'replayed from persistence',\n confidence: vote.confidence,\n });\n }\n\n tracker.recordProposalVotes(proposal.proposalId, votes, proposal.outcome);\n replayed++;\n }\n\n return replayed;\n}\n\n/**\n * Creates a correlation tracker pre-loaded with persisted history.\n *\n * On first run (no persisted data), returns a fresh tracker.\n * On subsequent runs, replays all stored proposals through the\n * tracker's public API to reconstruct correlation state.\n *\n * This enables Higher-Order Voting to accumulate enough history\n * across process restarts to activate Bayesian correlation awareness.\n *\n * @param config - Optional partial higher-order voting config\n * @returns A correlation tracker with any persisted history replayed\n */\nexport function createPersistentCorrelationTracker(\n config?: Partial<HigherOrderVotingConfig>\n): ICorrelationTracker {\n const tracker = createCorrelationTracker(config);\n\n const loadResult = loadCorrelationData();\n if (!loadResult.ok) {\n logger.info('Starting with fresh correlation tracker', {\n reason: loadResult.error.message,\n });\n return tracker;\n }\n\n const replayedCount = replayProposals(tracker, loadResult.value.proposals);\n\n logger.info('Correlation tracker restored from persistence', {\n replayedProposals: replayedCount,\n stats: tracker.getStats(),\n });\n\n return tracker;\n}\n\n// ============================================================================\n// Proposal Recording Helper\n// ============================================================================\n\n/**\n * Creates a persistable proposal record from vote data.\n *\n * Use this to build proposals that can be passed to `saveCorrelationData()`.\n *\n * @param proposalId - Unique proposal identifier\n * @param votes - Map of agent IDs to their votes\n * @param outcome - Final proposal outcome\n * @returns A persistable proposal record\n */\nexport function createPersistedProposal(\n proposalId: string,\n votes: ReadonlyMap<string, Vote>,\n outcome: 'approved' | 'rejected'\n): PersistedProposal {\n const persistedVotes: PersistedVote[] = [];\n\n for (const [agentId, vote] of votes) {\n persistedVotes.push({\n agentId,\n decision: vote.decision,\n confidence: vote.confidence,\n });\n }\n\n return {\n proposalId,\n votes: persistedVotes,\n outcome,\n timestamp: new Date().toISOString(),\n };\n}\n","/**\n * Types, schemas, and response helpers for the consensus_vote MCP tool.\n * Extracted from consensus-vote.ts for file size compliance (Issue #708).\n *\n * @module mcp/tools/consensus-vote-types\n */\n\nimport { z } from 'zod';\nimport type { AgentVoteResult, VotingResult } from '../../cli/vote-types.js';\nimport { VOTER_ROLES } from '../../cli/vote-types.js';\nimport type { HigherOrderVotingResult } from '../../consensus/higher-order-types.js';\n\n/** Maximum proposal length (memory bounds per Issue #435). */\nexport const MAX_PROPOSAL_LENGTH = 4000;\n\n// ============================================================================\n// Strategy Types\n// ============================================================================\n\n/**\n * Available consensus voting strategies.\n *\n * - `simple_majority`: Standard majority voting (>50%)\n * - `supermajority`: Requires >=67% approval\n * - `unanimous`: Requires 100% approval\n * - `proof_of_learning`: Weighted by agent performance (Issue #103)\n * - `higher_order`: Bayesian-optimal with correlation awareness (Issue #514)\n * - `opinion_wise`: Alias for higher_order (Issue #333)\n */\nexport type VotingStrategy =\n | 'simple_majority'\n | 'supermajority'\n | 'unanimous'\n | 'proof_of_learning'\n | 'higher_order'\n | 'opinion_wise';\n\nexport const VotingStrategySchema = z.enum([\n 'simple_majority',\n 'supermajority',\n 'unanimous',\n 'proof_of_learning',\n 'higher_order',\n 'opinion_wise',\n]);\n\n// ============================================================================\n// Input / Output Schemas\n// ============================================================================\n\nexport const ConsensusVoteInputSchema = z.object({\n proposal: z.string().min(1).max(MAX_PROPOSAL_LENGTH).describe('Proposal text to vote on'),\n threshold: z\n .enum(['majority', 'supermajority', 'unanimous'])\n .optional()\n .describe(\n 'Voting threshold (legacy): majority, supermajority, unanimous. Use strategy instead.'\n ),\n strategy: VotingStrategySchema.optional().describe(\n 'Voting strategy: simple_majority (default), supermajority, unanimous, proof_of_learning, or higher_order (Bayesian-optimal)'\n ),\n quickMode: z\n .boolean()\n .optional()\n .default(false)\n .describe('Use 3 agents instead of 5 for faster execution'),\n simulateVotes: z\n .boolean()\n .optional()\n .default(false)\n .describe(\n 'TESTS ONLY — when true, voters return random decisions. Output must not be used for real decisions. (#2319)'\n ),\n});\n\nexport type ConsensusVoteInput = z.infer<typeof ConsensusVoteInputSchema>;\n\n// ============================================================================\n// Response Types\n// ============================================================================\n\nexport interface AgentVoteSummary {\n role: string;\n decision: 'approve' | 'reject' | 'abstain';\n confidence: number;\n reasoning: string;\n simulated: boolean;\n /** True when this vote was generated from an error (Issue #815). */\n error: boolean;\n /** Model used for this agent's vote (Issue #817). */\n modelUsed?: string;\n /** Structured rejection categories for reject→refine→re-vote loops (Issue #1213). */\n rejectionCategories?: readonly string[];\n}\n\nexport type VoteDecisionStatus = 'approved' | 'rejected' | 'pending' | 'timeout' | 'no_quorum';\n\n/** Higher-Order Voting metadata (Issue #514). */\nexport interface HigherOrderMetadata {\n posteriorApproval: number;\n posteriorRejection: number;\n effectiveVoteCount: number;\n method: 'ow' | 'isp' | 'simple';\n usedCorrelationData: boolean;\n improvementOverBaseline: number;\n downweightedAgents: readonly string[];\n reasoning: string;\n}\n\nexport interface ConsensusVoteResponse {\n proposal: string;\n threshold?: 'majority' | 'supermajority' | 'unanimous';\n strategy: VotingStrategy;\n decision: VoteDecisionStatus;\n approvalPercentage: number;\n voteCounts: { approve: number; reject: number; abstain: number; error: number };\n votes: AgentVoteSummary[];\n durationMs: number;\n simulateVotes: boolean;\n higherOrderMetadata?: HigherOrderMetadata;\n}\n\n/** Extended voting result with optional Higher-Order metadata. */\nexport interface ExtendedVotingResult extends VotingResult {\n strategy: VotingStrategy;\n higherOrderResult?: HigherOrderVotingResult;\n}\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\n/** Converts AgentVoteResult to AgentVoteSummary for response. */\nexport function toAgentVoteSummary(result: AgentVoteResult): AgentVoteSummary {\n const roleName = VOTER_ROLES[result.role].split(' - ')[0] ?? result.role;\n return {\n role: roleName,\n decision: result.vote.decision,\n confidence: result.vote.confidence,\n reasoning: result.vote.reasoning,\n simulated: result.source === 'simulation',\n error: result.source === 'error',\n ...(result.vote.rejectionCategories !== undefined\n ? { rejectionCategories: result.vote.rejectionCategories }\n : {}),\n };\n}\n\n/** Maps ProposalStatus to VoteDecisionStatus. */\nexport function mapOutcomeToDecision(outcome: string): VoteDecisionStatus {\n switch (outcome) {\n case 'approved':\n return 'approved';\n case 'rejected':\n return 'rejected';\n case 'timeout':\n return 'timeout';\n default:\n return 'pending';\n }\n}\n\n/** Builds the response from voting result. */\nexport function buildResponse(\n input: ConsensusVoteInput,\n result: ExtendedVotingResult\n): ConsensusVoteResponse {\n const proposalTruncated =\n input.proposal.length > 200 ? input.proposal.slice(0, 200) + '...' : input.proposal;\n\n const errorCount = result.votes.filter((v) => v.source === 'error').length;\n\n const allErrors = errorCount === result.votes.length && errorCount > 0;\n const decision: VoteDecisionStatus =\n !result.result.quorumReached && allErrors\n ? 'no_quorum'\n : mapOutcomeToDecision(result.result.outcome);\n\n const response: ConsensusVoteResponse = {\n proposal: proposalTruncated,\n strategy: result.strategy,\n decision,\n approvalPercentage: result.result.approvalPercentage,\n voteCounts: {\n approve: result.result.voteCounts.approve,\n reject: result.result.voteCounts.reject,\n abstain: result.result.voteCounts.abstain,\n error: errorCount,\n },\n votes: result.votes.map(toAgentVoteSummary),\n durationMs: result.totalTimeMs,\n simulateVotes: result.simulateVotes,\n };\n\n if (input.threshold !== undefined) {\n response.threshold = input.threshold;\n }\n\n if (result.strategy === 'higher_order' && result.higherOrderResult) {\n response.higherOrderMetadata = {\n posteriorApproval: result.higherOrderResult.posteriorApproval,\n posteriorRejection: result.higherOrderResult.posteriorRejection,\n effectiveVoteCount: result.higherOrderResult.effectiveVoteCount,\n method: result.higherOrderResult.method,\n usedCorrelationData: result.higherOrderResult.usedCorrelationData,\n improvementOverBaseline: result.higherOrderResult.improvementOverBaseline,\n downweightedAgents: result.higherOrderResult.downweightedAgents,\n reasoning: result.higherOrderResult.reasoning,\n };\n }\n\n return response;\n}\n","/* eslint-disable max-lines -- Cohesive memory facade composing 6 backends (governance: 400-600 lines OK if cohesive) */\n/**\n * nexus-agents/mcp - Tool Memory Integration\n *\n * Unified memory facade for MCP tools. Composes SessionMemory (episodic),\n * BeliefMemory (structured knowledge), and optionally AgenticMemory\n * (Zettelkasten-style), AdaptiveMemory (priority-scored), TypedMemory\n * (MIRIX-style typed access), and MobiMem (post-deployment learning)\n * when SQLite is available. Graceful degradation when backends are absent.\n *\n * @module mcp/tools/tool-memory\n * (Source: Issue #690, #714, #746 - Unified memory facade)\n */\n\nimport * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { nexusDataPath } from '../../config/nexus-data-dir.js';\nimport type { ILogger } from '../../core/index.js';\nimport { getErrorMessage, createLogger, getTimeProvider } from '../../core/index.js';\n\nimport { SessionMemory } from '../../context/session-memory.js';\nimport type {\n SessionLearning,\n CompletedTask,\n ResolvedError,\n} from '../../context/session-memory-types.js';\nimport { HindsightBeliefMemory } from '../../context/belief-memory.js';\nimport { BeliefConfidence, BeliefSourceType } from '../../context/belief-core-types.js';\nimport type { Belief } from '../../context/belief-core-types.js';\nimport { AgenticMemoryBackend } from '../../context/agentic-memory.js';\nimport { AdaptiveMemoryBackend } from '../../context/adaptive-memory.js';\nimport type { MemoryMetadata, MemoryImportance } from '../../context/memory-backend-types.js';\nimport { saveBeliefSnapshot, loadBeliefSnapshot } from '../../context/belief-memory-persistence.js';\nimport { HybridMemoryBackend } from '../../context/memory-backend.js';\nimport { createTypedMemory } from '../../context/typed-memory.js';\nimport type {\n ITypedMemory,\n TypedMemoryEntry,\n TypedMemoryStats,\n TypedMemoryPruneResult,\n MemoryType,\n} from '../../context/memory-types.js';\nimport type { AgentRole } from '../../core/types/agent.js';\nimport { MobiMem } from '../../context/mobimem.js';\nimport type { MobiMemStats } from '../../context/mobimem-types.js';\nimport {\n MemoryPromoter,\n type PromotionStats,\n type MemoryPromotionConfig,\n} from './memory-promotion.js';\nimport {\n MemoryDecayManager,\n type DecayRunStats,\n type DecayAggregateStats,\n} from './memory-decay.js';\nimport type { UnifiedMemoryResult } from './tool-memory-types.js';\nimport {\n querySessionMemory as querySessionMemoryHelper,\n queryBeliefMemory as queryBeliefMemoryHelper,\n queryAgenticMemory as queryAgenticMemoryHelper,\n queryTypedMemory as queryTypedMemoryHelper,\n queryAdaptiveMemory as queryAdaptiveMemoryHelper,\n} from './tool-memory-query.js';\n\n// Re-export types tools may need\nexport type { SessionLearning, CompletedTask, ResolvedError, Belief };\n\n// UnifiedMemoryResult extracted to tool-memory-types.ts to avoid circular imports (#1671)\nexport type { UnifiedMemoryResult } from './tool-memory-types.js';\nexport type {\n TypedMemoryEntry,\n TypedMemoryStats,\n TypedMemoryPruneResult,\n MemoryType,\n} from '../../context/memory-types.js';\nexport type { AgentRole } from '../../core/types/agent.js';\nexport type { MobiMemStats } from '../../context/mobimem-types.js';\nexport type { PromotionStats, MemoryPromotionConfig } from './memory-promotion.js';\n\n/**\n * Status of memory backend availability (#754).\n */\nexport interface MemoryBackendStatus {\n session: boolean;\n belief: boolean;\n agentic: boolean;\n adaptive: boolean;\n typed: boolean;\n mobimem: boolean;\n decay: boolean;\n}\nexport type { DecayRunStats, DecayAggregateStats } from './memory-decay.js';\n\n// ============================================================================\n// Constants\n// ============================================================================\n\n/** Default memory directory under the resolved nexus data dir (#2302). */\nconst MEMORY_BASE = nexusDataPath('memory');\nconst DEFAULT_MEMORY_DIR = path.join(MEMORY_BASE, 'sessions');\nconst AGENTIC_DB_PATH = path.join(MEMORY_BASE, 'agentic.db');\nconst ADAPTIVE_DB_PATH = path.join(MEMORY_BASE, 'adaptive.db');\nconst TYPED_DB_PATH = path.join(MEMORY_BASE, 'typed.db');\nconst MOBIMEM_DB_PATH = path.join(MEMORY_BASE, 'mobimem.db');\nconst MARKDOWN_DIR = path.join(MEMORY_BASE, 'markdown');\n\n// ============================================================================\n// Shared Instance (Singleton per process)\n// ============================================================================\n\nlet sharedInstance: ToolMemoryManager | null = null;\n\n/**\n * Get or create the shared ToolMemoryManager singleton.\n * Automatically starts a session on first access.\n */\nexport function getToolMemory(logger?: ILogger): ToolMemoryManager {\n sharedInstance ??= new ToolMemoryManager(logger);\n return sharedInstance;\n}\n\n/**\n * Shut down the shared memory instance. Call during server cleanup.\n */\nexport function shutdownToolMemory(): void {\n if (sharedInstance !== null) {\n sharedInstance.endSession();\n sharedInstance = null;\n }\n}\n\n/**\n * Reinitialize SQLite-based memory backends that failed during startup.\n * Useful after installing better-sqlite3 to enable full memory functionality.\n * @returns Status of each backend after reinitialization\n */\nexport async function reinitializeMemoryBackends(): Promise<MemoryBackendStatus> {\n return getToolMemory().reinitializeSqliteBackends();\n}\n\n// ============================================================================\n// ToolMemoryManager\n// ============================================================================\n\n/**\n * Manages session memory for MCP tool execution.\n * Auto-initializes a session and provides safe recording methods\n * that silently degrade if memory is unavailable.\n */\nexport class ToolMemoryManager {\n private readonly memory: SessionMemory;\n private readonly beliefs: HindsightBeliefMemory;\n private readonly log: ILogger;\n private pastLearnings: readonly SessionLearning[] = [];\n private agentic: AgenticMemoryBackend | null = null;\n private adaptive: AdaptiveMemoryBackend | null = null;\n private typed: ITypedMemory | null = null;\n private typedBackend: HybridMemoryBackend | null = null;\n private mobimem: MobiMem | null = null;\n private decayManager: MemoryDecayManager | null = null;\n private initPromise: Promise<void> | null = null;\n\n constructor(logger?: ILogger) {\n this.log = logger ?? createLogger({ component: 'ToolMemory' });\n\n this.memory = new SessionMemory({\n memoryDir: DEFAULT_MEMORY_DIR,\n logger: this.log,\n });\n this.beliefs = new HindsightBeliefMemory(undefined, this.log);\n this.loadBeliefSnapshotFromDisk();\n\n // Auto-start session\n const sessionId = `mcp-${String(getTimeProvider().now())}`;\n const result = this.memory.startSession(sessionId);\n if (result.ok) {\n this.pastLearnings = result.value;\n this.log.info('Tool memory session started', {\n sessionId,\n pastLearnings: this.pastLearnings.length,\n });\n } else {\n this.log.warn('Tool memory session start failed', {\n error: result.error.message,\n });\n }\n\n // Phase 2: activate SQLite backends (best-effort, non-blocking)\n this.initPromise = this.initSqliteBackends();\n }\n\n /** Try to activate SQLite backends (best-effort, non-blocking). */\n private async initSqliteBackends(): Promise<void> {\n fs.mkdirSync(MARKDOWN_DIR, { recursive: true });\n await this.initAgenticMemory();\n await this.initAdaptiveMemory();\n await this.initTypedMemory();\n this.initMobiMem();\n this.initDecayManager();\n }\n\n /** Initialize AgenticMemory (Phase 2). */\n private async initAgenticMemory(): Promise<void> {\n try {\n const backend = new AgenticMemoryBackend({\n dbPath: AGENTIC_DB_PATH,\n markdownDir: MARKDOWN_DIR,\n });\n const result = await backend.initialize();\n if (result.ok) {\n this.agentic = backend;\n this.log.info('AgenticMemory activated (Phase 2)');\n } else {\n this.log.info('AgenticMemory unavailable', { reason: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('AgenticMemory init failed', {\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Initialize AdaptiveMemory (Phase 2). */\n private async initAdaptiveMemory(): Promise<void> {\n try {\n const backend = new AdaptiveMemoryBackend({\n dbPath: ADAPTIVE_DB_PATH,\n markdownDir: MARKDOWN_DIR,\n });\n const result = await backend.initialize();\n if (result.ok) {\n this.adaptive = backend;\n this.log.info('AdaptiveMemory activated (Phase 2)');\n } else {\n this.log.info('AdaptiveMemory unavailable', { reason: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('AdaptiveMemory init failed', {\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Initialize TypedMemory (Phase 1 #746 - MIRIX-style typed access). */\n private async initTypedMemory(): Promise<void> {\n try {\n const backend = new HybridMemoryBackend({\n dbPath: TYPED_DB_PATH,\n markdownDir: MARKDOWN_DIR,\n logger: this.log,\n });\n const result = await backend.initialize();\n if (result.ok) {\n this.typedBackend = backend;\n this.typed = createTypedMemory(backend);\n this.log.info('TypedMemory activated (Phase 1 #746)');\n } else {\n this.log.info('TypedMemory unavailable', { reason: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('TypedMemory init failed', {\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Initialize MobiMem (Phase 2 #746 - post-deployment learning). */\n private initMobiMem(): void {\n try {\n this.mobimem = new MobiMem({\n dbPath: MOBIMEM_DB_PATH,\n autoEviction: true,\n });\n this.log.info('MobiMem activated (Phase 2 #746)');\n } catch (error: unknown) {\n this.log.debug('MobiMem init failed', {\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Initialize coordinated decay manager (Phase 5 #746). */\n private initDecayManager(): void {\n try {\n this.decayManager = new MemoryDecayManager({}, this.log);\n this.decayManager.initialize({\n beliefs: this.beliefs,\n agentic: this.agentic,\n adaptive: this.adaptive,\n mobimem: this.mobimem,\n });\n // Start auto-decay for long-running sessions\n this.decayManager.startAutoDecay();\n // Run decay once at startup — the setInterval timer may never fire\n // in short-lived MCP sessions, so this ensures at least one run (#1673).\n void this.decayManager.runDecay().catch((error: unknown) => {\n this.log.debug('Startup decay run failed', {\n error: getErrorMessage(error),\n });\n });\n this.log.info('MemoryDecayManager activated (Phase 5 #746)');\n } catch (error: unknown) {\n this.log.debug('MemoryDecayManager init failed', {\n error: getErrorMessage(error),\n });\n }\n }\n\n /**\n * Re-initialize SQLite backends that failed during startup.\n * Skips already-initialized backends. Useful after installing better-sqlite3.\n * @returns Status of each backend after reinitialization attempt\n */\n async reinitializeSqliteBackends(): Promise<MemoryBackendStatus> {\n this.log.info('Reinitializing SQLite backends');\n // Wait for any in-flight initialization to complete first (#794)\n if (this.initPromise !== null) {\n await this.initPromise;\n this.initPromise = null;\n }\n if (this.agentic === null) await this.initAgenticMemory();\n if (this.adaptive === null) await this.initAdaptiveMemory();\n if (this.typed === null) await this.initTypedMemory();\n if (this.mobimem === null) this.initMobiMem();\n if (this.decayManager === null) this.initDecayManager();\n return this.getBackendStatus();\n }\n\n /** Get current backend availability status. */\n getBackendStatus(): MemoryBackendStatus {\n return {\n session: true,\n belief: true,\n agentic: this.agentic !== null,\n adaptive: this.adaptive !== null,\n typed: this.typed !== null,\n mobimem: this.mobimem !== null,\n decay: this.decayManager !== null,\n };\n }\n\n /**\n * Get learnings from previous sessions.\n */\n getPastLearnings(): readonly SessionLearning[] {\n return this.pastLearnings;\n }\n\n /**\n * Record a completed task. Safe to call even if session inactive.\n */\n recordTask(task: CompletedTask): void {\n if (!this.memory.isSessionActive()) return;\n\n const result = this.memory.recordTask(task);\n if (!result.ok) {\n this.log.debug('Failed to record task', { error: result.error.message });\n }\n }\n\n /**\n * Record a learning. Safe to call even if session inactive.\n * High-confidence learnings are also stored as beliefs for structured retrieval.\n */\n recordLearning(learning: SessionLearning): void {\n if (!this.memory.isSessionActive()) return;\n\n const result = this.memory.recordLearning(learning);\n if (!result.ok) {\n this.log.debug('Failed to record learning', { error: result.error.message });\n }\n\n // Auto-create belief for high-confidence learnings (cross-backend sync)\n if (learning.confidence >= 0.8) {\n void this.retainBeliefFromLearning(learning);\n }\n }\n\n /**\n * Record a structured belief (subject-predicate-object triple).\n * Safe to call at any time; failures are logged but not thrown.\n */\n async recordBelief(\n subject: string,\n predicate: string,\n object: string,\n confidence: 'high' | 'medium' | 'low' = 'medium'\n ): Promise<void> {\n try {\n await this.beliefs.retain({\n subject,\n predicate,\n object,\n confidence,\n sourceType: BeliefSourceType.OBSERVATION,\n sourceRef: 'mcp-tool-execution',\n });\n } catch (error) {\n this.log.debug('Failed to record belief', { subject, error });\n }\n }\n\n /**\n * Query beliefs relevant to a subject. Returns formatted string or undefined.\n */\n async getRelevantBeliefs(subject: string, limit = 5): Promise<string | undefined> {\n try {\n const result = await this.beliefs.recallBySubject(subject, limit);\n if (!result.ok || result.value.length === 0) return undefined;\n const active = result.value.filter((b) => !b.superseded);\n if (active.length === 0) return undefined;\n return active\n .map((b) => `- [${b.confidence}] ${b.subject} ${b.predicate} ${b.object}`)\n .join('\\n');\n } catch (e: unknown) {\n this.log.debug('Belief recall failed', { error: String(e) });\n return undefined;\n }\n }\n\n /**\n * Record a resolved error. Safe to call even if session inactive.\n */\n recordError(error: ResolvedError): void {\n if (!this.memory.isSessionActive()) return;\n\n const result = this.memory.recordError(error);\n if (!result.ok) {\n this.log.debug('Failed to record error', { error: result.error.message });\n }\n }\n\n /**\n * Search past learnings for relevant patterns.\n */\n searchLearnings(query: string): readonly SessionLearning[] {\n return this.memory.searchLearnings(query);\n }\n\n /**\n * Get recent error solutions.\n */\n getRecentErrorSolutions(limit?: number): readonly ResolvedError[] {\n return this.memory.getRecentErrorSolutions(limit);\n }\n\n /**\n * Retrieve past learnings relevant to a task description.\n * Searches by keywords, falls back to highest-confidence learnings.\n * Returns formatted string or undefined if none found.\n */\n getRelevantLearnings(taskDescription: string, maxResults = 5): string | undefined {\n const past = this.pastLearnings;\n if (past.length === 0) return undefined;\n\n const keywords = taskDescription.split(/\\s+/).slice(0, 5).join(' ');\n const searched = this.searchLearnings(keywords).slice(0, maxResults);\n const learnings =\n searched.length > 0\n ? searched\n : [...past].sort((a, b) => b.confidence - a.confidence).slice(0, 3);\n\n if (learnings.length === 0) return undefined;\n return learnings\n .map((l) => `- [${String(l.confidence)}] ${l.pattern} (${l.context})`)\n .join('\\n');\n }\n\n /**\n * Retrieve past error solutions relevant to a given role or context.\n * Returns formatted string or undefined if none found.\n */\n getRelevantErrorHints(role: string, maxResults = 3): string | undefined {\n const errors = this.getRecentErrorSolutions(10);\n if (errors.length === 0) return undefined;\n\n const relevant = errors.filter(\n (e) => e.filePattern?.includes('execute-expert') === true || e.error.includes(role)\n );\n if (relevant.length === 0) return undefined;\n\n return relevant\n .slice(0, maxResults)\n .map((e) => `- Error: ${e.error.slice(0, 80)} → ${e.solution.slice(0, 80)}`)\n .join('\\n');\n }\n\n /** Whether SQLite-backed memory backends are available (Phase 2). */\n isAdvancedMemoryAvailable(): boolean {\n return this.agentic !== null || this.adaptive !== null;\n }\n\n /** Whether AgenticMemory backend is available. */\n isAgenticMemoryAvailable(): boolean {\n return this.agentic !== null;\n }\n\n /** Whether AdaptiveMemory backend is available. */\n isAdaptiveMemoryAvailable(): boolean {\n return this.adaptive !== null;\n }\n\n /** Store knowledge with auto-extracted attributes (AgenticMemory). Best-effort. */\n async recordKnowledge(key: string, value: unknown, metadata: MemoryMetadata): Promise<void> {\n if (this.agentic === null) return;\n try {\n const result = await this.agentic.storeWithAttributes(key, value, metadata);\n if (!result.ok) {\n this.log.debug('Failed to record knowledge', { key, error: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('Knowledge recording failed', {\n key,\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Store a value in AdaptiveMemory with importance scoring. Best-effort. */\n async storeAdaptive(key: string, value: unknown, importance: number): Promise<void> {\n if (this.adaptive === null) return;\n try {\n const level = importance >= 0.8 ? 'high' : importance >= 0.6 ? 'medium' : 'low';\n const result = await this.adaptive.store(key, value, {\n importance: level,\n tags: ['memory_write_tool'],\n });\n if (!result.ok) {\n this.log.debug('Failed to store adaptive memory', { key, error: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('Adaptive memory store failed', {\n key,\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Store a value in TypedMemory (via HybridMemoryBackend). Best-effort. */\n async storeTyped(key: string, value: unknown, importance: MemoryImportance): Promise<void> {\n if (this.typedBackend === null) return;\n try {\n const result = await this.typedBackend.store(`semantic ${key}`, value, {\n importance,\n tags: ['memory_write_tool', 'semantic'],\n });\n if (!result.ok) {\n this.log.debug('Failed to store typed memory', { key, error: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('Typed memory store failed', {\n key,\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Query knowledge with attribute-based search (AgenticMemory). Best-effort. */\n async queryKnowledge(query: string, limit = 5): Promise<string | undefined> {\n if (this.agentic === null) return undefined;\n try {\n const result = await this.agentic.searchAgentic(query, limit);\n if (!result.ok || result.value.length === 0) return undefined;\n return result.value.map((e) => `- [${e.attributes.keywords.join(',')}] ${e.key}`).join('\\n');\n } catch (e: unknown) {\n this.log.debug('Agentic knowledge query failed', { error: String(e) });\n return undefined;\n }\n }\n\n // ==========================================================================\n // TypedMemory (Phase 1 #746 - MIRIX-style typed memory access)\n // ==========================================================================\n\n /** Whether TypedMemory is available (requires SQLite). */\n isTypedMemoryAvailable(): boolean {\n return this.typed !== null;\n }\n\n /**\n * Query memories by type (core, episodic, semantic, procedural, resource, vault, belief).\n * Returns formatted results or undefined if TypedMemory unavailable.\n */\n async queryByMemoryType(\n type: MemoryType,\n query: string,\n limit = 10\n ): Promise<readonly TypedMemoryEntry[] | undefined> {\n if (this.typed === null) return undefined;\n try {\n const result = await this.typed.queryByType(type, query, limit);\n if (!result.ok) {\n this.log.debug('TypedMemory query failed', { type, error: result.error.message });\n return undefined;\n }\n return result.value;\n } catch (e: unknown) {\n this.log.debug('TypedMemory queryByType failed', { type, error: String(e) });\n return undefined;\n }\n }\n\n /**\n * Filter memories by relevance to an agent role.\n * Uses MIRIX role-memory type mappings (e.g., tech_lead gets core, episodic, vault, belief).\n * Returns filtered entries or undefined if TypedMemory unavailable.\n */\n async filterMemoriesForRole(\n role: AgentRole,\n limit = 50\n ): Promise<readonly TypedMemoryEntry[] | undefined> {\n if (this.typed === null) return undefined;\n try {\n const result = await this.typed.filterByRelevance(role, limit);\n if (!result.ok) {\n this.log.debug('TypedMemory filter failed', { role, error: result.error.message });\n return undefined;\n }\n return result.value;\n } catch (e: unknown) {\n this.log.debug('TypedMemory filterByRelevance failed', { role, error: String(e) });\n return undefined;\n }\n }\n\n /**\n * Get statistics across all typed memory categories.\n * Returns stats object with counts per type or undefined if unavailable.\n */\n async getTypedMemoryStats(): Promise<TypedMemoryStats | undefined> {\n if (this.typed === null) return undefined;\n try {\n const result = await this.typed.getStats();\n if (!result.ok) return undefined;\n return result.value;\n } catch (e: unknown) {\n this.log.debug('TypedMemory getStats failed', { error: String(e) });\n return undefined;\n }\n }\n\n /**\n * Prune expired entries from TypedMemory.\n * Returns prune result with counts per type or undefined if unavailable.\n */\n async pruneTypedMemory(): Promise<TypedMemoryPruneResult | undefined> {\n if (this.typed === null) return undefined;\n try {\n const result = await this.typed.pruneExpired();\n if (!result.ok) return undefined;\n return result.value;\n } catch (e: unknown) {\n this.log.debug('TypedMemory pruneExpired failed', { error: String(e) });\n return undefined;\n }\n }\n\n // ==========================================================================\n // MobiMem (Phase 2 #746 - Post-deployment learning)\n // ==========================================================================\n\n /** Whether MobiMem is available for post-deployment learning. */\n isMobiMemAvailable(): boolean {\n return this.mobimem !== null;\n }\n\n /**\n * Get the MobiMem instance for direct access to profile, experience, and action cache.\n * Returns null if MobiMem is unavailable.\n */\n getMobiMem(): MobiMem | null {\n return this.mobimem;\n }\n\n /**\n * Get MobiMem statistics across all three modules.\n * Returns stats object or undefined if unavailable.\n */\n getMobiMemStats(): MobiMemStats | undefined {\n if (this.mobimem === null) return undefined;\n return this.mobimem.getStats();\n }\n\n /** Returns the count of beliefs in the belief memory backend. */\n getBeliefCount(): number {\n const data = this.beliefs.exportData();\n return data.beliefs.size;\n }\n\n /**\n * Run MobiMem maintenance (eviction and cleanup).\n * Safe to call even if MobiMem is unavailable.\n */\n runMobiMemMaintenance(): void {\n if (this.mobimem === null) return;\n this.mobimem.runMaintenance();\n }\n\n // ==========================================================================\n // Cross-Memory Query (Phase 3 #746 - Unified search across all backends)\n // ==========================================================================\n\n /**\n * Unified search across all active memory systems.\n * Returns results from SessionMemory, BeliefMemory, AgenticMemory, and TypedMemory\n * with source attribution and relevance scoring.\n */\n async queryAll(query: string, limit = 10): Promise<readonly UnifiedMemoryResult[]> {\n // Wait for SQLite backends to finish initializing before querying (#794 pattern)\n if (this.initPromise !== null) {\n await this.initPromise;\n this.initPromise = null;\n }\n const keywords = query\n .toLowerCase()\n .split(/\\s+/)\n .filter((k) => k.length > 2);\n const sourceCount = 4 + (this.adaptive !== null ? 1 : 0);\n const perSource = Math.ceil(limit / sourceCount);\n const results = [\n ...this.querySessionMemory(query, keywords, perSource),\n ...(await this.queryBeliefMemory(query, keywords, perSource)),\n ...(await this.queryAgenticMemory(query, keywords, perSource)),\n ...(await this.queryTypedMemory(query, keywords, Math.ceil(perSource / 2))),\n ...(await this.queryAdaptiveMemory(query, keywords, perSource)),\n ];\n return results.sort((a, b) => b.relevance - a.relevance).slice(0, limit);\n }\n\n /**\n * Query a specific memory backend directly, bypassing cross-backend limit dilution.\n * When source is 'all', delegates to queryAll(). Otherwise dispatches to the\n * single-backend method so the full limit is applied to that backend only.\n */\n async queryBySource(\n source: 'session' | 'belief' | 'agentic' | 'typed' | 'adaptive' | 'all',\n query: string,\n limit = 10\n ): Promise<readonly UnifiedMemoryResult[]> {\n if (source === 'all') {\n return this.queryAll(query, limit);\n }\n // Wait for SQLite backends to finish initializing\n if (this.initPromise !== null) {\n await this.initPromise;\n this.initPromise = null;\n }\n const keywords = query\n .toLowerCase()\n .split(/\\s+/)\n .filter((k) => k.length > 2);\n let results: UnifiedMemoryResult[];\n switch (source) {\n case 'session':\n results = this.querySessionMemory(query, keywords, limit);\n break;\n case 'belief':\n results = await this.queryBeliefMemory(query, keywords, limit);\n break;\n case 'agentic':\n results = await this.queryAgenticMemory(query, keywords, limit);\n break;\n case 'typed':\n results = await this.queryTypedMemory(query, keywords, limit);\n break;\n case 'adaptive':\n results = await this.queryAdaptiveMemory(query, keywords, limit);\n break;\n }\n return results.sort((a, b) => b.relevance - a.relevance).slice(0, limit);\n }\n\n /** Query SessionMemory for learnings. */\n // Per-backend query helpers delegated to tool-memory-query.ts (#1671).\n\n private querySessionMemory(\n query: string,\n keywords: readonly string[],\n limit: number\n ): UnifiedMemoryResult[] {\n return querySessionMemoryHelper(this.searchLearnings(query), keywords, limit);\n }\n\n private async queryBeliefMemory(\n query: string,\n keywords: readonly string[],\n limit: number\n ): Promise<UnifiedMemoryResult[]> {\n return queryBeliefMemoryHelper(this.beliefs, query, keywords, limit, this.log);\n }\n\n private async queryAgenticMemory(\n query: string,\n keywords: readonly string[],\n limit: number\n ): Promise<UnifiedMemoryResult[]> {\n if (this.agentic === null) return [];\n\n return queryAgenticMemoryHelper(this.agentic, query, keywords, limit, this.log);\n }\n\n private async queryTypedMemory(\n query: string,\n keywords: readonly string[],\n limitPerType: number\n ): Promise<UnifiedMemoryResult[]> {\n if (this.typed === null) return [];\n\n return queryTypedMemoryHelper(this.typed, query, keywords, limitPerType, this.log);\n }\n\n private async queryAdaptiveMemory(\n query: string,\n keywords: readonly string[],\n limit: number\n ): Promise<UnifiedMemoryResult[]> {\n if (this.adaptive === null) return [];\n\n return queryAdaptiveMemoryHelper(this.adaptive, query, keywords, limit, this.log);\n }\n\n // ==========================================================================\n // Memory Promotion Pipeline (Phase 4 #746)\n // ==========================================================================\n\n /**\n * Run the memory promotion pipeline.\n * Promotes high-confidence learnings to beliefs, and stable beliefs to AgenticMemory.\n * Returns statistics about the promotion run.\n */\n async runPromotionPipeline(config?: Partial<MemoryPromotionConfig>): Promise<PromotionStats> {\n const promoter = new MemoryPromoter(this.beliefs, this.agentic, config, this.log);\n\n // Get current learnings and beliefs for promotion evaluation\n const learnings = this.pastLearnings;\n const beliefData = this.beliefs.exportData();\n const beliefs = Array.from(beliefData.beliefs.values());\n\n const stats = await promoter.runPromotionPipeline(learnings, beliefs);\n\n this.log.info('Promotion pipeline completed', {\n learningsPromoted: stats.learningsPromotedToBelief,\n beliefsPromoted: stats.beliefsPromotedToAgentic,\n });\n\n return stats;\n }\n\n // ==========================================================================\n // Coordinated Decay (Phase 5 #746)\n // ==========================================================================\n\n /** Whether coordinated decay is available. */\n isDecayManagerAvailable(): boolean {\n return this.decayManager !== null;\n }\n\n /**\n * Run coordinated decay across all memory systems.\n * Implements FADE (Forgetting with Adaptive Decay) principles.\n * Returns statistics about the decay run.\n */\n async runDecay(): Promise<DecayRunStats | undefined> {\n if (this.decayManager === null) return undefined;\n return this.decayManager.runDecay();\n }\n\n /**\n * Get aggregate statistics across all decay runs.\n */\n getDecayStats(): DecayAggregateStats | undefined {\n if (this.decayManager === null) return undefined;\n return this.decayManager.getAggregateStats();\n }\n\n /**\n * Get the last N decay run results.\n */\n getRecentDecayRuns(limit = 10): readonly DecayRunStats[] {\n if (this.decayManager === null) return [];\n return this.decayManager.getRecentRuns(limit);\n }\n\n /**\n * Register a cross-reference between memory systems.\n * Used to prevent orphaned references during decay.\n */\n registerCrossReference(\n sourceMemory: 'session' | 'belief' | 'agentic' | 'adaptive' | 'mobimem',\n sourceKey: string,\n targetMemory: 'session' | 'belief' | 'agentic' | 'adaptive' | 'mobimem',\n targetKey: string\n ): void {\n if (this.decayManager === null) return;\n this.decayManager.registerCrossReference(sourceMemory, sourceKey, targetMemory, targetKey);\n }\n\n /** End the current session and persist to disk. Closes SQLite backends. */\n endSession(): void {\n // Persist belief memory to disk (Phase 3, Issue #714)\n this.saveBeliefSnapshotToDisk();\n\n if (this.memory.isSessionActive()) {\n const result = this.memory.endSession('MCP session ended');\n if (result.ok) {\n this.log.info('Tool memory session saved', {\n learnings: result.value.learnings.length,\n tasks: result.value.tasksCompleted.length,\n errors: result.value.errorsResolved.length,\n });\n }\n }\n if (this.agentic !== null) {\n this.agentic.close();\n this.agentic = null;\n }\n if (this.adaptive !== null) {\n this.adaptive.close();\n this.adaptive = null;\n }\n // TypedMemory uses HybridMemoryBackend which needs explicit close\n if (this.typedBackend !== null) {\n this.typedBackend.close();\n this.typedBackend = null;\n }\n this.typed = null;\n // MobiMem has its own close method\n if (this.mobimem !== null) {\n this.mobimem.close();\n this.mobimem = null;\n }\n // Shutdown decay manager (Phase 5 #746)\n if (this.decayManager !== null) {\n this.decayManager.shutdown();\n this.decayManager = null;\n }\n }\n\n /** Convert a high-confidence learning into a structured belief. */\n private async retainBeliefFromLearning(learning: SessionLearning): Promise<void> {\n try {\n const confidence =\n learning.confidence >= 0.9 ? BeliefConfidence.HIGH : BeliefConfidence.MEDIUM;\n await this.beliefs.retain({\n subject: learning.context,\n predicate: 'learned-pattern',\n object: learning.pattern,\n confidence,\n sourceType: BeliefSourceType.OBSERVATION,\n sourceRef: `session-learning`,\n });\n } catch (e: unknown) {\n this.log.debug('Belief creation from learning failed', { error: String(e) });\n }\n }\n\n /** Load belief snapshot from disk on startup (Phase 3, Issue #714). */\n private loadBeliefSnapshotFromDisk(): void {\n try {\n const result = loadBeliefSnapshot(this.log);\n if (!result.ok) {\n this.log.warn('Failed to load belief snapshot', { error: result.error.message });\n return;\n }\n if (result.value === null) return;\n this.beliefs.hydrate(result.value);\n } catch (error: unknown) {\n this.log.debug('Belief snapshot load failed', {\n error: getErrorMessage(error),\n });\n }\n }\n\n /** Save belief snapshot to disk on shutdown (Phase 3, Issue #714). */\n private saveBeliefSnapshotToDisk(): void {\n try {\n const data = this.beliefs.exportData();\n if (data.beliefs.size === 0) {\n this.log.debug('No beliefs to persist, skipping snapshot');\n return;\n }\n const result = saveBeliefSnapshot(data, this.log);\n if (!result.ok) {\n this.log.warn('Failed to save belief snapshot', { error: result.error.message });\n }\n } catch (error: unknown) {\n this.log.debug('Belief snapshot save failed', {\n error: getErrorMessage(error),\n });\n }\n }\n}\n","/**\n * nexus-agents/context - Belief Core Types\n *\n * Core type definitions for belief states including confidence levels,\n * source types, and the fundamental Belief interface.\n *\n * @module context/belief-core-types\n * @see belief-types for re-exports\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport { z } from 'zod';\n\n// ============================================================================\n// Belief Confidence Levels\n// ============================================================================\n\n/**\n * Confidence level for belief states.\n * Based on evidence quality and reasoning chain length.\n */\nexport const BeliefConfidence = {\n /** Strong evidence, short reasoning chain */\n HIGH: 'high',\n /** Moderate evidence or indirect inference */\n MEDIUM: 'medium',\n /** Weak evidence or long inference chain */\n LOW: 'low',\n /** Speculative or hypothetical */\n SPECULATIVE: 'speculative',\n} as const;\n\nexport type BeliefConfidence = (typeof BeliefConfidence)[keyof typeof BeliefConfidence];\n\nexport const BeliefConfidenceSchema = z.enum(['high', 'medium', 'low', 'speculative']);\n\n// ============================================================================\n// Belief Source Types\n// ============================================================================\n\n/**\n * Source type for belief origin tracking.\n */\nexport const BeliefSourceType = {\n /** Direct observation from environment */\n OBSERVATION: 'observation',\n /** Inference from other beliefs */\n INFERENCE: 'inference',\n /** External knowledge or provided fact */\n EXTERNAL: 'external',\n /** User-provided information */\n USER_INPUT: 'user_input',\n /** Hindsight correction from outcome */\n HINDSIGHT: 'hindsight',\n /** Default or prior belief */\n PRIOR: 'prior',\n} as const;\n\nexport type BeliefSourceType = (typeof BeliefSourceType)[keyof typeof BeliefSourceType];\n\nexport const BeliefSourceTypeSchema = z.enum([\n 'observation',\n 'inference',\n 'external',\n 'user_input',\n 'hindsight',\n 'prior',\n]);\n\n// ============================================================================\n// Belief State\n// ============================================================================\n\n/**\n * A belief represents an agent's held proposition about the world.\n * Beliefs are versioned and traceable to their sources.\n */\nexport interface Belief {\n /** Unique identifier for this belief */\n readonly beliefId: string;\n /** The entity this belief is about */\n readonly subject: string;\n /** The property or relation being described */\n readonly predicate: string;\n /** The value or target of the relation */\n readonly object: string;\n /** Confidence level in this belief */\n readonly confidence: BeliefConfidence;\n /** Source type for this belief */\n readonly sourceType: BeliefSourceType;\n /** Reference to source evidence or reasoning */\n readonly sourceRef?: string;\n /** Parent belief IDs if derived through inference */\n readonly derivedFrom?: readonly string[];\n /** Version number for tracking updates */\n readonly version: number;\n /** When this belief was formed */\n readonly createdAt: Date;\n /** When this belief was last updated */\n readonly updatedAt: Date;\n /** Whether this belief has been superseded */\n readonly superseded: boolean;\n /** ID of belief that superseded this one */\n readonly supersededBy?: string;\n /** Domain or context for this belief */\n readonly domain?: string;\n /** Additional metadata */\n readonly metadata?: Record<string, unknown>;\n}\n\nexport const BeliefSchema = z.object({\n beliefId: z.string().min(1),\n subject: z.string().min(1),\n predicate: z.string().min(1),\n object: z.string(),\n confidence: BeliefConfidenceSchema,\n sourceType: BeliefSourceTypeSchema,\n sourceRef: z.string().optional(),\n derivedFrom: z.array(z.string()).optional(),\n version: z.number().int().min(1),\n createdAt: z.date(),\n updatedAt: z.date(),\n superseded: z.boolean(),\n supersededBy: z.string().optional(),\n domain: z.string().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n});\n","/**\n * nexus-agents/context - Belief Update Types\n *\n * Type definitions for belief update operations, queries, and audit records.\n *\n * @module context/belief-update-types\n * @see belief-types for re-exports\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport { z } from 'zod';\nimport type { Belief } from './belief-core-types.js';\nimport { BeliefConfidenceSchema, BeliefSourceTypeSchema } from './belief-core-types.js';\nimport type { BeliefConfidence, BeliefSourceType } from './belief-core-types.js';\n\n// ============================================================================\n// Belief Update Operations\n// ============================================================================\n\n/**\n * Types of belief update operations.\n */\nexport const BeliefUpdateType = {\n /** Add a new belief (retain) */\n RETAIN: 'retain',\n /** Update confidence or metadata */\n REVISE: 'revise',\n /** Mark belief as superseded */\n SUPERSEDE: 'supersede',\n /** Hindsight correction based on outcome */\n CORRECT: 'correct',\n /** Strengthen belief based on corroboration */\n REINFORCE: 'reinforce',\n /** Weaken belief based on contradicting evidence */\n WEAKEN: 'weaken',\n} as const;\n\nexport type BeliefUpdateType = (typeof BeliefUpdateType)[keyof typeof BeliefUpdateType];\n\nexport const BeliefUpdateTypeSchema = z.enum([\n 'retain',\n 'revise',\n 'supersede',\n 'correct',\n 'reinforce',\n 'weaken',\n]);\n\n/**\n * Record of a belief update for audit trail.\n */\nexport interface BeliefUpdate {\n /** Unique identifier for this update */\n readonly updateId: string;\n /** ID of the belief being updated */\n readonly beliefId: string;\n /** Type of update operation */\n readonly updateType: BeliefUpdateType;\n /** Previous state (for revisions) */\n readonly previousState?: Partial<Belief>;\n /** New state after update */\n readonly newState: Partial<Belief>;\n /** Reason for the update */\n readonly reason: string;\n /** Evidence supporting this update */\n readonly evidence?: string;\n /** When this update occurred */\n readonly timestamp: Date;\n /** Agent or process that made the update */\n readonly updatedBy?: string;\n}\n\nexport const BeliefUpdateSchema = z.object({\n updateId: z.string().min(1),\n beliefId: z.string().min(1),\n updateType: BeliefUpdateTypeSchema,\n previousState: z.record(z.string(), z.unknown()).optional(),\n newState: z.record(z.string(), z.unknown()),\n reason: z.string().min(1),\n evidence: z.string().optional(),\n timestamp: z.date(),\n updatedBy: z.string().optional(),\n});\n\n// ============================================================================\n// Belief Query Types\n// ============================================================================\n\n/**\n * Query options for retrieving beliefs.\n */\nexport interface BeliefQuery {\n /** Filter by subject entity */\n readonly subject?: string;\n /** Filter by predicate */\n readonly predicate?: string;\n /** Filter by domain */\n readonly domain?: string;\n /** Minimum confidence level */\n readonly minConfidence?: BeliefConfidence;\n /** Include superseded beliefs */\n readonly includeSuperseded?: boolean;\n /** Filter by source type */\n readonly sourceType?: BeliefSourceType;\n /** Maximum number of results */\n readonly limit?: number;\n /** Order by field */\n readonly orderBy?: 'createdAt' | 'updatedAt' | 'confidence';\n /** Order direction */\n readonly orderDirection?: 'asc' | 'desc';\n}\n\nexport const BeliefQuerySchema = z.object({\n subject: z.string().optional(),\n predicate: z.string().optional(),\n domain: z.string().optional(),\n minConfidence: BeliefConfidenceSchema.optional(),\n includeSuperseded: z.boolean().optional(),\n sourceType: BeliefSourceTypeSchema.optional(),\n limit: z.number().int().positive().max(1000).optional(),\n orderBy: z.enum(['createdAt', 'updatedAt', 'confidence']).optional(),\n orderDirection: z.enum(['asc', 'desc']).optional(),\n});\n","/**\n * nexus-agents/context - Belief Hindsight Types\n *\n * Type definitions for counterfactual reasoning and hindsight learning.\n *\n * @module context/belief-hindsight-types\n * @see belief-types for re-exports\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport { z } from 'zod';\n\n// ============================================================================\n// Counterfactual Reasoning\n// ============================================================================\n\n/**\n * A counterfactual represents an alternative scenario for reasoning.\n */\nexport interface Counterfactual {\n /** Unique identifier */\n readonly counterfactualId: string;\n /** The hypothetical change to consider */\n readonly hypothesis: string;\n /** Beliefs that would change under this hypothesis */\n readonly affectedBeliefs: readonly string[];\n /** Predicted outcomes under this scenario */\n readonly predictedOutcomes: readonly string[];\n /** Actual outcomes if hypothesis was tested */\n readonly actualOutcomes?: readonly string[];\n /** Whether the counterfactual was validated */\n readonly validated: boolean;\n /** When this counterfactual was created */\n readonly createdAt: Date;\n /** Task or context that prompted this counterfactual */\n readonly taskContext?: string;\n}\n\nexport const CounterfactualSchema = z.object({\n counterfactualId: z.string().min(1),\n hypothesis: z.string().min(1),\n affectedBeliefs: z.array(z.string()),\n predictedOutcomes: z.array(z.string()),\n actualOutcomes: z.array(z.string()).optional(),\n validated: z.boolean(),\n createdAt: z.date(),\n taskContext: z.string().optional(),\n});\n\n// ============================================================================\n// Hindsight Learning\n// ============================================================================\n\n/**\n * Hindsight record captures learning from outcomes.\n */\nexport interface HindsightRecord {\n /** Unique identifier */\n readonly hindsightId: string;\n /** Task that produced this hindsight */\n readonly taskId: string;\n /** Beliefs held before the task */\n readonly priorBeliefs: readonly string[];\n /** Expected outcome based on prior beliefs */\n readonly expectedOutcome: string;\n /** Actual outcome observed */\n readonly actualOutcome: string;\n /** Whether expectation matched reality */\n readonly outcomeMatched: boolean;\n /** Beliefs that were corrected */\n readonly correctedBeliefs: readonly string[];\n /** New beliefs formed from this experience */\n readonly newBeliefs: readonly string[];\n /** Lessons learned */\n readonly lessons: readonly string[];\n /** When this record was created */\n readonly createdAt: Date;\n}\n\nexport const HindsightRecordSchema = z.object({\n hindsightId: z.string().min(1),\n taskId: z.string().min(1),\n priorBeliefs: z.array(z.string()),\n expectedOutcome: z.string(),\n actualOutcome: z.string(),\n outcomeMatched: z.boolean(),\n correctedBeliefs: z.array(z.string()),\n newBeliefs: z.array(z.string()),\n lessons: z.array(z.string()),\n createdAt: z.date(),\n});\n","/**\n * nexus-agents/context - Belief Memory Interface\n *\n * Interface definition for Hindsight Belief Memory operations,\n * statistics types, and configuration options.\n *\n * @module context/belief-memory-interface\n * @see belief-types for re-exports\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport { z } from 'zod';\nimport type { Result } from '../core/result.js';\nimport type { MemoryError } from './memory-backend-types.js';\nimport type { Belief, BeliefConfidence, BeliefSourceType } from './belief-core-types.js';\nimport { BeliefConfidenceSchema, BeliefSourceTypeSchema } from './belief-core-types.js';\nimport { BeliefConfidence as BeliefConfidenceConst } from './belief-core-types.js';\nimport type { BeliefUpdate, BeliefQuery } from './belief-update-types.js';\nimport type { Counterfactual, HindsightRecord } from './belief-hindsight-types.js';\n\n// ============================================================================\n// Belief Memory Interface\n// ============================================================================\n\n/**\n * Interface for Hindsight Belief Memory operations.\n * Implements the three core operations: retain, recall, and reflect.\n */\nexport interface IHindsightBeliefMemory {\n // === Retain Operations (Adding Information) ===\n\n /**\n * Store a new belief.\n * @param belief - The belief to store (without beliefId, version, timestamps)\n */\n retain(\n belief: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>\n ): Promise<Result<Belief, MemoryError>>;\n\n /**\n * Store multiple beliefs atomically.\n */\n retainBatch(\n beliefs: readonly Omit<\n Belief,\n 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'\n >[]\n ): Promise<Result<readonly Belief[], MemoryError>>;\n\n // === Recall Operations (Accessing Information) ===\n\n /**\n * Retrieve a belief by ID.\n */\n recall(beliefId: string): Promise<Result<Belief | null, MemoryError>>;\n\n /**\n * Query beliefs with filters.\n */\n query(query: BeliefQuery): Promise<Result<readonly Belief[], MemoryError>>;\n\n /**\n * Get all beliefs about a subject.\n */\n recallBySubject(subject: string, limit?: number): Promise<Result<readonly Belief[], MemoryError>>;\n\n /**\n * Get current belief for a subject-predicate pair.\n */\n recallCurrent(subject: string, predicate: string): Promise<Result<Belief | null, MemoryError>>;\n\n /**\n * Get belief history for a subject-predicate pair.\n */\n recallHistory(\n subject: string,\n predicate: string,\n limit?: number\n ): Promise<Result<readonly Belief[], MemoryError>>;\n\n // === Reflect Operations (Updating Information) ===\n\n /**\n * Update a belief with a new version.\n */\n revise(\n beliefId: string,\n updates: Partial<Pick<Belief, 'object' | 'confidence' | 'metadata'>>,\n reason: string\n ): Promise<Result<Belief, MemoryError>>;\n\n /**\n * Supersede a belief with a new one.\n */\n supersede(\n beliefId: string,\n newBelief: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>,\n reason: string\n ): Promise<Result<Belief, MemoryError>>;\n\n /**\n * Apply hindsight correction to beliefs.\n */\n applyHindsight(record: HindsightRecord): Promise<Result<readonly Belief[], MemoryError>>;\n\n /**\n * Reinforce a belief based on corroborating evidence.\n */\n reinforce(beliefId: string, evidence: string): Promise<Result<Belief, MemoryError>>;\n\n /**\n * Weaken a belief based on contradicting evidence.\n */\n weaken(beliefId: string, evidence: string): Promise<Result<Belief, MemoryError>>;\n\n // === Counterfactual Reasoning ===\n\n /**\n * Create a counterfactual scenario.\n */\n createCounterfactual(\n hypothesis: string,\n taskContext?: string\n ): Promise<Result<Counterfactual, MemoryError>>;\n\n /**\n * Validate a counterfactual with actual outcomes.\n */\n validateCounterfactual(\n counterfactualId: string,\n actualOutcomes: readonly string[]\n ): Promise<Result<Counterfactual, MemoryError>>;\n\n /**\n * Get counterfactuals for a task context.\n */\n getCounterfactuals(taskContext: string): Promise<Result<readonly Counterfactual[], MemoryError>>;\n\n // === Audit and History ===\n\n /**\n * Get update history for a belief.\n */\n getUpdateHistory(beliefId: string): Promise<Result<readonly BeliefUpdate[], MemoryError>>;\n\n /**\n * Get hindsight records for a task.\n */\n getHindsightRecords(taskId: string): Promise<Result<readonly HindsightRecord[], MemoryError>>;\n\n // === Statistics ===\n\n /**\n * Get belief memory statistics.\n */\n getStats(): Promise<Result<BeliefMemoryStats, MemoryError>>;\n\n /**\n * Prune old superseded beliefs.\n */\n pruneSuperseded(olderThan: Date): Promise<Result<number, MemoryError>>;\n}\n\n// ============================================================================\n// Statistics\n// ============================================================================\n\n/**\n * Statistics for belief memory.\n */\nexport interface BeliefMemoryStats {\n readonly totalBeliefs: number;\n readonly activeBeliefs: number;\n readonly supersededBeliefs: number;\n readonly beliefsByConfidence: Record<BeliefConfidence, number>;\n readonly beliefsBySource: Record<BeliefSourceType, number>;\n readonly totalUpdates: number;\n readonly totalCounterfactuals: number;\n readonly totalHindsightRecords: number;\n readonly oldestBelief?: Date;\n readonly newestBelief?: Date;\n}\n\nexport const BeliefMemoryStatsSchema = z.object({\n totalBeliefs: z.number().int().min(0),\n activeBeliefs: z.number().int().min(0),\n supersededBeliefs: z.number().int().min(0),\n beliefsByConfidence: z.record(BeliefConfidenceSchema, z.number().int().min(0)),\n beliefsBySource: z.record(BeliefSourceTypeSchema, z.number().int().min(0)),\n totalUpdates: z.number().int().min(0),\n totalCounterfactuals: z.number().int().min(0),\n totalHindsightRecords: z.number().int().min(0),\n oldestBelief: z.date().optional(),\n newestBelief: z.date().optional(),\n});\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\n/**\n * Configuration for HindsightBeliefMemory.\n */\nexport interface BeliefMemoryConfig {\n /** Maximum beliefs to retain per subject */\n readonly maxBeliefsPerSubject?: number;\n /** Maximum total beliefs across all subjects (prevents unbounded growth) */\n readonly maxTotalBeliefs?: number;\n /** Auto-prune superseded beliefs older than this (ms) */\n readonly autoPruneAge?: number;\n /** Enable belief inference from observations */\n readonly enableInference?: boolean;\n /** Default confidence for new beliefs without explicit confidence */\n readonly defaultConfidence?: BeliefConfidence;\n /** Maximum depth for derived belief chains */\n readonly maxInferenceDepth?: number;\n}\n\nexport const BeliefMemoryConfigSchema = z.object({\n maxBeliefsPerSubject: z.number().int().positive().optional(),\n maxTotalBeliefs: z.number().int().positive().optional(),\n autoPruneAge: z.number().positive().optional(),\n enableInference: z.boolean().optional(),\n defaultConfidence: BeliefConfidenceSchema.optional(),\n maxInferenceDepth: z.number().int().positive().max(10).optional(),\n});\n\n/**\n * Default configuration values.\n */\nexport const DEFAULT_BELIEF_CONFIG: Required<BeliefMemoryConfig> = {\n maxBeliefsPerSubject: 100,\n maxTotalBeliefs: 10_000,\n autoPruneAge: 30 * 24 * 60 * 60 * 1000, // 30 days\n enableInference: true,\n defaultConfidence: BeliefConfidenceConst.MEDIUM,\n maxInferenceDepth: 5,\n};\n","/**\n * nexus-agents/utils - ID Generation Utilities\n *\n * Shared utility functions for generating unique identifiers.\n * Consolidates duplicate generateId implementations per ADR-0013.\n *\n * Uses getTimeProvider and getRandomProvider for deterministic testing.\n *\n * @module utils/id-utils\n * @see docs/adr/0013-memory-helpers-consolidation.md\n */\n\nimport { randomUUID } from 'node:crypto';\nimport { getTimeProvider, getRandomProvider } from '../core/index.js';\n\n/**\n * Generate a unique ID with prefix using timestamp and random suffix.\n *\n * Format: `${prefix}_${timestamp}_${random}` (underscore-separated)\n *\n * Uses getTimeProvider() and getRandomProvider() for deterministic testing.\n *\n * @param prefix - ID prefix (e.g., 'belief', 'update', 'exec')\n * @param randomLength - Length of random suffix (default: 8)\n * @returns Unique ID string\n *\n * @example\n * ```typescript\n * const beliefId = generateId('belief'); // 'belief_1p5k3x2_a1b2c3d4'\n * const execId = generateId('exec', 6); // 'exec_1p5k3x2_a1b2c3'\n * ```\n */\nexport function generateId(prefix: string, randomLength = 8): string {\n const timestamp = getTimeProvider().now().toString(36);\n const random = getRandomProvider()\n .random()\n .toString(36)\n .substring(2, 2 + randomLength);\n return `${prefix}_${timestamp}_${random}`;\n}\n\n/**\n * Generate a unique ID with hyphen separators.\n *\n * Format: `${prefix}-${timestamp}-${random}` (hyphen-separated)\n *\n * @param prefix - ID prefix\n * @param randomLength - Length of random suffix (default: 6)\n * @returns Unique ID string\n *\n * @example\n * ```typescript\n * const id = generateHyphenId('workflow'); // 'workflow-1769876392192-a1b2c3'\n * ```\n */\nexport function generateHyphenId(prefix: string, randomLength = 6): string {\n const timestamp = String(getTimeProvider().now());\n const random = getRandomProvider()\n .random()\n .toString(36)\n .slice(2, 2 + randomLength);\n return `${prefix}-${timestamp}-${random}`;\n}\n\n/**\n * Generate a short unique ID using randomUUID.\n *\n * Format: `${prefix}-${shortUuid}` where shortUuid is first 8 chars of UUID.\n *\n * @param prefix - ID prefix\n * @returns Short unique ID string\n *\n * @example\n * ```typescript\n * const id = generateShortUuid('workflow'); // 'workflow-a1b2c3d4'\n * ```\n */\nexport function generateShortUuid(prefix: string): string {\n // Use random provider for determinism, generate UUID-like pattern\n const rand1 = getRandomProvider().random().toString(16).substring(2, 6);\n const rand2 = getRandomProvider().random().toString(16).substring(2, 6);\n return `${prefix}-${rand1}${rand2}`;\n}\n\n// ============================================================================\n// UUID Generation (node:crypto)\n// ============================================================================\n\n/**\n * Generates a UUID v4 using node:crypto.randomUUID().\n * This is the canonical UUID generator - use instead of `uuid` package.\n *\n * Note: This uses node:crypto directly for performance. For deterministic\n * testing scenarios, use generateId() or generateShortUuid() instead.\n *\n * @returns A UUID v4 string\n *\n * @example\n * ```typescript\n * const id = generateUUID(); // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'\n * ```\n */\nexport function generateUUID(): string {\n return randomUUID();\n}\n\n/**\n * Generates a short UUID (first 8 characters).\n * Useful for display purposes or step IDs.\n *\n * @param prefix - Optional prefix for the ID\n * @returns A short UUID string, optionally prefixed\n *\n * @example\n * ```typescript\n * const id = generateShortUUIDv4(); // 'f47ac10b'\n * const stepId = generateShortUUIDv4('step'); // 'step-f47ac10b'\n * ```\n */\nexport function generateShortUUIDv4(prefix?: string): string {\n const shortId = randomUUID().slice(0, 8);\n return prefix !== undefined ? `${prefix}-${shortId}` : shortId;\n}\n\n/**\n * Generates a step ID using UUID.\n * Replacement for `step-${uuidv4().slice(0, 8)}` pattern.\n *\n * @param prefix - Prefix for the step ID (default: 'step')\n * @returns A prefixed short UUID\n */\nexport function generateStepId(prefix = 'step'): string {\n return generateShortUUIDv4(prefix);\n}\n","/**\n * nexus-agents/context - Hindsight Belief Memory Helpers\n *\n * Helper functions for the Hindsight Belief Memory implementation.\n *\n * @module context/belief-memory-helpers\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport { getTimeProvider } from '../core/index.js';\nimport type {\n Belief,\n BeliefConfidence,\n BeliefMemoryStats,\n BeliefQuery,\n BeliefSourceType,\n} from './belief-types.js';\nimport { BeliefConfidence as BeliefConfidenceEnum } from './belief-types.js';\n// Shared utilities per ADR-0013\nimport { generateId as sharedGenerateId } from '../utils/id-utils.js';\n\n// ============================================================================\n// Confidence Level Operations\n// ============================================================================\n\n/** Compare confidence levels for ordering */\nexport function compareConfidence(a: BeliefConfidence, b: BeliefConfidence): number {\n const order: Record<BeliefConfidence, number> = {\n high: 4,\n medium: 3,\n low: 2,\n speculative: 1,\n };\n return order[a] - order[b];\n}\n\n/** Get next confidence level up */\nexport function strengthenConfidence(current: BeliefConfidence): BeliefConfidence {\n const progression: Record<BeliefConfidence, BeliefConfidence> = {\n speculative: BeliefConfidenceEnum.LOW,\n low: BeliefConfidenceEnum.MEDIUM,\n medium: BeliefConfidenceEnum.HIGH,\n high: BeliefConfidenceEnum.HIGH,\n };\n return progression[current];\n}\n\n/** Get next confidence level down */\nexport function weakenConfidence(current: BeliefConfidence): BeliefConfidence {\n const regression: Record<BeliefConfidence, BeliefConfidence> = {\n high: BeliefConfidenceEnum.MEDIUM,\n medium: BeliefConfidenceEnum.LOW,\n low: BeliefConfidenceEnum.SPECULATIVE,\n speculative: BeliefConfidenceEnum.SPECULATIVE,\n };\n return regression[current];\n}\n\n// ============================================================================\n// Belief Sorting\n// ============================================================================\n\n/** Sort beliefs by query options */\nexport function sortBeliefs(\n beliefs: readonly Belief[],\n orderBy?: 'createdAt' | 'updatedAt' | 'confidence',\n direction?: 'asc' | 'desc'\n): Belief[] {\n const sorted = [...beliefs];\n const multiplier = direction === 'asc' ? 1 : -1;\n\n sorted.sort((a, b) => {\n switch (orderBy) {\n case 'createdAt':\n return (a.createdAt.getTime() - b.createdAt.getTime()) * multiplier;\n case 'updatedAt':\n return (a.updatedAt.getTime() - b.updatedAt.getTime()) * multiplier;\n case 'confidence':\n return compareConfidence(a.confidence, b.confidence) * multiplier;\n default:\n return (b.updatedAt.getTime() - a.updatedAt.getTime()) * multiplier;\n }\n });\n\n return sorted;\n}\n\n// ============================================================================\n// Query Filtering\n// ============================================================================\n\n/** Check if belief matches query filters */\nexport function matchesQueryFilters(belief: Belief, query: BeliefQuery): boolean {\n if (query.includeSuperseded !== true && belief.superseded) {\n return false;\n }\n if (query.minConfidence !== undefined) {\n if (compareConfidence(belief.confidence, query.minConfidence) < 0) {\n return false;\n }\n }\n if (query.sourceType !== undefined && belief.sourceType !== query.sourceType) {\n return false;\n }\n return true;\n}\n\n/** Intersect two sets */\nexport function intersectSets<T>(a: Set<T>, b: Set<T>): Set<T> {\n const result = new Set<T>();\n for (const item of a) {\n if (b.has(item)) {\n result.add(item);\n }\n }\n return result;\n}\n\n// ============================================================================\n// Statistics Computation\n// ============================================================================\n\n/** Initialize empty stats counters */\nexport function initializeStatsCounters(): {\n beliefsByConfidence: Record<BeliefConfidence, number>;\n beliefsBySource: Record<BeliefSourceType, number>;\n} {\n return {\n beliefsByConfidence: {\n high: 0,\n medium: 0,\n low: 0,\n speculative: 0,\n },\n beliefsBySource: {\n observation: 0,\n inference: 0,\n external: 0,\n user_input: 0,\n hindsight: 0,\n prior: 0,\n },\n };\n}\n\n/** Build final stats object with optional date fields */\nexport function buildStatsResult(\n baseStats: Omit<BeliefMemoryStats, 'oldestBelief' | 'newestBelief'>,\n oldestBelief: Date | undefined,\n newestBelief: Date | undefined\n): BeliefMemoryStats {\n if (oldestBelief !== undefined && newestBelief !== undefined) {\n return { ...baseStats, oldestBelief, newestBelief };\n } else if (oldestBelief !== undefined) {\n return { ...baseStats, oldestBelief };\n } else if (newestBelief !== undefined) {\n return { ...baseStats, newestBelief };\n }\n return baseStats;\n}\n\n// ============================================================================\n// Update Record Creation\n// ============================================================================\n\nimport type { BeliefUpdate, BeliefUpdateType } from './belief-types.js';\n\n/** Options for creating an update record */\nexport interface UpdateRecordOptions {\n readonly beliefId: string;\n readonly updateType: BeliefUpdateType;\n readonly previousState: Record<string, unknown>;\n readonly newState: Record<string, unknown>;\n readonly reason: string;\n readonly evidence?: string;\n}\n\n/** Create a new update record */\nexport function createUpdateRecord(options: UpdateRecordOptions): BeliefUpdate {\n return {\n updateId: sharedGenerateId('update', 8),\n beliefId: options.beliefId,\n updateType: options.updateType,\n previousState: options.previousState,\n newState: options.newState,\n reason: options.reason,\n ...(options.evidence !== undefined ? { evidence: options.evidence } : {}),\n timestamp: new Date(getTimeProvider().now()),\n };\n}\n","/**\n * nexus-agents/context - Belief Memory Recall Operations\n *\n * Query and retrieval operations for Hindsight Belief Memory.\n * Extracted from belief-memory.ts to comply with 400-line limit.\n *\n * @module context/belief-memory-recall\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { MemoryError } from './memory-backend-types.js';\nimport type { Belief, BeliefQuery } from './belief-types.js';\nimport { BeliefQuerySchema } from './belief-types.js';\nimport { sortBeliefs, matchesQueryFilters, intersectSets } from './belief-memory-helpers.js';\n\n// ============================================================================\n// Types for Recall Data Stores\n// ============================================================================\n\n/**\n * Data stores required for recall operations.\n */\nexport interface RecallDataStores {\n readonly beliefs: Map<string, Belief>;\n readonly subjectIndex: Map<string, Set<string>>;\n readonly predicateIndex: Map<string, Set<string>>;\n readonly domainIndex: Map<string, Set<string>>;\n}\n\n// ============================================================================\n// Recall Operations\n// ============================================================================\n\n/**\n * Recall a single belief by ID.\n */\nexport function recallInternal(\n stores: Pick<RecallDataStores, 'beliefs'>,\n beliefId: string\n): Promise<Result<Belief | null, MemoryError>> {\n try {\n return Promise.resolve(ok(stores.beliefs.get(beliefId) ?? null));\n } catch (error) {\n return Promise.resolve(\n err(\n new MemoryError('Failed to recall belief', {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n )\n );\n }\n}\n\n/**\n * Get candidate belief IDs based on indexed fields.\n */\nfunction getCandidateIds(stores: RecallDataStores, query: BeliefQuery): Set<string> | null {\n let ids: Set<string> | null = null;\n\n if (query.subject !== undefined) {\n ids = new Set(stores.subjectIndex.get(query.subject) ?? []);\n }\n\n if (query.predicate !== undefined) {\n const pIds = stores.predicateIndex.get(query.predicate) ?? new Set();\n ids = ids ? intersectSets(ids, pIds) : new Set(pIds);\n }\n\n if (query.domain !== undefined) {\n const dIds = stores.domainIndex.get(query.domain) ?? new Set();\n ids = ids ? intersectSets(ids, dIds) : new Set(dIds);\n }\n\n return ids;\n}\n\n/**\n * Filter candidates by query criteria.\n */\nfunction filterCandidates(\n stores: Pick<RecallDataStores, 'beliefs'>,\n candidateIds: Set<string> | null,\n query: BeliefQuery\n): Belief[] {\n const allIds = candidateIds ?? new Set(stores.beliefs.keys());\n const filtered: Belief[] = [];\n\n for (const id of allIds) {\n const b = stores.beliefs.get(id);\n if (b !== undefined && matchesQueryFilters(b, query)) {\n filtered.push(b);\n }\n }\n\n return filtered;\n}\n\n/**\n * Query beliefs with filters, sorting, and pagination.\n */\nexport function queryInternal(\n stores: RecallDataStores,\n query: BeliefQuery\n): Promise<Result<readonly Belief[], MemoryError>> {\n try {\n const validation = BeliefQuerySchema.safeParse(query);\n if (!validation.success) {\n return Promise.resolve(\n err(new MemoryError('Invalid query', { context: { errors: validation.error.issues } }))\n );\n }\n\n const candidateIds = getCandidateIds(stores, query);\n const filtered = filterCandidates(stores, candidateIds, query);\n const sorted = sortBeliefs(filtered, query.orderBy, query.orderDirection);\n const limited = query.limit !== undefined ? sorted.slice(0, query.limit) : sorted;\n\n return Promise.resolve(ok(limited));\n } catch (error) {\n return Promise.resolve(\n err(\n new MemoryError('Failed to query beliefs', {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n )\n );\n }\n}\n\n/**\n * Recall beliefs by subject.\n */\nexport function recallBySubjectInternal(\n stores: RecallDataStores,\n subject: string,\n limit?: number\n): Promise<Result<readonly Belief[], MemoryError>> {\n const q: BeliefQuery = { subject, includeSuperseded: false };\n return limit !== undefined ? queryInternal(stores, { ...q, limit }) : queryInternal(stores, q);\n}\n\n/**\n * Recall current (most recent, non-superseded) belief for subject-predicate.\n */\nexport async function recallCurrentInternal(\n stores: RecallDataStores,\n subject: string,\n predicate: string\n): Promise<Result<Belief | null, MemoryError>> {\n const result = await queryInternal(stores, {\n subject,\n predicate,\n includeSuperseded: false,\n orderBy: 'updatedAt',\n orderDirection: 'desc',\n limit: 1,\n });\n\n if (!result.ok) return result;\n return ok(result.value[0] ?? null);\n}\n\n/**\n * Recall belief history for subject-predicate (including superseded).\n */\nexport function recallHistoryInternal(\n stores: RecallDataStores,\n subject: string,\n predicate: string,\n limit?: number\n): Promise<Result<readonly Belief[], MemoryError>> {\n const q: BeliefQuery = {\n subject,\n predicate,\n includeSuperseded: true,\n orderBy: 'updatedAt',\n orderDirection: 'desc',\n };\n return limit !== undefined ? queryInternal(stores, { ...q, limit }) : queryInternal(stores, q);\n}\n","/**\n * nexus-agents/context - Hindsight Belief Memory Audit\n *\n * Counterfactual reasoning, audit, and statistics methods.\n *\n * @module context/belief-memory-audit\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { ILogger } from '../core/logger.js';\nimport { MemoryError } from './memory-backend-types.js';\nimport type {\n Belief,\n BeliefMemoryStats,\n BeliefUpdate,\n Counterfactual,\n HindsightRecord,\n} from './belief-types.js';\nimport { initializeStatsCounters, buildStatsResult } from './belief-memory-helpers.js';\n// Shared utilities per ADR-0013\nimport { generateId } from '../utils/id-utils.js';\n\n/** Data stores required for audit operations */\nexport interface AuditDataStores {\n readonly beliefs: Map<string, Belief>;\n readonly updates: Map<string, BeliefUpdate[]>;\n readonly counterfactuals: Map<string, Counterfactual>;\n readonly hindsightRecords: Map<string, HindsightRecord[]>;\n readonly logger: ILogger;\n}\n\n/** Create a counterfactual scenario */\nexport function createCounterfactualInternal(\n stores: AuditDataStores,\n hypothesis: string,\n taskContext?: string\n): Promise<Result<Counterfactual, MemoryError>> {\n try {\n const affectedBeliefs = findAffectedBeliefs(stores.beliefs, hypothesis);\n const baseCounterfactual = {\n counterfactualId: generateId('cf'),\n hypothesis,\n affectedBeliefs: affectedBeliefs.map((b) => b.beliefId),\n predictedOutcomes: predictOutcomes(hypothesis, affectedBeliefs),\n validated: false as const,\n createdAt: new Date(getTimeProvider().now()),\n };\n const counterfactual: Counterfactual =\n taskContext !== undefined ? { ...baseCounterfactual, taskContext } : baseCounterfactual;\n stores.counterfactuals.set(counterfactual.counterfactualId, counterfactual);\n stores.logger.debug('Counterfactual created', {\n counterfactualId: counterfactual.counterfactualId,\n });\n return Promise.resolve(ok(counterfactual));\n } catch (error) {\n const causeError = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to create counterfactual', { cause: causeError }))\n );\n }\n}\n\n/** Validate a counterfactual with actual outcomes */\nexport function validateCounterfactualInternal(\n stores: AuditDataStores,\n counterfactualId: string,\n actualOutcomes: readonly string[]\n): Promise<Result<Counterfactual, MemoryError>> {\n try {\n const existing = stores.counterfactuals.get(counterfactualId);\n if (existing === undefined) {\n return Promise.resolve(\n err(new MemoryError('Counterfactual not found', { context: { counterfactualId } }))\n );\n }\n const validated: Counterfactual = { ...existing, actualOutcomes, validated: true };\n stores.counterfactuals.set(counterfactualId, validated);\n stores.logger.debug('Counterfactual validated', { counterfactualId });\n return Promise.resolve(ok(validated));\n } catch (error) {\n const causeError = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to validate counterfactual', { cause: causeError }))\n );\n }\n}\n\n/** Get counterfactuals for a task context */\nexport function getCounterfactualsInternal(\n stores: AuditDataStores,\n taskContext: string\n): Promise<Result<readonly Counterfactual[], MemoryError>> {\n try {\n const results: Counterfactual[] = [];\n for (const cf of stores.counterfactuals.values()) {\n if (cf.taskContext === taskContext) results.push(cf);\n }\n return Promise.resolve(ok(results));\n } catch (error) {\n const causeError = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to get counterfactuals', { cause: causeError }))\n );\n }\n}\n\n/** Get update history for a belief */\nexport function getUpdateHistoryInternal(\n stores: AuditDataStores,\n beliefId: string\n): Promise<Result<readonly BeliefUpdate[], MemoryError>> {\n try {\n return Promise.resolve(ok(stores.updates.get(beliefId) ?? []));\n } catch (error) {\n const causeError = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to get update history', { cause: causeError }))\n );\n }\n}\n\n/** Get hindsight records for a task */\nexport function getHindsightRecordsInternal(\n stores: AuditDataStores,\n taskId: string\n): Promise<Result<readonly HindsightRecord[], MemoryError>> {\n try {\n return Promise.resolve(ok(stores.hindsightRecords.get(taskId) ?? []));\n } catch (error) {\n const causeError = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to get hindsight records', { cause: causeError }))\n );\n }\n}\n\n/** Compute belief memory statistics */\nexport function computeStatsInternal(stores: AuditDataStores): BeliefMemoryStats {\n const { beliefsByConfidence, beliefsBySource } = initializeStatsCounters();\n let activeBeliefs = 0,\n supersededBeliefs = 0;\n let oldestBelief: Date | undefined, newestBelief: Date | undefined;\n\n for (const belief of stores.beliefs.values()) {\n beliefsByConfidence[belief.confidence]++;\n beliefsBySource[belief.sourceType]++;\n if (belief.superseded) {\n supersededBeliefs++;\n } else {\n activeBeliefs++;\n }\n if (oldestBelief === undefined || belief.createdAt < oldestBelief)\n oldestBelief = belief.createdAt;\n if (newestBelief === undefined || belief.createdAt > newestBelief)\n newestBelief = belief.createdAt;\n }\n\n let totalUpdates = 0,\n totalHindsightRecords = 0;\n for (const history of stores.updates.values()) totalUpdates += history.length;\n for (const records of stores.hindsightRecords.values()) totalHindsightRecords += records.length;\n\n return buildStatsResult(\n {\n totalBeliefs: stores.beliefs.size,\n activeBeliefs,\n supersededBeliefs,\n beliefsByConfidence,\n beliefsBySource,\n totalUpdates,\n totalCounterfactuals: stores.counterfactuals.size,\n totalHindsightRecords,\n },\n oldestBelief,\n newestBelief\n );\n}\n\n/** Find beliefs affected by a hypothesis */\nfunction findAffectedBeliefs(beliefs: Map<string, Belief>, hypothesis: string): Belief[] {\n const keywords = hypothesis.toLowerCase().split(/\\s+/);\n const affected: Belief[] = [];\n for (const belief of beliefs.values()) {\n if (belief.superseded) continue;\n const beliefText = `${belief.subject} ${belief.predicate} ${belief.object}`.toLowerCase();\n if (keywords.some((k) => beliefText.includes(k))) affected.push(belief);\n }\n return affected;\n}\n\n/** Predict outcomes for a hypothesis */\nfunction predictOutcomes(hypothesis: string, affectedBeliefs: Belief[]): string[] {\n return affectedBeliefs.map(\n (b) => `If \"${hypothesis}\", then ${b.subject} ${b.predicate} may change`\n );\n}\n","/**\n * nexus-agents/context - Belief Memory Reflect Operations\n *\n * Extracted reflect operations for HindsightBeliefMemory to comply with 400-line limit.\n *\n * @module context/belief-memory-reflect\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { ILogger } from '../core/logger.js';\nimport { MemoryError } from './memory-backend-types.js';\nimport type { Belief, BeliefUpdate, HindsightRecord } from './belief-types.js';\nimport {\n BeliefSourceType as BeliefSourceTypeEnum,\n BeliefUpdateType as BeliefUpdateTypeEnum,\n} from './belief-types.js';\nimport { strengthenConfidence, weakenConfidence } from './belief-memory-helpers.js';\n\n/**\n * Options for revising a belief.\n */\nexport interface ReviseOptions {\n readonly beliefId: string;\n readonly updates: Partial<Pick<Belief, 'object' | 'confidence' | 'metadata'>>;\n readonly reason: string;\n}\n\n/**\n * Options for superseding a belief.\n */\nexport interface SupersedeOptions {\n readonly beliefId: string;\n readonly newBelief: Omit<\n Belief,\n 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'\n >;\n readonly reason: string;\n}\n\n/**\n * Internal stores used by reflect operations.\n */\nexport interface ReflectDataStores {\n readonly beliefs: Map<string, Belief>;\n readonly updates: Map<string, BeliefUpdate[]>;\n readonly hindsightRecords: Map<string, HindsightRecord[]>;\n readonly logger: ILogger;\n readonly recordUpdate: (opts: RecordUpdateOptions) => void;\n}\n\n/**\n * Options for recording an update.\n */\nexport interface RecordUpdateOptions {\n readonly beliefId: string;\n readonly updateType: BeliefUpdate['updateType'];\n readonly previousState: Record<string, unknown>;\n readonly newState: Record<string, unknown>;\n readonly reason: string;\n readonly evidence?: string;\n}\n\n/**\n * Revise an existing belief with updates.\n */\nexport function reviseBeliefInternal(\n stores: ReflectDataStores,\n opts: ReviseOptions\n): Promise<Result<Belief, MemoryError>> {\n const { beliefId, updates, reason } = opts;\n try {\n const existing = stores.beliefs.get(beliefId);\n if (existing === undefined) {\n return Promise.resolve(err(new MemoryError('Belief not found', { context: { beliefId } })));\n }\n if (existing.superseded) {\n return Promise.resolve(\n err(new MemoryError('Cannot revise superseded belief', { context: { beliefId } }))\n );\n }\n const now = new Date(getTimeProvider().now());\n const revised: Belief = {\n ...existing,\n ...updates,\n version: existing.version + 1,\n updatedAt: now,\n };\n stores.beliefs.set(beliefId, revised);\n stores.recordUpdate({\n beliefId,\n updateType: BeliefUpdateTypeEnum.REVISE,\n previousState: { object: existing.object, confidence: existing.confidence },\n newState: updates,\n reason,\n });\n stores.logger.debug('Belief revised', { beliefId, reason });\n return Promise.resolve(ok(revised));\n } catch (error) {\n return Promise.resolve(\n err(\n new MemoryError('Failed to revise belief', {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n )\n );\n }\n}\n\n/**\n * Apply hindsight correction to beliefs.\n */\nexport function applyHindsightInternal(\n stores: ReflectDataStores,\n record: HindsightRecord\n): Promise<Result<readonly Belief[], MemoryError>> {\n try {\n const correctedBeliefs: Belief[] = [];\n const now = new Date(getTimeProvider().now());\n\n // Store the hindsight record\n const taskRecords = stores.hindsightRecords.get(record.taskId) ?? [];\n taskRecords.push(record);\n stores.hindsightRecords.set(record.taskId, taskRecords);\n\n // Apply corrections to all affected beliefs\n for (const beliefId of record.correctedBeliefs) {\n const belief = stores.beliefs.get(beliefId);\n if (belief === undefined || belief.superseded) continue;\n\n const corrected: Belief = {\n ...belief,\n confidence: weakenConfidence(belief.confidence),\n sourceType: BeliefSourceTypeEnum.HINDSIGHT,\n version: belief.version + 1,\n updatedAt: now,\n };\n stores.beliefs.set(beliefId, corrected);\n correctedBeliefs.push(corrected);\n\n stores.recordUpdate({\n beliefId,\n updateType: BeliefUpdateTypeEnum.CORRECT,\n previousState: { confidence: belief.confidence },\n newState: { confidence: corrected.confidence },\n reason: `Hindsight correction: expected \"${record.expectedOutcome}\", got \"${record.actualOutcome}\"`,\n evidence: record.hindsightId,\n });\n }\n\n stores.logger.info('Hindsight applied', {\n hindsightId: record.hindsightId,\n correctedCount: correctedBeliefs.length,\n });\n return Promise.resolve(ok(correctedBeliefs));\n } catch (error) {\n return Promise.resolve(\n err(\n new MemoryError('Failed to apply hindsight', {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n )\n );\n }\n}\n\n/**\n * Adjust confidence of a belief (reinforce or weaken).\n */\nexport function adjustConfidenceInternal(\n stores: ReflectDataStores,\n beliefId: string,\n evidence: string,\n direction: 'reinforce' | 'weaken'\n): Promise<Result<Belief, MemoryError>> {\n try {\n const existing = stores.beliefs.get(beliefId);\n if (existing === undefined) {\n return Promise.resolve(err(new MemoryError('Belief not found', { context: { beliefId } })));\n }\n if (existing.superseded) {\n return Promise.resolve(\n err(new MemoryError(`Cannot ${direction} superseded belief`, { context: { beliefId } }))\n );\n }\n\n const now = new Date(getTimeProvider().now());\n const newConfidence =\n direction === 'reinforce'\n ? strengthenConfidence(existing.confidence)\n : weakenConfidence(existing.confidence);\n\n const updated: Belief = {\n ...existing,\n confidence: newConfidence,\n version: existing.version + 1,\n updatedAt: now,\n };\n stores.beliefs.set(beliefId, updated);\n\n const updateType =\n direction === 'reinforce' ? BeliefUpdateTypeEnum.REINFORCE : BeliefUpdateTypeEnum.WEAKEN;\n stores.recordUpdate({\n beliefId,\n updateType,\n previousState: { confidence: existing.confidence },\n newState: { confidence: newConfidence },\n reason: direction === 'reinforce' ? 'Corroborating evidence' : 'Contradicting evidence',\n evidence,\n });\n\n stores.logger.debug(`Belief ${direction}d`, { beliefId, newConfidence });\n return Promise.resolve(ok(updated));\n } catch (error) {\n return Promise.resolve(\n err(\n new MemoryError(`Failed to ${direction} belief`, {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n )\n );\n }\n}\n\n/**\n * Prune superseded beliefs older than the given date.\n */\nexport function pruneSupersededInternal(\n stores: ReflectDataStores & {\n readonly subjectIndex: Map<string, Set<string>>;\n readonly predicateIndex: Map<string, Set<string>>;\n readonly domainIndex: Map<string, Set<string>>;\n },\n olderThan: Date\n): Promise<Result<number, MemoryError>> {\n try {\n let pruned = 0;\n const cutoff = olderThan.getTime();\n\n for (const [id, belief] of stores.beliefs.entries()) {\n if (belief.superseded && belief.updatedAt.getTime() < cutoff) {\n stores.beliefs.delete(id);\n // Remove from indices\n stores.subjectIndex.get(belief.subject)?.delete(belief.beliefId);\n stores.predicateIndex.get(belief.predicate)?.delete(belief.beliefId);\n if (belief.domain !== undefined) {\n stores.domainIndex.get(belief.domain)?.delete(belief.beliefId);\n }\n stores.updates.delete(id);\n pruned++;\n }\n }\n\n stores.logger.info('Pruned superseded beliefs', { pruned, olderThan: olderThan.toISOString() });\n return Promise.resolve(ok(pruned));\n } catch (error) {\n return Promise.resolve(\n err(\n new MemoryError('Failed to prune superseded beliefs', {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n )\n );\n }\n}\n","/* eslint-disable max-lines */\n/**\n * nexus-agents/context - Hindsight Belief Memory\n *\n * Implements the Hindsight Belief Memory layer for reasoning agents.\n * Core operations: retain, recall, and reflect.\n *\n * @module context/belief-memory\n * (Source: Issue #336, arXiv:2512.12818 - Hindsight Belief Memory)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { getTimeProvider, formatZodError } from '../core/index.js';\nimport type { ILogger } from '../core/logger.js';\nimport { createLogger } from '../core/logger.js';\nimport { MemoryError } from './memory-backend-types.js';\nimport type {\n Belief,\n BeliefMemoryConfig,\n BeliefMemoryStats,\n BeliefQuery,\n BeliefUpdate,\n Counterfactual,\n HindsightRecord,\n IHindsightBeliefMemory,\n} from './belief-types.js';\nimport {\n BeliefMemoryConfigSchema,\n BeliefSchema,\n BeliefUpdateType as BeliefUpdateTypeEnum,\n DEFAULT_BELIEF_CONFIG,\n} from './belief-types.js';\nimport { createUpdateRecord } from './belief-memory-helpers.js';\n// Shared utilities per ADR-0013\nimport { generateId } from '../utils/id-utils.js';\nimport {\n recallInternal,\n queryInternal,\n recallBySubjectInternal,\n recallCurrentInternal,\n recallHistoryInternal,\n type RecallDataStores,\n} from './belief-memory-recall.js';\nimport {\n createCounterfactualInternal,\n validateCounterfactualInternal,\n getCounterfactualsInternal,\n getUpdateHistoryInternal,\n getHindsightRecordsInternal,\n computeStatsInternal,\n type AuditDataStores,\n} from './belief-memory-audit.js';\nimport {\n reviseBeliefInternal,\n applyHindsightInternal,\n adjustConfidenceInternal,\n pruneSupersededInternal,\n type ReflectDataStores,\n} from './belief-memory-reflect.js';\nimport type { BeliefMemoryData, HydratedBeliefData } from './belief-memory-persistence.js';\n\n/**\n * In-memory implementation of Hindsight Belief Memory.\n */\nexport class HindsightBeliefMemory implements IHindsightBeliefMemory {\n private readonly beliefs: Map<string, Belief> = new Map();\n private readonly updates: Map<string, BeliefUpdate[]> = new Map();\n private readonly counterfactuals: Map<string, Counterfactual> = new Map();\n private readonly hindsightRecords: Map<string, HindsightRecord[]> = new Map();\n private readonly subjectIndex: Map<string, Set<string>> = new Map();\n private readonly predicateIndex: Map<string, Set<string>> = new Map();\n private readonly domainIndex: Map<string, Set<string>> = new Map();\n private readonly config: Required<BeliefMemoryConfig>;\n private readonly logger: ILogger;\n\n constructor(config?: BeliefMemoryConfig, logger?: ILogger) {\n const validation = BeliefMemoryConfigSchema.safeParse(config ?? {});\n if (!validation.success) {\n throw new MemoryError(`Invalid BeliefMemoryConfig: ${formatZodError(validation.error)}`);\n }\n this.config = { ...DEFAULT_BELIEF_CONFIG, ...config };\n this.logger = logger ?? createLogger({ component: 'HindsightBeliefMemory' });\n this.logger.info('HindsightBeliefMemory initialized', { config: this.config });\n }\n\n private get auditStores(): AuditDataStores {\n return {\n beliefs: this.beliefs,\n updates: this.updates,\n counterfactuals: this.counterfactuals,\n hindsightRecords: this.hindsightRecords,\n logger: this.logger,\n };\n }\n\n private get reflectStores(): ReflectDataStores {\n return {\n beliefs: this.beliefs,\n updates: this.updates,\n hindsightRecords: this.hindsightRecords,\n logger: this.logger,\n recordUpdate: (opts) => {\n this.recordUpdate(opts);\n },\n };\n }\n\n private get recallStores(): RecallDataStores {\n return {\n beliefs: this.beliefs,\n subjectIndex: this.subjectIndex,\n predicateIndex: this.predicateIndex,\n domainIndex: this.domainIndex,\n };\n }\n\n // =========================================================================\n // Retain Operations\n // =========================================================================\n\n retain(\n belief: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>\n ): Promise<Result<Belief, MemoryError>> {\n return this.retainInternal(belief, false);\n }\n\n private retainInternal(\n belief: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>,\n skipDedup: boolean\n ): Promise<Result<Belief, MemoryError>> {\n try {\n const validationError = this.validateBeliefInput(belief);\n if (validationError !== undefined) return Promise.resolve(err(validationError));\n\n const existingBelief = skipDedup\n ? undefined\n : this.findActiveBySubjectPredicate(belief.subject, belief.predicate);\n\n // Content-hash dedup: skip write if identical content already exists (#1455)\n const dedupResult = this.checkDedupMatch(existingBelief, belief);\n if (dedupResult !== undefined) {\n return Promise.resolve(ok(dedupResult));\n }\n\n const version = existingBelief !== undefined ? existingBelief.version + 1 : 1;\n const now = new Date(getTimeProvider().now());\n const newBelief: Belief = {\n ...belief,\n beliefId: generateId('belief'),\n version,\n createdAt: now,\n updatedAt: now,\n superseded: false,\n };\n this.beliefs.set(newBelief.beliefId, newBelief);\n this.indexBelief(newBelief);\n if (existingBelief !== undefined) {\n this.markSuperseded(existingBelief, newBelief.beliefId, now);\n }\n this.recordRetainUpdate(newBelief, existingBelief !== undefined);\n this.evictIfOverCapacity();\n return Promise.resolve(ok(newBelief));\n } catch (error) {\n const causeError = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to retain belief', { cause: causeError }))\n );\n }\n }\n\n async retainBatch(\n beliefs: readonly Omit<\n Belief,\n 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'\n >[]\n ): Promise<Result<readonly Belief[], MemoryError>> {\n const results: Belief[] = [];\n for (const belief of beliefs) {\n const result = await this.retainInternal(belief, false);\n if (!result.ok) return result;\n results.push(result.value);\n }\n return ok(results);\n }\n\n // =========================================================================\n // Recall Operations (delegated to belief-memory-recall.ts)\n // =========================================================================\n\n recall(beliefId: string): Promise<Result<Belief | null, MemoryError>> {\n return recallInternal(this.recallStores, beliefId);\n }\n\n query(query: BeliefQuery): Promise<Result<readonly Belief[], MemoryError>> {\n return queryInternal(this.recallStores, query);\n }\n\n recallBySubject(\n subject: string,\n limit?: number\n ): Promise<Result<readonly Belief[], MemoryError>> {\n return recallBySubjectInternal(this.recallStores, subject, limit);\n }\n\n recallCurrent(subject: string, predicate: string): Promise<Result<Belief | null, MemoryError>> {\n return recallCurrentInternal(this.recallStores, subject, predicate);\n }\n\n recallHistory(\n subject: string,\n predicate: string,\n limit?: number\n ): Promise<Result<readonly Belief[], MemoryError>> {\n return recallHistoryInternal(this.recallStores, subject, predicate, limit);\n }\n\n // =========================================================================\n // Reflect Operations (delegated to belief-memory-reflect.ts)\n // =========================================================================\n\n revise(\n beliefId: string,\n updates: Partial<Pick<Belief, 'object' | 'confidence' | 'metadata'>>,\n reason: string\n ): Promise<Result<Belief, MemoryError>> {\n return reviseBeliefInternal(this.reflectStores, { beliefId, updates, reason });\n }\n\n async supersede(\n beliefId: string,\n newBelief: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>,\n reason: string\n ): Promise<Result<Belief, MemoryError>> {\n try {\n const existing = this.beliefs.get(beliefId);\n if (existing === undefined)\n return err(new MemoryError('Belief not found', { context: { beliefId } }));\n const retainResult = await this.retainInternal(newBelief, true);\n if (!retainResult.ok) return retainResult;\n const now = new Date(getTimeProvider().now());\n const superseded: Belief = {\n ...existing,\n superseded: true,\n supersededBy: retainResult.value.beliefId,\n updatedAt: now,\n };\n this.beliefs.set(beliefId, superseded);\n this.recordUpdate({\n beliefId,\n updateType: BeliefUpdateTypeEnum.SUPERSEDE,\n previousState: { superseded: false },\n newState: { superseded: true, supersededBy: retainResult.value.beliefId },\n reason,\n });\n this.logger.debug('Belief superseded', {\n oldBeliefId: beliefId,\n newBeliefId: retainResult.value.beliefId,\n });\n return ok(retainResult.value);\n } catch (error) {\n return err(\n new MemoryError('Failed to supersede belief', {\n cause: error instanceof Error ? error : new Error(String(error)),\n })\n );\n }\n }\n\n applyHindsight(record: HindsightRecord): Promise<Result<readonly Belief[], MemoryError>> {\n return applyHindsightInternal(this.reflectStores, record);\n }\n\n reinforce(beliefId: string, evidence: string): Promise<Result<Belief, MemoryError>> {\n return adjustConfidenceInternal(this.reflectStores, beliefId, evidence, 'reinforce');\n }\n\n weaken(beliefId: string, evidence: string): Promise<Result<Belief, MemoryError>> {\n return adjustConfidenceInternal(this.reflectStores, beliefId, evidence, 'weaken');\n }\n\n // =========================================================================\n // Counterfactual, Audit, and Stats (delegated to belief-memory-audit.ts)\n // =========================================================================\n\n createCounterfactual(\n hypothesis: string,\n taskContext?: string\n ): Promise<Result<Counterfactual, MemoryError>> {\n return createCounterfactualInternal(this.auditStores, hypothesis, taskContext);\n }\n\n validateCounterfactual(\n counterfactualId: string,\n actualOutcomes: readonly string[]\n ): Promise<Result<Counterfactual, MemoryError>> {\n return validateCounterfactualInternal(this.auditStores, counterfactualId, actualOutcomes);\n }\n\n getCounterfactuals(taskContext: string): Promise<Result<readonly Counterfactual[], MemoryError>> {\n return getCounterfactualsInternal(this.auditStores, taskContext);\n }\n\n getUpdateHistory(beliefId: string): Promise<Result<readonly BeliefUpdate[], MemoryError>> {\n return getUpdateHistoryInternal(this.auditStores, beliefId);\n }\n\n getHindsightRecords(taskId: string): Promise<Result<readonly HindsightRecord[], MemoryError>> {\n return getHindsightRecordsInternal(this.auditStores, taskId);\n }\n\n getStats(): Promise<Result<BeliefMemoryStats, MemoryError>> {\n try {\n return Promise.resolve(ok(computeStatsInternal(this.auditStores)));\n } catch (e) {\n return Promise.resolve(\n err(\n new MemoryError('Failed to get stats', {\n cause: e instanceof Error ? e : new Error(String(e)),\n })\n )\n );\n }\n }\n\n pruneSuperseded(olderThan: Date): Promise<Result<number, MemoryError>> {\n return pruneSupersededInternal(\n {\n ...this.reflectStores,\n subjectIndex: this.subjectIndex,\n predicateIndex: this.predicateIndex,\n domainIndex: this.domainIndex,\n },\n olderThan\n );\n }\n\n // =========================================================================\n // Persistence (Issue #714 Phase 3)\n // =========================================================================\n\n /** Export all internal data for disk serialization. */\n exportData(): BeliefMemoryData {\n return {\n beliefs: this.beliefs,\n updates: this.updates,\n counterfactuals: this.counterfactuals,\n hindsightRecords: this.hindsightRecords,\n };\n }\n\n /** Hydrate from a previously loaded snapshot. Rebuilds indexes. */\n hydrate(data: HydratedBeliefData): void {\n this.beliefs.clear();\n this.updates.clear();\n this.counterfactuals.clear();\n this.hindsightRecords.clear();\n this.subjectIndex.clear();\n this.predicateIndex.clear();\n this.domainIndex.clear();\n\n for (const [id, belief] of data.beliefs) {\n this.beliefs.set(id, belief);\n this.indexBelief(belief);\n }\n for (const [id, records] of data.updates) {\n this.updates.set(id, records);\n }\n for (const [id, cf] of data.counterfactuals) {\n this.counterfactuals.set(id, cf);\n }\n for (const [id, records] of data.hindsightRecords) {\n this.hindsightRecords.set(id, records);\n }\n this.logger.info('BeliefMemory hydrated from snapshot', {\n beliefs: this.beliefs.size,\n updates: this.updates.size,\n counterfactuals: this.counterfactuals.size,\n hindsightRecords: this.hindsightRecords.size,\n });\n }\n\n // =========================================================================\n // Private Helpers\n // =========================================================================\n\n /** Validate belief input against schema; returns MemoryError if invalid. */\n private validateBeliefInput(\n belief: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>\n ): MemoryError | undefined {\n const validation = BeliefSchema.omit({\n beliefId: true,\n version: true,\n createdAt: true,\n updatedAt: true,\n superseded: true,\n }).safeParse(belief);\n if (!validation.success) {\n return new MemoryError('Invalid belief data', {\n context: { errors: validation.error.issues },\n });\n }\n return undefined;\n }\n\n /** Return existing belief if content-identical, otherwise undefined (#1455). */\n private checkDedupMatch(\n existing: Belief | undefined,\n incoming: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>\n ): Belief | undefined {\n if (existing === undefined || !this.isIdenticalBelief(existing, incoming)) {\n return undefined;\n }\n this.logger.debug('Skipping duplicate belief retain', {\n beliefId: existing.beliefId,\n subject: incoming.subject,\n predicate: incoming.predicate,\n });\n return existing;\n }\n\n /** Check if an existing belief has identical content to a new belief (#1455). */\n private isIdenticalBelief(\n existing: Belief,\n incoming: Omit<Belief, 'beliefId' | 'version' | 'createdAt' | 'updatedAt' | 'superseded'>\n ): boolean {\n return (\n existing.object === incoming.object &&\n existing.confidence === incoming.confidence &&\n existing.sourceType === incoming.sourceType\n );\n }\n\n /** Find a non-superseded belief matching the given (subject, predicate). */\n private findActiveBySubjectPredicate(subject: string, predicate: string): Belief | undefined {\n const subjectIds = this.subjectIndex.get(subject);\n if (subjectIds === undefined) return undefined;\n for (const id of subjectIds) {\n const belief = this.beliefs.get(id);\n if (belief?.predicate === predicate && !belief.superseded) {\n return belief;\n }\n }\n return undefined;\n }\n\n /** Mark an existing belief as superseded by a newer one. */\n private markSuperseded(existing: Belief, newBeliefId: string, now: Date): void {\n const superseded: Belief = {\n ...existing,\n superseded: true,\n supersededBy: newBeliefId,\n updatedAt: now,\n };\n this.beliefs.set(existing.beliefId, superseded);\n this.recordUpdate({\n beliefId: existing.beliefId,\n updateType: BeliefUpdateTypeEnum.SUPERSEDE,\n previousState: { superseded: false },\n newState: { superseded: true, supersededBy: newBeliefId },\n reason: 'Auto-superseded by retain with same subject+predicate',\n });\n }\n\n /** Record the RETAIN update event for a newly created belief. */\n private recordRetainUpdate(newBelief: Belief, wasSuperseding: boolean): void {\n this.recordUpdate({\n beliefId: newBelief.beliefId,\n updateType: BeliefUpdateTypeEnum.RETAIN,\n previousState: {},\n newState: Object.fromEntries(Object.entries(newBelief)),\n reason: wasSuperseding ? 'Belief creation (superseding existing)' : 'Initial belief creation',\n });\n this.logger.debug('Belief retained', {\n beliefId: newBelief.beliefId,\n subject: newBelief.subject,\n supersededExisting: wasSuperseding,\n });\n }\n\n /** Evicts oldest superseded beliefs when total beliefs exceed maxTotalBeliefs. */\n private evictIfOverCapacity(): void {\n const max = this.config.maxTotalBeliefs;\n if (this.beliefs.size <= max) return;\n\n // Collect superseded beliefs sorted by updatedAt (oldest first)\n const superseded: Belief[] = [];\n for (const belief of this.beliefs.values()) {\n if (belief.superseded) superseded.push(belief);\n }\n superseded.sort((a, b) => a.updatedAt.getTime() - b.updatedAt.getTime());\n\n const toEvict = this.beliefs.size - max;\n const evicted = Math.min(toEvict, superseded.length);\n for (let i = 0; i < evicted; i++) {\n const belief = superseded[i];\n if (belief !== undefined) this.removeBelief(belief.beliefId);\n }\n if (evicted > 0) {\n this.logger.debug('Evicted superseded beliefs for capacity', {\n evicted,\n remaining: this.beliefs.size,\n maxTotalBeliefs: max,\n });\n }\n }\n\n /** Removes a belief and cleans up all indexes. */\n private removeBelief(beliefId: string): void {\n const belief = this.beliefs.get(beliefId);\n if (belief === undefined) return;\n this.beliefs.delete(beliefId);\n this.removeFromIndex(this.subjectIndex, belief.subject, beliefId);\n this.removeFromIndex(this.predicateIndex, belief.predicate, beliefId);\n if (belief.domain !== undefined) {\n this.removeFromIndex(this.domainIndex, belief.domain, beliefId);\n }\n }\n\n private removeFromIndex(index: Map<string, Set<string>>, key: string, id: string): void {\n const set = index.get(key);\n if (set === undefined) return;\n set.delete(id);\n if (set.size === 0) index.delete(key);\n }\n\n private indexBelief(belief: Belief): void {\n const subjectSet = this.subjectIndex.get(belief.subject) ?? new Set();\n subjectSet.add(belief.beliefId);\n this.subjectIndex.set(belief.subject, subjectSet);\n const predicateSet = this.predicateIndex.get(belief.predicate) ?? new Set();\n predicateSet.add(belief.beliefId);\n this.predicateIndex.set(belief.predicate, predicateSet);\n if (belief.domain !== undefined) {\n const domainSet = this.domainIndex.get(belief.domain) ?? new Set();\n domainSet.add(belief.beliefId);\n this.domainIndex.set(belief.domain, domainSet);\n }\n }\n\n private recordUpdate(opts: {\n beliefId: string;\n updateType: BeliefUpdate['updateType'];\n previousState: Record<string, unknown>;\n newState: Record<string, unknown>;\n reason: string;\n evidence?: string;\n }): void {\n const history = this.updates.get(opts.beliefId) ?? [];\n history.push(createUpdateRecord(opts));\n this.updates.set(opts.beliefId, history);\n }\n}\n\n// Re-export types and schemas\nexport type {\n Belief,\n BeliefConfidence,\n BeliefMemoryConfig,\n BeliefMemoryStats,\n BeliefQuery,\n BeliefSourceType,\n BeliefUpdate,\n BeliefUpdateType,\n Counterfactual,\n HindsightRecord,\n IHindsightBeliefMemory,\n} from './belief-types.js';\nexport {\n BeliefConfidence as BeliefConfidenceEnum,\n BeliefMemoryConfigSchema,\n BeliefQuerySchema,\n BeliefSchema,\n BeliefSourceType as BeliefSourceTypeEnum,\n BeliefUpdateType as BeliefUpdateTypeEnum,\n DEFAULT_BELIEF_CONFIG,\n} from './belief-types.js';\n","/**\n * Graph-Based Memory Types\n *\n * Type definitions for graph-structured memory with entity relationships.\n * Implements concepts from arXiv:2308.09687 (MiRIX) for knowledge graphs.\n *\n * @module context/graph-memory-types\n * (Source: Issue #142, arXiv:2308.09687)\n */\n\nimport { z } from 'zod';\nimport type { Result } from '../core/result.js';\nimport type {\n MemoryEntry,\n MemoryMetadata,\n MemoryError,\n IMemoryBackend,\n} from './memory-backend-types.js';\n\n// ============================================================================\n// Relationship Types\n// ============================================================================\n\n/**\n * Types of relationships between memory nodes.\n */\nexport const RelationType = {\n /** General association between memories */\n RELATED_TO: 'related_to',\n /** One memory derived from another */\n DERIVED_FROM: 'derived_from',\n /** Memories that contradict each other */\n CONTRADICTS: 'contradicts',\n /** One memory supersedes another */\n SUPERSEDES: 'supersedes',\n /** Parent-child hierarchy */\n PARENT_OF: 'parent_of',\n /** Memories about the same entity */\n SAME_ENTITY: 'same_entity',\n /** Temporal sequence */\n PRECEDES: 'precedes',\n /** Causal relationship */\n CAUSES: 'causes',\n} as const;\n\nexport type RelationType = (typeof RelationType)[keyof typeof RelationType];\n\n/** Zod schema for RelationType validation. */\nexport const RelationTypeSchema = z.enum([\n 'related_to',\n 'derived_from',\n 'contradicts',\n 'supersedes',\n 'parent_of',\n 'same_entity',\n 'precedes',\n 'causes',\n]);\n\n// ============================================================================\n// Graph Structures\n// ============================================================================\n\n/**\n * A relationship edge between two memory nodes.\n */\nexport interface GraphEdge {\n /** Source node key */\n readonly from: string;\n /** Target node key */\n readonly to: string;\n /** Type of relationship */\n readonly type: RelationType;\n /** Relationship strength/weight (0-1) */\n readonly weight: number;\n /** When the relationship was created */\n readonly createdAt: Date;\n /** Optional metadata about the relationship */\n readonly metadata?: Record<string, unknown>;\n}\n\n/** Zod schema for GraphEdge validation. */\nexport const GraphEdgeSchema = z.object({\n from: z.string().min(1),\n to: z.string().min(1),\n type: RelationTypeSchema,\n weight: z.number().min(0).max(1),\n createdAt: z.date(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n});\n\n/**\n * A memory node with its relationships.\n */\nexport interface GraphNode extends MemoryEntry {\n /** Outgoing edges from this node */\n readonly outEdges: readonly GraphEdge[];\n /** Incoming edges to this node */\n readonly inEdges: readonly GraphEdge[];\n}\n\n/**\n * Options for graph traversal operations.\n */\nexport interface TraversalOptions {\n /** Maximum depth to traverse (default: 2) */\n readonly maxDepth?: number;\n /** Filter by relationship types */\n readonly relationTypes?: readonly RelationType[];\n /** Minimum edge weight to follow (default: 0) */\n readonly minWeight?: number;\n /** Maximum number of results */\n readonly limit?: number;\n /** Whether to include the starting node */\n readonly includeStart?: boolean;\n /** Direction: outgoing, incoming, or both */\n readonly direction?: 'outgoing' | 'incoming' | 'both';\n}\n\n/** Zod schema for TraversalOptions validation. */\nexport const TraversalOptionsSchema = z.object({\n maxDepth: z.number().int().positive().optional(),\n relationTypes: z.array(RelationTypeSchema).optional(),\n minWeight: z.number().min(0).max(1).optional(),\n limit: z.number().int().positive().optional(),\n includeStart: z.boolean().optional(),\n direction: z.enum(['outgoing', 'incoming', 'both']).optional(),\n});\n\n/**\n * Result of a graph traversal with path information.\n */\nexport interface TraversalResult {\n /** The memory entry found */\n readonly entry: MemoryEntry;\n /** Distance from the starting node */\n readonly depth: number;\n /** Path of keys from start to this node */\n readonly path: readonly string[];\n /** Edge that led to this node (if not start) */\n readonly edge?: GraphEdge;\n}\n\n/**\n * Options for adding a relationship.\n */\nexport interface AddRelationshipOptions {\n /** Relationship strength (default: 1.0) */\n readonly weight?: number;\n /** Optional metadata about the relationship */\n readonly metadata?: Record<string, unknown>;\n}\n\n// ============================================================================\n// Graph Memory Interface\n// ============================================================================\n\n/**\n * Extended memory backend with graph-based relationship operations.\n */\nexport interface IGraphMemory extends IMemoryBackend {\n /**\n * Add a relationship between two memory entries.\n * @param from - Source memory key\n * @param to - Target memory key\n * @param type - Type of relationship\n * @param opts - Additional options\n */\n addRelationship(\n from: string,\n to: string,\n type: RelationType,\n opts?: AddRelationshipOptions\n ): Promise<Result<void, MemoryError>>;\n\n /**\n * Remove a relationship between two memory entries.\n * @param from - Source memory key\n * @param to - Target memory key\n * @param type - Optional: specific type to remove (removes all if not specified)\n */\n removeRelationship(\n from: string,\n to: string,\n type?: RelationType\n ): Promise<Result<void, MemoryError>>;\n\n /**\n * Get all relationships for a memory entry.\n * @param key - Memory key\n * @param direction - Which edges to retrieve\n */\n getRelationships(\n key: string,\n direction?: 'outgoing' | 'incoming' | 'both'\n ): Promise<Result<GraphEdge[], MemoryError>>;\n\n /**\n * Traverse the graph from a starting node.\n * @param startKey - Starting memory key\n * @param opts - Traversal options\n */\n traverse(\n startKey: string,\n opts?: TraversalOptions\n ): Promise<Result<TraversalResult[], MemoryError>>;\n\n /**\n * Find the shortest path between two memories.\n * @param from - Source memory key\n * @param to - Target memory key\n * @param opts - Traversal constraints\n */\n findPath(\n from: string,\n to: string,\n opts?: Pick<TraversalOptions, 'relationTypes' | 'minWeight'>\n ): Promise<Result<string[] | null, MemoryError>>;\n\n /**\n * Get all memories connected to a key within a certain depth.\n * @param key - Central memory key\n * @param depth - Maximum hops (default: 1)\n */\n getNeighbors(key: string, depth?: number): Promise<Result<MemoryEntry[], MemoryError>>;\n\n /**\n * Store a memory with automatic relationship inference.\n * @param key - Memory key\n * @param value - Memory value\n * @param metadata - Memory metadata\n * @param relatedTo - Keys of related memories\n */\n storeWithRelations(\n key: string,\n value: unknown,\n metadata: MemoryMetadata,\n relatedTo?: readonly string[]\n ): Promise<Result<void, MemoryError>>;\n}\n\n// ============================================================================\n// SQLite Row Types\n// ============================================================================\n\n/**\n * Row structure for the graph_edges table.\n */\nexport interface GraphEdgeRow {\n from_key: string;\n to_key: string;\n relation_type: string;\n weight: number;\n created_at: number;\n metadata: string | null;\n}\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\n/**\n * Configuration for GraphMemoryBackend.\n */\nexport interface GraphMemoryConfig {\n /** Path to SQLite database file */\n readonly dbPath: string;\n /** Directory for Markdown exports */\n readonly markdownDir: string;\n /** Default traversal depth (default: 2) */\n readonly defaultTraversalDepth?: number;\n /** Maximum allowed traversal depth (default: 5) */\n readonly maxTraversalDepth?: number;\n /** Whether to auto-expire TTL entries (default: true) */\n readonly autoExpire?: boolean;\n}\n\n/** Zod schema for GraphMemoryConfig validation. */\nexport const GraphMemoryConfigSchema = z.object({\n dbPath: z.string().min(1),\n markdownDir: z.string().min(1),\n defaultTraversalDepth: z.number().int().positive().optional(),\n maxTraversalDepth: z.number().int().positive().optional(),\n autoExpire: z.boolean().optional(),\n});\n\n// ============================================================================\n// Default Configuration\n// ============================================================================\n\n/** Default configuration values. */\nexport const DEFAULT_GRAPH_MEMORY_CONFIG = {\n defaultTraversalDepth: 2,\n maxTraversalDepth: 5,\n autoExpire: true,\n} as const;\n","/**\n * Graph Memory Helpers\n *\n * Helper functions for graph memory operations including SQL queries\n * and traversal algorithms.\n *\n * @module context/graph-memory-helpers\n * (Source: Issue #142, arXiv:2308.09687)\n */\n\nimport type {\n GraphEdge,\n GraphEdgeRow,\n TraversalOptions,\n TraversalResult,\n RelationType,\n} from './graph-memory-types.js';\nimport { DEFAULT_GRAPH_MEMORY_CONFIG } from './graph-memory-types.js';\nimport type { MemoryEntry, ISQLiteDatabase } from './memory-backend-types.js';\n// Shared utilities per ADR-0013\nimport { getMemoryEntry as sharedGetMemoryEntry } from '../utils/memory-db-utils.js';\n\n// ============================================================================\n// SQL Schema\n// ============================================================================\n\n/** SQL to create the graph_edges table. */\nexport const CREATE_EDGES_TABLE_SQL = `\n CREATE TABLE IF NOT EXISTS graph_edges (\n from_key TEXT NOT NULL,\n to_key TEXT NOT NULL,\n relation_type TEXT NOT NULL,\n weight REAL NOT NULL DEFAULT 1.0,\n created_at INTEGER NOT NULL,\n metadata TEXT,\n PRIMARY KEY (from_key, to_key, relation_type),\n FOREIGN KEY (from_key) REFERENCES memories(key) ON DELETE CASCADE,\n FOREIGN KEY (to_key) REFERENCES memories(key) ON DELETE CASCADE\n )\n`;\n\n/** SQL to create index on from_key. */\nexport const CREATE_FROM_INDEX_SQL = `\n CREATE INDEX IF NOT EXISTS idx_edges_from ON graph_edges(from_key)\n`;\n\n/** SQL to create index on to_key. */\nexport const CREATE_TO_INDEX_SQL = `\n CREATE INDEX IF NOT EXISTS idx_edges_to ON graph_edges(to_key)\n`;\n\n// ============================================================================\n// Row Conversion\n// ============================================================================\n\n/** Convert a database row to a GraphEdge. */\nexport function rowToEdge(row: GraphEdgeRow): GraphEdge {\n const base = {\n from: row.from_key,\n to: row.to_key,\n type: row.relation_type as RelationType,\n weight: row.weight,\n createdAt: new Date(row.created_at),\n };\n if (row.metadata !== null) {\n try {\n return { ...base, metadata: JSON.parse(row.metadata) as Record<string, unknown> };\n } catch {\n return { ...base, metadata: {} };\n }\n }\n return base;\n}\n\n// ============================================================================\n// Traversal Options\n// ============================================================================\n\n/** Resolved traversal options with defaults applied. */\nexport interface ResolvedTraversalOptions {\n readonly maxDepth: number;\n readonly relationTypes: readonly RelationType[] | undefined;\n readonly minWeight: number;\n readonly limit: number;\n readonly includeStart: boolean;\n readonly direction: 'outgoing' | 'incoming' | 'both';\n}\n\n/** Default values for traversal options. */\nconst TRAVERSAL_DEFAULTS: Omit<ResolvedTraversalOptions, 'relationTypes'> = {\n maxDepth: DEFAULT_GRAPH_MEMORY_CONFIG.defaultTraversalDepth,\n minWeight: 0,\n limit: 100,\n includeStart: false,\n direction: 'both',\n};\n\n/** Resolve traversal options with defaults. */\nexport function resolveTraversalOptions(opts?: TraversalOptions): ResolvedTraversalOptions {\n if (opts === undefined) return { ...TRAVERSAL_DEFAULTS, relationTypes: undefined };\n return {\n maxDepth: opts.maxDepth ?? TRAVERSAL_DEFAULTS.maxDepth,\n relationTypes: opts.relationTypes,\n minWeight: opts.minWeight ?? TRAVERSAL_DEFAULTS.minWeight,\n limit: opts.limit ?? TRAVERSAL_DEFAULTS.limit,\n includeStart: opts.includeStart ?? TRAVERSAL_DEFAULTS.includeStart,\n direction: opts.direction ?? TRAVERSAL_DEFAULTS.direction,\n };\n}\n\n// ============================================================================\n// Edge Queries\n// ============================================================================\n\n/** Query configuration for edge retrieval. */\nexport interface EdgeQueryConfig {\n readonly db: ISQLiteDatabase;\n readonly key: string;\n readonly direction: 'outgoing' | 'incoming' | 'both';\n}\n\n/** Get edges for a key based on direction. */\nexport function getEdgesForKey(config: EdgeQueryConfig): GraphEdge[] {\n const { db, key, direction } = config;\n const edges: GraphEdge[] = [];\n\n if (direction === 'outgoing' || direction === 'both') {\n const stmt = db.prepare<GraphEdgeRow>('SELECT * FROM graph_edges WHERE from_key = ?');\n const rows = stmt.all(key);\n edges.push(...rows.map(rowToEdge));\n }\n\n if (direction === 'incoming' || direction === 'both') {\n const stmt = db.prepare<GraphEdgeRow>('SELECT * FROM graph_edges WHERE to_key = ?');\n const rows = stmt.all(key);\n edges.push(...rows.map(rowToEdge));\n }\n\n return edges;\n}\n\n// ============================================================================\n// BFS Traversal\n// ============================================================================\n\n/** State for BFS traversal. */\ninterface BFSState {\n readonly visited: Set<string>;\n readonly queue: Array<{ key: string; depth: number; path: string[]; edge?: GraphEdge }>;\n readonly results: TraversalResult[];\n}\n\n/** Configuration for BFS traversal. */\nexport interface BFSConfig {\n readonly db: ISQLiteDatabase;\n readonly startKey: string;\n readonly opts: ResolvedTraversalOptions;\n}\n\n/** Check if an edge passes the filter criteria. */\nexport function edgePassesFilter(edge: GraphEdge, opts: ResolvedTraversalOptions): boolean {\n if (edge.weight < opts.minWeight) return false;\n if (opts.relationTypes !== undefined && !opts.relationTypes.includes(edge.type)) return false;\n return true;\n}\n\n/** Get next keys to visit from current edges. */\nexport function getNextKeys(\n currentKey: string,\n edges: GraphEdge[],\n opts: ResolvedTraversalOptions\n): Array<{ key: string; edge: GraphEdge }> {\n const next: Array<{ key: string; edge: GraphEdge }> = [];\n\n for (const edge of edges) {\n if (!edgePassesFilter(edge, opts)) continue;\n\n if (opts.direction !== 'incoming' && edge.from === currentKey) {\n next.push({ key: edge.to, edge });\n }\n if (opts.direction !== 'outgoing' && edge.to === currentKey) {\n next.push({ key: edge.from, edge });\n }\n }\n\n return next;\n}\n\n/** Build a TraversalResult, conditionally including the edge if present. */\nfunction buildTraversalResult(\n entry: MemoryEntry,\n depth: number,\n path: readonly string[],\n edge: GraphEdge | undefined\n): TraversalResult {\n if (edge !== undefined) {\n return { entry, depth, path, edge };\n }\n return { entry, depth, path };\n}\n\n/** Perform BFS traversal. */\nexport function bfsTraverse(config: BFSConfig): TraversalResult[] {\n const { db, startKey, opts } = config;\n\n const state: BFSState = {\n visited: new Set([startKey]),\n queue: [{ key: startKey, depth: 0, path: [startKey] }],\n results: [],\n };\n\n while (state.queue.length > 0 && state.results.length < opts.limit) {\n const current = state.queue.shift();\n if (current === undefined) break;\n\n if (current.depth > 0 || opts.includeStart) {\n const entry = sharedGetMemoryEntry(db, current.key);\n if (entry !== undefined) {\n state.results.push(buildTraversalResult(entry, current.depth, current.path, current.edge));\n }\n }\n\n if (current.depth >= opts.maxDepth) continue;\n\n const edges = getEdgesForKey({ db, key: current.key, direction: opts.direction });\n const nextKeys = getNextKeys(current.key, edges, opts);\n\n for (const { key: nextKey, edge } of nextKeys) {\n if (!state.visited.has(nextKey)) {\n state.visited.add(nextKey);\n state.queue.push({\n key: nextKey,\n depth: current.depth + 1,\n path: [...current.path, nextKey],\n edge,\n });\n }\n }\n }\n\n return state.results;\n}\n\n// ============================================================================\n// Path Finding (BFS-based shortest path)\n// ============================================================================\n\n/** Find shortest path between two keys. */\nexport function findShortestPath(\n db: ISQLiteDatabase,\n from: string,\n to: string,\n opts: Pick<ResolvedTraversalOptions, 'relationTypes' | 'minWeight'>\n): string[] | null {\n const visited = new Set<string>([from]);\n const queue: Array<{ key: string; path: string[] }> = [{ key: from, path: [from] }];\n const resolvedOpts = { ...resolveTraversalOptions(), ...opts, maxDepth: 10 };\n\n while (queue.length > 0) {\n const current = queue.shift();\n if (current === undefined) break;\n\n if (current.key === to) return current.path;\n\n const edges = getEdgesForKey({ db, key: current.key, direction: 'both' });\n const nextKeys = getNextKeys(current.key, edges, resolvedOpts);\n\n for (const { key: nextKey } of nextKeys) {\n if (!visited.has(nextKey)) {\n visited.add(nextKey);\n queue.push({ key: nextKey, path: [...current.path, nextKey] });\n }\n }\n }\n\n return null;\n}\n","/**\n * Graph-Based Memory Backend\n *\n * Implements graph-structured memory with entity relationships.\n * Extends HybridMemoryBackend with graph operations for traversal\n * and relationship management.\n *\n * @module context/graph-memory\n * (Source: Issue #142, arXiv:2308.09687)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { ILogger } from '../core/logger.js';\nimport { createLogger } from '../core/logger.js';\nimport type { MemoryEntry, MemoryMetadata, ISQLiteDatabase } from './memory-backend-types.js';\nimport { MemoryError } from './memory-backend-types.js';\nimport { HybridMemoryBackend } from './memory-backend.js';\nimport type {\n IGraphMemory,\n GraphEdge,\n GraphMemoryConfig,\n TraversalOptions,\n TraversalResult,\n RelationType,\n AddRelationshipOptions,\n} from './graph-memory-types.js';\nimport {\n GraphMemoryConfigSchema,\n DEFAULT_GRAPH_MEMORY_CONFIG,\n RelationType as RelationTypes,\n} from './graph-memory-types.js';\nimport {\n CREATE_EDGES_TABLE_SQL,\n CREATE_FROM_INDEX_SQL,\n CREATE_TO_INDEX_SQL,\n resolveTraversalOptions,\n getEdgesForKey,\n bfsTraverse,\n findShortestPath,\n} from './graph-memory-helpers.js';\n// Shared utilities per ADR-0013\nimport { memoryExists } from '../utils/memory-db-utils.js';\n\n// Re-export types\nexport type {\n IGraphMemory,\n GraphEdge,\n GraphMemoryConfig,\n TraversalOptions,\n TraversalResult,\n RelationType,\n AddRelationshipOptions,\n} from './graph-memory-types.js';\nexport { RelationType as RelationTypes } from './graph-memory-types.js';\n\nconst logger = createLogger({ component: 'GraphMemoryBackend' });\n\n/**\n * Graph-based memory backend with relationship traversal.\n */\nexport class GraphMemoryBackend implements IGraphMemory {\n private readonly config: GraphMemoryConfig;\n private readonly log: ILogger;\n private readonly base: HybridMemoryBackend;\n private db: ISQLiteDatabase | null = null;\n private initialized = false;\n private initPromise: Promise<Result<void, MemoryError>> | undefined;\n\n constructor(config: GraphMemoryConfig) {\n const validation = GraphMemoryConfigSchema.safeParse(config);\n if (!validation.success) {\n const msg = validation.error.issues\n .map((i) => `${i.path.join('.')}: ${i.message}`)\n .join('; ');\n throw new MemoryError(`Invalid GraphMemoryBackend config: ${msg}`);\n }\n this.config = { ...DEFAULT_GRAPH_MEMORY_CONFIG, ...config };\n this.log = logger;\n this.base = new HybridMemoryBackend({ dbPath: config.dbPath, markdownDir: config.markdownDir });\n }\n\n async initialize(): Promise<Result<void, MemoryError>> {\n if (this.initialized) return ok(undefined);\n this.initPromise ??= this.doInitialize().finally(() => {\n this.initPromise = undefined;\n });\n return this.initPromise;\n }\n\n private async doInitialize(): Promise<Result<void, MemoryError>> {\n const baseInit = await this.base.initialize();\n if (!baseInit.ok) return baseInit;\n\n try {\n const mod = await import('better-sqlite3').catch((cause: unknown) => {\n logger.debug('better-sqlite3 import failed', { error: String(cause) });\n return null;\n });\n if (mod === null)\n return err(\n new MemoryError('better-sqlite3 not installed. Install: npm install better-sqlite3')\n );\n const Database = mod.default;\n this.db = new Database(this.config.dbPath);\n this.createGraphTables();\n this.initialized = true;\n this.log.info('GraphMemoryBackend initialized');\n return ok(undefined);\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return err(new MemoryError('Failed to initialize graph backend', { cause }));\n }\n }\n\n initializeWithDatabase(database: ISQLiteDatabase): void {\n this.base.initializeWithDatabase(database);\n this.db = database;\n this.createGraphTables();\n this.initialized = true;\n this.log.info('GraphMemoryBackend initialized with database');\n }\n\n private createGraphTables(): void {\n const db = this.getDb();\n db.exec(CREATE_EDGES_TABLE_SQL);\n db.exec(CREATE_FROM_INDEX_SQL);\n db.exec(CREATE_TO_INDEX_SQL);\n this.log.debug('Graph tables created');\n }\n\n private getDb(): ISQLiteDatabase {\n if (this.db === null) throw new MemoryError('Database not initialized');\n return this.db;\n }\n\n private ensureInit(): void {\n if (!this.initialized) throw new MemoryError('GraphMemoryBackend not initialized');\n }\n\n // =========================================================================\n // IMemoryBackend Methods (delegated to base)\n // =========================================================================\n\n store(key: string, value: unknown, metadata: MemoryMetadata): Promise<Result<void, MemoryError>> {\n return this.base.store(key, value, metadata);\n }\n\n retrieve(key: string): Promise<Result<unknown, MemoryError>> {\n return this.base.retrieve(key);\n }\n\n search(query: string, limit: number): Promise<Result<MemoryEntry[], MemoryError>> {\n return this.base.search(query, limit);\n }\n\n prune(olderThan: Date): Promise<Result<number, MemoryError>> {\n return this.base.prune(olderThan);\n }\n\n // =========================================================================\n // Graph Relationship Methods\n // =========================================================================\n\n addRelationship(\n from: string,\n to: string,\n type: RelationType,\n opts?: AddRelationshipOptions\n ): Promise<Result<void, MemoryError>> {\n try {\n this.ensureInit();\n const db = this.getDb();\n if (!memoryExists(db, from))\n return Promise.resolve(err(new MemoryError(`Source key not found: ${from}`)));\n if (!memoryExists(db, to))\n return Promise.resolve(err(new MemoryError(`Target key not found: ${to}`)));\n\n const weight = opts?.weight ?? 1.0;\n const metadata = opts?.metadata !== undefined ? JSON.stringify(opts.metadata) : null;\n const stmt = db.prepare(`INSERT OR REPLACE INTO graph_edges VALUES (?, ?, ?, ?, ?, ?)`);\n stmt.run(from, to, type, weight, getTimeProvider().now(), metadata);\n this.log.debug('Added relationship', { from, to, type, weight });\n return Promise.resolve(ok(undefined));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to add relationship', { cause })));\n }\n }\n\n removeRelationship(\n from: string,\n to: string,\n type?: RelationType\n ): Promise<Result<void, MemoryError>> {\n try {\n this.ensureInit();\n const db = this.getDb();\n const sql =\n type !== undefined\n ? 'DELETE FROM graph_edges WHERE from_key = ? AND to_key = ? AND relation_type = ?'\n : 'DELETE FROM graph_edges WHERE from_key = ? AND to_key = ?';\n const args = type !== undefined ? [from, to, type] : [from, to];\n db.prepare(sql).run(...args);\n this.log.debug('Removed relationship', { from, to, type });\n return Promise.resolve(ok(undefined));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to remove relationship', { cause })));\n }\n }\n\n getRelationships(\n key: string,\n direction: 'outgoing' | 'incoming' | 'both' = 'both'\n ): Promise<Result<GraphEdge[], MemoryError>> {\n try {\n this.ensureInit();\n const edges = getEdgesForKey({ db: this.getDb(), key, direction });\n return Promise.resolve(ok(edges));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to get relationships', { cause })));\n }\n }\n\n // =========================================================================\n // Graph Traversal Methods\n // =========================================================================\n\n traverse(\n startKey: string,\n opts?: TraversalOptions\n ): Promise<Result<TraversalResult[], MemoryError>> {\n try {\n this.ensureInit();\n const db = this.getDb();\n if (!memoryExists(db, startKey))\n return Promise.resolve(err(new MemoryError(`Key not found: ${startKey}`)));\n\n const maxDepth =\n this.config.maxTraversalDepth ?? DEFAULT_GRAPH_MEMORY_CONFIG.maxTraversalDepth;\n const resolved = resolveTraversalOptions(opts);\n if (resolved.maxDepth > maxDepth) {\n const msg = `Max depth exceeded: ${String(resolved.maxDepth)} > ${String(maxDepth)}`;\n return Promise.resolve(err(new MemoryError(msg)));\n }\n\n const results = bfsTraverse({ db, startKey, opts: resolved });\n return Promise.resolve(ok(results));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Traversal failed', { cause })));\n }\n }\n\n findPath(\n from: string,\n to: string,\n opts?: Pick<TraversalOptions, 'relationTypes' | 'minWeight'>\n ): Promise<Result<string[] | null, MemoryError>> {\n try {\n this.ensureInit();\n const db = this.getDb();\n if (!memoryExists(db, from))\n return Promise.resolve(err(new MemoryError(`From key not found: ${from}`)));\n if (!memoryExists(db, to))\n return Promise.resolve(err(new MemoryError(`To key not found: ${to}`)));\n\n const resolved = resolveTraversalOptions(opts);\n const path = findShortestPath(db, from, to, resolved);\n return Promise.resolve(ok(path));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Path finding failed', { cause })));\n }\n }\n\n async getNeighbors(key: string, depth = 1): Promise<Result<MemoryEntry[], MemoryError>> {\n const result = await this.traverse(key, { maxDepth: depth, includeStart: false });\n if (!result.ok) return result;\n return ok(result.value.map((r) => r.entry));\n }\n\n // =========================================================================\n // Store with Relations\n // =========================================================================\n\n async storeWithRelations(\n key: string,\n value: unknown,\n metadata: MemoryMetadata,\n relatedTo?: readonly string[]\n ): Promise<Result<void, MemoryError>> {\n const storeResult = await this.store(key, value, metadata);\n if (!storeResult.ok) return storeResult;\n\n if (relatedTo !== undefined && relatedTo.length > 0) {\n for (const relKey of relatedTo) {\n const addResult = await this.addRelationship(key, relKey, RelationTypes.RELATED_TO);\n if (!addResult.ok) this.log.warn('Failed to add relation', { from: key, to: relKey });\n }\n }\n return ok(undefined);\n }\n\n // =========================================================================\n // Lifecycle\n // =========================================================================\n\n close(): void {\n this.base.close();\n if (this.db !== null) {\n this.db.close();\n this.db = null;\n }\n this.initialized = false;\n this.log.info('GraphMemoryBackend closed');\n }\n}\n\n/** Create a GraphMemoryBackend instance. */\nexport function createGraphMemory(config: GraphMemoryConfig): GraphMemoryBackend {\n return new GraphMemoryBackend(config);\n}\n","/**\n * Agentic Memory Types\n *\n * Type definitions for A-MEM agentic memory with Zettelkasten-style\n * dynamic linking, attribute extraction, and memory evolution.\n *\n * @module context/agentic-memory-types\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport { z } from 'zod';\nimport type { Result } from '../core/result.js';\nimport type { MemoryEntry, MemoryError, IMemoryBackend } from './memory-backend-types.js';\nimport type { RelationType } from './graph-memory-types.js';\nimport { RelationTypeSchema } from './graph-memory-types.js';\n\n// Re-export primitives for backward compatibility\nexport {\n EntityType,\n EntityTypeSchema,\n type EntityReference,\n EntityReferenceSchema,\n EvolutionType,\n EvolutionTypeSchema,\n type EvolutionResult,\n EvolutionResultSchema,\n type LinkSuggestion,\n LinkSuggestionSchema,\n} from './agentic-memory-primitives.js';\n\n// Import primitives for use in this file\nimport type {\n EntityReference,\n EvolutionResult,\n LinkSuggestion,\n} from './agentic-memory-primitives.js';\nimport { EntityReferenceSchema } from './agentic-memory-primitives.js';\n\n// ============================================================================\n// Memory Attributes (Zettelkasten-style)\n// ============================================================================\n\n/**\n * A-MEM attributes for a memory entry.\n * These are automatically extracted when storing memories.\n */\nexport interface MemoryAttributes {\n /** Auto-extracted keywords from content */\n readonly keywords: readonly string[];\n /** Semantic tags (broader categories) */\n readonly semanticTags: readonly string[];\n /** Contextual description (brief summary) */\n readonly contextDescription: string;\n /** Entity references extracted from content */\n readonly entities: readonly EntityReference[];\n /** When attributes were last updated */\n readonly attributesUpdatedAt: Date;\n}\n\n/** Zod schema for MemoryAttributes validation. */\nexport const MemoryAttributesSchema = z.object({\n keywords: z.array(z.string().max(100)).max(50),\n semanticTags: z.array(z.string().max(50)).max(20),\n contextDescription: z.string().max(500),\n entities: z.array(EntityReferenceSchema).max(30),\n attributesUpdatedAt: z.date(),\n});\n\n// ============================================================================\n// Agentic Memory Entry\n// ============================================================================\n\n/**\n * Memory entry with A-MEM attributes attached.\n */\nexport interface AgenticMemoryEntry extends MemoryEntry {\n /** Auto-extracted attributes */\n readonly attributes: MemoryAttributes;\n}\n\n// ============================================================================\n// Store Result\n// ============================================================================\n\n/**\n * Result of an agentic store operation.\n */\nexport interface AgenticStoreResult {\n /** The stored entry with extracted attributes */\n readonly entry: AgenticMemoryEntry;\n /** Suggested links to other memories */\n readonly linkSuggestions: readonly LinkSuggestion[];\n /** Detected evolution relationships */\n readonly evolution: readonly EvolutionResult[];\n}\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\n/**\n * Configuration for attribute extraction.\n */\nexport interface ExtractionConfig {\n /** Maximum keywords to extract (default: 10) */\n readonly maxKeywords: number;\n /** Maximum semantic tags (default: 5) */\n readonly maxSemanticTags: number;\n /** Maximum context description length in chars (default: 200) */\n readonly maxContextLength: number;\n /** Maximum entities to extract (default: 10) */\n readonly maxEntities: number;\n}\n\n/** Zod schema for ExtractionConfig validation. */\nexport const ExtractionConfigSchema = z.object({\n maxKeywords: z.number().int().positive().max(50),\n maxSemanticTags: z.number().int().positive().max(20),\n maxContextLength: z.number().int().positive().max(500),\n maxEntities: z.number().int().positive().max(30),\n});\n\n/**\n * Configuration for automatic linking behavior.\n */\nexport interface LinkingConfig {\n /** Minimum confidence to suggest links (default: 0.5) */\n readonly suggestionThreshold: number;\n /** Maximum link suggestions per memory (default: 5) */\n readonly maxSuggestions: number;\n /** Relationship types to consider for linking */\n readonly allowedTypes: readonly RelationType[];\n}\n\n/** Zod schema for LinkingConfig validation. */\nexport const LinkingConfigSchema = z.object({\n suggestionThreshold: z.number().min(0).max(1),\n maxSuggestions: z.number().int().positive().max(20),\n allowedTypes: z.array(RelationTypeSchema),\n});\n\n/**\n * Options for linking related memories.\n */\nexport interface LinkingOptions {\n /** Minimum confidence threshold for applying links (default: 0.7) */\n readonly threshold?: number;\n /** Maximum links to apply (default: config.maxSuggestions) */\n readonly maxLinks?: number;\n /** Whether to create bidirectional links (default: true) */\n readonly bidirectional?: boolean;\n}\n\n/** Zod schema for LinkingOptions validation. */\nexport const LinkingOptionsSchema = z.object({\n threshold: z.number().min(0).max(1).optional(),\n maxLinks: z.number().int().positive().optional(),\n bidirectional: z.boolean().optional(),\n});\n\n// ============================================================================\n// Agentic Memory Interface\n// ============================================================================\n\n/**\n * Agentic memory interface implementing A-MEM Zettelkasten-style organization.\n * Extends IMemoryBackend with automatic attribute extraction, dynamic linking,\n * and memory evolution detection.\n *\n * (Source: Issue #122, arXiv:2502.12110)\n */\nexport interface IAgenticMemory extends IMemoryBackend {\n /**\n * Store a memory with automatic attribute extraction.\n * Extracts keywords, tags, entities, and context from the value.\n * Does NOT auto-link; call linkRelatedMemories() separately.\n * @param key - Memory key\n * @param value - Memory value (text content for extraction)\n * @param metadata - Memory metadata (importance, tags, ttl)\n * @returns Store result with extracted attributes and suggestions\n */\n storeWithAttributes(\n key: string,\n value: unknown,\n metadata: import('./memory-backend-types.js').MemoryMetadata\n ): Promise<Result<AgenticStoreResult, MemoryError>>;\n\n /**\n * Retrieve a memory with its A-MEM attributes.\n * @param key - Memory key\n * @returns Entry with attributes or null if not found\n */\n retrieveWithAttributes(key: string): Promise<Result<AgenticMemoryEntry | null, MemoryError>>;\n\n /**\n * Search with A-MEM enhanced retrieval.\n * Uses keywords and semantic tags for better relevance scoring.\n * @param query - Search query\n * @param limit - Max results (default: 10)\n * @returns Entries sorted by relevance\n */\n searchAgentic(query: string, limit?: number): Promise<Result<AgenticMemoryEntry[], MemoryError>>;\n\n /**\n * Get link suggestions for a memory entry.\n * Analyzes keyword overlap and entity co-occurrence.\n * Does NOT modify state.\n * @param key - Memory key to analyze\n * @param limit - Max suggestions (default: config.maxSuggestions)\n * @returns Suggested links ranked by confidence\n */\n suggestLinks(key: string, limit?: number): Promise<Result<LinkSuggestion[], MemoryError>>;\n\n /**\n * Link related memories based on suggestions.\n * Creates graph relationships for suggestions meeting threshold.\n * @param key - Memory key to link from\n * @param opts - Linking options (threshold, maxLinks, bidirectional)\n * @returns Number of links created\n */\n linkRelatedMemories(key: string, opts?: LinkingOptions): Promise<Result<number, MemoryError>>;\n\n /**\n * Detect evolution relationships with existing memories.\n * Identifies refinements, contradictions, extensions, supersessions.\n * @param key - Memory key to analyze\n * @returns Detected evolution relationships\n */\n detectEvolution(key: string): Promise<Result<EvolutionResult[], MemoryError>>;\n\n /**\n * Refresh attributes for a memory (re-extract).\n * Updates keywords, tags, entities, and context from current value.\n * @param key - Memory key\n * @returns Updated attributes\n */\n refreshAttributes(key: string): Promise<Result<MemoryAttributes, MemoryError>>;\n\n /**\n * Find memories that share attributes with the given key.\n * @param key - Memory key\n * @param attributeType - Type of attribute to match\n * @param limit - Max results (default: 10)\n * @returns Entries sharing attributes\n */\n findBySharedAttributes(\n key: string,\n attributeType: 'keywords' | 'semanticTags' | 'entities',\n limit?: number\n ): Promise<Result<AgenticMemoryEntry[], MemoryError>>;\n\n /** Get extraction config. */\n getExtractionConfig(): ExtractionConfig;\n\n /** Update extraction config. */\n updateExtractionConfig(config: Partial<ExtractionConfig>): void;\n\n /** Get linking config. */\n getLinkingConfig(): LinkingConfig;\n\n /** Update linking config. */\n updateLinkingConfig(config: Partial<LinkingConfig>): void;\n}\n\n// ============================================================================\n// Backend Configuration\n// ============================================================================\n\n/**\n * Configuration for AgenticMemoryBackend.\n */\nexport interface AgenticMemoryConfig {\n /** Path to SQLite database file */\n readonly dbPath: string;\n /** Directory for Markdown exports */\n readonly markdownDir: string;\n /** Attribute extraction config (uses defaults if not provided) */\n readonly extraction?: Partial<ExtractionConfig>;\n /** Linking config (uses defaults if not provided) */\n readonly linking?: Partial<LinkingConfig>;\n /** Whether to auto-expire TTL entries (default: true) */\n readonly autoExpire?: boolean;\n}\n\n/** Zod schema for AgenticMemoryConfig validation. */\nexport const AgenticMemoryConfigSchema = z.object({\n dbPath: z.string().min(1),\n markdownDir: z.string().min(1),\n extraction: ExtractionConfigSchema.partial().optional(),\n linking: LinkingConfigSchema.partial().optional(),\n autoExpire: z.boolean().optional(),\n});\n\n// ============================================================================\n// Default Configuration\n// ============================================================================\n\n/** Default extraction configuration. */\nexport const DEFAULT_EXTRACTION_CONFIG: ExtractionConfig = {\n maxKeywords: 10,\n maxSemanticTags: 5,\n maxContextLength: 200,\n maxEntities: 10,\n};\n\n/** Default linking configuration. */\nexport const DEFAULT_LINKING_CONFIG: LinkingConfig = {\n suggestionThreshold: 0.5,\n maxSuggestions: 5,\n allowedTypes: ['related_to', 'derived_from', 'same_entity'],\n};\n\n/** Default agentic memory configuration. */\nexport const DEFAULT_AGENTIC_MEMORY_CONFIG = {\n extraction: DEFAULT_EXTRACTION_CONFIG,\n linking: DEFAULT_LINKING_CONFIG,\n autoExpire: true,\n} as const;\n","/**\n * Agentic Memory Primitives\n *\n * Core primitive types for A-MEM agentic memory including entity types,\n * evolution types, and link suggestions. Extracted from agentic-memory-types.ts\n * for module size compliance.\n *\n * @module context/agentic-memory-primitives\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport { z } from 'zod';\nimport type { RelationType } from './graph-memory-types.js';\nimport { RelationTypeSchema } from './graph-memory-types.js';\n\n// ============================================================================\n// Entity Types\n// ============================================================================\n\n/**\n * Types of entities that can be extracted from memory content.\n */\nexport const EntityType = {\n PERSON: 'person',\n ORGANIZATION: 'organization',\n CONCEPT: 'concept',\n CODE: 'code',\n FILE: 'file',\n UNKNOWN: 'unknown',\n} as const;\n\nexport type EntityType = (typeof EntityType)[keyof typeof EntityType];\n\n/** Zod schema for EntityType validation. */\nexport const EntityTypeSchema = z.enum([\n 'person',\n 'organization',\n 'concept',\n 'code',\n 'file',\n 'unknown',\n]);\n\n/**\n * An entity reference extracted from memory content.\n */\nexport interface EntityReference {\n /** Name or identifier of the entity */\n readonly name: string;\n /** Type of entity */\n readonly type: EntityType;\n}\n\n/** Zod schema for EntityReference validation. */\nexport const EntityReferenceSchema = z.object({\n name: z.string().min(1).max(200),\n type: EntityTypeSchema,\n});\n\n// ============================================================================\n// Evolution Types\n// ============================================================================\n\n/**\n * Types of memory evolution.\n */\nexport const EvolutionType = {\n /** New memory refines/improves existing knowledge */\n REFINEMENT: 'refinement',\n /** New memory contradicts existing knowledge */\n CONTRADICTION: 'contradiction',\n /** New memory extends existing knowledge */\n EXTENSION: 'extension',\n /** New memory supersedes (replaces) existing knowledge */\n SUPERSESSION: 'supersession',\n} as const;\n\nexport type EvolutionType = (typeof EvolutionType)[keyof typeof EvolutionType];\n\n/** Zod schema for EvolutionType validation. */\nexport const EvolutionTypeSchema = z.enum([\n 'refinement',\n 'contradiction',\n 'extension',\n 'supersession',\n]);\n\n/**\n * Result of memory evolution analysis.\n */\nexport interface EvolutionResult {\n /** Type of evolution detected */\n readonly type: EvolutionType;\n /** Key of the affected existing memory */\n readonly affectedKey: string;\n /** Confidence score (0-1) */\n readonly confidence: number;\n /** Description of what changed */\n readonly description: string;\n}\n\n/** Zod schema for EvolutionResult validation. */\nexport const EvolutionResultSchema = z.object({\n type: EvolutionTypeSchema,\n affectedKey: z.string().min(1),\n confidence: z.number().min(0).max(1),\n description: z.string().max(500),\n});\n\n// ============================================================================\n// Link Suggestions\n// ============================================================================\n\n/**\n * A suggested link between memories.\n */\nexport interface LinkSuggestion {\n /** Source memory key */\n readonly from: string;\n /** Target memory key */\n readonly to: string;\n /** Suggested relationship type */\n readonly relationType: RelationType;\n /** Reason for the suggestion */\n readonly reason: string;\n /** Confidence score (0-1) */\n readonly confidence: number;\n}\n\n/** Zod schema for LinkSuggestion validation. */\nexport const LinkSuggestionSchema = z.object({\n from: z.string().min(1),\n to: z.string().min(1),\n relationType: RelationTypeSchema,\n reason: z.string().max(200),\n confidence: z.number().min(0).max(1),\n});\n","/**\n * Agentic Memory Extraction Helpers\n *\n * Rule-based attribute extraction for A-MEM agentic memory.\n *\n * @module context/agentic-memory-extraction\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport { getTimeProvider } from '../core/index.js';\nimport type {\n MemoryAttributes,\n EntityReference,\n EntityType,\n ExtractionConfig,\n} from './agentic-memory-types.js';\nimport { DEFAULT_EXTRACTION_CONFIG } from './agentic-memory-types.js';\n// Shared utilities per ADR-0013\nimport {\n tokenizeFiltered as sharedTokenizeFiltered,\n stringifyValue as sharedStringifyValue,\n} from '../utils/text-utils.js';\n\n// ============================================================================\n// Keyword Extraction (Rule-based)\n// ============================================================================\n\n/**\n * Extract keywords from text using TF-IDF-like frequency analysis.\n * Returns the most frequent significant words.\n */\nexport function extractKeywords(text: string, maxKeywords: number): string[] {\n const tokens = sharedTokenizeFiltered(text, 2);\n if (tokens.length === 0) return [];\n\n // Count frequencies\n const freq = new Map<string, number>();\n for (const token of tokens) {\n freq.set(token, (freq.get(token) ?? 0) + 1);\n }\n\n // Sort by frequency descending and take top\n return [...freq.entries()]\n .sort((a, b) => b[1] - a[1])\n .slice(0, maxKeywords)\n .map(([word]) => word);\n}\n\n// ============================================================================\n// Semantic Tag Extraction\n// ============================================================================\n\nconst TAG_PATTERNS: Array<{ pattern: RegExp; tag: string }> = [\n {\n pattern: /\\b(function|class|method|interface|type|const|let|var|import|export)\\b/i,\n tag: 'code',\n },\n { pattern: /\\b(test|spec|expect|describe|it|assert|mock)\\b/i, tag: 'testing' },\n { pattern: /\\b(error|exception|bug|fix|issue|problem)\\b/i, tag: 'debugging' },\n { pattern: /\\b(api|endpoint|request|response|http|rest|graphql)\\b/i, tag: 'api' },\n { pattern: /\\b(database|sql|query|table|schema|migration)\\b/i, tag: 'database' },\n { pattern: /\\b(config|setting|option|parameter|environment)\\b/i, tag: 'configuration' },\n { pattern: /\\b(security|auth|token|permission|access|credential)\\b/i, tag: 'security' },\n { pattern: /\\b(performance|latency|throughput|optimize|cache)\\b/i, tag: 'performance' },\n { pattern: /\\b(deploy|release|pipeline|ci|cd|build)\\b/i, tag: 'devops' },\n { pattern: /\\b(documentation|readme|guide|tutorial|example)\\b/i, tag: 'documentation' },\n { pattern: /\\b(agent|workflow|task|orchestrate|delegate)\\b/i, tag: 'agents' },\n { pattern: /\\b(memory|context|recall|retrieve|store)\\b/i, tag: 'memory' },\n];\n\n/**\n * Extract semantic tags by matching content against patterns.\n */\nexport function extractSemanticTags(text: string, maxTags: number): string[] {\n const matched: string[] = [];\n for (const { pattern, tag } of TAG_PATTERNS) {\n if (pattern.test(text)) {\n matched.push(tag);\n if (matched.length >= maxTags) break;\n }\n }\n return matched;\n}\n\n// ============================================================================\n// Entity Extraction\n// ============================================================================\n\nconst ENTITY_PATTERNS: Array<{ pattern: RegExp; type: EntityType }> = [\n { pattern: /(?:^|[\\s'\"(])((?:\\.\\/|\\.\\.\\/|\\/)[\\w\\-./]+\\.\\w+)/g, type: 'file' },\n { pattern: /(?:^|[\\s'\"(])([\\w-]+\\.(ts|js|tsx|jsx|py|go|rs|md|json|yaml|yml))/g, type: 'file' },\n { pattern: /\\b([A-Z][a-z]+(?:[A-Z][a-z]+)+)\\b/g, type: 'code' },\n { pattern: /\\b([a-z]+_[a-z_]+)\\b/g, type: 'code' },\n { pattern: /\\b([A-Z][a-z]+(?: [A-Z][a-z]+)+)\\b/g, type: 'concept' },\n];\n\n/**\n * Check if a string looks like PII (SSN, phone, email, etc.)\n */\nfunction isPotentialPII(text: string): boolean {\n if (/^\\d{3}-\\d{2}-\\d{4}$/.test(text)) return true; // SSN\n if (/^\\d{3}[-.\\s]?\\d{3}[-.\\s]?\\d{4}$/.test(text)) return true; // Phone\n if (/^\\+?\\d{10,15}$/.test(text)) return true; // Intl phone\n if (/^[\\w.-]+@[\\w.-]+\\.\\w+$/.test(text)) return true; // Email\n if (/^\\d{4}[-\\s]?\\d{4}[-\\s]?\\d{4}[-\\s]?\\d{4}$/.test(text)) return true; // CC\n return false;\n}\n\n/**\n * Extract entities from text using pattern matching.\n * PII filtering: excludes patterns that look like SSN, phone, email.\n */\nexport function extractEntities(text: string, maxEntities: number): EntityReference[] {\n const entities: EntityReference[] = [];\n const seen = new Set<string>();\n\n for (const { pattern, type } of ENTITY_PATTERNS) {\n pattern.lastIndex = 0;\n let match: RegExpExecArray | null;\n while ((match = pattern.exec(text)) !== null) {\n const name = match[1];\n if (name === undefined) continue;\n if (seen.has(name.toLowerCase()) || name.length < 3) continue;\n if (isPotentialPII(name)) continue;\n\n seen.add(name.toLowerCase());\n entities.push({ name, type });\n if (entities.length >= maxEntities) break;\n }\n if (entities.length >= maxEntities) break;\n }\n return entities;\n}\n\n// ============================================================================\n// Context Description Generation\n// ============================================================================\n\n/**\n * Generate a brief context description from text.\n */\nexport function generateContextDescription(text: string, maxLength: number): string {\n const firstSentence = text.match(/^[^.!?]+[.!?]/);\n if (firstSentence !== null && firstSentence[0].length <= maxLength) {\n return firstSentence[0].trim();\n }\n if (text.length <= maxLength) return text.trim();\n\n const truncated = text.slice(0, maxLength);\n const lastSpace = truncated.lastIndexOf(' ');\n if (lastSpace > maxLength * 0.7) {\n return truncated.slice(0, lastSpace).trim() + '...';\n }\n return truncated.trim() + '...';\n}\n\n// ============================================================================\n// Attribute Extraction Pipeline\n// ============================================================================\n\n/**\n * Extract all A-MEM attributes from a value.\n */\nexport function extractAttributes(value: unknown, config: ExtractionConfig): MemoryAttributes {\n const text = sharedStringifyValue(value);\n return {\n keywords: extractKeywords(text, config.maxKeywords),\n semanticTags: extractSemanticTags(text, config.maxSemanticTags),\n contextDescription: generateContextDescription(text, config.maxContextLength),\n entities: extractEntities(text, config.maxEntities),\n attributesUpdatedAt: new Date(getTimeProvider().now()),\n };\n}\n\n/**\n * Merge partial extraction config with defaults.\n * Canonical location per ADR-0013.\n */\nexport function mergeExtractionConfig(partial?: Partial<ExtractionConfig>): ExtractionConfig {\n if (partial === undefined) return DEFAULT_EXTRACTION_CONFIG;\n return {\n maxKeywords: partial.maxKeywords ?? DEFAULT_EXTRACTION_CONFIG.maxKeywords,\n maxSemanticTags: partial.maxSemanticTags ?? DEFAULT_EXTRACTION_CONFIG.maxSemanticTags,\n maxContextLength: partial.maxContextLength ?? DEFAULT_EXTRACTION_CONFIG.maxContextLength,\n maxEntities: partial.maxEntities ?? DEFAULT_EXTRACTION_CONFIG.maxEntities,\n };\n}\n","/**\n * Agentic Memory Linking\n *\n * Similarity calculation, link suggestion, and evolution detection\n * for A-MEM agentic memory system.\n *\n * @module context/agentic-memory-linking\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport { RelationType } from './graph-memory-types.js';\nimport type {\n MemoryAttributes,\n LinkingConfig,\n LinkSuggestion,\n EvolutionResult,\n EvolutionType,\n} from './agentic-memory-types.js';\nimport { DEFAULT_LINKING_CONFIG } from './agentic-memory-types.js';\n\n// ============================================================================\n// Similarity Calculation (FTS-based)\n// ============================================================================\n\n/**\n * Calculate keyword overlap similarity between two memory entries.\n * Uses Jaccard-like coefficient: |intersection| / |union|\n */\nexport function calculateKeywordSimilarity(\n attrs1: MemoryAttributes,\n attrs2: MemoryAttributes\n): number {\n const set1 = new Set(attrs1.keywords);\n const set2 = new Set(attrs2.keywords);\n\n if (set1.size === 0 && set2.size === 0) return 0;\n\n let intersection = 0;\n for (const k of set1) {\n if (set2.has(k)) intersection++;\n }\n\n const union = set1.size + set2.size - intersection;\n return union > 0 ? intersection / union : 0;\n}\n\n/**\n * Calculate entity overlap similarity.\n * Returns the fraction of shared entities.\n */\nexport function calculateEntitySimilarity(\n attrs1: MemoryAttributes,\n attrs2: MemoryAttributes\n): number {\n const names1 = new Set(attrs1.entities.map((e) => e.name.toLowerCase()));\n const names2 = new Set(attrs2.entities.map((e) => e.name.toLowerCase()));\n\n if (names1.size === 0 && names2.size === 0) return 0;\n\n let intersection = 0;\n for (const name of names1) {\n if (names2.has(name)) intersection++;\n }\n\n const union = names1.size + names2.size - intersection;\n return union > 0 ? intersection / union : 0;\n}\n\n/**\n * Calculate overall similarity between two memories.\n * Combines keyword similarity (60%) and entity similarity (40%).\n */\nexport function calculateOverallSimilarity(\n attrs1: MemoryAttributes,\n attrs2: MemoryAttributes\n): number {\n const keywordSim = calculateKeywordSimilarity(attrs1, attrs2);\n const entitySim = calculateEntitySimilarity(attrs1, attrs2);\n\n return keywordSim * 0.6 + entitySim * 0.4;\n}\n\n// ============================================================================\n// Link Suggestion Generation\n// ============================================================================\n\n/**\n * Infer relationship type based on similarity and content.\n */\nexport function inferRelationType(\n fromAttrs: MemoryAttributes,\n toAttrs: MemoryAttributes,\n fromCreatedAt: Date,\n toCreatedAt: Date\n): RelationType {\n // Check for shared entities (same_entity)\n const fromNames = new Set(fromAttrs.entities.map((e) => e.name.toLowerCase()));\n const toNames = new Set(toAttrs.entities.map((e) => e.name.toLowerCase()));\n let sharedEntities = 0;\n for (const name of fromNames) {\n if (toNames.has(name)) sharedEntities++;\n }\n if (sharedEntities > 0) return RelationType.SAME_ENTITY;\n\n // Check temporal relationship (precedes)\n if (fromCreatedAt < toCreatedAt) return RelationType.PRECEDES;\n\n // Default to related_to\n return RelationType.RELATED_TO;\n}\n\n/**\n * Generate a human-readable reason for a link suggestion.\n */\nfunction generateLinkReason(\n fromAttrs: MemoryAttributes,\n toAttrs: MemoryAttributes,\n similarity: number\n): string {\n // Find shared keywords\n const sharedKeywords = fromAttrs.keywords.filter((k) => toAttrs.keywords.includes(k));\n\n if (sharedKeywords.length > 0) {\n const sample = sharedKeywords.slice(0, 3).join(', ');\n return `Shared keywords: ${sample}`;\n }\n\n // Find shared entities\n const fromNames = fromAttrs.entities.map((e) => e.name);\n const sharedEntities = fromNames.filter((n) =>\n toAttrs.entities.some((e) => e.name.toLowerCase() === n.toLowerCase())\n );\n\n const firstSharedEntity = sharedEntities[0];\n if (firstSharedEntity !== undefined) {\n return `Shared entity: ${firstSharedEntity}`;\n }\n\n return `Similarity: ${(similarity * 100).toFixed(0)}%`;\n}\n\n/**\n * Generate link suggestions for a memory.\n */\nexport function generateLinkSuggestions(\n key: string,\n attrs: MemoryAttributes,\n createdAt: Date,\n candidates: Array<{ key: string; attrs: MemoryAttributes; createdAt: Date }>,\n config: LinkingConfig\n): LinkSuggestion[] {\n const suggestions: LinkSuggestion[] = [];\n\n for (const candidate of candidates) {\n // Skip self\n if (candidate.key === key) continue;\n\n // Calculate similarity\n const similarity = calculateOverallSimilarity(attrs, candidate.attrs);\n\n // Skip if below threshold\n if (similarity < config.suggestionThreshold) continue;\n\n // Infer relationship type\n const relationType = inferRelationType(attrs, candidate.attrs, createdAt, candidate.createdAt);\n\n // Skip if type not allowed\n if (!config.allowedTypes.includes(relationType)) continue;\n\n // Generate reason\n const reason = generateLinkReason(attrs, candidate.attrs, similarity);\n\n suggestions.push({\n from: key,\n to: candidate.key,\n relationType,\n reason,\n confidence: similarity,\n });\n\n if (suggestions.length >= config.maxSuggestions) break;\n }\n\n // Sort by confidence descending\n suggestions.sort((a, b) => b.confidence - a.confidence);\n\n return suggestions.slice(0, config.maxSuggestions);\n}\n\n// ============================================================================\n// Evolution Detection\n// ============================================================================\n\n/**\n * Detect evolution relationship between two memories.\n * Returns null if no evolution detected.\n */\nexport function detectEvolutionPair(\n newAttrs: MemoryAttributes,\n newCreatedAt: Date,\n existingKey: string,\n existingAttrs: MemoryAttributes,\n existingCreatedAt: Date\n): EvolutionResult | null {\n // Calculate similarity\n const similarity = calculateOverallSimilarity(newAttrs, existingAttrs);\n\n // Only consider if sufficiently similar\n if (similarity < 0.5) return null;\n\n // Determine evolution type\n let type: EvolutionType;\n let description: string;\n\n // Check if new memory is newer\n if (newCreatedAt > existingCreatedAt) {\n // Check for contradictions (high similarity but different content keywords)\n const newOnlyKeywords = newAttrs.keywords.filter((k) => !existingAttrs.keywords.includes(k));\n const existingOnlyKeywords = existingAttrs.keywords.filter(\n (k) => !newAttrs.keywords.includes(k)\n );\n\n if (newOnlyKeywords.length > existingOnlyKeywords.length + 2) {\n type = 'extension';\n description = `Extends with new concepts: ${newOnlyKeywords.slice(0, 3).join(', ')}`;\n } else if (similarity > 0.8) {\n type = 'supersession';\n description = 'Highly similar newer memory, may supersede';\n } else {\n type = 'refinement';\n description = `Refines existing memory (${(similarity * 100).toFixed(0)}% similar)`;\n }\n } else {\n // New memory is older (shouldn't happen in typical flow)\n type = 'refinement';\n description = 'Related historical memory';\n }\n\n return {\n type,\n affectedKey: existingKey,\n confidence: similarity,\n description,\n };\n}\n\n/**\n * Detect all evolution relationships for a new memory.\n */\nexport function detectEvolution(\n newKey: string,\n newAttrs: MemoryAttributes,\n newCreatedAt: Date,\n existingMemories: Array<{ key: string; attrs: MemoryAttributes; createdAt: Date }>\n): EvolutionResult[] {\n const results: EvolutionResult[] = [];\n\n for (const existing of existingMemories) {\n if (existing.key === newKey) continue;\n\n const evolution = detectEvolutionPair(\n newAttrs,\n newCreatedAt,\n existing.key,\n existing.attrs,\n existing.createdAt\n );\n\n if (evolution !== null) {\n results.push(evolution);\n }\n }\n\n // Sort by confidence descending\n results.sort((a, b) => b.confidence - a.confidence);\n\n return results;\n}\n\n// ============================================================================\n// Configuration Merging\n// ============================================================================\n\n/**\n * Merge partial linking config with defaults.\n * Canonical location per ADR-0013.\n */\nexport function mergeLinkingConfig(partial?: Partial<LinkingConfig>): LinkingConfig {\n if (partial === undefined) return DEFAULT_LINKING_CONFIG;\n return {\n suggestionThreshold: partial.suggestionThreshold ?? DEFAULT_LINKING_CONFIG.suggestionThreshold,\n maxSuggestions: partial.maxSuggestions ?? DEFAULT_LINKING_CONFIG.maxSuggestions,\n allowedTypes: partial.allowedTypes ?? DEFAULT_LINKING_CONFIG.allowedTypes,\n };\n}\n","/**\n * Agentic Memory Database Helpers\n *\n * Database-related helper functions for A-MEM attribute storage and retrieval.\n *\n * @module context/agentic-memory-db-helpers\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport type { MemoryRow, ISQLiteDatabase } from './memory-backend-types.js';\nimport type {\n MemoryAttributes,\n ExtractionConfig,\n AgenticMemoryEntry,\n} from './agentic-memory-types.js';\n// Shared utilities per ADR-0013\nimport { memoryRowToEntry } from '../utils/memory-db-utils.js';\nimport { extractAttributes } from './agentic-memory-extraction.js';\nimport { createLogger } from '../core/index.js';\n\nconst logger = createLogger({ component: 'AgenticMemoryDbHelpers' });\n\n// ============================================================================\n// Database Helpers\n// ============================================================================\n\n/**\n * Safely extract MemoryAttributes from a raw amem record using type guards.\n * Returns null if the record shape is invalid.\n */\nfunction safeExtractAttributes(amem: Record<string, unknown>): MemoryAttributes | null {\n if (!Array.isArray(amem.keywords) || amem.attributesUpdatedAt === undefined) return null;\n\n const updatedAt = amem.attributesUpdatedAt;\n if (typeof updatedAt !== 'number' && typeof updatedAt !== 'string') return null;\n\n return {\n keywords: amem.keywords.filter((k): k is string => typeof k === 'string'),\n semanticTags: Array.isArray(amem.semanticTags)\n ? amem.semanticTags.filter((t): t is string => typeof t === 'string')\n : [],\n contextDescription: typeof amem.contextDescription === 'string' ? amem.contextDescription : '',\n entities: Array.isArray(amem.entities) ? (amem.entities as MemoryAttributes['entities']) : [],\n attributesUpdatedAt: new Date(updatedAt),\n };\n}\n\n/**\n * Parse A-MEM attributes from memory metadata JSON.\n */\nexport function parseAmemAttributes(metadata: unknown): MemoryAttributes | null {\n if (typeof metadata !== 'object' || metadata === null) return null;\n\n const meta = metadata as Record<string, unknown>;\n if (meta.amem === undefined || typeof meta.amem !== 'object' || meta.amem === null) return null;\n\n return safeExtractAttributes(meta.amem as Record<string, unknown>);\n}\n\n/**\n * Convert MemoryRow to AgenticMemoryEntry.\n */\nexport function memoryRowToAgenticEntry(\n row: MemoryRow,\n extractionConfig: ExtractionConfig\n): AgenticMemoryEntry {\n const baseEntry = memoryRowToEntry(row);\n\n let parsedMeta: Record<string, unknown> = {};\n try {\n parsedMeta = JSON.parse(row.metadata) as Record<string, unknown>;\n } catch {\n logger.warn('Corrupt metadata JSON in memory row, using fallback extraction', {\n key: row.key,\n });\n }\n\n const attributes =\n parseAmemAttributes(parsedMeta) ?? extractAttributes(baseEntry.value, extractionConfig);\n\n return { ...baseEntry, attributes };\n}\n\n/**\n * Search for memories with FTS and return with A-MEM attributes.\n */\nexport function searchWithAttributes(\n db: ISQLiteDatabase,\n query: string,\n limit: number,\n extractionConfig: ExtractionConfig\n): AgenticMemoryEntry[] {\n const sanitized = query\n .replace(/[*()\":^]/g, '')\n .replace(/\\s+/g, ' ')\n .trim();\n if (sanitized.length === 0) return [];\n\n const stmt = db.prepare<MemoryRow>(`\n SELECT m.key, m.value, m.metadata, m.created_at, m.accessed_at, m.expires_at\n FROM memories m INNER JOIN memories_fts fts ON m.rowid = fts.rowid\n WHERE memories_fts MATCH ? ORDER BY rank LIMIT ?\n `);\n\n const rows = stmt.all(sanitized, limit);\n return rows.map((row) => memoryRowToAgenticEntry(row, extractionConfig));\n}\n\n/**\n * Get a set of attribute values for comparison.\n */\nexport function getAttributeSet(\n attrs: MemoryAttributes,\n type: 'keywords' | 'semanticTags' | 'entities'\n): Set<string> {\n switch (type) {\n case 'keywords':\n return new Set(attrs.keywords);\n case 'semanticTags':\n return new Set(attrs.semanticTags);\n case 'entities':\n return new Set(attrs.entities.map((e) => e.name.toLowerCase()));\n }\n}\n\n/**\n * Extract attributes from a memory row's metadata or value.\n */\nexport function getAttributesFromRow(\n row: MemoryRow,\n extractionConfig: ExtractionConfig\n): MemoryAttributes {\n let meta: Record<string, unknown> = {};\n try {\n meta = JSON.parse(row.metadata) as Record<string, unknown>;\n } catch {\n logger.warn('Corrupt metadata JSON in getAttributesFromRow, using value extraction', {\n key: row.key,\n });\n }\n\n if (meta.amem !== undefined && typeof meta.amem === 'object' && meta.amem !== null) {\n const parsed = safeExtractAttributes(meta.amem as Record<string, unknown>);\n if (parsed !== null) return parsed;\n }\n\n let parsedValue: unknown = row.value;\n try {\n parsedValue = JSON.parse(row.value);\n } catch {\n logger.warn('Corrupt value JSON in getAttributesFromRow, using raw string', {\n key: row.key,\n });\n }\n\n return extractAttributes(parsedValue, extractionConfig);\n}\n\n/**\n * Find memories with overlapping attributes.\n */\nexport function findMatchingMemories(\n rows: MemoryRow[],\n sourceSet: Set<string>,\n attributeType: 'keywords' | 'semanticTags' | 'entities',\n extractionConfig: ExtractionConfig\n): Array<{ entry: AgenticMemoryEntry; overlap: number }> {\n const matches: Array<{ entry: AgenticMemoryEntry; overlap: number }> = [];\n for (const row of rows) {\n const attrs = getAttributesFromRow(row, extractionConfig);\n const targetSet = getAttributeSet(attrs, attributeType);\n let overlap = 0;\n for (const item of sourceSet) if (targetSet.has(item)) overlap++;\n if (overlap > 0)\n matches.push({ entry: memoryRowToAgenticEntry(row, extractionConfig), overlap });\n }\n matches.sort((a, b) => b.overlap - a.overlap);\n return matches;\n}\n","/**\n * Agentic Memory Database Operations\n *\n * Pure database operation helpers for AgenticMemoryBackend.\n * These functions take database instances as parameters to avoid\n * class state dependencies.\n *\n * @module context/agentic-memory-operations\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport { getTimeProvider } from '../core/index.js';\nimport type { ISQLiteDatabase, MemoryRow, MemoryMetadata } from './memory-backend-types.js';\nimport type {\n MemoryAttributes,\n ExtractionConfig,\n AgenticMemoryEntry,\n LinkSuggestion,\n} from './agentic-memory-types.js';\nimport { getAttributesFromRow, memoryRowToAgenticEntry } from './agentic-memory-db-helpers.js';\nimport { GraphMemoryBackend } from './graph-memory.js';\n\n// ============================================================================\n// Database Query Operations\n// ============================================================================\n\n/**\n * Query existing memories for analysis, excluding a specific key.\n * Returns memories with their attributes for linking and evolution detection.\n */\nexport function queryMemoriesForAnalysis(\n db: ISQLiteDatabase,\n excludeKey: string,\n extractionConfig: ExtractionConfig,\n limit = 100\n): Array<{ key: string; attrs: MemoryAttributes; createdAt: Date }> {\n const rows = db\n .prepare<MemoryRow>('SELECT * FROM memories WHERE key != ? ORDER BY accessed_at DESC LIMIT ?')\n .all(excludeKey, limit);\n return rows.map((row) => ({\n key: row.key,\n attrs: getAttributesFromRow(row, extractionConfig),\n createdAt: new Date(row.created_at),\n }));\n}\n\n/**\n * Query a single memory by key.\n */\nexport function queryMemoryByKey(db: ISQLiteDatabase, key: string): MemoryRow | undefined {\n return db.prepare<MemoryRow>('SELECT * FROM memories WHERE key = ?').get(key);\n}\n\n/**\n * Query candidate memories for link suggestions.\n */\nexport function queryCandidateMemories(\n db: ISQLiteDatabase,\n excludeKey: string,\n extractionConfig: ExtractionConfig,\n limit = 100\n): Array<{ key: string; attrs: MemoryAttributes; createdAt: Date }> {\n const rows = db\n .prepare<MemoryRow>('SELECT * FROM memories WHERE key != ? ORDER BY accessed_at DESC LIMIT ?')\n .all(excludeKey, limit);\n return rows.map((row) => ({\n key: row.key,\n attrs: getAttributesFromRow(row, extractionConfig),\n createdAt: new Date(row.created_at),\n }));\n}\n\n/**\n * Query memories for evolution detection.\n */\nexport function queryMemoriesForEvolution(\n db: ISQLiteDatabase,\n excludeKey: string,\n extractionConfig: ExtractionConfig,\n limit = 50\n): Array<{ key: string; attrs: MemoryAttributes; createdAt: Date }> {\n const rows = db\n .prepare<MemoryRow>('SELECT * FROM memories WHERE key != ? ORDER BY created_at DESC LIMIT ?')\n .all(excludeKey, limit);\n return rows.map((row) => ({\n key: row.key,\n attrs: getAttributesFromRow(row, extractionConfig),\n createdAt: new Date(row.created_at),\n }));\n}\n\n/**\n * Query all memories except one for attribute matching.\n */\nexport function queryAllMemoriesExcept(\n db: ISQLiteDatabase,\n excludeKey: string,\n limit = 200\n): MemoryRow[] {\n return db\n .prepare<MemoryRow>('SELECT * FROM memories WHERE key != ? ORDER BY accessed_at DESC LIMIT ?')\n .all(excludeKey, limit);\n}\n\n/**\n * Update memory metadata with refreshed attributes.\n */\nexport function updateMemoryMetadata(\n db: ISQLiteDatabase,\n key: string,\n metadata: Record<string, unknown>\n): void {\n db.prepare('UPDATE memories SET metadata = ? WHERE key = ?').run(JSON.stringify(metadata), key);\n}\n\n// ============================================================================\n// Graph Relationship Operations\n// ============================================================================\n\n/**\n * Apply link suggestions to the graph backend.\n * Returns the number of links successfully created.\n */\nexport async function applyLinkSuggestions(\n graph: GraphMemoryBackend,\n suggestions: LinkSuggestion[],\n bidirectional: boolean\n): Promise<number> {\n let count = 0;\n for (const s of suggestions) {\n const result = await graph.addRelationship(s.from, s.to, s.relationType, {\n weight: s.confidence,\n metadata: { reason: s.reason },\n });\n if (!result.ok) continue;\n count++;\n if (bidirectional) {\n await graph.addRelationship(s.to, s.from, s.relationType, {\n weight: s.confidence,\n metadata: { reason: s.reason },\n });\n }\n }\n return count;\n}\n\n// ============================================================================\n// Attribute Refresh Operations\n// ============================================================================\n\n/**\n * Prepare updated metadata with refreshed attributes.\n */\nexport function prepareRefreshedMetadata(\n currentMeta: Record<string, unknown>,\n attributes: MemoryAttributes\n): Record<string, unknown> {\n return {\n ...currentMeta,\n amem: { ...attributes, attributesUpdatedAt: getTimeProvider().now() },\n };\n}\n\n// ============================================================================\n// Entry Conversion Operations\n// ============================================================================\n\n/**\n * Convert memory row to agentic entry with attributes.\n * Re-exported for convenience.\n */\nexport { memoryRowToAgenticEntry, getAttributesFromRow };\n\n/**\n * Build agentic memory entry from components.\n */\nexport function buildAgenticEntry(\n key: string,\n value: unknown,\n metadata: MemoryMetadata,\n attributes: MemoryAttributes,\n createdAt: Date\n): AgenticMemoryEntry {\n return {\n key,\n value,\n metadata,\n createdAt,\n accessedAt: createdAt,\n attributes,\n };\n}\n","/**\n * Agentic Memory Backend\n *\n * Implements A-MEM Zettelkasten-style agentic memory with:\n * - Automatic attribute extraction (keywords, tags, entities)\n * - Dynamic linking between related memories\n * - Memory evolution detection\n *\n * Composes with HybridMemoryBackend for storage and GraphMemoryBackend\n * for relationship management.\n *\n * @module context/agentic-memory\n * (Source: Issue #122, arXiv:2502.12110)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { ILogger } from '../core/logger.js';\nimport { createLogger } from '../core/logger.js';\nimport type { MemoryEntry, MemoryMetadata, ISQLiteDatabase } from './memory-backend-types.js';\nimport { MemoryError } from './memory-backend-types.js';\nimport { HybridMemoryBackend } from './memory-backend.js';\nimport { GraphMemoryBackend } from './graph-memory.js';\nimport type {\n IAgenticMemory,\n AgenticMemoryConfig,\n AgenticMemoryEntry,\n AgenticStoreResult,\n MemoryAttributes,\n ExtractionConfig,\n LinkingConfig,\n LinkingOptions,\n LinkSuggestion,\n EvolutionResult,\n} from './agentic-memory-types.js';\nimport { AgenticMemoryConfigSchema } from './agentic-memory-types.js';\nimport { extractAttributes, mergeExtractionConfig } from './agentic-memory-extraction.js';\nimport {\n generateLinkSuggestions,\n detectEvolution,\n mergeLinkingConfig,\n} from './agentic-memory-linking.js';\nimport {\n searchWithAttributes,\n getAttributeSet,\n findMatchingMemories,\n} from './agentic-memory-db-helpers.js';\nimport {\n queryMemoriesForAnalysis,\n queryMemoryByKey,\n queryCandidateMemories,\n queryMemoriesForEvolution,\n queryAllMemoriesExcept,\n updateMemoryMetadata,\n applyLinkSuggestions,\n prepareRefreshedMetadata,\n buildAgenticEntry,\n memoryRowToAgenticEntry,\n getAttributesFromRow,\n} from './agentic-memory-operations.js';\n\n// Re-export types (type-only exports)\nexport type {\n IAgenticMemory,\n AgenticMemoryConfig,\n AgenticMemoryEntry,\n AgenticStoreResult,\n MemoryAttributes,\n ExtractionConfig,\n LinkingConfig,\n LinkingOptions,\n LinkSuggestion,\n EvolutionResult,\n EntityReference,\n} from './agentic-memory-types.js';\n\n// Re-export values (EntityType and EvolutionType are both const values and types)\nexport {\n EntityType,\n EvolutionType,\n DEFAULT_EXTRACTION_CONFIG,\n DEFAULT_LINKING_CONFIG,\n DEFAULT_AGENTIC_MEMORY_CONFIG,\n} from './agentic-memory-types.js';\n\nconst logger = createLogger({ component: 'AgenticMemoryBackend' });\n\n/**\n * Agentic memory backend with A-MEM Zettelkasten-style organization.\n *\n * Provides automatic attribute extraction, dynamic linking suggestions,\n * and memory evolution detection on top of the hybrid storage backend.\n */\nexport class AgenticMemoryBackend implements IAgenticMemory {\n private readonly config: AgenticMemoryConfig;\n private readonly log: ILogger;\n private readonly base: HybridMemoryBackend;\n private readonly graph: GraphMemoryBackend;\n private extractionConfig: ExtractionConfig;\n private linkingConfig: LinkingConfig;\n private db: ISQLiteDatabase | null = null;\n private initialized = false;\n private initPromise: Promise<Result<void, MemoryError>> | undefined;\n\n constructor(config: AgenticMemoryConfig) {\n const validation = AgenticMemoryConfigSchema.safeParse(config);\n if (!validation.success) {\n const msg = validation.error.issues\n .map((i) => `${i.path.join('.')}: ${i.message}`)\n .join('; ');\n throw new MemoryError(`Invalid AgenticMemoryBackend config: ${msg}`);\n }\n this.config = config;\n this.log = logger;\n this.extractionConfig = mergeExtractionConfig(config.extraction);\n this.linkingConfig = mergeLinkingConfig(config.linking);\n this.base = new HybridMemoryBackend({\n dbPath: config.dbPath,\n markdownDir: config.markdownDir,\n autoExpire: config.autoExpire ?? true,\n });\n this.graph = new GraphMemoryBackend({\n dbPath: config.dbPath,\n markdownDir: config.markdownDir,\n autoExpire: config.autoExpire ?? true,\n });\n }\n\n async initialize(): Promise<Result<void, MemoryError>> {\n if (this.initialized) return ok(undefined);\n this.initPromise ??= this.doInitialize().finally(() => {\n this.initPromise = undefined;\n });\n return this.initPromise;\n }\n\n private async doInitialize(): Promise<Result<void, MemoryError>> {\n const baseInit = await this.base.initialize();\n if (!baseInit.ok) return baseInit;\n\n const graphInit = await this.graph.initialize();\n if (!graphInit.ok) return graphInit;\n\n try {\n const mod = await import('better-sqlite3').catch((cause: unknown) => {\n this.log.debug('better-sqlite3 import failed', { error: String(cause) });\n return null;\n });\n if (mod === null)\n return err(\n new MemoryError('better-sqlite3 not installed. Install: npm install better-sqlite3')\n );\n const Database = mod.default;\n this.db = new Database(this.config.dbPath);\n this.initialized = true;\n this.log.info('AgenticMemoryBackend initialized');\n return ok(undefined);\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return err(new MemoryError('Failed to initialize agentic backend', { cause }));\n }\n }\n\n initializeWithDatabase(database: ISQLiteDatabase): void {\n this.base.initializeWithDatabase(database);\n this.graph.initializeWithDatabase(database);\n this.db = database;\n this.initialized = true;\n this.log.info('AgenticMemoryBackend initialized with database');\n }\n\n private getDb(): ISQLiteDatabase {\n if (this.db === null) throw new MemoryError('Database not initialized');\n return this.db;\n }\n\n private ensureInit(): void {\n if (!this.initialized) throw new MemoryError('AgenticMemoryBackend not initialized');\n }\n\n // IMemoryBackend Methods (delegated to base)\n store(key: string, value: unknown, metadata: MemoryMetadata): Promise<Result<void, MemoryError>> {\n return this.base.store(key, value, metadata);\n }\n retrieve(key: string): Promise<Result<unknown, MemoryError>> {\n return this.base.retrieve(key);\n }\n search(query: string, limit: number): Promise<Result<MemoryEntry[], MemoryError>> {\n return this.base.search(query, limit);\n }\n prune(olderThan: Date): Promise<Result<number, MemoryError>> {\n return this.base.prune(olderThan);\n }\n\n // IAgenticMemory Methods\n async storeWithAttributes(\n key: string,\n value: unknown,\n metadata: MemoryMetadata\n ): Promise<Result<AgenticStoreResult, MemoryError>> {\n try {\n this.ensureInit();\n const attributes = extractAttributes(value, this.extractionConfig);\n const extendedMetadata = { ...metadata, amem: { ...attributes } };\n\n const storeResult = await this.base.store(key, value, extendedMetadata);\n if (!storeResult.ok) return storeResult;\n\n const existingMemories = queryMemoriesForAnalysis(this.getDb(), key, this.extractionConfig);\n const now = new Date(getTimeProvider().now());\n const linkSuggestions = generateLinkSuggestions(\n key,\n attributes,\n now,\n existingMemories,\n this.linkingConfig\n );\n const evolution = detectEvolution(key, attributes, now, existingMemories);\n const entry = buildAgenticEntry(key, value, extendedMetadata, attributes, now);\n\n this.log.debug('Stored with attributes', { key, keywords: attributes.keywords.length });\n return ok({ entry, linkSuggestions, evolution });\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return err(new MemoryError('Failed to store with attributes', { cause }));\n }\n }\n\n retrieveWithAttributes(key: string): Promise<Result<AgenticMemoryEntry | null, MemoryError>> {\n try {\n this.ensureInit();\n const row = queryMemoryByKey(this.getDb(), key);\n if (row === undefined) return Promise.resolve(ok(null));\n const entry = memoryRowToAgenticEntry(row, this.extractionConfig);\n return Promise.resolve(ok(entry));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to retrieve with attributes', { cause })));\n }\n }\n\n searchAgentic(query: string, limit = 10): Promise<Result<AgenticMemoryEntry[], MemoryError>> {\n try {\n this.ensureInit();\n const entries = searchWithAttributes(this.getDb(), query, limit, this.extractionConfig);\n return Promise.resolve(ok(entries));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to search agentic', { cause })));\n }\n }\n\n suggestLinks(key: string, limit?: number): Promise<Result<LinkSuggestion[], MemoryError>> {\n try {\n this.ensureInit();\n const sourceRow = queryMemoryByKey(this.getDb(), key);\n if (sourceRow === undefined)\n return Promise.resolve(err(new MemoryError(`Memory not found: ${key}`)));\n\n const sourceAttrs = getAttributesFromRow(sourceRow, this.extractionConfig);\n const candidates = queryCandidateMemories(this.getDb(), key, this.extractionConfig);\n const maxSuggestions = limit ?? this.linkingConfig.maxSuggestions;\n const suggestions = generateLinkSuggestions(\n key,\n sourceAttrs,\n new Date(sourceRow.created_at),\n candidates,\n { ...this.linkingConfig, maxSuggestions }\n );\n return Promise.resolve(ok(suggestions));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to suggest links', { cause })));\n }\n }\n\n async linkRelatedMemories(\n key: string,\n opts?: LinkingOptions\n ): Promise<Result<number, MemoryError>> {\n try {\n this.ensureInit();\n const { threshold = 0.7, maxLinks, bidirectional = true } = opts ?? {};\n const limit = maxLinks ?? this.linkingConfig.maxSuggestions;\n\n const suggestionsResult = await this.suggestLinks(key, limit);\n if (!suggestionsResult.ok) return suggestionsResult;\n\n const filtered = suggestionsResult.value.filter((s) => s.confidence >= threshold);\n const linksCreated = await applyLinkSuggestions(this.graph, filtered, bidirectional);\n\n this.log.debug('Linked related memories', { key, linksCreated, threshold });\n return ok(linksCreated);\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return err(new MemoryError('Failed to link related memories', { cause }));\n }\n }\n\n detectEvolution(key: string): Promise<Result<EvolutionResult[], MemoryError>> {\n try {\n this.ensureInit();\n const sourceRow = queryMemoryByKey(this.getDb(), key);\n if (sourceRow === undefined)\n return Promise.resolve(err(new MemoryError(`Memory not found: ${key}`)));\n\n const sourceAttrs = getAttributesFromRow(sourceRow, this.extractionConfig);\n const existingMemories = queryMemoriesForEvolution(this.getDb(), key, this.extractionConfig);\n const evolution = detectEvolution(\n key,\n sourceAttrs,\n new Date(sourceRow.created_at),\n existingMemories\n );\n return Promise.resolve(ok(evolution));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to detect evolution', { cause })));\n }\n }\n\n refreshAttributes(key: string): Promise<Result<MemoryAttributes, MemoryError>> {\n try {\n this.ensureInit();\n const row = queryMemoryByKey(this.getDb(), key);\n if (row === undefined)\n return Promise.resolve(err(new MemoryError(`Memory not found: ${key}`)));\n\n const value = JSON.parse(row.value) as unknown;\n const attributes = extractAttributes(value, this.extractionConfig);\n const currentMeta = JSON.parse(row.metadata) as Record<string, unknown>;\n const updatedMeta = prepareRefreshedMetadata(currentMeta, attributes);\n updateMemoryMetadata(this.getDb(), key, updatedMeta);\n\n this.log.debug('Refreshed attributes', { key, keywords: attributes.keywords.length });\n return Promise.resolve(ok(attributes));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(err(new MemoryError('Failed to refresh attributes', { cause })));\n }\n }\n\n findBySharedAttributes(\n key: string,\n attributeType: 'keywords' | 'semanticTags' | 'entities',\n limit = 10\n ): Promise<Result<AgenticMemoryEntry[], MemoryError>> {\n try {\n this.ensureInit();\n const sourceRow = queryMemoryByKey(this.getDb(), key);\n if (sourceRow === undefined)\n return Promise.resolve(err(new MemoryError(`Memory not found: ${key}`)));\n\n const sourceAttrs = getAttributesFromRow(sourceRow, this.extractionConfig);\n const sourceSet = getAttributeSet(sourceAttrs, attributeType);\n if (sourceSet.size === 0) return Promise.resolve(ok([]));\n\n const allRows = queryAllMemoriesExcept(this.getDb(), key);\n const matches = findMatchingMemories(\n allRows,\n sourceSet,\n attributeType,\n this.extractionConfig\n );\n return Promise.resolve(ok(matches.slice(0, limit).map((m) => m.entry)));\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return Promise.resolve(\n err(new MemoryError('Failed to find by shared attributes', { cause }))\n );\n }\n }\n\n // Configuration getters and setters\n getExtractionConfig(): ExtractionConfig {\n return this.extractionConfig;\n }\n updateExtractionConfig(config: Partial<ExtractionConfig>): void {\n this.extractionConfig = mergeExtractionConfig({ ...this.extractionConfig, ...config });\n this.log.info('Updated extraction config');\n }\n getLinkingConfig(): LinkingConfig {\n return this.linkingConfig;\n }\n updateLinkingConfig(config: Partial<LinkingConfig>): void {\n this.linkingConfig = mergeLinkingConfig({ ...this.linkingConfig, ...config });\n this.log.info('Updated linking config');\n }\n\n close(): void {\n this.base.close();\n this.graph.close();\n if (this.db !== null) {\n this.db.close();\n this.db = null;\n }\n this.initialized = false;\n this.log.info('AgenticMemoryBackend closed');\n }\n}\n\n/** Create an AgenticMemoryBackend instance. */\nexport function createAgenticMemory(config: AgenticMemoryConfig): AgenticMemoryBackend {\n return new AgenticMemoryBackend(config);\n}\n","/**\n * nexus-agents/context - Belief Memory Persistence\n *\n * Handles disk serialization/deserialization for HindsightBeliefMemory.\n * Converts in-memory Maps to JSON snapshots and back. Date fields are\n * serialized as ISO strings and restored on load.\n *\n * @module context/belief-memory-persistence\n * (Source: Issue #714 Phase 3 - Unified memory persistence)\n */\n\nimport * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { nexusDataPath } from '../config/nexus-data-dir.js';\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport type { ILogger } from '../core/logger.js';\nimport type { Belief } from './belief-core-types.js';\nimport type { BeliefUpdate } from './belief-update-types.js';\nimport type { Counterfactual, HindsightRecord } from './belief-hindsight-types.js';\nimport {\n BeliefSnapshotSchema,\n type BeliefSnapshot,\n type BeliefMemoryData,\n type HydratedBeliefData,\n type SerializedBelief,\n type SerializedBeliefUpdate,\n type SerializedCounterfactual,\n} from './belief-persistence-types.js';\nimport { getErrorMessage } from '../core/index.js';\n\n// Re-export types for consumers\nexport type {\n BeliefSnapshot,\n BeliefMemoryData,\n HydratedBeliefData,\n} from './belief-persistence-types.js';\n\n// ============================================================================\n// Constants\n// ============================================================================\n\n/** Directory for belief memory snapshots. Resolved at call time via getBeliefsDir(). */\nfunction getBeliefsDir(): string {\n return nexusDataPath('memory', 'beliefs');\n}\nconst MAX_SNAPSHOT_FILES = 10;\nconst SNAPSHOT_VERSION = 1;\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\n/** Conditionally add an optional property to an object. */\nfunction optProp<K extends string, V>(\n key: K,\n value: V | undefined\n): { [P in K]: V } | Record<string, never> {\n if (value === undefined) return {};\n return { [key]: value } as { [P in K]: V };\n}\n\n// ============================================================================\n// Serialization\n// ============================================================================\n\n/** Convert a Belief to its serialized form (Date → ISO string). */\nfunction serializeBelief(b: Belief): SerializedBelief {\n return {\n beliefId: b.beliefId,\n subject: b.subject,\n predicate: b.predicate,\n object: b.object,\n confidence: b.confidence,\n sourceType: b.sourceType,\n version: b.version,\n superseded: b.superseded,\n createdAt: b.createdAt.toISOString(),\n updatedAt: b.updatedAt.toISOString(),\n ...optProp('sourceRef', b.sourceRef),\n ...optProp('derivedFrom', b.derivedFrom),\n ...optProp('supersededBy', b.supersededBy),\n ...optProp('domain', b.domain),\n ...optProp('metadata', b.metadata),\n };\n}\n\n/** Safely extract a string from an unknown value. */\nfunction str(val: unknown, fallback = ''): string {\n return typeof val === 'string' ? val : fallback;\n}\n\n/** Convert a SerializedBelief back to Belief (ISO string → Date). */\nfunction deserializeBelief(s: { [k: string]: unknown }): Belief {\n return {\n beliefId: str(s.beliefId),\n subject: str(s.subject),\n predicate: str(s.predicate),\n object: str(s.object),\n confidence: str(s.confidence, 'medium') as Belief['confidence'],\n sourceType: str(s.sourceType, 'inference') as Belief['sourceType'],\n version: typeof s.version === 'number' ? s.version : 0,\n superseded: s.superseded === true,\n createdAt: new Date(str(s.createdAt)),\n updatedAt: new Date(str(s.updatedAt)),\n ...optProp('sourceRef', typeof s.sourceRef === 'string' ? s.sourceRef : undefined),\n ...optProp(\n 'derivedFrom',\n Array.isArray(s.derivedFrom) ? (s.derivedFrom as readonly string[]) : undefined\n ),\n ...optProp('supersededBy', typeof s.supersededBy === 'string' ? s.supersededBy : undefined),\n ...optProp('domain', typeof s.domain === 'string' ? s.domain : undefined),\n ...optProp(\n 'metadata',\n typeof s.metadata === 'object' && s.metadata !== null\n ? (s.metadata as Record<string, unknown>)\n : undefined\n ),\n };\n}\n\n/** Serialize a Counterfactual to JSON-safe form. */\nfunction serializeCounterfactual(c: Counterfactual): SerializedCounterfactual {\n return {\n counterfactualId: c.counterfactualId,\n hypothesis: c.hypothesis,\n affectedBeliefs: c.affectedBeliefs,\n predictedOutcomes: c.predictedOutcomes,\n validated: c.validated,\n createdAt: c.createdAt.toISOString(),\n ...optProp('actualOutcomes', c.actualOutcomes),\n ...optProp('taskContext', c.taskContext),\n };\n}\n\n/** Create a snapshot from in-memory belief data. */\nexport function createSnapshot(data: BeliefMemoryData): BeliefSnapshot {\n return {\n version: SNAPSHOT_VERSION,\n exportedAt: new Date().toISOString(),\n beliefs: Array.from(data.beliefs.values()).map(serializeBelief),\n updates: Array.from(data.updates.entries()).map(([beliefId, records]) => ({\n beliefId,\n records: records.map(\n (u): SerializedBeliefUpdate => ({\n updateId: u.updateId,\n beliefId: u.beliefId,\n updateType: u.updateType,\n newState: u.newState,\n reason: u.reason,\n timestamp: u.timestamp.toISOString(),\n ...optProp('previousState', u.previousState as Record<string, unknown> | undefined),\n ...optProp('evidence', u.evidence),\n ...optProp('updatedBy', u.updatedBy),\n })\n ),\n })),\n counterfactuals: Array.from(data.counterfactuals.values()).map(serializeCounterfactual),\n hindsightRecords: Array.from(data.hindsightRecords.entries()).map(([taskId, records]) => ({\n taskId,\n records: records.map((r) => ({\n hindsightId: r.hindsightId,\n taskId: r.taskId,\n priorBeliefs: r.priorBeliefs,\n expectedOutcome: r.expectedOutcome,\n actualOutcome: r.actualOutcome,\n outcomeMatched: r.outcomeMatched,\n correctedBeliefs: r.correctedBeliefs,\n newBeliefs: r.newBeliefs,\n lessons: r.lessons,\n createdAt: r.createdAt.toISOString(),\n })),\n })),\n };\n}\n\n// ============================================================================\n// Hydration\n// ============================================================================\n\n/** Hydrate update records from serialized form. */\nfunction hydrateUpdates(entries: BeliefSnapshot['updates']): Map<string, BeliefUpdate[]> {\n const updates = new Map<string, BeliefUpdate[]>();\n for (const entry of entries) {\n updates.set(\n entry.beliefId,\n entry.records.map(\n (u): BeliefUpdate => ({\n updateId: u.updateId,\n beliefId: u.beliefId,\n updateType: u.updateType as BeliefUpdate['updateType'],\n newState: u.newState,\n reason: u.reason,\n timestamp: new Date(u.timestamp),\n ...optProp('previousState', u.previousState as Partial<Belief> | undefined),\n ...optProp('evidence', u.evidence),\n ...optProp('updatedBy', u.updatedBy),\n })\n )\n );\n }\n return updates;\n}\n\n/** Hydrate counterfactuals from serialized form. */\nfunction hydrateCounterfactuals(\n items: BeliefSnapshot['counterfactuals']\n): Map<string, Counterfactual> {\n const map = new Map<string, Counterfactual>();\n for (const c of items) {\n map.set(c.counterfactualId, {\n counterfactualId: c.counterfactualId,\n hypothesis: c.hypothesis,\n affectedBeliefs: [...c.affectedBeliefs],\n predictedOutcomes: [...c.predictedOutcomes],\n validated: c.validated,\n createdAt: new Date(c.createdAt),\n ...optProp('actualOutcomes', c.actualOutcomes ? [...c.actualOutcomes] : undefined),\n ...optProp('taskContext', c.taskContext),\n });\n }\n return map;\n}\n\n/** Hydrate hindsight records from serialized form. */\nfunction hydrateHindsight(\n entries: BeliefSnapshot['hindsightRecords']\n): Map<string, HindsightRecord[]> {\n const map = new Map<string, HindsightRecord[]>();\n for (const entry of entries) {\n map.set(\n entry.taskId,\n entry.records.map((r) => ({\n hindsightId: r.hindsightId,\n taskId: r.taskId,\n priorBeliefs: [...r.priorBeliefs],\n expectedOutcome: r.expectedOutcome,\n actualOutcome: r.actualOutcome,\n outcomeMatched: r.outcomeMatched,\n correctedBeliefs: [...r.correctedBeliefs],\n newBeliefs: [...r.newBeliefs],\n lessons: [...r.lessons],\n createdAt: new Date(r.createdAt),\n }))\n );\n }\n return map;\n}\n\n/** Hydrate a validated snapshot back into Map structures. */\nexport function hydrateSnapshot(snapshot: BeliefSnapshot): HydratedBeliefData {\n const beliefs = new Map<string, Belief>();\n for (const s of snapshot.beliefs) {\n const belief = deserializeBelief(s as unknown as Record<string, unknown>);\n beliefs.set(belief.beliefId, belief);\n }\n return {\n beliefs,\n updates: hydrateUpdates(snapshot.updates),\n counterfactuals: hydrateCounterfactuals(snapshot.counterfactuals),\n hindsightRecords: hydrateHindsight(snapshot.hindsightRecords),\n };\n}\n\n// ============================================================================\n// Disk I/O\n// ============================================================================\n\nfunction ensureBeliefsDir(): void {\n if (!fs.existsSync(getBeliefsDir())) fs.mkdirSync(getBeliefsDir(), { recursive: true });\n}\n\nfunction getSnapshotFiles(): readonly string[] {\n try {\n return fs\n .readdirSync(getBeliefsDir())\n .filter((f) => f.startsWith('beliefs-') && f.endsWith('.json'))\n .sort()\n .reverse();\n } catch {\n return [];\n }\n}\n\nfunction enforceRetention(logger: ILogger): void {\n try {\n const files = getSnapshotFiles();\n if (files.length <= MAX_SNAPSHOT_FILES) return;\n const toDelete = files.slice(MAX_SNAPSHOT_FILES);\n for (const file of toDelete) fs.unlinkSync(path.join(getBeliefsDir(), file));\n logger.debug('Belief snapshot retention enforced', {\n kept: MAX_SNAPSHOT_FILES,\n deleted: toDelete.length,\n });\n } catch (error: unknown) {\n logger.debug('Belief snapshot retention cleanup failed', {\n error: getErrorMessage(error),\n });\n }\n}\n\n/** Save belief memory data to disk as a JSON snapshot. */\nexport function saveBeliefSnapshot(data: BeliefMemoryData, logger: ILogger): Result<string, Error> {\n try {\n ensureBeliefsDir();\n const snapshot = createSnapshot(data);\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\n const filename = `beliefs-${timestamp}.json`;\n const filepath = path.join(getBeliefsDir(), filename);\n fs.writeFileSync(filepath, JSON.stringify(snapshot, null, 2), 'utf-8');\n logger.info('Belief memory snapshot saved', { filename, beliefs: snapshot.beliefs.length });\n enforceRetention(logger);\n return ok(filepath);\n } catch (error) {\n const message = getErrorMessage(error);\n return err(new Error(`Failed to save belief snapshot: ${message}`));\n }\n}\n\n/** Load the most recent belief memory snapshot from disk. */\nexport function loadBeliefSnapshot(logger: ILogger): Result<HydratedBeliefData | null, Error> {\n try {\n ensureBeliefsDir();\n const files = getSnapshotFiles();\n if (files.length === 0) {\n logger.debug('No belief snapshots found');\n return ok(null);\n }\n for (const file of files.slice(0, 3)) {\n try {\n const filepath = path.join(getBeliefsDir(), file);\n const raw = JSON.parse(fs.readFileSync(filepath, 'utf-8')) as unknown;\n const validation = BeliefSnapshotSchema.safeParse(raw);\n if (!validation.success) {\n logger.warn('Invalid belief snapshot file', { file, errors: validation.error.issues });\n continue;\n }\n // Cast required due to exactOptionalPropertyTypes — Zod validated the data\n const hydrated = hydrateSnapshot(validation.data);\n logger.info('Belief memory snapshot loaded', { file, beliefs: hydrated.beliefs.size });\n return ok(hydrated);\n } catch (error: unknown) {\n logger.warn('Failed to load belief snapshot file', {\n file,\n error: getErrorMessage(error),\n });\n }\n }\n logger.warn('All recent belief snapshots are invalid');\n return ok(null);\n } catch (error) {\n const message = getErrorMessage(error);\n return err(new Error(`Failed to load belief snapshot: ${message}`));\n }\n}\n","/**\n * nexus-agents/context - Belief Persistence Types & Schema\n *\n * Type definitions, Zod validation schema, and data interfaces\n * for belief memory disk persistence.\n *\n * @module context/belief-persistence-types\n * (Source: Issue #714 Phase 3 - Unified memory persistence)\n */\n\nimport { z } from 'zod';\nimport type { Belief } from './belief-core-types.js';\nimport { BeliefConfidenceSchema, BeliefSourceTypeSchema } from './belief-core-types.js';\nimport type { BeliefUpdate } from './belief-update-types.js';\nimport { BeliefUpdateTypeSchema } from './belief-update-types.js';\nimport type { Counterfactual, HindsightRecord } from './belief-hindsight-types.js';\n\n// ============================================================================\n// Serialized Types (Date → string for JSON)\n// ============================================================================\n\n/** Belief with Date fields as ISO strings. */\nexport interface SerializedBelief {\n readonly beliefId: string;\n readonly subject: string;\n readonly predicate: string;\n readonly object: string;\n readonly confidence: string;\n readonly sourceType: string;\n readonly sourceRef?: string | undefined;\n readonly derivedFrom?: readonly string[] | undefined;\n readonly version: number;\n readonly createdAt: string;\n readonly updatedAt: string;\n readonly superseded: boolean;\n readonly supersededBy?: string | undefined;\n readonly domain?: string | undefined;\n readonly metadata?: Record<string, unknown> | undefined;\n}\n\n/** BeliefUpdate with Date fields as ISO strings. */\nexport interface SerializedBeliefUpdate {\n readonly updateId: string;\n readonly beliefId: string;\n readonly updateType: string;\n readonly previousState?: Record<string, unknown> | undefined;\n readonly newState: Record<string, unknown>;\n readonly reason: string;\n readonly evidence?: string | undefined;\n readonly timestamp: string;\n readonly updatedBy?: string | undefined;\n}\n\n/** Counterfactual with Date fields as ISO strings. */\nexport interface SerializedCounterfactual {\n readonly counterfactualId: string;\n readonly hypothesis: string;\n readonly affectedBeliefs: readonly string[];\n readonly predictedOutcomes: readonly string[];\n readonly actualOutcomes?: readonly string[] | undefined;\n readonly validated: boolean;\n readonly createdAt: string;\n readonly taskContext?: string | undefined;\n}\n\n/** HindsightRecord with Date fields as ISO strings. */\nexport interface SerializedHindsightRecord {\n readonly hindsightId: string;\n readonly taskId: string;\n readonly priorBeliefs: readonly string[];\n readonly expectedOutcome: string;\n readonly actualOutcome: string;\n readonly outcomeMatched: boolean;\n readonly correctedBeliefs: readonly string[];\n readonly newBeliefs: readonly string[];\n readonly lessons: readonly string[];\n readonly createdAt: string;\n}\n\n// ============================================================================\n// Snapshot Type\n// ============================================================================\n\n/** Full snapshot of belief memory state for disk persistence. */\nexport interface BeliefSnapshot {\n readonly version: number;\n readonly exportedAt: string;\n readonly beliefs: readonly SerializedBelief[];\n readonly updates: ReadonlyArray<{\n beliefId: string;\n records: readonly SerializedBeliefUpdate[];\n }>;\n readonly counterfactuals: readonly SerializedCounterfactual[];\n readonly hindsightRecords: ReadonlyArray<{\n taskId: string;\n records: readonly SerializedHindsightRecord[];\n }>;\n}\n\n// ============================================================================\n// Data Interfaces\n// ============================================================================\n\n/** Raw data extracted from HindsightBeliefMemory for serialization. */\nexport interface BeliefMemoryData {\n readonly beliefs: ReadonlyMap<string, Belief>;\n readonly updates: ReadonlyMap<string, readonly BeliefUpdate[]>;\n readonly counterfactuals: ReadonlyMap<string, Counterfactual>;\n readonly hindsightRecords: ReadonlyMap<string, readonly HindsightRecord[]>;\n}\n\n/** Deserialized data ready to hydrate into HindsightBeliefMemory. */\nexport interface HydratedBeliefData {\n readonly beliefs: Map<string, Belief>;\n readonly updates: Map<string, BeliefUpdate[]>;\n readonly counterfactuals: Map<string, Counterfactual>;\n readonly hindsightRecords: Map<string, HindsightRecord[]>;\n}\n\n// ============================================================================\n// Zod Schema for Validation\n// ============================================================================\n\n/** Zod schema for snapshot validation on load. */\nexport const BeliefSnapshotSchema = z.object({\n version: z.number().int().min(1),\n exportedAt: z.string(),\n beliefs: z.array(\n z.object({\n beliefId: z.string(),\n subject: z.string(),\n predicate: z.string(),\n object: z.string(),\n confidence: BeliefConfidenceSchema,\n sourceType: BeliefSourceTypeSchema,\n sourceRef: z.string().optional(),\n derivedFrom: z.array(z.string()).optional(),\n version: z.number(),\n createdAt: z.string(),\n updatedAt: z.string(),\n superseded: z.boolean(),\n supersededBy: z.string().optional(),\n domain: z.string().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n })\n ),\n updates: z.array(\n z.object({\n beliefId: z.string(),\n records: z.array(\n z.object({\n updateId: z.string(),\n beliefId: z.string(),\n updateType: BeliefUpdateTypeSchema,\n previousState: z.record(z.string(), z.unknown()).optional(),\n newState: z.record(z.string(), z.unknown()),\n reason: z.string().max(10_000),\n evidence: z.string().max(10_000).optional(),\n timestamp: z.string(),\n updatedBy: z.string().optional(),\n })\n ),\n })\n ),\n counterfactuals: z.array(\n z.object({\n counterfactualId: z.string(),\n hypothesis: z.string().max(10_000),\n affectedBeliefs: z.array(z.string()),\n predictedOutcomes: z.array(z.string()),\n actualOutcomes: z.array(z.string()).optional(),\n validated: z.boolean(),\n createdAt: z.string(),\n taskContext: z.string().optional(),\n })\n ),\n hindsightRecords: z.array(\n z.object({\n taskId: z.string(),\n records: z.array(\n z.object({\n hindsightId: z.string(),\n taskId: z.string(),\n priorBeliefs: z.array(z.string()),\n expectedOutcome: z.string().max(10_000),\n actualOutcome: z.string().max(10_000),\n outcomeMatched: z.boolean(),\n correctedBeliefs: z.array(z.string()),\n newBeliefs: z.array(z.string()),\n lessons: z.array(z.string()),\n createdAt: z.string(),\n })\n ),\n })\n ),\n});\n","/**\n * nexus-agents/context - Typed Memory Architecture\n *\n * Implements MIRIX-style typed memory system with six distinct memory types\n * for improved agent coordination and context management.\n *\n * @module context/memory-types\n * (Source: Issue #101, arXiv:2507.07957 - MIRIX Architecture)\n */\n\nimport { z } from 'zod';\nimport type { Result } from '../core/result.js';\nimport type { AgentRole } from '../core/types/agent.js';\nimport type { MemoryError, MemoryMetadata } from './memory-backend-types.js';\n\n// Re-export all memory module types for backward compatibility\nexport type {\n CoreMemoryData,\n ICoreMemory,\n EpisodeData,\n IEpisodicMemory,\n SemanticFact,\n ISemanticMemory,\n ProcedureStep,\n Procedure,\n IProceduralMemory,\n ResourceReference,\n IResourceMemory,\n VaultEntry,\n IKnowledgeVault,\n} from './memory-module-types.js';\n\nexport {\n CoreMemoryDataSchema,\n EpisodeDataSchema,\n SemanticFactSchema,\n ProcedureStepSchema,\n ProcedureSchema,\n ResourceReferenceSchema,\n VaultEntrySchema,\n} from './memory-module-types.js';\n\n// Re-export Hindsight Belief Memory types (arXiv:2512.12818)\nexport type {\n Belief,\n BeliefConfidence,\n BeliefMemoryConfig,\n BeliefMemoryStats,\n BeliefQuery,\n BeliefSourceType,\n BeliefUpdate,\n BeliefUpdateType,\n Counterfactual,\n HindsightRecord,\n IHindsightBeliefMemory,\n} from './belief-types.js';\n\nexport {\n BeliefConfidence as BeliefConfidenceEnum,\n BeliefConfidenceSchema,\n BeliefMemoryConfigSchema,\n BeliefMemoryStatsSchema,\n BeliefQuerySchema,\n BeliefSchema,\n BeliefSourceType as BeliefSourceTypeEnum,\n BeliefSourceTypeSchema,\n BeliefUpdateSchema,\n BeliefUpdateType as BeliefUpdateTypeEnum,\n BeliefUpdateTypeSchema,\n CounterfactualSchema,\n DEFAULT_BELIEF_CONFIG,\n HindsightRecordSchema,\n} from './belief-types.js';\n\n// Import interfaces for use in ITypedMemory\nimport type {\n ICoreMemory,\n IEpisodicMemory,\n ISemanticMemory,\n IProceduralMemory,\n IResourceMemory,\n IKnowledgeVault,\n} from './memory-module-types.js';\n\n// Import belief memory interface for extended typed memory\nimport type { IHindsightBeliefMemory } from './belief-types.js';\n\n// ============================================================================\n// Memory Type Enum\n// ============================================================================\n\n/**\n * Seven distinct memory types: six from MIRIX architecture plus Belief Memory.\n * (Source: arXiv:2507.07957 - MIRIX, arXiv:2512.12818 - Hindsight Belief Memory)\n */\nexport const MemoryType = {\n CORE: 'core',\n EPISODIC: 'episodic',\n SEMANTIC: 'semantic',\n PROCEDURAL: 'procedural',\n RESOURCE: 'resource',\n VAULT: 'vault',\n /** Hindsight Belief Memory for reasoning agents (arXiv:2512.12818) */\n BELIEF: 'belief',\n} as const;\n\nexport type MemoryType = (typeof MemoryType)[keyof typeof MemoryType];\n\nexport const MemoryTypeSchema = z.enum([\n 'core',\n 'episodic',\n 'semantic',\n 'procedural',\n 'resource',\n 'vault',\n 'belief',\n]);\n\n// ============================================================================\n// Typed Memory Entry\n// ============================================================================\n\n/**\n * Base typed memory entry with type discrimination.\n */\nexport interface TypedMemoryEntry<T extends MemoryType = MemoryType> {\n readonly id: string;\n readonly type: T;\n readonly key: string;\n readonly value: unknown;\n readonly metadata: MemoryMetadata;\n readonly createdAt: Date;\n readonly accessedAt: Date;\n readonly agentId?: string;\n readonly relevanceScore?: number;\n}\n\n// ============================================================================\n// Typed Memory Interface\n// ============================================================================\n\n/**\n * Unified typed memory interface providing access to all seven memory types.\n * (Source: Issue #101, arXiv:2507.07957 - MIRIX, arXiv:2512.12818 - Hindsight)\n */\nexport interface ITypedMemory {\n readonly core: ICoreMemory;\n readonly episodic: IEpisodicMemory;\n readonly semantic: ISemanticMemory;\n readonly procedural: IProceduralMemory;\n readonly resource: IResourceMemory;\n readonly vault: IKnowledgeVault;\n /** Hindsight Belief Memory for reasoning agents (arXiv:2512.12818) */\n readonly belief: IHindsightBeliefMemory;\n\n /** Query entries by memory type */\n queryByType(\n type: MemoryType,\n query: string,\n limit?: number\n ): Promise<Result<readonly TypedMemoryEntry[], MemoryError>>;\n\n /** Filter memories by relevance to an agent role */\n filterByRelevance(\n agentRole: AgentRole,\n limit?: number\n ): Promise<Result<readonly TypedMemoryEntry[], MemoryError>>;\n\n /** Get memory statistics across all types */\n getStats(): Promise<Result<TypedMemoryStats, MemoryError>>;\n\n /** Prune expired entries across all memory types */\n pruneExpired(): Promise<Result<TypedMemoryPruneResult, MemoryError>>;\n}\n\n/**\n * Statistics for typed memory usage.\n */\nexport interface TypedMemoryStats {\n readonly totalEntries: number;\n readonly entriesByType: Record<MemoryType, number>;\n readonly oldestEntry?: Date;\n readonly newestEntry?: Date;\n readonly totalSizeBytes?: number;\n}\n\n/**\n * Result of pruning expired entries.\n */\nexport interface TypedMemoryPruneResult {\n readonly prunedCount: number;\n readonly prunedByType: Record<MemoryType, number>;\n readonly freedBytes?: number;\n}\n\n// ============================================================================\n// Relevance Filter Configuration\n// ============================================================================\n\n/**\n * Configuration for role-based memory filtering.\n */\nexport interface RelevanceFilterConfig {\n /** Memory types relevant to each role */\n readonly roleMemoryTypes: Record<AgentRole, readonly MemoryType[]>;\n /** Minimum relevance score to include (0-1) */\n readonly minRelevanceScore: number;\n /** Maximum entries to return per type */\n readonly maxEntriesPerType: number;\n}\n\n/**\n * Default relevance filter configuration.\n * Belief memory is relevant for roles that require reasoning and decision-making.\n */\nexport const DEFAULT_RELEVANCE_CONFIG: RelevanceFilterConfig = {\n roleMemoryTypes: {\n orchestrator: [MemoryType.CORE, MemoryType.EPISODIC, MemoryType.VAULT, MemoryType.BELIEF],\n code_expert: [MemoryType.PROCEDURAL, MemoryType.RESOURCE, MemoryType.EPISODIC],\n architecture_expert: [\n MemoryType.SEMANTIC,\n MemoryType.RESOURCE,\n MemoryType.VAULT,\n MemoryType.BELIEF,\n ],\n security_expert: [\n MemoryType.SEMANTIC,\n MemoryType.VAULT,\n MemoryType.PROCEDURAL,\n MemoryType.BELIEF,\n ],\n documentation_expert: [MemoryType.SEMANTIC, MemoryType.RESOURCE, MemoryType.EPISODIC],\n testing_expert: [MemoryType.SEMANTIC, MemoryType.EPISODIC, MemoryType.VAULT],\n devops_expert: [\n MemoryType.PROCEDURAL,\n MemoryType.RESOURCE,\n MemoryType.EPISODIC,\n MemoryType.VAULT,\n ],\n research_expert: [\n MemoryType.SEMANTIC,\n MemoryType.RESOURCE,\n MemoryType.VAULT,\n MemoryType.EPISODIC,\n ],\n pm_expert: [MemoryType.SEMANTIC, MemoryType.EPISODIC, MemoryType.CORE, MemoryType.BELIEF],\n ux_expert: [MemoryType.SEMANTIC, MemoryType.EPISODIC, MemoryType.RESOURCE],\n infrastructure_expert: [\n MemoryType.PROCEDURAL,\n MemoryType.RESOURCE,\n MemoryType.EPISODIC,\n MemoryType.VAULT,\n ],\n qa_expert: [MemoryType.SEMANTIC, MemoryType.EPISODIC, MemoryType.PROCEDURAL, MemoryType.BELIEF],\n data_visualization_expert: [MemoryType.SEMANTIC, MemoryType.RESOURCE, MemoryType.EPISODIC],\n custom: [MemoryType.SEMANTIC, MemoryType.PROCEDURAL, MemoryType.RESOURCE],\n // TRINITY roles (arXiv:2512.04695) - thinker and verifier benefit from belief memory\n thinker: [MemoryType.SEMANTIC, MemoryType.CORE, MemoryType.VAULT, MemoryType.BELIEF],\n worker: [MemoryType.PROCEDURAL, MemoryType.RESOURCE, MemoryType.EPISODIC],\n verifier: [MemoryType.SEMANTIC, MemoryType.VAULT, MemoryType.PROCEDURAL, MemoryType.BELIEF],\n },\n minRelevanceScore: 0.5,\n maxEntriesPerType: 10,\n};\n","/**\n * nexus-agents/context - Memory Module Types\n *\n * Individual memory module interfaces and data types for the MIRIX-style\n * typed memory system. These are the building blocks for ITypedMemory.\n *\n * @module context/memory-module-types\n * (Source: Issue #101, arXiv:2507.07957 - MIRIX Architecture)\n */\n\nimport { z } from 'zod';\nimport type { Result } from '../core/result.js';\nimport type { AgentRole } from '../core/types/agent.js';\nimport type { MemoryError } from './memory-backend-types.js';\n\n// ============================================================================\n// Core Memory - Agent Identity\n// ============================================================================\n\n/**\n * Core memory stores agent identity, constraints, and role definitions.\n * This is the most persistent memory type, rarely modified after initialization.\n */\nexport interface CoreMemoryData {\n readonly agentId: string;\n readonly role: AgentRole;\n readonly name: string;\n readonly constraints: readonly string[];\n readonly capabilities: readonly string[];\n readonly systemPrompt?: string;\n readonly temperament?: 'cautious' | 'balanced' | 'exploratory';\n}\n\nexport const CoreMemoryDataSchema = z.object({\n agentId: z.string().min(1),\n role: z.string(),\n name: z.string().min(1),\n constraints: z.array(z.string()),\n capabilities: z.array(z.string()),\n systemPrompt: z.string().max(50_000).optional(),\n temperament: z.enum(['cautious', 'balanced', 'exploratory']).optional(),\n});\n\nexport interface ICoreMemory {\n getIdentity(agentId: string): Promise<Result<CoreMemoryData | null, MemoryError>>;\n setIdentity(data: CoreMemoryData): Promise<Result<void, MemoryError>>;\n getConstraints(agentId: string): Promise<Result<readonly string[], MemoryError>>;\n updateConstraints(\n agentId: string,\n constraints: readonly string[]\n ): Promise<Result<void, MemoryError>>;\n}\n\n// ============================================================================\n// Episodic Memory - Task Experiences\n// ============================================================================\n\n/**\n * Episodic memory stores task experiences and interaction history.\n * Used for learning from past interactions and avoiding repeated mistakes.\n */\nexport interface EpisodeData {\n readonly episodeId: string;\n readonly taskId: string;\n readonly agentId: string;\n readonly action: string;\n readonly outcome: 'success' | 'failure' | 'partial';\n readonly context: Record<string, unknown>;\n readonly learnings?: readonly string[];\n readonly timestamp: Date;\n readonly durationMs?: number;\n}\n\nexport const EpisodeDataSchema = z.object({\n episodeId: z.string().min(1),\n taskId: z.string().min(1),\n agentId: z.string().min(1),\n action: z.string().min(1),\n outcome: z.enum(['success', 'failure', 'partial']),\n context: z.record(z.string(), z.unknown()),\n learnings: z.array(z.string()).optional(),\n timestamp: z.date(),\n durationMs: z.number().positive().optional(),\n});\n\nexport interface IEpisodicMemory {\n recordEpisode(episode: EpisodeData): Promise<Result<void, MemoryError>>;\n getEpisodes(\n agentId: string,\n limit?: number\n ): Promise<Result<readonly EpisodeData[], MemoryError>>;\n getEpisodesByTask(taskId: string): Promise<Result<readonly EpisodeData[], MemoryError>>;\n getRecentFailures(\n agentId: string,\n limit?: number\n ): Promise<Result<readonly EpisodeData[], MemoryError>>;\n searchEpisodes(\n query: string,\n limit?: number\n ): Promise<Result<readonly EpisodeData[], MemoryError>>;\n}\n\n// ============================================================================\n// Semantic Memory - Domain Knowledge\n// ============================================================================\n\n/**\n * Semantic memory stores domain facts and learned information.\n * Used for general knowledge that applies across tasks.\n */\nexport interface SemanticFact {\n readonly factId: string;\n readonly domain: string;\n readonly subject: string;\n readonly predicate: string;\n readonly object: string;\n readonly confidence: number;\n readonly source?: string;\n readonly validUntil?: Date;\n}\n\nexport const SemanticFactSchema = z.object({\n factId: z.string().min(1),\n domain: z.string().min(1),\n subject: z.string().min(1),\n predicate: z.string().min(1),\n object: z.string(),\n confidence: z.number().min(0).max(1),\n source: z.string().optional(),\n validUntil: z.date().optional(),\n});\n\nexport interface ISemanticMemory {\n storeFact(fact: SemanticFact): Promise<Result<void, MemoryError>>;\n getFact(factId: string): Promise<Result<SemanticFact | null, MemoryError>>;\n queryByDomain(\n domain: string,\n limit?: number\n ): Promise<Result<readonly SemanticFact[], MemoryError>>;\n queryBySubject(\n subject: string,\n limit?: number\n ): Promise<Result<readonly SemanticFact[], MemoryError>>;\n searchFacts(query: string, limit?: number): Promise<Result<readonly SemanticFact[], MemoryError>>;\n invalidateFact(factId: string): Promise<Result<void, MemoryError>>;\n}\n\n// ============================================================================\n// Procedural Memory - Skills and Workflows\n// ============================================================================\n\n/**\n * Procedural memory stores skills, workflows, and action patterns.\n * Used for learned procedures that can be reused across tasks.\n */\nexport interface ProcedureStep {\n readonly stepId: string;\n readonly action: string;\n readonly parameters?: Record<string, unknown>;\n readonly preconditions?: readonly string[];\n readonly postconditions?: readonly string[];\n}\n\nexport interface Procedure {\n readonly procedureId: string;\n readonly name: string;\n readonly description: string;\n readonly steps: readonly ProcedureStep[];\n readonly triggerConditions: readonly string[];\n readonly successRate: number;\n readonly executionCount: number;\n readonly averageDurationMs?: number;\n readonly tags?: readonly string[];\n}\n\nexport const ProcedureStepSchema = z.object({\n stepId: z.string().min(1),\n action: z.string().min(1),\n parameters: z.record(z.string(), z.unknown()).optional(),\n preconditions: z.array(z.string()).optional(),\n postconditions: z.array(z.string()).optional(),\n});\n\nexport const ProcedureSchema = z.object({\n procedureId: z.string().min(1),\n name: z.string().min(1),\n description: z.string().max(10_000),\n steps: z.array(ProcedureStepSchema).max(100),\n triggerConditions: z.array(z.string()),\n successRate: z.number().min(0).max(1),\n executionCount: z.number().int().min(0),\n averageDurationMs: z.number().positive().optional(),\n tags: z.array(z.string()).optional(),\n});\n\nexport interface IProceduralMemory {\n storeProcedure(procedure: Procedure): Promise<Result<void, MemoryError>>;\n getProcedure(procedureId: string): Promise<Result<Procedure | null, MemoryError>>;\n findProcedures(\n triggerContext: string,\n limit?: number\n ): Promise<Result<readonly Procedure[], MemoryError>>;\n updateSuccessRate(procedureId: string, success: boolean): Promise<Result<void, MemoryError>>;\n searchProcedures(\n query: string,\n limit?: number\n ): Promise<Result<readonly Procedure[], MemoryError>>;\n}\n\n// ============================================================================\n// Resource Memory - External References\n// ============================================================================\n\n/**\n * Resource memory stores references to external data (files, URLs, APIs).\n * Used for tracking data sources and their freshness.\n */\nexport interface ResourceReference {\n readonly resourceId: string;\n readonly type: 'file' | 'url' | 'api' | 'database' | 'other';\n readonly location: string;\n readonly name: string;\n readonly mimeType?: string;\n readonly size?: number;\n readonly hash?: string;\n readonly lastAccessed: Date;\n readonly lastModified?: Date;\n readonly metadata?: Record<string, unknown>;\n}\n\nexport const ResourceReferenceSchema = z.object({\n resourceId: z.string().min(1),\n type: z.enum(['file', 'url', 'api', 'database', 'other']),\n location: z.string().min(1),\n name: z.string().min(1),\n mimeType: z.string().optional(),\n size: z.number().int().positive().optional(),\n hash: z.string().optional(),\n lastAccessed: z.date(),\n lastModified: z.date().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n});\n\nexport interface IResourceMemory {\n storeResource(resource: ResourceReference): Promise<Result<void, MemoryError>>;\n getResource(resourceId: string): Promise<Result<ResourceReference | null, MemoryError>>;\n findByType(\n type: ResourceReference['type'],\n limit?: number\n ): Promise<Result<readonly ResourceReference[], MemoryError>>;\n findByLocation(\n locationPattern: string\n ): Promise<Result<readonly ResourceReference[], MemoryError>>;\n updateLastAccessed(resourceId: string): Promise<Result<void, MemoryError>>;\n searchResources(\n query: string,\n limit?: number\n ): Promise<Result<readonly ResourceReference[], MemoryError>>;\n}\n\n// ============================================================================\n// Knowledge Vault - Persistent Cross-Session Storage\n// ============================================================================\n\n/**\n * Knowledge vault stores persistent data that survives across sessions.\n * Used for long-term knowledge and critical information.\n */\nexport interface VaultEntry {\n readonly vaultId: string;\n readonly category: 'insight' | 'decision' | 'pattern' | 'config' | 'archive';\n readonly title: string;\n readonly content: unknown;\n readonly importance: 'critical' | 'high' | 'normal';\n readonly createdAt: Date;\n readonly updatedAt: Date;\n readonly expiresAt?: Date;\n readonly tags?: readonly string[];\n readonly relatedIds?: readonly string[];\n}\n\nexport const VaultEntrySchema = z.object({\n vaultId: z.string().min(1),\n category: z.enum(['insight', 'decision', 'pattern', 'config', 'archive']),\n title: z.string().min(1),\n content: z.unknown(),\n importance: z.enum(['critical', 'high', 'normal']),\n createdAt: z.date(),\n updatedAt: z.date(),\n expiresAt: z.date().optional(),\n tags: z.array(z.string()).optional(),\n relatedIds: z.array(z.string()).optional(),\n});\n\nexport interface IKnowledgeVault {\n store(entry: VaultEntry): Promise<Result<void, MemoryError>>;\n retrieve(vaultId: string): Promise<Result<VaultEntry | null, MemoryError>>;\n findByCategory(\n category: VaultEntry['category'],\n limit?: number\n ): Promise<Result<readonly VaultEntry[], MemoryError>>;\n findByImportance(\n importance: VaultEntry['importance'],\n limit?: number\n ): Promise<Result<readonly VaultEntry[], MemoryError>>;\n searchVault(query: string, limit?: number): Promise<Result<readonly VaultEntry[], MemoryError>>;\n archive(vaultId: string): Promise<Result<void, MemoryError>>;\n getExpired(): Promise<Result<readonly VaultEntry[], MemoryError>>;\n}\n","/**\n * Typed memory implementation classes.\n *\n * @module context/typed-memory-impl\n * (Source: Issue #101, arXiv:2507.07957 - MIRIX Architecture)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok, err } from '../core/result.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { IMemoryBackend, MemoryMetadata, MemoryError } from './memory-backend-types.js';\nimport { MemoryImportance, MemoryError as MemError } from './memory-backend-types.js';\nimport type {\n ICoreMemory,\n IEpisodicMemory,\n ISemanticMemory,\n IProceduralMemory,\n IResourceMemory,\n IKnowledgeVault,\n CoreMemoryData,\n EpisodeData,\n SemanticFact,\n Procedure,\n ResourceReference,\n VaultEntry,\n} from './memory-types.js';\n\n// ============================================================================\n// Core Memory Implementation\n// ============================================================================\n\nexport class CoreMemoryImpl implements ICoreMemory {\n constructor(private readonly backend: IMemoryBackend) {}\n\n async getIdentity(agentId: string): Promise<Result<CoreMemoryData | null, MemoryError>> {\n const result = await this.backend.retrieve(`core:identity:${agentId}`);\n if (!result.ok) return result;\n return ok(result.value as CoreMemoryData | null);\n }\n\n async setIdentity(data: CoreMemoryData): Promise<Result<void, MemoryError>> {\n const meta: MemoryMetadata = {\n importance: MemoryImportance.HIGH,\n tags: ['core', 'identity', data.role],\n };\n return this.backend.store(`core:identity:${data.agentId}`, data, meta);\n }\n\n async getConstraints(agentId: string): Promise<Result<readonly string[], MemoryError>> {\n const result = await this.getIdentity(agentId);\n if (!result.ok) return result;\n return ok(result.value?.constraints ?? []);\n }\n\n async updateConstraints(\n agentId: string,\n constraints: readonly string[]\n ): Promise<Result<void, MemoryError>> {\n const identity = await this.getIdentity(agentId);\n if (!identity.ok) return identity;\n if (identity.value === null) return err(new MemError(`Agent ${agentId} not found`));\n return this.setIdentity({ ...identity.value, constraints });\n }\n}\n\n// ============================================================================\n// Episodic Memory Implementation\n// ============================================================================\n\nexport class EpisodicMemoryImpl implements IEpisodicMemory {\n constructor(private readonly backend: IMemoryBackend) {}\n\n async recordEpisode(episode: EpisodeData): Promise<Result<void, MemoryError>> {\n const importance =\n episode.outcome === 'failure' ? MemoryImportance.HIGH : MemoryImportance.MEDIUM;\n const meta: MemoryMetadata = {\n importance,\n tags: ['episodic', episode.outcome, episode.agentId],\n };\n return this.backend.store(`episodic:${episode.episodeId}`, episode, meta);\n }\n\n async getEpisodes(\n agentId: string,\n limit = 20\n ): Promise<Result<readonly EpisodeData[], MemoryError>> {\n const result = await this.backend.search(`episodic ${agentId}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as EpisodeData).filter((e) => e.agentId === agentId));\n }\n\n async getEpisodesByTask(taskId: string): Promise<Result<readonly EpisodeData[], MemoryError>> {\n const result = await this.backend.search(`episodic ${taskId}`, 50);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as EpisodeData).filter((e) => e.taskId === taskId));\n }\n\n async getRecentFailures(\n agentId: string,\n limit = 10\n ): Promise<Result<readonly EpisodeData[], MemoryError>> {\n const result = await this.backend.search(`episodic failure ${agentId}`, limit * 2);\n if (!result.ok) return result;\n const failures = result.value\n .map((e) => e.value as EpisodeData)\n .filter((e) => e.outcome === 'failure' && e.agentId === agentId)\n .slice(0, limit);\n return ok(failures);\n }\n\n async searchEpisodes(\n query: string,\n limit = 20\n ): Promise<Result<readonly EpisodeData[], MemoryError>> {\n const result = await this.backend.search(`episodic ${query}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as EpisodeData));\n }\n}\n\n// ============================================================================\n// Semantic Memory Implementation\n// ============================================================================\n\nexport class SemanticMemoryImpl implements ISemanticMemory {\n constructor(private readonly backend: IMemoryBackend) {}\n\n async storeFact(fact: SemanticFact): Promise<Result<void, MemoryError>> {\n const meta: MemoryMetadata = {\n importance: MemoryImportance.MEDIUM,\n tags: ['semantic', fact.domain, fact.subject],\n };\n if (fact.validUntil !== undefined) {\n meta.ttl = fact.validUntil.getTime() - getTimeProvider().now();\n }\n return this.backend.store(`semantic:${fact.factId}`, fact, meta);\n }\n\n async getFact(factId: string): Promise<Result<SemanticFact | null, MemoryError>> {\n const result = await this.backend.retrieve(`semantic:${factId}`);\n if (!result.ok) return result;\n return ok(result.value as SemanticFact | null);\n }\n\n async queryByDomain(\n domain: string,\n limit = 20\n ): Promise<Result<readonly SemanticFact[], MemoryError>> {\n const result = await this.backend.search(`semantic ${domain}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as SemanticFact).filter((f) => f.domain === domain));\n }\n\n async queryBySubject(\n subject: string,\n limit = 20\n ): Promise<Result<readonly SemanticFact[], MemoryError>> {\n const result = await this.backend.search(`semantic ${subject}`, limit);\n if (!result.ok) return result;\n return ok(\n result.value.map((e) => e.value as SemanticFact).filter((f) => f.subject === subject)\n );\n }\n\n async searchFacts(\n query: string,\n limit = 20\n ): Promise<Result<readonly SemanticFact[], MemoryError>> {\n const result = await this.backend.search(`semantic ${query}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as SemanticFact));\n }\n\n async invalidateFact(factId: string): Promise<Result<void, MemoryError>> {\n const fact = await this.getFact(factId);\n if (!fact.ok) return fact;\n if (fact.value === null) return ok(undefined);\n const updated = { ...fact.value, validUntil: new Date(getTimeProvider().now()) };\n return this.storeFact(updated);\n }\n}\n\n// ============================================================================\n// Procedural Memory Implementation\n// ============================================================================\n\nexport class ProceduralMemoryImpl implements IProceduralMemory {\n constructor(private readonly backend: IMemoryBackend) {}\n\n async storeProcedure(procedure: Procedure): Promise<Result<void, MemoryError>> {\n const tags = ['procedural', procedure.name, ...(procedure.tags ?? [])];\n const meta: MemoryMetadata = { importance: MemoryImportance.MEDIUM, tags };\n return this.backend.store(`procedural:${procedure.procedureId}`, procedure, meta);\n }\n\n async getProcedure(procedureId: string): Promise<Result<Procedure | null, MemoryError>> {\n const result = await this.backend.retrieve(`procedural:${procedureId}`);\n if (!result.ok) return result;\n return ok(result.value as Procedure | null);\n }\n\n async findProcedures(\n triggerContext: string,\n limit = 10\n ): Promise<Result<readonly Procedure[], MemoryError>> {\n const result = await this.backend.search(`procedural ${triggerContext}`, limit * 2);\n if (!result.ok) return result;\n const procs = result.value\n .map((e) => e.value as Procedure)\n .filter((p) =>\n p.triggerConditions.some((t) => triggerContext.toLowerCase().includes(t.toLowerCase()))\n )\n .sort((a, b) => b.successRate - a.successRate)\n .slice(0, limit);\n return ok(procs);\n }\n\n async updateSuccessRate(\n procedureId: string,\n success: boolean\n ): Promise<Result<void, MemoryError>> {\n const proc = await this.getProcedure(procedureId);\n if (!proc.ok) return proc;\n if (proc.value === null) return err(new MemError(`Procedure ${procedureId} not found`));\n const count = proc.value.executionCount + 1;\n const rate = (proc.value.successRate * proc.value.executionCount + (success ? 1 : 0)) / count;\n return this.storeProcedure({ ...proc.value, successRate: rate, executionCount: count });\n }\n\n async searchProcedures(\n query: string,\n limit = 10\n ): Promise<Result<readonly Procedure[], MemoryError>> {\n const result = await this.backend.search(`procedural ${query}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as Procedure));\n }\n}\n\n// ============================================================================\n// Resource Memory Implementation\n// ============================================================================\n\nexport class ResourceMemoryImpl implements IResourceMemory {\n constructor(private readonly backend: IMemoryBackend) {}\n\n async storeResource(resource: ResourceReference): Promise<Result<void, MemoryError>> {\n const meta: MemoryMetadata = {\n importance: MemoryImportance.LOW,\n tags: ['resource', resource.type, resource.name],\n };\n return this.backend.store(`resource:${resource.resourceId}`, resource, meta);\n }\n\n async getResource(resourceId: string): Promise<Result<ResourceReference | null, MemoryError>> {\n const result = await this.backend.retrieve(`resource:${resourceId}`);\n if (!result.ok) return result;\n return ok(result.value as ResourceReference | null);\n }\n\n async findByType(\n type: ResourceReference['type'],\n limit = 20\n ): Promise<Result<readonly ResourceReference[], MemoryError>> {\n const result = await this.backend.search(`resource ${type}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as ResourceReference).filter((r) => r.type === type));\n }\n\n async findByLocation(\n locationPattern: string\n ): Promise<Result<readonly ResourceReference[], MemoryError>> {\n const result = await this.backend.search(`resource ${locationPattern}`, 50);\n if (!result.ok) return result;\n return ok(\n result.value\n .map((e) => e.value as ResourceReference)\n .filter((r) => r.location.includes(locationPattern))\n );\n }\n\n async updateLastAccessed(resourceId: string): Promise<Result<void, MemoryError>> {\n const res = await this.getResource(resourceId);\n if (!res.ok) return res;\n if (res.value === null) return ok(undefined);\n return this.storeResource({ ...res.value, lastAccessed: new Date(getTimeProvider().now()) });\n }\n\n async searchResources(\n query: string,\n limit = 20\n ): Promise<Result<readonly ResourceReference[], MemoryError>> {\n const result = await this.backend.search(`resource ${query}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as ResourceReference));\n }\n}\n\n// ============================================================================\n// Knowledge Vault Implementation\n// ============================================================================\n\nexport class KnowledgeVaultImpl implements IKnowledgeVault {\n constructor(private readonly backend: IMemoryBackend) {}\n\n async store(entry: VaultEntry): Promise<Result<void, MemoryError>> {\n const importance =\n entry.importance === 'critical' ? MemoryImportance.HIGH : MemoryImportance.MEDIUM;\n const tags = ['vault', entry.category, entry.importance, ...(entry.tags ?? [])];\n const meta: MemoryMetadata = { importance, tags };\n if (entry.expiresAt !== undefined) {\n meta.ttl = entry.expiresAt.getTime() - getTimeProvider().now();\n }\n return this.backend.store(`vault:${entry.vaultId}`, entry, meta);\n }\n\n async retrieve(vaultId: string): Promise<Result<VaultEntry | null, MemoryError>> {\n const result = await this.backend.retrieve(`vault:${vaultId}`);\n if (!result.ok) return result;\n return ok(result.value as VaultEntry | null);\n }\n\n async findByCategory(\n category: VaultEntry['category'],\n limit = 20\n ): Promise<Result<readonly VaultEntry[], MemoryError>> {\n const result = await this.backend.search(`vault ${category}`, limit);\n if (!result.ok) return result;\n return ok(\n result.value.map((e) => e.value as VaultEntry).filter((v) => v.category === category)\n );\n }\n\n async findByImportance(\n importance: VaultEntry['importance'],\n limit = 20\n ): Promise<Result<readonly VaultEntry[], MemoryError>> {\n const result = await this.backend.search(`vault ${importance}`, limit);\n if (!result.ok) return result;\n return ok(\n result.value.map((e) => e.value as VaultEntry).filter((v) => v.importance === importance)\n );\n }\n\n async searchVault(\n query: string,\n limit = 20\n ): Promise<Result<readonly VaultEntry[], MemoryError>> {\n const result = await this.backend.search(`vault ${query}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => e.value as VaultEntry));\n }\n\n async archive(vaultId: string): Promise<Result<void, MemoryError>> {\n const entry = await this.retrieve(vaultId);\n if (!entry.ok) return entry;\n if (entry.value === null) return ok(undefined);\n return this.store({\n ...entry.value,\n category: 'archive',\n updatedAt: new Date(getTimeProvider().now()),\n });\n }\n\n async getExpired(): Promise<Result<readonly VaultEntry[], MemoryError>> {\n const result = await this.backend.search('vault', 100);\n if (!result.ok) return result;\n const now = new Date(getTimeProvider().now());\n const expired = result.value\n .map((e) => e.value as VaultEntry)\n .filter((v) => v.expiresAt !== undefined && v.expiresAt < now);\n return ok(expired);\n }\n}\n","/**\n * nexus-agents/context - Typed Memory Implementation\n *\n * Implements MIRIX-style typed memory system using HybridMemoryBackend.\n *\n * @module context/typed-memory\n * (Source: Issue #101, arXiv:2507.07957 - MIRIX Architecture)\n */\n\nimport type { Result } from '../core/result.js';\nimport { ok } from '../core/result.js';\nimport { getTimeProvider } from '../core/index.js';\nimport type { AgentRole } from '../core/types/agent.js';\nimport { createLogger } from '../core/logger.js';\nimport type { IMemoryBackend, MemoryEntry, MemoryError } from './memory-backend-types.js';\nimport type {\n ITypedMemory,\n ICoreMemory,\n IEpisodicMemory,\n ISemanticMemory,\n IProceduralMemory,\n IResourceMemory,\n IKnowledgeVault,\n TypedMemoryEntry,\n TypedMemoryStats,\n TypedMemoryPruneResult,\n RelevanceFilterConfig,\n} from './memory-types.js';\nimport type { IHindsightBeliefMemory } from './belief-types.js';\nimport { MemoryType, DEFAULT_RELEVANCE_CONFIG } from './memory-types.js';\nimport {\n CoreMemoryImpl,\n EpisodicMemoryImpl,\n SemanticMemoryImpl,\n ProceduralMemoryImpl,\n ResourceMemoryImpl,\n KnowledgeVaultImpl,\n} from './typed-memory-impl.js';\nimport { HindsightBeliefMemory } from './belief-memory.js';\n\nconst logger = createLogger({ component: 'typed-memory' });\n\n/**\n * Typed memory system implementing MIRIX architecture.\n */\nexport class TypedMemory implements ITypedMemory {\n readonly core: ICoreMemory;\n readonly episodic: IEpisodicMemory;\n readonly semantic: ISemanticMemory;\n readonly procedural: IProceduralMemory;\n readonly resource: IResourceMemory;\n readonly vault: IKnowledgeVault;\n readonly belief: IHindsightBeliefMemory;\n\n private readonly backend: IMemoryBackend;\n private readonly config: RelevanceFilterConfig;\n\n constructor(backend: IMemoryBackend, config: RelevanceFilterConfig = DEFAULT_RELEVANCE_CONFIG) {\n this.backend = backend;\n this.config = config;\n this.core = new CoreMemoryImpl(backend);\n this.episodic = new EpisodicMemoryImpl(backend);\n this.semantic = new SemanticMemoryImpl(backend);\n this.procedural = new ProceduralMemoryImpl(backend);\n this.resource = new ResourceMemoryImpl(backend);\n this.vault = new KnowledgeVaultImpl(backend);\n this.belief = new HindsightBeliefMemory();\n logger.info('TypedMemory initialized');\n }\n\n async queryByType(\n type: MemoryType,\n query: string,\n limit = 20\n ): Promise<Result<readonly TypedMemoryEntry[], MemoryError>> {\n const result = await this.backend.search(`${type} ${query}`, limit);\n if (!result.ok) return result;\n return ok(result.value.map((e) => this.toTypedEntry(e, type)));\n }\n\n async filterByRelevance(\n agentRole: AgentRole,\n limit = 50\n ): Promise<Result<readonly TypedMemoryEntry[], MemoryError>> {\n const configuredTypes = this.config.roleMemoryTypes[agentRole];\n const relevantTypes =\n configuredTypes.length > 0 ? configuredTypes : [MemoryType.CORE, MemoryType.VAULT];\n const entries: TypedMemoryEntry[] = [];\n for (const type of relevantTypes) {\n const result = await this.backend.search(type, this.config.maxEntriesPerType);\n if (result.ok) {\n entries.push(...result.value.map((e) => this.toTypedEntry(e, type)));\n }\n }\n return ok(entries.slice(0, limit));\n }\n\n async getStats(): Promise<Result<TypedMemoryStats, MemoryError>> {\n const counts: Record<MemoryType, number> = {\n core: 0,\n episodic: 0,\n semantic: 0,\n procedural: 0,\n resource: 0,\n vault: 0,\n belief: 0,\n };\n let total = 0;\n for (const type of Object.values(MemoryType)) {\n const result = await this.backend.search(type, 1000);\n if (result.ok) {\n counts[type] = result.value.length;\n total += result.value.length;\n }\n }\n return ok({ totalEntries: total, entriesByType: counts });\n }\n\n async pruneExpired(): Promise<Result<TypedMemoryPruneResult, MemoryError>> {\n const result = await this.backend.prune(new Date(getTimeProvider().now()));\n if (!result.ok) return result;\n logger.info('Pruned expired entries', { count: result.value });\n return ok({\n prunedCount: result.value,\n prunedByType: {\n core: 0,\n episodic: 0,\n semantic: 0,\n procedural: 0,\n resource: 0,\n vault: 0,\n belief: 0,\n },\n });\n }\n\n private toTypedEntry(entry: MemoryEntry, type: MemoryType): TypedMemoryEntry {\n return {\n id: entry.key,\n type,\n key: entry.key,\n value: entry.value,\n metadata: entry.metadata,\n createdAt: entry.createdAt,\n accessedAt: entry.accessedAt,\n };\n }\n}\n\n/** Create a typed memory instance. */\nexport function createTypedMemory(\n backend: IMemoryBackend,\n config?: RelevanceFilterConfig\n): ITypedMemory {\n return new TypedMemory(backend, config);\n}\n","/**\n * nexus-agents/mcp - Memory Promotion Pipeline\n *\n * Implements automatic memory promotion between layers:\n * - SessionLearning → Belief (high-confidence learnings become structured knowledge)\n * - Belief → AgenticMemory (stable beliefs become searchable knowledge graph entries)\n *\n * @module mcp/tools/memory-promotion\n * (Source: Issue #746 Phase 4 - Memory Promotion Pipeline)\n */\n\nimport type { ILogger } from '../../core/index.js';\nimport { getErrorMessage, createLogger } from '../../core/index.js';\n\nimport type { HindsightBeliefMemory } from '../../context/belief-memory.js';\nimport { BeliefConfidence, BeliefSourceType } from '../../context/belief-core-types.js';\nimport type { Belief } from '../../context/belief-core-types.js';\nimport type { AgenticMemoryBackend } from '../../context/agentic-memory.js';\nimport type { SessionLearning } from '../../context/session-memory-types.js';\nimport { MemoryImportance } from '../../context/memory-backend-types.js';\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\n/**\n * Configuration for memory promotion thresholds.\n * Based on research analysis: arXiv:2512.21567 (Decision-Theoretic Memory)\n */\nexport interface MemoryPromotionConfig {\n /** Minimum confidence for SessionLearning → Belief promotion (default: 0.75) */\n readonly sessionToBeliefConfidence: number;\n /** Minimum confidence level for Belief → Agentic promotion (default: 'medium') */\n readonly beliefToAgenticMinConfidence: BeliefConfidence;\n /** Minimum belief age in ms before promotion to AgenticMemory (default: 7 days) */\n readonly beliefStabilizationMs: number;\n /** Whether to auto-promote on session end (default: true) */\n readonly autoPromoteOnSessionEnd: boolean;\n}\n\n/** Default promotion configuration. */\nexport const DEFAULT_PROMOTION_CONFIG: MemoryPromotionConfig = {\n sessionToBeliefConfidence: 0.75,\n beliefToAgenticMinConfidence: BeliefConfidence.MEDIUM,\n beliefStabilizationMs: 7 * 24 * 60 * 60 * 1000, // 7 days\n autoPromoteOnSessionEnd: true,\n};\n\n// ============================================================================\n// Promotion Statistics\n// ============================================================================\n\n/**\n * Statistics from a promotion run.\n */\nexport interface PromotionStats {\n readonly learningsEvaluated: number;\n readonly learningsPromotedToBelief: number;\n readonly beliefsEvaluated: number;\n readonly beliefsPromotedToAgentic: number;\n readonly errors: number;\n}\n\n// ============================================================================\n// MemoryPromoter\n// ============================================================================\n\n/**\n * Handles memory promotion between layers.\n * Stateless utility class - instantiate with dependencies for each promotion run.\n */\nexport class MemoryPromoter {\n private readonly beliefs: HindsightBeliefMemory;\n private readonly agentic: AgenticMemoryBackend | null;\n private readonly config: MemoryPromotionConfig;\n private readonly log: ILogger;\n\n constructor(\n beliefs: HindsightBeliefMemory,\n agentic: AgenticMemoryBackend | null,\n config: Partial<MemoryPromotionConfig> = {},\n logger?: ILogger\n ) {\n this.beliefs = beliefs;\n this.agentic = agentic;\n this.config = { ...DEFAULT_PROMOTION_CONFIG, ...config };\n this.log = logger ?? createLogger({ component: 'MemoryPromoter' });\n }\n\n /**\n * Promote high-confidence session learnings to structured beliefs.\n * Returns the number of learnings successfully promoted.\n */\n async promoteLearningsToBelief(learnings: readonly SessionLearning[]): Promise<number> {\n let promoted = 0;\n for (const learning of learnings) {\n if (learning.confidence >= this.config.sessionToBeliefConfidence) {\n const success = await this.promoteSingleLearning(learning);\n if (success) promoted++;\n }\n }\n if (promoted > 0) {\n this.log.info('Promoted learnings to beliefs', { count: promoted });\n }\n return promoted;\n }\n\n /**\n * Promote stable, high-confidence beliefs to AgenticMemory knowledge graph.\n * Returns the number of beliefs successfully promoted.\n */\n async promoteBeliefToAgentic(beliefs: readonly Belief[]): Promise<number> {\n if (this.agentic === null) {\n this.log.debug('AgenticMemory unavailable, skipping belief promotion');\n return 0;\n }\n\n let promoted = 0;\n const now = Date.now();\n\n for (const belief of beliefs) {\n // Skip superseded beliefs\n if (belief.superseded) continue;\n\n // Check confidence threshold\n if (!this.meetsConfidenceThreshold(belief.confidence)) continue;\n\n // Check stabilization period\n const ageMs = now - belief.createdAt.getTime();\n if (ageMs < this.config.beliefStabilizationMs) continue;\n\n const success = await this.promoteSingleBelief(belief);\n if (success) promoted++;\n }\n\n if (promoted > 0) {\n this.log.info('Promoted beliefs to AgenticMemory', { count: promoted });\n }\n return promoted;\n }\n\n /**\n * Run full promotion pipeline: learnings → beliefs → agentic.\n */\n async runPromotionPipeline(\n learnings: readonly SessionLearning[],\n beliefs: readonly Belief[]\n ): Promise<PromotionStats> {\n const learningsPromoted = await this.promoteLearningsToBelief(learnings);\n const beliefsPromoted = await this.promoteBeliefToAgentic(beliefs);\n\n return {\n learningsEvaluated: learnings.length,\n learningsPromotedToBelief: learningsPromoted,\n beliefsEvaluated: beliefs.length,\n beliefsPromotedToAgentic: beliefsPromoted,\n errors: 0,\n };\n }\n\n /** Promote a single learning to a belief. */\n private async promoteSingleLearning(learning: SessionLearning): Promise<boolean> {\n try {\n const confidence = this.mapSessionConfidenceToBeliefConfidence(learning.confidence);\n await this.beliefs.retain({\n subject: learning.context,\n predicate: 'learned-pattern',\n object: learning.pattern,\n confidence,\n sourceType: BeliefSourceType.OBSERVATION,\n sourceRef: learning.source ?? 'session-learning',\n });\n return true;\n } catch (error) {\n this.log.debug('Failed to promote learning to belief', {\n pattern: learning.pattern,\n error: getErrorMessage(error),\n });\n return false;\n }\n }\n\n /** Promote a single belief to AgenticMemory. */\n private async promoteSingleBelief(belief: Belief): Promise<boolean> {\n if (this.agentic === null) return false;\n\n try {\n // Create a knowledge entry from the belief triple\n const key = `belief:${belief.subject}:${belief.predicate}`;\n const value = {\n subject: belief.subject,\n predicate: belief.predicate,\n object: belief.object,\n confidence: belief.confidence,\n sourceType: belief.sourceType,\n beliefId: belief.beliefId,\n };\n\n const result = await this.agentic.storeWithAttributes(key, value, {\n importance: this.mapConfidenceToImportance(belief.confidence),\n tags: [belief.predicate, belief.sourceType],\n });\n\n return result.ok;\n } catch (error) {\n this.log.debug('Failed to promote belief to AgenticMemory', {\n beliefId: belief.beliefId,\n error: getErrorMessage(error),\n });\n return false;\n }\n }\n\n /** Map session confidence (0-1) to belief confidence level. */\n private mapSessionConfidenceToBeliefConfidence(confidence: number): BeliefConfidence {\n if (confidence >= 0.9) return BeliefConfidence.HIGH;\n if (confidence >= 0.75) return BeliefConfidence.MEDIUM;\n if (confidence >= 0.5) return BeliefConfidence.LOW;\n return BeliefConfidence.SPECULATIVE;\n }\n\n /** Check if belief confidence meets minimum threshold. */\n private meetsConfidenceThreshold(confidence: BeliefConfidence): boolean {\n const order = [\n BeliefConfidence.SPECULATIVE,\n BeliefConfidence.LOW,\n BeliefConfidence.MEDIUM,\n BeliefConfidence.HIGH,\n ];\n const beliefIndex = order.indexOf(confidence);\n const thresholdIndex = order.indexOf(this.config.beliefToAgenticMinConfidence);\n return beliefIndex >= thresholdIndex;\n }\n\n /** Map belief confidence to memory importance for AgenticMemory. */\n private mapConfidenceToImportance(confidence: BeliefConfidence): MemoryImportance {\n switch (confidence) {\n case BeliefConfidence.HIGH:\n return MemoryImportance.HIGH;\n case BeliefConfidence.MEDIUM:\n return MemoryImportance.MEDIUM;\n default:\n return MemoryImportance.LOW;\n }\n }\n}\n","/**\n * nexus-agents/mcp - Coordinated Memory Decay\n *\n * Implements FADE (Forgetting with Adaptive Decay) principles for coordinated\n * memory management across all memory systems:\n * - SessionMemory (FIFO)\n * - BeliefMemory (age-based)\n * - AgenticMemory (importance-based)\n * - AdaptiveMemory (priority decay)\n * - MobiMem (TTL + capacity)\n *\n * Key features:\n * - Cross-memory reference tracking to prevent orphaned references\n * - Coordinated decay scheduling across memory types\n * - Configurable decay strategies per memory type\n *\n * @module mcp/tools/memory-decay\n * (Source: Issue #746 Phase 5 - Coordinated Decay/Forgetting)\n * (Research: arXiv:2512.21567 - Decision-Theoretic Memory)\n */\n\nimport type { ILogger } from '../../core/index.js';\nimport { getErrorMessage, createLogger, getTimeProvider } from '../../core/index.js';\n\nimport type { HindsightBeliefMemory } from '../../context/belief-memory.js';\nimport type { AgenticMemoryBackend } from '../../context/agentic-memory.js';\nimport type { AdaptiveMemoryBackend } from '../../context/adaptive-memory.js';\nimport type { MobiMem } from '../../context/mobimem.js';\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\n/**\n * Decay strategy for each memory type.\n */\nexport type DecayStrategy = 'fifo' | 'age' | 'importance' | 'ttl' | 'exponential';\n\n/**\n * Configuration for coordinated memory decay.\n * Based on FADE principles from arXiv:2512.21567.\n */\nexport interface MemoryDecayConfig {\n /** Whether decay is enabled (default: true) */\n readonly enabled: boolean;\n\n /** Interval between automatic decay runs in ms (default: 1 hour) */\n readonly decayIntervalMs: number;\n\n /** Belief decay - age in days before pruning superseded beliefs (default: 30) */\n readonly beliefMaxAgeDays: number;\n\n /** Agentic decay - max entries before importance-based eviction (default: 10000) */\n readonly agenticMaxEntries: number;\n\n /** Agentic decay - importance threshold for eviction (default: 0.3) */\n readonly agenticImportanceThreshold: number;\n\n /** Adaptive decay - priority score threshold for eviction (default: 0.2) */\n readonly adaptivePriorityThreshold: number;\n\n /** MobiMem decay - run TTL eviction on coordinated decay (default: true) */\n readonly mobimemEvictOnDecay: boolean;\n\n /** Whether to check cross-references before eviction (default: true) */\n readonly checkCrossReferences: boolean;\n\n /** Grace period in ms before removing items with cross-references (default: 7 days) */\n readonly crossReferenceGracePeriodMs: number;\n}\n\n/** Default decay configuration. */\nexport const DEFAULT_DECAY_CONFIG: MemoryDecayConfig = {\n enabled: true,\n decayIntervalMs: 60 * 60 * 1000, // 1 hour\n beliefMaxAgeDays: 30,\n agenticMaxEntries: 10000,\n agenticImportanceThreshold: 0.3,\n adaptivePriorityThreshold: 0.2,\n mobimemEvictOnDecay: true,\n checkCrossReferences: true,\n crossReferenceGracePeriodMs: 7 * 24 * 60 * 60 * 1000, // 7 days\n};\n\n// ============================================================================\n// Decay Statistics\n// ============================================================================\n\n/**\n * Statistics from a single decay run.\n */\nexport interface DecayRunStats {\n readonly startedAt: Date;\n readonly completedAt: Date;\n readonly beliefsPruned: number;\n readonly agenticEvicted: number;\n readonly adaptiveEvicted: number;\n readonly mobimemEvicted: number;\n readonly crossReferencesPreserved: number;\n readonly errors: readonly string[];\n}\n\n/**\n * Aggregate statistics across multiple decay runs.\n */\nexport interface DecayAggregateStats {\n readonly totalRuns: number;\n readonly lastRunAt: Date | null;\n readonly totalBeliefsPruned: number;\n readonly totalAgenticEvicted: number;\n readonly totalAdaptiveEvicted: number;\n readonly totalMobimemEvicted: number;\n readonly totalCrossReferencesPreserved: number;\n readonly totalErrors: number;\n}\n\n/** Internal results from decay phases. */\ninterface DecayPhaseResults {\n beliefsPruned?: number;\n agenticEvicted?: number;\n adaptiveEvicted?: number;\n mobimemEvicted?: number;\n crossReferencesPreserved?: number;\n errors: string[];\n}\n\n// ============================================================================\n// Cross-Reference Tracking\n// ============================================================================\n\n/**\n * Represents a cross-memory reference.\n */\nexport interface CrossReference {\n readonly sourceMemory: 'session' | 'belief' | 'agentic' | 'adaptive' | 'mobimem';\n readonly sourceKey: string;\n readonly targetMemory: 'session' | 'belief' | 'agentic' | 'adaptive' | 'mobimem';\n readonly targetKey: string;\n readonly createdAt: Date;\n}\n\n/**\n * Tracks cross-references between memory systems.\n * Used to prevent orphaned references during decay.\n */\nexport class CrossReferenceTracker {\n private readonly references: Map<string, CrossReference[]> = new Map();\n private readonly log: ILogger;\n\n constructor(logger?: ILogger) {\n this.log = logger ?? createLogger({ component: 'CrossReferenceTracker' });\n }\n\n /**\n * Register a cross-reference between two memories.\n */\n registerReference(ref: Omit<CrossReference, 'createdAt'>): void {\n const key = this.makeKey(ref.sourceMemory, ref.sourceKey);\n const refs = this.references.get(key) ?? [];\n refs.push({\n ...ref,\n createdAt: new Date(getTimeProvider().now()),\n });\n this.references.set(key, refs);\n this.log.debug('Registered cross-reference', {\n source: `${ref.sourceMemory}:${ref.sourceKey}`,\n target: `${ref.targetMemory}:${ref.targetKey}`,\n });\n }\n\n /**\n * Check if a memory entry has cross-references.\n */\n hasReferences(memory: CrossReference['sourceMemory'], key: string): boolean {\n const refKey = this.makeKey(memory, key);\n const refs = this.references.get(refKey);\n return refs !== undefined && refs.length > 0;\n }\n\n /**\n * Get all references for a memory entry.\n */\n getReferences(memory: CrossReference['sourceMemory'], key: string): readonly CrossReference[] {\n const refKey = this.makeKey(memory, key);\n return this.references.get(refKey) ?? [];\n }\n\n /**\n * Remove references for a deleted memory entry.\n */\n removeReferences(memory: CrossReference['sourceMemory'], key: string): number {\n const refKey = this.makeKey(memory, key);\n const refs = this.references.get(refKey);\n if (refs === undefined) return 0;\n this.references.delete(refKey);\n\n // Also remove any references targeting this key\n let removed = refs.length;\n for (const [k, targetRefs] of this.references.entries()) {\n const filtered = targetRefs.filter(\n (r) => !(r.targetMemory === memory && r.targetKey === key)\n );\n if (filtered.length !== targetRefs.length) {\n removed += targetRefs.length - filtered.length;\n if (filtered.length === 0) {\n this.references.delete(k);\n } else {\n this.references.set(k, filtered);\n }\n }\n }\n return removed;\n }\n\n /**\n * Get statistics about tracked references.\n */\n getStats(): { totalReferences: number; uniqueSources: number } {\n let totalReferences = 0;\n for (const refs of this.references.values()) {\n totalReferences += refs.length;\n }\n return {\n totalReferences,\n uniqueSources: this.references.size,\n };\n }\n\n private makeKey(memory: string, key: string): string {\n return `${memory}:${key}`;\n }\n}\n\n// ============================================================================\n// MemoryDecayManager\n// ============================================================================\n\n/**\n * Manages coordinated decay across all memory systems.\n * Implements FADE (Forgetting with Adaptive Decay) principles.\n */\nexport class MemoryDecayManager {\n private readonly config: MemoryDecayConfig;\n private readonly log: ILogger;\n private readonly tracker: CrossReferenceTracker;\n private readonly runHistory: DecayRunStats[] = [];\n private decayTimer: ReturnType<typeof setInterval> | null = null;\n\n // Memory system references (set during initialization)\n private beliefs: HindsightBeliefMemory | null = null;\n private agentic: AgenticMemoryBackend | null = null;\n private adaptive: AdaptiveMemoryBackend | null = null;\n private mobimem: MobiMem | null = null;\n\n constructor(config: Partial<MemoryDecayConfig> = {}, logger?: ILogger) {\n this.config = { ...DEFAULT_DECAY_CONFIG, ...config };\n this.log = logger ?? createLogger({ component: 'MemoryDecayManager' });\n this.tracker = new CrossReferenceTracker(this.log);\n }\n\n /**\n * Initialize with memory system references.\n */\n initialize(options: {\n beliefs?: HindsightBeliefMemory;\n agentic?: AgenticMemoryBackend | null;\n adaptive?: AdaptiveMemoryBackend | null;\n mobimem?: MobiMem | null;\n }): void {\n this.beliefs = options.beliefs ?? null;\n this.agentic = options.agentic ?? null;\n this.adaptive = options.adaptive ?? null;\n this.mobimem = options.mobimem ?? null;\n this.log.info('MemoryDecayManager initialized', {\n beliefs: this.beliefs !== null,\n agentic: this.agentic !== null,\n adaptive: this.adaptive !== null,\n mobimem: this.mobimem !== null,\n });\n }\n\n /**\n * Start automatic decay scheduling.\n */\n startAutoDecay(): void {\n if (!this.config.enabled) {\n this.log.info('Auto-decay disabled by configuration');\n return;\n }\n if (this.decayTimer !== null) {\n this.log.warn('Auto-decay already running');\n return;\n }\n\n this.decayTimer = setInterval(() => {\n void this.runDecay().catch((error: unknown) => {\n const err = error instanceof Error ? error : new Error(String(error));\n this.log.error('Auto-decay failed', err, {});\n });\n }, this.config.decayIntervalMs);\n\n this.log.info('Auto-decay started', { intervalMs: this.config.decayIntervalMs });\n }\n\n /**\n * Stop automatic decay scheduling.\n */\n stopAutoDecay(): void {\n if (this.decayTimer !== null) {\n clearInterval(this.decayTimer);\n this.decayTimer = null;\n this.log.info('Auto-decay stopped');\n }\n }\n\n /**\n * Run a coordinated decay pass across all memory systems.\n */\n async runDecay(): Promise<DecayRunStats> {\n const startedAt = new Date(getTimeProvider().now());\n this.log.info('Starting coordinated decay run');\n\n const results = await this.executeDecayPhases();\n const stats = this.buildStats(startedAt, results);\n this.recordHistory(stats);\n this.logCompletion(stats);\n\n return stats;\n }\n\n /** Execute all decay phases and collect results. */\n private async executeDecayPhases(): Promise<DecayPhaseResults> {\n const results: DecayPhaseResults = { errors: [] };\n\n // Phase 1: Belief Memory\n const beliefResult = await this.safeDecay('Belief', () => this.decayBeliefs());\n results.beliefsPruned = beliefResult.pruned;\n results.crossReferencesPreserved = beliefResult.preserved;\n if (beliefResult.error !== undefined) results.errors.push(beliefResult.error);\n\n // Phase 2: Agentic Memory\n const agenticResult = await this.safeDecay('Agentic', () => this.decayAgentic());\n results.agenticEvicted = agenticResult.evicted;\n results.crossReferencesPreserved =\n (results.crossReferencesPreserved ?? 0) + agenticResult.preserved;\n if (agenticResult.error !== undefined) results.errors.push(agenticResult.error);\n\n // Phase 3: Adaptive Memory\n const adaptiveResult = await this.safeDecay('Adaptive', () => this.decayAdaptive());\n results.adaptiveEvicted = adaptiveResult.evicted;\n results.crossReferencesPreserved =\n (results.crossReferencesPreserved ?? 0) + adaptiveResult.preserved;\n if (adaptiveResult.error !== undefined) results.errors.push(adaptiveResult.error);\n\n // Phase 4: MobiMem\n results.mobimemEvicted = this.decayMobiMem(results.errors);\n\n return results;\n }\n\n /** Safely execute a decay function with error handling. */\n private async safeDecay(\n name: string,\n fn: () => Promise<{ pruned?: number; evicted?: number; preserved: number }>\n ): Promise<{ pruned: number; evicted: number; preserved: number; error?: string }> {\n try {\n const result = await fn();\n return {\n pruned: result.pruned ?? 0,\n evicted: result.evicted ?? 0,\n preserved: result.preserved,\n };\n } catch (error) {\n const msg = `${name} decay failed: ${getErrorMessage(error)}`;\n this.log.warn(msg);\n return { pruned: 0, evicted: 0, preserved: 0, error: msg };\n }\n }\n\n /** Execute MobiMem maintenance. */\n private decayMobiMem(errors: string[]): number {\n if (this.mobimem === null || !this.config.mobimemEvictOnDecay) return 0;\n try {\n this.mobimem.runMaintenance();\n return this.mobimem.getStats().action.totalEntries > 0 ? 1 : 0;\n } catch (error) {\n const msg = `MobiMem decay failed: ${getErrorMessage(error)}`;\n errors.push(msg);\n this.log.warn(msg);\n return 0;\n }\n }\n\n /** Build DecayRunStats from phase results. */\n private buildStats(startedAt: Date, results: DecayPhaseResults): DecayRunStats {\n return {\n startedAt,\n completedAt: new Date(getTimeProvider().now()),\n beliefsPruned: results.beliefsPruned ?? 0,\n agenticEvicted: results.agenticEvicted ?? 0,\n adaptiveEvicted: results.adaptiveEvicted ?? 0,\n mobimemEvicted: results.mobimemEvicted ?? 0,\n crossReferencesPreserved: results.crossReferencesPreserved ?? 0,\n errors: results.errors,\n };\n }\n\n /** Record stats in history, keeping last 100. */\n private recordHistory(stats: DecayRunStats): void {\n this.runHistory.push(stats);\n if (this.runHistory.length > 100) this.runHistory.shift();\n }\n\n /** Log completion of decay run. */\n private logCompletion(stats: DecayRunStats): void {\n this.log.info('Coordinated decay completed', {\n durationMs: stats.completedAt.getTime() - stats.startedAt.getTime(),\n beliefsPruned: stats.beliefsPruned,\n agenticEvicted: stats.agenticEvicted,\n adaptiveEvicted: stats.adaptiveEvicted,\n mobimemEvicted: stats.mobimemEvicted,\n crossReferencesPreserved: stats.crossReferencesPreserved,\n errors: stats.errors.length,\n });\n }\n\n /**\n * Decay beliefs older than the configured age.\n */\n private async decayBeliefs(): Promise<{ pruned: number; preserved: number }> {\n if (this.beliefs === null) return { pruned: 0, preserved: 0 };\n\n const maxAgeMs = this.config.beliefMaxAgeDays * 24 * 60 * 60 * 1000;\n const cutoffDate = new Date(getTimeProvider().now() - maxAgeMs);\n\n // If cross-reference checking is enabled, we need to check before pruning\n // For now, use the simple age-based pruning\n const result = await this.beliefs.pruneSuperseded(cutoffDate);\n if (!result.ok) {\n throw new Error(result.error.message);\n }\n\n return { pruned: result.value, preserved: 0 };\n }\n\n /**\n * Decay agentic memories with low importance.\n */\n private async decayAgentic(): Promise<{ evicted: number; preserved: number }> {\n if (this.agentic === null) return { evicted: 0, preserved: 0 };\n\n // Use the base prune functionality with age-based cutoff\n // Agentic memory importance-based eviction requires searching all entries\n // For Phase 5, we use a conservative approach: prune entries older than 90 days\n const cutoffDate = new Date(getTimeProvider().now() - 90 * 24 * 60 * 60 * 1000);\n const result = await this.agentic.prune(cutoffDate);\n if (!result.ok) {\n throw new Error(result.error.message);\n }\n\n return { evicted: result.value, preserved: 0 };\n }\n\n /**\n * Decay adaptive memories with low priority scores.\n */\n private async decayAdaptive(): Promise<{ evicted: number; preserved: number }> {\n if (this.adaptive === null) return { evicted: 0, preserved: 0 };\n\n // Use the base prune functionality with age-based cutoff\n // Priority-based eviction would require retrieving all entries and filtering\n // For Phase 5, we use a conservative approach: prune entries older than 60 days\n const cutoffDate = new Date(getTimeProvider().now() - 60 * 24 * 60 * 60 * 1000);\n const result = await this.adaptive.prune(cutoffDate);\n if (!result.ok) {\n throw new Error(result.error.message);\n }\n\n return { evicted: result.value, preserved: 0 };\n }\n\n /**\n * Register a cross-reference between memory systems.\n * Call this when promoting memory between layers.\n */\n registerCrossReference(\n sourceMemory: CrossReference['sourceMemory'],\n sourceKey: string,\n targetMemory: CrossReference['targetMemory'],\n targetKey: string\n ): void {\n this.tracker.registerReference({\n sourceMemory,\n sourceKey,\n targetMemory,\n targetKey,\n });\n }\n\n /**\n * Get aggregate statistics across all decay runs.\n */\n getAggregateStats(): DecayAggregateStats {\n const lastRun = this.runHistory.length > 0 ? this.runHistory[this.runHistory.length - 1] : null;\n\n return {\n totalRuns: this.runHistory.length,\n lastRunAt: lastRun?.completedAt ?? null,\n totalBeliefsPruned: this.runHistory.reduce((sum, r) => sum + r.beliefsPruned, 0),\n totalAgenticEvicted: this.runHistory.reduce((sum, r) => sum + r.agenticEvicted, 0),\n totalAdaptiveEvicted: this.runHistory.reduce((sum, r) => sum + r.adaptiveEvicted, 0),\n totalMobimemEvicted: this.runHistory.reduce((sum, r) => sum + r.mobimemEvicted, 0),\n totalCrossReferencesPreserved: this.runHistory.reduce(\n (sum, r) => sum + r.crossReferencesPreserved,\n 0\n ),\n totalErrors: this.runHistory.reduce((sum, r) => sum + r.errors.length, 0),\n };\n }\n\n /**\n * Get the last N decay run results.\n */\n getRecentRuns(limit = 10): readonly DecayRunStats[] {\n return this.runHistory.slice(-limit);\n }\n\n /**\n * Get cross-reference tracker statistics.\n */\n getCrossReferenceStats(): { totalReferences: number; uniqueSources: number } {\n return this.tracker.getStats();\n }\n\n /**\n * Clean up resources.\n */\n shutdown(): void {\n this.stopAutoDecay();\n this.log.info('MemoryDecayManager shutdown');\n }\n}\n","/**\n * Cross-Memory Query Helpers\n *\n * Standalone functions for querying individual memory backends and\n * scoring relevance. Extracted from ToolMemoryManager (#1671) to\n * reduce tool-memory.ts below the 400-line governance limit.\n *\n * @module mcp/tools/tool-memory-query\n */\n\nimport type { ILogger } from '../../core/index.js';\nimport type { HindsightBeliefMemory, Belief } from '../../context/belief-memory.js';\nimport type { AgenticMemoryBackend } from '../../context/agentic-memory.js';\nimport type { AdaptiveMemoryBackend } from '../../context/adaptive-memory.js';\nimport type { ITypedMemory } from '../../context/memory-types.js';\nimport type { UnifiedMemoryResult } from './tool-memory-types.js';\nimport type { SessionLearning } from '../../context/session-memory-types.js';\n\n// ============================================================================\n// Relevance Scoring\n// ============================================================================\n\n/**\n * Calculate relevance score based on keyword matches (#1227).\n * Uses graduated scoring: base ratio + partial match bonus + exact phrase bonus.\n */\nexport function scoreRelevance(text: string, keywords: readonly string[]): number {\n if (keywords.length === 0) return 0.5;\n const lower = text.toLowerCase();\n const matched = keywords.filter((k) => lower.includes(k));\n const matchRatio = matched.length / keywords.length;\n let tfBonus = 0;\n for (const k of matched) {\n const count = lower.split(k).length - 1;\n if (count > 1) tfBonus += 0.05 * Math.min(count - 1, 3);\n }\n const phrase = keywords.join(' ');\n const phraseBonus = lower.includes(phrase) ? 0.15 : 0;\n return Math.min(1, matchRatio * 0.8 + tfBonus + phraseBonus);\n}\n\n// ============================================================================\n// Per-Backend Query Helpers\n// ============================================================================\n\n/** Query SessionMemory learnings. */\nexport function querySessionMemory(\n learnings: readonly SessionLearning[],\n keywords: readonly string[],\n limit: number\n): UnifiedMemoryResult[] {\n const now = new Date();\n return learnings.slice(0, limit).map((l) => ({\n source: 'session' as const,\n type: 'learning',\n content: `${l.pattern} (${l.context})`,\n relevance: scoreRelevance(l.pattern + ' ' + l.context, keywords),\n timestamp: now,\n metadata: { confidence: l.confidence, source: l.source },\n }));\n}\n\n/** Query BeliefMemory. Falls back to keyword search when exact match misses (#1225). */\nexport async function queryBeliefMemory(\n beliefs: HindsightBeliefMemory,\n query: string,\n keywords: readonly string[],\n limit: number,\n log: ILogger\n): Promise<UnifiedMemoryResult[]> {\n const results: UnifiedMemoryResult[] = [];\n try {\n const beliefResult = await beliefs.recallBySubject(query, limit);\n let matched: readonly Belief[] = [];\n if (beliefResult.ok && beliefResult.value.length > 0) {\n matched = beliefResult.value;\n } else if (keywords.length > 0) {\n const KEYWORD_SCAN_LIMIT = 1000;\n const allResult = await beliefs.query({\n includeSuperseded: false,\n limit: KEYWORD_SCAN_LIMIT,\n });\n if (allResult.ok) {\n matched = allResult.value.filter((b) => {\n const text = (b.subject + ' ' + b.predicate + ' ' + b.object).toLowerCase();\n return keywords.some((k) => text.includes(k));\n });\n }\n }\n for (const b of matched.filter((x) => !x.superseded)) {\n results.push({\n source: 'belief',\n type: 'belief',\n content: `${b.subject} ${b.predicate} ${b.object}`,\n relevance: scoreRelevance(b.subject + ' ' + b.predicate + ' ' + b.object, keywords),\n timestamp: b.createdAt,\n metadata: { confidence: b.confidence },\n });\n }\n } catch (e: unknown) {\n log.debug('Belief memory query failed', { error: String(e) });\n }\n return results;\n}\n\n/** Query AgenticMemory for knowledge. */\nexport async function queryAgenticMemory(\n agentic: AgenticMemoryBackend,\n query: string,\n keywords: readonly string[],\n limit: number,\n log: ILogger\n): Promise<UnifiedMemoryResult[]> {\n const results: UnifiedMemoryResult[] = [];\n try {\n const agResult = await agentic.searchAgentic(query, limit);\n if (agResult.ok) {\n for (const e of agResult.value) {\n results.push({\n source: 'agentic',\n type: 'knowledge',\n content: `${e.key}: ${JSON.stringify(e.value).slice(0, 100)}`,\n relevance: scoreRelevance(e.key + ' ' + e.attributes.keywords.join(' '), keywords),\n timestamp: e.createdAt,\n metadata: { keywords: e.attributes.keywords },\n });\n }\n }\n } catch (e: unknown) {\n log.debug('Agentic memory query failed', { error: String(e) });\n }\n return results;\n}\n\n/** Query TypedMemory for semantic and episodic entries. */\nexport async function queryTypedMemory(\n typed: ITypedMemory,\n query: string,\n keywords: readonly string[],\n limitPerType: number,\n log: ILogger\n): Promise<UnifiedMemoryResult[]> {\n const results: UnifiedMemoryResult[] = [];\n try {\n const [semanticResult, episodicResult] = await Promise.all([\n typed.queryByType('semantic', query, limitPerType),\n typed.queryByType('episodic', query, limitPerType),\n ]);\n for (const r of [semanticResult, episodicResult]) {\n if (r.ok) {\n for (const e of r.value) {\n results.push({\n source: 'typed',\n type: e.type,\n content: String(e.value).slice(0, 150),\n relevance: scoreRelevance(String(e.value), keywords),\n timestamp: e.createdAt,\n });\n }\n }\n }\n } catch (e: unknown) {\n log.debug('Typed memory query failed', { error: String(e) });\n }\n return results;\n}\n\n/** Query AdaptiveMemory for priority-scored entries (#1226). */\nexport async function queryAdaptiveMemory(\n adaptive: AdaptiveMemoryBackend,\n query: string,\n keywords: readonly string[],\n limit: number,\n log: ILogger\n): Promise<UnifiedMemoryResult[]> {\n const results: UnifiedMemoryResult[] = [];\n try {\n const searchResult = await adaptive.search(query, limit);\n if (searchResult.ok) {\n for (const e of searchResult.value) {\n results.push({\n source: 'adaptive',\n type: 'adaptive',\n content: `${e.key}: ${JSON.stringify(e.value).slice(0, 100)}`,\n relevance: scoreRelevance(e.key + ' ' + JSON.stringify(e.value).slice(0, 200), keywords),\n timestamp: e.createdAt,\n metadata: { importance: e.metadata.importance },\n });\n }\n }\n } catch (e: unknown) {\n log.debug('Adaptive memory query failed', { error: String(e) });\n }\n return results;\n}\n","/**\n * Persistent OutcomeStore — JSONL-backed cross-session persistence.\n *\n * Extends the in-memory OutcomeStore with disk-backed append-only\n * JSONL storage. Hydrates on construction, appends on every write.\n * Corrupt lines are skipped with a warning (graceful degradation).\n *\n * @module orchestration/outcomes/outcome-store-persistence\n * (Source: Issue #1009 — Cross-session persistence)\n */\n\nimport { appendFileSync, readFileSync, writeFileSync, existsSync } from 'node:fs';\n\nimport type { ILogger } from '../../core/index.js';\nimport { createLogger, getErrorMessage } from '../../core/index.js';\nimport { TaskOutcomeSchema } from './outcome-types.js';\nimport type { TaskOutcome } from './outcome-types.js';\nimport { OutcomeStore, registerPersistentOutcomeStoreFactory } from './outcome-store.js';\nimport type { OutcomeStoreConfig } from './outcome-store.js';\nimport { ensureLearningDir, getOutcomesFile } from '../../config/learning-persistence.js';\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\nexport interface PersistentOutcomeStoreConfig extends OutcomeStoreConfig {\n /** Override the file path (useful for testing). */\n readonly filePath?: string;\n /** Override the data directory (useful for testing). */\n readonly dataDir?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * OutcomeStore that persists entries to a JSONL file on disk.\n *\n * - Construction: hydrates from existing JSONL file (Zod-validates each line)\n * - Append: calls super.append() then appendFileSync one JSON line\n * - Corruption: bad lines are skipped with a warning log\n */\nexport class PersistentOutcomeStore extends OutcomeStore {\n private readonly filePath: string;\n private readonly logger: ILogger;\n\n constructor(config?: PersistentOutcomeStoreConfig, logger?: ILogger) {\n super(config);\n this.filePath = config?.filePath ?? getOutcomesFile();\n this.logger = logger ?? createLogger({ component: 'PersistentOutcomeStore' });\n\n const dataDir = config?.dataDir;\n ensureLearningDir(dataDir);\n this.hydrate();\n this.reclassifyHydrated();\n this.purgeSkippedOnHydrate();\n }\n\n /** Override append to persist each entry to disk. */\n override append(outcome: TaskOutcome): void {\n super.append(outcome);\n this.persistLine(outcome);\n }\n\n // ==========================================================================\n // Private\n // ==========================================================================\n\n /**\n * Reclassify hydrated entries that lack a failureCategory.\n * Bounded: reclassifyAll() skips success outcomes and already-classified\n * entries, so only unclassified failures are processed (#1457).\n */\n /**\n * Purge false failures from skipped workers on hydration (#1528).\n * These are 0ms non-success worker-* entries created before the\n * recording fix, representing routing decisions not real failures.\n */\n private purgeSkippedOnHydrate(): void {\n if (this.size === 0) return;\n const purged = this.purgeSkippedWorkers();\n if (purged > 0) {\n this.logger.info('Purged skipped-worker false failures from history', { purged });\n this.rewriteFile();\n }\n }\n\n private reclassifyHydrated(): void {\n if (this.size === 0) return;\n const reclassified = this.reclassifyAll();\n if (reclassified > 0) {\n this.logger.info('Reclassified hydrated outcomes with updated categories', {\n reclassified,\n });\n this.rewriteFile();\n }\n }\n\n private hydrate(): void {\n if (!existsSync(this.filePath)) {\n this.logger.debug('No outcomes file found, starting fresh', {\n path: this.filePath,\n });\n return;\n }\n\n try {\n const content = readFileSync(this.filePath, 'utf-8');\n const lines = content.split('\\n').filter((line) => line.trim().length > 0);\n let loaded = 0;\n let skipped = 0;\n\n for (const line of lines) {\n try {\n const parsed: unknown = JSON.parse(line);\n const result = TaskOutcomeSchema.safeParse(parsed);\n if (result.success) {\n super.append(result.data);\n loaded++;\n } else {\n skipped++;\n }\n } catch (parseErr: unknown) {\n this.logger.debug('Skipping malformed outcome line during hydration', {\n error: getErrorMessage(parseErr),\n linePreview: line.slice(0, 80),\n });\n skipped++;\n }\n }\n\n this.logger.info('Hydrated outcomes from disk', {\n loaded,\n skipped,\n total: lines.length,\n path: this.filePath,\n });\n } catch (error: unknown) {\n const msg = getErrorMessage(error);\n this.logger.warn('Failed to hydrate outcomes from disk', {\n error: msg,\n path: this.filePath,\n });\n }\n }\n\n /** Rewrite the JSONL file from in-memory state after reclassification. */\n private rewriteFile(): void {\n try {\n const entries = this.query();\n const content = entries.map((e) => JSON.stringify(e)).join('\\n') + '\\n';\n writeFileSync(this.filePath, content, 'utf-8');\n } catch (error: unknown) {\n const msg = getErrorMessage(error);\n this.logger.warn('Failed to rewrite outcomes file after reclassification', {\n error: msg,\n path: this.filePath,\n });\n }\n }\n\n private persistLine(outcome: TaskOutcome): void {\n try {\n appendFileSync(this.filePath, JSON.stringify(outcome) + '\\n', 'utf-8');\n } catch (error: unknown) {\n const msg = getErrorMessage(error);\n this.logger.warn('Failed to persist outcome to disk', {\n error: msg,\n path: this.filePath,\n });\n }\n }\n}\n\n// Self-register factory so getOutcomeStore() can create PersistentOutcomeStore\n// without a circular top-level import.\nregisterPersistentOutcomeStoreFactory(() => new PersistentOutcomeStore());\n","/**\n * Learning Events — Event emission helpers for the learning loop (Issue #901, Phase 4)\n *\n * Thin wrappers over the V2 EventBus to emit typed learning events\n * when adaptive thresholds change or trends are detected.\n *\n * @module orchestration/outcomes/learning-events\n */\n\nimport type { IEventBus, PipelineEvent } from '../../pipeline/event-types.js';\nimport type { Trend } from './adaptive-thresholds.js';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/** Detail payload for threshold update events. */\nexport interface ThresholdUpdateDetail {\n readonly cli: string;\n readonly category: string;\n readonly oldBaseline: number;\n readonly newBaseline: number;\n readonly trend: Trend;\n}\n\n/** Detail payload for trend detection events. */\nexport interface TrendDetectedDetail {\n readonly cli: string;\n readonly category: string;\n readonly trend: Trend;\n readonly confidence: number;\n}\n\n// ============================================================================\n// Public API\n// ============================================================================\n\n/**\n * Emits a learning.threshold_updated event on the bus.\n * Fires when the adaptive baseline changes for a CLI+category pair.\n */\nexport function emitThresholdUpdate(bus: IEventBus, detail: ThresholdUpdateDetail): void {\n const event: PipelineEvent = {\n type: 'learning.threshold_updated',\n timestamp: Date.now(),\n cli: detail.cli,\n category: detail.category,\n oldBaseline: detail.oldBaseline,\n newBaseline: detail.newBaseline,\n trend: detail.trend,\n };\n bus.emit(event);\n}\n\n/**\n * Emits a learning.trend_detected event on the bus.\n * Fires when a meaningful performance trend is identified.\n */\nexport function emitTrendDetected(bus: IEventBus, detail: TrendDetectedDetail): void {\n const event: PipelineEvent = {\n type: 'learning.trend_detected',\n timestamp: Date.now(),\n cli: detail.cli,\n category: detail.category,\n trend: detail.trend,\n confidence: detail.confidence,\n };\n bus.emit(event);\n}\n","/**\n * Consensus Vote — Recording Helpers\n *\n * Memory and outcome store recording for consensus votes.\n * Extracted from consensus-vote.ts for file size compliance.\n *\n * @module mcp/tools/consensus-vote-recording\n * (Source: Issue #753 memory, Issue #1134 cold start)\n */\n\nimport {\n createLogger,\n getErrorMessage,\n getTimeProvider,\n getRandomProvider,\n} from '../../core/index.js';\nimport type { AgentVoteResult } from '../../cli/vote-types.js';\nimport { getToolMemory } from './tool-memory.js';\nimport {\n getOutcomeStore,\n categorizeOutcomeErrorMessage,\n} from '../../orchestration/outcomes/index.js';\nimport {\n DEFAULT_CLI,\n CLI_NAMES,\n type CliNameLiteral,\n} from '../../config/model-capabilities-types.js';\n\nconst logger = createLogger({ tool: 'consensus-vote' });\n\n/**\n * Records a successful consensus vote to session memory AND outcome store. Best-effort.\n *\n * When every vote is simulated, this is a no-op: simulated votes are random\n * (#2319) and must not seed the learning store or outcome store, otherwise\n * test/demo runs poison real routing decisions.\n */\nexport function recordVoteSuccess(\n proposal: string,\n strategy: string,\n outcome: string,\n duration: number,\n votes?: readonly AgentVoteResult[]\n): void {\n const allSimulated =\n votes !== undefined && votes.length > 0 && votes.every((v) => v.source === 'simulation');\n if (allSimulated) {\n logger.debug('Skipping memory + outcome recording — all votes simulated');\n return;\n }\n\n try {\n const memory = getToolMemory();\n memory.recordTask({\n approach: `Consensus vote: ${strategy} on \"${proposal.slice(0, 50)}\"`,\n challenges: [],\n durationMs: duration,\n });\n memory.recordLearning({\n pattern: `${strategy} vote → ${outcome}`,\n context: `proposal=\"${proposal.slice(0, 40)}\" duration=${String(duration)}ms`,\n confidence: 0.8,\n source: 'consensus-vote',\n });\n void memory.runPromotionPipeline().catch((error: unknown) => {\n logger.warn('Promotion pipeline failed', { error });\n });\n } catch (error: unknown) {\n logger.warn('Failed to record vote success to memory', { error: getErrorMessage(error) });\n }\n\n // Also record to outcome store for adaptive routing feedback (#1551).\n // recordVoteOutcomes already filters per-vote `source === 'simulation'`,\n // but we keep the all-simulated guard above to skip the memory writes too.\n if (votes !== undefined) {\n recordVoteOutcomes(votes);\n }\n}\n\n/** Records a failed consensus vote to session memory. Best-effort. */\nexport function recordVoteError(proposal: string, errorMessage: string): void {\n try {\n const memory = getToolMemory();\n memory.recordError({\n error: `Consensus vote failed: ${errorMessage.slice(0, 150)}`,\n solution: 'Pending - vote execution failed',\n filePattern: 'mcp/tools/consensus-vote',\n });\n } catch (error: unknown) {\n logger.warn('Failed to record vote error', { error: getErrorMessage(error) });\n }\n}\n\n/**\n * Records per-vote outcomes to the outcome store for adaptive routing.\n * Each successful LLM vote contributes a sample to its CLI×category pair.\n * (Issue #1134 — cold start mitigation)\n */\nexport function recordVoteOutcomes(votes: readonly AgentVoteResult[]): void {\n try {\n const store = getOutcomeStore();\n const now = new Date().toISOString();\n for (const vote of votes) {\n if (vote.source === 'simulation') continue;\n const cliName: CliNameLiteral =\n vote.cli !== undefined && (CLI_NAMES as readonly string[]).includes(vote.cli)\n ? (vote.cli as CliNameLiteral)\n : DEFAULT_CLI;\n const voteSuccess = vote.source === 'llm';\n store.append({\n id: `vote-${String(getTimeProvider().now())}-${getRandomProvider().random().toString(36).slice(2, 8)}`,\n cli: cliName,\n category: 'planning',\n model: 'consensus',\n success: voteSuccess,\n durationMs: vote.processingTimeMs,\n timestamp: now,\n source: 'consensus',\n ...(!voteSuccess && vote.error !== undefined\n ? {\n failureCategory: categorizeOutcomeErrorMessage(vote.error),\n errorMessage: vote.error.slice(0, 500),\n }\n : {}),\n });\n }\n } catch (error: unknown) {\n logger.debug('Best-effort vote outcome recording failed', { error: getErrorMessage(error) });\n }\n}\n","/**\n * Simulation Guard — runtime safety net for `simulateVotes: true`.\n *\n * Simulated votes are random and exist only for unit tests and demos.\n * If a caller passes `simulateVotes: true` outside a test runner, this module\n * emits a one-shot stderr warning so the misuse cannot be silent.\n * (Source: Issue #2317, #2319)\n *\n * @module mcp/tools/simulation-guard\n */\n\nimport type { ILogger } from '../../core/index.js';\n\nconst WARNED = new Set<string>();\n\n/** Returns true when running under vitest or another test runner. */\nexport function isTestRunner(): boolean {\n return process.env.VITEST === 'true' || process.env.NODE_ENV === 'test';\n}\n\n/**\n * If `simulate` is true and we are not in a test runner, log a one-shot\n * warning per (tool, process) pair via the supplied logger. Returns the\n * `simulate` value unchanged so it can be used inline.\n *\n * Why: `simulateVotes: true` is a unit-test affordance; using it as a\n * fallback when adapters are unavailable produces random \"decisions\" that\n * silently corrupt downstream behavior. A loud warning is the minimum\n * defense; #2319 also stops simulated runs from polluting tool memory.\n */\nexport function warnIfSimulatedOutsideTests(toolName: string, logger: ILogger): void {\n if (isTestRunner()) return;\n if (WARNED.has(toolName)) return;\n WARNED.add(toolName);\n logger.warn(\n `[${toolName}] simulateVotes=true: output is RANDOM and reserved for tests/demos. Do not treat the result as a real decision.`\n );\n}\n\n/** Test-only: clear the warned-set so repeated tests can re-trigger the warning. */\nexport function _resetWarned(): void {\n WARNED.clear();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,SAAS,KAAAA,WAAS;;;ACWlB,SAAS,yBAAyB;AAqBlC,IAAM,iBAAiB,aAAa,EAAE,WAAW,eAAe,CAAC;AAQ1D,SAAS,kBAAkB,QAAiC;AACjE,WAAS,KAAK,OAAoBC,UAAgB,MAAqC;AACrF,QAAI;AACF,aAAO,mBA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;AAAA,IACA,EAAE,aAAa,aAAa,YAAY,cAAc,6BAA6B;AAAA,IACnF,cAAc,cAAc;AAAA,EAC9B;AACA,EAAAA,SAAO,KAAK,2EAA2E;AACzF;","names":["z","logger","logger","path","READ_ONLY_TOOLS","AsyncLocalStorage","AsyncLocalStorage","path","logger","err","logger","logger","Anthropic","Anthropic","logger","logger","cliAdapter","z","z","validateTimeout","logger","logger","defaultLogger","logger","z","z","z","TaskOutcomeSchema","z","logger","err","logger","logger","logger","logger","z","logger","z","z","z","fs","path","z","z","z","z","z","z","randomUUID","randomUUID","logger","z","path","logger","path","z","z","z","z","z","logger","logger","fs","z","z","logger","z","z","z","logger","logger","logger","err","logger","logger","readFileSync","writeFileSync","existsSync","logger","existsSync","readFileSync","writeFileSync","logger","logger","logger","z"]}