ReMe/reme/steps/proactive/data_connectors/_finance.py
jinliyl ae55e2e570
Some checks are pending
CI / Python packages / Build and verify distributions (push) Waiting to run
CI / Python quality / GitHub Actions (push) Waiting to run
CI / Python quality / Pre-commit (push) Waiting to run
CI / Python tests / Hermes embedded - ubuntu-latest (push) Waiting to run
CI / Python tests / Hermes embedded - windows-latest (push) Waiting to run
CI / Python tests / Unit Tests - py3.11 (push) Waiting to run
CI / Python tests / Unit Tests - py3.12 (push) Waiting to run
CI / Python tests / Unit Tests - py3.13 (push) Waiting to run
CI / Python tests / Unit Tests - py3.14 (push) Waiting to run
CI / ReMe Studio / Studio checks (push) Waiting to run
CI / Windows / CLI smoke - py3.11 (push) Waiting to run
Deploy / Documentation / Build documentation (push) Waiting to run
Deploy / Documentation / deploy (push) Blocked by required conditions
Security / CodeQL / Analyze javascript-typescript (push) Waiting to run
Security / CodeQL / Analyze python (push) Waiting to run
feat!: split proactive runtimes and add Studio backend management (#776)
* refactor(proactive)!: separate runtime ownership and unify delivery

* refactor(proactive): finish runtime and Studio migration

* test(proactive): make scheduled skip coverage deterministic

* refactor(studio): unify navigation and notebook resource loading

* feat(studio): edit workspace documents and update proactive task names

* refactor(studio): simplify task settings and navigation

* feat(studio): secure and supervise backend settings

* feat(studio): unify workbench timelines and refine desktop updates

Use shared timeline panes for notebook, memory, and resources. Add Todo creation ordering, persist companion visibility, and align desktop update handling and release artifacts.

Recheck editor file versions at read and publication boundaries. Retry reminder detail requests from the resource view.

* fix(studio): support unsigned GitHub preview releases

* fix: verify container state and isolate proactive view errors

Mount private supervisor state in Docker smoke tests and verify token and settings persistence. Exercise Studio startup with a loopback-only model fixture, and exclude packaged desktop output from Docker contexts.

Keep Remy subscriptions and healthy tasks available when an individual task has an invalid workspace path or session history.

* fix(home): align candidate Docker context exclusions

* chore(deps): update openai-codex dependency to v0.172.0

- Bump openai-codex from version 0.169.0 to 0.172.0 in pyproject.toml
- Keep other dependencies unchanged
- Ensure compatibility with latest openai-codex features and fixes

* fix(daemon): discover parent dotenv and isolate listeners

Restore parent-directory environment discovery in workers while retaining system precedence. Partition the default supervisor profile and lock by the configured bind address and port.
2026-11-25 21:33:16 +08:00

142 lines
5.9 KiB
Python

"""Shared HTTP collection for financial news and quotes."""
from abc import abstractmethod
from datetime import datetime
import math
from pathlib import Path
import httpx
from .base_connector import BaseConnectorStep
from ....utils.files.workspace_files import WorkspaceFiles
from ....schema import ResourceMetadata
from ....schema.resource_metadata import RESOURCE_METADATA_FILENAME
class FinanceCollectorStep(BaseConnectorStep):
"""Read and publish financial source records within the resource package lifecycle."""
collected_at: datetime
files: WorkspaceFiles
resource_root: Path
def __init__(self, task_names=None, max_retries=3, request_timeout=20, **kwargs):
super().__init__(task_names=["proactive_fin"] if task_names is None else task_names, **kwargs)
if type(max_retries) is not int or max_retries < 1:
raise ValueError("max_retries must be a positive integer")
if type(request_timeout) not in (int, float) or not math.isfinite(request_timeout) or request_timeout <= 0:
raise ValueError("request_timeout must be finite and positive")
self.max_retries = max_retries
self.request_timeout = request_timeout
def resource_target(self):
target, self.collected_at = super().resource_target()
self.files = WorkspaceFiles(self.workspace_path)
self.resource_root = self.files.checked(target.parent.parent)
return target, self.collected_at
def manifests(self):
"""Find published source packages, including pending deliveries."""
for path in self.resource_root.glob(f"*/*/{RESOURCE_METADATA_FILENAME}"):
self.require_path(path)
yield path
def client(self, **kwargs):
"""Use the same timeouts across finance feeds."""
return httpx.Client(timeout=httpx.Timeout(self.request_timeout, connect=5), **kwargs)
def write_records(self, directory, filename, records):
"""Write a bounded, nonempty JSONL payload."""
if not records:
return []
content = "".join(record.model_dump_json() + "\n" for record in records)
if len(content.encode("utf-8")) > self.max_file_bytes():
raise ValueError("Finance data exceeds max_file_bytes")
self.check_stopped()
(directory / filename).write_text(content, encoding="utf-8")
return [{"path": filename}]
def check_stopped(self):
"""Interrupt HTTP work and retry delays when the owning Job is stopped."""
if self._cancelled.is_set() or (self.context.stop_event is not None and self.context.stop_event.is_set()):
raise RuntimeError("Finance collection stopped")
def wait(self, seconds):
"""Wait off-loop in short, cancellable intervals."""
remaining = seconds
while remaining > 0:
self.check_stopped()
delay = min(remaining, 0.1)
self._cancelled.wait(delay)
remaining -= delay
self.check_stopped()
def request(self, client: httpx.Client, url: str, *, params=None) -> bytes:
"""Bound each HTTP response and retry transient transport or HTTP failures."""
for attempt in range(self.max_retries):
self.check_stopped()
try:
with client.stream("GET", url, params=params) as response:
response.raise_for_status()
data = bytearray()
for chunk in response.iter_bytes():
self.check_stopped()
data.extend(chunk)
if len(data) > self.max_file_bytes():
raise ValueError("Finance response exceeds max_file_bytes")
return bytes(data)
except httpx.HTTPError:
if attempt + 1 == self.max_retries:
raise
self.wait(2**attempt)
raise RuntimeError("Finance request exhausted retries")
class QuoteCollectorStep(FinanceCollectorStep):
"""Share polling and deduplication across independent quote feeds."""
filename: str
quote_model: type
def _already_collected(self):
"""Use published packages to avoid duplicate quote snapshots in the same minute."""
minute = self.collected_at.replace(second=0, microsecond=0)
pattern = f"{minute:%Y-%m-%d}/{minute:%H%M}??_*/{RESOURCE_METADATA_FILENAME}"
for manifest in self.resource_root.glob(pattern):
self.require_path(manifest)
metadata = ResourceMetadata.model_validate_json(self.files.read(manifest))
if metadata.created_at.replace(second=0, microsecond=0) == minute:
return True
return False
def _saved_quotes(self):
"""Rebuild quote state from published resources."""
for manifest in self.manifests():
path = manifest.parent / self.filename
self.require_path(path)
for value in self.files.iter_jsonl(path):
self.check_stopped()
yield self.quote_model.model_validate(value)
@abstractmethod
def fetch_quote(self, client: httpx.Client):
"""Read and validate the provider's quote."""
@staticmethod
@abstractmethod
def changed(quote, previous):
"""Check whether the quote is new and materially different."""
def received_at(self):
"""Use the collection timezone for the time the response was received."""
return datetime.now(self.collected_at.tzinfo)
def collect(self, directory: Path) -> list[dict]:
if self._already_collected():
return []
with self.client() as client:
quote = self.fetch_quote(client)
previous = max(self._saved_quotes(), key=lambda item: item.collected_at, default=None)
if previous is not None and not self.changed(quote, previous):
return []
return self.write_records(directory, self.filename, [quote])