Skip to content

Personal AI, On Personal Devices

Python

fix(agents): preserve schedule preset defaults (#928)

Haotian Zheng2da0b0065c41
diff --git a/frontend/src/lib/agent-schedule.test.ts b/frontend/src/lib/agent-schedule.test.ts
index 67e042fc..1f8ba977 100644
--- a/frontend/src/lib/agent-schedule.test.ts
+++ b/frontend/src/lib/agent-schedule.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { getAgentSchedule } from './agent-schedule';
+import { getAgentSchedule, normalizeAgentSchedule } from './agent-schedule';
describe('getAgentSchedule', () => {
it('reads schedule fields from the nested agent config', () => {
@@ -45,3 +45,54 @@ describe('getAgentSchedule', () => {
).toEqual({ type: 'interval', value: '60' });
});
});
+
+describe('normalizeAgentSchedule', () => {
+ it('fills the displayed one-hour default for an untouched hourly preset', () => {
+ expect(normalizeAgentSchedule('hourly', '')).toEqual({
+ type: 'interval',
+ value: '3600',
+ });
+ });
+
+ it('does not reuse a cron value as an interval', () => {
+ expect(normalizeAgentSchedule('hourly', '0 9 * * *')).toEqual({
+ type: 'interval',
+ value: '3600',
+ });
+ });
+
+ it('replaces a non-positive interval with the one-hour default', () => {
+ expect(normalizeAgentSchedule('hourly', 0)).toEqual({
+ type: 'interval',
+ value: '3600',
+ });
+ });
+
+ it('does not reuse an interval value as a daily cron expression', () => {
+ expect(normalizeAgentSchedule('daily', '3600')).toEqual({
+ type: 'cron',
+ value: '0 9 * * *',
+ });
+ });
+
+ it('preserves a user-selected hourly interval', () => {
+ expect(normalizeAgentSchedule('hourly', '7200')).toEqual({
+ type: 'interval',
+ value: '7200',
+ });
+ });
+
+ it('uses a safe weekly default when no day has been selected', () => {
+ expect(normalizeAgentSchedule('weekly', '')).toEqual({
+ type: 'cron',
+ value: '0 9 * * 1',
+ });
+ });
+
+ it('clears the value for a manual schedule', () => {
+ expect(normalizeAgentSchedule('manual', '3600')).toEqual({
+ type: 'manual',
+ value: '',
+ });
+ });
+});
diff --git a/frontend/src/lib/agent-schedule.ts b/frontend/src/lib/agent-schedule.ts
index 1f5e1e88..f89ed26f 100644
--- a/frontend/src/lib/agent-schedule.ts
+++ b/frontend/src/lib/agent-schedule.ts
@@ -10,6 +10,28 @@ export interface AgentSchedule {
value?: string;
}
+export interface AgentScheduleConfig {
+ type: string;
+ value: string;
+}
+
+const DEFAULT_DAILY_CRON = '0 9 * * *';
+const DEFAULT_WEEKLY_CRON = '0 9 * * 1';
+const DEFAULT_HOURLY_SECONDS = '3600';
+
+function isPositiveInterval(value: string): boolean {
+ const seconds = Number(value);
+ return value.length > 0 && Number.isFinite(seconds) && seconds > 0;
+}
+
+function isDailyCron(value: string): boolean {
+ return /^0\s+(?:[0-9]|1[0-9]|2[0-3])\s+\*\s+\*\s+\*$/.test(value);
+}
+
+function isWeeklyCron(value: string): boolean {
+ return /^0\s+(?:[0-9]|1[0-9]|2[0-3])\s+\*\s+\*\s+[1-7](?:,[1-7])*$/.test(value);
+}
+
function scheduleValue(value: unknown): string | undefined {
if (typeof value === 'string') return value;
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
@@ -30,3 +52,33 @@ export function getAgentSchedule(agent: AgentScheduleSource): AgentSchedule {
scheduleValue(agent.schedule_value),
};
}
+
+/** Convert the launch wizard's friendly presets to a valid API schedule. */
+export function normalizeAgentSchedule(type: string, value: unknown): AgentScheduleConfig {
+ const current = scheduleValue(value)?.trim() ?? '';
+
+ switch (type) {
+ case 'manual':
+ return { type: 'manual', value: '' };
+ case 'daily':
+ return {
+ type: 'cron',
+ value: isDailyCron(current) ? current : DEFAULT_DAILY_CRON,
+ };
+ case 'weekly':
+ return {
+ type: 'cron',
+ value: isWeeklyCron(current) ? current : DEFAULT_WEEKLY_CRON,
+ };
+ case 'hourly':
+ case 'interval':
+ return {
+ type: 'interval',
+ value: isPositiveInterval(current) ? current : DEFAULT_HOURLY_SECONDS,
+ };
+ case 'cron':
+ return { type: 'cron', value: current };
+ default:
+ return { type, value: current };
+ }
+}
diff --git a/frontend/src/pages/AgentsPage.tsx b/frontend/src/pages/AgentsPage.tsx
index cb4d2cc1..e9cfb121 100644
--- a/frontend/src/pages/AgentsPage.tsx
+++ b/frontend/src/pages/AgentsPage.tsx
@@ -67,7 +67,7 @@ import type { ConnectRequest } from '../types/connectors';
import { listConnectors, connectSource } from '../lib/connectors-api';
import type { ToolCallInfo } from '../types';
import { ToolCallCard } from '../components/Chat/ToolCallCard';
-import { getAgentSchedule } from '../lib/agent-schedule';
+import { getAgentSchedule, normalizeAgentSchedule } from '../lib/agent-schedule';
// ---------------------------------------------------------------------------
// Status helpers
@@ -731,16 +731,10 @@ function LaunchWizard({
if (!wizard.name.trim()) { toast.error('Name is required'); return; }
setLaunching(true);
try {
- // Map friendly schedule presets to API schedule_type/schedule_value
- let apiScheduleType = wizard.scheduleType;
- let apiScheduleValue = wizard.scheduleValue;
- if (wizard.scheduleType === 'daily' || wizard.scheduleType === 'weekly') {
- apiScheduleType = 'cron';
- // scheduleValue already holds the cron expression
- } else if (wizard.scheduleType === 'hourly') {
- apiScheduleType = 'interval';
- // scheduleValue already holds seconds as string
- }
+ const { type: apiScheduleType, value: apiScheduleValue } = normalizeAgentSchedule(
+ wizard.scheduleType,
+ wizard.scheduleValue,
+ );
const config: Record<string, unknown> = {
schedule_type: apiScheduleType,