import path from 'node:path';
import os from 'node:os';
import { exists, readText, writeTextAtomic, PACKAGE_VERSION } from './fsx.js';
export const HERMES_SKILL_NAME = 'sneakoscope-codex';
export function defaultHermesHome(env = process.env) {
    return path.resolve(env.HERMES_HOME || path.join(env.HOME || os.homedir(), '.hermes'));
}
export function defaultHermesSkillDir(env = process.env) {
    return path.join(defaultHermesHome(env), 'skills', HERMES_SKILL_NAME);
}
export function buildHermesSkillFiles(options = {}) {
    const sksCommand = options.sksCommand || 'sks';
    const version = options.version || PACKAGE_VERSION;
    const skillName = options.skillName || HERMES_SKILL_NAME;
    return {
        'SKILL.md': hermesSkillMarkdown({ sksCommand, skillName, version }),
        'README.md': hermesSkillReadme({ sksCommand, skillName, version }),
        'hermes-config.example.yaml': hermesConfigExample({ skillName }),
        'skill-bundle.example.yaml': hermesSkillBundleExample({ skillName })
    };
}
export async function installHermesSkill(options = {}) {
    const targetDir = path.resolve(options.targetDir || defaultHermesSkillDir(options.env || process.env));
    const files = buildHermesSkillFiles(options);
    const existingSkill = path.join(targetDir, 'SKILL.md');
    const existing = await exists(existingSkill);
    if (existing && !options.force) {
        const text = await readText(existingSkill, '');
        if (!text.includes('generated_by: sneakoscope') && !text.includes('Generated by Sneakoscope')) {
            return {
                ok: false,
                status: 'blocked_existing_skill',
                target_dir: targetDir,
                reason: 'Existing Hermes skill is not marked as generated by Sneakoscope. Re-run with --force to overwrite.'
            };
        }
    }
    if (options.dryRun) {
        return { ok: true, status: existing ? 'would_update' : 'would_create', target_dir: targetDir, files: Object.keys(files) };
    }
    for (const [name, content] of Object.entries(files)) {
        await writeTextAtomic(path.join(targetDir, name), content);
    }
    return { ok: true, status: existing ? 'updated' : 'created', target_dir: targetDir, files: Object.keys(files) };
}
function hermesSkillMarkdown({ sksCommand, skillName, version }) {
    return `---
name: ${skillName}
description: Use Sneakoscope Codex (sks) routes, TriWiki, proof/trust, and safety gates from Hermes Agent.
version: ${version}
generated_by: sneakoscope
metadata:
  hermes:
    category: autonomous-ai-agents
    tags: [sks, codex, coding-agent, orchestration, qa, safety]
    requires_toolsets: [terminal]
    config:
      - key: sks.command
        description: Local Sneakoscope Codex command to run from the active workspace.
        default: ${sksCommand}
        prompt: Path or command name for Sneakoscope Codex.
---

# Sneakoscope Codex For Hermes

## LLM Init Prompt For Hermes Agent Builders

\`\`\`text
Set up this Hermes agent project with Sneakoscope Codex. Use [[mandarange/Sneakoscope-Codex](https://github.com/mandarange/Sneakoscope-Codex)] as the repo reference and [npm i -g sneakoscope] as the recommended global install command, then run \`SKS_HERMES=1 ${sksCommand} root --json\`, setup/doctor, Codex App, and proof checks for this repo.
\`\`\`

## When to Use

Use this skill when a Hermes Agent is operating in a repository that mentions Sneakoscope Codex, SKS, \`sks\`, Team, QA-LOOP, TriWiki, Context7, DB safety, Completion Proof, Trust Kernel, Honest Mode, or \`$...\` SKS prompt routes.

Hermes exposes installed skills as slash commands, so users can run \`/${skillName} <task>\` from the CLI or any messaging gateway.

## Required Tool Access

The Hermes agent needs the \`terminal\` toolset or an equivalent shell/command tool. Prefer running commands in the target repository root.

Set \`SKS_HERMES=1\` for SKS shell commands. Hermes mode lets SKS treat dependency/update prompts as non-interactive agent prompts, matching the OpenClaw agent mode without changing database or destructive-operation safety rules.

## Procedure

1. Run \`SKS_HERMES=1 ${sksCommand} root --json\` from the target workspace.
2. Read \`AGENTS.md\` if present, then run \`SKS_HERMES=1 ${sksCommand} status --json\`.
3. Discover available surfaces with \`SKS_HERMES=1 ${sksCommand} commands --json\` and \`SKS_HERMES=1 ${sksCommand} dollar-commands --json\`.
4. For broad implementation, use the repo's selected SKS route. General code work normally belongs on Team; tiny direct edits may use DFix; UI/API dogfood belongs on QA-LOOP; DB/Supabase work belongs on DB safety routes.
5. Before coding, preserve existing user changes. Do not reset the worktree or overwrite unrelated files.
6. After edits, run the smallest relevant tests, then run \`${sksCommand} proof validate --json\` or \`${sksCommand} trust validate latest --json\` when a mission exists.

## Safety Rules

- Do not invent fallback implementation code when SKS reports that a requested path is blocked. Report the blocker and the source artifact instead.
- Database, migration, Supabase, and credential work stays read-only unless the user explicitly authorizes a scoped write path.
- \`$MAD-SKS\` or \`sks --mad\` is a temporary high-risk permission modifier only when the user explicitly invokes it. It does not remove catastrophic safeguards.
- Mock, fixture, or static evidence must be reported as partial verification, never as live runtime proof.

## Useful Commands

- \`${sksCommand} root --json\`
- \`${sksCommand} status --json\`
- \`${sksCommand} commands --json\`
- \`${sksCommand} dollar-commands --json\`
- \`${sksCommand} deps check --json\`
- \`${sksCommand} codex compatibility --json\`
- \`${sksCommand} proof-field scan --intent "<task>" --changed file1,file2 --json\`
- \`${sksCommand} proof validate --json\`
- \`${sksCommand} trust validate latest --json\`

## Pitfalls

- SKS prompt routes such as \`$Team\` are prompt commands in Codex App, not shell commands. In Hermes terminal workflows, prefer the corresponding \`sks team\`, \`sks qa-loop\`, \`sks dfix\`, \`sks db\`, or \`sks pipeline\` command.
- If Hermes runs in a sandbox, grant shell access only for trusted workspaces and keep SKS DB safety gates enabled.
- If \`latest\` moved to a fixture mission, pin the explicit mission id from \`${sksCommand} status --json\` before final proof/trust closeout.

## Verification

The minimum healthy check is:

\`\`\`sh
SKS_HERMES=1 ${sksCommand} root --json
SKS_HERMES=1 ${sksCommand} commands --json
SKS_HERMES=1 ${sksCommand} status --json
\`\`\`

For code changes, add the route-specific tests and proof/trust validation before claiming completion.
`;
}
function hermesSkillReadme({ sksCommand, skillName, version }) {
    return `# ${skillName}

Version: ${version}

Generated by Sneakoscope Codex for Hermes Agent.

This Hermes skill lets Hermes discover and use Sneakoscope Codex through the local \`${sksCommand}\` command. It installs as \`/${skillName}\` and assumes the Hermes \`terminal\` toolset is available.

## LLM Init Prompt

\`\`\`text
Set up this Hermes agent project with Sneakoscope Codex. Use [[mandarange/Sneakoscope-Codex](https://github.com/mandarange/Sneakoscope-Codex)] as the repo reference and [npm i -g sneakoscope] as the recommended global install command, then run \`SKS_HERMES=1 ${sksCommand} root --json\`, setup/doctor, Codex App, and proof checks for this repo.
\`\`\`

## Install

\`\`\`sh
${sksCommand} hermes install
\`\`\`

The default target is \`~/.hermes/skills/${skillName}/\`.

## Verify From Hermes

\`\`\`sh
hermes skills list | grep ${skillName}
SKS_HERMES=1 ${sksCommand} root --json
SKS_HERMES=1 ${sksCommand} commands --json
\`\`\`

Then start a fresh Hermes session and invoke \`/${skillName} Use SKS in this repository.\`

## Optional External Skill Directory

If you keep SKS/Codex skills in a shared folder, add it under \`skills.external_dirs\` in \`~/.hermes/config.yaml\`. Hermes can update writable external skill directories, so use filesystem permissions when shared skills must remain read-only. See \`hermes-config.example.yaml\`.
`;
}
function hermesConfigExample({ skillName }) {
    return `# Optional Hermes config snippet for Sneakoscope Codex.
# The installed skill under ~/.hermes/skills/${skillName}/ works without this.

skills:
  external_dirs:
    - ~/.agents/skills

# Hermes may update writable external skill directories. Use filesystem
# permissions if shared skills must remain read-only.
#
# For terminal sessions, run SKS commands with:
#   SKS_HERMES=1 sks status --json
`;
}
function hermesSkillBundleExample({ skillName }) {
    return `# Optional ~/.hermes/skill-bundles/sks.yaml
name: sks
description: Load Sneakoscope Codex operating rules for SKS-enabled repositories.
skills:
  - ${skillName}
`;
}
//# sourceMappingURL=hermes.js.map