Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

29.4 KB
Raw Blame
import { PR_COMPLETION_VALUES } from '../lib/prDisposition.js';
import { AUDIT_DEFINITIONS } from '../lib/auditCatalog.js';
/**
 * CoS Task Schedule Routes
 */

import { Router } from 'express';
import { z } from 'zod';
import * as taskSchedule from '../services/taskSchedule.js';
import { logCosScheduleUpdate } from '../services/userActionScheduleLog.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { sanitizeTaskMetadata, taskDataInputsSchema, validateRequest, parsePagination, cosScheduleTargetBodySchema, heldCheckoutRetireBodySchema, heldCheckoutReviewBodySchema, initializedCheckoutRetireBodySchema, initializedCheckoutTransactionSchema, initializedCheckoutRestoreSchema, initializedCheckoutBranchRetireSchema, initializedCheckoutMetadataRestoreSchema, initializedCheckoutFinalizationSchema, initializedCheckoutQuarantineResumptionSchema } from '../lib/validation.js';
import { promptSourceSchema, PROMPT_SOURCES, scheduleExecutionFieldsSchema } from '../lib/cosValidation.js';
import { EFFORT_LEVELS } from '../lib/providerModels.js';
import { INTERVAL_TYPES, decodeIntervalType, isCronExpression, isKnownIntervalType } from '../services/taskScheduleConstants.js';
import { normalizeSuggestedAfter, SUGGESTED_AFTER_MAX } from '../lib/scheduleRunOrder.js';
import { findCronExpressionError } from '../lib/cronValidation.js';
import { setMaintenanceRunArchived, updateMaintenanceStep, listMaintenanceRuns, resumeMaintenanceRun, startMaintenanceRun, stopMaintenanceRun } from '../services/maintenanceRun.js';

const templateTaskSchema = z.object({
  name: z.string().min(1),
  description: z.string().min(1),
  category: z.string().optional(),
  taskType: z.string().optional(),
  priority: z.string().optional(),
  metadata: z.record(z.unknown()).optional(),
});

const scheduleLabelsSchema = z.array(z.string().trim().min(1).max(40)).max(20)
  .transform(labels => [...new Set(labels.map(label => label.toLowerCase()))]);

// Shape only — normalizeSuggestedAfter does the trimming, self-drop and dedupe
// so the storage rules live with the field's own helper, not in the route.
const suggestedAfterSchema = z.array(z.string()).max(SUGGESTED_AFTER_MAX);

// A manual maintenance run names the app, the provider and the model up front
// (AGENTS.md AI-policy: the click IS the consent). Any enabled CLI/TUI provider
// is accepted — one outside every subscription family runs unfamilied, see
// `maintenanceRun.js#manualBurnFamily` — so the family check belongs to the
// service's live provider gate, not to this schema. Blank effort inherits each
// scheduled task's saved effort.
const maintenanceRunStartSchema = z.object({
  taskTypes: z.array(z.enum(Object.keys(AUDIT_DEFINITIONS))).min(1).max(Object.keys(AUDIT_DEFINITIONS).length).optional(),
  // The user named these checks one by one (the Quality tab's per-category Run),
  // so each runs even where the repository scan says it cannot apply.
  explicitCheck: z.boolean().optional(),
  mode: z.enum(['file-issues', 'fix']).optional(),
  auditDepth: z.enum(['quick', 'deep']).optional(),
  prCompletion: z.enum([...PR_COMPLETION_VALUES, 'inherit']).optional(),
  claimBetweenAudits: z.boolean().optional(),
  claimHandler: z.object({
    providerId: z.string().trim().min(1),
    model: z.string().trim().min(1),
    effort: z.enum(EFFORT_LEVELS).nullable().optional(),
  }).nullable().optional(),
  appId: z.string().trim().min(1),
  providerId: z.string().trim().min(1),
  model: z.string().trim().min(1),
  effort: z.enum(EFFORT_LEVELS).nullable().optional(),
}).strict();

const router = Router();

