Skip to content

bug: /goal lost after context compression rotates session_id #45059

Closed

Description

@skycaier

Bug Description

When context compression creates a child session (rotating session_id), the standing /goal is lost because state_meta key goal:<old_session_id> is never migrated to goal:<new_session_id>.

Root Cause

  1. Compression changes session_entry.session_id in-place at gateway/run.py:8638-8643
  2. The post-turn goal hook (_post_turn_goal_continuation) reads the new session_id from get_or_create_session(source) at line 7569
  3. GoalManager(session_id=new_sid) calls load_goal() which queries state_meta for goal:<new_sid> → returns None
  4. is_active() returns False → Ralph loop silently stops

Reproduction

  1. Set a /goal in a gateway session
  2. Have a long enough conversation to trigger context compression
  3. After compression, the goal loop stops — _post_turn_goal_continuation returns early because is_active() is False for the new session_id
  4. state_meta still has goal:<old_session_id> with status=active, but nothing reads it

Key Code Path

_handle_message_with_agent()
  └─ agent runs, triggers compression
  └─ line 8638: session_entry.session_id = new_sid  # in-place mutation
  
_post_turn_goal_continuation()  
  └─ line 7569: session_entry = get_or_create_session(source)  # returns entry with new_sid
  └─ line 9512: GoalManager(session_id=new_sid)
  └─ load_goal("goal:<new_sid>") → None  # state_meta has goal:<old_sid> not new_sid
  └─ is_active() → False → return  # goal loop broken

Suggested Fix

Two complementary fixes:

1. Migrate goal on compression (primary)

After line 8638-8643 in gateway/run.py, copy the active goal to the new session:

if agent_result.get("session_id") and agent_result["session_id"] != session_entry.session_id:
    _old_sid_for_goal = session_entry.session_id
    session_entry.session_id = agent_result["session_id"]
    self.session_store._save()
    # ... existing topic binding sync ...
    
    # Migrate active goal to new session
    try:
        from hermes_cli.goals import load_goal, save_goal
        _old_goal = load_goal(_old_sid_for_goal)
        if _old_goal and _old_goal.status == "active":
            save_goal(session_entry.session_id, _old_goal)
    except Exception:
        pass

2. Parent chain fallback in _goal_still_active_for_session

Walk the parent_session_id chain as a safety net:

def _goal_still_active_for_session(self, session_id: str) -> bool:
    if not session_id:
        return False
    try:
        from hermes_cli.goals import GoalManager
        if GoalManager(session_id=session_id).is_active():
            return True
        # Walk parent chain
        if self._session_db is not None:
            cur = session_id
            for _ in range(10):
                row = self._session_db._conn.execute(
                    "SELECT parent_session_id FROM sessions WHERE id = ?", (cur,)
                ).fetchone()
                parent = row[0] if row else None
                if not parent:
                    break
                if GoalManager(session_id=parent).is_active():
                    return True
                cur = parent
        return False
    except Exception:
        return False

Environment

  • Hermes Agent v0.16.0 (git install)
  • Gateway mode (Feishu platform)
  • auxiliary.goal_judge uses auto (main model)
  • Context compression is frequent in long quantitative research sessions

Activity

  1. added
    type/bugSomething isn't working
    comp/gatewayGateway runner, session dispatch, delivery
    P2Medium — degraded but workaround exists
    duplicateThis issue or pull request already exists
    on Jun 12, 2026
  2. alt-glitch commented on Jun 12, 2026

    @alt-glitch

    Duplicate of #33618 — same root cause: state_meta key goal:<old_session_id> is not migrated when context compression rotates session_id, so GoalManager(new_sid).is_active() returns False and the goal loop stops. Competing fix PRs already open (#34035, #29955, #18749, #18427).

  3. teknium1 commented on Jun 21, 2026

    @teknium1
    Collaborator

    Fixed in #49772 (duplicate of #33618 — /goal migrates across compression rotation). #49772

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/gatewayGateway runner, session dispatch, deliveryduplicateThis issue or pull request already existstype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions