Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

feat(slack): re-anchor resumed sessions outside start thread

Wang Siyuanee2737e71bec
diff --git a/core/controller.py b/core/controller.py
index fbf229936..138dc394a 100644
--- a/core/controller.py
+++ b/core/controller.py
@@ -624,17 +624,23 @@ class Controller:
thread_id: Optional[str],
agent: Optional[str],
session_id: Optional[str],
+ host_message_ts: Optional[str] = None,
) -> None:
"""Bind a provided session_id to the current thread for the chosen agent."""
try:
if not agent or not session_id:
raise ValueError("Agent and session ID are required to resume.")
- target_thread = (
- thread_id
- or (channel_id if channel_id and channel_id.startswith("D") else None)
- )
- # Build context for replies (use thread_id if provided)
+ # Decide whether to reuse current thread or start a new one for clarity.
+ reuse_thread = True
+ if host_message_ts and thread_id and thread_id == host_message_ts:
+ # Resume was initiated from the /start menu message; start a fresh thread.
+ reuse_thread = False
+
+ # If DM and no thread provided, reuse channel unless we intentionally create new thread later
+ target_thread = thread_id if reuse_thread else None
+
+ # Build confirmation context (top-level message when starting fresh)
context = MessageContext(
user_id=user_id,
channel_id=channel_id or user_id,
@@ -642,24 +648,29 @@ class Controller:
platform_specific={},
)
+ agent_label = agent.capitalize()
+ confirmation = (
+ f"✅ Resumed {agent_label} session.\n"
+ f"Session ID: `{session_id}`\n"
+ "Reply in this thread to continue."
+ )
+
+ confirmation_ts = await self.im_client.send_message(context, confirmation)
+
+ # If we created a fresh top-level message, use it as the new thread anchor
+ mapped_thread = target_thread or confirmation_ts
+ base_session_id = f"slack_{mapped_thread}"
+
settings_key = self._get_settings_key(context)
- base_session_id = f"slack_{target_thread or context.channel_id}"
# Persist mapping
self.settings_manager.set_agent_session_mapping(
settings_key, agent, base_session_id, session_id
)
- # Mark thread active for visibility/cleanup semantics
- if target_thread:
- self.settings_manager.mark_thread_active(user_id, context.channel_id, target_thread)
-
- agent_label = agent.capitalize()
- confirmation = (
- f"✅ Resumed {agent_label} session.\n"
- f"Session ID: `{session_id}`\n"
- "You can now continue the conversation in this thread."
+ # Mark thread active
+ self.settings_manager.mark_thread_active(
+ user_id, context.channel_id, mapped_thread
)
- await self.im_client.send_message(context, confirmation)
except Exception as e:
logger.error(f"Error resuming session: {e}", exc_info=True)
context = MessageContext(
diff --git a/core/handlers/command_handlers.py b/core/handlers/command_handlers.py
index 589b07d38..d285f6bd8 100644
--- a/core/handlers/command_handlers.py
+++ b/core/handlers/command_handlers.py
@@ -348,6 +348,7 @@ Use the buttons below to manage your {agent_display_name} sessions, or simply ty
sessions_by_agent=sessions_by_agent,
channel_id=context.channel_id,
thread_id=context.thread_id or context.message_id or "",
+ host_message_ts=context.message_id,
)
except Exception as e:
logger.error(f"Error opening resume modal: {e}")
diff --git a/docs/plans/resume-session.md b/docs/plans/resume-session.md
index 9ac37f9cd..5f3edec4b 100644
--- a/docs/plans/resume-session.md
+++ b/docs/plans/resume-session.md
@@ -30,7 +30,7 @@ Add a user-facing resume entry point in Slack (/start menu) to let users pick or
- [x] Add modal schema + open handler for `cmd_resume` callback.
- [x] Implement modal submission handler: resolve session_id (manual input wins), persist via SettingsManager, mark thread active, send confirmation.
- [x] Add helper(s) in SettingsManager to list stored session mappings per agent with metadata for display.
-- [ ] Add tests / manual check notes (if no test harness, include sanity steps).
+- [x] Add tests / manual check notes (if no test harness, include sanity steps).
## Open Questions
- Do we surface stored sessions across all channels or per-user? (Plan: per-user across channels, consistent with SessionStore namespace.)
diff --git a/modules/im/slack.py b/modules/im/slack.py
index ddae54048..ed39541ec 100644
--- a/modules/im/slack.py
+++ b/modules/im/slack.py
@@ -911,18 +911,20 @@ class SlackBot(BaseIMClient):
metadata_raw = view.get("private_metadata")
channel_id = None
thread_id = None
+ host_message_ts = None
try:
import json
md = json.loads(metadata_raw) if metadata_raw else {}
channel_id = md.get("channel_id")
thread_id = md.get("thread_id")
+ host_message_ts = md.get("host_message_ts")
except Exception:
pass
if hasattr(self, "_on_resume_session"):
await self._on_resume_session(
- user_id, channel_id, thread_id, chosen_agent, chosen_session
+ user_id, channel_id, thread_id, chosen_agent, chosen_session, host_message_ts
)
elif callback_id == "opencode_question_modal":
@@ -1439,6 +1441,7 @@ class SlackBot(BaseIMClient):
sessions_by_agent: Dict[str, Dict[str, str]],
channel_id: Optional[str],
thread_id: Optional[str],
+ host_message_ts: Optional[str],
):
"""Open a modal to let users select or input a session to resume."""
self._ensure_clients()
@@ -1541,7 +1544,11 @@ class SlackBot(BaseIMClient):
}
)
- metadata = {"channel_id": channel_id, "thread_id": thread_id}
+ metadata = {
+ "channel_id": channel_id,
+ "thread_id": thread_id,
+ "host_message_ts": host_message_ts,
+ }
import json
view = {
diff --git a/tests/test_resume_session.py b/tests/test_resume_session.py
index 41accfa91..a20086ede 100644
--- a/tests/test_resume_session.py
+++ b/tests/test_resume_session.py
@@ -27,10 +27,14 @@ class _StubIMClient:
self.resume_calls = []
async def send_message(self, context, text):
- self.messages.append((context.channel_id, context.thread_id, text))
+ ts = f"T{len(self.messages)+1}"
+ self.messages.append((context.channel_id, context.thread_id, text, ts))
+ return ts
- async def open_resume_session_modal(self, trigger_id, sessions_by_agent, channel_id, thread_id):
- self.resume_calls.append((trigger_id, sessions_by_agent, channel_id, thread_id))
+ async def open_resume_session_modal(
+ self, trigger_id, sessions_by_agent, channel_id, thread_id, host_message_ts
+ ):
+ self.resume_calls.append((trigger_id, sessions_by_agent, channel_id, thread_id, host_message_ts))
class _StubConfig:
@@ -87,9 +91,9 @@ class ResumeSessionTests(unittest.IsolatedAsyncioTestCase):
session_id="sess_dm",
)
- # In DM, thread falls back to channel id for base session key
- self.assertEqual(settings.set_calls, [("DXYZ", "codex", "slack_DXYZ", "sess_dm")])
- self.assertEqual(settings.mark_calls, [("U999", "DXYZ", "DXYZ")])
+ # No thread provided -> new confirmation message anchor used
+ self.assertEqual(settings.set_calls, [("DXYZ", "codex", "slack_T1", "sess_dm")])
+ self.assertEqual(settings.mark_calls, [("U999", "DXYZ", "T1")])
async def test_command_handlers_handle_resume_opens_modal(self):
settings = _StubSettingsManager()
@@ -110,8 +114,8 @@ class ResumeSessionTests(unittest.IsolatedAsyncioTestCase):
# One info message about missing sessions
self.assertEqual(len(im_client.messages), 1)
self.assertIn("No stored sessions", im_client.messages[0][2])
- # Modal opened with empty sessions map
- self.assertEqual(im_client.resume_calls, [("TRIG", {}, "CCHAN", "TH1")])
+ # Modal opened with empty sessions map and host ts
+ self.assertEqual(im_client.resume_calls, [("TRIG", {}, "CCHAN", "TH1", "TS1")])
if __name__ == "__main__":