MCPcopy Create free account
hub / github.com/Louisym/MiniCC / _dedupe_instruction_files

Function _dedupe_instruction_files

tutorials/06_system_prompt_builder.py:138–160  ·  view source on GitHub ↗

去重:内容相同的文件只保留第一个。 为什么需要去重? 有时候项目根目录和子目录都有 CLAUDE.md,但内容完全一样。 重复加载浪费 token(花更多钱),所以要去重。 对应源码: prompt.rs:326-341

(files: list[ContextFile])

Source from the content-addressed store, hash-verified

136
137
138def _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
163def _normalize(content: str) -> str:

Callers 1

Calls 3

_normalizeFunction · 0.85
appendMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected