mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* refactor: organize agent hook installation and server owners * refactor: align hook mock registries and packaged entries
124 lines
5.2 KiB
TypeScript
124 lines
5.2 KiB
TypeScript
import { buildHookProcessCapture } from '../agent-hooks/installation/scripts/hook-process-capture'
|
|
import type { AgentHookSource } from '../../shared/agent-hook-listener/transport/agent-hook-relay'
|
|
import { buildWindowsAgentHookCurlPostCommand } from '../agent-hooks/installation/commands/windows-agent-hook-post-command'
|
|
import {
|
|
buildPosixAgentHookPostCommand,
|
|
buildPosixHookMetadataCacheLines
|
|
} from '../agent-hooks/installation/scripts/hook-post-command'
|
|
import {
|
|
buildPosixScrubSafeEnvRestoreLines,
|
|
buildWindowsScrubSafeEnvRestoreLines
|
|
} from '../../shared/agent-hook-listener/transport/agent-hook-scrub-safe-env'
|
|
import {
|
|
buildPosixCursorReplayGuardLines,
|
|
buildWindowsCursorReplayGuardLines
|
|
} from '../agent-hooks/installation/scripts/cursor-replay-guard'
|
|
import {
|
|
buildPosixGrokReplayGuardLines,
|
|
buildWindowsGrokReplayGuardLines
|
|
} from '../agent-hooks/installation/scripts/grok-replay-guard'
|
|
import {
|
|
buildPosixHookPayloadCapture,
|
|
buildPosixHookSpoolLines,
|
|
buildWindowsHookEnvironmentGuardLines,
|
|
POSIX_HOOK_STDIN_EXEC
|
|
} from '../agent-hooks/installation/scripts/hook-stdin-contract'
|
|
import {
|
|
buildPosixClaudeDecisionModeLines,
|
|
buildPosixClaudeDecisionPreludeLines,
|
|
wrapPosixClaudeDecisionPost
|
|
} from './claude-hook-decision-script'
|
|
import {
|
|
WINDOWS_CLAUDE_BACKGROUND_JOB_GUARD,
|
|
buildPosixClaudeBackgroundJobGuardLines
|
|
} from '../agent-hooks/installation/scripts/claude-background-job-guard'
|
|
|
|
export function getManagedScript(
|
|
target: 'local' | 'posix' = 'local',
|
|
options: {
|
|
source?: AgentHookSource
|
|
skipWhenDevinImportsClaude?: boolean
|
|
skipWhenGrokImportsClaude?: boolean
|
|
skipWhenCursorImportsClaude?: boolean
|
|
// Only Claude Code itself reads PreToolUse decisions; compatible CLIs keep print-then-post.
|
|
decidesPreToolUse?: boolean
|
|
} = {}
|
|
): string {
|
|
const source = options.source ?? 'claude'
|
|
if (target === 'local' && process.platform === 'win32') {
|
|
return [
|
|
'@echo off',
|
|
'setlocal',
|
|
// Permission hooks fail closed on empty stdout, including skip paths.
|
|
'echo {}',
|
|
// CLAUDE_CODE_SUBPROCESS_ENV_SCRUB drops ORCA_PANE_KEY; restore it before any guard reads it.
|
|
...buildWindowsScrubSafeEnvRestoreLines(),
|
|
// Refresh endpoint coordinates for PTYs surviving an Orca restart.
|
|
'if defined ORCA_AGENT_HOOK_ENDPOINT if exist "%ORCA_AGENT_HOOK_ENDPOINT%" call "%ORCA_AGENT_HOOK_ENDPOINT%" 2>nul',
|
|
// Outside an Orca pane the caller may abandon stdin; exit before owning it (#11549).
|
|
...buildWindowsHookEnvironmentGuardLines(),
|
|
WINDOWS_CLAUDE_BACKGROUND_JOB_GUARD,
|
|
...(options.skipWhenGrokImportsClaude ? buildWindowsGrokReplayGuardLines() : []),
|
|
...(options.skipWhenCursorImportsClaude ? buildWindowsCursorReplayGuardLines() : []),
|
|
...(options.skipWhenDevinImportsClaude
|
|
? [
|
|
// Devin imports Claude hooks; skip duplicate posts to preserve Devin attribution.
|
|
'if not "%DEVIN_PROJECT_DIR%"=="" exit /b 0'
|
|
]
|
|
: []),
|
|
// Avoid a PowerShell startup per hook.
|
|
buildWindowsAgentHookCurlPostCommand(source),
|
|
'exit /b 0',
|
|
''
|
|
].join('\r\n')
|
|
}
|
|
|
|
const decides = options.decidesPreToolUse === true
|
|
return [
|
|
'#!/bin/sh',
|
|
// Permission hooks fail closed on empty stdout, including skip paths.
|
|
...(decides ? buildPosixClaudeDecisionPreludeLines() : ['printf "{}\\n"']),
|
|
// CLAUDE_CODE_SUBPROCESS_ENV_SCRUB drops ORCA_PANE_KEY; restore it before any guard or spool reads it.
|
|
...buildPosixScrubSafeEnvRestoreLines(),
|
|
...buildPosixHookPayloadCapture('exit', POSIX_HOOK_STDIN_EXEC),
|
|
...(decides ? buildPosixClaudeDecisionModeLines() : []),
|
|
...(options.skipWhenGrokImportsClaude ? buildPosixGrokReplayGuardLines() : []),
|
|
...(options.skipWhenCursorImportsClaude ? buildPosixCursorReplayGuardLines() : []),
|
|
...buildPosixHookSpoolLines(source),
|
|
...(options.skipWhenDevinImportsClaude
|
|
? [
|
|
// Devin imports Claude hooks; skip duplicate posts to preserve Devin attribution.
|
|
'if [ -n "$DEVIN_PROJECT_DIR" ]; then',
|
|
' exit 0',
|
|
'fi'
|
|
]
|
|
: []),
|
|
...buildPosixClaudeBackgroundJobGuardLines(),
|
|
...(source === 'claude' ? buildHookProcessCapture() : []),
|
|
// Surviving PTYs need fresh endpoint coordinates; suppress errors and survive outer set -e.
|
|
'if [ -n "$ORCA_AGENT_HOOK_ENDPOINT" ] && [ -r "$ORCA_AGENT_HOOK_ENDPOINT" ]; then',
|
|
' unset ORCA_AGENT_HOOK_TRANSPORT',
|
|
' . "$ORCA_AGENT_HOOK_ENDPOINT" 2>/dev/null || :',
|
|
'fi',
|
|
'if [ -z "$ORCA_AGENT_HOOK_PORT" ] || [ -z "$ORCA_AGENT_HOOK_TOKEN" ] || [ -z "$ORCA_PANE_KEY" ]; then',
|
|
' spool_hook_event',
|
|
' exit 0',
|
|
'fi',
|
|
...buildPosixHookMetadataCacheLines(),
|
|
// Keep hook JSON off argv and avoid URL-encoded paths that trigger IDS rules.
|
|
...(decides
|
|
? wrapPosixClaudeDecisionPost(
|
|
buildPosixAgentHookPostCommand(source, {
|
|
decisionHeaderVar: 'orca_hook_decides',
|
|
cachedMetadata: true
|
|
})
|
|
)
|
|
: buildPosixAgentHookPostCommand(source, { cachedMetadata: true }).map(
|
|
(line, index, lines) =>
|
|
index === lines.length - 1 ? `${line} >/dev/null 2>&1 || spool_hook_event` : line
|
|
)),
|
|
'exit 0',
|
|
''
|
|
].join('\n')
|
|
}
|