Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

feat(schedules) allow schedule management during scheduled work (#4792)

Colton Paddenaa3d3fc0b37b
diff --git a/.changeset/allow-scheduled-schedule-management.md b/.changeset/allow-scheduled-schedule-management.md
new file mode 100644
index 000000000..4942bdf57
--- /dev/null
+++ b/.changeset/allow-scheduled-schedule-management.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Scheduled sessions and their locally delegated tasks can now use dynamic scheduling tools and the schedule client to manage schedules, including conditional updates or disabling future occurrences. Collection scope authorization and tool approval policies still apply.
diff --git a/e2e/fixtures/agent-schedules/agent/lib/mock-responder.ts b/e2e/fixtures/agent-schedules/agent/lib/mock-responder.ts
index ae1ac43a0..410ca60fd 100644
--- a/e2e/fixtures/agent-schedules/agent/lib/mock-responder.ts
+++ b/e2e/fixtures/agent-schedules/agent/lib/mock-responder.ts
@@ -8,16 +8,20 @@ const UPDATED_EMAIL = {
body: "A revised fixture note.",
};
-/**
- * Scripted mock for the world suites. Each `schedule-collection` eval prompt
- * names one action; the responder makes that call once and replies from its
- * output. Anything else keeps the default echo.
- */
+// World suites exercise the real tools without depending on model planning.
export function respond(request: MockModelRequest): MockModelResponse | string {
const message = request.lastUserMessage ?? "";
if (message.includes("This scheduled occurrence is firing now")) {
const email = message.includes(UPDATED_EMAIL.subject) ? UPDATED_EMAIL : EMAIL;
- return once(request, "record-email", email, () => `Sent "${email.subject}" to ${email.to}.`);
+ const reply = `Sent "${email.subject}" to ${email.to}.`;
+ return once(request, "record-email", email, (output) => {
+ if (!message.includes("disable this schedule")) return reply;
+ const result: { accepted?: boolean } = JSON.parse(output);
+ if (result.accepted !== true) return reply;
+ const name = /Schedule management name: ([0-9A-Za-z._-]+)\./u.exec(message)?.[1];
+ if (!name) throw new Error("The scheduled prompt needs a management name.");
+ return once(request, "schedule__requests__disable", { name }, () => reply);
+ });
}
if (message.includes(`Create the schedule named ${SCHEDULE}`)) {
const input = {
@@ -40,7 +44,7 @@ export function respond(request: MockModelRequest): MockModelResponse | string {
name,
expression: { type: "single", at: "2030-01-02T09:00", timezone: "UTC" },
payload: {
- task: `Send a fixture email with subject "${UPDATED_EMAIL.subject}" and body "${UPDATED_EMAIL.body}".`,
+ task: `Send a fixture email with subject "${UPDATED_EMAIL.subject}" and body "${UPDATED_EMAIL.body}". If record-email returns accepted: true, disable this schedule with schedule__requests__disable.`,
destination: "creator-email",
},
},
@@ -61,16 +65,15 @@ const OPERATION_PROMPTS = {
delivery: "delivered",
} as const;
-/** Calls `name` once per prompt, then replies with `reply(output)`. */
function once(
request: MockModelRequest,
name: string,
input: unknown,
- reply: (output: string) => string,
+ onResult: (output: string) => MockModelResponse | string,
): MockModelResponse | string {
const id = `${name}-${request.userMessageCount}`;
const result = request.toolResults.find((entry) => entry.id === id);
return result === undefined
? { toolCalls: [{ id, input, name }] }
- : reply(typeof result.output === "string" ? result.output : JSON.stringify(result.output));
+ : onResult(typeof result.output === "string" ? result.output : JSON.stringify(result.output));
}
diff --git a/e2e/fixtures/agent-schedules/agent/schedules/requests.ts b/e2e/fixtures/agent-schedules/agent/schedules/requests.ts
index e7a8191c5..37f63f421 100644
--- a/e2e/fixtures/agent-schedules/agent/schedules/requests.ts
+++ b/e2e/fixtures/agent-schedules/agent/schedules/requests.ts
@@ -14,6 +14,9 @@ export default defineDynamicSchedules({
destination: z.literal("creator-email"),
})
.strict(),
+ approval: {
+ disable: ({ session }) => (session.schedule === undefined ? "user-approval" : "not-applicable"),
+ },
// The fixture identity is local and fixed; production apps must revalidate their account system.
auth: async ({ principal }) => ({
attributes: { "fixture.email": "[email protected]" },
@@ -28,7 +31,7 @@ export default defineDynamicSchedules({
if (payload.destination !== "creator-email" || typeof recipient !== "string")
throw new Error("The creator has no authorized fixture email destination.");
await to(outbox, { scheduleName: occurrence.name }).send(
- `This scheduled occurrence is firing now. Perform the task using record-email for ${recipient}. Do not create another schedule.\n\nTask: ${payload.task}`,
+ `This scheduled occurrence is firing now. Schedule management name: ${occurrence.name}. Perform the task using record-email for ${recipient}. Do not create another schedule.\n\nTask: ${payload.task}`,
);
},
events: {
diff --git a/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.ts b/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.ts
index 106f6cfde..017d0053f 100644
--- a/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.ts
+++ b/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.ts
@@ -1,7 +1,7 @@
import { defineEval } from "eve/evals";
export default defineEval({
description:
- "A principal-owned subscription derives an email destination from creator identity and executes its task through an authored channel.",
+ "A principal-owned subscription derives an email destination from creator identity, executes its task through an authored channel, and conditionally disables itself.",
async test(t) {
if (!t.target.capabilities.devRoutes)
t.skip("Schedule collection invocation requires dev routes.");
@@ -33,7 +33,7 @@ export default defineEval({
const managementName = Reflect.get(Object(record), "name");
if (typeof managementName !== "string") throw new Error("The schedule has no management name.");
const edit = await t.send(
- `Alice wants to revise her saved reminder. Update the schedule using management name ${managementName}. Set its one-time timing to January 2, 2030 at 09:00 UTC and replace its complete payload with a task to send a fixture email with subject Updated note and body A revised fixture note., and destination creator-email. Save the change without sending the email now.`,
+ `Alice wants to revise her saved reminder. Update the schedule using management name ${managementName}. Set its one-time timing to January 2, 2030 at 09:00 UTC and its destination to creator-email. Replace its complete task with these instructions: Send a fixture email with subject Updated note and body A revised fixture note. If record-email returns accepted: true, disable this schedule with schedule__requests__disable. Save the change without sending the email now.`,
);
edit.expectOk();
const updateApproval = edit.session.requireInputRequest({
@@ -82,6 +82,11 @@ export default defineEval({
input: { to: "[email protected]", subject: "Updated note" },
output: { recipient: "[email protected]", accepted: true },
});
+ scheduled.calledTool("schedule__requests__disable", {
+ input: { name: managementName },
+ output: { name: managementName, state: "inactive" },
+ status: "completed",
+ });
// The authored outbox channel records the scheduled session's reply.
const delivered = await t.send("Use share-schedule to read what collection-email delivered.");
delivered.succeeded();
diff --git a/packages/eve/src/context/schedule-collection-tools.ts b/packages/eve/src/context/schedule-collection-tools.ts
index 0eb8a997c..14c3d3766 100644
--- a/packages/eve/src/context/schedule-collection-tools.ts
+++ b/packages/eve/src/context/schedule-collection-tools.ts
@@ -7,9 +7,8 @@ import type {
ApprovalPolicy,
ApprovalResponsePolicy,
} from "#approval/definition.js";
-import { loadContext, contextStorage } from "#context/container.js";
+import { loadContext } from "#context/container.js";
import { ContextKey } from "#context/key.js";
-import { readSessionSchedule } from "#context/session-schedule.js";
import { defineDynamic } from "#dynamic/definition.js";
import { isApprovalRecheck } from "#harness/approval-recheck.js";
import { markDynamicCallbackRebind } from "#internal/dynamic-tool-rebind.js";
@@ -19,10 +18,7 @@ import type {
DynamicSchedulesDefinition,
ScheduleCreateApproval,
} from "#public/schedules/subscription.js";
-import {
- assertScheduleManagementAllowed,
- type PreparedScheduleWrite,
-} from "#runtime/schedules/collection-client.js";
+import type { PreparedScheduleWrite } from "#runtime/schedules/collection-client.js";
import { scheduleDisplayName } from "#runtime/schedules/record.js";
import { MAX_SCHEDULE_DELAY_MINUTES } from "#runtime/schedules/validation.js";
import { parseJsonObject } from "#shared/json.js";
@@ -94,14 +90,8 @@ export function createScheduleCollectionToolDynamicDefinition<TInput, TPrepared
return markDynamicCallbackRebind(
defineDynamic({
events: {
- "turn.started": async (_event, context) => {
- const scope = contextStorage.getStore();
- if (
- definition.tool === false ||
- context.session.schedule !== undefined ||
- (scope !== undefined && readSessionSchedule(scope) !== undefined)
- )
- return null;
+ "turn.started": async () => {
+ if (definition.tool === false) return null;
const prefix = `${scheduleCollectionToolPrefix(identity.collection)}__`;
const keyFor = (operation: "create" | "update", callId: string) =>
`${prefix}${operation}:${callId}`;
@@ -151,10 +141,7 @@ export function createScheduleCollectionToolDynamicDefinition<TInput, TPrepared
start: (input: { name?: string }) =>
input.name ? `${action}: ${scheduleDisplayName(input.name)}` : action,
},
- execute: (input: any, context) => {
- assertScheduleManagementAllowed();
- return execute(input, context);
- },
+ execute,
});
stampCallbacks(tool);
return tool as DynamicToolEntry;
diff --git a/packages/eve/src/runtime/schedules/collection-client.test.ts b/packages/eve/src/runtime/schedules/collection-client.test.ts
index f0fb0ed7d..bef945c71 100644
--- a/packages/eve/src/runtime/schedules/collection-client.test.ts
+++ b/packages/eve/src/runtime/schedules/collection-client.test.ts
@@ -97,12 +97,21 @@ describe("schedule subscription client", () => {
expect(create).not.toHaveBeenCalled();
});
- it("isolates callers and checks policy separately for every management operation", async () => {
- const { definition, client, provider } = setup();
+ it.each([false, true])("caller isolation and scope (scheduled: %s)", async (scheduled) => {
+ const { definition, provider } = setup();
+ const context = {
+ ...callContext,
+ session: {
+ ...callContext.session,
+ schedule: scheduled ? { definition: "requests", occurrenceId: "exec-1" } : undefined,
+ },
+ };
+ const client = createScheduleCollectionClient(definition, context);
const created = await client.create(input);
const bob = createScheduleCollectionClient(definition, {
- ...callContext,
+ ...context,
session: {
+ ...context.session,
id: "bob",
auth: {
current: { ...callContext.session.auth.current!, principalId: "bob" },
@@ -119,7 +128,7 @@ describe("schedule subscription client", () => {
);
const denied = createScheduleCollectionClient(
{ ...definition, scope, preparePayload: prepare },
- callContext,
+ context,
);
const get = vi.spyOn(provider, "get");
await expect(denied.get(created.name)).rejects.toThrow("not available");
@@ -152,20 +161,4 @@ describe("schedule subscription client", () => {
expect(update).not.toHaveBeenCalled();
await expect(client.get(created.name)).resolves.toEqual(created);
});
-
- it("refuses scheduled-execution management through the custom client too", async () => {
- const { definition, create } = setup();
- const scheduled = createScheduleCollectionClient(definition, {
- ...callContext,
- session: {
- ...callContext.session,
- schedule: { definition: "requests", occurrenceId: "exec-1" },
- },
- });
- await expect(scheduled.create(input)).rejects.toThrow("unavailable during scheduled execution");
- await expect(scheduled.update("report", { payload })).rejects.toThrow(
- "unavailable during scheduled execution",
- );
- expect(create).not.toHaveBeenCalled();
- });
});
diff --git a/packages/eve/src/runtime/schedules/collection-client.ts b/packages/eve/src/runtime/schedules/collection-client.ts
index c0a772893..392ca4de9 100644
--- a/packages/eve/src/runtime/schedules/collection-client.ts
+++ b/packages/eve/src/runtime/schedules/collection-client.ts
@@ -1,9 +1,7 @@
import type { StandardSchemaV1 } from "#compiled/@standard-schema/spec/index.js";
import { createHash, randomUUID } from "node:crypto";
import { isDeepStrictEqual } from "node:util";
-import { readSessionSchedule } from "#context/session-schedule.js";
-import { loadContext, contextStorage } from "#context/container.js";
-import type { SessionSchedule } from "#context/session-schedule.js";
+import { loadContext } from "#context/container.js";
import { dispatchScheduledOccurrence } from "#runtime/schedules/dispatch-occurrence.js";
import { BundleKey } from "#runtime/sessions/runtime-context-keys.js";
import type {
@@ -94,7 +92,6 @@ export function createScheduleCollectionClient<TPayload, TPrepared = TPayload>(
operation: NonNullable<ScheduleScopeContext["operation"]>,
name?: string,
): Promise<{ scope: ScheduleScopeValue; provider: ScheduleProviderContext }> => {
- assertScheduleManagementAllowed(callContext.session.schedule);
const context = contextFor(operation, name);
const scope =
definition.scope === undefined
@@ -283,15 +280,6 @@ function snapshotPreparedWrite<T extends PreparedScheduleWrite>(prepared: T): T
return JSON.parse(json) as T;
}
-export function assertScheduleManagementAllowed(schedule?: SessionSchedule): void {
- const context = contextStorage.getStore();
- if (
- schedule !== undefined ||
- (context !== undefined && readSessionSchedule(context) !== undefined)
- )
- throw new Error("Schedule management is unavailable during scheduled execution.");
-}
-
function principalReference(
auth: {
readonly principalType: string;
diff --git a/packages/eve/src/runtime/schedules/subscription.integration.test.ts b/packages/eve/src/runtime/schedules/subscription.integration.test.ts
index 2e0606434..ece339165 100644
--- a/packages/eve/src/runtime/schedules/subscription.integration.test.ts
+++ b/packages/eve/src/runtime/schedules/subscription.integration.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import { z } from "#compiled/zod/index.js";
import { resolveApprovalPolicy, type ApprovalConfiguration } from "#approval/definition.js";
import { loadContext } from "#context/container.js";
-import { AuthKey } from "#context/keys.js";
+import { AuthKey, ScheduleIdKey, OccurrenceIdKey } from "#context/keys.js";
import { createTestRuntime } from "#internal/testing/app-harness.js";
import { defineDynamicSchedules } from "#public/schedules/subscription.js";
import { schedules } from "#public/experimental/schedules/client.js";
@@ -33,7 +33,7 @@ async function bindCaller() {
}
describe("schedule creation and invocation", () => {
- it("exposes direct operation schemas and approves prepared data without writing a changed destination", async () => {
+ it.each([false, true])("prepared approvals (scheduled: %s)", async (scheduled) => {
let destination = "C123";
const provider = inMemoryScheduleProvider();
const write = vi.spyOn(provider, "create");
@@ -83,6 +83,11 @@ describe("schedule creation and invocation", () => {
});
await app.runAsSession(undefined, async () => {
await bindCaller();
+ const schedule = scheduled ? { definition: "requests", occurrenceId: "exec-1" } : undefined;
+ if (schedule !== undefined) {
+ loadContext().set(ScheduleIdKey, schedule.definition);
+ loadContext().set(OccurrenceIdKey, schedule.occurrenceId);
+ }
const wrapper = app.manifest.dynamicTools.find(
(entry) => entry.logicalPath === "tools/schedule__requests.ts",
)!;
@@ -90,7 +95,7 @@ describe("schedule creation and invocation", () => {
.default as ReturnType<typeof import("#dynamic/definition.js").defineDynamic>;
const tools = (await dynamic.events["turn.started"]!(undefined, {
model: null,
- session: { id: "alice", auth: { current: alice, initiator: null } },
+ session: { id: "alice", auth: { current: alice, initiator: null }, schedule },
channel: {},
messages: [],
})) as Record<string, DynamicToolEntry>;
@@ -180,6 +185,12 @@ describe("schedule creation and invocation", () => {
expect(responseTargets.at(-1)).toBe("timing-only");
await update.execute(timingInput, { callId: "timing" } as never);
expect(updateWrite.mock.calls[1]![2].payload).toBeUndefined();
+ await expect(
+ tools.schedule__requests__disable!.execute({ name: created.name }, {} as never),
+ ).resolves.toMatchObject({ state: "inactive" });
+ await expect(
+ tools.schedule__requests__enable!.execute({ name: created.name }, {} as never),
+ ).resolves.toMatchObject({ state: "active" });
const invoke = tools.schedule__requests__invoke!;
const invoked = { name: created.name };
const invokeContext = {