diff --git a/plugins/auto-fin/README.md b/plugins/auto-fin/README.md index 5f007478..03688a9c 100644 --- a/plugins/auto-fin/README.md +++ b/plugins/auto-fin/README.md @@ -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/.md # one per topic that had relevant news -.reme/daily/YYYY-MM-DD/.md # today's merged brief, linking back to every note +.reme/daily/YYYY-MM-DD/.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 diff --git a/plugins/auto-fin/README_ZH.md b/plugins/auto-fin/README_ZH.md index 2ebf9348..c9aeb946 100644 --- a/plugins/auto-fin/README_ZH.md +++ b/plugins/auto-fin/README_ZH.md @@ -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 输出 会明确失败;没有相关当前新闻则成功跳过。 diff --git a/plugins/auto-fin/src/reme_auto_fin/base.py b/plugins/auto-fin/src/reme_auto_fin/base.py index 928f2c81..4e2de978 100644 --- a/plugins/auto-fin/src/reme_auto_fin/base.py +++ b/plugins/auto-fin/src/reme_auto_fin/base.py @@ -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\[\[(?P[^\[\]\n]+)\]\])\((?P[^()\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, diff --git a/plugins/auto-fin/src/reme_auto_fin/digest.py b/plugins/auto-fin/src/reme_auto_fin/digest.py index 2971461f..9c9a5743 100644 --- a/plugins/auto-fin/src/reme_auto_fin/digest.py +++ b/plugins/auto-fin/src/reme_auto_fin/digest.py @@ -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, diff --git a/plugins/auto-fin/src/reme_auto_fin/digest.yaml b/plugins/auto-fin/src/reme_auto_fin/digest.yaml index bbc39b4e..afec4f90 100644 --- a/plugins/auto-fin/src/reme_auto_fin/digest.yaml +++ b/plugins/auto-fin/src/reme_auto_fin/digest.yaml @@ -17,4 +17,4 @@ digest_user: | - 做提炼而不是摘抄,不要整段复制笔记原文,也不要给投资建议。 - 不要输出 wikilink,代码会在正文末尾自动附上各主题笔记的链接。 - 按结构化输出契约返回 title、description 和完整中文 Markdown body。 \ No newline at end of file + title 用不超过 20 字的短标题,description 用一句话概括;按结构化输出契约返回 title、description 和完整中文 Markdown body。 \ No newline at end of file diff --git a/plugins/auto-fin/src/reme_auto_fin/research.py b/plugins/auto-fin/src/reme_auto_fin/research.py index 41b85e24..c4e1b5ae 100644 --- a/plugins/auto-fin/src/reme_auto_fin/research.py +++ b/plugins/auto-fin/src/reme_auto_fin/research.py @@ -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.""" diff --git a/plugins/auto-fin/src/reme_auto_fin/research.yaml b/plugins/auto-fin/src/reme_auto_fin/research.yaml index 125bec0e..c06394aa 100644 --- a/plugins/auto-fin/src/reme_auto_fin/research.yaml +++ b/plugins/auto-fin/src/reme_auto_fin/research.yaml @@ -22,4 +22,4 @@ research_user: | 相关句子,例如:这与[[daily/2026-08-01/auto_fin.md|此前的供给判断]]相似,但本次政策范围不同。 不得写裸链接行或虚构路径。代码会把不存在、越界或指向本文的链接降级为普通文本。 - 按结构化输出契约返回 title、description 和完整中文 Markdown body。 \ No newline at end of file + title 用不超过 20 字的短标题,description 用一句话概括;按结构化输出契约返回 title、description 和完整中文 Markdown body。 \ No newline at end of file diff --git a/plugins/auto-fin/tests/test_auto_fin.py b/plugins/auto-fin/tests/test_auto_fin.py index 5cbb4eda..46316b52 100644 --- a/plugins/auto-fin/tests/test_auto_fin.py +++ b/plugins/auto-fin/tests/test_auto_fin.py @@ -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"]