fix(chat): harden session auto-title and keep a single no-tool LLM call

- Add session_auto_title prompts, finalize_auto_title (reject prose, truncate, fallback).
- Third-round naming: one model.chat(system, user) with empty tools; reuse executor.model.
- Extend worker title tests for garbage model output.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-05-06 15:19:22 +08:00
parent 16169b3de2
commit 9f4fa3d319
5 changed files with 153 additions and 28 deletions

View file

@ -14,8 +14,11 @@ class _DummyResp:
class _DummyModel:
def __init__(self, content: str = "第三轮标题") -> None:
self._content = content
def chat(self, _messages, _tools, on_token=None): # noqa: ANN001
return _DummyResp("第三轮标题")
return _DummyResp(self._content)
def test_worker_third_round_title_generation_updates_stage3(tmp_path: Path) -> None:
@ -57,3 +60,45 @@ def test_worker_third_round_title_generation_updates_stage3(tmp_path: Path) -> N
assert renamed is not None
assert str(getattr(renamed, "title", "") or "") == "第三轮标题"
assert str(store.get_setting(f"AIA_SESSION_AUTO_TITLE_STAGE:{sid}") or "") == "3"
def test_worker_third_round_rejects_prose_title_uses_fallback(tmp_path: Path) -> None:
store = SqliteStore(str(tmp_path / "worker-title-fb.sqlite"))
tenant = store.create_tenant("Team")
user = store.create_user_account(
tenant_id=str(tenant["id"]),
username="tester",
display_name="Tester",
role="owner",
password_hash=hashlib.sha256("test-pass".encode("utf-8")).hexdigest(),
is_active=True,
)
session = store.create_session_for_user(
title="第一轮标题",
tenant_id=str(tenant["id"]),
user_id=str(user["id"]),
)
sid = str(session.id)
store.set_setting(f"AIA_SESSION_AUTO_TITLE_STAGE:{sid}", "1")
store.add_message(session_id=sid, role="user", content="第一轮问题")
store.add_message(session_id=sid, role="assistant", content="第一轮回答")
store.add_message(session_id=sid, role="user", content="第二轮问题")
store.add_message(session_id=sid, role="assistant", content="第二轮回答")
msg = StandardMessage(
session_id=sid,
tenant_id=str(tenant["id"]),
user_id=str(user["id"]),
role="member",
channel="admin_chat",
text="第三轮问题",
attachments=[],
metadata={"lang": "zh"},
)
garbage = "您好!我是 Claude Code,一个软件工程助手。" + "x" * 80
_maybe_generate_title_on_third_round(store=store, msg=msg, model=_DummyModel(content=garbage))
renamed = store.get_session(sid)
assert renamed is not None
assert str(getattr(renamed, "title", "") or "") == "第一轮问题"
assert str(store.get_setting(f"AIA_SESSION_AUTO_TITLE_STAGE:{sid}") or "") == "3"

View file

@ -0,0 +1,27 @@
from __future__ import annotations
from oclaw.runtime.session_auto_title import AUTO_TITLE_CHAR_MAX, finalize_auto_title, should_reject_auto_title
def test_should_reject_long_prose() -> None:
blob = "您好!我是 Claude Code,一个软件工程助手。" + "x" * 80
assert should_reject_auto_title(blob) is True
def test_should_reject_claude_code_intro() -> None:
assert should_reject_auto_title("Hello! I am Claude Code, a software engineering assistant.") is True
def test_should_accept_short_title() -> None:
assert should_reject_auto_title("告警查询配置") is False
def test_finalize_truncates_fallback() -> None:
long_fb = "一二三四五六七八九十11121314151617181920"
out = finalize_auto_title(raw="x" * 100, fallback=long_fb)
assert len(out) == AUTO_TITLE_CHAR_MAX
assert out == long_fb[:AUTO_TITLE_CHAR_MAX]
def test_finalize_uses_model_when_sane() -> None:
assert finalize_auto_title(raw=" 网管同步 ", fallback="fallback") == "网管同步"