refactor: split precheck state snapshot and analysis
This commit is contained in:
@@ -904,23 +904,15 @@ def update_state_after_observation(state: dict, snapshot: dict, analysis: dict):
|
||||
|
||||
|
||||
def mark_run_requested(note: str = ""):
|
||||
state = load_state()
|
||||
state["run_requested_at"] = utc_iso()
|
||||
state["run_request_note"] = note
|
||||
save_state(state)
|
||||
print(json.dumps({"ok": True, "run_requested_at": state["run_requested_at"], "note": note}, ensure_ascii=False))
|
||||
from .services.precheck_state import mark_run_requested as service_mark_run_requested
|
||||
|
||||
return service_mark_run_requested(note)
|
||||
|
||||
|
||||
def ack_analysis(note: str = ""):
|
||||
state = load_state()
|
||||
state["last_deep_analysis_at"] = utc_iso()
|
||||
state["pending_trigger"] = False
|
||||
state["pending_reasons"] = []
|
||||
state["last_ack_note"] = note
|
||||
state.pop("run_requested_at", None)
|
||||
state.pop("run_request_note", None)
|
||||
save_state(state)
|
||||
print(json.dumps({"ok": True, "acked_at": state["last_deep_analysis_at"], "note": note}, ensure_ascii=False))
|
||||
from .services.precheck_state import ack_analysis as service_ack_analysis
|
||||
|
||||
return service_ack_analysis(note)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
25
src/coinhunter/services/precheck_analysis.py
Normal file
25
src/coinhunter/services/precheck_analysis.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""Analysis helpers for precheck."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .. import precheck as precheck_module
|
||||
|
||||
|
||||
def analyze_trigger(snapshot: dict, state: dict) -> dict:
|
||||
return precheck_module.analyze_trigger(snapshot, state)
|
||||
|
||||
|
||||
def build_failure_payload(exc: Exception) -> dict:
|
||||
return {
|
||||
"generated_at": precheck_module.utc_iso(),
|
||||
"status": "deep_analysis_required",
|
||||
"should_analyze": True,
|
||||
"pending_trigger": True,
|
||||
"cooldown_active": False,
|
||||
"reasons": ["precheck-error"],
|
||||
"hard_reasons": ["precheck-error"],
|
||||
"soft_reasons": [],
|
||||
"soft_score": 0,
|
||||
"details": [str(exc)],
|
||||
"compact_summary": f"预检查失败,转入深度分析兜底: {exc}",
|
||||
}
|
||||
@@ -5,39 +5,26 @@ from __future__ import annotations
|
||||
import json
|
||||
import sys
|
||||
|
||||
from .. import precheck as precheck_module
|
||||
from . import precheck_analysis, precheck_snapshot, precheck_state
|
||||
|
||||
|
||||
def run(argv: list[str] | None = None) -> int:
|
||||
argv = list(sys.argv[1:] if argv is None else argv)
|
||||
|
||||
if argv and argv[0] == "--ack":
|
||||
precheck_module.ack_analysis(" ".join(argv[1:]).strip())
|
||||
precheck_state.ack_analysis(" ".join(argv[1:]).strip())
|
||||
return 0
|
||||
if argv and argv[0] == "--mark-run-requested":
|
||||
precheck_module.mark_run_requested(" ".join(argv[1:]).strip())
|
||||
precheck_state.mark_run_requested(" ".join(argv[1:]).strip())
|
||||
return 0
|
||||
|
||||
try:
|
||||
state = precheck_module.sanitize_state_for_stale_triggers(precheck_module.load_state())
|
||||
snapshot = precheck_module.build_snapshot()
|
||||
analysis = precheck_module.analyze_trigger(snapshot, state)
|
||||
precheck_module.save_state(precheck_module.update_state_after_observation(state, snapshot, analysis))
|
||||
state = precheck_state.sanitize_state_for_stale_triggers(precheck_state.load_state())
|
||||
snapshot = precheck_snapshot.build_snapshot()
|
||||
analysis = precheck_analysis.analyze_trigger(snapshot, state)
|
||||
precheck_state.save_state(precheck_state.update_state_after_observation(state, snapshot, analysis))
|
||||
print(json.dumps(analysis, ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
except Exception as exc:
|
||||
failure = {
|
||||
"generated_at": precheck_module.utc_iso(),
|
||||
"status": "deep_analysis_required",
|
||||
"should_analyze": True,
|
||||
"pending_trigger": True,
|
||||
"cooldown_active": False,
|
||||
"reasons": ["precheck-error"],
|
||||
"hard_reasons": ["precheck-error"],
|
||||
"soft_reasons": [],
|
||||
"soft_score": 0,
|
||||
"details": [str(exc)],
|
||||
"compact_summary": f"预检查失败,转入深度分析兜底: {exc}",
|
||||
}
|
||||
print(json.dumps(failure, ensure_ascii=False, indent=2))
|
||||
print(json.dumps(precheck_analysis.build_failure_payload(exc), ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
9
src/coinhunter/services/precheck_snapshot.py
Normal file
9
src/coinhunter/services/precheck_snapshot.py
Normal file
@@ -0,0 +1,9 @@
|
||||
"""Snapshot construction helpers for precheck."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .. import precheck as precheck_module
|
||||
|
||||
|
||||
def build_snapshot() -> dict:
|
||||
return precheck_module.build_snapshot()
|
||||
47
src/coinhunter/services/precheck_state.py
Normal file
47
src/coinhunter/services/precheck_state.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""State helpers for precheck orchestration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from .. import precheck as precheck_module
|
||||
|
||||
|
||||
def load_state() -> dict:
|
||||
return precheck_module.load_state()
|
||||
|
||||
|
||||
def save_state(state: dict) -> None:
|
||||
precheck_module.save_state(state)
|
||||
|
||||
|
||||
def sanitize_state_for_stale_triggers(state: dict) -> dict:
|
||||
return precheck_module.sanitize_state_for_stale_triggers(state)
|
||||
|
||||
|
||||
def update_state_after_observation(state: dict, snapshot: dict, analysis: dict) -> dict:
|
||||
return precheck_module.update_state_after_observation(state, snapshot, analysis)
|
||||
|
||||
|
||||
def mark_run_requested(note: str = "") -> dict:
|
||||
state = load_state()
|
||||
state["run_requested_at"] = precheck_module.utc_iso()
|
||||
state["run_request_note"] = note
|
||||
save_state(state)
|
||||
payload = {"ok": True, "run_requested_at": state["run_requested_at"], "note": note}
|
||||
print(json.dumps(payload, ensure_ascii=False))
|
||||
return payload
|
||||
|
||||
|
||||
def ack_analysis(note: str = "") -> dict:
|
||||
state = load_state()
|
||||
state["last_deep_analysis_at"] = precheck_module.utc_iso()
|
||||
state["pending_trigger"] = False
|
||||
state["pending_reasons"] = []
|
||||
state["last_ack_note"] = note
|
||||
state.pop("run_requested_at", None)
|
||||
state.pop("run_request_note", None)
|
||||
save_state(state)
|
||||
payload = {"ok": True, "acked_at": state["last_deep_analysis_at"], "note": note}
|
||||
print(json.dumps(payload, ensure_ascii=False))
|
||||
return payload
|
||||
Reference in New Issue
Block a user