forked from snarktank/antfarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks.ts
More file actions
234 lines (203 loc) · 7.81 KB
/
Copy pathchecks.ts
File metadata and controls
234 lines (203 loc) · 7.81 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
/**
* Medic health checks — modular functions that inspect DB state and return findings.
*/
import fs from "node:fs/promises";
import { getDb } from "../db.js";
import { getMaxRoleTimeoutSeconds } from "../installer/install.js";
import { loadWorkflowSpec } from "../installer/workflow-spec.js";
import { resolveWorkflowDir, resolveWorkflowRoot } from "../installer/paths.js";
import type { WorkflowSpec } from "../installer/types.js";
export type MedicSeverity = "info" | "warning" | "critical";
export type MedicActionType =
| "reset_step"
| "fail_run"
| "teardown_crons"
| "none";
export interface MedicFinding {
check: string;
severity: MedicSeverity;
message: string;
action: MedicActionType;
/** IDs of affected entities */
runId?: string;
stepId?: string;
/** Whether the medic auto-remediated this */
remediated: boolean;
}
// ── Check: Stuck Steps ──────────────────────────────────────────────
const MAX_ROLE_TIMEOUT_MS = (getMaxRoleTimeoutSeconds() + 5 * 60) * 1000;
/**
* Find steps that have been "running" longer than the max role timeout.
* These are likely abandoned by crashed/timed-out agents.
*/
export function checkStuckSteps(): MedicFinding[] {
const db = getDb();
const findings: MedicFinding[] = [];
const stuck = db.prepare(`
SELECT s.id, s.step_id, s.run_id, s.agent_id, s.updated_at, s.abandoned_count,
r.workflow_id, r.task
FROM steps s
JOIN runs r ON r.id = s.run_id
WHERE s.status = 'running'
AND r.status = 'running'
AND (julianday('now') - julianday(s.updated_at)) * 86400000 > ?
`).all(MAX_ROLE_TIMEOUT_MS) as Array<{
id: string; step_id: string; run_id: string; agent_id: string;
updated_at: string; abandoned_count: number; workflow_id: string; task: string;
}>;
for (const step of stuck) {
const ageMin = Math.round(
(Date.now() - new Date(step.updated_at).getTime()) / 60000
);
findings.push({
check: "stuck_steps",
severity: "warning",
message: `Step "${step.step_id}" in run ${step.run_id.slice(0, 8)} (${step.workflow_id}) has been running for ${ageMin}min — likely abandoned by agent ${step.agent_id}`,
action: "reset_step",
runId: step.run_id,
stepId: step.id,
remediated: false, // caller decides whether to remediate
});
}
return findings;
}
// ── Check: Stalled Runs ─────────────────────────────────────────────
const STALL_THRESHOLD_MS = MAX_ROLE_TIMEOUT_MS * 2;
/**
* Find runs where no step has transitioned in 2x the max role timeout.
* This catches systemic issues (all agents broken, crons failing, etc).
*/
export function checkStalledRuns(): MedicFinding[] {
const db = getDb();
const findings: MedicFinding[] = [];
// Get running runs where the most recent step update is stale
const stalled = db.prepare(`
SELECT r.id, r.workflow_id, r.task, r.updated_at,
MAX(s.updated_at) as last_step_update
FROM runs r
JOIN steps s ON s.run_id = r.id
WHERE r.status = 'running'
GROUP BY r.id
HAVING (julianday('now') - julianday(MAX(s.updated_at))) * 86400000 > ?
`).all(STALL_THRESHOLD_MS) as Array<{
id: string; workflow_id: string; task: string;
updated_at: string; last_step_update: string;
}>;
for (const run of stalled) {
const ageMin = Math.round(
(Date.now() - new Date(run.last_step_update).getTime()) / 60000
);
findings.push({
check: "stalled_runs",
severity: "critical",
message: `Run ${run.id.slice(0, 8)} (${run.workflow_id}: "${run.task.slice(0, 60)}") has had no step progress for ${ageMin}min`,
action: "none", // alert only — don't auto-fail without human input
runId: run.id,
remediated: false,
});
}
return findings;
}
// ── Check: Dead Runs ────────────────────────────────────────────────
/**
* Find runs marked as "running" but all steps are terminal (done/failed)
* with no waiting/pending/running steps left. These are zombie runs.
*/
export function checkDeadRuns(): MedicFinding[] {
const db = getDb();
const findings: MedicFinding[] = [];
const zombies = db.prepare(`
SELECT r.id, r.workflow_id, r.task
FROM runs r
WHERE r.status = 'running'
AND NOT EXISTS (
SELECT 1 FROM steps s
WHERE s.run_id = r.id
AND s.status IN ('waiting', 'pending', 'running')
)
`).all() as Array<{ id: string; workflow_id: string; task: string }>;
for (const run of zombies) {
// Check if all steps are done (should be completed) or some are failed
const failed = db.prepare(
"SELECT COUNT(*) as cnt FROM steps WHERE run_id = ? AND status = 'failed'"
).get(run.id) as { cnt: number };
const action: MedicActionType = "fail_run";
const detail = failed.cnt > 0
? `${failed.cnt} failed step(s), no active steps remaining`
: `All steps terminal but run still marked as running`;
findings.push({
check: "dead_runs",
severity: "critical",
message: `Run ${run.id.slice(0, 8)} (${run.workflow_id}) is a zombie — ${detail}`,
action,
runId: run.id,
remediated: false,
});
}
return findings;
}
// ── Check: Orphaned Crons ───────────────────────────────────────────
/**
* Check if agent crons exist for workflows with zero active runs.
* Returns workflow IDs that should have their crons torn down.
*
* NOTE: This check requires the list of current cron jobs to be passed in,
* since reading crons is async (gateway API). The medic runner handles this.
*/
export function checkOrphanedCrons(
cronJobs: Array<{ id: string; name: string }>,
): MedicFinding[] {
const db = getDb();
const findings: MedicFinding[] = [];
// Extract unique workflow IDs from antfarm cron job names
const workflowIds = new Set<string>();
for (const job of cronJobs) {
const match = job.name.match(/^antfarm\/([^/]+)\//);
if (match) workflowIds.add(match[1]);
}
for (const wfId of workflowIds) {
const active = db.prepare(
"SELECT COUNT(*) as cnt FROM runs WHERE workflow_id = ? AND status = 'running'"
).get(wfId) as { cnt: number };
if (active.cnt === 0) {
const jobCount = cronJobs.filter(j => j.name.startsWith(`antfarm/${wfId}/`)).length;
findings.push({
check: "orphaned_crons",
severity: "warning",
message: `${jobCount} cron job(s) for workflow "${wfId}" still running but no active runs exist`,
action: "teardown_crons",
remediated: false,
});
}
}
return findings;
}
// ── Workflow Spec Loader for Medic ─────────────────────────────────
/**
* Load a workflow spec by its ID from disk.
* Returns null if the workflow directory doesn't exist.
* Used by medic to determine expected agents for cron gap detection.
*/
export async function loadWorkflowSpecForMedic(
workflowId: string
): Promise<WorkflowSpec | null> {
const workflowDir = resolveWorkflowDir(workflowId);
try {
await fs.access(workflowDir);
} catch {
// Directory doesn't exist
return null;
}
return loadWorkflowSpec(workflowDir);
}
// ── Run All Checks ──────────────────────────────────────────────────
/**
* Run all synchronous checks (everything except orphaned crons which needs async cron list).
*/
export function runSyncChecks(): MedicFinding[] {
return [
...checkStuckSteps(),
...checkStalledRuns(),
...checkDeadRuns(),
];
}