diff --git a/plugins/auto-fin/README.md b/plugins/auto-fin/README.md index 03688a9c..71a48169 100644 --- a/plugins/auto-fin/README.md +++ b/plugins/auto-fin/README.md @@ -129,7 +129,8 @@ the Agent title is kept in the `title` frontmatter field. Every file carries `ki `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 +to the topic notes. A fetch failure fails the run explicitly, and so does a run in which every topic fails; a single +failing topic only logs a warning and the remaining topics continue. No relevant current news is a successful skip. ## Validation diff --git a/plugins/auto-fin/README_ZH.md b/plugins/auto-fin/README_ZH.md index c9aeb946..02ce5f78 100644 --- a/plugins/auto-fin/README_ZH.md +++ b/plugins/auto-fin/README_ZH.md @@ -115,8 +115,8 @@ workspace 的 Markdown 目标。不存在、绝对路径、越界、带反斜杠 文件名取自配置的主题(`topics`)和运行日期,而不是 Agent 生成的标题——Agent 标题是自由文本,可能长到超出文件名长度上限。文件名经过 非法字符净化、字节数截断和重名消歧;Agent 标题保留在 frontmatter 的 `title` 中。每份文件都带 `kind` frontmatter(`auto-fin-topic` 或 `auto-fin-digest`),因此同日重跑会找回并覆盖自己产出的笔记,而不是重复生成。文件包含标题、说明、当前 CLS 证据、历史分析、上下文 -wikilink 和固定非投资建议声明;总览末尾另有一个 `## 主题详解` 列表,链接到各主题笔记。网络错误与无效 Agent 输出 -会明确失败;没有相关当前新闻则成功跳过。 +wikilink 和固定非投资建议声明;总览末尾另有一个 `## 主题详解` 列表,链接到各主题笔记。抓取失败与全部主题研究失败 +会明确失败;单个主题研究失败只记 warning 并继续其余主题;没有相关当前新闻则成功跳过。 ## 验证 diff --git a/plugins/auto-fin/src/reme_auto_fin/research.py b/plugins/auto-fin/src/reme_auto_fin/research.py index c4e1b5ae..a2bd6d32 100644 --- a/plugins/auto-fin/src/reme_auto_fin/research.py +++ b/plugins/auto-fin/src/reme_auto_fin/research.py @@ -68,7 +68,7 @@ class AutoFinResearchStep(AutoFinStep): 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.""" + """Write one note per topic that had relevant news, isolating per-topic failures.""" assert self.context is not None self.context["changes"] = [] if self.context.get("auto_fin_skipped"): @@ -76,16 +76,24 @@ class AutoFinResearchStep(AutoFinStep): run_date = str(self._required("auto_fin_date")) by_topic = self._required("auto_fin_news_by_topic") notes: list[AutoFinNote] = [] + failures: list[dict[str, str]] = [] for topic in self._required("auto_fin_topics"): if not by_topic[topic]: self.logger.info(f"[{self.name}] skipping topic={topic} reason=no_related_news") continue - notes.append(await self._research(topic, by_topic[topic], run_date)) + try: + notes.append(await self._research(topic, by_topic[topic], run_date)) + except Exception as exc: + failures.append({"topic": topic, "error": str(exc)}) + self.logger.warning(f"[{self.name}] topic={topic} failed; continuing with the rest: {exc}") + if failures and not notes: + raise RuntimeError(f"Auto Fin research failed for every topic: {failures}") self.context["auto_fin_notes"] = notes self.context.response.answer = f"Researched {len(notes)} topic(s): {', '.join(note.path for note in notes)}" self.context.response.metadata.update( { "note_paths": [note.path for note in notes], + "failed_topics": failures, "selected_news_count": len(self._required("auto_fin_selected_news")), }, ) diff --git a/plugins/auto-fin/tests/test_auto_fin.py b/plugins/auto-fin/tests/test_auto_fin.py index 46316b52..4b2eb207 100644 --- a/plugins/auto-fin/tests/test_auto_fin.py +++ b/plugins/auto-fin/tests/test_auto_fin.py @@ -411,6 +411,51 @@ async def test_research_skips_topics_without_news_and_honours_the_skip_flag(tmp_ assert len(agent.calls) == 0 +@pytest.mark.asyncio +async def test_research_keeps_the_other_topics_when_one_fails(tmp_path: Path): + """One broken topic must not discard the notes the other topics already produced.""" + + class _FlakyAgent(_ReportAgent): + async def reply(self, inputs, **kwargs): + if "当前主题:机器人" in str(inputs): + raise RuntimeError("agent exploded") + return await super().reply(inputs, **kwargs) + + app_context = ApplicationContext(workspace_dir=str(tmp_path), timezone="Asia/Shanghai") + agent = _FlakyAgent(app_context=app_context) + context = _context( + auto_fin_news_by_topic={ + "黄金": [_news("1", "2026-08-10T09:00:00+08:00")], + "机器人": [_news("2", "2026-08-10T09:05:00+08:00")], + "半导体": [], + }, + ) + + response = await AutoFinResearchStep(app_context=app_context, agent_wrapper=agent)(context) + + assert [item.path for item in context["auto_fin_notes"]] == ["daily/2026-08-10/黄金.md"] + assert response.success is True + assert response.metadata["failed_topics"] == [{"topic": "机器人", "error": "agent exploded"}] + assert [path.name for path in (tmp_path / "daily" / "2026-08-10").glob("*.md")] == ["黄金.md"] + + +@pytest.mark.asyncio +async def test_research_fails_the_run_when_every_topic_fails(tmp_path: Path): + """A run that produced no note at all must fail instead of sending an empty brief.""" + + class _DeadAgent(BaseAgentWrapper): + async def reply(self, *_args, **_kwargs): + raise RuntimeError("agent exploded") + + 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")], "机器人": [], "半导体": []}, + ) + + with pytest.raises(RuntimeError, match="failed for every topic"): + await AutoFinResearchStep(app_context=app_context, agent_wrapper=_DeadAgent(app_context=app_context))(context) + + @pytest.mark.asyncio async def test_digest_merges_notes_and_links_back_to_each_of_them(tmp_path: Path): _history(tmp_path) diff --git a/reme/steps/file_io/frontmatter_read.py b/reme/steps/file_io/frontmatter_read.py index 0b127986..fea6312d 100644 --- a/reme/steps/file_io/frontmatter_read.py +++ b/reme/steps/file_io/frontmatter_read.py @@ -13,7 +13,7 @@ from pathlib import Path import frontmatter import yaml -from ._path import display_path, gate_md, resolve_path +from ._path import _check_path_permission, display_path, gate_md, resolve_path from ..base_step import BaseStep from ...components import R @@ -41,6 +41,14 @@ class FrontmatterReadStep(BaseStep): if target != original_target: resolved["resolved_path"] = display_path(workspace_dir, target) probed = resolved.get("resolved_path", path) + if not _check_path_permission(workspace_dir, target, self.context.get("_allowed_paths")): + self.context.response.success = False + self.context.response.answer = "Error: no permission to access this file" + self.context.response.metadata.update( + {"path": path, "error": "no permission to access this file", **resolved}, + ) + self.logger.info(f"[{self.name}] path={path} error=no_permission") + return if not target.is_file(): self.context.response.success = False self.context.response.answer = f"Error: {probed} not found" diff --git a/tests/unit/test_frontmatter_steps.py b/tests/unit/test_frontmatter_steps.py index 51a49f34..f95504d8 100644 --- a/tests/unit/test_frontmatter_steps.py +++ b/tests/unit/test_frontmatter_steps.py @@ -76,6 +76,23 @@ async def test_read_no_suffix_autoappends_md(): await store.close() +@pytest.mark.asyncio +async def test_read_honors_injected_path_scope(): + """An injected allowed-path scope denies frontmatter reads outside of it.""" + with tempfile.TemporaryDirectory() as tmp, temp_chdir(tmp): + _seed(Path(tmp), NOTE, BODY) + store = await _make_store() + + allowed = await _run(FrontmatterReadStep, store, path=NOTE, _allowed_paths=[NOTE]) + assert allowed.success is True + assert allowed.metadata["frontmatter"] == {"name": "n", "tags": ["a"]} + + denied = await _run(FrontmatterReadStep, store, path=NOTE, _allowed_paths=["notes/other.md"]) + assert denied.success is False + assert denied.metadata["error"] == "no permission to access this file" + await store.close() + + @pytest.mark.asyncio async def test_update_no_suffix_autoappends_md(): """frontmatter_update on a suffix-less path resolves to the ``.md`` file."""