fix(claude-code-mods): make Blast Radius holds independent of sleep

This commit is contained in:
Tianyi Cui committed 2026-10-06 10:20:25 +08:00
1 parent 6f566013b9
commit cbcbbf8825
7 files changed
+24 -10

No files matched your search

@@ -15,8 +15,8 @@
en: d152484eb41ac6b4
zh: 09388d293f9be9cb
/deepseek-ai-dsh-experimental-claude-code-mods/use-this-package:
en: 95dcbcd789f3a4dd
zh: 34aa7c9d225f6fe4
en: 47ac0f415bce9013
zh: 80c3fc5c0337f1dc
/deepseek-ai-dsh-experimental-claude-code-mods/use-this-package/which-events-your-mod-receives:
en: d2d735b07524c08a
zh: 6b90dc578d61ab22
@@ -55,6 +55,8 @@ export default defineMod({ name: 'token-weather', version: '0.1.0', root: import
The generated [configuration catalog](../../../docs/config-catalog.md#deepseek-aidsh-experimental-claude-code-mods) is the exhaustive source for every accepted field. The [examples](examples/) directory holds the three mods from Anthropic's [Getting started with Claude Code mods](https://claude.dev/blog/getting-started-with-claude-code-mods/) post as plugin directories that also run under `claude --plugin-dir`: Token Weather with its published module, types, and test unchanged, and Blast Radius and Replay Theater completed from the published fragments. The [opt-in overlay](cordis.source.patch.yml) composes the bridge, the three mods, and the [Web band](../client-ui-claude-code-mods/README.md) for a source launch.
Blast Radius's DSH wrapper supplies a Node timer subprocess using the running executable, so its Proceed/Cancel hold does not require POSIX `sleep` on Windows. The wait still uses `$.process.run`, which pauses the hook's running-time budget and cancels the child with the event. The standalone Claude Code hooks module defaults to `sleep 0.25`; its dry-run commands also require the corresponding host utilities.
### Which events your mod receives
| Event | Raised from | A hook can |
@@ -55,6 +55,8 @@ export default defineMod({ name: 'token-weather', version: '0.1.0', root: import
生成的[配置目录](../../../docs/config-catalog.zh.md#deepseek-aidsh-experimental-claude-code-mods)是所有可接受字段的完整来源。[examples](examples/) 目录以插件目录的形式收录了 Anthropic [Getting started with Claude Code mods](https://claude.dev/blog/getting-started-with-claude-code-mods/) 一文中的三个模组,它们同样可在 `claude --plugin-dir` 下运行:Token Weather 的已发布模块、类型与测试原样保留,Blast Radius 与 Replay Theater 则由已发布片段补全。[可选叠加层](cordis.source.patch.yml)把桥接、三个模组与 [Web 横幅](../client-ui-claude-code-mods/README.zh.md)组合起来用于源码启动。
Blast Radius 的 DSH 包装使用当前运行的可执行文件提供 Node 计时子进程,因此 Proceed/Cancel 等待在 Windows 上不依赖 POSIX `sleep`。等待仍通过 `$.process.run` 执行,暂停钩子自身的运行时间预算,并随事件取消子进程。独立的 Claude Code hooks 模块默认使用 `sleep 0.25`;其预演命令也需要宿主机提供相应工具。
### 你的模组会收到哪些事件
| 事件 | 触发自 | 钩子可以 |
@@ -1,6 +1,6 @@
// Blast Radius, from "Getting started with Claude Code mods"
// (https://claude.dev/blog/getting-started-with-claude-code-mods/, Anthropic, 2026-10-01).
// The post publishes the tool.call hook below unchanged; the rest of the module
// The tool.call hook adds a waitArgv option for DSH's portable timer; the rest of the module
// (classify, measure, the pane and band trees) is completed to the post's
// description of the mod, which is marked where it starts.
@@ -19,7 +19,7 @@ const RISKS = [
// What the hook is holding: one command at a time, until a button decides.
let held = null;
export function register(on) {
export function register(on, { waitArgv = ["sleep", "0.25"] } = {}) {
on("tool.call", { tool: "Bash" }, async ($, e, next) => {
const risk = classify(String(e.command ?? ""));
if (risk === null) return next(e); // everything else runs as normal
@@ -30,7 +30,7 @@ export function register(on) {
if (!opened.isPlaced) held.where = "band"; // too narrow for a pane: draw above the prompt
while (held.decision === null && !next.signal.aborted) {
await $.process.run(["sleep", "0.25"]); // time inside $ calls doesn't count against the hook's time limit
await $.process.run(waitArgv); // time inside $ calls doesn't count against the hook's time limit
}
if (held.decision === "proceed") return next(e); // let it run
return { deny: `Blast Radius held this command: the user pressed Cancel. It would have: ${report.summary}.` };
@@ -1,10 +1,12 @@
import { defineMod } from '../../src/define-mod.ts'
import { register } from './hooks/blast-radius.mjs'
/** Blast Radius as a DSH plugin: holds a risky Bash command until a Proceed or Cancel press. */
/** Blast Radius as a DSH plugin, using the running Node executable for its cancellable hold timer. */
export default defineMod({
name: 'blast-radius',
version: '0.1.0',
root: import.meta.dirname,
register,
register(on, options) {
register(on, { ...options, waitArgv: [process.execPath, '-e', 'setTimeout(() => {}, 250)'] })
},
})
@@ -21,7 +21,7 @@ describe('blast-radius', () => {
on('process.run', (_$, e: { argv: string[] }) => {
ran.push(e.argv.join(' '))
// The hold loop waits on short sleeps; give each one real time so a press can land between them.
if (e.argv[0] === 'sleep') return new Promise(resolve => setTimeout(() => resolve({ value: { exitCode: 0, stdout: '', stderr: '' } }), 5))
if (e.argv[0] === 'sleep' || e.argv[1] === '-e') return new Promise(resolve => setTimeout(() => resolve({ value: { exitCode: 0, stdout: '', stderr: '' } }), 5))
if (e.argv[0] === 'git' && e.argv[1] === 'status') return { value: { exitCode: 0, stdout: ' M src/index.ts\n M README.md\n', stderr: '' } }
return { value: { exitCode: 0, stdout: '', stderr: '' } }
})
@@ -56,7 +56,7 @@ describe('blast-radius', () => {
test('measures a recursive delete with du and find, and draws into a placed pane', async ($, on) => {
on('session.cwd', () => ({ value: '/work' }))
on('process.run', (_$, e: { argv: string[] }) => {
if (e.argv[0] === 'sleep') return new Promise(resolve => setTimeout(() => resolve({ value: { exitCode: 0, stdout: '', stderr: '' } }), 5))
if (e.argv[0] === 'sleep' || e.argv[1] === '-e') return new Promise(resolve => setTimeout(() => resolve({ value: { exitCode: 0, stdout: '', stderr: '' } }), 5))
if (e.argv[0] === 'du') return { value: { exitCode: 0, stdout: '1.1M\tbuild\n', stderr: '' } }
if (e.argv[0] === 'find') return { value: { exitCode: 0, stdout: 'build/a.js\nbuild/b.js\nbuild/c.map\n', stderr: '' } }
return { value: { exitCode: 0, stdout: '', stderr: '' } }
@@ -4,7 +4,7 @@ import type { AddressInfo } from 'node:net'
import { tmpdir } from 'node:os'
import { pathToFileURL } from 'node:url'
import { join, resolve } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest'
import { Context, type Fiber } from '@deepseek-ai/cordis'
import Loader from '@deepseek-ai/cordis-plugin-loader'
import type { Agent } from '@deepseek-ai/dsh-agent'
@@ -1077,6 +1077,13 @@ describe('the band above the prompt', () => {
await ctx.plugin(LocalSubprocessRuntime)
},
})
// Windows does not provide the POSIX sleep executable; other subprocesses run normally.
const spawn = h.ctx.subprocess.spawn.bind(h.ctx.subprocess)
const withoutSleep = vi.spyOn(h.ctx.subprocess, 'spawn').mockImplementation((spec) => {
if (spec.argv[0] === 'sleep') throw new Error('spawn sleep ENOENT')
return spawn(spec)
})
onTestFinished(() => { withoutSleep.mockRestore() })
h.ctx.tools.register(echoTool('bash', ran))
const agent = await h.agent()
const mods = h.ctx.claudeCodeMods
@@ -1104,6 +1111,7 @@ describe('the band above the prompt', () => {
expect(cancelId).toBeDefined()
await mods.pressBand(agent, drawn.generation, cancelId ?? '')
await holding
expect(h.warn).not.toHaveBeenCalledWith(expect.stringMatching(/tool.call hook skipped/))
expect(ran).toEqual([])
expect(toolResult(agent)?.text).toMatch(/Blast Radius held this command: the user pressed Cancel/)
await waitFor(() => !JSON.stringify(seen.at(-1)).includes('Cancel'))