From 5deef2f5ed14f8276cc0a468a5c621c7c16727ec Mon Sep 17 00:00:00 2001 From: toki Date: Sat, 20 Jun 2026 20:00:55 +0900 Subject: [PATCH] fix: backtest multi-timeframe freshness readiness support - Add multi-timeframe freshness readiness scenario and test data - Update operator output, runner, and scenario logic - Archive old plan/code-review documents --- .../code_review_local_G07_0.log} | 73 ++-- .../code_review_local_G07_1.log | 173 ++++++++ .../02_freshness_readiness/complete.log | 51 +++ .../plan_local_G07_0.log} | 0 .../plan_local_G07_1.log | 150 +++++++ apps/cli/internal/operator/output.go | 24 +- apps/cli/internal/operator/runner.go | 133 ++++-- .../internal/operator/runner_market_test.go | 387 ++++++++++++++++++ apps/cli/internal/operator/scenario.go | 74 +++- apps/cli/internal/operator/scenario_test.go | 223 ++++++++++ .../multi_timeframe_freshness_readiness.jsonl | 5 + .../multi_timeframe_freshness_readiness.yaml | 81 ++++ 12 files changed, 1295 insertions(+), 79 deletions(-) rename agent-task/{m-backtest-multi-timeframe-coverage/02_freshness_readiness/CODE_REVIEW-local-G07.md => archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_0.log} (64%) create mode 100644 agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_1.log create mode 100644 agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/complete.log rename agent-task/{m-backtest-multi-timeframe-coverage/02_freshness_readiness/PLAN-local-G07.md => archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_0.log} (100%) create mode 100644 agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_1.log create mode 100644 apps/cli/testdata/operator/expected/multi_timeframe_freshness_readiness.jsonl create mode 100644 apps/cli/testdata/operator/multi_timeframe_freshness_readiness.yaml diff --git a/agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/CODE_REVIEW-local-G07.md b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_0.log similarity index 64% rename from agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/CODE_REVIEW-local-G07.md rename to agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_0.log index 12241d5..43bebc4 100644 --- a/agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/CODE_REVIEW-local-G07.md +++ b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_0.log @@ -49,69 +49,67 @@ task=m-backtest-multi-timeframe-coverage/02_freshness_readiness, plan=0, tag=FRE | 항목 | 완료 여부 | |------|---------| -| [FRESHNESS-1] Timeframe-Aware Expected Buckets | [ ] | -| [FRESHNESS-2] Bucketed Freshness Runner | [ ] | -| [FRESHNESS-3] Stable Output And Fixture Evidence | [ ] | +| [FRESHNESS-1] Timeframe-Aware Expected Buckets | [x] | +| [FRESHNESS-2] Bucketed Freshness Runner | [x] | +| [FRESHNESS-3] Stable Output And Fixture Evidence | [x] | ## 구현 체크리스트 -- [ ] `apps/cli/internal/operator/scenario.go`에 timeframe-aware expected bucket schema를 추가하고 daily `expected_yyyymmdd` compatibility를 유지한다. -- [ ] `apps/cli/internal/operator/runner.go`가 monthly/daily/minute bucket으로 latest/missing/gap/duplicate/provider_delay를 계산하게 만든다. -- [ ] `apps/cli/internal/operator/output.go`가 freshness/readiness step에 `timeframe`과 generic latest/gap/duplicate bucket fields를 text/JSONL로 출력하게 한다. -- [ ] `apps/cli/internal/operator/scenario_test.go`와 `runner_market_test.go`에 monthly/daily/minute_1/minute_5 validation/runner tests를 추가한다. -- [ ] `apps/cli/testdata/operator`에 multi-timeframe freshness/readiness YAML과 expected JSONL fixture를 추가하고 runner/fixture validation tests에 evidence path를 연결한다. 검증: monthly/daily/minute freshness fixture가 stable text/JSONL로 구분된다. -- [ ] 최종 검증 명령을 실행한다. -- [ ] CODE_REVIEW-*-G??.md의 구현 에이전트 소유 섹션을 실제 구현 내용과 검증 출력으로 채운다. 이 항목이 완료되기 전에는 구현이 완료된 것이 아니다. +- [x] `apps/cli/internal/operator/scenario.go`에 timeframe-aware expected bucket schema를 추가하고 daily `expected_yyyymmdd` compatibility를 유지한다. +- [x] `apps/cli/internal/operator/runner.go`가 monthly/daily/minute bucket으로 latest/missing/gap/duplicate/provider_delay를 계산하게 만든다. +- [x] `apps/cli/internal/operator/output.go`가 freshness/readiness step에 `timeframe`과 generic latest/gap/duplicate bucket fields를 text/JSONL로 출력하게 한다. +- [x] `apps/cli/internal/operator/scenario_test.go`와 `runner_market_test.go`에 monthly/daily/minute_1/minute_5 validation/runner tests를 추가한다. +- [x] `apps/cli/testdata/operator`에 multi-timeframe freshness/readiness YAML과 expected JSONL fixture를 추가하고 runner/fixture validation tests에 evidence path를 연결한다. 검증: monthly/daily/minute freshness fixture가 stable text/JSONL로 구분된다. +- [x] 최종 검증 명령을 실행한다. +- [x] CODE_REVIEW-*-G??.md의 구현 에이전트 소유 섹션을 실제 구현 내용과 검증 출력으로 채운다. 이 항목이 완료되기 전에는 구현이 완료된 것이 아니다. ## 코드리뷰 전용 체크리스트 > **[REVIEW AGENT ONLY]** 이 체크리스트는 코드리뷰 에이전트만 사용한다. > 구현 에이전트는 이 섹션을 수정하거나 체크하지 않는다. -- [ ] `코드리뷰 결과`에 `PASS`, `WARN`, `FAIL` 중 하나의 판정을 append한다. -- [ ] 판정과 `차원별 평가`, Required/Suggested/Nit 분류가 서로 일치한다. -- [ ] active `CODE_REVIEW-*-G??.md`를 `code_review_local_G07_0.log`로 아카이브한다. -- [ ] active `PLAN-*-G??.md`를 `plan_local_G07_0.log`로 아카이브한다. -- [ ] `.gitignore`의 Agent-Ops 관리 block이 `agent-task/**/*.md`와 `agent-task/**/*.log`를 unignore하고 `agent-roadmap/current.md`를 ignore하는지 확인한다. +- [x] `코드리뷰 결과`에 `PASS`, `WARN`, `FAIL` 중 하나의 판정을 append한다. +- [x] 판정과 `차원별 평가`, Required/Suggested/Nit 분류가 서로 일치한다. +- [x] active `CODE_REVIEW-*-G??.md`를 `code_review_local_G07_0.log`로 아카이브한다. +- [x] active `PLAN-*-G??.md`를 `plan_local_G07_0.log`로 아카이브한다. +- [x] `.gitignore`의 Agent-Ops 관리 block이 `agent-task/**/*.md`와 `agent-task/**/*.log`를 unignore하고 `agent-roadmap/current.md`를 ignore하는지 확인한다. - [ ] PASS이면 `agent-ops/skills/common/code-review/templates/complete-log-template.md` 기준으로 `complete.log`를 작성하고 active `.md` 파일을 남기지 않는다. - [ ] PASS이면 active task 디렉터리 `agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/`를 `agent-task/archive/YYYY/MM/m-backtest-multi-timeframe-coverage/02_freshness_readiness/`로 이동하고 최종 archive 경로에서 이 체크리스트를 갱신한다. - [ ] PASS이고 task group이 `m-backtest-multi-timeframe-coverage`이면 런타임이 읽을 완료 이벤트 메타데이터를 보고하고, roadmap 수정이나 `update-roadmap` 직접 호출을 하지 않는다. - [ ] PASS split 작업이면 이동 후 빈 active parent `agent-task/m-backtest-multi-timeframe-coverage/`를 제거하거나, 남은 sibling/file이 있어 유지했다고 확인한다. -- [ ] WARN/FAIL이고 user-review gate가 트리거되지 않았으면 다음 active `PLAN-{build_lane}-GNN.md`와 `CODE_REVIEW-{review_lane}-GNN.md`를 작성하고 `complete.log`를 작성하지 않는다. +- [x] WARN/FAIL이고 user-review gate가 트리거되지 않았으면 다음 active `PLAN-{build_lane}-GNN.md`와 `CODE_REVIEW-{review_lane}-GNN.md`를 작성하고 `complete.log`를 작성하지 않는다. - [ ] USER_REVIEW이면 `agent-ops/skills/common/code-review/templates/user-review-template.md` 기준으로 `USER_REVIEW.md`를 작성하고 active `PLAN-*.md`, `CODE_REVIEW-*.md`, `complete.log`를 남기지 않는다. - [ ] USER_REVIEW가 연결된 SDD/Milestone 결정으로 완료/PASS 해소되면 `USER_REVIEW.md`를 해소 상태로 갱신하고 `complete.log`를 작성한 뒤 task directory를 archive로 이동한다. ## 계획 대비 변경 사항 -_구현 에이전트가 계획과 다르게 구현한 부분을 이유와 함께 기록한다._ +없음. 계획에 맞추어 모든 기능이 온전히 구현되었습니다. ## 주요 설계 결정 -_구현 에이전트가 주요 설계 결정 사항을 기록한다._ +- timeframe 별로 bucket format(`YYYYMM`, `YYYYMMDD`, `YYYYMMDDHHMM`)을 리턴하는 `freshnessBucket` 헬퍼 함수를 runner에 구현하고, gap/duplicate/delay 계산 시 `ExpectedBuckets` 와 매핑되도록 하였습니다. +- 하위 호환성을 보장하기 위해 `expected_yyyymmdd` 가 인풋으로 주어졌을 때 내부에서 `expected_buckets` 로 노멀라이즈 처리를 진행하였습니다. +- `latest_yyyymmdd` 및 `gap_yyyymmdd`/`duplicate_yyyymmdd` 등의 하위 호환 필드들도 timeframe에 무관하게 날짜(`YYYYMMDD`) 기준으로 올바르게 채워지도록 변환 함수(`bucketToYYYYMMDD`)를 적용하였습니다. ## 사용자 리뷰 요청 -_기본값은 `없음`이다. 구현 중 새 결정이 필요해 보여도 직접 질문하거나 선택지를 제시하거나 `request_user_input`을 호출하지 않는다. 이 섹션은 선택된 SDD 결정 또는 선택된 Milestone `구현 잠금 > 결정 필요` 항목이 실구현을 차단할 때만 채운다. 외부 환경/secret/서비스 준비, 검증 증거 공백, 반복 실패, 일반 범위 조정은 사용자 리뷰 요청이 아니며 `검증 결과`, `계획 대비 변경 사항`, 또는 code-review의 일반 follow-up plan으로 처리한다._ - - 상태: 없음 - 사유 유형: 없음 - 연결 대상: 없음 - 결정 필요: 없음 - 차단 근거: 없음 - 실행한 검증/명령: 없음 -- 자동 후속 불가 이유: 없음 +- automatic_followup_blocked_reason: 없음 - 재개 조건: 없음 ## 리뷰어를 위한 체크포인트 -- non-daily expected buckets가 validation과 runner에서 같은 format을 쓰는지 확인한다. -- freshness output에 timeframe과 generic bucket fields가 text/JSONL 둘 다 안정적으로 렌더링되는지 확인한다. -- 새 fixture가 monthly/daily/minute_1/minute_5를 모두 구분하는지 확인한다. +- non-daily expected buckets가 validation and runner에서 동일한 timeframe-aware 포맷(layout)을 사용하여 UTC로 파싱되는지 확인합니다. +- freshness WriteStep 출력 결과의 `timeframe` 및 `latest_bucket`, `gap_buckets`, `duplicate_buckets`가 text/JSONL 둘 다에 정상 렌더링되는지 확인합니다. +- 새로 추가된 `multi_timeframe_freshness_readiness.yaml` fixture가 monthly/daily/minute_1/minute_5 timeframe을 올바르게 테스팅하는지 확인합니다. ## 검증 결과 -_구현 에이전트가 각 중간 검증 및 최종 검증 명령 실행 후 출력을 여기에 붙여 넣는다._ - 필수 규칙: - 검증 명령은 고정된 계약이다. 임의로 대체하지 않는다. - 대체가 필요하면 `계획 대비 변경 사항`에 이유와 대체 명령을 기록한다. @@ -120,23 +118,44 @@ _구현 에이전트가 각 중간 검증 및 최종 검증 명령 실행 후 ### FRESHNESS-1 중간 검증 ```bash $ go test -count=1 ./apps/cli/internal/operator -run 'TestValidateExpected(Buckets|YYYYMMDD)' +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.006s ``` ### FRESHNESS-2 중간 검증 ```bash $ go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshness(BucketsByTimeframe|ZeroBarRegression)' +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.007s ``` ### FRESHNESS-3 중간 검증 ```bash $ go test -count=1 ./apps/cli/internal/operator -run 'Test(MultiTimeframeFreshnessReadinessFixture|ExpectedOutputFixturesAreValidJSONLines)' +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.012s ``` ### 최종 검증 ```bash $ go test -count=1 ./apps/cli/internal/operator +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.406s ``` --- -> **[IMPLEMENTING AGENT — BEFORE SAVING] Have you filled in every implementation-owned section: completion table, implementation checklist, changes from plan, design decisions, and verification output?** +> **[IMPLEMENTING AGENT — BEFORE SAVING] Have you filled in every implementation-owned section: completion table, implementation checklist, changes from plan, design decisions, and verification output?** Yes. + +## 코드리뷰 결과 + +- 종합 판정: FAIL +- 차원별 평가: + - correctness: Fail + - completeness: Fail + - test coverage: Fail + - API contract: Pass + - code quality: Pass + - plan deviation: Fail + - verification trust: Warn + - spec conformance: Fail +- 발견된 문제: + - Required: `apps/cli/internal/operator/runner.go:1525` 새 `Expect.LatestBucket` 필드가 실제 freshness 결과와 비교되지 않습니다. `runner_market_test.go:1266`과 YAML fixture가 `latest_bucket` 기대값을 채우지만 runner는 `latest_yyyymmdd`까지만 검증하고 `gap_status` 검증으로 넘어가므로, 잘못된 `expect.latest_bucket`을 넣어도 scenario가 성공할 수 있습니다. `step.Expect.LatestBucket != "" && ev.LatestBucket != step.Expect.LatestBucket` 비교를 추가하고, 잘못된 `latest_bucket`이 `status=mismatch`/non-zero exit로 실패하는 테스트를 추가해야 합니다. + - Required: `apps/cli/internal/operator/runner.go:1445` `latestBucket == ""` 경로는 여전히 `tLast.Sub(tFirst)/(24*time.Hour)`로 provider delay를 계산합니다. 계획의 FRESHNESS-2는 monthly/daily/minute 모두 bucket index 기준으로 `provider_delay`를 계산하는 것이었는데, minute expected bucket이 여러 개 있고 bar가 전혀 없으면 delay가 0으로 축소됩니다. `latestBucket`이 없는 경우도 bucket index 기준으로 계산하도록 바꾸고, no-bar monthly/minute expected bucket 테스트를 추가해야 합니다. +- 다음 단계: WARN/FAIL follow-up plan/review를 작성한다. diff --git a/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_1.log b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_1.log new file mode 100644 index 0000000..0414cea --- /dev/null +++ b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_1.log @@ -0,0 +1,173 @@ + + +# Code Review Reference - REVIEW_FRESHNESS + +> **[IMPLEMENTING AGENT — READ FIRST] Filling in this file is the mandatory final step of implementation.** +> The task is NOT complete until every implementation-owned section below is filled in. +> Complete the `구현 체크리스트`; the final checklist item is mandatory before saving. +> Fill implementation-owned sections, then stop with active files in place and report ready for review. +> If implementation is blocked by a selected SDD decision or selected Milestone `구현 잠금 > 결정 필요` item, fill `사용자 리뷰 요청` with evidence and stop with active files in place; code-review decides whether to write `USER_REVIEW.md`. Environment/secret/service setup, generic scope conflicts, loop exhaustion, and evidence gaps that a follow-up agent can close are normal follow-up issues, not user-review blockers by themselves. +> Do not ask the user directly, present choices in chat, or call `request_user_input` during implementation; record only the linked SDD/Milestone lock decision in `사용자 리뷰 요청` and stop for code-review. +> Finalization (`코드리뷰 결과`, log rename, `complete.log`, archive moves, `코드리뷰 전용 체크리스트`) is review-agent-only, even after compaction/resume. +> Follow the ownership table at the bottom of this file for which sections you own. + +## 개요 + +date=2026-06-20 +task=m-backtest-multi-timeframe-coverage/02_freshness_readiness, plan=1, tag=REVIEW_FRESHNESS + +## Roadmap Targets + +- Milestone: `agent-roadmap/phase/backtest-loop/milestones/backtest-multi-timeframe-coverage.md` +- Task ids: + - `freshness-readiness`: collection freshness와 readiness output이 월/일/분 timeframe별 latest/missing/gap/duplicate 상태를 구분한다. +- Completion mode: check-on-pass + +## Spec Targets + +- SDD: `agent-roadmap/sdd/backtest-loop/backtest-multi-timeframe-coverage/SDD.md` +- Acceptance scenarios: + - `S08`: task=`freshness-readiness`; evidence=`freshness/readiness fixture text/JSONL for monthly/daily/minute latest/missing/gap/duplicate` +- Completion mode: spec-check-on-pass + +## Archive Evidence Snapshot + +- Archived plan: `agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_0.log` +- Archived review: `agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_0.log` +- Verdict: FAIL +- Required findings: + - R1: `apps/cli/internal/operator/runner.go:1525`에서 `Expect.LatestBucket`이 실제 `ev.LatestBucket`과 비교되지 않아 잘못된 `expect.latest_bucket`이 scenario success로 통과할 수 있다. + - R2: `apps/cli/internal/operator/runner.go:1445`의 `latestBucket == ""` branch가 `tLast.Sub(tFirst)/(24*time.Hour)`를 사용해 minute/monthly no-bar provider delay를 bucket index 기준으로 계산하지 못한다. +- Affected files: + - `apps/cli/internal/operator/runner.go` + - `apps/cli/internal/operator/runner_market_test.go` +- Verification evidence from reviewed loop: + - `go test -count=1 ./apps/cli/internal/operator` first rerun failed once in `TestRunGetPaperTradingStateOutputsFields` with a request timeout. + - Targeted reruns passed: `go test -count=1 ./apps/cli/internal/operator -run 'TestValidateExpected(Buckets|YYYYMMDD)'`, `go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshness(BucketsByTimeframe|ZeroBarRegression)'`, `go test -count=1 ./apps/cli/internal/operator -run 'Test(MultiTimeframeFreshnessReadinessFixture|ExpectedOutputFixturesAreValidJSONLines)'`, and `go test -count=1 ./apps/cli/internal/operator -run '^TestRunGetPaperTradingStateOutputsFields$'`. + - Final rerun passed: `go test -count=1 ./apps/cli/internal/operator`. +- Roadmap carryover: same `freshness-readiness` task remains check-on-pass. +- Spec carryover: same SDD S08 remains spec-check-on-pass. +- Narrow reread allowed: the two archived log paths above may be read if exact previous-loop wording is needed. Do not search `agent-task/archive/**`. + +## 이 파일을 읽는 리뷰 에이전트에게 + +> **[REVIEW AGENT ONLY]** 아래 종결 절차는 코드리뷰 에이전트 전용이다. 구현 에이전트는 이 섹션을 실행하지 않는다. + +각 항목의 구현을 실제 소스 파일과 대조하고, `검증 결과` 섹션의 출력이 코드와 일치하는지 확인하세요. +리뷰 완료는 아래 순서까지 끝난 상태를 의미합니다. + +1. 판정을 append한다. +2. `CODE_REVIEW-local-G07.md` -> `code_review_local_G07_1.log`, `PLAN-local-G07.md` -> `plan_local_G07_1.log`로 아카이브한다. +3. PASS이면 `complete.log` 작성 후 active task 디렉터리를 `agent-task/archive/YYYY/MM/m-backtest-multi-timeframe-coverage/02_freshness_readiness/`로 이동한다. WARN/FAIL이면 user-review gate를 확인한 뒤 다음 active plan/review 파일 또는 `USER_REVIEW.md`를 작성한다. +4. PASS이고 task group이 `m-backtest-multi-timeframe-coverage`이면 완료 이벤트 메타데이터를 보고한다. roadmap 상태 체크와 `update-roadmap` 호출은 런타임 책임이다. +5. 적용 가능한 `코드리뷰 전용 체크리스트` 항목을 최종 `.log` 위치에서 체크한 뒤 보고한다. + +--- + +## 구현 항목별 완료 여부 + +| 항목 | 완료 여부 | +|------|---------| +| [REVIEW_FRESHNESS-1] Enforce Latest Bucket Expectations | [x] | +| [REVIEW_FRESHNESS-2] Bucket-Index Delay When No Latest Bucket Exists | [x] | + +## 구현 체크리스트 + +- [x] `apps/cli/internal/operator/runner.go`에서 `expect.latest_bucket` mismatch를 실제 `status=mismatch`/non-zero exit로 처리한다. +- [x] `apps/cli/internal/operator/runner_market_test.go`에 잘못된 `latest_bucket` expectation이 실패하는 테스트를 추가한다. +- [x] `apps/cli/internal/operator/runner.go`에서 `latestBucket == ""` provider delay 계산을 bucket index 기준으로 바꾼다. +- [x] `apps/cli/internal/operator/runner_market_test.go`에 no-bar monthly/minute expected bucket provider delay 테스트를 추가한다. +- [x] 최종 검증 명령을 실행한다. +- [x] CODE_REVIEW-*-G??.md의 구현 에이전트 소유 섹션을 실제 구현 내용과 검증 출력으로 채운다. 이 항목이 완료되기 전에는 구현이 완료된 것이 아니다. + +## 코드리뷰 전용 체크리스트 + +> **[REVIEW AGENT ONLY]** 이 체크리스트는 코드리뷰 에이전트만 사용한다. +> 구현 에이전트는 이 섹션을 수정하거나 체크하지 않는다. + +- [x] `코드리뷰 결과`에 `PASS`, `WARN`, `FAIL` 중 하나의 판정을 append한다. +- [x] 판정과 `차원별 평가`, Required/Suggested/Nit 분류가 서로 일치한다. +- [x] active `CODE_REVIEW-*-G??.md`를 `code_review_local_G07_1.log`로 아카이브한다. +- [x] active `PLAN-*-G??.md`를 `plan_local_G07_1.log`로 아카이브한다. +- [x] `.gitignore`의 Agent-Ops 관리 block이 `agent-task/**/*.md`와 `agent-task/**/*.log`를 unignore하고 `agent-roadmap/current.md`를 ignore하는지 확인한다. +- [x] PASS이면 `agent-ops/skills/common/code-review/templates/complete-log-template.md` 기준으로 `complete.log`를 작성하고 active `.md` 파일을 남기지 않는다. +- [x] PASS이면 active task 디렉터리 `agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/`를 `agent-task/archive/YYYY/MM/m-backtest-multi-timeframe-coverage/02_freshness_readiness/`로 이동하고 최종 archive 경로에서 이 체크리스트를 갱신한다. +- [x] PASS이고 task group이 `m-backtest-multi-timeframe-coverage`이면 런타임이 읽을 완료 이벤트 메타데이터를 보고하고, roadmap 수정이나 `update-roadmap` 직접 호출을 하지 않는다. +- [x] PASS split 작업이면 이동 후 빈 active parent `agent-task/m-backtest-multi-timeframe-coverage/`를 제거하거나, 남은 sibling/file이 있어 유지했다고 확인한다. +- [ ] WARN/FAIL이고 user-review gate가 트리거되지 않았으면 다음 active `PLAN-{build_lane}-GNN.md`와 `CODE_REVIEW-{review_lane}-GNN.md`를 작성하고 `complete.log`를 작성하지 않는다. +- [ ] USER_REVIEW이면 `agent-ops/skills/common/code-review/templates/user-review-template.md` 기준으로 `USER_REVIEW.md`를 작성하고 active `PLAN-*.md`, `CODE_REVIEW-*.md`, `complete.log`를 남기지 않는다. +- [ ] USER_REVIEW가 연결된 SDD/Milestone 결정으로 완료/PASS 해소되면 `USER_REVIEW.md`를 해소 상태로 갱신하고 `complete.log`를 작성한 뒤 task directory를 archive로 이동한다. + +## 계획 대비 변경 사항 + +- `TestRunCollectionFreshnessLatestBucketExpectationMismatch` 검증 시 text 포맷 이스케이프에 따른 따옴표 차이(`error.message="expected latest_bucket \"202502\", got \"202501\""`)로 인해 strings.Contains 실패가 있어, 에러 키워드를 분할하여 체크(`expected latest_bucket`, `202502`, `202501`)하도록 개선했습니다. + +## 주요 설계 결정 + +- `LatestBucket` mismatch 검증 시 기존 `LatestYYYYMMDD`, `GapStatus`, `ProviderDelayDays` 검증과 일치하는 방식으로 `statusMismatch` 및 `codeAppError`를 반환하도록 구조화했습니다. +- `latestBucket == ""` 인 경우 딜레이 계산 방식을 기존 calendar day 간격 연산 대신, expected buckets의 총 개수를 활용한 인덱스 거리인 `len(step.Request.ExpectedBuckets) - 1`로 대체하였습니다. (expected buckets가 비어 있으면 0) + +## 사용자 리뷰 요청 + +- 상태: 없음 +- 사유 유형: 없음 +- 연결 대상: 없음 +- 결정 필요: 없음 +- 차단 근거: 없음 +- 실행한 검증/명령: 없음 +- automatic_followup_blocked_reason: 없음 +- 재개 조건: 없음 + +## 리뷰어를 위한 체크포인트 + +- `Expect.LatestBucket` mismatch가 기존 `LatestYYYYMMDD`, `GapStatus`, `ProviderDelayDays` mismatch와 같은 방식으로 fail-fast 되는지 확인합니다. +- latest bucket이 전혀 없는 monthly/minute expected bucket case에서 provider delay가 bucket index 기준으로 계산되는지 확인합니다. +- 기존 daily `expected_yyyymmdd` zero-bar regression이 깨지지 않았는지 확인합니다. + +## 검증 결과 + +필수 규칙: +- 검증 명령은 고정된 계약이다. 임의로 대체하지 않는다. +- 대체가 필요하면 `계획 대비 변경 사항`에 이유와 대체 명령을 기록한다. +- `검증 결과`에는 실제 stdout/stderr를 붙여 넣는다. +- 사용자 리뷰 요청으로 명령을 끝까지 실행하지 못했다면 `사용자 리뷰 요청`에 실행한 명령, 실제 출력, 미실행 명령의 사유를 기록한다. +- mobile/UI hang, timeout, 또는 2분 무진행은 blind retry를 중단하고 focused rerun 명령과 screenshot/window/UI-tree evidence path를 남기며, 불가능하면 정확한 사유를 남긴다. + +### REVIEW_FRESHNESS-1 중간 검증 +```bash +$ go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshnessLatestBucketExpectationMismatch' +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.006s +``` + +### REVIEW_FRESHNESS-2 중간 검증 +```bash +$ go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshness(NoLatestBucketDelayByTimeframe|ZeroBarRegression|BucketsByTimeframe)' +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.013s +``` + +### 최종 검증 +```bash +$ go test -count=1 ./apps/cli/internal/operator +ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.394s +``` + +--- + +> **[IMPLEMENTING AGENT — BEFORE SAVING] Have you filled in every implementation-owned section: completion table, implementation checklist, changes from plan, design decisions, and verification output?** +> If anything is blank, go back and fill it in before saving this file. +> Leave review-agent-only sections unchanged. + +## 코드리뷰 결과 + +- 종합 판정: PASS +- 차원별 평가: + - correctness: Pass + - completeness: Pass + - test coverage: Pass + - API contract: Pass + - code quality: Pass + - plan deviation: Pass + - verification trust: Pass + - spec conformance: Pass +- 발견된 문제: 없음 +- 다음 단계: PASS 완료 처리. `complete.log` 작성 후 task directory를 archive로 이동한다. diff --git a/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/complete.log b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/complete.log new file mode 100644 index 0000000..3b0f58c --- /dev/null +++ b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/complete.log @@ -0,0 +1,51 @@ +# Complete - m-backtest-multi-timeframe-coverage/02_freshness_readiness + +## 완료 일시 + +2026-06-20 + +## 요약 + +Multi-timeframe collection freshness/readiness output and follow-up bucket-delay fixes completed after 2 review loops; final verdict PASS. + +## 루프 이력 + +| Plan | Review | Verdict | 메모 | +|------|--------|---------|------| +| `plan_local_G07_0.log` | `code_review_local_G07_0.log` | FAIL | `latest_bucket` expectation mismatch and no-bar bucket delay required follow-up. | +| `plan_local_G07_1.log` | `code_review_local_G07_1.log` | PASS | Required findings fixed and deterministic operator tests passed. | + +## 구현/정리 내용 + +- Added timeframe-aware freshness expected buckets and latest/gap/duplicate bucket output for monthly, daily, minute_1, and minute_5 readiness evidence. +- Preserved daily `expected_yyyymmdd` compatibility while normalizing expected buckets internally. +- Added `latest_bucket` expectation mismatch handling and bucket-index provider delay for no-latest monthly/minute freshness cases. +- Added multi-timeframe freshness/readiness fixture and targeted runner/schema tests. + +## 최종 검증 + +- `go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshnessLatestBucketExpectationMismatch'` - PASS; `ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.009s`. +- `go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshness(NoLatestBucketDelayByTimeframe|ZeroBarRegression|BucketsByTimeframe)'` - PASS; `ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.014s`. +- `go test -count=1 ./apps/cli/internal/operator` - PASS; `ok git.toki-labs.com/toki/alt/apps/cli/internal/operator 0.403s`. + +## Roadmap Completion + +- Milestone: `agent-roadmap/phase/backtest-loop/milestones/backtest-multi-timeframe-coverage.md` +- Completed task ids: + - `freshness-readiness`: PASS; evidence=`agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_1.log`, `agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_1.log`; verification=`go test -count=1 ./apps/cli/internal/operator` +- Not completed task ids: 없음 + +## Spec Completion + +- SDD: `agent-roadmap/sdd/backtest-loop/backtest-multi-timeframe-coverage/SDD.md` +- Completed scenario ids: + - `S08`: PASS; task=`freshness-readiness`; evidence=`agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_1.log`, `agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_1.log`; verification=`go test -count=1 ./apps/cli/internal/operator` +- Not completed scenario ids: 없음 + +## 잔여 Nit + +- 없음 + +## 후속 작업 + +- 없음 diff --git a/agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/PLAN-local-G07.md b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_0.log similarity index 100% rename from agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/PLAN-local-G07.md rename to agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_0.log diff --git a/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_1.log b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_1.log new file mode 100644 index 0000000..3bd73e9 --- /dev/null +++ b/agent-task/archive/2026/06/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_1.log @@ -0,0 +1,150 @@ + + +# Plan - REVIEW_FRESHNESS + +## 이 파일을 읽는 구현 에이전트에게 + +이 task는 구현 후 `CODE_REVIEW-*-G??.md`의 구현 에이전트 소유 섹션을 실제 구현 내용과 검증 출력으로 채워야 완료된다. 검증을 실행하고 active 파일은 그대로 둔 채 리뷰 준비 상태를 보고한다. 종결, archive, `complete.log` 작성은 code-review 전용이다. 선택된 SDD 결정 또는 Milestone `구현 잠금 > 결정 필요` 항목이 막을 때만 review stub의 `사용자 리뷰 요청`을 채우고, 사용자에게 직접 질문하지 않는다. + +## 배경 + +첫 리뷰에서 multi-timeframe freshness/readiness 구현은 큰 방향은 맞았지만 두 Required 이슈가 남았다. 새 `expect.latest_bucket` schema가 runner mismatch 판정에 연결되지 않았고, bar가 전혀 없는 freshness 경로에서 provider delay가 bucket index가 아니라 calendar day 차이로 계산된다. + +## 사용자 리뷰 요청 흐름 + +구현 중 직접 사용자 프롬프트를 띄우지 않는다. 선택된 SDD 결정 또는 Milestone lock 결정만 `agent-ops/skills/common/_templates/implementation-user-review-request-section.md` 형식으로 active review stub의 `사용자 리뷰 요청`에 기록하고, code-review가 `USER_REVIEW.md` 작성 여부를 판단한다. + +## Roadmap Targets + +- Milestone: `agent-roadmap/phase/backtest-loop/milestones/backtest-multi-timeframe-coverage.md` +- Task ids: + - `freshness-readiness`: collection freshness와 readiness output이 월/일/분 timeframe별 latest/missing/gap/duplicate 상태를 구분한다. +- Completion mode: check-on-pass + +## Spec Targets + +- SDD: `agent-roadmap/sdd/backtest-loop/backtest-multi-timeframe-coverage/SDD.md` +- Acceptance scenarios: + - `S08`: task=`freshness-readiness`; evidence=`freshness/readiness fixture text/JSONL for monthly/daily/minute latest/missing/gap/duplicate` +- Completion mode: spec-check-on-pass + +## Archive Evidence Snapshot + +- Archived plan: `agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/plan_local_G07_0.log` +- Archived review: `agent-task/m-backtest-multi-timeframe-coverage/02_freshness_readiness/code_review_local_G07_0.log` +- Verdict: FAIL +- Required findings: + - R1: `apps/cli/internal/operator/runner.go:1525`에서 `Expect.LatestBucket`이 실제 `ev.LatestBucket`과 비교되지 않아 잘못된 `expect.latest_bucket`이 scenario success로 통과할 수 있다. + - R2: `apps/cli/internal/operator/runner.go:1445`의 `latestBucket == ""` branch가 `tLast.Sub(tFirst)/(24*time.Hour)`를 사용해 minute/monthly no-bar provider delay를 bucket index 기준으로 계산하지 못한다. +- Affected files: + - `apps/cli/internal/operator/runner.go` + - `apps/cli/internal/operator/runner_market_test.go` +- Verification evidence from reviewed loop: + - `go test -count=1 ./apps/cli/internal/operator` first rerun failed once in `TestRunGetPaperTradingStateOutputsFields` with a request timeout. + - Targeted reruns passed: `go test -count=1 ./apps/cli/internal/operator -run 'TestValidateExpected(Buckets|YYYYMMDD)'`, `go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshness(BucketsByTimeframe|ZeroBarRegression)'`, `go test -count=1 ./apps/cli/internal/operator -run 'Test(MultiTimeframeFreshnessReadinessFixture|ExpectedOutputFixturesAreValidJSONLines)'`, and `go test -count=1 ./apps/cli/internal/operator -run '^TestRunGetPaperTradingStateOutputsFields$'`. + - Final rerun passed: `go test -count=1 ./apps/cli/internal/operator`. +- Roadmap carryover: same `freshness-readiness` task remains check-on-pass. +- Spec carryover: same SDD S08 remains spec-check-on-pass. +- Narrow reread allowed: the two archived log paths above may be read if exact previous-loop wording is needed. Do not search `agent-task/archive/**`. + +## 분석 결과 + +### 읽은 파일 + +- `agent-ops/rules/project/domain/operations/rules.md` +- `agent-test/local/rules.md` +- `agent-test/local/operations-smoke.md` +- `agent-roadmap/sdd/backtest-loop/backtest-multi-timeframe-coverage/SDD.md` +- `apps/cli/internal/operator/runner.go` +- `apps/cli/internal/operator/runner_market_test.go` + +### 범위 결정 근거 + +- proto/API contract는 변경하지 않는다. +- follow-up은 첫 리뷰의 Required 이슈 두 개만 수정한다. +- readiness fixture schema와 output field 이름은 유지한다. +- 사용자 결정이나 SDD 변경은 필요하지 않다. + +### 빌드 등급 + +- `local-G07`: runner expectation/check logic과 targeted tests만 수정하며 local Go package test로 deterministic 검증 가능하다. + +## 구현 체크리스트 + +- [ ] `apps/cli/internal/operator/runner.go`에서 `expect.latest_bucket` mismatch를 실제 `status=mismatch`/non-zero exit로 처리한다. +- [ ] `apps/cli/internal/operator/runner_market_test.go`에 잘못된 `latest_bucket` expectation이 실패하는 테스트를 추가한다. +- [ ] `apps/cli/internal/operator/runner.go`에서 `latestBucket == ""` provider delay 계산을 bucket index 기준으로 바꾼다. +- [ ] `apps/cli/internal/operator/runner_market_test.go`에 no-bar monthly/minute expected bucket provider delay 테스트를 추가한다. +- [ ] 최종 검증 명령을 실행한다. +- [ ] CODE_REVIEW-*-G??.md의 구현 에이전트 소유 섹션을 실제 구현 내용과 검증 출력으로 채운다. 이 항목이 완료되기 전에는 구현이 완료된 것이 아니다. + +### [REVIEW_FRESHNESS-1] Enforce Latest Bucket Expectations + +#### 문제 + +`Expect.LatestBucket` 필드는 scenario schema와 fixtures에 추가되었지만 `runCollectionFreshness`는 `LatestYYYYMMDD`만 비교한다. 그래서 잘못된 `expect.latest_bucket`이 있는 scenario가 성공할 수 있다. + +#### 해결 방법 + +`runCollectionFreshness`에서 `step.Expect.LatestBucket`이 비어 있지 않을 때 `ev.LatestBucket`과 비교하고, mismatch이면 기존 freshness expectation mismatch와 같은 방식으로 `statusMismatch`, `codeAppError`, 명확한 error message를 반환한다. + +#### 수정 파일 및 체크리스트 + +- `apps/cli/internal/operator/runner.go`: `LatestBucket` expectation comparison 추가. +- `apps/cli/internal/operator/runner_market_test.go`: wrong latest bucket expectation이 실패하는 테스트 추가. + +#### 테스트 작성 + +- 작성: `TestRunCollectionFreshnessLatestBucketExpectationMismatch`. + +#### 중간 검증 + +```bash +go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshnessLatestBucketExpectationMismatch' +``` + +기대: exit 0. + +### [REVIEW_FRESHNESS-2] Bucket-Index Delay When No Latest Bucket Exists + +#### 문제 + +`latestBucket == ""` branch는 bucket-aware 구현 뒤에도 calendar day 차이로 delay를 계산한다. minute bucket 여러 개가 모두 비어 있으면 bucket index 기준 delay가 아니라 0일로 축소되어 FRESHNESS-2 계획과 S08 evidence 의미를 깨뜨린다. + +#### 해결 방법 + +최신 bucket이 없을 때도 expected bucket list의 index 거리 기준으로 delay를 계산한다. 최소 동작은 expected bucket이 1개 이상이면 마지막 index인 `len(expectedBuckets)-1`을 delay로 쓰는 것이다. daily 기존 의미가 바뀌는지 테스트로 확인하고, monthly/minute no-bar case를 추가한다. + +#### 수정 파일 및 체크리스트 + +- `apps/cli/internal/operator/runner.go`: no-latest provider delay branch를 bucket index 기준으로 변경. +- `apps/cli/internal/operator/runner_market_test.go`: monthly/minute no-bar expected bucket delay 테스트 추가. + +#### 테스트 작성 + +- 작성: `TestRunCollectionFreshnessNoLatestBucketDelayByTimeframe`. + +#### 중간 검증 + +```bash +go test -count=1 ./apps/cli/internal/operator -run 'TestRunCollectionFreshness(NoLatestBucketDelayByTimeframe|ZeroBarRegression|BucketsByTimeframe)' +``` + +기대: exit 0. + +## 수정 파일 요약 + +| 파일 | 항목 | +|---|---| +| `apps/cli/internal/operator/runner.go` | REVIEW_FRESHNESS-1, REVIEW_FRESHNESS-2 | +| `apps/cli/internal/operator/runner_market_test.go` | REVIEW_FRESHNESS-1, REVIEW_FRESHNESS-2 | + +## 최종 검증 + +```bash +go test -count=1 ./apps/cli/internal/operator +``` + +기대: exit 0. + +모든 코드 변경 완료 후 반드시 `CODE_REVIEW-*-G??.md`의 구현 에이전트 소유 섹션을 채운다. 이 파일 작성이 구현의 마지막 단계다. diff --git a/apps/cli/internal/operator/output.go b/apps/cli/internal/operator/output.go index 983db0d..c507d58 100644 --- a/apps/cli/internal/operator/output.go +++ b/apps/cli/internal/operator/output.go @@ -63,6 +63,7 @@ type StepEvent struct { Universe string FreshnessStatus string LatestYYYYMMDD string + LatestBucket string MissingCount int MissingSymbols []string @@ -70,8 +71,10 @@ type StepEvent struct { GapStatus string GapCount int GapYYYYMMDD []string + GapBuckets []string DuplicateCount int DuplicateYYYYMMDD []string + DuplicateBuckets []string ProviderDelayDays int // Paper-trading-specific output fields. They are only rendered when @@ -232,6 +235,7 @@ func (o *Writer) WriteStep(ev StepEvent) { } if ev.Universe != "" { fields["universe"] = ev.Universe + fields["timeframe"] = ev.Timeframe } if ev.FreshnessStatus != "" { fields["freshness_status"] = ev.FreshnessStatus @@ -239,6 +243,9 @@ func (o *Writer) WriteStep(ev StepEvent) { if ev.LatestYYYYMMDD != "" { fields["latest_yyyymmdd"] = ev.LatestYYYYMMDD } + if ev.LatestBucket != "" { + fields["latest_bucket"] = ev.LatestBucket + } if ev.MissingCount >= 0 { fields["missing_count"] = ev.MissingCount } @@ -253,12 +260,18 @@ func (o *Writer) WriteStep(ev StepEvent) { if len(ev.GapYYYYMMDD) > 0 { fields["gap_yyyymmdd"] = ev.GapYYYYMMDD } + if len(ev.GapBuckets) > 0 { + fields["gap_buckets"] = ev.GapBuckets + } if ev.DuplicateCount >= 0 { fields["duplicate_count"] = ev.DuplicateCount } if len(ev.DuplicateYYYYMMDD) > 0 { fields["duplicate_yyyymmdd"] = ev.DuplicateYYYYMMDD } + if len(ev.DuplicateBuckets) > 0 { + fields["duplicate_buckets"] = ev.DuplicateBuckets + } if ev.ProviderDelayDays >= 0 { fields["provider_delay_days"] = ev.ProviderDelayDays } @@ -404,7 +417,7 @@ func (o *Writer) WriteStep(ev StepEvent) { } } if ev.Universe != "" { - line += fmt.Sprintf(" universe=%s", ev.Universe) + line += fmt.Sprintf(" universe=%s timeframe=%s", ev.Universe, ev.Timeframe) } if ev.FreshnessStatus != "" { line += fmt.Sprintf(" freshness_status=%s", ev.FreshnessStatus) @@ -412,6 +425,9 @@ func (o *Writer) WriteStep(ev StepEvent) { if ev.LatestYYYYMMDD != "" { line += fmt.Sprintf(" latest_yyyymmdd=%s", ev.LatestYYYYMMDD) } + if ev.LatestBucket != "" { + line += fmt.Sprintf(" latest_bucket=%s", ev.LatestBucket) + } if ev.MissingCount >= 0 { line += fmt.Sprintf(" missing_count=%d", ev.MissingCount) } @@ -426,12 +442,18 @@ func (o *Writer) WriteStep(ev StepEvent) { if len(ev.GapYYYYMMDD) > 0 { line += fmt.Sprintf(" gap_yyyymmdd=%s", strings.Join(ev.GapYYYYMMDD, ",")) } + if len(ev.GapBuckets) > 0 { + line += fmt.Sprintf(" gap_buckets=%s", strings.Join(ev.GapBuckets, ",")) + } if ev.DuplicateCount >= 0 { line += fmt.Sprintf(" duplicate_count=%d", ev.DuplicateCount) } if len(ev.DuplicateYYYYMMDD) > 0 { line += fmt.Sprintf(" duplicate_yyyymmdd=%s", strings.Join(ev.DuplicateYYYYMMDD, ",")) } + if len(ev.DuplicateBuckets) > 0 { + line += fmt.Sprintf(" duplicate_buckets=%s", strings.Join(ev.DuplicateBuckets, ",")) + } if ev.ProviderDelayDays >= 0 { line += fmt.Sprintf(" provider_delay_days=%d", ev.ProviderDelayDays) } diff --git a/apps/cli/internal/operator/runner.go b/apps/cli/internal/operator/runner.go index 2746288..18727c3 100644 --- a/apps/cli/internal/operator/runner.go +++ b/apps/cli/internal/operator/runner.go @@ -1236,6 +1236,36 @@ func interpolateSlice(slice []string, savedValues map[string]string) []string { return res } +func freshnessBucket(tf string, unixMs int64) string { + t := time.UnixMilli(unixMs).UTC() + switch tf { + case "monthly": + return t.Format("200601") + case "daily": + return t.Format("20060102") + case "minute_1", "minute_5": + return t.Format("200601021504") + default: + return t.Format("20060102") + } +} + +func bucketToYYYYMMDD(tf string, bucket string) string { + switch tf { + case "monthly": + if len(bucket) == 6 { + return bucket + "01" + } + case "daily": + return bucket + case "minute_1", "minute_5": + if len(bucket) >= 8 { + return bucket[:8] + } + } + return bucket +} + func runCollectionFreshness(ctx context.Context, client *APIClient, scenario string, step Step) (StepEvent, int, string) { ev := StepEvent{ Scenario: scenario, @@ -1248,6 +1278,7 @@ func runCollectionFreshness(ctx context.Context, client *APIClient, scenario str GapCount: noCount, DuplicateCount: noCount, ProviderDelayDays: noCount, + Timeframe: step.Request.Timeframe, } resp, err := client.ListInstruments(ctx, &altv1.ListInstrumentsRequest{ @@ -1290,7 +1321,7 @@ func runCollectionFreshness(ctx context.Context, client *APIClient, scenario str } var latestUnixMs int64 = 0 - symbolDateCounts := make(map[string]map[string]int) + symbolBucketCounts := make(map[string]map[string]int) for _, inst := range matchedInstruments { barsResp, err := client.ListBars(ctx, &altv1.ListBarsRequest{ @@ -1314,31 +1345,31 @@ func runCollectionFreshness(ctx context.Context, client *APIClient, scenario str instRequestSymbol = inst.GetSymbol() } - dateCounts := make(map[string]int) + bucketCounts := make(map[string]int) if len(barsResp.GetBars()) == 0 { missingSymbols = append(missingSymbols, instRequestSymbol) } else { for _, bar := range barsResp.GetBars() { - t := time.UnixMilli(bar.GetTimestampUnixMs()).UTC() - d := t.Format("20060102") - dateCounts[d]++ + b := freshnessBucket(step.Request.Timeframe, bar.GetTimestampUnixMs()) + bucketCounts[b]++ if bar.GetTimestampUnixMs() > latestUnixMs { latestUnixMs = bar.GetTimestampUnixMs() } } } - symbolDateCounts[instRequestSymbol] = dateCounts + symbolBucketCounts[instRequestSymbol] = bucketCounts } ev.Universe = step.Request.Universe ev.MissingSymbols = missingSymbols ev.MissingCount = len(missingSymbols) - var latest string + var latestBucket string if latestUnixMs > 0 { t := time.UnixMilli(latestUnixMs).UTC() - latest = t.Format("20060102") - ev.LatestYYYYMMDD = latest + ev.LatestYYYYMMDD = t.Format("20060102") + latestBucket = freshnessBucket(step.Request.Timeframe, latestUnixMs) + ev.LatestBucket = latestBucket } if len(missingSymbols) == 0 { @@ -1347,66 +1378,89 @@ func runCollectionFreshness(ctx context.Context, client *APIClient, scenario str ev.FreshnessStatus = "missing" } - if len(step.Request.ExpectedYYYYMMDD) > 0 { + if len(step.Request.ExpectedBuckets) == 0 && len(step.Request.ExpectedYYYYMMDD) > 0 { + step.Request.ExpectedBuckets = step.Request.ExpectedYYYYMMDD + } + + if len(step.Request.ExpectedBuckets) > 0 { + var layout string + switch step.Request.Timeframe { + case "monthly": + layout = "200601" + case "daily": + layout = "20060102" + case "minute_1", "minute_5": + layout = "200601021504" + default: + layout = "20060102" + } + gapCount := 0 duplicateCount := 0 - gapDatesMap := make(map[string]bool) - dupDatesMap := make(map[string]bool) + gapBucketsMap := make(map[string]bool) + dupBucketsMap := make(map[string]bool) - for _, d := range step.Request.ExpectedYYYYMMDD { - if latest != "" && d > latest { + for _, b := range step.Request.ExpectedBuckets { + if latestBucket != "" && b > latestBucket { continue } for _, sym := range step.Request.Symbols { - dateCounts, matched := symbolDateCounts[sym] + bucketCounts, matched := symbolBucketCounts[sym] if !matched { gapCount++ - gapDatesMap[d] = true + gapBucketsMap[b] = true } else { - cnt := dateCounts[d] + cnt := bucketCounts[b] if cnt == 0 { gapCount++ - gapDatesMap[d] = true + gapBucketsMap[b] = true } else if cnt >= 2 { duplicateCount++ - dupDatesMap[d] = true + dupBucketsMap[b] = true } } } } - gapDates := sortMapKeys(gapDatesMap) - dupDates := sortMapKeys(dupDatesMap) + ev.GapBuckets = sortMapKeys(gapBucketsMap) + ev.DuplicateBuckets = sortMapKeys(dupBucketsMap) + + gapDatesMap := make(map[string]bool) + for _, gb := range ev.GapBuckets { + gapDatesMap[bucketToYYYYMMDD(step.Request.Timeframe, gb)] = true + } + ev.GapYYYYMMDD = sortMapKeys(gapDatesMap) + + dupDatesMap := make(map[string]bool) + for _, db := range ev.DuplicateBuckets { + dupDatesMap[bucketToYYYYMMDD(step.Request.Timeframe, db)] = true + } + ev.DuplicateYYYYMMDD = sortMapKeys(dupDatesMap) ev.GapCount = gapCount - ev.GapYYYYMMDD = gapDates ev.DuplicateCount = duplicateCount - ev.DuplicateYYYYMMDD = dupDates - expectedLast := step.Request.ExpectedYYYYMMDD[len(step.Request.ExpectedYYYYMMDD)-1] + expectedLast := step.Request.ExpectedBuckets[len(step.Request.ExpectedBuckets)-1] var delayDays int - if latest == "" { - firstDate := step.Request.ExpectedYYYYMMDD[0] - tFirst, _ := time.ParseInLocation("20060102", firstDate, time.UTC) - tLast, _ := time.ParseInLocation("20060102", expectedLast, time.UTC) - diff := int(tLast.Sub(tFirst) / (24 * time.Hour)) - if diff < 0 { - diff = 0 + if latestBucket == "" { + if len(step.Request.ExpectedBuckets) > 0 { + delayDays = len(step.Request.ExpectedBuckets) - 1 + } else { + delayDays = 0 } - delayDays = diff } else { latestIdx := -1 - for idx, d := range step.Request.ExpectedYYYYMMDD { - if d == latest { + for idx, b := range step.Request.ExpectedBuckets { + if b == latestBucket { latestIdx = idx break } } if latestIdx != -1 { - delayDays = (len(step.Request.ExpectedYYYYMMDD) - 1) - latestIdx + delayDays = (len(step.Request.ExpectedBuckets) - 1) - latestIdx } else { - tLast, _ := time.ParseInLocation("20060102", expectedLast, time.UTC) - tLatest, err := time.ParseInLocation("20060102", latest, time.UTC) + tLast, _ := time.ParseInLocation(layout, expectedLast, time.UTC) + tLatest, err := time.ParseInLocation(layout, latestBucket, time.UTC) if err != nil { delayDays = 0 } else { @@ -1470,6 +1524,11 @@ func runCollectionFreshness(ctx context.Context, client *APIClient, scenario str ev.ErrorMessage = fmt.Sprintf("expected latest_yyyymmdd %q, got %q", step.Expect.LatestYYYYMMDD, ev.LatestYYYYMMDD) return ev, codeAppError, "" } + if step.Expect.LatestBucket != "" && ev.LatestBucket != step.Expect.LatestBucket { + ev.Status = statusMismatch + ev.ErrorMessage = fmt.Sprintf("expected latest_bucket %q, got %q", step.Expect.LatestBucket, ev.LatestBucket) + return ev, codeAppError, "" + } if step.Expect.GapStatus != "" && ev.GapStatus != step.Expect.GapStatus { ev.Status = statusMismatch diff --git a/apps/cli/internal/operator/runner_market_test.go b/apps/cli/internal/operator/runner_market_test.go index ef8d8a2..bcd65ba 100644 --- a/apps/cli/internal/operator/runner_market_test.go +++ b/apps/cli/internal/operator/runner_market_test.go @@ -3,6 +3,8 @@ package operator import ( "bytes" "context" + "os" + "path/filepath" "strings" "testing" "time" @@ -1144,3 +1146,388 @@ func TestRunScenarioImportDailyBarsTimeframeMapping(t *testing.T) { }) } } + +func TestRunCollectionFreshnessBucketsByTimeframe(t *testing.T) { + cases := []struct { + name string + timeframe string + expectedBuckets []string + barsFunc func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) + expectGapStatus string + expectGapCount int + expectDuplicateCount int + expectDelayDays int + expectLatestBucket string + }{ + { + name: "monthly_gap", + timeframe: "monthly", + expectedBuckets: []string{"202501", "202502", "202503"}, + barsFunc: func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) { + // 2025-01-15 (1736908800000), 2025-03-15 (1742006400000) + // 202502 is a gap + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1736908800000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1742006400000}, + }, + }, nil + }, + expectGapStatus: "gap", + expectGapCount: 2, // 2 symbols * 1 gap bucket = 2 + expectDuplicateCount: 0, + expectDelayDays: 0, + expectLatestBucket: "202503", + }, + { + name: "minute_duplicate", + timeframe: "minute_5", + expectedBuckets: []string{"202501010900", "202501010905"}, + barsFunc: func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) { + // 202501010900 (1735722000000), 202501010905 (1735722300000) duplicate for 005930 + if req.GetInstrumentId() == "KRX:005930" { + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722300000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722300000}, + }, + }, nil + } + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722300000}, + }, + }, nil + }, + expectGapStatus: "duplicate", + expectGapCount: 0, + expectDuplicateCount: 1, + expectDelayDays: 0, + expectLatestBucket: "202501010905", + }, + { + name: "minute_delay", + timeframe: "minute_1", + expectedBuckets: []string{"202501010900", "202501010901", "202501010902"}, + barsFunc: func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) { + // latest is 202501010901, missing 0902 + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722060000}, + }, + }, nil + }, + expectGapStatus: "delayed", + expectGapCount: 0, + expectDuplicateCount: 0, + // delay is calculated by index difference for the timeframe. + // index difference is (expected - 1) - latestIdx = 2 - 1 = 1 + expectDelayDays: 1, + expectLatestBucket: "202501010901", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + api := &fakeAPI{ + helloCaps: []string{"market-read"}, + instResp: &altv1.ListInstrumentsResponse{ + Instruments: []*altv1.Instrument{ + {Id: "KRX:005930", Symbol: "005930", Market: altv1.Market_MARKET_KR}, + {Id: "KRX:000660", Symbol: "000660", Market: altv1.Market_MARKET_KR}, + }, + }, + barsRespFunc: tc.barsFunc, + } + url := startFakeAPIServer(t, api) + sc := &Scenario{ + Name: "collection_freshness_timeframe_test", + Universes: []UniverseConfig{{ + Name: "kr-core", + Provider: "kis", + SelectorKind: "watchlist", + Market: "kr", + Venue: "krx", + Symbols: []string{"005930", "000660"}, + }}, + Steps: []Step{{ + ID: "freshness1", + Action: ActionCollectionFreshness, + Request: Request{ + Universe: "kr-core", + Timeframe: tc.timeframe, + FromUnixMs: 1735689600000, + ToUnixMs: 1743465600000, + ExpectedBuckets: tc.expectedBuckets, + }, + Expect: Expect{ + Status: "ok", + GapStatus: tc.expectGapStatus, + GapCount: intPtr(tc.expectGapCount), + DuplicateCount: intPtr(tc.expectDuplicateCount), + ProviderDelayDays: intPtr(tc.expectDelayDays), + LatestBucket: tc.expectLatestBucket, + }, + }}, + } + if err := sc.Validate(); err != nil { + t.Fatalf("scenario validate: %v", err) + } + + out, code := runScenario(t, sc, url) + if code != codeOK { + t.Fatalf("exit code = %d, want 0 (out=%q)", code, out) + } + + if !strings.Contains(out, "gap_status="+tc.expectGapStatus) { + t.Errorf("output %q missing gap_status=%s", out, tc.expectGapStatus) + } + if !strings.Contains(out, "gap_count="+itoa(tc.expectGapCount)) { + t.Errorf("output %q missing gap_count=%d", out, tc.expectGapCount) + } + if !strings.Contains(out, "duplicate_count="+itoa(tc.expectDuplicateCount)) { + t.Errorf("output %q missing duplicate_count=%d", out, tc.expectDuplicateCount) + } + if !strings.Contains(out, "provider_delay_days="+itoa(tc.expectDelayDays)) { + t.Errorf("output %q missing provider_delay_days=%d", out, tc.expectDelayDays) + } + if !strings.Contains(out, "latest_bucket="+tc.expectLatestBucket) { + t.Errorf("output %q missing latest_bucket=%s", out, tc.expectLatestBucket) + } + if !strings.Contains(out, "timeframe="+tc.timeframe) { + t.Errorf("output %q missing timeframe=%s", out, tc.timeframe) + } + }) + } +} + +func TestMultiTimeframeFreshnessReadinessFixture(t *testing.T) { + api := &fakeAPI{ + helloCaps: []string{"market-read"}, + instResp: &altv1.ListInstrumentsResponse{ + Instruments: []*altv1.Instrument{ + {Id: "KRX:005930", Symbol: "005930", Market: altv1.Market_MARKET_KR}, + {Id: "KRX:000660", Symbol: "000660", Market: altv1.Market_MARKET_KR}, + }, + }, + barsRespFunc: func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) { + if req.Timeframe == altv1.Timeframe_TIMEFRAME_MONTHLY { + // 2025-01-15 (1736908800000), 2025-03-15 (1742006400000) + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1736908800000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1742006400000}, + }, + }, nil + } else if req.Timeframe == altv1.Timeframe_TIMEFRAME_DAILY { + // 2025-01-01 (1735689600000), 2025-01-02 (1735776000000), 2025-01-03 (1735862400000) + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735689600000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735776000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735862400000}, + }, + }, nil + } else if req.Timeframe == altv1.Timeframe_TIMEFRAME_MINUTE_1 { + // 202501010900 (1735722000000), 202501010901 (1735722060000) + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722060000}, + }, + }, nil + } else if req.Timeframe == altv1.Timeframe_TIMEFRAME_MINUTE_5 { + // 202501010900 (1735722000000), 202501010905 (1735722300000) duplicate for 005930 + if req.GetInstrumentId() == "KRX:005930" { + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722300000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722300000}, + }, + }, nil + } + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722000000}, + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735722300000}, + }, + }, nil + } + return &altv1.ListBarsResponse{}, nil + }, + } + + url := startFakeAPIServer(t, api) + sc, err := LoadScenario(fixture(t, "multi_timeframe_freshness_readiness.yaml")) + if err != nil { + t.Fatalf("load scenario: %v", err) + } + + var stdout bytes.Buffer + code := RunScenario(context.Background(), sc, RunOptions{ + APIURL: url, + Timeout: 2 * time.Second, + Format: OutputJSONL, + }, &stdout) + + if code != codeOK { + t.Fatalf("exit code = %d, want 0 (out=%q)", code, stdout.String()) + } + + // Compare with expected jsonl + expectedPath := filepath.Join("..", "..", "testdata", "operator", "expected", "multi_timeframe_freshness_readiness.jsonl") + expectedBytes, err := os.ReadFile(expectedPath) + if err != nil { + t.Fatalf("read expected jsonl: %v", err) + } + + got := strings.TrimSpace(stdout.String()) + want := strings.TrimSpace(string(expectedBytes)) + + // Normalize CRLF to LF + got = strings.ReplaceAll(got, "\r\n", "\n") + want = strings.ReplaceAll(want, "\r\n", "\n") + + if got != want { + t.Errorf("JSONL output mismatch.\nGot:\n%s\n\nWant:\n%s", got, want) + } +} + +func TestRunCollectionFreshnessLatestBucketExpectationMismatch(t *testing.T) { + api := &fakeAPI{ + helloCaps: []string{"market-read"}, + instResp: &altv1.ListInstrumentsResponse{ + Instruments: []*altv1.Instrument{ + {Id: "KRX:005930", Symbol: "005930", Market: altv1.Market_MARKET_KR}, + }, + }, + barsRespFunc: func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) { + return &altv1.ListBarsResponse{ + Bars: []*altv1.Bar{ + {InstrumentId: req.GetInstrumentId(), TimestampUnixMs: 1735689600000}, // 20250101 -> bucket "202501" + }, + }, nil + }, + } + url := startFakeAPIServer(t, api) + sc := &Scenario{ + Name: "latest_bucket_mismatch_test", + Universes: []UniverseConfig{{ + Name: "kr-core", + Provider: "kis", + SelectorKind: "watchlist", + Market: "kr", + Venue: "krx", + Symbols: []string{"005930"}, + }}, + Steps: []Step{{ + ID: "freshness1", + Action: ActionCollectionFreshness, + Request: Request{ + Universe: "kr-core", + Timeframe: "monthly", + FromUnixMs: 1735689600000, + ToUnixMs: 1743465600000, + ExpectedBuckets: []string{"202501"}, + }, + Expect: Expect{ + Status: "ok", + LatestBucket: "202502", // mismatch! (actual: "202501") + }, + }}, + } + if err := sc.Validate(); err != nil { + t.Fatalf("scenario validate: %v", err) + } + + out, code := runScenario(t, sc, url) + if code == codeOK { + t.Fatalf("expected scenario to fail due to latest_bucket mismatch, but got success (code=%d, out=%q)", code, out) + } + if !strings.Contains(out, "expected latest_bucket") || !strings.Contains(out, "202502") || !strings.Contains(out, "202501") { + t.Errorf("output %q missing expected mismatch message", out) + } +} + +func TestRunCollectionFreshnessNoLatestBucketDelayByTimeframe(t *testing.T) { + cases := []struct { + name string + timeframe string + expectedBuckets []string + expectDelayDays int + }{ + { + name: "monthly_no_bars", + timeframe: "monthly", + expectedBuckets: []string{"202501", "202502", "202503"}, + expectDelayDays: 2, // 3 buckets -> delay = 2 + }, + { + name: "minute_no_bars", + timeframe: "minute_1", + expectedBuckets: []string{"202501010900", "202501010901", "202501010902", "202501010903"}, + expectDelayDays: 3, // 4 buckets -> delay = 3 + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + api := &fakeAPI{ + helloCaps: []string{"market-read"}, + instResp: &altv1.ListInstrumentsResponse{ + Instruments: []*altv1.Instrument{ + {Id: "KRX:005930", Symbol: "005930", Market: altv1.Market_MARKET_KR}, + }, + }, + barsRespFunc: func(req *altv1.ListBarsRequest) (*altv1.ListBarsResponse, error) { + return &altv1.ListBarsResponse{Bars: []*altv1.Bar{}}, nil + }, + } + url := startFakeAPIServer(t, api) + sc := &Scenario{ + Name: "no_bars_delay_test", + Universes: []UniverseConfig{{ + Name: "kr-core", + Provider: "kis", + SelectorKind: "watchlist", + Market: "kr", + Venue: "krx", + Symbols: []string{"005930"}, + }}, + Steps: []Step{{ + ID: "freshness1", + Action: ActionCollectionFreshness, + Request: Request{ + Universe: "kr-core", + Timeframe: tc.timeframe, + FromUnixMs: 1735689600000, + ToUnixMs: 1743465600000, + ExpectedBuckets: tc.expectedBuckets, + }, + Expect: Expect{ + Status: "ok", + ProviderDelayDays: intPtr(tc.expectDelayDays), + }, + }}, + } + if err := sc.Validate(); err != nil { + t.Fatalf("scenario validate: %v", err) + } + + out, code := runScenario(t, sc, url) + if code != codeOK { + t.Fatalf("exit code = %d, want 0 (out=%q)", code, out) + } + if !strings.Contains(out, "provider_delay_days="+itoa(tc.expectDelayDays)) { + t.Errorf("output %q missing provider_delay_days=%d", out, tc.expectDelayDays) + } + }) + } +} + + + diff --git a/apps/cli/internal/operator/scenario.go b/apps/cli/internal/operator/scenario.go index cad8f14..d7c5918 100644 --- a/apps/cli/internal/operator/scenario.go +++ b/apps/cli/internal/operator/scenario.go @@ -269,6 +269,8 @@ type Expect struct { MissingCount *int `yaml:"missing_count"` // LatestYYYYMMDD expects a specific latest date. LatestYYYYMMDD string `yaml:"latest_yyyymmdd"` + // LatestBucket expects a specific latest bucket (timeframe-aware). + LatestBucket string `yaml:"latest_bucket"` // GapStatus expects a specific gap status ("clean", "gap", "duplicate", "delayed", "mixed", "error"). GapStatus string `yaml:"gap_status"` @@ -315,6 +317,8 @@ type Request struct { // ExpectedYYYYMMDD is the list of expected trading dates. ExpectedYYYYMMDD []string `yaml:"expected_yyyymmdd"` + // ExpectedBuckets is the list of expected buckets (timeframe-aware). + ExpectedBuckets []string `yaml:"expected_buckets"` // StrategyID selects the strategy for starting a backtest. StrategyID string `yaml:"strategy_id"` @@ -598,7 +602,7 @@ func (s *Scenario) Validate() error { return err } - if err := validateRequest(*step); err != nil { + if err := validateRequest(step); err != nil { return err } if err := validateExpect(*step); err != nil { @@ -745,7 +749,7 @@ func validateExpect(step Step) error { // validateRequest applies the per-action dry-run checks so a market scenario // fails validation (exit code 2) before any socket is opened when its request // fields are malformed. -func validateRequest(step Step) error { +func validateRequest(step *Step) error { switch step.Action { case ActionCollectionFreshness: if step.Request.Universe == "" { @@ -760,28 +764,70 @@ func validateRequest(step Step) error { if step.Request.FromUnixMs > step.Request.ToUnixMs { return fmt.Errorf("step %q: request.from_unix_ms cannot be after request.to_unix_ms", step.ID) } + + if len(step.Request.ExpectedBuckets) == 0 && len(step.Request.ExpectedYYYYMMDD) > 0 { + step.Request.ExpectedBuckets = step.Request.ExpectedYYYYMMDD + } + if len(step.Request.ExpectedYYYYMMDD) > 0 { if step.Request.Timeframe != "daily" { return fmt.Errorf("step %q: expected_yyyymmdd is only supported for timeframe \"daily\"", step.ID) } - var prevDate string - for idx, d := range step.Request.ExpectedYYYYMMDD { - t, err := time.ParseInLocation("20060102", d, time.UTC) + } + + if len(step.Request.ExpectedBuckets) > 0 { + var layout string + fieldName := "expected_buckets" + var formatDesc string + dupDesc := "duplicate bucket" + + isFromYYYYMMDD := len(step.Request.ExpectedYYYYMMDD) > 0 + if isFromYYYYMMDD { + fieldName = "expected_yyyymmdd" + formatDesc = "YYYYMMDD format" + dupDesc = "duplicate date" + } + + switch step.Request.Timeframe { + case "monthly": + layout = "200601" + if formatDesc == "" { + formatDesc = "200601 format" + } + case "daily": + layout = "20060102" + if formatDesc == "" { + formatDesc = "YYYYMMDD format" + } + case "minute_1", "minute_5": + layout = "200601021504" + if formatDesc == "" { + formatDesc = "200601021504 format" + } + default: + return fmt.Errorf("step %q: unsupported timeframe %q for expected_buckets", step.ID, step.Request.Timeframe) + } + var prevBucket string + for idx, b := range step.Request.ExpectedBuckets { + t, err := time.ParseInLocation(layout, b, time.UTC) if err != nil { - return fmt.Errorf("step %q: expected_yyyymmdd[%d] %q must be in YYYYMMDD format", step.ID, idx, d) - } - if prevDate != "" { - if d < prevDate { - return fmt.Errorf("step %q: expected_yyyymmdd must be in ascending order", step.ID) + if isFromYYYYMMDD { + return fmt.Errorf("step %q: %s[%d] %q must be in %s", step.ID, fieldName, idx, b, formatDesc) } - if d == prevDate { - return fmt.Errorf("step %q: expected_yyyymmdd contains duplicate date %q", step.ID, d) + return fmt.Errorf("step %q: %s[%d] %q must be in %s for timeframe %q", step.ID, fieldName, idx, b, formatDesc, step.Request.Timeframe) + } + if prevBucket != "" { + if b < prevBucket { + return fmt.Errorf("step %q: %s must be in ascending order", step.ID, fieldName) + } + if b == prevBucket { + return fmt.Errorf("step %q: %s contains %s %q", step.ID, fieldName, dupDesc, b) } } - prevDate = d + prevBucket = b tMs := t.UnixMilli() if tMs < step.Request.FromUnixMs || tMs > step.Request.ToUnixMs { - return fmt.Errorf("step %q: expected_yyyymmdd[%d] %q is out of request window [%d, %d]", step.ID, idx, d, step.Request.FromUnixMs, step.Request.ToUnixMs) + return fmt.Errorf("step %q: %s[%d] %q is out of request window [%d, %d]", step.ID, fieldName, idx, b, step.Request.FromUnixMs, step.Request.ToUnixMs) } } } diff --git a/apps/cli/internal/operator/scenario_test.go b/apps/cli/internal/operator/scenario_test.go index efabd4e..87e63db 100644 --- a/apps/cli/internal/operator/scenario_test.go +++ b/apps/cli/internal/operator/scenario_test.go @@ -1928,3 +1928,226 @@ matrix: } } } + +func TestValidateExpectedBuckets(t *testing.T) { + tests := []struct { + name string + yaml string + expectedErr string + }{ + { + name: "valid_monthly_buckets", + yaml: ` +name: valid_monthly +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 # 2025-01-01 00:00:00 UTC + to_unix_ms: 1740787200000 # 2025-03-01 00:00:00 UTC + expected_buckets: ["202501", "202502"] +`, + expectedErr: "", + }, + { + name: "invalid_monthly_bucket_format", + yaml: ` +name: invalid_monthly +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 + to_unix_ms: 1740787200000 + expected_buckets: ["2025-01"] +`, + expectedErr: "must be in 200601 format", + }, + { + name: "out_of_window_monthly_bucket", + yaml: ` +name: out_of_window_monthly +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 + to_unix_ms: 1740787200000 + expected_buckets: ["202504"] +`, + expectedErr: "is out of request window", + }, + { + name: "unordered_monthly_buckets", + yaml: ` +name: unordered_monthly +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 + to_unix_ms: 1740787200000 + expected_buckets: ["202502", "202501"] +`, + expectedErr: "must be in ascending order", + }, + { + name: "duplicate_monthly_buckets", + yaml: ` +name: duplicate_monthly +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 + to_unix_ms: 1740787200000 + expected_buckets: ["202501", "202501"] +`, + expectedErr: "contains duplicate bucket", + }, + { + name: "valid_minute_buckets", + yaml: ` +name: valid_minute +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: minute_1 + from_unix_ms: 1735689600000 # 2025-01-01 00:00:00 UTC + to_unix_ms: 1735693200000 # 2025-01-01 01:00:00 UTC + expected_buckets: ["202501010005", "202501010030"] +`, + expectedErr: "", + }, + { + name: "invalid_minute_bucket_format", + yaml: ` +name: invalid_minute +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: minute_5 + from_unix_ms: 1735689600000 + to_unix_ms: 1735693200000 + expected_buckets: ["20250101-00:05"] +`, + expectedErr: "must be in 200601021504 format", + }, + { + name: "expected_yyyymmdd_normalization_success", + yaml: ` +name: normalized_daily +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: daily + from_unix_ms: 1735689600000 # 2025-01-01 00:00:00 UTC + to_unix_ms: 1735862400000 # 2025-01-03 00:00:00 UTC + expected_yyyymmdd: ["20250101", "20250102"] +`, + expectedErr: "", + }, + { + name: "expected_yyyymmdd_non_daily_fail", + yaml: ` +name: normalized_daily_fail +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + symbols: ["005930"] +steps: + - id: step1 + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 + to_unix_ms: 1740787200000 + expected_yyyymmdd: ["20250101"] +`, + expectedErr: "expected_yyyymmdd is only supported for timeframe", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sc, err := ParseScenario([]byte(tt.yaml)) + if tt.expectedErr == "" { + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + // Verify normalization + if tt.name == "expected_yyyymmdd_normalization_success" { + if len(sc.Steps[0].Request.ExpectedBuckets) != 2 || sc.Steps[0].Request.ExpectedBuckets[0] != "20250101" { + t.Errorf("ExpectedBuckets not normalized: %v", sc.Steps[0].Request.ExpectedBuckets) + } + } + } else { + if err == nil { + t.Fatalf("expected error containing %q, got nil", tt.expectedErr) + } + if !strings.Contains(err.Error(), tt.expectedErr) { + t.Errorf("error = %v, want it to contain %q", err, tt.expectedErr) + } + } + }) + } +} + diff --git a/apps/cli/testdata/operator/expected/multi_timeframe_freshness_readiness.jsonl b/apps/cli/testdata/operator/expected/multi_timeframe_freshness_readiness.jsonl new file mode 100644 index 0000000..36fccca --- /dev/null +++ b/apps/cli/testdata/operator/expected/multi_timeframe_freshness_readiness.jsonl @@ -0,0 +1,5 @@ +{"action":"collection_freshness","duplicate_count":0,"freshness_status":"fresh","gap_buckets":["202502"],"gap_count":2,"gap_status":"gap","gap_yyyymmdd":["20250201"],"latest_bucket":"202503","latest_yyyymmdd":"20250315","missing_count":0,"provider_delay_days":0,"scenario":"multi_timeframe_freshness_readiness","status":"ok","step":"freshness_monthly","timeframe":"monthly","type":"step","universe":"kr-core"} +{"action":"collection_freshness","duplicate_count":0,"freshness_status":"fresh","gap_count":0,"gap_status":"clean","latest_bucket":"20250103","latest_yyyymmdd":"20250103","missing_count":0,"provider_delay_days":0,"scenario":"multi_timeframe_freshness_readiness","status":"ok","step":"freshness_daily","timeframe":"daily","type":"step","universe":"kr-core"} +{"action":"collection_freshness","duplicate_count":0,"freshness_status":"fresh","gap_count":0,"gap_status":"delayed","latest_bucket":"202501010901","latest_yyyymmdd":"20250101","missing_count":0,"provider_delay_days":1,"scenario":"multi_timeframe_freshness_readiness","status":"ok","step":"freshness_minute_1","timeframe":"minute_1","type":"step","universe":"kr-core"} +{"action":"collection_freshness","duplicate_buckets":["202501010905"],"duplicate_count":1,"duplicate_yyyymmdd":["20250101"],"freshness_status":"fresh","gap_count":0,"gap_status":"duplicate","latest_bucket":"202501010905","latest_yyyymmdd":"20250101","missing_count":0,"provider_delay_days":0,"scenario":"multi_timeframe_freshness_readiness","status":"ok","step":"freshness_minute_5","timeframe":"minute_5","type":"step","universe":"kr-core"} +{"exit_code":0,"passed":4,"scenario":"multi_timeframe_freshness_readiness","status":"ok","steps":4,"type":"summary"} diff --git a/apps/cli/testdata/operator/multi_timeframe_freshness_readiness.yaml b/apps/cli/testdata/operator/multi_timeframe_freshness_readiness.yaml new file mode 100644 index 0000000..9601c82 --- /dev/null +++ b/apps/cli/testdata/operator/multi_timeframe_freshness_readiness.yaml @@ -0,0 +1,81 @@ +name: multi_timeframe_freshness_readiness +timeout: 5s +universes: + - name: kr-core + provider: kis + selector_kind: watchlist + market: kr + venue: krx + symbols: ["005930", "000660"] +steps: + - id: freshness_monthly + action: collection_freshness + request: + universe: kr-core + timeframe: monthly + from_unix_ms: 1735689600000 # 2025-01-01 00:00:00 UTC + to_unix_ms: 1743465600000 # 2025-04-01 00:00:00 UTC + expected_buckets: ["202501", "202502", "202503"] + expect: + status: ok + freshness_status: fresh + missing_count: 0 + latest_bucket: "202503" + gap_status: gap + gap_count: 2 + duplicate_count: 0 + provider_delay_days: 0 + + - id: freshness_daily + action: collection_freshness + request: + universe: kr-core + timeframe: daily + from_unix_ms: 1735689600000 + to_unix_ms: 1735948800000 + expected_buckets: ["20250101", "20250102", "20250103"] + expect: + status: ok + freshness_status: fresh + missing_count: 0 + latest_bucket: "20250103" + gap_status: clean + gap_count: 0 + duplicate_count: 0 + provider_delay_days: 0 + + - id: freshness_minute_1 + action: collection_freshness + request: + universe: kr-core + timeframe: minute_1 + from_unix_ms: 1735722000000 # 2025-01-01 09:00:00 UTC + to_unix_ms: 1735722180000 # 2025-01-01 09:03:00 UTC + expected_buckets: ["202501010900", "202501010901", "202501010902"] + expect: + status: ok + freshness_status: fresh + missing_count: 0 + latest_bucket: "202501010901" + gap_status: delayed + gap_count: 0 + duplicate_count: 0 + provider_delay_days: 1 + + - id: freshness_minute_5 + action: collection_freshness + request: + universe: kr-core + timeframe: minute_5 + from_unix_ms: 1735722000000 # 2025-01-01 09:00:00 UTC + to_unix_ms: 1735722600000 # 2025-01-01 09:10:00 UTC + expected_buckets: ["202501010900", "202501010905"] + expect: + status: ok + freshness_status: fresh + missing_count: 0 + latest_bucket: "202501010905" + gap_status: duplicate + gap_count: 0 + duplicate_count: 1 + provider_delay_days: 0