我剛剛發現如何利用一些 tmux 的魔法,進一步自動化我的代理工作流程。 現在我正在使用我的 mcp 代理郵件專案,讓一群代理互相交流,討論實施計畫(同時也利用 beads 專案進行任務管理),我仍然需要通過在 codex 中排隊一堆消息來“餵養”這些代理,以保持他們忙碌。 這涉及到逐一通過各個 tmux 窗格(每個 codex 實例一個)並粘貼一些預設消息,或者按幾次上箭頭來重用過去的消息,例如: “選擇下一個你現在可以有效使用的 beads,並立即開始編碼;通過代理郵件告訴其他代理你在做什麼。” 這樣做感覺有點傻且低效,儘管給每個代理足夠的指示讓他們忙碌超過一小時並不需要太長時間。 但現在我意識到,我可以自動在每個相關的 tmux 窗格中排隊一堆消息,只需將這段代碼複製並粘貼到 tmux 會話外的控制台中(這在 zsh 中已測試並正常運行): --- PANES=(${(f)"$(tmux list-panes -a -F '#S:#I.#P' | tail -n +3 | head -n -2)"}) for pane in $PANES; do tmux send-keys -t $pane -l '選擇下一個你現在可以有效使用的 beads,並立即開始編碼;通過代理郵件告訴其他代理你在做什麼。' sleep 0.1 tmux send-keys -t $pane Enter for i in {1..4}; do tmux send-keys -t $pane -l '繼續努力,做有用的工作!並保持溝通!' sleep 0.1 tmux send-keys -t $pane Enter done tmux send-keys -t $pane -l '很好,現在我希望你仔細閱讀你剛剛寫的所有新代碼和你剛剛修改的其他現有代碼,帶著“新鮮的眼光”仔細尋找任何明顯的錯誤、問題、困惑等。' sleep 0.1 tmux send-keys -t $pane Enter tmux send-keys -t $pane -l '務必檢查你的代理郵件,並在需要時及時回覆任何消息;然後仔細按照計畫進行,系統地完成所有剩餘的未完成任務,並在計畫文件中、通過 beads 和代理郵件消息中持續記錄你的進展。不要陷入“溝通煉獄”,讓事情無法進行;主動開始需要完成的任務,但在這樣做時告知你的同事代理,並在計畫文件中記錄下來。當你真的不確定該做什麼時,選擇下一個你可以有效工作的 beads,並開始。' sleep 0.1 tmux send-keys -t $pane Enter tmux send-keys -t $pane -l '好吧,你現在能否將注意力轉向審查你同事代理寫的代碼,檢查任何問題、錯誤、缺陷、效率低下、安全問題、可靠性問題等,並仔細診斷其根本原因,然後在必要時修復或修訂它們?不要僅限於最新的提交,擴大範圍,深入挖掘!' sleep 0.1 tmux send-keys -t $pane Enter done --- 這段腳本: 獲取窗格:查找所有 tmux 窗格,排除前兩個和最後兩個 向每個選定的窗格發送 8 條消息: “選擇下一個 beads...” - 告訴代理開始處理下一個任務 “繼續努力...” × 4 - 重複鼓勵繼續工作 “仔細閱讀...” - 指示進行新代碼審查 “檢查代理郵件...” - 關於協調、避免溝通癱瘓、保持生產力的長消息 “審查同事代理寫的代碼...” - 對錯誤/問題進行同儕代碼審查 每條消息都以字面意義發送(-l 標誌),並在 Enter 之前有 0.1 秒的延遲,以確保 Codex CLI 正確處理它們。
Actually, this is a better way to do it, by filtering on the name of the pane (in my case, it's "node" for the codex panes), and it has the initial 0.1 second sleep, without which it skips the first matching pane without sending the messages correctly: PANES=(${(f)"$(tmux list-panes -a -F '#S:#I.#P #{pane_current_command}' | rg ' node$' | cut -d' ' -f1)"}) for pane in $PANES; do sleep 0.1 # Initial sleep to ensure pane is ready tmux send-keys -t $pane -l 'pick the next bead you can actually do usefully now and start coding on it immediately; communicate what you'"'"'re doing to the other agents via agent mail.' sleep 0.1 tmux send-keys -t $pane Enter for i in {1..4}; do tmux send-keys -t $pane -l 'keep going, doing useful work! and communicate!' sleep 0.1 tmux send-keys -t $pane Enter done tmux send-keys -t $pane -l 'great, now I want you to carefully read over all of the new code you just wrote and other existing code you just modified with "fresh eyes" looking super carefully for any obvious bugs, errors, problems, issues, confusion, etc.' sleep 0.1 tmux send-keys -t $pane Enter tmux send-keys -t $pane -l 'Be sure to check your agent mail and to promptly respond if needed to any messages; thereafter proceed meticulously with the plan, doing all of your remaining unfinished tasks systematically and continuing to notate your progress in-line in the plan document, via beads, and via agent mail messages. Don'"'"'t get stuck in "communication purgatory" where nothing is getting done; be proactive about starting tasks that need to be done, but inform your fellow agents via messages when you do so and notate that in-line in the plan document. When you'"'"'re really not sure what to do, pick the next bead that you can usefully work on and get started.' sleep 0.1 tmux send-keys -t $pane Enter tmux send-keys -t $pane -l 'ok can you now turn your attention to reviewing the code written by your fellow agents and checking for any issues, bugs, errors, problems, inefficiencies, security problems, reliability issues, etc. and carefully diagnose their underlying root causes using first-principle analysis and thereafter fix or revise them if necessary? Dont restrict yourself to the latest commits, cast a wider net and go super deep!' sleep 0.1 tmux send-keys -t $pane Enter done
如果你的機器很慢,你可能需要增加睡眠時間。我討厭這一切是如此挑剔;我打算找到一種更可靠的方法來做到這一點。
25.91K