brainclaw 0.19.14 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Adaptive instruction file templates — generates brainclaw section content
3
+ * based on the agent's capability profile (tier A/B/C).
4
+ *
5
+ * Core (static) vs Run (dynamic) separation:
6
+ * Core: protocol, "why brainclaw", constraints, instructions, estimation rule
7
+ * Run: traps, plans, decisions, claims, handoffs, runtime notes
8
+ *
9
+ * Tier A (MCP + hooks): lightweight — hooks inject run content automatically
10
+ * Tier B (MCP, no hooks): directive — includes top traps, forces MCP calls
11
+ * Tier C (no MCP): rich — includes plans, traps, decisions (only source)
12
+ */
13
+ /**
14
+ * Render the brainclaw section content for an instruction file,
15
+ * adapted to the agent's capability profile.
16
+ */
17
+ export function renderBrainclawSection(input) {
18
+ const { profile } = input;
19
+ switch (profile.templateTier) {
20
+ case 'A': return renderTierA(input);
21
+ case 'B': return renderTierB(input);
22
+ case 'C': return renderTierC(input);
23
+ }
24
+ }
25
+ // ─── Tier A: Full integration (MCP + hooks) ─────────────────────────────────
26
+ function renderTierA(input) {
27
+ const sections = [];
28
+ const included = [];
29
+ sections.push(renderHeader(input));
30
+ included.push('header');
31
+ sections.push(renderWhySection(input.profile));
32
+ included.push('why');
33
+ sections.push(renderProtocolTierA());
34
+ included.push('protocol');
35
+ sections.push(renderEstimationRule());
36
+ included.push('estimation');
37
+ sections.push(renderVersionCheckRule());
38
+ included.push('version-check');
39
+ const constraints = renderConstraints(input.state);
40
+ if (constraints) {
41
+ sections.push(constraints);
42
+ included.push('constraints');
43
+ }
44
+ const instructions = renderInstructions(input.resolvedInstructions);
45
+ if (instructions) {
46
+ sections.push(instructions);
47
+ included.push('instructions');
48
+ }
49
+ return {
50
+ content: sections.join('\n\n'),
51
+ tier: 'A',
52
+ sectionsIncluded: included,
53
+ };
54
+ }
55
+ // ─── Tier B: Standard integration (MCP, no hooks) ───────────────────────────
56
+ function renderTierB(input) {
57
+ const sections = [];
58
+ const included = [];
59
+ sections.push(renderHeader(input));
60
+ included.push('header');
61
+ sections.push(renderWhySection(input.profile));
62
+ included.push('why');
63
+ sections.push(renderProtocolTierB());
64
+ included.push('protocol');
65
+ sections.push(renderEstimationRule());
66
+ included.push('estimation');
67
+ sections.push(renderVersionCheckRule());
68
+ included.push('version-check');
69
+ const constraints = renderConstraints(input.state);
70
+ if (constraints) {
71
+ sections.push(constraints);
72
+ included.push('constraints');
73
+ }
74
+ const instructions = renderInstructions(input.resolvedInstructions);
75
+ if (instructions) {
76
+ sections.push(instructions);
77
+ included.push('instructions');
78
+ }
79
+ // Tier B includes top traps statically (agent has no hooks to get them)
80
+ const traps = renderTopTraps(input.state, input.maxTraps ?? 5);
81
+ if (traps) {
82
+ sections.push(traps);
83
+ included.push('traps');
84
+ }
85
+ return {
86
+ content: sections.join('\n\n'),
87
+ tier: 'B',
88
+ sectionsIncluded: included,
89
+ };
90
+ }
91
+ // ─── Tier C: Limited integration (no MCP) ────────────────────────────────────
92
+ function renderTierC(input) {
93
+ const sections = [];
94
+ const included = [];
95
+ sections.push(renderHeader(input));
96
+ included.push('header');
97
+ sections.push(renderWhySection(input.profile));
98
+ included.push('why');
99
+ sections.push(renderProtocolTierC(input.profile));
100
+ included.push('protocol');
101
+ sections.push(renderEstimationRule());
102
+ included.push('estimation');
103
+ const constraints = renderConstraints(input.state);
104
+ if (constraints) {
105
+ sections.push(constraints);
106
+ included.push('constraints');
107
+ }
108
+ const instructions = renderInstructions(input.resolvedInstructions);
109
+ if (instructions) {
110
+ sections.push(instructions);
111
+ included.push('instructions');
112
+ }
113
+ // Tier C includes everything statically — it's the only source of context
114
+ const traps = renderTopTraps(input.state, input.maxTraps ?? 10);
115
+ if (traps) {
116
+ sections.push(traps);
117
+ included.push('traps');
118
+ }
119
+ const plans = renderActivePlans(input.state, input.maxPlans ?? 10);
120
+ if (plans) {
121
+ sections.push(plans);
122
+ included.push('plans');
123
+ }
124
+ const decisions = renderRecentDecisions(input.state);
125
+ if (decisions) {
126
+ sections.push(decisions);
127
+ included.push('decisions');
128
+ }
129
+ return {
130
+ content: sections.join('\n\n'),
131
+ tier: 'C',
132
+ sectionsIncluded: included,
133
+ };
134
+ }
135
+ // ─── Shared section renderers ────────────────────────────────────────────────
136
+ function renderHeader(input) {
137
+ return [
138
+ `> Managed by brainclaw v${input.brainclawVersion} — do not edit manually.`,
139
+ `> Regenerate: brainclaw export --format ${formatForAgent(input.profile.name)} --write`,
140
+ ].join('\n');
141
+ }
142
+ function renderWhySection(profile) {
143
+ const lines = [
144
+ '## brainclaw — why this matters',
145
+ '',
146
+ 'This project uses brainclaw for shared memory and multi-agent coordination.',
147
+ ];
148
+ if (profile.hasMcp) {
149
+ lines.push('Other agents and developers work in this repo. Without brainclaw context:', '- You may edit files another agent is actively working on', '- You will miss known traps and architectural decisions', '- You will duplicate or contradict planned work');
150
+ }
151
+ else {
152
+ lines.push('Project context is maintained in brainclaw memory. This includes:', '- Active constraints that must be respected', '- Known traps to avoid repeating mistakes', '- Plans and decisions for ongoing work coordination');
153
+ }
154
+ return lines.join('\n');
155
+ }
156
+ function renderProtocolTierA() {
157
+ return [
158
+ '## brainclaw — session protocol',
159
+ '',
160
+ 'Brainclaw context is injected automatically via hooks at each prompt.',
161
+ '',
162
+ '1. Read the injected board state before acting',
163
+ '2. Call `bclaw_get_context(target)` for deeper memory on a specific scope',
164
+ '3. Call `bclaw_claim(scope)` before editing if other agents are active',
165
+ '4. Call `bclaw_session_end(auto_release: true)` when done',
166
+ ].join('\n');
167
+ }
168
+ function renderProtocolTierB() {
169
+ return [
170
+ '## brainclaw — session protocol (REQUIRED)',
171
+ '',
172
+ 'You MUST follow this protocol before any work in this project.',
173
+ '',
174
+ '1. Call `bclaw_session_start` — identify yourself and see the active board',
175
+ '2. Call `bclaw_get_context(target: "<file or dir>")` — load relevant memory',
176
+ '3. Call `bclaw_get_execution_context` — check for brainclaw updates',
177
+ '4. Check that no other agent has claimed your target scope',
178
+ '5. Call `bclaw_claim(scope)` before editing',
179
+ '6. Call `bclaw_write_note(text)` to record observations',
180
+ '7. Call `bclaw_session_end(auto_release: true)` when done',
181
+ ].join('\n');
182
+ }
183
+ function renderProtocolTierC(profile) {
184
+ const lines = [
185
+ '## brainclaw — project coordination',
186
+ '',
187
+ ];
188
+ if (profile.hasSkills) {
189
+ lines.push('Use the brainclaw-context skill to refresh project memory.', 'Trigger: "refresh project memory", "load brainclaw context", "inspect active plans"', '');
190
+ }
191
+ lines.push('Before working:', '- Review the constraints and active plans below', '- Check the known traps section for pitfalls in your scope', '', 'The sections below are regenerated from brainclaw memory.', 'For the freshest state, use the brainclaw skill or ask the developer to run `brainclaw context`.');
192
+ return lines.join('\n');
193
+ }
194
+ function renderEstimationRule() {
195
+ return [
196
+ '## brainclaw — plans and estimation',
197
+ '',
198
+ 'When creating a plan or step, always estimate duration in minutes.',
199
+ 'When completing, report actual effort. This calibrates future estimates.',
200
+ ].join('\n');
201
+ }
202
+ function renderVersionCheckRule() {
203
+ return [
204
+ '## brainclaw — version check',
205
+ '',
206
+ 'Call `bclaw_get_execution_context` at session start to check for brainclaw updates.',
207
+ 'If a newer version is available, inform the developer and suggest updating.',
208
+ 'Brainclaw updates may include new features, bug fixes, and improved coordination.',
209
+ ].join('\n');
210
+ }
211
+ function renderConstraints(state) {
212
+ const active = state.active_constraints.filter((c) => c.status === 'active');
213
+ if (active.length === 0)
214
+ return undefined;
215
+ return [
216
+ '## brainclaw — active constraints',
217
+ '',
218
+ ...active.map((c) => `- ${c.text}`),
219
+ ].join('\n');
220
+ }
221
+ function renderInstructions(instructions) {
222
+ if (instructions.length === 0)
223
+ return undefined;
224
+ return [
225
+ '## brainclaw — active instructions',
226
+ '',
227
+ ...instructions.map((text) => `- ${text}`),
228
+ ].join('\n');
229
+ }
230
+ function renderTopTraps(state, limit) {
231
+ const traps = state.known_traps
232
+ .filter((t) => t.visibility === 'shared' && (!t.status || t.status === 'active') && !t.platform_scope)
233
+ .sort((a, b) => severityOrder(b.severity) - severityOrder(a.severity))
234
+ .slice(0, limit);
235
+ if (traps.length === 0)
236
+ return undefined;
237
+ return [
238
+ '## brainclaw — known traps',
239
+ '',
240
+ ...traps.map((t) => `- [${t.severity}] ${t.text}`),
241
+ ].join('\n');
242
+ }
243
+ function renderActivePlans(state, limit) {
244
+ const active = state.plan_items
245
+ .filter((p) => p.status === 'in_progress' || p.status === 'todo')
246
+ .sort((a, b) => {
247
+ if (a.status === 'in_progress' && b.status !== 'in_progress')
248
+ return -1;
249
+ if (b.status === 'in_progress' && a.status !== 'in_progress')
250
+ return 1;
251
+ return priorityOrder(b.priority) - priorityOrder(a.priority);
252
+ })
253
+ .slice(0, limit);
254
+ if (active.length === 0)
255
+ return undefined;
256
+ return [
257
+ '## brainclaw — active plans',
258
+ '',
259
+ ...active.map((p) => {
260
+ const assignee = p.assignee ? ` (@${p.assignee})` : '';
261
+ return `- [${p.status}] ${p.text}${assignee}`;
262
+ }),
263
+ ].join('\n');
264
+ }
265
+ function renderRecentDecisions(state) {
266
+ const decisions = state.recent_decisions.slice(-5);
267
+ if (decisions.length === 0)
268
+ return undefined;
269
+ return [
270
+ '## brainclaw — recent decisions',
271
+ '',
272
+ ...decisions.map((d) => `- ${d.text}`),
273
+ ].join('\n');
274
+ }
275
+ // ─── Helpers ─────────────────────────────────────────────────────────────────
276
+ function severityOrder(severity) {
277
+ switch (severity) {
278
+ case 'high': return 3;
279
+ case 'medium': return 2;
280
+ case 'low': return 1;
281
+ default: return 0;
282
+ }
283
+ }
284
+ function priorityOrder(priority) {
285
+ switch (priority) {
286
+ case 'critical': return 4;
287
+ case 'high': return 3;
288
+ case 'medium': return 2;
289
+ case 'low': return 1;
290
+ default: return 0;
291
+ }
292
+ }
293
+ function formatForAgent(agentName) {
294
+ switch (agentName) {
295
+ case 'claude-code': return 'claude-md';
296
+ case 'cursor': return 'cursor-rules';
297
+ case 'github-copilot': return 'copilot-instructions';
298
+ case 'opencode':
299
+ case 'codex': return 'agents-md';
300
+ case 'antigravity': return 'gemini-md';
301
+ case 'windsurf': return 'windsurf';
302
+ case 'cline': return 'cline';
303
+ case 'roo': return 'roo';
304
+ case 'continue': return 'continue';
305
+ default: return 'agents-md';
306
+ }
307
+ }
308
+ //# sourceMappingURL=instruction-templates.js.map
@@ -96,6 +96,7 @@ export const TrapSchema = z.object({
96
96
  visibility: MemoryVisibilitySchema.default('shared'),
97
97
  host_id: z.string().optional(),
98
98
  expires_at: z.string().optional(),
99
+ platform_scope: z.string().optional(),
99
100
  });
