Repository navigation
Expand file tree
/
Copy pathdiagnostic.py
More file actions
329 lines (264 loc) · 12.3 KB
/
Copy pathdiagnostic.py
File metadata and controls
329 lines (264 loc) · 12.3 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
"""Diagnose (and often fix) a broken Copilot session — for bug reports.
One run does two jobs and writes two files under ``session/``:
* **Capture** (interactive): opens a browser on the shared profile at
copilot.com; you send ONE message in the Copilot UI. That captures the live
chat protocol to ``session/ws_capture.log`` — what's needed to follow a
protocol change in :mod:`copilot.protocol`.
* **Log** (always): writes a redacted, shareable report to
``session/diagnostic_report.txt`` — environment, the *shape* of your saved
session (never the secrets), a live chat probe, and redacted log tails.
Run::
python tests/diagnostic.py # browser capture + fix + report
python tests/diagnostic.py --report-only # headless/VPS: report only, no browser
SAFE TO SHARE. Secrets never reach either file as plaintext: the report reports
cookie *names* only and the token's *length*, and every captured/log line is run
through a redactor that strips access tokens, JWTs, OAuth codes, and emails —
including inside ``ws_capture.log`` itself. Skim before posting anyway, and
attach ``diagnostic_report.txt`` (not the raw capture).
"""
import argparse
import io
import json
import platform
import re
import sys
import time
import traceback
from pathlib import Path
# Run as a plain script (`python tests/diagnostic.py`): put the project root on
# sys.path so the `copilot` package imports without installing it.
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
SESSION_DIR = Path("session")
TOKEN_FILE = SESSION_DIR / "token.json"
PROFILE_DIR = SESSION_DIR / "profile"
LOGIN_LOG = SESSION_DIR / "login.log"
WS_LOG = SESSION_DIR / "ws_capture.log"
REPORT = SESSION_DIR / "diagnostic_report.txt"
COPILOT_URL = "https://copilot.com/"
CHAT_HINT = "/m365Copilot/Chathub/" # the chat socket; other sockets are telemetry noise
# Packages whose versions matter for reproducing chat/auth issues.
_PACKAGES = ["curl_cffi", "playwright", "fastapi", "uvicorn", "pydantic", "websockets"]
_LOG_TAIL_LINES = 40 # trailing log lines to include (redacted)
_CAPTURE_TICKS = 600 # 600 * 500ms = 5 min ceiling on the browser capture
# --- redaction ------------------------------------------------------------
# Order matters: specific named params first, then a generic long-token catch-all.
_REDACTORS = [
(re.compile(r"(accessToken|access_token)=[^&\s\"]+", re.I), r"\1=<REDACTED>"),
(re.compile(r"(code|client_info|state|nonce|epct|epctrc|reconnectionToken)=[^&\s\"]+", re.I), r"\1=<REDACTED>"),
# JWTs / MSAL artifacts: eyJ... .... (.optional third segment)
(re.compile(r"eyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+(?:\.[A-Za-z0-9_\-]+)?"), "<JWT>"),
(re.compile(r"\bM\.[A-Za-z0-9_\-.!*$%]{20,}"), "<AUTHCODE>"),
(re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+"), "<EMAIL>"),
# Catch-all: any remaining opaque run that looks like a secret.
(re.compile(r"[A-Za-z0-9_\-]{40,}"), "<REDACTED>"),
]
def redact(text: str) -> str:
"""Strip secrets (tokens, JWTs, auth codes, emails) from a line of text."""
for pattern, repl in _REDACTORS:
text = pattern.sub(repl, text)
return text
def _to_text(payload) -> str:
if isinstance(payload, (bytes, bytearray)):
return payload.decode("utf-8", "replace")
return str(payload)
# --- report assembly ------------------------------------------------------
class Report:
"""Accumulates the report and echoes each line to the console as it's built."""
def __init__(self) -> None:
self._buf = io.StringIO()
def line(self, text: str = "") -> None:
print(text)
self._buf.write(text + "\n")
def section(self, title: str) -> None:
self.line()
self.line(f"== {title} " + "=" * max(0, 60 - len(title)))
def text(self) -> str:
return self._buf.getvalue()
def env_section(r: Report) -> None:
r.section("Environment")
r.line(f"python : {platform.python_version()} ({sys.executable})")
r.line(f"platform : {platform.platform()}")
r.line(f"machine : {platform.machine()}")
def packages_section(r: Report) -> None:
r.section("Package versions")
try:
from importlib.metadata import PackageNotFoundError, version
except ImportError: # pragma: no cover - py<3.8
r.line("importlib.metadata unavailable")
return
for name in _PACKAGES:
try:
r.line(f"{name:<12}: {version(name)}")
except PackageNotFoundError:
r.line(f"{name:<12}: NOT INSTALLED")
def playwright_section(r: Report) -> None:
r.section("Playwright browser")
try:
from playwright.sync_api import sync_playwright
with sync_playwright() as pw:
exe = Path(pw.chromium.executable_path)
r.line(f"chromium : {'present' if exe.exists() else 'MISSING'} ({exe})")
except Exception as e: # noqa: BLE001 - report whatever went wrong
r.line(f"could not query Playwright: {type(e).__name__}: {e}")
r.line("hint: run `playwright install chromium`")
def session_section(r: Report) -> None:
r.section("Session state")
r.line(f"profile dir : {'present' if PROFILE_DIR.is_dir() else 'MISSING'} ({PROFILE_DIR})")
if not TOKEN_FILE.exists():
r.line("token.json : MISSING (not signed in yet — run `python -m copilot login`)")
return
import json
try:
data = json.loads(TOKEN_FILE.read_text(encoding="utf-8"))
except (ValueError, OSError) as e:
r.line(f"token.json : UNREADABLE ({type(e).__name__}: {e})")
return
saved_at = data.get("saved_at", 0) or 0
age = time.time() - saved_at if saved_at else None
token = data.get("access_token")
left = (data.get("expires_at") or 0) - time.time()
r.line("token.json : present")
r.line(f" saved_at : {age:.0f}s ago" if age is not None else " saved_at : unknown")
# Report presence + length only — never the token itself.
r.line(f" access_token : {'yes (len ' + str(len(token)) + ')' if token else 'NO'}"
+ (f", expires in {left / 60:.0f} min" if token else ""))
r.line(f" refresh_token : {'yes' if data.get('refresh_token') else 'NO — re-run login'}")
r.line(f" account oid : {'yes' if data.get('oid') else 'NO — re-run login'}")
def browser_capture(r: Report) -> None:
"""Open the real browser, sniff the chat socket (redacted) to ws_capture.log."""
r.section("Live protocol capture (browser)")
try:
from playwright.sync_api import sync_playwright
from copilot.auth import DEFAULT_PROFILE_DIR
except Exception as e: # noqa: BLE001
r.line(f"skipped — Playwright unavailable: {type(e).__name__}: {e}")
return
summary = {"chat_open": False, "challenge": None, "append": False,
"done": False, "frames": 0}
SESSION_DIR.mkdir(parents=True, exist_ok=True)
sink = WS_LOG.open("w", encoding="utf-8")
def write(tag: str, payload) -> str:
raw = _to_text(payload)
if not sink.closed: # frames can arrive after we stop; don't crash
sink.write(redact(f"{tag} {raw}") + "\n")
sink.flush()
return raw
print("\n" + "=" * 70)
print("A browser is opening with your signed-in profile.")
print("Type ONE short message into the Copilot UI and send it.")
print("(If a 'verify you're human' check appears, pass it — that's the fix.)")
print("Frames stream to session/ws_capture.log. CLOSE THE WINDOW when the")
print("reply finishes (or after ~15s if it hangs). Auto-stops after 5 min.")
print("=" * 70 + "\n")
try:
with sync_playwright() as pw:
ctx = pw.chromium.launch_persistent_context(
str(Path(DEFAULT_PROFILE_DIR).resolve()),
headless=False,
args=["--disable-blink-features=AutomationControlled"],
)
page = ctx.pages[0] if ctx.pages else ctx.new_page()
def on_ws(ws) -> None:
is_chat = CHAT_HINT in ws.url
if is_chat:
summary["chat_open"] = True
write("[OPEN]", f"{'CHAT' if is_chat else 'other'} {ws.url}")
def on_recv(*a) -> None:
text = write("[RECV]", a[0] if a else b"")
summary["frames"] += 1
if '"writeAtCursor"' in text:
summary["append"] = True
if re.search(r'"type"\s*:\s*3\b', text):
summary["done"] = True
ws.on("framesent", lambda *a: write("[SENT]", a[0] if a else b""))
ws.on("framereceived", on_recv)
ws.on("close", lambda *a: write("[CLOSE]", ws.url))
page.on("websocket", on_ws)
page.goto(COPILOT_URL, wait_until="domcontentloaded")
ticks = _CAPTURE_TICKS
try:
while not page.is_closed() and ticks > 0:
page.wait_for_timeout(500)
ticks -= 1
except Exception:
pass # window/context torn down by the close
sink.close()
try:
ctx.close()
except Exception:
pass
except Exception: # noqa: BLE001 - capture failures shouldn't kill the report
if not sink.closed:
sink.close()
r.line("capture failed (traceback redacted):")
for tb in redact(traceback.format_exc()).splitlines():
r.line(f" {tb}")
return
# Summarise what we saw — this is the gold for captcha/protocol diagnosis.
r.line(f"ws_capture.log written: {summary['frames']} frames (tokens redacted)")
if summary["chat_open"]:
states = [k for k in ("append", "done") if summary[k]]
r.line(f"chat socket seen (saw: {', '.join(states) or 'connect only'})")
else:
r.line("no chat socket observed — was a message sent in the Copilot window?")
def live_probe_section(r: Report) -> None:
r.section("Live chat probe (HTTP driver)")
r.line("sending one short message (60s budget)...")
started = time.time()
try:
from copilot import CopilotClient
reply = CopilotClient().chat("Reply with exactly one word: pong", timeout=60)
elapsed = time.time() - started
snippet = (reply.text or "").strip().replace("\n", " ")[:80]
r.line(f"RESULT : OK in {elapsed:.1f}s")
r.line(f"reply : {snippet!r}")
r.line(f"conv_id : {'set' if reply.conversation_id else 'none'}")
except Exception: # noqa: BLE001 - the whole point is to capture the failure
elapsed = time.time() - started
r.line(f"RESULT : FAILED after {elapsed:.1f}s")
r.line("traceback (redacted):")
for tb in redact(traceback.format_exc()).splitlines():
r.line(f" {tb}")
def log_tail_section(r: Report, title: str, path: Path) -> None:
r.section(f"{title} (last {_LOG_TAIL_LINES} lines, redacted)")
if not path.exists():
r.line("(not present)")
return
try:
lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
except OSError as e:
r.line(f"(unreadable: {e})")
return
for raw in lines[-_LOG_TAIL_LINES:]:
r.line(redact(raw))
def main() -> None:
parser = argparse.ArgumentParser(description="Diagnose/fix a Copilot session for bug reports.")
parser.add_argument(
"--report-only",
action="store_true",
help="Skip the interactive browser capture (for headless/VPS); just write the report.",
)
args = parser.parse_args()
SESSION_DIR.mkdir(parents=True, exist_ok=True)
r = Report()
r.line("Windows Copilot API — diagnostic report")
r.line("(safe to share: secrets are redacted; skim before posting)")
env_section(r)
packages_section(r)
playwright_section(r)
session_section(r)
if args.report_only:
r.section("Live protocol capture (browser)")
r.line("skipped (--report-only)")
else:
browser_capture(r)
live_probe_section(r)
log_tail_section(r, "login.log", LOGIN_LOG)
log_tail_section(r, "ws_capture.log", WS_LOG)
REPORT.write_text(r.text(), encoding="utf-8")
print("\n" + "=" * 62)
print(f"Report written to {REPORT}")
print("Attach that file to your GitHub issue. Secrets are already redacted,")
print("but give it a skim before posting.")
if __name__ == "__main__":
main()