polydyno 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
autopsy.py ADDED
@@ -0,0 +1,129 @@
1
+ """Black Box Autopsy — SQL diff diagnostics on invariant drift."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import difflib
6
+ import html
7
+ import re
8
+
9
+ from pydantic import BaseModel, Field
10
+ from rich.console import Console, RenderableType
11
+ from rich.panel import Panel
12
+ from rich.syntax import Syntax
13
+ from rich.text import Text
14
+
15
+
16
+ class BlackBoxAutopsy(BaseModel):
17
+ drift_turn: int
18
+ previous_turn: int
19
+ adversarial_context_noise: str = ""
20
+ unified_diff: str = ""
21
+ schema_before: str = ""
22
+ schema_after: str = ""
23
+
24
+
25
+ def compute_unified_sql_diff(
26
+ schema_before: str,
27
+ schema_after: str,
28
+ *,
29
+ previous_turn: int,
30
+ drift_turn: int,
31
+ ) -> str:
32
+ before_lines = schema_before.splitlines(keepends=True)
33
+ after_lines = schema_after.splitlines(keepends=True)
34
+ if not before_lines and schema_before:
35
+ before_lines = [schema_before if schema_before.endswith("\n") else schema_before + "\n"]
36
+ if not after_lines and schema_after:
37
+ after_lines = [schema_after if schema_after.endswith("\n") else schema_after + "\n"]
38
+ diff_iter = difflib.unified_diff(
39
+ before_lines,
40
+ after_lines,
41
+ fromfile=f"turn_{previous_turn:02d}_valid.sql",
42
+ tofile=f"turn_{drift_turn:02d}_drift.sql",
43
+ lineterm="",
44
+ )
45
+ return "\n".join(diff_iter)
46
+
47
+
48
+ def build_black_box_autopsy(
49
+ *,
50
+ drift_turn: int,
51
+ previous_turn: int,
52
+ schema_before: str,
53
+ schema_after: str,
54
+ adversarial_context_noise: str,
55
+ ) -> BlackBoxAutopsy:
56
+ unified = compute_unified_sql_diff(
57
+ schema_before,
58
+ schema_after,
59
+ previous_turn=previous_turn,
60
+ drift_turn=drift_turn,
61
+ )
62
+ return BlackBoxAutopsy(
63
+ drift_turn=drift_turn,
64
+ previous_turn=previous_turn,
65
+ adversarial_context_noise=adversarial_context_noise,
66
+ unified_diff=unified,
67
+ schema_before=schema_before,
68
+ schema_after=schema_after,
69
+ )
70
+
71
+
72
+ def render_autopsy_panel(autopsy: BlackBoxAutopsy) -> Panel:
73
+ diff_body = autopsy.unified_diff or "(no diff — schemas identical or empty)"
74
+ diff_syntax = Syntax(diff_body, "diff", theme="monokai", line_numbers=True)
75
+ noise = Text(autopsy.adversarial_context_noise or "(no context noise recorded)", style="dim")
76
+ title = (
77
+ f"[bold red]Black Box Autopsy — turn {autopsy.previous_turn:02d} → "
78
+ f"{autopsy.drift_turn:02d}[/bold red]"
79
+ )
80
+ from rich.console import Group
81
+
82
+ return Panel(
83
+ Group(
84
+ Text("Adversarial / context noise at drift turn:", style="bold yellow"),
85
+ noise,
86
+ Text(""),
87
+ Text("Unified SQL diff (git-style):", style="bold"),
88
+ diff_syntax,
89
+ ),
90
+ title=title,
91
+ border_style="red",
92
+ padding=(1, 2),
93
+ )
94
+
95
+
96
+ def render_autopsy_console(autopsy: BlackBoxAutopsy, console: Console | None = None) -> None:
97
+ console = console or Console()
98
+ console.print()
99
+ console.print(render_autopsy_panel(autopsy))
100
+
101
+
102
+ def _diff_line_html(line: str) -> str:
103
+ escaped = html.escape(line)
104
+ if line.startswith("+++") or line.startswith("---"):
105
+ return f'<div class="diff-meta">{escaped}</div>'
106
+ if line.startswith("@@"):
107
+ return f'<div class="diff-hunk">{escaped}</div>'
108
+ if line.startswith("+"):
109
+ return f'<div class="diff-add">{escaped}</div>'
110
+ if line.startswith("-"):
111
+ return f'<div class="diff-del">{escaped}</div>'
112
+ return f'<div class="diff-ctx">{escaped}</div>'
113
+
114
+
115
+ def autopsy_diff_to_html(autopsy: BlackBoxAutopsy | None) -> str:
116
+ if autopsy is None or not autopsy.unified_diff:
117
+ return '<pre class="diff-empty">No drift autopsy recorded.</pre>'
118
+ lines = autopsy.unified_diff.splitlines()
119
+ return "".join(_diff_line_html(line) for line in lines)
120
+
121
+
122
+ def parse_retention_fraction(score: str) -> float | None:
123
+ match = re.match(r"^\s*(\d+)\s*/\s*(\d+)\s*$", score)
124
+ if not match:
125
+ return None
126
+ kept, total = int(match.group(1)), int(match.group(2))
127
+ if total <= 0:
128
+ return None
129
+ return kept / total