From 18c4f551181c9de1e947212eb48283d973c84f9a Mon Sep 17 00:00:00 2001 From: Adam Hearn <22334119+hearnadam@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:32:55 -0800 Subject: [PATCH] fix(core): poll Console config every 5 minutes with jitter (#66673) --- packages/core/src/plugin/provider/opencode.ts | 19 +++-- .../provider-opencode-artifacts.test.ts | 10 ++- .../plugin/provider-opencode-watches.test.ts | 9 ++- .../test/plugin/provider-opencode.test.ts | 78 ++++++++++++++----- services/site/src/docs/content/policies.mdx | 12 +-- 5 files changed, 90 insertions(+), 38 deletions(-) diff --git a/packages/core/src/plugin/provider/opencode.ts b/packages/core/src/plugin/provider/opencode.ts index 7cd822fd485..24106873872 100644 --- a/packages/core/src/plugin/provider/opencode.ts +++ b/packages/core/src/plugin/provider/opencode.ts @@ -1,5 +1,5 @@ import { isDeepStrictEqual } from "node:util" -import { Duration, Effect, Equal, FiberHandle, Option, Schema, Scope, Semaphore, Stream } from "effect" +import { Duration, Effect, Equal, FiberHandle, Option, Random, Schema, Scope, Semaphore, Stream } from "effect" import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/effect/integration" import { define } from "@opencode/plugin/effect/plugin" import type { SessionHttpResponse } from "@opencode/plugin/effect/session" @@ -36,6 +36,8 @@ import { ProviderRefresh } from "./refresh.js" const clientID = "opencode-cli" const methodID = Integration.MethodID.make("device") +const pollInterval = Duration.minutes(5) +const pollJitter = Duration.minutes(1) const Device = Schema.Struct({ device_code: Schema.String, user_code: Schema.String, @@ -224,7 +226,7 @@ export const OpencodePlugin = define< const artifacts = yield* OpencodeArtifacts.make(ctx, http) // Every project signed in to the same workspace shares one fetch, reused while it is younger than `maxAge`. - const load = Effect.fn("OpencodePlugin.load")(function* (maxAge: Duration.Input = Duration.minutes(1)) { + const load = Effect.fn("OpencodePlugin.load")(function* (maxAge: Duration.Input = pollInterval) { const connection = yield* ctx.integration.connection.active("opencode") if (!connection) { reported = undefined @@ -571,12 +573,19 @@ export const OpencodePlugin = define< // Console config can change independently of local credential activity, so re-fetch // periodically and only rebuild the catalog and search providers when the snapshot differs. // Only while connected: a signed-out location runs no timer and no credential lookups. + // Additive jitter stays >= pollInterval so SharedCache never skips a wake-up as still fresh. const polling = yield* FiberHandle.make() + const nextPoll = Effect.gen(function* () { + const jitter = yield* Random.nextIntBetween(0, Duration.toMillis(pollJitter)) + return Duration.millis(Duration.toMillis(pollInterval) + jitter) + }) const poll = Effect.fnUntraced(function* () { if (!snapshot.connection) return yield* FiberHandle.clear(polling) - yield* FiberHandle.run(polling, Effect.sleep(Duration.minutes(1)).pipe(Effect.andThen(check()), Effect.forever), { - onlyIfMissing: true, - }) + yield* FiberHandle.run( + polling, + nextPoll.pipe(Effect.flatMap(Effect.sleep), Effect.andThen(check()), Effect.forever), + { onlyIfMissing: true }, + ) }) yield* ProviderRefresh.onSwitch("opencode", refresh().pipe(Effect.andThen(poll()))) yield* poll() diff --git a/packages/core/test/plugin/provider-opencode-artifacts.test.ts b/packages/core/test/plugin/provider-opencode-artifacts.test.ts index a16840d0485..fecc605fb5b 100644 --- a/packages/core/test/plugin/provider-opencode-artifacts.test.ts +++ b/packages/core/test/plugin/provider-opencode-artifacts.test.ts @@ -10,6 +10,7 @@ import { PluginHost } from "@opencode/core/plugin/host" import { OpencodePlugin } from "@opencode/core/plugin/provider/opencode" import { OpencodeArtifacts } from "@opencode/core/plugin/provider/opencode-artifacts" import { OpencodeWatches } from "@opencode/core/plugin/provider/opencode-watches" +import { advance } from "../lib/clock" import { eventually } from "../lib/eventually" import { testEffect } from "../lib/effect" import { PluginTestLayer } from "./fixture" @@ -21,6 +22,7 @@ function fakeConsole() { const state = { artifacts: true, watches: true, + configs: 0, blobs: new Map(), blobReads: new Array(), uploads: new Array(), @@ -34,7 +36,8 @@ function fakeConsole() { fetch: async (request) => { const url = new URL(request.url) const slug = url.pathname.split("/").at(-1) ?? "" - if (url.pathname === "/api/v2/config") + if (url.pathname === "/api/v2/config") { + state.configs++ return Response.json( {}, { @@ -44,6 +47,7 @@ function fakeConsole() { }, }, ) + } if (url.pathname === "/api/artifacts/prepare") { const body = (await request.json()) as { files: { sha256: string }[] } state.prepared.push(body) @@ -135,7 +139,9 @@ describe("OpencodePlugin artifacts", () => { expect((yield* artifacts.prompts()).defaultProvider.prompt).toContain("Comments wake this session.") console.state.artifacts = false - yield* TestClock.adjust("1 minute") + const before = console.state.configs + yield* TestClock.adjust("5 minutes") + yield* advance(() => console.state.configs > before) const providers = yield* eventually(artifacts.providers(), (list) => list.length === 1) expect(providers.map((provider) => [provider.id, provider.default])).toEqual([[Artifact.LocalProviderID, true]]) }), diff --git a/packages/core/test/plugin/provider-opencode-watches.test.ts b/packages/core/test/plugin/provider-opencode-watches.test.ts index d160bb35e24..fa5a9073d4f 100644 --- a/packages/core/test/plugin/provider-opencode-watches.test.ts +++ b/packages/core/test/plugin/provider-opencode-watches.test.ts @@ -15,6 +15,7 @@ import { SyntheticConflictError } from "@opencode/core/session/error" import { SessionMessage } from "@opencode/core/session/message" import { Tool } from "@opencode/core/tool" import { Agent } from "@opencode/core/agent" +import { advance } from "../lib/clock" import { eventually, waitUntil } from "../lib/eventually" import { testEffect } from "../lib/effect" import { PluginTestLayer } from "./fixture" @@ -1196,13 +1197,13 @@ describe("OpencodePlugin event watches", () => { expect(yield* watchTools).toEqual([]) console.state.watches = true - yield* TestClock.adjust("1 minute") - yield* waitUntil(Effect.sync(() => console.state.opens === 1)) + yield* TestClock.adjust("5 minutes") + yield* advance(() => console.state.opens === 1) expect(yield* eventually(watchTools, (tools) => tools.length === 4)).toHaveLength(4) console.state.watches = false - yield* TestClock.adjust("1 minute") - yield* waitUntil(Effect.sync(() => console.state.sockets.size === 0)) + yield* TestClock.adjust("5 minutes") + yield* advance(() => console.state.sockets.size === 0) expect(yield* eventually(watchTools, (tools) => tools.length === 0)).toEqual([]) }), ), diff --git a/packages/core/test/plugin/provider-opencode.test.ts b/packages/core/test/plugin/provider-opencode.test.ts index a9f72e63673..a2bede6aacd 100644 --- a/packages/core/test/plugin/provider-opencode.test.ts +++ b/packages/core/test/plugin/provider-opencode.test.ts @@ -4,7 +4,7 @@ import { LLMClient, RequestExecutor } from "@opencode/ai/route" import { Document, Event, Info } from "@opencode/schema/config" import { ConfigPolicy } from "@opencode/schema/config/policy" import { Money } from "@opencode/schema/money" -import { Effect, Fiber, Layer, Schema, Stream } from "effect" +import { Effect, Fiber, Layer, Random, Schema, Stream } from "effect" import { TestClock } from "effect/testing" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" import { Agent } from "@opencode/core/agent" @@ -811,26 +811,28 @@ describe("OpencodePlugin", () => { expect(yield* websearch.providers()).toEqual([]) state.advertised = true - yield* TestClock.adjust("50 seconds") + yield* TestClock.adjust("4 minutes") yield* drain expect(state.requests).toBe(1) expect(rebuilds).toEqual(initial) expect(yield* websearch.providers()).toEqual([]) - yield* TestClock.adjust("10 seconds") + yield* TestClock.adjust("2 minutes") yield* drain expect(state.requests).toBe(2) expect(rebuilds).toEqual({ provider: initial.provider + 1, websearch: initial.websearch + 1 }) expect(yield* websearch.providers()).toContainEqual(consoleSearch) - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(state.requests).toBe(3) expect(rebuilds).toEqual({ provider: initial.provider + 1, websearch: initial.websearch + 1 }) expect(yield* websearch.providers()).toContainEqual(consoleSearch) state.failing = true - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") + yield* drain + yield* TestClock.adjust("10 seconds") yield* drain // The failed fetch and its 3 retries. expect(state.requests).toBe(7) @@ -839,7 +841,7 @@ describe("OpencodePlugin", () => { state.failing = false state.advertised = false - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(state.requests).toBe(8) expect(rebuilds).toEqual({ provider: initial.provider + 2, websearch: initial.websearch + 2 }) @@ -887,25 +889,25 @@ describe("OpencodePlugin", () => { {}, ]) { state.policies = [env, malformed] - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* consolePolicies(config)).toEqual([sudo]) expect(yield* providers.get(Provider.ID.opencode)).toBeDefined() } state.policies = [env, { action: "tool.use", resource: "shell:*", effect: "future-effect" }] - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* consolePolicies(config)).toEqual([env]) state.policies = [{ action: "future.use", future: { values: [true] } }] - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* consolePolicies(config)).toEqual([]) expect(yield* providers.get(Provider.ID.opencode)).toBeDefined() state.policies = [{ action: "future.use", effect: { next: true } }, env] - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* consolePolicies(config)).toEqual([env]) }), @@ -956,14 +958,14 @@ describe("OpencodePlugin", () => { expect(yield* status()).toBeUndefined() state.sso = true - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain const url = `${server.url.origin}/console/auth/sso/ssoconn%2F1/start?redirectTo=%2Fconsole%2F` expect(yield* status()).toEqual({ status: "needs_auth", message: "Sign in with SSO again to use Acme", url }) expect(yield* consolePolicies(config)).toEqual([sudo]) state.sso = false - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* status()).toBeUndefined() expect(yield* consolePolicies(config)).toEqual([sudo]) @@ -1087,7 +1089,7 @@ describe("OpencodePlugin", () => { () => Response.json({ _tag: "Forbidden", orgId: "org", connectionId: "ssoconn" }, { status: 403 }), ]) { state.body = body() - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect((yield* integrations.get(Integration.ID.make("opencode")))?.connections[0]?.status).toBeUndefined() expect(yield* consolePolicies(config)).toEqual([sudo]) @@ -1113,7 +1115,7 @@ describe("OpencodePlugin", () => { yield* addPlugin().pipe(Effect.provideService(HttpClient.HttpClient, http)) yield* drain state.status = 503 - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain yield* TestClock.adjust("10 seconds") yield* drain @@ -1249,7 +1251,7 @@ describe("OpencodePlugin", () => { expect((yield* credentials.get(credential.id))?.value).toMatchObject({ access: "revoked" }) state.revoked = false - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* status()).toBeUndefined() expect(state.refreshes).toBe(0) @@ -1258,6 +1260,40 @@ describe("OpencodePlugin", () => { ), ) + it.effect("adds 0-1 minute jitter on top of the 5-minute Console config poll interval", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const state = { requests: 0 } + const server = { + fetch: (request: Request) => { + if (new URL(request.url).pathname !== "/api/v2/config") return new Response(null, { status: 404 }) + state.requests++ + return Response.json({ providers: {} }) + }, + } + yield* credentials.create({ + integrationID: Integration.ID.make("opencode"), + value: Credential.Key.make({ type: "key", key: "secret", metadata: { server: "https://console.test" } }), + }) + yield* addPlugin().pipe( + inProcess(server), + Effect.provideService(Random.Random, { nextIntUnsafe: () => 0, nextDoubleUnsafe: () => 0.5 }), + ) + yield* drain + expect(state.requests).toBe(1) + + // 5 minutes + drain is still before 5m30s (base 5m + 30s jitter). + yield* TestClock.adjust("5 minutes") + yield* drain + expect(state.requests).toBe(1) + + // Crossing 5m30s triggers the jittered poll and refreshes SharedCache. + yield* TestClock.adjust("30 seconds") + yield* drain + expect(state.requests).toBe(2) + }), + ) + it.live("loads and executes hosted web search from the connected OpenCode server", () => Effect.acquireUseRelease( Effect.sync(() => { @@ -1735,13 +1771,13 @@ describe("OpencodePlugin", () => { // The config is unchanged, so config keeps the Console document it already holds. state.instruction = "Use spaces." - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* loaded).toEqual({ instructions: ["Use spaces."], skill: "Deploys the service." }) // A Console error keeps both, through the retries. state.lists = 503 - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain yield* TestClock.adjust("10 seconds") yield* drain @@ -1749,7 +1785,7 @@ describe("OpencodePlugin", () => { // A 404 means the workspace has none. state.lists = 404 - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* loaded).toEqual({ instructions: [], skill: undefined }) }), @@ -1796,12 +1832,12 @@ describe("OpencodePlugin", () => { expect(yield* instructions).toEqual([]) state.lists = "ok" - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* instructions).toEqual(["Use tabs."]) state.lists = "malformed" - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* instructions).toEqual(["Use tabs."]) expect(yield* consolePolicies(config)).toEqual([sudo]) @@ -2518,7 +2554,7 @@ describe("OpencodePlugin with a Console config", () => { expect(yield* websearch.default()).toBeUndefined() state.selected = true - yield* TestClock.adjust("1 minute") + yield* TestClock.adjust("6 minutes") yield* drain expect(yield* announced(opencode)).toEqual(opencode) expect(yield* websearch.providers()).toContainEqual(opencode) diff --git a/services/site/src/docs/content/policies.mdx b/services/site/src/docs/content/policies.mdx index 066303bf72b..9913f4605b3 100644 --- a/services/site/src/docs/content/policies.mdx +++ b/services/site/src/docs/content/policies.mdx @@ -315,12 +315,12 @@ Blocked by Acme's policy Statements follow the connected account. -| Event | Result | -| --------------------------- | -------------------------------------------------- | -| Console policy changes | Applied on the next fetch, about once a minute | -| Switch to another workspace | Statements replaced with that workspace's | -| Disconnect | Statements cleared | -| Console unreachable | Last statements for that connection stay in effect | +| Event | Result | +| --------------------------- | --------------------------------------------------- | +| Console policy changes | Applied on the next fetch, about every five minutes | +| Switch to another workspace | Statements replaced with that workspace's | +| Disconnect | Statements cleared | +| Console unreachable | Last statements for that connection stay in effect | The Console plugin and the policy plugin cannot be disabled from `plugins`; see [plugin control](/plugins#control).