@mono-agent/agent-runtime 0.18.2 → 0.18.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -323,6 +323,8 @@ export function buildDiagnostics(params) {
|
|
|
323
323
|
lastToolName,
|
|
324
324
|
structuredRetry,
|
|
325
325
|
contextCompactionDiagnostics,
|
|
326
|
+
transportErrorCode,
|
|
327
|
+
transportErrorSource,
|
|
326
328
|
} = params;
|
|
327
329
|
return {
|
|
328
330
|
provider_session_id: providerSessionId,
|
|
@@ -335,6 +337,15 @@ export function buildDiagnostics(params) {
|
|
|
335
337
|
pi_max_retries: maxRetries,
|
|
336
338
|
pi_transport_requested: piTransport,
|
|
337
339
|
...(lastToolName ? { last_tool_name: lastToolName } : {}),
|
|
340
|
+
// Present only when an opaque transport failure was resolved to a real
|
|
341
|
+
// reason. `provider_transport_error_source` says whether that came from the
|
|
342
|
+
// error's own cause chain (exact) or from process-wide correlation.
|
|
343
|
+
...(transportErrorCode
|
|
344
|
+
? {
|
|
345
|
+
provider_transport_error_code: transportErrorCode,
|
|
346
|
+
provider_transport_error_source: transportErrorSource,
|
|
347
|
+
}
|
|
348
|
+
: {}),
|
|
338
349
|
...structuredOutputRetryDiagnostics(
|
|
339
350
|
structuredRetry.attempts,
|
|
340
351
|
structuredRetry.reason,
|
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
} from "./pi-messages.js";
|
|
45
45
|
import { emitCaptured } from "./pi-events.js";
|
|
46
46
|
import { normalizePiErrorMessage } from "./pi-errors.js";
|
|
47
|
+
import { annotateProviderErrorMessage, installTransportErrorProbe } from "./transport-errors.js";
|
|
47
48
|
import {
|
|
48
49
|
runStructuredOutputFinalizationRetry,
|
|
49
50
|
shouldRetryStructuredOutputFinalization,
|
|
@@ -252,6 +253,10 @@ function splitUserContent(content) {
|
|
|
252
253
|
}
|
|
253
254
|
|
|
254
255
|
export async function generatePiNativeResponse(systemPrompt, options = {}) {
|
|
256
|
+
// Idempotent; arms the undici diagnostics-channel probe so a transport
|
|
257
|
+
// failure during this run can be resolved back to a real reason even after
|
|
258
|
+
// an intermediate layer flattens the Error to its message.
|
|
259
|
+
installTransportErrorProbe();
|
|
255
260
|
const resolved = options.model;
|
|
256
261
|
const start = Date.now();
|
|
257
262
|
const events = [];
|
|
@@ -682,7 +687,15 @@ export async function generatePiNativeResponse(systemPrompt, options = {}) {
|
|
|
682
687
|
: (stopReason === "error" || stopReason === "aborted"
|
|
683
688
|
? lastAssistant?.errorMessage || runError?.message || "Pi agent aborted before final output"
|
|
684
689
|
: (runError ? runError.message || String(runError) : null));
|
|
685
|
-
|
|
690
|
+
// pi-agent-core stores `error.message` on the failure assistant message and
|
|
691
|
+
// discards the cause, so `runError` is usually the only object still
|
|
692
|
+
// carrying one. When neither has a cause, fall back to the correlated
|
|
693
|
+
// transport probe rather than surfacing a bare "terminated".
|
|
694
|
+
const annotatedError = annotateProviderErrorMessage(
|
|
695
|
+
normalizePiErrorMessage(rawErrorMessage),
|
|
696
|
+
runError,
|
|
697
|
+
);
|
|
698
|
+
const errorMessage = annotatedError.message;
|
|
686
699
|
|
|
687
700
|
const structuredRetry = {
|
|
688
701
|
attempts: structuredOutputFinalizationRetryAttempts,
|
|
@@ -702,6 +715,8 @@ export async function generatePiNativeResponse(systemPrompt, options = {}) {
|
|
|
702
715
|
lastToolName: runState.lastToolName,
|
|
703
716
|
structuredRetry,
|
|
704
717
|
contextCompactionDiagnostics: runState.compaction.diagnostics,
|
|
718
|
+
transportErrorCode: annotatedError.causeCode,
|
|
719
|
+
transportErrorSource: annotatedError.causeSource,
|
|
705
720
|
});
|
|
706
721
|
const errorDetails = buildErrorDetails({
|
|
707
722
|
errorMessage,
|
|
@@ -812,7 +827,12 @@ export async function generatePiNativeResponse(systemPrompt, options = {}) {
|
|
|
812
827
|
// leaf for host/runtime-side throws that landed after the harness already
|
|
813
828
|
// mutated the live session (guards preserved in cleanupSessionOnThrow).
|
|
814
829
|
await cleanupSessionOnThrow(runState, { durableRepo });
|
|
815
|
-
|
|
830
|
+
// The throw path still holds the original Error, so its cause chain is the
|
|
831
|
+
// authoritative source here — no correlation guesswork needed.
|
|
832
|
+
const errorMessage = annotateProviderErrorMessage(
|
|
833
|
+
normalizePiErrorMessage(err?.message || String(err)),
|
|
834
|
+
err,
|
|
835
|
+
).message;
|
|
816
836
|
const isRetryable = retryableProviderFailureInfo({
|
|
817
837
|
errorText: errorMessage,
|
|
818
838
|
failureKind: "provider_unavailable",
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Recover the real reason behind opaque provider transport failures.
|
|
2
|
+
//
|
|
3
|
+
// Node's fetch (undici) reports a cut response body as `TypeError: terminated`
|
|
4
|
+
// with the actual reason — UND_ERR_BODY_TIMEOUT, ECONNRESET, "other side
|
|
5
|
+
// closed" — only on `error.cause`. Several layers between the socket and us
|
|
6
|
+
// flatten errors to `error.message`, most notably pi-agent-core's
|
|
7
|
+
// `handleRunFailure`, which stores `error.message` and drops the cause before
|
|
8
|
+
// any mono-agent code sees the Error object. The result is a run that fails
|
|
9
|
+
// with the single uninformative word "terminated".
|
|
10
|
+
//
|
|
11
|
+
// Two recovery paths, in order of trustworthiness:
|
|
12
|
+
//
|
|
13
|
+
// 1. `describeErrorCause` walks the cause chain of an Error we still hold.
|
|
14
|
+
// Exact, but only available where the original object survives.
|
|
15
|
+
// 2. `recentTransportErrorCode` reads a bounded ring of `undici:request:error`
|
|
16
|
+
// diagnostics-channel events. This sees the error before any library
|
|
17
|
+
// flattens it, at the cost of attribution: the channel is process-wide, so
|
|
18
|
+
// with concurrent requests in flight we cannot prove which run a given
|
|
19
|
+
// socket error belongs to. Correlation is reported as such, and a window
|
|
20
|
+
// containing conflicting codes reports ambiguity instead of guessing.
|
|
21
|
+
|
|
22
|
+
import diagnosticsChannel from "node:diagnostics_channel";
|
|
23
|
+
|
|
24
|
+
// Messages that carry no diagnostic content on their own. Annotating only these
|
|
25
|
+
// keeps already-descriptive provider errors untouched.
|
|
26
|
+
const OPAQUE_ERROR_RE = /^(?:terminated|fetch failed|connection error\.?|network error|socket hang up|premature close|other side closed)$/i;
|
|
27
|
+
|
|
28
|
+
const MAX_RECORDED = 32;
|
|
29
|
+
// A stream cut is observed on the socket within milliseconds of the rejection
|
|
30
|
+
// surfacing. Anything older is a different request's failure.
|
|
31
|
+
const DEFAULT_CORRELATION_WINDOW_MS = 5_000;
|
|
32
|
+
const MAX_CAUSE_DEPTH = 5;
|
|
33
|
+
|
|
34
|
+
/** @type {{code: string, at: number, origin: string|null}[]} */
|
|
35
|
+
const recorded = [];
|
|
36
|
+
let installed = false;
|
|
37
|
+
|
|
38
|
+
function codeFromError(error) {
|
|
39
|
+
if (!error || typeof error !== "object") return null;
|
|
40
|
+
const code = error.code ?? error.errno;
|
|
41
|
+
if (typeof code === "string" && code.trim()) return code.trim();
|
|
42
|
+
// Undici's timeout errors expose a stable `name` even when `code` is absent.
|
|
43
|
+
const name = typeof error.name === "string" ? error.name.trim() : "";
|
|
44
|
+
if (name && name !== "Error" && name !== "TypeError") return name;
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Walk an error's `cause` chain and return the first transport-level code found.
|
|
50
|
+
* Returns null when the chain carries no code (i.e. nothing worth appending).
|
|
51
|
+
* @param {unknown} error
|
|
52
|
+
* @returns {string|null}
|
|
53
|
+
*/
|
|
54
|
+
export function describeErrorCause(error) {
|
|
55
|
+
/** @type {any} */
|
|
56
|
+
let current = error;
|
|
57
|
+
for (let depth = 0; depth < MAX_CAUSE_DEPTH && current; depth += 1) {
|
|
58
|
+
// Skip the outermost error: its code (if any) is what the caller already
|
|
59
|
+
// has. We want the reason underneath it.
|
|
60
|
+
if (depth > 0) {
|
|
61
|
+
const code = codeFromError(current);
|
|
62
|
+
if (code) return code;
|
|
63
|
+
}
|
|
64
|
+
current = current?.cause;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Subscribe to undici's request-error channel. Idempotent and safe to call on
|
|
71
|
+
* every run; a Node build without the channel simply records nothing.
|
|
72
|
+
*/
|
|
73
|
+
export function installTransportErrorProbe() {
|
|
74
|
+
if (installed) return;
|
|
75
|
+
installed = true;
|
|
76
|
+
try {
|
|
77
|
+
diagnosticsChannel.subscribe("undici:request:error", (/** @type {any} */ message) => {
|
|
78
|
+
const error = message?.error;
|
|
79
|
+
const code = codeFromError(error) || describeErrorCause(error);
|
|
80
|
+
if (!code) return;
|
|
81
|
+
/** @type {string|null} */
|
|
82
|
+
let origin = null;
|
|
83
|
+
try {
|
|
84
|
+
const raw = message?.request?.origin;
|
|
85
|
+
origin = typeof raw === "string" ? raw : (raw?.origin ?? null);
|
|
86
|
+
} catch {
|
|
87
|
+
origin = null;
|
|
88
|
+
}
|
|
89
|
+
recorded.push({ code, at: Date.now(), origin });
|
|
90
|
+
if (recorded.length > MAX_RECORDED) recorded.splice(0, recorded.length - MAX_RECORDED);
|
|
91
|
+
});
|
|
92
|
+
} catch {
|
|
93
|
+
// Channel unavailable on this runtime: fall back to cause-chain walking only.
|
|
94
|
+
installed = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Most recent transport error code seen within the correlation window.
|
|
100
|
+
* `ambiguous` is true when the window holds more than one distinct code, in
|
|
101
|
+
* which case attribution to a specific run would be a guess.
|
|
102
|
+
* @param {{withinMs?: number, now?: number}} [opts]
|
|
103
|
+
* @returns {{code: string, ambiguous: boolean}|null}
|
|
104
|
+
*/
|
|
105
|
+
export function recentTransportErrorCode(opts = {}) {
|
|
106
|
+
const withinMs = Number.isFinite(Number(opts.withinMs))
|
|
107
|
+
? Number(opts.withinMs)
|
|
108
|
+
: DEFAULT_CORRELATION_WINDOW_MS;
|
|
109
|
+
const now = Number.isFinite(Number(opts.now)) ? Number(opts.now) : Date.now();
|
|
110
|
+
const fresh = recorded.filter((entry) => now - entry.at <= withinMs);
|
|
111
|
+
if (fresh.length === 0) return null;
|
|
112
|
+
const distinct = new Set(fresh.map((entry) => entry.code));
|
|
113
|
+
return { code: fresh[fresh.length - 1].code, ambiguous: distinct.size > 1 };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Append the underlying transport reason to an otherwise contentless provider
|
|
118
|
+
* error. Descriptive messages, and messages that already name their cause, are
|
|
119
|
+
* returned unchanged.
|
|
120
|
+
*
|
|
121
|
+
* @param {string|null} message normalized provider error text
|
|
122
|
+
* @param {unknown} [error] the original Error, when it survived
|
|
123
|
+
* @returns {{message: string|null, causeCode: string|null, causeSource: "cause_chain"|"transport_probe"|null}}
|
|
124
|
+
*/
|
|
125
|
+
export function annotateProviderErrorMessage(message, error) {
|
|
126
|
+
const text = String(message || "").trim();
|
|
127
|
+
if (!text) return { message: message ?? null, causeCode: null, causeSource: null };
|
|
128
|
+
if (!OPAQUE_ERROR_RE.test(text)) return { message: text, causeCode: null, causeSource: null };
|
|
129
|
+
|
|
130
|
+
const fromChain = describeErrorCause(error);
|
|
131
|
+
if (fromChain) {
|
|
132
|
+
return { message: `${text} (${fromChain})`, causeCode: fromChain, causeSource: "cause_chain" };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const correlated = recentTransportErrorCode();
|
|
136
|
+
if (correlated && !correlated.ambiguous) {
|
|
137
|
+
return {
|
|
138
|
+
message: `${text} (${correlated.code}, correlated)`,
|
|
139
|
+
causeCode: correlated.code,
|
|
140
|
+
causeSource: "transport_probe",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return { message: text, causeCode: null, causeSource: null };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Test seam: drop recorded transport errors. */
|
|
147
|
+
export function resetTransportErrorProbeForTests() {
|
|
148
|
+
recorded.length = 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Test seam: record a transport error without a real socket. */
|
|
152
|
+
export function recordTransportErrorForTests(code, at = Date.now()) {
|
|
153
|
+
recorded.push({ code, at, origin: null });
|
|
154
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walk an error's `cause` chain and return the first transport-level code found.
|
|
3
|
+
* Returns null when the chain carries no code (i.e. nothing worth appending).
|
|
4
|
+
* @param {unknown} error
|
|
5
|
+
* @returns {string|null}
|
|
6
|
+
*/
|
|
7
|
+
export function describeErrorCause(error: unknown): string | null;
|
|
8
|
+
/**
|
|
9
|
+
* Subscribe to undici's request-error channel. Idempotent and safe to call on
|
|
10
|
+
* every run; a Node build without the channel simply records nothing.
|
|
11
|
+
*/
|
|
12
|
+
export function installTransportErrorProbe(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Most recent transport error code seen within the correlation window.
|
|
15
|
+
* `ambiguous` is true when the window holds more than one distinct code, in
|
|
16
|
+
* which case attribution to a specific run would be a guess.
|
|
17
|
+
* @param {{withinMs?: number, now?: number}} [opts]
|
|
18
|
+
* @returns {{code: string, ambiguous: boolean}|null}
|
|
19
|
+
*/
|
|
20
|
+
export function recentTransportErrorCode(opts?: {
|
|
21
|
+
withinMs?: number;
|
|
22
|
+
now?: number;
|
|
23
|
+
}): {
|
|
24
|
+
code: string;
|
|
25
|
+
ambiguous: boolean;
|
|
26
|
+
} | null;
|
|
27
|
+
/**
|
|
28
|
+
* Append the underlying transport reason to an otherwise contentless provider
|
|
29
|
+
* error. Descriptive messages, and messages that already name their cause, are
|
|
30
|
+
* returned unchanged.
|
|
31
|
+
*
|
|
32
|
+
* @param {string|null} message normalized provider error text
|
|
33
|
+
* @param {unknown} [error] the original Error, when it survived
|
|
34
|
+
* @returns {{message: string|null, causeCode: string|null, causeSource: "cause_chain"|"transport_probe"|null}}
|
|
35
|
+
*/
|
|
36
|
+
export function annotateProviderErrorMessage(message: string | null, error?: unknown): {
|
|
37
|
+
message: string | null;
|
|
38
|
+
causeCode: string | null;
|
|
39
|
+
causeSource: "cause_chain" | "transport_probe" | null;
|
|
40
|
+
};
|
|
41
|
+
/** Test seam: drop recorded transport errors. */
|
|
42
|
+
export function resetTransportErrorProbeForTests(): void;
|
|
43
|
+
/** Test seam: record a transport error without a real socket. */
|
|
44
|
+
export function recordTransportErrorForTests(code: any, at?: number): void;
|