Resolve the CLI session ID for a given MetaClaw session. Returns (cli_session_id, is_new). If the session is invalidated (auth or system prompt changed), a new session is started.
(
self,
metaclaw_session_id: str,
auth_profile_id: str | None = None,
system_prompt: str | None = None,
)
| 89 | # ── session resolution (mirrors OpenClaw's resolveCliSessionReuse) ─── |
| 90 | |
| 91 | def resolve_session( |
| 92 | self, |
| 93 | metaclaw_session_id: str, |
| 94 | auth_profile_id: str | None = None, |
| 95 | system_prompt: str | None = None, |
| 96 | ) -> tuple[str, bool]: |
| 97 | """Resolve the CLI session ID for a given MetaClaw session. |
| 98 | |
| 99 | Returns (cli_session_id, is_new). |
| 100 | If the session is invalidated (auth or system prompt changed), |
| 101 | a new session is started. |
| 102 | """ |
| 103 | current_sp_hash = _hash_text(system_prompt) |
| 104 | existing = self._bindings.get(metaclaw_session_id) |
| 105 | |
| 106 | if existing: |
| 107 | # Check if session is still valid |
| 108 | invalidated = self._check_invalidation( |
| 109 | existing, auth_profile_id, current_sp_hash |
| 110 | ) |
| 111 | if not invalidated: |
| 112 | return existing.cli_session_id, False |
| 113 | else: |
| 114 | logger.info( |
| 115 | "[CliSessionStore] session %s invalidated: %s", |
| 116 | metaclaw_session_id, |
| 117 | invalidated, |
| 118 | ) |
| 119 | |
| 120 | # Create new CLI session |
| 121 | import time |
| 122 | |
| 123 | cli_sid = str(uuid.uuid4()) |
| 124 | self._bindings[metaclaw_session_id] = CliSessionBinding( |
| 125 | cli_session_id=cli_sid, |
| 126 | auth_profile_id=auth_profile_id, |
| 127 | system_prompt_hash=current_sp_hash, |
| 128 | created_at=time.time(), |
| 129 | ) |
| 130 | self.save() |
| 131 | return cli_sid, True |
| 132 | |
| 133 | def _check_invalidation( |
| 134 | self, |
no test coverage detected