From a6948044855ac17033c965ac2d06369d2eeab012 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sun, 1 Nov 2026 21:16:34 -0600 Subject: [PATCH] refactor(core): remove duplicated schedule code (#61172) --- packages/core/src/schedule.ts | 261 +++++++++++----------------- packages/core/src/schedule/check.ts | 56 +++--- 2 files changed, 126 insertions(+), 191 deletions(-) diff --git a/packages/core/src/schedule.ts b/packages/core/src/schedule.ts index dbfec7e6d90..f9fdda76ac7 100644 --- a/packages/core/src/schedule.ts +++ b/packages/core/src/schedule.ts @@ -103,6 +103,7 @@ const FAILED_RETRY = 5 * MINUTE const CHECK_FAILED_RETRY = MINUTE const MAX_CHECK_FAILURES = 3 const TICK_CONCURRENCY = 4 +const UNSEEDED = { matched: false as const, state: null as Schema.Json, checks: 0 } const duration = (value: string) => Option.match(durationMillis(value), { @@ -288,132 +289,50 @@ const layer = Layer.effect( const late = now - due >= LATE_AFTER const next = following(row, now) - if (row.check !== null) { - const check = row.check - const checkMessageID = SessionMessage.ID.make(`msg_${row.id}_check_${row.checks + 1}`) - const outcome = yield* ScheduleCheck.evaluate(session, check, row.state ?? null, { - messageID: checkMessageID, - now, - checks: row.checks, - }).pipe(instances.provide(session)) + const check = row.check + const outcome: ScheduleCheck.Outcome = + check === null + ? { ok: true, wake: true, state: null } + : yield* ScheduleCheck.evaluate(session, check, row.state ?? null, { + messageID: SessionMessage.ID.make(`msg_${row.id}_check_${row.checks + 1}`), + now, + checks: row.checks, + }).pipe(instances.provide(session)) - if (!outcome.ok) { - const strikes = row.failures + 1 - if (!outcome.invalid && strikes < MAX_CHECK_FAILURES) { - yield* update(row, { - failures: strikes, - last_outcome: "failed", - retry_at: now + CHECK_FAILED_RETRY, - time_updated: now, - }) - 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${note}`, - { error: outcome.error, failures: strikes }, - ) - yield* db - .update(ScheduleTable) - .set({ - status: "paused", - failures: strikes, - run_count: run, - last_message_id: id, - last_fired_at: now, - last_outcome: "failed", - retry_at: null, - time_updated: now, - }) - .where(eq(ScheduleTable.id, row.id)) - .run() - .pipe(Effect.orDie) - return - } - - if (!outcome.wake) { - const checks = row.checks + 1 - const ended = next === undefined || (row.ends_at !== null && next > row.ends_at) - if (check.mode === "until" && ended) { - const run = row.run_count + 1 - const id = yield* admitWake( - row, - session.id, - run, - due, - late, - `${row.action.prompt}\n\n[Schedule timed out without matching: ${ScheduleCheck.describe(check.condition)}]`, - { expired: true }, - ) - yield* db - .update(ScheduleTable) - .set({ - status: "done", - checks, - failures: 0, - state: outcome.state, - run_count: run, - last_message_id: id, - last_fired_at: now, - last_outcome: "expired", - next_due_at: null, - remaining_runs: 0, - retry_at: null, - time_updated: now, - }) - .where(eq(ScheduleTable.id, row.id)) - .run() - .pipe(Effect.orDie) - return - } + if (!outcome.ok) { + const strikes = row.failures + 1 + if (!outcome.invalid && strikes < MAX_CHECK_FAILURES) { yield* update(row, { - checks, - failures: 0, - state: outcome.state, - last_outcome: "checked", - retry_at: null, - ...(ended ? { status: "done" as const, next_due_at: null } : { next_due_at: next }), + failures: strikes, + last_outcome: "failed", + retry_at: now + CHECK_FAILED_RETRY, time_updated: now, }) 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, - promptText(row.action.prompt, outcome.message), - outcome.message === undefined ? {} : { message: outcome.message }, + `${row.action.prompt}\n\n${note}`, + { error: outcome.error, failures: strikes }, ) - const remaining = - check.mode === "until" ? 0 : row.remaining_runs === null ? null : row.remaining_runs - 1 - const done = check.mode === "until" || finished(row, next, remaining) - // Record the run even if a pause landed during the prompt, or the next firing would reuse this message id. yield* db .update(ScheduleTable) .set({ + status: "paused", + failures: strikes, run_count: run, - checks: row.checks + 1, - failures: 0, - state: outcome.state, last_message_id: id, last_fired_at: now, - last_outcome: late ? "late" : "fired", + last_outcome: "failed", retry_at: null, - remaining_runs: remaining, - ...(done ? { status: "done" as const } : {}), - next_due_at: done ? null : next, time_updated: now, }) .where(eq(ScheduleTable.id, row.id)) @@ -422,14 +341,71 @@ const layer = Layer.effect( return } + if (check !== null && !outcome.wake) { + const checks = row.checks + 1 + const ended = next === undefined || (row.ends_at !== null && next > row.ends_at) + if (check.mode === "until" && ended) { + const run = row.run_count + 1 + const id = yield* admitWake( + row, + session.id, + run, + due, + late, + `${row.action.prompt}\n\n[Schedule timed out without matching: ${ScheduleCheck.describe(check.condition)}]`, + { expired: true }, + ) + yield* db + .update(ScheduleTable) + .set({ + status: "done", + checks, + failures: 0, + state: outcome.state, + run_count: run, + last_message_id: id, + last_fired_at: now, + last_outcome: "expired", + next_due_at: null, + remaining_runs: 0, + retry_at: null, + time_updated: now, + }) + .where(eq(ScheduleTable.id, row.id)) + .run() + .pipe(Effect.orDie) + return + } + yield* update(row, { + checks, + failures: 0, + state: outcome.state, + last_outcome: "checked", + retry_at: null, + ...(ended ? { status: "done" as const, next_due_at: null } : { next_due_at: next }), + time_updated: now, + }) + return + } + const run = row.run_count + 1 - const id = yield* admitWake(row, session.id, run, due, late, row.action.prompt) - const remaining = row.remaining_runs === null ? null : row.remaining_runs - 1 - const done = finished(row, next, remaining) + const id = yield* admitWake( + row, + session.id, + run, + due, + late, + promptText(row.action.prompt, outcome.message), + check === null ? undefined : outcome.message === undefined ? {} : { message: outcome.message }, + ) + const remaining = check?.mode === "until" ? 0 : row.remaining_runs === null ? null : row.remaining_runs - 1 + const done = check?.mode === "until" || finished(row, next, remaining) + // Record the run even if a pause landed during the prompt, or the next firing would reuse this message id. yield* db .update(ScheduleTable) .set({ run_count: run, + ...(check === null ? {} : { checks: row.checks + 1, failures: 0, state: outcome.state }), last_message_id: id, last_fired_at: now, last_outcome: late ? "late" : "fired", @@ -701,10 +677,10 @@ const layer = Layer.effect( return Service.of({ create: Effect.fn("Schedule.create")(function* (input) { const session = yield* sessions.get(input.sessionID) + const missingDirectory = () => + Effect.fail(new InvalidError({ reason: `Session directory does not exist: ${session.location.directory}` })) const config = yield* settings(session).pipe( - Effect.catchTag("FileSystem.DirectoryNotFoundError", () => - Effect.fail(new InvalidError({ reason: `Session directory does not exist: ${session.location.directory}` })), - ), + Effect.catchTag("FileSystem.DirectoryNotFoundError", missingDirectory), ) if (!config.enabled) return yield* new DisabledError() const check = yield* resolveStoredCheck(input) @@ -723,74 +699,36 @@ const layer = Layer.effect( const scheduleID = ID.create() const seeded = check === undefined - ? { matched: false as const, state: null as Schema.Json, checks: 0 } + ? UNSEEDED : yield* Effect.gen(function* () { 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", () => - Effect.fail( - new InvalidError({ reason: `Session directory does not exist: ${session.location.directory}` }), - ), - ), + Effect.catchTag("FileSystem.DirectoryNotFoundError", missingDirectory), ) const shouldSeed = check.mode === "until" || 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 } + if (!shouldSeed) return UNSEEDED const init = yield* ScheduleCheck.evaluate(session, check, null, { messageID: SessionMessage.ID.make(`msg_${scheduleID}_check_0`), now, checks: 0, }).pipe( instances.provide(session), - Effect.catchTag("FileSystem.DirectoryNotFoundError", () => - Effect.fail( - new InvalidError({ reason: `Session directory does not exist: ${session.location.directory}` }), - ), - ), + Effect.catchTag("FileSystem.DirectoryNotFoundError", missingDirectory), ) if (!init.ok) { // 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 } + return UNSEEDED } if (check.mode === "until" && init.wake) - return { matched: true as const, state: init.state, message: init.message } + return { matched: true as const, state: init.state, checks: 1, message: init.message } return { matched: false as const, state: init.state, checks: 1 } }) - if (seeded.matched) { - const row = yield* db - .insert(ScheduleTable) - .values({ - id: scheduleID, - session_id: session.id, - name: input.name ?? null, - check: check ?? null, - state: seeded.state, - checks: 1, - action: input.action, - when, - tz: DateTime.zoneToString(zone), - status: "done", - next_due_at: null, - ends_at: ends, - remaining_runs: 0, - run_count: 1, - last_fired_at: now, - last_outcome: "fired", - time_created: now, - time_updated: now, - }) - .returning() - .get() - .pipe(Effect.orDie) - return toInfo(row, seeded.message) - } - - const remaining = check?.mode === "until" ? 1 : (input.ends?.times ?? null) const row = yield* db .insert(ScheduleTable) .values({ @@ -805,13 +743,24 @@ const layer = Layer.effect( tz: DateTime.zoneToString(zone), next_due_at: due, ends_at: ends, - remaining_runs: remaining, + remaining_runs: check?.mode === "until" ? 1 : (input.ends?.times ?? null), time_created: now, time_updated: now, + ...(seeded.matched + ? { + status: "done" as const, + next_due_at: null, + remaining_runs: 0, + run_count: 1, + last_fired_at: now, + last_outcome: "fired" as const, + } + : {}), }) .returning() .get() .pipe(Effect.orDie) + if (seeded.matched) return toInfo(row, seeded.message) yield* doorbell.open return toInfo(row) }), diff --git a/packages/core/src/schedule/check.ts b/packages/core/src/schedule/check.ts index d628e29d4ae..816422d13de 100644 --- a/packages/core/src/schedule/check.ts +++ b/packages/core/src/schedule/check.ts @@ -120,8 +120,7 @@ export const authorize = Effect.fnUntraced(function* ( when: ScheduleSchema.When, check: ScheduleSchema.StoredCheck, ) { - const plugins = yield* Plugin.Service - yield* plugins.awaitActivation + yield* Plugin.awaitActivation switch (check.condition.type) { case "shell": return yield* authorizeShell(session, name, when, check.mode, check.condition) @@ -138,8 +137,7 @@ export const evaluate = Effect.fnUntraced(function* ( context: RunContext, ) { const run = Effect.gen(function* () { - const plugins = yield* Plugin.Service - yield* plugins.awaitActivation + yield* Plugin.awaitActivation switch (check.condition.type) { case "shell": return yield* runShell(session, check.condition, prevState) @@ -158,7 +156,14 @@ export const evaluate = Effect.fnUntraced(function* ( ) }) -const resolveCodeTools = Effect.fnUntraced(function* (paths: ReadonlyArray) { +const wakeOutcome = (wake: boolean, state: Schema.Json, message: string | undefined) => ({ + ok: true as const, + wake, + state, + ...(wake && message !== undefined ? { message } : {}), +}) + +const codeActions = Effect.fnUntraced(function* (paths: ReadonlyArray) { const mcp = yield* McpTool.Service const tools = yield* Tool.Service if (paths.length > 0) yield* mcp.flush @@ -177,7 +182,7 @@ const resolveCodeTools = Effect.fnUntraced(function* (paths: ReadonlyArray tool.options?.permission ?? tool.id)) } }) const authorizeCode = Effect.fnUntraced(function* ( @@ -201,8 +206,7 @@ const authorizeCode = Effect.fnUntraced(function* ( agent: agent.id, }) - const { resolved } = yield* resolveCodeTools(paths) - const actions = Array.dedupe(resolved.map((tool) => tool.options?.permission ?? tool.id)) + const actions = (yield* codeActions(paths)).actions if (actions.length === 0) { yield* permission.assert({ @@ -258,10 +262,10 @@ const runCode = Effect.fnUntraced(function* ( if (Result.isFailure(scanned)) return { ok: false as const, error: scanned.failure.message } const paths = scanned.success - const resolvedExit = yield* Effect.result(resolveCodeTools(paths)) - if (Result.isFailure(resolvedExit)) return { ok: false as const, error: resolvedExit.failure } + const resolved = yield* Effect.result(codeActions(paths)) + if (Result.isFailure(resolved)) return { ok: false as const, error: resolved.failure } - const actions = Array.dedupe(resolvedExit.success.resolved.map((tool) => tool.options?.permission ?? tool.id)) + const actions = resolved.success.actions const unattended: Permission.Ruleset = ["execute", ...actions].map((action) => ({ action, resource: "*", @@ -289,7 +293,7 @@ const runCode = Effect.fnUntraced(function* ( input: { state: prevState, now: context.now, runs: context.checks }, } - const result = yield* resolvedExit.success.tools + const result = yield* resolved.success.tools .check({ code: condition.code, tools: paths, @@ -328,12 +332,7 @@ const runCode = Effect.fnUntraced(function* ( const state = decoded.success.state === undefined ? prevState : decoded.success.state const trimmed = decoded.success.message?.trim() const message = trimmed ? trimmed : undefined - return { - ok: true as const, - wake: decoded.success.wake, - state, - ...(decoded.success.wake && message !== undefined ? { message } : {}), - } + return wakeOutcome(decoded.success.wake, state, message) }) const scanShell = Effect.fnUntraced(function* (command: string) { @@ -463,12 +462,7 @@ const runShell = Effect.fnUntraced(function* ( condition.is !== undefined || condition.matches !== undefined || condition.changes === true if (!hasPredicate) { - return { - ok: true as const, - wake: output.exitCode === 0, - state: null, - ...(output.exitCode === 0 && message !== undefined ? { message } : {}), - } + return wakeOutcome(output.exitCode === 0, null, message) } if (output.exitCode !== 0) { @@ -480,23 +474,15 @@ const runShell = Effect.fnUntraced(function* ( } if (condition.is !== undefined) { - const wake = trimmed === condition.is.trim() - return { ok: true as const, wake, state: null, ...(wake && message !== undefined ? { message } : {}) } + return wakeOutcome(trimmed === condition.is.trim(), null, message) } if (condition.matches !== undefined) { - const wake = new RegExp(condition.matches).test(output.stdout) - return { ok: true as const, wake, state: null, ...(wake && message !== undefined ? { message } : {}) } + return wakeOutcome(new RegExp(condition.matches).test(output.stdout), null, message) } const prevStdout = isRecord(prevState) && typeof prevState.stdout === "string" ? prevState.stdout : null - const wake = prevStdout !== null && output.stdout !== prevStdout - return { - ok: true as const, - wake, - state: { stdout: output.stdout }, - ...(wake && message !== undefined ? { message } : {}), - } + return wakeOutcome(prevStdout !== null && output.stdout !== prevStdout, { stdout: output.stdout }, message) }) function utf8Decode(bytes: Uint8Array): string {