Write ``metadata.json`` to the session's output directory. Merges the current git commit and timestamp into any existing metadata so that stats from other workflows (e.g. CLI ``generate_docs``) are preserved.
(session: SessionState)
| 554 | |
| 555 | |
| 556 | def _write_generation_metadata(session: SessionState) -> None: |
| 557 | """Write ``metadata.json`` to the session's output directory. |
| 558 | |
| 559 | Merges the current git commit and timestamp into any existing metadata |
| 560 | so that stats from other workflows (e.g. CLI ``generate_docs``) are |
| 561 | preserved. |
| 562 | """ |
| 563 | try: |
| 564 | output_dir = Path(session.output_dir) |
| 565 | repo_path = Path(session.repo_path) |
| 566 | |
| 567 | # Baseline on the commit analyze_repo saw, NOT the current HEAD: |
| 568 | # commits made mid-session were never analyzed, and recording HEAD |
| 569 | # here would silently exclude them from the next incremental run. |
| 570 | commit_id: str | None = session.analyzed_commit |
| 571 | if not commit_id: |
| 572 | from codewiki.cli.utils.repo_validator import get_git_commit_hash |
| 573 | commit_id = get_git_commit_hash(repo_path) or None |
| 574 | |
| 575 | from datetime import datetime |
| 576 | |
| 577 | # Read existing metadata (if any) and merge |
| 578 | metadata_path = output_dir / "metadata.json" |
| 579 | existing: dict = {} |
| 580 | if metadata_path.exists(): |
| 581 | try: |
| 582 | existing = json.loads(metadata_path.read_text(encoding="utf-8")) |
| 583 | except (json.JSONDecodeError, OSError): |
| 584 | pass |
| 585 | |
| 586 | existing["generation_info"] = { |
| 587 | **existing.get("generation_info", {}), |
| 588 | "commit_id": commit_id, |
| 589 | "timestamp": datetime.now().isoformat(), |
| 590 | } |
| 591 | metadata_path.write_text( |
| 592 | json.dumps(existing, indent=2, ensure_ascii=False), |
| 593 | encoding="utf-8", |
| 594 | ) |
| 595 | except Exception as e: |
| 596 | logger.warning("Failed to write metadata.json: %s", e) |
| 597 | |
| 598 | |
| 599 | # =================================================================== |
no test coverage detected