refactor(agent-loop): inline the turn error shaping

This commit is contained in:
_Kerman
2026-08-03 16:43:45 +08:00
parent a597763393
commit f0622f3280

View File

@@ -60,20 +60,6 @@ function requestProposal(header: EpochHeader): LlmCallConfig {
}
/** Drives one session through turn and step boundaries. */
/**
* Shape a caught turn error into its durable reason: every failure is a
* structured LlmFailure — an `LlmError` keeps its facts, anything else
* flattens to `errorChain` text under the `UNKNOWN` code.
*/
function turnErrorReason(error: unknown): Extract<TurnEndReason, { kind: 'error' }> {
return {
kind: 'error',
error: error instanceof LlmError
? error.failure
: { message: errorChain(error), code: 'UNKNOWN' },
}
}
export class ReactLoopAgent implements Agent {
readonly inbox: Inbox
private phase: Phase
@@ -293,7 +279,14 @@ export class ReactLoopAgent implements Agent {
turnEnds = { kind: 'aborted', reason: signal.reason as AgentCancelCause }
throw error
}
turnEnds = turnErrorReason(error)
// Every failure is structured: an `LlmError` keeps its facts, anything
// else flattens to `errorChain` text under the `UNKNOWN` code.
turnEnds = {
kind: 'error',
error: error instanceof LlmError
? error.failure
: { message: errorChain(error), code: 'UNKNOWN' },
}
this.throwError(error)
} finally {
try {