fix: adapt to AgentState.error → errorMessage rename

pi-coding-agent 0.70 replaced the mutable \`AgentState.error\` field with
a readonly \`AgentState.errorMessage\`. \`session.prompt()\` still does
not throw when retries are exhausted, so we still need to re-raise the
stored error after each prompt.

- checkSessionError (usage-limit-detector): widen parameter to accept
  either key; prefer errorMessage so new sessions work, fall back to
  error so we can deploy without forcing everyone's caches to rebuild.
- agent-reflection: same widening at the call site.
- pi.ts helpers: read both keys, best-effort clear both (the new field
  is readonly, so the write is a no-op on 0.70 sessions but still
  matters for mock sessions in tests).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-23 17:13:45 -07:00
parent 6a8568e77c
commit 4ce0bf57ac
3 changed files with 29 additions and 16 deletions

View File

@@ -111,8 +111,10 @@ export class AgentReflectionService {
try {
await promptWithFallback(session, this.buildReflectionPrompt(context, options.triggerDetail));
if (session.state?.error) {
throw new Error(session.state.error);
const sessionError = (session.state as { errorMessage?: string; error?: string } | undefined);
const stateErr = sessionError?.errorMessage ?? sessionError?.error;
if (stateErr) {
throw new Error(stateErr);
}
} finally {
try {

View File

@@ -47,20 +47,28 @@ export interface PromptableSession extends AgentSession {
}
function getSessionStateError(session: AgentSession): string {
const error = (session as any).state?.error;
const state = (session as any).state;
const error = state?.errorMessage ?? state?.error;
return typeof error === "string" ? error : "";
}
function clearSessionStateError(session: AgentSession): void {
const state = (session as any).state;
if (!state || typeof state !== "object" || !("error" in state)) {
if (!state || typeof state !== "object") {
return;
}
try {
state.error = undefined;
} catch {
// Best effort only. Some session implementations may expose readonly state.
// pi-coding-agent 0.70+ exposes `errorMessage` as readonly — writes are
// silently ignored. Pre-0.70 used mutable `state.error`. Best-effort clear
// both so transcripts carry forward to the next prompt cleanly.
for (const key of ["errorMessage", "error"]) {
if (key in state) {
try {
state[key] = undefined;
} catch {
// readonly — no-op
}
}
}
}

View File

@@ -55,16 +55,19 @@ export function isUsageLimitError(errorMessage: string): boolean {
* Check if an agent session resolved with an error after exhausting retries.
*
* pi-coding-agent's `session.prompt()` does **not** throw when retries are
* exhausted — it resolves normally and stores the error on `session.state.error`.
* Call this immediately after every `await session.prompt(...)` to re-raise
* the swallowed error so existing `catch` blocks (with `isUsageLimitError`
* checks) can detect rate-limit conditions and trigger `UsageLimitPauser`.
* exhausted — it resolves normally and stores the error on
* `session.state.errorMessage` (was `session.state.error` prior to
* pi-coding-agent 0.70). Call this immediately after every
* `await session.prompt(...)` to re-raise the swallowed error so existing
* `catch` blocks (with `isUsageLimitError` checks) can detect rate-limit
* conditions and trigger `UsageLimitPauser`.
*
* @param session — The agent session (or any object with `state.error?: string`)
* @throws {Error} If `session.state.error` is set and non-empty
* @param session — The agent session (or any object with `state.errorMessage?: string`)
* @throws {Error} If `session.state.errorMessage` is set and non-empty
*/
export function checkSessionError(session: { state: { error?: string } }): void {
const error = session.state?.error;
export function checkSessionError(session: { state: { errorMessage?: string; error?: string } }): void {
const state = session.state;
const error = state?.errorMessage ?? state?.error;
if (error) {
throw new Error(error);
}