feat(asset-dedup): 素材去重机制6项优化 — 调参+警告+贪心分配 #1611

Merged
auto-approve-bot merged 2 commits from feat/asset-dedup-optimization into develop 2026-09-01 17:40:34 +08:00
Owner

改动

①②③ 调参(asset_segment_tracker.py)

  • MAX_RANGE_USE_COUNT: 3 → 2(单区间最多复用2次)
  • REUSE_RATIO_LIMIT: 0.15 → 0.10(复用占比上限收紧至10%)
  • SEGMENT_EDGE_GAP: 0.3 → 1.5(边缘间隙扩大避免首尾紧贴)

④ 成片级去重拦截(clips.py)

  • 新增 _calc_plan_internal_duplicate_rate() 辅助函数
  • 创建片段后估算成片内部重复率
  • 重复率 > 50% 时在 response 中返回 duplicate_warning

⑤ 素材耗尽提示(clips.py)

  • 追踪 all_assets_exhausted 标志
  • 素材区间不足时返回 exhaustion_warning,建议补充素材

⑥ 跨素材组合打散(clips.py)

  • 固定轮询 asset_ids[(i + offset) % n] → 按使用次数升序贪心分配
  • 同次数素材随机打散,避免固定组合反复出现

Schema 变更

  • ClipsFromAssetsResponse 新增 duplicate_warningexhaustion_warning 可选字段

测试

  • 更新 test_config_constants 适配新常量值
  • 更新 test_find_reusable_* 适配新 MAX_RANGE_USE_COUNT=2
  • 更新 test_conflict_* 适配新 SEGMENT_EDGE_GAP=1.5
  • test_assets_round_robin_assignmenttest_assets_balanced_assignment
  • 全部 107 个相关单测通过
## 改动 ### ①②③ 调参(asset_segment_tracker.py) - `MAX_RANGE_USE_COUNT`: 3 → 2(单区间最多复用2次) - `REUSE_RATIO_LIMIT`: 0.15 → 0.10(复用占比上限收紧至10%) - `SEGMENT_EDGE_GAP`: 0.3 → 1.5(边缘间隙扩大避免首尾紧贴) ### ④ 成片级去重拦截(clips.py) - 新增 `_calc_plan_internal_duplicate_rate()` 辅助函数 - 创建片段后估算成片内部重复率 - 重复率 > 50% 时在 response 中返回 `duplicate_warning` ### ⑤ 素材耗尽提示(clips.py) - 追踪 `all_assets_exhausted` 标志 - 素材区间不足时返回 `exhaustion_warning`,建议补充素材 ### ⑥ 跨素材组合打散(clips.py) - 固定轮询 `asset_ids[(i + offset) % n]` → 按使用次数升序贪心分配 - 同次数素材随机打散,避免固定组合反复出现 ### Schema 变更 - `ClipsFromAssetsResponse` 新增 `duplicate_warning` 和 `exhaustion_warning` 可选字段 ### 测试 - 更新 `test_config_constants` 适配新常量值 - 更新 `test_find_reusable_*` 适配新 MAX_RANGE_USE_COUNT=2 - 更新 `test_conflict_*` 适配新 SEGMENT_EDGE_GAP=1.5 - `test_assets_round_robin_assignment` → `test_assets_balanced_assignment` - 全部 107 个相关单测通过

🚀 预览环境已部署

