Skip to content

Instantly share code, notes, and snippets.

@caksoylar
Created September 25, 2026 02:51
Show Gist options
  • Select an option

  • Save caksoylar/6d641c9978052292ec3443e5fb6ddd3a to your computer and use it in GitHub Desktop.

Select an option

Save caksoylar/6d641c9978052292ec3443e5fb6ddd3a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Patch the Windows clipboard command in the current cached Copilot CLI."""
import argparse
import os
from pathlib import Path
import re
import shutil
import subprocess
import sys
import tempfile
OLD = b'`chcp 65001 >nul & "${n}"`'
NEW = b"`chcp 65001 >nul & ${n}`"
BACKUP_SUFFIX = ".before-copy-patch"
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true", help="inspect without changing files")
args = parser.parse_args()
output = subprocess.check_output(["copilot", "--version"], text=True)
match = re.search(
r"^GitHub Copilot CLI ([0-9]+\.[0-9]+\.[0-9]+(?:[-+][A-Za-z0-9.-]+)?)(?=\.?(?:\s|$))",
output,
re.M,
)
if not match:
raise ValueError(f"Could not determine Copilot CLI version from: {output!r}")
version = match.group(1)
root = Path.home() / ".copilot" / "pkg"
bundles = sorted(root.glob(f"*/{version}"))
if not bundles:
raise FileNotFoundError(f"No cached bundle for Copilot CLI {version} under {root}")
changes = []
for bundle in bundles:
for name in ("app.js", "sdk/index.js"):
path = bundle / name
data = path.read_bytes()
old_count, new_count = data.count(OLD), data.count(NEW)
if (old_count, new_count) not in ((1, 0), (0, 1)):
raise ValueError(
f"{path}: expected exactly one original or patched command, "
f"found {old_count} original and {new_count} patched"
)
backup = path.with_name(path.name + BACKUP_SUFFIX)
if old_count == 1 and backup.exists() and backup.read_bytes() != data:
raise FileExistsError(f"Backup differs from current file: {backup}")
changes.append((path, data, old_count == 1))
for path, data, needs_patch in changes:
if not needs_patch:
print(f"Already patched: {path}")
continue
if args.check:
print(f"Needs patch: {path}")
continue
backup = path.with_name(path.name + BACKUP_SUFFIX)
if not backup.exists():
shutil.copy2(path, backup)
fd, temp_name = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
try:
with os.fdopen(fd, "wb") as temp:
temp.write(data.replace(OLD, NEW, 1))
shutil.copymode(path, temp_name)
os.replace(temp_name, path)
finally:
if os.path.exists(temp_name):
os.unlink(temp_name)
print(f"Patched: {path} (backup: {backup})")
if not args.check:
print("Restart Copilot to load the patched bundle.")
if __name__ == "__main__":
try:
main()
except (OSError, subprocess.CalledProcessError, ValueError) as exc:
print(f"Error: {exc}", file=sys.stderr)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment