Repository navigation
Expand file tree
/
Copy pathai_triage.py
More file actions
540 lines (453 loc) · 22.1 KB
/
Copy pathai_triage.py
File metadata and controls
540 lines (453 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
"""
AI-powered issue triage using GitHub Models inference API.
This module replaces the regex/heuristic classifier with an actual LLM call
(GitHub Models — same models Copilot uses) that reads the full issue body
and reasons about what to do.
Required env vars:
GITHUB_TOKEN — already present in every Actions run.
For private repos / org models you may need a token with
the `models:read` scope. In public repos the default
GITHUB_TOKEN works.
Optional env vars:
AI_MODEL — model id to use. Default: "openai/gpt-4o-mini".
AI_DEBUG — if set to "1", prints the full prompt and raw response.
The module exposes two functions:
ai_triage_issue(issue) -> dict
ai_or_fallback(issue) -> dict (falls back to issue_analyzer if AI fails)
The returned dict always has the shape:
{
"classification": "auto_fix" | "needs_human" | "needs_context" | "spam",
"confidence": int (0-100),
"reasoning": str, # why the AI chose this label
"comment": str, # markdown body to post on the issue
"fix_plan": { # populated only when classification == auto_fix
"module_url": str | null,
"current_text": str | null, # short literal phrase to find in the docs
"desired_text": str | null, # short literal phrase to replace it with
"explanation": str | null,
} | null,
"source": "ai" | "fallback",
}
"""
from __future__ import annotations
import json
import os
import sys
import urllib.error
import urllib.request
# GitHub Models inference endpoint (OpenAI-compatible Chat Completions API)
_GITHUB_MODELS_URL = "https://models.github.ai/inference/chat/completions"
_DEFAULT_MODEL = os.environ.get("AI_MODEL", "openai/gpt-4o-mini")
_DEBUG = os.environ.get("AI_DEBUG") == "1"
SYSTEM_PROMPT = """You are an automated issue triage agent for the
MicrosoftDocs/learn-pr GitHub repository. You read user-submitted issues
about Microsoft Learn training modules and decide what action to take.
Your job has THREE parts:
1) IGNORE THE ISSUE TEMPLATE SCAFFOLDING.
Issues are submitted through a GitHub issue form whose body contains a lot
of fixed template text — section headers, checkbox option labels, and
placeholder values. You must mentally strip ALL of this out and only
reason about what the USER actually typed. Treat these as noise:
• "Which of the MS Learn modules from the dropdown are you submitting..."
• "Additional information"
• "Information about the requested update"
• "Fix a broken user experience (broken links, exercise error, etc.)"
• "Update incorrect information"
• "Add new content to the module"
• "Some other request"
• "[REPLACE_WITH_MODULE_TITLE]"
• "No response", standalone "None"
• Any line consisting solely of "- [ ]" or "- [x]" plus one of the option labels above.
2) DECIDE A CLASSIFICATION (exactly one):
• spam — After stripping template noise the user wrote nothing of
substance (empty, gibberish, single word, placeholder
left in, etc.).
• auto_fix — The user clearly identifies a SPECIFIC, LITERAL piece of
text in a SPECIFIC Microsoft Learn page that is wrong,
AND tells you the SHORT, LITERAL replacement text. The
change must be small enough to apply as a plain text
substitution (typo fix, wrong answer marked correct,
swapped term, broken URL, etc.). If the fix requires
rewriting a paragraph, restructuring content, or making
an editorial judgement call, this is NOT auto_fix.
• needs_human — The user reports a real, substantive problem with a
specific module but the fix requires human judgement
(rewrite, restructure, editorial decision, ambiguous
correction).
• needs_context — The user's report is too vague to act on: no module link,
no specific section, or no clear statement of what is
wrong vs. what should be there. A selected module name by
itself, with no requested action, is needs_context rather
than spam or needs_human.
3) PRODUCE A USER-FRIENDLY COMMENT for the issue thread that:
- Tells the user what you classified the issue as and WHY in 1–2 plain
sentences.
- For needs_human: ask precisely for what is missing.
- For needs_context: explain that the issue will be closed for now and can
be reopened when the user provides a URL and specific requested change.
- For auto_fix: confirm you understood the requested change and say a PR
attempt is being made.
- For spam: politely explain the issue appears empty/template-only and ask
them to refile with the form filled out.
OUTPUT FORMAT — STRICT.
Reply with a SINGLE JSON object, no markdown fences, no prose before/after.
Schema:
{
"classification": "spam" | "auto_fix" | "needs_human" | "needs_context",
"confidence": <integer 0-100>,
"reasoning": "<one paragraph explaining your decision>",
"comment": "<markdown body to post on the issue>",
"fix_plan": {
"module_url": "<the MS Learn URL the user cited, or null>",
"current_text": "<short literal phrase in the docs to be replaced, or null>",
"desired_text": "<short literal replacement phrase, or null>",
"explanation": "<one sentence describing the change, or null>"
}
}
Rules:
- "fix_plan" MUST be present in every response.
- Set every field of "fix_plan" to null UNLESS classification == "auto_fix".
- When classification == "auto_fix", "current_text" and "desired_text" must
both be non-null AND must be short literal phrases (typically 1–15 words)
that could be found by exact-string search in the source markdown.
- Never invent a URL the user did not provide.
- Never include the issue template scaffolding text in any field.
"""
def _build_user_message(issue: dict) -> str:
"""Format the issue into the user turn of the chat."""
number = issue.get("number", "?")
title = issue.get("title") or ""
body = issue.get("body") or ""
return (
f"Triage this GitHub issue.\n\n"
f"Issue number: #{number}\n"
f"Issue title:\n{title}\n\n"
f"Issue body (raw, includes template scaffolding — strip it mentally):\n"
f"---BODY START---\n{body}\n---BODY END---\n"
)
def _call_github_models(messages: list[dict], model: str, token: str) -> dict:
"""POST to the GitHub Models chat completions endpoint."""
payload = json.dumps(
{
"model": model,
"messages": messages,
"temperature": 0.1,
"response_format": {"type": "json_object"},
}
).encode("utf-8")
req = urllib.request.Request(
_GITHUB_MODELS_URL,
data=payload,
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"Accept": "application/json",
"X-GitHub-Api-Version": "2022-11-28",
},
method="POST",
)
with urllib.request.urlopen(req, timeout=60) as resp:
return json.loads(resp.read().decode("utf-8"))
def _parse_ai_response(api_response: dict) -> dict:
"""Pull the JSON object out of the chat-completion response."""
try:
content = api_response["choices"][0]["message"]["content"]
except (KeyError, IndexError) as exc:
raise ValueError(f"Unexpected API response shape: {api_response}") from exc
if _DEBUG:
print("─── AI raw content ───")
print(content)
print("──────────────────────")
# Models occasionally wrap JSON in ```json fences despite response_format.
cleaned = content.strip()
if cleaned.startswith("```"):
cleaned = cleaned.strip("`")
if cleaned.lower().startswith("json"):
cleaned = cleaned[4:]
cleaned = cleaned.strip()
return json.loads(cleaned)
_VALID_CLASSIFICATIONS = {"spam", "auto_fix", "needs_human", "needs_context"}
def _normalize(result: dict) -> dict:
"""Make sure the AI's response matches the contract we promise callers."""
classification = result.get("classification")
if classification not in _VALID_CLASSIFICATIONS:
raise ValueError(
f"AI returned invalid classification: {classification!r}. "
f"Expected one of {_VALID_CLASSIFICATIONS}."
)
confidence_raw = result.get("confidence", 50)
try:
confidence = max(0, min(100, int(confidence_raw)))
except (TypeError, ValueError):
confidence = 50
fix_plan = result.get("fix_plan") or {
"module_url": None,
"current_text": None,
"desired_text": None,
"explanation": None,
}
# If the AI said auto_fix but didn't actually give us a swap, downgrade.
if classification == "auto_fix":
if not (fix_plan.get("current_text") and fix_plan.get("desired_text")):
classification = "needs_human"
fix_plan = {
"module_url": fix_plan.get("module_url"),
"current_text": None,
"desired_text": None,
"explanation": (
"AI proposed auto_fix but did not provide both current_text "
"and desired_text; downgraded to needs_human."
),
}
return {
"classification": classification,
"confidence": confidence,
"reasoning": (result.get("reasoning") or "").strip(),
"comment": (result.get("comment") or "").strip(),
"fix_plan": fix_plan if classification == "auto_fix" else None,
"source": "ai",
}
def ai_triage_issue(issue: dict, token: str | None = None, model: str | None = None) -> dict:
"""Run a single issue through GitHub Models and return the structured decision.
Raises on any error — callers that want a fallback should use
``ai_or_fallback`` instead.
"""
token = token or os.environ.get("GITHUB_TOKEN")
if not token:
raise RuntimeError("GITHUB_TOKEN not set — cannot call GitHub Models.")
model = model or _DEFAULT_MODEL
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": _build_user_message(issue)},
]
if _DEBUG:
print("─── AI prompt (user turn) ───")
print(messages[-1]["content"])
print("─────────────────────────────")
api_response = _call_github_models(messages, model=model, token=token)
parsed = _parse_ai_response(api_response)
return _normalize(parsed)
def ai_or_fallback(issue: dict) -> dict:
"""Try the AI; if anything goes wrong, fall back to the heuristic analyzer."""
try:
return ai_triage_issue(issue)
except (urllib.error.URLError, urllib.error.HTTPError, ValueError,
json.JSONDecodeError, RuntimeError, KeyError) as exc:
print(f"⚠️ AI triage failed ({type(exc).__name__}: {exc}); "
f"falling back to heuristic analyzer.")
# Local import keeps this file usable even if issue_analyzer is missing.
from issue_analyzer import intelligent_classify, generate_context_aware_comment
classification, confidence, analysis = intelligent_classify(issue)
analysis = analysis or {}
# The spam fast-path returns an empty analysis dict; build a minimal
# comment ourselves rather than asking the templated comment generator
# (which expects fully populated keys).
if classification == "spam" or not analysis:
comment = (
"🗑️ This issue appears to be empty or to contain only the "
"unfilled MS Learn issue template. If this was a mistake, "
"please reopen with the form filled in."
)
else:
try:
comment = generate_context_aware_comment(analysis, classification)
except (KeyError, TypeError) as comment_exc:
print(f"⚠️ Heuristic comment generation failed ({comment_exc}); "
f"using a generic fallback comment.")
comment = (
f"This issue has been classified as `{classification}` by the "
"heuristic fallback (the AI triage was unavailable). A human "
"reviewer will follow up shortly."
)
return {
"classification": classification,
"confidence": int(confidence) if isinstance(confidence, (int, float)) else 50,
"reasoning": "Heuristic fallback (no AI response).",
"comment": comment,
"fix_plan": None, # heuristic path doesn't propose a structured fix
"source": "fallback",
}
# ───────────────────────────────────────────────────────────────────
# Fix-plan refinement: ground current/desired in the actual file
# ───────────────────────────────────────────────────────────────────
REFINEMENT_SYSTEM_PROMPT = """You are an expert at producing patches for
Microsoft Learn module source files.
A `MicrosoftDocs/learn-pr` module directory looks like:
learn-pr/learn-pr/<topic>/<module-slug>/
├── index.yml ← module metadata + unit list
├── <n>-knowledge-check.yml ← knowledge-check questions/answers (YAML)
├── ...
└── includes/
├── <n>-<unit-name>.md ← unit prose content (Markdown)
└── ...
You will be given:
1. A user-reported issue about something incorrect in a module.
2. The user's high-level "current"/"desired" description (which may be
paraphrased and may NOT match the file verbatim).
3. The file we have determined is the one to edit, including its path
and full contents. Pay attention to the FILE TYPE:
• `.yml` / `.yaml` → structured data; edit fields, not prose.
• `.md` → prose; edit the literal text.
Your job: produce a literal text substitution that, when applied with a
plain Python `str.replace`, fixes the bug described by the user.
Output a SINGLE JSON object — no markdown fences, no prose:
{
"current_text": "<EXACT substring copied verbatim from the file>",
"desired_text": "<the replacement string>",
"explanation": "<one sentence describing the change>"
}
CRITICAL RULES (read carefully):
• "current_text" MUST appear EXACTLY in the file (whitespace,
indentation, punctuation, casing — all matching). If you cannot
find such a substring, return all three fields as null instead of
guessing.
• "current_text" should be the smallest UNIQUE snippet that captures
the bug. Include enough surrounding context (1–3 lines) so the
substring appears only ONCE in the file.
• The substitution must leave the document syntactically valid
(preserve YAML indentation, Markdown link syntax, etc.).
HOW TO HANDLE KNOWLEDGE-CHECK YAML FILES (.yml):
These files contain entries like:
- content: "Project name"
isCorrect: true
explanation: "..."
- content: "Project description"
isCorrect: false
explanation: "..."
To CHANGE WHICH ANSWER IS CORRECT, toggle the `isCorrect:` flags —
do not edit the `content:` strings.
SPECIAL CASE — "NONE OF THE ABOVE":
If the user says something like "none of the options are correct",
"the correct answer is 'none of the above'", "none of them", or similar:
1. FIRST, check if there is ALREADY an answer choice with content
containing "none of the above", "none of them", "none", or similar.
2. If such an option EXISTS: Set that choice's `isCorrect: true` and
ALL others to `isCorrect: false`.
3. If NO "none" option exists: Set ALL existing `isCorrect:` values
to `false`. In your "explanation" field, include exactly this
marker: "[NEEDS_HUMAN: no 'none of the above' option exists]"
IMPORTANT: Your substitution must leave the YAML valid. Normally at
least one answer should be `isCorrect: true`. The ONLY exception is
the "none of the above" case when no such option exists — then the
marker in explanation signals human review is required.
Your `current_text` and `desired_text` should be MULTI-LINE blocks
that include enough YAML to be unambiguous (e.g. the `content:`
line above the `isCorrect:` line, so we know WHICH answer's flag
is being toggled).
HOW TO HANDLE UNIT MARKDOWN FILES (.md):
Edit the literal prose. For typos, broken links, factually-wrong
sentences, etc., make the smallest change that fixes the bug. Do
NOT rewrite whole paragraphs unless the user explicitly says so.
GENERAL:
• Never invent quotes or content that isn't in the file.
• Keep desired_text minimal — change only what is necessary.
• If the file genuinely doesn't contain anything matching the user's
description, return all three fields as null. We'd rather skip a
fix than ship a wrong one.
"""
def refine_fix_plan_with_file_content(
issue: dict,
fix_plan: dict,
file_path: str,
token: str | None = None,
model: str | None = None,
max_file_chars: int = 16000,
) -> dict | None:
"""Ask the LLM to convert a vague current/desired pair into an
exact verbatim substitution rooted in the actual file contents.
Returns the refined fix_plan dict (with the SAME shape as the input
plus a guaranteed-grounded current_text/desired_text), or None when
the model gives up / errors out.
"""
token = token or os.environ.get("GITHUB_TOKEN")
if not token:
return None
try:
with open(file_path, "r", encoding="utf-8") as fh:
file_contents = fh.read()
except OSError as exc:
print(f"⚠️ Could not read {file_path} for refinement: {exc}")
return None
# Keep the prompt under control by truncating very large files. Most
# Learn unit files are small (a few KB); 16 KB is plenty.
truncated = file_contents[:max_file_chars]
truncation_note = (
f"\n\n[file truncated to {max_file_chars} chars of {len(file_contents)}]"
if len(file_contents) > max_file_chars else ""
)
file_ext = os.path.splitext(file_path)[1].lower()
file_kind = {
".yml": "YAML (likely a knowledge-check; edit `isCorrect:` flags, not `content:` strings)",
".yaml": "YAML (likely a knowledge-check; edit `isCorrect:` flags, not `content:` strings)",
".md": "Markdown unit prose (edit the literal text)",
}.get(file_ext, "unknown file type")
user_msg = (
f"Issue title:\n{issue.get('title', '')}\n\n"
f"Issue body:\n{issue.get('body', '')}\n\n"
f"AI's first-pass interpretation of the change:\n"
f" current (paraphrased): {fix_plan.get('current_text')!r}\n"
f" desired (paraphrased): {fix_plan.get('desired_text')!r}\n"
f" explanation: {fix_plan.get('explanation')!r}\n\n"
f"File path: {file_path}\n"
f"File type: {file_kind}\n\n"
f"File contents below — your current_text MUST be a verbatim "
f"substring of this:\n"
f"---FILE START---\n{truncated}{truncation_note}\n---FILE END---"
)
messages = [
{"role": "system", "content": REFINEMENT_SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
]
if _DEBUG:
print("─── Refinement prompt (user turn) ───")
print(user_msg[:1200] + ("...[trimmed]" if len(user_msg) > 1200 else ""))
print("─────────────────────────────────────")
try:
api_response = _call_github_models(messages, model=model or _DEFAULT_MODEL, token=token)
raw_content = api_response["choices"][0]["message"]["content"].strip()
except (urllib.error.URLError, urllib.error.HTTPError, KeyError,
IndexError, ValueError) as exc:
print(f"⚠️ Refinement call failed ({type(exc).__name__}: {exc})")
return None
if _DEBUG:
print("─── Refinement raw content ───")
print(raw_content)
print("──────────────────────────────")
# Strip stray markdown fences if any
cleaned = raw_content
if cleaned.startswith("```"):
cleaned = cleaned.strip("`")
if cleaned.lower().startswith("json"):
cleaned = cleaned[4:]
cleaned = cleaned.strip()
try:
refined = json.loads(cleaned)
except json.JSONDecodeError as exc:
print(f"⚠️ Refinement JSON parse failed: {exc}")
return None
current = refined.get("current_text")
desired = refined.get("desired_text")
explanation = refined.get("explanation") or fix_plan.get("explanation")
if not current or not desired:
print("⚠️ Refinement returned null current/desired — model declined to guess.")
return None
# Hard guard: the model is REQUIRED to ground current in the file.
# If it didn't, the substitution would silently fail.
if current not in file_contents:
print(f"⚠️ Refined current_text not found verbatim in {file_path}; rejecting.")
return None
return {
"module_url": fix_plan.get("module_url"),
"current_text": current,
"desired_text": desired,
"explanation": explanation,
}
# Convenience for ad-hoc debugging: `python3 ai_triage.py <issue.json>`
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 ai_triage.py <issue.json>")
sys.exit(1)
with open(sys.argv[1]) as fh:
issue_data = json.load(fh)
result = ai_or_fallback(issue_data)
print(json.dumps(result, indent=2))