Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

16.1 KB
Raw Blame
/**
 * Schedule routes:
 *   GET|POST   /api/projects/:p/agents/:a/schedules
 *   POST       /api/projects/:p/agents/:a/schedules/template-placeholder  # insert the {{SCHEDULES}} placeholder
 *   GET|PUT|DELETE /api/projects/:p/agents/:a/schedules/:name (name is the file name)
 *   GET        /api/projects/:p/schedules  # every Agent's tasks in the Project, in one list
 *   GET        /api/schedules              # every Project the caller may reach, in one read-only answer
 * Any member can read; only the owner can modify. The file is declarative intent:
 * POST/PUT fully replace the file, validation always goes through parseScheduleFile
 * (same rules as hand-edited files), and writes take effect immediately via reconciliation.
 * Two writes are refused: any task whose end_at is already past (it could never fire, and the
 * scheduler would archive it the moment it landed), and a one-shot whose start_at is already
 * past (it would fire immediately; "run once now" is not offered — user decision 2026-10-15).
 * Settled tasks are archived by the scheduler, so no listing here ever shows one.
 */
import { createHash } from "node:crypto";
import { Hono } from "hono";
import { isValidId } from "@adelie/core";
import type {
  AllProjectSchedulesResponse,
  ProjectSchedulesResponse,
  ScheduleItem,
  ScheduleStatus,
  SchedulesResponse,
} from "../../api/types.js";
import type { AppEnv } from "../../auth/middleware.js";
import type { ServerConfig } from "../../config.js";

/** What this route group reaches — bound by its module (src/modules). */
export interface SchedulesRouteDeps {
  agentConfigService: AgentConfig;
  config: ServerConfig;
  projectConfigService: ProjectConfigStore;
  access: Access;
  scheduler: Scheduling;
  schedulesRepo: Schedules;
}
import { HttpError } from "../errors.js";
import {
  badRequest,
  optionalString,
  readJson,
  requireString,
  requireValidId,
} from "../validate.js";
import type { ScheduleDefinition } from "../../runtime/schedule-file.js";
import {
  latestSlotAt,
  nextSlotAfter,
  parseScheduleFile,
  slotInWindow,
} from "../../runtime/schedule-file.js";
import type { ScheduleStateRow } from "../../db/repos/schedules.js";
import {
  deleteScheduleFile,
  readScheduleFile,
  serializeSchedule,
  validateScheduleModelRef,
  writeScheduleFile,
} from "../../runtime/schedule-store.js";
import { scheduleStatus } from "../../runtime/scheduler.js";
import { Bind, Component, Use } from "@adelie/core/kernel";
import type { Config } from "../../hmr/capabilities.js";
import type { Access, ProjectConfigStore } from "../../mechanisms/projects.js";
import type { Schedules, Scheduling } from "../../mechanisms/sessions.js";
import type { AgentConfig } from "../../mechanisms/agents.js";

/** Validate and shape the POST/PUT request body into file fields (semantic validation is left to parseScheduleFile). */
function parseUpsertBody(body: Record<string, unknown>): {
  prompt: string;
  enabled: boolean;
  startAt: string;
  period?: string;
  endAt?: string;
  sessionId?: string;
  workspace?: string;
  modelId?: string;
  provider?: string;
} {
  if (typeof body.enabled !== "boolean") throw badRequest("enabled must be a boolean.");
  const prompt = requireString(body, "prompt", { minLen: 1, maxLen: 100_000 });
  const startAt = requireString(body, "startAt", { minLen: 1, maxLen: 100 });
  const period = optionalString(body, "period", { minLen: 1, maxLen: 20 });
  const endAt = optionalString(body, "endAt", { minLen: 1, maxLen: 100 });
  const sessionId = optionalString(body, "sessionId", { minLen: 1, maxLen: 200 });
  const workspace = optionalString(body, "workspace", { minLen: 1, maxLen: 4096 });
  const modelId = optionalString(body, "modelId", { minLen: 1, maxLen: 200 });
  const provider = optionalString(body, "provider", { minLen: 1, maxLen: 64 });
  return {
    prompt,
    enabled: body.enabled,
    startAt,
    ...(period !== undefined ? { period } : {}),
    ...(endAt !== undefined ? { endAt } : {}),
    ...(sessionId !== undefined ? { sessionId } : {}),
    ...(workspace !== undefined ? { workspace } : {}),
    ...(modelId !== undefined ? { modelId } : {}),
    ...(provider !== undefined ? { provider } : {}),
  };
}

