Skip to content
Merged
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
49 changes: 49 additions & 0 deletions src/cli/handlers/agent-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,53 @@ describe('agent hooks CLI handler', () => {
readBack.db.close()
}
})

it.each([
{ label: 'offline', reachable: false, enabled: true, appliedBy: 'offline' },
{ label: 'connected', reachable: true, enabled: false, appliedBy: 'runtime' }
] as const)(
'reports $label hook status against the SQLite file that holds the setting',
async ({ reachable, enabled, appliedBy }) => {
const profileId = 'sqlite-status'
writeActiveProfileIndex(userDataPath, profileId)
const databaseFile = getOrcaProfileStateDatabaseFile(profileId, userDataPath)
mkdirSync(join(userDataPath, 'profiles', profileId), { recursive: true })
const opened = openProfileStateDatabase(databaseFile, profileId)
try {
importProfileStateJson(
opened.db,
JSON.stringify({ settings: { agentStatusHooksEnabled: true } })
)
} finally {
opened.db.close()
}
getDefaultUserDataPathMock.mockReturnValue(userDataPath)
if (reachable) {
getCliStatusMock.mockResolvedValueOnce({
id: 'test-status',
ok: true,
result: {
app: { running: true, pid: null },
runtime: { state: 'ready', reachable: true, runtimeId: null },
graph: { state: 'ready' }
},
_meta: { runtimeId: 'test' }
})
// The running app has turned hooks off but not yet written it to disk.
callMock.mockResolvedValueOnce({
id: 'settings',
ok: true,
result: { settings: { agentStatusHooksEnabled: false } },
_meta: { runtimeId: 'runtime-1' }
})
}

await main(['agent', 'hooks', 'status', '--json'], userDataPath)

const payload: unknown = JSON.parse(String(vi.mocked(console.log).mock.calls.at(-1)?.[0]))
expect(payload).toMatchObject({
result: { enabled, appliedBy, settingsPath: databaseFile }
})
}
)
})
30 changes: 22 additions & 8 deletions src/cli/handlers/agent-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ type AgentHookCommandResult = {
// Covers managed-home verification, WSL identity, trust grant, and bounded app-server reap.
const WSL_CODEX_PREPARE_TIMEOUT_MS = 50_000

async function getDataPath(): Promise<string> {
return (
(await getProfileStateLocation())?.dataFile ?? join(getDefaultUserDataPath(), 'orca-data.json')
async function getSettingsPath(): Promise<string> {
const { resolveAgentHookSettingsPath } =
await import('../../main/persistence/profile-state/offline-access/profile-state-offline-settings.js')
return resolveAgentHookSettingsPath(
(await getProfileStateLocation()) ?? legacyProfileStateLocation()
)
}

Expand Down Expand Up @@ -128,6 +130,17 @@ async function updateRunningRuntime(client: RuntimeClient, enabled: boolean): Pr
return true
}

/** Null when no runtime answers; a running app owns the live setting. */
async function readRunningRuntimeHooksEnabled(client: RuntimeClient): Promise<boolean | null> {
if (!(await client.getOfflineRuntimeAdmission()).reachable) {
return null
}
const response = await client.call<{
settings: Pick<GlobalSettings, 'agentStatusHooksEnabled'>
}>('settings.get')
return response.result.settings.agentStatusHooksEnabled !== false
}

function localSuccess<TResult>(result: TResult): RuntimeRpcSuccess<TResult> {
return {
id: 'local',
Expand Down Expand Up @@ -166,7 +179,7 @@ async function setAgentHooksEnabled(
await import('../../main/agent-hooks/installation/managed-agent-hook-controls.js')
const updatedRuntime = await updateRunningRuntime(client, enabled)
const offlineUpdate = updatedRuntime ? null : await updateEnabledOnDisk(enabled)
const settingsPath = offlineUpdate?.settingsPath ?? (await getDataPath())
const settingsPath = offlineUpdate?.settingsPath ?? (await getSettingsPath())
const statuses = updatedRuntime
? getManagedAgentHookStatuses()
: await applyAgentStatusHooksEnabled(enabled, offlineUpdate?.settings)
Expand Down Expand Up @@ -199,14 +212,15 @@ export const AGENT_HOOK_HANDLERS: Record<string, CommandHandler> = {
// Best effort: old or unavailable runtimes must not block Codex launch.
}
},
'agent hooks status': async ({ json, flags }) => {
'agent hooks status': async ({ client, json, flags }) => {
rejectRemoteHookSelection(flags)
const { getManagedAgentHookStatuses } =
await import('../../main/agent-hooks/installation/managed-agent-hook-controls.js')
const runtimeEnabled = await readRunningRuntimeHooksEnabled(client)
const result: AgentHookCommandResult = {
enabled: (await readHookSettingsFromDisk()).agentStatusHooksEnabled,
settingsPath: await getDataPath(),
appliedBy: 'offline',
enabled: runtimeEnabled ?? (await readHookSettingsFromDisk()).agentStatusHooksEnabled,
settingsPath: await getSettingsPath(),
appliedBy: runtimeEnabled === null ? 'offline' : 'runtime',
statuses: getManagedAgentHookStatuses()
}
printResult(localSuccess(result), json, formatAgentHookCommandResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export type AgentHookSettingsUpdate = {
settingsPath: string
}

/** The file the reader below takes settings from. */
export function resolveAgentHookSettingsPath(location: ProfileStateOfflineLocation): string {
const classification = classifyProfileStateStorage(location.dataFile, location.databaseFile)
return classification === 'json-only' || classification === 'neither'
? location.dataFile
: location.databaseFile
}

/** JSON-only profiles must not create SQLite while reading settings. */
export function readAgentHookSettingsFromProfileState(
location: ProfileStateOfflineLocation
Expand Down
Loading