Peach: Local-first video metadata scraper and media manager.
docs: 上下文预算加上字节数与单行长度
longmeidaobd5ecf48ea7f
diff --git a/docs/adr/0015-agent-context-layering.md b/docs/adr/0015-agent-context-layering.mdindex 26f3ec32e..22fadfe2a 100644--- a/docs/adr/0015-agent-context-layering.md+++ b/docs/adr/0015-agent-context-layering.md@@ -45,7 +45,7 @@| --- | --- | --- | --- || L0 全局配置 | `~/.claude/CLAUDE.md`、`~/.codex/AGENTS.md` | 与仓库无关、每个任务都成立的身份与语言偏好 | ≤ 20 行 || L1 项目入口 | `AGENTS.md` | 进入 Peach 必须先成立的边界、硬门槛、术语表与技能索引 | ≤ 90 行 |-| L2 仓库文档 | `README.md`、`docs/**` | 事实、数字、历史证据与架构决策,被指向时才读 | 无行数上限,但按主题分节 |+| L2 仓库文档 | `README.md`、`docs/**` | 事实、数字、历史证据与架构决策,被指向时才读 | 按主题分节;每轮必读的 `docs/HANDOFF.md`、`docs/STATUS.md` 另有预算 || L3 技能 | `.claude/skills/<name>/SKILL.md`、`~/.claude/skills/<name>/SKILL.md` | 某类任务开始时才需要的可执行流程 | 每个 ≤ 120 行 |### 预算只能降不能升@@ -63,6 +63,12 @@- 内部预算(`AGENTS.md` 90、`SKILL.md` 120、`description` 200)一律严于天花板,且只能降不能升。天花板管「绝不越过」,内部预算管「当前写作纪律」,两者不是一回事。+2026-09-03 补充:行数不是可信的度量。`docs/STATUS.md` 当时只有 226 行却重 155 KB,最长一行+一万三千多字符,行数预算完全没被触发。所以入口文件改按三条判:行数、字节数、单行字符数。+当前值为 `AGENTS.md` 12 KB、`docs/HANDOFF.md` 24 KB、`docs/STATUS.md` 12 KB,单行上限 400 字符,+天花板分别是 16 KB、32 KB、16 KB。字节预算的变更记录放在 `BYTE_BUDGET_CHANGES`,语义与行数预算+完全一致:只能降不能升,上调要写明为什么下沉不了。字节按文件实际大小算,单行按字符算。+能自动挡住的只有「留痕」和「封顶」。脚本判断不了某段内容能不能下沉,那是复核时看理由站不站得住。### 判据(决定一条内容写在哪一层)@@ -114,7 +120,7 @@与项目既有规则一致:Git 是归档,不为历史保留死接口。6. **被 ADR 取代的段落**只留一行指向该 ADR。7. **执行门槛**:`tests/test_context_budget.py` 在唯一测试入口 `& .\scripts\test.ps1` 中强制- 校验行数预算、技能 frontmatter 与 `最后复核` 行的存在。日期是否过期只报告,不使全量测试+ 校验行数、字节数与单行长度预算,以及技能 frontmatter 与 `最后复核` 行的存在。日期是否过期只报告,不使全量测试因时间流逝而变红。## 放弃方案diff --git a/scripts/check_context_budget.py b/scripts/check_context_budget.pyindex 96bdb2799..fa45e737e 100644--- a/scripts/check_context_budget.py+++ b/scripts/check_context_budget.py@@ -45,6 +45,29 @@ BUDGET_CHANGES = {LINE_BUDGET = {name: changes[-1][0] for name, changes in BUDGET_CHANGES.items()}+# 行数管不住实际体积:一行可以写两千字符,行数照样合规,而模型读到的是字节。+# 2026-09-03 就是这样撞上的——`docs/STATUS.md` 只有 226 行却有 155 KB,单行最长+# 一万三千多字符。所以入口文件另加字节预算和单行上限,三条都要过。+# 字节天花板同样只在官方或项目口径变化时才动。+MAX_EVER_BYTES = {+ "AGENTS.md": 16 * 1024,+ "docs/HANDOFF.md": 32 * 1024,+ "docs/STATUS.md": 16 * 1024,+}++# 字节预算的变更记录,语义与 BUDGET_CHANGES 完全一致:只能降,上调要写明理由。+BYTE_BUDGET_CHANGES = {+ "AGENTS.md": [(12 * 1024, "2026-09-03", "初始值")],+ "docs/HANDOFF.md": [(24 * 1024, "2026-09-03", "初始值")],+ "docs/STATUS.md": [(12 * 1024, "2026-09-03", "初始值")],+}++BYTE_BUDGET = {name: changes[-1][0] for name, changes in BYTE_BUDGET_CHANGES.items()}++# 单行上限。超过这个长度的段落在 diff、评审和上下文窗口里都读不动,+# 通常意味着该拆成几条或搬进 `docs/` 的分节。+MAX_LINE_CHARS = 400+SKILL_MAX_LINES = 120DESCRIPTION_MAX_CHARS = 200REVIEW_MAX_DAYS = 180@@ -78,25 +101,35 @@ def _skill_files(root: Path) -> list[Path]:return sorted(skills.glob("*/SKILL.md"))-def check_budget_history() -> list[str]:- """预算只能降不能升;上调必须留下理由;任何值都不得越过天花板。"""+def _check_history(+ changes_by_name: dict[str, list[tuple[int, str, str]]],+ ceilings: dict[str, int],+ unit: str,+) -> list[str]:problems: list[str] = []- for name, changes in BUDGET_CHANGES.items():+ for name, changes in changes_by_name.items():if not changes:- problems.append(f"{name}: 缺少预算变更记录")+ problems.append(f"{name}: 缺少{unit}预算变更记录")continue- ceiling = MAX_EVER.get(name)+ ceiling = ceilings.get(name)previous = Nonefor value, date, reason in changes:if ceiling is not None and value > ceiling:- problems.append(f"{name}: 预算 {value} 越过天花板 {ceiling}({date})")+ problems.append(f"{name}: {unit}预算 {value} 越过天花板 {ceiling}({date})")if previous is not None and value > previous:if not reason.strip() or reason.strip() == "初始值":problems.append(- f"{name}: {previous} → {value}({date})是上调,必须写明"+ f"{name}: {previous} → {value}({date})是上调({unit}预算),必须写明""这些内容为什么无法下沉到 docs/ 或技能")previous = value+ return problems+++def check_budget_history() -> list[str]:+ """预算只能降不能升;上调必须留下理由;任何值都不得越过天花板。"""+ problems = _check_history(BUDGET_CHANGES, MAX_EVER, "行数")+ problems += _check_history(BYTE_BUDGET_CHANGES, MAX_EVER_BYTES, "字节")if SKILL_MAX_LINES > MAX_EVER["SKILL.md"]:problems.append(@@ -122,6 +155,34 @@ def check_line_budgets(root: Path) -> list[str]:return problems+def check_byte_budgets(root: Path) -> list[str]:+ problems: list[str] = []+ for name, budget in BYTE_BUDGET.items():+ path = root / name+ if not path.is_file():+ problems.append(f"{name}: 文件缺失")+ continue+ size = len(path.read_bytes())+ if size > budget:+ problems.append(f"{name}: {size} 字节,超出预算 {budget} 字节")+ return problems+++def check_line_lengths(root: Path) -> list[str]:+ """入口文件的单行长度按字符数算,不按字节:中文一个字算一个。"""+ problems: list[str] = []+ for name in BYTE_BUDGET:+ path = root / name+ if not path.is_file():+ continue+ for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):+ if len(line) > MAX_LINE_CHARS:+ problems.append(+ f"{name}: 第 {number} 行 {len(line)} 字符,超出单行上限 {MAX_LINE_CHARS}"+ )+ return problems++def check_skills(root: Path) -> tuple[list[str], list[tuple[str, datetime.date]]]:problems: list[str] = []reviewed: list[tuple[str, datetime.date]] = []@@ -187,6 +248,8 @@ def stale_reviews(def check_repo(root: Path) -> tuple[list[str], list[tuple[str, datetime.date]]]:problems = check_budget_history()problems += check_line_budgets(root)+ problems += check_byte_budgets(root)+ problems += check_line_lengths(root)skill_problems, reviewed = check_skills(root)problems += skill_problemsproblems += check_skill_index(root)@@ -211,7 +274,8 @@ def main(argv: list[str] | None = None) -> int:return 1if stale:return 3- print(f"上下文预算检查通过:{len(reviewed)} 个技能,{len(LINE_BUDGET)} 个入口文件。")+ entries = set(LINE_BUDGET) | set(BYTE_BUDGET)+ print(f"上下文预算检查通过:{len(reviewed)} 个技能,{len(entries)} 个入口文件。")return 0diff --git a/tests/test_context_budget.py b/tests/test_context_budget.pyindex b0c79a8b2..3527ba38e 100644--- a/tests/test_context_budget.py+++ b/tests/test_context_budget.py@@ -47,6 +47,8 @@ class ContextBudgetTests(unittest.TestCase):self.assertLessEqual(self.checker.DESCRIPTION_MAX_CHARS, self.checker.MAX_EVER["description"])+ for name, budget in self.checker.BYTE_BUDGET.items():+ self.assertLessEqual(budget, self.checker.MAX_EVER_BYTES[name], name)def test_raising_a_budget_without_a_reason_is_reported(self):original = dict(self.checker.BUDGET_CHANGES)@@ -78,6 +80,55 @@ class ContextBudgetTests(unittest.TestCase):problems = self.checker.check_line_budgets(root)self.assertTrue(any("超出预算" in problem for problem in problems), problems)+ def test_raising_a_byte_budget_without_a_reason_is_reported(self):+ original = dict(self.checker.BYTE_BUDGET_CHANGES)+ try:+ self.checker.BYTE_BUDGET_CHANGES = {+ "AGENTS.md": [(8 * 1024, "2026-09-03", "初始值"), (12 * 1024, "2026-09-04", "")]+ }+ problems = self.checker.check_budget_history()+ finally:+ self.checker.BYTE_BUDGET_CHANGES = original+ self.assertTrue(any("字节预算" in problem for problem in problems), problems)++ def test_byte_budget_above_ceiling_is_reported(self):+ original = dict(self.checker.BYTE_BUDGET_CHANGES)+ try:+ self.checker.BYTE_BUDGET_CHANGES = {+ "AGENTS.md": [(64 * 1024, "2026-09-03", "初始值")]+ }+ problems = self.checker.check_budget_history()+ finally:+ self.checker.BYTE_BUDGET_CHANGES = original+ self.assertTrue(any("越过天花板" in problem for problem in problems), problems)++ def test_a_file_within_the_line_budget_can_still_be_over_the_byte_budget(self):+ """行数合规不代表体积合规:这正是 STATUS.md 155 KB 那次没被拦住的原因。"""+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ budget = self.checker.BYTE_BUDGET["AGENTS.md"]+ (root / "AGENTS.md").write_text("字" * budget, encoding="utf-8")+ self.assertEqual(+ self.checker.check_line_budgets(root),+ ["docs/HANDOFF.md: 文件缺失"],+ )+ problems = self.checker.check_byte_budgets(root)+ self.assertTrue(+ any("AGENTS.md" in problem and "字节" in problem for problem in problems),+ problems,+ )++ def test_over_long_line_is_reported(self):+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ (root / "AGENTS.md").write_text(+ "短行\n" + "长" * (self.checker.MAX_LINE_CHARS + 1) + "\n",+ encoding="utf-8",+ )+ problems = self.checker.check_line_lengths(root)+ self.assertEqual(len(problems), 1, problems)+ self.assertIn("第 2 行", problems[0])+def test_skill_frontmatter_and_review_line_are_required(self):with tempfile.TemporaryDirectory() as tmp:root = Path(tmp)