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.
This commit is contained in:
Neil
2026-10-29 00:11:56 -07:00
committed by GitHub
parent 3d812eb9bf
commit 83ee93e442
3 changed files with 88 additions and 17 deletions
+1 -1
View File
@@ -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
@@ -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<unknown>()): string[] {
if (seen.has(schema)) {
return []
}
seen.add(schema)
const def: Record<string, unknown> = { ...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'])
})
})
@@ -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[]