Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions tools/productivity/company_context/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]:
Expand Down
56 changes: 56 additions & 0 deletions tools/productivity/company_context/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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=[])

Expand Down
Loading