mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 01:08:31 +03:00
fix(core): fail schedule creation when a code check throws and pause invalid checks immediately (#60349)
This commit is contained in:
committed by
GitHub
Unverified
parent
8609cc55d0
commit
055a2a9c13
@@ -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.<path>(...)` 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.<path>(...)` 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.
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<strin
|
||||
const authorizeCode = Effect.fnUntraced(function* (
|
||||
session: SessionSchema.Info,
|
||||
name: string | undefined,
|
||||
when: ScheduleSchema.When,
|
||||
mode: ScheduleSchema.CheckMode,
|
||||
condition: ScheduleSchema.CodeCondition,
|
||||
) {
|
||||
@@ -206,7 +208,7 @@ const authorizeCode = Effect.fnUntraced(function* (
|
||||
yield* permission.assert({
|
||||
action: "execute",
|
||||
resources: ["*"],
|
||||
metadata: { schedule: name, mode, code: condition.code, tools: [] },
|
||||
metadata: { schedule: name, mode, when, unattended: true, code: condition.code, tools: [] },
|
||||
sessionID: session.id,
|
||||
agent: agent.id,
|
||||
})
|
||||
@@ -233,7 +235,7 @@ const authorizeCode = Effect.fnUntraced(function* (
|
||||
action,
|
||||
resources: ["*"],
|
||||
// No `save`: "always" would save a project-wide allow, while scheduled ticks run via Unattended.
|
||||
metadata: { schedule: name, mode, code: condition.code, tools: [...paths] },
|
||||
metadata: { schedule: name, mode, when, unattended: true, code: condition.code, tools: [...paths] },
|
||||
sessionID: session.id,
|
||||
agent: agent.id,
|
||||
}),
|
||||
@@ -388,6 +390,7 @@ const assertShellPermissions = Effect.fnUntraced(function* (
|
||||
const authorizeShell = Effect.fnUntraced(function* (
|
||||
session: SessionSchema.Info,
|
||||
name: string | undefined,
|
||||
when: ScheduleSchema.When,
|
||||
mode: ScheduleSchema.CheckMode,
|
||||
condition: ScheduleSchema.ShellCondition,
|
||||
) {
|
||||
@@ -395,6 +398,8 @@ const authorizeShell = Effect.fnUntraced(function* (
|
||||
yield* assertShellPermissions(session, scanned, {
|
||||
schedule: name,
|
||||
mode,
|
||||
when,
|
||||
unattended: true,
|
||||
run: condition.run,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1358,7 +1358,13 @@ describe("Schedule", () => {
|
||||
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:") },
|
||||
})
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Generated
+1
-1
@@ -21281,7 +21281,7 @@
|
||||
"code": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"description": "Code Mode JavaScript program. Static tools.<path>(...) 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.<ns>.<tool>(...) 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": {}
|
||||
|
||||
@@ -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.<path>(...) 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.<ns>.<tool>(...) 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" })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user