// tools/init-project/agents-md-writer.ts — SPEC-518: Generate AGENTS.md and .cursorrules
// so Planu SDD workflow is available in IDEs that don't read CLAUDE.md
// SPEC-972: Added host-aware tool filtering for Codex and other MCP hosts.
import { writeFile, mkdir, access } from 'node:fs/promises';
import { join } from 'node:path';
import { assertEnglishOnlyArtifactText } from '../../engine/spec-language/english-only.js';
import { CORE_AGENTS_MD_TOOLS, loadHostRegistry, filterToolsByHost, formatToolsForAgentsMd, } from '../../engine/host-tool-filter.js';
const AGENTS_MD_PATH = 'AGENTS.md';
const CURSORRULES_PATH = '.cursorrules';
const WINDSURFRULES_PATH = '.windsurfrules';
/** Build AGENTS.md content with an optional host-filtered tool list. */
async function buildAgentsMdContent(host) {
    let toolsTable = '| Goal | Tool |\n|------|------|\n' +
        '| New feature | `facilitate("description")` |\n' +
        '| Project status | `planu_status` |\n' +
        '| List specs | `list_specs` |\n' +
        '| Mark done | `update_status` → `done` |\n' +
        '| Verify | `validate` |';
    if (host !== undefined && host !== 'universal') {
        const registry = await loadHostRegistry(host);
        const result = filterToolsByHost(CORE_AGENTS_MD_TOOLS, host, registry);
        if (result.included.length > 0 || result.restricted.length > 0) {
            toolsTable = formatToolsForAgentsMd(result, { prefix: 'mcp__planu__', tableFormat: true });
        }
    }
    return `# Planu SDD — AI Agent Instructions

Auto-generated by \`init_project\`. Mirrors CLAUDE.md for IDEs that read AGENTS.md.

## Mandatory Workflow

**ALWAYS follow Spec Driven Development. Never write code without an approved spec.**

\`\`\`
facilitate("what to build") → create_spec → challenge_spec → check_readiness
→ update_status(review) → approve → update_status(approved) → implement
→ validate → update_status(done)
\`\`\`

## Session Start

1. Call \`planu_status\` to see active specs
2. Resume any \`implementing\` spec before starting new work

## Session End

- Call \`session_checkpoint\` to save context
- Call \`update_status(done)\` if work is complete

## Quick Reference

${toolsTable}

## Rules

- NEVER write production code before spec is \`approved\`
- NEVER skip \`validate\` after implementation
- NEVER create design docs outside Planu specs

## Skill Discovery

Use find-skills to discover available workflows.
`;
}
const CURSORRULES_CONTENT = `# Planu SDD — Cursor Rules

Auto-generated by init_project.

## Mandatory

1. Before writing any code, call facilitate() or create_spec() first.
2. Never write code without an approved Planu spec.
3. Always call validate() after implementation.
4. Use update_status() to track progress: draft → review → approved → implementing → done.

## Session

- Start: call planu_status to see active work
- End: call session_checkpoint to save context

## Tools

- New feature: facilitate("what to build")
- Check status: planu_status
- Mark done: update_status(done) then validate
`;
async function writeIfMissing(path, content) {
    try {
        await access(path);
        return false;
    }
    catch {
        assertEnglishOnlyArtifactText(content, 'agent');
        await writeFile(path, content, 'utf-8');
        return true;
    }
}
/**
 * SPEC-518: Generate AGENTS.md, .cursorrules, and .windsurfrules if missing.
 * Idempotent — skips files that already exist.
 *
 * SPEC-972: When host is provided (e.g. 'codex'), the AGENTS.md tool list is
 * filtered by host capability. Unsupported tools are omitted; restricted tools
 * include a warning comment.
 */
export async function generateAgentFilesIfMissing(projectPath, host) {
    const agentsMdPath = join(projectPath, AGENTS_MD_PATH);
    const cursorPath = join(projectPath, CURSORRULES_PATH);
    const windsurfPath = join(projectPath, WINDSURFRULES_PATH);
    await mkdir(projectPath, { recursive: true });
    const agentsMdContent = await buildAgentsMdContent(host);
    const [agentsMdWritten, cursorRulesWritten, windsurfRulesWritten] = await Promise.all([
        writeIfMissing(agentsMdPath, agentsMdContent),
        writeIfMissing(cursorPath, CURSORRULES_CONTENT),
        writeIfMissing(windsurfPath, CURSORRULES_CONTENT),
    ]);
    return { agentsMdWritten, cursorRulesWritten, windsurfRulesWritten };
}
//# sourceMappingURL=agents-md-writer.js.map