const SCHEDULE_FIELDS = ['type', 'autoStart', 'perpetual', 'enabled', 'intervalMs', 'cronExpression', 'providerId', 'model', 'effort', 'prompt', 'description', 'labels', 'dataInputs', 'taskMetadata', 'runAfter',
  // Advisory run order — which OTHER scheduled tasks a user should generally run
  // first. Editable and unenforced; `runAfter` above is the enforced gate.
  'suggestedAfter',
  // Perpetual (drain-until-done) recheck cadence: after a perpetual task drains
  // its backlog and parks, it re-probes its work-detector on this cadence.
  // `recheckCron` (5-field) takes precedence over `recheckIntervalMs`.
  'recheckCron', 'recheckIntervalMs',
  // Provenance of the stored prompt ('user' | 'legacy-inferred' | null). An
  // explicit prompt write re-stamps it below in updateTaskInterval, so a body
  // carrying it only matters when the prompt itself is not being changed.
  'promptSource'];

/**
 * Pick only defined values from body for schedule settings updates. `taskType`
 * is the row being written, so a field that must not name itself (the advisory
 * `suggestedAfter`) can be cleaned here rather than again at the call site.
 */
function pickScheduleSettings(body, executionFields, taskType) {
  const settings = {};
  for (const key of SCHEDULE_FIELDS) {
    if (body[key] !== undefined) settings[key] = body[key];
  }
  Object.assign(settings, executionFields);
  if (settings.enabled !== undefined && typeof settings.enabled !== 'boolean') {
    throw new ServerError('enabled must be a boolean', { status: 400, code: 'VALIDATION_ERROR' });
  }
  if (settings.autoStart !== undefined && typeof settings.autoStart !== 'boolean') {
    throw new ServerError('autoStart must be a boolean', { status: 400, code: 'VALIDATION_ERROR' });
  }
  if (settings.perpetual !== undefined && typeof settings.perpetual !== 'boolean') {
    throw new ServerError('perpetual must be a boolean', { status: 400, code: 'VALIDATION_ERROR' });
  }
  if (settings.type !== undefined) {
    // Reject an unrecognized string outright — decoding it would silently make
    // an unreadable cadence 'on-demand', i.e. quietly stop the task running.
    if (!isKnownIntervalType(settings.type)) {
      throw new ServerError(`type must be one of ${Object.values(INTERVAL_TYPES).join(', ')}`, { status: 400, code: 'VALIDATION_ERROR' });
    }
    // A client still on the previous release (or a peer machine mid-upgrade)
    // may send a retired cadence name. Rewrite it onto the two-variant model
    // rather than 400-ing, so an older UI keeps working through the upgrade.
    const decoded = decodeIntervalType(settings.type, { intervalMs: settings.intervalMs });
    settings.type = decoded.type;
    if (decoded.perpetual && settings.perpetual === undefined) settings.perpetual = true;
    if (decoded.type === INTERVAL_TYPES.CRON && !isCronExpression(settings.cronExpression) && decoded.cronExpression) {
      settings.cronExpression = decoded.cronExpression;
    }
  }
  // Field RANGES, not just the 5-token shape: an out-of-range expression saved
  // on an enabled schedule would be silently dropped by the walker and never
  // fire (#6634).
  if (settings.cronExpression !== undefined && settings.cronExpression !== null) {
    const cronError = findCronExpressionError(settings.cronExpression);
    if (cronError) {
      throw new ServerError(`cronExpression is invalid: ${cronError}`, { status: 400, code: 'VALIDATION_ERROR' });
    }
  }
  if (settings.labels !== undefined) {
    const parsed = scheduleLabelsSchema.safeParse(settings.labels);
    if (!parsed.success) {
      throw new ServerError('labels must be at most 20 non-empty strings of at most 40 characters', { status: 400, code: 'VALIDATION_ERROR' });
    }
    settings.labels = parsed.data;
  }
  if (settings.description !== undefined) {
    if (settings.description !== null && typeof settings.description !== 'string') {
      throw new ServerError('description must be a string or null', { status: 400, code: 'VALIDATION_ERROR' });
    }
    const description = settings.description == null ? '' : settings.description.trim();
    if (description.length > 240) {
      throw new ServerError('description must be 240 characters or fewer', { status: 400, code: 'VALIDATION_ERROR' });
    }
    settings.description = description || null;
  }
  if (settings.intervalMs !== undefined && settings.intervalMs !== null && (typeof settings.intervalMs !== 'number' || settings.intervalMs < 0)) {
    throw new ServerError('intervalMs must be a non-negative number or null', { status: 400, code: 'VALIDATION_ERROR' });
  }
  if (settings.recheckIntervalMs !== undefined && settings.recheckIntervalMs !== null && (typeof settings.recheckIntervalMs !== 'number' || settings.recheckIntervalMs < 0)) {
    throw new ServerError('recheckIntervalMs must be a non-negative number or null', { status: 400, code: 'VALIDATION_ERROR' });
  }
  if (settings.recheckCron !== undefined && settings.recheckCron !== null) {
    if (typeof settings.recheckCron !== 'string') {
      throw new ServerError('recheckCron must be a cron string or null', { status: 400, code: 'VALIDATION_ERROR' });
    }
    const trimmed = settings.recheckCron.trim();
    // Empty string clears it; otherwise require a 5-field cron expression.
    if (trimmed === '') {
      settings.recheckCron = null;
    } else {
      // An invalid recheck cron does not merely 'not fire' —
      // computePerpetualRecheckAt silently falls back to another cadence — so
      // reject it here with the same shared validator (#6634).
      const recheckError = findCronExpressionError(trimmed);
      if (recheckError) {
        throw new ServerError(`recheckCron is invalid: ${recheckError}`, { status: 400, code: 'VALIDATION_ERROR' });
      }
      settings.recheckCron = trimmed;
    }
  }
  if (settings.taskMetadata !== undefined && settings.taskMetadata !== null) {
    if (typeof settings.taskMetadata !== 'object' || Array.isArray(settings.taskMetadata)) {
      throw new ServerError('taskMetadata must be an object or null', { status: 400, code: 'VALIDATION_ERROR' });
    }
    const sanitized = sanitizeTaskMetadata(settings.taskMetadata);
    if (sanitized === null) {
      throw new ServerError('Invalid taskMetadata: unrecognized keys or values', { status: 400, code: 'VALIDATION_ERROR' });
    }
    settings.taskMetadata = sanitized;
  }
  if (settings.dataInputs !== undefined) {
    const parsed = taskDataInputsSchema.safeParse(settings.dataInputs);
    if (!parsed.success) {
      throw new ServerError('dataInputs must contain only registered task data input ids', { status: 400, code: 'VALIDATION_ERROR' });
    }
    settings.dataInputs = parsed.data;
  }
  if (settings.promptSource !== undefined) {
    const parsed = promptSourceSchema.safeParse(settings.promptSource);
    if (!parsed.success) {
      throw new ServerError(`promptSource must be one of ${PROMPT_SOURCES.join(', ')} or null`, { status: 400, code: 'VALIDATION_ERROR' });
    }
    settings.promptSource = parsed.data ?? null;
  }
  if (settings.effort !== undefined && settings.effort !== null && !EFFORT_LEVELS.includes(settings.effort)) {
    throw new ServerError(`effort must be one of ${EFFORT_LEVELS.join(', ')} or null`, { status: 400, code: 'VALIDATION_ERROR' });
  }
  // Advisory ordering. `normalizeSuggestedAfter` owns the shape rules (self,
  // blanks, duplicates, cap) and why an emptied list stays `[]` rather than
  // becoming null the way `runAfter` does.
  if (settings.suggestedAfter !== undefined) {
    const parsed = suggestedAfterSchema.safeParse(settings.suggestedAfter ?? []);
    if (!parsed.success) {
      throw new ServerError(`suggestedAfter must be at most ${SUGGESTED_AFTER_MAX} task type strings, or null`, { status: 400, code: 'VALIDATION_ERROR' });
    }
    settings.suggestedAfter = normalizeSuggestedAfter(parsed.data, taskType);
  }
  if (settings.runAfter !== undefined && settings.runAfter !== null) {
    if (!Array.isArray(settings.runAfter)) {
      throw new ServerError('runAfter must be an array of task type strings or null', { status: 400, code: 'VALIDATION_ERROR' });
    }
    if (!settings.runAfter.every(v => typeof v === 'string')) {
      throw new ServerError('runAfter entries must be strings', { status: 400, code: 'VALIDATION_ERROR' });
    }
    if (settings.runAfter.length === 0) {
      settings.runAfter = null;
    }
  }
  return settings;
}

