discord-bot: fix "no response", serialize turns, attribution, mentions, post-at-bottom

client.py: send() falls back to ResultMessage.result when no TextBlock streams
(the "(no response)" bug) and reconnects+retries once on a closed SDK session.

message_handler.py: per-thread turn lock so messages arriving mid-turn or from a
second user queue in order (nothing dropped); per-session requester-attribution
env (discord_id -> users.json key), pinned to the thread opener; _USER_MAP caches
only on a successful load; final answer posts as a fresh message at the BOTTOM
(no edit-in-place); a <@id> tag goes out as a fresh send so it actually pings.

main.py: allowed_mentions permits user pings, blocks @everyone/@here/roles.

DISCORD_CLAUDE.md: no thread auto-delete; tiered close-out (Q&A -> one-line rolling
log, substantive -> /save); @mention guidance; opener-pinned attribution note.

whoami-block.sh / sync.sh: bot-context attribution (Executed by ClaudeTools Bot /
Requested by <person>; git author = mapped requester, committer = bot). Strict
no-op for interactive sessions.

users.json: discord_id for Mike/Howard; added Winter Williams (bot-only, full trust).

Reviewed by Code Review Agent + Grok + Gemini (Gemini's "malformed email" finding
verified as a false positive).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-08 21:00:20 -07:00
parent 7fc29a7c5f
commit 2efd4a4fb3
7 changed files with 264 additions and 37 deletions

View File

