cfgit-impact 0.4.7__tar.gz → 0.5.0__tar.gz

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.
@@ -114,6 +114,7 @@ activemq-data/
114
114
 
115
115
  # cfgit local state
116
116
  .cfg/
117
+ .cfgit/
117
118
  /.cfg.toml
118
119
 
119
120
  # Local project notes and recording drafts
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: cfgit-impact
3
- Version: 0.4.7
3
+ Version: 0.5.0
4
4
  Summary: Optional cfgit plugin for deterministic system-impact summaries and opt-in LLM narration of database record diffs
5
5
  Project-URL: Homepage, https://github.com/AusafMo/cfgit
6
6
  Project-URL: Repository, https://github.com/AusafMo/cfgit
@@ -9,7 +9,7 @@ Project-URL: Documentation, https://github.com/AusafMo/cfgit/tree/main/plugins/c
9
9
  Author: Mohammad Ausaf
10
10
  License-Expression: Apache-2.0
11
11
  Requires-Python: >=3.11
12
- Requires-Dist: cfgit<0.5.0,>=0.4.7
12
+ Requires-Dist: cfgit<0.6.0,>=0.5.0
13
13
  Requires-Dist: httpx>=0.27
14
14
  Description-Content-Type: text/markdown
15
15
 
@@ -0,0 +1,58 @@
1
+ # Copyright 2026 Mohammad Ausaf. Licensed under the Apache License, Version 2.0.
2
+ """Find where application code reads a record by id (`code_refs` on a [[collection]])."""
3
+ from __future__ import annotations
4
+
5
+ import fnmatch
6
+ import os
7
+ from pathlib import Path
8
+ import re
9
+ from typing import Any
10
+
11
+ _SKIP_DIRS = {".git", "node_modules", ".venv", "venv", "__pycache__", "dist", "build", ".mypy_cache", ".pytest_cache"}
12
+ _MAX_FILE_BYTES = 1_000_000
13
+ MAX_MATCHES = 50
14
+
15
+
16
+ def scan(code_refs: dict[str, Any], record_id: str, *, base_dir: Path) -> dict[str, Any]:
17
+ """Search `roots` (relative to the config file) for `pattern`, with `{id}` replaced by the
18
+ escaped record id. Returns {"checked": True, "matches": [{file, line, text}], "truncated"}."""
19
+ regex = re.compile(code_refs["pattern"].replace("{id}", re.escape(record_id)))
20
+ globs = code_refs.get("globs") or []
21
+ matches: list[dict[str, Any]] = []
22
+ missing_roots: list[str] = []
23
+ for root in code_refs["roots"]:
24
+ root_path = Path(root).expanduser()
25
+ root_path = root_path if root_path.is_absolute() else base_dir / root_path
26
+ if not root_path.exists():
27
+ missing_roots.append(str(root_path))
28
+ continue
29
+ for path in _files(root_path, globs):
30
+ for number, text in _grep(path, regex):
31
+ matches.append({"file": str(path), "line": number, "text": text.strip()[:200]})
32
+ if len(matches) >= MAX_MATCHES:
33
+ return {"checked": True, "matches": matches, "truncated": True, "missing_roots": missing_roots}
34
+ return {"checked": True, "matches": matches, "truncated": False, "missing_roots": missing_roots}
35
+
36
+
37
+ def _files(root: Path, globs: list[str]):
38
+ if root.is_file():
39
+ yield root
40
+ return
41
+ for dirpath, dirnames, filenames in os.walk(root):
42
+ dirnames[:] = sorted(d for d in dirnames if d not in _SKIP_DIRS)
43
+ for name in sorted(filenames):
44
+ if globs and not any(fnmatch.fnmatch(name, g) for g in globs):
45
+ continue
46
+ yield Path(dirpath) / name
47
+
48
+
49
+ def _grep(path: Path, regex: re.Pattern[str]):
50
+ try:
51
+ if path.stat().st_size > _MAX_FILE_BYTES:
52
+ return
53
+ with path.open("r", encoding="utf-8") as f:
54
+ for number, line in enumerate(f, start=1):
55
+ if regex.search(line):
56
+ yield number, line
57
+ except (UnicodeDecodeError, OSError):
58
+ return
@@ -8,7 +8,7 @@ import re
8
8
  import sys
9
9
  from typing import Any
10
10
 
11
- from cfg.core.engine import Engine, RecordRef
11
+ from cfg.core.engine import Engine, RecordRef, version_content
12
12
  from cfg.core.hashing import strip_for_hash
