Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions src/main/agent-hooks/managed-hook-command-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
wrapReadablePosixHookCommand
} from '../codex/codex-hook-definition'
import { ANTIGRAVITY_EVENTS, ANTIGRAVITY_PRE_TOOL_USE_DECISION } from '../antigravity/hook-events'
import { getManagedCommand as getAntigravityCommand } from '../antigravity/hook-service'
import { CURSOR_EVENTS } from '../cursor/hook-events'
import {
getManagedCommand as getCursorCommand,
Expand All @@ -24,11 +25,7 @@ import { getGrokManagedCommand } from '../grok/grok-hook-script'
import { getMuseManagedCommand, getMuseRemoteManagedCommand } from '../muse/hook-settings'
import { getDshManagedCommand, getDshRemoteManagedCommand } from '../dsh/hook-settings'
import { getZCodeManagedCommand, getZCodeRemoteManagedCommand } from '../zcode/hook-settings'
import {
wrapPosixHookCommand,
wrapWindowsCmdHookCommand,
wrapWindowsHookCommand
} from './installer-utils'
import { wrapPosixHookCommand, wrapWindowsHookCommand } from './installer-utils'
import { MANAGED_AGENT_HOOK_INSTALLERS } from './managed-agent-hook-registry'
import { REMOTE_MANAGED_HOOK_INSTALLER_AGENTS } from './remote-managed-hook-installers'
import {
Expand Down Expand Up @@ -107,14 +104,9 @@ const buildersByAgent = new Map<string, CommandBuilders>([
[
'antigravity',
{
local: (path) =>
process.platform === 'win32'
? ANTIGRAVITY_EVENTS.map((event) =>
wrapWindowsCmdHookCommand(
path.replace('antigravity-hook.cmd', event.windowsWrapperFileName)
)
)
: antigravityPosixCommands(path),
// Why: drive the installer's own builder rather than re-deriving it here; a private copy
// of the Windows branch stopped reflecting what Antigravity actually registers.
local: (path) => ANTIGRAVITY_EVENTS.map((event) => getAntigravityCommand(path, event)),
remote: antigravityPosixCommands
}
],
Expand Down
27 changes: 17 additions & 10 deletions src/main/antigravity/hook-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { homedir } from 'node:os'
import { join } from 'node:path'
import { dirname, join } from 'node:path'
import type { SFTPWrapper } from 'ssh2'
import type { AgentHookInstallState, AgentHookInstallStatus } from '../../shared/agent-hook-types'
import {
getSharedManagedScriptPath,
readHooksJson,
wrapPosixHookCommand,
wrapWindowsCmdHookCommand,
writeHooksJson,
writeManagedScript,
type HookDefinition
Expand Down Expand Up @@ -46,8 +45,9 @@ function getManagedScriptPath(): string {
return getSharedManagedScriptPath(getManagedScriptFileName())
}

function getWindowsWrapperScriptPath(event: AntigravityEvent): string {
return getSharedManagedScriptPath(event.windowsWrapperFileName)
// Why: the wrapper reaches the core through `%~dp0`, so it only works from the core's own directory.
function getWindowsWrapperScriptPath(scriptPath: string, event: AntigravityEvent): string {
return join(dirname(scriptPath), event.windowsWrapperFileName)
}

function getPosixManagedCommand(scriptPath: string, event: AntigravityEvent): string {
Expand All @@ -59,9 +59,16 @@ function getPosixManagedCommand(scriptPath: string, event: AntigravityEvent): st
)
}

function getManagedCommand(scriptPath: string, event: AntigravityEvent): string {
export function getManagedCommand(scriptPath: string, event: AntigravityEvent): string {
if (process.platform === 'win32') {
return wrapWindowsCmdHookCommand(getWindowsWrapperScriptPath(event))
// Why (#8737): Antigravity starts a hook `command` as a program, so the entry has to name a
// file. The shared cmd wrapper swaps any path cmd.exe could not carry bare — a profile with a
// space, the ordinary `C:\Users\First Last` — for a multi-token PowerShell launcher, which is
// not a program name and so never starts. That produces no stdout, and Antigravity reads
// silence on PreToolUse as deny (hook-events.ts), denying every tool call on a clean install.
// The per-event wrapper is the Windows half of the POSIX fallbackStdout above: it answers the
// gate itself when the core script is missing, so the startable path is also the answering one.
return getWindowsWrapperScriptPath(scriptPath, event)
}
return getPosixManagedCommand(scriptPath, event)
}
Expand All @@ -72,7 +79,7 @@ export class AntigravityHookService {
if (process.platform === 'win32') {
for (const event of ANTIGRAVITY_EVENTS) {
await refreshManagedScriptIfPresent(
getWindowsWrapperScriptPath(event),
getWindowsWrapperScriptPath(getManagedScriptPath(), event),
getWindowsWrapperScript(event.eventName)
)
}
Expand Down Expand Up @@ -156,11 +163,11 @@ export class AntigravityHookService {
)
writeManagedScript(scriptPath, getManagedScript())
if (process.platform === 'win32') {
// Why: Antigravity wraps hook commands in cmd.exe. Keeping event env
// setup inside event-specific .cmd files avoids nested hooks.json quotes.
// Why: the entry stays a bare path, so per-event env has to live inside the wrapper .cmd
// rather than in a hooks.json command line that would need nested quoting.
for (const event of ANTIGRAVITY_EVENTS) {
writeManagedScript(
getWindowsWrapperScriptPath(event),
getWindowsWrapperScriptPath(scriptPath, event),
getWindowsWrapperScript(event.eventName)
)
}
Expand Down
80 changes: 80 additions & 0 deletions src/main/antigravity/windows-hook-command-spawnability.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Why (#2426): Antigravity reads silence on PreToolUse as a deny, so a Windows hook entry it
// cannot start is not lost status — it denies every tool call. Antigravity starts a hook
// `command` as a program (#8737), so the entry has to name a file on every profile, and that
// file has to be the one that answers the gate.
import { describe, expect, it, vi } from 'vitest'
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import type * as osModule from 'node:os'
import { join } from 'node:path'

const { homedirMock } = vi.hoisted(() => ({ homedirMock: vi.fn<() => string>() }))

vi.mock('os', async (importOriginal) => {
const actual = await importOriginal<typeof osModule>()
return { ...actual, homedir: homedirMock }
})

import { AntigravityHookService, getManagedCommand } from './hook-service'
import { ANTIGRAVITY_EVENTS, ANTIGRAVITY_PRE_TOOL_USE_DECISION } from './hook-events'
import { WINDOWS_CMD_SAFE_PATH } from '../agent-hooks/installer-utils'

const WRAPPER_TAIL = '\\.orca\\agent-hooks\\antigravity-pre-tool-use.cmd'

/**
* Installs into a Windows-shaped home whose path carries a space, then hands back the managed
* script path. `state === 'installed'` is the load-bearing part: getStatus() only reports it when
* every event's hooks.json entry equals getManagedCommand(), so the assertions below read the
* string Antigravity is actually handed.
*/
function withSpacedWindowsInstall(run: (home: string, scriptPath: string) => void): void {
vi.spyOn(process, 'platform', 'get').mockReturnValue('win32')
// Why: a space is the ordinary shape of a Windows profile (`C:\Users\First Last`) and the one
// character that decides which launcher the installer picks.
const home = mkdtempSync(join(tmpdir(), 'orca antigravity spaced '))
homedirMock.mockReturnValue(home)
try {
expect(new AntigravityHookService().install().state).toBe('installed')
run(home, join(home, '.orca', 'agent-hooks', 'antigravity-hook.cmd'))
} finally {
vi.restoreAllMocks()
rmSync(home, { recursive: true, force: true })
}
}

describe('Antigravity Windows hook command spawnability', () => {
// Why a control: every assertion below is worthless if a spaced profile were not in the class
// the installer treats specially. Pin both sides of that gate before relying on it.
it('treats a spaced Windows profile path as the diverted class, and an ASCII one as safe', () => {
expect(WINDOWS_CMD_SAFE_PATH.test(`C:\\Users\\alice${WRAPPER_TAIL}`)).toBe(true)
expect(WINDOWS_CMD_SAFE_PATH.test(`C:\\Users\\First Last${WRAPPER_TAIL}`)).toBe(false)
})

it('registers a single startable script path for every event on a spaced profile', () => {
withSpacedWindowsInstall((home, scriptPath) => {
for (const event of ANTIGRAVITY_EVENTS) {
const command = getManagedCommand(scriptPath, event)
expect(command, event.eventName).toBe(
join(home, '.orca', 'agent-hooks', event.windowsWrapperFileName)
)
// Why: a program name is startable only if it names a file. A multi-token launcher string
// never does, and a hook that never starts writes no stdout at all.
expect(existsSync(command), `${event.eventName} command is not a file`).toBe(true)
expect(command, event.eventName).not.toMatch(/powershell|-EncodedCommand/i)
Comment on lines +60 to +63

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Spaced-path launch remains untested The test confirms that each registered command names an existing file, but it never launches that command. The live Windows suite launches wrappers without guaranteeing a spaced home directory. As a result, a failure to start the bare .cmd path under a spaced profile could leave these tests green while PreToolUse denies tool calls. Please add Windows-host coverage that launches a command from a guaranteed spaced path and checks its gate response.

}
})
})

it('keeps answering the PreToolUse gate from that path when the core script is gone', () => {
withSpacedWindowsInstall((home, scriptPath) => {
// Why: hooks.json lives in ~/.gemini and outlives ~/.orca, so a swept core must not take the
// gate answer with it — this is what fallbackStdout buys the POSIX command.
rmSync(join(home, '.orca', 'agent-hooks', 'antigravity-hook.cmd'))
const preToolUse = ANTIGRAVITY_EVENTS.find((event) => event.eventName === 'PreToolUse')!

const command = getManagedCommand(scriptPath, preToolUse)
expect(existsSync(command)).toBe(true)
expect(readFileSync(command, 'utf8')).toContain(`echo ${ANTIGRAVITY_PRE_TOOL_USE_DECISION}`)
})
})
})
Loading