autoupgrade-icp-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Set up automatic
icp
CLI upgrades

设置
icp
CLI 自动升级

A one-time installer: it adds a
SessionStart
hook that checks whether
icp
and
ic-wasm
are current and — if the user wants — upgrades them before work starts. Once these steps are done the user never needs this skill again.
ic-wasm
is covered alongside
icp
because the official recipes (
@dfinity/motoko
,
@dfinity/rust
,
@dfinity/static-site
,
@dfinity/asset-canister
) invoke it on every build. An
ic-wasm
left behind while
icp
moves forward produces build failures that read as recipe bugs, so upgrading only one of the pair is the worse default.
icp
has its own check (
icp settings update-check
) that is worth leaving on, but it only speaks once the user runs an
icp
command — by which point the agent has already chosen what to write. This hook runs before the session's first prompt, so the version reaches Claude's context up front, and it covers
ic-wasm
too.
一次性安装器:它会添加一个
SessionStart
hook,检查
icp
和
ic-wasm
是否为最新版本,并且——如果用户愿意——在开始工作前升级它们。完成这些步骤后,用户永远不需要再次使用此 skill。
之所以同时处理
ic-wasm
和
icp
,是因为官方 recipes(
@dfinity/motoko
、
@dfinity/rust
、
@dfinity/static-site
、
@dfinity/asset-canister
)每次构建时都会调用它。如果
icp
已升级而
ic-wasm
仍停留在旧版本,构建失败时看起来会像是 recipe 的问题。因此,只升级两者中的一个并不是更稳妥的默认方案。
icp
自身提供了检查命令(
icp settings update-check
),值得保持启用,但它只会在用户运行
icp
命令后才开始检查——那时 agent 已经选好了要写入的内容。这个 hook 会在会话首次提示之前运行,因此版本信息会在一开始就进入 Claude 的上下文,并且它也会检查
ic-wasm
。

Step 1 — Ask the user which mode they want

第 1 步——询问用户希望使用哪种模式

The installer cannot make this call for the user, so ask before writing anything:
"Two options for how the hook behaves when a newer version exists: notify — it prints the versions and the exact upgrade command, and nothing is installed until you run it. auto — it runs that upgrade itself at session start. Which do you want? You can switch later by editing one flag in the hook."
Default to notify if the user has no preference — it is the reversible choice.
Also tell them what adding a hook means, and do not attempt to bypass the approval:
"I'm adding a
SessionStart
hook that runs
.claude/upgrade-icp-cli.sh
. Claude Code will ask you to approve/trust it before it runs automatically."
安装器无法替用户做这个决定,因此写入任何内容前都要先询问:
"当存在新版本时,hook 有两种行为方式: notify——打印版本和准确的升级命令,在你运行该命令前不会安装任何内容。auto——在会话启动时自行执行升级。 你想要哪一种?以后可以编辑 hook 中的一个标志来切换。"
如果用户没有偏好,默认使用 notify——这是可逆的选择。
同时还要告诉用户添加 hook 意味着什么,并且不要试图绕过审批:
"我正在添加一个运行
.claude/upgrade-icp-cli.sh
的
SessionStart
hook。Claude Code 会要求你批准或信任它,然后才会自动运行。"

Step 2 — Check prerequisites

第 2 步——检查先决条件

bash
command -v curl   >/dev/null 2>&1 && echo "curl: ok" || echo "curl: MISSING"
icp --version     2>/dev/null     || echo "icp: not installed"
ic-wasm --version 2>/dev/null     || echo "ic-wasm: not installed"
If either tool is missing, mention the install command (
npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm
) but do not run it unprompted — installing a toolchain is a different decision from keeping one current.
bash
command -v curl   >/dev/null 2>&1 && echo "curl: ok" || echo "curl: MISSING"
icp --version     2>/dev/null     || echo "icp: not installed"
ic-wasm --version 2>/dev/null     || echo "ic-wasm: not installed"
如果缺少任一工具,请告知安装命令(
npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm
),但不要在未经提示的情况下运行它——安装一套工具链与保持现有工具链最新是不同的决定。

Step 3 — Download the script

第 3 步——下载脚本

Fetch the published script verbatim rather than transcribing it, so the channel detection stays correct as it is updated upstream:
bash
mkdir -p .claude
curl -fsSL https://skills.internetcomputer.org/.well-known/skills/autoupgrade-icp-cli/scripts/upgrade-icp-cli.sh \
  -o .claude/upgrade-icp-cli.sh
Do not hand-write or paraphrase it.
应原样获取已发布的脚本,而不是手动转录,这样上游更新渠道检测逻辑时,脚本也能保持正确:
bash
mkdir -p .claude
curl -fsSL https://skills.internetcomputer.org/.well-known/skills/autoupgrade-icp-cli/scripts/upgrade-icp-cli.sh \
  -o .claude/upgrade-icp-cli.sh
