rd0-jinx 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.
- rd0_jinx/__init__.py +6 -0
- rd0_jinx/action.py +110 -0
- rd0_jinx/agents.py +35 -0
- rd0_jinx/cli.py +119 -0
- rd0_jinx/data/templates.json +456 -0
- rd0_jinx/data/weights.json +61 -0
- rd0_jinx/data/weights_emb.json +65 -0
- rd0_jinx/embed.py +26 -0
- rd0_jinx/features.py +61 -0
- rd0_jinx/pr.py +110 -0
- rd0_jinx/py.typed +0 -0
- rd0_jinx/scorer.py +156 -0
- rd0_jinx/templates.py +107 -0
- rd0_jinx/text.py +81 -0
- rd0_jinx-0.1.0.dist-info/METADATA +108 -0
- rd0_jinx-0.1.0.dist-info/RECORD +20 -0
- rd0_jinx-0.1.0.dist-info/WHEEL +5 -0
- rd0_jinx-0.1.0.dist-info/entry_points.txt +2 -0
- rd0_jinx-0.1.0.dist-info/licenses/LICENSE +9 -0
- rd0_jinx-0.1.0.dist-info/top_level.txt +1 -0
rd0_jinx/__init__.py
ADDED
rd0_jinx/action.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""GitHub Action entry point: comment on a newly opened PR when likely duplicates are open.
|
|
2
|
+
|
|
3
|
+
Uses only the GitHub REST API with the workflow's GITHUB_TOKEN. No LLM or paid API calls."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
import urllib.request
|
|
11
|
+
|
|
12
|
+
from .pr import PR
|
|
13
|
+
from .scorer import Detector
|
|
14
|
+
|
|
15
|
+
MARKER = "<!-- rd0_jinx:v1 -->"
|
|
16
|
+
API = os.environ.get("GITHUB_API_URL", "https://api.github.com")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class GitHub:
|
|
20
|
+
def __init__(self, token: str, repo: str):
|
|
21
|
+
self.token, self.repo = token, repo
|
|
22
|
+
|
|
23
|
+
def call(self, method: str, path: str, body: dict | None = None):
|
|
24
|
+
req = urllib.request.Request(
|
|
25
|
+
f"{API}/repos/{self.repo}{path}",
|
|
26
|
+
method=method,
|
|
27
|
+
data=json.dumps(body).encode() if body is not None else None,
|
|
28
|
+
)
|
|
29
|
+
req.add_header("Authorization", f"Bearer {self.token}")
|
|
30
|
+
req.add_header("Accept", "application/vnd.github+json")
|
|
31
|
+
req.add_header("X-GitHub-Api-Version", "2022-11-28")
|
|
32
|
+
with urllib.request.urlopen(req, timeout=30) as r:
|
|
33
|
+
return json.loads(r.read() or b"null")
|
|
34
|
+
|
|
35
|
+
def pages(self, path: str, limit: int):
|
|
36
|
+
out, page = [], 1
|
|
37
|
+
while len(out) < limit:
|
|
38
|
+
sep = "&" if "?" in path else "?"
|
|
39
|
+
batch = self.call("GET", f"{path}{sep}per_page=100&page={page}")
|
|
40
|
+
if not batch:
|
|
41
|
+
break
|
|
42
|
+
out.extend(batch)
|
|
43
|
+
page += 1
|
|
44
|
+
return out[:limit]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def pr_from_api(gh: GitHub, d: dict) -> PR:
|
|
48
|
+
files = gh.pages(f"/pulls/{d['number']}/files", 300)
|
|
49
|
+
diff = "".join(f"diff --git a/{f['filename']} b/{f['filename']}\n{f.get('patch') or ''}\n" for f in files)
|
|
50
|
+
return PR.from_dict({**d, "diff": diff, "files": [f["filename"] for f in files]})
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def render(new: PR, matches, threshold: float) -> str:
|
|
54
|
+
rows = [m for m in matches if m.score >= threshold]
|
|
55
|
+
lines = [MARKER, f"**Jinx:** this PR may duplicate {len(rows)} open PR(s).", ""]
|
|
56
|
+
for m in rows:
|
|
57
|
+
lines.append(f"- #{m.number} (score {m.score:.2f}): " + "; ".join(m.reasons))
|
|
58
|
+
lines += ["", "_Automated hint, not a decision. A maintainer should confirm before closing anything._"]
|
|
59
|
+
return "\n".join(lines)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def run(event: dict, gh: GitHub, threshold: float | None, max_open: int, top: int, embed: bool = False) -> str | None:
|
|
63
|
+
new = pr_from_api(gh, event["pull_request"])
|
|
64
|
+
open_prs = [
|
|
65
|
+
d
|
|
66
|
+
for d in gh.pages("/pulls?state=open&sort=created&direction=desc", max_open)
|
|
67
|
+
if d["number"] != new.number
|
|
68
|
+
]
|
|
69
|
+
queue = [pr_from_api(gh, d) for d in open_prs]
|
|
70
|
+
embedder = None
|
|
71
|
+
if embed:
|
|
72
|
+
from .embed import Embedder
|
|
73
|
+
|
|
74
|
+
embedder = Embedder()
|
|
75
|
+
det = Detector(embedder=embedder)
|
|
76
|
+
thr = det.threshold if threshold is None else threshold
|
|
77
|
+
matches = det.rank(new, queue, top=top)
|
|
78
|
+
if not any(m.score >= thr for m in matches):
|
|
79
|
+
return None
|
|
80
|
+
body = render(new, matches, thr)
|
|
81
|
+
old = [c for c in gh.pages(f"/issues/{new.number}/comments", 300) if MARKER in (c.get("body") or "")]
|
|
82
|
+
if old:
|
|
83
|
+
gh.call("PATCH", f"/issues/comments/{old[0]['id']}", {"body": body})
|
|
84
|
+
else:
|
|
85
|
+
gh.call("POST", f"/issues/{new.number}/comments", {"body": body})
|
|
86
|
+
return body
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def main() -> int:
|
|
90
|
+
with open(os.environ["GITHUB_EVENT_PATH"]) as fh:
|
|
91
|
+
event = json.load(fh)
|
|
92
|
+
if "pull_request" not in event:
|
|
93
|
+
print("rd0_jinx: not a pull_request event, skipping")
|
|
94
|
+
return 0
|
|
95
|
+
thr = os.environ.get("INPUT_THRESHOLD") or None
|
|
96
|
+
gh = GitHub(os.environ["INPUT_GITHUB_TOKEN"], os.environ["GITHUB_REPOSITORY"])
|
|
97
|
+
body = run(
|
|
98
|
+
event,
|
|
99
|
+
gh,
|
|
100
|
+
float(thr) if thr else None,
|
|
101
|
+
int(os.environ.get("INPUT_MAX_OPEN_PRS", "200")),
|
|
102
|
+
int(os.environ.get("INPUT_TOP", "3")),
|
|
103
|
+
os.environ.get("INPUT_EMBED", "true").lower() == "true",
|
|
104
|
+
)
|
|
105
|
+
print(body or "rd0_jinx: no likely duplicates")
|
|
106
|
+
return 0
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if __name__ == "__main__":
|
|
110
|
+
sys.exit(main())
|
rd0_jinx/agents.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Map a PR author / description to the agent names used in the AIDev dataset."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
_LOGINS = {
|
|
6
|
+
"copilot": "Copilot",
|
|
7
|
+
"copilot-swe-agent[bot]": "Copilot",
|
|
8
|
+
"devin-ai-integration[bot]": "Devin",
|
|
9
|
+
"chatgpt-codex-connector[bot]": "OpenAI_Codex",
|
|
10
|
+
"google-labs-jules[bot]": "Google_Jules",
|
|
11
|
+
"cursor[bot]": "Cursor",
|
|
12
|
+
"cursoragent": "Cursor",
|
|
13
|
+
"claude[bot]": "Claude_Code",
|
|
14
|
+
}
|
|
15
|
+
_SIGNATURES = [
|
|
16
|
+
("generated with [claude code]", "Claude_Code"),
|
|
17
|
+
("claude.ai/code", "Claude_Code"),
|
|
18
|
+
("co-authored-by: claude", "Claude_Code"),
|
|
19
|
+
("chatgpt.com/codex", "OpenAI_Codex"),
|
|
20
|
+
("app.devin.ai", "Devin"),
|
|
21
|
+
("cursor.com/agents", "Cursor"),
|
|
22
|
+
("jules.google", "Google_Jules"),
|
|
23
|
+
("copilot coding agent", "Copilot"),
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def guess_agent(author: str = "", body: str = "") -> str:
|
|
28
|
+
a = (author or "").lower()
|
|
29
|
+
if a in _LOGINS:
|
|
30
|
+
return _LOGINS[a]
|
|
31
|
+
b = (body or "").lower()
|
|
32
|
+
for sig, name in _SIGNATURES:
|
|
33
|
+
if sig in b:
|
|
34
|
+
return name
|
|
35
|
+
return ""
|
rd0_jinx/cli.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
from .pr import PR
|
|
9
|
+
from .scorer import Detector
|
|
10
|
+
from .templates import TemplateModel
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _load(path: str):
|
|
14
|
+
with open(path) as fh:
|
|
15
|
+
raw = fh.read().strip()
|
|
16
|
+
if not raw:
|
|
17
|
+
return []
|
|
18
|
+
if raw[0] in "[{":
|
|
19
|
+
try:
|
|
20
|
+
d = json.loads(raw)
|
|
21
|
+
return d if isinstance(d, list) else [d]
|
|
22
|
+
except json.JSONDecodeError:
|
|
23
|
+
pass
|
|
24
|
+
return [json.loads(x) for x in raw.splitlines() if x.strip()]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _attach_diff(d: dict, diff_path: str | None) -> dict:
|
|
28
|
+
if diff_path:
|
|
29
|
+
with open(diff_path) as fh:
|
|
30
|
+
d = {**d, "diff": fh.read()}
|
|
31
|
+
return d
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def cmd_check(a) -> int:
|
|
35
|
+
t0 = time.perf_counter()
|
|
36
|
+
new = PR.from_dict(_attach_diff(_load(a.new_pr)[0], a.diff))
|
|
37
|
+
queue = [PR.from_dict(d) for d in _load(a.open_queue)]
|
|
38
|
+
embedder = None
|
|
39
|
+
if a.embed:
|
|
40
|
+
from .embed import Embedder
|
|
41
|
+
|
|
42
|
+
embedder = Embedder(a.embed)
|
|
43
|
+
templates = None
|
|
44
|
+
if a.templates:
|
|
45
|
+
with open(a.templates) as fh:
|
|
46
|
+
templates = TemplateModel.from_json(fh.read())
|
|
47
|
+
det = Detector(templates=templates, embedder=embedder)
|
|
48
|
+
thr = det.threshold if a.threshold is None else a.threshold
|
|
49
|
+
matches = det.rank(new, queue, top=a.top)
|
|
50
|
+
ms = (time.perf_counter() - t0) * 1000
|
|
51
|
+
if a.json:
|
|
52
|
+
print(
|
|
53
|
+
json.dumps(
|
|
54
|
+
{
|
|
55
|
+
"pr": new.number,
|
|
56
|
+
"threshold": thr,
|
|
57
|
+
"ms": round(ms, 1),
|
|
58
|
+
"matches": [m.to_dict() | {"flag": m.score >= thr} for m in matches],
|
|
59
|
+
},
|
|
60
|
+
indent=2,
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
return 0
|
|
64
|
+
flagged = [m for m in matches if m.score >= thr]
|
|
65
|
+
print(
|
|
66
|
+
f"PR #{new.number}: checked {len(queue)} open PRs in {ms:.0f} ms. "
|
|
67
|
+
f"{len(flagged)} likely duplicate(s) (score >= {thr:.2f})."
|
|
68
|
+
)
|
|
69
|
+
for m in matches:
|
|
70
|
+
mark = "DUPLICATE?" if m.score >= thr else " "
|
|
71
|
+
print(f" {mark} #{m.number} score {m.score:.2f} {m.title[:70]}")
|
|
72
|
+
for r in m.reasons:
|
|
73
|
+
print(f" - {r}")
|
|
74
|
+
return 0
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def cmd_learn(a) -> int:
|
|
78
|
+
rows = _load(a.corpus)
|
|
79
|
+
tm = TemplateModel.learn(
|
|
80
|
+
((d.get("agent", ""), d.get("repo", ""), d.get("body", "")) for d in rows),
|
|
81
|
+
min_share=a.min_share,
|
|
82
|
+
min_repos=a.min_repos,
|
|
83
|
+
)
|
|
84
|
+
with open(a.output, "w") as fh:
|
|
85
|
+
fh.write(tm.to_json())
|
|
86
|
+
print(f"learned {sum(len(v) for v in tm.lines_by_agent.values())} template lines -> {a.output}")
|
|
87
|
+
return 0
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def main(argv=None) -> int:
|
|
91
|
+
p = argparse.ArgumentParser(prog="jinx", description="Find likely duplicate PRs in an open-PR queue.")
|
|
92
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
93
|
+
c = sub.add_parser("check", help="rank open PRs that may duplicate a new PR")
|
|
94
|
+
c.add_argument("new_pr", help="JSON file with the new PR (GitHub API shape; optional files/diff/agent)")
|
|
95
|
+
c.add_argument("--open-queue", required=True, help="JSON list or JSON-lines file of open PRs")
|
|
96
|
+
c.add_argument("--diff", help="unified diff file for the new PR")
|
|
97
|
+
c.add_argument("--top", type=int, default=5)
|
|
98
|
+
c.add_argument("--threshold", type=float)
|
|
99
|
+
c.add_argument(
|
|
100
|
+
"--embed",
|
|
101
|
+
nargs="?",
|
|
102
|
+
const="sentence-transformers/all-MiniLM-L6-v2",
|
|
103
|
+
help="also use a local embedding model (needs rd0-jinx[embed])",
|
|
104
|
+
)
|
|
105
|
+
c.add_argument("--templates", help="template file from `jinx learn-templates`")
|
|
106
|
+
c.add_argument("--json", action="store_true")
|
|
107
|
+
c.set_defaults(fn=cmd_check)
|
|
108
|
+
lt = sub.add_parser("learn-templates", help="learn agent boilerplate lines from a PR corpus")
|
|
109
|
+
lt.add_argument("corpus", help="JSON-lines with agent, repo, body")
|
|
110
|
+
lt.add_argument("-o", "--output", required=True)
|
|
111
|
+
lt.add_argument("--min-share", type=float, default=0.01)
|
|
112
|
+
lt.add_argument("--min-repos", type=int, default=5)
|
|
113
|
+
lt.set_defaults(fn=cmd_learn)
|
|
114
|
+
a = p.parse_args(argv)
|
|
115
|
+
return a.fn(a)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
if __name__ == "__main__":
|
|
119
|
+
sys.exit(main())
|