mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): stop failing model catalog fetch spans on 304 cache hits (#60953)
This commit is contained in:
committed by
GitHub
Unverified
parent
b95c1f379c
commit
d50d485842
@@ -1,5 +1,5 @@
|
||||
import { Cause, Context, Duration, Effect, Layer, Option, Result, Schedule, Schema, Semaphore } from "effect"
|
||||
import { HttpClient, HttpClientError, HttpClientRequest } from "effect/http"
|
||||
import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import { ConfigProvider } from "@opencode/schema/config/provider"
|
||||
import { MaxTokensField, MediaInput } from "@opencode/schema/model-compatibility"
|
||||
import { ModelsDev } from "@opencode/schema/models-dev"
|
||||
@@ -535,14 +535,12 @@ export const layer = (options?: Options) =>
|
||||
const bus = yield* Bus.Service
|
||||
const app = yield* App.Metadata
|
||||
const kv = yield* KV.Service
|
||||
const http = HttpClient.filterStatusOk(
|
||||
(yield* HttpClient.HttpClient).pipe(
|
||||
HttpClient.retryTransient({
|
||||
retryOn: "errors-and-responses",
|
||||
times: 2,
|
||||
schedule: Schedule.exponential(200).pipe(Schedule.jittered),
|
||||
}),
|
||||
),
|
||||
const http = (yield* HttpClient.HttpClient).pipe(
|
||||
HttpClient.retryTransient({
|
||||
retryOn: "errors-and-responses",
|
||||
times: 2,
|
||||
schedule: Schedule.exponential(200).pipe(Schedule.jittered),
|
||||
}),
|
||||
)
|
||||
|
||||
const source = options?.url || defaultSource
|
||||
@@ -575,7 +573,13 @@ export const layer = (options?: Options) =>
|
||||
HttpClientRequest.setHeader("User-Agent", userAgent),
|
||||
HttpClientRequest.setHeaders(etag ? { "If-None-Match": etag } : {}),
|
||||
http.execute,
|
||||
Effect.flatMap((res) => Effect.map(res.text, (text) => ({ text, etag: res.headers.etag }))),
|
||||
Effect.flatMap((res) =>
|
||||
etag && res.status === 304
|
||||
? Effect.undefined
|
||||
: HttpClientResponse.filterStatusOk(res).pipe(
|
||||
Effect.flatMap((ok) => Effect.map(ok.text, (text) => ({ text, etag: ok.headers.etag }))),
|
||||
),
|
||||
),
|
||||
Effect.timeout("10 seconds"),
|
||||
)
|
||||
|
||||
@@ -625,6 +629,7 @@ export const layer = (options?: Options) =>
|
||||
|
||||
const fetchAndWrite = Effect.fn("ModelsDev.fetchAndWrite")(function* () {
|
||||
const response = yield* fetchApi()
|
||||
if (!response) return {}
|
||||
const catalog = yield* decodeCatalog(response.text)
|
||||
yield* writeCache(response.text, response.etag)
|
||||
return catalog
|
||||
@@ -660,12 +665,7 @@ export const layer = (options?: Options) =>
|
||||
Schema.decodeUnknownOption(CacheMeta)((yield* kv.get(metaKey)) ?? (yield* kv.get(key))),
|
||||
)
|
||||
if (!force && stored && Date.now() - stored.updatedAt < Duration.toMillis(ttl)) return
|
||||
const response = yield* fetchApi(force ? undefined : stored?.etag).pipe(
|
||||
Effect.catchIf(
|
||||
(error) => HttpClientError.isHttpClientError(error) && error.response?.status === 304,
|
||||
() => Effect.undefined,
|
||||
),
|
||||
)
|
||||
const response = yield* fetchApi(force ? undefined : stored?.etag)
|
||||
const digest = response && bodyDigest(response.text)
|
||||
// models.dev rarely changes between polls; skip invalidation and
|
||||
// the Refreshed event for a byte-identical body so downstream
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Money } from "@opencode/schema/money"
|
||||
import { Effect, Fiber, Layer, Ref, Scope, Stream } from "effect"
|
||||
import { Effect, Exit, Fiber, Layer, Ref, Scope, Stream, Tracer } from "effect"
|
||||
import { HttpClient, HttpClientResponse } from "effect/http"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { LayerNodePlatform } from "@opencode/util/effect/app-node-platform"
|
||||
@@ -994,6 +994,14 @@ describe("ModelsDev Service", () => {
|
||||
|
||||
it.live("refresh(false) revalidates with the stored ETag and treats 304 as unchanged", () =>
|
||||
Effect.gen(function* () {
|
||||
const spans: Tracer.NativeSpan[] = []
|
||||
const tracer = Tracer.make({
|
||||
span(options) {
|
||||
const span = new Tracer.NativeSpan(options)
|
||||
spans.push(span)
|
||||
return span
|
||||
},
|
||||
})
|
||||
const cache = makeCache()
|
||||
const state = yield* Ref.make<MockState>({ ...initialState, etag: 'W/"v1"' })
|
||||
const body = yield* provided(
|
||||
@@ -1006,6 +1014,7 @@ describe("ModelsDev Service", () => {
|
||||
cache.values.set(metaKey, { ...meta, updatedAt: Date.now() - 10 * 60 * 1000 })
|
||||
const body = cache.values.get(cacheKey)
|
||||
yield* Ref.update(state, (s) => ({ ...s, status: 304 }))
|
||||
spans.length = 0
|
||||
const bus = yield* Bus.Service
|
||||
const event = yield* bus.subscribe(ModelsDev.Event.Refreshed).pipe(
|
||||
Stream.take(1),
|
||||
@@ -1022,13 +1031,36 @@ describe("ModelsDev Service", () => {
|
||||
expect(event._tag).toBe("None")
|
||||
expect(yield* svc.get()).toEqual(fixtureSnapshot)
|
||||
return body
|
||||
}),
|
||||
}).pipe(Effect.provideService(Tracer.Tracer, tracer)),
|
||||
)
|
||||
const final = yield* Ref.get(state)
|
||||
expect(final.calls.map((call) => call.ifNoneMatch)).toEqual([undefined, 'W/"v1"'])
|
||||
expect(cache.values.get(cacheKey)).toBe(body)
|
||||
expect(cache.values.get(metaKey)).toMatchObject({ etag: 'W/"v1"' })
|
||||
expect(Date.now() - (cache.values.get(metaKey) as { updatedAt: number }).updatedAt).toBeLessThan(60 * 1000)
|
||||
const fetchSpans = spans.filter((span) => span.name === "ModelsDev.fetchApi")
|
||||
expect(fetchSpans).toHaveLength(1)
|
||||
for (const span of spans.filter((span) => span.name.startsWith("ModelsDev."))) {
|
||||
if (span.status._tag === "Ended") {
|
||||
expect(Exit.isFailure(span.status.exit)).toBe(false)
|
||||
}
|
||||
}
|
||||
|
||||
spans.length = 0
|
||||
yield* provided(
|
||||
state,
|
||||
cache,
|
||||
Effect.gen(function* () {
|
||||
const svc = yield* ModelsDev.Service
|
||||
yield* svc.refresh(true)
|
||||
expect(yield* svc.get()).toEqual(fixtureSnapshot)
|
||||
}).pipe(Effect.provideService(Tracer.Tracer, tracer)),
|
||||
)
|
||||
const unrequested304 = spans.find((span) => span.name === "ModelsDev.fetchApi")
|
||||
expect(unrequested304?.status._tag).toBe("Ended")
|
||||
if (unrequested304?.status._tag === "Ended") {
|
||||
expect(Exit.isFailure(unrequested304.status.exit)).toBe(true)
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user