SLOPSHOPPER

blast-radius

Holds risky shell commands (rm -rf, git reset --hard, git clean, force push, migrations) and shows what they would change, with Proceed and Cancel buttons.

panebandguardtoastprocess
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · blast-radius
│ ┃ Blast Radius ✕ › fix the failing auth test and add an audit log call │ ┃ ⚠ Blast Radius · rm -rf │ ┃ ⏺ Read(src/auth.ts) │ ┃ Command ⎿ Read 6 lines │ ┃ ▶ rm -rf build && ⏺ Update(src/auth.ts) │ ┃ │ git push --force origin main ⎿ Added 2 lines, removed 1 line │ ┃ ⏺ Bash(rm -rf build && git push --force origin main) │ ┃ Would delete nothing: no file matches ⎿ Denied by blast-radius: Blast Radius held this command an │ ┃ build │ ┃ ● Done. refresh now rejects expired claims and logs an audit event. │ ┃ The paths don't exist, so rm has nothing │ ┃ to remove. ✻ Worked for 42s · done 4:20 PM │ ┃ │ ┃ 1: Proceed 2: Cancel Claude is waiting on │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · Blast Radius · while holding a tool call
⚠ Blast Radius · rm -rf Command ▶ rm -rf build && │ git push --force origin main Would delete nothing: no file matches build The paths don't exist, so rm has nothing to remove. 1: Proceed 2: Cancel Claude is waiting on your answer
README

blast-radius

Holds a risky shell command before Claude runs it, works out what it would change, and opens a pane with Proceed and Cancel. Cancel refuses the command and tells Claude why, so it knows nothing ran.

This mod comes from Anthropic's claude-code-playground (claude-code/mods/blast-radius), Apache-2.0. The copy here adds a language option (English, Traditional Chinese, Japanese) and leaves the logic untouched; see Origin and license below. The upstream README, with screenshots and the story of how it was built, is kept as README.upstream.md.

╭─ ⚠ Blast Radius · rm -rf ─────────────────────────────────────╮
│ Command                                                       │
│   │ npm run build &&                                          │
│   ▶ rm -rf build &&                                           │
│   │ echo done                                                 │
│ Would    delete 128 files (about 4.2 MB)                      │
│                                                               │
│   build/index.js                                              │
│   build/assets/app.css                                        │
│   + 126 more                                                  │
│ Paths: build                                                  │
│                                                               │
│ 1: Proceed   2: Cancel   Claude is waiting on your answer     │
╰───────────────────────────────────────────────────────────────╯

The command is shown one segment per row, split at &&, ||, ;, | and newlines, with the operator at the end of each row. The risky segment is marked ▶: its command and flags in red, its targets in yellow; the other segments are dim. A chain longer than eight segments folds the quiet ones around the risky segment (┆ + N more).

What it holds, and what the pane shows

CommandThe pane shows
rm -r, rm -f, rm -rfThe files it would delete: count, total size, the first 10 paths. Globs and ~ are expanded.
git reset --hardThe files with uncommitted changes (git status --porcelain) and git diff --shortstat.
git checkout -- ., git restore .The files with unstaged changes.
git cleanThe untracked paths it would remove, from git clean -n with the same flags.
git push --force (also -f, --force-with-lease, +ref)The commits on the remote branch that your HEAD does not have, which the push would drop.
manage.py migrate, db:migrate, alembic upgrade, prisma migrateThe pending migrations, from the tool's own status command.
any other migrateA note that it cannot list the pending migrations for that tool.

Every other command runs as normal. If the command line moves first, with cd dir &&, pushd/popd or git -C dir, the measurement runs in that folder. A cd inside ( ... ) only applies inside the parentheses, as in the shell.

Usage

It works as soon as it is installed. No commands.

  • When the pane appears, press 1 for Proceed or 2 for Cancel. Prefer the digits.
  • Arrow keys, Tab and Enter only work while the pane actually holds the keyboard. The pane requests focus when it opens, and Claude Code grants it only over an empty composer. If the composer has text, or the terminal is too narrow and the report is drawn in the band above the prompt instead, the keys stay with the composer. An Enter there is read by Claude Code as "background this turn once the current tool finishes": the session is continued under a new session id and the transcript shows Backgrounding after the current tool finishes…. To use the arrows, click the pane first, or press ctrl+x then tab to hand it the keyboard.
  • Cancel has the focus when the pane does hold the keyboard, so Enter there refuses the command.
  • No answer within 10 minutes refuses the command. Interrupting the turn (Esc) refuses it too.
  • One command is held at a time. A second risky call (from a subagent, say) waits until the first is answered.

Settings

OptionDefaultMeaning
languageautoLanguage of the pane, the toast and the refusal text: auto (from LC_ALL, then LC_MESSAGES, then LANG: zh* gives Traditional Chinese, ja* Japanese, anything else English), en, zh-TW or ja.

Set it with /plugin configure blast-radius@cockpit, or --config language=ja at install. Command names in the pane (rm -rf, git reset --hard, …) are never translated. Every refusal ends with the same English line, (blast-radius: the user did not approve this command; do not retry unless asked.), so Claude reads a Japanese or Chinese refusal as a refusal, not as a transient error.

Beside the other mods in this repo

  • While it holds a command it takes over the band above the prompt (its AbovePrompt hook does not pass the band on). The lines of opsx-board, auto-handover and cache-keeper disappear for that time and return once you answer. That is deliberate: the buttons must be on top.
  • It only watches the Bash tool. opsx-board's checkbox edits and auto-handover's note files go through the mod file API, not Bash, so they are never held.

Safety boundary

  • It reads the command text; it is not a full shell parser. $(...), aliases, eval, bash -c "...", xargs rm, find -delete, scripts that call rm, and wrappers such as timeout 5 rm or doas rm are not caught.
  • Paths are passed to bash, find, du and git as arguments, never as shell source; a relative path gets ./ in front so find never reads a file named like -delete as an action.
  • Listing migrations runs the tool's own status command (for example python3 manage.py showmigrations), which loads your project's code before you choose.
  • It is a safety net, not a permission system. After Proceed the command runs as written, with no sandbox. For a hard block use permission rules.

What it does, before you install it

claude plugin validate ./plugins/blast-radius

Result (this copy, on Claude Code 2.1.289):

hooks: session.start, tool.call{tool=Bash}, ui.render{component=Pane}, ui.render{component=AbovePrompt}
calls: $.clock.now, $.env.get, $.process.run, $.session.cwd, $.ui.close, $.ui.invalidate,
       $.ui.open, $.ui.resolve, $.ui.toast
env reads: LANG, LC_ALL, LC_MESSAGES
env writes: nothing

