schedule: add paseo schedule update to edit schedules in place (#694)

* schedule: add `paseo schedule update` to edit schedules in place

Editing a schedule today requires delete+recreate, losing run history
and the schedule id. This adds an additive RPC and CLI command that
patches name, prompt, cadence, new-agent target fields, max-runs, and
expires-in without touching runs or in-flight executions. nextRunAt is
recomputed only when the cadence actually changes.

* schedule(cli): share cadence flag parser between create and update

Both paths were turning --every/--cron into a ScheduleCadence with
near-identical code. Extract `parseCadenceFromFlags` so the literals
and the exclusivity check live in one place; create wraps it to
require a value, update lets it stay optional.
This commit is contained in:
Mohamed Boudra
2026-05-05 19:56:52 +07:00
parent 82cf11aaae
commit 400934f19c
13 changed files with 887 additions and 8 deletions
@@ -392,6 +392,10 @@ type ScheduleRunOncePayload = Extract<
SessionOutboundMessage,
{ type: "schedule/run-once/response" }
>["payload"];
type ScheduleUpdatePayload = Extract<
SessionOutboundMessage,
{ type: "schedule/update/response" }
>["payload"];
export type FetchAgentTimelinePayload = FetchAgentTimelineResponseMessage["payload"];
export type FetchAgentTimelineDirection = FetchAgentTimelinePayload["direction"];
@@ -552,6 +556,30 @@ export interface InspectScheduleOptions {
id: string;
requestId?: string;
}
export interface UpdateScheduleNewAgentConfig {
provider?: string;
model?: string | null;
modeId?: string | null;
cwd?: string;
}
export interface UpdateScheduleOptions {
id: string;
name?: string | null;
prompt?: string;
cadence?:
| {
type: "every";
everyMs: number;
}
| {
type: "cron";
expression: string;
};
newAgentConfig?: UpdateScheduleNewAgentConfig;
maxRuns?: number | null;
expiresAt?: string | null;
requestId?: string;
}
type ListAvailableEditorsPayload = ListAvailableEditorsResponseMessage["payload"];
type OpenInEditorPayload = OpenInEditorResponseMessage["payload"];
type OpenProjectPayload = OpenProjectResponseMessage["payload"];
@@ -3762,6 +3790,24 @@ export class DaemonClient {
});
}
async scheduleUpdate(options: UpdateScheduleOptions): Promise<ScheduleUpdatePayload> {
return this.sendCorrelatedSessionRequest({
requestId: options.requestId,
message: {
type: "schedule/update",
scheduleId: options.id,
...(options.name !== undefined ? { name: options.name } : {}),
...(options.prompt !== undefined ? { prompt: options.prompt } : {}),
...(options.cadence !== undefined ? { cadence: options.cadence } : {}),
...(options.newAgentConfig !== undefined ? { newAgentConfig: options.newAgentConfig } : {}),
...(options.maxRuns !== undefined ? { maxRuns: options.maxRuns } : {}),
...(options.expiresAt !== undefined ? { expiresAt: options.expiresAt } : {}),
},
responseType: "schedule/update/response",
timeout: 10000,
});
}
async loopRun(options: RunLoopOptions): Promise<LoopRunPayload> {
return this.sendCorrelatedSessionRequest({
requestId: options.requestId,
@@ -75,6 +75,25 @@ export const ScheduleRunOnceRequestSchema = z.object({
scheduleId: z.string(),
});
const ScheduleUpdateNewAgentConfigSchema = z.object({
provider: z.string().trim().min(1).optional(),
model: z.string().trim().min(1).nullable().optional(),
modeId: z.string().trim().min(1).nullable().optional(),
cwd: z.string().trim().min(1).optional(),
});
export const ScheduleUpdateRequestSchema = z.object({
type: z.literal("schedule/update"),
requestId: z.string(),
scheduleId: z.string(),
name: z.string().nullable().optional(),
prompt: z.string().min(1).optional(),
cadence: ScheduleCadenceSchema.optional(),
newAgentConfig: ScheduleUpdateNewAgentConfigSchema.optional(),
maxRuns: z.number().int().positive().nullable().optional(),
expiresAt: z.string().nullable().optional(),
});
export const ScheduleCreateResponseSchema = z.object({
type: z.literal("schedule/create/response"),
payload: z.object({
@@ -146,3 +165,12 @@ export const ScheduleRunOnceResponseSchema = z.object({
error: z.string().nullable(),
}),
});
export const ScheduleUpdateResponseSchema = z.object({
type: z.literal("schedule/update/response"),
payload: z.object({
requestId: z.string(),
schedule: StoredScheduleSchema.nullable(),
error: z.string().nullable(),
}),
});
@@ -493,6 +493,244 @@ describe("ScheduleService", () => {
});
});
test("update mutates cadence, prompt, name, and target fields in place", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ok" }),
});
const created = await service.create({
name: "morning",
prompt: "first prompt",
cadence: { type: "every", everyMs: 60_000 },
target: {
type: "new-agent",
config: { provider: "claude", cwd: tempDir, modeId: "default" },
},
});
expect(created.runs).toEqual([]);
now = new Date("2026-01-01T00:00:30.000Z");
const updated = await service.update({
id: created.id,
prompt: "second prompt",
name: "renamed",
cadence: { type: "every", everyMs: 5 * 60_000 },
newAgentConfig: {
provider: "codex",
model: "gpt-5",
modeId: "full-access",
cwd: "/new/path",
},
});
expect(updated.prompt).toBe("second prompt");
expect(updated.name).toBe("renamed");
expect(updated.cadence).toEqual({ type: "every", everyMs: 5 * 60_000 });
expect(updated.target).toEqual({
type: "new-agent",
config: {
provider: "codex",
cwd: "/new/path",
model: "gpt-5",
modeId: "full-access",
},
});
expect(updated.nextRunAt).toBe("2026-01-01T00:05:30.000Z");
expect(updated.updatedAt).toBe("2026-01-01T00:00:30.000Z");
expect(updated.createdAt).toBe(created.createdAt);
});
test("update switches between every and cron cadences and recomputes nextRunAt", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ok" }),
});
const created = await service.create({
prompt: "p",
cadence: { type: "every", everyMs: 60_000 },
target: { type: "new-agent", config: { provider: "claude", cwd: tempDir } },
});
expect(created.nextRunAt).toBe("2026-01-01T00:00:00.000Z");
const cron = await service.update({
id: created.id,
cadence: { type: "cron", expression: "30 9 * * *" },
});
expect(cron.cadence).toEqual({ type: "cron", expression: "30 9 * * *" });
expect(cron.nextRunAt).toBe("2026-01-01T09:30:00.000Z");
const back = await service.update({
id: created.id,
cadence: { type: "every", everyMs: 2 * 60_000 },
});
expect(back.cadence).toEqual({ type: "every", everyMs: 2 * 60_000 });
expect(back.nextRunAt).toBe("2026-01-01T00:02:00.000Z");
});
test("update preserves nextRunAt and run history when cadence is unchanged", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ran" }),
});
const created = await service.create({
prompt: "p",
cadence: { type: "every", everyMs: 60_000 },
target: { type: "new-agent", config: { provider: "claude", cwd: tempDir } },
});
now = new Date("2026-01-01T00:01:00.000Z");
await service.tick();
const after = await service.inspect(created.id);
expect(after.runs).toHaveLength(1);
now = new Date("2026-01-01T00:01:30.000Z");
const updated = await service.update({ id: created.id, prompt: "new prompt" });
expect(updated.prompt).toBe("new prompt");
expect(updated.cadence).toEqual(created.cadence);
expect(updated.nextRunAt).toBe(after.nextRunAt);
expect(updated.runs).toEqual(after.runs);
expect(updated.lastRunAt).toBe(after.lastRunAt);
});
test("update clears the schedule name when given an empty string", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ok" }),
});
const created = await service.create({
name: "named",
prompt: "p",
cadence: { type: "every", everyMs: 60_000 },
target: { type: "new-agent", config: { provider: "claude", cwd: tempDir } },
});
expect(created.name).toBe("named");
const cleared = await service.update({ id: created.id, name: "" });
expect(cleared.name).toBeNull();
const renamed = await service.update({ id: created.id, name: "again" });
expect(renamed.name).toBe("again");
});
test("update rejects new-agent fields on agent-target schedules", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ok" }),
});
const created = await service.create({
prompt: "agent target",
cadence: { type: "every", everyMs: 60_000 },
target: { type: "agent", agentId: "00000000-0000-0000-0000-000000000005" },
});
await expect(
service.update({
id: created.id,
newAgentConfig: { provider: "codex" },
}),
).rejects.toThrow("only valid for new-agent target schedules");
});
test("update changes individual new-agent fields independently", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ok" }),
});
const created = await service.create({
prompt: "p",
cadence: { type: "every", everyMs: 60_000 },
target: {
type: "new-agent",
config: { provider: "claude", cwd: tempDir, model: "sonnet", modeId: "default" },
},
});
const modeOnly = await service.update({
id: created.id,
newAgentConfig: { modeId: "bypassPermissions" },
});
expect(modeOnly.target).toMatchObject({
type: "new-agent",
config: {
provider: "claude",
cwd: tempDir,
model: "sonnet",
modeId: "bypassPermissions",
},
});
const clearModel = await service.update({
id: created.id,
newAgentConfig: { model: null },
});
if (clearModel.target.type !== "new-agent") {
throw new Error("target type changed unexpectedly");
}
expect(clearModel.target.config.model).toBeUndefined();
expect(clearModel.target.config.modeId).toBe("bypassPermissions");
});
test("update returns a schedule that round-trips through the store", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
logger: createTestLogger(),
agentManager: new AgentManager({ logger: createTestLogger() }),
agentStorage,
now: () => now,
runner: async () => ({ agentId: null, output: "ok" }),
});
const created = await service.create({
prompt: "p",
cadence: { type: "every", everyMs: 60_000 },
target: { type: "new-agent", config: { provider: "claude", cwd: tempDir } },
});
await service.update({
id: created.id,
cadence: { type: "cron", expression: "0 9 * * *" },
newAgentConfig: { provider: "codex", modeId: "full-access" },
});
const reloaded = await service.inspect(created.id);
expect(reloaded.cadence).toEqual({ type: "cron", expression: "0 9 * * *" });
expect(reloaded.target).toEqual({
type: "new-agent",
config: { provider: "codex", cwd: tempDir, modeId: "full-access" },
});
});
test("runOnce rejects completed schedules", async () => {
const service = new ScheduleService({
paseoHome: tempDir,
@@ -13,7 +13,10 @@ import type {
CreateScheduleInput,
ScheduleExecutionResult,
ScheduleRun,
ScheduleTarget,
StoredSchedule,
UpdateScheduleInput,
UpdateScheduleNewAgentConfig,
} from "./types.js";
const SCHEDULE_TICK_INTERVAL_MS = 1000;
@@ -34,6 +37,44 @@ function normalizePrompt(prompt: string): string {
return trimmed;
}
function applyNewAgentConfig(
target: Extract<ScheduleTarget, { type: "new-agent" }>,
patch: UpdateScheduleNewAgentConfig,
): Extract<ScheduleTarget, { type: "new-agent" }> {
const config = { ...target.config };
if (patch.provider !== undefined) {
const trimmed = patch.provider.trim();
if (!trimmed) {
throw new Error("provider cannot be empty");
}
config.provider = trimmed;
}
if (patch.cwd !== undefined) {
const trimmed = patch.cwd.trim();
if (!trimmed) {
throw new Error("cwd cannot be empty");
}
config.cwd = trimmed;
}
if (patch.model !== undefined) {
const trimmed = patch.model?.trim();
if (trimmed) {
config.model = trimmed;
} else {
delete config.model;
}
}
if (patch.modeId !== undefined) {
const trimmed = patch.modeId?.trim();
if (trimmed) {
config.modeId = trimmed;
} else {
delete config.modeId;
}
}
return { ...target, config };
}
function normalizeMaxRuns(value: number | null | undefined): number | null {
if (value == null) {
return null;
@@ -219,6 +260,46 @@ export class ScheduleService {
return resumed;
}
async update(input: UpdateScheduleInput): Promise<StoredSchedule> {
const schedule = await this.inspect(input.id);
const now = this.now();
let updated: StoredSchedule = schedule;
if (input.prompt !== undefined) {
updated = { ...updated, prompt: normalizePrompt(input.prompt) };
}
if (input.name !== undefined) {
updated = { ...updated, name: trimOptionalName(input.name) };
}
if (input.cadence !== undefined) {
validateScheduleCadence(input.cadence);
const nextRunAt =
updated.status === "active" ? computeNextRunAt(input.cadence, now).toISOString() : null;
updated = { ...updated, cadence: input.cadence, nextRunAt };
}
if (input.newAgentConfig !== undefined) {
if (updated.target.type !== "new-agent") {
throw new Error("new-agent config updates are only valid for new-agent target schedules");
}
updated = { ...updated, target: applyNewAgentConfig(updated.target, input.newAgentConfig) };
}
if (input.maxRuns !== undefined) {
updated = { ...updated, maxRuns: normalizeMaxRuns(input.maxRuns) };
}
if (input.expiresAt !== undefined) {
updated = { ...updated, expiresAt: input.expiresAt };
}
updated = { ...updated, updatedAt: now.toISOString() };
await this.store.put(updated);
return updated;
}
async delete(id: string): Promise<void> {
await this.store.delete(id);
}
@@ -47,6 +47,44 @@ describe("ScheduleStore", () => {
expect(listed).toEqual([created]);
});
test("put round-trips an updated schedule to disk", async () => {
const created = await store.create({
name: "before",
prompt: "before",
cadence: { type: "every", everyMs: 60_000 },
target: {
type: "new-agent",
config: { provider: "claude", cwd: tempDir },
},
status: "active",
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
nextRunAt: "2026-01-01T00:01:00.000Z",
lastRunAt: null,
pausedAt: null,
expiresAt: null,
maxRuns: null,
runs: [],
});
const updated = {
...created,
name: "after",
prompt: "after",
cadence: { type: "cron" as const, expression: "0 9 * * *" },
target: {
type: "new-agent" as const,
config: { provider: "codex", cwd: "/elsewhere", modeId: "full-access" },
},
nextRunAt: "2026-01-01T09:00:00.000Z",
updatedAt: "2026-01-01T00:00:30.000Z",
};
await store.put(updated);
const reloaded = await new ScheduleStore(tempDir).get(created.id);
expect(reloaded).toEqual(updated);
});
test("deletes schedules from disk", async () => {
const created = await store.create({
name: null,
@@ -93,6 +93,23 @@ export interface CreateScheduleInput {
runOnCreate?: boolean | null;
}
export interface UpdateScheduleNewAgentConfig {
provider?: string;
model?: string | null;
modeId?: string | null;
cwd?: string;
}
export interface UpdateScheduleInput {
id: string;
name?: string | null;
prompt?: string;
cadence?: ScheduleCadence;
newAgentConfig?: UpdateScheduleNewAgentConfig;
maxRuns?: number | null;
expiresAt?: string | null;
}
export interface ScheduleExecutionResult {
agentId: string | null;
output: string | null;
+30 -1
View File
@@ -2163,6 +2163,8 @@ export class Session {
return this.handleScheduleDeleteRequest(msg);
case "schedule/run-once":
return this.handleScheduleRunOnceRequest(msg);
case "schedule/update":
return this.handleScheduleUpdateRequest(msg);
default:
return undefined;
}
@@ -8405,7 +8407,8 @@ export class Session {
| "schedule/pause"
| "schedule/resume"
| "schedule/delete"
| "schedule/run-once";
| "schedule/run-once"
| "schedule/update";
}
>,
error: unknown,
@@ -8579,6 +8582,32 @@ export class Session {
}
}
private async handleScheduleUpdateRequest(
request: Extract<SessionInboundMessage, { type: "schedule/update" }>,
): Promise<void> {
try {
const schedule = await this.scheduleService.update({
id: request.scheduleId,
...(request.name !== undefined ? { name: request.name } : {}),
...(request.prompt !== undefined ? { prompt: request.prompt } : {}),
...(request.cadence !== undefined ? { cadence: request.cadence } : {}),
...(request.newAgentConfig !== undefined ? { newAgentConfig: request.newAgentConfig } : {}),
...(request.maxRuns !== undefined ? { maxRuns: request.maxRuns } : {}),
...(request.expiresAt !== undefined ? { expiresAt: request.expiresAt } : {}),
});
this.emit({
type: "schedule/update/response",
payload: {
requestId: request.requestId,
schedule,
error: null,
},
});
} catch (error) {
this.emitScheduleRpcError(request, error);
}
}
private emitLoopRpcError(
request: Extract<
SessionInboundMessage,
+6
View File
@@ -29,6 +29,7 @@ import {
ScheduleResumeRequestSchema,
ScheduleDeleteRequestSchema,
ScheduleRunOnceRequestSchema,
ScheduleUpdateRequestSchema,
ScheduleCreateResponseSchema,
ScheduleListResponseSchema,
ScheduleInspectResponseSchema,
@@ -37,6 +38,7 @@ import {
ScheduleResumeResponseSchema,
ScheduleDeleteResponseSchema,
ScheduleRunOnceResponseSchema,
ScheduleUpdateResponseSchema,
} from "../server/schedule/rpc-schemas.js";
import {
LoopRunRequestSchema,
@@ -1768,6 +1770,7 @@ export const SessionInboundMessageSchema = z.discriminatedUnion("type", [
ScheduleResumeRequestSchema,
ScheduleDeleteRequestSchema,
ScheduleRunOnceRequestSchema,
ScheduleUpdateRequestSchema,
LoopRunRequestSchema,
LoopListRequestSchema,
LoopInspectRequestSchema,
@@ -3382,6 +3385,7 @@ export const SessionOutboundMessageSchema = z.discriminatedUnion("type", [
ScheduleResumeResponseSchema,
ScheduleDeleteResponseSchema,
ScheduleRunOnceResponseSchema,
ScheduleUpdateResponseSchema,
LoopRunResponseSchema,
LoopListResponseSchema,
LoopInspectResponseSchema,
@@ -3488,6 +3492,7 @@ export type SchedulePauseResponse = z.infer<typeof SchedulePauseResponseSchema>;
export type ScheduleResumeResponse = z.infer<typeof ScheduleResumeResponseSchema>;
export type ScheduleDeleteResponse = z.infer<typeof ScheduleDeleteResponseSchema>;
export type ScheduleRunOnceResponse = z.infer<typeof ScheduleRunOnceResponseSchema>;
export type ScheduleUpdateResponse = z.infer<typeof ScheduleUpdateResponseSchema>;
export type LoopRunResponse = z.infer<typeof LoopRunResponseSchema>;
export type LoopListResponse = z.infer<typeof LoopListResponseSchema>;
export type LoopInspectResponse = z.infer<typeof LoopInspectResponseSchema>;
@@ -3547,6 +3552,7 @@ export type SchedulePauseRequest = z.infer<typeof SchedulePauseRequestSchema>;
export type ScheduleResumeRequest = z.infer<typeof ScheduleResumeRequestSchema>;
export type ScheduleDeleteRequest = z.infer<typeof ScheduleDeleteRequestSchema>;
export type ScheduleRunOnceRequest = z.infer<typeof ScheduleRunOnceRequestSchema>;
export type ScheduleUpdateRequest = z.infer<typeof ScheduleUpdateRequestSchema>;
export type LoopRunRequest = z.infer<typeof LoopRunRequestSchema>;
export type LoopListRequest = z.infer<typeof LoopListRequestSchema>;
export type LoopInspectRequest = z.infer<typeof LoopInspectRequestSchema>;