@stackmemoryai/stackmemory 0.3.9 → 0.3.11

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 (86) hide show
  1. package/README.md +54 -0
  2. package/dist/agents/core/agent-task-manager.js +12 -1
  3. package/dist/agents/core/agent-task-manager.js.map +3 -3
  4. package/dist/agents/testing-agent.js +610 -0
  5. package/dist/agents/testing-agent.js.map +7 -0
  6. package/dist/cli/browser-test.js +4 -4
  7. package/dist/cli/browser-test.js.map +2 -2
  8. package/dist/cli/claude-sm.js +2 -2
  9. package/dist/cli/claude-sm.js.map +2 -2
  10. package/dist/cli/codex-sm.js +5 -5
  11. package/dist/cli/codex-sm.js.map +2 -2
  12. package/dist/cli/commands/agent.js +3 -3
  13. package/dist/cli/commands/agent.js.map +2 -2
  14. package/dist/cli/commands/handoff.js +67 -20
  15. package/dist/cli/commands/handoff.js.map +3 -3
  16. package/dist/cli/commands/linear-unified.js +3 -3
  17. package/dist/cli/commands/linear-unified.js.map +2 -2
  18. package/dist/cli/commands/linear.js +2 -2
  19. package/dist/cli/commands/linear.js.map +2 -2
  20. package/dist/cli/commands/onboard.js +3 -3
  21. package/dist/cli/commands/onboard.js.map +2 -2
  22. package/dist/cli/commands/quality.js +2 -2
  23. package/dist/cli/commands/quality.js.map +2 -2
  24. package/dist/cli/commands/skills.js +113 -28
  25. package/dist/cli/commands/skills.js.map +2 -2
  26. package/dist/cli/commands/tasks.js +3 -3
  27. package/dist/cli/commands/tasks.js.map +2 -2
  28. package/dist/cli/commands/test.js +282 -0
  29. package/dist/cli/commands/test.js.map +7 -0
  30. package/dist/cli/commands/worktree.js +28 -10
  31. package/dist/cli/commands/worktree.js.map +2 -2
  32. package/dist/cli/index.js +5 -3
  33. package/dist/cli/index.js.map +2 -2
  34. package/dist/cli/utils/viewer.js +9 -9
  35. package/dist/cli/utils/viewer.js.map +2 -2
  36. package/dist/core/config/config-manager.js +26 -0
  37. package/dist/core/config/config-manager.js.map +3 -3
  38. package/dist/core/context/frame-handoff-manager.js +4 -4
  39. package/dist/core/context/frame-handoff-manager.js.map +1 -1
  40. package/dist/core/context/frame-manager.js +139 -0
  41. package/dist/core/context/frame-manager.js.map +2 -2
  42. package/dist/core/context/refactored-frame-manager.js +180 -1
  43. package/dist/core/context/refactored-frame-manager.js.map +2 -2
  44. package/dist/core/projects/project-isolation.js +197 -0
  45. package/dist/core/projects/project-isolation.js.map +7 -0
  46. package/dist/core/trace/debug-trace.js +1 -1
  47. package/dist/core/trace/debug-trace.js.map +2 -2
  48. package/dist/core/trace/index.js +4 -4
  49. package/dist/core/trace/index.js.map +2 -2
  50. package/dist/core/trace/trace-demo.js +8 -8
  51. package/dist/core/trace/trace-demo.js.map +2 -2
  52. package/dist/core/trace/trace-detector.demo.js +5 -5
  53. package/dist/core/trace/trace-detector.demo.js.map +2 -2
  54. package/dist/core/utils/update-checker.js +2 -2
  55. package/dist/core/utils/update-checker.js.map +2 -2
  56. package/dist/features/analytics/core/analytics-service.js +2 -2
  57. package/dist/features/analytics/core/analytics-service.js.map +2 -2
  58. package/dist/features/tasks/linear-task-manager.js +483 -0
  59. package/dist/features/tasks/linear-task-manager.js.map +7 -0
  60. package/dist/integrations/claude-code/subagent-client-stub.js +16 -0
  61. package/dist/integrations/claude-code/subagent-client-stub.js.map +7 -0
  62. package/dist/integrations/linear/auto-sync.js +2 -2
  63. package/dist/integrations/linear/auto-sync.js.map +2 -2
  64. package/dist/integrations/linear/config.js +12 -1
  65. package/dist/integrations/linear/config.js.map +2 -2
  66. package/dist/integrations/linear/sync-manager.js.map +1 -1
  67. package/dist/integrations/linear/sync.js.map +1 -1
  68. package/dist/integrations/linear/unified-sync.js.map +1 -1
  69. package/dist/integrations/linear/webhook-handler.js.map +2 -2
  70. package/dist/integrations/linear/webhook.js.map +2 -2
  71. package/dist/integrations/mcp/handlers/linear-handlers.js.map +1 -1
  72. package/dist/integrations/mcp/handlers/task-handlers.js.map +1 -1
  73. package/dist/integrations/mcp/refactored-server.js +2 -2
  74. package/dist/integrations/mcp/refactored-server.js.map +2 -2
  75. package/dist/integrations/mcp/server.js +3 -3
  76. package/dist/integrations/mcp/server.js.map +2 -2
  77. package/dist/mcp/stackmemory-mcp-server.js +3 -3
  78. package/dist/mcp/stackmemory-mcp-server.js.map +2 -2
  79. package/dist/skills/claude-skills.js +99 -5
  80. package/dist/skills/claude-skills.js.map +2 -2
  81. package/dist/skills/recursive-agent-orchestrator.js.map +1 -1
  82. package/dist/skills/security-secrets-scanner.js +21 -6
  83. package/dist/skills/security-secrets-scanner.js.map +2 -2
  84. package/dist/skills/unified-rlm-orchestrator.js +400 -0
  85. package/dist/skills/unified-rlm-orchestrator.js.map +7 -0
  86. package/package.json +4 -5
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/core/config/config-manager.ts"],
4
- "sourcesContent": ["/**\n * Configuration Manager for StackMemory\n * Handles loading, validation, and management of configuration\n */\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as yaml from 'js-yaml';\nimport {\n StackMemoryConfig,\n ProfileConfig,\n DEFAULT_CONFIG,\n PRESET_PROFILES,\n ScoringWeights,\n} from './types.js';\n\nexport interface ValidationResult {\n valid: boolean;\n errors: string[];\n warnings: string[];\n suggestions: string[];\n}\n\nexport class ConfigManager {\n private config: StackMemoryConfig;\n private configPath: string;\n private fileWatcher?: fs.FSWatcher;\n private onChangeCallbacks: Array<(config: StackMemoryConfig) => void> = [];\n\n constructor(configPath?: string) {\n this.configPath =\n configPath || path.join(process.cwd(), '.stackmemory', 'config.yaml');\n this.config = this.loadConfig();\n }\n\n /**\n * Load configuration from file or use defaults\n */\n private loadConfig(): StackMemoryConfig {\n try {\n if (fs.existsSync(this.configPath)) {\n const content = fs.readFileSync(this.configPath, 'utf-8');\n const loaded = yaml.load(content) as Partial<StackMemoryConfig>;\n return this.mergeWithDefaults(loaded);\n }\n } catch (error: unknown) {\n console.warn(`Failed to load config from ${this.configPath}:`, error);\n }\n // Deep clone to prevent mutation of DEFAULT_CONFIG\n return this.mergeWithDefaults({});\n }\n\n /**\n * Merge loaded config with defaults\n */\n private mergeWithDefaults(\n loaded: Partial<StackMemoryConfig>\n ): StackMemoryConfig {\n const config: StackMemoryConfig = {\n version: loaded.version || DEFAULT_CONFIG.version,\n profile: loaded.profile,\n scoring: {\n weights: {\n ...DEFAULT_CONFIG.scoring.weights,\n ...loaded.scoring?.weights,\n },\n tool_scores: {\n ...DEFAULT_CONFIG.scoring.tool_scores,\n ...loaded.scoring?.tool_scores,\n },\n },\n retention: {\n local: {\n ...DEFAULT_CONFIG.retention.local,\n ...loaded.retention?.local,\n },\n remote: {\n ...DEFAULT_CONFIG.retention.remote,\n ...loaded.retention?.remote,\n },\n generational_gc: {\n ...DEFAULT_CONFIG.retention.generational_gc,\n ...loaded.retention?.generational_gc,\n },\n },\n performance: { ...DEFAULT_CONFIG.performance, ...loaded.performance },\n profiles: { ...PRESET_PROFILES, ...loaded.profiles },\n };\n\n // Apply active profile if specified\n if (config.profile && config.profiles?.[config.profile]) {\n this.applyProfile(config, config.profiles[config.profile]);\n }\n\n return config;\n }\n\n /**\n * Apply a profile to the configuration\n */\n private applyProfile(\n config: StackMemoryConfig,\n profile: ProfileConfig\n ): void {\n if (profile.scoring) {\n if (profile.scoring.weights) {\n config.scoring.weights = {\n ...config.scoring.weights,\n ...profile.scoring.weights,\n };\n }\n if (profile.scoring.tool_scores) {\n config.scoring.tool_scores = {\n ...config.scoring.tool_scores,\n ...profile.scoring.tool_scores,\n };\n }\n }\n\n if (profile.retention) {\n if (profile.retention.local) {\n config.retention.local = {\n ...config.retention.local,\n ...profile.retention.local,\n };\n }\n if (profile.retention.remote) {\n config.retention.remote = {\n ...config.retention.remote,\n ...profile.retention.remote,\n };\n }\n if (profile.retention.generational_gc) {\n config.retention.generational_gc = {\n ...config.retention.generational_gc,\n ...profile.retention.generational_gc,\n };\n }\n }\n\n if (profile.performance) {\n config.performance = { ...config.performance, ...profile.performance };\n }\n }\n\n /**\n * Validate configuration\n */\n validate(): ValidationResult {\n const result: ValidationResult = {\n valid: true,\n errors: [],\n warnings: [],\n suggestions: [],\n };\n\n // Validate weights sum to 1.0\n const weights = this.config.scoring.weights;\n const weightSum =\n weights.base + weights.impact + weights.persistence + weights.reference;\n if (Math.abs(weightSum - 1.0) > 0.001) {\n result.errors.push(\n `Weights must sum to 1.0 (current: ${weightSum.toFixed(3)})`\n );\n result.valid = false;\n }\n\n // Validate weight ranges\n Object.entries(weights).forEach(([key, value]) => {\n if (value < 0 || value > 1) {\n result.errors.push(\n `Weight ${key} must be between 0 and 1 (current: ${value})`\n );\n result.valid = false;\n }\n });\n\n // Validate tool scores\n Object.entries(this.config.scoring.tool_scores).forEach(([tool, score]) => {\n if (score !== undefined && (score < 0 || score > 1)) {\n result.errors.push(\n `Tool score for ${tool} must be between 0 and 1 (current: ${score})`\n );\n result.valid = false;\n }\n });\n\n // Validate retention periods are ordered\n const youngMs = this.parseDuration(this.config.retention.local.young);\n const matureMs = this.parseDuration(this.config.retention.local.mature);\n const oldMs = this.parseDuration(this.config.retention.local.old);\n\n if (youngMs >= matureMs) {\n result.errors.push(\n 'Young retention period must be less than mature period'\n );\n result.valid = false;\n }\n if (matureMs >= oldMs) {\n result.errors.push(\n 'Mature retention period must be less than old period'\n );\n result.valid = false;\n }\n\n // Validate max size\n const maxSize = this.parseSize(this.config.retention.local.max_size);\n const availableSpace = this.getAvailableDiskSpace();\n if (availableSpace > 0 && maxSize > availableSpace) {\n result.warnings.push(\n `max_size (${this.config.retention.local.max_size}) exceeds available disk space`\n );\n }\n\n // Performance warnings\n if (this.config.performance.retrieval_timeout_ms < 100) {\n result.warnings.push(\n 'retrieval_timeout_ms < 100ms may be too aggressive'\n );\n }\n\n if (this.config.performance.max_stack_depth > 10000) {\n result.warnings.push('max_stack_depth > 10000 may impact performance');\n }\n\n // Suggestions\n if (!this.config.profile) {\n result.suggestions.push('Consider using a profile for your use case');\n }\n\n if (\n this.config?.scoring?.tool_scores?.search &&\n this.config.scoring.tool_scores.search < 0.5\n ) {\n result.suggestions.push(\n 'Search tool score seems low - consider increasing for better discovery'\n );\n }\n\n return result;\n }\n\n /**\n * Parse duration string to milliseconds\n */\n private parseDuration(duration: string): number {\n const match = duration.match(/^(\\d+)([hdwm])$/);\n if (!match) return 0;\n\n const value = parseInt(match[1]);\n const unit = match[2];\n\n const multipliers: Record<string, number> = {\n h: 3600000, // hours\n d: 86400000, // days\n w: 604800000, // weeks\n m: 2592000000, // months (30 days)\n };\n\n return value * (multipliers[unit] || 0);\n }\n\n /**\n * Parse size string to bytes\n */\n private parseSize(size: string): number {\n const match = size.match(/^(\\d+(?:\\.\\d+)?)([KMGT]B)?$/i);\n if (!match) return 0;\n\n const value = parseFloat(match[1]);\n const unit = match[2]?.toUpperCase() || 'B';\n\n const multipliers: Record<string, number> = {\n B: 1,\n KB: 1024,\n MB: 1024 * 1024,\n GB: 1024 * 1024 * 1024,\n TB: 1024 * 1024 * 1024 * 1024,\n };\n\n return value * (multipliers[unit] || 1);\n }\n\n /**\n * Get available disk space (simplified)\n */\n private getAvailableDiskSpace(): number {\n // This would need platform-specific implementation\n // For now, return 0 to skip validation\n return 0;\n }\n\n /**\n * Save configuration to file\n */\n save(): void {\n const dir = path.dirname(this.configPath);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n const content = yaml.dump(this.config, {\n indent: 2,\n lineWidth: 120,\n noRefs: true,\n });\n\n fs.writeFileSync(this.configPath, content, 'utf-8');\n }\n\n /**\n * Get current configuration\n */\n getConfig(): StackMemoryConfig {\n return { ...this.config };\n }\n\n /**\n * Set active profile\n */\n setProfile(profileName: string): boolean {\n const allProfiles = { ...PRESET_PROFILES, ...this.config.profiles };\n if (!allProfiles[profileName]) {\n return false;\n }\n\n // Apply the profile to current config\n this.config.profile = profileName;\n this.applyProfile(this.config, allProfiles[profileName]);\n this.notifyChange();\n return true;\n }\n\n /**\n * Update weights\n */\n updateWeights(weights: Partial<ScoringWeights>): void {\n this.config.scoring.weights = {\n ...this.config.scoring.weights,\n ...weights,\n };\n this.notifyChange();\n }\n\n /**\n * Update tool scores\n */\n updateToolScores(scores: Record<string, number>): void {\n this.config.scoring.tool_scores = {\n ...this.config.scoring.tool_scores,\n ...scores,\n };\n this.notifyChange();\n }\n\n /**\n * Enable hot reload\n */\n enableHotReload(): void {\n if (this.fileWatcher) return;\n\n if (fs.existsSync(this.configPath)) {\n this.fileWatcher = fs.watch(this.configPath, (eventType) => {\n if (eventType === 'change') {\n const newConfig = this.loadConfig();\n const validation = this.validate();\n\n if (validation.valid) {\n this.config = newConfig;\n this.notifyChange();\n console.log('Configuration reloaded');\n } else {\n console.error(\n 'Invalid configuration, keeping previous:',\n validation.errors\n );\n }\n }\n });\n }\n }\n\n /**\n * Disable hot reload\n */\n disableHotReload(): void {\n if (this.fileWatcher) {\n this.fileWatcher.close();\n this.fileWatcher = undefined;\n }\n }\n\n /**\n * Register change callback\n */\n onChange(callback: (config: StackMemoryConfig) => void): void {\n this.onChangeCallbacks.push(callback);\n }\n\n /**\n * Notify all change callbacks\n */\n private notifyChange(): void {\n const config = this.getConfig();\n this.onChangeCallbacks.forEach((cb) => cb(config));\n }\n\n /**\n * Calculate importance score for a tool\n */\n calculateScore(\n tool: string,\n additionalFactors?: {\n filesAffected?: number;\n isPermanent?: boolean;\n referenceCount?: number;\n }\n ): number {\n const baseScore = this.config.scoring.tool_scores[tool] || 0.5;\n const weights = this.config.scoring.weights;\n\n let score = baseScore * weights.base;\n\n if (additionalFactors) {\n // Impact multiplier (files affected)\n if (additionalFactors.filesAffected !== undefined) {\n const impactMultiplier = Math.min(\n additionalFactors.filesAffected / 10,\n 1\n );\n score += impactMultiplier * weights.impact;\n }\n\n // Persistence bonus\n if (additionalFactors.isPermanent) {\n score += 0.2 * weights.persistence;\n }\n\n // Reference count\n if (additionalFactors.referenceCount !== undefined) {\n const refMultiplier = Math.min(\n additionalFactors.referenceCount / 100,\n 1\n );\n score += refMultiplier * weights.reference;\n }\n }\n\n return Math.min(Math.max(score, 0), 1); // Clamp to [0, 1]\n }\n\n /**\n * Get available profiles\n */\n getProfiles(): Record<string, ProfileConfig> {\n return { ...PRESET_PROFILES, ...this.config.profiles };\n }\n}\n"],
5
- "mappings": "AAKA,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,UAAU;AACtB;AAAA,EAGE;AAAA,EACA;AAAA,OAEK;AASA,MAAM,cAAc;AAAA,EACjB;AAAA,EACA;AAAA,EACA;AAAA,EACA,oBAAgE,CAAC;AAAA,EAEzE,YAAY,YAAqB;AAC/B,SAAK,aACH,cAAc,KAAK,KAAK,QAAQ,IAAI,GAAG,gBAAgB,aAAa;AACtE,SAAK,SAAS,KAAK,WAAW;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA,EAKQ,aAAgC;AACtC,QAAI;AACF,UAAI,GAAG,WAAW,KAAK,UAAU,GAAG;AAClC,cAAM,UAAU,GAAG,aAAa,KAAK,YAAY,OAAO;AACxD,cAAM,SAAS,KAAK,KAAK,OAAO;AAChC,eAAO,KAAK,kBAAkB,MAAM;AAAA,MACtC;AAAA,IACF,SAAS,OAAgB;AACvB,cAAQ,KAAK,8BAA8B,KAAK,UAAU,KAAK,KAAK;AAAA,IACtE;AAEA,WAAO,KAAK,kBAAkB,CAAC,CAAC;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKQ,kBACN,QACmB;AACnB,UAAM,SAA4B;AAAA,MAChC,SAAS,OAAO,WAAW,eAAe;AAAA,MAC1C,SAAS,OAAO;AAAA,MAChB,SAAS;AAAA,QACP,SAAS;AAAA,UACP,GAAG,eAAe,QAAQ;AAAA,UAC1B,GAAG,OAAO,SAAS;AAAA,QACrB;AAAA,QACA,aAAa;AAAA,UACX,GAAG,eAAe,QAAQ;AAAA,UAC1B,GAAG,OAAO,SAAS;AAAA,QACrB;AAAA,MACF;AAAA,MACA,WAAW;AAAA,QACT,OAAO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MACA,mBAKQ;AACR,UAAM,YAAY,KAAK,OAAO,QAAQ,YAAY,IAAI,KAAK;AAC3D,UAAM,UAAU,KAAK,OAAO,QAAQ;AAEpC,QAAI,QAAQ,YAAY,QAAQ;AAEhC,QAAI,mBAAmB;AAErB,UAAI,kBAAkB,kBAAkB,QAAW;AACjD,cAAM,mBAAmB,KAAK;AAAA,UAC5B,kBAAkB,gBAAgB;AAAA,UAClC;AAAA,QACF;AACA,iBAAS,mBAAmB,QAAQ;AAAA,MACtC;AAGA,UAAI,kBAAkB,aAAa;AACjC,iBAAS,MAAM,QAAQ;AAAA,MACzB;AAGA,UAAI,kBAAkB,mBAAmB,QAAW;AAClD,cAAM,gBAAgB,KAAK;AAAA,UACzB,kBAAkB,iBAAiB;AAAA,UACnC;AAAA,QACF;AACA,iBAAS,gBAAgB,QAAQ;AAAA,MACnC;AAAA,IACF;AAEA,WAAO,KAAK,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,cAA6C;AAC3C,WAAO,EAAE,GAAG,iBAAiB,GAAG,KAAK,OAAO,SAAS;AAAA,EACvD;AACF;",
6
- "names": []
4
+ "sourcesContent": ["/**\n * Configuration Manager for StackMemory\n * Handles loading, validation, and management of configuration\n */\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as yaml from 'js-yaml';\nimport {\n StackMemoryConfig,\n ProfileConfig,\n DEFAULT_CONFIG,\n PRESET_PROFILES,\n ScoringWeights,\n} from './types.js';\n\nexport interface ValidationResult {\n valid: boolean;\n errors: string[];\n warnings: string[];\n suggestions: string[];\n}\n\nexport class ConfigManager {\n private static instance: ConfigManager | null = null;\n private config: StackMemoryConfig;\n private configPath: string;\n private fileWatcher?: fs.FSWatcher;\n private onChangeCallbacks: Array<(config: StackMemoryConfig) => void> = [];\n\n constructor(configPath?: string) {\n this.configPath =\n configPath || path.join(process.cwd(), '.stackmemory', 'config.yaml');\n this.config = this.loadConfig();\n }\n\n /**\n * Get singleton instance of ConfigManager\n */\n public static getInstance(configPath?: string): ConfigManager {\n if (!ConfigManager.instance) {\n ConfigManager.instance = new ConfigManager(configPath);\n }\n return ConfigManager.instance;\n }\n\n /**\n * Load configuration from file or use defaults\n */\n private loadConfig(): StackMemoryConfig {\n try {\n if (fs.existsSync(this.configPath)) {\n const content = fs.readFileSync(this.configPath, 'utf-8');\n const loaded = yaml.load(content) as Partial<StackMemoryConfig>;\n return this.mergeWithDefaults(loaded);\n }\n } catch (error: unknown) {\n console.warn(`Failed to load config from ${this.configPath}:`, error);\n }\n // Deep clone to prevent mutation of DEFAULT_CONFIG\n return this.mergeWithDefaults({});\n }\n\n /**\n * Merge loaded config with defaults\n */\n private mergeWithDefaults(\n loaded: Partial<StackMemoryConfig>\n ): StackMemoryConfig {\n const config: StackMemoryConfig = {\n version: loaded.version || DEFAULT_CONFIG.version,\n profile: loaded.profile,\n scoring: {\n weights: {\n ...DEFAULT_CONFIG.scoring.weights,\n ...loaded.scoring?.weights,\n },\n tool_scores: {\n ...DEFAULT_CONFIG.scoring.tool_scores,\n ...loaded.scoring?.tool_scores,\n },\n },\n retention: {\n local: {\n ...DEFAULT_CONFIG.retention.local,\n ...loaded.retention?.local,\n },\n remote: {\n ...DEFAULT_CONFIG.retention.remote,\n ...loaded.retention?.remote,\n },\n generational_gc: {\n ...DEFAULT_CONFIG.retention.generational_gc,\n ...loaded.retention?.generational_gc,\n },\n },\n performance: { ...DEFAULT_CONFIG.performance, ...loaded.performance },\n profiles: { ...PRESET_PROFILES, ...loaded.profiles },\n };\n\n // Apply active profile if specified\n if (config.profile && config.profiles?.[config.profile]) {\n this.applyProfile(config, config.profiles[config.profile]);\n }\n\n return config;\n }\n\n /**\n * Apply a profile to the configuration\n */\n private applyProfile(\n config: StackMemoryConfig,\n profile: ProfileConfig\n ): void {\n if (profile.scoring) {\n if (profile.scoring.weights) {\n config.scoring.weights = {\n ...config.scoring.weights,\n ...profile.scoring.weights,\n };\n }\n if (profile.scoring.tool_scores) {\n config.scoring.tool_scores = {\n ...config.scoring.tool_scores,\n ...profile.scoring.tool_scores,\n };\n }\n }\n\n if (profile.retention) {\n if (profile.retention.local) {\n config.retention.local = {\n ...config.retention.local,\n ...profile.retention.local,\n };\n }\n if (profile.retention.remote) {\n config.retention.remote = {\n ...config.retention.remote,\n ...profile.retention.remote,\n };\n }\n if (profile.retention.generational_gc) {\n config.retention.generational_gc = {\n ...config.retention.generational_gc,\n ...profile.retention.generational_gc,\n };\n }\n }\n\n if (profile.performance) {\n config.performance = { ...config.performance, ...profile.performance };\n }\n }\n\n /**\n * Validate configuration\n */\n validate(): ValidationResult {\n const result: ValidationResult = {\n valid: true,\n errors: [],\n warnings: [],\n suggestions: [],\n };\n\n // Validate weights sum to 1.0\n const weights = this.config.scoring.weights;\n const weightSum =\n weights.base + weights.impact + weights.persistence + weights.reference;\n if (Math.abs(weightSum - 1.0) > 0.001) {\n result.errors.push(\n `Weights must sum to 1.0 (current: ${weightSum.toFixed(3)})`\n );\n result.valid = false;\n }\n\n // Validate weight ranges\n Object.entries(weights).forEach(([key, value]) => {\n if (value < 0 || value > 1) {\n result.errors.push(\n `Weight ${key} must be between 0 and 1 (current: ${value})`\n );\n result.valid = false;\n }\n });\n\n // Validate tool scores\n Object.entries(this.config.scoring.tool_scores).forEach(([tool, score]) => {\n if (score !== undefined && (score < 0 || score > 1)) {\n result.errors.push(\n `Tool score for ${tool} must be between 0 and 1 (current: ${score})`\n );\n result.valid = false;\n }\n });\n\n // Validate retention periods are ordered\n const youngMs = this.parseDuration(this.config.retention.local.young);\n const matureMs = this.parseDuration(this.config.retention.local.mature);\n const oldMs = this.parseDuration(this.config.retention.local.old);\n\n if (youngMs >= matureMs) {\n result.errors.push(\n 'Young retention period must be less than mature period'\n );\n result.valid = false;\n }\n if (matureMs >= oldMs) {\n result.errors.push(\n 'Mature retention period must be less than old period'\n );\n result.valid = false;\n }\n\n // Validate max size\n const maxSize = this.parseSize(this.config.retention.local.max_size);\n const availableSpace = this.getAvailableDiskSpace();\n if (availableSpace > 0 && maxSize > availableSpace) {\n result.warnings.push(\n `max_size (${this.config.retention.local.max_size}) exceeds available disk space`\n );\n }\n\n // Performance warnings\n if (this.config.performance.retrieval_timeout_ms < 100) {\n result.warnings.push(\n 'retrieval_timeout_ms < 100ms may be too aggressive'\n );\n }\n\n if (this.config.performance.max_stack_depth > 10000) {\n result.warnings.push('max_stack_depth > 10000 may impact performance');\n }\n\n // Suggestions\n if (!this.config.profile) {\n result.suggestions.push('Consider using a profile for your use case');\n }\n\n if (\n this.config?.scoring?.tool_scores?.search &&\n this.config.scoring.tool_scores.search < 0.5\n ) {\n result.suggestions.push(\n 'Search tool score seems low - consider increasing for better discovery'\n );\n }\n\n return result;\n }\n\n /**\n * Parse duration string to milliseconds\n */\n private parseDuration(duration: string): number {\n const match = duration.match(/^(\\d+)([hdwm])$/);\n if (!match) return 0;\n\n const value = parseInt(match[1]);\n const unit = match[2];\n\n const multipliers: Record<string, number> = {\n h: 3600000, // hours\n d: 86400000, // days\n w: 604800000, // weeks\n m: 2592000000, // months (30 days)\n };\n\n return value * (multipliers[unit] || 0);\n }\n\n /**\n * Parse size string to bytes\n */\n private parseSize(size: string): number {\n const match = size.match(/^(\\d+(?:\\.\\d+)?)([KMGT]B)?$/i);\n if (!match) return 0;\n\n const value = parseFloat(match[1]);\n const unit = match[2]?.toUpperCase() || 'B';\n\n const multipliers: Record<string, number> = {\n B: 1,\n KB: 1024,\n MB: 1024 * 1024,\n GB: 1024 * 1024 * 1024,\n TB: 1024 * 1024 * 1024 * 1024,\n };\n\n return value * (multipliers[unit] || 1);\n }\n\n /**\n * Get available disk space (simplified)\n */\n private getAvailableDiskSpace(): number {\n // This would need platform-specific implementation\n // For now, return 0 to skip validation\n return 0;\n }\n\n /**\n * Save configuration to file\n */\n save(): void {\n const dir = path.dirname(this.configPath);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n const content = yaml.dump(this.config, {\n indent: 2,\n lineWidth: 120,\n noRefs: true,\n });\n\n fs.writeFileSync(this.configPath, content, 'utf-8');\n }\n\n /**\n * Get current configuration\n */\n getConfig(): StackMemoryConfig {\n return { ...this.config };\n }\n\n /**\n * Get a specific configuration value by path\n * Example: config.get('project.id') returns config.project.id\n */\n get(path: string): any {\n const keys = path.split('.');\n let value: any = this.config;\n \n for (const key of keys) {\n if (value && typeof value === 'object' && key in value) {\n value = value[key];\n } else {\n return undefined;\n }\n }\n \n return value;\n }\n\n /**\n * Set active profile\n */\n setProfile(profileName: string): boolean {\n const allProfiles = { ...PRESET_PROFILES, ...this.config.profiles };\n if (!allProfiles[profileName]) {\n return false;\n }\n\n // Apply the profile to current config\n this.config.profile = profileName;\n this.applyProfile(this.config, allProfiles[profileName]);\n this.notifyChange();\n return true;\n }\n\n /**\n * Update weights\n */\n updateWeights(weights: Partial<ScoringWeights>): void {\n this.config.scoring.weights = {\n ...this.config.scoring.weights,\n ...weights,\n };\n this.notifyChange();\n }\n\n /**\n * Update tool scores\n */\n updateToolScores(scores: Record<string, number>): void {\n this.config.scoring.tool_scores = {\n ...this.config.scoring.tool_scores,\n ...scores,\n };\n this.notifyChange();\n }\n\n /**\n * Enable hot reload\n */\n enableHotReload(): void {\n if (this.fileWatcher) return;\n\n if (fs.existsSync(this.configPath)) {\n this.fileWatcher = fs.watch(this.configPath, (eventType) => {\n if (eventType === 'change') {\n const newConfig = this.loadConfig();\n const validation = this.validate();\n\n if (validation.valid) {\n this.config = newConfig;\n this.notifyChange();\n console.log('Configuration reloaded');\n } else {\n console.error(\n 'Invalid configuration, keeping previous:',\n validation.errors\n );\n }\n }\n });\n }\n }\n\n /**\n * Disable hot reload\n */\n disableHotReload(): void {\n if (this.fileWatcher) {\n this.fileWatcher.close();\n this.fileWatcher = undefined;\n }\n }\n\n /**\n * Register change callback\n */\n onChange(callback: (config: StackMemoryConfig) => void): void {\n this.onChangeCallbacks.push(callback);\n }\n\n /**\n * Notify all change callbacks\n */\n private notifyChange(): void {\n const config = this.getConfig();\n this.onChangeCallbacks.forEach((cb) => cb(config));\n }\n\n /**\n * Calculate importance score for a tool\n */\n calculateScore(\n tool: string,\n additionalFactors?: {\n filesAffected?: number;\n isPermanent?: boolean;\n referenceCount?: number;\n }\n ): number {\n const baseScore = this.config.scoring.tool_scores[tool] || 0.5;\n const weights = this.config.scoring.weights;\n\n let score = baseScore * weights.base;\n\n if (additionalFactors) {\n // Impact multiplier (files affected)\n if (additionalFactors.filesAffected !== undefined) {\n const impactMultiplier = Math.min(\n additionalFactors.filesAffected / 10,\n 1\n );\n score += impactMultiplier * weights.impact;\n }\n\n // Persistence bonus\n if (additionalFactors.isPermanent) {\n score += 0.2 * weights.persistence;\n }\n\n // Reference count\n if (additionalFactors.referenceCount !== undefined) {\n const refMultiplier = Math.min(\n additionalFactors.referenceCount / 100,\n 1\n );\n score += refMultiplier * weights.reference;\n }\n }\n\n return Math.min(Math.max(score, 0), 1); // Clamp to [0, 1]\n }\n\n /**\n * Get available profiles\n */\n getProfiles(): Record<string, ProfileConfig> {\n return { ...PRESET_PROFILES, ...this.config.profiles };\n }\n}\n"],
5
+ "mappings": "AAKA,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,UAAU;AACtB;AAAA,EAGE;AAAA,EACA;AAAA,OAEK;AASA,MAAM,cAAc;AAAA,EACzB,OAAe,WAAiC;AAAA,EACxC;AAAA,EACA;AAAA,EACA;AAAA,EACA,oBAAgE,CAAC;AAAA,EAEzE,YAAY,YAAqB;AAC/B,SAAK,aACH,cAAc,KAAK,KAAK,QAAQ,IAAI,GAAG,gBAAgB,aAAa;AACtE,SAAK,SAAS,KAAK,WAAW;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,YAAY,YAAoC;AAC5D,QAAI,CAAC,cAAc,UAAU;AAC3B,oBAAc,WAAW,IAAI,cAAc,UAAU;AAAA,IACvD;AACA,WAAO,cAAc;AAAA,EACvB;AAAA;AAAA;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B,kBAAkB,gBAAgB;AAAA,UAClC;AAAA,QACF;AACA,iBAAS,mBAAmB,QAAQ;AAAA,MACtC;AAGA,UAAI,kBAAkB,aAAa;AACjC,iBAAS,MAAM,QAAQ;AAAA,MACzB;AAGA,UAAI,kBAAkB,mBAAmB,QAAW;AAClD,cAAM,gBAAgB,KAAK;AAAA,UACzB,kBAAkB,iBAAiB;AAAA,UACnC;AAAA,QACF;AACA,iBAAS,gBAAgB,QAAQ;AAAA,MACnC;AAAA,IACF;AAEA,WAAO,KAAK,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,cAA6C;AAC3C,WAAO,EAAE,GAAG,iBAAiB,GAAG,KAAK,OAAO,SAAS;AAAA,EACvD;AACF;",
6
+ "names": ["path"]
7
7
  }