// GET /api/cos/schedule - Get full schedule status
router.get('/schedule', asyncHandler(async (req, res) => {
  const status = await taskSchedule.getScheduleStatus();
  res.json(status);
}));

// GET /api/cos/upcoming - Get upcoming tasks preview
router.get('/upcoming', asyncHandler(async (req, res) => {
  const { limit } = parsePagination(req.query, { defaultLimit: 10, maxLimit: 500 });
  // getUpcomingTasks now rejects (rather than returning a partial []) when an
  // app inventory/override/readiness read fails (#7527) — surface that as a
  // real failure instead of a misleading 200 with incomplete deadlines.
  const upcoming = await taskSchedule.getUpcomingTasks(limit).catch((err) => {
    throw new ServerError(`Upcoming tasks unavailable: ${err?.message || err}`, {
      status: 503,
      code: 'UPCOMING_TASKS_UNAVAILABLE'
    });
  });
  res.json(upcoming);
}));

// GET /api/cos/schedule/task/:taskType - Get interval for a task type (unified)
router.get('/schedule/task/:taskType', asyncHandler(async (req, res) => {
  const { taskType } = req.params;
  const interval = await taskSchedule.getTaskInterval(taskType);
  const shouldRun = await taskSchedule.shouldRunTask(taskType);
  res.json({ taskType, interval, shouldRun });
}));

