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

@@ -66,6 +66,32 @@ purge_garbled_paths() {
# then vault) before any commit happens.
reconcile_git_identity() {
local want_name="$1" want_email="$2" cur
# Bot-context override: when invoked by the Discord bot, attribute the COMMIT
# to the human who requested it (git AUTHOR = mapped requester from users.json)
# with "ClaudeTools Bot" as the COMMITTER. Unmapped/unknown requester falls
# back to bot-as-author. Strict no-op when CLAUDETOOLS_ACTOR is unset, so
# interactive sessions keep identity.json attribution.
if [ "${CLAUDETOOLS_ACTOR:-}" = "discord-bot" ]; then
local _bot_id
_bot_id=$("${PYTHON:-python}" - "$REPO_ROOT/.claude/users.json" "${CLAUDETOOLS_REQUESTER_USER:-}" <<'BOTID'
import json, sys
usersp, ukey = sys.argv[1], sys.argv[2]
name, email = "ClaudeTools Bot", "bot@azcomputerguru.com"
if ukey:
try:
u = json.load(open(usersp))["users"].get(ukey, {})
name = u.get("git_name") or u.get("full_name") or name
email = u.get("git_email") or u.get("email") or email
except Exception:
pass
print(name + "|" + email)
BOTID
)
want_name="${_bot_id%%|*}"
want_email="${_bot_id##*|}"
export GIT_COMMITTER_NAME="ClaudeTools Bot"
export GIT_COMMITTER_EMAIL="bot@azcomputerguru.com"
fi
if [ -n "$want_name" ]; then
cur=$(git config user.name 2>/dev/null || true)
if [ "$cur" != "$want_name" ]; then

View File

@@ -30,6 +30,34 @@ if [ -z "$PYTHON" ]; then
exit 0
fi
# Bot-context override: the Discord bot sets CLAUDETOOLS_ACTOR=discord-bot plus
# the requester it is acting for (CLAUDETOOLS_REQUESTER / _USER, per session).
# Attribute the log to the BOT as executor and the human requester as originator.
# Strict no-op when the env is unset — interactive sessions are unaffected.
if [ "${CLAUDETOOLS_ACTOR:-}" = "discord-bot" ]; then
"$PYTHON" - "$ID" "$USERS" <<'BOTEOF'
import json, os, sys
idp, usersp = sys.argv[1], sys.argv[2]
try:
machine = json.load(open(idp)).get("machine", "unknown")
except Exception:
machine = "unknown"
requester = os.environ.get("CLAUDETOOLS_REQUESTER", "an unrecognized Discord user")
ukey = os.environ.get("CLAUDETOOLS_REQUESTER_USER", "")
role = ""
if ukey:
try:
role = json.load(open(usersp))["users"].get(ukey, {}).get("role", "")
except Exception:
pass
print("## User")
print(f"- **Executed by:** ClaudeTools Discord Bot ({machine})")
print(f"- **Requested by:** {requester}" + (f" - {role}" if role else ""))
print("- **Role:** automation (acting on the requester's behalf)")
BOTEOF
exit 0
fi
"$PYTHON" - "$ID" "$USERS" <<'PYEOF'
import json, sys, socket, re
idp, usersp = sys.argv[1], sys.argv[2]

View File

@@ -13,6 +13,7 @@
],
"git_name": "Mike Swanson",
"git_email": "mike@azcomputerguru.com",
"discord_id": "264814939619721216",
"notes": "Owner. Full access to everything. Primary machine: GURU-5070 (as of 2026-05-25). Previous machine DESKTOP-0O8A1RL retired."
},
"howard": {
@@ -26,6 +27,7 @@
],
"git_name": "Howard Enos",
"git_email": "howard@azcomputerguru.com",
"discord_id": "624667664501178379",
"gitea_username": "howard",
"notes": "Employee, Mike's brother. Full trust. Same access as Mike for MSP tracking and daily work. Has own Gitea account (howard) with admin access to all repos. Password rotated 2026-04-21 — stored in Howard's 1Password, not in this file."
},
@@ -38,6 +40,18 @@
"discord_id": "261978810713505792",
"known_machines": [],
"notes": "Web developer contractor. No direct ClaudeTools CLI access. Interacts only through the Discord bot. Authorized scope: M365/365 remediations (remediation-tool skill), IX hosting changes (DNS, cPanel accounts, file management on IX/Websvr), Syncro read. Cannot modify bot behavior, skills, CLAUDE.md, DISCORD_CLAUDE.md, users.json, vault entries, or git history."
},
"winter": {
"full_name": "Winter Williams",
"email": "wwilliams@azcomputerguru.com",
"role": "tech",
"title": "Syncro SME (Discord bot only)",
"syncro_user_id": 1737,
"discord_id": "624666486362996755",
"git_name": "Winter Williams",
"git_email": "wwilliams@azcomputerguru.com",
"known_machines": [],
"notes": "Full trust. Go-to SME for Syncro / ticketing — defer Syncro decisions to her. Interacts ONLY through the Discord bot; no installed Claude CLI sessions."
}
},
"roles": {

View File

@@ -98,34 +98,41 @@ For every request, work this loop:
name, ID) to determine who is asking, and address them by name.
2. **Do the work** — perform the action or answer the question. Ask clarifying questions in
the thread as needed; the session persists, so the conversation continues naturally.
3. **Anything else?** — when the task is done, ask: "Anything else for this one?" Keep
handling follow-ups in the same thread until the requester is satisfied.
4. **Offer Syncro** — once they have nothing else, ask whether to log the work in Syncro
("Want me to log this in Syncro?"). If yes, invoke `/syncro` to create or update the ticket.
5. **Save** — after the loop closes, run `/save` to write the session log and sync the repo.
6. **Kill the thread** — after `/save` completes successfully, delete the thread:
```bash
bash C:/Users/guru/ClaudeTools/projects/discord-bot/scripts/delete-thread.sh <Thread ID>
```
The Thread ID is in every `[DISCORD_CONTEXT]` block as `Thread ID: <id>`. Do not delete
if `/save` failed or errored. Do not post a closing message — the deletion is immediate.
3. **Anything else?** — when the task is done, ask "Anything else for this one?" and keep
handling follow-ups in the same thread. A directly-connected second topic stays in the
SAME thread/session; only a genuinely unrelated request warrants a fresh thread.
4. **Close the loop — match the capture to the work:**
- **Pure Q&A / read-only / nothing changed in the repo** → do NOT run `/save`. Append a
one-line entry to the rolling bot log
`session-logs/bot/<YYYY-MM>/<YYYY-MM-DD>-bot-activity.md` (create the month folder if
needed): `HH:MM PT - <requester> - <topic> - <outcome / links>`. The Discord thread holds
the full detail, and the on-disk transcript is recoverable via `/recover` if a full
narrative is ever needed. No Syncro prompt unless the work is billable.
- **Substantive work** (changed a client record, infra, a ticket, or repo files) → offer
Syncro if it is billable/ticketable, then run `/save` to write a full session log to the
correct client/project location.
5. **Sync**`/save` already syncs. For the one-line rolling-log case a `/sync` is enough, and
batching is fine (the periodic sync sweeps it up). Never push empty commits for pure Q&A.
6. **Keep the thread** — never auto-delete. The thread is the conversation record. Delete only
if the requester explicitly asks (`scripts/delete-thread.sh <Thread ID>`).
**Attribution is automatic — do not set it manually.** Each thread runs with session env
(`CLAUDETOOLS_ACTOR=discord-bot`, `CLAUDETOOLS_REQUESTER`, `CLAUDETOOLS_REQUESTER_USER`) derived
from the `[DISCORD_CONTEXT]` sender. So `/save`'s User block renders as "Executed by: ClaudeTools
Discord Bot / Requested by: <them>", and commits are authored as the mapped requester with the
bot as committer. Attribution **pins to the thread opener**: if a second person posts in someone
else's thread, the work is still credited to whoever started the thread (a thread = one person's
request).
---
## Thread Lifecycle — Auto-Delete on Completion
## Thread Lifecycle — Threads Are Kept (No Auto-Delete)
Every `[DISCORD_CONTEXT]` block now includes `Thread ID: <id>` (injected by the bot after
the thread is resolved or created). This ID is what you pass to the delete script.
Threads are the durable conversation record and are **NOT auto-deleted** on completion.
Leave every thread in place after the task and `/save` finish.
**When to delete:** Only after step 6 of the Task Loop — `/save` succeeded, session log
committed and pushed. The thread is the conversation record; don't kill it before the log lands.
**When NOT to delete:**
- `/save` failed or sync errored
- The user said "yes" to continuing (open follow-up items remain)
- The thread is an ongoing informational channel, not a single-task session
**Script:**
**Delete ONLY on explicit request** — if the requester says to delete/close the thread,
pass the `Thread ID` (in every `[DISCORD_CONTEXT]` block) to the delete script:
```bash
bash C:/Users/guru/ClaudeTools/projects/discord-bot/scripts/delete-thread.sh <Thread ID>
```
@@ -275,6 +282,21 @@ This creates an audit trail and keeps the repo in sync.
---
## Tagging a user (@mention)
To actually notify someone in Discord, emit a real mention `<@THEIR_DISCORD_ID>` — NOT
literal "@name" text (plain "@winter" pings no one). Discord IDs live in `.claude/users.json`
under each user's `discord_id`:
- Mike `<@264814939619721216>` - Howard `<@624667664501178379>` - Winter
`<@624666486362996755>` - Rob `<@261978810713505792>`
Read `users.json` for any ID you do not have. Tag only when it serves the task (e.g. "this
needs <@...>'s sign-off") — never gratuitously, and never `@everyone`/`@here` (the bot blocks
those). A reply containing a tag is posted as a fresh message so the ping actually lands.
---
## Local Machine Rules (BEAST)
- Working directory: `C:/Users/guru/ClaudeTools`

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