-
-
Save GoXLd/cdbaa5598df152d1ef63feec4c50a717 to your computer and use it in GitHub Desktop.
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # ----------------------------------------------------------------------------- | |
| # Patch Script: OpenAI ChatGPT VS Code extension (macOS Apple Silicon) | |
| # Author: GoXLd (https://github.com/GoXLd) | |
| # Please retain attribution when reusing this code. | |
| # License/Disclaimer: Provided "as is", without any warranties or guarantees | |
| # of any kind (express or implied). Use at your own risk. | |
| # ----------------------------------------------------------------------------- | |
| usage() { | |
| cat <<'EOF' | |
| Usage: patch-openai-chatgpt-extension-arm64.sh [--force] | |
| Options: | |
| --force Force reapply: if file is already patched, try restoring latest backup | |
| and patch again; if no backup exists, still rerun patch pass in place. | |
| -h, --help Show this help message. | |
| EOF | |
| } | |
| force=0 | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| --force) | |
| force=1 | |
| shift | |
| ;; | |
| -h|--help) | |
| usage | |
| exit 0 | |
| ;; | |
| *) | |
| echo "Unknown option: $1" | |
| usage | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| if [[ "$(uname -s)" != "Darwin" ]]; then | |
| echo "This patch script is for macOS only." | |
| exit 1 | |
| fi | |
| if [[ "$(uname -m)" != "arm64" ]]; then | |
| echo "This patch script is for Apple Silicon (arm64) only." | |
| exit 1 | |
| fi | |
| if pgrep -f '(/Applications/Visual Studio Code\.app/|/Applications/Visual Studio Code - Insiders\.app/|(^|[[:space:]])code([[:space:]]|$)|(^|[[:space:]])code-insiders([[:space:]]|$)|/Applications/Antigravity\.app/|(^|[[:space:]])antigravity([[:space:]]|$))' >/dev/null 2>&1; then | |
| echo "VS Code, VS Code Insiders, or Antigravity appears to be running." | |
| echo "Please close VS Code, VS Code Insiders, and Antigravity before patching." | |
| exit 1 | |
| fi | |
| roots=( | |
| "$HOME/.vscode/extensions" | |
| "$HOME/.vscode-insiders/extensions" | |
| "$HOME/.antigravity/extensions" | |
| ) | |
| targets=() | |
| for root in "${roots[@]}"; do | |
| [[ -d "$root" ]] || continue | |
| while IFS= read -r -d '' file; do | |
| targets+=("$file") | |
| done < <(find "$root" -type f -path '*/openai.chatgpt-*-darwin-arm64/out/extension.js' -print0 2>/dev/null) | |
| done | |
| if [[ ${#targets[@]} -eq 0 ]]; then | |
| echo "No extension.js found for openai.chatgpt-*-darwin-arm64." | |
| echo "Checked:" | |
| printf ' - %s\n' "${roots[@]}" | |
| exit 1 | |
| fi | |
| timestamp="$(date +%Y%m%d-%H%M%S)" | |
| patched_count=0 | |
| already_patched_count=0 | |
| force_reapplied_count=0 | |
| failed_count=0 | |
| expected=( | |
| '"local-environment":async()=>({environment:{type:"error",error:{message:"Local environments are not available in the extension."}}})' | |
| '"local-environments":async()=>({environments:[]})' | |
| '"local-environment-config":async({configPath:e})=>({configPath:e,exists:!1,raw:null})' | |
| '"local-environment-config-save":async({configPath:e})=>({configPath:e,success:!1})' | |
| '"open-in-targets":async()=>({preferredTarget:null,availableTargets:[],targets:[]})' | |
| '"set-preferred-app":async()=>({success:!1})' | |
| ) | |
| expected_stable_metadata='async handleResolveStableMetadata(r,{appServerClient:n}){let o={commonDir:r.cwd,root:r.cwd};return ne(o)}' | |
| is_already_patched() { | |
| local target_file="$1" | |
| local pattern | |
| for pattern in "${expected[@]}"; do | |
| if ! grep -Fq "$pattern" "$target_file"; then | |
| return 1 | |
| fi | |
| done | |
| return 0 | |
| } | |
| for file in "${targets[@]}"; do | |
| if [[ $force -eq 1 ]] && is_already_patched "$file"; then | |
| latest_backup="$(ls -1t "${file}.bak."* 2>/dev/null | head -n 1 || true)" | |
| if [[ -n "${latest_backup:-}" && -f "$latest_backup" ]]; then | |
| cp "$latest_backup" "$file" | |
| echo "FORCE: restored latest backup before reapplying: $latest_backup" | |
| else | |
| echo "FORCE: already patched, no previous backup found; rerunning patch in place." | |
| fi | |
| fi | |
| backup="${file}.bak.${timestamp}" | |
| cp "$file" "$backup" | |
| if ! perl -0777 -i -pe ' | |
| s/"local-environment"\s*:\s*async\([^)]*\)\s*=>\s*.*?(?=,\s*"local-environments"\s*:)/"local-environment":async()=>({environment:{type:"error",error:{message:"Local environments are not available in the extension."}}})/sg; | |
| s/"local-environments"\s*:\s*async\([^)]*\)\s*=>\s*.*?(?=,\s*"local-environment-config"\s*:)/"local-environments":async()=>({environments:[]})/sg; | |
| s/"local-environment-config"\s*:\s*async\([^)]*\)\s*=>\s*.*?(?=,\s*"local-environment-config-save"\s*:)/"local-environment-config":async({configPath:e})=>({configPath:e,exists:!1,raw:null})/sg; | |
| s/"local-environment-config-save"\s*:\s*async\([^)]*\)\s*=>\s*.*?(?=,\s*"ide-context"\s*:)/"local-environment-config-save":async({configPath:e})=>({configPath:e,success:!1})/sg; | |
| s/"open-in-targets"\s*:\s*async\([^)]*\)\s*=>\s*.*?(?=,\s*"set-preferred-app"\s*:)/"open-in-targets":async()=>({preferredTarget:null,availableTargets:[],targets:[]})/sg; | |
| s/"set-preferred-app"\s*:\s*async\([^)]*\)\s*=>\s*.*?(?=,\s*"terminal-shell-options"\s*:)/"set-preferred-app":async()=>({success:!1})/sg; | |
| s/async handleResolveStableMetadata\(r,\{appServerClient:n\}\)\{.*?\}(?=async handleUpstreamBranch\()/async handleResolveStableMetadata(r,{appServerClient:n}){let o={commonDir:r.cwd,root:r.cwd};return ne(o)}/sg; | |
| ' "$file"; then | |
| cp "$backup" "$file" | |
| echo "FAILED: $file (patching error, restored from backup)" | |
| failed_count=$((failed_count + 1)) | |
| continue | |
| fi | |
| verify_ok=1 | |
| for pattern in "${expected[@]}"; do | |
| if ! grep -Fq "$pattern" "$file"; then | |
| verify_ok=0 | |
| break | |
| fi | |
| done | |
| if [[ $verify_ok -eq 1 ]] && grep -Fq 'handleResolveStableMetadata' "$file"; then | |
| if ! grep -Fq "$expected_stable_metadata" "$file"; then | |
| verify_ok=0 | |
| fi | |
| fi | |
| if [[ $verify_ok -ne 1 ]]; then | |
| cp "$backup" "$file" | |
| echo "FAILED: $file (restored from backup)" | |
| failed_count=$((failed_count + 1)) | |
| continue | |
| fi | |
| if cmp -s "$backup" "$file"; then | |
| if [[ $force -eq 1 ]]; then | |
| echo "FORCE: patch pass completed (file bytes unchanged): $file" | |
| echo "BACKUP: $backup" | |
| force_reapplied_count=$((force_reapplied_count + 1)) | |
| else | |
| rm -f "$backup" | |
| echo "UNCHANGED (already patched): $file" | |
| already_patched_count=$((already_patched_count + 1)) | |
| fi | |
| else | |
| echo "PATCHED: $file" | |
| echo "BACKUP: $backup" | |
| patched_count=$((patched_count + 1)) | |
| fi | |
| done | |
| echo | |
| echo "Done." | |
| echo "Patched: $patched_count" | |
| echo "Already patched: $already_patched_count" | |
| echo "Force reapplied: $force_reapplied_count" | |
| echo "Failed: $failed_count" | |
| echo | |
| echo "Note: VS Code extension updates may overwrite this patch." | |
| echo "Disclaimer: This script is provided as-is, with no warranties or guarantees." |
I am executing this on my M2 Air since codex has been causing overheating.
Will let you know how it goes
Thanks for trying it on your M2 Air. Yeah, itโs a bit odd that the issue hasnโt been fixed, but people have reported that this patch helps in practice. Looking forward to hearing how it works for you.
this isn't only affecting macbooks, I get massive CPU usage on every platform, even linux, it's just most visible with macbooks air because they overheat because of this, other platforms usually have weaker CPUs with better cooling so it's not so visible, still annoying though
Okay, after some good days of testing, this worked just fine for me. Thanks man, so incase of updates, I should just update and run the script right ?
Okay, after some good days of testing, this worked just fine for me. Thanks man, so incase of updates, I should just update and run the script right ?
Unfortunately, this patch also needs to be updated. But if similar issues come up again, Iโll do my best to help.
this isn't only affecting macbooks, I get massive CPU usage on every platform, even linux, it's just most visible with macbooks air because they overheat because of this, other platforms usually have weaker CPUs with better cooling so it's not so visible, still annoying though
I'm sure Codex team focused on fixing this bug
Thank you @GoXLd for the help, surely, you saved my battery as well.
In case of any changes, we will surely appreciate updates on your end.
Am sure codex team got way too many issues on their hands, this thing might take time to fix.
thank you for solving my problem.
@wwwxxx0501 which mac are are you on
@wwwxxx0501 which mac are are you on
mac air M5
@wwwxxx0501, I noticed the effects are more visible on MacBook Airs because of the lack of fans
@wwwxxx0501, I noticed the effects are more visible on MacBook Airs because of the lack of fans
Me too - on my M4 MacBook Air, I noticed the temperature goes through the roof.
I noticed it seems disables local env related configs. maybe codex's related capability be infected?
@Axfff , i had the same worries at first , but I haven't got any issues with the model performance after applying the patch. It's just so nice really, my CPU usage is at an all time low and long battery life ๐

I am executing this on my M2 Air since codex has been causing overheating.
Will let you know how it goes