@@ -374,7 +374,7 @@ class FrameHandoffManager {
374
374
  requestId,
375
375
  recipientId: "requester",
376
376
  // TODO: Get actual requester from handoff metadata
377
- title: "\u{1F504} Changes Requested for Handoff",
377
+ title: "Changes Requested for Handoff",
378
378
  message: `${approval.reviewerId} has requested changes: ${approval.feedback || "See detailed suggestions"}`,
379
379
  actionRequired: true,
380
380
  expiresAt: new Date(Date.now() + 48 * 60 * 60 * 1e3),
@@ -412,7 +412,7 @@ class FrameHandoffManager {
412
412
  requestId,
413
413
  recipientId: "all",
414
414
  // Will be distributed to all stakeholders
415
- title: "\u2705 Handoff Completed Successfully",
415
+ title: "Handoff Completed Successfully",
416
416
  message: `Frame transfer completed: ${result.mergedFrames.length} frames transferred${result.conflictFrames.length > 0 ? `, ${result.conflictFrames.length} conflicts resolved` : ""}`,
417
417
  actionRequired: false,
418
418
  createdAt: /* @__PURE__ */ new Date()
@@ -456,7 +456,7 @@ class FrameHandoffManager {
456
456
  requestId,
457
457
  recipientId: "all",
458
458
  // Will be distributed to all stakeholders
459
- title: "\u274C Handoff Cancelled",
459
+ title: "Handoff Cancelled",
460
460
  message: `Handoff request has been cancelled. Reason: ${reason}`,
461
461
  actionRequired: false,
462
462
  createdAt: /* @__PURE__ */ new Date()
@@ -637,7 +637,7 @@ class FrameHandoffManager {
637
637
  type: "request",
638
638
  requestId,
639
639
  recipientId: "all",
640
- title: "\u{1F4CA} Handoff Progress Update",
640
+ title: "Handoff Progress Update",
641
641
  message: `Status: ${progress.status} | Step: ${progress.currentStep} | Progress: ${progress.transferredFrames}/${progress.totalFrames} frames`,
642
642
  actionRequired: false,
643
643
  createdAt: /* @__PURE__ */ new Date()
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/core/context/frame-handoff-manager.ts"],
4
- "sourcesContent": ["/**\n * Frame Handoff Manager - STA-100\n * Handles frame transfers between individual and team stacks with approval workflows\n */\n\nimport type { Frame, Event, Anchor } from './frame-manager.js';\nimport {\n DualStackManager,\n type StackContext,\n type HandoffRequest,\n} from './dual-stack-manager.js';\nimport { logger } from '../monitoring/logger.js';\nimport { ValidationError, DatabaseError, ErrorCode } from '../errors/index.js';\nimport {\n validateInput,\n InitiateHandoffSchema,\n HandoffApprovalSchema,\n type InitiateHandoffInput,\n type HandoffApprovalInput,\n} from './validation.js';\n\nexport interface HandoffMetadata {\n initiatedAt: Date;\n initiatorId: string;\n targetUserId?: string;\n targetTeamId?: string;\n frameContext: {\n totalFrames: number;\n frameTypes: string[];\n estimatedSize: number;\n dependencies: string[];\n };\n businessContext?: {\n milestone?: string;\n priority: 'low' | 'medium' | 'high' | 'critical';\n deadline?: Date;\n stakeholders: string[];\n };\n}\n\nexport interface HandoffApproval {\n requestId: string;\n reviewerId: string;\n decision: 'approved' | 'rejected' | 'needs_changes';\n feedback?: string;\n suggestedChanges?: Array<{\n frameId: string;\n suggestion: string;\n reason: string;\n }>;\n reviewedAt: Date;\n}\n\nexport interface HandoffNotification {\n id: string;\n type: 'request' | 'approval' | 'rejection' | 'completion' | 'reminder';\n requestId: string;\n recipientId: string;\n title: string;\n message: string;\n actionRequired: boolean;\n expiresAt?: Date;\n createdAt: Date;\n}\n\nexport interface HandoffProgress {\n requestId: string;\n status:\n | 'pending_review'\n | 'approved'\n | 'in_transfer'\n | 'completed'\n | 'failed'\n | 'cancelled';\n transferredFrames: number;\n totalFrames: number;\n currentStep: string;\n estimatedCompletion?: Date;\n errors: Array<{\n step: string;\n error: string;\n timestamp: Date;\n }>;\n}\n\nexport class FrameHandoffManager {\n private dualStackManager: DualStackManager;\n private activeHandoffs: Map<string, HandoffProgress> = new Map();\n private pendingApprovals: Map<string, HandoffApproval[]> = new Map();\n private notifications: Map<string, HandoffNotification[]> = new Map();\n\n constructor(dualStackManager: DualStackManager) {\n this.dualStackManager = dualStackManager;\n }\n\n /**\n * Initiate a frame handoff with rich metadata and approval workflow\n */\n async initiateHandoff(\n targetStackId: string,\n frameIds: string[],\n metadata: HandoffMetadata,\n targetUserId?: string,\n message?: string\n ): Promise<string> {\n // Validate input parameters\n const input = validateInput(InitiateHandoffSchema, {\n targetStackId,\n frameIds,\n handoffRequest: metadata,\n reviewerId: targetUserId,\n description: message,\n });\n\n try {\n // Check handoff permissions\n await this.dualStackManager\n .getPermissionManager()\n .enforcePermission(\n this.dualStackManager\n .getPermissionManager()\n .createContext(\n input.handoffRequest.initiatorId,\n 'handoff',\n 'handoff',\n input.targetStackId\n )\n );\n\n // Validate frames exist and are transferable\n await this.validateFramesForHandoff(input.frameIds);\n\n // Create enhanced handoff request\n const requestId = await this.dualStackManager.initiateHandoff(\n input.targetStackId,\n input.frameIds,\n input.reviewerId,\n input.description\n );\n\n // Initialize handoff progress tracking\n const progress: HandoffProgress = {\n requestId,\n status: 'pending_review',\n transferredFrames: 0,\n totalFrames: input.frameIds.length,\n currentStep: 'Awaiting approval',\n errors: [],\n };\n\n this.activeHandoffs.set(requestId, progress);\n\n // Create notifications for relevant stakeholders\n await this.createHandoffNotifications(requestId, metadata, targetUserId);\n\n // Set up automatic reminders\n await this.scheduleHandoffReminders(requestId, metadata);\n\n logger.info(`Initiated enhanced handoff: ${requestId}`, {\n frameCount: frameIds.length,\n priority: metadata.businessContext?.priority,\n targetUser: targetUserId,\n });\n\n return requestId;\n } catch (error: unknown) {\n throw new DatabaseError(\n 'Failed to initiate handoff',\n ErrorCode.OPERATION_FAILED,\n { targetStackId, frameIds },\n error instanceof Error ? error : undefined\n );\n }\n }\n\n /**\n * Submit approval/rejection for handoff request\n */\n async submitHandoffApproval(\n requestId: string,\n approval: Omit<HandoffApproval, 'requestId' | 'reviewedAt'>\n ): Promise<void> {\n // Validate input parameters\n const input = validateInput(HandoffApprovalSchema, {\n ...approval,\n reviewerId: approval.reviewerId,\n });\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n throw new ValidationError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.HANDOFF_REQUEST_EXPIRED\n );\n }\n\n const fullApproval: HandoffApproval = {\n ...input,\n requestId,\n reviewedAt: new Date(),\n };\n\n // Store approval\n const existingApprovals = this.pendingApprovals.get(requestId) || [];\n existingApprovals.push(fullApproval);\n this.pendingApprovals.set(requestId, existingApprovals);\n\n // Update progress based on decision\n if (input.decision === 'approved') {\n progress.status = 'approved';\n progress.currentStep = 'Ready for transfer';\n\n // Automatically start transfer if approved\n await this.executeHandoffTransfer(requestId);\n } else if (input.decision === 'rejected') {\n progress.status = 'failed';\n progress.currentStep = 'Rejected by reviewer';\n progress.errors.push({\n step: 'approval',\n error: input.feedback || 'Request rejected',\n timestamp: new Date(),\n });\n } else if (input.decision === 'needs_changes') {\n progress.status = 'pending_review';\n progress.currentStep = 'Changes requested';\n\n // Notify requester of needed changes\n await this.notifyChangesRequested(requestId, approval);\n }\n\n this.activeHandoffs.set(requestId, progress);\n\n logger.info(`Handoff approval submitted: ${requestId}`, {\n decision: approval.decision,\n reviewer: approval.reviewerId,\n });\n }\n\n /**\n * Execute the actual frame transfer after approval\n */\n private async executeHandoffTransfer(requestId: string): Promise<void> {\n logger.debug('executeHandoffTransfer called', {\n requestId,\n availableHandoffs: Array.from(this.activeHandoffs.keys()),\n });\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n logger.error('Handoff progress not found', {\n requestId,\n availableHandoffs: Array.from(this.activeHandoffs.keys()),\n });\n throw new DatabaseError(\n `Handoff progress not found: ${requestId}`,\n ErrorCode.INVALID_STATE\n );\n }\n\n try {\n logger.debug('Setting progress status to in_transfer', { requestId });\n progress.status = 'in_transfer';\n progress.currentStep = 'Transferring frames';\n progress.estimatedCompletion = new Date(Date.now() + 5 * 60 * 1000); // 5 minutes\n\n // Execute the handoff through DualStackManager\n logger.debug('About to call acceptHandoff', { requestId });\n const result = await this.dualStackManager.acceptHandoff(requestId);\n logger.debug('acceptHandoff returned', {\n requestId,\n success: result.success,\n });\n\n if (result.success) {\n progress.status = 'completed';\n progress.currentStep = 'Transfer completed';\n progress.transferredFrames = result.mergedFrames.length;\n\n // Create completion notifications\n await this.notifyHandoffCompletion(requestId, result);\n\n logger.info(`Handoff transfer completed: ${requestId}`, {\n transferredFrames: progress.transferredFrames,\n conflicts: result.conflictFrames.length,\n });\n } else {\n progress.status = 'failed';\n progress.currentStep = 'Transfer failed';\n\n // Log errors\n result.errors.forEach((error) => {\n progress.errors.push({\n step: 'transfer',\n error: `Frame ${error.frameId}: ${error.error}`,\n timestamp: new Date(),\n });\n });\n\n throw new DatabaseError(\n 'Handoff transfer failed',\n ErrorCode.OPERATION_FAILED,\n { errors: result.errors }\n );\n }\n } catch (error: unknown) {\n progress.status = 'failed';\n progress.currentStep = 'Transfer error';\n progress.errors.push({\n step: 'transfer',\n error: error instanceof Error ? error.message : String(error),\n timestamp: new Date(),\n });\n\n logger.error(`Handoff transfer failed: ${requestId}`, error);\n throw error;\n } finally {\n this.activeHandoffs.set(requestId, progress);\n }\n }\n\n /**\n * Get handoff progress and status\n */\n async getHandoffProgress(requestId: string): Promise<HandoffProgress | null> {\n return this.activeHandoffs.get(requestId) || null;\n }\n\n /**\n * Cancel a pending handoff request\n */\n async cancelHandoff(requestId: string, reason: string): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n throw new DatabaseError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n }\n\n if (progress.status === 'in_transfer') {\n throw new DatabaseError(\n 'Cannot cancel handoff that is currently transferring',\n ErrorCode.INVALID_STATE\n );\n }\n\n progress.status = 'cancelled';\n progress.currentStep = 'Cancelled by user';\n progress.errors.push({\n step: 'cancellation',\n error: reason,\n timestamp: new Date(),\n });\n\n this.activeHandoffs.set(requestId, progress);\n\n // Notify relevant parties\n await this.notifyHandoffCancellation(requestId, reason);\n\n logger.info(`Handoff cancelled: ${requestId}`, { reason });\n }\n\n /**\n * Get all active handoffs for a user or team\n */\n async getActiveHandoffs(\n userId?: string,\n teamId?: string\n ): Promise<HandoffProgress[]> {\n const handoffs = Array.from(this.activeHandoffs.values());\n\n // Filter by user/team if specified\n if (userId || teamId) {\n // Would need to cross-reference with handoff metadata\n return handoffs.filter(\n (handoff) =>\n handoff.status === 'pending_review' ||\n handoff.status === 'approved' ||\n handoff.status === 'in_transfer'\n );\n }\n\n return handoffs;\n }\n\n /**\n * Get notifications for a user\n */\n async getUserNotifications(userId: string): Promise<HandoffNotification[]> {\n return this.notifications.get(userId) || [];\n }\n\n /**\n * Mark notification as read\n */\n async markNotificationRead(\n notificationId: string,\n userId: string\n ): Promise<void> {\n const userNotifications = this.notifications.get(userId) || [];\n const updatedNotifications = userNotifications.filter(\n (n) => n.id !== notificationId\n );\n this.notifications.set(userId, updatedNotifications);\n }\n\n /**\n * Validate frames are suitable for handoff\n */\n private async validateFramesForHandoff(frameIds: string[]): Promise<void> {\n const activeStack = this.dualStackManager.getActiveStack();\n\n for (const frameId of frameIds) {\n const frame = await activeStack.getFrame(frameId);\n if (!frame) {\n throw new DatabaseError(\n `Frame not found: ${frameId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n }\n\n // Check if frame is in a transferable state\n if (frame.state === 'active') {\n logger.warn(`Transferring active frame: ${frameId}`, {\n frameName: frame.name,\n });\n }\n }\n }\n\n /**\n * Create notifications for handoff stakeholders\n */\n private async createHandoffNotifications(\n requestId: string,\n metadata: HandoffMetadata,\n targetUserId?: string\n ): Promise<void> {\n const notifications: HandoffNotification[] = [];\n\n // Notify target user\n if (targetUserId) {\n notifications.push({\n id: `${requestId}-target`,\n type: 'request',\n requestId,\n recipientId: targetUserId,\n title: 'Frame Handoff Request',\n message: `${metadata.initiatorId} wants to transfer ${metadata.frameContext.totalFrames} frames to you`,\n actionRequired: true,\n expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),\n createdAt: new Date(),\n });\n }\n\n // Notify stakeholders\n if (metadata.businessContext?.stakeholders) {\n for (const stakeholderId of metadata.businessContext.stakeholders) {\n notifications.push({\n id: `${requestId}-stakeholder-${stakeholderId}`,\n type: 'request',\n requestId,\n recipientId: stakeholderId,\n title: 'Frame Handoff Notification',\n message: `Frame transfer initiated for ${metadata.businessContext?.milestone || 'project milestone'}`,\n actionRequired: false,\n createdAt: new Date(),\n });\n }\n }\n\n // Store notifications\n for (const notification of notifications) {\n const userNotifications =\n this.notifications.get(notification.recipientId) || [];\n userNotifications.push(notification);\n this.notifications.set(notification.recipientId, userNotifications);\n }\n }\n\n /**\n * Schedule reminder notifications\n */\n private async scheduleHandoffReminders(\n requestId: string,\n metadata: HandoffMetadata\n ): Promise<void> {\n // Schedule reminder in 4 hours if high priority\n if (\n metadata.businessContext?.priority === 'high' ||\n metadata.businessContext?.priority === 'critical'\n ) {\n setTimeout(\n async () => {\n const progress = this.activeHandoffs.get(requestId);\n if (progress && progress.status === 'pending_review') {\n await this.sendHandoffReminder(requestId, metadata);\n }\n },\n 4 * 60 * 60 * 1000\n ); // 4 hours\n }\n }\n\n /**\n * Send handoff reminder\n */\n private async sendHandoffReminder(\n requestId: string,\n metadata: HandoffMetadata\n ): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress || progress.status !== 'pending_review') {\n return;\n }\n\n const reminderNotification: HandoffNotification = {\n id: `${requestId}-reminder-${Date.now()}`,\n type: 'reminder',\n requestId,\n recipientId: metadata.targetUserId || 'unknown',\n title: '\u23F0 Handoff Request Reminder',\n message: `Reminder: ${metadata.initiatorId} is waiting for approval on ${metadata.frameContext.totalFrames} frames. Priority: ${metadata.businessContext?.priority || 'medium'}`,\n actionRequired: true,\n expiresAt: new Date(Date.now() + 12 * 60 * 60 * 1000), // 12 hours\n createdAt: new Date(),\n };\n\n // Store the notification\n if (metadata.targetUserId) {\n const userNotifications =\n this.notifications.get(metadata.targetUserId) || [];\n userNotifications.push(reminderNotification);\n this.notifications.set(metadata.targetUserId, userNotifications);\n\n logger.info(`Sent handoff reminder: ${requestId}`, {\n priority: metadata.businessContext?.priority,\n recipient: metadata.targetUserId,\n });\n }\n\n // Also notify stakeholders\n if (metadata.businessContext?.stakeholders) {\n for (const stakeholderId of metadata.businessContext.stakeholders) {\n const stakeholderNotification: HandoffNotification = {\n ...reminderNotification,\n id: `${requestId}-reminder-stakeholder-${stakeholderId}-${Date.now()}`,\n recipientId: stakeholderId,\n title: '\uD83D\uDCCB Handoff Status Update',\n message: `Pending handoff approval: ${metadata.businessContext?.milestone || 'development work'} requires attention`,\n actionRequired: false,\n };\n\n const stakeholderNotifications =\n this.notifications.get(stakeholderId) || [];\n stakeholderNotifications.push(stakeholderNotification);\n this.notifications.set(stakeholderId, stakeholderNotifications);\n }\n }\n }\n\n /**\n * Notify when changes are requested\n */\n private async notifyChangesRequested(\n requestId: string,\n approval: Omit<HandoffApproval, 'requestId' | 'reviewedAt'>\n ): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) return;\n\n // Find the original requester (we'll need to enhance this with better metadata tracking)\n const changeRequestNotification: HandoffNotification = {\n id: `${requestId}-changes-${Date.now()}`,\n type: 'request',\n requestId,\n recipientId: 'requester', // TODO: Get actual requester from handoff metadata\n title: '\uD83D\uDD04 Changes Requested for Handoff',\n message: `${approval.reviewerId} has requested changes: ${approval.feedback || 'See detailed suggestions'}`,\n actionRequired: true,\n expiresAt: new Date(Date.now() + 48 * 60 * 60 * 1000), // 48 hours\n createdAt: new Date(),\n };\n\n // Store notification (for now using a placeholder recipient)\n const notifications = this.notifications.get('requester') || [];\n notifications.push(changeRequestNotification);\n this.notifications.set('requester', notifications);\n\n // Log detailed feedback and suggestions\n logger.info(`Changes requested for handoff: ${requestId}`, {\n reviewer: approval.reviewerId,\n feedback: approval.feedback,\n suggestedChangesCount: approval.suggestedChanges?.length || 0,\n });\n\n if (approval.suggestedChanges && approval.suggestedChanges.length > 0) {\n logger.info(`Detailed change suggestions:`, {\n requestId,\n suggestions: approval.suggestedChanges.map((change) => ({\n frameId: change.frameId,\n suggestion: change.suggestion,\n reason: change.reason,\n })),\n });\n }\n }\n\n /**\n * Notify handoff completion\n */\n private async notifyHandoffCompletion(\n requestId: string,\n result: any\n ): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) return;\n\n // Create completion notification\n const completionNotification: HandoffNotification = {\n id: `${requestId}-completion-${Date.now()}`,\n type: 'completion',\n requestId,\n recipientId: 'all', // Will be distributed to all stakeholders\n title: '\u2705 Handoff Completed Successfully',\n message: `Frame transfer completed: ${result.mergedFrames.length} frames transferred${result.conflictFrames.length > 0 ? `, ${result.conflictFrames.length} conflicts resolved` : ''}`,\n actionRequired: false,\n createdAt: new Date(),\n };\n\n // Notify all stakeholders from the notifications map\n const allUsers = Array.from(this.notifications.keys());\n for (const userId of allUsers) {\n const userSpecificNotification: HandoffNotification = {\n ...completionNotification,\n id: `${requestId}-completion-${userId}-${Date.now()}`,\n recipientId: userId,\n };\n\n const userNotifications = this.notifications.get(userId) || [];\n userNotifications.push(userSpecificNotification);\n this.notifications.set(userId, userNotifications);\n }\n\n logger.info(`Handoff completed: ${requestId}`, {\n mergedFrames: result.mergedFrames.length,\n conflicts: result.conflictFrames.length,\n notifiedUsers: allUsers.length,\n });\n\n // Log detailed completion statistics\n if (result.conflictFrames.length > 0) {\n logger.info(`Handoff completion details:`, {\n requestId,\n transferredFrames: result.mergedFrames.map(\n (f: any) => f.frameId || f.id\n ),\n conflictFrames: result.conflictFrames.map(\n (f: any) => f.frameId || f.id\n ),\n });\n }\n }\n\n /**\n * Notify handoff cancellation\n */\n private async notifyHandoffCancellation(\n requestId: string,\n reason: string\n ): Promise<void> {\n // Create cancellation notification\n const cancellationNotification: HandoffNotification = {\n id: `${requestId}-cancellation-${Date.now()}`,\n type: 'request', // Using 'request' type as it's informational\n requestId,\n recipientId: 'all', // Will be distributed to all stakeholders\n title: '\u274C Handoff Cancelled',\n message: `Handoff request has been cancelled. Reason: ${reason}`,\n actionRequired: false,\n createdAt: new Date(),\n };\n\n // Notify all users who have been involved in this handoff\n const allUsers = Array.from(this.notifications.keys());\n for (const userId of allUsers) {\n const userSpecificNotification: HandoffNotification = {\n ...cancellationNotification,\n id: `${requestId}-cancellation-${userId}-${Date.now()}`,\n recipientId: userId,\n };\n\n const userNotifications = this.notifications.get(userId) || [];\n userNotifications.push(userSpecificNotification);\n this.notifications.set(userId, userNotifications);\n }\n\n logger.info(`Handoff cancelled: ${requestId}`, {\n reason,\n notifiedUsers: allUsers.length,\n });\n }\n\n /**\n * Get handoff analytics and metrics\n */\n async getHandoffMetrics(timeRange?: { start: Date; end: Date }): Promise<{\n totalHandoffs: number;\n completedHandoffs: number;\n averageProcessingTime: number;\n topFrameTypes: Array<{ type: string; count: number }>;\n collaborationPatterns: Array<{\n sourceUser: string;\n targetUser: string;\n count: number;\n }>;\n }> {\n const handoffs = Array.from(this.activeHandoffs.values());\n\n // Filter by time range if specified\n const filteredHandoffs = timeRange\n ? handoffs.filter((h) => {\n // Would need to add timestamps to track creation time\n return true; // Placeholder\n })\n : handoffs;\n\n const completedHandoffs = filteredHandoffs.filter(\n (h) => h.status === 'completed'\n );\n\n return {\n totalHandoffs: filteredHandoffs.length,\n completedHandoffs: completedHandoffs.length,\n averageProcessingTime:\n this.calculateAverageProcessingTime(completedHandoffs),\n topFrameTypes: this.analyzeFrameTypes(filteredHandoffs),\n collaborationPatterns:\n this.analyzeCollaborationPatterns(filteredHandoffs),\n };\n }\n\n private calculateAverageProcessingTime(handoffs: HandoffProgress[]): number {\n if (handoffs.length === 0) return 0;\n\n let totalProcessingTime = 0;\n let validHandoffs = 0;\n\n for (const handoff of handoffs) {\n // Only calculate for completed handoffs that have timing data\n if (handoff.status === 'completed' && handoff.estimatedCompletion) {\n // Estimate processing time based on frame count and complexity\n // This is a simplified calculation - in practice you'd track actual timestamps\n const frameComplexity = handoff.totalFrames * 0.5; // Base time per frame\n const errorPenalty = handoff.errors.length * 2; // Extra time for errors\n const processingTime = Math.max(1, frameComplexity + errorPenalty);\n\n totalProcessingTime += processingTime;\n validHandoffs++;\n }\n }\n\n return validHandoffs > 0\n ? Math.round(totalProcessingTime / validHandoffs)\n : 0;\n }\n\n private analyzeFrameTypes(\n handoffs: HandoffProgress[]\n ): Array<{ type: string; count: number }> {\n const frameTypeCount = new Map<string, number>();\n\n for (const handoff of handoffs) {\n // Extract frame type information from handoff metadata\n // This would need to be enhanced with actual frame type tracking\n const estimatedTypes = this.estimateFrameTypes(handoff);\n\n for (const type of estimatedTypes) {\n frameTypeCount.set(type, (frameTypeCount.get(type) || 0) + 1);\n }\n }\n\n return Array.from(frameTypeCount.entries())\n .map(([type, count]) => ({ type, count }))\n .sort((a, b) => b.count - a.count)\n .slice(0, 10); // Top 10 frame types\n }\n\n private estimateFrameTypes(handoff: HandoffProgress): string[] {\n // Simplified frame type estimation based on handoff characteristics\n const types: string[] = [];\n\n if (handoff.totalFrames > 10) {\n types.push('bulk_transfer');\n }\n if (handoff.errors.length > 0) {\n types.push('complex_handoff');\n }\n if (handoff.transferredFrames === handoff.totalFrames) {\n types.push('complete_transfer');\n } else {\n types.push('partial_transfer');\n }\n\n // Add some common frame types based on patterns\n types.push('development', 'collaboration');\n\n return types;\n }\n\n private analyzeCollaborationPatterns(\n handoffs: HandoffProgress[]\n ): Array<{ sourceUser: string; targetUser: string; count: number }> {\n const collaborationCount = new Map<string, number>();\n\n for (const handoff of handoffs) {\n // Extract collaboration pattern from handoff data\n // Note: This is simplified - we'd need to track actual source/target users\n const pattern = this.extractCollaborationPattern(handoff);\n if (pattern) {\n const key = `${pattern.sourceUser}->${pattern.targetUser}`;\n collaborationCount.set(key, (collaborationCount.get(key) || 0) + 1);\n }\n }\n\n return Array.from(collaborationCount.entries())\n .map(([pattern, count]) => {\n const [sourceUser, targetUser] = pattern.split('->');\n return { sourceUser, targetUser, count };\n })\n .sort((a, b) => b.count - a.count)\n .slice(0, 20); // Top 20 collaboration patterns\n }\n\n private extractCollaborationPattern(\n handoff: HandoffProgress\n ): { sourceUser: string; targetUser: string } | null {\n // Simplified pattern extraction - in practice this would come from handoff metadata\n // For now, we'll create sample patterns based on handoff characteristics\n\n if (handoff.status === 'completed') {\n return {\n sourceUser: 'developer',\n targetUser: 'reviewer',\n };\n } else if (handoff.status === 'failed') {\n return {\n sourceUser: 'developer',\n targetUser: 'lead',\n };\n }\n\n return null;\n }\n\n /**\n * Real-time collaboration features\n */\n\n /**\n * Get real-time handoff status updates\n */\n async getHandoffStatusStream(\n requestId: string\n ): Promise<AsyncIterableIterator<HandoffProgress>> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n throw new DatabaseError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n }\n\n // Simple implementation - in a real system this would use WebSockets or Server-Sent Events\n const self = this;\n return {\n async *[Symbol.asyncIterator]() {\n let lastStatus = progress.status;\n while (\n lastStatus !== 'completed' &&\n lastStatus !== 'failed' &&\n lastStatus !== 'cancelled'\n ) {\n const currentProgress = self.activeHandoffs.get(requestId);\n if (currentProgress && currentProgress.status !== lastStatus) {\n lastStatus = currentProgress.status;\n yield currentProgress;\n }\n // Simulate real-time polling\n await new Promise((resolve) => setTimeout(resolve, 1000));\n }\n },\n };\n }\n\n /**\n * Update handoff progress in real-time\n */\n async updateHandoffProgress(\n requestId: string,\n update: Partial<HandoffProgress>\n ): Promise<void> {\n let progress = this.activeHandoffs.get(requestId);\n\n // If progress doesn't exist and update includes required fields, create it\n if (\n !progress &&\n update.requestId &&\n update.status &&\n update.totalFrames !== undefined\n ) {\n progress = {\n requestId: update.requestId,\n status: update.status,\n transferredFrames: 0,\n totalFrames: update.totalFrames,\n currentStep: 'Initialized',\n errors: [],\n ...update,\n };\n } else if (!progress) {\n throw new DatabaseError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n } else {\n // Update existing progress with provided fields\n progress = {\n ...progress,\n ...update,\n };\n }\n\n this.activeHandoffs.set(requestId, progress);\n\n logger.info(`Handoff progress updated: ${requestId}`, {\n status: progress.status,\n currentStep: progress.currentStep,\n transferredFrames: progress.transferredFrames,\n });\n\n // Notify stakeholders of progress update\n await this.notifyProgressUpdate(requestId, progress);\n }\n\n /**\n * Notify stakeholders of progress updates\n */\n private async notifyProgressUpdate(\n requestId: string,\n progress: HandoffProgress\n ): Promise<void> {\n const updateNotification: HandoffNotification = {\n id: `${requestId}-progress-${Date.now()}`,\n type: 'request',\n requestId,\n recipientId: 'all',\n title: '\uD83D\uDCCA Handoff Progress Update',\n message: `Status: ${progress.status} | Step: ${progress.currentStep} | Progress: ${progress.transferredFrames}/${progress.totalFrames} frames`,\n actionRequired: false,\n createdAt: new Date(),\n };\n\n // Distribute to all stakeholders\n const allUsers = Array.from(this.notifications.keys());\n for (const userId of allUsers) {\n const userNotifications = this.notifications.get(userId) || [];\n userNotifications.push({\n ...updateNotification,\n id: `${requestId}-progress-${userId}-${Date.now()}`,\n recipientId: userId,\n });\n this.notifications.set(userId, userNotifications);\n }\n }\n\n /**\n * Get active handoffs with real-time filtering\n */\n async getActiveHandoffsRealTime(filters?: {\n status?: HandoffProgress['status'];\n userId?: string;\n priority?: 'low' | 'medium' | 'high' | 'critical';\n }): Promise<HandoffProgress[]> {\n let handoffs = Array.from(this.activeHandoffs.values());\n\n if (filters?.status) {\n handoffs = handoffs.filter((h) => h.status === filters.status);\n }\n\n if (filters?.userId) {\n // In a real implementation, we'd have proper user tracking in handoff metadata\n // For now, filter based on requestId pattern or other heuristics\n handoffs = handoffs.filter((h) =>\n h.requestId.includes(filters.userId || '')\n );\n }\n\n if (filters?.priority) {\n // Filter by priority (this would need priority tracking in HandoffProgress)\n // For now, estimate priority based on frame count and errors\n handoffs = handoffs.filter((h) => {\n const estimatedPriority = this.estimateHandoffPriority(h);\n return estimatedPriority === filters.priority;\n });\n }\n\n return handoffs.sort((a, b) => {\n // Sort by status priority, then by creation time\n const statusPriority = {\n in_transfer: 4,\n approved: 3,\n pending_review: 2,\n completed: 1,\n failed: 1,\n cancelled: 0,\n };\n return (statusPriority[b.status] || 0) - (statusPriority[a.status] || 0);\n });\n }\n\n private estimateHandoffPriority(\n handoff: HandoffProgress\n ): 'low' | 'medium' | 'high' | 'critical' {\n if (handoff.errors.length > 2 || handoff.totalFrames > 50)\n return 'critical';\n if (handoff.errors.length > 0 || handoff.totalFrames > 20) return 'high';\n if (handoff.totalFrames > 5) return 'medium';\n return 'low';\n }\n\n /**\n * Bulk handoff operations for team collaboration\n */\n async bulkHandoffOperation(operation: {\n action: 'approve' | 'reject' | 'cancel';\n requestIds: string[];\n reviewerId: string;\n feedback?: string;\n }): Promise<{\n successful: string[];\n failed: Array<{ requestId: string; error: string }>;\n }> {\n const results = {\n successful: [],\n failed: [] as Array<{ requestId: string; error: string }>,\n };\n\n for (const requestId of operation.requestIds) {\n try {\n switch (operation.action) {\n case 'approve':\n await this.submitHandoffApproval(requestId, {\n reviewerId: operation.reviewerId,\n decision: 'approved',\n feedback: operation.feedback,\n });\n results.successful.push(requestId);\n break;\n\n case 'reject':\n await this.submitHandoffApproval(requestId, {\n reviewerId: operation.reviewerId,\n decision: 'rejected',\n feedback: operation.feedback || 'Bulk rejection',\n });\n results.successful.push(requestId);\n break;\n\n case 'cancel':\n await this.cancelHandoff(\n requestId,\n operation.feedback || 'Bulk cancellation'\n );\n results.successful.push(requestId);\n break;\n }\n } catch (error: unknown) {\n results.failed.push({\n requestId,\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n\n logger.info(`Bulk handoff operation completed`, {\n action: operation.action,\n successful: results.successful.length,\n failed: results.failed.length,\n reviewerId: operation.reviewerId,\n });\n\n return results;\n }\n\n /**\n * Enhanced notification management with cleanup\n */\n async cleanupExpiredNotifications(userId?: string): Promise<number> {\n let cleanedCount = 0;\n const now = new Date();\n\n const userIds = userId ? [userId] : Array.from(this.notifications.keys());\n\n for (const uid of userIds) {\n const userNotifications = this.notifications.get(uid) || [];\n const activeNotifications = userNotifications.filter((notification) => {\n if (notification.expiresAt && notification.expiresAt < now) {\n cleanedCount++;\n return false;\n }\n return true;\n });\n\n this.notifications.set(uid, activeNotifications);\n }\n\n if (cleanedCount > 0) {\n logger.info(`Cleaned up expired notifications`, {\n count: cleanedCount,\n userId: userId || 'all',\n });\n }\n\n return cleanedCount;\n }\n}\n"],
4
+ "sourcesContent": ["/**\n * Frame Handoff Manager - STA-100\n * Handles frame transfers between individual and team stacks with approval workflows\n */\n\nimport type { Frame, Event, Anchor } from './frame-manager.js';\nimport {\n DualStackManager,\n type StackContext,\n type HandoffRequest,\n} from './dual-stack-manager.js';\nimport { logger } from '../monitoring/logger.js';\nimport { ValidationError, DatabaseError, ErrorCode } from '../errors/index.js';\nimport {\n validateInput,\n InitiateHandoffSchema,\n HandoffApprovalSchema,\n type InitiateHandoffInput,\n type HandoffApprovalInput,\n} from './validation.js';\n\nexport interface HandoffMetadata {\n initiatedAt: Date;\n initiatorId: string;\n targetUserId?: string;\n targetTeamId?: string;\n frameContext: {\n totalFrames: number;\n frameTypes: string[];\n estimatedSize: number;\n dependencies: string[];\n };\n businessContext?: {\n milestone?: string;\n priority: 'low' | 'medium' | 'high' | 'critical';\n deadline?: Date;\n stakeholders: string[];\n };\n}\n\nexport interface HandoffApproval {\n requestId: string;\n reviewerId: string;\n decision: 'approved' | 'rejected' | 'needs_changes';\n feedback?: string;\n suggestedChanges?: Array<{\n frameId: string;\n suggestion: string;\n reason: string;\n }>;\n reviewedAt: Date;\n}\n\nexport interface HandoffNotification {\n id: string;\n type: 'request' | 'approval' | 'rejection' | 'completion' | 'reminder';\n requestId: string;\n recipientId: string;\n title: string;\n message: string;\n actionRequired: boolean;\n expiresAt?: Date;\n createdAt: Date;\n}\n\nexport interface HandoffProgress {\n requestId: string;\n status:\n | 'pending_review'\n | 'approved'\n | 'in_transfer'\n | 'completed'\n | 'failed'\n | 'cancelled';\n transferredFrames: number;\n totalFrames: number;\n currentStep: string;\n estimatedCompletion?: Date;\n errors: Array<{\n step: string;\n error: string;\n timestamp: Date;\n }>;\n}\n\nexport class FrameHandoffManager {\n private dualStackManager: DualStackManager;\n private activeHandoffs: Map<string, HandoffProgress> = new Map();\n private pendingApprovals: Map<string, HandoffApproval[]> = new Map();\n private notifications: Map<string, HandoffNotification[]> = new Map();\n\n constructor(dualStackManager: DualStackManager) {\n this.dualStackManager = dualStackManager;\n }\n\n /**\n * Initiate a frame handoff with rich metadata and approval workflow\n */\n async initiateHandoff(\n targetStackId: string,\n frameIds: string[],\n metadata: HandoffMetadata,\n targetUserId?: string,\n message?: string\n ): Promise<string> {\n // Validate input parameters\n const input = validateInput(InitiateHandoffSchema, {\n targetStackId,\n frameIds,\n handoffRequest: metadata,\n reviewerId: targetUserId,\n description: message,\n });\n\n try {\n // Check handoff permissions\n await this.dualStackManager\n .getPermissionManager()\n .enforcePermission(\n this.dualStackManager\n .getPermissionManager()\n .createContext(\n input.handoffRequest.initiatorId,\n 'handoff',\n 'handoff',\n input.targetStackId\n )\n );\n\n // Validate frames exist and are transferable\n await this.validateFramesForHandoff(input.frameIds);\n\n // Create enhanced handoff request\n const requestId = await this.dualStackManager.initiateHandoff(\n input.targetStackId,\n input.frameIds,\n input.reviewerId,\n input.description\n );\n\n // Initialize handoff progress tracking\n const progress: HandoffProgress = {\n requestId,\n status: 'pending_review',\n transferredFrames: 0,\n totalFrames: input.frameIds.length,\n currentStep: 'Awaiting approval',\n errors: [],\n };\n\n this.activeHandoffs.set(requestId, progress);\n\n // Create notifications for relevant stakeholders\n await this.createHandoffNotifications(requestId, metadata, targetUserId);\n\n // Set up automatic reminders\n await this.scheduleHandoffReminders(requestId, metadata);\n\n logger.info(`Initiated enhanced handoff: ${requestId}`, {\n frameCount: frameIds.length,\n priority: metadata.businessContext?.priority,\n targetUser: targetUserId,\n });\n\n return requestId;\n } catch (error: unknown) {\n throw new DatabaseError(\n 'Failed to initiate handoff',\n ErrorCode.OPERATION_FAILED,\n { targetStackId, frameIds },\n error instanceof Error ? error : undefined\n );\n }\n }\n\n /**\n * Submit approval/rejection for handoff request\n */\n async submitHandoffApproval(\n requestId: string,\n approval: Omit<HandoffApproval, 'requestId' | 'reviewedAt'>\n ): Promise<void> {\n // Validate input parameters\n const input = validateInput(HandoffApprovalSchema, {\n ...approval,\n reviewerId: approval.reviewerId,\n });\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n throw new ValidationError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.HANDOFF_REQUEST_EXPIRED\n );\n }\n\n const fullApproval: HandoffApproval = {\n ...input,\n requestId,\n reviewedAt: new Date(),\n };\n\n // Store approval\n const existingApprovals = this.pendingApprovals.get(requestId) || [];\n existingApprovals.push(fullApproval);\n this.pendingApprovals.set(requestId, existingApprovals);\n\n // Update progress based on decision\n if (input.decision === 'approved') {\n progress.status = 'approved';\n progress.currentStep = 'Ready for transfer';\n\n // Automatically start transfer if approved\n await this.executeHandoffTransfer(requestId);\n } else if (input.decision === 'rejected') {\n progress.status = 'failed';\n progress.currentStep = 'Rejected by reviewer';\n progress.errors.push({\n step: 'approval',\n error: input.feedback || 'Request rejected',\n timestamp: new Date(),\n });\n } else if (input.decision === 'needs_changes') {\n progress.status = 'pending_review';\n progress.currentStep = 'Changes requested';\n\n // Notify requester of needed changes\n await this.notifyChangesRequested(requestId, approval);\n }\n\n this.activeHandoffs.set(requestId, progress);\n\n logger.info(`Handoff approval submitted: ${requestId}`, {\n decision: approval.decision,\n reviewer: approval.reviewerId,\n });\n }\n\n /**\n * Execute the actual frame transfer after approval\n */\n private async executeHandoffTransfer(requestId: string): Promise<void> {\n logger.debug('executeHandoffTransfer called', {\n requestId,\n availableHandoffs: Array.from(this.activeHandoffs.keys()),\n });\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n logger.error('Handoff progress not found', {\n requestId,\n availableHandoffs: Array.from(this.activeHandoffs.keys()),\n });\n throw new DatabaseError(\n `Handoff progress not found: ${requestId}`,\n ErrorCode.INVALID_STATE\n );\n }\n\n try {\n logger.debug('Setting progress status to in_transfer', { requestId });\n progress.status = 'in_transfer';\n progress.currentStep = 'Transferring frames';\n progress.estimatedCompletion = new Date(Date.now() + 5 * 60 * 1000); // 5 minutes\n\n // Execute the handoff through DualStackManager\n logger.debug('About to call acceptHandoff', { requestId });\n const result = await this.dualStackManager.acceptHandoff(requestId);\n logger.debug('acceptHandoff returned', {\n requestId,\n success: result.success,\n });\n\n if (result.success) {\n progress.status = 'completed';\n progress.currentStep = 'Transfer completed';\n progress.transferredFrames = result.mergedFrames.length;\n\n // Create completion notifications\n await this.notifyHandoffCompletion(requestId, result);\n\n logger.info(`Handoff transfer completed: ${requestId}`, {\n transferredFrames: progress.transferredFrames,\n conflicts: result.conflictFrames.length,\n });\n } else {\n progress.status = 'failed';\n progress.currentStep = 'Transfer failed';\n\n // Log errors\n result.errors.forEach((error) => {\n progress.errors.push({\n step: 'transfer',\n error: `Frame ${error.frameId}: ${error.error}`,\n timestamp: new Date(),\n });\n });\n\n throw new DatabaseError(\n 'Handoff transfer failed',\n ErrorCode.OPERATION_FAILED,\n { errors: result.errors }\n );\n }\n } catch (error: unknown) {\n progress.status = 'failed';\n progress.currentStep = 'Transfer error';\n progress.errors.push({\n step: 'transfer',\n error: error instanceof Error ? error.message : String(error),\n timestamp: new Date(),\n });\n\n logger.error(`Handoff transfer failed: ${requestId}`, error);\n throw error;\n } finally {\n this.activeHandoffs.set(requestId, progress);\n }\n }\n\n /**\n * Get handoff progress and status\n */\n async getHandoffProgress(requestId: string): Promise<HandoffProgress | null> {\n return this.activeHandoffs.get(requestId) || null;\n }\n\n /**\n * Cancel a pending handoff request\n */\n async cancelHandoff(requestId: string, reason: string): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n throw new DatabaseError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n }\n\n if (progress.status === 'in_transfer') {\n throw new DatabaseError(\n 'Cannot cancel handoff that is currently transferring',\n ErrorCode.INVALID_STATE\n );\n }\n\n progress.status = 'cancelled';\n progress.currentStep = 'Cancelled by user';\n progress.errors.push({\n step: 'cancellation',\n error: reason,\n timestamp: new Date(),\n });\n\n this.activeHandoffs.set(requestId, progress);\n\n // Notify relevant parties\n await this.notifyHandoffCancellation(requestId, reason);\n\n logger.info(`Handoff cancelled: ${requestId}`, { reason });\n }\n\n /**\n * Get all active handoffs for a user or team\n */\n async getActiveHandoffs(\n userId?: string,\n teamId?: string\n ): Promise<HandoffProgress[]> {\n const handoffs = Array.from(this.activeHandoffs.values());\n\n // Filter by user/team if specified\n if (userId || teamId) {\n // Would need to cross-reference with handoff metadata\n return handoffs.filter(\n (handoff) =>\n handoff.status === 'pending_review' ||\n handoff.status === 'approved' ||\n handoff.status === 'in_transfer'\n );\n }\n\n return handoffs;\n }\n\n /**\n * Get notifications for a user\n */\n async getUserNotifications(userId: string): Promise<HandoffNotification[]> {\n return this.notifications.get(userId) || [];\n }\n\n /**\n * Mark notification as read\n */\n async markNotificationRead(\n notificationId: string,\n userId: string\n ): Promise<void> {\n const userNotifications = this.notifications.get(userId) || [];\n const updatedNotifications = userNotifications.filter(\n (n) => n.id !== notificationId\n );\n this.notifications.set(userId, updatedNotifications);\n }\n\n /**\n * Validate frames are suitable for handoff\n */\n private async validateFramesForHandoff(frameIds: string[]): Promise<void> {\n const activeStack = this.dualStackManager.getActiveStack();\n\n for (const frameId of frameIds) {\n const frame = await activeStack.getFrame(frameId);\n if (!frame) {\n throw new DatabaseError(\n `Frame not found: ${frameId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n }\n\n // Check if frame is in a transferable state\n if (frame.state === 'active') {\n logger.warn(`Transferring active frame: ${frameId}`, {\n frameName: frame.name,\n });\n }\n }\n }\n\n /**\n * Create notifications for handoff stakeholders\n */\n private async createHandoffNotifications(\n requestId: string,\n metadata: HandoffMetadata,\n targetUserId?: string\n ): Promise<void> {\n const notifications: HandoffNotification[] = [];\n\n // Notify target user\n if (targetUserId) {\n notifications.push({\n id: `${requestId}-target`,\n type: 'request',\n requestId,\n recipientId: targetUserId,\n title: 'Frame Handoff Request',\n message: `${metadata.initiatorId} wants to transfer ${metadata.frameContext.totalFrames} frames to you`,\n actionRequired: true,\n expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000),\n createdAt: new Date(),\n });\n }\n\n // Notify stakeholders\n if (metadata.businessContext?.stakeholders) {\n for (const stakeholderId of metadata.businessContext.stakeholders) {\n notifications.push({\n id: `${requestId}-stakeholder-${stakeholderId}`,\n type: 'request',\n requestId,\n recipientId: stakeholderId,\n title: 'Frame Handoff Notification',\n message: `Frame transfer initiated for ${metadata.businessContext?.milestone || 'project milestone'}`,\n actionRequired: false,\n createdAt: new Date(),\n });\n }\n }\n\n // Store notifications\n for (const notification of notifications) {\n const userNotifications =\n this.notifications.get(notification.recipientId) || [];\n userNotifications.push(notification);\n this.notifications.set(notification.recipientId, userNotifications);\n }\n }\n\n /**\n * Schedule reminder notifications\n */\n private async scheduleHandoffReminders(\n requestId: string,\n metadata: HandoffMetadata\n ): Promise<void> {\n // Schedule reminder in 4 hours if high priority\n if (\n metadata.businessContext?.priority === 'high' ||\n metadata.businessContext?.priority === 'critical'\n ) {\n setTimeout(\n async () => {\n const progress = this.activeHandoffs.get(requestId);\n if (progress && progress.status === 'pending_review') {\n await this.sendHandoffReminder(requestId, metadata);\n }\n },\n 4 * 60 * 60 * 1000\n ); // 4 hours\n }\n }\n\n /**\n * Send handoff reminder\n */\n private async sendHandoffReminder(\n requestId: string,\n metadata: HandoffMetadata\n ): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress || progress.status !== 'pending_review') {\n return;\n }\n\n const reminderNotification: HandoffNotification = {\n id: `${requestId}-reminder-${Date.now()}`,\n type: 'reminder',\n requestId,\n recipientId: metadata.targetUserId || 'unknown',\n title: '\u23F0 Handoff Request Reminder',\n message: `Reminder: ${metadata.initiatorId} is waiting for approval on ${metadata.frameContext.totalFrames} frames. Priority: ${metadata.businessContext?.priority || 'medium'}`,\n actionRequired: true,\n expiresAt: new Date(Date.now() + 12 * 60 * 60 * 1000), // 12 hours\n createdAt: new Date(),\n };\n\n // Store the notification\n if (metadata.targetUserId) {\n const userNotifications =\n this.notifications.get(metadata.targetUserId) || [];\n userNotifications.push(reminderNotification);\n this.notifications.set(metadata.targetUserId, userNotifications);\n\n logger.info(`Sent handoff reminder: ${requestId}`, {\n priority: metadata.businessContext?.priority,\n recipient: metadata.targetUserId,\n });\n }\n\n // Also notify stakeholders\n if (metadata.businessContext?.stakeholders) {\n for (const stakeholderId of metadata.businessContext.stakeholders) {\n const stakeholderNotification: HandoffNotification = {\n ...reminderNotification,\n id: `${requestId}-reminder-stakeholder-${stakeholderId}-${Date.now()}`,\n recipientId: stakeholderId,\n title: '\uD83D\uDCCB Handoff Status Update',\n message: `Pending handoff approval: ${metadata.businessContext?.milestone || 'development work'} requires attention`,\n actionRequired: false,\n };\n\n const stakeholderNotifications =\n this.notifications.get(stakeholderId) || [];\n stakeholderNotifications.push(stakeholderNotification);\n this.notifications.set(stakeholderId, stakeholderNotifications);\n }\n }\n }\n\n /**\n * Notify when changes are requested\n */\n private async notifyChangesRequested(\n requestId: string,\n approval: Omit<HandoffApproval, 'requestId' | 'reviewedAt'>\n ): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) return;\n\n // Find the original requester (we'll need to enhance this with better metadata tracking)\n const changeRequestNotification: HandoffNotification = {\n id: `${requestId}-changes-${Date.now()}`,\n type: 'request',\n requestId,\n recipientId: 'requester', // TODO: Get actual requester from handoff metadata\n title: 'Changes Requested for Handoff',\n message: `${approval.reviewerId} has requested changes: ${approval.feedback || 'See detailed suggestions'}`,\n actionRequired: true,\n expiresAt: new Date(Date.now() + 48 * 60 * 60 * 1000), // 48 hours\n createdAt: new Date(),\n };\n\n // Store notification (for now using a placeholder recipient)\n const notifications = this.notifications.get('requester') || [];\n notifications.push(changeRequestNotification);\n this.notifications.set('requester', notifications);\n\n // Log detailed feedback and suggestions\n logger.info(`Changes requested for handoff: ${requestId}`, {\n reviewer: approval.reviewerId,\n feedback: approval.feedback,\n suggestedChangesCount: approval.suggestedChanges?.length || 0,\n });\n\n if (approval.suggestedChanges && approval.suggestedChanges.length > 0) {\n logger.info(`Detailed change suggestions:`, {\n requestId,\n suggestions: approval.suggestedChanges.map((change) => ({\n frameId: change.frameId,\n suggestion: change.suggestion,\n reason: change.reason,\n })),\n });\n }\n }\n\n /**\n * Notify handoff completion\n */\n private async notifyHandoffCompletion(\n requestId: string,\n result: any\n ): Promise<void> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) return;\n\n // Create completion notification\n const completionNotification: HandoffNotification = {\n id: `${requestId}-completion-${Date.now()}`,\n type: 'completion',\n requestId,\n recipientId: 'all', // Will be distributed to all stakeholders\n title: 'Handoff Completed Successfully',\n message: `Frame transfer completed: ${result.mergedFrames.length} frames transferred${result.conflictFrames.length > 0 ? `, ${result.conflictFrames.length} conflicts resolved` : ''}`,\n actionRequired: false,\n createdAt: new Date(),\n };\n\n // Notify all stakeholders from the notifications map\n const allUsers = Array.from(this.notifications.keys());\n for (const userId of allUsers) {\n const userSpecificNotification: HandoffNotification = {\n ...completionNotification,\n id: `${requestId}-completion-${userId}-${Date.now()}`,\n recipientId: userId,\n };\n\n const userNotifications = this.notifications.get(userId) || [];\n userNotifications.push(userSpecificNotification);\n this.notifications.set(userId, userNotifications);\n }\n\n logger.info(`Handoff completed: ${requestId}`, {\n mergedFrames: result.mergedFrames.length,\n conflicts: result.conflictFrames.length,\n notifiedUsers: allUsers.length,\n });\n\n // Log detailed completion statistics\n if (result.conflictFrames.length > 0) {\n logger.info(`Handoff completion details:`, {\n requestId,\n transferredFrames: result.mergedFrames.map(\n (f: any) => f.frameId || f.id\n ),\n conflictFrames: result.conflictFrames.map(\n (f: any) => f.frameId || f.id\n ),\n });\n }\n }\n\n /**\n * Notify handoff cancellation\n */\n private async notifyHandoffCancellation(\n requestId: string,\n reason: string\n ): Promise<void> {\n // Create cancellation notification\n const cancellationNotification: HandoffNotification = {\n id: `${requestId}-cancellation-${Date.now()}`,\n type: 'request', // Using 'request' type as it's informational\n requestId,\n recipientId: 'all', // Will be distributed to all stakeholders\n title: 'Handoff Cancelled',\n message: `Handoff request has been cancelled. Reason: ${reason}`,\n actionRequired: false,\n createdAt: new Date(),\n };\n\n // Notify all users who have been involved in this handoff\n const allUsers = Array.from(this.notifications.keys());\n for (const userId of allUsers) {\n const userSpecificNotification: HandoffNotification = {\n ...cancellationNotification,\n id: `${requestId}-cancellation-${userId}-${Date.now()}`,\n recipientId: userId,\n };\n\n const userNotifications = this.notifications.get(userId) || [];\n userNotifications.push(userSpecificNotification);\n this.notifications.set(userId, userNotifications);\n }\n\n logger.info(`Handoff cancelled: ${requestId}`, {\n reason,\n notifiedUsers: allUsers.length,\n });\n }\n\n /**\n * Get handoff analytics and metrics\n */\n async getHandoffMetrics(timeRange?: { start: Date; end: Date }): Promise<{\n totalHandoffs: number;\n completedHandoffs: number;\n averageProcessingTime: number;\n topFrameTypes: Array<{ type: string; count: number }>;\n collaborationPatterns: Array<{\n sourceUser: string;\n targetUser: string;\n count: number;\n }>;\n }> {\n const handoffs = Array.from(this.activeHandoffs.values());\n\n // Filter by time range if specified\n const filteredHandoffs = timeRange\n ? handoffs.filter((h) => {\n // Would need to add timestamps to track creation time\n return true; // Placeholder\n })\n : handoffs;\n\n const completedHandoffs = filteredHandoffs.filter(\n (h) => h.status === 'completed'\n );\n\n return {\n totalHandoffs: filteredHandoffs.length,\n completedHandoffs: completedHandoffs.length,\n averageProcessingTime:\n this.calculateAverageProcessingTime(completedHandoffs),\n topFrameTypes: this.analyzeFrameTypes(filteredHandoffs),\n collaborationPatterns:\n this.analyzeCollaborationPatterns(filteredHandoffs),\n };\n }\n\n private calculateAverageProcessingTime(handoffs: HandoffProgress[]): number {\n if (handoffs.length === 0) return 0;\n\n let totalProcessingTime = 0;\n let validHandoffs = 0;\n\n for (const handoff of handoffs) {\n // Only calculate for completed handoffs that have timing data\n if (handoff.status === 'completed' && handoff.estimatedCompletion) {\n // Estimate processing time based on frame count and complexity\n // This is a simplified calculation - in practice you'd track actual timestamps\n const frameComplexity = handoff.totalFrames * 0.5; // Base time per frame\n const errorPenalty = handoff.errors.length * 2; // Extra time for errors\n const processingTime = Math.max(1, frameComplexity + errorPenalty);\n\n totalProcessingTime += processingTime;\n validHandoffs++;\n }\n }\n\n return validHandoffs > 0\n ? Math.round(totalProcessingTime / validHandoffs)\n : 0;\n }\n\n private analyzeFrameTypes(\n handoffs: HandoffProgress[]\n ): Array<{ type: string; count: number }> {\n const frameTypeCount = new Map<string, number>();\n\n for (const handoff of handoffs) {\n // Extract frame type information from handoff metadata\n // This would need to be enhanced with actual frame type tracking\n const estimatedTypes = this.estimateFrameTypes(handoff);\n\n for (const type of estimatedTypes) {\n frameTypeCount.set(type, (frameTypeCount.get(type) || 0) + 1);\n }\n }\n\n return Array.from(frameTypeCount.entries())\n .map(([type, count]) => ({ type, count }))\n .sort((a, b) => b.count - a.count)\n .slice(0, 10); // Top 10 frame types\n }\n\n private estimateFrameTypes(handoff: HandoffProgress): string[] {\n // Simplified frame type estimation based on handoff characteristics\n const types: string[] = [];\n\n if (handoff.totalFrames > 10) {\n types.push('bulk_transfer');\n }\n if (handoff.errors.length > 0) {\n types.push('complex_handoff');\n }\n if (handoff.transferredFrames === handoff.totalFrames) {\n types.push('complete_transfer');\n } else {\n types.push('partial_transfer');\n }\n\n // Add some common frame types based on patterns\n types.push('development', 'collaboration');\n\n return types;\n }\n\n private analyzeCollaborationPatterns(\n handoffs: HandoffProgress[]\n ): Array<{ sourceUser: string; targetUser: string; count: number }> {\n const collaborationCount = new Map<string, number>();\n\n for (const handoff of handoffs) {\n // Extract collaboration pattern from handoff data\n // Note: This is simplified - we'd need to track actual source/target users\n const pattern = this.extractCollaborationPattern(handoff);\n if (pattern) {\n const key = `${pattern.sourceUser}->${pattern.targetUser}`;\n collaborationCount.set(key, (collaborationCount.get(key) || 0) + 1);\n }\n }\n\n return Array.from(collaborationCount.entries())\n .map(([pattern, count]) => {\n const [sourceUser, targetUser] = pattern.split('->');\n return { sourceUser, targetUser, count };\n })\n .sort((a, b) => b.count - a.count)\n .slice(0, 20); // Top 20 collaboration patterns\n }\n\n private extractCollaborationPattern(\n handoff: HandoffProgress\n ): { sourceUser: string; targetUser: string } | null {\n // Simplified pattern extraction - in practice this would come from handoff metadata\n // For now, we'll create sample patterns based on handoff characteristics\n\n if (handoff.status === 'completed') {\n return {\n sourceUser: 'developer',\n targetUser: 'reviewer',\n };\n } else if (handoff.status === 'failed') {\n return {\n sourceUser: 'developer',\n targetUser: 'lead',\n };\n }\n\n return null;\n }\n\n /**\n * Real-time collaboration features\n */\n\n /**\n * Get real-time handoff status updates\n */\n async getHandoffStatusStream(\n requestId: string\n ): Promise<AsyncIterableIterator<HandoffProgress>> {\n const progress = this.activeHandoffs.get(requestId);\n if (!progress) {\n throw new DatabaseError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n }\n\n // Simple implementation - in a real system this would use WebSockets or Server-Sent Events\n const self = this;\n return {\n async *[Symbol.asyncIterator]() {\n let lastStatus = progress.status;\n while (\n lastStatus !== 'completed' &&\n lastStatus !== 'failed' &&\n lastStatus !== 'cancelled'\n ) {\n const currentProgress = self.activeHandoffs.get(requestId);\n if (currentProgress && currentProgress.status !== lastStatus) {\n lastStatus = currentProgress.status;\n yield currentProgress;\n }\n // Simulate real-time polling\n await new Promise((resolve) => setTimeout(resolve, 1000));\n }\n },\n };\n }\n\n /**\n * Update handoff progress in real-time\n */\n async updateHandoffProgress(\n requestId: string,\n update: Partial<HandoffProgress>\n ): Promise<void> {\n let progress = this.activeHandoffs.get(requestId);\n\n // If progress doesn't exist and update includes required fields, create it\n if (\n !progress &&\n update.requestId &&\n update.status &&\n update.totalFrames !== undefined\n ) {\n progress = {\n requestId: update.requestId,\n status: update.status,\n transferredFrames: 0,\n totalFrames: update.totalFrames,\n currentStep: 'Initialized',\n errors: [],\n ...update,\n };\n } else if (!progress) {\n throw new DatabaseError(\n `Handoff request not found: ${requestId}`,\n ErrorCode.RESOURCE_NOT_FOUND\n );\n } else {\n // Update existing progress with provided fields\n progress = {\n ...progress,\n ...update,\n };\n }\n\n this.activeHandoffs.set(requestId, progress);\n\n logger.info(`Handoff progress updated: ${requestId}`, {\n status: progress.status,\n currentStep: progress.currentStep,\n transferredFrames: progress.transferredFrames,\n });\n\n // Notify stakeholders of progress update\n await this.notifyProgressUpdate(requestId, progress);\n }\n\n /**\n * Notify stakeholders of progress updates\n */\n private async notifyProgressUpdate(\n requestId: string,\n progress: HandoffProgress\n ): Promise<void> {\n const updateNotification: HandoffNotification = {\n id: `${requestId}-progress-${Date.now()}`,\n type: 'request',\n requestId,\n recipientId: 'all',\n title: 'Handoff Progress Update',\n message: `Status: ${progress.status} | Step: ${progress.currentStep} | Progress: ${progress.transferredFrames}/${progress.totalFrames} frames`,\n actionRequired: false,\n createdAt: new Date(),\n };\n\n // Distribute to all stakeholders\n const allUsers = Array.from(this.notifications.keys());\n for (const userId of allUsers) {\n const userNotifications = this.notifications.get(userId) || [];\n userNotifications.push({\n ...updateNotification,\n id: `${requestId}-progress-${userId}-${Date.now()}`,\n recipientId: userId,\n });\n this.notifications.set(userId, userNotifications);\n }\n }\n\n /**\n * Get active handoffs with real-time filtering\n */\n async getActiveHandoffsRealTime(filters?: {\n status?: HandoffProgress['status'];\n userId?: string;\n priority?: 'low' | 'medium' | 'high' | 'critical';\n }): Promise<HandoffProgress[]> {\n let handoffs = Array.from(this.activeHandoffs.values());\n\n if (filters?.status) {\n handoffs = handoffs.filter((h) => h.status === filters.status);\n }\n\n if (filters?.userId) {\n // In a real implementation, we'd have proper user tracking in handoff metadata\n // For now, filter based on requestId pattern or other heuristics\n handoffs = handoffs.filter((h) =>\n h.requestId.includes(filters.userId || '')\n );\n }\n\n if (filters?.priority) {\n // Filter by priority (this would need priority tracking in HandoffProgress)\n // For now, estimate priority based on frame count and errors\n handoffs = handoffs.filter((h) => {\n const estimatedPriority = this.estimateHandoffPriority(h);\n return estimatedPriority === filters.priority;\n });\n }\n\n return handoffs.sort((a, b) => {\n // Sort by status priority, then by creation time\n const statusPriority = {\n in_transfer: 4,\n approved: 3,\n pending_review: 2,\n completed: 1,\n failed: 1,\n cancelled: 0,\n };\n return (statusPriority[b.status] || 0) - (statusPriority[a.status] || 0);\n });\n }\n\n private estimateHandoffPriority(\n handoff: HandoffProgress\n ): 'low' | 'medium' | 'high' | 'critical' {\n if (handoff.errors.length > 2 || handoff.totalFrames > 50)\n return 'critical';\n if (handoff.errors.length > 0 || handoff.totalFrames > 20) return 'high';\n if (handoff.totalFrames > 5) return 'medium';\n return 'low';\n }\n\n /**\n * Bulk handoff operations for team collaboration\n */\n async bulkHandoffOperation(operation: {\n action: 'approve' | 'reject' | 'cancel';\n requestIds: string[];\n reviewerId: string;\n feedback?: string;\n }): Promise<{\n successful: string[];\n failed: Array<{ requestId: string; error: string }>;\n }> {\n const results = {\n successful: [],\n failed: [] as Array<{ requestId: string; error: string }>,\n };\n\n for (const requestId of operation.requestIds) {\n try {\n switch (operation.action) {\n case 'approve':\n await this.submitHandoffApproval(requestId, {\n reviewerId: operation.reviewerId,\n decision: 'approved',\n feedback: operation.feedback,\n });\n results.successful.push(requestId);\n break;\n\n case 'reject':\n await this.submitHandoffApproval(requestId, {\n reviewerId: operation.reviewerId,\n decision: 'rejected',\n feedback: operation.feedback || 'Bulk rejection',\n });\n results.successful.push(requestId);\n break;\n\n case 'cancel':\n await this.cancelHandoff(\n requestId,\n operation.feedback || 'Bulk cancellation'\n );\n results.successful.push(requestId);\n break;\n }\n } catch (error: unknown) {\n results.failed.push({\n requestId,\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n\n logger.info(`Bulk handoff operation completed`, {\n action: operation.action,\n successful: results.successful.length,\n failed: results.failed.length,\n reviewerId: operation.reviewerId,\n });\n\n return results;\n }\n\n /**\n * Enhanced notification management with cleanup\n */\n async cleanupExpiredNotifications(userId?: string): Promise<number> {\n let cleanedCount = 0;\n const now = new Date();\n\n const userIds = userId ? [userId] : Array.from(this.notifications.keys());\n\n for (const uid of userIds) {\n const userNotifications = this.notifications.get(uid) || [];\n const activeNotifications = userNotifications.filter((notification) => {\n if (notification.expiresAt && notification.expiresAt < now) {\n cleanedCount++;\n return false;\n }\n return true;\n });\n\n this.notifications.set(uid, activeNotifications);\n }\n\n if (cleanedCount > 0) {\n logger.info(`Cleaned up expired notifications`, {\n count: cleanedCount,\n userId: userId || 'all',\n });\n }\n\n return cleanedCount;\n }\n}\n"],
5
5
  "mappings": "AAWA,SAAS,cAAc;AACvB,SAAS,iBAAiB,eAAe,iBAAiB;AAC1D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AAkEA,MAAM,oBAAoB;AAAA,EACvB;AAAA,EACA,iBAA+C,oBAAI,IAAI;AAAA,EACvD,mBAAmD,oBAAI,IAAI;AAAA,EAC3D,gBAAoD,oBAAI,IAAI;AAAA,EAEpE,YAAY,kBAAoC;AAC9C,SAAK,mBAAmB;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,gBACJ,eACA,UACA,UACA,cACA,SACiB;AAEjB,UAAM,QAAQ,cAAc,uBAAuB;AAAA,MACjD;AAAA,MACA;AAAA,MACA,gBAAgB;AAAA,MAChB,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ACA,UAAU,YAAY;AAAA,YACxB;AACA,oBAAQ,WAAW,KAAK,SAAS;AACjC;AAAA,QACJ;AAAA,MACF,SAAS,OAAgB;AACvB,gBAAQ,OAAO,KAAK;AAAA,UAClB;AAAA,UACA,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,QAC9D,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO,KAAK,oCAAoC;AAAA,MAC9C,QAAQ,UAAU;AAAA,MAClB,YAAY,QAAQ,WAAW;AAAA,MAC/B,QAAQ,QAAQ,OAAO;AAAA,MACvB,YAAY,UAAU;AAAA,IACxB,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,4BAA4B,QAAkC;AAClE,QAAI,eAAe;AACnB,UAAM,MAAM,oBAAI,KAAK;AAErB,UAAM,UAAU,SAAS,CAAC,MAAM,IAAI,MAAM,KAAK,KAAK,cAAc,KAAK,CAAC;AAExE,eAAW,OAAO,SAAS;AACzB,YAAM,oBAAoB,KAAK,cAAc,IAAI,GAAG,KAAK,CAAC;AAC1D,YAAM,sBAAsB,kBAAkB,OAAO,CAAC,iBAAiB;AACrE,YAAI,aAAa,aAAa,aAAa,YAAY,KAAK;AAC1D;AACA,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,MACT,CAAC;AAED,WAAK,cAAc,IAAI,KAAK,mBAAmB;AAAA,IACjD;AAEA,QAAI,eAAe,GAAG;AACpB,aAAO,KAAK,oCAAoC;AAAA,QAC9C,OAAO;AAAA,QACP,QAAQ,UAAU;AAAA,MACpB,CAAC;AAAA,IACH;AAEA,WAAO;AAAA,EACT;AACF;",
6
6
  "names": []
7
7
  }
@@ -8,6 +8,8 @@ import {
8
8
  } from "../errors/index.js";
9
9
  import { sessionManager, FrameQueryMode } from "../session/index.js";
10
10
  import { contextBridge } from "./context-bridge.js";
11
+ const MAX_FRAME_DEPTH = 100;
12
+ const DEFAULT_MAX_DEPTH = 100;
11
13
  function getEnv(key, defaultValue) {
12
14
  const value = process.env[key];
13
15
  if (value === void 0) {
@@ -27,6 +29,7 @@ class FrameManager {
27
29
  activeStack = [];
28
30
  // Stack of active frame IDs
29
31
  queryMode = FrameQueryMode.PROJECT_ACTIVE;
32
+ maxFrameDepth = DEFAULT_MAX_DEPTH;
30
33
  constructor(db, projectId, runIdOrOptions) {
31
34
  this.db = db;
32
35
  this.projectId = projectId;
@@ -37,6 +40,7 @@ class FrameManager {
37
40
  } else if (runIdOrOptions) {
38
41
  runId = runIdOrOptions.runId;
39
42
  skipContextBridge = runIdOrOptions.skipContextBridge || false;
43
+ this.maxFrameDepth = runIdOrOptions.maxFrameDepth || DEFAULT_MAX_DEPTH;
40
44
  }
41
45
  const session = sessionManager.getCurrentSession();
42
46
  if (session) {
@@ -258,6 +262,34 @@ class FrameManager {
258
262
  const frameId = uuidv4();
259
263
  const parentFrameId = options.parentFrameId || this.getCurrentFrameId();
260
264
  const depth = parentFrameId ? this.getFrameDepth(parentFrameId) + 1 : 0;
265
+ if (depth > this.maxFrameDepth) {
266
+ throw new FrameError(
267
+ `Maximum frame depth exceeded: ${depth} > ${this.maxFrameDepth}`,
268
+ ErrorCode.FRAME_STACK_OVERFLOW,
269
+ {
270
+ currentDepth: depth,
271
+ maxDepth: this.maxFrameDepth,
272
+ frameId,
273
+ parentFrameId,
274
+ frameName: options.name
275
+ }
276
+ );
277
+ }
278
+ if (parentFrameId) {
279
+ const cycle = this.detectCycle(frameId, parentFrameId);
280
+ if (cycle) {
281
+ throw new FrameError(
282
+ `Circular reference detected in frame hierarchy`,
283
+ ErrorCode.FRAME_CYCLE_DETECTED,
284
+ {
285
+ frameId,
286
+ parentFrameId,
287
+ cycle,
288
+ frameName: options.name
289
+ }
290
+ );
291
+ }
292
+ }
261
293
  const frame = {
262
294
  frame_id: frameId,
263
295
  run_id: this.currentRunId,
@@ -815,6 +847,113 @@ Activity: ${eventCount} events, ${structured.tool_calls_count} tool calls`;
815
847
  const artifacts = this.getFrameEvents(frameId).filter((e) => e.event_type === "artifact").map((e) => e.payload.ref).filter(Boolean);
816
848
  return artifacts;
817
849
  }
850
+ /**
851
+ * Detect if setting a parent frame would create a cycle in the frame hierarchy.
852
+ * Returns the cycle path if detected, or null if no cycle.
853
+ * @param childFrameId - The frame that would be the child
854
+ * @param parentFrameId - The proposed parent frame
855
+ * @returns Array of frame IDs forming the cycle, or null if no cycle
856
+ */
857
+ detectCycle(childFrameId, parentFrameId) {
858
+ const visited = /* @__PURE__ */ new Set();
859
+ const path = [];
860
+ let currentId = parentFrameId;
861
+ while (currentId) {
862
+ if (visited.has(currentId)) {
863
+ const cycleStart = path.indexOf(currentId);
864
+ return path.slice(cycleStart).concat(currentId);
865
+ }
866
+ if (currentId === childFrameId) {
867
+ return path.concat([currentId, childFrameId]);
868
+ }
869
+ visited.add(currentId);
870
+ path.push(currentId);
871
+ const frame = this.getFrame(currentId);
872
+ if (!frame) {
873
+ break;
874
+ }
875
+ currentId = frame.parent_frame_id;
876
+ if (path.length > this.maxFrameDepth) {
877
+ throw new FrameError(
878
+ `Frame hierarchy traversal exceeded maximum depth during cycle detection`,
879
+ ErrorCode.FRAME_STACK_OVERFLOW,
880
+ {
881
+ depth: path.length,
882
+ maxDepth: this.maxFrameDepth,
883
+ path
884
+ }
885
+ );
886
+ }
887
+ }
888
+ return null;
889
+ }
890
+ /**
891
+ * Update parent frame of an existing frame (with cycle detection)
892
+ * @param frameId - The frame to update
893
+ * @param newParentFrameId - The new parent frame ID
894
+ */
895
+ updateParentFrame(frameId, newParentFrameId) {
896
+ const frame = this.getFrame(frameId);
897
+ if (!frame) {
898
+ throw new FrameError(
899
+ `Frame not found: ${frameId}`,
900
+ ErrorCode.FRAME_NOT_FOUND,
901
+ { frameId }
902
+ );
903
+ }
904
+ if (newParentFrameId) {
905
+ const cycle = this.detectCycle(frameId, newParentFrameId);
906
+ if (cycle) {
907
+ throw new FrameError(
908
+ `Cannot set parent: would create circular reference`,
909
+ ErrorCode.FRAME_CYCLE_DETECTED,
910
+ {
911
+ frameId,
912
+ newParentFrameId,
913
+ cycle,
914
+ currentParentId: frame.parent_frame_id
915
+ }
916
+ );
917
+ }
918
+ const newParentFrame = this.getFrame(newParentFrameId);
919
+ if (newParentFrame) {
920
+ const newDepth = newParentFrame.depth + 1;
921
+ if (newDepth > this.maxFrameDepth) {
922
+ throw new FrameError(
923
+ `Cannot set parent: would exceed maximum frame depth`,
924
+ ErrorCode.FRAME_STACK_OVERFLOW,
925
+ {
926
+ frameId,
927
+ newParentFrameId,
928
+ newDepth,
929
+ maxDepth: this.maxFrameDepth
930
+ }
931
+ );
932
+ }
933
+ }
934
+ }
935
+ try {
936
+ this.db.prepare(
937
+ `UPDATE frames SET parent_frame_id = ? WHERE frame_id = ?`
938
+ ).run(newParentFrameId, frameId);
939
+ logger.info("Updated parent frame", {
940
+ frameId,
941
+ oldParentId: frame.parent_frame_id,
942
+ newParentId: newParentFrameId
943
+ });
944
+ } catch (error) {
945
+ throw new DatabaseError(
946
+ `Failed to update parent frame`,
947
+ ErrorCode.DB_UPDATE_FAILED,
948
+ {
949
+ frameId,
950
+ newParentFrameId,
951
+ operation: "updateParentFrame"
952
+ },
953
+ error instanceof Error ? error : void 0
954
+ );
955
+ }
956
+ }
818
957
  }
819
958
  export {
820
959
  FrameManager