From cbcbbf8825614e7133d9f0a96445bbacad3082ea Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Tue, 6 Oct 2026 10:20:15 +0800 Subject: [PATCH] fix(claude-code-mods): make Blast Radius holds independent of sleep --- .../experimental/claude-code-mods/README.i18n.yaml | 4 ++-- packages/experimental/claude-code-mods/README.md | 2 ++ packages/experimental/claude-code-mods/README.zh.md | 2 ++ .../examples/blast-radius/hooks/blast-radius.mjs | 6 +++--- .../claude-code-mods/examples/blast-radius/index.ts | 6 ++++-- .../examples/blast-radius/tests/blast-radius.test.ts | 4 ++-- .../experimental/claude-code-mods/tests/bridge.spec.ts | 10 +++++++++- 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/experimental/claude-code-mods/README.i18n.yaml b/packages/experimental/claude-code-mods/README.i18n.yaml index b74490c148..75a4be20d1 100644 --- a/packages/experimental/claude-code-mods/README.i18n.yaml +++ b/packages/experimental/claude-code-mods/README.i18n.yaml @@ -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 diff --git a/packages/experimental/claude-code-mods/README.md b/packages/experimental/claude-code-mods/README.md index d259150e9a..da606f5620 100644 --- a/packages/experimental/claude-code-mods/README.md +++ b/packages/experimental/claude-code-mods/README.md @@ -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 | diff --git a/packages/experimental/claude-code-mods/README.zh.md b/packages/experimental/claude-code-mods/README.zh.md index d8f789461d..8d1edb94ff 100644 --- a/packages/experimental/claude-code-mods/README.zh.md +++ b/packages/experimental/claude-code-mods/README.zh.md @@ -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`;其预演命令也需要宿主机提供相应工具。 + ### 你的模组会收到哪些事件 | 事件 | 触发自 | 钩子可以 | diff --git a/packages/experimental/claude-code-mods/examples/blast-radius/hooks/blast-radius.mjs b/packages/experimental/claude-code-mods/examples/blast-radius/hooks/blast-radius.mjs index 94af360a45..b907cbb5c8 100644 --- a/packages/experimental/claude-code-mods/examples/blast-radius/hooks/blast-radius.mjs +++ b/packages/experimental/claude-code-mods/examples/blast-radius/hooks/blast-radius.mjs @@ -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}.` }; diff --git a/packages/experimental/claude-code-mods/examples/blast-radius/index.ts b/packages/experimental/claude-code-mods/examples/blast-radius/index.ts index 49ecde7a6f..46b1580800 100644 --- a/packages/experimental/claude-code-mods/examples/blast-radius/index.ts +++ b/packages/experimental/claude-code-mods/examples/blast-radius/index.ts @@ -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)'] }) + }, }) diff --git a/packages/experimental/claude-code-mods/examples/blast-radius/tests/blast-radius.test.ts b/packages/experimental/claude-code-mods/examples/blast-radius/tests/blast-radius.test.ts index c26ec91183..77f3fe3dc9 100644 --- a/packages/experimental/claude-code-mods/examples/blast-radius/tests/blast-radius.test.ts +++ b/packages/experimental/claude-code-mods/examples/blast-radius/tests/blast-radius.test.ts @@ -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: '' } } diff --git a/packages/experimental/claude-code-mods/tests/bridge.spec.ts b/packages/experimental/claude-code-mods/tests/bridge.spec.ts index 5111e575c3..de0dafc66a 100644 --- a/packages/experimental/claude-code-mods/tests/bridge.spec.ts +++ b/packages/experimental/claude-code-mods/tests/bridge.spec.ts @@ -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'))