← 提示词库 Anthropic/claude-code/skills/session-start-hook/SKILL.md 原文 md
🌐 中英双语对照

name: session-start-hook
description: Creating and developing startup hooks for Claude Code on the web. Use when the user wants to set up a repository for Claude Code on the web, create a SessionStart hook to ensure their project can run tests and linters during web sessions.

Startup Hook Skill for Claude Code on the web / Claude Code 网页版启动钩子技能

Create SessionStart hooks that install dependencies so tests and linters work in Claude Code on the web sessions.

创建 SessionStart 钩子以安装依赖,使测试和代码检查器(linter)能在 Claude Code 网页版会话中正常运行。

Hook Basics / 钩子基础

Input (via stdin) / 输入(通过 stdin)

{
  "session_id": "abc123",
  "source": "startup|resume|clear|compact",
  "transcript_path": "/path/to/transcript.jsonl",
  "permission_mode": "default",
  "hook_event_name": "SessionStart",
  "cwd": "/workspace/repo"
}

Async Mode / 异步模式

#!/bin/bash
set -euo pipefail

echo '{"async": true, "asyncTimeout": 300000}'

npm install

The hook runs in background while the session starts. Using async mode reduces latency, but introduces a race condition where the agent loop might depend on something that is being done in the startup hook before it completed.

钩子在会话启动期间于后台运行。使用异步模式可以降低延迟,但会引入竞态条件:代理循环可能依赖启动钩子中尚未完成的某项操作。

Environment Variables / 环境变量

Available environment variables:

Use $CLAUDE_ENV_FILE to persist variables for the session:
使用 $CLAUDE_ENV_FILE 为会话持久化变量:

echo 'export PYTHONPATH="."' >> "$CLAUDE_ENV_FILE"

Use $CLAUDE_CODE_REMOTE to only run a script in a remote env:
使用 $CLAUDE_CODE_REMOTE 让脚本仅在远程环境中运行:

if [ "${CLAUDE_CODE_REMOTE:-}" != "true" ]; then
  exit 0
fi

Workflow / 工作流

Make a todo list for all the tasks in this workflow and work on them one after another

为该工作流中的所有任务建立待办清单,并依次逐项处理

1. Analyze Dependencies / 分析依赖

Find dependency manifests and analyze them. Examples:

Additionally, read though any documentation (i.e. README.md or similar) to see if you can get additional context on how the environment setup works

此外,通读任何文档(即 README.md 或类似文件),看能否获得关于环境设置方式的更多上下文

2. Design Hook / 设计钩子

Create a script that installs dependencies.

创建一个安装依赖的脚本。

Key principles:
关键原则:

3. Create Hook File / 创建钩子文件

mkdir -p .claude/hooks
cat > .claude/hooks/session-start.sh << 'EOF'
#!/bin/bash
set -euo pipefail

echo '{"async": true, "asyncTimeout": 300000}'
# Install dependencies here
EOF

chmod +x .claude/hooks/session-start.sh

4. Register in Settings / 在设置中注册

Add to .claude/settings.json (create if doesn't exist):
添加到 .claude/settings.json(若不存在则创建):

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/session-start.sh"
          }
        ]
      }
    ]
  }
}

If .claude/settings.json exists, merge the hooks configuration.

如果 .claude/settings.json 已存在,则合并钩子配置。

5. Validate Hook / 验证钩子

Run the hook script directly:

直接运行钩子脚本:

CLAUDE_CODE_REMOTE=true ./.claude/hooks/session-start.sh

IMPORTANT: Verify dependencies are installed and script completes successfully.

重要:确认依赖已安装且脚本成功执行完毕。

6. Validate Linter / 验证代码检查器

IMPORTANT: Figure out what the right command is to run the linters and run it for an example file. No need to lint the whole project. If there are any issues, update the startup script accordingly and re-test.

重要:弄清运行代码检查器的正确命令,并对一个示例文件执行。无需对整个项目做检查。如果有任何问题,相应更新启动脚本并重新测试。

7. Validate Test / 验证测试

IMPORTANT: Figure out what the right command is to run the tests and run it for one test. No need to run the whole test suite. If there are any issues, update the startup script accordingly and re-test.

重要:弄清运行测试的正确命令,并对其中一个测试执行。无需运行整个测试套件。如果有任何问题,相应更新启动脚本并重新测试。

8. Commit and push / 提交并推送

Make a commit and push it to the remote branch

创建一次提交并将其推送到远程分支

Wrap up / 收尾

We're all done. In your last message to the user, Provide a detailed summary to the user with the format below:

全部完成。在给用户的最后一条消息中,按以下格式提供详细总结:

【评论】该技能体现了执行环境与提示词的耦合:容器状态在钩子完成后被缓存,因此规格明确建议优先使用 npm install 而非 npm ci 这类可利用缓存的安装方式。