bmad-plus 0.12.0 → 0.12.2

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 (160) hide show
  1. package/CHANGELOG.md +612 -554
  2. package/README.md +557 -577
  3. package/osint-agent-package/agents/osint-investigator.md +12 -0
  4. package/osint-agent-package/skills/bmad-osint-investigate/osint/SKILL.md +491 -482
  5. package/osint-agent-package/skills/bmad-osint-investigate/osint/assets/dossier-template.md +126 -126
  6. package/osint-agent-package/skills/bmad-osint-investigate/osint/assets/lawful-basis-record.md +48 -48
  7. package/osint-agent-package/skills/bmad-osint-investigate/osint/references/content-extraction.md +100 -100
  8. package/osint-agent-package/skills/bmad-osint-investigate/osint/references/gdpr-osint.md +48 -48
  9. package/osint-agent-package/skills/bmad-osint-investigate/osint/references/platforms.md +130 -130
  10. package/osint-agent-package/skills/bmad-osint-investigate/osint/references/psychoprofile.md +69 -69
  11. package/osint-agent-package/skills/bmad-osint-investigate/osint/references/tools.md +281 -281
  12. package/osint-agent-package/skills/bmad-osint-investigate/osint/scripts/mcp-client.py +136 -136
  13. package/package.json +104 -91
  14. package/readme-international/README.de.md +596 -589
  15. package/readme-international/README.es.md +613 -606
  16. package/readme-international/README.fr.md +611 -604
  17. package/src/bmad-plus/agents/agent-shadow/SKILL.md +18 -0
  18. package/src/bmad-plus/data/role-triggers.yaml +52 -0
  19. package/src/bmad-plus/module.yaml +283 -283
  20. package/src/bmad-plus/packs/pack-animated/animated-website-agent.md +325 -325
  21. package/src/bmad-plus/packs/pack-animated/templates/animated-website-workflow.md +55 -55
  22. package/src/bmad-plus/packs/pack-backup/backup-agent.md +71 -71
  23. package/src/bmad-plus/packs/pack-backup/templates/backup-workflow.md +51 -51
  24. package/src/bmad-plus/packs/pack-dev-studio/README.md +162 -162
  25. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/analyst-agent.md +73 -73
  26. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/document-project.md +61 -61
  27. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/domain-research.md +95 -95
  28. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/market-research.md +95 -95
  29. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/prfaq.md +134 -134
  30. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/product-brief.md +80 -80
  31. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/tech-writer-agent.md +73 -73
  32. package/src/bmad-plus/packs/pack-dev-studio/categories/analysis/technical-research.md +95 -95
  33. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/architect-agent.md +73 -73
  34. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/create-architecture.md +73 -73
  35. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/create-epics-stories.md +92 -92
  36. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/generate-project-context.md +80 -80
  37. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/implementation-readiness.md +90 -90
  38. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-01-init.md +153 -153
  39. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-01b-continue.md +173 -173
  40. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-02-context.md +224 -224
  41. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-03-starter.md +329 -329
  42. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-04-decisions.md +318 -318
  43. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-05-patterns.md +359 -359
  44. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-06-structure.md +379 -379
  45. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-07-validation.md +361 -361
  46. package/src/bmad-plus/packs/pack-dev-studio/categories/architecture/steps/step-08-complete.md +81 -81
  47. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/checkpoint-preview.md +67 -67
  48. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/code-review-steps/step-01-gather-context.md +85 -85
  49. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/code-review-steps/step-02-review.md +35 -35
  50. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/code-review-steps/step-03-triage.md +49 -49
  51. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/code-review-steps/step-04-present.md +131 -131
  52. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/code-review.md +89 -89
  53. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/correct-course.md +300 -300
  54. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/create-story.md +428 -428
  55. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/dev-agent.md +73 -73
  56. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/dev-story-checklist.md +80 -80
  57. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/dev-story.md +484 -484
  58. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/investigate.md +193 -193
  59. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/qa-e2e-tests.md +175 -175
  60. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/quick-dev.md +110 -110
  61. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/retrospective.md +1511 -1511
  62. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/sprint-planning.md +298 -298
  63. package/src/bmad-plus/packs/pack-dev-studio/categories/implementation/sprint-status.md +296 -296
  64. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/create-prd.md +29 -29
  65. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/create-ux-design.md +74 -74
  66. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/edit-prd.md +29 -29
  67. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/pm-agent.md +73 -73
  68. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/prd.md +89 -89
  69. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/ux-designer-agent.md +73 -73
  70. package/src/bmad-plus/packs/pack-dev-studio/categories/planning/validate-prd.md +29 -29
  71. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/advanced-elicitation.md +141 -141
  72. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/adversarial-review.md +37 -37
  73. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/bmad-help.md +75 -75
  74. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/brainstorming.md +6 -6
  75. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/customize.md +110 -110
  76. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/distillator.md +176 -176
  77. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/edge-case-hunter.md +67 -67
  78. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/editorial-review-prose.md +86 -86
  79. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/editorial-review-structure.md +179 -179
  80. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/index-docs.md +66 -66
  81. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/party-mode.md +127 -127
  82. package/src/bmad-plus/packs/pack-dev-studio/categories/utilities/shard-doc.md +105 -105
  83. package/src/bmad-plus/packs/pack-dev-studio/dev-studio-orchestrator.md +120 -120
  84. package/src/bmad-plus/packs/pack-dev-studio/shared/architecture-decision-template.md +12 -12
  85. package/src/bmad-plus/packs/pack-dev-studio/shared/bwml-spec.md +328 -328
  86. package/src/bmad-plus/packs/pack-dev-studio/shared/module-help.csv +32 -32
  87. package/src/bmad-plus/packs/pack-dev-studio/upstream-sync.yaml +81 -81
  88. package/src/bmad-plus/packs/pack-memory/shared/memory-protocol.md +14 -0
  89. package/src/bmad-plus/packs/pack-seo/SKILL.md +171 -171
  90. package/src/bmad-plus/packs/pack-seo/checklist.md +140 -140
  91. package/src/bmad-plus/packs/pack-seo/pagespeed-playbook.md +320 -320
  92. package/src/bmad-plus/packs/pack-seo/ref/audit-schema.json +187 -187
  93. package/src/bmad-plus/packs/pack-seo/ref/cwv-thresholds.md +87 -87
  94. package/src/bmad-plus/packs/pack-seo/ref/eeat-criteria.md +123 -123
  95. package/src/bmad-plus/packs/pack-seo/ref/geo-signals.md +167 -167
  96. package/src/bmad-plus/packs/pack-seo/ref/hreflang-rules.md +153 -153
  97. package/src/bmad-plus/packs/pack-seo/ref/quality-gates.md +133 -133
  98. package/src/bmad-plus/packs/pack-seo/ref/schema-catalog.md +91 -91
  99. package/src/bmad-plus/packs/pack-seo/ref/schema-templates.json +356 -356
  100. package/src/bmad-plus/packs/pack-seo/requirements.txt +17 -0
  101. package/src/bmad-plus/packs/pack-seo/scripts/seo_apis.py +456 -0
  102. package/src/bmad-plus/packs/pack-seo/scripts/seo_crawl.py +359 -0
  103. package/src/bmad-plus/packs/pack-seo/scripts/seo_fetch.py +304 -0
  104. package/src/bmad-plus/packs/pack-seo/scripts/seo_parse.py +255 -0
  105. package/src/bmad-plus/packs/pack-seo/scripts/seo_report.py +410 -0
  106. package/src/bmad-plus/packs/pack-seo/scripts/seo_screenshot.py +202 -0
  107. package/src/bmad-plus/packs/pack-seo/seo-chief.md +294 -294
  108. package/src/bmad-plus/packs/pack-seo/seo-judge.md +241 -241
  109. package/src/bmad-plus/packs/pack-seo/seo-scout.md +171 -171
  110. package/src/bmad-plus/packs/pack-seo/templates/seo-audit-workflow.md +241 -241
  111. package/src/bmad-plus/packs/pack-shield/README.md +6 -6
  112. package/src/bmad-plus/packs/pack-shield/SKILL.md +2 -2
  113. package/src/bmad-plus/packs/pack-shield/categories/accessibility-esg/csrd-agent.md +11 -11
  114. package/src/bmad-plus/packs/pack-shield/categories/accessibility-esg/section508-agent.md +11 -11
  115. package/src/bmad-plus/packs/pack-shield/categories/accessibility-esg/wcag-agent.md +11 -11
  116. package/src/bmad-plus/packs/pack-shield/categories/ai-governance/eu-ai-act-agent.md +11 -11
  117. package/src/bmad-plus/packs/pack-shield/categories/ai-governance/iso42001-agent.md +11 -11
  118. package/src/bmad-plus/packs/pack-shield/categories/ai-governance/nist-ai-rmf-agent.md +11 -11
  119. package/src/bmad-plus/packs/pack-shield/categories/cybersecurity/cis-controls-agent.md +11 -11
  120. package/src/bmad-plus/packs/pack-shield/categories/cybersecurity/ism-agent.md +11 -11
  121. package/src/bmad-plus/packs/pack-shield/categories/cybersecurity/iso27001-agent.md +11 -11
  122. package/src/bmad-plus/packs/pack-shield/categories/cybersecurity/nis2-agent.md +11 -11
  123. package/src/bmad-plus/packs/pack-shield/categories/cybersecurity/nist-800-53-agent.md +11 -11
  124. package/src/bmad-plus/packs/pack-shield/categories/cybersecurity/nist-csf-agent.md +11 -11
  125. package/src/bmad-plus/packs/pack-shield/categories/defense-export/cmmc-agent.md +11 -11
  126. package/src/bmad-plus/packs/pack-shield/categories/defense-export/ear-agent.md +11 -11
  127. package/src/bmad-plus/packs/pack-shield/categories/defense-export/itar-agent.md +11 -11
  128. package/src/bmad-plus/packs/pack-shield/categories/defense-export/tsa-agent.md +11 -11
  129. package/src/bmad-plus/packs/pack-shield/categories/industry-compliance/dora-agent.md +11 -11
  130. package/src/bmad-plus/packs/pack-shield/categories/industry-compliance/fedramp-agent.md +11 -11
  131. package/src/bmad-plus/packs/pack-shield/categories/industry-compliance/hipaa-agent.md +11 -11
  132. package/src/bmad-plus/packs/pack-shield/categories/industry-compliance/pci-dss-agent.md +11 -11
  133. package/src/bmad-plus/packs/pack-shield/categories/industry-compliance/soc2-agent.md +11 -11
  134. package/src/bmad-plus/packs/pack-shield/categories/industry-compliance/swift-csp-agent.md +11 -11
  135. package/src/bmad-plus/packs/pack-shield/shield-orchestrator.md +1 -1
  136. package/tools/build/check-counts.js +628 -0
  137. package/tools/build/generated-adapters/.codex/AGENTS.md +1 -1
  138. package/tools/build/generated-adapters/.cursor/rules/bmad-plus.mdc +1 -1
  139. package/tools/build/generated-adapters/.opencode/AGENTS.md +1 -1
  140. package/tools/build/generated-adapters/AGENTS.md +1 -1
  141. package/tools/build/generated-adapters/CLAUDE.md +1 -1
  142. package/tools/build/generated-adapters/CONVENTIONS.md +1 -1
  143. package/tools/build/generated-adapters/GEMINI.md +1 -1
  144. package/tools/cli/commands/autoconfig.js +470 -472
  145. package/tools/cli/commands/doctor.js +233 -233
  146. package/tools/cli/commands/install.js +598 -495
  147. package/tools/cli/commands/memory-journal-cmd.js +311 -311
  148. package/tools/cli/commands/memory.js +195 -195
  149. package/tools/cli/commands/scan.js +348 -364
  150. package/tools/cli/commands/uninstall.js +101 -101
  151. package/tools/cli/commands/update.js +134 -134
  152. package/tools/cli/i18n.js +845 -845
  153. package/tools/cli/lib/README-memory-journal.md +125 -125
  154. package/tools/cli/lib/ide-config.js +267 -259
  155. package/tools/cli/lib/memory-init.js +204 -113
  156. package/tools/cli/lib/packs.js +209 -209
  157. package/tools/cli/lib/path-hash.js +39 -0
  158. package/tools/cli/lib/python-provision.js +508 -508
  159. package/tools/cli/lib/stack-detect.js +102 -102
  160. package/tools/cli/lib/validate.js +50 -50
