fix(omp): report approvals without claiming Claude's hook set

Review follow-up. omp declared `hooks: 'always'`, which every consumer reads
as Claude's full hook set, so an omp session picked up behaviour it cannot
back. omp goes back to `hooks: 'none'`; `capabilities.approvalHookFile` is now
a separate, narrower capability with its own predicate,
`approvalHookAvailableForMode()` (local, non-adopted sessions only).

- An explicit `stop` wait on omp is a 400 again; `blocked` still works.
- omp inbox items are alert-only (`answerInTerminal`, no `approvalId` on the
  push), and only `permission_prompt` / `elicitation_complete` are accepted for
  an approval-only session.
- The module is installed from POST /api/quick-start too (new and existing
  cases, not Docker or remote) and on reboot-restore, after the launch
  pre-flight on both create paths. Claude's settings.local.json block and the
  Docker CLAUDE.md no longer land in omp workspaces.
- The generated module passes Bun's `tls: { rejectUnauthorized: false }` for an
  https CODEMAN_API_URL (checked against a `--https` server), clears the alert
  only when the last pending toolCallId resolves, sends reports in order,
  carries omp's policy `reason` as `message`, and imports HookAPI from
  `@oh-my-pi/pi-coding-agent/extensibility/hooks`.
- Every path component and the target file are lstat-checked.
- docs/omp-integration.md, verbs.md and the stock.ts comments updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
timkjrandClaude Opus 5.5 committed 2026-11-10 10:03:38 -06:00
1 parent 0bbf483a35
commit 27d6e2e30a
14 files changed
+701 -125

No files matched your search

