-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Misc: Add script for patching the query history #20654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,148 @@ | ||||||||||
| #!/usr/bin/env python3 | ||||||||||
| """Duplicate the last CodeQL query history entry, pointing it at a given evaluator log. | ||||||||||
|
|
||||||||||
| Behavior: | ||||||||||
| 1. Locate the most relevant ``workspace-query-history.json`` (supports local & remote VS Code). | ||||||||||
| 2. Duplicate the final object in ``queries``. | ||||||||||
| 3. Generate a fresh random ID and a new timestamp. | ||||||||||
| 4. Set ``jsonEvalLogSummaryLocation`` to the provided summary file path. | ||||||||||
| 5. Set ``initialInfo.userSpecifiedLabel`` to ``Evaluator log at <dir>/<filename>`` (last 2 path parts). | ||||||||||
| 6. Write back atomically. | ||||||||||
|
|
||||||||||
| Usage: python3 misc/scripts/patch_query_history.py /path/to/evaluator-log.summary.jsonl | ||||||||||
| """ | ||||||||||
| from __future__ import annotations | ||||||||||
| import argparse | ||||||||||
| import json, os, random, string, tempfile, sys | ||||||||||
| from pathlib import Path | ||||||||||
| from typing import List | ||||||||||
| from datetime import datetime, timezone | ||||||||||
| import copy | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # Extension folder segment for CodeQL extension query history | ||||||||||
| EXT_SEGMENT = "GitHub.vscode-codeql" | ||||||||||
| HISTORY_FILENAME = "workspace-query-history.json" | ||||||||||
| WORKSPACE_JSON = "workspace.json" | ||||||||||
|
|
||||||||||
| def candidate_user_data_dirs() -> List[Path]: | ||||||||||
| """Return plausible VS Code user data dirs (ordered, deduped).""" | ||||||||||
| home = Path.home() | ||||||||||
| env = os.environ | ||||||||||
| override = env.get("VSCODE_USER_DATA_DIR") | ||||||||||
| bases: List[Path] = [] | ||||||||||
| if override: | ||||||||||
| bases.append(Path(override).expanduser()) | ||||||||||
| if os.name == "nt": | ||||||||||
| appdata = env.get("APPDATA") | ||||||||||
| if appdata: | ||||||||||
| bases.append(Path(appdata) / "Code" / "User") | ||||||||||
| elif sys.platform == "darwin": # macOS inline check | ||||||||||
| bases.append(home / "Library" / "Application Support" / "Code" / "User") | ||||||||||
| else: | ||||||||||
| bases.append(home / ".config" / "Code" / "User") | ||||||||||
| # Remote / server variants | ||||||||||
| bases.extend([ | ||||||||||
| home / ".vscode-remote" / "data" / "User", | ||||||||||
| home / ".vscode-server" / "data" / "User", | ||||||||||
| home / ".vscode" / "data" / "User", | ||||||||||
| ]) | ||||||||||
| seen: set[Path] = set() | ||||||||||
| ordered: List[Path] = [] | ||||||||||
| for b in bases: | ||||||||||
| if b not in seen: | ||||||||||
| seen.add(b) | ||||||||||
| ordered.append(b) | ||||||||||
| return ordered | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def find_history_files() -> List[Path]: | ||||||||||
| """Return all candidate history files sorted by descending modification time. | ||||||||||
| """ | ||||||||||
| candidates: List[Path] = [] | ||||||||||
| for base in candidate_user_data_dirs(): | ||||||||||
| storage_root = base / "workspaceStorage" | ||||||||||
| if not storage_root.is_dir(): | ||||||||||
| continue | ||||||||||
| for ws_entry in storage_root.iterdir(): | ||||||||||
| if not ws_entry.is_dir(): | ||||||||||
| continue | ||||||||||
| history_file = ws_entry / EXT_SEGMENT / HISTORY_FILENAME | ||||||||||
| if history_file.is_file(): | ||||||||||
| candidates.append(history_file) | ||||||||||
| candidates.sort(key=lambda p: p.stat().st_mtime, reverse=True) | ||||||||||
| return candidates | ||||||||||
|
|
||||||||||
| def _generate_new_id() -> str: | ||||||||||
| """Return a new random id (24 chars from allowed set, prefixed with 'evaluator-log-' for stability).""" | ||||||||||
| alphabet = string.ascii_letters + string.digits + "_-" | ||||||||||
| return "evaluator-log-" + "".join(random.choice(alphabet) for _ in range(23)) | ||||||||||
|
|
||||||||||
| def atomic_write_json(target: Path, obj) -> None: | ||||||||||
| fd, tmp = tempfile.mkstemp(dir=str(target.parent), prefix="history.", suffix=".json") | ||||||||||
| try: | ||||||||||
| with os.fdopen(fd, "w", encoding="utf-8") as out: | ||||||||||
| json.dump(obj, out, ensure_ascii=False, indent=2) | ||||||||||
| out.write("\n") | ||||||||||
| os.replace(tmp, target) | ||||||||||
| finally: | ||||||||||
| if os.path.exists(tmp): | ||||||||||
| try: | ||||||||||
| os.remove(tmp) | ||||||||||
| except OSError: | ||||||||||
| pass | ||||||||||
|
|
||||||||||
| def _duplicate_last_entry(path: Path, summary_path: Path) -> dict: | ||||||||||
| try: | ||||||||||
| data = json.loads(path.read_text(encoding="utf-8")) | ||||||||||
| except json.JSONDecodeError as e: | ||||||||||
| raise SystemExit(f"History file JSON is corrupt: {e}") | ||||||||||
| if not isinstance(data, dict) or not isinstance(data.get("queries"), list): | ||||||||||
| raise SystemExit("Unexpected history file structure: missing 'queries' list") | ||||||||||
| queries = data["queries"] | ||||||||||
| if not queries: | ||||||||||
| raise SystemExit("History file contains no queries to duplicate. Please run a query in VSCode and try again.") | ||||||||||
| last = queries[-1] | ||||||||||
| if not isinstance(last, dict): | ||||||||||
| raise SystemExit("Last query entry malformed") | ||||||||||
| payload = copy.deepcopy(last) | ||||||||||
| initial = payload.setdefault("initialInfo", {}) | ||||||||||
| if not isinstance(initial, dict): | ||||||||||
| initial = {} | ||||||||||
| payload["initialInfo"] = initial | ||||||||||
| new_id = _generate_new_id() | ||||||||||
| initial["id"] = new_id | ||||||||||
| initial["start"] = datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z") | ||||||||||
|
||||||||||
| initial["start"] = datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z") | |
| now = datetime.now(timezone.utc) | |
| # Format with milliseconds and 'Z' suffix | |
| initial["start"] = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z" |
Copilot
AI
Oct 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded forward slash separator may not be appropriate for all platforms. Consider using os.path.join() or Path.joinpath() for cross-platform compatibility, or use the path's native separator.
| last_two = "/".join(parts[-2:]) if len(parts) >= 2 else parts[-1] | |
| last_two = os.path.join(*parts[-2:]) if len(parts) >= 2 else parts[-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring states '24 chars from allowed set' but the code generates 23 characters plus the 'evaluator-log-' prefix. The docstring should clarify that it's 23 random characters plus the prefix, or the total length including prefix.