import type { IdeId } from '../ide/ide-types.js'; import type { HookScope } from '../ide/shared/safe-path.js'; /** * Install (and remove) the Peaks-managed hooks in an IDE's settings.json. * * The hook runs `peaks gate enforce` (Claude) or `peaks hook handle` (Trae / * other future adapters) before every relevant tool call; when a SOP guard's * gates fail it returns the adapter-specific deny shape, which blocks the * tool call BEFORE the IDE's permission checks — making the gate * un-bypassable by the agent. * * Slice #1 refactor: this service delegates to the `IdeAdapter` for * `claude-code`. Slice #2 added Trae. Adapter provides `dirName` / * `settingsFileName` / `envVar` / `hookEvent` / `toolMatcher`. The Claude * install path is byte-level-compat with slice #0 (AC-1). * * Slice #3 refactor (this commit): the service is now per-IDE aware via an * optional `options.ide` parameter. The CLI command is responsible for * resolving the IDE (env → stdin shape → cwd → fallback to 'claude-code') * via `detectIdeFromContext` and passing the result here. When `ide` is * omitted, the service defaults to `'claude-code'` so existing tests and * downstream callers continue to work without modification. * * Installation is an EXPLICIT user command (never postinstall): skills describe, * the CLI performs side effects. Writes preserve all other settings keys and * any other hooks, reject symlinked targets, and use an atomic rename so a * partial write can never corrupt the settings file. Our entry is merged into * (not replacing) the existing `hooks.` array and is identified by a * sentinel substring in its command, so install is idempotent and uninstall * removes only our own entry. */ export type { HookScope } from '../ide/shared/safe-path.js'; export type HookInstallOptions = { /** * Which IDE's adapter to install for. Defaults to `'claude-code'` for * backward compatibility. The CLI command should resolve this from * `detectIdeFromContext({ env, cwd, parsedStdin })` and pass the result. * Throws if the IDE is not registered in the adapter registry. */ readonly ide?: IdeId; /** * Slice #013 (bugfix — peaks hooks install --no-progress): when `true`, * skip emitting the progress-start PreToolUse hook entry while still * installing the gate-enforce entry. The progress-start hook auto-spawns * a new terminal running `peaks progress watch`; with dispatch + * heartbeat (slice #009 + #010) that auto-spawn is dead weight. Default * `false` preserves the pre-slice install shape (both entries). The * sentinel-based install is idempotent: re-running with `skipProgress: * true` over a settings.json that previously had the progress entry * installed will remove that entry. `uninstall` honors the same flag * so it can find and remove both entries when both are present and * only the gate-enforce entry when only the gate-enforce is present. * * Slice #014 (refactor — full removal of legacy progress-start surface): * the field is preserved for API stability, but the underlying * install only ever emits the gate-enforce entry. The progress-start * entry is no longer installed regardless of this flag's value. The * legacy `peaks progress start|watch|close` CLI surface is gone * (replaced by `peaks sub-agent dispatch|heartbeat|share`); the hook * entry would have been pointing at a `peaks progress start` that no * longer exists. The sentinel `peaks progress start` constant is still * exported (some tests + back-compat reads rely on it) but no new * hook entries use it. */ readonly skipProgress?: boolean; }; /** Sentinel substring identifying a Claude-Code gate-enforce hook entry. */ export declare const HOOK_ENFORCE_SENTINEL = "peaks gate enforce"; /** Default (claude-code) hook command — kept as a stable export for tests. */ export declare const HOOK_ENFORCE_COMMAND = "peaks gate enforce --project \"${CLAUDE_PROJECT_DIR}\""; export type HookInstallPlan = { scope: HookScope; settingsPath: string; exists: boolean; alreadyInstalled: boolean; desiredCommand: string; sentinel: string; matcher: string; }; export type HookInstallResult = HookInstallPlan & { applied: boolean; }; export type HookRemoveResult = { scope: HookScope; settingsPath: string; removed: boolean; }; export type HookStatus = { scope: HookScope; settingsPath: string; exists: boolean; installed: boolean; }; /** * Slice #014: read the *actually-installed* peaks-managed hook entries * from a settings object. Replaces the pre-#014 `listInstalledEntriesForIde` * helper in `hooks-commands.ts`, which returned the IDE-EXPECTED list * (a hardcoded 2-entry array per adapter) rather than what was on disk. * That bug surfaced when slice #013's local cleanup installed * `peaks hooks install --no-progress` (gate-enforce only), but the * status command still reported `entries: [Bash, Task]` because the * helper didn't read the file. * * The new helper: * 1. reads each `hooks.` array, * 2. filters to entries that are peaks-managed for the given IDE * (matches the legacy sentinel set: gate-enforce + the no-longer- * installed progress-start), * 3. returns one `{ matcher, sentinel }` row per entry, taking the * FIRST matching sentinel per entry (entries have a single command * handler in practice, but the loop tolerates multi-handler * entries by taking the first match). * * Pre-#014 settings.json files that have a stale progress-start entry * will see it surface in the result. This is intentional: the status * command is the user's tool for "what is on disk right now", and * surfacing a stale entry is the only way the user can know to run * `peaks hooks install` (which now strips it) or `peaks hooks * uninstall` (which removes it). */ export declare function readInstalledEntriesFromSettings(settings: Record, ide: IdeId): ReadonlyArray<{ matcher: string; sentinel: string; }>; /** A typed descriptor for a single peaks-managed hook entry. */ export type PeaksHookEntry = { sentinel: string; matcher: string; command: string; event: string; }; /** Default (claude-code) peaks-managed hook entries — kept as a stable export for tests. Slice #014: only the gate-enforce entry. */ export declare const PEAKS_HOOK_ENTRIES: ReadonlyArray; export declare function planHookInstall(scope: HookScope, projectRoot?: string, options?: HookInstallOptions): HookInstallPlan; export declare function applyHookInstall(scope: HookScope, projectRoot?: string, options?: HookInstallOptions): HookInstallResult; export declare function removeHookInstall(scope: HookScope, projectRoot?: string, options?: HookInstallOptions): HookRemoveResult; export declare function readHookStatus(scope: HookScope, projectRoot?: string, options?: HookInstallOptions): HookStatus;