feat: implement work heartbeat service for health checks and task dispatching

- Introduced a new heartbeat service that runs at defined intervals to perform health checks on workers and fill available task slots based on priority.
- Added a health tool to scan worker health across projects with optional auto-fix capabilities.
- Updated the status tool to provide a lightweight overview of worker states and queue counts without health checks.
- Enhanced task creation tool descriptions to clarify task state handling.
- Implemented tests for the work heartbeat logic, ensuring proper project resolution, worker state management, and task prioritization.
This commit is contained in:
Lauren ten Hoor
2026-02-11 01:04:30 +08:00
parent 71a3ea2352
commit f2e71a35d8
13 changed files with 1044 additions and 426 deletions

View File

@@ -1,40 +1,33 @@
/**
* status — Unified queue + health overview.
* status — Lightweight queue + worker state dashboard.
*
* Merges queue_status + session_health into a single tool.
* Shows worker state and queue counts per project. No health checks
* (use `health` tool), no complex sequencing.
* Context-aware: auto-filters to project in group chats.
*/
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { jsonResult } from "openclaw/plugin-sdk";
import type { ToolContext } from "../types.js";
import { readProjects, getProject, type Project } from "../projects.js";
import { readProjects, getProject } from "../projects.js";
import { generateGuardrails } from "../context-guard.js";
import { log as auditLog } from "../audit.js";
import { checkWorkerHealth } from "../services/health.js";
import {
fetchProjectQueues, buildParallelProjectSequences, buildGlobalTaskSequence,
formatProjectQueues, type ProjectQueues, type ProjectExecutionConfig,
} from "../services/queue.js";
import { requireWorkspaceDir, resolveContext, resolveProvider, getPluginConfig } from "../tool-helpers.js";
import { fetchProjectQueues, type QueueLabel } from "../services/queue.js";
import { requireWorkspaceDir, resolveContext, getPluginConfig } from "../tool-helpers.js";
export function createStatusTool(api: OpenClawPluginApi) {
return (ctx: ToolContext) => ({
name: "status",
label: "Status",
description: `Show task queue, worker status, and health across projects. Context-aware: auto-filters in group chats. Pass activeSessions for zombie detection.`,
description: `Show task queue and worker state per project. Context-aware: auto-filters in group chats. Use \`health\` tool for worker health checks.`,
parameters: {
type: "object",
properties: {
projectGroupId: { type: "string", description: "Filter to specific project. Omit for all." },
includeHealth: { type: "boolean", description: "Run health checks. Default: true." },
activeSessions: { type: "array", items: { type: "string" }, description: "Active session IDs for zombie detection." },
},
},
async execute(_id: string, params: Record<string, unknown>) {
const workspaceDir = requireWorkspaceDir(ctx);
const includeHealth = (params.includeHealth as boolean) ?? true;
const activeSessions = (params.activeSessions as string[]) ?? [];
const context = await resolveContext(ctx, api);
if (context.type === "via-agent") {
@@ -51,75 +44,42 @@ export function createStatusTool(api: OpenClawPluginApi) {
if (context.type === "group" && !groupId) groupId = context.groupId;
const pluginConfig = getPluginConfig(api);
const projectExecution = (pluginConfig?.projectExecution as "parallel" | "sequential") ?? "parallel";
const projectExecution = (pluginConfig?.projectExecution as string) ?? "parallel";
const data = await readProjects(workspaceDir);
const projectIds = groupId ? [groupId] : Object.keys(data.projects);
// Build execution configs + fetch queues
const configs: ProjectExecutionConfig[] = [];
const projectList: Array<{ id: string; project: Project }> = [];
// Build project summaries with queue counts
const projects = await Promise.all(
projectIds.map(async (pid) => {
const project = getProject(data, pid);
if (!project) return null;
for (const pid of projectIds) {
const project = getProject(data, pid);
if (!project) continue;
projectList.push({ id: pid, project });
configs.push({
name: project.name, groupId: pid,
roleExecution: project.roleExecution ?? "parallel",
devActive: project.dev.active, qaActive: project.qa.active,
devIssueId: project.dev.issueId, qaIssueId: project.qa.issueId,
});
}
const queues = await fetchProjectQueues(project);
const count = (label: QueueLabel) => queues[label].length;
// Health checks (read-only — never auto-fix from status)
const healthIssues: Array<Record<string, unknown>> = [];
if (includeHealth) {
for (const { id, project } of projectList) {
const { provider } = resolveProvider(project);
for (const role of ["dev", "qa"] as const) {
const fixes = await checkWorkerHealth({
workspaceDir, groupId: id, project, role, activeSessions,
autoFix: false, provider,
});
for (const f of fixes) healthIssues.push({ ...f.issue, fixed: f.fixed });
}
}
}
// Fetch queues
const projectQueues: ProjectQueues[] = await Promise.all(
projectList.map(async ({ id, project }) => ({
projectId: id, project,
queues: await fetchProjectQueues(project),
})),
return {
name: project.name,
groupId: pid,
roleExecution: project.roleExecution ?? "parallel",
dev: { active: project.dev.active, issueId: project.dev.issueId, tier: project.dev.tier, startTime: project.dev.startTime },
qa: { active: project.qa.active, issueId: project.qa.issueId, tier: project.qa.tier, startTime: project.qa.startTime },
queue: { toImprove: count("To Improve"), toTest: count("To Test"), toDo: count("To Do") },
};
}),
);
// Build sequences
const sequences = projectExecution === "sequential"
? { mode: "sequential" as const, global: buildGlobalTaskSequence(projectQueues) }
: { mode: "parallel" as const, projects: buildParallelProjectSequences(projectQueues) };
// Build project details
const projects = projectQueues.map(({ projectId, project, queues }) => ({
name: project.name, groupId: projectId,
dev: { active: project.dev.active, issueId: project.dev.issueId, tier: project.dev.tier, sessions: project.dev.sessions },
qa: { active: project.qa.active, issueId: project.qa.issueId, tier: project.qa.tier, sessions: project.qa.sessions },
queue: formatProjectQueues(queues),
}));
const filtered = projects.filter(Boolean);
await auditLog(workspaceDir, "status", {
projectCount: projects.length,
totalToImprove: projects.reduce((s, p) => s + p.queue.toImprove.length, 0),
totalToTest: projects.reduce((s, p) => s + p.queue.toTest.length, 0),
totalToDo: projects.reduce((s, p) => s + p.queue.toDo.length, 0),
healthIssues: healthIssues.length,
projectCount: filtered.length,
totalQueued: filtered.reduce((s, p) => s + p!.queue.toImprove + p!.queue.toTest + p!.queue.toDo, 0),
});
return jsonResult({
execution: { plugin: { projectExecution }, projects: configs },
sequences, projects,
health: includeHealth ? { issues: healthIssues, note: activeSessions.length === 0 ? "No activeSessions — zombie detection skipped." : undefined } : undefined,
success: true,
execution: { projectExecution },
projects: filtered,
context: {
type: context.type,
...(context.type === "group" && { projectName: context.projectName, autoFiltered: !params.projectGroupId }),