From cd80092e199d426f22c3c983c86f92486c749cad Mon Sep 17 00:00:00 2001 From: "fineas-bot[bot]" <258147136+fineas-bot[bot]@users.noreply.github.com> Date: Wed, 24 Jun 2026 16:49:41 +0000 Subject: [PATCH] Use sandbox thread key for company context live Slack --- tools/productivity/company_context/client.py | 12 +++- .../company_context/tests/test_client.py | 56 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/tools/productivity/company_context/client.py b/tools/productivity/company_context/client.py index 84d5db61b..1d46dca65 100644 --- a/tools/productivity/company_context/client.py +++ b/tools/productivity/company_context/client.py @@ -247,12 +247,18 @@ def _slack_after_query(query: str, latest_date: str | None) -> str: return f"{query} after:{latest_date[:10]}" -def _current_slack_channel_id() -> str | None: - """Return the channel/group id for the active Slack thread, or None for DMs.""" +def _current_thread_key() -> str | None: + """Return the active thread key from the tool context or sandbox environment.""" try: thread_key = get_tool_context().thread_key except LookupError: - return None + thread_key = None + return thread_key or os.getenv("CENTAUR_THREAD_KEY") + + +def _current_slack_channel_id() -> str | None: + """Return the channel/group id for the active Slack thread, or None for DMs.""" + thread_key = _current_thread_key() if not thread_key: return None for segment in str(thread_key).split(":")[1:]: diff --git a/tools/productivity/company_context/tests/test_client.py b/tools/productivity/company_context/tests/test_client.py index efdc110aa..54f322a0b 100644 --- a/tools/productivity/company_context/tests/test_client.py +++ b/tools/productivity/company_context/tests/test_client.py @@ -90,6 +90,33 @@ def test_default_database_url_does_not_fall_back_to_raw_database_url(monkeypatch reset_tool_context(token) +def test_current_slack_channel_id_prefers_tool_context(monkeypatch): + monkeypatch.setenv("CENTAUR_THREAD_KEY", "slack:CENV:1780000000.000000") + token = set_tool_context( + ToolContext( + name="company_context", + secrets={}, + thread_key="slack:CTOOL:1780000000.000000", + ) + ) + try: + assert company_context_client._current_slack_channel_id() == "CTOOL" + finally: + reset_tool_context(token) + + +def test_current_slack_channel_id_falls_back_to_centaur_thread_key(monkeypatch): + monkeypatch.setenv("CENTAUR_THREAD_KEY", "slack:CENV:1780000000.000000") + + assert company_context_client._current_slack_channel_id() == "CENV" + + +def test_current_slack_channel_id_ignores_dm_thread_key(monkeypatch): + monkeypatch.setenv("CENTAUR_THREAD_KEY", "slack:D12345678:1780000000.000000") + + assert company_context_client._current_slack_channel_id() is None + + def test_search_queries_bm25_and_returns_compact_results(monkeypatch): occurred_at = dt.datetime(2026, 5, 8, 12, 0, tzinfo=dt.UTC) source_updated_at = dt.datetime(2026, 5, 8, 12, 5, tzinfo=dt.UTC) @@ -312,6 +339,35 @@ async def fake_connect(*args, **kwargs): assert fake_slack.calls == [] +def test_search_uses_centaur_thread_key_for_live_slack_gap(monkeypatch): + fake = _FakeConnection( + rows=[], + row={ + "latest_date": dt.datetime(2026, 5, 10, 15, 30, tzinfo=dt.UTC), + "latest_source_updated_at": dt.datetime(2026, 5, 10, 15, 30, tzinfo=dt.UTC), + "latest_occurred_at": dt.datetime(2026, 5, 10, 14, 0, tzinfo=dt.UTC), + "document_count": 42, + }, + ) + fake_slack = _FakeSlackClient() + + async def fake_connect(*args, **kwargs): + return fake + + monkeypatch.setenv("CENTAUR_THREAD_KEY", "slack:CENV:1780000000.000000") + monkeypatch.setattr(company_context_client.asyncpg, "connect", fake_connect) + monkeypatch.setattr(company_context_client, "_load_slack_client", lambda: fake_slack) + + result = CompanyContextClient("postgresql://example").search( + "state root mismatch", + source="slack", + ) + + assert result["status"] == "ok" + assert result["live_error"] is None + assert fake_slack.calls == [("state root mismatch after:2026-05-10", 10, ["CENV"])] + + def test_search_uses_or_terms_and_drops_stop_words(monkeypatch): fake = _FakeConnection(rows=[])