decisis 0.1.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.
- decisis/__init__.py +0 -0
- decisis/audit.py +100 -0
- decisis/bench/tasks.json +341 -0
- decisis/checkout.py +94 -0
- decisis/cli.py +151 -0
- decisis/commit.py +137 -0
- decisis/compare.py +309 -0
- decisis/demo.py +222 -0
- decisis/external.py +168 -0
- decisis/fake_vinc.py +63 -0
- decisis/fixtures/audit/demo-lockfile.json +13 -0
- decisis/fixtures/audit/osv-cache.json +1501 -0
- decisis/fixtures/audit/vinc.json +12 -0
- decisis/fixtures/compare/impact-extra.json +46 -0
- decisis/fixtures/compare/lockfile.json +60 -0
- decisis/fixtures/compare/osv-cache.json +146 -0
- decisis/fixtures/demo/vinc_space.json +25 -0
- decisis/fixtures/impact/vinc.json +25 -0
- decisis/fixtures/vinc_subgraph.json +14 -0
- decisis/impact.py +211 -0
- decisis/queries/__init__.py +12 -0
- decisis/queries/decision_to_advisory.cypher +4 -0
- decisis/queries/dependency_paths.cypher +3 -0
- decisis/queries/governing_decisions.cypher +2 -0
- decisis/queries/governing_nodes.cypher +7 -0
- decisis/queries/paths_between.cypher +4 -0
- decisis/sandbox.py +110 -0
- decisis/semver.py +51 -0
- decisis-0.1.0.dist-info/METADATA +12 -0
- decisis-0.1.0.dist-info/RECORD +35 -0
- decisis-0.1.0.dist-info/WHEEL +5 -0
- decisis-0.1.0.dist-info/entry_points.txt +3 -0
- decisis-0.1.0.dist-info/licenses/LICENSE +202 -0
- decisis-0.1.0.dist-info/top_level.txt +2 -0
- vinc_relate/__init__.py +13 -0
decisis/__init__.py
ADDED
|
File without changes
|
decisis/audit.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
from . import external, semver
|
|
7
|
+
from .queries import load as query
|
|
8
|
+
from .sandbox import Subgraph, sandbox
|
|
9
|
+
|
|
10
|
+
SEVERITY = ["LOW", "MODERATE", "MEDIUM", "HIGH", "CRITICAL"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def governance_links(vinc_nodes: list[dict], repo: str) -> list[dict]:
|
|
15
|
+
pattern = re.compile(rf"^{re.escape(repo)}(:|$)")
|
|
16
|
+
out = []
|
|
17
|
+
for n in vinc_nodes:
|
|
18
|
+
ref = (n.get("props") or {}).get("code_ref") or ""
|
|
19
|
+
if isinstance(ref, str) and pattern.match(ref):
|
|
20
|
+
out.append({"source": f"repo:{repo}", "target": n["id"], "type": "DESCRIBED_BY",
|
|
21
|
+
"evidence": f"{n['id']} has code_ref {ref}"})
|
|
22
|
+
return out
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def rule_of(decision: dict) -> dict | None:
|
|
26
|
+
rule = (decision.get("props") or {}).get("rule")
|
|
27
|
+
if isinstance(rule, str):
|
|
28
|
+
try:
|
|
29
|
+
rule = json.loads(rule)
|
|
30
|
+
except json.JSONDecodeError:
|
|
31
|
+
return None
|
|
32
|
+
return rule if isinstance(rule, dict) and rule.get("advisory_severity_at_least") in SEVERITY else None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def compose(vinc: Subgraph, lock_text: str, repo: str, advisories=external.fetch_advisories) -> Subgraph:
|
|
36
|
+
pkg_nodes, dep_edges = external.parse_npm_lock(lock_text, repo)
|
|
37
|
+
adv_nodes, adv_edges = advisories(pkg_nodes)
|
|
38
|
+
links = governance_links(vinc.nodes, repo)
|
|
39
|
+
lock_hash = next((n["props"].get("content_hash") for n in pkg_nodes if n["kind"] == "Repository"), "")
|
|
40
|
+
nodes, edges = vinc.nodes + pkg_nodes + adv_nodes, vinc.edges + dep_edges + adv_edges + links
|
|
41
|
+
meta = {**vinc.meta, "nodes": len(nodes), "edges": len(edges), "repo": repo, "lockfile_hash": lock_hash, "packages": sum(1 for n in pkg_nodes if n["kind"] == "Package"),
|
|
42
|
+
"advisories": len(adv_nodes), "governance_links": len(links)}
|
|
43
|
+
return Subgraph(nodes, edges, meta)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def judge(governing: dict, rule: dict | None, status: str, severity: str, dev: bool) -> dict:
|
|
47
|
+
d = governing["decision"]
|
|
48
|
+
if rule is None:
|
|
49
|
+
return {"decision": d, "verdict": "relevant", "why": f"governs this repository through {governing['via']}; it states no machine-checkable rule"}
|
|
50
|
+
if rule.get("dependencies") == "shipped" and dev:
|
|
51
|
+
return {"decision": d, "verdict": "not_violated", "why": "reached only through development dependencies; the rule covers shipped ones"}
|
|
52
|
+
if status == "affected" and severity in SEVERITY and SEVERITY.index(severity) >= SEVERITY.index(rule["advisory_severity_at_least"]):
|
|
53
|
+
return {"decision": d, "verdict": "violated", "why": f"severity {severity} is at or above {rule['advisory_severity_at_least']}"}
|
|
54
|
+
return {"decision": d, "verdict": "not_violated", "why": f"status {status}, severity {severity}"}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def fold(decisions: list[dict]) -> list[dict]:
|
|
58
|
+
out: dict[str, dict] = {}
|
|
59
|
+
for d in decisions:
|
|
60
|
+
g = out.setdefault(d["decision"], {**d, "via": set()})
|
|
61
|
+
g["via"].add(d["via"])
|
|
62
|
+
return [{**g, "via": ", ".join(sorted(g["via"]))} for g in out.values()]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def verify(rows: list[dict], graph: Subgraph, decisions: list[dict]) -> list[dict]:
|
|
66
|
+
by_id = {n["id"]: n for n in graph.nodes}
|
|
67
|
+
rules = [(d, rule_of(by_id.get(d["decision"], {}))) for d in fold(decisions)]
|
|
68
|
+
findings, seen = [], set()
|
|
69
|
+
for row in rows:
|
|
70
|
+
key = (row["package"], row["advisory"])
|
|
71
|
+
if key in seen:
|
|
72
|
+
continue
|
|
73
|
+
seen.add(key)
|
|
74
|
+
pkg, adv = by_id[row["package"]]["props"], by_id[row["advisory"]]["props"]
|
|
75
|
+
applies = semver.affects(pkg["version"], pkg["ecosystem"], pkg["name"], json.loads(adv["affected_json"]))
|
|
76
|
+
status = {True: "affected", False: "not_affected", None: "unresolved"}[applies]
|
|
77
|
+
sev = adv.get("severity", "UNKNOWN")
|
|
78
|
+
policies = [judge(d, rule, status, sev, bool(pkg.get("dev"))) for d, rule in rules]
|
|
79
|
+
findings.append({"package": row["package"], "advisory": row["advisory"], "severity": sev, "status": status,
|
|
80
|
+
"dev_only": bool(pkg.get("dev")), "path": row["path"], "source_url": adv.get("source_url"),
|
|
81
|
+
"fetched_at": adv.get("fetched_at"), "content_hash": adv.get("content_hash"),
|
|
82
|
+
"advisory_summary": by_id[row["advisory"]].get("title") or "", "policies": policies})
|
|
83
|
+
order = {"affected": 0, "unresolved": 1, "not_affected": 2}
|
|
84
|
+
return sorted(findings, key=lambda f: (order[f["status"]], -SEVERITY.index(f["severity"]) if f["severity"] in SEVERITY else 0, len(f["path"])))
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def analyze(box, graph: Subgraph, repo: str, depth: int = 8) -> dict:
|
|
88
|
+
counts = box.load(graph)
|
|
89
|
+
rows = box.query(query("dependency_paths", depth=depth), {"repo": f"repo:{repo}"})
|
|
90
|
+
decisions = box.query(query("governing_decisions"), {"repo": f"repo:{repo}"})
|
|
91
|
+
findings = verify(rows, graph, decisions)
|
|
92
|
+
summary = {s: sum(1 for f in findings if f["status"] == s) for s in ("affected", "unresolved", "not_affected")}
|
|
93
|
+
return {"meta": graph.meta, "loaded": counts, "governing": decisions, "summary": summary,
|
|
94
|
+
"violations": sum(1 for f in findings for p in f["policies"] if p["verdict"] == "violated"), "findings": findings}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def run(vinc: Subgraph, lock_text: str, repo: str, depth: int = 8, advisories=external.fetch_advisories) -> dict:
|
|
98
|
+
graph = compose(vinc, lock_text, repo, advisories)
|
|
99
|
+
with sandbox() as box:
|
|
100
|
+
return analyze(box, graph, repo, depth)
|
decisis/bench/tasks.json
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"repo": "Demo",
|
|
4
|
+
"inputs": {
|
|
5
|
+
"audit": {
|
|
6
|
+
"snapshot": "fixtures/audit/vinc.json",
|
|
7
|
+
"lockfile": "fixtures/compare/lockfile.json",
|
|
8
|
+
"osv_cache": "fixtures/compare/osv-cache.json"
|
|
9
|
+
},
|
|
10
|
+
"impact": {
|
|
11
|
+
"snapshots": [
|
|
12
|
+
"fixtures/impact/vinc.json",
|
|
13
|
+
"fixtures/compare/impact-extra.json"
|
|
14
|
+
],
|
|
15
|
+
"hops": 3
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"methods": {
|
|
19
|
+
"A": "Vinc retrieval only: nodes whose code_ref matches the subject, then their one-hop neighbours",
|
|
20
|
+
"B": "A plus the lockfile and the advisories as flat records joined by package name",
|
|
21
|
+
"C": "A Relate graph of the Vinc snapshot, the lockfile and the advisories, queried with multi-hop paths"
|
|
22
|
+
},
|
|
23
|
+
"tasks": [
|
|
24
|
+
{
|
|
25
|
+
"id": "audit-direct-dependency",
|
|
26
|
+
"kind": "audit",
|
|
27
|
+
"subject": "pkg:npm/chart-kit@1.2.0",
|
|
28
|
+
"connection": "one hop: the repository depends on the affected package directly",
|
|
29
|
+
"graph_expected_to_help": false,
|
|
30
|
+
"why": "A direct dependency is named in the lockfile's root record, so a text join finds the package, the advisory and the path. MODERATE is below the HIGH rule.",
|
|
31
|
+
"expected": {
|
|
32
|
+
"findings": [
|
|
33
|
+
{
|
|
34
|
+
"package": "pkg:npm/chart-kit@1.2.0",
|
|
35
|
+
"advisory": "adv:DEMO-2026-0004",
|
|
36
|
+
"status": "affected"
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
"paths": [
|
|
40
|
+
[
|
|
41
|
+
"repo:Demo",
|
|
42
|
+
"pkg:npm/chart-kit@1.2.0",
|
|
43
|
+
"adv:DEMO-2026-0004"
|
|
44
|
+
]
|
|
45
|
+
],
|
|
46
|
+
"policy_links": [
|
|
47
|
+
{
|
|
48
|
+
"package": "pkg:npm/chart-kit@1.2.0",
|
|
49
|
+
"advisory": "adv:DEMO-2026-0004",
|
|
50
|
+
"decision": "decision:no-high-advisories-in-shipped-dependencies",
|
|
51
|
+
"verdict": "not_violated"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"package": "pkg:npm/chart-kit@1.2.0",
|
|
55
|
+
"advisory": "adv:DEMO-2026-0004",
|
|
56
|
+
"decision": "decision:dependencies-are-pinned-by-a-lockfile",
|
|
57
|
+
"verdict": "relevant"
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"id": "audit-transitive-shipped",
|
|
64
|
+
"kind": "audit",
|
|
65
|
+
"subject": "pkg:npm/url-parse-lite@1.0.2",
|
|
66
|
+
"connection": "three hops through two direct dependencies that share http-core",
|
|
67
|
+
"graph_expected_to_help": true,
|
|
68
|
+
"why": "The affected copy is reached only through http-core, which no root record names; both shipped chains count. HIGH in a shipped dependency violates the rule.",
|
|
69
|
+
"expected": {
|
|
70
|
+
"findings": [
|
|
71
|
+
{
|
|
72
|
+
"package": "pkg:npm/url-parse-lite@1.0.2",
|
|
73
|
+
"advisory": "adv:DEMO-2026-0001",
|
|
74
|
+
"status": "affected"
|
|
75
|
+
}
|
|
76
|
+
],
|
|
77
|
+
"paths": [
|
|
78
|
+
[
|
|
79
|
+
"repo:Demo",
|
|
80
|
+
"pkg:npm/web-kit@3.0.1",
|
|
81
|
+
"pkg:npm/http-core@2.4.0",
|
|
82
|
+
"pkg:npm/url-parse-lite@1.0.2",
|
|
83
|
+
"adv:DEMO-2026-0001"
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
"repo:Demo",
|
|
87
|
+
"pkg:npm/chart-kit@1.2.0",
|
|
88
|
+
"pkg:npm/http-core@2.4.0",
|
|
89
|
+
"pkg:npm/url-parse-lite@1.0.2",
|
|
90
|
+
"adv:DEMO-2026-0001"
|
|
91
|
+
]
|
|
92
|
+
],
|
|
93
|
+
"policy_links": [
|
|
94
|
+
{
|
|
95
|
+
"package": "pkg:npm/url-parse-lite@1.0.2",
|
|
96
|
+
"advisory": "adv:DEMO-2026-0001",
|
|
97
|
+
"decision": "decision:no-high-advisories-in-shipped-dependencies",
|
|
98
|
+
"verdict": "violated"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"package": "pkg:npm/url-parse-lite@1.0.2",
|
|
102
|
+
"advisory": "adv:DEMO-2026-0001",
|
|
103
|
+
"decision": "decision:dependencies-are-pinned-by-a-lockfile",
|
|
104
|
+
"verdict": "relevant"
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"id": "audit-nested-dev-copy",
|
|
111
|
+
"kind": "audit",
|
|
112
|
+
"subject": "pkg:npm/url-parse-lite@1.0.4",
|
|
113
|
+
"connection": "scope: a second copy of the same package, nested under a development dependency",
|
|
114
|
+
"graph_expected_to_help": true,
|
|
115
|
+
"why": "test-kit gets its own nested copy 1.0.4; the hoisted 1.0.2 is not reached through test-kit. The nested copy is development only, so the shipped rule is not violated.",
|
|
116
|
+
"expected": {
|
|
117
|
+
"findings": [
|
|
118
|
+
{
|
|
119
|
+
"package": "pkg:npm/url-parse-lite@1.0.4",
|
|
120
|
+
"advisory": "adv:DEMO-2026-0001",
|
|
121
|
+
"status": "affected"
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
"paths": [
|
|
125
|
+
[
|
|
126
|
+
"repo:Demo",
|
|
127
|
+
"pkg:npm/test-kit@1.0.3",
|
|
128
|
+
"pkg:npm/url-parse-lite@1.0.4",
|
|
129
|
+
"adv:DEMO-2026-0001"
|
|
130
|
+
]
|
|
131
|
+
],
|
|
132
|
+
"policy_links": [
|
|
133
|
+
{
|
|
134
|
+
"package": "pkg:npm/url-parse-lite@1.0.4",
|
|
135
|
+
"advisory": "adv:DEMO-2026-0001",
|
|
136
|
+
"decision": "decision:no-high-advisories-in-shipped-dependencies",
|
|
137
|
+
"verdict": "not_violated"
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
"package": "pkg:npm/url-parse-lite@1.0.4",
|
|
141
|
+
"advisory": "adv:DEMO-2026-0001",
|
|
142
|
+
"decision": "decision:dependencies-are-pinned-by-a-lockfile",
|
|
143
|
+
"verdict": "relevant"
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
"id": "audit-dev-only-direct",
|
|
150
|
+
"kind": "audit",
|
|
151
|
+
"subject": "pkg:npm/mock-server@0.9.0",
|
|
152
|
+
"connection": "scope: a direct development dependency with a CRITICAL advisory (decoy for the shipped rule)",
|
|
153
|
+
"graph_expected_to_help": false,
|
|
154
|
+
"why": "The lockfile record carries dev: true, so the text join sees the scope without a graph. Development only, so not violated.",
|
|
155
|
+
"expected": {
|
|
156
|
+
"findings": [
|
|
157
|
+
{
|
|
158
|
+
"package": "pkg:npm/mock-server@0.9.0",
|
|
159
|
+
"advisory": "adv:DEMO-2026-0003",
|
|
160
|
+
"status": "affected"
|
|
161
|
+
}
|
|
162
|
+
],
|
|
163
|
+
"paths": [
|
|
164
|
+
[
|
|
165
|
+
"repo:Demo",
|
|
166
|
+
"pkg:npm/mock-server@0.9.0",
|
|
167
|
+
"adv:DEMO-2026-0003"
|
|
168
|
+
]
|
|
169
|
+
],
|
|
170
|
+
"policy_links": [
|
|
171
|
+
{
|
|
172
|
+
"package": "pkg:npm/mock-server@0.9.0",
|
|
173
|
+
"advisory": "adv:DEMO-2026-0003",
|
|
174
|
+
"decision": "decision:no-high-advisories-in-shipped-dependencies",
|
|
175
|
+
"verdict": "not_violated"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"package": "pkg:npm/mock-server@0.9.0",
|
|
179
|
+
"advisory": "adv:DEMO-2026-0003",
|
|
180
|
+
"decision": "decision:dependencies-are-pinned-by-a-lockfile",
|
|
181
|
+
"verdict": "relevant"
|
|
182
|
+
}
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"id": "audit-version-outside-range",
|
|
188
|
+
"kind": "audit",
|
|
189
|
+
"subject": "pkg:npm/color-fmt@2.1.0",
|
|
190
|
+
"connection": "decoy: the advisory names the package but 2.1.0 is outside the affected range",
|
|
191
|
+
"graph_expected_to_help": false,
|
|
192
|
+
"why": "The range check reads the advisory record; it needs no graph. Not affected, so not violated.",
|
|
193
|
+
"expected": {
|
|
194
|
+
"findings": [
|
|
195
|
+
{
|
|
196
|
+
"package": "pkg:npm/color-fmt@2.1.0",
|
|
197
|
+
"advisory": "adv:DEMO-2026-0002",
|
|
198
|
+
"status": "not_affected"
|
|
199
|
+
}
|
|
200
|
+
],
|
|
201
|
+
"paths": [
|
|
202
|
+
[
|
|
203
|
+
"repo:Demo",
|
|
204
|
+
"pkg:npm/chart-kit@1.2.0",
|
|
205
|
+
"pkg:npm/color-fmt@2.1.0",
|
|
206
|
+
"adv:DEMO-2026-0002"
|
|
207
|
+
]
|
|
208
|
+
],
|
|
209
|
+
"policy_links": [
|
|
210
|
+
{
|
|
211
|
+
"package": "pkg:npm/color-fmt@2.1.0",
|
|
212
|
+
"advisory": "adv:DEMO-2026-0002",
|
|
213
|
+
"decision": "decision:no-high-advisories-in-shipped-dependencies",
|
|
214
|
+
"verdict": "not_violated"
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
"package": "pkg:npm/color-fmt@2.1.0",
|
|
218
|
+
"advisory": "adv:DEMO-2026-0002",
|
|
219
|
+
"decision": "decision:dependencies-are-pinned-by-a-lockfile",
|
|
220
|
+
"verdict": "relevant"
|
|
221
|
+
}
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
"id": "impact-two-hop-decision",
|
|
227
|
+
"kind": "impact",
|
|
228
|
+
"subject": "storage/page_store.py",
|
|
229
|
+
"connection": "two and three hops: the decision sets a budget that the check enforces",
|
|
230
|
+
"graph_expected_to_help": true,
|
|
231
|
+
"why": "The decision is ABOUT the page budget, which is RELATED_TO the check whose code_ref names the file; the review record hangs off the decision.",
|
|
232
|
+
"expected": {
|
|
233
|
+
"paths": [
|
|
234
|
+
[
|
|
235
|
+
"concept:demo-page-size-check",
|
|
236
|
+
"concept:demo-page-budget",
|
|
237
|
+
"decision:demo-oversized-pages-are-refused"
|
|
238
|
+
],
|
|
239
|
+
[
|
|
240
|
+
"concept:demo-page-size-check",
|
|
241
|
+
"concept:demo-page-budget",
|
|
242
|
+
"decision:demo-oversized-pages-are-refused",
|
|
243
|
+
"record:demo-page-size-check-review"
|
|
244
|
+
]
|
|
245
|
+
],
|
|
246
|
+
"policy_links": [
|
|
247
|
+
{
|
|
248
|
+
"path": "storage/page_store.py",
|
|
249
|
+
"node": "decision:demo-oversized-pages-are-refused"
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"path": "storage/page_store.py",
|
|
253
|
+
"node": "record:demo-page-size-check-review"
|
|
254
|
+
}
|
|
255
|
+
]
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
"id": "impact-one-hop-decision",
|
|
260
|
+
"kind": "impact",
|
|
261
|
+
"subject": "reports/export.py",
|
|
262
|
+
"connection": "one hop: the decision and the record attach to the concept that names the file",
|
|
263
|
+
"graph_expected_to_help": false,
|
|
264
|
+
"why": "Both governing nodes are one edge from the anchor, which one-hop retrieval returns.",
|
|
265
|
+
"expected": {
|
|
266
|
+
"paths": [
|
|
267
|
+
[
|
|
268
|
+
"concept:demo-report-export",
|
|
269
|
+
"decision:demo-report-exports-finish-in-time"
|
|
270
|
+
],
|
|
271
|
+
[
|
|
272
|
+
"concept:demo-report-export",
|
|
273
|
+
"record:demo-report-export-timed-out"
|
|
274
|
+
]
|
|
275
|
+
],
|
|
276
|
+
"policy_links": [
|
|
277
|
+
{
|
|
278
|
+
"path": "reports/export.py",
|
|
279
|
+
"node": "decision:demo-report-exports-finish-in-time"
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
"path": "reports/export.py",
|
|
283
|
+
"node": "record:demo-report-export-timed-out"
|
|
284
|
+
}
|
|
285
|
+
]
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
"id": "impact-directory-prefix-decoy",
|
|
290
|
+
"kind": "impact",
|
|
291
|
+
"subject": "reports/",
|
|
292
|
+
"connection": "decoy: reports_archive/ shares the prefix but is another directory",
|
|
293
|
+
"graph_expected_to_help": false,
|
|
294
|
+
"why": "All three methods use the same code_ref matcher, so the sibling directory is excluded by matching, not by the graph.",
|
|
295
|
+
"expected": {
|
|
296
|
+
"paths": [
|
|
297
|
+
[
|
|
298
|
+
"concept:demo-report-export",
|
|
299
|
+
"decision:demo-report-exports-finish-in-time"
|
|
300
|
+
],
|
|
301
|
+
[
|
|
302
|
+
"concept:demo-report-export",
|
|
303
|
+
"record:demo-report-export-timed-out"
|
|
304
|
+
]
|
|
305
|
+
],
|
|
306
|
+
"policy_links": [
|
|
307
|
+
{
|
|
308
|
+
"path": "reports/",
|
|
309
|
+
"node": "decision:demo-report-exports-finish-in-time"
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
"path": "reports/",
|
|
313
|
+
"node": "record:demo-report-export-timed-out"
|
|
314
|
+
}
|
|
315
|
+
]
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
"id": "impact-overreach-decoy",
|
|
320
|
+
"kind": "impact",
|
|
321
|
+
"subject": "reports_archive/index.py",
|
|
322
|
+
"connection": "decoy: a log retention decision two hops away through a shared logger",
|
|
323
|
+
"graph_expected_to_help": false,
|
|
324
|
+
"why": "The archive is logged through the shared logger, but the retention decision governs the logger's files, not the archive index. A bounded walk that ignores edge meaning reaches it anyway.",
|
|
325
|
+
"expected": {
|
|
326
|
+
"paths": [
|
|
327
|
+
[
|
|
328
|
+
"concept:demo-report-archive",
|
|
329
|
+
"decision:demo-archive-reads-are-rate-limited"
|
|
330
|
+
]
|
|
331
|
+
],
|
|
332
|
+
"policy_links": [
|
|
333
|
+
{
|
|
334
|
+
"path": "reports_archive/index.py",
|
|
335
|
+
"node": "decision:demo-archive-reads-are-rate-limited"
|
|
336
|
+
}
|
|
337
|
+
]
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
]
|
|
341
|
+
}
|
decisis/checkout.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import urllib.request
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
|
|
8
|
+
from .sandbox import Subgraph
|
|
9
|
+
|
|
10
|
+
DEFAULT_URL = "https://mcp.vincs.io/mcp"
|
|
11
|
+
EDGE_TYPES = {"RELATED_TO", "PREREQUISITE_OF", "DERIVES_FROM", "INHERITS_FROM", "CONNECTS_VIA", "ABOUT", "SUPERSEDES", "RETURNS_TO"}
|
|
12
|
+
WRITE_NAMES = {"RELATES_TO": "RELATED_TO"}
|
|
13
|
+
CARRIER = "ATTACHES_TO"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def relation(edge: dict) -> str:
|
|
17
|
+
rel = str(edge.get("type", "")).upper()
|
|
18
|
+
if rel == CARRIER:
|
|
19
|
+
rel = str(edge.get("role") or edge.get("subtype") or "").upper()
|
|
20
|
+
return WRITE_NAMES.get(rel, rel)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class VincMCP:
|
|
24
|
+
def __init__(self, token: str, url: str = DEFAULT_URL, space: str | None = None, opener=urllib.request.urlopen):
|
|
25
|
+
self.token, self.url, self.space, self.opener, self.session = token, url, space, opener, None
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def from_env(cls, key_kind: str = "read-only") -> "VincMCP":
|
|
29
|
+
token = os.environ.get("VINC_API_KEY")
|
|
30
|
+
if not token:
|
|
31
|
+
raise SystemExit(f"VINC_API_KEY is not set; use a {key_kind} key")
|
|
32
|
+
return cls(token, os.environ.get("VINC_MCP_URL", DEFAULT_URL), os.environ.get("VINC_SPACE"))
|
|
33
|
+
|
|
34
|
+
def _post(self, payload: dict) -> dict:
|
|
35
|
+
headers = {"Authorization": f"Bearer {self.token}", "Content-Type": "application/json", "Accept": "application/json, text/event-stream"}
|
|
36
|
+
if self.session:
|
|
37
|
+
headers["Mcp-Session-Id"] = self.session
|
|
38
|
+
req = urllib.request.Request(self.url, data=json.dumps(payload).encode(), headers=headers)
|
|
39
|
+
with self.opener(req, timeout=60) as resp:
|
|
40
|
+
self.session = resp.headers.get("Mcp-Session-Id") or self.session
|
|
41
|
+
body = resp.read().decode()
|
|
42
|
+
start = body.find("{")
|
|
43
|
+
return json.loads(body[start:]) if start >= 0 else {}
|
|
44
|
+
|
|
45
|
+
def tool(self, name: str, arguments: dict) -> dict:
|
|
46
|
+
if self.session is None:
|
|
47
|
+
self._post({"jsonrpc": "2.0", "id": 0, "method": "initialize",
|
|
48
|
+
"params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "decisis", "version": "0"}}})
|
|
49
|
+
args = {**arguments, **({"space": self.space} if self.space else {})}
|
|
50
|
+
reply = self._post({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": name, "arguments": args}})
|
|
51
|
+
result = reply.get("result") or {}
|
|
52
|
+
if result.get("isError") or "error" in reply:
|
|
53
|
+
raise RuntimeError(f"{name} failed: {json.dumps(reply)[:300]}")
|
|
54
|
+
if "structuredContent" in result:
|
|
55
|
+
return result["structuredContent"]
|
|
56
|
+
text = "".join(c.get("text", "") for c in result.get("content", []) if c.get("type") == "text")
|
|
57
|
+
return json.loads(text) if text else {}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def normalize(raw_graphs: list[dict], seeds: list[str], space: str | None) -> Subgraph:
|
|
61
|
+
nodes: dict[str, dict] = {}
|
|
62
|
+
edges: dict[tuple, dict] = {}
|
|
63
|
+
for g in raw_graphs:
|
|
64
|
+
for n in g.get("nodes", []):
|
|
65
|
+
nodes.setdefault(n["id"], {"id": n["id"], "kind": n.get("kind") or n["id"].split(":", 1)[0], "title": n.get("label") or n.get("title") or "",
|
|
66
|
+
"domain": n.get("domain") or "", "valid_until": n.get("valid_until"), "props": n.get("props") or {}})
|
|
67
|
+
for e in g.get("edges", []):
|
|
68
|
+
rel = relation(e)
|
|
69
|
+
if rel not in EDGE_TYPES:
|
|
70
|
+
continue
|
|
71
|
+
key = (e["source"], rel, e["target"])
|
|
72
|
+
edges.setdefault(key, {"source": e["source"], "target": e["target"], "type": rel, "evidence": e.get("evidence") or ""})
|
|
73
|
+
kept = [e for e in edges.values() if e["source"] in nodes and e["target"] in nodes]
|
|
74
|
+
meta = {"seeds": seeds, "space": space or "personal", "extracted_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
75
|
+
"nodes": len(nodes), "edges": len(kept)}
|
|
76
|
+
return Subgraph(sorted(nodes.values(), key=lambda n: n["id"]), sorted(kept, key=lambda e: (e["source"], e["type"], e["target"])), meta)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def checkout(client: VincMCP, seeds: list[str], depth: int = 1) -> Subgraph:
|
|
80
|
+
raws = [client.tool("vinc_graph", {"seed_id": s, "depth": depth}) for s in seeds]
|
|
81
|
+
return normalize(raws, seeds, client.space)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def dump(graph: Subgraph) -> str:
|
|
85
|
+
return json.dumps({"meta": graph.meta, "nodes": graph.nodes, "edges": graph.edges}, ensure_ascii=False, indent=1)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def load(text: str) -> Subgraph:
|
|
89
|
+
d = json.loads(text)
|
|
90
|
+
meta = d.get("meta", {})
|
|
91
|
+
for key in ("nodes", "edges"):
|
|
92
|
+
if isinstance(meta.get(key), int) and meta[key] != len(d[key]):
|
|
93
|
+
raise ValueError(f"the snapshot says it has {meta[key]} {key} but lists {len(d[key])}")
|
|
94
|
+
return Subgraph(d["nodes"], d["edges"], meta)
|