Skip to content

fix(discord): deduplicate bootstrap by bot token#367

Merged
JonasJesus42 merged 3 commits intomainfrom
JonasJesus42/bootstrap-dedup
Apr 7, 2026
Merged

fix(discord): deduplicate bootstrap by bot token#367
JonasJesus42 merged 3 commits intomainfrom
JonasJesus42/bootstrap-dedup

Conversation

@JonasJesus42
Copy link
Copy Markdown
Contributor

@JonasJesus42 JonasJesus42 commented Apr 7, 2026

Summary

  • Multiple connections in Supabase can share the same bot token (duplicate entries)
  • Without dedup, bootstrap started multiple Discord.js clients with the same token, causing conflicts and duplicate events
  • Now tracks started tokens with a Set and skips connections that share an already running bot
  • Config cache is still synced for all connections regardless

Test plan

  • Deploy and verify only one Discord client starts per unique bot token
  • Check logs show Skipping conn_xxx — bot already started for this token for duplicates
  • Verify config cache still works for all connections

🤖 Generated with Claude Code


Summary by cubic

Deduplicate Discord bootstrap by bot token so only one discord.js client starts per unique token; config cache still updates for every connection. Adds diagnostic logs around config save and trigger storage to help trace missing fields and credentials.

  • Bug Fixes
    • Track started tokens in a Set; skip starting a second client and log a skip message; add token only after ensureBotRunning succeeds.
    • Sync config-cache for all connections, including duplicates.
    • Add diagnostics in onChange and DISCORD_SAVE_CONFIG, and log TriggerStorage GET/SET/DELETE (present/missing fields, masked bot token, env/runtime flags; credential reads/writes on reload).

Written for commit 55d71af. Summary will update on new commits.

Multiple connections in Supabase can share the same bot token (duplicate
entries). Without dedup, the bootstrap started multiple Discord.js
clients with the same token, causing conflicts and duplicate events.

Now tracks started tokens and skips connections that share an already
running bot. Config cache is still synced for all connections.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="discord-read/server/main.ts">

<violation number="1" location="discord-read/server/main.ts:354">
P1: Skipping duplicate-token connections without registering an alias instance allows later onChange for that connection to start another Discord client with the same token.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

console.log(
`[BOOTSTRAP] Skipping ${connectionId} — bot already started for this token`,
);
continue;
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Skipping duplicate-token connections without registering an alias instance allows later onChange for that connection to start another Discord client with the same token.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At discord-read/server/main.ts, line 354:

<comment>Skipping duplicate-token connections without registering an alias instance allows later onChange for that connection to start another Discord client with the same token.</comment>

<file context>
@@ -353,13 +346,34 @@ async function bootstrapFromSupabase(): Promise<void> {
+        console.log(
+          `[BOOTSTRAP] Skipping ${connectionId} — bot already started for this token`,
+        );
+        continue;
+      }
+
</file context>
Fix with Cubic

JonasJesus42 and others added 2 commits April 7, 2026 14:50
Logs which fields are present/missing when onChange tries to save config
to Supabase, and when DISCORD_SAVE_CONFIG tool is called. This helps
debug why some connections (e.g. deco-help) fail to persist.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Logs every read/write to LazyStudioKV so we can see if TRIGGER_CONFIGURE
is saving credentials and if notify() is finding them on reload.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="discord-read/server/main.ts">

<violation number="1" location="discord-read/server/main.ts:176">
P1: Avoid logging any portion of the Discord bot token; log only presence/absence to prevent secret leakage in observability systems.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

`[CONFIG] Authorized Guilds: ${authorizedGuilds.length > 0 ? authorizedGuilds.join(", ") : "all"}`,
);
console.log(
`[CONFIG] Bot token: ${botToken ? `${botToken.slice(0, 10)}...${botToken.slice(-4)}` : "MISSING"}`,
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Avoid logging any portion of the Discord bot token; log only presence/absence to prevent secret leakage in observability systems.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At discord-read/server/main.ts, line 176:

<comment>Avoid logging any portion of the Discord bot token; log only presence/absence to prevent secret leakage in observability systems.</comment>

<file context>
@@ -169,6 +172,13 @@ const runtime = withRuntime<Env, typeof StateSchema, Registry>({
           `[CONFIG] Authorized Guilds: ${authorizedGuilds.length > 0 ? authorizedGuilds.join(", ") : "all"}`,
         );
+        console.log(
+          `[CONFIG] Bot token: ${botToken ? `${botToken.slice(0, 10)}...${botToken.slice(-4)}` : "MISSING"}`,
+        );
+      } else {
</file context>
Suggested change
`[CONFIG] Bot token: ${botToken ? `${botToken.slice(0, 10)}...${botToken.slice(-4)}` : "MISSING"}`,
`[CONFIG] Bot token: ${botToken ? "configured" : "MISSING"}`,
Fix with Cubic

@JonasJesus42 JonasJesus42 merged commit 5781f65 into main Apr 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant