- root SKILL.md is her upstream file (body byte-identical to the 5fb8379c mirror; description quoted so YAML parses and Mastra can load the skill) - chat binds the whole SKILL.md plus references/product_runtime_contract.md (safety-only) instead of a ~477-character excerpt - every turn carries her full strict-workflow-router (synced to upstream) and the domain's event-judgment file; product lists follow as an extra layer - import her 10 sub-skills, family router, interpretation status contract, technique ontology and the 54 files her method names - tests/test_upstream_skill_parity.py (in the quick gate) fails whenever her skill and ours drift - 18 frontend guards that locked the excerpt design updated with 原值/新值/原因 notes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
379 lines
17 KiB
Python
379 lines
17 KiB
Python
#!/usr/bin/env python3
|
|
"""Summarize the newest full-report authority without relying on stale outputs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import mmap
|
|
import re
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from typing import Any, Iterable
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_PACKET = ROOT / "tmp/Domi_PL9_AI_density_current.json"
|
|
DEFAULT_SCAN_DIRS = (ROOT / "output/user_reports", ROOT / "tmp/domi_2026_2028")
|
|
DEFAULT_PREFER_WINDOW = "2026-2028"
|
|
LARGE_JSON_SUMMARY_THRESHOLD_BYTES = 100 * 1024 * 1024
|
|
|
|
|
|
def _display_path(path: Path) -> str:
|
|
try:
|
|
return path.relative_to(ROOT).as_posix()
|
|
except ValueError:
|
|
# A path outside the repository still goes into the snapshot as a
|
|
# recorded value, so it is slash-separated too. str() here returned
|
|
# backslashes on Windows and every consumer comparing the tail of this
|
|
# string stopped matching.
|
|
return path.as_posix()
|
|
|
|
|
|
def _load_json(path: Path) -> dict[str, Any]:
|
|
if not path.exists():
|
|
return {}
|
|
if path.stat().st_size > LARGE_JSON_SUMMARY_THRESHOLD_BYTES:
|
|
return _load_large_packet_summary(path)
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
|
|
|
|
def _extract_top_level_json_value(path: Path, key: str, *, max_bytes: int = 16 * 1024 * 1024) -> Any:
|
|
key_pattern = re.compile(rb'"' + re.escape(key.encode()) + rb'"\s*:')
|
|
decoder = json.JSONDecoder()
|
|
with path.open("rb") as handle:
|
|
with mmap.mmap(handle.fileno(), 0, access=mmap.ACCESS_READ) as data:
|
|
match = key_pattern.search(data)
|
|
if not match:
|
|
return None
|
|
raw = data[match.end() : min(len(data), match.end() + max_bytes)].decode("utf-8", errors="ignore")
|
|
try:
|
|
value, _ = decoder.raw_decode(raw.lstrip())
|
|
except json.JSONDecodeError:
|
|
return None
|
|
return value
|
|
|
|
|
|
def _load_large_packet_summary(path: Path) -> dict[str, Any]:
|
|
worksheets = _as_dict(_extract_top_level_json_value(path, "worksheets"))
|
|
timing = _as_dict(_as_dict(worksheets.get("timing_and_predictive_systems")))
|
|
if not timing:
|
|
timing = {
|
|
"annual_tajika_series": _extract_top_level_json_value(path, "annual_tajika_series"),
|
|
"three_year_predictive_ephemeris_pack": _extract_top_level_json_value(
|
|
path,
|
|
"three_year_predictive_ephemeris_pack",
|
|
max_bytes=32 * 1024 * 1024,
|
|
),
|
|
}
|
|
worksheets = {"timing_and_predictive_systems": timing}
|
|
return {
|
|
"worksheets": worksheets,
|
|
"report_quality_gate": _extract_top_level_json_value(path, "report_quality_gate"),
|
|
"shared_full_report_authority": _extract_top_level_json_value(path, "shared_full_report_authority"),
|
|
"pl9_export_maturity_contract": _extract_top_level_json_value(path, "pl9_export_maturity_contract"),
|
|
"_summary_source": {
|
|
"mode": "large_json_top_level_extraction",
|
|
"path": _display_path(path),
|
|
"size_bytes": path.stat().st_size,
|
|
},
|
|
}
|
|
|
|
|
|
def _as_dict(value: Any) -> dict[str, Any]:
|
|
return value if isinstance(value, dict) else {}
|
|
|
|
|
|
def _scan_dirs(scan_dirs: Path | Iterable[Path]) -> list[Path]:
|
|
if isinstance(scan_dirs, Path):
|
|
return [scan_dirs]
|
|
return [Path(item) for item in scan_dirs]
|
|
|
|
|
|
def _window_years(prefer_window: str) -> list[str]:
|
|
match = re.fullmatch(r"(20\d{2})-(20\d{2})", prefer_window)
|
|
if not match:
|
|
return []
|
|
start, end = (int(item) for item in match.groups())
|
|
if end < start:
|
|
return []
|
|
return [str(year) for year in range(start, end + 1)]
|
|
|
|
|
|
def _single_report_year(display_name: str) -> str | None:
|
|
match = re.search(r"(20\d{2})年度", display_name)
|
|
if match:
|
|
return match.group(1)
|
|
return None
|
|
|
|
|
|
def _manifest_deliverable(item: dict[str, Any]) -> bool:
|
|
return bool(
|
|
item
|
|
and item.get("reader_hygiene_clean") is True
|
|
and item.get("page_count")
|
|
and item.get("annual_north_indian_chart_count")
|
|
and item.get("natal_north_indian_chart_count")
|
|
)
|
|
|
|
|
|
def _manifest_year_window(payload: dict[str, Any], display_name: str) -> list[str] | None:
|
|
window = _as_dict(payload.get('report_window'))
|
|
if window:
|
|
start, end = str(window.get('start_month') or ''), str(window.get('end_month') or '')
|
|
pattern = r'20\d{2}-(?:0[1-9]|1[0-2])'
|
|
if re.fullmatch(pattern, start) and re.fullmatch(pattern, end) and start <= end:
|
|
return [start[:4], end[:4]]
|
|
return None
|
|
match = re.search(r'(20\d{2})-(20\d{2})', display_name)
|
|
return list(match.groups()) if match else None
|
|
|
|
|
|
def _latest_manifests(scan_dirs: Path | Iterable[Path]) -> list[dict[str, Any]]:
|
|
manifests: list[dict[str, Any]] = []
|
|
for scan_dir in _scan_dirs(scan_dirs):
|
|
if not scan_dir.exists():
|
|
continue
|
|
candidates = [scan_dir] if scan_dir.is_file() else scan_dir.rglob('*.manifest.json')
|
|
for path in sorted(candidates, key=lambda item: item.stat().st_mtime, reverse=True):
|
|
payload = _load_json(path)
|
|
verification = _as_dict(payload.get("verification"))
|
|
maturity = _as_dict(verification.get("pl9_export_maturity_contract"))
|
|
display_name = str(payload.get("path") or path.name)
|
|
year_window = _manifest_year_window(payload, display_name)
|
|
single_year = (year_window[0] if year_window and year_window[0] == year_window[1] else None) if payload.get('report_window') else _single_report_year(display_name)
|
|
manifests.append(
|
|
{
|
|
"manifest_path": _display_path(path),
|
|
"report_path": payload.get("path"),
|
|
"edition": payload.get("edition"),
|
|
"year_window": year_window,
|
|
"single_report_year": single_year,
|
|
"mtime": path.stat().st_mtime,
|
|
"page_count": payload.get("page_count") or verification.get("page_count"),
|
|
"annual_north_indian_chart_count": verification.get("annual_north_indian_chart_count"),
|
|
"natal_north_indian_chart_count": verification.get("natal_north_indian_chart_count"),
|
|
"markdown_table_block_count": verification.get("markdown_table_block_count"),
|
|
"maturity_status": maturity.get("status"),
|
|
"reader_hygiene_clean": _as_dict(maturity.get("reader_hygiene")).get("clean"),
|
|
}
|
|
)
|
|
return manifests
|
|
|
|
|
|
def _annual_pdf_set(manifests: list[dict[str, Any]], prefer_window: str) -> dict[str, Any]:
|
|
expected_years = _window_years(prefer_window)
|
|
by_year: dict[str, dict[str, Any]] = {}
|
|
for item in manifests:
|
|
year = item.get("single_report_year")
|
|
if not year or year not in expected_years:
|
|
continue
|
|
if item.get("edition") != "pl9_ai_density":
|
|
continue
|
|
current = by_year.get(year)
|
|
if current is None or float(item.get("mtime") or 0) > float(current.get("mtime") or 0):
|
|
by_year[year] = item
|
|
|
|
selected = [by_year[year] for year in expected_years if year in by_year]
|
|
missing_years = [year for year in expected_years if year not in by_year]
|
|
maturity_counts: dict[str, int] = {}
|
|
for item in selected:
|
|
status = str(item.get("maturity_status") or "unknown")
|
|
maturity_counts[status] = maturity_counts.get(status, 0) + 1
|
|
|
|
return {
|
|
"expected_years": expected_years,
|
|
"years": [str(item.get("single_report_year")) for item in selected],
|
|
"missing_years": missing_years,
|
|
"complete": bool(expected_years) and not missing_years and all(_manifest_deliverable(item) for item in selected),
|
|
"deliverable_year_count": sum(1 for item in selected if _manifest_deliverable(item)),
|
|
"page_count_total": sum(int(item.get("page_count") or 0) for item in selected),
|
|
"maturity_status_counts": maturity_counts,
|
|
"reader_hygiene_clean_count": sum(1 for item in selected if item.get("reader_hygiene_clean") is True),
|
|
"manifests": selected,
|
|
}
|
|
|
|
|
|
def resolve_latest_ai_density_manifest(
|
|
scan_dir: Path | Iterable[Path] = DEFAULT_SCAN_DIRS,
|
|
*,
|
|
prefer_window: str = DEFAULT_PREFER_WINDOW,
|
|
) -> dict[str, Any] | None:
|
|
manifests = _latest_manifests(scan_dir)
|
|
return next(
|
|
(
|
|
item
|
|
for item in manifests
|
|
if item.get("edition") == "pl9_ai_density"
|
|
and item.get('year_window') == prefer_window.split('-')
|
|
),
|
|
None,
|
|
) or next((item for item in manifests if item.get("edition") == "pl9_ai_density"), None)
|
|
|
|
|
|
def build_snapshot(
|
|
packet_path: Path | None = DEFAULT_PACKET,
|
|
scan_dir: Path | Iterable[Path] = DEFAULT_SCAN_DIRS,
|
|
*,
|
|
prefer_window: str = DEFAULT_PREFER_WINDOW,
|
|
) -> dict[str, Any]:
|
|
packet = _load_json(packet_path) if packet_path is not None else {}
|
|
worksheets = _as_dict(packet.get("worksheets"))
|
|
timing = _as_dict(worksheets.get("timing_and_predictive_systems"))
|
|
annual_series = _as_dict(timing.get("annual_tajika_series"))
|
|
annual_years = sorted(str(year) for year in _as_dict(annual_series.get("years")))
|
|
three_year_pack = _as_dict(timing.get("three_year_predictive_ephemeris_pack"))
|
|
three_year_profile = _as_dict(three_year_pack.get("profile"))
|
|
quality_gate = _as_dict(packet.get("report_quality_gate"))
|
|
authority = _as_dict(packet.get("shared_full_report_authority"))
|
|
maturity = _as_dict(packet.get("pl9_export_maturity_contract"))
|
|
manifests = _latest_manifests(scan_dir)
|
|
annual_pdf_set = _annual_pdf_set(manifests, prefer_window)
|
|
latest_ai_density = resolve_latest_ai_density_manifest(scan_dir, prefer_window=prefer_window)
|
|
latest_window = latest_ai_density.get("year_window") if isinstance(latest_ai_density, dict) else None
|
|
packet_window = None
|
|
if three_year_profile.get("start_month") and three_year_profile.get("end_month"):
|
|
packet_window = [
|
|
str(three_year_profile["start_month"]).split("-", 1)[0],
|
|
str(three_year_profile["end_month"]).split("-", 1)[0],
|
|
]
|
|
packet_matches_latest_manifest = bool(packet_window and latest_window and packet_window == latest_window)
|
|
packet_scope = (
|
|
"current_window"
|
|
if packet_matches_latest_manifest
|
|
else "stale_window"
|
|
if packet_window and latest_window
|
|
else "unknown_window"
|
|
)
|
|
|
|
blocking_reasons = list(quality_gate.get("blocking_reasons") or [])
|
|
review_reasons = list(quality_gate.get("review_reasons") or [])
|
|
latest_manifest_deliverable = _manifest_deliverable(latest_ai_density or {})
|
|
latest_report_readiness = (
|
|
"deliverable_from_latest_manifest"
|
|
if latest_manifest_deliverable and not packet_matches_latest_manifest
|
|
else "deliverable_from_packet_and_manifest"
|
|
if latest_manifest_deliverable and packet_matches_latest_manifest and not blocking_reasons
|
|
else "blocked_or_missing_latest_manifest"
|
|
)
|
|
active_authority_source = (
|
|
"latest_manifest_and_annual_pdf_set"
|
|
if latest_manifest_deliverable and annual_pdf_set.get("complete")
|
|
else "latest_manifest_only"
|
|
if latest_manifest_deliverable
|
|
else "packet_only_or_missing_manifest"
|
|
)
|
|
return {
|
|
"schema": "jyotish.current_report_authority_snapshot.v1",
|
|
"generated_at": datetime.now(timezone.utc).isoformat(),
|
|
"packet_path": _display_path(packet_path) if packet_path is not None else None,
|
|
"status": (
|
|
"current_authority_available"
|
|
if latest_report_readiness in {"deliverable_from_latest_manifest", "deliverable_from_packet_and_manifest"}
|
|
else "blocked"
|
|
if blocking_reasons
|
|
else "review_required"
|
|
),
|
|
"why_prior_window_was_wrong": [
|
|
"PL9 final diff matrix measures source-page parity, not the newest complete user-report export.",
|
|
"Historical output/user_reports markdown files can be stale; current authority must come from packet quality gate plus fresh maturity manifests.",
|
|
"The three-year layer lives in annual_tajika_series and three_year_predictive_ephemeris_pack, so a PL9 page-only matrix will undercount it.",
|
|
],
|
|
"current_authority": {
|
|
"latest_report_readiness": latest_report_readiness,
|
|
"packet_quality_gate_status": quality_gate.get("status"),
|
|
"packet_blocking_reasons": blocking_reasons,
|
|
"packet_review_reasons": review_reasons,
|
|
"packet_scope": packet_scope,
|
|
"packet_review_reasons_apply_to_current_window": packet_scope == "current_window",
|
|
"active_authority_source": active_authority_source,
|
|
"shared_authority_status": authority.get("status"),
|
|
"publication_status": authority.get("publication_status"),
|
|
"maturity_status": maturity.get("status"),
|
|
"reader_hygiene_clean": _as_dict(maturity.get("reader_hygiene")).get("clean"),
|
|
"annual_series_years": annual_years,
|
|
"annual_series_year_count": len(annual_years),
|
|
"three_year_schema": three_year_pack.get("schema"),
|
|
"three_year_status": three_year_pack.get("status"),
|
|
"three_year_month_count": three_year_profile.get("month_count"),
|
|
"three_year_start_month": three_year_profile.get("start_month"),
|
|
"three_year_end_month": three_year_profile.get("end_month"),
|
|
"latest_ai_density_manifest": latest_ai_density,
|
|
"latest_ai_density_year_window": latest_window,
|
|
"packet_year_window": packet_window,
|
|
"packet_matches_latest_manifest": packet_matches_latest_manifest,
|
|
"packet_manifest_alignment": (
|
|
"same_three_year_window"
|
|
if packet_matches_latest_manifest
|
|
else "packet_json_not_same_window_as_latest_pdf_manifest"
|
|
if packet_window and latest_window
|
|
else "insufficient_window_metadata"
|
|
),
|
|
"latest_manifest_deliverable": latest_manifest_deliverable,
|
|
"annual_pdf_set": annual_pdf_set,
|
|
},
|
|
"latest_manifests": manifests[:8],
|
|
"decision_rule": (
|
|
"Use current_authority for product completion and user-report readiness. "
|
|
"Use pl9_final_diff_matrix only for PL9/JH8 page-family parity gaps."
|
|
),
|
|
}
|
|
|
|
|
|
def render_markdown(snapshot: dict[str, Any]) -> str:
|
|
authority = _as_dict(snapshot.get("current_authority"))
|
|
latest = _as_dict(authority.get("latest_ai_density_manifest"))
|
|
lines = [
|
|
"# Current Report Authority Snapshot",
|
|
"",
|
|
f"- Status: `{snapshot['status']}`",
|
|
f"- Latest report readiness: `{authority.get('latest_report_readiness')}`",
|
|
f"- Packet quality gate: `{authority.get('packet_quality_gate_status')}`",
|
|
f"- Packet blocking reasons: `{len(authority.get('packet_blocking_reasons') or [])}`",
|
|
f"- Packet review reasons: `{authority.get('packet_review_reasons')}`",
|
|
f"- Packet scope: `{authority.get('packet_scope')}`",
|
|
f"- Active authority source: `{authority.get('active_authority_source')}`",
|
|
f"- Annual years: `{', '.join(authority.get('annual_series_years') or [])}`",
|
|
f"- Three-year months: `{authority.get('three_year_month_count')}` (`{authority.get('three_year_start_month')}` to `{authority.get('three_year_end_month')}`)",
|
|
f"- Packet/latest PDF alignment: `{authority.get('packet_manifest_alignment')}`",
|
|
f"- Latest AI-density PDF pages: `{latest.get('page_count')}`",
|
|
f"- Latest AI-density report: `{latest.get('report_path')}`",
|
|
f"- Annual PDF set complete: `{_as_dict(authority.get('annual_pdf_set')).get('complete')}`",
|
|
f"- Annual PDF set pages: `{_as_dict(authority.get('annual_pdf_set')).get('page_count_total')}`",
|
|
"",
|
|
"## Why The Previous Window Drifted",
|
|
"",
|
|
]
|
|
lines.extend(f"- {item}" for item in snapshot["why_prior_window_was_wrong"])
|
|
lines.extend(["", "## Decision Rule", "", snapshot["decision_rule"], ""])
|
|
return "\n".join(lines)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--packet", default=str(DEFAULT_PACKET))
|
|
parser.add_argument(
|
|
"--scan-dir",
|
|
action="append",
|
|
dest="scan_dirs",
|
|
help="Manifest directory to scan. May be supplied more than once. Defaults to current report output plus tmp annual reports.",
|
|
)
|
|
parser.add_argument("--prefer-window", default=DEFAULT_PREFER_WINDOW)
|
|
parser.add_argument("--format", choices=("json", "markdown"), default="json")
|
|
parser.add_argument("--output")
|
|
args = parser.parse_args()
|
|
|
|
scan_dirs = [Path(item) for item in args.scan_dirs] if args.scan_dirs else list(DEFAULT_SCAN_DIRS)
|
|
snapshot = build_snapshot(Path(args.packet), scan_dirs, prefer_window=args.prefer_window)
|
|
content = json.dumps(snapshot, ensure_ascii=False, indent=2, sort_keys=True) if args.format == "json" else render_markdown(snapshot)
|
|
if args.output:
|
|
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
|
Path(args.output).write_text(content + ("\n" if not content.endswith("\n") else ""), encoding="utf-8", newline="\n")
|
|
else:
|
|
print(content)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|