-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtypes.ts
More file actions
336 lines (296 loc) · 9.04 KB
/
Copy pathtypes.ts
File metadata and controls
336 lines (296 loc) · 9.04 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { z } from 'zod';
// ==========================================
// COMMON SCHEMAS AND TYPES
// ==========================================
export const StreamTypeSchema = z.enum(['stdout', 'stderr']);
export type StreamType = z.infer<typeof StreamTypeSchema>;
export const LogLevelSchema = z.enum([
'debug', // Detailed diagnostic information
'info', // General informational messages
'warn', // Warning messages (non-error issues)
'error', // Error messages (already handled by error system)
'output' // Raw process output (stdout/stderr)
]);
export type LogLevel = z.infer<typeof LogLevelSchema>;
// ==========================================
// SIMPLIFIED ERROR TYPE FOR JSON LOGS
// ==========================================
export const SimpleErrorSchema = z.object({
timestamp: z.string(), // ISO timestamp
level: z.number(), // Pino log level (50=error, 60=fatal)
message: z.string(), // The 'msg' field from JSON log
rawOutput: z.string() // The complete raw JSON log line
});
export type SimpleError = z.infer<typeof SimpleErrorSchema>;
// ==========================================
// LOG TYPES
// ==========================================
export interface LogLine {
readonly content: string;
readonly timestamp: Date;
readonly stream: StreamType;
readonly processId: string;
}
// ==========================================
// STORAGE SCHEMAS - Extend base types
// ==========================================
// StoredError extends SimpleError with storage-specific fields
export const StoredErrorSchema = SimpleErrorSchema.extend({
id: z.number(),
instanceId: z.string(),
processId: z.string(),
errorHash: z.string(),
occurrenceCount: z.number(),
createdAt: z.string()
});
export type StoredError = z.infer<typeof StoredErrorSchema>;
// Base fields shared by stored entities
const StoredEntityBaseSchema = z.object({
id: z.number(),
instanceId: z.string(),
processId: z.string(),
timestamp: z.string(),
createdAt: z.string()
});
// StoredLog extends base with log-specific fields
export const StoredLogSchema = StoredEntityBaseSchema.extend({
level: LogLevelSchema,
message: z.string(),
stream: StreamTypeSchema,
source: z.string().optional(),
metadata: z.string().nullable(),
sequence: z.number()
});
export type StoredLog = z.infer<typeof StoredLogSchema>;
// ==========================================
// PROCESS MONITORING TYPES
// ==========================================
export const ProcessStateSchema = z.enum([
'starting',
'running',
'stopping',
'stopped',
'crashed'
]);
export type ProcessState = z.infer<typeof ProcessStateSchema>;
export interface ProcessInfo {
readonly id: string;
readonly instanceId: string;
readonly command: string;
readonly args?: readonly string[];
readonly cwd: string;
pid?: number;
readonly env?: Record<string, string>;
readonly startTime?: Date;
readonly status?: ProcessState;
readonly endTime?: Date;
readonly exitCode?: number;
readonly restartCount: number;
readonly lastError?: string;
}
export interface MonitoringOptions {
readonly autoRestart?: boolean;
readonly maxRestarts?: number;
readonly restartDelay?: number;
readonly healthCheckInterval?: number;
readonly errorBufferSize?: number;
readonly env?: Record<string, string>;
readonly killTimeout?: number;
readonly expectedPort?: number; // Port the child process should bind to (for health checks)
}
// ==========================================
// STORAGE OPTIONS
// ==========================================
// Base storage options shared by error and log stores
interface BaseStoreOptions {
readonly vacuumInterval?: number; // Hours between cleanup runs
}
export interface ErrorStoreOptions extends BaseStoreOptions {
readonly maxErrors?: number;
readonly retentionDays?: number;
}
export interface LogStoreOptions extends BaseStoreOptions {
readonly maxLogs?: number;
readonly retentionHours?: number;
readonly bufferSize?: number;
}
// ==========================================
// FILTER & CURSOR TYPES
// ==========================================
// Base filter options shared by all filters
interface BaseFilter {
readonly instanceId?: string;
readonly since?: Date;
readonly until?: Date;
readonly limit?: number;
readonly offset?: number;
readonly sortOrder?: 'asc' | 'desc';
}
export interface ErrorFilter extends BaseFilter {
readonly level?: number;
readonly includeRaw?: boolean;
readonly sortBy?: 'timestamp' | 'occurrenceCount';
}
export interface LogFilter extends BaseFilter {
readonly levels?: readonly LogLevel[];
readonly streams?: readonly StreamType[];
readonly includeMetadata?: boolean;
readonly afterSequence?: number;
}
export interface LogCursor {
readonly instanceId: string;
readonly lastSequence: number;
readonly lastRetrieved: Date;
}
// ==========================================
// SUMMARY TYPES
// ==========================================
export interface ErrorSummary {
readonly totalErrors: number;
readonly errorsByLevel: Record<number, number>;
readonly latestError?: Date;
readonly oldestError?: Date;
readonly uniqueErrors: number;
readonly repeatedErrors: number;
}
export interface LogRetrievalResponse {
readonly success: boolean;
readonly logs: readonly StoredLog[];
readonly cursor: LogCursor;
readonly hasMore: boolean;
readonly totalCount?: number;
readonly error?: string;
}
// ==========================================
// MONITORING EVENTS
// ==========================================
export type MonitoringEvent =
| {
type: 'process_started';
processId: string;
instanceId: string;
pid?: number;
command?: string;
timestamp: Date;
}
| {
type: 'process_stopped';
processId: string;
instanceId: string;
exitCode?: number | null;
reason?: string;
timestamp: Date;
}
| {
type: 'process_exited';
processId: string;
instanceId: string;
code: number | null;
signal: NodeJS.Signals | null;
timestamp: Date;
}
| {
type: 'process_error';
processId: string;
instanceId: string;
error: string;
timestamp: Date;
}
| {
type: 'error_detected';
processId: string;
instanceId: string;
error: SimpleError;
timestamp: Date;
}
| {
type: 'process_crashed';
processId: string;
instanceId: string;
exitCode?: number | null;
signal?: string | null;
willRestart?: boolean;
timestamp: Date;
}
| {
type: 'restart_failed';
processId: string;
instanceId: string;
attempt: number;
error?: string;
timestamp: Date;
}
| {
type: 'health_check_failed';
processId: string;
instanceId: string;
lastActivity: Date;
timestamp: Date;
}
| {
type: 'state_changed';
processId: string;
instanceId: string;
oldState: ProcessState;
newState: ProcessState;
timestamp: Date;
};
// ==========================================
// CONFIGURATION TYPES
// ==========================================
// Combines ProcessInfo with monitoring and storage config
export interface ProcessRunnerConfig {
readonly instanceId: string;
readonly command: string;
readonly args: readonly string[];
readonly cwd: string;
readonly monitoring?: MonitoringOptions;
readonly storage?: {
readonly error?: ErrorStoreOptions;
readonly log?: LogStoreOptions;
};
}
// ==========================================
// UTILITY TYPES
// ==========================================
export type Result<T, E = Error> =
| { readonly success: true; readonly data: T }
| { readonly success: false; readonly error: E };
// ==========================================
// CONSTANTS
// ==========================================
// Note: expectedPort is optional so we use Omit to exclude it from Required
export const DEFAULT_MONITORING_OPTIONS: Omit<Required<MonitoringOptions>, 'expectedPort'> & { expectedPort?: number } = {
autoRestart: true,
maxRestarts: 3,
restartDelay: 1000,
errorBufferSize: 100,
healthCheckInterval: 30000,
env: {},
killTimeout: 10000,
expectedPort: undefined
} as const;
export const DEFAULT_STORAGE_OPTIONS: ErrorStoreOptions = {
maxErrors: 1000,
retentionDays: 7,
vacuumInterval: 24
} as const;
export const DEFAULT_LOG_STORE_OPTIONS: LogStoreOptions = {
maxLogs: 10000,
retentionHours: 168, // 7 days
bufferSize: 1000
} as const;
// Configurable paths - use environment variables or default to ./data directory
export const getDataDirectory = (): string => {
return process.env.CLI_DATA_DIR || './.data';
};
export const getErrorDbPath = (): string => {
return process.env.CLI_ERROR_DB_PATH || `${getDataDirectory()}/errors.db`;
};
export const getLogDbPath = (): string => {
return process.env.CLI_LOG_DB_PATH || `${getDataDirectory()}/logs.db`;
};
// Legacy constants for backward compatibility
export const ERROR_DB_PATH = getErrorDbPath();
export const LOG_DB_PATH = getLogDbPath();
export const ERROR_HASH_ALGORITHM = 'sha256' as const;