The Open Framework for Building Agents
test(eve): cover schedule subscription creation and execution (#4012)
Colton Padden41cea61745e1
diff --git a/e2e/fixtures/agent-schedules/agent/agent.ts b/e2e/fixtures/agent-schedules/agent/agent.tsindex 384bb0e2a..95fd9acdd 100644--- a/e2e/fixtures/agent-schedules/agent/agent.ts+++ b/e2e/fixtures/agent-schedules/agent/agent.ts@@ -1,7 +1,8 @@import { e2eAgentConfig } from "@eve-e2e/config";import { defineAgent } from "eve";+import { respond } from "./lib/mock-responder.js";export default defineAgent({- ...e2eAgentConfig(),+ ...e2eAgentConfig({ mock: respond }),reasoning: "high",});diff --git a/e2e/fixtures/agent-schedules/agent/channels/outbox.ts b/e2e/fixtures/agent-schedules/agent/channels/outbox.tsnew file mode 100644index 000000000..1e657e900--- /dev/null+++ b/e2e/fixtures/agent-schedules/agent/channels/outbox.ts@@ -0,0 +1,22 @@+import { defineChannel, POST } from "eve/channels";+import { recordCollectionDelivery } from "../lib/collection-occurrences";++export default defineChannel<+ { scheduleName: string },+ { scheduleName: string },+ { scheduleName: string }+>({+ routes: [POST("/outbox", async () => new Response(null, { status: 405 }))],+ state: { scheduleName: "" },+ context: (state) => ({ scheduleName: state.scheduleName }),+ async receive(input, { from }) {+ return await from(input.target.scheduleName).send(input.message, {+ auth: input.auth,+ state: { scheduleName: input.target.scheduleName },+ });+ },+ events: {+ "message.completed": ({ message }, channel) =>+ recordCollectionDelivery(channel.scheduleName, message),+ },+});diff --git a/e2e/fixtures/agent-schedules/agent/instructions.md b/e2e/fixtures/agent-schedules/agent/instructions.mdindex bce87b044..52b1d724e 100644--- a/e2e/fixtures/agent-schedules/agent/instructions.md+++ b/e2e/fixtures/agent-schedules/agent/instructions.md@@ -1,3 +1 @@-# Identity--You are a helpful assistant.+You can manage schedules for the authenticated principal with the direct schedule__requests__create/get/list/enable/disable/invoke/delete tools. Supply a payload containing the task and destination creator-email. The subscription resolves the creator's current identity and derives their fixture email at execution time; do not put a recipient address in the scheduling payload. Use record-email only when a scheduled task asks you to send a fixture email. It validates the recipient against execution identity. The share-schedule tool demonstrates the custom client and applies the same management authorization. The outbox channel records the scheduled agent's reply in-process.diff --git a/e2e/fixtures/agent-schedules/agent/lib/collection-occurrences.ts b/e2e/fixtures/agent-schedules/agent/lib/collection-occurrences.tsnew file mode 100644index 000000000..331fbf71b--- /dev/null+++ b/e2e/fixtures/agent-schedules/agent/lib/collection-occurrences.ts@@ -0,0 +1,25 @@+const admitted = new Map<string, string>();++export function recordCollectionOccurrence(name: string, sessionId: string): void {+ if (admitted.size >= 100) admitted.delete(admitted.keys().next().value!);+ admitted.set(name, sessionId);+}++export function takeCollectionOccurrence(name: string): string | undefined {+ const sessionId = admitted.get(name);+ admitted.delete(name);+ return sessionId;+}++const deliveries = new Map<string, string>();++export function recordCollectionDelivery(name: string, content: string): void {+ if (deliveries.size >= 100) deliveries.delete(deliveries.keys().next().value!);+ deliveries.set(name, content);+}++export function takeCollectionDelivery(name: string): string | undefined {+ const content = deliveries.get(name);+ deliveries.delete(name);+ return content;+}diff --git a/e2e/fixtures/agent-schedules/agent/lib/mock-responder.ts b/e2e/fixtures/agent-schedules/agent/lib/mock-responder.tsnew file mode 100644index 000000000..2d932c123--- /dev/null+++ b/e2e/fixtures/agent-schedules/agent/lib/mock-responder.ts@@ -0,0 +1,53 @@+import type { MockModelRequest, MockModelResponse } from "eve/evals";++const SCHEDULE = "collection-email";+const EMAIL = { to: "[email protected]", subject: "Scheduled note", body: "A 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.+ */+export function respond(request: MockModelRequest): MockModelResponse | string {+ const message = request.lastUserMessage ?? "";+ if (message.includes("This scheduled occurrence is firing now")) {+ return once(request, "record-email", EMAIL, () => `Sent "${EMAIL.subject}" to ${EMAIL.to}.`);+ }+ if (message.includes(`Create the schedule named ${SCHEDULE}`)) {+ const input = {+ name: SCHEDULE,+ expression: { type: "single", at: "2030-01-01T09:00", timezone: "UTC" },+ payload: {+ task: `Send a fixture email with subject "${EMAIL.subject}" and body "${EMAIL.body}".`,+ destination: "creator-email",+ },+ };+ return once(request, "schedule__requests__create", input, () => `Saved ${SCHEDULE}.`);+ }+ for (const operation of ["get", "invoke", "delivery"] as const) {+ if (message.includes("share-schedule") && message.includes(OPERATION_PROMPTS[operation])) {+ return once(request, "share-schedule", { operation, name: SCHEDULE }, (output) => output);+ }+ }+ return `Mock reply: ${message}`;+}++const OPERATION_PROMPTS = {+ get: "get operation",+ invoke: "invoke",+ 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,+): 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));+}diff --git a/e2e/fixtures/agent-schedules/agent/schedules/requests.ts b/e2e/fixtures/agent-schedules/agent/schedules/requests.tsnew file mode 100644index 000000000..e7a8191c5--- /dev/null+++ b/e2e/fixtures/agent-schedules/agent/schedules/requests.ts@@ -0,0 +1,39 @@+import { defineDynamicSchedules } from "eve/experimental/schedules";+import { inMemoryScheduleProvider } from "eve/experimental/schedules/testing";+import { z } from "zod";+import outbox from "../channels/outbox";+import { recordCollectionOccurrence } from "../lib/collection-occurrences";++export default defineDynamicSchedules({+ description:+ "Manage process-local fixture tasks. Destination creator-email means the creator's fixture email, not a model-selected address.",+ provider: inMemoryScheduleProvider(),+ inputSchema: z+ .object({+ task: z.string().min(1).max(2000),+ destination: z.literal("creator-email"),+ })+ .strict(),+ // The fixture identity is local and fixed; production apps must revalidate their account system.+ auth: async ({ principal }) => ({+ attributes: { "fixture.email": "[email protected]" },+ authenticator: principal.authenticator,+ issuer: principal.issuer,+ subject: principal.subject,+ principalId: principal.principalId,+ principalType: principal.type,+ }),+ async run({ payload, occurrence, to, auth }) {+ const recipient = auth.attributes["fixture.email"];+ 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}`,+ );+ },+ events: {+ "occurrence.dispatched": ({ name, sessionIds }) => {+ if (sessionIds?.[0]) recordCollectionOccurrence(name, sessionIds[0]);+ },+ },+});diff --git a/e2e/fixtures/agent-schedules/agent/tools/record-email.ts b/e2e/fixtures/agent-schedules/agent/tools/record-email.tsnew file mode 100644index 000000000..156d9562e--- /dev/null+++ b/e2e/fixtures/agent-schedules/agent/tools/record-email.ts@@ -0,0 +1,16 @@+import { defineTool } from "eve/tools";+import { z } from "zod";++export default defineTool({+ description: "Send a deterministic fixture email to the authenticated recipient.",+ inputSchema: z+ .object({ to: z.string().email(), subject: z.string().min(1), body: z.string().min(1) })+ .strict(),+ // Only the unattended scheduled run sends the fixture email; anything else needs approval.+ approval: ({ session }) => (session.schedule !== undefined ? "not-applicable" : "user-approval"),+ async execute(input, context) {+ if (input.to !== context.session.auth.current?.attributes["fixture.email"])+ throw new Error("Fixture email recipient is not authorized.");+ return { accepted: true, recipient: input.to, token: "schedule-email-mock-accepted" };+ },+});diff --git a/e2e/fixtures/agent-schedules/agent/tools/share-schedule.ts b/e2e/fixtures/agent-schedules/agent/tools/share-schedule.tsnew file mode 100644index 000000000..7620639bd--- /dev/null+++ b/e2e/fixtures/agent-schedules/agent/tools/share-schedule.ts@@ -0,0 +1,28 @@+import { defineTool } from "eve/tools";+import { schedules } from "eve/experimental/schedules";+import { z } from "zod";+import personal from "../schedules/requests";+import { takeCollectionDelivery, takeCollectionOccurrence } from "../lib/collection-occurrences";++export default defineTool({+ description:+ "Inspect or invoke a schedule through the principal-authorized custom client, or read what its last occurrence delivered.",+ inputSchema: z+ .object({ operation: z.enum(["get", "invoke", "delivery"]), name: z.string().min(1) })+ .strict(),+ async execute({ operation, name }) {+ const client = await schedules(personal);+ const records = (await client.list({ limit: 100 })).data;+ const matches = records.filter((record) => record.name === name || record.displayName === name);+ if (matches.length !== 1)+ throw new Error("Select exactly one fixture schedule by its unique name.");+ const managementName = matches[0]!.name;+ if (operation === "delivery")+ return { content: takeCollectionDelivery(managementName) ?? null };+ if (operation === "get") return await client.get(managementName);+ await client.invoke(managementName);+ const sessionId = takeCollectionOccurrence(managementName);+ if (!sessionId) throw new Error("Local subscription invocation did not report dispatch.");+ return { accepted: true, sessionId };+ },+});diff --git a/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.ts b/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.tsnew file mode 100644index 000000000..4423b272b--- /dev/null+++ b/e2e/fixtures/agent-schedules/evals/schedule-collection.eval.ts@@ -0,0 +1,62 @@+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.",+ async test(t) {+ if (!t.target.capabilities.devRoutes)+ t.skip("Schedule collection invocation requires dev routes.");+ const turn = await t.send(+ "Alice is setting up a one-time email reminder for January 1, 2030. Create the schedule named collection-email with a payload task to send a fixture email with subject Scheduled note and body A fixture note., and destination creator-email. Confirm the schedule is saved and do not send the email now.",+ );+ turn.expectOk();+ const approval = turn.session.requireInputRequest({+ display: "confirmation",+ toolName: "schedule__requests__create",+ });+ const created = await turn.session.respond([+ { optionId: "approve", requestId: approval.requestId },+ ]);+ created.expectOk();+ created.session.calledTool("schedule__requests__create", {+ input: {+ name: "collection-email",+ payload: { destination: "creator-email" },+ },+ status: "completed",+ });+ const client = await t.send(+ "Inspect the schedule named collection-email with share-schedule using its get operation.",+ );+ client.succeeded();+ client.calledTool("share-schedule", { input: { operation: "get", name: "collection-email" } });+ const manual = await t.send("Use share-schedule to invoke collection-email once.");+ manual.succeeded();+ manual.calledTool("share-schedule", {+ input: { operation: "invoke", name: "collection-email" },+ });+ const output = manual.toolCalls.find(+ (call) => call.name === "share-schedule" && call.input.operation === "invoke",+ )?.output;+ const sessionId =+ output && typeof output === "object" && !Array.isArray(output)+ ? Reflect.get(output, "sessionId")+ : undefined;+ if (typeof sessionId !== "string")+ throw new Error("Dynamic invocation did not return its dispatched session.");+ const scheduled = await t.target.attachSession(sessionId);+ scheduled.succeeded();+ scheduled.calledTool("record-email", {+ input: { to: "[email protected]", subject: "Scheduled note" },+ output: { recipient: "[email protected]", accepted: true },+ });+ // 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();+ const content = delivered.toolCalls.find(+ (call) => call.name === "share-schedule" && call.input.operation === "delivery",+ )?.output;+ if (typeof Reflect.get(Object(content), "content") !== "string")+ throw new Error("The outbox channel did not record the occurrence's result.");+ t.succeeded();+ },+});diff --git a/packages/eve/src/channel/schedule.test.ts b/packages/eve/src/channel/schedule.test.tsindex a5f114b76..596498f43 100644--- a/packages/eve/src/channel/schedule.test.ts+++ b/packages/eve/src/channel/schedule.test.ts@@ -1,7 +1,9 @@import { describe, expect, it, vi } from "vitest";--import { CHANNEL_SENTINEL, type CompiledChannel } from "#channel/compiled-channel.js";-import { isCompiledChannel } from "#channel/compiled-channel.js";+import {+ CHANNEL_SENTINEL,+ type CompiledChannel,+ isCompiledChannel,+} from "#channel/compiled-channel.js";import type { MessageStreamEvent } from "#protocol/message.js";import {SCHEDULE_ADAPTER,@@ -15,14 +17,15 @@ import type { RunHandle, Runtime, SessionAuthContext } from "#channel/types.js";import { slackChannel } from "#public/channels/slack/slackChannel.js";import { isScheduleAuth } from "#public/schedules/index.js";import type { ResolvedChannelDefinition } from "#runtime/types.js";+import { defineDynamicSchedules } from "#public/schedules/subscription.js";+import { inMemoryScheduleProvider } from "#public/schedules/providers/in-memory.js";+import { createScheduleCollectionPayload } from "#runtime/schedules/payload.js";+import { z } from "#compiled/zod/index.js";+import { defineChannel } from "#public/definitions/channel.js";function createMockRunHandle(): RunHandle {- return {- events: new ReadableStream<MessageStreamEvent>(),- sessionId: "mock-session-id",- };+ return { events: new ReadableStream<MessageStreamEvent>(), sessionId: "mock-session-id" };}-function createMockRuntime(): Runtime {return {createSession: vi.fn().mockResolvedValue(createMockRunHandle()),@@ -30,18 +33,19 @@ function createMockRuntime(): Runtime {dispatchSession: vi.fn(),getEventStream: vi.fn().mockResolvedValue(new ReadableStream<MessageStreamEvent>()),getStreamTailIndex: vi.fn().mockResolvedValue(-1),- resolveContinuation: vi.fn(),+ resolveContinuation: vi+ .fn()+ .mockResolvedValueOnce(undefined)+ .mockResolvedValue({ sessionId: "mock-session-id" }),};}-function makeSlackChannelEntry(): {definition: CompiledChannel;resolved: ResolvedChannelDefinition;} {const channel = slackChannel();- if (!isCompiledChannel(channel)) {+ if (!isCompiledChannel(channel))throw new Error("expected a compiled slack channel for this test");- }return {definition: channel,resolved: {@@ -60,16 +64,147 @@ function makeSlackChannelEntry(): {}describe("ScheduleDispatcher", () => {+ describe("collection form", () => {+ const creator = {+ attributes: {},+ authenticator: "test",+ principalId: "alice",+ principalType: "user",+ };+ const occurrence = {+ collection: "requests",+ executionId: "exec-1",+ name: "weekly",+ scheduleId: "sch-1",+ scheduledAt: "2026-09-29T19:00:00Z",+ };+ function setup(auth: typeof creator | null = creator) {+ const runtime = createMockRuntime();+ const targets: string[] = [];+ const channel = defineChannel<undefined, void, { channelId: string }>({+ routes: [],+ async receive(input, context) {+ targets.push(input.target.channelId);+ return await context+ .from(input.target.channelId)+ .send(input.message, { auth: input.auth });+ },+ });+ const run = vi.fn(+ async (args: {+ payload: { task: string; destination: string };+ to: import("#public/schedules/subscription.js").DynamicSchedulesToFn;+ auth: SessionAuthContext;+ waitUntil: (task: Promise<unknown>) => void;+ }) => {+ const channelId =+ args.payload.destination === "my-dm" ? `dm-${args.auth.principalId}` : "team-channel";+ args.waitUntil(args.to(channel, { channelId }).send(args.payload.task));+ },+ );+ const definition = defineDynamicSchedules({+ provider: inMemoryScheduleProvider(),+ inputSchema: z.object({ task: z.string(), destination: z.enum(["my-dm", "team-channel"]) }),+ auth: () => auth,+ run,+ });+ const payload = createScheduleCollectionPayload({+ application: "fixture",+ collection: "requests",+ envelope: {+ version: 3,+ payload: { task: "Summarize the week.", destination: "my-dm" },+ scope: "alice",+ principal: { type: "user", authenticator: "test", principalId: "alice" },+ },+ });+ const dispatcher = new ScheduleDispatcher({+ runtime,+ channels: [+ {+ name: "outbox",+ method: "POST",+ urlPath: "/outbox",+ logicalPath: "channels/outbox.ts",+ sourceId: "outbox",+ sourceKind: "module",+ definition: channel as CompiledChannel,+ adapter: (channel as CompiledChannel).adapter,+ receive: channel.receive as CompiledChannel["receive"],+ fetch: async () => new Response(),+ },+ ],+ });+ const input = {+ collectionId: "requests",+ definition,+ occurrence,+ payload,+ };+ return { dispatcher, input, runtime, run, targets };+ }++ it("derives a destination from payload intent and starts fresh unattended work as the creator", async () => {+ const { dispatcher, input, runtime, run, targets } = setup();+ const first = await dispatcher.triggerCollection(input);+ const second = await dispatcher.triggerCollection(input);+ expect(targets).toEqual(["dm-alice", "dm-alice"]);+ expect(run).toHaveBeenCalledTimes(2);+ expect(first.sessions).toHaveLength(1);+ expect(second.sessions).toHaveLength(1);+ const started = vi.mocked(runtime.createSession).mock.calls.map(([value]) => value);+ expect(started[0]).toMatchObject({+ auth: { principalId: "alice", attributes: {} },+ capabilities: { requestInput: false },+ input: { message: "Summarize the week." },+ });+ expect(started[0]!.continuationToken).not.toBe(started[1]!.continuationToken);+ expect(started[0]!.continuationConflictCommand).toBeUndefined();+ expect(runtime.dispatchContinuation).not.toHaveBeenCalled();+ });++ it.each([null, { ...creator, principalId: "bob" }])(+ "does not invoke authored code when creator resolution fails (%j)",+ async (auth) => {+ const { dispatcher, input, runtime, run } = setup(auth);+ await expect(dispatcher.triggerCollection(input)).rejects.toThrow(+ /no longer authorized|must resolve the schedule creator/,+ );+ expect(run).not.toHaveBeenCalled();+ expect(runtime.createSession).not.toHaveBeenCalled();+ },+ );++ it("validates provider-delivered payloads before auth or callback side effects", async () => {+ const { dispatcher, input, run } = setup();+ input.payload.envelope.payload.destination = "unknown";+ await expect(dispatcher.triggerCollection(input)).rejects.toThrow("Invalid schedule payload");+ expect(run).not.toHaveBeenCalled();+ });++ it("propagates registered background work failure", async () => {+ const { dispatcher, input } = setup();+ const definition = {+ ...input.definition,+ run: ({+ waitUntil,+ }: import("#public/schedules/subscription.js").DynamicSchedulesRunArgs<unknown>) =>+ waitUntil(Promise.reject(new Error("send failed"))),+ };+ await expect(dispatcher.triggerCollection({ ...input, definition })).rejects.toThrow(+ "send failed",+ );+ });+ });+describe("markdown form", () => {it("starts a Session via runtime.createSession with the SCHEDULE_ADAPTER", async () => {const runtime = createMockRuntime();const dispatcher = new ScheduleDispatcher({ runtime, channels: [] });-const result = await dispatcher.trigger({scheduleId: "heartbeat",markdown: "Run heartbeat task.",});-expect(runtime.createSession).toHaveBeenCalledWith(expect.objectContaining({adapter: SCHEDULE_ADAPTER,@@ -82,89 +217,74 @@ describe("ScheduleDispatcher", () => {expect(result.sessions[0]!.id).toBe("mock-session-id");expect(result.waitUntilTasks).toHaveLength(0);});-it("propagates runtime.createSession failures", async () => {const runtime = createMockRuntime();runtime.createSession = vi.fn().mockRejectedValue(new Error("boom"));- const dispatcher = new ScheduleDispatcher({ runtime, channels: [] });-- await expect(dispatcher.trigger({ scheduleId: "heartbeat", markdown: "x" })).rejects.toThrow(- "boom",- );+ await expect(+ new ScheduleDispatcher({ runtime, channels: [] }).trigger({+ scheduleId: "heartbeat",+ markdown: "x",+ }),+ ).rejects.toThrow("boom");});});describe("run handler form", () => {- it("invokes the author's run() with { to, waitUntil, appAuth }", async () => {+ it("invokes run() with { to, waitUntil, appAuth }", async () => {const runtime = createMockRuntime();vi.stubEnv("SLACK_BOT_TOKEN", "xoxb-test");vi.stubEnv("SLACK_SIGNING_SECRET", "test-secret");try {const { definition, resolved } = makeSlackChannelEntry();const dispatcher = new ScheduleDispatcher({ runtime, channels: [resolved] });-- const observed: { hasAppAuth: boolean; hasWaitUntil: boolean } = {- hasAppAuth: false,- hasWaitUntil: false,- };-+ let observed = false;const result = await dispatcher.trigger({scheduleId: "daily-digest",async run({ to, waitUntil, appAuth }) {- observed.hasAppAuth = appAuth.principalId === "eve:app";- observed.hasWaitUntil = typeof waitUntil === "function";+ observed = appAuth.principalId === "eve:app" && typeof waitUntil === "function";await to(definition, { channelId: "C0123ABC" }).send("post the digest", {auth: appAuth,});},});-- expect(observed.hasAppAuth).toBe(true);- expect(observed.hasWaitUntil).toBe(true);+ expect(observed).toBe(true);expect(result.sessions).toHaveLength(1);expect(runtime.createSession).toHaveBeenCalledTimes(1);-- const runInput = vi.mocked(runtime.createSession).mock.calls[0]![0];- expect(runInput.continuationToken).toMatch(+ const run = vi.mocked(runtime.createSession).mock.calls[0]![0];+ expect(run.auth).toEqual(SCHEDULE_APP_AUTH);+ // A threadless destination needs a fresh conversation per dispatch.+ expect(run.continuationToken).toMatch(/^slack:C0123ABC:[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/,);- expect(runInput.auth).toEqual(SCHEDULE_APP_AUTH);} finally {vi.unstubAllEnvs();}});-- it("collects waitUntil promises so the caller can await them", async () => {+ it("collects waitUntil promises", async () => {const runtime = createMockRuntime();- const dispatcher = new ScheduleDispatcher({ runtime, channels: [] });-- const result = await dispatcher.trigger({+ const result = await new ScheduleDispatcher({ runtime, channels: [] }).trigger({scheduleId: "background-job",async run({ waitUntil }) {waitUntil(Promise.resolve("done"));waitUntil(Promise.resolve(42));},});-expect(result.waitUntilTasks).toHaveLength(2);await expect(Promise.all(result.waitUntilTasks)).resolves.toEqual(["done", 42]);expect(result.sessions).toHaveLength(0);});-it("scopes user-auth sessions started through waitUntil to the active schedule", async () => {const runtime = createMockRuntime();- const observedScheduleIds: Array<string | undefined> = [];+ const observed: Array<string | undefined> = [];runtime.createSession = vi.fn(async () => {- observedScheduleIds.push(contextStorage.getStore()?.get(ScheduleIdKey));+ observed.push(contextStorage.getStore()?.get(ScheduleIdKey));return createMockRunHandle();});vi.stubEnv("SLACK_BOT_TOKEN", "xoxb-test");vi.stubEnv("SLACK_SIGNING_SECRET", "test-secret");try {const { definition, resolved } = makeSlackChannelEntry();- const dispatcher = new ScheduleDispatcher({ runtime, channels: [resolved] });-- const result = await dispatcher.trigger({+ const result = await new ScheduleDispatcher({ runtime, channels: [resolved] }).trigger({scheduleId: "dynamic-tasks",run({ to, waitUntil }) {waitUntil(@@ -173,7 +293,7 @@ describe("ScheduleDispatcher", () => {auth: {attributes: {},authenticator: "slack",- principalId: "schedule-owner",+ principalId: "owner",principalType: "user",},}),@@ -181,26 +301,21 @@ describe("ScheduleDispatcher", () => {);},});-await Promise.all(result.waitUntilTasks);- expect(observedScheduleIds).toEqual(["dynamic-tasks"]);+ expect(observed).toEqual(["dynamic-tasks"]);expect(contextStorage.getStore()?.get(ScheduleIdKey)).toBeUndefined();} finally {vi.unstubAllEnvs();}});-- it("throws when ctx.to(channel) is called with an unregistered channel", async () => {- const runtime = createMockRuntime();- const dispatcher = new ScheduleDispatcher({ runtime, channels: [] });+ it("rejects unregistered channels", async () => {const stranger = {__kind: CHANNEL_SENTINEL,routes: [],adapter: { kind: "x" },} satisfies CompiledChannel;-await expect(- dispatcher.trigger({+ new ScheduleDispatcher({ runtime: createMockRuntime(), channels: [] }).trigger({scheduleId: "stranger",async run({ to }) {await to(stranger, {}).send("x", { auth: null });@@ -237,11 +352,10 @@ describe("ScheduleDispatcher", () => {});it("throws when neither run nor markdown is provided", async () => {- const runtime = createMockRuntime();- const dispatcher = new ScheduleDispatcher({ runtime, channels: [] });-- await expect(dispatcher.trigger({ scheduleId: "empty" })).rejects.toThrow(- /has neither "run" nor "markdown"/,- );+ await expect(+ new ScheduleDispatcher({ runtime: createMockRuntime(), channels: [] }).trigger({+ scheduleId: "empty",+ }),+ ).rejects.toThrow(/has neither "run" nor "markdown"/);});});diff --git a/packages/eve/src/compiler/normalize-manifest.test.ts b/packages/eve/src/compiler/normalize-manifest.test.tsindex 05b61f270..6b75f17b7 100644--- a/packages/eve/src/compiler/normalize-manifest.test.ts+++ b/packages/eve/src/compiler/normalize-manifest.test.ts@@ -22,6 +22,9 @@ import { defineMcpClientConnection } from "#public/definitions/connections/mcp.jimport { defineHook } from "#public/definitions/hook.js";import { defineInstructions } from "#public/definitions/instructions.js";import { defineSchedule } from "#public/definitions/schedule.js";+import { defineDynamicSchedules } from "#public/schedules/subscription.js";+import { inMemoryScheduleProvider } from "#public/schedules/providers/in-memory.js";+import { z } from "#compiled/zod/index.js";import { defineSkill } from "#public/definitions/skill.js";import { resolveAgent } from "#runtime/resolve-agent.js";import { resolveRuntimeAgentGraph } from "#runtime/resolve-agent-graph.js";@@ -400,6 +403,76 @@ describe("compileAgentManifest source graph", () => {expect(order).toContain("tool");});+ it("materializes schedule factories once per compile", async () => {+ const staticFactory = vi.fn(() =>+ defineSchedule({ cron: "0 9 * * *", markdown: "Run the report." }),+ );+ const sourceRegistry = registry([+ {+ logicalPath: "agent.ts",+ loadNamespace: async () => ({ default: defineAgent({ model: "openai/gpt-5.4" }) }),+ },+ {+ logicalPath: "schedules/daily.ts",+ loadNamespace: async () => ({ default: staticFactory }),+ },+ ]);++ const compiled = await compileAgentManifest(manifest(), { sourceRegistries: [sourceRegistry] });+ expect(compiled.schedules).toHaveLength(1);+ expect(staticFactory).toHaveBeenCalledOnce();+ });++ it("keeps nested collection identity but flattens generated management tool names", async () => {+ const sourceRegistry = registry([+ {+ logicalPath: "schedules/billing/requests.ts",+ loadNamespace: async () => ({+ default: defineDynamicSchedules({+ provider: inMemoryScheduleProvider(),+ inputSchema: z.object({ task: z.string() }),+ auth: () => null,+ run: async () => {},+ }),+ }),+ },+ ]);+ const compiled = await compileAgentManifest(manifest(), { sourceRegistries: [sourceRegistry] });+ expect(compiled.scheduleCollections).toContainEqual(+ expect.objectContaining({ name: "billing/requests" }),+ );+ const wrapper = compiled.dynamicTools.find(+ (tool) => tool.logicalPath === "tools/schedule__billing-requests.ts",+ )!;+ expect(wrapper).toMatchObject({ slug: "schedule__billing-requests" });+ const moduleMap = await createProgrammaticCompiledModuleMap(compiled, [+ frameworkAgentSourceRegistry,+ sourceRegistry,+ ]);+ const namespace = moduleMap.nodes.__root__!.modules[wrapper.sourceId]!;+ const tools = await (namespace.default as ReturnType<typeof defineDynamic>).events[+ "turn.started"+ ]!(undefined, {+ model: null,+ session: { id: "session", auth: { current: null, initiator: null } },+ channel: {},+ messages: [],+ });+ expect(Object.keys(tools as object).sort()).toEqual(+ ["create", "delete", "disable", "enable", "get", "invoke", "list"].map(+ (operation) => `schedule__billing-requests__${operation}`,+ ),+ );+ const entries = tools as Record<string, import("#tools/dynamic.js").DynamicToolEntry>;+ expect(+ entries["schedule__billing-requests__create"]!.label?.start({ name: "daily-digest" }),+ ).toBe("Create schedule: daily-digest");+ expect(entries["schedule__billing-requests__list"]!.label?.start({})).toBe("List schedules");+ expect(+ entries["schedule__billing-requests__delete"]!.label?.start({ name: "daily-digest" }),+ ).toBe("Delete schedule: daily-digest");+ });+it("classifies compile and runtime usage from normalized authored semantics", async () => {const sourceRegistry = registry([{diff --git a/packages/eve/src/context/accessors.integration.test.ts b/packages/eve/src/context/accessors.integration.test.tsindex 19bbaf6fa..a3371fd02 100644--- a/packages/eve/src/context/accessors.integration.test.ts+++ b/packages/eve/src/context/accessors.integration.test.ts@@ -1,5 +1,7 @@import { describe, expect, it } from "vitest";+import { loadContext } from "#context/container.js";+import { AuthKey, ScheduleIdKey, ScheduleInstanceKey, OccurrenceIdKey } from "#context/keys.js";import { buildCallbackContext } from "#context/build-callback-context.js";import { createTestRuntime } from "#internal/testing/app-harness.js";import { mockSandbox } from "#internal/testing/mocks/mock-sandbox.js";@@ -48,6 +50,29 @@ describe("buildCallbackContext – session", () => {});});+ it("exposes framework schedule provenance without trusting auth attributes", async () => {+ const runtime = await createTestRuntime();+ await runtime.runAsSession(undefined, async () => {+ const ctx = loadContext();+ ctx.set(AuthKey, {+ attributes: { "eve.scheduled_run": "true" },+ authenticator: "fixture",+ principalId: "alice",+ principalType: "user",+ });+ expect(buildCallbackContext().session.schedule).toBeUndefined();+ ctx.set(ScheduleIdKey, "tasks");+ expect(buildCallbackContext().session.schedule).toMatchObject({ definition: "tasks" });+ ctx.set(ScheduleInstanceKey, "daily-report--id");+ ctx.set(OccurrenceIdKey, "exec-1");+ expect(buildCallbackContext().session.schedule).toEqual({+ definition: "tasks",+ instance: "daily-report--id",+ occurrenceId: "exec-1",+ });+ });+ });+it("preserves parent lineage on the public session", async () => {const runtime = await createTestRuntime();diff --git a/packages/eve/src/execution/session/turn-step.test.ts b/packages/eve/src/execution/session/turn-step.test.tsindex f0429d18c..f7eb41909 100644--- a/packages/eve/src/execution/session/turn-step.test.ts+++ b/packages/eve/src/execution/session/turn-step.test.ts@@ -11,6 +11,7 @@ import { ContextContainer, loadContext } from "#context/container.js";import { ContextKey } from "#context/key.js";import {AuthKey,+ ScheduleIdKey,ChannelInstrumentationKey,ContinuationHookTokensKey,ContinuationTokenKey,@@ -1536,12 +1537,15 @@ describe("turnStep", () => {ctx.set(BundleKey, bundle);ctx.set(ChannelKey, threadContextAdapter);ctx.set(ContinuationTokenKey, "http:auth-replacement");+ ctx.set(ScheduleIdKey, "previous-scheduled-turn");ctx.set(SessionIdKey, "session-1");+ let observedSchedule: string | undefined;let observed: SessionAuthContext | null | undefined;vi.mocked(createExecutionNodeStep).mockImplementation(() => {return async (session): Promise<StepResult> => {observed = loadContext().get(AuthKey);+ observedSchedule = loadContext().get(ScheduleIdKey);return { next: null, session };};});@@ -1554,6 +1558,7 @@ describe("turnStep", () => {});expect(observed).toEqual(expected);+ expect(observedSchedule).toBeUndefined();});it("keeps a session-scoped dynamic model selection when the first turn is cancelled", async () => {diff --git a/packages/eve/src/internal/authored-definition/schedule-collection.test.ts b/packages/eve/src/internal/authored-definition/schedule-collection.test.tsnew file mode 100644index 000000000..ddb0d31c3--- /dev/null+++ b/packages/eve/src/internal/authored-definition/schedule-collection.test.ts@@ -0,0 +1,72 @@+import { describe, expect, it, vi } from "vitest";+import { z } from "#compiled/zod/index.js";+import { normalizeScheduleCollectionDefinition } from "#internal/authored-definition/schedule-collection.js";+import { defineDynamicSchedules } from "#public/schedules/subscription.js";+import { inMemoryScheduleProvider } from "#public/schedules/providers/in-memory.js";++const valid = {+ provider: inMemoryScheduleProvider(),+ inputSchema: z.object({ task: z.string() }),+ auth: () => null,+ run: async () => {},+};++describe("normalizeScheduleCollectionDefinition", () => {+ it.each([+ ["no auth", { auth: undefined }, '"auth" is required'],+ ["no input schema", { inputSchema: undefined }, '"inputSchema" is required'],+ ["a malformed schema", { inputSchema: { "~standard": {} } }, "validate function"],+ ["no run", { run: undefined }, '"run" is required'],+ [+ "a malformed preparation hook",+ { preparePayload: true },+ '"preparePayload" must be a function',+ ],+ [+ "approval for the removed update tool",+ { approval: { update: () => "user-approval" } },+ "update",+ ],+ ["unsupported deferral", { tool: "deferred" }, "deferred tools are not supported"],+ ["the removed capture hook", { resolvePayload: () => ({}) }, "resolvePayload"],+ ["the removed deliveries", { deliveries: {} }, "deliveries"],+ ])("rejects a subscription with %s", (_label, override, message) => {+ const definition = defineDynamicSchedules({ ...valid, ...override } as never);+ expect(() => normalizeScheduleCollectionDefinition(definition, "Invalid.")).toThrow(message);+ });++ it("rejects model approval configuration on code-only definitions at type and runtime boundaries", () => {+ const invalid = {+ ...valid,+ tool: false as const,+ approval: { create: () => "user-approval" as const },+ };+ // @ts-expect-error Code-only scheduling cannot configure model-call approval.+ const definition = defineDynamicSchedules(invalid);+ expect(() => normalizeScheduleCollectionDefinition(definition, "Invalid.")).toThrow(+ "cannot be configured",+ );+ });++ it("defaults to Vercel and preserves an explicit provider override", () => {+ vi.stubEnv("EVE_DEV", "");+ try {+ const { provider, ...options } = valid;+ const implicit = defineDynamicSchedules({ ...options });+ expect(normalizeScheduleCollectionDefinition(implicit, "Invalid.").provider.kind).toBe(+ "vercel",+ );+ expect(defineDynamicSchedules({ ...options, provider }).provider).toBe(provider);+ vi.stubEnv("EVE_DEV", "1");+ expect(defineDynamicSchedules({ ...options }).provider.kind).toBe("in-memory");+ } finally {+ vi.unstubAllEnvs();+ }+ });++ it("accepts a schema and callback without configured deliveries", () => {+ expect(() =>+ normalizeScheduleCollectionDefinition(defineDynamicSchedules(valid), "Invalid."),+ ).not.toThrow();+ });+});diff --git a/packages/eve/src/internal/nitro/host/vercel-build-output-config.test.ts b/packages/eve/src/internal/nitro/host/vercel-build-output-config.test.tsindex d78ea57a1..226484f37 100644--- a/packages/eve/src/internal/nitro/host/vercel-build-output-config.test.ts+++ b/packages/eve/src/internal/nitro/host/vercel-build-output-config.test.ts@@ -7,6 +7,8 @@ import {EVE_WORKFLOW_FLOW_ROUTE_PATH,} from "#internal/nitro/host/vercel-build-output-config.js";import { deriveEveWorkflowQueueTopic } from "#internal/workflow/queue-namespace.js";+import { EVE_SCHEDULE_COLLECTION_CONSUMER_ROUTE_PATH } from "#internal/schedules/consumer-route.js";+import { deriveEveScheduleQueueTopic } from "#runtime/schedules/queue-namespace.js";describe("createEveVercelOptions", () => {it("returns undefined when the Vercel build output is disabled", () => {@@ -45,6 +47,35 @@ describe("createEveVercelOptions", () => {});});+ it("conditionally declares the private schedule collection consumer", () => {+ const withoutCollections = createEveVercelOptions({+ agentName: "test-agent",+ enabled: true,+ });+ const withCollections = createEveVercelOptions({+ agentName: "test-agent",+ enabled: true,+ hasVercelScheduleCollections: true,+ });++ expect(withoutCollections?.functionRules).not.toHaveProperty(+ EVE_SCHEDULE_COLLECTION_CONSUMER_ROUTE_PATH,+ );+ expect(withCollections?.functionRules[EVE_SCHEDULE_COLLECTION_CONSUMER_ROUTE_PATH]).toEqual({+ maxDuration: "max",+ experimentalTriggers: [+ {+ type: "queue/v2beta",+ topic: deriveEveScheduleQueueTopic("test-agent"),+ retryAfterSeconds: 5,+ initialDelaySeconds: 0,+ maxDeliveries: 10,+ },+ ],+ environment: { WORKFLOW_PRECONDITION_GUARD: "1" },+ });+ });+it("omits the public route prefix from the flow environment when none is set", () => {for (const publicRoutePrefix of [undefined, ""]) {const options = createEveVercelOptions({diff --git a/packages/eve/src/internal/nitro/routes/schedule-collection-consumer.test.ts b/packages/eve/src/internal/nitro/routes/schedule-collection-consumer.test.tsnew file mode 100644index 000000000..1c4df1961--- /dev/null+++ b/packages/eve/src/internal/nitro/routes/schedule-collection-consumer.test.ts@@ -0,0 +1,124 @@+import { describe, expect, it, vi } from "vitest";++import type { ScheduleOccurrenceEvent } from "#public/schedules/subscription.js";+import { createScheduleCollectionPayload } from "#runtime/schedules/payload.js";+import { deriveEveScheduleQueueTopic } from "#runtime/schedules/queue-namespace.js";++type Callback = (message: unknown, metadata: Record<string, unknown>) => Promise<void>;+type RetryHandler = (error: unknown) => unknown;++const mocks = vi.hoisted(() => ({+ admit: vi.fn(),+ failed: vi.fn(),+ queue: {} as { callback?: Callback; retry?: RetryHandler },+}));+vi.mock("@vercel/queue", () => ({+ handleCallback: (callback: Callback, options: { retry: RetryHandler }) => {+ mocks.queue.callback = callback;+ mocks.queue.retry = options.retry;+ return async () => new Response(null);+ },+}));+vi.mock("#internal/nitro/routes/runtime-artifacts.js", () => ({+ resolveNitroCompiledArtifactsSource: () => ({}),+}));+vi.mock("#runtime/schedules/dispatch-occurrence.js", () => ({+ dispatchScheduledOccurrence: mocks.admit,+}));+vi.mock("#runtime/sessions/compiled-agent-cache.js", () => ({+ getCompiledRuntimeAgentBundle: async () => ({}),+}));+vi.mock("#runtime/loaders/manifest.js", () => ({+ loadCompiledManifest: async () => ({ config: { name: "fixture" } }),+}));+vi.mock("#runtime/schedules/load-collection.js", () => ({+ ScheduleCollectionNotDeployedError: class extends Error {},+ loadScheduleCollectionDefinition: async () => ({+ provider: {},+ events: { "occurrence.failed": mocks.failed },+ }),+}));++const { handleScheduleCollectionConsumer } =+ await import("#internal/nitro/routes/schedule-collection-consumer.js");+const { EVE_SCHEDULE_CONSUMER_MAX_DELIVERIES } =+ await import("#internal/schedules/consumer-route.js");+const { PermanentScheduleMessageError } = await import("#internal/schedules/verify-delivery.js");++const payload = createScheduleCollectionPayload({+ application: "fixture",+ collection: "requests",+ envelope: {+ version: 3,+ payload: { task: "Summarize the week.", destination: "my-dm" },+ scope: "alice",+ principal: { type: "user", authenticator: "test", principalId: "alice" },+ },+});++async function deliver(deliveryCount: number) {+ await handleScheduleCollectionConsumer({} as never, new Request("https://agent.test"));+ const message = {+ source: "dynamic",+ scheduleId: "sch-1",+ name: "weekly",+ namespace: "eve-namespace",+ executionId: "exec-1",+ scheduledAt: "2026-09-29T19:00:00Z",+ payload: { eve: { application: "fixture", collection: "requests", version: 1 }, payload },+ };+ const metadata = {+ createdAt: new Date(),+ deliveryCount,+ messageId: "msg-1",+ topicName: deriveEveScheduleQueueTopic("fixture"),+ };+ return await mocks.queue.callback!(message, metadata).then(+ () => undefined,+ (error: unknown) => error,+ );+}++describe("schedule collection consumer", () => {+ it.each([+ [+ "a transient error before the final delivery",+ new Error("Schedules API 503"),+ 1,+ false,+ undefined,+ ],+ [+ "a transient error on the final delivery",+ new Error("Schedules API 503"),+ EVE_SCHEDULE_CONSUMER_MAX_DELIVERIES,+ true,+ undefined,+ ],+ [+ "a permanent error",+ new PermanentScheduleMessageError("Schedule delivery does not match this agent."),+ 1,+ true,+ { acknowledge: true },+ ],+ ])(+ "reports %s as failed only when the queue stops retrying",+ async (_label, error, count, reported, directive) => {+ mocks.failed.mockClear();+ mocks.admit.mockRejectedValueOnce(error);++ const thrown = await deliver(count);++ expect(thrown).toBe(error);+ expect(mocks.queue.retry!(thrown)).toEqual(directive);+ expect(mocks.failed).toHaveBeenCalledTimes(reported ? 1 : 0);+ if (reported)+ expect(mocks.failed.mock.calls[0]![0] as ScheduleOccurrenceEvent).toMatchObject({+ executionId: "exec-1",+ reason: error.message,+ type: "occurrence.failed",+ });+ },+ );+});diff --git a/packages/eve/src/internal/schedules/verify-delivery.test.ts b/packages/eve/src/internal/schedules/verify-delivery.test.tsnew file mode 100644index 000000000..22da44d0d--- /dev/null+++ b/packages/eve/src/internal/schedules/verify-delivery.test.ts@@ -0,0 +1,74 @@+import { describe, expect, it } from "vitest";+import type { Schedule as VercelSchedule } from "@vercel/schedules";++import {+ expectScheduleQueueMessage,+ PermanentScheduleMessageError,+ verifyScheduleDelivery,+} from "#internal/schedules/verify-delivery.js";+import { deriveEveScheduleQueueTopic } from "#runtime/schedules/queue-namespace.js";++const application = "test-agent";+const topic = deriveEveScheduleQueueTopic(application);+const message = {+ executionId: "execution_1",+ name: "reminder",+ namespace: "eve-scope-a",+ payload: {+ eve: { application, collection: "reminders", version: 1 },+ payload: "Review open incidents",+ },+ scheduleId: "sch_1",+ scheduledAt: "2026-09-23T12:00:00.000Z",+ source: "dynamic",+};+const schedule = {+ name: message.name,+ namespace: message.namespace,+ scheduleId: message.scheduleId,+ source: "dynamic",+ target: { type: "queue", topic },+} as VercelSchedule;++describe("schedule queue delivery", () => {+ it("accepts matching occurrences with optional identity/timing and extra transport metadata", () => {+ const { executionId, scheduledAt, ...withoutOptionalFields } = message;+ for (const body of [message, { ...withoutOptionalFields, transportAttempt: 1 }]) {+ const parsed = expectScheduleQueueMessage(body);+ expect(parsed.payload.payload).toBe("Review open incidents");+ expect(() => verifyScheduleDelivery(parsed, schedule, application, topic)).not.toThrow();+ }+ });++ it.each([+ null,+ undefined,+ { ...message, executionId: 1n },+ { ...message, source: "static" },+ { ...message, name: "" },+ { ...message, executionId: 42 },+ { ...message, scheduledAt: { invalid: true } },+ { ...message, payload: null },+ { ...message, payload: { ...message.payload, eve: { ...message.payload.eve, version: 2 } } },+ { ...message, payload: { ...message.payload, payload: "x".repeat(256 * 1024) } },+ ])("rejects invalid or oversized queue bodies", (body) => {+ expect(() => expectScheduleQueueMessage(body)).toThrow(PermanentScheduleMessageError);+ });++ it.each([+ [{ ...schedule, scheduleId: "sch_other" }, topic],+ [{ ...schedule, namespace: "eve-other" }, topic],+ [{ ...schedule, target: { type: "queue", topic: "other" } }, topic],+ [schedule, deriveEveScheduleQueueTopic("another-agent")],+ [null, topic],+ ] as const)("rejects a different resource or topic", (record, receivedTopic) => {+ expect(() =>+ verifyScheduleDelivery(+ expectScheduleQueueMessage(message),+ record,+ application,+ receivedTopic,+ ),+ ).toThrow(PermanentScheduleMessageError);+ });+});diff --git a/packages/eve/src/public/schedules/providers/in-memory.test.ts b/packages/eve/src/public/schedules/providers/in-memory.test.tsnew file mode 100644index 000000000..36fbf41cf--- /dev/null+++ b/packages/eve/src/public/schedules/providers/in-memory.test.ts@@ -0,0 +1,73 @@+import { describe, expect, it, vi } from "vitest";+import { inMemoryScheduleProvider } from "#public/schedules/providers/in-memory.js";+import type { ScheduleProviderContext } from "#runtime/schedules/provider-types.js";++const context = (+ operationId: string,+ target: ScheduleProviderContext["target"] = { key: "app" },+): ScheduleProviderContext => ({+ abortSignal: new AbortController().signal,+ collection: "tasks",+ namespace: "namespace",+ operationId,+ target,+});++describe("inMemoryScheduleProvider", () => {+ it("executes manual invocations with unique schedule and occurrence identities", async () => {+ const deliveries: unknown[] = [];+ let now = Date.parse("2026-09-28T12:00:00.000Z");+ const provider = inMemoryScheduleProvider({ now: () => new Date(now++) });+ const deliver = vi.fn(async (delivery: unknown) => {+ deliveries.push(delivery);+ });+ await provider.create(context("create", { key: "app", deliver }), {+ name: "daily",+ expression: { type: "cron", cron: "0 9 * * *" },+ payload: { request: "do work" },+ });+ await provider.invoke(context("invoke-1"), "daily");+ await provider.invoke(context("invoke-2"), "daily");+ expect(deliveries).toHaveLength(2);+ const [first, second] = deliveries as Array<{+ occurrence: { scheduleId: string; executionId: string };+ payload: unknown;+ }>;+ expect(first!.occurrence.scheduleId).not.toContain("daily");+ expect(first!.occurrence.scheduleId).toBe(second!.occurrence.scheduleId);+ expect(first!.occurrence.executionId).not.toBe(second!.occurrence.executionId);+ expect(first!.payload).toEqual({ request: "do work" });+ });++ it("redelivers the same occurrence when its operation is retried", async () => {+ const deliveries: unknown[] = [];+ const provider = inMemoryScheduleProvider();+ const deliver = async (delivery: unknown) => {+ deliveries.push(delivery);+ };+ await provider.create(context("create", { key: "app", deliver }), {+ name: "weekly",+ expression: { type: "cron", cron: "0 9 * * 1" },+ payload: { request: "do work" },+ });+ await provider.invoke(context("invoke-1"), "weekly");+ await provider.invoke(context("invoke-2", { key: "app", deliver }), "weekly");+ await provider.invoke(context("invoke-2", { key: "app", deliver }), "weekly");+ expect(deliveries).toHaveLength(3);+ expect(deliveries[1]).toEqual(deliveries[2]);+ });++ it("does not reuse a schedule ID after deleting and recreating the same name", async () => {+ const provider = inMemoryScheduleProvider();+ const createContext = context("create-1");+ const input = {+ name: "daily",+ expression: { type: "cron" as const, cron: "0 9 * * *" },+ payload: {},+ };+ const first = await provider.create(createContext, input);+ await provider.delete(context("delete"), "daily");+ const recreated = await provider.create(context("create-2"), input);+ expect(recreated.scheduleId).not.toBe(first.scheduleId);+ });+});diff --git a/packages/eve/src/public/schedules/providers/vercel.test.ts b/packages/eve/src/public/schedules/providers/vercel.test.tsindex 5c6136944..d0369e33c 100644--- a/packages/eve/src/public/schedules/providers/vercel.test.ts+++ b/packages/eve/src/public/schedules/providers/vercel.test.ts@@ -92,7 +92,6 @@ describe("vercelScheduleProvider", () => {).rejects.toThrow("was created active, but disabling it failed; it may remain active");expect(fetchImpl).toHaveBeenCalledTimes(2);});-it("omits a blank first-page cursor", async () => {vi.stubEnv("VERCEL", "1");vi.stubEnv("VERCEL_ENV", "production");diff --git a/packages/eve/src/runtime/schedules/collection-client.test.ts b/packages/eve/src/runtime/schedules/collection-client.test.tsnew file mode 100644index 000000000..2d3256937--- /dev/null+++ b/packages/eve/src/runtime/schedules/collection-client.test.ts@@ -0,0 +1,144 @@+import { describe, expect, it, vi } from "vitest";+import { z } from "#compiled/zod/index.js";+import { defineDynamicSchedules } from "#public/schedules/subscription.js";+import { inMemoryScheduleProvider } from "#public/schedules/providers/in-memory.js";+import {+ createScheduleCollectionClient,+ type ScheduleBoundCallContext,+} from "#runtime/schedules/collection-client.js";++const callContext: ScheduleBoundCallContext = {+ abortSignal: new AbortController().signal,+ application: "fixture",+ collection: "requests",+ channel: {},+ session: {+ id: "session-alice",+ auth: {+ current: {+ attributes: { secret: "not-persisted" },+ authenticator: "test",+ principalId: "alice",+ principalType: "user",+ },+ initiator: null,+ },+ },+};+const payload = { task: "Summarize the week.", destination: "my-dm" as const };+function setup() {+ const provider = inMemoryScheduleProvider();+ const create = vi.spyOn(provider, "create");+ const definition = defineDynamicSchedules({+ provider,+ inputSchema: z+ .object({ task: z.string().min(1), destination: z.enum(["my-dm", "team-channel"]) })+ .strict(),+ auth: () => null,+ run: async () => {},+ });+ const client = createScheduleCollectionClient(definition, callContext);+ return { provider, definition, client, create };+}+const input = { name: "report", expression: { type: "delay" as const, minutes: 5 }, payload };++describe("schedule subscription client", () => {+ it("allows repeated display names while returning distinct management names", async () => {+ const { client } = setup();+ const first = await client.create(input);+ const second = await client.create(input);+ expect(first.displayName).toBe("report");+ expect(second.displayName).toBe("report");+ expect(first.name).not.toBe(second.name);+ await client.delete(first.name);+ await expect(client.get(second.name)).resolves.toMatchObject({ displayName: "report" });+ const longest = await client.create({ ...input, name: "r".repeat(256) });+ expect(longest.name).toHaveLength(256);+ await expect(client.get(longest.name)).resolves.toMatchObject({ displayName: "r".repeat(218) });+ });+ it("stores validated destination intent and a separate credential-free creator reference", async () => {+ const { client, create } = setup();+ await client.create(input);+ expect(create.mock.calls[0]![1].payload).toEqual({+ eve: { application: "fixture", collection: "requests", version: 3 },+ envelope: {+ version: 3,+ payload,+ scope: '["user","test",null,"alice"]',+ principal: { type: "user", authenticator: "test", principalId: "alice" },+ },+ });+ });++ it("rejects invalid input before preparation, and rejected or oversized preparation before writes", async () => {+ const { definition, create } = setup();+ const prepare = vi.fn((value: { task: string; destination: "my-dm" | "team-channel" }) => {+ if (value.task === "reject") throw new Error("Destination unavailable.");+ return { message: "x".repeat(64 * 1024) };+ });+ const client = createScheduleCollectionClient(+ {+ inputSchema: definition.inputSchema,+ provider: definition.provider,+ preparePayload: prepare,+ auth: () => null,+ run: () => {},+ },+ callContext,+ );+ await expect(client.create({ ...input, payload: { ...payload, task: "" } })).rejects.toThrow(+ "Invalid schedule payload",+ );+ expect(prepare).not.toHaveBeenCalled();+ await expect(+ client.create({ ...input, payload: { ...payload, task: "reject" } }),+ ).rejects.toThrow("Destination unavailable");+ await expect(client.create(input)).rejects.toThrow("recreate the schedule");+ expect(create).not.toHaveBeenCalled();+ });++ it("isolates callers and checks policy separately for every management operation", async () => {+ const { definition, client, provider } = setup();+ const created = await client.create(input);+ const bob = createScheduleCollectionClient(definition, {+ ...callContext,+ session: {+ id: "bob",+ auth: {+ current: { ...callContext.session.auth.current!, principalId: "bob" },+ initiator: null,+ },+ },+ });+ await expect(bob.get(created.name)).resolves.toBeNull();+ await expect(bob.list()).resolves.toEqual({ cursor: null, data: [] });+ const scope = vi.fn(() => null);+ const prepare = vi.fn(+ (value: { task: string; destination: "my-dm" | "team-channel" }) => value,+ );+ const denied = createScheduleCollectionClient(+ { ...definition, scope, preparePayload: prepare },+ callContext,+ );+ const get = vi.spyOn(provider, "get");+ await expect(denied.get(created.name)).rejects.toThrow("not available");+ await expect(denied.delete(created.name)).rejects.toThrow("not available");+ await expect(denied.create(input)).rejects.toThrow("not available");+ expect(scope.mock.calls).toHaveLength(3);+ expect(prepare).not.toHaveBeenCalled();+ expect(get).not.toHaveBeenCalled();+ });++ 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");+ expect(create).not.toHaveBeenCalled();+ });+});diff --git a/packages/eve/src/runtime/schedules/payload.test.ts b/packages/eve/src/runtime/schedules/payload.test.tsnew file mode 100644index 000000000..4a4ae5b33--- /dev/null+++ b/packages/eve/src/runtime/schedules/payload.test.ts@@ -0,0 +1,49 @@+import { describe, expect, it } from "vitest";+import {+ createScheduleCollectionPayload,+ parseSchedulePayload,+} from "#runtime/schedules/payload.js";++const principal = { type: "user", authenticator: "test", principalId: "alice" };+const envelope = {+ payload: { task: "Summarize the week.", destination: "my-dm" },+ scope: "alice",+ principal,+ version: 3 as const,+};++describe("parseSchedulePayload", () => {+ it("rejects the old delivery envelope instead of guessing its callback payload", () => {+ expect(() =>+ parseSchedulePayload({+ eve: { application: "fixture", collection: "requests", version: 2 },+ envelope: {+ version: 2,+ request: "Summarize the week.",+ metadata: {},+ deliveries: { log: {} },+ scope: "alice",+ principal,+ },+ }),+ ).toThrow("recreate the schedule");+ });++ it("bounds the complete persisted envelope and rejects resource mismatch", () => {+ expect(() =>+ createScheduleCollectionPayload({+ application: "fixture",+ collection: "requests",+ envelope: { ...envelope, payload: "x".repeat(64 * 1024) },+ }),+ ).toThrow("recreate the schedule");+ const stored = createScheduleCollectionPayload({+ application: "fixture",+ collection: "requests",+ envelope,+ });+ expect(() =>+ parseSchedulePayload(stored, { application: "other", collection: "requests" }),+ ).toThrow("identity changed");+ });+});diff --git a/packages/eve/src/runtime/schedules/subscription.integration.test.ts b/packages/eve/src/runtime/schedules/subscription.integration.test.tsnew file mode 100644index 000000000..f0dfa1add--- /dev/null+++ b/packages/eve/src/runtime/schedules/subscription.integration.test.ts@@ -0,0 +1,217 @@+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 { createTestRuntime } from "#internal/testing/app-harness.js";+import { defineDynamicSchedules } from "#public/schedules/subscription.js";+import { schedules } from "#public/experimental/schedules/client.js";+import { inMemoryScheduleProvider } from "#public/schedules/providers/in-memory.js";+import { BundleKey } from "#runtime/sessions/runtime-context-keys.js";+import { getCompiledRuntimeAgentBundle } from "#runtime/sessions/compiled-agent-cache.js";+import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";+import { once } from "#tools/approval/policies.js";+import { serializeInputSchema } from "#tools/schema.js";+import { readDurableDynamicToolCallbacks } from "#tools/durable-callbacks.js";+import type { DynamicToolEntry } from "#tools/dynamic.js";+import type { ScheduleOccurrenceEvent, ScheduleRecord } from "#public/schedules/subscription.js";++const alice = {+ attributes: {},+ authenticator: "fixture",+ principalId: "alice",+ principalType: "user",+};+async function bindCaller() {+ loadContext().set(AuthKey, alice);+ loadContext().set(+ BundleKey,+ await getCompiledRuntimeAgentBundle({+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),+ }),+ );+}++describe("schedule creation and invocation", () => {+ it("exposes direct operation schemas and approves prepared data without writing a changed destination", async () => {+ let destination = "C123";+ const provider = inMemoryScheduleProvider();+ const write = vi.spyOn(provider, "create");+ const approvedTargets: string[] = [];+ const responseTargets: string[] = [];+ const dispatched = vi.fn();+ const subscription = defineDynamicSchedules({+ inputSchema: z.object({ task: z.string() }).strict(),+ provider,+ preparePayload(input) {+ return { task: input.task, target: destination };+ },+ approval: {+ create: {+ request: ({ payload }) => {+ approvedTargets.push(payload.target);+ return "user-approval";+ },+ response: ({ payload }) => {+ responseTargets.push(payload.target);+ return { status: "allowed" };+ },+ },+ invoke: once(),+ },+ auth: () => alice,+ run: dispatched,+ });+ const app = await createTestRuntime({+ modules: [+ {+ logicalPath: "schedules/requests.ts",+ loadNamespace: async () => ({ default: subscription }),+ },+ ],+ });+ await app.runAsSession(undefined, async () => {+ await bindCaller();+ const wrapper = app.manifest.dynamicTools.find(+ (entry) => entry.logicalPath === "tools/schedule__requests.ts",+ )!;+ const dynamic = app.moduleMap.nodes.__root__!.modules[wrapper.sourceId]!+ .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 } },+ channel: {},+ messages: [],+ })) as Record<string, DynamicToolEntry>;+ expect(Object.keys(tools).sort()).toEqual(+ ["create", "delete", "disable", "enable", "get", "invoke", "list"].map(+ (operation) => `schedule__requests__${operation}`,+ ),+ );+ const create = tools.schedule__requests__create!;+ const input = {+ name: "joke",+ expression: { type: "delay" as const, minutes: 1 },+ payload: { task: "A joke" },+ };+ expect(serializeInputSchema(create.inputSchema)).toMatchObject({+ type: "object",+ required: ["name", "expression", "payload"],+ });+ expect(tools.schedule__requests__list!.approval).toBeUndefined();+ const policyContext = (+ callId: string,+ toolInput: unknown,+ approvedTools = new Set<string>(),+ ) => ({ callId, toolInput, approvedTools, toolName: "schedule__requests__create" }) as never;+ const request = resolveApprovalPolicy(create.approval!);+ await expect(+ request(policyContext("invalid", { ...input, payload: {} })),+ ).resolves.toMatchObject({ type: "denied" });+ expect(approvedTargets).toEqual([]);+ await expect(request(policyContext("changed", input))).resolves.toBe("user-approval");+ expect(approvedTargets).toEqual(["C123"]);+ const response = (create.approval as ApprovalConfiguration).response!;+ await expect(response({ request: { callId: "changed" } } as never)).resolves.toEqual({+ status: "allowed",+ });+ expect(responseTargets).toEqual(["C123"]);+ destination = "C999";+ await expect(create.execute(input, { callId: "changed" } as never)).rejects.toThrow(+ "changed after approval",+ );+ expect(write).not.toHaveBeenCalled();+ await expect(request(policyContext("accepted", input))).resolves.toBe("user-approval");+ const callbacks = readDurableDynamicToolCallbacks(create)!;+ expect(callbacks.label!.start!.callback({}, input as never)).toBe("Create schedule: joke");+ const created = (await callbacks.execute!.callback(+ {},+ input as never,+ { callId: "accepted" } as never,+ )) as ScheduleRecord;+ expect(write.mock.calls[0]![1].payload).toMatchObject({+ envelope: { payload: { task: "A joke", target: "C999" } },+ });+ await expect(tools.schedule__requests__list!.execute({}, {} as never)).resolves.toMatchObject(+ { data: [{ name: created.name }] },+ );+ const invoke = tools.schedule__requests__invoke!;+ const invoked = { name: created.name };+ const invokeContext = {+ toolName: "schedule__requests__invoke",+ toolInput: invoked,+ approvedTools: new Set(["schedule__requests__invoke"]),+ } as never;+ expect(await resolveApprovalPolicy(invoke.approval!)(invokeContext)).toBe("not-applicable");+ expect(+ await resolveApprovalPolicy(tools.schedule__requests__delete!.approval!)(invokeContext),+ ).toBe("user-approval");+ await invoke.execute(invoked, {} as never);+ expect(dispatched).toHaveBeenCalledOnce();+ await tools.schedule__requests__delete!.execute(invoked, {} as never);+ await expect(+ tools.schedule__requests__get!.execute(invoked, {} as never),+ ).resolves.toBeNull();+ });+ });++ it("prepares code-only creation once and revalidates the creator on each occurrence", async () => {+ let allowed = true;+ let preparations = 0;+ const messages: string[] = [];+ const outcomes: ScheduleOccurrenceEvent[] = [];+ const subscription = defineDynamicSchedules({+ inputSchema: z.object({ task: z.string() }).strict(),+ provider: inMemoryScheduleProvider(),+ tool: false,+ async preparePayload(input, context) {+ preparations += 1;+ return { message: input.task, author: context.session.auth.current!.principalId };+ },+ auth: ({ principal, payload }) =>+ allowed && payload.author === principal.principalId ? alice : null,+ run: ({ payload }) => {+ messages.push(payload.message);+ },+ events: {+ "occurrence.dispatched": (event) => {+ outcomes.push(event);+ },+ },+ });+ const app = await createTestRuntime({+ modules: [+ {+ logicalPath: "schedules/prepared.ts",+ loadNamespace: async () => ({ default: subscription }),+ },+ ],+ });+ await app.runAsSession(undefined, async () => {+ await bindCaller();+ const client = await schedules(subscription);+ const created = await client.create({+ name: "prepared",+ expression: { type: "delay", minutes: 5 },+ payload: { task: "A joke" },+ });+ await client.get(created.name);+ await client.disable(created.name);+ await client.enable(created.name);+ await client.invoke(created.name);+ await client.invoke(created.name);+ expect(preparations).toBe(1);+ expect(messages).toEqual(["A joke", "A joke"]);+ expect(outcomes.map((event) => event.sessionIds)).toEqual([[], []]);+ expect(outcomes[0]!.occurrence).toMatchObject({+ name: created.name,+ displayName: "prepared",+ });+ expect(outcomes[0]!.executionId).not.toBe(outcomes[1]!.executionId);+ allowed = false;+ await expect(client.invoke(created.name)).rejects.toThrow("no longer authorized");+ expect(messages).toHaveLength(2);+ expect(outcomes).toHaveLength(2);+ });+ });+});diff --git a/packages/eve/src/runtime/schedules/validation.test.ts b/packages/eve/src/runtime/schedules/validation.test.tsnew file mode 100644index 000000000..b790809ae--- /dev/null+++ b/packages/eve/src/runtime/schedules/validation.test.ts@@ -0,0 +1,43 @@+import { describe, expect, it } from "vitest";+import {+ resolveScheduleTiming,+ MAX_SCHEDULE_DELAY_MINUTES,+} from "#runtime/schedules/validation.js";++describe("relative schedule timing", () => {+ it("resolves from the supplied clock and rounds upward to a UTC minute", () => {+ expect(+ resolveScheduleTiming({ type: "delay", minutes: 1 }, Date.parse("2026-09-29T23:21:37Z")),+ ).toEqual({ type: "single", at: "2026-09-29T23:23", timezone: "UTC" });+ });+ it("does not add an extra minute at an exact boundary", () => {+ expect(+ resolveScheduleTiming({ type: "delay", minutes: 1 }, Date.parse("2026-09-29T23:21:00Z")),+ ).toEqual({ type: "single", at: "2026-09-29T23:22", timezone: "UTC" });+ });+ it("crosses midnight without asking for a timezone", () => {+ expect(+ resolveScheduleTiming({ type: "delay", minutes: 1 }, Date.parse("2026-09-29T23:59:42Z")),+ ).toEqual({ type: "single", at: "2026-09-30T00:01", timezone: "UTC" });+ });+ it.each([0, -1, 1.5, NaN, Infinity, MAX_SCHEDULE_DELAY_MINUTES + 1])(+ "rejects invalid minutes %s",+ (minutes) => {+ expect(() => resolveScheduleTiming({ type: "delay", minutes })).toThrow("whole number");+ },+ );+ it("rejects timezone on a relative delay", () => {+ expect(() =>+ resolveScheduleTiming({ type: "delay", minutes: 1, timezone: "UTC" } as never),+ ).toThrow("no timezone");+ });+ it("preserves explicit wall-clock timezone semantics", () => {+ expect(+ resolveScheduleTiming({+ type: "single",+ at: "2030-01-01T09:00",+ timezone: "America/Los_Angeles",+ }),+ ).toEqual({ type: "single", at: "2030-01-01T09:00", timezone: "America/Los_Angeles" });+ });+});