不要手动编写或改写该脚本。

Step 4 — Register the SessionStart hook (idempotently)

第 4 步——注册 SessionStart hook(幂等方式)

If
.claude/settings.json
does not exist, create it with the content below. If it does, merge: preserve every existing key, hook, and permission, add the
SessionStart
entry only if an equivalent
bash .claude/upgrade-icp-cli.sh
command is not already there, and write the parsed JSON back. Never overwrite the file.
Notify mode (the default):
json
{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "bash .claude/upgrade-icp-cli.sh --mode notify" }
        ]
      }
    ]
  }
}
Auto mode — same entry with
--mode auto
, plus a raised
timeout
, because installing a CLI binary can exceed the default 60 s and a timed-out install leaves the tool half-written:
json
{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "bash .claude/upgrade-icp-cli.sh --mode auto", "timeout": 300 }
        ]
      }
    ]
  }
}
Whenever you hand someone an auto-mode hook, say the thing that makes it a real choice: it upgrades the build toolchain while a project is open and blocks session start while it runs, so a session can begin against a different
icp
than the last one ended with — which can change build behaviour and output for a project that built fine yesterday. That is what some teams want and what others pin against.
如果
.claude/settings.json
不存在,则使用下面的内容创建它。如果已经存在,则进行合并:保留所有现有键、hook 和权限配置,仅在尚不存在等效的
bash .claude/upgrade-icp-cli.sh
命令时添加
SessionStart
条目,然后将解析后的 JSON 写回。绝不覆盖整个文件。
Notify 模式(默认):
json
{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "bash .claude/upgrade-icp-cli.sh --mode notify" }
        ]
      }
    ]
  }
}
Auto 模式——使用相同的条目并设置
--mode auto
,同时提高
timeout
,因为安装 CLI 二进制文件可能超过默认的 60 秒,而安装超时会使工具处于只写入一部分的状态:
json
{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "bash .claude/upgrade-icp-cli.sh --mode auto", "timeout": 300 }
        ]
      }
    ]
  }
}
每当向用户提供 auto 模式的 hook 时,都必须说明这个选项为何是一项真正的选择:它会在项目处于打开状态时升级构建工具链,并在运行期间阻塞会话启动。因此,本次会话开始时使用的
icp
可能与上次会话结束时不同,从而改变原本昨天还能正常构建的项目的行为和输出。有些团队需要这一特性,而另一些团队则会固定版本以避免这种变化。

Step 5 — Run it once, verify, report

第 5 步——运行一次、验证并报告

bash
bash .claude/upgrade-icp-cli.sh --mode notify   # or --mode auto, matching the hook
Silence means both tools are current — that is the success case, not a failure. A hand-run always checks the network; the hook throttles (below), so this is also the command for whenever someone wants an answer now.
Then confirm the hook entry appears exactly once, report the versions found and whether anything was upgraded, and remind the user of the trust prompt. If the project commits
.claude/
, add
.claude/.icp-upgrade-check
to
.gitignore
— it is a per-machine timestamp, not shared configuration.
bash
bash .claude/upgrade-icp-cli.sh --mode notify   # 或使用 --mode auto,与 hook 保持一致
没有输出表示两个工具都已是最新版本——这是成功情况,而不是失败。手动运行始终会检查网络;hook 会进行节流(见下文),因此每当用户想立即获取结果时,都应使用这条命令。
随后确认 hook 条目只出现一次,报告检测到的版本以及是否执行了升级,并提醒用户注意信任提示。如果项目会将
.claude/
提交到版本控制,请将
.claude/.icp-upgrade-check
添加到
.gitignore
——它是每台机器独立使用的时间戳,不属于共享配置。

How the script decides what to run

脚本如何决定运行什么

It resolves each binary through its symlinks and reads the install channel off the path, because the upgrade command differs per channel and guessing wrong leaves two copies of the CLI on
PATH
shadowing each other:
Where the binary resolves toChannelUpgrade command (
icp
/
ic-wasm
)
…/node_modules/@icp-sdk/…
npm
npm install -g @icp-sdk/icp-cli@latest
/
npm install -g @icp-sdk/ic-wasm@latest
…/Cellar/…
Homebrew
brew upgrade icp-cli
/
brew upgrade ic-wasm
a cargo-dist receipt in
~/.config/<app>/
shell installer
icp-cli-update
/
ic-wasm-update
anything elseunknown(none — reports the release URL instead)
That last row is deliberate: an unrecognised install is reported, never guessed at.
Two consequences worth passing on to users:
  • A shadowed install never moves. If an upgrade succeeds but
    PATH
    still resolves to the old version, a second copy from another channel is winning — commonly an npm global under
    nvm
    alongside a shell-installer copy in
    ~/.cargo/bin
    . Upgrading one channel can never touch the other's copy; resolve the binary (
    command -v icp
    ) to see which wins, then remove one or upgrade the one actually on
    PATH
    .
  • Auto mode never runs
    sudo
    .
    A non-writable npm prefix is reported as a
    sudo …
    command instead, since a password prompt inside a hook has no terminal to answer it.
