fix(goal): the re-paste pointer applies only to a near-whole re-paste, and on every surface

Independent review: substring matching lost the selected objective (after a
message offering API or UI work, '/goal ship the API' and '/goal ship the UI'
kicked identically), and the gateway and TUI /goal paths still re-sent the
full goal. The rule now lives once in hermes_cli.goals.goal_kick_prompt:
the pointer replaces the goal only when the goal is >= 400 chars, contained
in the last user message, and >= 80% of it. The CLI reads the last user
message from its history; the gateway and TUI read it from the SessionDB
(fail-safe: goal sent verbatim).

Tests: a fragment that selects one option is kicked verbatim; a long goal
that is a minority of a longer message is kicked verbatim; all three
surfaces call the shared function.
This commit is contained in:
Teknium
2026-09-05 06:36:52 -07:00
parent 7e2dbcfb33
commit d3fdcef132
5 changed files with 93 additions and 26 deletions
+5 -2
View File
@@ -196,9 +196,12 @@ class GatewayGoalCommandsMixin:
except ValueError as exc:
return t("gateway.goal.invalid", error=str(exc))
# Queue the goal text as an immediate first turn; the post-turn hook takes over after.
# Queue the goal text as an immediate first turn (a short pointer when the user just pasted that
# very text: hermes_cli.goals.goal_kick_prompt); the post-turn hook takes over after.
from hermes_cli.goals import goal_kick_prompt, last_user_message_from_db
kick = goal_kick_prompt(state.goal, last_user_message_from_db(getattr(mgr, "session_id", None)))
self._enqueue_goal_turn(
event, state.goal, label="kickoff enqueue", kickoff=True, route=self._adapter_and_key_for(event)
event, kick, label="kickoff enqueue", kickoff=True, route=self._adapter_and_key_for(event)
)
base = t("gateway.goal.set", budget=state.max_turns, goal=state.goal)
+4 -19
View File
@@ -2223,26 +2223,11 @@ class CLICommandsMixin:
else:
self._goal_set(mgr, arg)
# `/goal <text>` kicks the loop by queueing the goal text as the next user turn. When that text
# is what the user JUST said (a pasted handoff note, a plan the agent already has), re-sending
# it makes the agent spend a turn deciding it is a replay (11 API calls, 6 min, in one run) and
# duplicates ~2k tokens of context. A short pointer starts the loop just as well.
_GOAL_ALREADY_SEEN_KICK = "[Goal set] Continue with the goal you were just given; there is no need to re-read it."
def _goal_kick_prompt(self, goal: str) -> str:
"""The goal text, or a short pointer when the last user message already carries it."""
last_user = ""
for msg in reversed(getattr(self, "conversation_history", None) or []):
if msg.get("role") == "user":
content = msg.get("content")
if isinstance(content, list):
content = " ".join(str(b.get("text", "")) for b in content if isinstance(b, dict))
last_user = str(content or "")
break
goal_norm = " ".join(goal.split())
if goal_norm and goal_norm in " ".join(last_user.split()):
return self._GOAL_ALREADY_SEEN_KICK
return goal
"""The goal text, or a short pointer when the last user message is essentially that text
(shared rule: ``hermes_cli.goals.goal_kick_prompt``)."""
from hermes_cli.goals import goal_kick_prompt, last_user_message_content
return goal_kick_prompt(goal, last_user_message_content(getattr(self, "conversation_history", None)))
def _kick_goal(self, prompt: str) -> bool:
"""Queue ``prompt`` as the next turn so the loop starts without a separate message."""
+49
View File
@@ -909,6 +909,55 @@ def judge_goal(
return verdict, reason, parse_failed, wait_directive, False
# `/goal <text>` kicks the loop by sending the goal as the next user turn. When that text IS what
# the user just said (a pasted handoff note, a plan the agent already has), re-sending it makes the
# agent spend a turn deciding it is a replay (11 API calls, 6 min, in one run) and duplicates ~2k
# tokens of context. The pointer is used only when the goal is substantially the WHOLE last
# message: a short goal that merely appears inside a longer one ("ship the API" after a message
# offering API or UI work) selects one option, and two different goals must not kick identically.
GOAL_ALREADY_SEEN_KICK = "[Goal set] Continue with the goal you were just given; there is no need to re-read it."
_GOAL_REPASTE_MIN_CHARS = 400
_GOAL_REPASTE_MIN_SHARE = 0.8
def goal_kick_prompt(goal: str, last_user_message: Any) -> str:
"""The goal text, or ``GOAL_ALREADY_SEEN_KICK`` when ``last_user_message`` is essentially that text."""
content = last_user_message
if isinstance(content, list):
content = " ".join(str(b.get("text", "")) for b in content if isinstance(b, dict))
goal_norm, last_norm = " ".join(str(goal or "").split()), " ".join(str(content or "").split())
if (
len(goal_norm) >= _GOAL_REPASTE_MIN_CHARS
and goal_norm in last_norm
and len(goal_norm) >= _GOAL_REPASTE_MIN_SHARE * len(last_norm)
):
return GOAL_ALREADY_SEEN_KICK
return goal
def last_user_message_content(history: Any) -> Any:
"""Content of the newest ``role == "user"`` message in an OpenAI-shaped history, else ``""``."""
for msg in reversed(history or []):
if isinstance(msg, dict) and msg.get("role") == "user":
return msg.get("content")
return ""
def last_user_message_from_db(session_id: Optional[str]) -> Any:
"""Newest user message of ``session_id`` from the SessionDB (gateway/TUI surfaces have no live
history object at slash-command time); ``""`` on any error."""
if not session_id:
return ""
try:
db = _get_session_db()
if db is None:
return ""
rows = db.get_messages(str(session_id), limit=20, latest=True)
return last_user_message_content(rows)
except Exception:
return ""
def gather_background_processes(task_id: Optional[str] = None) -> List[Dict[str, Any]]:
"""Fail-safe snapshot of RUNNING ``process_registry`` sessions for the judge; ``[]`` on any error
so the loop degrades to its pre-wait-barrier behavior."""
+32 -4
View File
@@ -6,6 +6,7 @@ a user message; the kickoff re-sent it and the agent spent 11 API calls / 6 min
import queue
from hermes_cli.cli_commands_mixin import CLICommandsMixin
from hermes_cli.goals import GOAL_ALREADY_SEEN_KICK
def _cli(history):
@@ -15,16 +16,16 @@ def _cli(history):
return cli
HANDOFF = "HANDOFF: resume round 3 integration.\n - merge r3-16 first\n - then run the full suite"
HANDOFF = "HANDOFF: resume round 3 integration.\n" + "\n".join(f" - step {i}: merge r3-{i} and run its targeted suite, then the full suite" for i in range(8))
def test_goal_that_the_user_just_pasted_kicks_with_a_pointer_not_the_text():
cli = _cli([{"role": "user", "content": "Here is the plan.\n\n" + HANDOFF + "\n\nGo."},
cli = _cli([{"role": "user", "content": HANDOFF + "\n\nGo."},
{"role": "assistant", "content": "ok"}])
assert cli._goal_kick_prompt(HANDOFF) == CLICommandsMixin._GOAL_ALREADY_SEEN_KICK
assert cli._goal_kick_prompt(HANDOFF) == GOAL_ALREADY_SEEN_KICK
# block-style content is handled too
cli = _cli([{"role": "user", "content": [{"type": "text", "text": HANDOFF}]}])
assert cli._goal_kick_prompt(HANDOFF) == CLICommandsMixin._GOAL_ALREADY_SEEN_KICK
assert cli._goal_kick_prompt(HANDOFF) == GOAL_ALREADY_SEEN_KICK
def test_a_new_goal_or_a_goal_from_an_older_turn_is_kicked_verbatim():
@@ -35,3 +36,30 @@ def test_a_new_goal_or_a_goal_from_an_older_turn_is_kicked_verbatim():
cli = _cli([{"role": "user", "content": HANDOFF}, {"role": "assistant", "content": "done"},
{"role": "user", "content": "now something else"}])
assert cli._goal_kick_prompt(HANDOFF) == HANDOFF
def test_a_short_goal_that_selects_one_option_from_the_last_message_is_kicked_verbatim():
"""Independent-review witness: after a message offering API or UI work, `/goal ship the API` and
`/goal ship the UI` produced identical kickoffs. A goal that is a fragment of the last message
carries the selection; only a near-whole re-paste is replaced by the pointer."""
offer = "I can either ship the API or ship the UI next; which do you want? " * 8
cli = _cli([{"role": "user", "content": offer}])
assert cli._goal_kick_prompt("ship the API") == "ship the API"
assert cli._goal_kick_prompt("ship the UI") == "ship the UI"
# a long goal that is only a minority of a much longer message is also kept verbatim
long_goal = "x" * 500
cli = _cli([{"role": "user", "content": long_goal + " " + "y" * 2000}])
assert cli._goal_kick_prompt(long_goal) == long_goal
def test_gateway_and_tui_surfaces_use_the_same_rule(tmp_path, monkeypatch):
"""Independent review: other surfaces still duplicated the full goal. One shared function now."""
from hermes_cli import goals
long_goal = "HANDOFF " + "step; " * 120
assert goals.goal_kick_prompt(long_goal, long_goal) == goals.GOAL_ALREADY_SEEN_KICK
assert goals.goal_kick_prompt("ship the API", "ship the API or ship the UI? " * 8) == "ship the API"
# DB-backed lookup fails safe to "" (goal kicked verbatim) when no session/db
assert goals.last_user_message_from_db(None) == ""
import gateway.slash_commands_goals as g, tui_gateway.methods_tools as m # noqa: E401
import inspect
assert "goal_kick_prompt" in inspect.getsource(g) and "goal_kick_prompt" in inspect.getsource(m)
+3 -1
View File
@@ -709,7 +709,9 @@ def _cmd_goal(rid, params, session, name, arg):
f"⊙ Goal set ({state.max_turns}-turn budget): {state.goal}\n"
"I'll keep working until the goal is done, you pause/clear it, or the budget is exhausted.\n"
"Controls: /goal status · /goal pause · /goal resume · /goal clear")
return _ok(rid, {"type": "send", "notice": notice, "message": state.goal})
from hermes_cli.goals import goal_kick_prompt, last_user_message_from_db
kick = goal_kick_prompt(state.goal, last_user_message_from_db(getattr(mgr, "session_id", None)))
return _ok(rid, {"type": "send", "notice": notice, "message": kick})
def _cmd_loop(rid, params, session, name, arg):