diff --git a/packages/core/src/plugin/command/schedule.txt b/packages/core/src/plugin/command/schedule.txt index c8a05153606..820a36b3302 100644 --- a/packages/core/src/plugin/command/schedule.txt +++ b/packages/core/src/plugin/command/schedule.txt @@ -4,7 +4,7 @@ Request: $ARGUMENTS Use the schedule tools in the `opencode` namespace. They only see this session's schedules. -- To schedule something, call `schedule_create`. Choose `when` (`{ in: "20 minutes" }`, `{ at: "2026-10-27T09:00" }`, `{ every: "1 hour" }` or `{ cron: "0 9 * * 1-5" }`, optional with `until` which defaults to every 30s). Add `until` (wake once then stop, 24h default timeout) or `if` (gate each recurring slot with no model call) using either `{ run: "..." }` for a shell check or `{ code: "..." }` for a Code Mode JavaScript check calling `tools.(...)` statically and returning a boolean or `{ wake, state?, message? }`. To stop a repeating schedule, add `ends`: `{ after: "8 hours" }`, `{ at: "..." }` or `{ times: 3 }`. Write `prompt` as the instruction you will receive when it wakes, with enough context to act on it without this conversation. +- To schedule something, call `schedule_create`. Choose `when` (`{ in: "20 minutes" }`, `{ at: "2026-10-27T09:00" }`, `{ every: "1 hour" }` or `{ cron: "0 9 * * 1-5" }`, optional with `until` which defaults to every 30s). Add `until` (wake once then stop, 24h default timeout; matches immediately if already true) or `if` (gate each recurring slot with no model call; baselines `state` at creation without waking) using either `{ run: "..." }` for a shell check or `{ code: "..." }` for a Code Mode JavaScript check calling `tools.(...)` statically (no `fetch`, 30s / 20 calls / 32 KB caps) and returning a boolean or `{ wake, state?, message? }`. Code checks run once at creation with `state = null` and fail `schedule_create` if they throw. To stop a repeating schedule, add `ends`: `{ after: "8 hours" }`, `{ at: "..." }` or `{ times: 3 }`. Write `prompt` as the instruction you will receive when it wakes, with enough context to act on it without this conversation. - To pause, resume or delete one, find its id with `schedule_list`, then call `schedule_pause`, `schedule_resume` or `schedule_delete`. - If the request is empty, call `schedule_list` and show this session's schedules: name, when, status and next run. diff --git a/packages/core/src/schedule.ts b/packages/core/src/schedule.ts index 6900f334ee3..a31ec5243ad 100644 --- a/packages/core/src/schedule.ts +++ b/packages/core/src/schedule.ts @@ -294,7 +294,7 @@ const layer = Layer.effect( if (!outcome.ok) { const strikes = row.failures + 1 - if (strikes < MAX_CHECK_FAILURES) { + if (!outcome.invalid && strikes < MAX_CHECK_FAILURES) { yield* update(row, { failures: strikes, last_outcome: "failed", @@ -304,13 +304,16 @@ const layer = Layer.effect( return } const run = row.run_count + 1 + const note = outcome.invalid + ? `[Schedule paused after invalid check: ${outcome.error}]` + : `[Schedule paused after ${strikes} failed checks: ${outcome.error}]` const id = yield* admitWake( row, session.id, run, due, late, - `${row.action.prompt}\n\n[Schedule paused after ${strikes} failed checks: ${outcome.error}]`, + `${row.action.prompt}\n\n${note}`, { error: outcome.error, failures: strikes }, ) yield* db @@ -636,7 +639,7 @@ const layer = Layer.effect( check === undefined ? { matched: false as const, state: null as Schema.Json, checks: 0 } : yield* Effect.gen(function* () { - yield* ScheduleCheck.authorize(session, input.name, check).pipe( + yield* ScheduleCheck.authorize(session, input.name, when, check).pipe( instances.provide(session), Effect.mapError((error) => (typeof error === "string" ? new InvalidError({ reason: error }) : error)), Effect.catchTag("FileSystem.DirectoryNotFoundError", () => @@ -647,7 +650,7 @@ const layer = Layer.effect( ) const shouldSeed = check.mode === "until" || - (check.condition.type === "code" && when.type === "every") || + check.condition.type === "code" || (check.condition.type === "shell" && check.condition.changes === true) if (!shouldSeed) return { matched: false as const, state: null as Schema.Json, checks: 0 } const init = yield* ScheduleCheck.evaluate(session, check, null, { @@ -663,7 +666,8 @@ const layer = Layer.effect( ), ) if (!init.ok) { - if (init.invalid) return yield* new InvalidError({ reason: init.error }) + // A code check that throws or fails at creation is broken (e.g. wrong MCP response field); fail right away. + if (init.invalid || check.condition.type === "code") return yield* new InvalidError({ reason: init.error }) return { matched: false as const, state: null as Schema.Json, checks: 0 } } if (check.mode === "until" && init.wake) diff --git a/packages/core/src/schedule/check.ts b/packages/core/src/schedule/check.ts index 16f0633a382..aea13b2ee3a 100644 --- a/packages/core/src/schedule/check.ts +++ b/packages/core/src/schedule/check.ts @@ -117,15 +117,16 @@ export function describe(condition: ScheduleSchema.Condition): string { export const authorize = Effect.fnUntraced(function* ( session: SessionSchema.Info, name: string | undefined, + when: ScheduleSchema.When, check: ScheduleSchema.StoredCheck, ) { const plugins = yield* Plugin.Service yield* plugins.awaitActivation switch (check.condition.type) { case "shell": - return yield* authorizeShell(session, name, check.mode, check.condition) + return yield* authorizeShell(session, name, when, check.mode, check.condition) case "code": - return yield* authorizeCode(session, name, check.mode, check.condition) + return yield* authorizeCode(session, name, when, check.mode, check.condition) } }) @@ -182,6 +183,7 @@ const resolveCodeTools = Effect.fnUntraced(function* (paths: ReadonlyArray { expect(asked).toHaveLength(1) expect(asked[0]).toMatchObject({ action: "slack_read_thread", - metadata: { schedule: "thread", mode: "if", tools: ["slack.read_thread"] }, + metadata: { + schedule: "thread", + mode: "if", + when: { type: "every", every: "1 minute" }, + unattended: true, + tools: ["slack.read_thread"], + }, }) expect(asked[0]?.save).toBeUndefined() @@ -1419,5 +1425,46 @@ describe("Schedule", () => { expect(yield* bad("return true")).toMatchObject({ _tag: "Permission.BlockedError" }) }), ) + + it.effect("fails schedule_create when a t=0 code run throws, and pauses on strike 1 when a later tick returns an invalid shape", () => + Effect.gen(function* () { + const schedules = yield* setup() + const thrown = yield* Effect.flip( + schedules.create({ + sessionID, + when: { type: "cron", cron: "0 9 * * *" }, + if: { + type: "code", + code: `const t = await tools.slack.read_thread({}); return t.nonexistent.length > 0`, + }, + action: { type: "prompt", prompt: "Morning check." }, + }), + ) + expect(thrown).toMatchObject({ _tag: "Schedule.InvalidError" }) + + // First run (state === null) returns valid { wake: false, state: { ok: true } }; + // first scheduled tick (state !== null) returns an invalid string, pausing immediately on strike 1. + const schedule = yield* schedules.create({ + sessionID, + when: { type: "every", every: "1 minute" }, + if: { + type: "code", + code: `if (state === null) return { wake: false, state: { ok: true } }; return "not-a-check-result"`, + }, + action: { type: "prompt", prompt: "Watch." }, + }) + yield* TestClock.adjust("1 minute") + yield* schedules.tick + expect(yield* schedules.get(schedule.id)).toMatchObject({ + status: "paused", + lastOutcome: "failed", + runCount: 1, + }) + const { db } = yield* Database.Service + expect(yield* SessionInbox.find(db, messageID(schedule, 1))).toMatchObject({ + payload: { text: expect.stringContaining("[Schedule paused after invalid check:") }, + }) + }), + ) }) }) diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 50a923b4ad9..3fc515d3710 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -21281,7 +21281,7 @@ "code": { "type": "string", "minLength": 1, - "description": "Code Mode JavaScript program. Static tools.(...) calls are authorized at creation; gets state, now and runs, and returns a boolean or { wake, state?, message? }." + "description": "Code Mode JavaScript check (no fetch; 30s, 20 tool calls, 32 KB output cap). Call tools statically as tools..(...) or tools[\"ns-name\"].tool(...); each is authorized once at creation to run unattended. Runs once at creation with state = null (if baselines state without waking; until matches immediately if true), with globals state, now (epoch ms) and runs. Return a boolean or { wake, state?, message? } (message is appended to prompt on wake), e.g.: const t = await tools.slack.read_thread({ id }); return { wake: state !== null && t.count > state.count, state: { count: t.count }, message: \"New reply\" }" }, "run": { "not": {} diff --git a/packages/schema/src/schedule.ts b/packages/schema/src/schedule.ts index 4525862b009..9b4acc1bf7c 100644 --- a/packages/schema/src/schedule.ts +++ b/packages/schema/src/schedule.ts @@ -188,8 +188,13 @@ export const ShellConditionInput = Schema.Struct({ export const CodeConditionInput = Schema.Struct({ code: Schema.String.check(Schema.isMinLength(1)).annotate({ - description: - "Code Mode JavaScript program. Static tools.(...) calls are authorized at creation; gets state, now and runs, and returns a boolean or { wake, state?, message? }.", + description: [ + "Code Mode JavaScript check (no fetch; 30s, 20 tool calls, 32 KB output cap).", + 'Call tools statically as tools..(...) or tools["ns-name"].tool(...); each is authorized once at creation to run unattended.', + "Runs once at creation with state = null (if baselines state without waking; until matches immediately if true), with globals state, now (epoch ms) and runs.", + "Return a boolean or { wake, state?, message? } (message is appended to prompt on wake), e.g.:", + 'const t = await tools.slack.read_thread({ id }); return { wake: state !== null && t.count > state.count, state: { count: t.count }, message: "New reply" }', + ].join(" "), }), }).annotate({ identifier: "Schedule.CodeConditionInput" })