项目 详情
PR号 #1611
预览链接 https://pr-1611.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1611 | | 预览链接 | [https://pr-1611.preview.xiaoxiajianji.com](https://pr-1611.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
xiaoxia force-pushed feat/asset-dedup-optimization from 22690e6a9f to 4e701abeeb 2026-09-01 16:57:00 +08:00 Compare
xiaoxia added 1 commit 2026-09-01 17:10:06 +08:00
feat(asset-dedup): 素材去重机制6项优化
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 5m5s
AI Code Review / AI Code Review (pull_request) Successful in 5m24s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 5m39s
PR Automation / Auto Approve on CI Green (pull_request) Has been cancelled
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1s
CI/CD Pipeline / Validate - Style (pull_request) Waiting to run
CI/CD Pipeline / Validate - Security (pull_request) Waiting to run
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Waiting to run
CI/CD Pipeline / Unit Tests (pull_request) Waiting to run
CI/CD Pipeline / Integration Tests (pull_request) Waiting to run
CI/CD Pipeline / Frontend Lint (pull_request) Waiting to run
CI/CD Pipeline / Frontend Unit Tests (pull_request) Waiting to run
CI/CD Pipeline / PR Build API Image (pull_request) Waiting to run
CI/CD Pipeline / PR Build Web Image (pull_request) Waiting to run
CI/CD Pipeline / PR Build Worker Image (pull_request) Waiting to run
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Waiting to run
CI/CD Pipeline / Build Staging Web Image (pull_request) Waiting to run
CI/CD Pipeline / Build Staging Worker Image (pull_request) Waiting to run
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Blocked by required conditions
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Blocked by required conditions
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Blocked by required conditions
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Blocked by required conditions
CI/CD Pipeline / Staging E2E Tests (pull_request) Blocked by required conditions
CI/CD Pipeline / Staging API Integration Tests (pull_request) Blocked by required conditions
CI/CD Pipeline / Build Production API Image (pull_request) Blocked by required conditions
CI/CD Pipeline / Build Production Web Image (pull_request) Blocked by required conditions
CI/CD Pipeline / Build Production Worker Image (pull_request) Blocked by required conditions
CI/CD Pipeline / Deploy Production (pull_request) Blocked by required conditions
CI/CD Pipeline / Production Browser E2E (pull_request) Blocked by required conditions
CI/CD Pipeline / ACR Image Cleanup (pull_request) Blocked by required conditions
CI/CD Pipeline / Canary Release to Production (pull_request) Blocked by required conditions
CI/CD Pipeline / CI Gate (pull_request) Blocked by required conditions
99a1b368a9
① MAX_RANGE_USE_COUNT 3→2:单区间最多复用2次
② REUSE_RATIO_LIMIT 0.15→0.10:复用占比上限收紧至10%
③ SEGMENT_EDGE_GAP 0.3→1.5:边缘间隙扩大避免首尾紧贴
④ 成片级去重拦截:dup_rate>50% 返回 duplicate_warning
⑤ 素材耗尽提示:区间不足时返回 exhaustion_warning
⑥ 跨素材组合打散:固定轮询改为按使用次数贪心+随机打散

同步更新 schemas(ClipsFromAssetsResponse 增加两个可选 warning 字段)
和相关单元测试(107 tests passed)。
xiaoxia force-pushed feat/asset-dedup-optimization from 6d34e3cdf9 to 99a1b368a9 2026-09-01 17:10:06 +08:00 Compare
auto-approve-bot added 1 commit 2026-09-01 17:16:54 +08:00
style: auto-format with black + isort + prettier [skip ci-format-check]
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 4m37s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 6m13s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 6m30s
AI Code Review / AI Code Review (pull_request) Failing after 7m51s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m9s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m13s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m28s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 4m10s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 4m11s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 4m43s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 8m12s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 1s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 3m35s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 3m53s
965d5cc2a5
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:是
  • 阻塞级问题数量:2 个

📊 审查概览

  • 整体评价:需修改
  • 建议级问题数量:2 个

🔴 阻塞级问题(必须修复)

  1. [apps/api/app/api/routes/templates_editor/clips.py: ~765] 素材分配循环缺少 break 语句

    • 问题类型:逻辑bug
    • 问题描述:在 for candidate in sorted_candidates: 循环中,当找到可用素材(candidate_start is not None)并设置 asset_id 等变量后,代码没有 break 跳出循环。这会导致循环继续执行,如果后续还有其他满足条件的素材,当前的 asset_id 会被覆盖。由于 sorted_candidates 是按“使用次数最少”排序的,这会导致最终选中的素材可能不是最优解(即不是使用次数最少的),破坏了贪心算法的均衡分配策略。
    • 修改建议:在 if candidate_start is not None: 判断块内的逻辑执行完毕后,添加 break 语句,确保选中第一个最优候选后立即停止搜索。
  2. [apps/api/app/api/routes/templates_editor/clips.py: ~768] 异常抛出与警告提示逻辑冲突

    • 问题类型:逻辑bug
    • 问题描述:代码在 if not asset_id or start_time is None: 判断块中设置了 all_assets_exhausted = True,随后立即抛出 HTTPException。这会导致函数直接终止,后续位于函数末尾的 if all_assets_exhausted and created_count < len(segments): 判断逻辑永远不会执行,新增的 exhaustion_warning 字段也永远不会被返回给客户端。这与 PR 意图(支持部分生成并返回警告)相悖。
    • 修改建议:应根据业务需求修改逻辑。如果目的是“允许部分生成并提示警告”,应将 raise HTTPException 替换为 break(跳出外层循环,停止生成后续片段),以便函数能正常执行到末尾并返回带有警告信息的响应对象。

💡 改进建议(不阻塞合并)

  1. [apps/api/app/api/routes/templates_editor/clips.py: ~730] 循环内排序的性能隐患

    • 具体内容:在外层循环 for i, ... in enumerate(segments) 内部,每次都执行 sorted(asset_ids, ...)。如果片段数量(N)和素材数量(M)较大(例如均超过100),这将导致 O(N * M * log M) 的时间复杂度。虽然当前场景下数据量可能不大,但从代码健壮性角度,建议使用优先队列(堆)来维护素材使用顺序,将复杂度降低至 O(N * log M)。
  2. [apps/api/app/services/asset_segment_tracker.py: 46] 边缘间隙配置值过大

    • 具体内容:SEGMENT_EDGE_GAP0.3 调整为 1.5(秒)。这是一个较大的缓冲区,意味着两个片段之间必须间隔 1.5 秒以上才被视为不冲突。在短视频或素材时长较短的场景下,这可能导致大量可用区间被判定为冲突,从而过早触发“素材耗尽”或强制复用逻辑。建议确认该值是否符合所有业务场景的需求,或考虑将其改为可配置参数。

