From e9d285f3a8b989fe3d2ff97e00e9b58ed1ff8769 Mon Sep 17 00:00:00 2001 From: Bruce-Yii Date: Wed, 23 Sep 2026 16:56:43 +0800 Subject: [PATCH] fix(agent): preserve newest turn in chronological conversation lists --- laya/agent.py | 7 +- tests/test_truncation_direction.py | 138 +++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 tests/test_truncation_direction.py diff --git a/laya/agent.py b/laya/agent.py index 70163bf98..48dec537c 100644 --- a/laya/agent.py +++ b/laya/agent.py @@ -341,11 +341,16 @@ def system_one(self, state: Union[str, dict, list], questions: Dict[str, Dict[st items = [] max_len = self.cfg.get("max_len", 512) head_max_len = self.cfg.get("head_max_len", 192) + # A chronological conversation list is serialized newest-last, so the default + # right-truncation (st[:room]) would silently drop the newest turn. Truncate + # from the left for lists so the most recent intent is preserved. + truncate_left = isinstance(state, list) for qid in ids: self._check_question(qid, questions[qid]) q = self._to_internal(questions[qid]) - seq, markers = build_sequence(self.tok, state, q, max_len, head_max_len) + seq, markers = build_sequence(self.tok, state, q, max_len, head_max_len, + truncate_left=truncate_left) if len(markers) != len(render_options(q)): raise ValueError("question %r options exceed head_max_len=%d" % (qid, head_max_len)) items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]]}) diff --git a/tests/test_truncation_direction.py b/tests/test_truncation_direction.py new file mode 100644 index 000000000..3cb1722de --- /dev/null +++ b/tests/test_truncation_direction.py @@ -0,0 +1,138 @@ +"""Regression: Agent.system_one preserves the newest conversation turn. + +The public Agent API advertises `state` as accepting a chronological +conversation-turn list. build_sequence defaults to truncate_left=False +(st[:room]), which preserves the head and drops the tail — for a chronological +list that means the newest turn is silently lost. For list-shaped state the agent +now truncates from the left so the most recent intent survives. Strings and +dicts are unaffected (backward compatible). +""" +import json + +import torch +from transformers import AutoConfig, AutoModel + +from laya.agent import Agent +from laya.common import DecisionModel, build_sequence, serialize_state + + +class _FakeTok: + cls_token_id, sep_token_id, mask_token_id, pad_token_id = 0, 1, 4, 2 + mask_token = "[MASK]" + + def __call__(self, text, add_special_tokens=False): + return {"input_ids": [10 + (len(w) % 90) for w in text.split() if w]} + + +def _tiny_agent(): + agent = object.__new__(Agent) + cfg = AutoConfig.for_model("bert", hidden_size=16, num_hidden_layers=1, num_attention_heads=1, + intermediate_size=32, vocab_size=64) + agent.cfg = {"max_len": 30, "head_max_len": 12, "encoder": "tiny"} + agent.tok = _FakeTok() + agent.model = DecisionModel(AutoModel.from_config(cfg), head_layers=1, n_act=2).eval() + agent.device = torch.device("cpu") + agent.dtype = torch.float32 + agent.temperature = [1.0, 1.0, 1.0] + agent.temperature_by_options = {} + return agent + + +def _state_prefix_len(tok, q, max_len, head_max_len): + ref, _ = build_sequence(tok, "", q, max_len, head_max_len) + return len(ref) - 1 + + +Q = {"t": "choice", "ins": "What action?", "crit": {"refund": "money back", "escalate": "manager", "hold": "wait"}} + + +def test_agent_list_vs_string_truncation_direction(): + """Agent must truncate left for lists, right for strings (via mock inspection).""" + from unittest.mock import patch + + agent = _tiny_agent() + questions = {"q": {"type": "choice", "instructions": "Act?", "criteria": {"a": "", "b": ""}}} + + list_capture = {} + string_capture = {} + + def fake_build(tok, state, q, max_len, head_max_len, truncate_left=False): + if isinstance(state, list): + list_capture["truncate_left"] = truncate_left + else: + string_capture["truncate_left"] = truncate_left + return build_sequence(tok, state, q, max_len, head_max_len, truncate_left=truncate_left) + + with patch("laya.agent.build_sequence", side_effect=fake_build): + agent.system_one([{"role": "user", "content": "hi"}, {"role": "user", "content": "newest"}], questions) + agent.system_one("string state", questions) + + assert list_capture["truncate_left"] is True + assert string_capture["truncate_left"] is False + + +def test_agent_system_one_passes_truncate_left_for_list(): + """Agent.system_one must pass truncate_left=True for list state.""" + from unittest.mock import patch + + agent = _tiny_agent() + conversation = [{"role": "user", "content": "hello"}, {"role": "user", "content": "NEWEST"}] + questions = {"q": {"type": "choice", "instructions": "Act?", "criteria": {"a": "", "b": ""}}} + + captured = {} + + def fake_build(tok, state, q, max_len, head_max_len, truncate_left=False): + captured["truncate_left"] = truncate_left + captured["state"] = state + return build_sequence(tok, state, q, max_len, head_max_len, truncate_left=truncate_left) + + with patch("laya.agent.build_sequence", side_effect=fake_build): + agent.system_one(conversation, questions) + + assert captured["truncate_left"] is True, "list state must trigger truncate_left=True" + assert isinstance(captured["state"], list) + + +def test_agent_system_one_string_state_default_truncation(): + """Agent.system_one with string state keeps default (truncate_left=False).""" + from unittest.mock import patch + + agent = _tiny_agent() + questions = {"q": {"type": "choice", "instructions": "Act?", "criteria": {"a": "", "b": ""}}} + + captured = {} + + def fake_build(tok, state, q, max_len, head_max_len, truncate_left=False): + captured["truncate_left"] = truncate_left + return build_sequence(tok, state, q, max_len, head_max_len, truncate_left=truncate_left) + + with patch("laya.agent.build_sequence", side_effect=fake_build): + agent.system_one("just a string state", questions) + + assert captured["truncate_left"] is False, "string state must keep default truncation" + + +def test_string_state_preserves_head(): + """A string state must still preserve the head (backward compatible).""" + tok = _FakeTok() + state = "HEADMARKERWORD " + "filler " * 20 + " TAILMARKER" + state_ids = tok(state.replace("[MASK]", " "), add_special_tokens=False)["input_ids"] + head_id = state_ids[0] + tail_id = state_ids[-1] + assert head_id != tail_id, "head and tail must have different ids" + + seq, _ = build_sequence(tok, state, Q, 30, 12) + prefix = _state_prefix_len(tok, Q, 30, 12) + kept = seq[prefix:-1] + + assert head_id in kept, "string state must preserve the head (default mode)" + assert tail_id not in kept, "string state must drop the tail (default mode)" + + +def test_build_sequence_default_unchanged(): + """build_sequence's default behavior is unchanged for non-list callers.""" + tok = _FakeTok() + state = "OLDFRONT " + "filler " * 20 + " NEWBACK" + seq_default, _ = build_sequence(tok, state, Q, 30, 12) + seq_explicit, _ = build_sequence(tok, state, Q, 30, 12, truncate_left=False) + assert seq_default == seq_explicit, "default must remain truncate_left=False"