去重:内容相同的文件只保留第一个。 为什么需要去重? 有时候项目根目录和子目录都有 CLAUDE.md,但内容完全一样。 重复加载浪费 token(花更多钱),所以要去重。 对应源码: prompt.rs:326-341
(files: list[ContextFile])
| 136 | |
| 137 | |
| 138 | def _dedupe_instruction_files(files: list[ContextFile]) -> list[ContextFile]: |
| 139 | """ |
| 140 | 去重:内容相同的文件只保留第一个。 |
| 141 | |
| 142 | 为什么需要去重? |
| 143 | 有时候项目根目录和子目录都有 CLAUDE.md,但内容完全一样。 |
| 144 | 重复加载浪费 token(花更多钱),所以要去重。 |
| 145 | |
| 146 | 对应源码: prompt.rs:326-341 |
| 147 | """ |
| 148 | seen_hashes: set[str] = set() |
| 149 | deduped: list[ContextFile] = [] |
| 150 | |
| 151 | for file in files: |
| 152 | # "标准化":去掉多余空行和首尾空白,然后算哈希 |
| 153 | normalized = _normalize(file.content) |
| 154 | content_hash = hashlib.sha256(normalized.encode()).hexdigest() |
| 155 | |
| 156 | if content_hash not in seen_hashes: |
| 157 | seen_hashes.add(content_hash) |
| 158 | deduped.append(file) |
| 159 | |
| 160 | return deduped |
| 161 | |
| 162 | |
| 163 | def _normalize(content: str) -> str: |
no test coverage detected