良好实践

  • 新增的 _calc_plan_internal_duplicate_rate 函数逻辑清晰,通过排序和线性扫描高效计算了重叠时长,算法正确。
  • 测试用例及时更新了常量配置和预期行为,保证了代码修改的覆盖率。
  • 将素材分配策略从“轮询”改为“贪心(最少使用优先)”是合理的优化,能有效提升素材利用率。

🤖 由 AI 代码审查机器人自动生成 | 2026-09-01 09:24:45 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:是 - 阻塞级问题数量:2 个 ### 📊 审查概览 - 整体评价:需修改 - 建议级问题数量:2 个 ### 🔴 阻塞级问题(必须修复) 1. **[apps/api/app/api/routes/templates_editor/clips.py: ~765] 素材分配循环缺少 break 语句** - 问题类型:逻辑bug - 问题描述:在 `for candidate in sorted_candidates:` 循环中,当找到可用素材(`candidate_start is not None`)并设置 `asset_id` 等变量后,代码没有 `break` 跳出循环。这会导致循环继续执行,如果后续还有其他满足条件的素材,当前的 `asset_id` 会被覆盖。由于 `sorted_candidates` 是按“使用次数最少”排序的,这会导致最终选中的素材可能不是最优解(即不是使用次数最少的),破坏了贪心算法的均衡分配策略。 - 修改建议:在 `if candidate_start is not None:` 判断块内的逻辑执行完毕后,添加 `break` 语句,确保选中第一个最优候选后立即停止搜索。 2. **[apps/api/app/api/routes/templates_editor/clips.py: ~768] 异常抛出与警告提示逻辑冲突** - 问题类型:逻辑bug - 问题描述:代码在 `if not asset_id or start_time is None:` 判断块中设置了 `all_assets_exhausted = True`,随后立即抛出 `HTTPException`。这会导致函数直接终止,后续位于函数末尾的 `if all_assets_exhausted and created_count < len(segments):` 判断逻辑永远不会执行,新增的 `exhaustion_warning` 字段也永远不会被返回给客户端。这与 PR 意图(支持部分生成并返回警告)相悖。 - 修改建议:应根据业务需求修改逻辑。如果目的是“允许部分生成并提示警告”,应将 `raise HTTPException` 替换为 `break`(跳出外层循环,停止生成后续片段),以便函数能正常执行到末尾并返回带有警告信息的响应对象。 ### 💡 改进建议(不阻塞合并) 1. **[apps/api/app/api/routes/templates_editor/clips.py: ~730] 循环内排序的性能隐患** - 具体内容:在外层循环 `for i, ... in enumerate(segments)` 内部,每次都执行 `sorted(asset_ids, ...)`。如果片段数量(N)和素材数量(M)较大(例如均超过100),这将导致 O(N * M * log M) 的时间复杂度。虽然当前场景下数据量可能不大,但从代码健壮性角度,建议使用优先队列(堆)来维护素材使用顺序,将复杂度降低至 O(N * log M)。 2. **[apps/api/app/services/asset_segment_tracker.py: 46] 边缘间隙配置值过大** - 具体内容:`SEGMENT_EDGE_GAP` 从 `0.3` 调整为 `1.5`(秒)。这是一个较大的缓冲区,意味着两个片段之间必须间隔 1.5 秒以上才被视为不冲突。在短视频或素材时长较短的场景下,这可能导致大量可用区间被判定为冲突,从而过早触发“素材耗尽”或强制复用逻辑。建议确认该值是否符合所有业务场景的需求,或考虑将其改为可配置参数。 ### ✅ 良好实践 - 新增的 `_calc_plan_internal_duplicate_rate` 函数逻辑清晰,通过排序和线性扫描高效计算了重叠时长,算法正确。 - 测试用例及时更新了常量配置和预期行为,保证了代码修改的覆盖率。 - 将素材分配策略从“轮询”改为“贪心(最少使用优先)”是合理的优化,能有效提升素材利用率。 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-09-01 09:24:45 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
auto-approve-bot approved these changes 2026-09-01 17:40:04 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot approved these changes 2026-09-01 17:40:04 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot merged commit a0c14db33c into develop 2026-09-01 17:40:34 +08:00
auto-approve-bot deleted branch feat/asset-dedup-optimization 2026-09-01 17:40:35 +08:00

🗑️ 预览环境已清理

PR #1611 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1611 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.