Skip to content

Commit ee9a0c7

Browse files
committed
updater: keep update_history across the metadata rewrite
The update path calls create_documentation_metadata, which writes metadata.json from scratch, and only then appends the step summary. So update_history never held more than the last step and a chain of updates lost its history. Read the history before the rewrite and restore it in merge_into_metadata.
1 parent 878bf38 commit ee9a0c7

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎codewiki/cli/adapters/doc_generator.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,11 @@ async def _run_incremental_update(
442442
)
443443
working_dir = str(self.output_dir.absolute())
444444
if record.outcome in ("incremental", "no_change"):
445+
# create_documentation_metadata rewrites metadata.json from scratch;
446+
# keep the history of earlier updates so a chain of updates accumulates.
447+
prior_history = self._read_update_history(working_dir)
445448
doc_generator.create_documentation_metadata(working_dir, components, len(leaf_nodes))
446-
self._merge_update_summary(working_dir)
449+
self._merge_update_summary(working_dir, prior_history)
447450
for file_path in os.listdir(working_dir):
448451
if file_path.endswith((".md", ".json")):
449452
self.job.files_generated.append(file_path)
@@ -480,7 +483,17 @@ def _move_docs_aside(self) -> None:
480483
if self.verbose:
481484
self.progress_tracker.update_stage(0.1, f"Previous docs preserved at {target}")
482485

483-
def _merge_update_summary(self, working_dir: str) -> None:
486+
@staticmethod
487+
def _read_update_history(working_dir: str) -> list:
488+
path = os.path.join(working_dir, "metadata.json")
489+
try:
490+
with open(path, encoding="utf-8") as f:
491+
history = (json.load(f) or {}).get("update_history")
492+
except (OSError, json.JSONDecodeError):
493+
return []
494+
return history if isinstance(history, list) else []
495+
496+
def _merge_update_summary(self, working_dir: str, prior_history: list | None = None) -> None:
484497
record = getattr(self, "_last_update_record", None)
485498
if record is None:
486499
return
@@ -490,7 +503,7 @@ def _merge_update_summary(self, working_dir: str) -> None:
490503
preserved = getattr(self, "_preserved_docs_dir", None)
491504
if preserved:
492505
summary["previous_docs"] = preserved
493-
merge_into_metadata(working_dir, summary)
506+
merge_into_metadata(working_dir, summary, prior_history=prior_history)
494507
try:
495508
record.save(working_dir)
496509
except OSError:

‎codewiki/src/be/updater/record.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,14 @@ def save(self, docs_dir: str) -> str:
9696
return path
9797

9898

99-
def merge_into_metadata(docs_dir: str, summary: dict[str, Any]) -> None:
100-
"""Append ``summary`` under ``last_update`` (and an ``update_history`` list)."""
99+
def merge_into_metadata(
100+
docs_dir: str, summary: dict[str, Any], prior_history: list[dict[str, Any]] | None = None
101+
) -> None:
102+
"""Append ``summary`` under ``last_update`` (and an ``update_history`` list).
103+
104+
``prior_history`` restores the history when the caller rewrote metadata.json
105+
in between (the update path regenerates it before merging).
106+
"""
101107
path = os.path.join(docs_dir, "metadata.json")
102108
meta: dict[str, Any] = {}
103109
if os.path.exists(path):
@@ -110,6 +116,8 @@ def merge_into_metadata(docs_dir: str, summary: dict[str, Any]) -> None:
110116
history = meta.get("update_history")
111117
if not isinstance(history, list):
112118
history = []
119+
if not history and prior_history:
120+
history = list(prior_history)
113121
history.append(summary)
114122
meta["update_history"] = history
115123
with open(path, "w", encoding="utf-8") as f:

0 commit comments

Comments
 (0)