GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(core,server): enable schedules by default and run loop in opencode serve by kitlangton · Pull Request #60478 · anomalyco/opencode

feat(core,server): enable schedules by default and run loop in opencode serve #60478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
kitlangton merged 6 commits into dev from schedules-default-on
Oct 30, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat(core,server): enable schedules by default and run loop in opencode serve #60478

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
2290df0
feat(core,server): enable schedules by default and run loop in openco…
kitlangton Oct 30, 2026
5e82f36
test(server): gate standalone schedule tick test with offline shell c…
kitlangton Oct 30, 2026
5aecb52
fix(core,protocol): coordinate single scheduler leader per DB and upd…
kitlangton Oct 30, 2026
505801d
fix(core): renew scheduler leader lease during long ticks and tighten…
kitlangton Oct 30, 2026
d92a13d
perf(core): sleep on doorbell with zero timers or DB writes when no s…
kitlangton Oct 30, 2026
537fde0
fix(core): bound idle doorbell wait to MAX_SLEEP for cross-process ha…
kitlangton Oct 30, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
fix(core): renew scheduler leader lease during long ticks and tighten…
… leader test
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@kitlangton
kitlangton committed Oct 30, 2026
commit 505801d3ad28a7f1aaf1b6ec8c8c5fe2c625fb3d

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

17 changes: 15 additions & 2 deletions 17 packages/core/src/schedule.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * as Schedule from "./schedule.js"

import { randomUUID } from "node:crypto"
import { and, asc, eq, gt, isNull, lte, min, or, sql } from "drizzle-orm"
import { Cause, Clock, Context, Cron, DateTime, Effect, Latch, Layer, Option, Result, Schema } from "effect"
import { Cause, Clock, Context, Cron, DateTime, Effect, Latch, Layer, Option, Result, Schedule as EffectSchedule, Schema } from "effect"
import { Node, makeGlobalNode } from "@opencode/util/effect/app-node"
import { LayerNode } from "@opencode/util/effect/layer-node"
import {
Expand Down Expand Up @@ -447,6 +447,7 @@ const layer = Layer.effect(
const step = Effect.fnUntraced(function* (row: Row, now: number) {
const due = row.next_due_at
if (due === null || row.session_id === null) return
if (!(yield* claimLeader(yield* Clock.currentTimeMillis))) return
const session = yield* sessions.get(row.session_id)
if (!(yield* settings(session)).enabled) {
// Reading the flag boots the Session's location, so check again in an hour rather than every tick. The row
Expand Down Expand Up @@ -574,6 +575,12 @@ const layer = Layer.effect(
),
)

const heartbeat = Clock.currentTimeMillis.pipe(
Effect.flatMap(claimLeader),
Effect.ignore,
Effect.schedule(EffectSchedule.spaced(LEADER_TTL / 3)),
)

const loop = Effect.gen(function* () {
yield* doorbell.close
const now = yield* Clock.currentTimeMillis
Expand All @@ -587,7 +594,13 @@ const layer = Layer.effect(
leading = true
yield* rewake.pipe(Effect.catchCause((cause) => Effect.logError("schedule rewake failed", { cause })))
}
yield* tickAt(now).pipe(Effect.catchCause((cause) => Effect.logError("schedule tick failed", { cause })))
yield* Effect.scoped(
Effect.forkScoped(heartbeat).pipe(
Effect.andThen(
tickAt(now).pipe(Effect.catchCause((cause) => Effect.logError("schedule tick failed", { cause }))),
),
),
)
const wait = yield* nextWake(now)
if (wait > 0) yield* doorbell.await.pipe(Effect.timeoutOption(wait))
})
Expand Down
47 changes: 34 additions & 13 deletions 47 packages/server/test/schedule.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,40 @@ it.live("ticks due schedules in standalone ServerProcess.start without lifecycle
}).pipe(Effect.scoped),
)

it.live("elects one scheduler leader across two server processes sharing a database", () =>
it.live("elects one scheduler leader when two server processes boot against a due schedule", () =>
Effect.gen(function* () {
const dir = (yield* tmpdirScoped("opencode-schedule-leader-")).path
const dbPath = path.join(dir, "shared.db")
const marker = path.join(dir, "runs.txt")
yield* Effect.promise(() =>
fs.writeFile(path.join(dir, "opencode.json"), JSON.stringify({ permission: { shell: "allow" } })),
)

// Seed an active schedule in a short-lived scope so it falls due while no server is running.
const createdID = yield* Effect.scoped(
Effect.gen(function* () {
const handler = yield* ServerFetch.make({
app: { version: "test" },
database: { path: dbPath },
config: { directory: dir, project: true, claude: false },
models: { fetch: false },
fs: { filewatcher: false },
})
const seededApi = client(handler)
return yield* Effect.promise(async () => {
const session = await seededApi.session.create({ location: { directory: dir } })
const created = await seededApi.experimental.schedule.create({
sessionID: session.id,
when: { in: "80 millis" },
if: { run: `printf 'x\\n' >> ${JSON.stringify(marker)} && false` },
prompt: "single leader check",
})
return created.id
})
}),
)
yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 120)))

const boot = () =>
ServerProcess.start<never, never>({
hostname: "127.0.0.1",
Expand All @@ -192,28 +221,20 @@ it.live("elects one scheduler leader across two server processes sharing a datab
fs: { filewatcher: false },
models: { fetch: false },
})
const first = yield* boot()
yield* boot()
// Both servers start against the same already-due row at t=0; without leader election both run the check.
const [first] = yield* Effect.all([boot(), boot()], { concurrency: 2 })
const base = `http://127.0.0.1:${"port" in first.address ? first.address.port : 0}`
const api = OpenCode.make({
baseUrl: base,
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
})

yield* Effect.promise(async () => {
await fs.writeFile(path.join(dir, "opencode.json"), JSON.stringify({ permission: { shell: "allow" } }))
const session = await api.session.create({ location: { directory: dir } })
const created = await api.experimental.schedule.create({
sessionID: session.id,
when: { in: "200 millis" },
if: { run: `printf 'x\\n' >> ${JSON.stringify(marker)} && false` },
prompt: "single leader check",
})
const deadline = Date.now() + 5_000
let current = created
let current = await api.experimental.schedule.get({ scheduleID: createdID })
while (current.status === "active" && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 50))
current = await api.experimental.schedule.get({ scheduleID: created.id })
current = await api.experimental.schedule.get({ scheduleID: createdID })
}
await new Promise((resolve) => setTimeout(resolve, 150))
const lines = (await fs.readFile(marker, "utf8")).trim().split("\n").filter(Boolean)
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.