0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub CLIでCodespaceを操作し、AIエージェントにLinuxサンドボックスをプレゼントしよう!

0
Last updated at Posted at 2026-06-17

記事は頑張りません。ご了承ください。

GitHubに課金設定がある場合、Codespace利用時の課金に注意してください。
複数エージェントでghコマンドを使いすぎるとGithubからBanされる可能性があるようです。

趣味でEmscriptenを使いたいものの、DockerやWSL2、仮想環境をこのためだけに準備するのは面倒です。
ということで、GitHub CLI + Github Codespaceをお手軽Linux系サンドボックスとして使ってみることにします。

AI側の準備

私の使用エディタは断固としてJetBrains派なので、無料でC++とJavaScriptの編集もできるCLionまたはJavaScript、TypeScriptの編集ができるWebStormにjetbrains-cc-guiを入れて使うといいでしょう。
codexとnodejsの導入についてはここでは省略します
ローカルツールとしては、nodejsやテキスト検索ツールのrgも入れておくと便利です。
さらに、ウェブへの直接デバッグ用にChromeとchrome-devtools-mcpも導入しておくとよいでしょう。

JetBrains側のMCPサーバーも有効化しておくと便利です
image.png

人間側の準備

Emscriptenで何を作りたいか考えておきましょう。

GitHub CLIとCodespaceのセットアップ

GitHub CLIを導入します。

winget install --id GitHub.cli	

アクセストークンを下記の権限で作成し、

image.png
https://github.com/settings/tokens/new

この設定では、少なくとも本記事で利用するリポジトリ操作とCodespace操作は実行できました。
GitHubの権限は、利用前に各権限の説明を確認してください。
下記のようにアクセストークンを渡してログインします。

gh auth login --with-token < GITHUB_TOKEN_FILE_PATH.txt
repository作成(オプション)

まずはcodespace_emptyという名の空の公開リポジトリを作ります

gh repo create codespace_empty --add-readme --public --license MIT
D:\software>gh repo create codespace_empty --add-readme --public --license MIT
✓ Created repository DaisukeDaisuke/codespace_empty on github.com
  https://github.com/DaisukeDaisuke/codespace_empty

D:\software>

次に、そのリポジトリのCodespaceを作成します。

このコマンドを打つと無料枠が消費され、もしcodespaceに課金設定をしている場合、最悪の場合課金されます。
1か月あたり120時間/コア、つまり2コアでは60時間無料です

gh codespace create --idle-timeout "15m" -r DaisukeDaisuke/codespace_empty -b main

image.png

image.png

もしAIにcodespace作成までさせたい場合は、下記のようなコマンドで可能です。

gh codespace create --idle-timeout "15m" -r DaisukeDaisuke/desmume_webassembly -b main --machine basicLinux32gb

Codespaceはここから管理できます。
https://github.com/codespaces

AIには、gh codespace listで Codespace を取得させるようにしましょう。

gh codespace list
D:\software>gh codespace list
NAME                                 DISPLAY NAME         REPOSITORY               BRANCH  STATE      CREATED AT
miniature-adventure-************...  miniature adventure  DaisukeDaisuke/codes...  main    Available  about 1 minute ago

D:\software>

そしたら、IDを使用して任意のコマンドを実行しましょう。
またこのコマンドはコードスペースを起動できるようです。

gh codespace ssh -c miniature-adventure-??????????????????? echo hi

AIに毎回始動と停止を行うように指示すると良いでしょう。

始動 → gh codespace ssh "echo Started"
停止 → gh codespace stop

ファイルのアップロード

gh codespace cp local-file.txt remote:~/path/ -c <name>
gh codespace cp -r remote:/workspaces/repo/dist/ ./dist/ -c <name>

AGENTS.mdへの登録

ここまで設定した内容をAGENTS.mdに書けば、AIがCodespace上で必要なツールを追加できる環境になります。
また、コマンドエスケープには注意させましょう。
aptのログを捨てるのと、-yなどを忘れないことは言っておきましょう。

その後、エージェントでこれらのツールが利用できることをAGENTS.mdで触れましょう。
私の場合:

AGENTS.md

https://gist.github.com/DaisukeDaisuke/88bb1da88fbe020e1f033606096f3849

あとはプロジェクトルールを追加すれば、GitHub Codespaceを使用してあらゆることを実行できます。

# project rule
In GitHub Codespace, you can run emcc and build projects.
It also allows you to set up a server and debug it with Chrome.
emcc provides build scripts and also supports building with GitHub Actions.
You are allowed to create commits, push except with -f, and add submodules.
You can use submodules for old code during deployment.
Regarding `old\desmume`, commits are permitted on the `webassembly` branch. Also, you will be updating the submodules.
You need to keep detailed records of the deployment using GitHub Actions and future AI-related notes.
system.md is always created.
GitHub Codespace is the ultimate sandbox for you, provided you don't make any quotation mistakes.

開発リポジトリで運用する場合

また、開発先にリンクしたCodespaceを使うとリポジトリと同期しながら作業できます

つまづいたこと

gh codespace cpが失敗する

このような問題が発生しました。

PS C:\Users\owner\CLionProjects\deweb> gh codespace cp ./webassembly/wasm-port.cpp remote:/workspaces/desmume_webassembly/webassembly/wasm-port.cpp -c curly-parakeet-
C:\WINDOWS\System32\OpenSSH\scp.exe: dest open "'/workspaces/desmume_webassembly/webassembly/wasm-port.cpp'": No such file or directory
C:\WINDOWS\System32\OpenSSH\scp.exe: failed to upload file ./webassembly/wasm-port.cpp to '/workspaces/desmume_webassembly/webassembly/wasm-port.cpp'
shell closed: exit status 1            gh codespace cp ./webassembly/wasm-port.cpp remote:/workspaces/desmume_webassembly/webassembly/wasm-port.cpp -c curly-parakeet-v6797wvg9vw2wvr9eb> 

どうやら-eオプションを付けないと失敗することがあるようです。

PS C:\Users\owner\CLionProjects\deweb> gh codespace cp ./webassembly/wasm-port.cpp remote:/workspaces/desmume_webassembly/webassembly/wasm-port.cpp -c curly-parakeet-******* -e                                                                                                             
wasm-port.cpp    

github codespaceが壊れることがある

エージェントが作業しているワークスペースが突然壊れることがあるようです。
その場合は、エージェントが壊れたと判断したら再作成するように指示しましょう。

gh codespace list
gh codespace stop -c <name>
gh codespace delete -c <name>
gh codespace create --idle-timeout "15m" -r DaisukeDaisuke/desmume_webassembly -b main --machine basicLinux32gb 
gh codespace list
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?