MCPcopy Create free account
hub / github.com/VectifyAI/OpenKB / write_diff

Function write_diff

openkb/skill/workspace.py:139–188  ·  view source on GitHub ↗

Write a human-readable structural diff from ``prev`` to ``curr``. Covers: * description: line changes (from SKILL.md frontmatter) * files added/removed under ``references/`` and ``scripts/`` * line-count delta on SKILL.md

(prev: Path, curr: Path, diff_path: Path)

Source from the content-addressed store, hash-verified

137
138
139def write_diff(prev: Path, curr: Path, diff_path: Path) -> None:
140 """Write a human-readable structural diff from ``prev`` to ``curr``.
141
142 Covers:
143 * description: line changes (from SKILL.md frontmatter)
144 * files added/removed under ``references/`` and ``scripts/``
145 * line-count delta on SKILL.md
146 """
147 prev_desc = extract_description(prev / "SKILL.md")
148 curr_desc = extract_description(curr / "SKILL.md")
149
150 prev_refs = _list_files(prev, "references")
151 curr_refs = _list_files(curr, "references")
152 prev_scripts = _list_files(prev, "scripts")
153 curr_scripts = _list_files(curr, "scripts")
154
155 added = sorted((curr_refs - prev_refs) | (curr_scripts - prev_scripts))
156 removed = sorted((prev_refs - curr_refs) | (prev_scripts - curr_scripts))
157
158 prev_lc = _line_count(prev / "SKILL.md")
159 curr_lc = _line_count(curr / "SKILL.md")
160 delta = curr_lc - prev_lc
161
162 lines: list[str] = []
163 lines.append(f"# Skill diff: {prev.name} -> current\n")
164
165 lines.append("## description\n")
166 if prev_desc == curr_desc:
167 lines.append("_unchanged_\n")
168 else:
169 lines.append(f"- before: {prev_desc or '(none)'}")
170 lines.append(f"- after: {curr_desc or '(none)'}\n")
171
172 lines.append("## files\n")
173 if not added and not removed:
174 lines.append("_no files added or removed_\n")
175 else:
176 for p in added:
177 lines.append(f"- added: {p}")
178 for p in removed:
179 lines.append(f"- removed: {p}")
180 lines.append("")
181
182 lines.append("## SKILL.md line count\n")
183 sign = "+" if delta >= 0 else ""
184 lines.append(f"- before: {prev_lc} lines")
185 lines.append(f"- after: {curr_lc} lines ({sign}{delta})\n")
186
187 diff_path.parent.mkdir(parents=True, exist_ok=True)
188 diff_path.write_text("\n".join(lines), encoding="utf-8")

Calls 3

extract_descriptionFunction · 0.90
_list_filesFunction · 0.85
_line_countFunction · 0.70