はじめに 僕のメンターからおすすめされた本の紹介連載、第11回です。
※本記事では、以降 antigravity CLI のコマンド表記を agy と統一して記述します。
今回は以下のテーマで行きます
「そもそも自律ループを回すべきか?」を見極める4つの判断基準について書きます
原文
Four yeses and a loop is probably right. Any no, and reach for a single prompt instead. And there's a tactical follow-up check for the green-lit loops, a thirty-second gut check before each run: does it have a hard stop, a budget or iteration or time ceiling, and is there a human approval gate before anything irreversible, a merge, a deploy, a dependency change? Automate the work; keep a human on the trigger of anything you can't take back. Run it down the list, every time, before you spend a token:
Cook, J. Claude Code Loop Engineering: Build Self-Correcting Agentic Loops That Ship Trusted Code, Not a $200 Overnight Bill (English Edition) (p. 137). (Function). Kindle Edition.
4つの基準すべてが「YES」であれば、ループを回すのがおそらく正解です。1つでも「NO」があるなら、代わりに1回限りの通常のプロンプト(シングルプロンプト)に留めておきましょう。 そして、4つの基準をクリアしてゴーサインが出たループに対しては、実行前に行うべき実戦的な「30秒の直感チェック(最終確認)」があります。それは以下の2点です。 ハードストップ(ブレーキ)が設定されているか?:ターン数、予算、あるいは実行時間の制限(天井)が設けられているか。 取り返しのつかない操作の前に、人間の承認ゲートがあるか?:マージ、デプロイ、依存関係の変更など、後戻りができない処理の直前に人間が介在するようになっているか。 「作業は自動化せよ。ただし、取り返しのつかない操作のトリガーには、常に人間を配置せよ」 トークンを消費し始める前に、毎回必ずこのチェックリストに照らし合わせて確認を行ってください。
ここで日本語訳にある4つの基準は以下の4つ
(紹介している原文のすぐ後ろにあった。)
One(反復性)タスクが定常的に繰り返されるか
Two(自動検証可能性)人間なしで成否を判定できるか
Three(予算の許容性)成果ゼロのコストを許容できるか
Four(十分なツール環境)エージェントがタスクを完遂するためのツール(環境)が揃っているか
何でもかんでもループを組むのは確かにめんどくさい
(大切なことなので2回言いました)
またコストも掛かる
ループって結構準備に時間かかるんですよね(経験則)
色々考えてプロンプト組んでいるのは、前回の投稿でわかると思いますが・・・
で原文から持ってきました
Add input validation to parseConfig() so it rejects a missing "port" key, then run the test suite and show me the result.
Cook, J. Claude Code Loop Engineering: Build Self-Correcting Agentic Loops That Ship Trusted Code, Not a $200 Overnight Bill (English Edition) (p. 134). (Function). Kindle Edition.
でagy向けに書き換え
Add input validation to parseConfig() so it rejects a missing "port" key, then run the test suite and show me the result.
実行結果
ご指定の通り、検証用コードおよびテストコードの作成とバリデーションロジックの追加を行い、テストスイートを実行しました。
書籍に記載のある**「自己チェック付きプロンプト(Self-CheckingPrompt)」**のアプローチに基づき、自律的な反復型ループを構成せずに、一発でテストを実行して結果を確認しています。
──────
1. 作成したファイル
• 設定解析ロジック
(config.js):
port キーが存在しない、またはnullの場合に例外(Error)をスローするように入力検証を追加しました。
function parseConfig(configStr)
{
const config = JSON.
parse(configStr);
if (config.port === undefined
||
config.port === null) {
throw new Error('Missing
"port" key in configuration');
}
return config;
}
module.exports = { parseConfig
};
• テストスイート
(config.test.js):
正常な設定がパースできること、およびportキーが欠けている場合に意図した通りのエラーが発生することを検証するテストです。
const { parseConfig } =
require('./config');
const assert =
require('assert');
function runTests() {
console.log('=== Test Suite:
config.js ===');
let passed = 0;
let total = 0;
// Test 1: Valid
configuration
total++;
try {
const validConfig =
'{"host": "localhost", "port":
8080}';
const result =
parseConfig(validConfig);
assert.strictEqual(result.
port, 8080);
console.log('PASS Test 1:
parseConfig() parses valid config
with port key');
passed++;
} catch (error) {
console.error('FAIL Test
1:', error.message);
}
// Test 2: Missing port key
total++;
try {
const invalidConfig =
'{"host": "localhost"}';
parseConfig(invalidConfig);
console.error('FAIL Test
2: parseConfig() should have
thrown an error for missing port
key');
} catch (error) {
if (error.message.
includes('Missing "port" key')) {
console.log('PASS Test
2: parseConfig() rejects missing
port key with correct error
message');
passed++;
} else {
console.error('FAIL Test
2: parseConfig() threw unexpected
error:', error.message);
}
}
console.
log('\n======================================');
console.log(Results: ${passed} passed, ${total} total);
console.
log('======================================');
if (passed !== total) {
process.exit(1);
}
}
runTests();
──────
2. テストの実行結果
ローカル環境にて node
config.test.js
を実行し、すべてのテストが合格(PASS)したことを確認しました。
=== Test Suite: config.js ===
PASS Test 1: parseConfig()
parses valid config with port key
PASS Test 2: parseConfig()
rejects missing port key with
correct error message
======================================
Results: 2 passed, 2 total
=================================
考察
One(反復性:NO):単発の例外チェック追加であり、何度も繰り返す作業ではない。
Two(自動検証可能性:YES):テストコードで機械的に合格・不合格が判定できる。
Three(予算の許容性:YES):1回実行するだけなので、無限ループで予算を溶かすリスクがない。
Four(十分なツール環境:YES):Node.jsと組み込みの assert だけで完結している。
という感じでした
テストのためのテストと言う感じでしょうか
反復可能性がないならそもそも、ループはいらないでしょう
今回のような単発の修正であれば、シングルプロンプトで十分でした。
「ループを回す技術」だけでなく、「ループを回さない判断」もAIエージェントを使いこなすための重要なスキルなのだと思います。