fix(quick-260328-48g): fix heartbeat task duplication with Paperclip-style coalescing

- Remove self-defeating heartbeat task filter that excluded heartbeat tasks from candidates
- Add Paperclip-style task reuse: recycle existing done/in_review heartbeat tasks instead of creating new ones
- Fix status bug: new heartbeat tasks created as "todo" instead of "in_progress" so CAS checkout succeeds
- Wire one-time duplicate cleanup sweep into heartbeat scheduler startup

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 4.6 committed 2026-03-28 03:17:17 -04:00
1 parent 4b5492ea34
commit 758bc077ff
1 file changed
+29 -5
+29 -5
View File
@@ -357,9 +357,7 @@ export function createOrchestrationService({
const assignedTasks = db.listAgentTasks(agentId, companyId, ["todo", "in_progress", "blocked"]); const assignedTasks = db.listAgentTasks(agentId, companyId, ["todo", "in_progress", "blocked"]);
const isTimer = trigger === "timer"; const isTimer = trigger === "timer";
const candidateTasks = isTimer const candidateTasks = assignedTasks;
? assignedTasks.filter((entry) => !entry.title.startsWith("[Heartbeat]"))
: assignedTasks;
let task = candidateTasks.find((entry) => !entry.activeRunId) ?? null; let task = candidateTasks.find((entry) => !entry.activeRunId) ?? null;
@@ -382,6 +380,15 @@ export function createOrchestrationService({
return null; return null;
} }
// Paperclip-style task reuse: coalesce into existing heartbeat task instead of creating duplicates
const reusableTask = db.findReusableHeartbeatTask(agentId, companyId);
if (reusableTask) {
// Reset to "todo" so checkoutTask CAS succeeds (requires status IN ('todo', 'backlog'))
db.advanceTaskStatus(reusableTask.id, "todo", `Heartbeat task recycled for ${trigger} cycle.`);
task = db.getTask(reusableTask.id);
logger.info(`[heartbeat] Reusing existing heartbeat task for ${agent.name}: ${task.title}`);
} else {
// Only create new heartbeat task if none exists to reuse
const taskTitle = isCeo const taskTitle = isCeo
? `[Heartbeat] ${agent.name} — Strategic planning & task delegation` ? `[Heartbeat] ${agent.name} — Strategic planning & task delegation`
: isManager : isManager
@@ -393,17 +400,21 @@ export function createOrchestrationService({
? "Review your department's goals and your team's workload. Create tasks for your direct reports if they have capacity. Review any work submitted for your approval. Ensure your team is productive and unblocked." ? "Review your department's goals and your team's workload. Create tasks for your direct reports if they have capacity. Review any work submitted for your approval. Ensure your team is productive and unblocked."
: `Automated heartbeat run for ${agent.name}. Review assigned tasks, check goals, and take action as needed.`; : `Automated heartbeat run for ${agent.name}. Review assigned tasks, check goals, and take action as needed.`;
// CRITICAL FIX: Create as "todo" (not "in_progress") so checkoutTask CAS works.
// Previously created as "in_progress" which caused checkoutTask to always fail
// (it requires status IN ('todo', 'backlog')), silently dropping the run.
const taskId = db.saveTask({ const taskId = db.saveTask({
companyId, companyId,
title: taskTitle, title: taskTitle,
description: taskDescription, description: taskDescription,
assigneeAgentId: agentId, assigneeAgentId: agentId,
workspaceId: agent.workspaceId ?? undefined, workspaceId: agent.workspaceId ?? undefined,
status: "in_progress", status: "todo",
priority: isCeo ? "high" : "medium", priority: isCeo ? "high" : "medium",
}); });
task = db.getTask(taskId); task = db.getTask(taskId);
logger.info(`[heartbeat] Created bootstrap task for ${agent.name}: ${taskTitle}`); logger.info(`[heartbeat] Created heartbeat task for ${agent.name}: ${taskTitle}`);
}
} }
if (task.activeRunId || hasActiveRun({ taskId: task.id, companyId })) { if (task.activeRunId || hasActiveRun({ taskId: task.id, companyId })) {
@@ -617,6 +628,19 @@ export function createOrchestrationService({
function startHeartbeatScheduler() { function startHeartbeatScheduler() {
if (heartbeatTimer) clearInterval(heartbeatTimer); if (heartbeatTimer) clearInterval(heartbeatTimer);
// One-time cleanup of existing duplicate heartbeat tasks from before the coalescing fix
try {
const activeCompanies = db.listActiveCompanyIds();
for (const company of activeCompanies) {
const cleaned = db.cleanupDuplicateHeartbeatTasks(company.id);
if (cleaned > 0) {
logger.info(`[heartbeat] Cleaned up ${cleaned} duplicate heartbeat task(s) for company ${company.name}`);
}
}
} catch (err) {
logger.error(`[heartbeat] Heartbeat task cleanup failed: ${err instanceof Error ? err.message : String(err)}`);
}
heartbeatTimer = setInterval(() => { heartbeatTimer = setInterval(() => {
try { try {
if (isQuitting() || !hasWorkerReady()) return; if (isQuitting() || !hasWorkerReady()) return;