Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(context): preserve Markdown publish errors

Saurabh Meena55f7eba389c2
diff --git a/semantica/context/context_graph.py b/semantica/context/context_graph.py
index 9d4e73cf..dea62d66 100644
--- a/semantica/context/context_graph.py
+++ b/semantica/context/context_graph.py
@@ -1214,10 +1214,30 @@ class ContextGraph:
try:
os.replace(staging_path, destination)
staging_path = None
- except BaseException:
+ except BaseException as publish_error:
if backup_path is not None and not destination.exists():
- os.replace(backup_path, destination)
- backup_path = None
+ try:
+ os.replace(backup_path, destination)
+ except BaseException as restore_error:
+ self.logger.error(
+ "Failed to restore previous ContextGraph Markdown "
+ "export from %s after publish failure; preserving "
+ "the original publish error",
+ backup_path,
+ exc_info=(
+ type(restore_error),
+ restore_error,
+ restore_error.__traceback__,
+ ),
+ )
+ add_note = getattr(publish_error, "add_note", None)
+ if add_note is not None:
+ add_note(
+ "Restoring the previous ContextGraph Markdown "
+ f"export also failed: {restore_error}"
+ )
+ else:
+ backup_path = None
raise
if backup_path is not None:
diff --git a/tests/context/test_context_graph_markdown.py b/tests/context/test_context_graph_markdown.py
index b2cd0c24..18e54e51 100644
--- a/tests/context/test_context_graph_markdown.py
+++ b/tests/context/test_context_graph_markdown.py
@@ -421,6 +421,41 @@ def test_markdown_export_restores_previous_directory_when_publish_fails(
assert not list(tmp_path.glob(".graph.backup-*"))
+def test_markdown_export_preserves_publish_error_when_restore_fails(
+ tmp_path, monkeypatch, caplog
+):
+ graph = ContextGraph(advanced_analytics=False)
+ graph.add_node("original", "Note", "Original")
+ destination = tmp_path / "graph"
+ graph.save_to_file(destination, format="markdown")
+ original_contents = _directory_contents(destination)
+ graph.add_node("new", "Note", "New")
+
+ real_replace = context_graph_module.os.replace
+
+ def fail_publish_and_restore(source, target):
+ source_path = Path(source)
+ if ".staging-" in source_path.name and Path(target) == destination:
+ raise OSError("simulated publish failure")
+ if ".backup-" in source_path.name and Path(target) == destination:
+ raise PermissionError("simulated restore failure")
+ return real_replace(source, target)
+
+ monkeypatch.setattr(context_graph_module.os, "replace", fail_publish_and_restore)
+ caplog.set_level("ERROR")
+
+ with pytest.raises(OSError, match="simulated publish failure"):
+ graph.save_to_file(destination, format="markdown")
+
+ assert "preserving the original publish error" in caplog.text
+ assert "simulated restore failure" in caplog.text
+ assert not destination.exists()
+ backup_paths = list(tmp_path.glob(".graph.backup-*"))
+ assert len(backup_paths) == 1
+ assert _directory_contents(backup_paths[0]) == original_contents
+ assert not list(tmp_path.glob(".graph.staging-*"))
+
+
def test_markdown_load_rebuilds_indexes_and_emits_one_reload_event(tmp_path):
source, _, _ = _sample_graph()
destination = tmp_path / "graph"