// tools/init-project/planu-workflow-generator.ts — Generate Planu SDD Workflow section for CLAUDE.md
// SPEC-263: Autonomous CLAUDE.md that makes Planu mandatory
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { dirname } from 'node:path';
import { assertEnglishOnlyArtifactText } from '../../engine/spec-language/english-only.js';
const PLANU_WORKFLOW_MARKER = '<!-- planu-sdd-workflow -->';
const PLANU_WORKFLOW_CONTENT = `<!-- planu-sdd-workflow -->
## Planu SDD Workflow (MANDATORY)

### Session Start
1. Call \`planu_status\` — load active specs and git state
2. If spec is \`implementing\` → resume it before starting new work
3. Call \`semantic_search\` before starting any new task

### For ANY task > 5 minutes
NEVER write production code without an approved spec.
1. Call \`facilitate\` with what the user wants to build
2. Planu creates a spec — user reviews and approves
3. Only THEN implement

### Session End
Call \`session_checkpoint\` to save context for the next session.

### For Multiple Specs (3+ parallel work)
When implementing multiple independent specs simultaneously, use Agent Teams:
1. Call \`plan_team_distribution\` with spec IDs → auto-assigns file ownership
2. Call \`generate_teammate_prompt\` for each teammate → isolated context per agent
3. Spawn N Claude Code agents with the generated prompts
4. Call \`validate_team_results\` after completion → verify merged code

Use teams when: 3+ specs ready, estimated effort >8h, monorepo with clear module boundaries.
Quick ref: parallel work → \`plan_team_distribution\` → teammates → \`validate_team_results\`

### Quick reference
- New feature → \`facilitate("description")\`
- Check progress → \`planu_status\`
- Find related work → \`semantic_search("keywords")\`
- List pending → \`list_specs\` with status filter
- Parallel work → \`plan_team_distribution\`
<!-- /planu-sdd-workflow -->`;
/**
 * Generate the Planu SDD section for CLAUDE.md.
 */
export function generatePlanuSection(_projectPath, _projectId) {
    return {
        marker: PLANU_WORKFLOW_MARKER,
        content: PLANU_WORKFLOW_CONTENT,
    };
}
/**
 * Inject or update the Planu SDD section in an existing CLAUDE.md.
 * - If the opening marker exists → replace the whole block (idempotent update).
 * - If not → append the block.
 * Creates the file (and parent directories) if it does not exist.
 */
export async function injectPlanuSection(claudeMdPath, section) {
    let existing = '';
    try {
        existing = await readFile(claudeMdPath, 'utf-8');
    }
    catch {
        await mkdir(dirname(claudeMdPath), { recursive: true });
    }
    const updated = injectOrReplacePlanuBlock(existing, section.content, section.marker);
    assertEnglishOnlyArtifactText(section.content, 'agent');
    await writeFile(claudeMdPath, updated, 'utf-8');
}
/**
 * Pure string helper — replace block between markers or append it.
 * Exported for unit testing.
 */
export function injectOrReplacePlanuBlock(existing, block, openMarker) {
    // Derive close marker: <!-- planu-sdd-workflow --> → <!-- /planu-sdd-workflow -->
    const closeMarker = openMarker.replace('<!-- ', '<!-- /');
    if (existing.includes(openMarker)) {
        // Replace everything from openMarker to closeMarker (inclusive)
        const escapedOpen = RegExp.escape(openMarker);
        const escapedClose = RegExp.escape(closeMarker);
        const re = new RegExp(`${escapedOpen}[\\s\\S]*?${escapedClose}`, 'g');
        return existing.replace(re, block);
    }
    // Append
    const separator = existing.length > 0 && !existing.endsWith('\n') ? '\n\n' : '\n';
    return existing + separator + block;
}
//# sourceMappingURL=planu-workflow-generator.js.map