From 83ee93e442099a448d8ca58b68ac169819abc03c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:11:56 -0700 Subject: [PATCH] perf(agent-state): use the bundled rule files without re-validating them at load (#35768) orcad spent ~25 ms of every start zod-parsing the 14 checked-in rule files. A test now proves their parse output deep-equals the raw JSON and that the schema can't change output, so the runtime uses them as bundled. Downloaded and override rules keep full validation. Also corrects the Codex-deferral comment: every orcad host defers it, not only WSL. --- src/main/orcad/orcad-managed-agent-hooks.ts | 2 +- .../agent-state-rules-catalog.test.ts | 66 +++++++++++++++++++ .../agent-state-rules-catalog.ts | 37 ++++++----- 3 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 src/main/runtime/agent-state-rules/agent-state-rules-catalog.test.ts diff --git a/src/main/orcad/orcad-managed-agent-hooks.ts b/src/main/orcad/orcad-managed-agent-hooks.ts index 6a1093b7b49..a5772bbbc34 100644 --- a/src/main/orcad/orcad-managed-agent-hooks.ts +++ b/src/main/orcad/orcad-managed-agent-hooks.ts @@ -39,7 +39,7 @@ export function startOrcadManagedAgentHooks( const shouldHydrateShellPath = getAppEnvironment().isPackaged() startCodexHooks({ pathReady: shouldHydrateShellPath ? hydrateAgentCliShellPath() : Promise.resolve(), - // WSL: finding codex stats every Windows-drive PATH entry, slow on a cold start. + // Every orcad host defers this; it pays off on WSL, where finding codex stats Windows-drive PATH. startAfter: published.promise, isEnabled: () => isAgentStatusHooksEnabledForAgent(getSettings(), 'codex'), resolveLaunchHome: () => null diff --git a/src/main/runtime/agent-state-rules/agent-state-rules-catalog.test.ts b/src/main/runtime/agent-state-rules/agent-state-rules-catalog.test.ts new file mode 100644 index 00000000000..90d68fbe27e --- /dev/null +++ b/src/main/runtime/agent-state-rules/agent-state-rules-catalog.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, it } from 'vitest' +import { z } from 'zod' +import { + BUNDLED_AGENT_STATE_RULE_FILES, + parseAgentStateRuleFiles, + RAW_BUNDLED_AGENT_STATE_RULE_FILES +} from './agent-state-rules-catalog' +import { AgentStateRulesFileSchema } from './agent-state-rules-schema' + +// Kinds whose parse output can differ from their input. +const OUTPUT_CHANGING_KINDS = new Set(['pipe', 'transform', 'default', 'prefault', 'catch']) + +/** Every reachable schema kind that would change parse output, or a coercing primitive. */ +function outputChangingKinds(schema: z.core.$ZodType, seen = new Set()): string[] { + if (seen.has(schema)) { + return [] + } + seen.add(schema) + const def: Record = { ...schema._zod.def } + const found = OUTPUT_CHANGING_KINDS.has(String(def.type)) ? [String(def.type)] : [] + if (def.coerce === true) { + found.push(`coerced ${String(def.type)}`) + } + const children: unknown[] = [ + def.innerType, + def.element, + def.in, + def.out, + def.keyType, + def.valueType, + def.rest, + ...(Array.isArray(def.options) ? def.options : []), + ...(Array.isArray(def.items) ? def.items : []), + ...(def.shape && typeof def.shape === 'object' ? Object.values(def.shape) : []), + ...(typeof def.getter === 'function' ? [def.getter()] : []) + ] + for (const child of children) { + if (child && typeof child === 'object' && '_zod' in child) { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: every zod schema carries `_zod`; the guard above checks it. + found.push(...outputChangingKinds(child as z.core.$ZodType, seen)) + } + } + return found +} + +describe('bundled agent state rule files', () => { + it('are used as bundled, without parsing, so parsing must not change them', () => { + expect(BUNDLED_AGENT_STATE_RULE_FILES).toBe(RAW_BUNDLED_AGENT_STATE_RULE_FILES) + expect(parseAgentStateRuleFiles(RAW_BUNDLED_AGENT_STATE_RULE_FILES)).toStrictEqual( + RAW_BUNDLED_AGENT_STATE_RULE_FILES + ) + }) + + it('keep a schema whose parse output always equals its input', () => { + expect(outputChangingKinds(AgentStateRulesFileSchema)).toEqual([]) + }) + + it('would catch a transform, default or coercion added to the schema', () => { + expect(outputChangingKinds(z.object({ a: z.string().default('x') }))).toEqual(['default']) + expect(outputChangingKinds(z.object({ a: z.string().transform((v) => v) }))).toEqual([ + 'pipe', + 'transform' + ]) + expect(outputChangingKinds(z.array(z.coerce.number()))).toEqual(['coerced number']) + }) +}) diff --git a/src/main/runtime/agent-state-rules/agent-state-rules-catalog.ts b/src/main/runtime/agent-state-rules/agent-state-rules-catalog.ts index 2592a3caaae..2fbbe148967 100644 --- a/src/main/runtime/agent-state-rules/agent-state-rules-catalog.ts +++ b/src/main/runtime/agent-state-rules/agent-state-rules-catalog.ts @@ -32,21 +32,26 @@ export function parseAgentStateRuleFiles(files: readonly unknown[]): AgentStateR return parsed } +/** The checked-in rule files exactly as bundled; the catalog test validates them against the schema. */ +export const RAW_BUNDLED_AGENT_STATE_RULE_FILES: readonly unknown[] = [ + antigravity, + claude, + cline, + codex, + cursor, + dsh, + gemini, + hermes, + omp, + opencode, + opencode2, + pi, + primeAgent, + unknownPane +] + // Inline imports keep packaged/headless builds independent of resource paths. +// Not parsed at load: zod-validating them cost ~25 ms of orcad startup on every launch. export const BUNDLED_AGENT_STATE_RULE_FILES: readonly AgentStateRulesFile[] = - parseAgentStateRuleFiles([ - antigravity, - claude, - cline, - codex, - cursor, - dsh, - gemini, - hermes, - omp, - opencode, - opencode2, - pi, - primeAgent, - unknownPane - ]) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: agent-state-rules-catalog.test.ts proves each bundled file parses and that the parse output deep-equals the raw JSON, and that the schema has no transform, default or coercion. + RAW_BUNDLED_AGENT_STATE_RULE_FILES as readonly AgentStateRulesFile[]