refactor-safely

refactor-safely is a skill for Claude Code, Codex from pingfanfan/hello-dsh. It costs 54 tokens per session (971 once invoked), scanned A, original, MIT.

A safe code-refactoring guide for changing code structure while keeping its external behavior the same. Refactoring means reorganizing code without changing what it does.

In plain words
What is it for?
Use it when extracting functions, splitting large files, removing duplicate code, or changing interfaces in stages.
Why use it?
It reduces the risk of breaking working software by requiring small changes, verification after each step, and separate commits for refactoring and feature changes.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/pingfanfan/hello-dsh/refactor-safely
Any agent
npx skills add pingfanfan/hello-dsh --skill refactor-safely
Clone the repo
git clone --depth 1 https://github.com/pingfanfan/hello-dsh

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for refactor-safely

README.md
[![agentmods](https://agentmods.dev/badge/skills/pingfanfan/hello-dsh/refactor-safely.svg)](https://agentmods.dev/skills/pingfanfan/hello-dsh/refactor-safely)
Your own site
<a href="https://agentmods.dev/skills/pingfanfan/hello-dsh/refactor-safely"><img src="https://agentmods.dev/badge/skills/pingfanfan/hello-dsh/refactor-safely.svg" alt="Measured on agentmods" height="20"></a>
Per session 54 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 971 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00054 $0.00971
Opus 5 $0.00027 $0.00485
Sonnet 5 $0.00011 $0.00194
Haiku 4.5 $0.00005 $0.00097

Measured 5d ago against content hash 52cb8a570951, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-05, from the pricing page.

Security

Grade A, and why

refactor-safely scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 5d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

examples/skills/refactor-safely/SKILL.md · 82 lines

What it actually says

安全重构

重构的定义:改变代码结构,不改变外部行为。

一旦掺进了行为变更,它就不再是重构,出问题时也无法用「行为应该完全一样」来定位。这是这件事唯一的纪律。

开始之前

三个前置条件,缺一个就先补:

  1. 有能验证行为不变的手段 —— 测试、快照、或一段能对比输出的脚本。没有就先写,这不是浪费时间,是重构的唯一安全网。
  2. 工作区是干净的 —— 重构的 diff 必须只包含重构。
  3. 说得清为什么要改 —— 「这样更好看」不算理由。真实理由通常是:这里要加功能但加不进去、这段重复已经导致过 bug、这个函数没人敢动。

说不清理由就不要动。 没有具体痛点驱动的重构,是在用风险换取审美。

小步前进

每一步都要满足:改完立刻能验证,验证失败能立刻回退。

一步的合理大小:

  • 提取一个函数
  • 重命名一个符号
  • 移动一个函数到另一个文件
  • 内联一个只用一次的变量

不合理的一步:

  • 「把这个模块重新组织一下」
  • 「顺便把命名统一了」

每步之后跑验证。不要攒着一起验。 攒了三步再跑,失败时你不知道是哪步的问题,而这三步之间往往互相依赖,回退也变难。

常见重构的顺序

提取函数:先复制代码到新函数,参数用原地变量名,跑通;再把原处替换成调用;最后清理参数名。不要一步到位。

拆分大文件:先把要移走的部分整块移动,import 补上,验证;再调整命名和接口。移动和改造分开做。

消除重复:先确认这两处真的是同一件事,不是长得像。长得像但会独立演化的代码,合并之后会变成带一堆 flag 的函数,比重复更糟。

改接口:先加新的,让新旧并存并验证;再逐个迁移调用方;最后删旧的。三次提交,不是一次。

什么时候停

  • 验证挂了,且十分钟内没定位到原因 —— 回退这一步,重新想
  • 发现需要改行为才能继续 —— 停下,把行为变更单独提出来做,做完再回来重构
  • 改动范围开始扩散 —— 说明这一步太大了,回退,切小

回退不是失败。 在重构里回退是廉价的,卡在半途才是昂贵的。

提交

重构和功能修改必须分开提交

判断标准:这两部分能不能单独回退? 不能就说明混在一起了,该拆。

提交信息用 refactor: 前缀,正文说清楚为什么要改:

refactor(session): 把事件投影从 SessionStore 里拆出来

SessionStore 现在同时负责持久化和投影,加新的事件类型时两处都要
改,已经漏过一次。拆开之后投影逻辑可以单独测。

行为不变,投影结果的快照测试全部通过。

最后那句「行为不变 + 怎么验证的」是重构提交该有的东西。

不要做的事

  • 不要在重构的同时修 bug,哪怕顺手
  • 不要在重构的同时改格式(格式化单独一个提交)
  • 不要引入新抽象,除非当前的重复已经造成了具体问题
  • 不要在没有验证手段的情况下动手
  • 不要一次改超过你能一眼看完的量
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 5d ago First seen · 82 lines · 54 tokens per session scan A 52cb8a570951

Subscribe to this mod's changes

refactor-safely is a skill published in the GitHub repository pingfanfan/hello-dsh (88 stars, last pushed 22d ago), licensed MIT. It adds 54 tokens to every session and 971 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

Other skills, from other repositories

dsh-plugin-guide

Use when developing, reviewing, packaging, debugging, or answering questions about DeepSeek Harness (DSH) plugins — the plugin-based agent harness on vendored Cordis. Applies the official plugin-development constraints (plugin contract, cordis.yml layers, services/events/effects, tool DSL, bundles/profiles) backed by…

PerryLink/dsh-plugin-guide · 76 tokens

dsh-web-release

Release and publish the dsh-web monorepo (DSH Web GUI plugin family + skin collection) — bump all packages to one unified version, commit and tag (tags are cut from main after dev integration; dev is the integration branch), push the vX.Y.Z tag that triggers the GitHub Actions publish pipeline, and verify the npm…

zhu1090093659/dsh-web · 151 tokens

dsh-web-community-plugin-developer

Develop a DSH community plugin and register it in the dsh-web Community Plugins index — author the plugin in the contributor's own repository following the official cordis bundle standard, add its entry to packages/dsh-community-plugins/community.json, regenerate the index with scripts/community-index, rebuild and…

zhu1090093659/dsh-web · 123 tokens

dsh-web-skin-developer

Build a new skin for the dsh-web skin collection (DSH Web GUI) and publish it into the Skin Center — the first-level settings section — scaffold with scripts/dsh-skin-new, author the v2 skin.json manifest plus skin.css token remap (pure asset directory, no package.json, no build step), validate with scripts/dsh-skin…

zhu1090093659/dsh-web · 120 tokens

dsh-web-pre-push-checks

Use before pushing, opening or updating a pull request, or claiming dsh-web checks pass. Selects the required repository gates and diff-specific generation, build, and GUI evidence.

zhu1090093659/dsh-web · 45 tokens

dsh-web-documentation

Use when adding or editing dsh-web README files, docs, AGENTS.md instructions, user-facing configuration text, or bilingual documentation pairs.

zhu1090093659/dsh-web · 34 tokens