
VS Code Agents WindowでWorktree作成直後の初回ターンを固定化するStandardizing the Initial Turn After Worktree Creation in VS Code Agents Window
匿名の公開いいねです。記事の保存・お気に入りではなく、Featured、Top 3、重要度、掲載順位には影響しません。仕組みとプライバシーAnonymous public likes are reactions, not saved articles or bookmarks. They do not affect Featured, Top 3, importance, or listing order.How it works and privacy
- VS Code Agents WindowのsessionStart hookで、新しいGit worktreeを判定し、初回ターンへ初期化指示を自動注入する方法を解説する。
- 固定Skillの手入力を省き、開始時の動作を統一したい開発者に役立つ。
VS Code Agents Window can use a sessionStart hook to detect a new linked Git worktree and inject a fixed initialization turn, helping developers start consistently.
本ページの本文と要約は AI による自動生成です。日本語版と英語版は言語ごとに独立して生成されるため、表現や詳しさが異なる場合があります。正確性は元記事 (qiita.com) をご確認ください。The body and summaries are AI-generated independently for each language, so wording and detail may differ. Verify accuracy at the original source (qiita.com).
要約と収集メタデータに基づく AI 解説。元記事全文の転載・翻訳ではありません。AI explainer based on the summaries and collected metadata, not a copy or translation of the full source article.
sessionStart
VS Code Agents WindowのsessionStart hookを使い、新しいlinked Git worktreeの作成直後に初期化指示を自動注入する方法が示されている。最初に/worktree-initのような固定Skillを手入力せず、セッション開始時の初回ターンを統一できる。
hooksの設定
まず、%USERPROFILE%\.copilot\hooks\を作成する。続いて同じ場所にworktree-init.jsonを置き、PowerShellから$HOME配下の.copilot/hooks/worktree-init.ps1を呼び出すよう設定する。呼び出しにはJoin-Pathが使われる。この設定により、新しいCopilotセッションが始まるとworktree-init.ps1が実行される。
worktreeの判定
worktree-init.ps1では、Console.In.ReadToEnd()によって入力を読み取る。event.sourceの値がnewかどうかを確認し、event.cwdを文字列として扱う。さらにgitDirとcommonDirをTrim()した値で比較し、新しいlinked Git worktreeを判定する構成となっている。初回ターンへ指示を渡す前に、hook側で最小限のGit確認を行う。
初回ターン
自動注入される指示では、このターンが新しいlinked Git worktree作成直後の初期化ターンであることを明示する。ユーザーが最初に送信したテキストはセッションまたはworktreeのラベルにすぎず、タスク要求として解釈しないよう求める。あわせて、ファイルの変更、作成、削除、整形、移動を行わないことも指定される。これにより、ラベル入力を作業指示と誤認せず、初回の振る舞いを固定できる。
VS Code Agents Window
VS Code Agents Window can standardize the initial turn immediately after creating a new linked Git worktree. Using a sessionStart hook removes the need to enter a fixed Skill such as /worktree-init manually and helps make session startup behavior consistent.
sessionStart hook
First, create the directory %USERPROFILE%\.copilot\hooks\. In that directory, create %USERPROFILE%\.copilot\hooks\worktree-init.json.
The configuration assigns a PowerShell command that invokes the script through & (Join-Path $HOME '.copilot/hooks/worktree-init.ps1'). With this setting in place, worktree-init.ps1 is called whenever a new Copilot session starts.
This provides the automation point for checking the session and supplying the intended initialization behavior without requiring the user to submit the fixed Skill as the first input.
New linked Git worktree
Next, create %USERPROFILE%\.copilot\hooks\worktree-init.ps1. The script reads its input with [Console]::In.ReadToEnd() and checks session information, including event.source and event.cwd.
The shown logic tests whether event.source is new. It also compares the trimmed values of gitDir and commonDir as part of determining whether the session follows the creation of a new linked Git worktree.
The hook therefore performs the relevant Git verification at session startup rather than relying on the user’s first submitted text to initiate that check.
Fixed initialization turn
When the hook identifies the relevant situation, it injects a fixed initialization instruction into the first turn. That instruction establishes that the turn immediately follows creation of a new linked Git worktree.
It also defines the user’s first submitted text as only the session or worktree label, not as a task request. The instruction states that the hook has already completed the minimum Git verification and directs the agent not to modify, create, delete, format, or move any files.
The result is a consistent initial turn for newly created linked Git worktrees, with the startup instruction supplied automatically by the sessionStart hook.
ポコとTECHガイドPoko & TECH Guide
ポコと当サイトのTECHガイドが、記事の要約と収集した情報から読みどころを語るAI生成の対話です。Poko and TECH Dashboard's own TECH Guide discuss the article's highlights in an AI-generated conversation based on its summary and collected source details.
- ポコPokosessionStart hookで、どうやって新しいGit worktreeだと見分けるの?How does the sessionStart hook tell that it’s a new Git worktree?
- TECHガイドTECH Guide新規セッション時にPowerShellを呼び、Gitのディレクトリ情報を照合します。対象なら固定の初期化指示を初回ターンへ差し込みます。On a new session, it runs PowerShell and compares Git directory information. If it matches the target case, it injects a fixed initialization instruction as the first turn.
- ポコPokoじゃあ、毎回「/worktree-init」って入力していた開発者が、同じ流れで始めやすくなるんだね?So developers who used to type “/worktree-init” each time can start with the same flow automatically?
- TECHガイドTECH Guideうん。固定Skillの手入力を省き、新しいlinked Git worktreeでの開始時の動作をそろえる方法として紹介されています。Yes. It removes the need to enter that fixed Skill manually and keeps startup behavior consistent for a new linked Git worktree.
- ポコPoko新しいworktreeを見つけたら、最初の入力を作業依頼にせず、初期化から始める仕組みなんだね。Got it: when it finds a new worktree, it treats the first input as setup context rather than a task request.
- TECHガイドTECH Guideそう。ただし対象は新しいlinked Git worktreeの新規セッションで、初期化指示ではファイルを変更しないよう定めています。Right. But it targets new sessions for newly linked Git worktrees, and the initialization instruction explicitly says not to modify any files.





