claude-rpc 1.7.2 → 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-rpc",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "Discord Rich Presence for Claude Code — live model, project, tokens, and lifetime stats driven by Claude Code's hook system.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/daemon.js CHANGED
@@ -4,7 +4,7 @@ import { existsSync, unlinkSync, watch, appendFileSync, mkdirSync, statSync, ren
4
4
  import { basename, dirname, join } from 'node:path';
5
5
  import { Client, isBridgeUser } from './discord-ipc.js';
6
6
  import { readState, sweepStaleStateTmp, listSessionStates, sweepStaleSessionStates } from './state.js';
7
- import { makeRotationCursor, pickFrames, selectFrame, resolveLargeImageKey, shouldShowGithubButton, pickActiveSession, throttleDecision } from './presence.js';
7
+ import { makeRotationCursor, pickFrames, selectFrame, resolveLargeImageKey, shouldShowGithubButton, pickActiveSession, throttleDecision, reconnectPlan, RECONNECT_BASE_MS } from './presence.js';
8
8
  import { buildVars, fillTemplate, framePasses, applyIdle, applyShipped, applyTrigger, humanProject } from './format.js';
9
9
  import { scan, readAggregate, findLiveSessions, readSessionTokens, readSessionModel } from './scanner.js';
10
10
  import { detectClaudeProcess } from './claude-proc.js';
@@ -139,14 +139,15 @@ let recentSends = [];
139
139
  // Tracked separately from the render hash so we fire once per transition.
140
140
  let lastNotifiedStatus = null;
141
141
  let reconnectTimer = null;
142
- // Exponential backoff for Discord reconnect: 5s → 10s → 20s → … → 300s cap.
143
- // Reset to RECONNECT_BASE_MS on a successful connect so the next outage
144
- // also starts gentle. Jitter (±30%) keeps multiple daemons (e.g. a user
145
- // running both packaged and dev simultaneously) from synchronizing
146
- // reconnect storms against Discord's IPC socket.
147
- const RECONNECT_BASE_MS = 5_000;
148
- const RECONNECT_CAP_MS = 300_000;
142
+ // Current Discord reconnect step — pacing lives in presence.reconnectPlan
143
+ // (fast flat poll while Discord isn't running, exponential backoff to 5min
144
+ // when a live client keeps failing). Reset on a successful connect.
149
145
  let reconnectDelayMs = RECONNECT_BASE_MS;
146
+ // The "Discord isn't running" poll is every ~15s; logging each miss would
147
+ // write ~5,700 lines a day. One line per this interval keeps the log quiet
148
+ // while still landing inside the tail doctor reads for IPC state.
149
+ const ABSENT_LOG_EVERY_MS = 5 * 60_000;
150
+ let lastAbsentLogAt = 0;
150
151
  // Rotation cursor (index + lastAt + status). selectFrame in presence.js resets
151
152
  // it on a status transition — otherwise the cursor carries over from idle's
152
153
  // 12-frame rotation into a single-frame working state and back, producing a
@@ -622,6 +623,7 @@ async function connect() {
622
623
  connected = true;
623
624
  // Reset backoff so the next outage also starts at RECONNECT_BASE_MS.
624
625
  reconnectDelayMs = RECONNECT_BASE_MS;
626
+ lastAbsentLogAt = 0;
625
627
  bridgeClient = isBridgeUser(client.user);
626
628
  if (bridgeClient) {
627
629
  // Doctor greps this line: "connected as" feeds ipcStateFromLog, "arrpc"
@@ -641,24 +643,26 @@ async function connect() {
641
643
  });
642
644
  try { await self.login(); }
643
645
  catch (e) {
644
- if (client === self) scheduleReconnect(`Discord login failed: ${e.message}`);
646
+ // ECONNREFUSED is login()'s "no IPC socket accepted" — Discord isn't
647
+ // running, as opposed to a client that answered and then failed.
648
+ if (client === self) scheduleReconnect(`Discord login failed: ${e.message}`, { absent: e.code === 'ECONNREFUSED' });
645
649
  } finally {
646
650
  if (client === self) connecting = false;
647
651
  }
648
652
  }
649
653
 
650
- function scheduleReconnect(reason = 'reconnect') {
654
+ function scheduleReconnect(reason = 'reconnect', { absent = false } = {}) {
651
655
  connected = false;
652
656
  if (reconnectTimer) return;
653
- // ±30% jitter on the current step. Cheap protection against
654
- // synchronized reconnect storms from sibling daemons.
655
- const jitter = 0.7 + Math.random() * 0.6;
656
- const wait = Math.round(reconnectDelayMs * jitter);
657
- log(`${reason} — retry in ${Math.round(wait / 1000)}s. Is Discord desktop running?`);
658
- reconnectTimer = setTimeout(() => { reconnectTimer = null; connect(); }, wait);
659
- // Step the base for the *next* failure. Cap at 5min so a long Discord
660
- // outage doesn't push us into multi-hour silences.
661
- reconnectDelayMs = Math.min(RECONNECT_CAP_MS, reconnectDelayMs * 2);
657
+ const { waitMs, nextDelayMs } = reconnectPlan({ delayMs: reconnectDelayMs, absent });
658
+ const now = Date.now();
659
+ if (!absent || now - lastAbsentLogAt >= ABSENT_LOG_EVERY_MS) {
660
+ // Doctor greps "retry in Ns" (ipcStateFromLog) — keep the token if rewording.
661
+ log(`${reason} — retry in ${Math.round(waitMs / 1000)}s. Is Discord desktop running?`);
662
+ if (absent) lastAbsentLogAt = now;
663
+ }
664
+ reconnectTimer = setTimeout(() => { reconnectTimer = null; connect(); }, waitMs);
665
+ reconnectDelayMs = nextDelayMs;
662
666
  }
663
667
 
664
668
  function watchFiles() {
package/src/presence.js CHANGED
@@ -162,6 +162,31 @@ export function throttleDecision({ hash, lastSentHash, lastSentAt, now, gapMs, f
162
162
  return { action: 'defer', waitMs };
163
163
  }
164
164
 
165
+ // Discord reconnect pacing. Two different outages, two different caps:
166
+ //
167
+ // absent — no IPC socket accepted at all (Discord isn't running). A probe is
168
+ // a handful of local connects that fail instantly and touches no
169
+ // Discord process, so we keep polling fast. Otherwise someone who
170
+ // opens Discord after Claude Code waits out a multi-minute backoff
171
+ // and reads it as "presence doesn't work".
172
+ // present — a socket answered and then failed (handshake error, drop). Here
173
+ // the backoff protects a struggling client, so it climbs to 5min.
174
+ //
175
+ // `delayMs` is the current step; the caller stores `nextDelayMs` for the next
176
+ // failure and resets to RECONNECT_BASE_MS on a successful connect. ±30% jitter
177
+ // keeps sibling daemons from retrying in lockstep.
178
+ export const RECONNECT_BASE_MS = 5_000;
179
+ export const RECONNECT_CAP_MS = 300_000;
180
+ export const RECONNECT_ABSENT_CAP_MS = 15_000;
181
+ export function reconnectPlan({ delayMs, absent, rand = Math.random() }) {
182
+ const cap = absent ? RECONNECT_ABSENT_CAP_MS : RECONNECT_CAP_MS;
183
+ const step = Math.min(cap, Math.max(RECONNECT_BASE_MS, delayMs || 0));
184
+ return {
185
+ waitMs: Math.round(step * (0.7 + rand * 0.6)),
186
+ nextDelayMs: Math.min(cap, step * 2),
187
+ };
188
+ }
189
+
165
190
  // Large-image key precedence (returns the TEMPLATE string; the caller fills it):
166
191
  // 1. statusAssets[status] per-status art ("working" gif, etc.)
167
192
  // 2. modelAssets[fable|opus|sonnet|haiku|default] per-model art, never stale
package/src/version.js CHANGED
@@ -11,7 +11,7 @@ import { readFileSync } from 'node:fs';
11
11
  import { join } from 'node:path';
12
12
  import { ROOT } from './paths.js';
13
13
 
14
- const BAKED = '1.7.2';
14
+ const BAKED = '1.7.3';
15
15
 
16
16
  function readPkgVersion() {
17
17
  try {