@@ -1,113 +1,204 @@
1
- /**
2
- * BMAD+ Memory Initialization Module
3
- * Extracted from install.js — initializes brain with existing brain detection.
4
- *
5
- * Author: Laurent Rochetta
6
- */
7
-
8
- const path = require('node:path');
9
- const fs = require('node:fs');
10
- const os = require('node:os');
11
- const fsExtra = require('fs-extra');
12
- const clack = require('@clack/prompts');
13
-
14
- /**
15
- * Initialize the memory pack: create project memory files, detect or create global brain.
16
- *
17
- * @param {object} opts
18
- * @param {string} opts.projectDir - Target project directory
19
- * @param {string} opts.bmadSrc - Path to src/bmad-plus/
20
- * @param {string} opts.userName - User display name
21
- * @param {string} opts.commLang - Communication language
22
- * @param {string[]} opts.selectedPacks - Array of pack IDs being installed
23
- * @param {object} opts.i - i18n translations object
24
- * @returns {void}
25
- */
26
- function initMemory({ projectDir, bmadSrc, userName, commLang, selectedPacks }) {
27
- const memoryDir = path.join(projectDir, '.agents', 'memory');
28
- const sessionsDir = path.join(memoryDir, 'sessions');
29
- const globalBrainDir = path.join(os.homedir(), '.bmad-plus', 'brain', 'projects');
30
- const templateDir = path.join(bmadSrc, 'packs', 'pack-memory', 'templates');
31
-
32
- // Create project memory (never overwrite existing)
33
- fsExtra.ensureDirSync(sessionsDir);
34
- const memoryFiles = ['decisions.md', 'lessons.md', 'patterns.md', 'context.md'];
35
- for (const mf of memoryFiles) {
36
- const dest = path.join(memoryDir, mf);
37
- if (!fs.existsSync(dest)) {
38
- const src = path.join(templateDir, mf);
39
- if (fs.existsSync(src)) {
40
- let content = fs.readFileSync(src, 'utf8');
41
- content = content.replace(/\{\{date\}\}/g, new Date().toISOString().slice(0, 10));
42
- content = content.replace(/\{\{project_name\}\}/g, path.basename(projectDir));
43
- content = content.replace(/\{\{project_path\}\}/g, projectDir);
44
- fs.writeFileSync(dest, content, 'utf8');
45
- }
46
- }
47
- }
48
-
49
- // Detect existing brain directories
50
- const brainCandidates = [
51
- path.join(os.homedir(), '.bmad-plus', 'brain'),
52
- path.join(projectDir, '_brain'),
53
- path.join(os.homedir(), '.claude', 'memory'),
54
- ];
55
- const existingBrain = brainCandidates.find(p => fs.existsSync(p));
56
-
57
- if (existingBrain) {
58
- clack.log.info(`🧠 Existing brain detected: ${existingBrain}`);
59
- // Write brain link pointer
60
- fs.writeFileSync(
61
- path.join(memoryDir, '.brain-link'),
62
- JSON.stringify({ linked_brain: existingBrain, linked_at: new Date().toISOString() }, null, 2),
63
- 'utf8'
64
- );
65
- } else {
66
- // Create fresh global brain
67
- fsExtra.ensureDirSync(globalBrainDir);
68
- const identitySrc = path.join(templateDir, 'identity.yaml');
69
- const identityDest = path.join(os.homedir(), '.bmad-plus', 'brain', 'identity.yaml');
70
- if (fs.existsSync(identitySrc) && !fs.existsSync(identityDest)) {
71
- let content = fs.readFileSync(identitySrc, 'utf8');
72
- content = content.replace(/\{\{user_name\}\}/g, userName);
73
- content = content.replace(/\{\{language\}\}/g, commLang);
74
- content = content.replace(/\{\{date\}\}/g, new Date().toISOString().slice(0, 10));
75
- fs.writeFileSync(identityDest, content, 'utf8');
76
- }
77
- // Copy global memory templates
78
- for (const gf of ['decisions.md', 'lessons.md', 'patterns.md']) {
79
- const dest = path.join(os.homedir(), '.bmad-plus', 'brain', gf);
80
- if (!fs.existsSync(dest)) {
81
- const src = path.join(templateDir, gf);
82
- if (fs.existsSync(src)) {
83
- let content = fs.readFileSync(src, 'utf8');
84
- content = content.replace(/\{\{date\}\}/g, new Date().toISOString().slice(0, 10));
85
- content = content.replace(/\{\{project_name\}\}/g, 'Global Brain');
86
- fs.writeFileSync(dest, content, 'utf8');
87
- }
88
- }
89
- }
90
- clack.log.info(`🧠 Global brain created: ${path.join(os.homedir(), '.bmad-plus', 'brain')}`);
91
- }
92
-
93
- // Index this project in global brain
94
- const crypto = require('node:crypto');
95
- const projHash = crypto.createHash('sha256').update(projectDir).digest('hex').slice(0, 8);
96
- const projMeta = {
97
- path: projectDir,
98
- name: path.basename(projectDir),
99
- hash: projHash,
100
- status: 'active',
101
- bmad_installed: true,
102
- packs_installed: selectedPacks,
103
- last_scanned: new Date().toISOString().slice(0, 10),
104
- };
105
- fsExtra.ensureDirSync(globalBrainDir);
106
- fs.writeFileSync(
107
- path.join(globalBrainDir, `${projHash}.yaml`),
108
- Object.entries(projMeta).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join('\n'),
109
- 'utf8'
110
- );
111
- }
112
-
113
- module.exports = { initMemory };
1
+ /**
2
+ * BMAD+ Memory Initialization Module
3
+ * Extracted from install.js — initializes brain with existing brain detection.
4
+ *
5
+ * Author: Laurent Rochetta
6
+ */
7
+
8
+ const path = require('node:path');
9
+ const fs = require('node:fs');
10
+ const os = require('node:os');
11
+ const fsExtra = require('fs-extra');
12
+ const clack = require('@clack/prompts');
13
+ const { projectHash } = require('./path-hash');
14
+
15
+ /**
16
+ * True when `p` exists AND is a directory. A stray *file* named `_brain` (or a
17
+ * file-valued BMAD_PLUS_BRAIN) must never be linked as a brain.
18
+ * @param {string} p
19
+ * @returns {boolean}
20
+ */
21
+ function isBrainCandidateDir(p) {
22
+ try {
23
+ return fs.statSync(p).isDirectory();
24
+ } catch {
25
+ return false;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Resolve the brain directory a project should link to.
31
+ *
32
+ * Priority order:
33
+ * 1. `BMAD_PLUS_BRAIN` env var — explicit override, wins everywhere
34
+ * 2. A `_brain/` directory in the project dir or any ancestor (portfolio brain
35
+ * shared by sibling projects under a common workspace root)
36
+ * 3. `<home>/.bmad-plus/brain` BMAD+ global brain
37
+ * 4. `<home>/.claude/memory` — Claude Code persistent memory, only when it is
38
+ * a non-empty directory (an empty shell is not evidence of a real memory)
39
+ *
40
+ * @param {string} projectDir - Absolute path of the target project
41
+ * @param {string} [homeDir] - Home directory (injectable for tests; defaults to os.homedir())
42
+ * @returns {{dir: string, type: 'portfolio'|'bmad-global'|'claude-memory'}|null}
43
+ */
44
+ function resolveBrain(projectDir, homeDir = os.homedir()) {
45
+ const envBrain = process.env.BMAD_PLUS_BRAIN;
46
+ if (envBrain) {
47
+ if (isBrainCandidateDir(envBrain)) return { dir: envBrain, type: 'portfolio' };
48
+ clack.log.warn(`BMAD_PLUS_BRAIN is set but is not a directory: ${envBrain} — falling back to auto-detection`);
49
+ }
50
+
51
+ let dir = path.resolve(projectDir);
52
+ for (;;) {
53
+ const candidate = path.join(dir, '_brain');
54
+ if (isBrainCandidateDir(candidate)) return { dir: candidate, type: 'portfolio' };
55
+ const parent = path.dirname(dir);
56
+ if (parent === dir) break;
57
+ dir = parent;
58
+ }
59
+
60
+ const globalBrain = path.join(homeDir, '.bmad-plus', 'brain');
61
+ if (isBrainCandidateDir(globalBrain)) return { dir: globalBrain, type: 'bmad-global' };
62
+
63
+ const claudeMemory = path.join(homeDir, '.claude', 'memory');
64
+ if (isBrainCandidateDir(claudeMemory)) {
65
+ let entries;
66
+ try {
67
+ entries = fs.readdirSync(claudeMemory);
68
+ } catch {
69
+ entries = [];
70
+ }
71
+ if (entries.length > 0) return { dir: claudeMemory, type: 'claude-memory' };
72
+ }
73
+
74
+ return null;
75
+ }
76
+
77
+ /**
78
+ * Idempotently ensure the global brain is COMPLETE: identity.yaml, the global
79
+ * memory files, and projects/. Creates only what is missing, never overwrites.
80
+ *
81
+ * This must run before ANY write into the global brain: older versions wrote
82
+ * the projects/ index unconditionally while identity creation lived in a
83
+ * branch that was skipped whenever any brain was detected — leaving a
84
+ * permanent "half brain" (projects/ but no identity.yaml) that resolveBrain
85
+ * then detected as a valid global brain forever.
86
+ *
87
+ * @param {string} brainRoot - Path to <home>/.bmad-plus/brain
88
+ * @param {string} templateDir - Path to pack-memory templates
89
+ * @param {string} userName - User display name (identity.yaml)
90
+ * @param {string} commLang - Communication language (identity.yaml)
91
+ * @returns {void}
92
+ */
93
+ function ensureGlobalBrain(brainRoot, templateDir, userName, commLang) {
94
+ fsExtra.ensureDirSync(path.join(brainRoot, 'projects'));
95
+
96
+ const identitySrc = path.join(templateDir, 'identity.yaml');
97
+ const identityDest = path.join(brainRoot, 'identity.yaml');
98
+ if (fs.existsSync(identitySrc) && !fs.existsSync(identityDest)) {
99
+ let content = fs.readFileSync(identitySrc, 'utf8');
100
+ content = content.replace(/\{\{user_name\}\}/g, userName);
101
+ content = content.replace(/\{\{language\}\}/g, commLang);
102
+ content = content.replace(/\{\{date\}\}/g, new Date().toISOString().slice(0, 10));
103
+ fs.writeFileSync(identityDest, content, 'utf8');
104
+ }
105
+
106
+ for (const gf of ['decisions.md', 'lessons.md', 'patterns.md']) {
107
+ const dest = path.join(brainRoot, gf);
108
+ if (!fs.existsSync(dest)) {
109
+ const src = path.join(templateDir, gf);
110
+ if (fs.existsSync(src)) {
111
+ let content = fs.readFileSync(src, 'utf8');
112
+ content = content.replace(/\{\{date\}\}/g, new Date().toISOString().slice(0, 10));
113
+ content = content.replace(/\{\{project_name\}\}/g, 'Global Brain');
114
+ fs.writeFileSync(dest, content, 'utf8');
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Initialize the memory pack: create project memory files, detect or create global brain.
122
+ *
123
+ * @param {object} opts
124
+ * @param {string} opts.projectDir - Target project directory
125
+ * @param {string} opts.bmadSrc - Path to src/bmad-plus/
126
+ * @param {string} opts.userName - User display name
127
+ * @param {string} opts.commLang - Communication language
128
+ * @param {string[]} opts.selectedPacks - Array of pack IDs being installed
129
+ * @param {string} [opts.homeDir] - Home directory (injectable for tests; defaults to os.homedir())
130
+ * @returns {void}
131
+ */
132
+ function initMemory({ projectDir, bmadSrc, userName, commLang, selectedPacks, homeDir = os.homedir() }) {
133
+ const memoryDir = path.join(projectDir, '.agents', 'memory');
134
+ const sessionsDir = path.join(memoryDir, 'sessions');
135
+ const globalBrainRoot = path.join(homeDir, '.bmad-plus', 'brain');
136
+ const templateDir = path.join(bmadSrc, 'packs', 'pack-memory', 'templates');
137
+
138
+ // Create project memory (never overwrite existing)
139
+ fsExtra.ensureDirSync(sessionsDir);
140
+ const memoryFiles = ['decisions.md', 'lessons.md', 'patterns.md', 'context.md'];
141
+ for (const mf of memoryFiles) {
142
+ const dest = path.join(memoryDir, mf);
143
+ if (!fs.existsSync(dest)) {
144
+ const src = path.join(templateDir, mf);
145
+ if (fs.existsSync(src)) {
146
+ let content = fs.readFileSync(src, 'utf8');
147
+ content = content.replace(/\{\{date\}\}/g, new Date().toISOString().slice(0, 10));
148
+ content = content.replace(/\{\{project_name\}\}/g, path.basename(projectDir));
149
+ content = content.replace(/\{\{project_path\}\}/g, projectDir);
150
+ fs.writeFileSync(dest, content, 'utf8');
151
+ }
152
+ }
153
+ }
154
+
155
+ // Detect existing brain (env var > portfolio _brain walk-up > global > claude)
156
+ const detected = resolveBrain(projectDir, homeDir);
157
+
158
+ let linkedBrain;
159
+ if (detected) {
160
+ clack.log.info(`🧠 Existing brain detected (${detected.type}): ${detected.dir}`);
161
+ linkedBrain = detected;
162
+ } else {
163
+ ensureGlobalBrain(globalBrainRoot, templateDir, userName, commLang);
164
+ clack.log.info(`🧠 Global brain created: ${globalBrainRoot}`);
165
+ linkedBrain = { dir: globalBrainRoot, type: 'bmad-global' };
166
+ }
167
+
168
+ // Write the brain link pointer in EVERY case — including first-ever creation,
169
+ // so a fresh machine's first install is linked exactly like the second one.
170
+ fs.writeFileSync(
171
+ path.join(memoryDir, '.brain-link'),
172
+ JSON.stringify(
173
+ { linked_brain: linkedBrain.dir, brain_type: linkedBrain.type, linked_at: new Date().toISOString() },
174
+ null,
175
+ 2
176
+ ),
177
+ 'utf8'
178
+ );
179
+
180
+ // Index this project in the global brain — only when the global brain IS the
181
+ // linked brain. Portfolio and claude-memory brains keep their own registries
182
+ // (see pack-memory/shared/memory-protocol.md); writing into ~/.bmad-plus from
183
+ // those installs is what used to create orphaned "half brains".
184
+ if (linkedBrain.type === 'bmad-global') {
185
+ ensureGlobalBrain(globalBrainRoot, templateDir, userName, commLang);
186
+ const projHash = projectHash(projectDir);
187
+ const projMeta = {
188
+ path: projectDir,
189
+ name: path.basename(projectDir),
190
+ hash: projHash,
191
+ status: 'active',
192
+ bmad_installed: true,
193
+ packs_installed: selectedPacks,
194
+ last_scanned: new Date().toISOString().slice(0, 10),
195
+ };
196
+ fs.writeFileSync(
197
+ path.join(globalBrainRoot, 'projects', `${projHash}.yaml`),
198
+ Object.entries(projMeta).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join('\n'),
199
+ 'utf8'
200
+ );
201
+ }
202
+ }
203
+
204
+ module.exports = { initMemory, resolveBrain, ensureGlobalBrain };