100
101
  export const HandoffSchema = z.object({
101
102
  schema_version: z.number().int().positive().optional(),
@@ -484,6 +485,12 @@ export const MemorySeedSourceKindSchema = z.enum([
484
485
  'machine',
485
486
  'skill',
486
487
  'mcp',
488
+ 'ci_config',
489
+ 'contributing',
490
+ 'changelog',
491
+ 'docker',
492
+ 'env_example',
493
+ 'adr',
487
494
  ]);
488
495
  export const MemorySeedConfidenceSchema = z.enum(['low', 'medium', 'high']);
489
496
  export const MemorySeedDocumentSchema = z.object({
@@ -626,9 +633,11 @@ export const AgentIntegrationSurfaceSchema = z.object({
626
633
  location: AgentIntegrationLocationSchema,
627
634
  path: z.string().optional(),
628
635
  });
636
+ export const AgentIntegrationLevelSchema = z.enum(['full', 'standard', 'limited', 'custom']);
629
637
  export const AgentIntegrationDeclarationSchema = z.object({
630
638
  agent_name: AgentIntegrationNameSchema,
631
639
  declaration_source: AgentIntegrationDeclarationSourceSchema.default('manual'),
640
+ level: AgentIntegrationLevelSchema.optional(),
632
641
  surfaces: z.array(AgentIntegrationSurfaceSchema).default([]),
633
642
  notes: z.string().optional(),
634
643
  });
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Setup flow logic for the new onboarding experience.
3
+ *
4
+ * Two modes:
5
+ * - Quick: init the current repo (1-2 MCP calls)
6
+ * - Batch: scan roots and init multiple repos (legacy 4-step flow)
7
+ *
8
+ * Quick flow:
9
+ * 1. Auto-detect repo, agent, nearby stores → ask project type + topology
10
+ * 2. Init + optional bootstrap → done
11
+ */
12
+ import fs from 'node:fs';
13
+ import path from 'node:path';
14
+ import { memoryExists } from './io.js';
15
+ import { detectAiAgent } from './ai-agent-detection.js';
16
+ import { resolveStoreChain } from './store-resolution.js';
17
+ import { analyzeRepository } from './repo-analysis.js';
18
+ import { getAgentCapabilityProfile, getAllAgentCapabilityProfiles } from './agent-capability.js';
19
+ import { describeAgentSurfaces } from './agent-capability.js';
20
+ import { loadState } from './state.js';
21
+ /**
22
+ * Probe the current working directory to understand what we're working with.
23
+ * This is the first step of the quick setup flow — no questions yet, just detection.
24
+ */
25
+ export function probeForQuickSetup(cwd = process.cwd()) {
26
+ const isGitRepo = fs.existsSync(path.join(cwd, '.git'));
27
+ const alreadyInitialized = memoryExists(cwd);
28
+ const repoName = path.basename(cwd);
29
+ // Detect agent
30
+ const detectedAi = detectAiAgent();
31
+ const detectedAgent = detectedAi
32
+ ? { name: detectedAi.name, profile: getAgentCapabilityProfile(detectedAi.name) }
33
+ : undefined;
34
+ // Find other known agent profiles (for info)
35
+ const otherAgents = detectedAgent
36
+ ? getAllAgentCapabilityProfiles().filter((p) => p.name !== detectedAgent.name)
37
+ : getAllAgentCapabilityProfiles();
38
+ // Scan nearby stores
39
+ const nearbyStores = resolveStoreChain(cwd);
40
+ // Has content?
41
+ const IGNORED = new Set(['.git', '.brainclaw', '.gitignore', '.gitattributes', '.DS_Store', 'Thumbs.db']);
42
+ let hasContent = false;
43
+ try {
44
+ const entries = fs.readdirSync(cwd);
45
+ hasContent = entries.some((e) => !IGNORED.has(e));
46
+ }
47
+ catch {
48
+ // empty or unreadable
49
+ }
50
+ // Analyze repo for project type suggestion
51
+ let suggestedProjectType = 'standalone';
52
+ let analysisReasons = [];
53
+ try {
54
+ const analysis = analyzeRepository(cwd);
55
+ if (analysis.recommendedMode === 'multi-project') {
56
+ suggestedProjectType = 'workspace';
57
+ }
58
+ analysisReasons = analysis.reasons;
59
+ }
60
+ catch {
61
+ // analysis failed — default to standalone
62
+ }
63
+ // If there are nearby stores, suggest linking
64
+ const parentStores = nearbyStores.filter((s) => s.depth > 0 && s.role === 'workspace');
65
+ if (parentStores.length > 0 && !alreadyInitialized) {
66
+ suggestedProjectType = 'linked';
67
+ }
68
+ // Build repo summary
69
+ const summaryParts = [];
70
+ if (isGitRepo)
71
+ summaryParts.push('git repo');
72
+ if (hasContent)
73
+ summaryParts.push(`"${repoName}"`);
74
+ if (analysisReasons.length > 0)
75
+ summaryParts.push(analysisReasons[0]);
76
+ const repoSummary = summaryParts.join(', ') || 'empty directory';
77
+ return {
78
+ cwd,
79
+ isGitRepo,
80
+ alreadyInitialized,
81
+ repoName,
82
+ repoSummary,
83
+ detectedAgent,
84
+ otherAgents,
85
+ nearbyStores,
86
+ hasContent,
87
+ suggestedProjectType,
88
+ analysisReasons,
89
+ };
90
+ }
91
+ /**
92
+ * Build the structured response for the initial probe step of quick setup.
93
+ * Returns data the agent can use to present choices to the user.
94
+ */
95
+ export function buildQuickSetupProbeResponse(probe) {
96
+ const lines = [];
97
+ if (probe.alreadyInitialized) {
98
+ lines.push(`This project (${probe.repoName}) is already initialized with brainclaw.`);
99
+ lines.push('Use `brainclaw export --detect --write` to regenerate agent files, or `brainclaw upgrade` to migrate.');
100
+ return {
101
+ text: lines.join('\n'),
102
+ structured: {
103
+ already_initialized: true,
104
+ cwd: probe.cwd,
105
+ repo_name: probe.repoName,
106
+ },
107
+ };
108
+ }
109
+ lines.push(`Detected: ${probe.repoSummary}`);
110
+ if (probe.detectedAgent) {
111
+ lines.push(`Agent: ${probe.detectedAgent.name} (${probe.detectedAgent.profile.templateTier === 'A' ? 'full integration' : probe.detectedAgent.profile.templateTier === 'B' ? 'standard integration' : 'limited integration'})`);
112
+ lines.push(`Surfaces: ${describeAgentSurfaces(probe.detectedAgent.name).join(', ')}`);
113
+ }
114
+ if (probe.nearbyStores.length > 0) {
115
+ lines.push('');
116
+ lines.push('Nearby brainclaw stores:');
117
+ for (const store of probe.nearbyStores) {
118
+ lines.push(` - ${store.role} at ${store.cwd} (depth ${store.depth})`);
119
+ }
120
+ }
121
+ lines.push('');
122
+ lines.push('Ask the user:');
123
+ lines.push('');
124
+ lines.push('1. What kind of project is this?');
125
+ lines.push(` - Standalone project (single .brainclaw/ for the whole repo)${probe.suggestedProjectType === 'standalone' ? ' ← suggested' : ''}`);
126
+ lines.push(` - Workspace with sub-projects (monorepo)${probe.suggestedProjectType === 'workspace' ? ' ← suggested' : ''}`);
127
+ if (probe.nearbyStores.some((s) => s.role === 'workspace')) {
128
+ lines.push(` - Linked to an existing workspace${probe.suggestedProjectType === 'linked' ? ' ← suggested' : ''}`);
129
+ }
130
+ lines.push('');
131
+ lines.push('2. Should the memory be shared with the team?');
132
+ lines.push(' - Yes, shared via git (.brainclaw/ tracked in git) ← recommended');
133
+ lines.push(' - No, local only (.brainclaw/ gitignored)');
134
+ return {
135
+ text: lines.join('\n'),
136
+ structured: {
137
+ pending_question: 'quick_init',
138
+ probe: {
139
+ cwd: probe.cwd,
140
+ repo_name: probe.repoName,
141
+ repo_summary: probe.repoSummary,
142
+ is_git_repo: probe.isGitRepo,
143
+ has_content: probe.hasContent,
144
+ detected_agent: probe.detectedAgent?.name ?? null,
145
+ agent_surfaces: probe.detectedAgent ? describeAgentSurfaces(probe.detectedAgent.name) : [],
146
+ nearby_stores: probe.nearbyStores.map((s) => ({ role: s.role, path: s.cwd, depth: s.depth })),
147
+ suggested_project_type: probe.suggestedProjectType,
148
+ },
149
+ choices: {
150
+ project_type: ['standalone', 'workspace', ...(probe.nearbyStores.some((s) => s.role === 'workspace') ? ['linked'] : [])],
151
+ topology: ['embedded', 'sidecar'],
152
+ },
153
+ },
154
+ };
155
+ }
156
+ /**
157
+ * Generate a "moment aha" preview after init — shows what an agent
158
+ * would see when calling bclaw_get_context on this project.
159
+ */
160
+ export function buildOnboardingPreview(cwd) {
161
+ try {
162
+ const state = loadState(cwd);
163
+ const constraints = state.active_constraints.filter((c) => c.status === 'active');
164
+ const traps = state.known_traps.filter((t) => t.visibility === 'shared' && (!t.status || t.status === 'active'));
165
+ const plans = state.plan_items.filter((p) => p.status === 'in_progress' || p.status === 'todo');
166
+ if (constraints.length === 0 && traps.length === 0 && plans.length === 0) {
167
+ return 'Memory is empty. Run bclaw_bootstrap to extract initial context from this repo.';
168
+ }
169
+ const lines = ['Here is what your agent will see:'];
170
+ if (constraints.length > 0) {
171
+ lines.push(` Constraints: ${constraints.length} active`);
172
+ for (const c of constraints.slice(0, 3))
173
+ lines.push(` - ${c.text}`);
174
+ }
175
+ if (traps.length > 0) {
176
+ lines.push(` Traps: ${traps.length} known`);
177
+ for (const t of traps.slice(0, 3))
178
+ lines.push(` - [${t.severity}] ${t.text}`);
179
+ }
180
+ if (plans.length > 0) {
181
+ lines.push(` Plans: ${plans.length} active`);
182
+ for (const p of plans.slice(0, 3))
183
+ lines.push(` - [${p.status}] ${p.text}`);
184
+ }
185
+ return lines.join('\n');
186
+ }
187
+ catch {
188
+ return 'Memory is empty. Run bclaw_bootstrap to extract initial context from this repo.';
189
+ }
190
+ }
191
+ //# sourceMappingURL=setup-flow.js.map
@@ -1,6 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { writeFileAtomic } from './io.js';
3
+ import { ensureMemoryDir, writeFileAtomic } from './io.js';
4
+ import { defaultConfig, saveConfig } from './config.js';
4
5
  export function resolveHomeDir(env = process.env) {
5
6
  return env.HOME?.trim() || env.USERPROFILE?.trim() || undefined;
6
7
  }
@@ -47,4 +48,32 @@ export function hasCompletedSetup(env = process.env) {
47
48
  const configPath = userStoreConfigPath(env);
48
49
  return configPath ? fs.existsSync(configPath) : false;
49
50
  }
51
+ /**
52
+ * Ensure the user-global store (~/.brainclaw/) exists, creating it implicitly
53
+ * if absent. This replaces the old "setup required before init" guard —
54
+ * init can now auto-create the minimal user store on first run.
55
+ *
56
+ * Idempotent: returns immediately if the user store already exists.
57
+ * Non-fatal: logs a warning if creation fails but does not throw.
58
+ */
59
+ export function ensureUserStore(env = process.env) {
60
+ const home = resolveHomeDir(env);
61
+ if (!home)
62
+ return false;
63
+ const configPath = path.join(home, '.brainclaw', 'config.yaml');
64
+ if (fs.existsSync(configPath)) {
65
+ return true; // already exists
66
+ }
67
+ try {
68
+ ensureMemoryDir(home);
69
+ const cfg = defaultConfig('user-global');
70
+ saveConfig(cfg, home);
71
+ fs.appendFileSync(configPath, 'store_type: user\n');
72
+ return true;
73
+ }
74
+ catch (err) {
75
+ console.warn(`Warning: could not create user store at ${path.join(home, '.brainclaw')}:`, err instanceof Error ? err.message : String(err));
76
+ return false;
77
+ }
78
+ }
50
79
  //# sourceMappingURL=setup-state.js.map
@@ -1,7 +1,9 @@
1
1
  import fs from 'node:fs';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
+ import { loadConfig } from './config.js';
4
5
  import { MEMORY_DIR } from './io.js';
6
+ import { summarizeWorkspaceProjects } from './workspace-projects.js';
5
7
  /**
6
8
  * Walk up the filesystem from `cwd`, collecting every `.brainclaw/` directory
7
9
  * found along the way, up to (and including) `boundary`.
@@ -82,6 +84,49 @@ export function resolveTargetStore(cwd = process.cwd(), target = 'local', option
82
84
  const match = chain.find((s) => s.role === 'user');
83
85
  return match?.cwd ?? os.homedir();
84
86
  }
87
+ /**
88
+ * Resolve the most specific child store that should answer a context request.
89
+ *
90
+ * This keeps the current cwd by default, but when `target` clearly points inside
91
+ * a nested Brainclaw project (for example from a workspace root in folder mode),
92
+ * it returns that child store cwd instead.
93
+ */
94
+ export function resolveContextStoreCwd(cwd = process.cwd(), target) {
95
+ const trimmedTarget = target?.trim();
96
+ if (!trimmedTarget) {
97
+ return cwd;
98
+ }
99
+ const primary = resolvePrimaryStore(cwd);
100
+ if (!primary) {
101
+ return cwd;
102
+ }
103
+ const absoluteTarget = resolveAbsoluteTargetPath(cwd, trimmedTarget);
104
+ if (!absoluteTarget) {
105
+ return cwd;
106
+ }
107
+ let config;
108
+ try {
109
+ config = loadConfig(primary.cwd);
110
+ }
111
+ catch {
112
+ return cwd;
113
+ }
114
+ const summary = summarizeWorkspaceProjects(primary.cwd, config);
115
+ if (summary.discovered_projects.length === 0) {
116
+ return cwd;
117
+ }
118
+ const candidates = summary.discovered_projects
119
+ .map((project) => path.resolve(primary.cwd, project.path))
120
+ .filter((candidatePath) => candidatePath !== primary.cwd)
121
+ .filter((candidatePath) => fs.existsSync(path.join(candidatePath, MEMORY_DIR)))
122
+ .sort((a, b) => b.length - a.length);
123
+ for (const candidate of candidates) {
124
+ if (isAtOrBelow(absoluteTarget, candidate)) {
125
+ return candidate;
126
+ }
127
+ }
128
+ return cwd;
129
+ }
85
130
  /**
86
131
  * Return true if `dir` is at or below `ancestor` in the filesystem hierarchy.
87
132
  */
@@ -90,6 +135,19 @@ function isAtOrBelow(dir, ancestor) {
90
135
  // If relative path starts with '..', dir is above ancestor
91
136
  return !rel.startsWith('..');
92
137
  }
138
+ function resolveAbsoluteTargetPath(cwd, target) {
139
+ if (path.isAbsolute(target)) {
140
+ return path.resolve(target);
141
+ }
142
+ const joined = path.resolve(cwd, target);
143
+ if (fs.existsSync(joined)) {
144
+ return joined;
145
+ }
146
+ if (target.includes('/') || target.includes('\\') || target.startsWith('.')) {
147
+ return joined;
148
+ }
149
+ return undefined;
150
+ }
93
151
  /**
94
152
  * Infer the store role from config.yaml store_type field, or fall back to
95
153
  * heuristics (presence of .git sibling = repo, no parent store = workspace).