diff --git a/packages/core/src/plugin/provider/opencode-watches.ts b/packages/core/src/plugin/provider/opencode-watches.ts index 4d12d6f4ef1..610284a2e91 100644 --- a/packages/core/src/plugin/provider/opencode-watches.ts +++ b/packages/core/src/plugin/provider/opencode-watches.ts @@ -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 }) diff --git a/packages/core/test/plugin/provider-opencode-watches.test.ts b/packages/core/test/plugin/provider-opencode-watches.test.ts index 6e005418f32..a88c3ac6761 100644 --- a/packages/core/test/plugin/provider-opencode-watches.test.ts +++ b/packages/core/test/plugin/provider-opencode-watches.test.ts @@ -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", () =>