1- import { AppError , normalizeError , type NormalizedError } from '@agent-device/kernel/errors' ;
1+ import {
2+ AppError ,
3+ normalizeError ,
4+ readErrorCandidateViews ,
5+ type ErrorCandidateView ,
6+ type NormalizedError ,
7+ } from '@agent-device/kernel/errors' ;
28import { formatReplayDivergenceReport } from '@agent-device/contracts/divergence' ;
39
4- export async function printHumanError (
10+ export function printHumanError (
511 err : AppError | NormalizedError ,
612 options : { showDetails ?: boolean } = { } ,
7- ) : Promise < void > {
13+ ) : void {
814 const normalized = err instanceof AppError ? normalizeError ( err ) : err ;
9- const { formatErrorCandidateLines } = await import ( '../../daemon/handlers/error-candidates.ts' ) ;
1015 process . stderr . write ( `Error (${ normalized . code } ): ${ normalized . message } \n` ) ;
1116 if ( normalized . cause ) {
1217 const code = normalized . cause . code ? `${ normalized . cause . code } ` : '' ;
@@ -15,10 +20,7 @@ export async function printHumanError(
1520 if ( normalized . hint ) {
1621 process . stderr . write ( `Hint: ${ normalized . hint } \n` ) ;
1722 }
18- // #1597: printed unconditionally (not gated behind --debug), same as the
19- // divergence report below — an agent must see the candidate refs without a
20- // follow-up round trip.
21- const candidateLines = formatErrorCandidateLines ( normalized . details ) ;
23+ const candidateLines = formatErrorCandidateViews ( readErrorCandidateViews ( normalized . details ) ) ;
2224 if ( candidateLines . length > 0 ) {
2325 process . stderr . write ( `${ candidateLines . join ( '\n' ) } \n` ) ;
2426 }
@@ -44,3 +46,24 @@ export async function printHumanError(
4446 process . stderr . write ( `${ JSON . stringify ( normalized . details , null , 2 ) } \n` ) ;
4547 }
4648}
49+
50+ function formatErrorCandidateViews ( views : ErrorCandidateView [ ] ) : string [ ] {
51+ return views . flatMap ( ( view ) => {
52+ if ( view . kind === 'element-match' ) {
53+ const remaining = view . matches - view . candidates . length ;
54+ return [
55+ 'Candidates:' ,
56+ ...view . candidates . map (
57+ ( candidate ) => ` ${ pinCandidateLine ( candidate , view . refsGeneration ) } ` ,
58+ ) ,
59+ ...( remaining > 0 ? [ ` +${ remaining } more` ] : [ ] ) ,
60+ ] ;
61+ }
62+ return [ 'Devices:' , ...view . devices . map ( ( device ) => ` ${ device . id } ${ device . name } ` ) ] ;
63+ } ) ;
64+ }
65+
66+ function pinCandidateLine ( candidate : string , generation : number | undefined ) : string {
67+ if ( generation === undefined ) return candidate ;
68+ return candidate . replace ( / ^ @ ( e \d + ) (? = \s | $ ) / , `@$1~s${ generation } ` ) ;
69+ }
0 commit comments