refactor(auto-fin): name notes after the topic, not the Agent title

The research Agent returned a whole paragraph as its title; that became a
filename and blew past the filesystem's 255-byte name limit, failing with
ENAMETOOLONG inside resolve_note_path. Topics are configured values, so
they are short and predictable - use them for file names and keep the
Agent title in frontmatter.

- Name topic notes after the topic and the digest after the run date
- Fold a byte budget into normalize_title as a safety net for long topics
- Take an AutoFinReportOutput in _write_report instead of loose fields
- Ask both prompts for a short title now that it is display-only

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
jinli.yl 2026-09-19 18:44:36 +08:00
parent 8f678c1f85
commit 6cb88683b4
8 changed files with 94 additions and 46 deletions

View file

@ -119,11 +119,14 @@ The plugin cron Job starts with the application and runs daily at 09:00 in the a
## Output
```text
.reme/daily/YYYY-MM-DD/<topic-note-title>.md # one per topic that had relevant news
.reme/daily/YYYY-MM-DD/<brief-title>.md # today's merged brief, linking back to every note
.reme/daily/YYYY-MM-DD/<topic>.md # one per topic that had relevant news
.reme/daily/YYYY-MM-DD/主题新闻观察(YYYY-MM-DD).md # today's merged brief, linking back to every note
```
Every file carries `kind` frontmatter (`auto-fin-topic` or `auto-fin-digest`), so a rerun finds and replaces the notes
File names come from the configured topics and the run date rather than the Agent's title: an Agent title is free text
and can outgrow the filesystem limit for one name component. Names are sanitized, byte-truncated, and disambiguated;
the Agent title is kept in the `title` frontmatter field. Every file carries `kind` frontmatter (`auto-fin-topic` or
`auto-fin-digest`), so a rerun finds and replaces the notes
it produced instead of duplicating them. Each file includes a title, description, current CLS evidence, historical
analysis, contextual wikilinks, and a fixed non-investment disclaimer; the brief ends with a `## 主题详解` list linking
to the topic notes. Network errors and invalid Agent output fail explicitly; no relevant current news is a successful

View file

@ -108,12 +108,13 @@ workspace 的 Markdown 目标。不存在、绝对路径、越界、带反斜杠
## 产物
```text
.reme/daily/YYYY-MM-DD/<主题笔记标题>.md # 每个有相关新闻的主题一份
.reme/daily/YYYY-MM-DD/<当日总览标题>.md # 合并总览,回链到每一份主题笔记
.reme/daily/YYYY-MM-DD/<主题>.md # 每个有相关新闻的主题一份
.reme/daily/YYYY-MM-DD/主题新闻观察(YYYY-MM-DD).md # 合并总览,回链到每一份主题笔记
```
文件名取自 Agent 生成的中文标题,并经过非法字符净化和重名消歧。每份文件都带 `kind` frontmatter(`auto-fin-topic` 或
`auto-fin-digest`),因此同日重跑会找回并覆盖自己产出的笔记,而不是重复生成。文件包含标题、说明、当前 CLS 证据、历史分析、上下文
文件名取自配置的主题(`topics`)和运行日期,而不是 Agent 生成的标题——Agent 标题是自由文本,可能长到超出文件名长度上限。文件名经过
非法字符净化、字节数截断和重名消歧;Agent 标题保留在 frontmatter 的 `title` 中。每份文件都带 `kind` frontmatter(`auto-fin-topic`
或 `auto-fin-digest`),因此同日重跑会找回并覆盖自己产出的笔记,而不是重复生成。文件包含标题、说明、当前 CLS 证据、历史分析、上下文
wikilink 和固定非投资建议声明;总览末尾另有一个 `## 主题详解` 列表,链接到各主题笔记。网络错误与无效 Agent 输出
会明确失败;没有相关当前新闻则成功跳过。

View file

