Files
opencode-manager/backend/src/services/schedule-config.ts
T
Chris Scott 68cd6b5a0b feat: add schedule permission config and workspace tracking (#297)
* feat: add schedule permission config and workspace tracking

Adds permission configuration for scheduled jobs (migration 016) and
workspace ID tracking on schedule runs (migration 017). Includes
service logic, UI controls, schema updates, and full test coverage.

* refactor: convert permission ruleset to object format, exclude active schedule worktrees from sibling repos

* refine: reduce destructive bash patterns to host-level commands only
2026-06-30 12:03:35 -04:00

164 lines
5.3 KiB
TypeScript

import { Cron } from 'croner'
import type {
CreateScheduleJobRequest,
ScheduleJob,
ScheduleMode,
SchedulePermissionConfig,
ScheduleSkillMetadata,
UpdateScheduleJobRequest,
} from '@opencode-manager/shared/types'
const DEFAULT_CRON_TIMEZONE = 'UTC'
export interface ScheduleJobPersistenceInput {
name: string
description: string | null
enabled: boolean
scheduleMode: ScheduleMode
intervalMinutes: number | null
cronExpression: string | null
timezone: string | null
agentSlug: string | null
prompt: string
model: string | null
skillMetadata: ScheduleSkillMetadata | null | undefined
permissionConfig: SchedulePermissionConfig | null
branch: string | null
nextRunAt: number | null
}
function validateTimeZone(timezone: string): string {
try {
new Intl.DateTimeFormat('en-US', { timeZone: timezone })
return timezone
} catch {
throw new Error(`Invalid timezone: ${timezone}`)
}
}
function getCronNextRunAt(cronExpression: string, timezone: string, currentDate: number): number {
const cron = new Cron(cronExpression, { timezone })
const next = cron.nextRun(new Date(currentDate))
if (!next) {
throw new Error(`Cron expression "${cronExpression}" has no upcoming run`)
}
return next.getTime()
}
function normalizeCronConfig(cronExpression: string, timezone: string | null | undefined, currentDate: number) {
const normalizedTimezone = validateTimeZone(timezone?.trim() || DEFAULT_CRON_TIMEZONE)
const normalizedCronExpression = cronExpression.trim()
const nextRunAt = getCronNextRunAt(normalizedCronExpression, normalizedTimezone, currentDate)
return {
scheduleMode: 'cron' as const,
intervalMinutes: null,
cronExpression: normalizedCronExpression,
timezone: normalizedTimezone,
nextRunAt,
}
}
function normalizeIntervalConfig(intervalMinutes: number, currentDate: number) {
return {
scheduleMode: 'interval' as const,
intervalMinutes,
cronExpression: null,
timezone: null,
nextRunAt: currentDate + intervalMinutes * 60_000,
}
}
export function computeNextRunAtForJob(job: ScheduleJob, currentDate: number): number | null {
if (!job.enabled) {
return null
}
if (job.scheduleMode === 'cron') {
if (!job.cronExpression) {
throw new Error('Cron expression is required for cron schedules')
}
return getCronNextRunAt(job.cronExpression, job.timezone || DEFAULT_CRON_TIMEZONE, currentDate)
}
if (!job.intervalMinutes) {
throw new Error('Interval minutes are required for interval schedules')
}
return currentDate + job.intervalMinutes * 60_000
}
export function buildCreateSchedulePersistenceInput(input: CreateScheduleJobRequest, currentDate: number = Date.now()): ScheduleJobPersistenceInput {
const base = {
name: input.name.trim(),
description: input.description?.trim() || null,
enabled: input.enabled !== false,
agentSlug: input.agentSlug?.trim() || null,
prompt: input.prompt.trim(),
model: input.model?.trim() || null,
skillMetadata: input.skillMetadata,
branch: input.branch?.trim() || null,
}
const scheduleConfig = input.scheduleMode === 'cron'
? normalizeCronConfig(input.cronExpression, input.timezone, currentDate)
: normalizeIntervalConfig(input.intervalMinutes, currentDate)
return {
...base,
...scheduleConfig,
permissionConfig: input.permissionConfig ?? null,
nextRunAt: base.enabled ? scheduleConfig.nextRunAt : null,
}
}
export function buildUpdatedSchedulePersistenceInput(
existing: ScheduleJob,
input: UpdateScheduleJobRequest,
currentDate: number = Date.now(),
): ScheduleJobPersistenceInput {
const enabled = input.enabled ?? existing.enabled
const scheduleMode = input.scheduleMode ?? existing.scheduleMode
const scheduleConfig = scheduleMode === 'cron'
? normalizeCronConfig(
input.cronExpression ?? existing.cronExpression ?? '',
input.timezone ?? existing.timezone ?? DEFAULT_CRON_TIMEZONE,
currentDate,
)
: normalizeIntervalConfig(
input.intervalMinutes ?? existing.intervalMinutes ?? 60,
currentDate,
)
const scheduleChanged =
input.scheduleMode !== undefined ||
input.intervalMinutes !== undefined ||
input.cronExpression !== undefined ||
input.timezone !== undefined
const nextRunAt = enabled
? scheduleChanged || input.enabled !== undefined || existing.nextRunAt === null
? scheduleConfig.nextRunAt
: existing.nextRunAt
: null
return {
name: input.name?.trim() || existing.name,
description: input.description === undefined ? existing.description : (input.description?.trim() || null),
enabled,
scheduleMode: scheduleConfig.scheduleMode,
intervalMinutes: scheduleConfig.intervalMinutes,
cronExpression: scheduleConfig.cronExpression,
timezone: scheduleConfig.timezone,
agentSlug: input.agentSlug === undefined ? existing.agentSlug : (input.agentSlug?.trim() || null),
prompt: input.prompt?.trim() || existing.prompt,
model: input.model === undefined ? existing.model : (input.model?.trim() || null),
skillMetadata: input.skillMetadata !== undefined ? input.skillMetadata : existing.skillMetadata,
permissionConfig: input.permissionConfig === undefined ? existing.permissionConfig : (input.permissionConfig ?? null),
branch: input.branch === undefined ? existing.branch : (input.branch?.trim() || null),
nextRunAt,
}
}