fix(engine): make pi.js imports static to fail fast on partial dist

Replace lazy `await import("./pi.js")` and `require("./pi.js")` calls in
runtime-resolution, agent-session-helpers, agent-heartbeat, and
cron-runner with top-level static imports. These dynamic imports were
documented as plugin-decoupling, but pi.js is already eagerly loaded
through index.ts re-exports and static imports in executor/merger/
reviewer/triage/mission-execution-loop, so the deferral never paid off
in practice.

The deferral did, however, introduce a TOCTOU race: a tsc rebuild that
momentarily emptied dist/pi.js would let the engine load fine and only
fail minutes later when the first session was created (e.g. FN-2860
errored two minutes into execution while pi.js was being rewritten).
With static imports, a missing/half-built dist now fails immediately at
process startup with a clear stack — verified by `mv dist/pi.js
dist/pi.js.bak` reproducing ERR_MODULE_NOT_FOUND on the first import of
runtime-resolution.js.

Also drops the DefaultPiRuntime.describeModelFn cache, which only
existed to paper over the require-on-first-call latency.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-28 07:48:03 -07:00
parent 479f25d50f
commit 38933c770a
4 changed files with 11 additions and 42 deletions

View File

@@ -12,6 +12,7 @@ import type { PluginRunner } from "./plugin-runner.js";
import type { AgentSession } from "@mariozechner/pi-coding-agent";
import { resolveRuntime, buildRuntimeResolutionContext, type SessionPurpose } from "./runtime-resolution.js";
import { createLogger } from "./logger.js";
import { promptWithFallback, describeModel } from "./pi.js";
/** Logger for agent session helpers */
const sessionLog = createLogger("agent-session");
@@ -130,10 +131,7 @@ export async function promptWithAutoRetry(
prompt: string,
options?: unknown,
): Promise<void> {
// Dynamic import to get the default runtime's promptWithFallback
// This works because the default runtime delegates to the existing implementation
const { promptWithFallback: pwf } = await import("./pi.js");
return pwf(session, prompt, options);
return promptWithFallback(session, prompt, options);
}
/**
@@ -143,6 +141,5 @@ export async function promptWithAutoRetry(
* @returns Model description string
*/
export async function describeAgentModel(session: AgentSession): Promise<string> {
const { describeModel: dm } = await import("./pi.js");
return dm(session);
return describeModel(session);
}