diff options
| author | Clawd <ai@clawd.bot> | 2026-02-28 15:35:00 -0800 |
|---|---|---|
| committer | Clawd <ai@clawd.bot> | 2026-02-28 15:35:00 -0800 |
| commit | 0edd7235cd861ef77d4ceb37a594ae65df52624b (patch) | |
| tree | c169d9bb1d0bda40198817e26c781101b1637730 /src/main/claude | |
| parent | 5bd5da261732a1fce6a94ffa4d348dd6c80887cf (diff) | |
Add session-specific artifacts, CLAUDE.md, and git worktree support
- Store artifacts in .claude-flow/sessions/{sessionId}/
- Each session now has isolated research.md and plan.md
- Concurrent sessions no longer conflict
- Add CLAUDE.md support for shared codebase documentation
- Add git worktree creation on session start
- Add git commit/status IPC handlers
- Update all artifact APIs to be session-specific
- Remove artifact clearing on new session (no longer needed)
Diffstat (limited to 'src/main/claude')
| -rw-r--r-- | src/main/claude/index.ts | 91 | ||||
| -rw-r--r-- | src/main/claude/phases.ts | 62 |
2 files changed, 121 insertions, 32 deletions
diff --git a/src/main/claude/index.ts b/src/main/claude/index.ts index 8bdcccd..4d8909b 100644 --- a/src/main/claude/index.ts +++ b/src/main/claude/index.ts | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | import { query, type SDKMessage, type Query } from "@anthropic-ai/claude-agent-sdk"; | 1 | import { query, type SDKMessage, type Query } from "@anthropic-ai/claude-agent-sdk"; |
| 2 | import type { Session } from "../db/sessions"; | 2 | import type { Session } from "../db/sessions"; |
| 3 | import { getPhaseConfig, getNextPhase, getArtifactFilename } from "./phases"; | 3 | import { getPhaseConfig, getNextPhase, getArtifactFilename, getSessionArtifactDir } from "./phases"; |
| 4 | import type { Phase, UserPermissionMode } from "./phases"; | 4 | import type { Phase, UserPermissionMode } from "./phases"; |
| 5 | import { getProject } from "../db/projects"; | 5 | import { getProject } from "../db/projects"; |
| 6 | import { updateSession } from "../db/sessions"; | 6 | import { updateSession } from "../db/sessions"; |
| @@ -10,10 +10,9 @@ import path from "node:path"; | |||
| 10 | // Track active queries by session ID | 10 | // Track active queries by session ID |
| 11 | const activeQueries = new Map<string, Query>(); | 11 | const activeQueries = new Map<string, Query>(); |
| 12 | 12 | ||
| 13 | function ensureArtifactDir(projectPath: string): void { | 13 | function ensureDir(dirPath: string): void { |
| 14 | const dir = path.join(projectPath, ".claude-flow"); | 14 | if (!fs.existsSync(dirPath)) { |
| 15 | if (!fs.existsSync(dir)) { | 15 | fs.mkdirSync(dirPath, { recursive: true }); |
| 16 | fs.mkdirSync(dir, { recursive: true }); | ||
| 17 | } | 16 | } |
| 18 | } | 17 | } |
| 19 | 18 | ||
| @@ -31,7 +30,9 @@ export async function sendMessage({ | |||
| 31 | const project = getProject(session.project_id); | 30 | const project = getProject(session.project_id); |
| 32 | if (!project) throw new Error("Project not found"); | 31 | if (!project) throw new Error("Project not found"); |
| 33 | 32 | ||
| 34 | ensureArtifactDir(project.path); | 33 | // Ensure session artifact directory exists |
| 34 | const sessionDir = path.join(project.path, getSessionArtifactDir(session.id)); | ||
| 35 | ensureDir(sessionDir); | ||
| 35 | 36 | ||
| 36 | const phaseConfig = getPhaseConfig( | 37 | const phaseConfig = getPhaseConfig( |
| 37 | session.phase as Phase, | 38 | session.phase as Phase, |
| @@ -81,8 +82,8 @@ export async function triggerReview( | |||
| 81 | session: Session, | 82 | session: Session, |
| 82 | onMessage: (msg: SDKMessage) => void | 83 | onMessage: (msg: SDKMessage) => void |
| 83 | ): Promise<void> { | 84 | ): Promise<void> { |
| 84 | const docName = getArtifactFilename(session.phase as Phase); | 85 | const artifactPath = getArtifactPath(session); |
| 85 | const message = `I've updated .claude-flow/${docName} with annotations. Read the file, find all my inline notes (marked with // REVIEW:, // NOTE:, TODO:, or similar), address each one, and update the document accordingly. Do not implement anything yet.`; | 86 | const message = `I've updated ${artifactPath} with annotations. Read the file, find all my inline notes (marked with // REVIEW:, // NOTE:, TODO:, or similar), address each one, and update the document accordingly. Do not implement anything yet.`; |
| 86 | 87 | ||
| 87 | await sendMessage({ session, message, onMessage }); | 88 | await sendMessage({ session, message, onMessage }); |
| 88 | } | 89 | } |
| @@ -99,7 +100,63 @@ export function advancePhase(session: Session): Phase | null { | |||
| 99 | } | 100 | } |
| 100 | 101 | ||
| 101 | /** | 102 | /** |
| 102 | * Read an artifact file from the project's .claude-flow directory | 103 | * Get the artifact path for a session and phase |
| 104 | */ | ||
| 105 | export function getArtifactPath(session: Session): string { | ||
| 106 | const filename = getArtifactFilename(session.phase as Phase); | ||
| 107 | return path.join(getSessionArtifactDir(session.id), filename); | ||
| 108 | } | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Read an artifact file for a session | ||
| 112 | */ | ||
| 113 | export function readSessionArtifact( | ||
| 114 | projectPath: string, | ||
| 115 | sessionId: string, | ||
| 116 | filename: string | ||
| 117 | ): string | null { | ||
| 118 | const filePath = path.join(projectPath, getSessionArtifactDir(sessionId), filename); | ||
| 119 | if (fs.existsSync(filePath)) { | ||
| 120 | return fs.readFileSync(filePath, "utf-8"); | ||
| 121 | } | ||
| 122 | return null; | ||
| 123 | } | ||
| 124 | |||
| 125 | /** | ||
| 126 | * Write an artifact file for a session | ||
| 127 | */ | ||
| 128 | export function writeSessionArtifact( | ||
| 129 | projectPath: string, | ||
| 130 | sessionId: string, | ||
| 131 | filename: string, | ||
| 132 | content: string | ||
| 133 | ): void { | ||
| 134 | const dir = path.join(projectPath, getSessionArtifactDir(sessionId)); | ||
| 135 | ensureDir(dir); | ||
| 136 | fs.writeFileSync(path.join(dir, filename), content, "utf-8"); | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 | * Read CLAUDE.md from project root | ||
| 141 | */ | ||
| 142 | export function readClaudeMd(projectPath: string): string | null { | ||
| 143 | const filePath = path.join(projectPath, "CLAUDE.md"); | ||
| 144 | if (fs.existsSync(filePath)) { | ||
| 145 | return fs.readFileSync(filePath, "utf-8"); | ||
| 146 | } | ||
| 147 | return null; | ||
| 148 | } | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Write CLAUDE.md to project root | ||
| 152 | */ | ||
| 153 | export function writeClaudeMd(projectPath: string, content: string): void { | ||
| 154 | const filePath = path.join(projectPath, "CLAUDE.md"); | ||
| 155 | fs.writeFileSync(filePath, content, "utf-8"); | ||
| 156 | } | ||
| 157 | |||
| 158 | /** | ||
| 159 | * Read an artifact file from the project's .claude-flow directory (legacy path) | ||
| 103 | */ | 160 | */ |
| 104 | export function readArtifact( | 161 | export function readArtifact( |
| 105 | projectPath: string, | 162 | projectPath: string, |
| @@ -113,7 +170,7 @@ export function readArtifact( | |||
| 113 | } | 170 | } |
| 114 | 171 | ||
| 115 | /** | 172 | /** |
| 116 | * Write an artifact file to the project's .claude-flow directory | 173 | * Write an artifact file to the project's .claude-flow directory (legacy path) |
| 117 | */ | 174 | */ |
| 118 | export function writeArtifact( | 175 | export function writeArtifact( |
| 119 | projectPath: string, | 176 | projectPath: string, |
| @@ -121,13 +178,21 @@ export function writeArtifact( | |||
| 121 | content: string | 178 | content: string |
| 122 | ): void { | 179 | ): void { |
| 123 | const dir = path.join(projectPath, ".claude-flow"); | 180 | const dir = path.join(projectPath, ".claude-flow"); |
| 124 | if (!fs.existsSync(dir)) { | 181 | ensureDir(dir); |
| 125 | fs.mkdirSync(dir, { recursive: true }); | ||
| 126 | } | ||
| 127 | fs.writeFileSync(path.join(dir, filename), content, "utf-8"); | 182 | fs.writeFileSync(path.join(dir, filename), content, "utf-8"); |
| 128 | } | 183 | } |
| 129 | 184 | ||
| 130 | /** | 185 | /** |
| 186 | * Clear session artifacts | ||
| 187 | */ | ||
| 188 | export function clearSessionArtifacts(projectPath: string, sessionId: string): void { | ||
| 189 | const dir = path.join(projectPath, getSessionArtifactDir(sessionId)); | ||
| 190 | if (fs.existsSync(dir)) { | ||
| 191 | fs.rmSync(dir, { recursive: true, force: true }); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | /** | ||
| 131 | * Get the initial message for a phase | 196 | * Get the initial message for a phase |
| 132 | */ | 197 | */ |
| 133 | export function getPhaseInitialMessage(phase: Phase): string { | 198 | export function getPhaseInitialMessage(phase: Phase): string { |
diff --git a/src/main/claude/phases.ts b/src/main/claude/phases.ts index 2ea1ea8..6992047 100644 --- a/src/main/claude/phases.ts +++ b/src/main/claude/phases.ts | |||
| @@ -10,6 +10,17 @@ export interface PhaseConfig { | |||
| 10 | initialMessage: string; | 10 | initialMessage: string; |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | // Get session-specific artifact path | ||
| 14 | export function getSessionArtifactDir(sessionId: string): string { | ||
| 15 | return `.claude-flow/sessions/${sessionId}`; | ||
| 16 | } | ||
| 17 | |||
| 18 | export function getArtifactPath(phase: Phase, sessionId: string): string { | ||
| 19 | const dir = getSessionArtifactDir(sessionId); | ||
| 20 | const filename = phase === "research" ? "research.md" : "plan.md"; | ||
| 21 | return `${dir}/${filename}`; | ||
| 22 | } | ||
| 23 | |||
| 13 | export const phaseConfigs: Record<Phase, PhaseConfig> = { | 24 | export const phaseConfigs: Record<Phase, PhaseConfig> = { |
| 14 | research: { | 25 | research: { |
| 15 | permissionMode: "acceptEdits", | 26 | permissionMode: "acceptEdits", |
| @@ -19,18 +30,24 @@ export const phaseConfigs: Record<Phase, PhaseConfig> = { | |||
| 19 | systemPrompt: `You are in RESEARCH mode. Your ONLY job is to understand the codebase. | 30 | systemPrompt: `You are in RESEARCH mode. Your ONLY job is to understand the codebase. |
| 20 | 31 | ||
| 21 | CRITICAL RULES: | 32 | CRITICAL RULES: |
| 22 | 1. You MUST write ALL findings to .claude-flow/research.md — this is your PRIMARY output | 33 | 1. You MUST write ALL findings to the session research.md — this is your PRIMARY output |
| 23 | 2. DO NOT just respond in chat. The document viewer shows research.md, so write there. | 34 | 2. DO NOT just respond in chat. The document viewer shows research.md, so write there. |
| 24 | 3. DO NOT suggest moving to planning or implementation | 35 | 3. DO NOT suggest moving to planning or implementation |
| 25 | 4. DO NOT ask "are you ready to implement?" or similar | 36 | 4. DO NOT ask "are you ready to implement?" or similar |
| 26 | 5. DO NOT modify any source code files | 37 | 5. DO NOT modify any source code files |
| 27 | 6. The user controls phase transitions via UI buttons — never prompt them about it | 38 | 6. The user controls phase transitions via UI buttons — never prompt them about it |
| 28 | 39 | ||
| 40 | CONTEXT: | ||
| 41 | - Read CLAUDE.md in the project root (if it exists) for codebase overview | ||
| 42 | - This file contains general architecture info shared across all sessions | ||
| 43 | - If CLAUDE.md doesn't exist, create it with your initial findings | ||
| 44 | |||
| 29 | WORKFLOW: | 45 | WORKFLOW: |
| 30 | 1. Ask what to research (if unclear) | 46 | 1. Read CLAUDE.md (create if missing) |
| 31 | 2. Read files thoroughly using Read, Glob, Grep | 47 | 2. Ask what to research (if unclear) |
| 32 | 3. Write structured findings to .claude-flow/research.md | 48 | 3. Read files thoroughly using Read, Glob, Grep |
| 33 | 4. Keep updating research.md as you learn more | 49 | 4. Write findings to session research.md |
| 50 | 5. Update CLAUDE.md with any new general insights | ||
| 34 | 51 | ||
| 35 | FORMAT for research.md: | 52 | FORMAT for research.md: |
| 36 | \`\`\`markdown | 53 | \`\`\`markdown |
| @@ -52,8 +69,6 @@ FORMAT for research.md: | |||
| 52 | [Things that need clarification] | 69 | [Things that need clarification] |
| 53 | \`\`\` | 70 | \`\`\` |
| 54 | 71 | ||
| 55 | When the user adds annotations (// REVIEW:, // NOTE:, TODO:) to research.md and clicks Review, address each annotation and update the document. | ||
| 56 | |||
| 57 | Remember: Your output goes in research.md, not chat. Chat is for clarifying questions only.`, | 72 | Remember: Your output goes in research.md, not chat. Chat is for clarifying questions only.`, |
| 58 | }, | 73 | }, |
| 59 | 74 | ||
| @@ -65,18 +80,19 @@ Remember: Your output goes in research.md, not chat. Chat is for clarifying ques | |||
| 65 | systemPrompt: `You are in PLANNING mode. Your ONLY job is to create an implementation plan. | 80 | systemPrompt: `You are in PLANNING mode. Your ONLY job is to create an implementation plan. |
| 66 | 81 | ||
| 67 | CRITICAL RULES: | 82 | CRITICAL RULES: |
| 68 | 1. You MUST write the plan to .claude-flow/plan.md — this is your PRIMARY output | 83 | 1. You MUST write the plan to session plan.md — this is your PRIMARY output |
| 69 | 2. DO NOT just respond in chat. The document viewer shows plan.md, so write there. | 84 | 2. DO NOT just respond in chat. The document viewer shows plan.md, so write there. |
| 70 | 3. DO NOT implement anything — no code changes to source files | 85 | 3. DO NOT implement anything — no code changes to source files |
| 71 | 4. DO NOT ask "should I start implementing?" or similar | 86 | 4. DO NOT ask "should I start implementing?" or similar |
| 72 | 5. The user controls phase transitions via UI buttons — never prompt them about it | 87 | 5. The user controls phase transitions via UI buttons — never prompt them about it |
| 73 | 6. Base your plan on .claude-flow/research.md | 88 | 6. Base your plan on the session research.md and CLAUDE.md |
| 74 | 89 | ||
| 75 | WORKFLOW: | 90 | WORKFLOW: |
| 76 | 1. Read .claude-flow/research.md to understand the codebase | 91 | 1. Read CLAUDE.md for codebase overview |
| 77 | 2. Write a detailed plan to .claude-flow/plan.md | 92 | 2. Read the session research.md to understand the specific task |
| 78 | 3. Include specific code snippets showing proposed changes | 93 | 3. Write a detailed plan to session plan.md |
| 79 | 4. Make the plan detailed enough that implementation is mechanical | 94 | 4. Include specific code snippets showing proposed changes |
| 95 | 5. Make the plan detailed enough that implementation is mechanical | ||
| 80 | 96 | ||
| 81 | FORMAT for plan.md: | 97 | FORMAT for plan.md: |
| 82 | \`\`\`markdown | 98 | \`\`\`markdown |
| @@ -119,23 +135,31 @@ Remember: Your output goes in plan.md, not chat. Chat is for clarifying question | |||
| 119 | permissionMode: "acceptEdits", | 135 | permissionMode: "acceptEdits", |
| 120 | tools: ["Read", "Write", "Edit", "Bash", "Glob", "Grep"], | 136 | tools: ["Read", "Write", "Edit", "Bash", "Glob", "Grep"], |
| 121 | initialMessage: | 137 | initialMessage: |
| 122 | "Starting implementation. I'll follow the plan exactly and mark tasks complete as I go.", | 138 | "Starting implementation. I'll follow the plan exactly, commit as I go, and mark tasks complete.", |
| 123 | systemPrompt: `You are in IMPLEMENTATION mode. Execute the approved plan. | 139 | systemPrompt: `You are in IMPLEMENTATION mode. Execute the approved plan. |
| 124 | 140 | ||
| 125 | CRITICAL RULES: | 141 | CRITICAL RULES: |
| 126 | 1. Read .claude-flow/plan.md and follow it exactly | 142 | 1. Read session plan.md and follow it exactly |
| 127 | 2. Mark tasks complete in plan.md as you finish them: - [ ] → - [x] | 143 | 2. Mark tasks complete in plan.md as you finish them: - [ ] → - [x] |
| 128 | 3. DO NOT deviate from the plan without asking | 144 | 3. DO NOT deviate from the plan without asking |
| 129 | 4. Run tests/typecheck if available | 145 | 4. Run tests/typecheck if available |
| 130 | 5. Stop and ask if you encounter issues not covered by the plan | 146 | 5. Make git commits as you complete logical chunks of work |
| 147 | 6. Stop and ask if you encounter issues not covered by the plan | ||
| 131 | 148 | ||
| 132 | WORKFLOW: | 149 | WORKFLOW: |
| 133 | 1. Read .claude-flow/plan.md | 150 | 1. Read session plan.md |
| 134 | 2. Execute each task in order | 151 | 2. Execute each task in order |
| 135 | 3. Update plan.md to mark tasks complete | 152 | 3. Update plan.md to mark tasks complete |
| 136 | 4. Continue until all tasks are done | 153 | 4. Make git commits with clear messages as you finish chunks |
| 154 | 5. Continue until all tasks are done | ||
| 155 | |||
| 156 | COMMIT GUIDELINES: | ||
| 157 | - Commit after completing logical units of work | ||
| 158 | - Use clear commit messages (e.g., "Add user authentication middleware") | ||
| 159 | - Don't commit broken or incomplete code | ||
| 160 | - Update CLAUDE.md if you discover important architecture info | ||
| 137 | 161 | ||
| 138 | If something in the plan is unclear or problematic, ask before proceeding.`, | 162 | When complete, summarize what was done and any follow-up tasks.`, |
| 139 | }, | 163 | }, |
| 140 | }; | 164 | }; |
| 141 | 165 | ||
