(sdk: ISdk, kv: StateKV)
| 44 | const ONE_HOUR_MS = 60 * 60 * 1000; |
| 45 | |
| 46 | export function registerDiagnosticsFunction(sdk: ISdk, kv: StateKV): void { |
| 47 | sdk.registerFunction("mem::diagnose", |
| 48 | async (data: { categories?: string[] }) => { |
| 49 | const categories = data.categories && data.categories.length > 0 |
| 50 | ? data.categories.filter((c) => ALL_CATEGORIES.includes(c)) |
| 51 | : ALL_CATEGORIES; |
| 52 | |
| 53 | const checks: DiagnosticCheck[] = []; |
| 54 | const now = Date.now(); |
| 55 | |
| 56 | if (categories.includes("actions")) { |
| 57 | const actions = await kv.list<Action>(KV.actions); |
| 58 | const allEdges = await kv.list<ActionEdge>(KV.actionEdges); |
| 59 | const leases = await kv.list<Lease>(KV.leases); |
| 60 | const actionMap = new Map(actions.map((a) => [a.id, a])); |
| 61 | |
| 62 | for (const action of actions) { |
| 63 | if (action.status === "active") { |
| 64 | const hasActiveLease = leases.some( |
| 65 | (l) => |
| 66 | l.actionId === action.id && |
| 67 | l.status === "active" && |
| 68 | new Date(l.expiresAt).getTime() > now, |
| 69 | ); |
| 70 | if (!hasActiveLease) { |
| 71 | checks.push({ |
| 72 | name: `active-no-lease:${action.id}`, |
| 73 | category: "actions", |
| 74 | status: "warn", |
| 75 | message: `Action "${action.title}" is active but has no active lease`, |
| 76 | fixable: false, |
| 77 | }); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (action.status === "blocked") { |
| 82 | const deps = allEdges.filter( |
| 83 | (e) => e.sourceActionId === action.id && e.type === "requires", |
| 84 | ); |
| 85 | if (deps.length > 0) { |
| 86 | const allDone = deps.every((d) => { |
| 87 | const target = actionMap.get(d.targetActionId); |
| 88 | return target && target.status === "done"; |
| 89 | }); |
| 90 | if (allDone) { |
| 91 | checks.push({ |
| 92 | name: `blocked-deps-done:${action.id}`, |
| 93 | category: "actions", |
| 94 | status: "fail", |
| 95 | message: `Action "${action.title}" is blocked but all dependencies are done`, |
| 96 | fixable: true, |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (action.status === "pending") { |
| 103 | const deps = allEdges.filter( |
nothing calls this directly
no test coverage detected