-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.ts
157 lines (136 loc) · 3.34 KB
/
logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Failure, Stopped, type AgentRuntimeEvent } from '../agent';
import type { DiffOperation } from '../operation';
import type { PlanAction } from '../planner';
import { SearchFailed } from '../planner';
import { diff } from '../distance';
import * as DAG from '../dag';
export interface Logger {
debug(...args: any[]): void;
info(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
}
export const NullLogger: Logger = {
debug: () => {
/* noop*/
},
info: () => {
/* noop*/
},
warn: () => {
/* noop*/
},
error: () => {
/* noop*/
},
};
export type Trace<S> = (e: AgentRuntimeEvent<S>) => void;
/**
* Create a human readable tracer of events during agent runtime
*/
export function readableTrace<S = any>(logger: Partial<Logger>): Trace<S> {
const log = {
...NullLogger,
...logger,
};
const toLog = (o: DiffOperation<S, any>) => {
if (o.op === 'create') {
return ['create', o.path, 'with value', o.target];
}
if (o.op === 'update') {
return ['update', o.path, 'from', o.source, 'to', o.target];
}
return ['delete', o.path];
};
return function (e: AgentRuntimeEvent<S>) {
switch (e.event) {
case 'start': {
log.info('applying new target state');
return;
}
case 'find-plan':
log.info('looking for a plan');
{
// avoid no-case-declarations
const changes = diff(e.state, e.target);
log.debug(`pending changes:${changes.length > 0 ? '' : ' none'}`);
changes.map(toLog).forEach((change) => {
log.debug('-', ...change);
});
}
break;
case 'plan-found':
log.info(
`plan found after ${
e.stats.iterations
} iterations in ${e.stats.time.toFixed(1)}ms`,
);
log.debug('will execute the following actions:');
DAG.toString(e.start, (a: PlanAction<S>) => a.action.description)
.split('\n')
.map((action) => {
log.debug(action);
});
break;
case 'plan-not-found':
if (e.cause !== SearchFailed) {
log.error(
'no plan found, reason:',
(e.cause as Error).message ?? e.cause,
);
} else {
log.warn('no plan found');
}
break;
case 'plan-timeout':
log.error(
`planning timed-out after ${e.timeout}(ms), this might be a bug`,
);
return;
case 'plan-executed': {
log.info('plan executed successfully');
return;
}
case 'action-condition-failed': {
log.warn(`${e.action.description}: condition failed`);
return;
}
case 'action-start': {
log.info(`${e.action.description}: running ...`);
return;
}
case 'action-success': {
log.info(`${e.action.description}: success`);
return;
}
case 'action-failure': {
log.error(`${e.action.description}: failed`, e.cause);
return;
}
case 'backoff': {
log.debug(`waiting ${e.delayMs / 1000}s before re - planning`);
return;
}
case 'success': {
log.info('nothing else to do: target state reached');
return;
}
case 'failure':
if (e.cause instanceof Failure) {
log.error(
`aborting target apply after ${e.cause.tries} failed attempts`,
);
} else if (e.cause instanceof Stopped) {
log.warn('plan execution cancelled');
} else {
log.error(
`unknown error occured while applying target state:`,
(e as any)?.message ?? e,
);
}
break;
default:
break;
}
};
}