// PUT /api/cos/schedule/task/:taskType - Update interval for a task type (unified)
router.put('/schedule/task/:taskType', asyncHandler(async (req, res) => {
  const { taskType } = req.params;
  const executionFields = validateRequest(scheduleExecutionFieldsSchema, req.body);
  const settings = pickScheduleSettings(req.body, executionFields, taskType);
  // Filter self-references from runAfter to prevent permanent blocking
  if (Array.isArray(settings.runAfter)) {
    settings.runAfter = settings.runAfter.filter(dep => dep !== taskType);
    if (settings.runAfter.length === 0) settings.runAfter = null;
  }
  const result = await taskSchedule.updateTaskInterval(taskType, settings);
  // The persisted interval is only half of the response the Schedule page
  // needs. `status.nextRunAt` is derived from the cron expression and the
  // user's timezone, so return its post-write value instead of making the
  // client keep the old countdown after a schedule edit.
  const status = await taskSchedule.shouldRunTask(taskType);
  await logCosScheduleUpdate({
    target: taskType,
    patch: settings,
    source: { route: `${req.baseUrl}${req.route?.path ?? ''}`, method: req.method },
  });
  res.json({ success: true, taskType, interval: result, status });
}));

// GET /api/cos/schedule/due - Get all tasks that are due to run
router.get('/schedule/due', asyncHandler(async (req, res) => {
  const tasks = await taskSchedule.getDueTasks();
  res.json({ tasks });
}));

// GET /api/cos/schedule/due/:appId - Get tasks due for specific app
router.get('/schedule/due/:appId', asyncHandler(async (req, res) => {
  const { appId } = req.params;
  const tasks = await taskSchedule.getDueTasks(appId);
  res.json({ appId, tasks });
}));

// POST /api/cos/schedule/trigger - Trigger an on-demand task
router.post('/schedule/trigger', asyncHandler(async (req, res) => {
  const { taskType, appId } = validateRequest(cosScheduleTargetBodySchema, req.body);

  const request = await taskSchedule.triggerOnDemandTask(taskType, appId);
  if (request?.error) {
    throw new ServerError(request.error, { status: 409, code: 'TRIGGER_REJECTED' });
  }
  res.json({ success: true, request });
}));

// Scan without changing Git state or scheduling provider work.
router.post('/schedule/branch-reconcile/refresh', asyncHandler(async (req, res) => {
  const { appId } = validateRequest(heldCheckoutReviewBodySchema.pick({ appId: true }).strict(), req.body);
  const { refreshBranchReconcileInventory } = await import('../services/branchReconcileRefresh.js');
  res.json(await refreshBranchReconcileInventory(appId));
}));