/** Next scheduled fire time: none when disabled/invalid/done/missed; an undigested due slot counts as-is. */
function nextFireAt(
  def: ScheduleDefinition,
  state: ScheduleStateRow,
  nowMs: number,
): string | undefined {
  if (!def.enabled || state.invalidReason !== null) return undefined;
  if (def.periodMs === undefined && (state.firedOnce || state.missed)) return undefined;
  const due = latestSlotAt(def, nowMs);
  if (
    due !== null &&
    slotInWindow(def, due) &&
    (state.lastSlotMs === null || due > state.lastSlotMs)
  ) {
    return new Date(due).toISOString();
  }
  const next = nextSlotAfter(def, nowMs);
  return next !== null ? new Date(next).toISOString() : undefined;
}

function toItem(
  def: ScheduleDefinition,
  state: ScheduleStateRow,
  queued: boolean,
  nowMs: number,
): ScheduleItem {
  const next = nextFireAt(def, state, nowMs);
  return {
    name: def.name,
    prompt: def.prompt,
    enabled: def.enabled,
    startAt: def.startAt,
    ...(def.period !== undefined ? { period: def.period } : {}),
    ...(def.endAt !== undefined ? { endAt: def.endAt } : {}),
    ...(def.sessionId !== undefined ? { sessionId: def.sessionId } : {}),
    ...(def.workspace !== undefined ? { workspace: def.workspace } : {}),
    ...(def.modelId !== undefined ? { modelId: def.modelId } : {}),
    ...(def.provider !== undefined ? { provider: def.provider } : {}),
    status: scheduleStatus(def, state, nowMs),
    ...(state.invalidReason !== null ? { invalidReason: state.invalidReason } : {}),
    ...(next !== undefined ? { nextFireAt: next } : {}),
    ...(state.lastFiredAt !== null ? { lastFiredAt: state.lastFiredAt } : {}),
    queued,
    ...(state.creatorUserId !== null ? { creatorUserId: state.creatorUserId } : {}),
  };
}

/** Schedule name in the path: same character rules as directories/files, validated before any path construction. */
function requireScheduleName(raw: string | undefined): string {
  if (!raw || !isValidId(raw)) throw badRequest("Invalid schedule name.");
  return raw;
}

