From f7f64e1b153e6045fcb656e1d3a59eedaf0a5718 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 13:53:37 +0800 Subject: [PATCH 01/12] feat(accounts): sync selected whitelists through individual account requests --- .github/scripts/batch-whitelist-ui.mjs | 96 +++++++++++++++++++ Makefile | 1 + frontend/src/api/admin/accounts.ts | 1 + .../account/BatchUpstreamWhitelist.vue | 47 +++++++++ .../account/BulkEditAccountModal.vue | 9 ++ .../__tests__/BatchUpstreamWhitelist.spec.ts | 61 ++++++++++++ .../src/i18n/locales/en/admin/accounts.ts | 7 ++ .../src/i18n/locales/zh/admin/accounts.ts | 7 ++ frontend/src/views/admin/AccountsView.vue | 4 + 9 files changed, 233 insertions(+) create mode 100644 .github/scripts/batch-whitelist-ui.mjs create mode 100644 frontend/src/components/account/BatchUpstreamWhitelist.vue create mode 100644 frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts diff --git a/.github/scripts/batch-whitelist-ui.mjs b/.github/scripts/batch-whitelist-ui.mjs new file mode 100644 index 000000000000..7c187876f955 --- /dev/null +++ b/.github/scripts/batch-whitelist-ui.mjs @@ -0,0 +1,96 @@ +// Run from frontend/. The Vite dev server serves the real modal, locales and Tailwind styles. +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { readFileSync, writeFileSync, rmSync, mkdirSync } from 'node:fs' +import { createRequire } from 'node:module' +import { resolve, join } from 'node:path' +import { pathToFileURL } from 'node:url' + +const [mode, out] = process.argv.slice(2) +assert.ok(['before', 'after'].includes(mode) && out) +mkdirSync(out, { recursive: true }) +const require = createRequire(resolve('package.json')) +const { createServer } = await import(pathToFileURL(require.resolve('vite').replace(/index\.cjs$/, 'dist/node/index.js'))) +// Resolve the browser from an isolated installation, outside the application. +const { chromium } = createRequire(resolve(process.env.PLAYWRIGHT_ROOT, 'package.json'))('playwright') +const vue = require('@vitejs/plugin-vue') +const html = 'batch-whitelist-evidence.html' +const entry = 'batch-whitelist-evidence.js' +const sha = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim() +const manifest = { sha, mode, screenshots: [], fixture: 'Real BulkEditAccountModal and CSS. Synthetic admin, selected IDs and Axios adapter.' } +let browser, server +try { + writeFileSync(html, '
', { flag: 'wx' }) + writeFileSync(entry, ` + import {createApp, h} from 'vue' + import {createPinia} from 'pinia' + import Modal from './src/components/account/BulkEditAccountModal.vue' + import {i18n, loadLocaleMessages} from './src/i18n' + import {useAuthStore} from './src/stores/auth' + import {apiClient} from './src/api/client' + import './src/style.css' + await loadLocaleMessages('zh') + i18n.global.locale.value='zh' + window.calls=[] + apiClient.defaults.adapter=async config=>{ + const url=config.url + let data={} + if(url.endsWith('/models/sync-upstream')){ + window.calls.push(url) + data=url.includes('/42/') && window.calls.length===2 ? {models:[],skipped:'empty'} : {models:['synthetic'],applied:true,changed:true} + } + return {data:{code:0,data},status:200,statusText:'OK',headers:{},config} + } + const pinia=createPinia() + useAuthStore(pinia).user={id:9,role:'admin'} + createApp({render:()=>h(Modal,{show:true,accountIds:[41,42],selectedPlatforms:['anthropic'],selectedTypes:['apikey'],groups:[],proxies:[]})}).use(pinia).use(i18n).mount('#app') + `, { flag: 'wx' }) + server = await createServer({ configFile: false, plugins: [vue()], cacheDir: join(out, 'vite-cache'), + resolve: { alias: { '@': resolve('src') } }, server: { host: '127.0.0.1', port: 0 } }) + await server.listen() + browser = await chromium.launch() + for (const [name, viewport] of [['desktop',{width:1366,height:900}],['mobile',{width:390,height:844}]]) { + const page = await browser.newPage({viewport}) + const errors = [] + page.on('pageerror', e => errors.push(e.message)) + await page.route('**/*', route => new URL(route.request().url()).hostname === '127.0.0.1' ? route.continue() : route.abort()) + await page.goto(server.resolvedUrls.local[0] + html) + await page.locator('#bulk-edit-model-restriction-enabled').check() + const shared = page.getByRole('button', {name:'同步最新支持模型',exact:true}) + const batch = page.getByRole('button', {name:'批量同步上游模型',exact:true}) + assert.equal(await batch.count(), mode === 'after' ? 1 : 0) + const focus = mode === 'before' ? shared : batch + await focus.evaluate(el => el.scrollIntoView({block:'center',behavior:'instant'})) + await page.evaluate(async () => { + await document.fonts.ready + await Promise.all(document.getAnimations().filter(a=>a.effect.getTiming().iterations!==Infinity).map(a=>a.finished.catch(()=>{}))) + }) + if (mode === 'after') { + await batch.click() + await page.getByRole('button',{name:'仅重试失败账号'}).waitFor() + } + const box = await focus.boundingBox() + assert.ok(box && box.x >= 0 && box.x + box.width <= viewport.width && box.y > 0 && box.y + box.height < viewport.height) + assert.ok(await focus.evaluate(el => { + const b=el.getBoundingClientRect() + return el.contains(document.elementFromPoint(b.x+b.width/2,b.y+b.height/2)) + })) + assert.deepEqual(errors, []) + const path = join(out, mode+'-'+name+'.png') + await page.screenshot({path}) + if(mode === 'after') { + await page.getByRole('button',{name:'仅重试失败账号'}).click() + await page.waitForFunction(()=>window.calls.length===3) + assert.deepEqual(await page.evaluate(()=>window.calls),[41,42,42].map(id=>'/admin/accounts/'+id+'/models/sync-upstream')) + } + manifest.screenshots.push({name,viewport,box,sha256:createHash('sha256').update(readFileSync(path)).digest('hex')}) + await page.close() + } + writeFileSync(join(out, mode+'-manifest.json'), JSON.stringify(manifest,null,2)+'\n') +} finally { + await browser?.close() + await server?.close() + rmSync(entry,{force:true}) + rmSync(html,{force:true}) +} diff --git a/Makefile b/Makefile index d9514a622215..4d3c4778c2fc 100644 --- a/Makefile +++ b/Makefile @@ -124,6 +124,7 @@ FRONTEND_CRITICAL_VITEST := \ src/components/account/__tests__/OpenAIReferralCell.transport.spec.ts \ src/components/account/__tests__/OpenAIQuotaResetCell.spark_shadow.spec.ts \ src/components/account/__tests__/ExcelBPS403Badge.spec.ts \ + src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts \ src/components/account/__tests__/BulkEditAccountModal.spec.ts \ src/constants/__tests__/platforms.spec.ts \ src/components/account/__tests__/credentialsBuilder.platformCatalog.spec.ts \ diff --git a/frontend/src/api/admin/accounts.ts b/frontend/src/api/admin/accounts.ts index 2e23f077428e..6b2b04a7bfb9 100644 --- a/frontend/src/api/admin/accounts.ts +++ b/frontend/src/api/admin/accounts.ts @@ -648,6 +648,7 @@ export interface SyncUpstreamModelsResult { } export interface SyncUpstreamModelsOptions { + whitelist_only?: boolean apply?: boolean automatic?: boolean } diff --git a/frontend/src/components/account/BatchUpstreamWhitelist.vue b/frontend/src/components/account/BatchUpstreamWhitelist.vue new file mode 100644 index 000000000000..f2667e868763 --- /dev/null +++ b/frontend/src/components/account/BatchUpstreamWhitelist.vue @@ -0,0 +1,47 @@ + + + diff --git a/frontend/src/components/account/BulkEditAccountModal.vue b/frontend/src/components/account/BulkEditAccountModal.vue index a43c1d2a9f01..7b217fe5e089 100644 --- a/frontend/src/components/account/BulkEditAccountModal.vue +++ b/frontend/src/components/account/BulkEditAccountModal.vue @@ -408,6 +408,13 @@ /> +
diff --git a/frontend/src/components/account/BulkEditAccountModal.vue b/frontend/src/components/account/BulkEditAccountModal.vue index 7b217fe5e089..5ceadacbc34d 100644 --- a/frontend/src/components/account/BulkEditAccountModal.vue +++ b/frontend/src/components/account/BulkEditAccountModal.vue @@ -402,6 +402,7 @@
' + props: ['show', 'target', 'filteredPreview'], + template: '
' } const makeProbeAccount = (id: number, rate = 0.25) => ({ @@ -328,9 +328,28 @@ describe('admin AccountsView bulk edit scope', () => { expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-show')).toBe('true') expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-target-mode')).toBe('selected') + expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-filter-preview')).toBe('true') expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-target-ids')).toBe('[7,11]') }) + it('passes only the explicitly checked IDs across pages to the upstream action', async () => { + listAccounts.mockImplementation(async (page: number) => + page === 2 ? probePage([21, 22], 22, 2) : probePage([7, 8], 22, 1)) + const wrapper = mountProbeView({ BulkEditAccountModal: BulkEditAccountModalStub }) + await flushPromises() + await wrapper.get('[data-account-id="7"] [data-test="select-row"] input').trigger('change') + await wrapper.get('[data-test="next-page"]').trigger('click') + await flushPromises() + await wrapper.get('[data-account-id="22"] [data-test="select-row"] input').trigger('change') + await wrapper.get('[data-test="edit-filtered"]').trigger('click') + const modal = wrapper.get('[data-test="bulk-edit-modal"]') + expect(modal.attributes('data-filter-preview')).toBe('false') + expect(modal.attributes('data-target-ids')).toBe('[7,22]') + await wrapper.get('[data-account-id="21"] [data-test="select-row"] input').trigger('change') + expect(modal.attributes('data-target-ids')).toBe('[7,22]') + wrapper.unmount() + }) + it('keeps checked OAuth IDs when a stale filtered-edit action fires', async () => { const page = probePage([7, 11, 13]) page.items[0] = { ...page.items[0], type: 'oauth' } From 0867559e3e4c2883912bf682f52e2d066f1bd486 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 14:09:31 +0800 Subject: [PATCH 05/12] fix(accounts): serialize form preflight and isolate PostgreSQL API evidence --- .github/scripts/batch-whitelist-ui.mjs | 1 + .../workflows/batch-upstream-whitelist.yml | 4 +- ...ndler_batch_whitelist_integration_test.go} | 32 ++++++++++--- .../account-batch-upstream-whitelist.md | 36 ++++++++++++++ .../account/BulkEditAccountModal.vue | 27 +++++++---- .../__tests__/BatchUpstreamWhitelist.spec.ts | 48 ++++++++++++++++++- .../src/i18n/locales/en/admin/accounts.ts | 2 +- .../src/i18n/locales/zh/admin/accounts.ts | 2 +- 8 files changed, 130 insertions(+), 22 deletions(-) rename backend/internal/{repository/account_repo_batch_whitelist_integration_test.go => handler/admin/account_handler_batch_whitelist_integration_test.go} (74%) create mode 100644 docs/features/account-batch-upstream-whitelist.md diff --git a/.github/scripts/batch-whitelist-ui.mjs b/.github/scripts/batch-whitelist-ui.mjs index 7c187876f955..d227425827ad 100644 --- a/.github/scripts/batch-whitelist-ui.mjs +++ b/.github/scripts/batch-whitelist-ui.mjs @@ -17,6 +17,7 @@ const { chromium } = createRequire(resolve(process.env.PLAYWRIGHT_ROOT, 'package const vue = require('@vitejs/plugin-vue') const html = 'batch-whitelist-evidence.html' const entry = 'batch-whitelist-evidence.js' +execFileSync('git', ['diff', '--quiet', 'HEAD', '--', 'src']) const sha = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim() const manifest = { sha, mode, screenshots: [], fixture: 'Real BulkEditAccountModal and CSS. Synthetic admin, selected IDs and Axios adapter.' } let browser, server diff --git a/.github/workflows/batch-upstream-whitelist.yml b/.github/workflows/batch-upstream-whitelist.yml index 1ec226b47242..6e503c120e52 100644 --- a/.github/workflows/batch-upstream-whitelist.yml +++ b/.github/workflows/batch-upstream-whitelist.yml @@ -66,7 +66,7 @@ jobs: working-directory: backend run: | set -o pipefail - go test -v -tags=integration ./internal/repository -run '^Test(BatchWhitelistPostgresHandlerReadback|ApplyUpstreamModelsPreservesCredentialsAndInvalidatesScheduler)$' -count=1 | tee "$RUNNER_TEMP/postgres.log" + go test -v -tags=integration ./internal/repository ./internal/handler/admin -run '^Test(BatchWhitelistPostgresHandlerReadback|ApplyUpstreamModelsPreservesCredentialsAndInvalidatesScheduler)$' -count=1 | tee "$RUNNER_TEMP/postgres.log" grep -F -- '--- PASS: TestBatchWhitelistPostgresHandlerReadback' "$RUNNER_TEMP/postgres.log" grep -F -- '--- PASS: TestApplyUpstreamModelsPreservesCredentialsAndInvalidatesScheduler' "$RUNNER_TEMP/postgres.log" - uses: actions/upload-artifact@v7 @@ -79,7 +79,7 @@ jobs: ${{ runner.temp }}/catalog-red.log ${{ runner.temp }}/postgres.log browser: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 timeout-minutes: 25 steps: - uses: actions/checkout@v6 diff --git a/backend/internal/repository/account_repo_batch_whitelist_integration_test.go b/backend/internal/handler/admin/account_handler_batch_whitelist_integration_test.go similarity index 74% rename from backend/internal/repository/account_repo_batch_whitelist_integration_test.go rename to backend/internal/handler/admin/account_handler_batch_whitelist_integration_test.go index 11b3fdad3f29..9f3f4a83d361 100644 --- a/backend/internal/repository/account_repo_batch_whitelist_integration_test.go +++ b/backend/internal/handler/admin/account_handler_batch_whitelist_integration_test.go @@ -1,6 +1,6 @@ //go:build integration -package repository +package admin import ( "context" @@ -12,12 +12,19 @@ import ( "strings" "testing" + "database/sql" + "entgo.io/ent/dialect" + entsql "entgo.io/ent/dialect/sql" + dbent "github.com/Wei-Shaw/sub2api/ent" + _ "github.com/Wei-Shaw/sub2api/ent/runtime" "github.com/Wei-Shaw/sub2api/internal/config" - adminhandler "github.com/Wei-Shaw/sub2api/internal/handler/admin" "github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint" + "github.com/Wei-Shaw/sub2api/internal/repository" "github.com/Wei-Shaw/sub2api/internal/service" "github.com/gin-gonic/gin" + _ "github.com/lib/pq" "github.com/stretchr/testify/require" + tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres" ) type batchWhitelistAdmin struct { @@ -51,21 +58,32 @@ func (u *batchWhitelistHTTP) DoWithTLS(req *http.Request, proxy string, id int64 func TestBatchWhitelistPostgresHandlerReadback(t *testing.T) { ctx := context.Background() - tx := testEntTx(t) - repo := newAccountRepositoryWithSQL(tx.Client(), tx, nil) + container, err := tcpostgres.Run(ctx, "postgres:18.1-alpine3.23", + tcpostgres.WithDatabase("whitelist"), tcpostgres.WithUsername("fixture"), tcpostgres.WithPassword("fixture"), tcpostgres.BasicWaitStrategies()) + require.NoError(t, err) + t.Cleanup(func() { _ = container.Terminate(context.Background()) }) + dsn, err := container.ConnectionString(ctx, "sslmode=disable", "TimeZone=UTC") + require.NoError(t, err) + db, err := sql.Open("postgres", dsn) + require.NoError(t, err) + t.Cleanup(func() { _ = db.Close() }) + require.NoError(t, repository.ApplyMigrations(ctx, db)) + client := dbent.NewClient(dbent.Driver(entsql.OpenDB(dialect.Postgres, db))) + repo := repository.NewAccountRepository(client, db, nil) ids := make([]int64, 0, 4) for range 4 { - a := mustCreateAccount(t, tx.Client(), &service.Account{ + a := &service.Account{ Name: "synthetic-whitelist", Platform: service.PlatformOpenAI, Type: service.AccountTypeAPIKey, Status: service.StatusActive, Schedulable: true, Credentials: map[string]any{"api_key": "synthetic", "model_mapping": map[string]any{"old": "old"}}, Extra: map[string]any{"keep": "unchanged"}, Concurrency: 3, Priority: 19, - }) + } + require.NoError(t, repo.Create(ctx, a)) a.Credentials["base_url"] = fmt.Sprintf("https://up-%d.example/v1", a.ID) require.NoError(t, repo.Update(ctx, a)) ids = append(ids, a.ID) } svc := service.NewAccountTestService(repo, nil, nil, nil, nil, &batchWhitelistHTTP{t: t, failID: ids[1]}, &config.Config{}, nil) - h := adminhandler.NewAccountHandler(&batchWhitelistAdmin{repo: repo}, nil, nil, nil, nil, nil, nil, nil, svc, nil, nil, nil, nil, nil) + h := NewAccountHandler(&batchWhitelistAdmin{repo: repo}, nil, nil, nil, nil, nil, nil, nil, svc, nil, nil, nil, nil, nil) r := gin.New() r.POST("/accounts/:id/models/sync-upstream", h.SyncUpstreamModels) r.GET("/accounts/:id", h.GetByID) diff --git a/docs/features/account-batch-upstream-whitelist.md b/docs/features/account-batch-upstream-whitelist.md new file mode 100644 index 000000000000..f3acbc86787c --- /dev/null +++ b/docs/features/account-batch-upstream-whitelist.md @@ -0,0 +1,36 @@ +# 批量同步账号上游模型白名单 + +管理员在账号列表显式勾选账号,再打开“批量编辑”,可在模型白名单区域点击“批量同步上游模型”。跨页勾选的 ID 会一起冻结在弹窗打开时的选择中;后续列表刷新不会增删本轮账号。没有勾选账号的筛选结果预览不提供此操作。 + +该操作使用每个账号已保存的连接和凭据,立即保存各自的上游模型白名单。其他表单修改仍需单独提交。现有“同步最新支持模型”继续填充共同的手工编辑列表。启动上游同步会取消勾选共同模型限制,防止随后提交表单覆盖各账号刚同步的结果。 + +每个账号显示独立结果。最多同时提交两个账号;某个账号失败不阻止其他账号。成功时移除失效模型、保留仍指向有效上游模型的别名;空目录、未验证目录和受保护的账号策略保留旧白名单。失败账号可以单独重试。连接异常时可能无法确认服务端是否已经保存,重试前应核对账号。 + +“停止排队”及关闭弹窗会停止尚未启动的账号。已经提交的请求仍可能完成并保存,界面不会声称将其撤销。上一轮已提交请求结束前,新打开的弹窗不能开始另一轮同步。 + +## API 与安全合同 + +复用 POST /api/v1/admin/accounts/:id/models/sync-upstream,显式传入: + +```json +{"apply": true, "whitelist_only": true} +``` + +whitelist_only 要求 apply=true 且 automatic=false,省略该字段时保留原同步行为。请求经过管理员认证;observer 不得 apply。每个请求只处理 URL 中的正整数账号 ID。 + +白名单模式复用上游目录读取、代理、凭据、模型 ID 归一化、订阅隐藏模型策略及现有 model_mapping 条件更新。它不写能力元数据、目录观测、质量状态或自动同步冷却;只有 model_mapping、账号更新时间和既有调度失效通知会变化。发现期间账号凭据/代理/状态有变化时不覆盖并发编辑。 + +Vertex 复用现有有上限的分页读取;其他目录如果仍带未消费的分页标记,就保留旧白名单并报告未验证目录。混合 BPS/native 账号要求相关目录完整成功,避免部分来源失败导致错误删除。没有 generation、付费探针或自动业务重试。 + +无数据库迁移。旧后端不认识 whitelist_only,因此必须配套部署本 PR 的前后端。回滚代码不恢复已经保存的白名单。 + +## 验证边界 + +- BatchUpstreamWhitelist.spec.ts 挂载真实弹窗,使用 Axios adapter 的虚构响应,覆盖冻结选择、双并发、失败重试、停止、关闭与重入、普通表单互斥以及提示持久化。 +- AccountsView.bulkEdit.spec.ts 覆盖列表跨页勾选和筛选预览边界。 +- account_handler_batch_whitelist_test.go 使用真实 Gin handler/service 和管理员认证;存储/上游边界使用虚构实现。 +- upstream_batch_whitelist_test.go 覆盖目录归一化、别名、隐藏策略、分页、部分 BPS 来源失败和并发编辑。 +- account_handler_batch_whitelist_integration_test.go 在临时 PostgreSQL 上运行真实 handler → service → repository 写入,再通过 GET 逐字段核对两个不同成功目录、失败账号和未选中账号。 +- UI 取证脚本通过 Vite 渲染原组件与真实 CSS,浏览器禁用外部网络。截图和带 SHA/哈希的 manifest 作为 PR 附件与 CI artifacts 保存,不进入源码。 + +管理员新功能提示 ID 为 account-batch-upstream-whitelist-v1,仅展示于上述批量操作入口。关闭记录遵循当前浏览器、当前管理员的 localStorage 边界。 diff --git a/frontend/src/components/account/BulkEditAccountModal.vue b/frontend/src/components/account/BulkEditAccountModal.vue index 5ceadacbc34d..ee5206f9a867 100644 --- a/frontend/src/components/account/BulkEditAccountModal.vue +++ b/frontend/src/components/account/BulkEditAccountModal.vue @@ -411,9 +411,9 @@
emit('whitelistsSaved') ) function startWhitelistSync(retry: boolean) { - if (submitting.value || showMixedChannelWarning.value || syncBusy.value || !syncValid.value || syncAdminId.value == null) return + if (submitting.value || checkingSubmit.value || showMixedChannelWarning.value || syncBusy.value || !syncValid.value || syncAdminId.value == null) return // A subsequent form submit must not overwrite individual results with the shared draft. enableModelRestriction.value = false void runSync(retry) @@ -1832,6 +1832,8 @@ const enableRpmLimit = ref(false) // State - field values const submitting = ref(false) +const checkingSubmit = ref(false) +let dialogGeneration = 0 const showMixedChannelWarning = ref(false) const mixedChannelWarningMessage = ref('') const pendingUpdatesForConfirm = ref | null>(null) @@ -2400,6 +2402,7 @@ const canPreCheck = () => (targetSelectedPlatforms.value[0] === 'antigravity' || targetSelectedPlatforms.value[0] === 'anthropic') const handleClose = () => { + dialogGeneration++ leaveSync() showMixedChannelWarning.value = false mixedChannelWarningMessage.value = '' @@ -2431,7 +2434,7 @@ const preCheckMixedChannelRisk = async (built: Record): Promise } const handleSubmit = async () => { - if (bpsDefaults.loading.value || syncBusy.value) return + if (bpsDefaults.loading.value || syncBusy.value || submitting.value || checkingSubmit.value) return if (targetMode.value === 'selected' && targetAccountIds.value.length === 0) { appStore.showError(t('admin.accounts.bulkEdit.noSelection')) return @@ -2523,10 +2526,15 @@ const handleSubmit = async () => { return } - const canContinue = await preCheckMixedChannelRisk(built) - if (!canContinue) return - - await submitBulkUpdate(built) + checkingSubmit.value = true + const generation = dialogGeneration + try { + const canContinue = await preCheckMixedChannelRisk(built) + if (!canContinue || !props.show || generation !== dialogGeneration) return + await submitBulkUpdate(built) + } finally { + checkingSubmit.value = false + } } const submitBulkUpdate = async (baseUpdates: Record) => { @@ -2609,6 +2617,7 @@ const handleMixedChannelCancel = () => { watch( () => props.show, (newShow) => { + dialogGeneration++ if (!newShow) { // Reset all enable flags enableBaseUrl.value = false diff --git a/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts b/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts index 72dc7c56c119..8dc9ff7432b4 100644 --- a/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts +++ b/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts @@ -11,6 +11,8 @@ import { i18n, loadLocaleMessages } from '@/i18n' const adapter = apiClient.defaults.adapter const calls: number[] = [] +let checkRisk: (() => Promise) | undefined +const bulkWrites: object[] = [] const wrappers: { unmount(): void }[] = [] const start = '[data-testid="batch-upstream-start"]' const row = (id: number) => `[data-sync-account="${id}"]` @@ -20,7 +22,12 @@ function fixture(ids: number[], sync: (id: number) => Promise, role = 'a useAuthStore(pinia).user = { id: 17, role } as ReturnType['user'] apiClient.defaults.adapter = (async config => { let data = {} - if (config.method === 'post') { + if (config.url === '/admin/accounts/check-mixed-channel') { + data = await checkRisk!() + } else if (config.url === '/admin/accounts/bulk-update') { + bulkWrites.push(JSON.parse(config.data)) + data = { success: 2, failed: 0, results: [] } + } else if (config.method === 'post') { const match = /^\/admin\/accounts\/(\d+)\/models\/sync-upstream$/.exec(config.url ?? '') expect(match).toBeTruthy() expect(JSON.parse(config.data)).toEqual({ apply: true, whitelist_only: true }) @@ -40,6 +47,8 @@ function fixture(ids: number[], sync: (id: number) => Promise, role = 'a const applied = (id: number) => ({ models: ['fixture-' + id], applied: true, changed: true }) beforeEach(async () => { calls.length = 0 + bulkWrites.length = 0 + checkRisk = undefined localStorage.clear() await loadLocaleMessages('en') i18n.global.locale.value = 'en' @@ -82,6 +91,41 @@ describe('batch upstream whitelist', () => { expect(w.find('[data-testid="batch-upstream-retry"]').exists()).toBe(false) }) + it('serializes the ordinary form preflight with immediate whitelist writes', async () => { + let release!: (value: object) => void + checkRisk = () => new Promise(resolve => { release = resolve }) + const w = fixture([41,42], async id => applied(id)) + await w.get('#bulk-edit-groups-enabled').setValue(true) + const groupSelector = w.findComponent({ name: 'GroupSelector' }) + groupSelector.vm.$emit('update:modelValue', [5]) + await flushPromises() + await w.get('#bulk-edit-account-form').trigger('submit.prevent') + await flushPromises() + expect(release).toBeTypeOf('function') + expect(w.get(start).attributes('disabled'), 'preflight must own the write lock').toBeDefined() + await w.get(start).trigger('click') + expect(calls).toEqual([]) + release({has_risk:false}) + await flushPromises() + expect(bulkWrites).toHaveLength(1) + }) + + it('does not submit an old preflight after closing and reopening another selection', async () => { + let release!: (value: object) => void + checkRisk = () => new Promise(resolve => { release = resolve }) + const w = fixture([41,42], async id => applied(id)) + await w.get('#bulk-edit-groups-enabled').setValue(true) + w.findComponent({ name: 'GroupSelector' }).vm.$emit('update:modelValue', [5]) + await flushPromises() + await w.get('#bulk-edit-account-form').trigger('submit.prevent') + await flushPromises() + await w.setProps({show:false}) + await w.setProps({show:true,accountIds:[77]}) + release({has_risk:false}) + await flushPromises() + expect(bulkWrites, 'old preflight cannot target the new selection').toEqual([]) + }) + it('retains the frozen selection across a table refresh and retries only failures', async () => { const w = fixture([41, 42, 73, 74], async id => { if (id === 42 && calls.filter(n => n === id).length === 1) throw Error('unreachable') @@ -131,7 +175,7 @@ describe('batch upstream whitelist', () => { const w = fixture([41], async id => applied(id), 'observer') expect(w.find(start).exists()).toBe(false) const admin = fixture([41], async id => applied(id)) - await admin.setProps({ filteredPreview: true } as never) + await admin.setProps({ filteredPreview: true }) expect(admin.find(start).exists()).toBe(false) expect(calls).toEqual([]) }) diff --git a/frontend/src/i18n/locales/en/admin/accounts.ts b/frontend/src/i18n/locales/en/admin/accounts.ts index 7ccc4ba09082..48b2631754fc 100644 --- a/frontend/src/i18n/locales/en/admin/accounts.ts +++ b/frontend/src/i18n/locales/en/admin/accounts.ts @@ -1208,7 +1208,7 @@ export default { unknown: 'Check the account before retrying', draining: 'Waiting for previously submitted accounts.', start: 'Batch sync upstream models', - hint: 'Save each selected account’s own upstream whitelist immediately. Other edits are saved separately.', + hint: 'Save each selected account’s own upstream whitelist immediately using its saved connection. Valid aliases are kept; empty or unsupported catalogs keep the old whitelist. Stopping or closing stops queued accounts; submitted requests can still save. Other edits are saved separately.', saved: 'Saved', failed: 'Failed', pending: 'Waiting', diff --git a/frontend/src/i18n/locales/zh/admin/accounts.ts b/frontend/src/i18n/locales/zh/admin/accounts.ts index 11a5abe716bf..500adb7567e7 100644 --- a/frontend/src/i18n/locales/zh/admin/accounts.ts +++ b/frontend/src/i18n/locales/zh/admin/accounts.ts @@ -1318,7 +1318,7 @@ export default { unknown: '重试前请核对账号状态', draining: '正在等待上一轮已提交的账号完成。', start: '批量同步上游模型', - hint: '按选中账号各自的上游目录立即保存白名单。其他编辑需单独提交。', + hint: '按选中账号各自已保存的连接立即同步白名单,保留有效别名。空目录或不支持同步时保留原白名单。停止或关闭只停止排队,已提交请求仍可能保存。其他编辑需单独提交。', saved: '已保存', failed: '失败', pending: '待处理', From ae1fbf31e9a8e1b606504f36f00e77b6ac3b3f35 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 14:11:31 +0800 Subject: [PATCH 06/12] test(accounts): provide avatar read in the authentication fixture --- .../handler/admin/account_handler_batch_whitelist_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/internal/handler/admin/account_handler_batch_whitelist_test.go b/backend/internal/handler/admin/account_handler_batch_whitelist_test.go index 47cb4652c478..f3026ac11c03 100644 --- a/backend/internal/handler/admin/account_handler_batch_whitelist_test.go +++ b/backend/internal/handler/admin/account_handler_batch_whitelist_test.go @@ -143,6 +143,10 @@ type whitelistActor struct { func (r *whitelistActor) GetByID(context.Context, int64) (*service.User, error) { return r.user, nil } +func (r *whitelistActor) GetUserAvatar(context.Context, int64) (*service.UserAvatar, error) { + return nil, nil +} + func TestAccountHandlerBatchWhitelistAuthentication(t *testing.T) { for _, tc := range []struct { role string From f11ea64878abe28db49899040e15032998dbf6e8 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 14:19:43 +0800 Subject: [PATCH 07/12] fix(accounts): retire delayed confirmations when the selection closes --- .github/scripts/batch-whitelist-ui.mjs | 5 ++++- .github/workflows/batch-upstream-whitelist.yml | 5 +++++ frontend/src/components/account/BulkEditAccountModal.vue | 3 +++ .../account/__tests__/BatchUpstreamWhitelist.spec.ts | 5 +++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/scripts/batch-whitelist-ui.mjs b/.github/scripts/batch-whitelist-ui.mjs index d227425827ad..11fb5e6b7354 100644 --- a/.github/scripts/batch-whitelist-ui.mjs +++ b/.github/scripts/batch-whitelist-ui.mjs @@ -34,8 +34,11 @@ try { await loadLocaleMessages('zh') i18n.global.locale.value='zh' window.calls=[] + window.requests=[] apiClient.defaults.adapter=async config=>{ const url=config.url + window.requests.push({method:config.method,url}) + if(config.method !== 'get' && !url.endsWith('/models/sync-upstream')) throw Error('Unexpected fixture write '+url) let data={} if(url.endsWith('/models/sync-upstream')){ window.calls.push(url) @@ -85,7 +88,7 @@ try { await page.waitForFunction(()=>window.calls.length===3) assert.deepEqual(await page.evaluate(()=>window.calls),[41,42,42].map(id=>'/admin/accounts/'+id+'/models/sync-upstream')) } - manifest.screenshots.push({name,viewport,box,sha256:createHash('sha256').update(readFileSync(path)).digest('hex')}) + manifest.screenshots.push({name,viewport,box,sha256:createHash('sha256').update(readFileSync(path)).digest('hex'), requests:await page.evaluate(()=>window.requests)}) await page.close() } writeFileSync(join(out, mode+'-manifest.json'), JSON.stringify(manifest,null,2)+'\n') diff --git a/.github/workflows/batch-upstream-whitelist.yml b/.github/workflows/batch-upstream-whitelist.yml index 6e503c120e52..fc0946482274 100644 --- a/.github/workflows/batch-upstream-whitelist.yml +++ b/.github/workflows/batch-upstream-whitelist.yml @@ -3,9 +3,14 @@ on: pull_request: paths: - 'frontend/src/components/account/*UpstreamWhitelist*' + - 'frontend/src/composables/useBatchUpstreamWhitelist.ts' + - 'frontend/src/components/account/BulkEditAccountModal.vue' - 'frontend/src/components/account/__tests__/BatchUpstreamWhitelist*' - 'backend/internal/**/**batch_whitelist*' - 'backend/internal/service/upstream_model_mapping_sync.go' + - 'backend/internal/service/upstream_models.go' + - 'backend/internal/service/openai_excel_bps_model_access.go' + - 'backend/internal/handler/admin/account_handler.go' - '.github/workflows/batch-upstream-whitelist.yml' - '.github/scripts/batch-whitelist-ui.mjs' permissions: diff --git a/frontend/src/components/account/BulkEditAccountModal.vue b/frontend/src/components/account/BulkEditAccountModal.vue index ee5206f9a867..4f58d311dde2 100644 --- a/frontend/src/components/account/BulkEditAccountModal.vue +++ b/frontend/src/components/account/BulkEditAccountModal.vue @@ -2415,12 +2415,14 @@ const handleClose = () => { const preCheckMixedChannelRisk = async (built: Record): Promise => { if (!canPreCheck()) return true if (mixedChannelConfirmed.value) return true + const generation = dialogGeneration try { const result = await adminAPI.accounts.checkMixedChannelRisk({ platform: targetSelectedPlatforms.value[0], group_ids: groupIds.value }) + if (generation !== dialogGeneration || !props.show) return false if (!result.has_risk) return true pendingUpdatesForConfirm.value = built @@ -2428,6 +2430,7 @@ const preCheckMixedChannelRisk = async (built: Record): Promise showMixedChannelWarning.value = true return false } catch (error: any) { + if (generation !== dialogGeneration || !props.show) return false appStore.showError(error.message || t('admin.accounts.bulkEdit.failed')) return false } diff --git a/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts b/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts index 8dc9ff7432b4..2aca9500196b 100644 --- a/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts +++ b/frontend/src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts @@ -110,7 +110,7 @@ describe('batch upstream whitelist', () => { expect(bulkWrites).toHaveLength(1) }) - it('does not submit an old preflight after closing and reopening another selection', async () => { + it.each([false, true])('does not retain an old preflight after reopening (risk=%s)', async risk => { let release!: (value: object) => void checkRisk = () => new Promise(resolve => { release = resolve }) const w = fixture([41,42], async id => applied(id)) @@ -121,8 +121,9 @@ describe('batch upstream whitelist', () => { await flushPromises() await w.setProps({show:false}) await w.setProps({show:true,accountIds:[77]}) - release({has_risk:false}) + release({has_risk:risk}) await flushPromises() + expect(w.findComponent({ name: 'ConfirmDialog' }).props('show'), 'old confirmation must not target the new selection').toBe(false) expect(bulkWrites, 'old preflight cannot target the new selection').toEqual([]) }) From add606e964972ded3d6389790586c781c89a58c1 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 14:26:11 +0800 Subject: [PATCH 08/12] ci(accounts): include complete frontend checks in scoped evidence --- .github/workflows/batch-upstream-whitelist.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/batch-upstream-whitelist.yml b/.github/workflows/batch-upstream-whitelist.yml index fc0946482274..6400ee7e16c8 100644 --- a/.github/workflows/batch-upstream-whitelist.yml +++ b/.github/workflows/batch-upstream-whitelist.yml @@ -85,7 +85,9 @@ jobs: ${{ runner.temp }}/postgres.log browser: runs-on: ubuntu-24.04 - timeout-minutes: 25 + timeout-minutes: 35 + env: + NODE_OPTIONS: --max-old-space-size=6144 steps: - uses: actions/checkout@v6 with: @@ -134,6 +136,8 @@ jobs: ${{ runner.temp }}/evidence/*.png ${{ runner.temp }}/evidence/*.json ${{ runner.temp }}/evidence/*.log + - name: Complete frontend CI (lint, typecheck, critical manifest) + run: make test-frontend - name: Build production frontend run: pnpm --dir frontend run build env: From bbbc3955bdfb9272bc505ec1d87ff1f609187555 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 15:23:04 +0800 Subject: [PATCH 09/12] fix(accounts): isolate whitelist discovery from caches and account recovery --- .../workflows/batch-upstream-whitelist.yml | 21 +- .../service/openai_codex_models_service.go | 16 +- .../service/openai_excel_bps_model_access.go | 45 ++- .../internal/service/openai_excel_oauth.go | 75 +++++ .../internal/service/openai_models_list.go | 30 +- ...am_batch_whitelist_identity_policy_test.go | 31 ++ ...stream_batch_whitelist_read_policy_test.go | 270 ++++++++++++++++++ 7 files changed, 475 insertions(+), 13 deletions(-) create mode 100644 backend/internal/service/upstream_batch_whitelist_identity_policy_test.go create mode 100644 backend/internal/service/upstream_batch_whitelist_read_policy_test.go diff --git a/.github/workflows/batch-upstream-whitelist.yml b/.github/workflows/batch-upstream-whitelist.yml index 6400ee7e16c8..b127a02e4ee9 100644 --- a/.github/workflows/batch-upstream-whitelist.yml +++ b/.github/workflows/batch-upstream-whitelist.yml @@ -10,6 +10,9 @@ on: - 'backend/internal/service/upstream_model_mapping_sync.go' - 'backend/internal/service/upstream_models.go' - 'backend/internal/service/openai_excel_bps_model_access.go' + - 'backend/internal/service/openai_models_list.go' + - 'backend/internal/service/openai_codex_models_service.go' + - 'backend/internal/service/openai_excel_oauth.go' - 'backend/internal/handler/admin/account_handler.go' - '.github/workflows/batch-upstream-whitelist.yml' - '.github/scripts/batch-whitelist-ui.mjs' @@ -60,11 +63,26 @@ jobs: cat "$RUNNER_TEMP/catalog-red.log" grep -F -- '--- FAIL: TestBatchWhitelistSafety/unconsumed' "$RUNNER_TEMP/catalog-red.log" grep -F 'partial BPS/native catalogs cannot replace the entire whitelist' "$RUNNER_TEMP/catalog-red.log" + - name: Mapping-only fresh read and account state regression on reviewed head + shell: bash + run: | + set -euo pipefail + reviewed=add606e964972ded3d6389790586c781c89a58c1 + git worktree add --detach "$RUNNER_TEMP/whitelist-read-before" "$reviewed" + file=backend/internal/service/upstream_batch_whitelist_read_policy_test.go + cp "$file" "$RUNNER_TEMP/whitelist-read-before/$file" + cd "$RUNNER_TEMP/whitelist-read-before/backend" + if go test -v -tags=unit ./internal/service -run '^TestBatchWhitelistBPS(NativeStructured401IsReadOnly|NativeStaleFailureKeepsMapping|NativeFreshCompleteSuccess|FreshCacheCannotHideFailure|ExpiredGrantDoesNotRefresh)$' -count=1 > "$RUNNER_TEMP/read-policy-red.log" 2>&1; then exit 1; fi + cat "$RUNNER_TEMP/read-policy-red.log" + grep -F 'whitelist discovery must not disable an account' "$RUNNER_TEMP/read-policy-red.log" + grep -F 'stale native cache cannot authorize a replacement whitelist' "$RUNNER_TEMP/read-policy-red.log" + grep -F 'BPS fresh cache must also be bypassed' "$RUNNER_TEMP/read-policy-red.log" + ! grep -Eq 'build failed|setup failed|undefined:|no tests to run|panic:' "$RUNNER_TEMP/read-policy-red.log" - name: Candidate handler and catalog policies working-directory: backend run: | set -o pipefail - go test -v -tags=unit ./internal/handler/admin ./internal/service ./internal/server/middleware -run 'Test(AccountHandler(Batch|SyncUpstreamModels)|BatchWhitelist|ApplyUpstreamModels|AutomaticUpstreamModels|FetchUpstreamSupportedModels|SyncUpstreamModelCatalog|ReconciledUpstreamModels|ExcelBPSAccess|ExcelBPSMixed|AdminAuth)' -count=1 | tee "$RUNNER_TEMP/handler-green.log" + go test -v -tags=unit ./internal/handler/admin ./internal/service ./internal/server/middleware -run 'Test(AccountHandler(Batch|SyncUpstreamModels)|BatchWhitelist|ApplyUpstreamModels|AutomaticUpstreamModels|FetchUpstreamSupportedModels|FetchOpenAIModelsList|FetchCodexModelsManifest(OAuth|AgentIdentity401|APIKey401)|SyncUpstreamModelCatalog|ReconciledUpstreamModels|ExcelBPSAccess|ExcelBPSMixed|ExcelOAuthRefresh|AdminAuth)' -count=1 | tee "$RUNNER_TEMP/handler-green.log" - name: Build production server run: make -C backend build - name: PostgreSQL application, scheduler invalidation and stale edit protection @@ -82,6 +100,7 @@ jobs: ${{ runner.temp }}/handler-red.log ${{ runner.temp }}/handler-green.log ${{ runner.temp }}/catalog-red.log + ${{ runner.temp }}/read-policy-red.log ${{ runner.temp }}/postgres.log browser: runs-on: ubuntu-24.04 diff --git a/backend/internal/service/openai_codex_models_service.go b/backend/internal/service/openai_codex_models_service.go index eb87ac7ee52e..198b36e2a787 100644 --- a/backend/internal/service/openai_codex_models_service.go +++ b/backend/internal/service/openai_codex_models_service.go @@ -1739,7 +1739,11 @@ func (s *OpenAIGatewayService) FetchCodexModelsManifest(ctx context.Context, acc } func (s *OpenAIGatewayService) fetchNativeCodexModelsManifest(ctx context.Context, account *Account, clientVersion, ifNoneMatch string) (*OpenAIModelsResponse, error) { - useCache := !isFreshOpenAICredentialProbe(ctx) + return s.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, ifNoneMatch, openAIModelsReadDefault) +} + +func (s *OpenAIGatewayService) fetchNativeCodexModelsManifestWithPolicy(ctx context.Context, account *Account, clientVersion, ifNoneMatch string, policy openAIModelsReadPolicy) (*OpenAIModelsResponse, error) { + useCache := policy != openAIModelsReadWhitelist && !isFreshOpenAICredentialProbe(ctx) if account == nil { return nil, infraerrors.New(http.StatusInternalServerError, "OPENAI_CODEX_MODELS_ACCOUNT_REQUIRED", "account is required") } @@ -1751,6 +1755,11 @@ func (s *OpenAIGatewayService) fetchNativeCodexModelsManifest(ctx context.Contex if credAccount.IsExcelOAuth() { return nil, errExcelOAuthRouteUnavailable } + if policy == openAIModelsReadWhitelist && credAccount.IsOpenAIAgentIdentity() { + // Even building these headers can create/persist a task. Whitelist + // discovery never owns the agent identity task lifecycle. + return nil, infraerrors.BadRequest("OPENAI_MODELS_READ_ONLY_ACCOUNT_UNSUPPORTED", "Read-only model discovery does not support agent identity accounts") + } clientVersion = strings.TrimSpace(clientVersion) if clientVersion == "" { clientVersion = CodexCanonicalClientVersion() @@ -1851,6 +1860,11 @@ func (s *OpenAIGatewayService) fetchNativeCodexModelsManifest(ctx context.Contex request.headers.Del("If-None-Match") request.headers.Set("Cache-Control", "no-cache") } + if policy == openAIModelsReadWhitelist { + // Bypass the shared display cache and its detached refresh completely. + // Return the actual response before OAuth health handling/task recovery. + return s.fetchCodexModelsManifestUpstream(ctx, request, "") + } if useAPIKeyUpstream { if !useCache { return s.fetchCodexModelsManifestUpstream(ctx, request, "") diff --git a/backend/internal/service/openai_excel_bps_model_access.go b/backend/internal/service/openai_excel_bps_model_access.go index 66e9944bf798..26ecd0521021 100644 --- a/backend/internal/service/openai_excel_bps_model_access.go +++ b/backend/internal/service/openai_excel_bps_model_access.go @@ -87,13 +87,23 @@ func excelBPSAccessModelsBody(raw []byte) ([]byte, error) { // FetchExcelBPSModelsList uses a separate fresh-only cache scoped by bearer, // account and proxy identity. Expired permissions are never served stale. func (s *OpenAIGatewayService) FetchExcelBPSModelsList(ctx context.Context, account *Account) (*OpenAIModelsResponse, error) { + return s.fetchExcelBPSModelsListWithPolicy(ctx, account, openAIModelsReadDefault) +} + +func (s *OpenAIGatewayService) fetchExcelBPSModelsListWithPolicy(ctx context.Context, account *Account, policy openAIModelsReadPolicy) (*OpenAIModelsResponse, error) { if err := ctx.Err(); err != nil { return nil, err } if s == nil || s.httpUpstream == nil || account == nil || !account.IsOpenAI() || !account.IsExcelBPSEnabled() { return nil, fmt.Errorf("excel BPS model discovery is unavailable") } - token, err := s.getExcelBPSAccessToken(ctx, account) + var token string + var err error + if policy == openAIModelsReadWhitelist { + token, err = s.getExcelBPSAccessTokenReadOnly(ctx, account) + } else { + token, err = s.getExcelBPSAccessToken(ctx, account) + } if err != nil { if IsExcelAuthorizationError(err) { return nil, err @@ -112,17 +122,25 @@ func (s *OpenAIGatewayService) FetchExcelBPSModelsList(ctx context.Context, acco request.Body = nil request.ContentLength = 0 request.Header.Set("Accept", "application/json") + if policy == openAIModelsReadWhitelist { + request.Header.Set("Cache-Control", "no-cache") + request.Header.Del("If-None-Match") + } request.URL.Path = "/basispoints/api/responses/access" request.URL.RawQuery = "include_models=true" proxy := upstreamModelsProxyURL(account) identity := openAIModelsRequest{url: request.URL.String(), headers: request.Header, proxyURL: fmt.Sprintf("%s|bps_pool:%s|managed:%t", proxy, account.ExcelBPSProxySource(), account.IsExcelBPSMihomoEnabled()), accountID: account.ID, credentialAccountID: account.ID, standardModelsList: true} key := buildOpenAIModelsCacheKey(identity) - if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { - return openAIModelsResponseForClient(cached, ""), nil + if policy != openAIModelsReadWhitelist { + if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { + return openAIModelsResponseForClient(cached, ""), nil + } } fetch := func(fetchCtx context.Context) (*OpenAIModelsResponse, error) { - if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { - return cached, nil + if policy != openAIModelsReadWhitelist { + if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { + return cached, nil + } } fetchCtx, cancel := context.WithTimeout(fetchCtx, 5*time.Second) defer cancel() @@ -166,9 +184,16 @@ func (s *OpenAIGatewayService) FetchExcelBPSModelsList(ctx context.Context, acco return nil, err } manifest := &OpenAIModelsResponse{Body: body, ETag: codexModelsManifestBodyETag(body)} - s.excelBPSModelsCache.set(key, manifest, time.Now()) + if policy != openAIModelsReadWhitelist { + s.excelBPSModelsCache.set(key, manifest, time.Now()) + } return manifest, nil } + if policy == openAIModelsReadWhitelist { + // No shared cache, detached singleflight or background work can verify + // the current destructive edit. Cancellation belongs to this caller. + return fetch(ctx) + } if HasAPIKeyAdmissionOwner(ctx) { result, err := fetch(ctx) if err != nil { @@ -285,7 +310,11 @@ func (s *OpenAIGatewayService) fetchExcelBPSAccountModels(ctx context.Context, a } func (s *OpenAIGatewayService) fetchExcelBPSAccountModelsWithPolicy(ctx context.Context, account *Account, requireComplete bool) (*OpenAIModelsResponse, error) { - access, accessErr := s.FetchExcelBPSModelsList(ctx, account) + policy := openAIModelsReadDefault + if requireComplete { + policy = openAIModelsReadWhitelist + } + access, accessErr := s.fetchExcelBPSModelsListWithPolicy(ctx, account, policy) if accessErr != nil { if ctx.Err() != nil { return nil, ctx.Err() @@ -297,7 +326,7 @@ func (s *OpenAIGatewayService) fetchExcelBPSAccountModelsWithPolicy(ctx context. } var native []byte if !account.IsExcelOAuth() && !account.isExcelBPSAllModelsEnabled() { - response, err := s.fetchNativeOpenAIModelsList(ctx, account) + response, err := s.fetchNativeOpenAIModelsListWithPolicy(ctx, account, policy) if err == nil { native = response.Body } else if requireComplete { diff --git a/backend/internal/service/openai_excel_oauth.go b/backend/internal/service/openai_excel_oauth.go index a9f833cc4b04..f12f5b0c584f 100644 --- a/backend/internal/service/openai_excel_oauth.go +++ b/backend/internal/service/openai_excel_oauth.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log/slog" + "strconv" "strings" "sync" "time" @@ -412,6 +413,80 @@ func (s *OpenAIGatewayService) getExcelBPSAccessToken(ctx context.Context, accou return token, err } +// A whitelist edit may read a saved grant, but must not refresh/rotate it or +// change its authorization state. Unready credentials fail this edit only. +func (s *OpenAIGatewayService) getExcelBPSAccessTokenReadOnly(ctx context.Context, account *Account) (string, error) { + if account == nil { + return "", errors.New("account is missing") + } + if err := validateOpenAIConnectionKind(account); err != nil { + return "", err + } + if s.excelOAuthReauth != nil { + if err := s.excelOAuthReauth.validateAuthConnectionPolicy(ctx, account); err != nil { + return "", err + } + } + if account.AuthConnectionID != "" { + reauth := s.excelOAuthReauth + if reauth == nil { + return "", errors.New("authorization connection service unavailable") + } + if err := reauth.validateConnectionMaintenanceProxyMode(ctx, account.ID); err != nil { + return "", err + } + if err := reauth.ValidateAuthConnectionSend(ctx, account); err != nil { + return "", err + } + connection, err := reauth.authConnection(ctx, account.ID, account.AuthConnectionID) + if err != nil { + return "", err + } + if connection.Legacy || connection.Kind != "bps" || connection.Revision != account.AuthConnectionRevision || + connection.Status != "active" || (connection.RefreshState != "" && connection.RefreshState != "idle") || + connection.RefreshCandidateCiphertext != "" || len(connection.Capabilities) == 0 || + (connection.CooldownUntil != nil && time.Now().Before(*connection.CooldownUntil)) { + return "", connectionRefreshUnavailable() + } + credentials, err := reauth.readConnectionCredentials(connection) + if err != nil { + return "", err + } + expiry, _ := strconv.ParseInt(stringCredential(credentials, "expires_at"), 10, 64) + token := strings.TrimSpace(stringCredential(credentials, "access_token")) + if token == "" || expiry <= time.Now().Add(openAITokenRefreshSkew).Unix() { + return "", connectionRefreshUnavailable() + } + return token, nil + } + if account.HasManagedOpenAIConnections() || account.IsOpenAIAgentIdentity() { + return "", errors.New("read-only BPS discovery requires a saved authorization") + } + if s.excelOAuthReauth != nil { + _, credentials, fresh, err := s.excelOAuthReauth.readExcelGrant(ctx, account) + if !errors.Is(err, errExcelLegacyCredentials) { + if err != nil { + return "", err + } + claims, err := openai.DecodeIDToken(reauthMapString(credentials, "access_token")) + if err != nil || credentials[excelAccessTokenRejectedKey] == true || claims.Exp <= time.Now().Add(openAITokenRefreshSkew).Unix() { + return "", errors.New("saved Excel access token requires refresh") + } + return validatedExcelAccessToken(fresh, credentials) + } + } + // Legacy/pure gateway accounts already carry their saved token. Do not + // enter GetAccessToken: its provider may refresh or disable the account. + if expiry := account.GetCredentialAsTime("expires_at"); expiry != nil && !expiry.After(time.Now().Add(openAITokenRefreshSkew)) { + return "", errors.New("saved BPS access token requires refresh") + } + token := strings.TrimSpace(account.GetOpenAIAccessToken()) + if token == "" { + return "", errors.New("saved BPS access token is unavailable") + } + return token, nil +} + // Reject only the access token actually used by this request. Preserve its RT // for the normal refresh flow, and never overwrite a concurrently rotated grant. func (s *OpenAIOAuthReauthService) invalidateExcelAccessToken(ctx context.Context, id int64, token string, cache OpenAITokenCache) error { diff --git a/backend/internal/service/openai_models_list.go b/backend/internal/service/openai_models_list.go index 2a423ffffdc8..0cd825bc83c0 100644 --- a/backend/internal/service/openai_models_list.go +++ b/backend/internal/service/openai_models_list.go @@ -13,6 +13,15 @@ import ( infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" ) +type openAIModelsReadPolicy uint8 + +const ( + openAIModelsReadDefault openAIModelsReadPolicy = iota + // A replacement whitelist needs this request's complete upstream response. + // It must not refresh credentials, recover tasks or change account health. + openAIModelsReadWhitelist +) + // FetchOpenAIModelsList discovers a single account's raw public model catalog. // API keys use the standard endpoint; OAuth reuses the authenticated, cached // Codex source. Account mappings and group policy are applied after this cache. @@ -24,6 +33,10 @@ func (s *OpenAIGatewayService) FetchOpenAIModelsList(ctx context.Context, accoun } func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, account *Account) (*OpenAIModelsResponse, error) { + return s.fetchNativeOpenAIModelsListWithPolicy(ctx, account, openAIModelsReadDefault) +} + +func (s *OpenAIGatewayService) fetchNativeOpenAIModelsListWithPolicy(ctx context.Context, account *Account, policy openAIModelsReadPolicy) (*OpenAIModelsResponse, error) { if s == nil || account == nil { return nil, infraerrors.New(http.StatusInternalServerError, "OPENAI_MODELS_ACCOUNT_REQUIRED", "OpenAI account is required") } @@ -39,10 +52,13 @@ func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, if s.settingService != nil { clientVersion = s.settingService.GetOpenAICodexClientVersion(ctx) } - response, err := s.fetchNativeCodexModelsManifest(ctx, account, clientVersion, "") + response, err := s.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, "", policy) if err != nil { return nil, err } + if response == nil || response.NotModified { + return nil, invalidOpenAIModelsList(fmt.Errorf("upstream returned no current model catalog")) + } body, err := standardOpenAIModelsBody(response.Body, true) if err != nil { return nil, invalidOpenAIModelsList(err) @@ -60,7 +76,7 @@ func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, accountConcurrency: account.Concurrency, useAPIKeyUpstream: true, standardModelsList: true, } - response, err := s.fetchCachedOpenAIModels(ctx, request, func(fetchCtx context.Context, etag string) (*OpenAIModelsResponse, error) { + fetch := func(fetchCtx context.Context, etag string) (*OpenAIModelsResponse, error) { response, err := s.fetchOpenAIModelsUpstream(fetchCtx, request, etag) if err != nil || response.NotModified { return response, err @@ -72,7 +88,15 @@ func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, response.Body = body response.ETag = codexModelsManifestBodyETag(body) return response, nil - }, "") + } + var response *OpenAIModelsResponse + if policy == openAIModelsReadWhitelist { + request.headers.Del("If-None-Match") + request.headers.Set("Cache-Control", "no-cache") + response, err = fetch(ctx, "") + } else { + response, err = s.fetchCachedOpenAIModels(ctx, request, fetch, "") + } if err != nil { return nil, err } diff --git a/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go b/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go new file mode 100644 index 000000000000..536716fe0366 --- /dev/null +++ b/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go @@ -0,0 +1,31 @@ +//go:build unit + +package service + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestBatchWhitelistNativePolicyRejectsTaskLifecycle(t *testing.T) { + for _, task := range []string{"", "existing-task"} { + t.Run(task, func(t *testing.T) { + key, privateKey := newTestAgentIdentityKey(t) + account := newCodexModelsTestAccount() + account.Credentials["auth_mode"] = OpenAIAuthModeAgentIdentity + account.Credentials["agent_runtime_id"] = key.runtimeID + account.Credentials["agent_private_key"] = privateKey + account.Credentials["task_id"] = task + repo := &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: account}} + gateway := newCodexModels401TestService(repo) + gateway.accountRepo = repo + _, err := gateway.fetchNativeOpenAIModelsListWithPolicy(context.Background(), account, openAIModelsReadWhitelist) + require.Error(t, err, "read-only discovery cannot create or recover an identity task") + require.Zero(t, repo.credentialWrites.Load()) + require.Equal(t, privateKey, account.GetCredential("agent_private_key")) + require.Equal(t, task, account.GetCredential("task_id")) + }) + } +} diff --git a/backend/internal/service/upstream_batch_whitelist_read_policy_test.go b/backend/internal/service/upstream_batch_whitelist_read_policy_test.go new file mode 100644 index 000000000000..9ac82911ee4b --- /dev/null +++ b/backend/internal/service/upstream_batch_whitelist_read_policy_test.go @@ -0,0 +1,270 @@ +//go:build unit + +package service + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type batchWhitelistReadOnlyRepo struct { + *upstreamModelMappingRepoStub + authErrors atomic.Int32 + tempUnsched atomic.Int32 + credentialWrites atomic.Int32 + extraWrites atomic.Int32 +} + +func (r *batchWhitelistReadOnlyRepo) SetError(context.Context, int64, string) error { + r.authErrors.Add(1) + return nil +} + +func (r *batchWhitelistReadOnlyRepo) SetTempUnschedulable(context.Context, int64, time.Time, string) error { + r.tempUnsched.Add(1) + return nil +} + +func (r *batchWhitelistReadOnlyRepo) UpdateCredentials(context.Context, int64, map[string]any) error { + r.credentialWrites.Add(1) + return nil +} + +func (r *batchWhitelistReadOnlyRepo) UpdateExtra(context.Context, int64, map[string]any) error { + r.extraWrites.Add(1) + return nil +} + +type batchWhitelistTokenInvalidator struct{ calls atomic.Int32 } + +func (i *batchWhitelistTokenInvalidator) InvalidateToken(context.Context, *Account) error { + i.calls.Add(1) + return nil +} + +// Only persistence and BPS HTTP are replaced. Native discovery traverses the +// real Codex request builder, HTTP client, envelope parser and error handling. +type batchWhitelistReadFixture struct { + service *AccountTestService + gateway *OpenAIGatewayService + repo *batchWhitelistReadOnlyRepo + invalidator *batchWhitelistTokenInvalidator + nativeCalls atomic.Int32 + bpsCalls atomic.Int32 + nativeStatus atomic.Int32 + bpsStatus atomic.Int32 + nativeBody atomic.Value + bpsBody atomic.Value +} + +func newBatchWhitelistReadFixture(t *testing.T) *batchWhitelistReadFixture { + t.Helper() + f := &batchWhitelistReadFixture{} + f.nativeStatus.Store(http.StatusOK) + f.bpsStatus.Store(http.StatusOK) + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"},{"slug":"gpt-native-b"}]}`) + f.bpsBody.Store(`{"allowed":true,"model_catalog":{"models":[{"id":"gpt-7-new"}]}}`) + account := excelAccount() + account.Credentials["refresh_token"] = "synthetic-refresh-token" + account.Credentials["model_mapping"] = map[string]any{ + "gpt-native-b": "gpt-native-b", "native-alias": "gpt-native-b", "obsolete": "obsolete", + } + account.Extra["openai_excel_bps_models"] = []string{"gpt-7-new", "gpt-7-cached"} + account.Extra["keep"] = "unchanged" + f.repo = &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: account}} + f.invalidator = &batchWhitelistTokenInvalidator{} + f.gateway = newCodexModels401TestService(f.repo) + f.gateway.accountRepo = f.repo + f.gateway.rateLimitService.SetTokenCacheInvalidator(f.invalidator) + f.gateway.httpUpstream = &codexModelsHTTPUpstreamStub{do: func(req *http.Request, _ string, id int64, _ int) (*http.Response, error) { + f.bpsCalls.Add(1) + assert.Equal(t, http.MethodGet, req.Method) + assert.Equal(t, "/basispoints/api/responses/access", req.URL.Path) + assert.Equal(t, account.ID, id) + response := ordinaryModelsUpstreamResponse(f.bpsBody.Load().(string)) + response.StatusCode = int(f.bpsStatus.Load()) + return response, nil + }} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + f.nativeCalls.Add(1) + assert.Equal(t, http.MethodGet, req.Method) + assert.Equal(t, "Bearer test-token", req.Header.Get("Authorization")) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(int(f.nativeStatus.Load())) + _, _ = w.Write([]byte(f.nativeBody.Load().(string))) + })) + original := chatgptCodexModelsURL + chatgptCodexModelsURL = server.URL + t.Cleanup(func() { chatgptCodexModelsURL = original; server.Close() }) + f.service = &AccountTestService{accountRepo: f.repo, openaiGatewayService: f.gateway} + return f +} + +func (f *batchWhitelistReadFixture) assertNoAccountSideEffects(t *testing.T, before []byte) { + t.Helper() + after, err := json.Marshal(f.repo.account) + require.NoError(t, err) + require.JSONEq(t, string(before), string(after), "discovery must preserve credentials, mapping, status, cooldowns and Extra") + require.Zero(t, f.repo.authErrors.Load(), "whitelist discovery must not disable an account") + require.Zero(t, f.repo.tempUnsched.Load(), "whitelist discovery must not park an account") + require.Zero(t, f.repo.credentialWrites.Load(), "whitelist discovery must not rotate credentials or recover tasks") + require.Zero(t, f.repo.extraWrites.Load(), "whitelist discovery must not write health or catalog state") + require.Zero(t, f.invalidator.calls.Load(), "whitelist discovery must not invalidate token caches") + require.Zero(t, f.repo.claimCalls) + require.False(t, f.gateway.isOpenAIAccountRuntimeBlocked(f.repo.account)) + require.Zero(t, f.gateway.openaiAccountRuntimeBlockSequence.Load(), "discovery must not mutate runtime health generations") +} + +func TestBatchWhitelistBPSNativeStructured401IsReadOnly(t *testing.T) { + for _, body := range []string{ + `{"error":{"code":"token_revoked","message":"synthetic revoked token"}}`, + `{"error":{"code":"token_invalidated"}}`, + `{"detail":{"message":"invalid token"}}`, + } { + t.Run(body, func(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + f.nativeStatus.Store(http.StatusUnauthorized) + f.nativeBody.Store(body) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + _, err = f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err) + require.EqualValues(t, 1, f.bpsCalls.Load()) + require.EqualValues(t, 1, f.nativeCalls.Load()) + require.Zero(t, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) + }) + } +} + +func TestBatchWhitelistBPSNativeStaleFailureKeepsMapping(t *testing.T) { + for _, admissionOwner := range []bool{false, true} { + t.Run(map[bool]string{false: "admin", true: "admission owner"}[admissionOwner], func(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"}]}`) + _, err := f.gateway.fetchNativeOpenAIModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + expireCodexModelsManifestCache(f.gateway, 2*time.Minute) + f.nativeStatus.Store(http.StatusServiceUnavailable) + f.nativeBody.Store(`{"error":{"code":"unavailable"}}`) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + ctx := context.Background() + if admissionOwner { + var cancel context.CancelFunc + ctx, cancel = WithAPIKeyAdmissionOwner(ctx) + defer cancel() + } + _, err = f.service.SyncUpstreamModelCatalogWithOptions(ctx, f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err, "stale native cache cannot authorize a replacement whitelist") + require.EqualValues(t, 2, f.nativeCalls.Load(), "this request must wait for its own native response") + require.Zero(t, f.repo.applyCalls, "failed live discovery must keep native-b and its alias") + f.assertNoAccountSideEffects(t, before) + }) + } +} + +func TestBatchWhitelistBPSNativeFreshCompleteSuccess(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + // Both caches contain old but still fresh display catalogs. A destructive + // sync must observe this round's successful responses from both sources. + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"}]}`) + f.bpsBody.Store(`{"allowed":true,"model_catalog":{"models":[{"id":"gpt-7-cached"}]}}`) + _, err := f.gateway.fetchNativeOpenAIModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + _, err = f.gateway.FetchExcelBPSModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"},{"slug":"gpt-native-b"}]}`) + f.bpsBody.Store(`{"allowed":true,"model_catalog":{"models":[{"id":"gpt-7-new"}]}}`) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + catalog, err := f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.NoError(t, err) + require.True(t, catalog.Applied) + assert.EqualValues(t, 2, f.nativeCalls.Load(), "native fresh cache must also be bypassed") + assert.EqualValues(t, 2, f.bpsCalls.Load(), "BPS fresh cache must also be bypassed") + require.Equal(t, map[string]string{ + "gpt-native-a": "gpt-native-a", "gpt-native-b": "gpt-native-b", + "native-alias": "gpt-native-b", "gpt-7-new": "gpt-7-new", + }, f.repo.mapping) + require.Equal(t, 1, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) +} + +func TestBatchWhitelistBPSFreshCacheCannotHideFailure(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + _, err := f.gateway.FetchExcelBPSModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + f.bpsStatus.Store(http.StatusUnauthorized) + f.bpsBody.Store(`{"error":{"code":"token_revoked"}}`) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + _, err = f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err, "fresh BPS display cache must not mask this round's failure") + require.EqualValues(t, 2, f.bpsCalls.Load()) + require.Zero(t, f.nativeCalls.Load()) + require.Zero(t, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) +} + +func TestBatchWhitelistBPSExpiredGrantDoesNotRefresh(t *testing.T) { + for _, rejected := range []bool{false, true} { + t.Run(map[bool]string{false: "expired", true: "rejected"}[rejected], func(t *testing.T) { + reauth, reader, grantRepo, updater, blocker := newExcelReauthTestService(t) + reader.account.Status = StatusActive + reader.account.Schedulable = true + reader.account.Extra = map[string]any{"openai_excel_bps": true, "openai_excel_bps_models": []string{"gpt-7-new"}} + expiry := time.Now().Add(-time.Hour) + if rejected { + expiry = time.Now().Add(time.Hour) + } + credentials := excelTestCredentials("read-only", expiry) + credentials[excelAccessTokenRejectedKey] = rejected + storeExcelTestCredentials(t, reauth, grantRepo, credentials) + refreshClient := &excelRefreshTestClient{credentials: excelTestCredentials("fresh", time.Now().Add(time.Hour))} + reauth.oauth.oauthClient = refreshClient + ciphertext := grantRepo.ciphertext + repo := &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: reader.account}} + gateway := newCodexModels401TestService(repo) + gateway.excelOAuthReauth = reauth + gateway.httpUpstream = &codexModelsHTTPUpstreamStub{do: func(*http.Request, string, int64, int) (*http.Response, error) { + t.Error("unready grant must not reach a catalog endpoint") + return nil, errors.New("synthetic catalog must not be called") + }} + svc := &AccountTestService{accountRepo: repo, openaiGatewayService: gateway} + _, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), reader.account, whitelistOnlyOptions(t)) + require.Error(t, err) + require.Zero(t, refreshClient.calls, "whitelist reads must not refresh expired/rejected grants") + require.Equal(t, ciphertext, grantRepo.ciphertext) + require.False(t, grantRepo.applied) + require.False(t, updater.applied) + require.Zero(t, blocker.clearedAccountID) + require.Zero(t, repo.applyCalls) + }) + } +} + +func TestBatchWhitelistBPSExpiredLegacyTokenDoesNotDisable(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + f.repo.account.Credentials["expires_at"] = time.Now().Add(-time.Hour).Format(time.RFC3339) + delete(f.repo.account.Credentials, "refresh_token") + f.gateway.openAITokenProvider = NewOpenAITokenProvider(f.repo, nil, nil) + f.gateway.openAITokenProvider.SetAccountRuntimeBlocker(f.gateway) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + _, err = f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err) + require.Zero(t, f.bpsCalls.Load()) + require.Zero(t, f.nativeCalls.Load()) + require.Zero(t, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) +} From ad4cd137683592dcba9623decd4d70fe924eb776 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 15:26:59 +0800 Subject: [PATCH 10/12] fix(accounts): reject identity tasks at whitelist sync entry --- ...am_batch_whitelist_identity_policy_test.go | 78 +++++++++++++++++++ .../service/upstream_model_mapping_sync.go | 5 ++ 2 files changed, 83 insertions(+) diff --git a/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go b/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go index 536716fe0366..8b60dfad9f46 100644 --- a/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go +++ b/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go @@ -4,6 +4,13 @@ package service import ( "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" "testing" "github.com/stretchr/testify/require" @@ -29,3 +36,74 @@ func TestBatchWhitelistNativePolicyRejectsTaskLifecycle(t *testing.T) { }) } } + +func TestBatchWhitelistSyncAgentIdentityTaskLifecycle(t *testing.T) { + for _, tc := range []struct { + name, task string + whitelist bool + }{ + {"whitelist without task", "", true}, + {"whitelist with task", "existing-task", true}, + {"legacy discovery still registers task", "", false}, + } { + t.Run(tc.name, func(t *testing.T) { + key, privateKey := newTestAgentIdentityKey(t) + account := newCodexModelsTestAccount() + account.Status, account.Schedulable = StatusActive, true + account.Credentials["auth_mode"] = OpenAIAuthModeAgentIdentity + account.Credentials["agent_runtime_id"] = key.runtimeID + account.Credentials["agent_private_key"] = privateKey + account.Credentials["task_id"] = tc.task + account.Credentials["model_mapping"] = map[string]any{"old": "old"} + require.False(t, account.IsExcelBPSEnabled(), "exercise the non-BPS sync entry") + before, err := json.Marshal(account) + require.NoError(t, err) + var registrations atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + registrations.Add(1) + _, _ = io.WriteString(w, `{"task_id":"registered-task"}`) + })) + original := openAIAgentIdentityAuthAPIBaseURL + openAIAgentIdentityAuthAPIBaseURL = server.URL + t.Cleanup(func() { openAIAgentIdentityAuthAPIBaseURL = original; server.Close() }) + repo := &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: account}} + ws := &agentIdentityWSInvalidationRecorder{} + upstream := &httpUpstreamRecorder{resp: &http.Response{ + StatusCode: http.StatusServiceUnavailable, Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(`{"error":"synthetic catalog failure"}`)), + }} + svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream, cfg: upstreamModelSyncTestConfig(), agentIdentityWS: ws} + options := UpstreamModelSyncOptions{} + if tc.whitelist { + options = whitelistOnlyOptions(t) + } + _, err = svc.SyncUpstreamModelCatalogWithOptions(context.Background(), account, options) + require.Error(t, err) + if tc.whitelist { + var syncErr *UpstreamModelSyncError + require.True(t, errors.As(err, &syncErr)) + require.Equal(t, UpstreamModelSyncErrorUnsupported, syncErr.Kind) + require.Zero(t, registrations.Load(), "whitelist sync must reject before task registration") + require.Zero(t, repo.credentialWrites.Load(), "whitelist sync must not persist a task") + require.Empty(t, ws.accountIDs) + require.Empty(t, upstream.requests) + after, marshalErr := json.Marshal(account) + require.NoError(t, marshalErr) + require.JSONEq(t, string(before), string(after)) + } else { + // Legacy discovery retains task setup even if its subsequent + // model-list request fails; the guard is whitelist-only. + require.EqualValues(t, 1, registrations.Load()) + require.EqualValues(t, 1, repo.credentialWrites.Load()) + require.Equal(t, "registered-task", account.GetCredential("task_id")) + require.Equal(t, []int64{account.ID}, ws.accountIDs) + require.Len(t, upstream.requests, 1) + } + require.Zero(t, repo.authErrors.Load()) + require.Zero(t, repo.tempUnsched.Load()) + require.Zero(t, repo.extraWrites.Load()) + require.Zero(t, repo.applyCalls) + require.Zero(t, repo.claimCalls) + }) + } +} diff --git a/backend/internal/service/upstream_model_mapping_sync.go b/backend/internal/service/upstream_model_mapping_sync.go index a318ec8e8b05..03ed38c7f501 100644 --- a/backend/internal/service/upstream_model_mapping_sync.go +++ b/backend/internal/service/upstream_model_mapping_sync.go @@ -147,6 +147,11 @@ func (s *AccountTestService) SyncUpstreamModelCatalogWithOptions(ctx context.Con // Mapping-only edits never refresh capability snapshots, observations, quality // state or the automatic cooldown. Use only the authenticated live listing. func (s *AccountTestService) fetchWhitelistCatalog(ctx context.Context, account *Account) (*UpstreamModelCatalog, error) { + if account.IsOpenAIAgentIdentity() { + // The non-BPS request builder can register and persist an identity task. + // Whitelist-only edits never own that credential lifecycle. + return nil, newUpstreamModelSyncUnsupportedError("Read-only model discovery does not support agent identity accounts", nil) + } var models []string var body []byte var err error From e18e9eb4fc6e07709d7e1671a78a918ac2053196 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 15:31:15 +0800 Subject: [PATCH 11/12] =?UTF-8?q?ci:=20=E5=AF=B9=E7=99=BD=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E5=8E=86=E5=8F=B2=E5=8F=8D=E4=BE=8B=E7=9A=84=E6=97=A0?= =?UTF-8?q?=E6=95=88=E5=A4=B1=E8=B4=A5=E6=98=BE=E5=BC=8F=E6=8B=92=E7=BB=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/batch-upstream-whitelist.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/batch-upstream-whitelist.yml b/.github/workflows/batch-upstream-whitelist.yml index b127a02e4ee9..3f9df09c1783 100644 --- a/.github/workflows/batch-upstream-whitelist.yml +++ b/.github/workflows/batch-upstream-whitelist.yml @@ -77,7 +77,7 @@ jobs: grep -F 'whitelist discovery must not disable an account' "$RUNNER_TEMP/read-policy-red.log" grep -F 'stale native cache cannot authorize a replacement whitelist' "$RUNNER_TEMP/read-policy-red.log" grep -F 'BPS fresh cache must also be bypassed' "$RUNNER_TEMP/read-policy-red.log" - ! grep -Eq 'build failed|setup failed|undefined:|no tests to run|panic:' "$RUNNER_TEMP/read-policy-red.log" + if grep -Eq 'build failed|setup failed|undefined:|no tests to run|panic:|WARNING: DATA RACE|--- SKIP:' "$RUNNER_TEMP/read-policy-red.log"; then exit 1; fi - name: Candidate handler and catalog policies working-directory: backend run: | From a7c06e639409a70c64d55977093db1e2592121ea Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Wed, 18 Nov 2026 15:46:36 +0800 Subject: [PATCH 12/12] ci: close historical model discovery fixture dependencies --- .github/workflows/account-request-proxy.yml | 13 ++++++++++++- .github/workflows/issue715-agent-recovery.yml | 18 +++++++++++++++++- .../workflows/quality-live-regressions.yml | 19 ++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/.github/workflows/account-request-proxy.yml b/.github/workflows/account-request-proxy.yml index 5d891bd36c62..f580e962b733 100644 --- a/.github/workflows/account-request-proxy.yml +++ b/.github/workflows/account-request-proxy.yml @@ -127,13 +127,24 @@ jobs: test "$(git hash-object "$target")" = "$baseline_blob" printf 'path=%s candidate_blob=%s restored_baseline_blob=%s\n' "$target" "$candidate_blob" "$baseline_blob" >> "$manifest" done + # Restoring the token lifecycle above removes its later read-only + # whitelist helper. Pin only the dependent catalog caller to the + # exact pre-helper source; keep the original config-switch defects. + caller_baseline=add606e964972ded3d6389790586c781c89a58c1 + target=backend/internal/service/openai_excel_bps_model_access.go + candidate_blob=$(git rev-parse "$candidate:$target") + test "$(git hash-object "$target")" = "$candidate_blob" + git show "$caller_baseline:$target" > "$target" + baseline_blob=$(git rev-parse "$caller_baseline:$target") + test "$(git hash-object "$target")" = "$baseline_blob" + printf 'compatibility_caller=%s baseline=%s candidate_blob=%s restored_baseline_blob=%s\n' "$target" "$caller_baseline" "$candidate_blob" "$baseline_blob" >> "$manifest" for file in pr885_config_switch_refresh_test.go pr885_siwc_config_switch_test.go pr885_oauth_config_switch_test.go; do target="backend/internal/service/$file" blob=$(git rev-parse "$candidate:$target") test "$(git hash-object "$target")" = "$blob" printf 'unchanged_candidate_fixture=%s blob=%s\n' "$target" "$blob" >> "$manifest" done - expected=$(printf '%s\n' backend/internal/service/openai_excel_oauth.go backend/internal/service/openai_maintenance_proxy.go backend/internal/service/openai_oauth_service.go backend/internal/service/openai_siwc.go backend/internal/service/openai_token_provider.go) + expected=$(printf '%s\n' backend/internal/service/openai_excel_bps_model_access.go backend/internal/service/openai_excel_oauth.go backend/internal/service/openai_maintenance_proxy.go backend/internal/service/openai_oauth_service.go backend/internal/service/openai_siwc.go backend/internal/service/openai_token_provider.go) test "$(git diff --name-only -- 'backend/**/*.go' ':!backend/**/*_test.go')" = "$expected" - name: Require all eight config switch failures to be behavioral shell: bash diff --git a/.github/workflows/issue715-agent-recovery.yml b/.github/workflows/issue715-agent-recovery.yml index 0844a7787598..f0a9c336d6ae 100644 --- a/.github/workflows/issue715-agent-recovery.yml +++ b/.github/workflows/issue715-agent-recovery.yml @@ -60,8 +60,9 @@ jobs: original=6df0cd5bb0ce4d774d34004008f9dd661d236468 bash .github/scripts/historical-recovery-timezone-callers.sh targets=(backend/internal/service/openai_codex_model_metadata.go backend/internal/service/openai_codex_models_service.go backend/internal/service/openai_excel_bps_models_manifest.go) + models_caller=backend/internal/service/openai_models_list.go git fetch --no-tags --depth=1 origin "$original" - trap 'git restore --source=HEAD -- "${targets[@]}" backend/internal/service/openai_request_timezone_egress.go backend/internal/service/openai_ws_client.go; git diff --exit-code' EXIT + trap 'git restore --source=HEAD -- "${targets[@]}" "$models_caller" backend/internal/service/openai_request_timezone_egress.go backend/internal/service/openai_ws_client.go; git diff --exit-code' EXIT git restore --source="$original" -- "${targets[@]}" for target in "${targets[@]}"; do expected=$(git rev-parse "$original:$target") @@ -69,6 +70,21 @@ jobs: echo "baseline=$original path=$target expected_blob=$expected tested_blob=$actual" test "$expected" = "$actual" done + # The historical manifest predates the whitelist-only policy API. + # Reconnect only its caller; retain candidate policy declarations and + # the exact historical capability implementation and test assertions. + python3 - "$models_caller" <<'PYTHON' + from pathlib import Path + import sys + path = Path(sys.argv[1]) + source = path.read_text() + current = 's.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, "", policy)' + historical = 's.fetchNativeCodexModelsManifest(ctx, account, clientVersion, "")' + if source.count(current) != 1: + raise SystemExit("missing or ambiguous historical manifest caller") + path.write_text(source.replace(current, historical, 1)) + PYTHON + echo "historical manifest compatibility caller path=$models_caller blob=$(git hash-object "$models_caller")" set +e (cd backend && go test -tags=unit ./internal/service -run '^TestIssue715(BridgeDoesNotAdvertiseEncryptedAgent|FetchedAndPinnedBridgeCapabilities|LiveBridgeCapabilityCannotOverrideWire)$' -count=1 -v -timeout=10m) > "$RUNNER_TEMP/issue715-red.log" 2>&1 result=$? diff --git a/.github/workflows/quality-live-regressions.yml b/.github/workflows/quality-live-regressions.yml index fa3e5e039e8c..91f190c4f6a0 100644 --- a/.github/workflows/quality-live-regressions.yml +++ b/.github/workflows/quality-live-regressions.yml @@ -36,15 +36,32 @@ jobs: set -euo pipefail sources=(backend/internal/service/quality_supported_models.go backend/internal/service/openai_codex_models_service.go backend/internal/service/pelican_scheduled.go) for source in "${sources[@]}"; do cp "$source" "$RUNNER_TEMP/$(basename "$source")"; done + models_caller=backend/internal/service/openai_models_list.go + cp "$models_caller" "$RUNNER_TEMP/quality-models-caller-current.go" # This later regression calls eligibleQualityModels, absent from the pinned # original sources. Keep it out of the historical overlay only; exercise # it explicitly with current sources in the next step. later_test=backend/internal/service/oauth_degradation_scope_test.go cp "$later_test" "$RUNNER_TEMP/oauth-degradation-scope-current.go" - trap 'for source in "${sources[@]}"; do cp "$RUNNER_TEMP/$(basename "$source")" "$source"; done; cp "$RUNNER_TEMP/oauth-degradation-scope-current.go" "$later_test"' EXIT + trap 'for source in "${sources[@]}"; do cp "$RUNNER_TEMP/$(basename "$source")" "$source"; done; cp "$RUNNER_TEMP/quality-models-caller-current.go" "$models_caller"; cp "$RUNNER_TEMP/oauth-degradation-scope-current.go" "$later_test"' EXIT rm "$later_test" git fetch --no-tags --depth=1 origin e082313679d5269eef3e3cce4101c22692695371 for source in "${sources[@]}"; do git show "e082313679d5269eef3e3cce4101c22692695371:$source" > "$source"; done + # Keep the selected historical manifest, with only its dependent + # caller using the API present in that baseline. Candidate policy + # declarations remain available to unrelated retained callers/tests. + python3 - "$models_caller" <<'PYTHON' + from pathlib import Path + import sys + path = Path(sys.argv[1]) + source = path.read_text() + current = 's.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, "", policy)' + historical = 's.fetchNativeCodexModelsManifest(ctx, account, clientVersion, "")' + if source.count(current) != 1: + raise SystemExit("missing or ambiguous historical manifest caller") + path.write_text(source.replace(current, historical, 1)) + PYTHON + echo "historical manifest compatibility caller path=$models_caller blob=$(git hash-object "$models_caller")" python3 .github/scripts/quality-live-red-compat.py "$RUNNER_TEMP/quality_supported_models.go" set +e (cd backend && go test -tags=unit ./internal/service -run '^TestQualityLive' -count=1 -v) > evidence/red-service.log 2>&1