Files
devclaw-gitea/lib/tools/health.ts
Lauren ten Hoor be8e0f4db1 refactor: Centralize role types and configuration (#190)
Creates a single source of truth for all worker roles via lib/roles/.

## New: lib/roles/

- **registry.ts** — All role definitions (dev, qa, architect) with
  levels, models, emoji, completion results, session key patterns
- **types.ts** — RoleConfig interface
- **selectors.ts** — Query helpers: getRole(), getLevelsForRole(),
  resolveModel(), isValidResult(), roleForLevel(), etc.
- **index.ts** — Barrel exports

## Migrated Files

- **lib/tiers.ts** — Now delegates to registry (backward compat kept)
- **lib/dispatch.ts** — Uses registry for emoji resolution
- **lib/bootstrap-hook.ts** — Uses registry for session key pattern
- **lib/services/tick.ts** — Uses registry for level detection
- **lib/services/heartbeat.ts** — Uses registry for role iteration
- **lib/tools/health.ts** — Uses registry for role iteration
- **lib/tools/work-start.ts** — Uses registry for role enum
- **lib/tools/work-finish.ts** — Uses registry for result validation
- **lib/tools/project-register.ts** — Uses registry for level lists

## Key Benefits

- Adding a new role = add entry to registry.ts (single file)
- No more scattered role unions ("dev" | "qa" | "architect")
- Type-safe role/level/result validation from registry
- Session key pattern auto-generated from registry
- All 64 tests passing (22 new registry tests + 42 existing)
2026-02-14 17:15:54 +08:00

99 lines
3.6 KiB
TypeScript

/**
* health — Worker health scan with optional auto-fix.
*
* Triangulates projects.json, issue labels, and session state to detect:
* - session_dead: active worker but session missing in gateway
* - label_mismatch: active worker but issue not in expected label
* - stale_worker: active for >2h
* - stuck_label: inactive but issue has Doing/Testing label
* - orphan_issue_id: inactive but issueId set
* - issue_gone: active but issue deleted/closed
* - orphaned_label: active label but no worker tracking it (NEW)
*
* Read-only by default (surfaces issues). Pass fix=true to apply fixes.
*/
import { jsonResult } from "openclaw/plugin-sdk";
import type { ToolContext } from "../types.js";
import { readProjects, getProject } from "../projects.js";
import { log as auditLog } from "../audit.js";
import { checkWorkerHealth, scanOrphanedLabels, fetchGatewaySessions, type HealthFix } from "../services/health.js";
import { requireWorkspaceDir, resolveProvider } from "../tool-helpers.js";
import { getAllRoleIds } from "../roles/index.js";
export function createHealthTool() {
return (ctx: ToolContext) => ({
name: "health",
label: "Health",
description: `Scan worker health across projects. Detects zombies, stale workers, orphaned state. Pass fix=true to auto-fix. Context-aware: auto-filters in group chats.`,
parameters: {
type: "object",
properties: {
projectGroupId: { type: "string", description: "Filter to specific project. Omit for all." },
fix: { type: "boolean", description: "Apply fixes for detected issues. Default: false (read-only)." },
},
},
async execute(_id: string, params: Record<string, unknown>) {
const workspaceDir = requireWorkspaceDir(ctx);
const fix = (params.fix as boolean) ?? false;
const groupId = params.projectGroupId as string | undefined;
const data = await readProjects(workspaceDir);
const projectIds = groupId ? [groupId] : Object.keys(data.projects);
// Fetch gateway sessions once for all projects
const sessions = await fetchGatewaySessions();
const issues: Array<HealthFix & { project: string; role: string }> = [];
for (const pid of projectIds) {
const project = getProject(data, pid);
if (!project) continue;
const { provider } = await resolveProvider(project);
for (const role of getAllRoleIds()) {
// Worker health check (session liveness, label consistency, etc)
const healthFixes = await checkWorkerHealth({
workspaceDir,
groupId: pid,
project,
role: role as any,
sessions,
autoFix: fix,
provider,
});
issues.push(...healthFixes.map((f) => ({ ...f, project: project.name, role })));
// Orphaned label scan (active labels with no tracking worker)
const orphanFixes = await scanOrphanedLabels({
workspaceDir,
groupId: pid,
project,
role: role as any,
autoFix: fix,
provider,
});
issues.push(...orphanFixes.map((f) => ({ ...f, project: project.name, role })));
}
}
await auditLog(workspaceDir, "health", {
projectCount: projectIds.length,
fix,
issuesFound: issues.length,
issuesFixed: issues.filter((i) => i.fixed).length,
sessionsCached: sessions?.size ?? 0,
});
return jsonResult({
success: true,
fix,
projectsScanned: projectIds.length,
sessionsQueried: sessions?.size ?? 0,
issues,
});
},
});
}