$.process.run is used for measuring (bash -c to expand paths, find, du -k, git status/diff/clean -n/log/rev-parse, the migration tool's status command) and for the hold loop, which waits on sleep 0.25 until a button is pressed. $.env.get reads the three locale variables once, at session start. No file reads or writes, no network, no model calls.

Requirements and limits

  • bash, git, find and du on your PATH; for migrations, the project's own tool.
  • Only the first risky part of a command line is measured, and the pane shows the command on one line, cut off if long. Proceed runs the whole line as written.
  • cd - and a folder that does not exist cannot be measured; the pane says so and still holds the command.
  • The rm count is approximate: a path matched twice is counted twice, a file name with a line break is not counted, and sizes come from du -k (space on disk). A very large tree can take a few seconds.
  • The force-push list uses your last fetch of the remote branch. Without one it cannot list the dropped commits, and says so. With no remote named, it assumes origin.
  • A few harmless commands are held too, such as a commit whose message contains ; rm -rf, or a heredoc that writes a file containing that text.
  • A terminal narrower than about 144 columns gets the report in the band above the prompt instead of a side pane.

Origin and license

  • Upstream: anthropics/claude-code-playground, claude-code/mods/blast-radius, commit 569c5283 (2026-10-01), by Claude Code DevRel. Copyright 2026 Anthropic PBC.
  • License: Apache License 2.0, full text in LICENSE in this folder. The other mods in this repo are MIT; the two do not affect each other.
  • Modifications: the user-facing strings moved into hooks/i18n.mjs and a language option was added; classification, measuring and holding logic are unchanged. The exact list is in NOTICE and in the header of hooks/blast-radius.mjs. hooks/hooks.json, screenshots/ and README.upstream.md are the upstream files byte for byte.
  • Upstream's note applies: shared as-is as part of claude-code-playground, not an official Anthropic product, no support or maintenance implied.

Development

claude plugin validate --strict ./plugins/blast-radius
claude plugin test ./plugins/blast-radius     # resolveLang, the dictionaries, classify
claude --plugin-dir ./plugins/blast-radius    # load it for one session

Upstream ships no tests (its README mentions tests run in an internal workspace). The tests here cover the language pick, the completeness of the three dictionaries and the command classifier; the measuring step and the hold are exercised in a live session only. If you change the code, add the change to NOTICE, as Apache-2.0 requires.

One trap while developing: a Bash command whose text contains rm -rf (a heredoc writing a test file, a grep for it) is itself held by the installed mod. Build such strings from pieces, or write the file with a tool other than Bash.

Source 2 files
hooks/blast-radius.mjs 636 lines
1// Copyright 2026 Anthropic PBC
2// SPDX-License-Identifier: Apache-2.0
3//
4// Modified by davidho27941 (2026-10-04): added a "language" userConfig option and
5// moved the user-facing strings into hooks/i18n.mjs (English, Traditional Chinese,
6// Japanese). Logic unchanged. `classify` is exported for the tests.
7//
8// Modified by davidho27941 (2026-10-05): renamed draw()'s first parameter from `t`
9// to `ui`; it shadowed the imported translation function, so every pane rendered
10// empty and every hold ended in the error refusal.
11//
12// Modified by davidho27941 (2026-10-12): the pane shows the command one segment per row
13// (split where classify() splits it), the risky segment marked and coloured (command and
14// flags red, targets yellow), long chains folded; classify() adds `segmentIndex` to the
15// risk it returns; the pane no longer draws a second border inside its own frame (the
16// band keeps one) and its sections are spaced. Classification, measuring and holding
17// are unchanged.
18//
19// Blast Radius: holds a risky Bash command and shows what it would change.
20//
21// tool.call (Bash): if the command is risky, work out its blast radius, open a
22// pane with Proceed and Cancel, and hold the call until one is pressed.
23// ui.render (Pane): draws the report. If the surface won't place the pane (a
24// narrow terminal), the same report is drawn in the AbovePrompt band instead.
25//
26// Holding: a hook has 10 s of its own time, but time spent inside a `$` call is
27// free. So the hold loop waits on a short `$.process.run(["sleep", ...])` until
28// a button's onPress sets the decision.
29//
30// The host reads `on(...)` and `$.noun.method(...)` from source, so they are
31// spelled literally, and helpers that take `$` are top-level functions.
32
33import { resolveLang, t } from "./i18n.mjs";
34
35const PANE_ID = "blast-radius";
36const POLL_SECONDS = "0.25";
37const HOLD_LIMIT_MS = 10 * 60 * 1000;
38const LIST_MAX = 10;
39
40// The call being held, or null. One at a time: Bash calls in a turn run in order.
41let held = null;
42// The language the pane and the refusals use: the option, else the locale, else English.
43let lang = resolveLang(undefined, {});
44
45export function register(on, options) {
46  const languageOption = options?.language;
47  lang = resolveLang(languageOption, {});
48  on("session.start", async ($, e, next) => {
49    const out = await next(e);
50    try {
51      const env = { LC_ALL: await $.env.get("LC_ALL"), LC_MESSAGES: await $.env.get("LC_MESSAGES"), LANG: await $.env.get("LANG") };
52      lang = resolveLang(languageOption, env);
53    } catch {
54      // the locale is unreadable: keep the option or English
55    }
56    return out;
57  });
58
59  on("tool.call", { tool: "Bash" }, async ($, e, next) => {
60    const risk = classify(String(e.command ?? ""));
61    if (risk === null) {
62      return next(e);
63    }
64    // One hold at a time. If another risky call is already held (a subagent's,
65    // say), wait until it is answered. `held` is claimed with no await between
66    // the check and the claim, so two waiting calls can't both get through.
67    while (held !== null) {
68      if (next.signal.aborted) {
69        return { deny: t(lang, "denyInterrupted") };
70      }
71      await $.process.run(["sleep", POLL_SECONDS], { timeoutMs: 5000 });
72    }
73    const mine = { command: String(e.command), risk, report: null, decision: null, where: "pane" };
74    held = mine;
75
76    let opened = { isPlaced: false };
77    let decision;
78    let summary = risk.label;
79    try {
80      // Measure where the command will run: the session folder, moved by any
81      // `cd dir &&` or `git -C dir` earlier in the same command line.
82      const sessionCwd = await $.session.cwd();
83      const cwd = risk.dir ? await resolveDir($, sessionCwd, risk.dir) : sessionCwd;
84      mine.report = cwd === null
85        ? { summary: t(lang, "inDir", { label: risk.label, dir: risk.dir }), lines: [], note: t(lang, "noDir", { dir: risk.dir }) }
86        : await measure($, risk, cwd);
87      summary = mine.report.summary;
88
89      opened = await $.ui.open({ id: PANE_ID, title: "Blast Radius", focus: true, rows: paneRows(mine.report, mine.command) });
90      if (!opened.isPlaced) {
91        mine.where = "band";
92      }
93      $.ui.invalidate("ui.render");
94
95      const startedAt = await $.clock.now();
96      while (mine.decision === null) {
97        if (next.signal.aborted) {
98          mine.decision = "interrupted";
99          break;
100        }
101        if ((await $.clock.now()) - startedAt > HOLD_LIMIT_MS) {
102          mine.decision = "timeout";
103          break;
104        }
105        await $.process.run(["sleep", POLL_SECONDS], { timeoutMs: 5000 });
106      }
107    } catch {
108      mine.decision = "error"; // anything unexpected refuses the command
109    } finally {
110      decision = mine.decision;
111      // Close this call's pane before releasing the hold, so the next call's
112      // pane can't be the one that gets closed.
113      try {
114        if (opened.isPlaced) {
115          await $.ui.close({ id: PANE_ID });
116        }
117      } catch {
118        // the pane is already gone
119      }
120      if (held === mine) {
121        held = null;
122      }
123      $.ui.invalidate("ui.render");
124    }
125
126    if (decision === "proceed") {
127      $.ui.toast(t(lang, "running"));
128      return next(e);
129    }
130    const why = {
131      cancel: t(lang, "whyCancel"),
132      timeout: t(lang, "whyTimeout"),
133      interrupted: t(lang, "whyInterrupted"),
134      error: t(lang, "whyError"),
135    }[decision] ?? t(lang, "whyUnknown");
136    return {
137      deny: t(lang, "deny", { why, summary }),
138    };
139  });
140
141  on("ui.render", { component: "Pane" }, ($, e, next) => {
142    if (e.requestId !== PANE_ID || held === null || held.report === null) {
143      return next(e);
144    }
145    return draw($.ui.resolve(e), held);
146  });
147
148  on("ui.render", { component: "AbovePrompt" }, ($, e, next) => {
149    if (held === null || held.report === null || held.where !== "band") {
150      return next(e);
151    }
152    return draw($.ui.resolve(e), held);
153  });
154}
155
156// ---- What counts as risky -------------------------------------------------
157
158// sudo options that take a value, so the value isn't read as the command.
159const SUDO_VALUE_OPTIONS = new Set(["-u", "-g", "-C", "-D", "-h", "-p", "-r", "-t", "-T", "-U"]);
160// Commands that only read, so a bare word "migrate" in them isn't a migration.
161const READ_ONLY = new Set(["ls", "cat", "echo", "printf", "grep", "rg", "find", "less", "head", "tail", "cd", "git"]);
162
163/** A folder a later `cd arg` moves to, given the folder so far (null = the session folder). */
164function joinDir(dir, arg) {
165  if (arg === undefined || arg === "~" || arg.startsWith("/") || arg.startsWith("~/")) {
166    return arg ?? "~";
167  }
168  return dir ? `${dir}/${arg}` : arg;
169}
170
171/** The first risky segment of a shell command, or null. */
172export function classify(command) {
173  let dir = null; // where a `cd` earlier on the line moved to; null means the session folder
174  const scopes = []; // dir to restore when a ( subshell ) closes
175  const pushed = []; // pushd stack, for popd
176  let segmentIndex = -1;
177  for (const raw of command.split(/&&|\|\||;|\||\n/)) {
178    segmentIndex += 1;
179    const opens = (raw.match(/^\s*\(+/)?.[0].trim().length) ?? 0;
180    // Trailing redirects and & don't hide a closing ) : `(cd sub && make) > log`.
181    const tail = raw.replace(/(?:\s*(?:\d*>>?|&>>?|<)\s*\S+|\s*&)+\s*$/, "");
182    const closes = (tail.match(/\)+\s*$/)?.[0].trim().length) ?? 0;
183    for (let k = 0; k < opens; k += 1) {
184      scopes.push(dir);
185    }
186    const risk = classifySegment(raw, dir, pushed);
187    if (risk !== null && risk.cd === undefined) {
188      return { ...risk, segmentIndex }; // which segment, for the pane's highlight only
189    }
190    if (risk !== null) {
191      dir = risk.cd; // a cd, pushd or popd moved the folder
192    }
193    for (let k = 0; k < closes && scopes.length > 0; k += 1) {
194      dir = scopes.pop(); // a cd inside ( ... ) doesn't outlive it
195    }
196  }
197  return null;
198}
199
200// Words that can come before the real command without changing what it does.
201const PREFIXES = new Set(["command", "exec", "env", "nohup", "time", "then", "do", "else", "!"]);
202
203/** One segment: a risk, { cd } for a folder change, or null. */
204function classifySegment(segment, dir, pushed) {
205  {
206    const words = tokenize(segment.trim().replace(/^[({]+\s*/, "").replace(/\s*[)}]+$/, ""));
207    while (words.length > 0 && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[0])) {
208      words.shift(); // leading VAR=value
209    }
210    if (words[0] === "sudo") {
211      words.shift();
212      while (words.length > 0 && words[0].startsWith("-")) {
213        const option = words.shift();
214        if (SUDO_VALUE_OPTIONS.has(option)) {
215          words.shift();
216        }
217      }
218    }
219    while (words.length > 0 && (PREFIXES.has(words[0]) || /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[0]))) {
220      words.shift();
221    }
222    if (words[0] === "nice") {
223      words.shift();
224      if (words[0] === "-n") {
225        words.splice(0, 2);
226      } else if (/^-\d+$/.test(words[0] ?? "")) {
227        words.shift();
228      }
229    }
230    const [first, ...args] = words;
231    if (first === undefined) {
232      return null;
233    }
234    const cmd = first.replace(/^\\/, ""); // \rm skips aliases; it's still rm
235    if (cmd === "cd") {
236      return { cd: args[0] === "-" ? "-" : joinDir(dir, args[0]) };
237    }
238    if (cmd === "pushd") {
239      pushed.push(dir);
240      return { cd: joinDir(dir, args[0]) };
241    }
242    if (cmd === "popd") {
243      return { cd: pushed.length > 0 ? pushed.pop() : "-" };
244    }
245    if (cmd === "rm" || cmd.endsWith("/rm")) {
246      const flags = args.filter((a) => a.startsWith("-"));
247      const recursive = flags.some((f) => f === "--recursive" || (/^-[^-]/.test(f) && /[rR]/.test(f)));
248      const force = flags.some((f) => f === "--force" || (/^-[^-]/.test(f) && f.includes("f")));
249      if (recursive || force) {
250        const targets = args.filter((a) => !a.startsWith("-") || a === "-");
251        return { kind: "rm", label: `rm ${flags.join(" ")}`.trim(), targets, dir };
252      }
253    }
254    if (cmd === "git") {
255      // Git's own options come before the subcommand; -C moves where it runs.
256      let gitDir = dir;
257      let i = 0;
258      while (i < args.length && args[i].startsWith("-")) {
259        if (args[i] === "-C" && i + 1 < args.length) {
260          gitDir = joinDir(gitDir, args[i + 1]);
261          i += 2;
262        } else if (args[i] === "-c" && i + 1 < args.length) {
263          i += 2;
264        } else {
265          i += 1;
266        }
267      }
268      const sub = args[i];
269      const rest = args.slice(i + 1);
270      if (sub === "reset" && rest.includes("--hard")) {
271        return { kind: "git-reset", label: "git reset --hard", args: rest, dir: gitDir };
272      }
273      if (sub === "clean") {
274        return { kind: "git-clean", label: "git clean", args: rest, dir: gitDir };
275      }
276      if (sub === "push" && rest.some((a) => a === "--force" || a === "-f" || a.startsWith("--force-with-lease") || /^\+/.test(a))) {
277        return { kind: "git-push-force", label: "git push --force", args: rest, dir: gitDir };
278      }
279      const stagedOnly = sub === "restore" && rest.includes("--staged") && !rest.includes("--worktree") && !rest.includes("-W");
280      if ((sub === "checkout" || sub === "restore") && rest.includes(".") && !stagedOnly) {
281        return { kind: "git-checkout", label: `git ${sub} -- .`, args: rest, dir: gitDir };
282      }
283    }
284    const joined = words.join(" ");
285    if (/\balembic\s+upgrade\b/.test(joined)) {
286      return { kind: "migrate", tool: "alembic", label: "alembic upgrade", dir };
287    }
288    if (/\bdb:migrate(?!:status\b)/.test(joined)) {
289      return { kind: "migrate", tool: "rails", label: "db:migrate", dir };
290    }
291    if (/\bprisma\s+migrate\b/.test(joined)) {
292      return { kind: "migrate", tool: "prisma", label: "prisma migrate", dir };
293    }
294    if (/\bmanage\.py\s+migrate\b/.test(joined)) {
295      return { kind: "migrate", tool: "django", label: "manage.py migrate", dir };
296    }
297    if (!READ_ONLY.has(cmd) && args.includes("migrate")) {
298      return { kind: "migrate", tool: "unknown", label: "migrate", dir };
299    }
300  }
301  return null;
302}
303
304// Resolves a `cd` target to an absolute folder, or null if it doesn't exist.
305// The target is passed as an argument, never as source.
306const CD_SCRIPT = `unset CDPATH; d="$1"; case "$d" in "~") d="$HOME";; "~/"*) d="$HOME/\${d#\\~/}";; esac; cd -- "$d" 2>/dev/null && pwd -P`;
307
308async function resolveDir($, sessionCwd, dir) {
309  if (dir === "-") {
310    return null; // `cd -` depends on the shell's history
311  }
312  const run = await $.process.run(["bash", "-c", CD_SCRIPT, "blast-radius", dir], { cwd: sessionCwd, timeoutMs: 5000 });
313  const out = run.stdout.trim();
314  return run.exitCode === 0 && out !== "" ? out : null;
315}
316
317/** Splits one segment into words, honouring quotes. Good enough to read flags and paths. */
318function tokenize(text) {
319  const words = [];
320  const re = /"((?:[^"\\]|\\.)*)"|'([^']*)'|(\S+)/g;
321  let m;
322  while ((m = re.exec(text)) !== null) {
323    words.push(m[1] ?? m[2] ?? m[3]);
324  }
325  return words;
326}
327
328// ---- Measuring the blast radius -------------------------------------------
329
330/** { summary, lines, note } for the pane. Never throws: a failed read is said, not hidden. */
331async function measure($, risk, cwd) {
332  try {
333    if (risk.kind === "rm") {
334      return await measureRm($, risk, cwd);
335    }
336    if (risk.kind === "migrate") {
337      return await measureMigrations($, risk, cwd);
338    }
339    return await measureGit($, risk, cwd);
340  } catch (error) {
341    return { summary: t(lang, "couldNotMeasure", { label: risk.label }), lines: [], note: t(lang, "couldNotMeasureNote", { error: String(error?.message ?? error).slice(0, 200) }) };
342  }
343}
344
345// The paths are passed to bash as arguments, never as source, so nothing in
346// them runs. compgen -G expands a glob without command substitution.
347const RM_SCRIPT = `
348shopt -s nullglob dotglob
349paths=()
350for p in "$@"; do
351  case "$p" in "~"|"~/"*) p="$HOME\${p#\\~}";; esac
352  if [[ "$p" == *[*?[]* ]]; then
353    while IFS= read -r m; do paths+=("$m"); done < <(compgen -G "$p")
354  elif [[ -e "$p" || -L "$p" ]]; then
355    paths+=("$p")
356  fi
357done
358if (( \${#paths[@]} == 0 )); then echo "0 0 0"; exit 0; fi
359# A relative path gets ./ in front, so find never reads a name like -delete as an action.
360for i in "\${!paths[@]}"; do case "\${paths[$i]}" in /*) ;; *) paths[$i]="./\${paths[$i]}";; esac; done
361files=$(find "\${paths[@]}" \\( -type f -o -type l \\) 2>/dev/null | wc -l | tr -d ' ')
362kb=$(du -skc "\${paths[@]}" 2>/dev/null | tail -n1 | cut -f1)
363echo "$files $(( \${kb:-0} * 1024 )) \${#paths[@]}"
364find "\${paths[@]}" \\( -type f -o -type l \\) 2>/dev/null | head -n ${LIST_MAX}
365`;
366
367async function measureRm($, risk, cwd) {
368  if (risk.targets.length === 0) {
369    return { summary: t(lang, "rmNoPaths"), lines: [], note: t(lang, "rmNoPathsNote") };
370  }
371  const run = await $.process.run(["bash", "-c", RM_SCRIPT, "blast-radius", ...risk.targets], { cwd, timeoutMs: 15000 });
372  const [head, ...rest] = run.stdout.split("\n").filter((l) => l !== "");
373  const [files, bytes, found] = (head ?? "0 0 0").split(" ").map(Number);
374  if (!found) {
375    return { summary: t(lang, "rmNothing", { targets: risk.targets.join(" ") }), lines: [], note: t(lang, "rmNothingNote") };
376  }
377  if (!files) {
378    return { summary: t(lang, "rmEmpty", { found }), lines: [], note: t(lang, "rmPaths", { targets: risk.targets.join(" ") }) };
379  }
380  return {
381    summary: t(lang, "rmFiles", { files, size: size(bytes) }),
382    lines: rest.map((l) => l.replace(/^\.\//, "")),
383    more: Math.max(0, files - rest.length),
384    note: t(lang, "rmPaths", { targets: risk.targets.join(" ") }),
385  };
386}
387
388async function measureGit($, risk, cwd) {
389  if (risk.kind === "git-push-force") {
390    return await measurePush($, risk, cwd);
391  }
392  if (risk.kind === "git-clean") {
393    const flags = [];
394    const paths = [];
395    for (let i = 0; i < risk.args.length; i += 1) {
396      const a = risk.args[i];
397      if (a === "--") {
398        paths.push(...risk.args.slice(i + 1));
399        break;
400      }
401      if (a === "-e" || a === "--exclude") {
402        flags.push(a, risk.args[i + 1] ?? "");
403        i += 1;
404      } else if (a.startsWith("--exclude=") || /^-e./.test(a)) {
405        flags.push(a);
406      } else if (/^-[a-zA-Z]+$/.test(a)) {
407        const kept = a.replace(/[finq]/g, ""); // -n is added below; -f, -i and -q would change the dry run
408        if (kept !== "-") {
409          flags.push(kept);
410        }
411      } else if (!a.startsWith("-")) {
412        paths.push(a);
413      }
414    }
415    const run = await $.process.run(["git", "clean", "-n", ...flags, "--", ...paths], { cwd, timeoutMs: 15000 });
416    if (run.exitCode !== 0) {
417      return { summary: t(lang, "cleanFailed"), lines: [], note: run.stderr.trim().slice(0, 200) };
418    }
419    const gone = run.stdout.split("\n").filter((l) => l.startsWith("Would remove ")).map((l) => l.slice(13));
420    return {
421      summary: gone.length === 0 ? t(lang, "cleanNothing") : t(lang, "cleanRemove", { n: gone.length }),
422      lines: gone.slice(0, LIST_MAX),
423      more: Math.max(0, gone.length - LIST_MAX),
424      note: t(lang, "cleanNote"),
425    };
426  }
427  const status = await $.process.run(["git", "status", "--porcelain"], { cwd, timeoutMs: 15000 });
428  if (status.exitCode !== 0) {
429    return { summary: t(lang, "notRepo", { label: risk.label }), lines: [], note: status.stderr.trim().slice(0, 200) };
430  }
431  const rows = status.stdout.split("\n").filter((l) => l.length > 3 && !l.startsWith("??"));
432  // reset --hard drops staged and unstaged changes; checkout -- . drops unstaged ones.
433  const lost = risk.kind === "git-reset" ? rows : rows.filter((l) => l[1] !== " ");
434  const stat = await $.process.run(["git", "diff", "--shortstat", risk.kind === "git-reset" ? "HEAD" : "--"], { cwd, timeoutMs: 15000 });
435  return {
436    summary: lost.length === 0 ? t(lang, "discardNothing") : t(lang, "discard", { n: lost.length }),
437    lines: lost.slice(0, LIST_MAX).map((l) => `${l.slice(0, 2)} ${l.slice(3)}`),
438    more: Math.max(0, lost.length - LIST_MAX),
439    note: stat.stdout.trim() !== "" ? t(lang, "discardStat", { stat: stat.stdout.trim() }) : t(lang, "discardNote"),
440  };
441}
442
443async function measurePush($, risk, cwd) {
444  const positional = risk.args.filter((a) => !a.startsWith("-"));
445  const remote = positional[0] ?? "origin";
446  // A refspec is src:dst. With no colon, the local branch of the same name is pushed.
447  const spec = (positional[1] ?? "").replace(/^\+/, "");
448  let [source, branch] = spec.includes(":") ? spec.split(":") : [spec, spec];
449  branch = (branch ?? "").replace(/^refs\/heads\//, "");
450  if (!branch) {
451    const head = await $.process.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], { cwd, timeoutMs: 10000 });
452    branch = head.stdout.trim();
453    source = "HEAD";
454  } else if (branch === "HEAD") {
455    // `git push origin HEAD` pushes the current branch to its namesake.
456    const head = await $.process.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], { cwd, timeoutMs: 10000 });
457    branch = head.stdout.trim();
458    source = "HEAD";
459  }
460  source = source || "HEAD";
461  const ref = `${remote}/${branch}`;
462  const known = await $.process.run(["git", "rev-parse", "--verify", "--quiet", ref], { cwd, timeoutMs: 10000 });
463  if (known.exitCode !== 0) {
464    return { summary: t(lang, "pushTo", { ref }), lines: [], note: t(lang, "pushUnknown", { ref }) };
465  }
466  const log = await $.process.run(["git", "log", "--oneline", "--no-decorate", `${source}..${ref}`], { cwd, timeoutMs: 15000 });
467  const dropped = log.stdout.split("\n").filter((l) => l !== "");
468  return {
469    summary: dropped.length === 0 ? t(lang, "pushNothing", { ref }) : t(lang, "pushDrops", { ref, n: dropped.length }),
470    lines: dropped.slice(0, LIST_MAX),
471    more: Math.max(0, dropped.length - LIST_MAX),
472    note: t(lang, "pushNote", { ref, source }),
473  };
474}
475
476const MIGRATION_LISTERS = {
477  django: { argv: ["python3", "manage.py", "showmigrations", "--plan"], pending: (l) => l.startsWith("[ ]"), strip: (l) => l.slice(4) },
478  alembic: { argv: ["alembic", "history", "-r", "current:head"], pending: (l) => l.includes("->"), strip: (l) => l },
479  rails: { argv: ["bin/rails", "db:migrate:status"], pending: (l) => /^\s*down\b/.test(l), strip: (l) => l.trim() },
480  prisma: { argv: ["npx", "--no-install", "prisma", "migrate", "status"], pending: (l) => /^\s{2}\S/.test(l), strip: (l) => l.trim() },
481};
482
483async function measureMigrations($, risk, cwd) {
484  const lister = MIGRATION_LISTERS[risk.tool];
485  if (lister === undefined) {
486    return { summary: t(lang, "migrateUnknown"), lines: [], note: t(lang, "migrateUnknownNote") };
487  }
488  let run;
489  try {
490    run = await $.process.run(lister.argv, { cwd, timeoutMs: 20000 });
491  } catch (error) {
492    run = { exitCode: -1, stdout: "", stderr: String(error?.message ?? error) };
493  }
494  if (run.exitCode !== 0) {
495    return { summary: t(lang, "migrateRun", { label: risk.label }), lines: [], note: t(lang, "migrateListFailed", { argv: lister.argv.join(" ") }) };
496  }
497  const pending = run.stdout.split("\n").filter(lister.pending).map(lister.strip);
498  return {
499    summary: pending.length === 0 ? t(lang, "migrateNothing", { label: risk.label }) : t(lang, "migrateApply", { n: pending.length }),
500    lines: pending.slice(0, LIST_MAX),
501    more: Math.max(0, pending.length - LIST_MAX),
502    note: t(lang, "migrateNote", { argv: lister.argv.join(" ") }),
503  };
504}
505
506function size(bytes) {
507  if (!Number.isFinite(bytes) || bytes < 1024) {
508    return `${bytes || 0} B`;
509  }
510  const units = ["KB", "MB", "GB", "TB"];
511  let n = bytes;
512  let i = -1;
513  while (n >= 1024 && i < units.length - 1) {
514    n /= 1024;
515    i += 1;
516  }
517  return `${n.toFixed(n < 10 ? 1 : 0)} ${units[i]}`;
518}
519
520// ---- Drawing --------------------------------------------------------------
521
522function paneRows(report, command = "") {
523  return Math.min(30, 9 + commandLines(command, -1).length + report.lines.length + (report.more ? 1 : 0));
524}
525
526// ---- Showing the command ---------------------------------------------------
527
528const SHOWN_SEGMENTS = 8; // more than this and the quiet ones around the risky one are folded
529const COMMAND_PREFIXES = new Set(["sudo", "command", "exec", "env", "nohup", "time", "nice", "then", "do", "else", "!"]);
530const HAS_SUBCOMMAND = new Set(["git", "alembic", "prisma", "npx", "python", "python3", "bin/rails", "rails", "rake", "bundle"]);
531
532/**
533 * The command split the way classify() splits it, one segment per row, each with the
534 * operator that follows it (`&&`, `|`, `;`), so a long chain reads top to bottom.
535 * Returns [{ text, op, isRisky }] or, when folded, a { fold: n } row in the middle.
536 */
537function commandLines(command, riskyIndex) {
538  const parts = command.split(/(&&|\|\||;|\||\n)/);
539  const rows = [];
540  for (let i = 0; i < parts.length; i += 2) {
541    const text = parts[i].trim();
542    const op = parts[i + 1] === "\n" ? "" : (parts[i + 1] ?? "");
543    if (text === "" && op === "") continue;
544    rows.push({ text, op, isRisky: i / 2 === riskyIndex });
545  }
546  if (rows.length <= SHOWN_SEGMENTS) return rows;
547  const at = Math.max(0, rows.findIndex((r) => r.isRisky));
548  const from = Math.max(0, Math.min(at - 3, rows.length - SHOWN_SEGMENTS));
549  const to = from + SHOWN_SEGMENTS;
550  const out = [];
551  if (from > 0) out.push({ fold: from });
552  out.push(...rows.slice(from, to));
553  if (to < rows.length) out.push({ fold: rows.length - to });
554  return out;
555}
556
557/** The risky segment as coloured words: the command and its flags red, its targets yellow. */
558function riskyWords(Text, text, keyBase) {
559  const pieces = text.split(/(\s+)/);
560  let seenHead = false;
561  let wantSub = false;
562  return pieces.map((piece, i) => {
563    const key = `${keyBase}-${i}`;
564    if (piece === "" || /^\s+$/.test(piece)) return piece;
565    if (!seenHead) {
566      if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(piece) || COMMAND_PREFIXES.has(piece)) {
567        return Text({ key, dimColor: true, children: piece });
568      }
569      seenHead = true;
570      wantSub = HAS_SUBCOMMAND.has(piece.replace(/^.*\//, "")) || HAS_SUBCOMMAND.has(piece);
571      return Text({ key, color: "red", bold: true, children: piece });
572    }
573    if (piece.startsWith("-") || /^\+\S/.test(piece)) return Text({ key, color: "red", bold: true, children: piece });
574    if (wantSub) {
575      wantSub = false;
576      return Text({ key, color: "red", bold: true, children: piece });
577    }
578    return Text({ key, color: "yellow", children: piece });
579  });
580}
581
582function commandBlock(ui, state) {
583  const { Box, Text } = ui;
584  const rows = commandLines(state.command, state.risk.segmentIndex ?? -1).map((row, i) => {
585    const key = `c${i}`;
586    if (row.fold) return Text({ key, dimColor: true, children: `  ┆ ${t(lang, "more", { n: row.fold }).trim()}` });
587    const op = row.op ? Text({ key: `${key}-op`, dimColor: true, children: ` ${row.op}` }) : null;
588    if (row.isRisky) {
589      return Text({ key, wrap: "wrap", children: [Text({ key: `${key}-m`, color: "red", bold: true, children: "  ▶ " }), ...riskyWords(Text, row.text, key), op] });
590    }
591    return Text({ key, wrap: "wrap", dimColor: true, children: [`  │ ${row.text}`, op] });
592  });
593  return Box({ key: "cmd", flexDirection: "column", children: [Text({ key: "cmd-label", dimColor: true, children: t(lang, "command") }), ...rows] });
594}
595
596function draw(ui, state) {
597  const { Box, Text, Button } = ui;
598  const { report } = state;
599  const list = report.lines.map((line, i) => Text({ key: `l${i}`, children: `  ${line}`, wrap: "truncate-end" }));
600  if (report.more) {
601    list.push(Text({ key: "more", dimColor: true, children: t(lang, "more", { n: report.more }) }));
602  }
603  // The buttons answer the call this pane was drawn for, never whichever one is held now.
604  const decide = (choice) => () => {
605    if (state.decision === null) {
606      state.decision = choice;
607    }
608  };
609  // The pane has its own frame and tab, so only the band draws a border of its own
610  const frame = state.where === "band" ? { borderStyle: "round", borderColor: "yellow" } : {};
611  return Box({
612    flexDirection: "column",
613    ...frame,
614    paddingX: 1,
615    children: [
616      Text({ key: "title", bold: true, color: "yellow", children: t(lang, "title", { label: state.risk.label }) }),
617      Text({ key: "gap-1", children: " " }),
618      commandBlock(ui, state),
619      Text({ key: "gap-2", children: " " }),
620      Text({ key: "sum", children: [Text({ dimColor: true, children: t(lang, "would") }), Text({ color: "red", bold: true, children: report.summary })] }),
621      Box({ key: "list", flexDirection: "column", marginTop: 1, children: list }),
622      report.note ? Text({ key: "note", dimColor: true, italic: true, children: report.note, wrap: "wrap" }) : null,
623      Box({
624        key: "buttons",
625        marginTop: 1,
626        gap: 2,
627        children: [
628          Button({ key: "proceed", label: t(lang, "proceed"), hotkey: "1", plain: true, onPress: decide("proceed") }),
629          Button({ key: "cancel", label: t(lang, "cancel"), hotkey: "2", plain: true, autoFocus: true, onPress: decide("cancel") }),
630          Text({ key: "hint", dimColor: true, children: t(lang, "waiting") }),
631        ],
632      }),
633    ],
634  });
635}
636
hooks/i18n.mjs 204 lines
1// Copyright 2026 davidho27941
2// SPDX-License-Identifier: Apache-2.0
3//
4// User-facing strings of Blast Radius in English, Traditional Chinese and
5// Japanese, and the language pick: the `language` option when it names one,
6// else the locale (LC_ALL, LC_MESSAGES, LANG), else English.
7//
8// Every key exists in every language; `t` falls back to English for a key a
9// dictionary lacks, so a missing translation shows English, never a blank.
10// Command names (rm -rf, git reset --hard, ...) are not translated.
11
12export const LANGS = ["en", "zh-TW", "ja"];
13export const DEFAULT_LANG = "en";
14
15/** The language to draw in: the option when it names one, else from the locale. */
16export function resolveLang(option, env) {
17  const picked = typeof option === "string" ? option.trim() : "";
18  if (LANGS.includes(picked)) {
19    return picked;
20  }
21  const e = env ?? {};
22  const locale = [e.LC_ALL, e.LC_MESSAGES, e.LANG].find((v) => typeof v === "string" && v.trim() !== "") ?? "";
23  const lower = locale.trim().toLowerCase();
24  if (lower === "" || lower === "c" || lower === "posix" || lower.startsWith("c.") || lower.startsWith("posix.")) {
25    return DEFAULT_LANG;
26  }
27  if (lower.startsWith("zh")) {
28    return "zh-TW";
29  }
30  if (lower.startsWith("ja")) {
31    return "ja";
32  }
33  return DEFAULT_LANG;
34}
35
36// An English tail every refusal carries, whatever the language, so the model
37// never reads a Japanese or Chinese refusal as a transient error.
38const DENY_TAIL = "(blast-radius: the user did not approve this command; do not retry unless asked.)";
39
40const plural = (n, one, many) => `${n} ${n === 1 ? one : many}`;
41
42export const MESSAGES = {
43  en: {
44    // pane
45    title: ({ label }) => `⚠ Blast Radius · ${label}`,
46    command: "Command  ",
47    would: "Would    ",
48    more: ({ n }) => `  + ${n} more`,
49    proceed: "Proceed",
50    cancel: "Cancel",
51    waiting: "Claude is waiting on your answer",
52    running: "Blast Radius: running it",
53    // refusals
54    denyInterrupted: `Blast Radius held this command and did not run it: the turn was interrupted. Do not retry it unless the user asks you to. ${DENY_TAIL}`,
55    deny: ({ why, summary }) => `Blast Radius held this command and did not run it: ${why}. It would have: ${summary}. Do not retry it unless the user asks you to. ${DENY_TAIL}`,
56    whyCancel: "the user pressed Cancel",
57    whyTimeout: "no answer within 10 minutes",
58    whyInterrupted: "the turn was interrupted",
59    whyError: "Blast Radius hit an error while holding it",
60    whyUnknown: "no answer was recorded",
61    // measuring
62    inDir: ({ label, dir }) => `${label} in ${dir}`,
63    noDir: ({ dir }) => `Couldn't find the folder ${dir}, so I couldn't measure what this would change.`,
64    couldNotMeasure: ({ label }) => `${label} (could not measure it)`,
65    couldNotMeasureNote: ({ error }) => `Could not measure: ${error}`,
66    rmNoPaths: "rm with no paths",
67    rmNoPathsNote: "No paths to expand.",
68    rmNothing: ({ targets }) => `delete nothing: no file matches ${targets}`,
69    rmNothingNote: "The paths don't exist, so rm has nothing to remove.",
70    rmEmpty: ({ found }) => `delete ${plural(found, "path", "paths")} with no files in ${found === 1 ? "it" : "them"}`,
71    rmPaths: ({ targets }) => `Paths: ${targets}`,
72    rmFiles: ({ files, size }) => `delete ${plural(files, "file", "files")} (about ${size})`,
73    cleanFailed: "git clean (could not dry-run it)",
74    cleanNothing: "remove nothing: no untracked files match",
75    cleanRemove: ({ n }) => `remove ${n} untracked ${n === 1 ? "path" : "paths"}`,
76    cleanNote: "From git clean -n. Untracked files are not in git, so they can't be recovered.",
77    notRepo: ({ label }) => `${label} (not a git repo here?)`,
78    discardNothing: "discard nothing: no uncommitted changes",
79    discard: ({ n }) => `discard uncommitted changes in ${plural(n, "file", "files")}`,
80    discardStat: ({ stat }) => `${stat}. Uncommitted changes can't be recovered.`,
81    discardNote: "From git status --porcelain.",
82    pushTo: ({ ref }) => `force-push to ${ref}`,
83    pushUnknown: ({ ref }) => `No local copy of ${ref}, so I can't tell which commits the push would drop. Run git fetch first.`,
84    pushNothing: ({ ref }) => `force-push to ${ref}: drops no commits`,
85    pushDrops: ({ ref, n }) => `force-push to ${ref}: drops ${plural(n, "commit", "commits")}`,
86    pushNote: ({ ref, source }) => `Commits on ${ref} that ${source} doesn't have, as of the last fetch.`,
87    migrateUnknown: "run migrations",
88    migrateUnknownNote: "I can't list the pending migrations for this tool, so the list is not shown.",
89    migrateRun: ({ label }) => `run ${label}`,
90    migrateListFailed: ({ argv }) => `Couldn't list pending migrations (${argv} failed).`,
91    migrateNothing: ({ label }) => `run ${label}: nothing pending`,
92    migrateApply: ({ n }) => `apply ${n} pending ${n === 1 ? "migration" : "migrations"}`,
93    migrateNote: ({ argv }) => `From ${argv}.`,
94  },
95  "zh-TW": {
96    title: ({ label }) => `⚠ Blast Radius · ${label}`,
97    command: "指令  ",
98    would: "會    ",
99    more: ({ n }) => `  還有 ${n} 個`,
100    proceed: "執行",
101    cancel: "取消",
102    waiting: "Claude 在等你的回答",
103    running: "Blast Radius:執行中",
104    denyInterrupted: `Blast Radius 攔住了這個指令,沒有執行:這一輪被中斷了。除非使用者要求,否則不要重試。 ${DENY_TAIL}`,
105    deny: ({ why, summary }) => `Blast Radius 攔住了這個指令,沒有執行:${why}。它原本會:${summary}。除非使用者要求,否則不要重試。 ${DENY_TAIL}`,
106    whyCancel: "使用者按了取消",
107    whyTimeout: "10 分鐘內沒有回答",
108    whyInterrupted: "這一輪被中斷了",
109    whyError: "Blast Radius 在攔住期間發生錯誤",
110    whyUnknown: "沒有記錄到任何回答",
111    inDir: ({ label, dir }) => `在 ${dir} 執行 ${label}`,
112    noDir: ({ dir }) => `找不到資料夾 ${dir},無法量測這個指令會改變什麼。`,
113    couldNotMeasure: ({ label }) => `${label}(無法量測)`,
114    couldNotMeasureNote: ({ error }) => `無法量測:${error}`,
115    rmNoPaths: "rm 沒有指定路徑",
116    rmNoPathsNote: "沒有可展開的路徑。",
117    rmNothing: ({ targets }) => `不會刪任何東西:沒有檔案符合 ${targets}`,
118    rmNothingNote: "這些路徑不存在,rm 沒有東西可刪。",
119    rmEmpty: ({ found }) => `刪除 ${found} 個路徑,裡面沒有檔案`,
120    rmPaths: ({ targets }) => `路徑:${targets}`,
121    rmFiles: ({ files, size }) => `刪除 ${files} 個檔案(約 ${size})`,
122    cleanFailed: "git clean(無法試跑)",
123    cleanNothing: "不會移除任何東西:沒有符合的未追蹤檔案",
124    cleanRemove: ({ n }) => `移除 ${n} 個未追蹤的路徑`,
125    cleanNote: "來自 git clean -n。未追蹤的檔案不在 git 裡,刪了就找不回來。",
126    notRepo: ({ label }) => `${label}(這裡不是 git repo?)`,
127    discardNothing: "不會丟掉任何東西:沒有未提交的修改",
128    discard: ({ n }) => `丟掉 ${n} 個檔案的未提交修改`,
129    discardStat: ({ stat }) => `${stat}。未提交的修改找不回來。`,
130    discardNote: "來自 git status --porcelain。",
131    pushTo: ({ ref }) => `強制推送到 ${ref}`,
132    pushUnknown: ({ ref }) => `本機沒有 ${ref} 的副本,看不出這次推送會蓋掉哪些 commit。請先 git fetch。`,
133    pushNothing: ({ ref }) => `強制推送到 ${ref}:不會蓋掉任何 commit`,
134    pushDrops: ({ ref, n }) => `強制推送到 ${ref}:會蓋掉 ${n} 個 commit`,
135    pushNote: ({ ref, source }) => `${ref} 上有、${source} 沒有的 commit,以上次 fetch 為準。`,
136    migrateUnknown: "執行 migration",
137    migrateUnknownNote: "無法列出這個工具的待執行 migration,所以不顯示清單。",
138    migrateRun: ({ label }) => `執行 ${label}`,
139    migrateListFailed: ({ argv }) => `無法列出待執行的 migration(${argv} 失敗)。`,
140    migrateNothing: ({ label }) => `執行 ${label}:沒有待執行的項目`,
141    migrateApply: ({ n }) => `套用 ${n} 個待執行的 migration`,
142    migrateNote: ({ argv }) => `來自 ${argv}。`,
143  },
144  ja: {
145    title: ({ label }) => `⚠ Blast Radius · ${label}`,
146    command: "コマンド  ",
147    would: "影響      ",
148    more: ({ n }) => `  他 ${n} 件`,
149    proceed: "実行",
150    cancel: "キャンセル",
151    waiting: "Claude はあなたの回答を待っています",
152    running: "Blast Radius:実行します",
153    denyInterrupted: `Blast Radius がこのコマンドを保留し、実行しませんでした:ターンが中断されました。ユーザーの指示がない限り再試行しないでください。 ${DENY_TAIL}`,
154    deny: ({ why, summary }) => `Blast Radius がこのコマンドを保留し、実行しませんでした:${why}。実行していれば:${summary}。ユーザーの指示がない限り再試行しないでください。 ${DENY_TAIL}`,
155    whyCancel: "ユーザーがキャンセルを押しました",
156    whyTimeout: "10 分以内に回答がありませんでした",
157    whyInterrupted: "ターンが中断されました",
158    whyError: "Blast Radius が保留中にエラーになりました",
159    whyUnknown: "回答が記録されていません",
160    inDir: ({ label, dir }) => `${dir} で ${label}`,
161    noDir: ({ dir }) => `フォルダ ${dir} が見つからないため、影響を測定できませんでした。`,
162    couldNotMeasure: ({ label }) => `${label}(測定できませんでした)`,
163    couldNotMeasureNote: ({ error }) => `測定できませんでした:${error}`,
164    rmNoPaths: "rm にパスがありません",
165    rmNoPathsNote: "展開するパスがありません。",
166    rmNothing: ({ targets }) => `何も削除しません:${targets} に一致するファイルがありません`,
167    rmNothingNote: "パスが存在しないため、rm が削除するものはありません。",
168    rmEmpty: ({ found }) => `${found} 個のパスを削除(中にファイルはありません)`,
169    rmPaths: ({ targets }) => `パス:${targets}`,
170    rmFiles: ({ files, size }) => `${files} 個のファイルを削除(約 ${size})`,
171    cleanFailed: "git clean(ドライランできませんでした)",
172    cleanNothing: "何も削除しません:一致する未追跡ファイルがありません",
173    cleanRemove: ({ n }) => `${n} 個の未追跡パスを削除`,
174    cleanNote: "git clean -n の結果。未追跡ファイルは git に無いため、復元できません。",
175    notRepo: ({ label }) => `${label}(ここは git リポジトリではない?)`,
176    discardNothing: "何も破棄しません:未コミットの変更がありません",
177    discard: ({ n }) => `${n} 個のファイルの未コミット変更を破棄`,
178    discardStat: ({ stat }) => `${stat}。未コミットの変更は復元できません。`,
179    discardNote: "git status --porcelain の結果。",
180    pushTo: ({ ref }) => `${ref} へ強制プッシュ`,
181    pushUnknown: ({ ref }) => `${ref} のローカルコピーが無いため、どのコミットが失われるか分かりません。先に git fetch してください。`,
182    pushNothing: ({ ref }) => `${ref} へ強制プッシュ:失われるコミットはありません`,
183    pushDrops: ({ ref, n }) => `${ref} へ強制プッシュ:${n} 件のコミットが失われます`,
184    pushNote: ({ ref, source }) => `${ref} にあって ${source} に無いコミット(最後の fetch 時点)。`,
185    migrateUnknown: "マイグレーションを実行",
186    migrateUnknownNote: "このツールの未適用マイグレーションを一覧できないため、リストは表示しません。",
187    migrateRun: ({ label }) => `${label} を実行`,
188    migrateListFailed: ({ argv }) => `未適用マイグレーションを一覧できませんでした(${argv} が失敗)。`,
189    migrateNothing: ({ label }) => `${label} を実行:未適用はありません`,
190    migrateApply: ({ n }) => `${n} 件の未適用マイグレーションを適用`,
191    migrateNote: ({ argv }) => `${argv} の結果。`,
192  },
193};
194
195/** The string for `key` in `lang` (English when the dictionary lacks it), with `params` applied. */
196export function t(lang, key, params = {}) {
197  const dict = MESSAGES[lang] ?? MESSAGES[DEFAULT_LANG];
198  const entry = dict[key] ?? MESSAGES[DEFAULT_LANG][key];
199  if (entry === undefined) {
200    return key;
201  }
202  return typeof entry === "function" ? entry(params) : entry;
203}
204