// Prepare evidence only; the separate confirmation binds its digest to fresh facts.
router.post('/schedule/branch-reconcile/checkouts/review', asyncHandler(async (req, res) => {
  const { appId, prUrl, kind, ...release } = validateRequest(heldCheckoutReviewBodySchema, req.body);
  const { prepareInventoriedCheckoutRetirement } = await import('../services/heldCheckoutRecovery.js');
  const result = await prepareInventoriedCheckoutRetirement({ appId, release, prUrl, ...(kind && { kind }) });
  if (!result.ok && result.reason === 'retirement-submodule-not-uninitialized' && !result.nextAction) {
    result.nextAction = 'This checkout has initialized Git submodules. Request its exact review through /api/cos/schedule/branch-reconcile/initialized-checkouts/review; do not deinitialize or remove modules to bypass this refusal.';
  }
  if (!result.ok) throw new ServerError(`Checkout review refused: ${result.reason}`, {
    status: 409, code: 'CHECKOUT_REVIEW_REFUSED', context: { reason: result.reason, ...(result.archiveLimit && { archiveLimit: result.archiveLimit }), ...(result.nextAction && { nextAction: result.nextAction }) },
  });
  res.json(result);
}));

// POST /api/cos/schedule/branch-reconcile/checkouts/retire - Archive and retire
// one held checkout from the last branch-reconcile inventory (#12002)
router.post('/schedule/branch-reconcile/checkouts/retire', asyncHandler(async (req, res) => {
  const { appId, review, ...release } = validateRequest(heldCheckoutRetireBodySchema, req.body);
  const { retireInventoriedCheckout } = await import('../services/heldCheckoutRecovery.js');
  const result = await retireInventoriedCheckout({ appId, release, review });
  if (!result.ok) {
    throw new ServerError(`Checkout kept: ${result.reason}${result.detail ? ` (${result.detail})` : ''}`, {
      status: result.reason === 'app-not-found' ? 404 : 409, code: 'CHECKOUT_RETIREMENT_REFUSED', context: { reason: result.reason, ...(result.archiveLimit && { archiveLimit: result.archiveLimit }), ...(result.nextAction && { nextAction: result.nextAction }) },
    });
  }
  res.json(result);
}));

// Initial retirement remains explicit. Only existing proof-bound journals can be
// continued by the deterministic daemon; ordinary cleanup keeps its module guard.
router.post('/schedule/branch-reconcile/initialized-checkouts/review', asyncHandler(async (req, res) => {
  const { appId, prUrl, ...release } = validateRequest(heldCheckoutReviewBodySchema.omit({ kind: true }).strict(), req.body);
  const { reviewInitializedCheckout } = await import('../services/initializedCheckoutRecovery.js');
  const result = await reviewInitializedCheckout({ appId, release, prUrl });
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/retire', asyncHandler(async (req, res) => {
  const { appId, review, ...release } = validateRequest(initializedCheckoutRetireBodySchema, req.body);
  const { retireInitializedCheckout } = await import('../services/initializedCheckoutRecovery.js');
  const result = await retireInitializedCheckout({ appId, release, review });
  res.status(result.ok ? 200 : 409).json(result);
}));

// Explicit whole-checkout preservation uses the same journal, byte/capacity
// bounds, metadata proof, quarantine and recovery as initialized modules. The
// ordinary archive limits and existing initialized-only endpoint stay intact.
router.post('/schedule/branch-reconcile/whole-checkouts/review', asyncHandler(async (req, res) => {
  const { appId, prUrl, ...release } = validateRequest(heldCheckoutReviewBodySchema.omit({ kind: true }).strict(), req.body);
  const { reviewInitializedCheckout } = await import('../services/initializedCheckoutRecovery.js');
  const result = await reviewInitializedCheckout({ appId, release, prUrl, wholeCheckout: true });
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/whole-checkouts/retire', asyncHandler(async (req, res) => {
  const { appId, review, ...release } = validateRequest(initializedCheckoutRetireBodySchema, req.body);
  const { retireInitializedCheckout } = await import('../services/initializedCheckoutRecovery.js');
  const result = await retireInitializedCheckout({ appId, release, review, wholeCheckout: true });
  res.status(result.ok ? 200 : 409).json(result);
}));

