diff --git a/src/main/ipc/register-core-handlers/register-core-handlers.ts b/src/main/ipc/register-core-handlers/register-core-handlers.ts index 6fe001c5d9c9..51fe71a39b19 100644 --- a/src/main/ipc/register-core-handlers/register-core-handlers.ts +++ b/src/main/ipc/register-core-handlers/register-core-handlers.ts @@ -95,7 +95,10 @@ import { } from '../../ai-vault/runtime-hosts/runtime-session-scanner' import { callRuntimeSessionSearch } from '../../ai-vault/runtime-hosts/runtime-session-search-call' import { configureRuntimeAiVaultSessionDelete } from '../ai-vault/ai-vault-delete' -import { ensureStructuredAgentSessionHostUnlessRefused } from '../../runtime/agent-session/installation/structured-agent-session-host-refusal' +import { + ensurePersistedStructuredAgentSessionHostUnlessRefused, + ensureStructuredAgentSessionHostUnlessRefused +} from '../../runtime/agent-session/installation/structured-agent-session-host-refusal' import { getAgentSessionAttachmentStore } from '../../native-chat/agent-session-attachments/agent-session-attachment-store-registry' import type { PluginService } from '../../plugins/plugin-service' import type { PluginMarketplaceHandlerServices } from '../plugins/plugin-marketplaces' @@ -225,9 +228,9 @@ export async function registerCoreHandlers( registerWslDriveProjectHandlers(store, runtime) installProjectRuntimeGitRouter(store) registerEphemeralVmHandlers(store, pluginService) - // Session history and terminal resume are not chats; a refused host leaves nothing to check. + // Session history and terminal resume are not chats; skip booting an empty chat journal host. const ensureStructuredSessionOwnership = () => - ensureStructuredAgentSessionHostUnlessRefused(() => + ensurePersistedStructuredAgentSessionHostUnlessRefused(() => runtime.ensureStructuredAgentSessionHost() ) registerAiVaultSearchHandlers({ diff --git a/src/main/runtime/agent-session/installation/structured-agent-session-host-refusal.ts b/src/main/runtime/agent-session/installation/structured-agent-session-host-refusal.ts index d780a1e146a1..f0505c256d11 100644 --- a/src/main/runtime/agent-session/installation/structured-agent-session-host-refusal.ts +++ b/src/main/runtime/agent-session/installation/structured-agent-session-host-refusal.ts @@ -2,6 +2,9 @@ import { isAgentSessionRefusalError, type AgentSessionRefusalError } from '../../../../shared/agent-session/failures/agent-session-wire-refusals' +import { getStructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-registry' +import { getProfileUserDataPath } from '../../../orca-profiles/profile-storage-paths' +import { hasPersistedStructuredAgentSessionDatabaseFile } from '../records/persisted-agent-session-database-file' let installRefusal: AgentSessionRefusalError | null = null @@ -32,6 +35,19 @@ export async function ensureStructuredAgentSessionHostUnlessRefused( } } +/** Skip booting the chat journal host for ownership lookups when no chat store exists yet. */ +export async function ensurePersistedStructuredAgentSessionHostUnlessRefused( + ensureHost: () => Promise +): Promise { + if ( + !getStructuredAgentSessionHost() && + !(await hasPersistedStructuredAgentSessionDatabaseFile(getProfileUserDataPath())) + ) { + return + } + await ensureStructuredAgentSessionHostUnlessRefused(ensureHost) +} + /** Chat-host setup is bookkeeping for a terminal resume; an existing owner is checked afterward. */ export async function tryEnsureStructuredAgentSessionHostForResume( ensureHost: () => Promise diff --git a/src/main/runtime/agent-session/lifecycle/pr-watch-runtime.ts b/src/main/runtime/agent-session/lifecycle/pr-watch-runtime.ts index cbfd6e53f8c7..650770e73f57 100644 --- a/src/main/runtime/agent-session/lifecycle/pr-watch-runtime.ts +++ b/src/main/runtime/agent-session/lifecycle/pr-watch-runtime.ts @@ -8,7 +8,11 @@ import type { JournalWriterCommit } from '../../../native-chat/agent-session-jou import type { AgentSessionJournal } from '../../../native-chat/agent-session-journal/journal-store' import type { StructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' import { getStructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-registry' -import { openAgentSessionRecordStoreOnce } from '../records/agent-session-record-store-slot' +import { + openAgentSessionRecordStoreOnce, + peekOpenedAgentSessionRecordStore +} from '../records/agent-session-record-store-slot' +import { hasPersistedStructuredAgentSessionDatabaseFile } from '../records/persisted-agent-session-database-file' import { isHostShuttingDown } from '../../../startup/quit/host-shutdown-state' export type PRWatchRuntimeDeps = { @@ -48,6 +52,16 @@ export function startPRWatchRuntime( admittedDeps = deps const startedIn = generation starting ??= (async () => { + // Skip opening the chat journal writer/reader workers when no structured chat DB exists yet. + if ( + !peekOpenedAgentSessionRecordStore() && + !(await hasPersistedStructuredAgentSessionDatabaseFile(deps.stateDirectory)) + ) { + if (startedIn === generation) { + starting = null + } + return + } const { store, journalDatabase } = await openAgentSessionRecordStoreOnce( withPRWatchRecovery({ stateDirectory: deps.stateDirectory, @@ -124,6 +138,9 @@ export function observePRWatchCommittedRows( journal: AgentSessionJournal, rows: JournalWriterCommit | undefined ): void { + if (!running && !starting && admittedDeps) { + resumePRWatchRuntime() + } // Before watches start, captured obligations simply wait in their table for the first drain. running?.observeCommittedRows(sessionId, journal, rows) } diff --git a/src/main/runtime/agent-session/records/persisted-agent-session-database-file.ts b/src/main/runtime/agent-session/records/persisted-agent-session-database-file.ts new file mode 100644 index 000000000000..72f5670e9a20 --- /dev/null +++ b/src/main/runtime/agent-session/records/persisted-agent-session-database-file.ts @@ -0,0 +1,24 @@ +import { access } from 'node:fs/promises' +import { + journalDatabasePath, + journalDomainDatabasePath +} from '../../../native-chat/agent-session-journal/database/journal-host-database' + +/** Non-blocking existence check so async callers skip spawning journal workers on empty profiles. + * Lives apart from the record-store slot so the host-refusal gate can use it without an import cycle. */ +export async function hasPersistedStructuredAgentSessionDatabaseFile( + stateDirectory: string +): Promise { + for (const candidate of [ + journalDomainDatabasePath(stateDirectory), + journalDatabasePath(stateDirectory) + ]) { + try { + await access(candidate) + return true + } catch { + // Absent file; check the next candidate. + } + } + return false +} diff --git a/src/main/runtime/rpc/methods/history/ai-vault.ts b/src/main/runtime/rpc/methods/history/ai-vault.ts index 18741f4d8638..4583d2f70f8e 100644 --- a/src/main/runtime/rpc/methods/history/ai-vault.ts +++ b/src/main/runtime/rpc/methods/history/ai-vault.ts @@ -25,7 +25,7 @@ import { searchWithStructuredOwners } from '../../../../ai-vault/structured-sessions/structured-session-ownership' import { - ensureStructuredAgentSessionHostUnlessRefused, + ensurePersistedStructuredAgentSessionHostUnlessRefused, tryEnsureStructuredAgentSessionHostForResume } from '../../../agent-session/installation/structured-agent-session-host-refusal' import { @@ -53,7 +53,10 @@ export const AI_VAULT_METHODS = [ // A client that reads no structured sessions keeps the transcript hit, as before. return supportsStructuredAgentSessions(context) ? searchWithStructuredOwners(response, { - ensureHost: () => context.runtime.ensureStructuredAgentSessionHost(), + ensureHost: () => + ensurePersistedStructuredAgentSessionHostUnlessRefused(() => + context.runtime.ensureStructuredAgentSessionHost() + ), transport, opensChat: (agent) => clientOpensStructuredChatFromHistory(context, agent) }) @@ -100,7 +103,7 @@ export const AI_VAULT_METHODS = [ permission: 'workspace', params: AiVaultListSessionsParams, handler: async (params, { runtime, clientKind, clientCapabilities, signal }) => { - await ensureStructuredAgentSessionHostUnlessRefused(() => + await ensurePersistedStructuredAgentSessionHostUnlessRefused(() => runtime.ensureStructuredAgentSessionHost() ) let result @@ -157,10 +160,13 @@ export const AI_VAULT_METHODS = [ executionHostId: LOCAL_EXECUTION_HOST_ID } if (!isWslUncPath(args.filePath)) { - const ensureHost = () => runtime.ensureStructuredAgentSessionHost() + const ensureHost = () => + ensurePersistedStructuredAgentSessionHostUnlessRefused(() => + runtime.ensureStructuredAgentSessionHost() + ) await (args.codexHome === null && !args.fork ? tryEnsureStructuredAgentSessionHostForResume(ensureHost) - : ensureStructuredAgentSessionHostUnlessRefused(ensureHost)) + : ensureHost()) assertLegacyAiVaultResumeAllowed(args) } return (