export function scheduleRoutes(deps: SchedulesRouteDeps): Hono<AppEnv> {
  const app = new Hono<AppEnv>();

  app.get("/", async (c) => {
    const projectId = requireValidId(c, "projectId");
    const agentId = requireValidId(c, "agentId");
    deps.access.requireProjectAccess(c.var.user.userId, projectId);
    await deps.agentConfigService.requireExists(projectId, agentId);
    const { entries, invalid } = await deps.scheduler.listAgent(projectId, agentId);
    const nowMs = Date.now();
    const res: SchedulesResponse = {
      schedules: entries.map((e) => toItem(e.def, e.state, e.queued, nowMs)),
      invalidFiles: invalid,
    };
    return c.json(res);
  });

  app.post("/", async (c) => {
    const projectId = requireValidId(c, "projectId");
    const agentId = requireValidId(c, "agentId");
    deps.access.requireProjectOwner(c.var.user.userId, projectId);
    await deps.agentConfigService.requireExists(projectId, agentId);
    const body = await readJson(c);
    const name = requireScheduleName(requireString(body, "name", { minLen: 1, maxLen: 100 }));
    if (await readScheduleFile(deps.config.root, projectId, agentId, name)) {
      throw new HttpError(409, "schedule_exists", `Schedule already exists: ${name}`);
    }
    await upsert(deps, c.var.user.userId, projectId, agentId, name, body);
    return c.json(await readItem(deps, projectId, agentId, name), 201);
  });

  // Insert the {{SCHEDULES}} placeholder into the prompt template — the explicit adoption
  // path mirroring memory's endpoint; idempotent config write, owner-level like this
  // router's other mutations. Registered before /:name, though the static path wins anyway.
  app.post("/template-placeholder", async (c) => {
    const projectId = requireValidId(c, "projectId");
    const agentId = requireValidId(c, "agentId");
    deps.access.requireProjectOwner(c.var.user.userId, projectId);
    const view = await deps.agentConfigService.insertTemplatePlaceholder(
      projectId,
      agentId,
      "schedules",
    );
    return c.json(view.config.schedules);
  });

  app.get("/:name", async (c) => {
    const projectId = requireValidId(c, "projectId");
    const agentId = requireValidId(c, "agentId");
    deps.access.requireProjectAccess(c.var.user.userId, projectId);
    const name = requireScheduleName(c.req.param("name"));
    const item = await readItem(deps, projectId, agentId, name);
    return c.json(item);
  });

  app.put("/:name", async (c) => {
    const projectId = requireValidId(c, "projectId");
    const agentId = requireValidId(c, "agentId");
    deps.access.requireProjectOwner(c.var.user.userId, projectId);
    const name = requireScheduleName(c.req.param("name"));
    if (!(await readScheduleFile(deps.config.root, projectId, agentId, name))) {
      throw new HttpError(404, "schedule_not_found", `Schedule does not exist: ${name}`);
    }
    const body = await readJson(c);
    await upsert(deps, c.var.user.userId, projectId, agentId, name, body);
    return c.json(await readItem(deps, projectId, agentId, name));
  });

  app.delete("/:name", async (c) => {
    const projectId = requireValidId(c, "projectId");
    const agentId = requireValidId(c, "agentId");
    deps.access.requireProjectOwner(c.var.user.userId, projectId);
    const name = requireScheduleName(c.req.param("name"));
    const removed = await deleteScheduleFile(deps.config.root, projectId, agentId, name);
    if (!removed)
      throw new HttpError(404, "schedule_not_found", `Schedule does not exist: ${name}`);
    deps.scheduler.dropEntry(projectId, agentId, name);
    return c.body(null, 204);
  });

  return app;
}

/**
 * The Project-wide listing. The sidebar's session list draws every Agent's Sessions, so the
 * scheduled-task mark it puts on a row is read from one request that spans the Project instead
 * of one request per Agent — reading a single Agent's list would leave every other row unmarked.
 */
export function projectScheduleRoutes(deps: SchedulesRouteDeps): Hono<AppEnv> {
  const app = new Hono<AppEnv>();

  app.get("/", async (c) => {
    const projectId = requireValidId(c, "projectId");
    deps.access.requireProjectAccess(c.var.user.userId, projectId);
    const { entries, invalid } = await deps.scheduler.listProject(projectId);
    // One clock reading for the whole response: every row's status and next fire time is derived
    // against the same instant, so a long list cannot straddle a slot boundary.
    const nowMs = Date.now();
    const res: ProjectSchedulesResponse = {
      schedules: entries.map((e) => ({
        agentId: e.agentId,
        ...toItem(e.def, e.state, e.queued, nowMs),
      })),
      invalidFiles: invalid,
    };
    return c.json(res);
  });

  return app;
}

/**
 * The cross-Project listing: every Project the caller may reach, in one answer. The
 * scheduled-tasks page's "all projects" scope reads this rather than walking the Project
 * switcher and asking once per Project — one request, and one clock reading for the whole
 * response, so a task's status and next fire time cannot differ between two Projects because
 * the listing straddled a slot boundary.
 *
 * Read-only, and deliberately so: this route writes nothing, and it names no Project. A write
 * still goes through the Project-scoped routes (one Project, one owner), which is what keeps
 * "which Project am I changing" a choice the user makes on a page about that Project.
 */
export function allProjectScheduleRoutes(deps: SchedulesRouteDeps): Hono<AppEnv> {
  const app = new Hono<AppEnv>();

  app.get("/", async (c) => {
    const projects = await deps.access.listProjects(c.var.user.userId);
    const nowMs = Date.now();
    const res: AllProjectSchedulesResponse = { projects: [] };
    for (const project of projects) {
      const { entries, invalid } = await deps.scheduler.listProject(project.projectId);
      res.projects.push({
        projectId: project.projectId,
        name: project.name ?? project.projectId,
        schedules: entries.map((e) => ({
          agentId: e.agentId,
          ...toItem(e.def, e.state, e.queued, nowMs),
        })),
        invalidFiles: invalid,
      });
    }
    return c.json(res);
  });

  return app;
}

