ReMe/reme/schema/auto_fin.py
jinliyl d5e0d2837b
Some checks failed
Tests ReMe / Unit Tests - py3.12 (push) Has been cancelled
Tests ReMe / Unit Tests - py3.13 (push) Has been cancelled
Windows Smoke / CLI smoke - py3.11 (push) Has been cancelled
Pre-commit / run (ubuntu-latest) (push) Has been cancelled
Tests ReMe / Unit Tests - py3.11 (push) Has been cancelled
refactor: rebuild auto-fin and daily-paper cookbooks on structured-output agents (#432)
* refactor: rebuild auto-fin and daily-paper cookbooks on structured-output agents

Rework the auto-fin and daily-paper cookbooks to run on structured-output
LLM agents instead of Claude Code agent wrappers, replace the SSH proxy with
data-source mirrors, and rewrite the affected unit tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor(auto_fin): unify JSON output serialization and writing

- Extracted _write_output static method to serialize and write Pydantic models as compact JSON
- Replaced inline JSON dump and write calls with _write_output usage across auto_fin steps
- Added _report_path and _current_report for managing intra-day reports in AutoFinMergeStep
- Updated auto_fin merge step to write output via new _write_output method
- Enhanced news reading with caching in AutoFinHistoryStep
- Refined returns calculation to handle events before close on non-trading days correctly

feat(daily_paper): improve note path resolution and metadata handling

- Introduced iter_note_metadata generator for safe Markdown frontmatter iteration
- Added resolve_unique_note_path to avoid note filename conflicts on disk and in used titles
- Updated analyze, collect, digest, and select steps to use centralized constants and helpers
- Used utc_now_iso for consistent timestamping in metadata
- Replaced direct frontmatter loads with iter_note_metadata in collect and analyze steps
- Replaced hardcoded paper selection count with PAPER_COUNT constant in all relevant places
- Added _MAX_SELECT_ATTEMPTS constant in select step for attempt management
- Improved error messages for filename validation in daily paper title normalization

feat(auto_fin): add multi-run cron schedules for intraday refinement

- Defined three auto_fin cron jobs at 09:30, 11:30, and 18:00 Shanghai time for gradual report updates
- Each intraday run adds evidence cumulatively instead of replacing prior output wholly
- Updated daily_cookbook.yaml to register new cron schedules and remove legacy 12:00 cron

refactor(auto_fin_data): clean ETF code handling and page limits

- Replaced hardcoded DEFAULT_ETF_CODES with required non-empty config value "etf_codes"
- Added constants for major news and fund page limits to control pagination
- Improved ETF name extraction logic to handle missing fields consistently

fix(auto_fin_merge): fix report retrieval and merging logic

- Added support for getting current intra-day report in addition to previous day's report
- Modified merge template to include prior and current report sections for better context
- Adjusted report path handling to consistently use Path objects

test(auto_fin): add coverage for returns calculation and report retrieval

- Added test for returns when event occurs before close on non-trading day, checking next session entry
- Added test for previous and current report retrieval feeding merge context with disk files
- Extended test asserts for auto_fin cron schedule changes in config

style(daily_paper): reorder and cleanup imports

- Reorganized imports in _common.py for clarity and added missing collections.abc.Iterator import
- Cleaned up commented and unused imports across daily_paper steps

* feat: add configurable upstream mirror proxy

* style: format auto-fin data step

* fix: align cookbook mirrors and contracts

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-07 23:53:14 +08:00

103 lines
2.5 KiB
Python

"""Public contracts for the Auto Fin workflow."""
from __future__ import annotations
from datetime import datetime
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
class AutoFinModel(BaseModel):
"""Strict program-owned Auto Fin data."""
model_config = ConfigDict(extra="forbid")
class AutoFinAgentModel(AutoFinModel):
"""Agent output tolerant of harmless extra fields."""
model_config = ConfigDict(extra="ignore")
class AutoFinEventReference(AutoFinAgentModel):
"""One current news item related to an ETF."""
news_id: str
reason: str
class AutoFinEtfSelection(AutoFinAgentModel):
"""One ETF selected from the configured codes."""
etf_code: str
etf_name: str = ""
events: list[AutoFinEventReference] = Field(default_factory=list)
class AutoFinEtfsOutput(AutoFinAgentModel):
"""Selections returned by the first Agent."""
etfs: list[AutoFinEtfSelection] = Field(default_factory=list)
class AutoFinHistoricalReference(AutoFinAgentModel):
"""One historical event selected by the second Agent."""
news_id: str
reason: str
direction: Literal["same", "opposite"]
class AutoFinHistoricalOutput(AutoFinAgentModel):
"""Historical matches for one current news item."""
historical_events: list[AutoFinHistoricalReference] = Field(default_factory=list)
class AutoFinReturns(AutoFinModel):
"""Adjusted cumulative ETF returns after one historical event."""
d1: float | None = None
d2: float | None = None
d3: float | None = None
d5: float | None = None
class AutoFinHistoricalEvent(AutoFinModel):
"""A resolved historical event and its observed ETF performance."""
news_id: str
event_time: datetime
title: str
content: str
reason: str
direction: Literal["same", "opposite"]
returns: AutoFinReturns
class AutoFinCurrentEvent(AutoFinModel):
"""One current event with comparable historical evidence."""
news_id: str
event_time: datetime
title: str
content: str
reason: str
historical_events: list[AutoFinHistoricalEvent] = Field(default_factory=list)
class AutoFinEtfAnalysis(AutoFinModel):
"""All evidence prepared for the final Agent for one ETF."""
etf_code: str
etf_name: str
events: list[AutoFinCurrentEvent] = Field(default_factory=list)
class AutoFinReportOutput(AutoFinAgentModel):
"""Final Markdown returned by the third Agent."""
title: str = ""
description: str = ""
body: str = ""