脚本会沿着符号链接解析每个二进制文件,并根据路径识别安装渠道,因为不同渠道的升级命令各不相同;如果猜测错误,
PATH
上可能会留下两份 CLI 并相互遮蔽:
二进制文件解析到的位置渠道升级命令(
icp
/
ic-wasm
)
…/node_modules/@icp-sdk/…
npm
npm install -g @icp-sdk/icp-cli@latest
/
npm install -g @icp-sdk/ic-wasm@latest
…/Cellar/…
Homebrew
brew upgrade icp-cli
/
brew upgrade ic-wasm
~/.config/<app>/
中的 cargo-dist receipt
shell 安装器
icp-cli-update
/
ic-wasm-update
其他位置未知(无——改为报告发布 URL)
最后一行是刻意设计的:无法识别的安装只会被报告,绝不会凭猜测执行升级。
有两个结果值得告知用户:
  • 被遮蔽的安装永远不会更新。 如果升级成功,但
    PATH
    仍然解析到旧版本,说明另一个渠道中的另一份副本正在生效——常见情况是
    nvm
    下的 npm 全局安装与
    ~/.cargo/bin
    中的 shell 安装器副本并存。升级一个渠道永远无法影响另一个渠道的副本;可使用
    command -v icp
    解析二进制文件以确定当前生效的版本,然后删除另一份副本,或升级
    PATH
    上实际生效的那一份。
  • Auto 模式绝不运行
    sudo
    。
    如果 npm prefix 不可写,脚本只会报告一条
    sudo …
    命令,因为 hook 内部没有可用于输入密码的终端。

Throttling

节流机制

A check that reached a release feed is stamped in
.claude/.icp-upgrade-check
and not repeated for 6 hours, so the hook costs ~10 ms on most session starts instead of ~1 s. Override with
ICP_UPGRADE_CHECK_INTERVAL=<seconds>
(
0
disables it) or bypass once with
--force
; a run from a terminal always checks. A failed probe is not stamped, so an offline session retries at the next one.
每次成功访问发布源的检查都会在
.claude/.icp-upgrade-check
中记录时间戳,6 小时内不会重复检查。因此,大多数会话启动时 hook 只需约 10 ms,而不是约 1 秒。可以使用
ICP_UPGRADE_CHECK_INTERVAL=<seconds>
覆盖间隔(设为
0
可禁用节流),也可以使用
--force
临时绕过一次;从终端手动运行时始终会检查。失败的探测不会记录时间戳,因此离线会话会在下一次启动时重试。

What this does not upgrade

此 hook 不会升级什么

The hook manages the two CLI binaries only. Recipe versions in
icp.yaml
(
@dfinity/motoko@v5.0.0
and friends) and the Motoko toolchain (
moc
,
mops
, the
[toolchain]
pin in
mops.toml
) are pinned per project and stay the user's call — load
icp-cli
and
mops-cli
respectively for those.
If a build starts failing right after an auto upgrade, check the icp-cli release notes before assuming the project broke.
该 hook 只管理两个 CLI 二进制文件。
icp.yaml
中的 recipe 版本(
@dfinity/motoko@v5.0.0
等)以及 Motoko 工具链(
moc
、
mops
和
mops.toml
中的
[toolchain]
版本固定)都按项目进行固定,仍应由用户决定——这些内容应分别加载
icp-cli
和
mops-cli
。
如果项目在 auto 升级后立即开始构建失败,请先查看 icp-cli 发布说明,不要立即认定项目本身出现了问题。

Additional References

附加参考

  • icp-cli
    — using the CLI itself:
    icp.yaml
    , recipes, environments, deployment.
  • mops-cli
    — the Motoko toolchain and its own version pinning.
  • autosync-ic-skills
    — the companion installer that keeps the IC skills in
    .claude/skills/
    current. The two are independent; installing both is common.
  • icp-cli
    ——CLI 本身的使用方法:
    icp.yaml
    、recipes、environments 和 deployment。
  • mops-cli
    ——Motoko 工具链及其版本固定机制。
  • autosync-ic-skills
    ——配套安装器,用于让
    .claude/skills/
    中的 IC skills 保持最新。两者彼此独立,通常可以同时安装。