fix(reply): known user location reaches getWeather; retries are not blocked by the previous reply
- Drop the engine's deterministic Korean weather shortcut. It bypassed the chat model and the tool runner, so a location the user had stated could never be used. The tool already returns one clean Korean sentence. - getWeather and the tool-argument prompts pass the place the user has said they are (conversation or memory) and leave the argument empty otherwise, so the detected location is not copied into it. - getWeather retries a "district, city, country" name with its leading parts dropped when the geocoder finds nothing. - The planner gets the live time/location line and plans searchMemory topic='where the user lives' only when the location is unknown or disabled. - The per-reply repeat guard counts only this reply's tool messages, so calls carried over from the previous reply no longer block a retry the user asks for. - Memory extractor: the context block sits after the examples and the no-location example uses a different query, so a location already in context is not asked for again. New counterpart eval checks that it is still asked for when the context has none. - Tool retry eval fails every call in turn 1, so a model that retries by itself inside turn 1 still exercises the user's "try again". - Specs (planner, reply), docs/llm_contexts.md and the roadmap updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -524,3 +524,32 @@ def test_direct_exec_fires_despite_prior_query_tool_carryover(
|
||||
"direct-exec must fire for the current plan's getWeather step even when "
|
||||
"prior-query tool results are present in dialogue carryover"
|
||||
)
|
||||
|
||||
|
||||
def test_planner_receives_live_location_context(mock_config, db, dialogue_memory):
|
||||
"""The planner gets the same live time/location line as the rest of
|
||||
the turn, so it can tell an unknown location from a known one."""
|
||||
from jarvis.reply import engine as engine_mod
|
||||
|
||||
mock_config.ollama_chat_model = "gpt-oss:20b"
|
||||
mock_config.evaluator_enabled = False
|
||||
mock_config.location_enabled = False
|
||||
|
||||
seen: dict = {}
|
||||
|
||||
def fake_plan(*args, **kwargs):
|
||||
seen.update(kwargs)
|
||||
return ["Reply to the user."]
|
||||
|
||||
with patch.object(engine_mod, "chat_with_messages",
|
||||
return_value=_assistant_content("Hello.")), \
|
||||
patch.object(engine_mod, "select_tools", return_value=["stop"]), \
|
||||
patch.object(engine_mod, "plan_query", side_effect=fake_plan):
|
||||
engine_mod.run_reply_engine(
|
||||
db=db, cfg=mock_config, tts=None,
|
||||
text="how is the weather today",
|
||||
dialogue_memory=dialogue_memory,
|
||||
)
|
||||
|
||||
assert seen.get("live_context") == engine_mod._live_time_location_string(mock_config)
|
||||
assert "Location: Disabled" in seen["live_context"]
|
||||
|
||||
@@ -225,3 +225,53 @@ def test_tool_carryover_text_tool_mode(
|
||||
assert any(
|
||||
"Paris" in (m.get("content") or "") for m in tool_like
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@patch("src.jarvis.reply.engine.plan_query", return_value=[])
|
||||
@patch("src.jarvis.reply.engine.extract_search_params_for_memory", return_value={})
|
||||
@patch("src.jarvis.reply.engine.run_tool_with_retries")
|
||||
@patch("src.jarvis.reply.engine.extract_text_from_response")
|
||||
@patch("src.jarvis.reply.engine.chat_with_messages")
|
||||
def test_retry_asked_in_next_reply_runs_despite_prior_reply_calls(
|
||||
mock_chat, mock_extract, mock_tool, _mock_extract, _mock_plan
|
||||
):
|
||||
"""Calls carried over from the previous reply do not count against the
|
||||
per-reply repeat guard: when reply 1 tried a tool twice and the user
|
||||
then asks to try again, reply 2 runs it."""
|
||||
def _call(cid, place):
|
||||
return {"message": {"content": "", "tool_calls": [{
|
||||
"id": cid, "type": "function",
|
||||
"function": {"name": "getWeather", "arguments": {"location": place}},
|
||||
}]}}
|
||||
|
||||
mock_tool.side_effect = [
|
||||
Mock(success=False, reply_text="", error_message="Could not find location."),
|
||||
Mock(success=False, reply_text="", error_message="Could not find location."),
|
||||
Mock(success=True, reply_text="Kazbegi: 2.5C, partly cloudy.", error_message=None),
|
||||
]
|
||||
mock_chat.side_effect = [
|
||||
_call("c1", "Kazbegi"),
|
||||
_call("c2", "Stepantsminda"),
|
||||
{"message": {"content": "I could not find that place."}},
|
||||
_call("c3", "Kazbegi, Georgia"),
|
||||
{"message": {"content": "It is 2.5C and partly cloudy in Kazbegi."}},
|
||||
]
|
||||
mock_extract.side_effect = [
|
||||
"", "", "I could not find that place.",
|
||||
"", "It is 2.5C and partly cloudy in Kazbegi.",
|
||||
]
|
||||
|
||||
db = Mock()
|
||||
cfg = _mock_cfg()
|
||||
dm = DialogueMemory()
|
||||
|
||||
run_reply_engine(db=db, cfg=cfg, tts=None,
|
||||
text="how's the weather in Kazbegi today?",
|
||||
dialogue_memory=dm)
|
||||
reply = run_reply_engine(db=db, cfg=cfg, tts=None,
|
||||
text="just try checking the weather one more time",
|
||||
dialogue_memory=dm)
|
||||
|
||||
assert mock_tool.call_count == 3
|
||||
assert "2.5" in (reply or "")
|
||||
|
||||
@@ -194,6 +194,32 @@ class TestPlanQuery:
|
||||
)
|
||||
assert steps == ["Reply to user."]
|
||||
|
||||
def test_planner_sees_live_context(self):
|
||||
"""The planner is told what the live context knows (time, location),
|
||||
so it can plan a memory lookup for a fact about the user, such as
|
||||
where they are, only when the live context lacks it."""
|
||||
cfg = _cfg()
|
||||
with patch.object(
|
||||
planner_mod, "call_llm_direct", return_value="Reply to user."
|
||||
) as llm:
|
||||
plan_query(
|
||||
cfg,
|
||||
"how is the weather today",
|
||||
"",
|
||||
[("getWeather", "Weather. (args: location)")],
|
||||
live_context="Current local time: Monday 09:00. Location: Unknown",
|
||||
)
|
||||
assert "Location: Unknown" in llm.call_args.kwargs["user_content"]
|
||||
|
||||
def test_prompt_plans_memory_for_user_facts_missing_from_live_context(self):
|
||||
"""The prompt must tie the searchMemory directive to a user fact
|
||||
(where they are) that the live context does not give, so a
|
||||
location-dependent tool still gets the user's place from memory
|
||||
when auto-detection is off."""
|
||||
prompt = planner_mod._PROMPT_TEMPLATE
|
||||
assert "LIVE CONTEXT" in prompt
|
||||
assert "Location: Unknown" in prompt
|
||||
|
||||
def test_prompt_warns_against_fabricating_optional_arguments(self):
|
||||
"""The planner prompt must explicitly tell the model to omit
|
||||
optional arguments when the user didn't supply a value, and warn
|
||||
|
||||
@@ -163,6 +163,40 @@ class TestWeatherTool:
|
||||
# The romanised name must be what the second geocode actually queried.
|
||||
assert mock_get.call_args_list[1].kwargs["params"]["name"] == "Seoul"
|
||||
|
||||
@patch('requests.get')
|
||||
def test_run_comma_place_falls_back_to_wider_area(self, mock_get):
|
||||
"""A "district, city, country" name the geocoder can't match is retried
|
||||
without its leading parts, so a location taken from the context (e.g.
|
||||
a detected "Gangnam-gu, Seoul, South Korea") still gets weather."""
|
||||
known = {
|
||||
"Seoul, South Korea": {
|
||||
"latitude": 37.566, "longitude": 126.9784,
|
||||
"name": "Seoul", "country": "South Korea", "admin1": "Seoul",
|
||||
},
|
||||
}
|
||||
|
||||
def fake_get(url, params=None, timeout=None):
|
||||
resp = Mock()
|
||||
resp.raise_for_status = Mock()
|
||||
if "geocoding" in url:
|
||||
place = known.get(params["name"])
|
||||
resp.json.return_value = {"results": [place]} if place else {}
|
||||
else:
|
||||
resp.json.return_value = {
|
||||
"current": {
|
||||
"temperature_2m": 12.0, "apparent_temperature": 11.0,
|
||||
"weather_code": 0,
|
||||
},
|
||||
}
|
||||
return resp
|
||||
|
||||
mock_get.side_effect = fake_get
|
||||
|
||||
result = self.tool.run({"location": "Gangnam-gu, Seoul, South Korea"}, self.context)
|
||||
|
||||
assert result.success is True
|
||||
assert "Seoul" in result.reply_text
|
||||
|
||||
def test_romanise_place_skips_ascii_names(self):
|
||||
"""ASCII names already geocode, so no LLM round-trip is spent on them."""
|
||||
cfg = Mock()
|
||||
|
||||
Reference in New Issue
Block a user