@@ -33,11 +33,21 @@ def _load_system_prompt() -> str:
class ThreadAgent:
"""One persistent Claude Code session bound to a Discord thread."""
def __init__(self, system_prompt: str, cwd: Path, model: str) -> None:
def __init__(
self,
system_prompt: str,
cwd: Path,
model: str,
env: Optional[dict[str, str]] = None,
) -> None:
# `env` is per-session (per Discord thread), so concurrent threads carry
# their own requester attribution without colliding. It reaches the
# Bash tool (and thus whoami-block.sh / sync.sh) via the SDK subprocess.
self._options = ClaudeAgentOptions(
system_prompt=system_prompt,
cwd=str(cwd),
model=model,
env=env or {},
)
self._client: Optional[ClaudeSDKClient] = None
@@ -59,9 +69,39 @@ class ThreadAgent:
if self._client is None:
raise RuntimeError("ThreadAgent.send() called before start()")
try:
return await self._query_once(user_message, on_text, on_tool_use)
except Exception as e: # noqa: BLE001 — recover a dead SDK session, then retry once
if "session is closed" in str(e).lower() or "session closed" in str(e).lower():
logger.warning(
"[WARNING] SDK session was closed; reconnecting and retrying once"
)
await self._reconnect()
return await self._query_once(user_message, on_text, on_tool_use)
raise
async def _reconnect(self) -> None:
"""Tear down and re-establish the SDK session (it can close on idle)."""
try:
if self._client is not None:
await self._client.disconnect()
except Exception as e: # noqa: BLE001
logger.warning("[WARNING] disconnect during reconnect failed: %s", e)
self._client = ClaudeSDKClient(options=self._options)
await self._client.connect()
async def _query_once(
self,
user_message: str,
on_text: Callable[[str], Awaitable[None]],
on_tool_use: Optional[Callable[[str], Awaitable[None]]],
) -> str:
assert self._client is not None
await self._client.query(user_message)
full_text = ""
result_text: Optional[str] = None
result_subtype: Optional[str] = None
async for message in self._client.receive_response():
if isinstance(message, AssistantMessage):
for block in message.content:
@@ -71,9 +111,27 @@ class ThreadAgent:
elif isinstance(block, ToolUseBlock) and on_tool_use is not None:
await on_tool_use(block.name)
elif isinstance(message, ResultMessage):
# The SDK delivers the final answer here; capture it as the
# fallback when no TextBlock streamed (the cause of "(no response)").
result_text = message.result
result_subtype = message.subtype
break
return full_text
if full_text.strip():
return full_text
if result_text and result_text.strip():
return result_text
# Genuinely nothing — never leave the user with a blank "no response":
# explain why so it's actionable.
logger.warning(
"[WARNING] empty turn: no text blocks and no result (subtype=%s)",
result_subtype,
)
return (
f"[INFO] I finished without a text reply (subtype={result_subtype}). "
"I may have only run tools or hit a turn limit — ask me to summarize "
"what I found, or rephrase the question."
)
class ClaudeAgentManager:
@@ -85,11 +143,17 @@ class ClaudeAgentManager:
self._model = settings.claude_model
self._agents: dict[int, ThreadAgent] = {}
async def get_or_create(self, thread_id: int) -> ThreadAgent:
async def get_or_create(
self, thread_id: int, env: Optional[dict[str, str]] = None
) -> ThreadAgent:
# `env` is applied only when the thread's session is first created, so
# attribution pins to the thread opener (the SDK bakes env at session
# spawn and cannot change it per turn without losing context). Follow-up
# turns reuse the opener's attribution by design.
agent = self._agents.get(thread_id)
if agent is None:
logger.info("[INFO] Starting new agent session for thread %d", thread_id)
agent = ThreadAgent(self._system_prompt, self._cwd, self._model)
agent = ThreadAgent(self._system_prompt, self._cwd, self._model, env=env)
await agent.start()
self._agents[thread_id] = agent
return agent

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
import asyncio
import json
import logging
import shutil
from pathlib import Path
@@ -21,9 +22,51 @@ ATTACHMENT_ROOT = settings.claudetools_root / "projects" / "discord-bot" / ".att
class MessageHandler:
_USER_MAP: dict[str, dict] | None = None
def __init__(self, bot: discord.Client, agents: ClaudeAgentManager) -> None:
self.bot = bot
self.agents = agents
# One lock per thread serializes turns: a message that arrives while the
# bot is mid-turn (or a second user chiming in) waits and is processed
# next, in order — nothing collides on the single SDK session, nothing
# is dropped. Different threads still run concurrently (separate locks).
self._thread_locks: dict[int, asyncio.Lock] = {}
@classmethod
def _users(cls) -> dict[str, dict]:
"""discord_id -> {user, full_name} map from .claude/users.json (cached)."""
if cls._USER_MAP is None:
mapping: dict[str, dict] = {}
try:
p = settings.claudetools_root / ".claude" / "users.json"
data = json.loads(p.read_text(encoding="utf-8"))
for key, u in (data.get("users") or {}).items():
did = str(u.get("discord_id") or "").strip()
if did:
mapping[did] = {"user": key, "full_name": u.get("full_name", key)}
cls._USER_MAP = mapping # cache only on a successful load
except Exception as e: # noqa: BLE001
logger.warning("[WARNING] could not load users.json discord map: %s", e)
return mapping # do NOT cache a failed/empty load — retry next call
return cls._USER_MAP
def _requester_env(self, author: discord.abc.User) -> dict[str, str]:
"""Per-session attribution env: marks the bot as executor and the Discord
requester (mapped to a known user key when their discord_id is on file)."""
mapped = self._users().get(str(author.id))
display = getattr(author, "display_name", author.name)
if mapped:
label = f"{mapped['full_name']} (@{author.name}, via Discord)"
user_key = mapped["user"]
else:
label = f"@{author.name} (display: {display}, via Discord)"
user_key = ""
return {
"CLAUDETOOLS_ACTOR": "discord-bot",
"CLAUDETOOLS_REQUESTER": label,
"CLAUDETOOLS_REQUESTER_USER": user_key,
}
async def handle_mention(self, message: discord.Message) -> None:
if message.author == self.bot.user:
@@ -75,7 +118,12 @@ class MessageHandler:
if not content:
content = "User uploaded file(s) without a message."
await self._run_turn(thread, content)
# Attribution pins to the THREAD OPENER: this env is honored only when the
# thread's SDK session is first created and is reused for every later turn.
# If a second person posts in the same thread, the work is still credited
# to whoever opened it (a thread = one person's request — see DISCORD_CLAUDE.md).
req_env = self._requester_env(author)
await self._run_turn(thread, content, req_env)
@staticmethod
async def _download_attachments(
@@ -130,12 +178,16 @@ class MessageHandler:
name += "..."
return name or "ClaudeTools Conversation"
async def _run_turn(self, thread: discord.Thread, user_message: str) -> None:
async def _run_turn(
self,
thread: discord.Thread,
user_message: str,
env: dict[str, str] | None = None,
) -> None:
# The Claude Code CLI cold-start can take a few seconds; show feedback.
# (Shown immediately; queued turns sit here while an earlier turn runs.)
status_msg = await thread.send("[INFO] Thinking...")
agent = await self.agents.get_or_create(thread.id)
buffer: list[str] = []
last_edit = 0.0
lock = asyncio.Lock()
@@ -162,7 +214,14 @@ class MessageHandler:
logger.info("[INFO] thread=%d tool=%s", thread.id, tool_name)
try:
full_text = await agent.send(user_message, on_text, on_tool_use)
# Serialize turns within this thread: a message that lands while a
# turn is in flight (or a second user chiming in) waits here and runs
# next, in order — nothing collides on the single SDK session, nothing
# is dropped. Separate threads still run concurrently.
turn_lock = self._thread_locks.setdefault(thread.id, asyncio.Lock())
async with turn_lock:
agent = await self.agents.get_or_create(thread.id, env=env)
full_text = await agent.send(user_message, on_text, on_tool_use)
except Exception as e:
logger.exception("[ERROR] Agent turn failed in thread %d", thread.id)
await status_msg.edit(content=f"[ERROR] {e}")
@@ -179,11 +238,17 @@ class MessageHandler:
text: str,
) -> None:
chunks = self._split(text)
# Deliver the finished answer as NEW message(s) at the BOTTOM of the
# thread, like a normal human reply — do NOT edit the in-place
# "Thinking..." preview into the answer. The live edit is good for showing
# progress while working; the final answer belongs at the bottom where the
# next person expects it. (A fresh send is also what makes any <@id>
# mention actually notify the user — edits do not ping.)
try:
await status_msg.edit(content=chunks[0])
except discord.errors.NotFound:
await thread.send(chunks[0])
for chunk in chunks[1:]:
await status_msg.delete()
except discord.errors.HTTPException:
pass
for chunk in chunks:
await thread.send(chunk)
@staticmethod

View File

@@ -36,7 +36,15 @@ intents.message_content = True
intents.guilds = True
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
# allowed_mentions: permit pinging specific users (so the bot can @tag a person
# from users.json by their discord_id) but never @everyone/@here or whole roles.
bot = commands.Bot(
command_prefix="!",
intents=intents,
allowed_mentions=discord.AllowedMentions(
everyone=False, users=True, roles=False, replied_user=True
),
)
agent_manager = ClaudeAgentManager()
message_handler: MessageHandler | None = None