feat: 优化日报总结与延续性跟踪
This commit is contained in:
@ -14,6 +14,10 @@ from snooper_summary import (
|
||||
MagpieClient,
|
||||
SummaryError,
|
||||
chunk_messages,
|
||||
escape_obsidian_tags,
|
||||
format_followup_checkboxes,
|
||||
load_previous_summary,
|
||||
previous_workday,
|
||||
require_efund_model,
|
||||
resolve_day,
|
||||
summarize_messages,
|
||||
@ -22,6 +26,7 @@ from snooper_summary import (
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
|
||||
def __init__(self, data, status_code=200):
|
||||
self.data = data
|
||||
self.status_code = status_code
|
||||
@ -31,6 +36,7 @@ class FakeResponse:
|
||||
|
||||
|
||||
class FakeSession:
|
||||
|
||||
def __init__(self, models=None, completions=None):
|
||||
self.models = models if models is not None else [DEFAULT_MODEL]
|
||||
self.completions = list(completions or [])
|
||||
@ -46,9 +52,14 @@ class FakeSession:
|
||||
if not self.completions:
|
||||
return FakeResponse({"model": DEFAULT_MODEL, "choices": []})
|
||||
model, content = self.completions.pop(0)
|
||||
return FakeResponse(
|
||||
{"model": model, "choices": [{"message": {"content": content}}]}
|
||||
)
|
||||
return FakeResponse({
|
||||
"model": model,
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": content
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
|
||||
def synthetic_row(
|
||||
@ -58,6 +69,8 @@ def synthetic_row(
|
||||
chat_id="chat-test",
|
||||
chat_name="合成群",
|
||||
chat_type=2,
|
||||
direction="in",
|
||||
sender_id="sender-test",
|
||||
):
|
||||
return SimpleNamespace(
|
||||
msg_id=msg_id,
|
||||
@ -65,10 +78,10 @@ def synthetic_row(
|
||||
chat_id=chat_id,
|
||||
chat_name=chat_name,
|
||||
chat_type=chat_type,
|
||||
sender_id="sender-test",
|
||||
sender_id=sender_id,
|
||||
sender_name="合成用户",
|
||||
msg_type_name="TEXT",
|
||||
direction="in",
|
||||
direction=direction,
|
||||
content=content,
|
||||
)
|
||||
|
||||
@ -79,13 +92,13 @@ def test_model_policy_checks_actual_model_after_optional_routing_prefix():
|
||||
routed_model = "maqqie/gg-chat/EFundGPT-dsv4pro"
|
||||
assert require_efund_model(routed_model) == routed_model
|
||||
for model in (
|
||||
"",
|
||||
"deepseek-v4-pro",
|
||||
"efundgpt-dsv4-pro",
|
||||
"maqqie/gg-chat/deepseek-v4-pro",
|
||||
"maqqie/gg-chat/efundgpt-dsv4-pro",
|
||||
"maqqie/gg-chat/",
|
||||
" EFundGPT-dsv4-pro",
|
||||
"",
|
||||
"deepseek-v4-pro",
|
||||
"efundgpt-dsv4-pro",
|
||||
"maqqie/gg-chat/deepseek-v4-pro",
|
||||
"maqqie/gg-chat/efundgpt-dsv4-pro",
|
||||
"maqqie/gg-chat/",
|
||||
" EFundGPT-dsv4-pro",
|
||||
):
|
||||
with pytest.raises(SummaryError):
|
||||
require_efund_model(model)
|
||||
@ -108,7 +121,9 @@ def test_non_efund_response_is_not_returned():
|
||||
|
||||
|
||||
def test_http_error_does_not_expose_response_body():
|
||||
|
||||
class ErrorSession(FakeSession):
|
||||
|
||||
def post(self, url, **kwargs):
|
||||
return FakeResponse({"echo": "敏感合成正文"}, status_code=400)
|
||||
|
||||
@ -120,56 +135,61 @@ def test_http_error_does_not_expose_response_body():
|
||||
|
||||
def test_chunking_preserves_every_character_and_respects_limit():
|
||||
original = "甲" * 4700
|
||||
rows = [synthetic_row(content=original), synthetic_row("m2", content="第二条")]
|
||||
messages = [
|
||||
{
|
||||
"msg_id": row.msg_id,
|
||||
"time": "2026-11-03T10:00:00+08:00",
|
||||
"chat_id": row.chat_id,
|
||||
"content": row.content,
|
||||
}
|
||||
for row in rows
|
||||
rows = [
|
||||
synthetic_row(content=original),
|
||||
synthetic_row("m2", content="第二条")
|
||||
]
|
||||
messages = [{
|
||||
"msg_id": row.msg_id,
|
||||
"time": "2026-11-03T10:00:00+08:00",
|
||||
"chat_id": row.chat_id,
|
||||
"content": row.content,
|
||||
} for row in rows]
|
||||
chunks = chunk_messages(messages, max_chars=2000)
|
||||
assert len(chunks) > 1
|
||||
flattened = [message for chunk in chunks for message in chunk]
|
||||
assert "".join(
|
||||
message["content"] for message in flattened if message["msg_id"] == "m1"
|
||||
) == original
|
||||
assert [message["content"] for message in flattened if message["msg_id"] == "m2"] == [
|
||||
"第二条"
|
||||
]
|
||||
assert "".join(message["content"] for message in flattened
|
||||
if message["msg_id"] == "m1") == original
|
||||
assert [
|
||||
message["content"] for message in flattened
|
||||
if message["msg_id"] == "m2"
|
||||
] == ["第二条"]
|
||||
for chunk in chunks:
|
||||
assert sum(len(json.dumps(item, ensure_ascii=False)) for item in chunk) < 2300
|
||||
assert sum(
|
||||
len(json.dumps(item, ensure_ascii=False)) for item in chunk) < 2300
|
||||
|
||||
|
||||
def test_multi_chunk_summary_uses_same_approved_model_for_all_calls():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "第一部分"),
|
||||
(DEFAULT_MODEL, "第二部分"),
|
||||
(DEFAULT_MODEL, "单对话摘要"),
|
||||
(DEFAULT_MODEL, "最终总结"),
|
||||
]
|
||||
)
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "第一部分"),
|
||||
(DEFAULT_MODEL, "第二部分"),
|
||||
(DEFAULT_MODEL, "单对话摘要"),
|
||||
(DEFAULT_MODEL, "最终总结"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [synthetic_row(content="一" * 1200), synthetic_row("m2", content="二" * 1200)]
|
||||
rows = [
|
||||
synthetic_row(content="一" * 1200),
|
||||
synthetic_row("m2", content="二" * 1200)
|
||||
]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
result = summarize_messages(client, rows, date(2026, 11, 3), tz, max_chars=2000)
|
||||
result = summarize_messages(client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
max_chars=2000)
|
||||
assert result == "最终总结"
|
||||
assert len(session.post_calls) == 4
|
||||
for _, kwargs in session.post_calls:
|
||||
assert kwargs["json"]["model"] == DEFAULT_MODEL
|
||||
assert kwargs["json"]["reasoning_effort"] == "high"
|
||||
|
||||
|
||||
def test_summary_is_per_conversation_before_daily_combination():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "群聊摘要"),
|
||||
(DEFAULT_MODEL, "私聊摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
]
|
||||
)
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "私聊摘要"),
|
||||
(DEFAULT_MODEL, "群聊摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row(
|
||||
@ -197,10 +217,10 @@ def test_summary_is_per_conversation_before_daily_combination():
|
||||
kwargs["json"]["messages"][1]["content"]
|
||||
for _, kwargs in session.post_calls
|
||||
]
|
||||
assert "群聊原始内容" in prompts[0]
|
||||
assert "私聊原始内容" not in prompts[0]
|
||||
assert "私聊原始内容" in prompts[1]
|
||||
assert "群聊原始内容" not in prompts[1]
|
||||
assert "私聊原始内容" in prompts[0]
|
||||
assert "群聊原始内容" not in prompts[0]
|
||||
assert "群聊原始内容" in prompts[1]
|
||||
assert "私聊原始内容" not in prompts[1]
|
||||
assert "群聊摘要" in prompts[2]
|
||||
assert "私聊摘要" in prompts[2]
|
||||
assert "群聊原始内容" not in prompts[2]
|
||||
@ -208,13 +228,11 @@ def test_summary_is_per_conversation_before_daily_combination():
|
||||
|
||||
|
||||
def test_summary_applies_chat_specific_prompts_to_each_relevant_stage():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "业务监控摘要"),
|
||||
(DEFAULT_MODEL, "普通群摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
]
|
||||
)
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "业务监控摘要"),
|
||||
(DEFAULT_MODEL, "普通群摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row(chat_id="monitor", chat_name="监控群"),
|
||||
@ -241,14 +259,264 @@ def test_summary_applies_chat_specific_prompts_to_each_relevant_stage():
|
||||
assert custom_prompt in prompts[2]
|
||||
|
||||
|
||||
def test_summary_enriches_names_and_requests_obsidian_wikilinks():
|
||||
def test_summary_prioritizes_discussions_with_current_user_messages():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "重点讨论摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row("incoming", content="同事提出的问题", chat_id="group-a"),
|
||||
synthetic_row(
|
||||
"outgoing",
|
||||
content="我的判断和回复",
|
||||
chat_id="group-a",
|
||||
direction="out",
|
||||
),
|
||||
]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(client, rows, date(2026, 11, 3), tz)
|
||||
|
||||
assert result == "最终日度总结"
|
||||
first_prompt = session.post_calls[0][1]["json"]["messages"][1]["content"]
|
||||
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert "我的判断和回复" in first_prompt
|
||||
assert '"direction": "out"' in first_prompt
|
||||
assert '"user_spoke": true' in first_prompt
|
||||
assert "重点总结当前用户参与的讨论" in first_prompt
|
||||
assert '"user_spoke": true' in final_prompt
|
||||
assert "优先保留当前用户参与讨论" in final_prompt
|
||||
|
||||
|
||||
def test_summary_orders_chats_by_participation_private_human_and_bot():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "本人参与群摘要"),
|
||||
(DEFAULT_MODEL, "私聊摘要"),
|
||||
(DEFAULT_MODEL, "真人群摘要"),
|
||||
(DEFAULT_MODEL, "机器人群摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row("bot-message",
|
||||
chat_id="bot-group",
|
||||
chat_name="机器人群",
|
||||
sender_id="bot"),
|
||||
synthetic_row(
|
||||
"human-message",
|
||||
chat_id="human-group",
|
||||
chat_name="真人群",
|
||||
sender_id="human",
|
||||
),
|
||||
synthetic_row(
|
||||
"private-message",
|
||||
chat_id="private-chat",
|
||||
chat_name="私聊",
|
||||
chat_type=1,
|
||||
sender_id="human",
|
||||
),
|
||||
synthetic_row(
|
||||
"own-message",
|
||||
chat_id="own-group",
|
||||
chat_name="本人参与群",
|
||||
direction="out",
|
||||
sender_id="human",
|
||||
),
|
||||
]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
sender_types={
|
||||
"human": 1,
|
||||
"bot": 2
|
||||
},
|
||||
)
|
||||
|
||||
assert result == "最终日度总结"
|
||||
prompts = [
|
||||
kwargs["json"]["messages"][1]["content"]
|
||||
for _, kwargs in session.post_calls
|
||||
]
|
||||
assert '"chat_id": "own-group"' in prompts[0]
|
||||
assert '"priority_rank": 1' in prompts[0]
|
||||
assert '"chat_id": "private-chat"' in prompts[1]
|
||||
assert '"priority_rank": 2' in prompts[1]
|
||||
assert '"chat_id": "human-group"' in prompts[2]
|
||||
assert '"priority_rank": 3' in prompts[2]
|
||||
assert '"has_human_messages": true' in prompts[2]
|
||||
assert '"chat_id": "bot-group"' in prompts[3]
|
||||
assert '"priority_rank": 4' in prompts[3]
|
||||
assert '"has_bot_messages": true' in prompts[3]
|
||||
final_prompt = prompts[4]
|
||||
assert "## 七、运维信息(必须位于全文最后)" in final_prompt
|
||||
assert "## 三、前日工作延续" in final_prompt
|
||||
assert "三级标题保持简短" in final_prompt
|
||||
assert "> [!note]" in final_prompt
|
||||
assert "> [!warning]" in final_prompt
|
||||
assert "仅知情、他人负责、一般通知和机器人监控无需跟进" in final_prompt
|
||||
assert final_prompt.index("本人参与群摘要") < final_prompt.index("私聊摘要")
|
||||
assert final_prompt.index("私聊摘要") < final_prompt.index("真人群摘要")
|
||||
assert final_prompt.index("真人群摘要") < final_prompt.index("机器人群摘要")
|
||||
|
||||
|
||||
def test_private_chat_labels_keep_only_the_other_party():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "张三确认了事项"),
|
||||
(DEFAULT_MODEL, "# 最终总结\n张三确认了事项"),
|
||||
(DEFAULT_MODEL, "排班([[王五]])\n模型([[王五]]、[[赵六]])"),
|
||||
(DEFAULT_MODEL, "| 事项 | 负责人 | 来源 |\n| 排班 | 未明确 | [[王五]] |"),
|
||||
]
|
||||
)
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row(
|
||||
"incoming",
|
||||
chat_id="private-chat",
|
||||
chat_name="赵六",
|
||||
chat_type=1,
|
||||
sender_id="other",
|
||||
),
|
||||
synthetic_row(
|
||||
"outgoing",
|
||||
chat_id="private-chat",
|
||||
chat_name="赵六",
|
||||
chat_type=1,
|
||||
direction="out",
|
||||
sender_id="me",
|
||||
),
|
||||
]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={"private-chat": {"me": "王五", "other": "赵六"}},
|
||||
)
|
||||
|
||||
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert "排班([[赵六]])" in final_prompt
|
||||
assert "模型([[赵六]])" in final_prompt
|
||||
assert "私聊:" not in final_prompt
|
||||
assert "[[王五]] 与 [[赵六]]" not in final_prompt
|
||||
assert result.endswith("| 排班 | 未明确 | [[赵六]] |")
|
||||
|
||||
|
||||
def test_multiple_private_sources_are_repaired_from_conversation_summaries():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "[[王五]] 私聊:数据库迁移"),
|
||||
(DEFAULT_MODEL, "[[王五]] 私聊:模型选型"),
|
||||
(
|
||||
DEFAULT_MODEL,
|
||||
"| 事项 | 来源 |\n| 数据库迁移 | [[王五]] |\n| 模型选型 | [[王五]] |",
|
||||
),
|
||||
(
|
||||
DEFAULT_MODEL,
|
||||
"| 事项 | 来源 |\n| 数据库迁移 | [[周八]] |\n| 模型选型 | [[孙七]] |",
|
||||
),
|
||||
]
|
||||
)
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = []
|
||||
for chat_id, other, topic in (
|
||||
("private-a", "周八", "数据库"),
|
||||
("private-b", "孙七", "模型"),
|
||||
):
|
||||
rows.append(
|
||||
synthetic_row(
|
||||
f"out-{topic}",
|
||||
content=topic,
|
||||
chat_id=chat_id,
|
||||
chat_name=other,
|
||||
chat_type=1,
|
||||
direction="out",
|
||||
sender_id="me",
|
||||
)
|
||||
)
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={
|
||||
"private-a": {"me": "王五", "other": "周八"},
|
||||
"private-b": {"me": "王五", "other": "孙七"},
|
||||
},
|
||||
)
|
||||
|
||||
assert len(session.post_calls) == 4
|
||||
assert "| 数据库迁移 | [[周八]] |" in result
|
||||
assert "| 模型选型 | [[孙七]] |" in result
|
||||
|
||||
|
||||
def test_person_named_group_links_support_commas_and_nested_wikilinks():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "[[吴九, [[李四]], 王五]]:讨论需求"),
|
||||
(DEFAULT_MODEL, "[[吴九, [[李四]], 王五]]:已确认需求"),
|
||||
]
|
||||
)
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
group_name = "吴九, 李四, 王五"
|
||||
rows = [synthetic_row(chat_id="group-a", chat_name=group_name)]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={
|
||||
"group-a": {
|
||||
"u1": "吴九",
|
||||
"u2": "李四",
|
||||
"u3": "王五",
|
||||
"sender-test": "李四",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert result == "吴九, 李四, 王五:已确认需求"
|
||||
assert "[[吴九, [[李四]], 王五]]" not in result
|
||||
|
||||
|
||||
def test_previous_day_summary_is_used_for_continuity():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "今日摘要"),
|
||||
(DEFAULT_MODEL, "最终日度总结"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
[synthetic_row()],
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
previous_summary="# 2026-11-02\n\n前日合成待办",
|
||||
)
|
||||
|
||||
assert result == "最终日度总结"
|
||||
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert "前日合成待办" in final_prompt
|
||||
assert "今日继续推进" in final_prompt
|
||||
assert "今日未见延续" in final_prompt
|
||||
|
||||
|
||||
def test_summary_enriches_names_and_requests_obsidian_wikilinks():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "张三确认了事项"),
|
||||
(DEFAULT_MODEL, "# 最终总结\n张三确认了事项"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row(
|
||||
content="三确认一下",
|
||||
@ -264,7 +532,9 @@ def test_summary_enriches_names_and_requests_obsidian_wikilinks():
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={"group-a": {"sender-test": "张三"}},
|
||||
contacts={"group-a": {
|
||||
"sender-test": "张三"
|
||||
}},
|
||||
)
|
||||
|
||||
assert result.startswith("# 最终总结")
|
||||
@ -281,13 +551,45 @@ def test_summary_enriches_names_and_requests_obsidian_wikilinks():
|
||||
assert "Wikilink" in final_prompt
|
||||
|
||||
|
||||
def test_summary_normalizes_people_aliases_after_both_model_stages():
|
||||
session = FakeSession(
|
||||
completions=[
|
||||
(DEFAULT_MODEL, "伦总发送了文件"),
|
||||
(DEFAULT_MODEL, "[[伦总]] 最终确认"),
|
||||
]
|
||||
def test_person_only_group_name_stays_plain_but_people_keep_wikilinks():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "[[张三、李四]] 中 [[张三]] 确认了事项"),
|
||||
(DEFAULT_MODEL, "来源:[[张三]]、[[李四]];[[张三]] 已确认"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [
|
||||
synthetic_row(
|
||||
chat_id="group-a",
|
||||
chat_name="张三、李四",
|
||||
chat_type=2,
|
||||
)
|
||||
]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={
|
||||
"group-a": {
|
||||
"sender-a": "张三",
|
||||
"sender-b": "李四"
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
chat_summary = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert "[[张三、李四]]" not in chat_summary
|
||||
assert "张三、李四 中 [[张三]] 确认了事项" in chat_summary
|
||||
assert result == "来源:张三、李四;[[张三]] 已确认"
|
||||
|
||||
|
||||
def test_summary_normalizes_people_aliases_after_both_model_stages():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "四总发送了文件"),
|
||||
(DEFAULT_MODEL, "[[四总]] 最终确认"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [synthetic_row(chat_id="group-a", chat_name="项目群")]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
@ -297,14 +599,40 @@ def test_summary_normalizes_people_aliases_after_both_model_stages():
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={"group-a": {"sender-test": "万轶伦"}},
|
||||
people_aliases={"伦总": "万轶伦"},
|
||||
contacts={"group-a": {
|
||||
"sender-test": "李四"
|
||||
}},
|
||||
people_aliases={"四总": "李四"},
|
||||
)
|
||||
|
||||
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert "[[万轶伦]]发送了文件" in final_prompt
|
||||
assert "伦总" not in final_prompt
|
||||
assert result == "[[万轶伦]] 最终确认"
|
||||
assert "[[李四]]发送了文件" in final_prompt
|
||||
assert "四总" not in final_prompt
|
||||
assert result == "[[李四]] 最终确认"
|
||||
|
||||
|
||||
def test_unconfirmed_person_wikilink_is_removed_but_group_link_is_preserved():
|
||||
session = FakeSession(completions=[
|
||||
(DEFAULT_MODEL, "[[张四]] 在 [[项目群]] 发送文件"),
|
||||
(DEFAULT_MODEL, "[[张四]] 在 [[项目群]] 已确认"),
|
||||
])
|
||||
client = MagpieClient(DEFAULT_MODEL, session=session)
|
||||
rows = [synthetic_row(chat_id="group-a", chat_name="项目群")]
|
||||
_, _, _, tz = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
|
||||
result = summarize_messages(
|
||||
client,
|
||||
rows,
|
||||
date(2026, 11, 3),
|
||||
tz,
|
||||
contacts={"group-a": {
|
||||
"sender-test": "张三"
|
||||
}},
|
||||
)
|
||||
|
||||
assert result == "张四 在 [[项目群]] 已确认"
|
||||
assert "[[张四]]" not in result
|
||||
assert "[[项目群]]" in result
|
||||
|
||||
|
||||
def test_storage_selects_only_received_messages_inside_day_and_chat():
|
||||
@ -320,20 +648,42 @@ def test_storage_selects_only_received_messages_inside_day_and_chat():
|
||||
"msg_type_name": "TEXT",
|
||||
"create_time": start + 100,
|
||||
}
|
||||
storage.save_message({**base, "msg_id": "in-day", "content": "合成收到"}, direction="in")
|
||||
storage.save_message({**base, "msg_id": "out-day", "content": "合成发出"}, direction="out")
|
||||
storage.save_message({
|
||||
**base, "msg_id": "in-day",
|
||||
"content": "合成收到"
|
||||
},
|
||||
direction="in")
|
||||
storage.save_message({
|
||||
**base, "msg_id": "out-day",
|
||||
"content": "合成发出"
|
||||
},
|
||||
direction="out")
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "in-other-chat", "chat_id": "chat-b", "content": "其他会话"},
|
||||
{
|
||||
**base, "msg_id": "in-other-chat",
|
||||
"chat_id": "chat-b",
|
||||
"content": "其他会话"
|
||||
},
|
||||
direction="in",
|
||||
)
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "before-day", "create_time": start - 1}, direction="in"
|
||||
)
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "at-end", "create_time": end}, direction="in"
|
||||
)
|
||||
{
|
||||
**base, "msg_id": "before-day",
|
||||
"create_time": start - 1
|
||||
},
|
||||
direction="in")
|
||||
storage.save_message({
|
||||
**base, "msg_id": "at-end",
|
||||
"create_time": end
|
||||
},
|
||||
direction="in")
|
||||
rows = storage.get_received_messages(start, end, chat_ids=["chat-a"])
|
||||
assert [row.msg_id for row in rows] == ["in-day"]
|
||||
rows = storage.get_received_messages(start,
|
||||
end,
|
||||
chat_ids=["chat-a"],
|
||||
include_outgoing=True)
|
||||
assert [row.msg_id for row in rows] == ["in-day", "out-day"]
|
||||
|
||||
|
||||
def test_yaml_config_resolves_dated_output_paths(tmp_path):
|
||||
@ -349,12 +699,78 @@ def test_yaml_config_resolves_dated_output_paths(tmp_path):
|
||||
|
||||
config = load_output_config(config_file)
|
||||
target_day = date(2026, 11, 3)
|
||||
assert configured_output_path(config, "summary", target_day) == (
|
||||
tmp_path / "summaries" / "2026-11-03.md"
|
||||
)
|
||||
assert configured_output_path(config, "dry_run", target_day) == (
|
||||
tmp_path / "dry-runs" / "2026-11-03.json"
|
||||
)
|
||||
assert configured_output_path(config, "summary",
|
||||
target_day) == (tmp_path / "summaries" /
|
||||
"2026-11-03.md")
|
||||
assert configured_output_path(config, "dry_run",
|
||||
target_day) == (tmp_path / "dry-runs" /
|
||||
"2026-11-03.json")
|
||||
|
||||
|
||||
def test_monday_loads_sunday_and_previous_workday(tmp_path):
|
||||
config = {"summary": str(tmp_path / "{date}.md")}
|
||||
(tmp_path / "2026-11-01.md").write_text("周日合成日志", encoding="utf-8")
|
||||
(tmp_path / "2026-10-30.md").write_text("周五合成日志", encoding="utf-8")
|
||||
|
||||
content, previous_missing = load_previous_summary(config, date(2026, 11, 2))
|
||||
|
||||
assert not previous_missing
|
||||
assert "【周日 2026-11-01】\n周日合成日志" in content
|
||||
assert "【上周最后工作日 2026-10-30】\n周五合成日志" in content
|
||||
|
||||
|
||||
def test_monday_missing_sunday_is_marked_even_with_workday_log(tmp_path):
|
||||
config = {"summary": str(tmp_path / "{date}.md")}
|
||||
(tmp_path / "2026-10-30.md").write_text("周五合成日志", encoding="utf-8")
|
||||
|
||||
content, previous_missing = load_previous_summary(config, date(2026, 11, 2))
|
||||
|
||||
assert previous_missing
|
||||
assert "【周日 2026-11-01】\n<日志不存在>" in content
|
||||
assert "周五合成日志" in content
|
||||
|
||||
|
||||
def test_previous_workday_skips_custom_holidays_and_weekends():
|
||||
holidays = {date(2026, 10, 30), date(2026, 11, 1)}
|
||||
|
||||
assert previous_workday(
|
||||
date(2026, 11, 2),
|
||||
is_workday=lambda day: day.weekday() < 5 and day not in holidays,
|
||||
) == date(2026, 10, 29)
|
||||
|
||||
|
||||
def test_followup_items_are_checkboxes_and_idempotent():
|
||||
content = ("## 五、明日跟进建议\n"
|
||||
"1. 第一项\n"
|
||||
"2) 第二项\n"
|
||||
"- 第三项\n"
|
||||
"- [ ] 已有复选框\n"
|
||||
"## 六、运维信息\n"
|
||||
"1. 不转换运维编号\n")
|
||||
expected = ("## 五、明日跟进建议\n"
|
||||
"- [ ] 第一项\n"
|
||||
"- [ ] 第二项\n"
|
||||
"- [ ] 第三项\n"
|
||||
"- [ ] 已有复选框\n"
|
||||
"## 六、运维信息\n"
|
||||
"1. 不转换运维编号\n")
|
||||
assert format_followup_checkboxes(content) == expected
|
||||
assert format_followup_checkboxes(expected) == expected
|
||||
|
||||
|
||||
def test_obsidian_tag_escaping_preserves_headings_existing_escapes_and_urls():
|
||||
content = ("# 日报\\n"
|
||||
"正文 #ETF放量 与 #123,已有 \\#标签。\\n"
|
||||
"## 二、标题内的 #重点\\n"
|
||||
"链接 https://example.com/#section 不变。\\n")
|
||||
|
||||
assert escape_obsidian_tags(content) == (
|
||||
"# 日报\\n"
|
||||
"正文 \\#ETF放量 与 \\#123,已有 \\#标签。\\n"
|
||||
"## 二、标题内的 \\#重点\\n"
|
||||
"链接 https://example.com/#section 不变。\\n")
|
||||
assert escape_obsidian_tags(
|
||||
escape_obsidian_tags(content)) == escape_obsidian_tags(content)
|
||||
|
||||
|
||||
def test_summary_file_is_private(tmp_path):
|
||||
@ -364,8 +780,7 @@ def test_summary_file_is_private(tmp_path):
|
||||
|
||||
|
||||
def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
|
||||
tmp_path, monkeypatch, capsys
|
||||
):
|
||||
tmp_path, monkeypatch, capsys):
|
||||
database_url = f"sqlite:///{tmp_path / 'synthetic.db'}"
|
||||
storage = Storage(url=database_url)
|
||||
_, start, _, _ = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
@ -377,12 +792,16 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
|
||||
"msg_type_name": "TEXT",
|
||||
"create_time": int(start.timestamp()) + 60,
|
||||
}
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "incoming", "content": "合成收到内容"}, direction="in"
|
||||
)
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "outgoing", "content": "合成发出内容"}, direction="out"
|
||||
)
|
||||
storage.save_message({
|
||||
**base, "msg_id": "incoming",
|
||||
"content": "合成收到内容"
|
||||
},
|
||||
direction="in")
|
||||
storage.save_message({
|
||||
**base, "msg_id": "outgoing",
|
||||
"content": "合成发出内容"
|
||||
},
|
||||
direction="out")
|
||||
monkeypatch.setenv("LARKX_STORAGE_URL", database_url)
|
||||
routed_model = "maqqie/gg-chat-efund/EFundGPT-dsv4pro"
|
||||
session = FakeSession(
|
||||
@ -396,7 +815,14 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
|
||||
dry_run.write_text("合成 dry-run", encoding="utf-8")
|
||||
config_file = tmp_path / "config.yaml"
|
||||
config_file.write_text(
|
||||
f"output:\n dry_run: '{tmp_path}/dry-runs/{{date}}.json'\n",
|
||||
f"output:\n summary: '{tmp_path}/previous/{{date}}.md'\n"
|
||||
f" dry_run: '{tmp_path}/dry-runs/{{date}}.json'\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
previous = tmp_path / "previous" / "2026-11-02.md"
|
||||
previous.parent.mkdir()
|
||||
previous.write_text(
|
||||
"---\nmodel: synthetic\n---\n\n# 前日日志\n\n前日合成待办\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
args = argparse.Namespace(
|
||||
@ -416,19 +842,19 @@ def test_cli_end_to_end_with_synthetic_db_and_fake_magpie(
|
||||
)
|
||||
assert cmd_summarize(args) == 0
|
||||
assert output.read_text(encoding="utf-8") == (
|
||||
"---\nmodel: EFundGPT-dsv4pro\n---\n\n# 合成总结\n"
|
||||
)
|
||||
"---\nmodel: EFundGPT-dsv4pro\nis_read: false\n---\n\n# 合成总结\n")
|
||||
assert not dry_run.exists()
|
||||
request_text = session.post_calls[0][1]["json"]["messages"][1]["content"]
|
||||
assert "合成收到内容" in request_text
|
||||
assert "合成发出内容" not in request_text
|
||||
assert "合成发出内容" in request_text
|
||||
final_prompt = session.post_calls[1][1]["json"]["messages"][1]["content"]
|
||||
assert "前日合成待办" in final_prompt
|
||||
terminal = capsys.readouterr()
|
||||
assert "合成收到内容" not in terminal.out + terminal.err
|
||||
|
||||
|
||||
def test_cli_dry_run_exports_records_without_calling_model(
|
||||
tmp_path, monkeypatch, capsys
|
||||
):
|
||||
tmp_path, monkeypatch, capsys):
|
||||
database_url = f"sqlite:///{tmp_path / 'synthetic.db'}"
|
||||
storage = Storage(url=database_url)
|
||||
_, start, _, _ = resolve_day("2026-11-03", "Asia/Shanghai")
|
||||
@ -440,12 +866,16 @@ def test_cli_dry_run_exports_records_without_calling_model(
|
||||
"msg_type_name": "TEXT",
|
||||
"create_time": int(start.timestamp()) + 60,
|
||||
}
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "incoming", "content": "合成收到内容"}, direction="in"
|
||||
)
|
||||
storage.save_message(
|
||||
{**base, "msg_id": "outgoing", "content": "合成发出内容"}, direction="out"
|
||||
)
|
||||
storage.save_message({
|
||||
**base, "msg_id": "incoming",
|
||||
"content": "合成收到内容"
|
||||
},
|
||||
direction="in")
|
||||
storage.save_message({
|
||||
**base, "msg_id": "outgoing",
|
||||
"content": "合成发出内容"
|
||||
},
|
||||
direction="out")
|
||||
monkeypatch.setenv("LARKX_STORAGE_URL", database_url)
|
||||
session = FakeSession(completions=[(DEFAULT_MODEL, "不应生成")])
|
||||
monkeypatch.setattr("snooper_summary.requests.Session", lambda: session)
|
||||
@ -473,7 +903,7 @@ def test_cli_dry_run_exports_records_without_calling_model(
|
||||
assert cmd_summarize(args) == 0
|
||||
assert session.post_calls == []
|
||||
records = json.loads(output.read_text(encoding="utf-8"))
|
||||
assert [record["msg_id"] for record in records] == ["incoming"]
|
||||
assert [record["msg_id"] for record in records] == ["incoming", "outgoing"]
|
||||
assert records[0]["content"] == "合成收到内容"
|
||||
assert stat.S_IMODE(output.stat().st_mode) == 0o600
|
||||
terminal = capsys.readouterr()
|
||||
@ -485,9 +915,8 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
|
||||
|
||||
database_url = f"sqlite:///{tmp_path / 'synthetic.db'}"
|
||||
monkeypatch.setenv("LARKX_STORAGE_URL", database_url)
|
||||
session = FakeSession(
|
||||
completions=[(DEFAULT_MODEL, "单对话摘要"), (DEFAULT_MODEL, "# 合成总结")]
|
||||
)
|
||||
session = FakeSession(completions=[(DEFAULT_MODEL,
|
||||
"单对话摘要"), (DEFAULT_MODEL, "# 合成总结")])
|
||||
monkeypatch.setattr("snooper_summary.requests.Session", lambda: session)
|
||||
monkeypatch.setattr("lark_snooper.LarkClient", lambda auth: object())
|
||||
monkeypatch.setattr("lark_snooper.LarkAuth", lambda: object())
|
||||
@ -503,7 +932,8 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
|
||||
exclude_chat_ids=None,
|
||||
force=False,
|
||||
):
|
||||
assert len(session.get_calls) == 1 # EFundGPT preflight still comes first.
|
||||
assert len(
|
||||
session.get_calls) == 1 # EFundGPT preflight still comes first.
|
||||
_, start, _, _ = resolve_summary_day("2026-11-03", "Asia/Shanghai")
|
||||
storage.save_message(
|
||||
{
|
||||
@ -531,13 +961,11 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
|
||||
},
|
||||
direction="in",
|
||||
)
|
||||
sync_calls.append(
|
||||
{
|
||||
"batch_size": batch_size,
|
||||
"chat_ids": chat_ids,
|
||||
"exclude_chat_ids": exclude_chat_ids,
|
||||
}
|
||||
)
|
||||
sync_calls.append({
|
||||
"batch_size": batch_size,
|
||||
"chat_ids": chat_ids,
|
||||
"exclude_chat_ids": exclude_chat_ids,
|
||||
})
|
||||
return {
|
||||
"failed": 0,
|
||||
"matched": 1,
|
||||
@ -551,7 +979,8 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
|
||||
output = tmp_path / "summary.md"
|
||||
config_file = tmp_path / "config.yaml"
|
||||
config_file.write_text(
|
||||
f"output:\n dry_run: '{tmp_path}/dry-runs/{{date}}.json'\n"
|
||||
f"output:\n summary: '{tmp_path}/summaries/{{date}}.md'\n"
|
||||
f" dry_run: '{tmp_path}/dry-runs/{{date}}.json'\n"
|
||||
"chats:\n exclude:\n - 'chat-excluded'\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
@ -573,20 +1002,18 @@ def test_cli_summarize_prefetches_before_reading(tmp_path, monkeypatch):
|
||||
)
|
||||
|
||||
assert cmd_summarize(args) == 0
|
||||
assert sync_calls == [
|
||||
{
|
||||
"batch_size": 50,
|
||||
"chat_ids": ["chat-synthetic"],
|
||||
"exclude_chat_ids": ["chat-excluded"],
|
||||
}
|
||||
]
|
||||
assert sync_calls == [{
|
||||
"batch_size": 50,
|
||||
"chat_ids": ["chat-synthetic"],
|
||||
"exclude_chat_ids": ["chat-excluded"],
|
||||
}]
|
||||
assert len(session.post_calls) == 2
|
||||
request_text = session.post_calls[0][1]["json"]["messages"][1]["content"]
|
||||
assert "合成预取消息" in request_text
|
||||
assert "不应读取的排除群消息" not in request_text
|
||||
assert output.read_text(encoding="utf-8") == (
|
||||
f"---\nmodel: {DEFAULT_MODEL}\n---\n\n# 合成总结\n"
|
||||
)
|
||||
assert output.read_text(
|
||||
encoding="utf-8") == (f"---\nmodel: {DEFAULT_MODEL}\nis_read: false\n"
|
||||
"tags:\n- no_previous_day_log\n---\n\n# 合成总结\n")
|
||||
|
||||
|
||||
def test_cli_model_failure_happens_before_storage_is_opened(monkeypatch):
|
||||
|
||||
Reference in New Issue
Block a user