mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 08:06:50 +00:00
fix: shorten Codex models ETag mapping TTL and clear it for upstream-less manifests(audit follow-up for #9199)
This commit is contained in:
1 parent
56e27df2d7
commit
385e07ca85
3 files changed
+99
-2
No files matched your search
@@ -29,7 +29,13 @@ const (
|
||||
codexModelsETagHeader = "x-models-etag"
|
||||
|
||||
codexModelsETagRegistryMaxEntries = 4096
|
||||
codexModelsETagRegistryTTL = 24 * time.Hour
|
||||
// The Codex client caches the catalog for 300s and only renews that cache
|
||||
// when the ETag is unchanged. The served manifest also depends on group
|
||||
// configuration and schedulable accounts, none of which change the upstream
|
||||
// ETag, so a recorded mapping can go stale. Ten minutes keeps the per-turn
|
||||
// full download away (at most one refresh per window) while bounding how
|
||||
// long a stale mapping can hide a changed catalog.
|
||||
codexModelsETagRegistryTTL = 10 * time.Minute
|
||||
)
|
||||
|
||||
type codexModelsETagKey struct {
|
||||
@@ -92,6 +98,20 @@ func (r *codexModelsETagRegistry) record(groupID int64, upstreamETags []string,
|
||||
r.evictLocked(now)
|
||||
}
|
||||
|
||||
// clearGroup drops every mapping of a group.
|
||||
func (r *codexModelsETagRegistry) clearGroup(groupID int64) {
|
||||
if r == nil || groupID <= 0 {
|
||||
return
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for key := range r.entries {
|
||||
if key.groupID == groupID {
|
||||
delete(r.entries, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *codexModelsETagRegistry) lookup(groupID int64, upstream string, now time.Time) (string, bool) {
|
||||
upstream = normalizeCodexModelsETag(upstream)
|
||||
if r == nil || groupID <= 0 || upstream == "" {
|
||||
@@ -154,11 +174,20 @@ func (r *OpenAIModelsResponse) upstreamCatalogETags() []string {
|
||||
// was served with for every upstream catalog ETag it was derived from. The
|
||||
// mapping lets codex.response.metadata events carry an ETag the client
|
||||
// recognizes. Verbatim manifests are recorded too, so their ETag is kept.
|
||||
// A manifest without any upstream ETag (for example one built from group
|
||||
// configuration alone) invalidates the group's earlier mappings: they describe
|
||||
// a catalog the group no longer serves, so later metadata events lose the
|
||||
// header instead of reporting a stale ETag.
|
||||
func (s *OpenAIGatewayService) RecordCodexModelsManifestETag(groupID int64, manifest *OpenAIModelsResponse) {
|
||||
if manifest == nil {
|
||||
return
|
||||
}
|
||||
codexModelsETags.record(groupID, manifest.upstreamCatalogETags(), manifest.ETag, time.Now())
|
||||
upstreamETags := manifest.upstreamCatalogETags()
|
||||
if len(upstreamETags) == 0 {
|
||||
codexModelsETags.clearGroup(groupID)
|
||||
return
|
||||
}
|
||||
codexModelsETags.record(groupID, upstreamETags, manifest.ETag, time.Now())
|
||||
}
|
||||
|
||||
// rewriteCodexResponseMetadataModelsETag translates the x-models-etag header of
|
||||
|
||||
@@ -158,3 +158,52 @@ func TestRecordCodexModelsManifestETagUsesEveryUpstreamSource(t *testing.T) {
|
||||
local := &OpenAIModelsResponse{ETag: `W/"local-only"`}
|
||||
require.Empty(t, local.upstreamCatalogETags())
|
||||
}
|
||||
|
||||
func TestCodexModelsETagRegistryTTLIsShort(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.Equal(t, 10*time.Minute, codexModelsETagRegistryTTL)
|
||||
|
||||
now := time.Unix(1_800_000_000, 0)
|
||||
registry := newCodexModelsETagRegistry(16, codexModelsETagRegistryTTL)
|
||||
registry.record(7, []string{`"u"`}, `"served"`, now)
|
||||
_, ok := registry.lookup(7, `"u"`, now.Add(9*time.Minute))
|
||||
require.True(t, ok)
|
||||
_, ok = registry.lookup(7, `"u"`, now.Add(11*time.Minute))
|
||||
require.False(t, ok, "mappings past the TTL must not be used")
|
||||
}
|
||||
|
||||
func TestCodexModelsETagRegistryClearGroup(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
now := time.Unix(1_800_000_000, 0)
|
||||
registry := newCodexModelsETagRegistry(16, time.Hour)
|
||||
registry.record(7, []string{`"a"`, `"b"`}, `"served-7"`, now)
|
||||
registry.record(8, []string{`"a"`}, `"served-8"`, now)
|
||||
registry.clearGroup(7)
|
||||
registry.clearGroup(0)
|
||||
(*codexModelsETagRegistry)(nil).clearGroup(7)
|
||||
|
||||
_, ok := registry.lookup(7, `"a"`, now)
|
||||
require.False(t, ok)
|
||||
_, ok = registry.lookup(7, `"b"`, now)
|
||||
require.False(t, ok)
|
||||
served, ok := registry.lookup(8, `"a"`, now)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, `"served-8"`, served)
|
||||
}
|
||||
|
||||
func TestRecordCodexModelsManifestETagWithoutUpstreamClearsGroup(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const groupID int64 = 77_002
|
||||
svc := &OpenAIGatewayService{}
|
||||
svc.RecordCodexModelsManifestETag(groupID, &OpenAIModelsResponse{ETag: `W/"scheduled"`, upstreamETag: `W/"up"`})
|
||||
_, ok := codexModelsETags.lookup(groupID, `W/"up"`, time.Now())
|
||||
require.True(t, ok)
|
||||
|
||||
// Group switched to the configuration branch: no upstream ETag.
|
||||
svc.RecordCodexModelsManifestETag(groupID, &OpenAIModelsResponse{ETag: `W/"configured"`})
|
||||
_, ok = codexModelsETags.lookup(groupID, `W/"up"`, time.Now())
|
||||
require.False(t, ok, "stale mapping must be dropped")
|
||||
}
|
||||
@@ -177,3 +177,22 @@ func TestCodexResponseMetadataUnknownModelsETagIsRemovedOverWebSocket(t *testing
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodexResponseMetadataETagNotRewrittenAfterConfiguredManifestOverWebSocket(t *testing.T) {
|
||||
const groupID int64 = 96_031
|
||||
svc := &OpenAIGatewayService{}
|
||||
svc.RecordCodexModelsManifestETag(groupID, &OpenAIModelsResponse{
|
||||
ETag: `W/"scheduled-manifest"`,
|
||||
upstreamETag: `W/"upstream-catalog"`,
|
||||
})
|
||||
// The group is now served from its configuration: no upstream ETag.
|
||||
svc.RecordCodexModelsManifestETag(groupID, &OpenAIModelsResponse{ETag: `W/"configured-manifest"`})
|
||||
|
||||
events := runCodexMetadataWSTurn(t, false, groupID, [][]byte{
|
||||
[]byte(`{"type":"codex.response.metadata","headers":{"x-models-etag":"W/\"upstream-catalog\"","x-codex-other":"kept"}}`),
|
||||
[]byte(`{"type":"response.completed","response":{"id":"resp_metadata_configured","model":"gpt-5.1","usage":{"input_tokens":1,"output_tokens":1}}}`),
|
||||
})
|
||||
metadata := codexMetadataEvent(t, events)
|
||||
require.False(t, gjson.GetBytes(metadata, "headers.x-models-etag").Exists())
|
||||
require.Equal(t, "kept", gjson.GetBytes(metadata, "headers.x-codex-other").String())
|
||||
}
|
||||
Reference in new issue
Block a user