router.get('/schedule/branch-reconcile/initialized-checkouts/status', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutTransactionSchema, req.query);
  const { initializedCheckoutStatus } = await import('../services/initializedCheckoutRecovery.js');
  res.json(await initializedCheckoutStatus(input));
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/restore', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutRestoreSchema, req.body);
  const { restoreInitializedCheckout } = await import('../services/initializedCheckoutRecovery.js');
  const result = await restoreInitializedCheckout(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/review-metadata-restore', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutRestoreSchema, req.body);
  const { reviewInitializedCheckoutMetadataRestore } = await import('../services/initializedCheckoutRecovery.js');
  const result = await reviewInitializedCheckoutMetadataRestore(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/restore-metadata', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutMetadataRestoreSchema, req.body);
  const { restoreInitializedCheckoutMetadata } = await import('../services/initializedCheckoutRecovery.js');
  const result = await restoreInitializedCheckoutMetadata(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/review-restored-finalization', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutRestoreSchema, req.body);
  const { reviewInitializedCheckoutRestoredFinalization } = await import('../services/initializedCheckoutRecovery.js');
  const result = await reviewInitializedCheckoutRestoredFinalization(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/finalize-restored', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutFinalizationSchema, req.body);
  const { finalizeInitializedCheckoutRestored } = await import('../services/initializedCheckoutRecovery.js');
  const result = await finalizeInitializedCheckoutRestored(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/review-quarantine-resumption', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutRestoreSchema, req.body);
  const { reviewInitializedCheckoutQuarantineResumption } = await import('../services/initializedCheckoutRecovery.js');
  const result = await reviewInitializedCheckoutQuarantineResumption(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/resume-quarantine', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutQuarantineResumptionSchema, req.body);
  const { resumeInitializedCheckoutQuarantine } = await import('../services/initializedCheckoutRecovery.js');
  const result = await resumeInitializedCheckoutQuarantine(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/verify-archive', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutRestoreSchema, req.body);
  const { verifyInitializedCheckoutArchive } = await import('../services/initializedCheckoutRecovery.js');
  res.json(await verifyInitializedCheckoutArchive(input));
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/review-branch', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutRestoreSchema, req.body);
  const { reviewInitializedCheckoutBranch } = await import('../services/initializedCheckoutRecovery.js');
  const result = await reviewInitializedCheckoutBranch(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

router.post('/schedule/branch-reconcile/initialized-checkouts/retire-branch', asyncHandler(async (req, res) => {
  const input = validateRequest(initializedCheckoutBranchRetireSchema, req.body);
  const { retireInitializedCheckoutBranch } = await import('../services/initializedCheckoutRecovery.js');
  const result = await retireInitializedCheckoutBranch(input);
  res.status(result.ok ? 200 : 409).json(result);
}));

// Manual maintenance runs — the Schedule tab's "Run maintenance now"; see
// services/maintenanceRun.js for why this is not a quota burn.
router.get('/schedule/deep-audits/:id', asyncHandler(async (req, res) => {
  const { getDeepAuditLedger } = await import('../services/deepAudit.js');
  const ledger = await getDeepAuditLedger(req.params.id);
  if (!ledger) throw new ServerError('Deep audit not found', { status: 404 });
  const { deepAuditProgress } = await import('../lib/deepAudit.js');
  res.json({ ...ledger, progress: deepAuditProgress(ledger) });
}));

router.get('/schedule/maintenance-runs', asyncHandler(async (_req, res) => {
  res.json({ runs: await listMaintenanceRuns() });
}));

router.post('/schedule/maintenance-runs', asyncHandler(async (req, res) => {
  const body = validateRequest(maintenanceRunStartSchema, req.body || {});
  res.status(201).json(await startMaintenanceRun(body));
}));

router.patch('/schedule/maintenance-runs/:id/steps/:stepId', asyncHandler(async (req, res) => {
  const body = validateRequest(maintenanceRunStartSchema.pick({ providerId: true, model: true, effort: true }), req.body || {});
  const run = await updateMaintenanceStep(req.params.id, req.params.stepId, body);
  if (!run) throw new ServerError('Maintenance run or stage not found', { status: 404 });
  res.json({ run });
}));

router.patch('/schedule/maintenance-runs/:id/archive', asyncHandler(async (req, res) => {
  const body = validateRequest(z.object({ appId: z.string().min(1), archived: z.boolean() }).strict(), req.body || {});
  const run = await setMaintenanceRunArchived(req.params.id, body);
  if (!run) throw new ServerError('Maintenance run not found for this app', { status: 404, code: 'NOT_FOUND' });
  res.json({ run });
}));

router.post('/schedule/maintenance-runs/:id/stop', asyncHandler(async (req, res) => {
  const run = await stopMaintenanceRun(req.params.id);
  if (!run) throw new ServerError('Maintenance run not found', { status: 404, code: 'NOT_FOUND' });
  res.json({ run });
}));

router.post('/schedule/maintenance-runs/:id/resume', asyncHandler(async (req, res) => {
  const resumed = await resumeMaintenanceRun(req.params.id);
  if (!resumed) throw new ServerError('Maintenance run not found', { status: 404, code: 'NOT_FOUND' });
  res.json(resumed);
}));

// GET /api/cos/schedule/on-demand - Get pending on-demand requests
router.get('/schedule/on-demand', asyncHandler(async (req, res) => {
  const requests = await taskSchedule.getOnDemandRequests();
  res.json({ requests });
}));

// DELETE /api/cos/schedule/on-demand/:requestId - Clear an on-demand request
router.delete('/schedule/on-demand/:requestId', asyncHandler(async (req, res) => {
  const { requestId } = req.params;
  const cleared = await taskSchedule.clearOnDemandRequest(requestId);
  if (!cleared) {
    throw new ServerError('Request not found', { status: 404, code: 'NOT_FOUND' });
  }
  res.json({ success: true, cleared });
}));

// POST /api/cos/schedule/reset - Reset execution history for a task type
router.post('/schedule/reset', asyncHandler(async (req, res) => {
  const { taskType, appId } = validateRequest(cosScheduleTargetBodySchema, req.body);

  const result = await taskSchedule.resetExecutionHistory(taskType, appId);
  if (result.error) {
    throw new ServerError(result.error, { status: 404, code: 'NOT_FOUND' });
  }
  res.json(result);
}));

// GET /api/cos/schedule/templates - Get all template tasks
router.get('/schedule/templates', asyncHandler(async (req, res) => {
  const templates = await taskSchedule.getTemplateTasks();
  res.json({ templates });
}));

// POST /api/cos/schedule/templates - Add a template task
router.post('/schedule/templates', asyncHandler(async (req, res) => {
  const { name, description, category, taskType, priority, metadata } = validateRequest(templateTaskSchema, req.body);

  const template = await taskSchedule.addTemplateTask({
    name,
    description,
    category,
    taskType,
    priority,
    metadata
  });
  res.json({ success: true, template });
}));

// DELETE /api/cos/schedule/templates/:templateId - Delete a template task
router.delete('/schedule/templates/:templateId', asyncHandler(async (req, res) => {
  const { templateId } = req.params;
  const result = await taskSchedule.deleteTemplateTask(templateId);
  if (result.error) {
    throw new ServerError(result.error, { status: 404, code: 'NOT_FOUND' });
  }
  res.json(result);
}));

// GET /api/cos/schedule/interval-types - Get available interval types
router.get('/schedule/interval-types', (req, res) => {
  res.json({
    types: taskSchedule.INTERVAL_TYPES,
    descriptions: {
      'on-demand': 'Only runs when manually triggered',
      cron: 'Scheduled on a cron expression (minute hour dayOfMonth month dayOfWeek)'
    },
    // `perpetual` is an orthogonal flag, not a type — it applies to either.
    perpetual: 'Drains actionable work back-to-back until none remains. On-demand tasks with autoStart false wait for another explicit trigger; otherwise rechecks on a cadence (its own cron expression when scheduled, else recheckCron / recheckIntervalMs, default daily)'
  });
});

export default router;