+58 -18
View File
@@ -1690,38 +1690,56 @@ export const OMP_APPROVAL_HOOK_FILE = 'codeman-approval.ts';
/**
* Source of the hook that turns omp's own approval events into Codeman hook events.
*
* WHY this exists: omp declares `capabilities.hooks: 'always'` (stock registry) but
* installs no Claude-style `settings.local.json` hook block — its hooks are modules.
* Without this file `hooksAvailableForMode()` is true while nothing ever POSTs, so
* `/api/approvals` stays empty, the tab never goes red and no notification fires: an
* omp pane asking for tool approval is silent. Claude solves the same problem by
* writing a `Notification: permission_prompt` block into `settings.local.json`.
* WHY this exists: omp installs no Claude-style `settings.local.json` hook block — its
* hooks are modules — so without this file an omp pane asking for tool approval is
* silent: `/api/approvals` stays empty, the tab never goes red and no notification
* fires. The registry declares it through `capabilities.approvalHookFile`, NOT
* `hooks: 'always'` (which would also promise `stop` and answerable dialogs).
*
* The mapping mirrors Claude's names exactly, because the server routes on them
* The mapping uses Claude's event names, because the server routes on them
* (`APPROVAL_KIND_BY_EVENT` in web/routes/hook-event-routes.ts):
* - `tool_approval_requested` -> `permission_prompt` (opens the inbox item)
* - `tool_approval_requested` -> `permission_prompt` (opens an alert-only inbox item)
* - `tool_approval_resolved` -> `elicitation_complete` (resolves it, whether the user
* approved or denied, so a denied dialog does not leave a red tab forever)
*
* Several calls in one batch can each wait on their own prompt, and the inbox keeps one
* item per session, so the module tracks pending `toolCallId`s and reports the close only
* when the last one resolves. That set drains because omp pairs every request with a
* resolve, an aborted prompt included (its approval wrapper emits `approved: false` from
* the catch, omp 18.18.6 `extensibility/extensions/wrapper.ts`); only omp dying mid-prompt
* skips it, which leaves the alert up exactly as it would without the set. Reports go out
* in order (one chain), so a fast approve cannot overtake the prompt that opened it.
*
* `reason` is omp's POLICY note (a restricted folder, a critical-pattern match), not the
* command, and is absent on an ordinary mode-raised prompt, so it rides `message`.
*
* HTTPS: `CODEMAN_API_URL` is loopback HTTPS with a self-signed certificate on `--https`
* and tailscale installs (Codeman's own hook curl passes `-k`, the DeepSeek shim sets
* `rejectUnauthorized: false`). omp runs on Bun, whose fetch verifies certificates by
* default, so an `https:` URL gets Bun's `tls: { rejectUnauthorized: false }`.
*
* ⚠️ Fails OPEN and SILENT, like the memory-gate hook: a hook that throws would break
* the approval flow it exists to report. Every path catches, and nothing is awaited
* unbounded — the fetch is fire-and-forget with a short AbortSignal.
* unbounded — each POST carries a short AbortSignal and the handlers never await it.
*/
export function generateOmpApprovalHook(): string {
return `// ${OMP_APPROVAL_HOOK_MARKER} — auto-generated by Codeman; safe to delete, rewritten when a session is created.
//
// Reports omp's tool-approval dialogs to Codeman's /api/hook-event so an omp pane
// raises a tab alert and a desktop notification exactly like a claude pane does.
// raises a tab alert and a desktop notification. Answer the dialog in the terminal.
// Codeman owns this file: a foreign file of the same name is never overwritten.
import { readFile } from "node:fs/promises";
import type { HookAPI } from "@oh-my-pi/pi-coding-agent";
import type { HookAPI } from "@oh-my-pi/pi-coding-agent/extensibility/hooks";
const API_URL = process.env.CODEMAN_API_URL;
const SESSION_ID = process.env.CODEMAN_SESSION_ID;
const SECRET_FILE = process.env.CODEMAN_HOOK_SECRET_FILE;
// Loopback HTTPS (--https, tailscale) uses a self-signed certificate, which Bun's fetch
// refuses by default; Codeman's own hook curl passes -k for the same reason.
const TLS = API_URL?.startsWith("https:") ? { tls: { rejectUnauthorized: false } } : {};
/** POST one Codeman hook event. Never throws, never awaited past its timeout. */
async function report(event: string, data: Record<string, unknown>): Promise<void> {
/** POST one Codeman hook event. Never throws; bounded by its timeout. */
async function send(event: string, data: Record<string, unknown>): Promise<void> {
if (!API_URL || !SESSION_ID) return;
try {
const secret = SECRET_FILE ? (await readFile(SECRET_FILE, "utf8")).trim() : "";
@@ -1730,22 +1748,38 @@ async function report(event: string, data: Record<string, unknown>): Promise<voi
headers: { "Content-Type": "application/json", "X-Codeman-Hook-Secret": secret },
body: JSON.stringify({ event, sessionId: SESSION_ID, data }),
signal: AbortSignal.timeout(5000),
...TLS,
});
} catch {
// Best effort by contract: a failed report must never disturb the dialog itself.
}
}
// One chain, so reports arrive in the order omp raised them: a fast approve must not
// overtake the prompt it answers and leave the alert up.
let chain: Promise<void> = Promise.resolve();
function report(event: string, data: Record<string, unknown>): void {
chain = chain.then(() => send(event, data));
}
export default function codemanApprovalHook(pi: HookAPI): void {
// Calls in one batch can each wait on their own prompt, and Codeman keeps one alert per
// session, so it is cleared only when the LAST pending approval resolves.
const pending = new Set<string>();
pi.on("tool_approval_requested", (event) => {
void report("permission_prompt", {
pending.add(event.toolCallId);
// \`reason\` is omp's policy note (restricted folder, critical pattern), not the command.
report("permission_prompt", {
tool_name: event.toolName,
tool_input: event.reason ? { description: event.reason } : undefined,
...(event.reason ? { message: event.reason } : {}),
});
});
pi.on("tool_approval_resolved", (event) => {
void report("elicitation_complete", {
pending.delete(event.toolCallId);
if (pending.size > 0) return;
report("elicitation_complete", {
tool_name: event.toolName,
approved: event.approved,
});
@@ -1762,7 +1796,9 @@ export type ApprovalHookApplyResult = 'installed' | 'refreshed' | 'unchanged' |
* Ownership: the file carries `OMP_APPROVAL_HOOK_MARKER` in its first line. A file
* WITHOUT the marker is somebody else's hook and is left untouched (`foreign`) — the
* same posture `installAgentSkillInto` takes, so Codeman never clobbers user work.
* A symlinked directory is refused rather than written through.
* EVERY component below the workspace (`.claude`, `.claude/hooks`, `.claude/hooks/pre`)
* and the target file itself is `lstat`ed, and a symlink anywhere is refused rather than
* written through: a linked `.claude/hooks` once put `pre/<file>` in the link's target.
*
* Idempotent: an unchanged file is not rewritten (`unchanged`), so this is safe to call
* on every omp session create.
@@ -1771,7 +1807,11 @@ export async function applyApprovalHookFile(workspace: string, fileName: string)
const hookDir = join(workspace, OMP_APPROVAL_HOOK_DIR);
const target = join(hookDir, fileName);
return withSettingsLock(target, async () => {
if ((await isSymlink(hookDir)) || (await isSymlink(join(workspace, '.claude')))) return 'symlink';
let component = workspace;
for (const part of [...OMP_APPROVAL_HOOK_DIR.split('/'), fileName]) {
component = join(component, part);
if (await isSymlink(component)) return 'symlink';
}
let existing: string | null = null;
try {