Skip to content

Commit 23e3493

Browse files
opencode-agent[bot]neriousyrekram1-node
authored
fix(update): cache D1 reads and use partial indexes for lookups (#55109)
Co-authored-by: neriousy <[email protected]> Co-authored-by: Aiden Cline <[email protected]>
1 parent 82833da commit 23e3493

2 files changed

Lines changed: 186 additions & 20 deletions

File tree

‎services/update/src/index.ts‎

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const select =
3636
"SELECT channel, name, distribution, version, metadata, active, minimum, time_created, time_updated FROM artifact"
3737
const audience = "https://update.opencode.ai"
3838
const githubKeys = createRemoteJWKSet(new URL("https://token.actions.githubusercontent.com/.well-known/jwks"))
39+
// Clients poll every lookup path, and querying D1 on each request overloaded it.
40+
// Cache the caller-independent reads per isolate; caller-specific selection still runs per request.
41+
const readTtl = 60_000
42+
const reads = new Map<string, { expires: number; value: Promise<unknown> }>()
3943

4044
export default {
4145
async fetch(request: Request, env: Env, ctx: Pick<ExecutionContext, "waitUntil">): Promise<Response> {
@@ -91,9 +95,11 @@ async function lookup(request: Request, env: Env, url: URL, path: string[]) {
9195
const current = url.searchParams.get("current") ?? agent?.[2] ?? agent?.[3]
9296
const source = agent?.[1] ?? current?.match(/^v?0\.0\.0-(.+)-\d+(?:\.\d+)?(?:\+.*)?$/)?.[1]
9397
const caller = source === undefined || resolveChannel(source) === resolved ? current : undefined
94-
const rollout = await env.DB.prepare("SELECT duration_hours FROM channel_rollout WHERE channel = ?")
95-
.bind(resolved)
96-
.first<{ duration_hours: number }>()
98+
const rollout = await cachedRead(`rollout:${resolved}`, () =>
99+
env.DB.prepare("SELECT duration_hours FROM channel_rollout WHERE channel = ?")
100+
.bind(resolved)
101+
.first<{ duration_hours: number }>(),
102+
)
97103
const ip = request.headers.get("CF-Connecting-IP")
98104
const hash =
99105
rollout?.duration_hours && ip
@@ -111,11 +117,8 @@ async function lookup(request: Request, env: Env, url: URL, path: string[]) {
111117
}
112118

113119
async function channel(db: D1Database, channel: string, current: string | undefined, cutoff: number) {
114-
const result = await db
115-
.prepare(`${select} WHERE channel = ? AND (active = 1 OR minimum = 1) ORDER BY name, distribution`)
116-
.bind(channel)
117-
.all<ArtifactRow>()
118-
const artifacts = await selectArtifacts(db, result.results, current, cutoff)
120+
const rows = await candidates(db, "channel = ?", [channel])
121+
const artifacts = await selectArtifacts(db, rows, current, cutoff)
119122
if (!artifacts.length) return json({ error: "Channel not found" }, 404)
120123
return updateResponse({ channel, artifacts: artifacts.map(decodeArtifact) })
121124
}
@@ -127,11 +130,8 @@ async function artifactName(
127130
current: string | undefined,
128131
cutoff: number,
129132
) {
130-
const result = await db
131-
.prepare(`${select} WHERE channel = ? AND name = ? AND (active = 1 OR minimum = 1) ORDER BY distribution`)
132-
.bind(channel, name)
133-
.all<ArtifactRow>()
134-
const artifacts = await selectArtifacts(db, result.results, current, cutoff)
133+
const rows = await candidates(db, "channel = ? AND name = ?", [channel, name])
134+
const artifacts = await selectArtifacts(db, rows, current, cutoff)
135135
if (!artifacts.length) return json({ error: "Artifact not found" }, 404)
136136
return updateResponse({ channel, name, artifacts: artifacts.map(decodeArtifact) })
137137
}
@@ -145,11 +145,8 @@ async function artifactDistribution(
145145
cutoff: number,
146146
manifest?: string,
147147
) {
148-
const result = await db
149-
.prepare(`${select} WHERE channel = ? AND name = ? AND distribution = ? AND (active = 1 OR minimum = 1)`)
150-
.bind(channel, name, distribution)
151-
.all<ArtifactRow>()
152-
const artifact = (await selectArtifacts(db, result.results, current, cutoff))[0]
148+
const rows = await candidates(db, "channel = ? AND name = ? AND distribution = ?", [channel, name, distribution])
149+
const artifact = (await selectArtifacts(db, rows, current, cutoff))[0]
153150
if (!artifact) return json({ error: "Artifact not found" }, 404)
154151
if (manifest) {
155152
const metadata = decodeMetadata(artifact.metadata)
@@ -167,6 +164,23 @@ async function artifactDistribution(
167164
return updateResponse(decodeArtifact(artifact))
168165
}
169166

167+
// `active = 1 OR minimum = 1` scanned every published build under the primary-key prefix.
168+
// Reading each partial index separately keeps the read proportional to the selected artifacts.
169+
function candidates(db: D1Database, filter: string, values: string[]) {
170+
return cachedRead(`artifacts:${values.join("/")}`, () =>
171+
db
172+
.prepare(
173+
`${select} INDEXED BY artifact_active WHERE ${filter} AND active = 1
174+
UNION ALL
175+
${select} INDEXED BY artifact_minimum WHERE ${filter} AND minimum = 1 AND active = 0
176+
ORDER BY name, distribution`,
177+
)
178+
.bind(...values, ...values)
179+
.all<ArtifactRow>()
180+
.then((result) => result.results),
181+
)
182+
}
183+
170184
async function selectArtifacts(db: D1Database, rows: ArtifactRow[], current: string | undefined, cutoff: number) {
171185
const caller = current === undefined ? undefined : releaseVersion(current)
172186
const artifacts = await Promise.all(
@@ -537,6 +551,35 @@ export function resolveChannel(channel: string) {
537551
return channel === "next" ? "beta" : channel
538552
}
539553

554+
function cachedRead<T>(key: string, load: () => Promise<T>) {
555+
const now = Date.now()
556+
const hit = reads.get(key)
557+
if (hit) {
558+
reads.delete(key)
559+
if (hit.expires > now) {
560+
reads.set(key, hit)
561+
return hit.value as Promise<T>
562+
}
563+
}
564+
// Lookup paths are caller-controlled identifiers, so bound the number of retained entries.
565+
if (reads.size >= 1_000) {
566+
reads.forEach((entry, k) => {
567+
if (entry.expires <= now) reads.delete(k)
568+
})
569+
if (reads.size >= 1_000) {
570+
const oldest = reads.keys().next().value
571+
if (oldest !== undefined) reads.delete(oldest)
572+
}
573+
}
574+
// Concurrent callers share one in-flight read. Failed reads are dropped so the next request retries D1.
575+
const value = load()
576+
reads.set(key, { expires: now + readTtl, value })
577+
value.catch(() => {
578+
if (reads.get(key)?.value === value) reads.delete(key)
579+
})
580+
return value
581+
}
582+
540583
function validMutation(request: Request) {
541584
const origin = request.headers.get("Origin")
542585
if (origin && origin !== new URL(request.url).origin) return json({ error: "Invalid origin" }, 403)

‎services/update/test/index.test.ts‎

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,64 @@
1-
import { expect, test } from "bun:test"
1+
import { Database } from "bun:sqlite"
2+
import { afterEach, expect, setSystemTime, test } from "bun:test"
3+
import { readdirSync, readFileSync } from "node:fs"
4+
import path from "node:path"
25
import worker from "../src/index"
36

7+
type Env = Parameters<typeof worker.fetch>[1]
8+
9+
const migrations = path.join(import.meta.dir, "../migrations")
10+
11+
afterEach(() => setSystemTime())
12+
13+
// Runs the real schema and queries through bun:sqlite and records every statement sent to D1.
14+
function database() {
15+
const sqlite = new Database(":memory:")
16+
readdirSync(migrations)
17+
.sort()
18+
.forEach((file) => sqlite.exec(readFileSync(path.join(migrations, file), "utf8")))
19+
const queries: string[] = []
20+
const DB = {
21+
prepare: (sql: string) => ({
22+
bind: (...values: unknown[]) => ({
23+
first: async () => {
24+
queries.push(sql)
25+
return sqlite.query(sql).get(...(values as never[])) ?? null
26+
},
27+
all: async () => {
28+
queries.push(sql)
29+
return { results: sqlite.query(sql).all(...(values as never[])) }
30+
},
31+
}),
32+
}),
33+
}
34+
const env = { DB, EVENTS: { send: () => Promise.resolve() } } as unknown as Env
35+
const insert = (input: { channel: string; version: string; time: number; active?: boolean; minimum?: boolean }) =>
36+
sqlite
37+
.query(
38+
"INSERT INTO artifact (channel, name, distribution, version, metadata, active, minimum, time_created, time_updated) VALUES (?, 'cli', 'opencode', ?, '{}', ?, ?, ?, ?)",
39+
)
40+
.run(input.channel, input.version, input.active ? 1 : 0, input.minimum ? 1 : 0, input.time, input.time)
41+
return { sqlite, env, queries, insert }
42+
}
43+
44+
function lookup(env: Env, pathname: string, headers?: HeadersInit) {
45+
return worker.fetch(new Request(`https://opencode.ai/update/api/${pathname}`, { headers }), env, {
46+
waitUntil: () => {},
47+
})
48+
}
49+
50+
async function version(response: Response) {
51+
expect(response.status).toBe(200)
52+
const body = (await response.json()) as { version: string }
53+
return body.version
54+
}
55+
456
test("returns a retryable 503 when D1 rejects a lookup", async () => {
557
const overloaded = new Error("D1_ERROR: D1 DB is overloaded. Requests queued for too long.")
658
const env = {
759
DB: { prepare: () => ({ bind: () => ({ first: () => Promise.reject(overloaded) }) }) },
860
EVENTS: { send: () => Promise.resolve() },
9-
} as unknown as Parameters<typeof worker.fetch>[1]
61+
} as unknown as Env
1062
const response = await worker.fetch(
1163
new Request("https://opencode.ai/update/api/latest/desktop/opencode/opencode-desktop-win-x64.exe"),
1264
env,
@@ -17,3 +69,74 @@ test("returns a retryable 503 when D1 rejects a lookup", async () => {
1769
expect(response.headers.get("Cache-Control")).toBe("no-store")
1870
expect(await response.json()).toEqual({ error: "Update service temporarily unavailable" })
1971
})
72+
73+
test("reuses D1 reads across lookups for 1 minute", async () => {
74+
const db = database()
75+
db.insert({ channel: "reuse", version: "1.0.0", time: 1, active: true })
76+
setSystemTime(new Date("2026-10-13T00:00:00Z"))
77+
78+
expect(
79+
await Promise.all([lookup(db.env, "reuse/cli/opencode"), lookup(db.env, "reuse/cli/opencode")]).then((responses) =>
80+
Promise.all(responses.map(version)),
81+
),
82+
).toEqual(["1.0.0", "1.0.0"])
83+
expect(db.queries).toHaveLength(2)
84+
85+
db.sqlite.query("UPDATE artifact SET active = 0").run()
86+
db.insert({ channel: "reuse", version: "1.0.1", time: 2, active: true })
87+
setSystemTime(new Date("2026-10-13T00:00:59Z"))
88+
expect(await version(await lookup(db.env, "reuse/cli/opencode"))).toBe("1.0.0")
89+
expect(db.queries).toHaveLength(2)
90+
91+
setSystemTime(new Date("2026-10-13T00:01:01Z"))
92+
expect(await version(await lookup(db.env, "reuse/cli/opencode"))).toBe("1.0.1")
93+
expect(db.queries).toHaveLength(4)
94+
})
95+
96+
test("evicts stale and cold entries without clearing hot reads", async () => {
97+
const db = database()
98+
db.insert({ channel: "hot", version: "1.0.0", time: 1, active: true })
99+
setSystemTime(new Date("2026-10-13T01:00:00Z"))
100+
101+
expect(await version(await lookup(db.env, "hot/cli/opencode"))).toBe("1.0.0")
102+
expect(db.queries).toHaveLength(2)
103+
104+
for (let i = 0; i < 600; i++) {
105+
expect((await lookup(db.env, `cold-${i}/cli/opencode`)).status).toBe(404)
106+
expect(await version(await lookup(db.env, "hot/cli/opencode"))).toBe("1.0.0")
107+
}
108+
expect(db.queries).toHaveLength(1202)
109+
})
110+
111+
test("retries D1 on the next lookup after a failed read", async () => {
112+
const db = database()
113+
db.insert({ channel: "retry", version: "1.0.0", time: 1, active: true })
114+
const prepare = db.env.DB.prepare
115+
let fail = true
116+
db.env.DB.prepare = ((sql: string) =>
117+
fail
118+
? { bind: () => ({ first: () => Promise.reject(new Error("overloaded")) }) }
119+
: prepare.call(db.env.DB, sql)) as typeof prepare
120+
121+
expect((await lookup(db.env, "retry/cli/opencode")).status).toBe(503)
122+
fail = false
123+
expect(await version(await lookup(db.env, "retry/cli/opencode"))).toBe("1.0.0")
124+
})
125+
126+
test("selects the minimum and active artifacts from the partial indexes", async () => {
127+
const db = database()
128+
db.insert({ channel: "minimum", version: "1.0.0", time: 1, minimum: true })
129+
db.insert({ channel: "minimum", version: "1.5.0", time: 2 })
130+
db.insert({ channel: "minimum", version: "2.0.0", time: 3, active: true })
131+
132+
expect(await version(await lookup(db.env, "minimum/cli/opencode?current=0.9.0"))).toBe("1.0.0")
133+
expect(await version(await lookup(db.env, "minimum/cli/opencode?current=1.0.0"))).toBe("2.0.0")
134+
expect(await version(await lookup(db.env, "minimum/cli/opencode"))).toBe("2.0.0")
135+
136+
// A release that is both active and minimum is listed once.
137+
db.insert({ channel: "both", version: "3.0.0", time: 1, active: true, minimum: true })
138+
const response = await lookup(db.env, "both")
139+
expect(response.status).toBe(200)
140+
const body = (await response.json()) as { artifacts: { version: string }[] }
141+
expect(body.artifacts.map((artifact) => artifact.version)).toEqual(["3.0.0"])
142+
})

0 commit comments

Comments
 (0)