13
13
  from cfg.interfaces.actions import parse_record, to_json
14
14
  from cfg_impact.providers.factory import ImpactProviderFactory
@@ -38,8 +38,8 @@ def deterministic_overview(
38
38
  ref = parse_record(record)
39
39
  coll = engine.config.collection(ref.collection)
40
40
  changes = engine.diff(ref, a, b)
41
- left = strip_for_hash(engine.resolve_ref(ref, a)["doc"], coll)
42
- right = strip_for_hash(engine.resolve_ref(ref, b)["doc"], coll)
41
+ left = version_content(engine.resolve_ref(ref, a), coll)
42
+ right = version_content(engine.resolve_ref(ref, b), coll)
43
43
  paths = [str(change.get("path", "")) for change in changes]
44
44
  changed_values = _changed_string_values(changes)
45
45
  affected = _find_affected_records(engine, source=ref, values=[ref.record_id, *changed_values])
@@ -47,6 +47,13 @@ def deterministic_overview(
47
47
  categories = _categories(paths)
48
48
  risk_level = _risk_level(paths, changes, affected)
49
49
  summary = _deterministic_summary(record, changes, categories, affected, declared_links, risk_level)
50
+ code = code_references(engine, record)
51
+ if code["checked"]:
52
+ n = len(code["matches"])
53
+ summary += (
54
+ f" Code reads it in {n}{'+' if code['truncated'] else ''} place(s)."
55
+ if n else " No code reference found by the configured code_refs scan."
56
+ )
50
57
 
51
58
  return {
52
59
  "record": record,
@@ -59,6 +66,7 @@ def deterministic_overview(
59
66
  "change_count": len(changes),
60
67
  "declared_links_changed": declared_links,
61
68
  "affected_records": affected,
69
+ "code_references": code,
62
70
  "rollback_note": "Use restore on this record or restore system by tag/as-of if this change already shipped.",
63
71
  "unknowns": _unknowns(left, right, affected),
64
72
  "changes": changes,
@@ -247,6 +255,38 @@ def _system_map(
247
255
  return result
248
256
 
249
257
 
258
+ def code_references(engine: Engine, record: str) -> dict[str, Any]:
259
+ """Where application code uses this record's id, per the collection's `code_refs`
260
+ setting ({"checked": False} when none is configured)."""
261
+ ref = parse_record(record)
262
+ cfg = engine.config.collection(ref.collection).code_refs
263
+ if not cfg:
264
+ return {"checked": False, "matches": []}
265
+ from cfg_impact.code_refs import scan
266
+
267
+ return scan(cfg, ref.record_id, base_dir=engine.config.path.parent)
268
+
269
+
270
+ def references(engine: Engine, record: str) -> list[dict[str, Any]]:
271
+ """Other tracked/live records whose live doc mentions this record's id (static scan).
272
+ `cfg rm` uses it to refuse deleting a record something still points at."""
273
+ ref = parse_record(record)
274
+ # the id as a whole token: `router` must not match `new_router` or `router_v2`
275
+ token = re.compile(rf"(?<![\w-]){re.escape(ref.record_id)}(?![\w-])")
276
+ affected: list[dict[str, Any]] = []
277
+ for row in engine.status():
278
+ if (row.collection, row.record_id) == (ref.collection, ref.record_id) or row.state in {"deleted", "missing"}:
279
+ continue
280
+ try:
281
+ doc = engine.resolve_ref(RecordRef(row.collection, row.record_id), "=live")["doc"]
282
+ except Exception:
283
+ continue
284
+ if token.search(json.dumps(to_json(doc), sort_keys=True, ensure_ascii=False)):
285
+ affected.append({"collection": row.collection, "record_id": row.record_id,
286
+ "state": row.state, "matched_values": [ref.record_id]})
287
+ return affected
288
+
289
+
250
290
  def _find_affected_records(
251
291
  engine: Engine,
252
292
  *,
@@ -1,13 +1,13 @@
1
1
  [project]
2
2
  name = "cfgit-impact"
3
- version = "0.4.7"
3
+ version = "0.5.0"
4
4
  description = "Optional cfgit plugin for deterministic system-impact summaries and opt-in LLM narration of database record diffs"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
7
7
  license = "Apache-2.0"
8
8
  authors = [{ name = "Mohammad Ausaf" }]
9
9
  dependencies = [
10
- "cfgit>=0.4.7,<0.5.0",
10
+ "cfgit>=0.5.0,<0.6.0",
11
11
  "httpx>=0.27",
12
12
  ]
13
13
 
File without changes