fix(core): wake a held watching session again after 30 quiet minutes (#64760)
Some checks are pending
check-nix / nix-eval (push) Waiting to run
check / typecheck (push) Waiting to run
deploy-files / files-build (push) Waiting to run
deploy-files / files-deploy (push) Blocked by required conditions
deploy-site / site-build (push) Waiting to run
deploy-site / site-deploy (push) Blocked by required conditions
deploy / legacy-build (push) Waiting to run
deploy / legacy-deploy (push) Blocked by required conditions
deploy / data-build (push) Waiting to run
deploy / data-deploy (push) Blocked by required conditions

This commit is contained in:
Adam 2026-11-18 18:31:08 -06:00 • committed by GitHub
parent 9bf497d3e8
commit 88ca792e02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 6 deletions

View file

@ -31,7 +31,9 @@ const POLL_RETRY_WEBSOCKET = Duration.minutes(10)
export const WINDOW_LIMIT = 5
export const PROMPT_LIMIT = 20
// The stored wakes since the prompt and their times; a count stored before the window reads as none.
const decodeWakes = Schema.decodeUnknownOption(Schema.Struct({ count: Schema.Number, at: Schema.Array(Schema.Number) }))
const decodeWakes = Schema.decodeUnknownOption(
Schema.Struct({ count: Schema.Number, at: Schema.Array(Schema.Number), last: Schema.optional(Schema.Number) }),
)
const DELETED_RETENTION = Duration.hours(72)
const namespace = "console_watch"
@ -128,9 +130,10 @@ export const make = Effect.fnUntraced(function* (ctx: Context, http: HttpClient.
return ids
}
const now = yield* Clock.currentTimeMillis
// The member's own words count from zero, as a prompt does.
// The member's own words count from zero, as a prompt does, and so does an update after 30 minutes without any.
const own = events.some((event) => event.actor.self === true && !event.actor.bot)
const kept = own ? undefined : Option.getOrUndefined(decodeWakes(yield* ctx.storage.get(streakKey(sessionID))))
const stored = Option.getOrUndefined(decodeWakes(yield* ctx.storage.get(streakKey(sessionID))))
const kept = own || now - (stored?.last ?? now) >= Duration.toMillis(Duration.minutes(30)) ? undefined : stored
const recent = (kept?.at ?? []).filter((at) => now - at < Duration.toMillis(Duration.minutes(10)))
const count = kept?.count ?? 0
// CI runs pace themselves, so their updates count toward 20 since the prompt but not the 10-minute window.
@ -155,8 +158,13 @@ export const make = Effect.fnUntraced(function* (ctx: Context, http: HttpClient.
Effect.as(true),
Effect.catchIf(Predicate.isTagged("Session.SyntheticConflictError"), () => Effect.succeed(false)),
)
if (delivered && resume)
yield* ctx.storage.set(streakKey(sessionID), { count: count + 1, at: ci ? recent : [...recent, now] })
// A held update counts toward the 30 minutes too; a replay conflicts above and counts toward nothing.
if (delivered)
yield* ctx.storage.set(streakKey(sessionID), {
count: resume ? count + 1 : count,
at: ci || !resume ? recent : [...recent, now],
last: now,
})
return ids
})

View file

@ -583,13 +583,38 @@ describe("OpencodePlugin event watches", () => {
yield* waitUntil(Effect.sync(() => console.state.opens === 1))
for (const index of Array.from({ length: 21 }, (_, index) => index)) {
yield* deliver(console, watchEvent(session.id, `evt_${index}`))
yield* TestClock.adjust("3 minutes")
yield* TestClock.adjust("7 minutes")
}
expect(wakes.map((wake) => wake.resume)).toEqual([...Array.from({ length: 20 }, () => true), false])
}),
),
)
it.effect("resumes a held session with a new update after 30 quiet minutes, but not with the held one replayed", () =>
withConsole({}, (console) =>
Effect.gen(function* () {
const { host, wakes, replays } = yield* start(console)
const session = yield* host.session.create({})
yield* waitUntil(Effect.sync(() => console.state.opens === 1))
for (const index of Array.from({ length: 20 }, (_, index) => index)) {
yield* deliver(console, watchEvent(session.id, `evt_${index}`))
yield* TestClock.adjust("7 minutes")
}
// The held update's acknowledgement is lost until 30 minutes on, so each connection's snapshot replays it.
console.state.lose = 3
console.push(watchEvent(session.id, "evt_held"))
yield* waitUntil(Effect.sync(() => console.state.lose === 2))
for (const replayed of [1, 2, 3]) {
yield* TestClock.adjust("10 minutes")
console.state.sockets.forEach((socket) => socket.close(OpencodeWatches.LEASE_CLOSE_CODE, "lease"))
yield* waitUntil(Effect.sync(() => replays.length === replayed))
}
yield* deliver(console, watchEvent(session.id, "evt_new"))
expect(wakes.map((wake) => wake.resume)).toEqual([...Array.from({ length: 20 }, () => true), false, true])
}),
),
)
it.effect(
"resumes for six CI updates in 10 minutes, which don't fill the window, so a Slack reply after them does too",
() =>