11/**
22 * Promote certain machine-readable `lastError` strings to user-facing copy.
33 *
4- * The mesh-llm seam (Max's commit `5196203…`) flows like this:
5- * buzz-agent — gets HTTP 401/403 from the OpenAI-compatible mesh endpoint
6- * → raises `AgentError::LlmAuth("…")` (json_rpc_code `-32001`,
7- * Display prefix `"llm auth: …"`)
8- * buzz-acp — wraps it as `AcpError::AgentError("Agent reported error: llm auth: …")`
9- * desktop managed-agent supervisor — on nonzero exit, scans `read_log_tail`
10- * for `"Agent reported error:"` / `"llm auth:"` and persists
11- * that line into `ManagedAgent.lastError` instead of the
12- * generic `"harness exited with status …"`.
4+ * The error classification seam flows like this:
5+ * buzz-agent — classifies LLM failures into `AgentError` variants with
6+ * JSON-RPC codes (`-32001` auth, `-32002` model-not-found,
7+ * `-32000` generic), defined in `crates/buzz-agent/src/types.rs`.
8+ * buzz-acp — preserves the code structurally in
9+ * `AcpError::AgentError { code, message }`, whose Display is
10+ * `"Agent reported error (code N): message"`, and includes
11+ * `code` in `turn_error` observer events.
12+ * desktop supervisor — on nonzero exit, recovers `{ message, code }` from
13+ * the log tail (`managed_agents/storage.rs`) into
14+ * `ManagedAgent.lastError` / `lastErrorCode`.
1315 *
14- * v1 caveat (named, not hidden): the typed `-32001` code never reaches
15- * desktop structurally — desktop only supervises the child process and ACP's
16- * `ObserverHandle` is in-process inside that child. So `lastError` is the
17- * recovered string, not the original code. The follow-up to make this 9/10
18- * structural is an ACP status file or desktop-owned observer sink; that lives
19- * elsewhere. For now we match the string this function exists to render.
16+ * This function dispatches on the numeric code first (works for any harness),
17+ * then recovers a code embedded in the message string (handles records where
18+ * the `lastErrorCode` field was lost, e.g. downgrade or pre-code records with
19+ * new-format strings), then falls back to legacy string prefixes for records
20+ * written before structured codes existed.
2021 *
2122 * Returns:
2223 * - null when there's nothing to show (null/empty lastError).
2324 * - A `{ severity: "denied"; copy: string }` object for the auth-failure
24- * case, so the UI can render with the right visual weight (destructive).
25+ * and model-not-found cases, so the UI can render with the right visual
26+ * weight (destructive).
2527 * - A `{ severity: "generic"; copy: string }` pass-through for any other
2628 * lastError, so generic harness exits still surface their text instead of
2729 * being swallowed.
@@ -40,6 +42,13 @@ export const RELAY_MESH_DENIED_COPY =
4042export const MODEL_NOT_FOUND_COPY =
4143 "The configured model is not available — open agent settings and select a different one from the dropdown." ;
4244
45+ const EMBEDDED_CODE_RE = / ^ A g e n t r e p o r t e d e r r o r \( c o d e ( - ? \d + ) \) : / ;
46+
47+ function recoverEmbeddedCode ( trimmed : string ) : number | null {
48+ const match = EMBEDDED_CODE_RE . exec ( trimmed ) ;
49+ return match ? Number ( match [ 1 ] ) : null ;
50+ }
51+
4352export function friendlyAgentLastError (
4453 raw : string | null ,
4554 code ?: number | null ,
@@ -48,18 +57,25 @@ export function friendlyAgentLastError(
4857 const trimmed = raw . trim ( ) ;
4958 if ( trimmed . length === 0 ) return null ;
5059
51- // Structured code path — no string matching, works across all providers.
52- if ( code != null ) {
53- switch ( code ) {
60+ // Structured code first; a code embedded in the message string is the
61+ // same signal recovered from a record that lost the field.
62+ const effectiveCode = Number . isFinite ( code )
63+ ? ( code as number )
64+ : recoverEmbeddedCode ( trimmed ) ;
65+ if ( effectiveCode != null ) {
66+ switch ( effectiveCode ) {
5467 case - 32001 :
5568 return { severity : "denied" , copy : RELAY_MESH_DENIED_COPY } ;
5669 case - 32002 :
5770 return { severity : "denied" , copy : MODEL_NOT_FOUND_COPY } ;
5871 }
72+ // A structured code we don't recognize is authoritative — don't let
73+ // string patterns cross-classify it.
74+ return { severity : "generic" , copy : trimmed } ;
5975 }
6076
61- // Match either the unwrapped buzz-agent prefix or the buzz-acp wrap .
62- // The desktop supervisor recovers whichever appears first in the log tail .
77+ // Legacy string fallback for records written before codes existed .
78+ // Match either the unwrapped buzz-agent prefix or the buzz-acp v0 wrap .
6379 if (
6480 trimmed . startsWith ( "Agent reported error: llm auth:" ) ||
6581 trimmed . startsWith ( "llm auth:" )
@@ -77,5 +93,6 @@ export function friendlyAgentLastError(
7793 */
7894export function friendlyTurnErrorCopy ( raw : string , code : unknown ) : string {
7995 const numeric = code == null ? null : Number ( code ) ;
80- return friendlyAgentLastError ( raw , numeric ) ?. copy ?? raw ;
96+ const safe = Number . isFinite ( numeric ) ? ( numeric as number ) : null ;
97+ return friendlyAgentLastError ( raw , safe ) ?. copy ?? raw ;
8198}
0 commit comments