← Back to Pull requests

소스 새로고침: 플랫폼별 공정한 재시도 + 남은 버그 수정 (#118 대체) #139

Closed
unknown closed this pull request without merging on Nov 22, 2026 01:06 UTC
View on GitHub
unknown unknown commented on Nov 22, 2026 00:28 UTC
Author

<!-- claude-projects-attribution: {"github_login":"torisKR"} -->
_Requested by toris · project thread_

무엇


draft #118(플랫폼별 공정한 재시도)의 커밋 4개를 그대로 가져오고, #118 본문에 "머지 전 해결 필요"로 남아 있던 문제를 고쳤습니다. 이 PR이 머지되면 #118은 닫습니다.

#118 자체는 세 가지를 합니다.

  • TikTok이 느리거나 실패해도 Instagram 확인이 밀리지 않습니다.

  • due 재시도 목록(전역 500개 상한)을 한 플랫폼이 다 채워도 다른 플랫폼 몫을 보장합니다.

  • 다른 출처나 다른 식별자의 복구 키는 해결하거나 덮어쓰지 않습니다.
  • 이번에 고친 것 (lib/sources/refresh.server.ts)


    1. OFFSET 페이지네이션 누락: 오래된 출처 선택을 플랫폼 그룹(YouTube+Shorts, TikTok, Instagram)마다 쿼리 한 번으로 바꿨습니다. last_checked_at, id 순서로 상한 + 여유분 50행까지 읽고, OFFSET은 쓰지 않습니다. 그래서 앞쪽 행이 바뀌어도 후보를 건너뛰지 않습니다.
    2. 충돌 키 무한 스캔: 주인 없는 플랫폼·ID 복구 키(source id가 없거나, 기록된 external id가 키와 다른 경우)에 해당하는 출처를 DB 쿼리에서 미리 제외합니다(staleRecoveryExclusion). 그래서 이런 행이 가장 오래된 자리를 계속 차지하며 정상 출처를 굶기지 못합니다.
    - 관리자 클라이언트는 PostgREST가 아니라 자체 플랜 클라이언트(lib/db/plan.ts)라서, 평평한 or 그룹만 씁니다(external_id.is.null,external_id.not.in.(...)). 이 그룹은 staleness 조건과 AND로 묶입니다.
    - external id가 없는 행은 제외하지 않으므로 식별자 격리로 이어질 수 있습니다.
    - 주인이 있는 키의 중복 출처는 메모리 안의 식별자 검사와 여유분 행이 처리합니다.
    3. 조회 실패가 다른 플랫폼 작업을 버림: 한 그룹의 오래된 출처 조회가 실패하거나 10초 타임아웃에 걸리면 그 그룹만 건너뜁니다. 나머지는 실행되고, 잡은 partial, 에러는 source_stale_lookup_failed:<group>입니다. due 후보 조회에도 같은 타임아웃을 걸었습니다.
    4. 플랫폼별 마감 시간: YouTube, TikTok, Instagram을 동시에 실행합니다. TikTok·Instagram 작업은 잡 시작 40초 뒤부터 새 출처를 시작하지 않습니다(cron 라우트는 60초, oEmbed 요청은 최대 8초). 남은 행은 건드리지 않고 다음 실행으로 넘기며, oembed.deferred_by_deadline에 개수를 남기고 잡은 partial입니다.
    5. 감사 기록에 recovery_unowned_keys를 추가했습니다. 문서는 docs/source-refresh-observability.md에 있습니다.

    마이그레이션은 없습니다.

    테스트


  • 추가한 테스트

  • - TikTok 조회가 실패해도 Instagram·YouTube는 실행됨
    - 그룹당 쿼리 1회, range 없음, limit 80/80/350, 타임아웃 적용
    - 제외 필터가 parseOrFilter로 파싱됨
    - 실제 플랜 클라이언트 + compileSelectPlan으로 세 쿼리가 SQL로 컴파일됨 (Codex 지적 반영. 예전 중첩 필터는 여기서 예외가 나는 것을 확인)
    - 40초 예산이 지난 뒤 세 번째 출처를 건드리지 않음
    - YouTube 배치가 대기 중일 때도 TikTok이 시작됨
  • 두 번째 OFFSET 페이지 실패를 다루던 테스트는 해당 경로가 없어져 지웠습니다.

  • tsc --noEmit 통과, pnpm lint 오류 0(기존 경고 11개), pnpm test 전체 3,535개 통과.
  • 남은 리스크


  • 실제 Postgres에서는 돌려 보지 않았습니다(컴파일까지만 확인). 배포 후 첫 refresh_media_sources job_runs에서 source_stale_lookup_failed가 없는지 확인이 필요합니다.

  • YouTube 그룹의 제외는 ID 기준이라, 주인 없는 YouTube 키와 ID가 같은 Shorts 행도 같이 보류됩니다(드문 경우).

  • #118이 언급한 동시 실행 중 쓰기 겹침과 캐시 일관성은 이 PR 범위 밖입니다.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01USnWowoLfxHffwBPb2TGMX

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Touches the scheduled media-source refresh path, recovery ledger writes, and selection queries; behavior changes are large but heavily tested and mostly degrade to partial runs rather than full failure.

    > Overview

    Refactors runSourceRefresh so stale picks use one bounded query per platform group (limit + headroom, 10s timeout, no OFFSET), with staleRecoveryExclusion keeping sources tied to unowned recovery keys out of the DB filter. Due retries are interleaved by platform, capped at four 500-id lookups within 15s, and mismatched keys are counted without consuming slots.

    > YouTube, TikTok, and Instagram now run in parallel; TikTok/Instagram stop starting new sources after 40s (deferred_by_deadline). Lookup failures mark the job partial per group instead of aborting the whole run.

    > Recovery handling adds identity-matching guards so mismatched platform/ID or TikTok provider-identity keys are not resolved or overwritten; inconclusive TikTok outcomes can record failures under the current platform key. Job detail gains recovery_unmatched_due_keys, recovery_unowned_keys, and recovery_conflicting_stale_sources, documented in docs/source-refresh-observability.md, with broad new unit coverage in refresh.server.test.ts.

    > <sup>Reviewed by Cursor Bugbot for commit cdd89e37b6c33da56a47334dce18e0caeb3bf445. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 523 lines (+392 / -131) across 6 files Reviewer: cursor[bot] Review duration: 38m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.53) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 523 lines diff across 6 files
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.375
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #139"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.8% (MEDIUM) · Higher = larger blast radius
    52.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.8% · Safe to merge
    22.8%