import type { DevTask } from './dev-task'; /** * 是否可以"手动"切到 in_progress * - 仅当 todo 且未到 expectedStartAt 时返回 true * - 已到点:自动机制接管,UI 不应再显示手动按钮 * - 已超期:返回 false,UI 应要求填 delayReason */ export function canManualStart(task: DevTask, now: Date = new Date()): boolean { if (task.status !== 'todo') return false; if (!task.expectedStartAt) return true; return now.getTime() < new Date(task.expectedStartAt).getTime(); } /** * 自动切换扫描:返回所有需要从 todo 切到 in_progress 的任务 * - status === 'todo' * - now >= expectedStartAt(且未超期超过 0 秒就自动切——超期未切的可单独走 needsDelayReason 流程) * * 注意:超期任务也包含在内,由调用方决定要不要扫;当前策略是 fetchTasks 调用时只扫"刚好到点", * 超期任务保留在 todo 等用户填 delayReason。所以这里加 maxOverdueMs 参数,默认 24h 内的算到点。 */ export function findTasksToAutoStart( tasks: DevTask[], now: Date = new Date(), maxOverdueMs: number = 24 * 60 * 60 * 1000, ): Array<{ taskId: string; actualStartAt: string }> { const result: Array<{ taskId: string; actualStartAt: string }> = []; for (const t of tasks) { if (t.status !== 'todo' || !t.expectedStartAt || t.actualStartAt) continue; const expected = new Date(t.expectedStartAt).getTime(); if (isNaN(expected)) continue; const diff = now.getTime() - expected; if (diff >= 0 && diff <= maxOverdueMs) { result.push({ taskId: t.id, actualStartAt: t.expectedStartAt }); } } return result; } /** * 是否处于"超期手动开干需填延后原因"状态 * - todo 且 now > expectedStartAt */ export function needsDelayReason(task: DevTask, now: Date = new Date()): boolean { if (task.status !== 'todo' || !task.expectedStartAt) return false; return now.getTime() > new Date(task.expectedStartAt).getTime(); } /** * 进入 in_progress 时计算 actualStartAt * - auto: 等于 expectedStartAt * - manual: 等于 now */ export function deriveActualStartAt( task: DevTask, trigger: 'auto' | 'manual', now: Date = new Date(), ): string { if (trigger === 'auto' && task.expectedStartAt) return task.expectedStartAt; return now.toISOString(); }