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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user