From 5b01a4abffb6c73d6e9d568075dca50b37f25375 Mon Sep 17 00:00:00 2001 From: Hsukqi Lee Date: Tue, 10 Nov 2026 19:09:05 +0800 Subject: [PATCH] fix: preserve paper URLs and normalize nested sender IDs Keep supplied URL evidence in verification queries and extract stable IDs from Feishu sender objects. Refs #3, #4. --- fastinsight/parse.py | 13 +++++++------ scripts/auto_pipeline.py | 10 +++++++--- tests/test_parse.py | 12 ++++++++++++ tests/test_pipeline.py | 15 +++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/fastinsight/parse.py b/fastinsight/parse.py index ced2d0e..2be6209 100644 --- a/fastinsight/parse.py +++ b/fastinsight/parse.py @@ -320,12 +320,13 @@ def cleaned_text(self) -> str: def _sender_id(sender: Dict[str, Any]) -> str: - sender_id = str(sender.get("id") or sender.get("sender_id") or "").strip() - if sender_id: - return sender_id - nested = sender.get("sender_id") - if isinstance(nested, dict): - return str(nested.get("open_id") or nested.get("user_id") or "").strip() + for value in (sender.get("id"), sender.get("sender_id")): + if isinstance(value, str) and value.strip(): + return value.strip() + if isinstance(value, dict): + nested = value.get("open_id") or value.get("user_id") + if isinstance(nested, str) and nested.strip(): + return nested.strip() return "" diff --git a/scripts/auto_pipeline.py b/scripts/auto_pipeline.py index e63cb7b..7f6d507 100644 --- a/scripts/auto_pipeline.py +++ b/scripts/auto_pipeline.py @@ -31,9 +31,13 @@ def _text(value): def _verification_query(paper, title): - """Combine identifiers with the best available title or content search query.""" + """Keep supplied locator evidence while adding the best search text.""" search_query = _text(paper.get("search_query")) - pieces = [search_query or _text(title)] + url = _text(paper.get("url")) + search_text = search_query or _text(title) + pieces = [url] + if search_text and search_text != url: + pieces.append(search_text) doi = _text(paper.get("doi")) arxiv_id = _text(paper.get("arxiv_id")) if doi: @@ -178,7 +182,7 @@ def enrich_paper(paper): search_query = _text(paper.get("search_query")) content_query = _text(paper.get("text"))[:500] paper = _normalize_paper(paper) - query = search_query or source_title or content_query + query = search_query or source_title or content_query or _text(paper.get("url")) if not query: return paper with tempfile.TemporaryDirectory(prefix="fastinsight-verify-") as temp_dir: diff --git a/tests/test_parse.py b/tests/test_parse.py index 3c4df51..6b64a4d 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -18,6 +18,18 @@ class ParseTests(unittest.TestCase): + def test_nested_sender_id_is_normalized_without_dict_repr(self) -> None: + parsed = parse_event( + { + "event": { + "sender": {"sender_id": {"open_id": "ou_nested", "user_id": "u_nested"}}, + "message": {"content": '{"text":"hello"}'}, + } + } + ) + self.assertEqual(parsed.sender_id, "ou_nested") + self.assertNotIn("{", parsed.sender_id) + def test_command_with_mention(self) -> None: parsed = parse_feishu_message( { diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 318b0cd..5703f60 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -58,6 +58,21 @@ def test_search_query_is_prioritized_for_verification(self) -> None: ) self.assertEqual(query, "FirmAgent firmware fuzzing") + def test_verification_query_keeps_existing_url(self) -> None: + query = _verification_query( + {"url": "https://arxiv.org/abs/1706.03762", "title": "A description"}, + "A description", + ) + self.assertEqual(query, "https://arxiv.org/abs/1706.03762 A description") + + def test_url_only_paper_reaches_verifier(self) -> None: + with patch("scripts.auto_pipeline._run_json", return_value=({}, 0)) as run: + from scripts.auto_pipeline import enrich_paper + + enrich_paper({"url": "https://example.test/paper"}) + command = run.call_args.args[0] + self.assertEqual(command[command.index("--query") + 1], "https://example.test/paper") + if __name__ == "__main__": unittest.main()