/** Write + register creator + reconcile immediately (API changes take effect right away). */
async function upsert(
  deps: SchedulesRouteDeps,
  userId: string,
  projectId: string,
  agentId: string,
  name: string,
  body: Record<string, unknown>,
): Promise<void> {
  const fields = parseUpsertBody(body);
  const raw = serializeSchedule(fields);
  const parsed = parseScheduleFile(name, raw);
  if (!parsed.ok) throw badRequest(`Invalid schedule configuration: ${parsed.error}`);
  // A one-shot in the past would fire at once (not offered); a past end_at could never fire.
  const nowMs = Date.now();
  if (parsed.def.periodMs === undefined && parsed.def.startAtMs <= nowMs) {
    throw badRequest("start_at is already past: a one-time task needs a future start time.");
  }
  if (parsed.def.endAtMs !== undefined && parsed.def.endAtMs <= nowMs) {
    throw badRequest(
      "end_at is already past: the task would never fire again. Pick a later end time, or delete the task.",
    );
  }
  // At save time, verify the (provider, modelId) pair names a configured model (same rules as reconciliation) so we never persist a broken file.
  // The pairing rule itself is enforced by parseScheduleFile above, which rejects half a reference.
  const refError = await validateScheduleModelRef(deps.projectConfigService, projectId, parsed.def);
  if (refError !== null) throw badRequest(`Invalid schedule configuration: ${refError}`);
  await writeScheduleFile(deps.config.root, projectId, agentId, name, raw);
  // Creator attribution: the API writer is the creator (falls back to the Project owner only for hand-edited files).
  deps.schedulesRepo.registerOrSync({
    projectId,
    agentId,
    name,
    startAtMs: parsed.def.startAtMs,
    defHash: createHash("sha1").update(raw).digest("hex"),
    creatorUserId: userId,
  });
  await deps.scheduler.reconcileAgent(projectId, agentId);
}

async function readItem(
  deps: SchedulesRouteDeps,
  projectId: string,
  agentId: string,
  name: string,
): Promise<ScheduleItem> {
  const { entries, invalid } = await deps.scheduler.listAgent(projectId, agentId);
  const entry = entries.find((e) => e.def.name === name);
  if (entry) return toItem(entry.def, entry.state, entry.queued, Date.now());
  const bad = invalid.find((i) => i.name === name);
  if (bad) throw badRequest(`Invalid schedule file: ${bad.error}`);
  throw new HttpError(404, "schedule_not_found", `Schedule does not exist: ${name}`);
}

@Component({
  contributes: {
    "HttpModule.routes": [
      {
        id: "SchedulerRoutes.routes",
        prefix: "/api/projects/:projectId/agents/:agentId/schedules",
        auth: "user",
        order: 200,
      },
      {
        id: "SchedulerRoutes.projectRoutes",
        prefix: "/api/projects/:projectId/schedules",
        auth: "user",
        order: 200,
      },
      {
        id: "SchedulerRoutes.allProjectRoutes",
        prefix: "/api/schedules",
        auth: "user",
        order: 200,
      },
    ],
  },
})
export class SchedulerRoutes {
  @Use() private readonly config!: Config;
  @Use() private readonly agentConfig!: AgentConfig;
  @Use() private readonly projectConfig!: ProjectConfigStore;
  @Use() private readonly access!: Access;
  @Use() private readonly scheduler!: Scheduling;
  @Use() private readonly schedulesRepo!: Schedules;
  @Bind("SchedulerRoutes.routes") routes!: Hono<AppEnv>;
  @Bind("SchedulerRoutes.projectRoutes") projectRoutes!: Hono<AppEnv>;
  @Bind("SchedulerRoutes.allProjectRoutes") allProjectRoutes!: Hono<AppEnv>;
  setup() {
    const deps: SchedulesRouteDeps = {
      agentConfigService: this.agentConfig,
      config: this.config,
      projectConfigService: this.projectConfig,
      access: this.access,
      scheduler: this.scheduler,
      schedulesRepo: this.schedulesRepo,
    };
    this.routes = scheduleRoutes(deps);
    this.projectRoutes = projectScheduleRoutes(deps);
    this.allProjectRoutes = allProjectScheduleRoutes(deps);
  }
}