@claudetools/tools 0.7.2 → 0.7.4

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.
@@ -34,7 +34,8 @@ export declare function resolveProjectId(): string;
34
34
  export declare function resolveProjectIdAsync(): Promise<string>;
35
35
  /**
36
36
  * Gets the default project ID (synchronous version)
37
- * Checks session-context.md first, then config, then falls back to cwd resolution
37
+ * ALWAYS checks session-context.md first (even if cached), then config, then falls back to cwd resolution
38
+ * This handles race conditions where MCP server starts before session-start hook writes the file
38
39
  */
39
40
  export declare function getDefaultProjectId(): string;
40
41
  /**
@@ -100,11 +100,21 @@ export function resolveProjectId() {
100
100
  * Should be called during server startup
101
101
  */
102
102
  export async function resolveProjectIdAsync() {
103
- // Return cached result if available
103
+ // ALWAYS check session-context.md first (even if cached) - handles race condition
104
+ // where MCP server starts before Claude Code's session-start hook writes the file
105
+ const sessionProjectId = getProjectIdFromSessionContext();
106
+ if (sessionProjectId) {
107
+ if (resolvedProjectId !== sessionProjectId) {
108
+ mcpLogger.info('MEMORY', `Project resolved from session context: ${sessionProjectId} (was: ${resolvedProjectId})`);
109
+ resolvedProjectId = sessionProjectId;
110
+ }
111
+ return resolvedProjectId;
112
+ }
113
+ // Return cached result if available (and no session-context.md)
104
114
  if (resolvedProjectId) {
105
115
  return resolvedProjectId;
106
116
  }
107
- // Check environment variable first
117
+ // Check environment variable
108
118
  if (process.env.CLAUDETOOLS_PROJECT_ID) {
109
119
  const envProjectId = process.env.CLAUDETOOLS_PROJECT_ID;
110
120
  // Validate UUID format
@@ -116,14 +126,6 @@ export async function resolveProjectIdAsync() {
116
126
  mcpLogger.debug('MEMORY', `Using project ID from env: ${resolvedProjectId}`);
117
127
  return resolvedProjectId;
118
128
  }
119
- // Check session-context.md (written by Claude Code's session-start hook)
120
- // This is the most reliable source since MCP server's cwd isn't the project dir
121
- const sessionProjectId = getProjectIdFromSessionContext();
122
- if (sessionProjectId) {
123
- resolvedProjectId = sessionProjectId;
124
- mcpLogger.info('MEMORY', `Project resolved from session context: ${resolvedProjectId}`);
125
- return resolvedProjectId;
126
- }
127
129
  const cwd = process.cwd();
128
130
  // Check projects.json cache
129
131
  const binding = findProjectBinding(cwd);
@@ -165,16 +167,23 @@ export async function resolveProjectIdAsync() {
165
167
  let _defaultProjectId = null;
166
168
  /**
167
169
  * Gets the default project ID (synchronous version)
168
- * Checks session-context.md first, then config, then falls back to cwd resolution
170
+ * ALWAYS checks session-context.md first (even if cached), then config, then falls back to cwd resolution
171
+ * This handles race conditions where MCP server starts before session-start hook writes the file
169
172
  */
170
173
  export function getDefaultProjectId() {
171
- if (_defaultProjectId) {
172
- return _defaultProjectId;
173
- }
174
- // Check session-context.md first (most reliable for MCP server)
174
+ // ALWAYS check session-context.md first - it may have been written after we cached a fallback value
175
+ // This handles the race condition where MCP server starts before Claude Code's session-start hook runs
175
176
  const sessionProjectId = getProjectIdFromSessionContext();
176
177
  if (sessionProjectId) {
177
- _defaultProjectId = sessionProjectId;
178
+ // Update cache if different (session-context.md is authoritative)
179
+ if (_defaultProjectId !== sessionProjectId) {
180
+ mcpLogger.debug('MEMORY', `Updating project ID from session-context: ${sessionProjectId} (was: ${_defaultProjectId})`);
181
+ _defaultProjectId = sessionProjectId;
182
+ }
183
+ return _defaultProjectId;
184
+ }
185
+ // If we have a cached value and no session-context.md, use cache
186
+ if (_defaultProjectId) {
178
187
  return _defaultProjectId;
179
188
  }
180
189
  // Try config second
@@ -198,10 +207,19 @@ export function getDefaultProjectId() {
198
207
  * Async version for server startup
199
208
  */
200
209
  export async function getDefaultProjectIdAsync() {
210
+ // ALWAYS check session-context.md first - same as sync version
211
+ const sessionProjectId = getProjectIdFromSessionContext();
212
+ if (sessionProjectId) {
213
+ if (_defaultProjectId !== sessionProjectId) {
214
+ mcpLogger.debug('MEMORY', `Updating project ID from session-context (async): ${sessionProjectId} (was: ${_defaultProjectId})`);
215
+ _defaultProjectId = sessionProjectId;
216
+ }
217
+ return _defaultProjectId;
218
+ }
201
219
  if (_defaultProjectId) {
202
220
  return _defaultProjectId;
203
221
  }
204
- // Try config first
222
+ // Try config
205
223
  if (config.defaultProjectId) {
206
224
  if (!isValidProjectId(config.defaultProjectId)) {
207
225
  // Legacy format - convert to local_ format instead of throwing
@@ -5,7 +5,7 @@ export declare const PROJECT_SECTION_END = "<!-- CLAUDETOOLS:PROJECT:END -->";
5
5
  /**
6
6
  * Global CLAUDE.md content - added to ~/.claude/CLAUDE.md
7
7
  */
8
- export declare const GLOBAL_TEMPLATE = "\n<!-- CLAUDETOOLS:START -->\n# ClaudeTools Memory System\n\nYou have access to a persistent memory system via the `claudetools_memory` MCP server. Use it to remember context across sessions.\n\n## Memory Tools\n\n### Searching Memory\n```\nmemory_search(query: \"authentication patterns\")\n```\nSearch for relevant facts, entities, and past context. Use this when:\n- Starting work on a feature to recall past decisions\n- Looking for patterns or conventions used before\n- Finding related code or architectural context\n\n### Storing Facts\n```\nmemory_store_fact(\n entity1: \"UserService\",\n relationship: \"USES\",\n entity2: \"bcrypt\",\n context: \"Password hashing uses bcrypt with 12 rounds\"\n)\n```\nStore important facts as relationships between entities. Use for:\n- Architectural decisions\n- Code patterns and conventions\n- Dependencies and relationships\n- User preferences learned during conversation\n\n### Context Injection\nContext is automatically injected at the start of each session based on the current project. Check `~/.claudetools/session-context.md` for project-specific context.\n\n## Task Management\n\n### Creating Work Plans with Auto-Dispatch\n```\ntask_plan(\n goal: \"Add user authentication\",\n epic_title: \"User Auth System\",\n tasks: [...]\n)\n```\nBreak down complex work into tracked tasks. **Tasks are automatically set to `ready` status.**\n\nAfter `task_plan` completes, you will receive Task tool invocations to spawn parallel agents.\n**Execute ALL Task tool calls in a SINGLE message** to enable parallel execution.\n\n### Parallel Agent Workflow\n1. User describes work needed\n2. Call `task_plan_draft` to present the plan\n3. User says \"go\" to approve\n4. Call `task_plan` - tasks created in `ready` status\n5. Execute the provided Task tool calls in ONE message\n6. Agents work in parallel, each calling `task_complete` when done\n\n### Manual Task Start (Sequential)\n```\ntask_start(task_id: \"task_xxx\")\n```\nClaim a task before working on it. Use for sequential execution.\n\n### Completing Tasks\n```\ntask_complete(task_id: \"task_xxx\", summary: \"Implemented JWT auth with refresh tokens\")\n```\nMark tasks done with a summary of work completed. **Always call this when a task is finished.**\n\n## Codebase Intelligence\n\n### Finding Code\n```\ncodebase_map() # Get overview of project structure\ncodebase_find(\"UserService\") # Find symbols/files\ncodebase_context(\"src/auth.ts\") # Get file dependencies\n```\n\n### Impact Analysis\n```\nanalyze_impact(function_name: \"validateToken\")\n```\nSee what would be affected by changing a function.\n\n## CodeDNA: Generate Code, Save 99% Tokens\n\n**When creating APIs/CRUD operations:** Call `codedna_generate_api` instead of writing code manually.\n\n```\ncodedna_generate_api({\n spec: \"User(email:string:unique, password:string:hashed, age:integer:min(18))\",\n framework: \"express\",\n options: { auth: true, validation: true, tests: true }\n})\n```\n\n**Generates 6 production files** (models, controllers, routes, validators, auth, tests) in ~5 seconds.\n**Saves:** 30,000 tokens \u2192 200 tokens (99% reduction)\n\n## Best Practices\n\n1. **Search before implementing** - Check memory for existing patterns\n2. **Store decisions** - Save architectural choices as facts\n3. **Use task tracking** - Break complex work into tasks\n4. **Use CodeDNA for APIs** - Generate instead of write (99% token savings)\n<!-- CLAUDETOOLS:END -->\n";
8
+ export declare const GLOBAL_TEMPLATE = "\n<!-- CLAUDETOOLS:START -->\n# ClaudeTools Memory System\n\nYou have access to a persistent memory system via the `claudetools_memory` MCP server. Use it to remember context across sessions.\n\n## Memory Tools\n\n### Searching Memory\n```\nmemory_search(query: \"authentication patterns\")\n```\nSearch for relevant facts, entities, and past context. Use this when:\n- Starting work on a feature to recall past decisions\n- Looking for patterns or conventions used before\n- Finding related code or architectural context\n\n### Storing Facts\n```\nmemory_store_fact(\n entity1: \"UserService\",\n relationship: \"USES\",\n entity2: \"bcrypt\",\n context: \"Password hashing uses bcrypt with 12 rounds\"\n)\n```\nStore important facts as relationships between entities. Use for:\n- Architectural decisions\n- Code patterns and conventions\n- Dependencies and relationships\n- User preferences learned during conversation\n\n### Context Injection\nContext is automatically injected at the start of each session based on the current project. Check `~/.claudetools/session-context.md` for project-specific context.\n\n## Task Management\n\n### Creating Work Plans with Auto-Dispatch\n```\ntask_plan(\n goal: \"Add user authentication\",\n epic_title: \"User Auth System\",\n tasks: [...]\n)\n```\nBreak down complex work into tracked tasks. **Tasks are automatically set to `ready` status.**\n\nAfter `task_plan` completes, you will receive Task tool invocations to spawn parallel agents.\n**Execute ALL Task tool calls in a SINGLE message** to enable parallel execution.\n\n### Parallel Agent Workflow\n1. User describes work needed\n2. Call `task_plan_draft` to present the plan\n3. User says \"go\" to approve\n4. Call `task_plan` - tasks created in `ready` status\n5. Execute the provided Task tool calls in ONE message\n6. Agents work in parallel, each calling `task_complete` when done\n\n### Manual Task Start (Sequential)\n```\ntask_start(task_id: \"task_xxx\")\n```\nClaim a task before working on it. Use for sequential execution.\n\n### Completing Tasks\n```\ntask_complete(task_id: \"task_xxx\", summary: \"Implemented JWT auth with refresh tokens\")\n```\nMark tasks done with a summary of work completed. **Always call this when a task is finished.**\n\n## Codebase Intelligence\n\n### Start with codebase_map() - ALWAYS\n```\ncodebase_map() # FIRST TOOL when exploring unfamiliar code\n```\n**When to use:** Starting a new task, exploring unfamiliar code, understanding project structure, finding entry points.\n\nThe map shows:\n- Project structure and key directories\n- Entry points and their exports\n- Framework detection (React, Express, etc.)\n- Key symbols and their locations\n\n**Use codebase_map BEFORE using Grep/Glob** - it gives you the lay of the land so you know where to look.\n\n### Then use targeted tools\n```\ncodebase_find(\"UserService\") # Find specific symbols/files\ncodebase_context(\"src/auth.ts\") # Get file dependencies\nanalyze_impact(\"validateToken\") # See what changing a function affects\n```\n\n## CodeDNA: Generate Code, Save 99% Tokens\n\n**When creating APIs/CRUD operations:** Call `codedna_generate_api` instead of writing code manually.\n\n```\ncodedna_generate_api({\n spec: \"User(email:string:unique, password:string:hashed, age:integer:min(18))\",\n framework: \"express\",\n options: { auth: true, validation: true, tests: true }\n})\n```\n\n**Generates 6 production files** (models, controllers, routes, validators, auth, tests) in ~5 seconds.\n**Saves:** 30,000 tokens \u2192 200 tokens (99% reduction)\n\n## Best Practices\n\n1. **Search before implementing** - Check memory for existing patterns\n2. **Store decisions** - Save architectural choices as facts\n3. **Use task tracking** - Break complex work into tasks\n4. **Use CodeDNA for APIs** - Generate instead of write (99% token savings)\n<!-- CLAUDETOOLS:END -->\n";
9
9
  /**
10
10
  * Project-level CLAUDE.md content - added to .claude/CLAUDE.md
11
11
  */
@@ -82,18 +82,26 @@ Mark tasks done with a summary of work completed. **Always call this when a task
82
82
 
83
83
  ## Codebase Intelligence
84
84
 
85
- ### Finding Code
85
+ ### Start with codebase_map() - ALWAYS
86
86
  \`\`\`
87
- codebase_map() # Get overview of project structure
88
- codebase_find("UserService") # Find symbols/files
89
- codebase_context("src/auth.ts") # Get file dependencies
87
+ codebase_map() # FIRST TOOL when exploring unfamiliar code
90
88
  \`\`\`
89
+ **When to use:** Starting a new task, exploring unfamiliar code, understanding project structure, finding entry points.
90
+
91
+ The map shows:
92
+ - Project structure and key directories
93
+ - Entry points and their exports
94
+ - Framework detection (React, Express, etc.)
95
+ - Key symbols and their locations
91
96
 
92
- ### Impact Analysis
97
+ **Use codebase_map BEFORE using Grep/Glob** - it gives you the lay of the land so you know where to look.
98
+
99
+ ### Then use targeted tools
93
100
  \`\`\`
94
- analyze_impact(function_name: "validateToken")
101
+ codebase_find("UserService") # Find specific symbols/files
102
+ codebase_context("src/auth.ts") # Get file dependencies
103
+ analyze_impact("validateToken") # See what changing a function affects
95
104
  \`\`\`
96
- See what would be affected by changing a function.
97
105
 
98
106
  ## CodeDNA: Generate Code, Save 99% Tokens
99
107
 
@@ -48,6 +48,21 @@ export function buildOrchestratorPrompt(params) {
48
48
  <!-- Layer 2: Behavioral Guidelines -->
49
49
  <behavioral_guidelines>
50
50
  <core_behaviors>
51
+ <behavior id="understand_first" priority="CRITICAL">
52
+ BEFORE creating a task plan, understand the codebase:
53
+
54
+ 1. Call codebase_map() FIRST
55
+ → See project structure, entry points, frameworks
56
+ → Identify which areas of the codebase are relevant
57
+ → Understand existing patterns before planning changes
58
+
59
+ 2. Call memory_search() for past decisions
60
+ → Check for architectural constraints
61
+ → Find relevant patterns already established
62
+
63
+ This ensures your plan fits the existing codebase architecture.
64
+ </behavior>
65
+
51
66
  <behavior id="delegation_first" priority="CRITICAL">
52
67
  NEVER write implementation code in task descriptions or prompts.
53
68
  NEVER provide step-by-step implementation guides with code snippets.
@@ -92,18 +92,24 @@ function buildBehavioralSection(taskId) {
92
92
  <behavior id="tool_first" priority="MANDATORY">
93
93
  BEFORE writing code, use available tools:
94
94
 
95
- 1. memory_search: Check for existing patterns and decisions
95
+ 1. codebase_map: START HERE - Get project overview
96
+ → Understand project structure and key entry points
97
+ → Identify frameworks and patterns in use
98
+ → Find relevant directories before diving in
99
+ → Use BEFORE Grep/Glob for unfamiliar code
100
+
101
+ 2. memory_search: Check for existing patterns and decisions
96
102
  → "How was authentication implemented?"
97
103
  → "What patterns are used for X?"
98
104
 
99
- 2. codebase_find: Find similar implementations
105
+ 3. codebase_find: Find specific symbols/files
100
106
  → Search for existing code to extend/adapt
101
107
 
102
- 3. docs_get: Retrieve cached documentation
103
- → Get up-to-date API references
104
-
105
108
  4. codebase_context: Understand file dependencies
106
109
  → See what a file imports/exports
110
+
111
+ 5. docs_get: Retrieve cached documentation
112
+ → Get up-to-date API references
107
113
  </behavior>
108
114
 
109
115
  <behavior id="minimal_changes" priority="IMPORTANT">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claudetools/tools",
3
- "version": "0.7.2",
3
+ "version": "0.7.4",
4
4
  "description": "Persistent AI memory, task management, and codebase intelligence for Claude Code",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",