Repository navigation
test(agent-hooks): guard the Windows direct-spawn hook launcher contract #11081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
src/main/agent-hooks/windows-direct-spawn-launcher.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { mkdtempSync, readFileSync, rmSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import type * as Os from 'node:os' | ||
| import { join } from 'node:path' | ||
| import type * as InstallerUtils from './installer-utils' | ||
|
|
||
| const { getPathMock, homedirMock } = vi.hoisted(() => ({ | ||
| getPathMock: vi.fn<(name: string) => string>(), | ||
| homedirMock: vi.fn<() => string>() | ||
| })) | ||
|
|
||
| vi.mock('electron', () => ({ app: { getPath: getPathMock } })) | ||
|
|
||
| vi.mock('os', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof Os>() | ||
| return { ...actual, homedir: homedirMock } | ||
| }) | ||
|
|
||
| // Why: stubbing only the path source keeps the real wrappers in play (so a wrapper swap still | ||
| // shows up) and pins a Windows path, letting this assert win32 behavior on ubuntu CI. | ||
| const WINDOWS_HOOK_DIR = 'C:\\Users\\alice\\.orca\\agent-hooks' | ||
|
|
||
| vi.mock('./installer-utils', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof InstallerUtils>() | ||
| return { | ||
| ...actual, | ||
| getSharedManagedScriptPath: (scriptFileName: string) => | ||
| `${WINDOWS_HOOK_DIR}\\${scriptFileName}`, | ||
| writeManagedScript: vi.fn() | ||
| } | ||
| }) | ||
|
|
||
| import { AntigravityHookService } from '../antigravity/hook-service' | ||
| import { cursorHookService } from '../cursor/hook-service' | ||
| import { getCodexManagedHookInstallMaterial } from '../codex/hook-service' | ||
| import { getDevinManagedCommand, getDevinManagedScriptPath } from '../devin/hook-settings' | ||
|
|
||
| const BARE_WINDOWS_CMD_LAUNCHER = /^C:\\Users\\alice\\\.orca\\agent-hooks\\[A-Za-z0-9._-]+\.cmd$/ | ||
| const ENCODED_POWERSHELL_LAUNCHER = | ||
| /powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ | ||
|
|
||
| function withPlatform<T>(platform: NodeJS.Platform, run: () => T): T { | ||
| const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform') | ||
| Object.defineProperty(process, 'platform', { configurable: true, value: platform }) | ||
| try { | ||
| return run() | ||
| } finally { | ||
| if (originalPlatform) { | ||
| Object.defineProperty(process, 'platform', originalPlatform) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Why: these agents hand the command to CreateProcess as argv[0], not to cmd.exe, so anything | ||
| // but a single token naming a real .cmd is unspawnable and exits every hook 1 (#8430). | ||
| function expectDirectlySpawnable(command: string, label: string): void { | ||
| expect(command, `${label}: launcher must be the bare managed .cmd path`).toMatch( | ||
| BARE_WINDOWS_CMD_LAUNCHER | ||
| ) | ||
| expect(command.split(' '), `${label}: launcher must be a single argv[0] token`).toHaveLength(1) | ||
| expect(command, `${label}: launcher must not be a shell launcher`).not.toMatch(/powershell/i) | ||
| } | ||
|
|
||
| function readCursorCommand(homeDir: string): string { | ||
| const config = JSON.parse(readFileSync(join(homeDir, '.cursor', 'hooks.json'), 'utf8')) as { | ||
| hooks: Record<string, { command?: string }[]> | ||
| } | ||
| return config.hooks.stop?.[0]?.command ?? '' | ||
| } | ||
|
|
||
| function readAntigravityCommands(homeDir: string): string[] { | ||
| const config = JSON.parse( | ||
| readFileSync(join(homeDir, '.gemini', 'config', 'hooks.json'), 'utf8') | ||
| ) as { | ||
| 'orca-status': Record<string, { command?: string; hooks?: { command?: string }[] }[]> | ||
| } | ||
| return Object.values(config['orca-status']).flatMap((definitions) => | ||
| definitions.map((definition) => definition.command ?? definition.hooks?.[0]?.command ?? '') | ||
| ) | ||
| } | ||
|
|
||
| describe('Windows direct-spawn hook launcher contract', () => { | ||
| let homeDir: string | ||
|
|
||
| beforeEach(() => { | ||
| homeDir = mkdtempSync(join(tmpdir(), 'orca-direct-spawn-home-')) | ||
| homedirMock.mockReturnValue(homeDir) | ||
| getPathMock.mockReturnValue(homeDir) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks() | ||
| rmSync(homeDir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| it('keeps the Codex managed command directly spawnable', () => { | ||
| const { command } = withPlatform('win32', () => getCodexManagedHookInstallMaterial()) | ||
| expectDirectlySpawnable(command, 'codex') | ||
| }) | ||
|
|
||
| it('keeps the Devin managed command directly spawnable', () => { | ||
| const command = withPlatform('win32', () => getDevinManagedCommand(getDevinManagedScriptPath())) | ||
| expectDirectlySpawnable(command, 'devin') | ||
| }) | ||
|
|
||
| it('keeps every Antigravity managed command directly spawnable', () => { | ||
| withPlatform('win32', () => new AntigravityHookService().install()) | ||
| const commands = readAntigravityCommands(homeDir) | ||
| expect(commands.length).toBeGreaterThan(0) | ||
| for (const command of commands) { | ||
| expectDirectlySpawnable(command, 'antigravity') | ||
| } | ||
| }) | ||
|
|
||
| // Why: pins the split — Cursor's runner goes through a shell, so it must keep the encoded | ||
| // launcher. Without this, "make every agent spawnable" would look like a valid refactor. | ||
| it('keeps a shell-launched agent on the encoded PowerShell launcher', () => { | ||
| withPlatform('win32', () => cursorHookService.install()) | ||
| const command = readCursorCommand(homeDir) | ||
| expect(command).toMatch(ENCODED_POWERSHELL_LAUNCHER) | ||
| expect(command).not.toMatch(BARE_WINDOWS_CMD_LAUNCHER) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
Seed the Antigravity hooks configuration before installation.
This test creates only
homeDir;AntigravityHookService.install()returns an error when.gemini/config/hooks.jsonis missing, soreadAntigravityCommands()then throws while reading a nonexistent file. Create a minimal valid configuration and assert the install status isinstalledbefore reading the generated commands.Based on the supplied
AntigravityHookService.install()implementation, missing configuration returns an error.