@ -24,6 +24,7 @@ from .schema import AutoFinReportOutput
AGENT_INPUT_LOG_LIMIT = 2000
AGENT_OUTPUT_LOG_LIMIT = 4000
NOTE_CHAR_LIMIT = 30_000
TITLE_BYTE_LIMIT = 180
_WIKILINK = re.compile(r"\[\[([^\[\]\n]+)\]\]")
_HYBRID_WIKILINK = re.compile(r"(?P<wikilink>\[\[(?P<inner>[^\[\]\n]+)\]\])\((?P<destination>[^()\n]+)\)")
@ -73,17 +74,29 @@ def utc_now_iso() -> str:
def normalize_title(raw: str, fallback: str) -> str:
"""Return an Agent title as a safe Markdown filename stem."""
"""Return a topic or Agent label as a safe, filesystem-sized Markdown filename stem."""
title = _UNSAFE_FILENAME.sub("-", _HEADING.sub("", str(raw or "").strip()))
title = re.sub(r"\s+", " ", title).strip(" .-")
if title.lower().endswith(".md"):
title = title[:-3].strip(" .-")
title = title or fallback
title = _truncate(title or fallback).strip(" .-") or fallback
if error := validate_filename_component(title, kind="title"):
raise ValueError(f"Unable to produce a safe Auto Fin title from {raw!r}: {error}")
return title
def _truncate(title: str) -> str:
"""Fit a title into the byte budget one filename component accepts, without splitting a character."""
kept: list[str] = []
size = 0
for character in title:
size += len(character.encode())
if size > TITLE_BYTE_LIMIT:
break
kept.append(character)
return "".join(kept)
def normalize_report(output: AutoFinReportOutput) -> AutoFinReportOutput:
"""Strip a duplicated H1 and fill in the fallbacks for one Agent report."""
body = output.body.strip()
@ -230,36 +243,33 @@ class AutoFinStep(BaseStep):
async def _write_report(
self,
title: str,
description: str,
body: str,
filename: str,
output: AutoFinReportOutput,
*,
kind: str,
existing: Path | None = None,
trailer: str = "",
**metadata: Any,
) -> tuple[str, str, list[str]]:
"""Write one report note, returning its title, relative path, and valid sources."""
title, path = resolve_note_path(self.day_dir, title, existing=existing)
body, sources = validate_wikilinks(normalize_hybrid_wikilinks(body), self.workspace_path, path)
) -> tuple[str, list[str]]:
"""Write one report note, returning its workspace-relative path and valid sources."""
title, path = resolve_note_path(self.day_dir, filename, existing=existing)
body, sources = validate_wikilinks(normalize_hybrid_wikilinks(output.body), self.workspace_path, path)
await write_markdown(
path,
"\n\n".join(section for section in (body, trailer, DISCLAIMER) if section),
{
"name": title,
"title": title,
"description": description,
"title": output.title,
"description": output.description,
"kind": kind,
"generated_at": utc_now_iso(),
**metadata,
},
)
self._track(path, existing)
self.logger.info(
f"[{self.name}] wrote kind={kind} path={path.relative_to(self.workspace_path).as_posix()} "
f"chars={len(body)} sources={len(sources)}",
)
return title, path.relative_to(self.workspace_path).as_posix(), sources
relative = path.relative_to(self.workspace_path).as_posix()
self.logger.info(f"[{self.name}] wrote kind={kind} path={relative} chars={len(body)} sources={len(sources)}")
return relative, sources
async def _reply(
self,

View file

@ -32,17 +32,15 @@ class AutoFinDigestStep(AutoFinStep):
earlier_brief=read_note(earlier),
),
)
title, path, sources = await self._write_report(
normalize_title(output.title, f"主题新闻观察({run_date})"),
output.description,
output.body,
path, sources = await self._write_report(
normalize_title(f"主题新闻观察({run_date})", "主题新闻观察"),
output,
kind="auto-fin-digest",
existing=earlier,
trailer="## 主题详解\n\n" + "\n".join(f"- [[{note.path}]]" for note in notes),
date=run_date,
source_notes=[note.path for note in notes],
)
self.logger.info(f"[{self.name}] digest title={title!r} path={path}")
await refresh_day_index(
SimpleNamespace(workspace_path=self.workspace_path),
run_date,

View file

@ -17,4 +17,4 @@ digest_user: |
- 做提炼而不是摘抄,不要整段复制笔记原文,也不要给投资建议。
- 不要输出 wikilink,代码会在正文末尾自动附上各主题笔记的链接。
按结构化输出契约返回 title、description 和完整中文 Markdown body。
title 用不超过 20 字的短标题,description 用一句话概括;按结构化输出契约返回 title、description 和完整中文 Markdown body。

View file

@ -56,17 +56,16 @@ class AutoFinResearchStep(AutoFinStep):
finally:
if self.app_context is not None:
self.app_context.metadata.get("__search_call_budgets", {}).pop(context_id, None)
title, path, _ = await self._write_report(
normalize_title(output.title, f"{topic}观察"),
output.description,
output.body,
path, _ = await self._write_report(
normalize_title(topic, "主题观察"),
output,
kind="auto-fin-topic",
existing=earlier,
date=run_date,
topic=topic,
source_news_ids=[row["news_id"] for row in recent],
)
return AutoFinNote(topic=topic, title=title, description=output.description, body=output.body, path=path)
return AutoFinNote(topic=topic, title=output.title, description=output.description, body=output.body, path=path)
async def execute(self):
"""Write one note per topic that had relevant news."""

View file

@ -22,4 +22,4 @@ research_user: |
相关句子,例如:这与[[daily/2026-08-01/auto_fin.md|此前的供给判断]]相似,但本次政策范围不同。
不得写裸链接行或虚构路径。代码会把不存在、越界或指向本文的链接降级为普通文本。
按结构化输出契约返回 title、description 和完整中文 Markdown body。
title 用不超过 20 字的短标题,description 用一句话概括;按结构化输出契约返回 title、description 和完整中文 Markdown body。

View file

@ -325,9 +325,10 @@ async def test_research_writes_one_note_per_topic_with_latest_twenty_news(tmp_pa
}
day = tmp_path / "daily" / "2026-08-10"
assert sorted(path.name for path in day.glob("*.md")) == ["机器人观察.md", "黄金观察.md"]
note = (day / "黄金观察.md").read_text(encoding="utf-8")
assert sorted(path.name for path in day.glob("*.md")) == ["机器人.md", "黄金.md"]
note = (day / "黄金.md").read_text(encoding="utf-8")
assert "kind: auto-fin-topic" in note and "topic: 黄金" in note
assert "title: 黄金观察" in note
assert "[[daily/2026-08-01/auto_fin.md|历史黄金观察]]" in note
assert "](daily/2026-08-01/auto_fin.md)" not in note
assert "缺失文章" in note and "越界文章" in note
@ -335,17 +336,44 @@ async def test_research_writes_one_note_per_topic_with_latest_twenty_news(tmp_pa
assert "不提供收益、目标价或买卖建议" in note
assert [item.path for item in context["auto_fin_notes"]] == [
"daily/2026-08-10/黄金观察.md",
"daily/2026-08-10/机器人观察.md",
"daily/2026-08-10/黄金.md",
"daily/2026-08-10/机器人.md",
]
assert [item.title for item in context["auto_fin_notes"]] == ["黄金观察", "机器人观察"]
assert context["changes"] == [
{"change": "added", "path": "daily/2026-08-10/黄金观察.md"},
{"change": "added", "path": "daily/2026-08-10/机器人观察.md"},
{"change": "added", "path": "daily/2026-08-10/黄金.md"},
{"change": "added", "path": "daily/2026-08-10/机器人.md"},
]
assert response.metadata["selected_news_count"] == 26
assert response.metadata["note_paths"] == [item.path for item in context["auto_fin_notes"]]
@pytest.mark.asyncio
async def test_research_names_the_note_after_the_topic_not_the_agent_title(tmp_path: Path):
"""A whole-paragraph Agent title used to become a filename and fail with ENAMETOOLONG."""
class _VerboseAgent(BaseAgentWrapper):
async def reply(self, _prompt, **_kwargs):
return {
"structured_output": AutoFinReportOutput(
title="机器人主题 9-18 研究:" + "量产与政策" * 60,
description="关注政策变化。",
body=LINKED_BODY,
),
}
app_context = ApplicationContext(workspace_dir=str(tmp_path), timezone="Asia/Shanghai")
context = _context(
auto_fin_news_by_topic={"黄金": [], "机器人": [_news("1", "2026-08-10T09:00:00+08:00")], "半导体": []},
)
await AutoFinResearchStep(app_context=app_context, agent_wrapper=_VerboseAgent(app_context=app_context))(context)
day = tmp_path / "daily" / "2026-08-10"
assert [path.name for path in day.glob("*.md")] == ["机器人.md"]
assert "title: 机器人主题 9-18 研究:量产与政策" in (day / "机器人.md").read_text(encoding="utf-8")
@pytest.mark.asyncio
async def test_research_rerun_replaces_the_same_note_for_a_topic(tmp_path: Path):
app_context = ApplicationContext(workspace_dir=str(tmp_path), timezone="Asia/Shanghai")
@ -361,8 +389,8 @@ async def test_research_rerun_replaces_the_same_note_for_a_topic(tmp_path: Path)
assert len(agent.calls) == 2
assert "本次为当日首次生成。" in agent.calls[0][0]
assert "## 今日判断" in agent.calls[1][0]
assert second["changes"] == [{"change": "modified", "path": "daily/2026-08-10/黄金观察.md"}]
assert [path.name for path in (tmp_path / "daily" / "2026-08-10").glob("*.md")] == ["黄金观察.md"]
assert second["changes"] == [{"change": "modified", "path": "daily/2026-08-10/黄金.md"}]
assert [path.name for path in (tmp_path / "daily" / "2026-08-10").glob("*.md")] == ["黄金.md"]
@pytest.mark.asyncio
@ -403,16 +431,18 @@ async def test_digest_merges_notes_and_links_back_to_each_of_them(tmp_path: Path
assert "当前主题:" not in prompt
assert "本次为当日首次生成。" in prompt
digest = (tmp_path / "daily" / "2026-08-10" / "主题新闻观察.md").read_text(encoding="utf-8")
digest_path = "daily/2026-08-10/主题新闻观察(2026-08-10).md"
digest = (tmp_path / digest_path).read_text(encoding="utf-8")
assert "kind: auto-fin-digest" in digest
assert "title: 主题新闻观察" in digest
assert "## 主题详解" in digest
assert f"- [[{note_path}]]" in digest
assert "[[daily/2026-08-01/auto_fin.md|历史黄金观察]]" in digest
assert "缺失文章" in digest and "missing.md" not in digest
assert digest.rstrip().endswith("不提供收益、目标价或买卖建议。")
assert context["markdown_path"] == "daily/2026-08-10/主题新闻观察.md"
assert context["changes"][-1] == {"change": "added", "path": "daily/2026-08-10/主题新闻观察.md"}
assert context["markdown_path"] == digest_path
assert context["changes"][-1] == {"change": "added", "path": digest_path}
assert response.metadata["digest_path"] == context["markdown_path"]
assert response.metadata["source_paths"] == ["daily/2026-08-01/auto_fin.md"]
assert response.metadata["note_paths"] == [note_path]
@ -453,6 +483,13 @@ def test_normalize_title_sanitizes_agent_titles_and_keeps_notes_importable():
assert AutoFinNote(topic="黄金", title="标题", description="说明", body="正文", path="a.md").topic == "黄金"
def test_normalize_title_fits_the_filename_component_byte_budget():
"""A whole-paragraph title used to reach os.stat and fail with ENAMETOOLONG."""
assert normalize_title("长" * 200, "黄金观察") == "长" * 60
assert len(normalize_title("long" * 100, "黄金观察").encode()) <= 180
assert normalize_title(" / ", "黄金观察") == "黄金观察"
def test_plugin_config_has_default_topics_and_two_report_steps():
jobs = PLUGIN_MANIFEST["application_defaults"]["jobs"]
job = jobs["auto_fin"]