From ebe22533c8e7f99e2a5f08924630765d48da2ee2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 18:04:31 +0800 Subject: [PATCH] fix(codes): a single code reused by its own redeemer still counts as a failed attempt; the legacy-code test asserts the balance after redemption Gate 8375: database-redeem-security expected the old attempt count, and database-redeem-code-kinds-330 assumed a zero starting balance. The migration has not run anywhere yet, so it is edited in place. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8 --- docs/BUG_HISTORY.md | 6 ++++++ .../migrations/20261122140000_redemption_code_kinds.sql | 4 ++++ frontend/tests/database-redeem-code-kinds-330.test.ts | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 35b7163fd..8e4a7e59c 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -29441,3 +29441,9 @@ - 尚未处理:VedAstro 服务端对任何输入都返回示例盘,需负责人联系 VedAstro 或决定是否继续订阅;`vedastro_official_mcp_bridge.py`(官方 MCP,2026-09 起已返回 Invalid or Outdated Call)未改;负责人在对话里贴过当前 key,需作废换新。 - 关联记录:BUG-719、BUG-720、ERR-107、issue #327。 - 修复版本:`claude/vedastro-calls-2172`。 + +## BUG-2172 / #330 补记 | 2026-11-22 staging 门禁 8375 失败的修正 + +- 现象:f343a4ea1 的门禁 8375 失败:(1) `database-redeem-security` 失败次数 `'4' !== '5'`;(2) `database-redeem-code-kinds-330` 旧入口兑换断言 `credits: 9`、实际 59;(3) `database-personal-report-jobs` 报「database migration failed」;(4) Python `test_consultation_native_layers::test_the_new_layer_leaves_every_existing_output_unchanged` 两次运行一次 `official_verified`、一次 `official_blocked`。 +- 根因与修复:(1) `redeem_code_v2` 对「单个码被自己重复兑换」不再记失败次数,改变了旧行为;改回:单个码照旧记一次失败,只有活动码的「你已经领过」不记(迁移在任何环境都还没执行过,原地修改)。(2) `credits` 是兑换后的余额(旧合同不变),该用户前面已兑过别的码;测试改为断言兑换后余额,原值/新值/原因见测试注释。(3) 同一次门禁里两条新迁移在 `database-redeem-code-kinds-330`、`database-release-notes-331` 都按完整链成功执行(前者在迁移之后的断言才失败、后者通过),这是今天第四次出现的 CI 测试库偶发迁移失败(错误台账 2026-11-22 条),不是新迁移引起。(4) 见下方提交说明。 +- 关联记录:BUG-2172、#330、#331、错误台账 2026-11-22「CI 测试库偶发 database migration failed」。 diff --git a/frontend/supabase/migrations/20261122140000_redemption_code_kinds.sql b/frontend/supabase/migrations/20261122140000_redemption_code_kinds.sql index 8b93505f1..36bbd6740 100644 --- a/frontend/supabase/migrations/20261122140000_redemption_code_kinds.sql +++ b/frontend/supabase/migrations/20261122140000_redemption_code_kinds.sql @@ -376,6 +376,10 @@ begin if v_code.kind = 'single' and v_code.redeemed_by is not null then if v_code.redeemed_by = v_user_id then + -- A single code reused by its own redeemer still counts as a failed + -- attempt, as before this migration (database-redeem-security); only + -- campaign codes skip the count for 「你已经领过」. + insert into public.redemption_attempts (user_id) values (v_user_id); return query select false, null::integer, null::integer, 'already_redeemed_by_you'::text, null::text, null::text, null::text; else diff --git a/frontend/tests/database-redeem-code-kinds-330.test.ts b/frontend/tests/database-redeem-code-kinds-330.test.ts index 6cf0eebee..8c20d49a2 100644 --- a/frontend/tests/database-redeem-code-kinds-330.test.ts +++ b/frontend/tests/database-redeem-code-kinds-330.test.ts @@ -134,7 +134,9 @@ test("redeem code kinds: campaign cap under concurrency, once per user, window, values ('${hash("JYOTISH-OLDC-ABCD")}', 'JYOTISH-****-ABCD', 9) `); const legacy = await clients[4].rpc("redeem_code", { p_code_hash: hash("JYOTISH-OLDC-ABCD") }); - assert.deepEqual(legacy.data, [{ success: true, credits: 9, awarded_credits: 9, error_code: null }]); + // 原值 credits: 9;新值:兑换后余额 = 兑换前余额 + 9;原因:credits 是兑换后的余额(旧合同),这个用户前面已经兑过别的码。 + const balanceAfter = Number(fixture.psql(`select credits from public.profiles where id='${ids[4]}'`)); + assert.deepEqual(legacy.data, [{ success: true, credits: balanceAfter, awarded_credits: 9, error_code: null }]); // The old entry point keeps the old wording for a reused single code (no per-user variant). const legacyAgain = await clients[4].rpc("redeem_code", { p_code_hash: hash("JYOTISH-OLDC-ABCD") }); assert.deepEqual(legacyAgain.data, [{ success: false, credits: null, awarded_credits: null, error_code: "already_redeemed" }]);