Files
orca/src/main/devin/hook-settings.ts
T
OrcaWin ee709b02ed fix(agent-hooks): make Windows cmd hook launcher directly spawnable (#8430 regression) (#8737)
Codex, Antigravity, and Devin launch their agent-hook `command` as a program
(argv[0]), not through cmd.exe. PR #8430 changed wrapWindowsCmdHookCommand to
emit an `if exist "path\." (drain) else if exist "path" (call "path") else
(drain)` compound whose argv[0] is the cmd builtin `if` — unspawnable — so every
Codex/Antigravity/Devin hook (SessionStart, UserPromptSubmit, Stop, ...) failed
with "hook exited with code 1" on Windows starting in v1.4.138.

Revert the cmd-safe fast path to the bare, directly-spawnable .cmd path (the
proven pre-#8430 form). A cmd-builtin drain and direct-spawnability are mutually
exclusive, and nesting cmd.exe /d /c breaks large-payload draining; the
missing-script stdin drain stays on the encoded-PowerShell fallback (used for
spaced/non-ASCII paths). Upgrades self-heal on first launch: startup install()
unconditionally rewrites the command and Codex trust entry, sweeping the old
compound form.

Add a platform-independent regression guard (launcher must resolve to a real
file, never a cmd-builtin fragment), update the lifecycle test + docs, and fix a
stale Devin comment.
2026-07-14 05:41:58 -07:00

119 lines
4.0 KiB
TypeScript

import { homedir } from 'node:os'
import { join } from 'node:path'
import {
buildManagedCommandHook,
createManagedCommandMatcher,
getSharedManagedScriptPath,
removeManagedCommands,
wrapPosixHookCommand,
wrapWindowsCmdHookCommand,
type HookDefinition,
type HooksConfig
} from '../agent-hooks/installer-utils'
const DEVIN_SCRIPT_BASE = 'devin-hook'
export const DEVIN_EVENTS = [
{ eventName: 'SessionStart', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'UserPromptSubmit', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'Stop', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'PostCompaction', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'SessionEnd', definition: { hooks: [{ type: 'command', command: '' }] } },
// Why: Devin treats matchers as regexes and says omitted means "all";
// Claude's "*" matcher is not a valid Devin regex.
{ eventName: 'PreToolUse', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'PostToolUse', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'PermissionRequest', definition: { hooks: [{ type: 'command', command: '' }] } }
] as const
export function getDevinConfigPath(): string {
if (process.platform === 'win32') {
const appData = process.env.APPDATA ?? join(homedir(), 'AppData', 'Roaming')
return join(appData, 'devin', 'config.json')
}
return join(homedir(), '.config', 'devin', 'config.json')
}
export function getDevinManagedScriptFileName(): string {
return process.platform === 'win32' ? `${DEVIN_SCRIPT_BASE}.cmd` : `${DEVIN_SCRIPT_BASE}.sh`
}
export function getDevinPosixManagedScriptFileName(): string {
return `${DEVIN_SCRIPT_BASE}.sh`
}
export function getDevinManagedScriptPath(): string {
return getSharedManagedScriptPath(getDevinManagedScriptFileName())
}
export function getDevinRemoteConfigPath(remoteHome: string): string {
return `${remoteHome.replace(/\/$/, '')}/.config/devin/config.json`
}
export function getDevinManagedCommand(scriptPath: string): string {
if (process.platform === 'win32') {
// Why: Devin spawns this command as argv[0], so the safe path stays a bare
// directly-spawnable .cmd; the encoded fallback protects spaced paths and
// drains stdin for a stale missing-script entry.
return wrapWindowsCmdHookCommand(scriptPath)
}
return wrapPosixHookCommand(scriptPath)
}
export function getDevinRemoteManagedCommand(scriptPath: string): string {
return wrapPosixHookCommand(scriptPath)
}
export function applyDevinManagedHooks(
config: HooksConfig,
command: string,
scriptFileName = getDevinManagedScriptFileName()
): HooksConfig {
const nextHooks = { ...config.hooks }
const isManagedCommand = createManagedCommandMatcher(scriptFileName)
for (const event of DEVIN_EVENTS) {
const current = Array.isArray(nextHooks[event.eventName]) ? nextHooks[event.eventName] : []
const cleaned = removeManagedCommands(current, isManagedCommand)
const definition: HookDefinition = {
...event.definition,
hooks: [buildManagedCommandHook(command)]
}
nextHooks[event.eventName] = [...cleaned, definition]
}
return { ...config, hooks: nextHooks }
}
export function removeDevinManagedHooks(
config: HooksConfig,
scriptFileName = getDevinManagedScriptFileName()
): {
config: HooksConfig
changed: boolean
} {
const nextHooks = { ...config.hooks }
const isManagedCommand = createManagedCommandMatcher(scriptFileName)
let changed = false
for (const [eventName, definitions] of Object.entries(nextHooks)) {
if (!Array.isArray(definitions)) {
continue
}
const cleaned = removeManagedCommands(definitions, isManagedCommand)
if (JSON.stringify(cleaned) !== JSON.stringify(definitions)) {
changed = true
}
if (cleaned.length === 0) {
delete nextHooks[eventName]
} else {
nextHooks[eventName] = cleaned
}
}
return {
config: { ...config, hooks: nextHooks },
changed
}
}