claude-rpc 1.4.2 → 1.5.0
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/README.md +16 -2
- package/SECURITY.md +28 -1
- package/package.json +1 -1
- package/src/calendar.js +35 -81
- package/src/claude-proc.js +12 -6
- package/src/cli.js +95 -68
- package/src/community.js +27 -1
- package/src/daemon.js +7 -0
- package/src/default-config.js +3 -0
- package/src/fmt.js +3 -3
- package/src/gist.js +7 -2
- package/src/heatmap.js +342 -0
- package/src/install.js +12 -0
- package/src/mcp.js +2 -1
- package/src/recap.js +2 -1
- package/src/server/assets/dashboard.client.js +2 -1
- package/src/server/assets/wrapped.client.js +2 -1
- package/src/verify.js +239 -0
- package/src/version.js +1 -1
package/src/verify.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// GitHub gist verification — the core shared by `profile verify` and setup's
|
|
2
|
+
// "connect GitHub?" question (both interactive, in cli.js) and by the daemon's
|
|
3
|
+
// background resume of a verification that didn't go through.
|
|
4
|
+
//
|
|
5
|
+
// Why the resume exists: from 2026-08-20 to 2026-09-11 the worker's KV write
|
|
6
|
+
// quota ran out by ~05:00 UTC every day, so /verify/start 500'd for anyone who
|
|
7
|
+
// answered "y" later in the day. Setup asks exactly once, so those installs sat
|
|
8
|
+
// unverified for good while their daemons kept publishing an unverified row.
|
|
9
|
+
// Consent is now recorded locally as `profile.verifyPending`, and the daemon
|
|
10
|
+
// finishes the same flow the user already agreed to once the server answers.
|
|
11
|
+
|
|
12
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
|
|
13
|
+
import { dirname } from 'node:path';
|
|
14
|
+
import { CONFIG_PATH } from './paths.js';
|
|
15
|
+
import { renameSyncRetry } from './atomic-rename.js';
|
|
16
|
+
import { normalizeGithubUser, profileIsPublishable } from './leaderboard.js';
|
|
17
|
+
|
|
18
|
+
const FETCH_TIMEOUT_MS = 10_000;
|
|
19
|
+
export const PROOF_FILENAME = 'claude-rpc-verify.txt';
|
|
20
|
+
|
|
21
|
+
// Daemon retry schedule: 30 min doubling up to a 12 h ceiling, for 14 days
|
|
22
|
+
// from the first background attempt — long enough to outlast a multi-day
|
|
23
|
+
// outage (this one ran three weeks, but writes came back for a few hours
|
|
24
|
+
// every night), short enough that a permanently broken setup goes quiet.
|
|
25
|
+
export const RESUME_BASE_MS = 30 * 60_000;
|
|
26
|
+
export const RESUME_MAX_MS = 12 * 3_600_000;
|
|
27
|
+
export const RESUME_WINDOW_MS = 14 * 86_400_000;
|
|
28
|
+
|
|
29
|
+
export function resumeBackoffMs(attempts) {
|
|
30
|
+
return Math.min(RESUME_MAX_MS, RESUME_BASE_MS * 2 ** Math.max(0, attempts - 1));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 1.4.0–1.4.2 recorded no intent, but setup's yes-path leaves a footprint no
|
|
34
|
+
// other flow does on its own: asked + publishing on + a GitHub login stored +
|
|
35
|
+
// never verified. migrateConfig turns that into an explicit (undated) pending
|
|
36
|
+
// marker once; a marker that exists in any state is never re-inferred.
|
|
37
|
+
export function inferLegacyVerifyIntent(profile) {
|
|
38
|
+
const p = profile || {};
|
|
39
|
+
return !!(p.ghConnectAsked && p.enabled === true && normalizeGithubUser(p.githubUser)
|
|
40
|
+
&& !p.verified && !p.verifyPending);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Record (or refresh) consent to verify on a raw user-config object. Keeps the
|
|
44
|
+
// earliest consent time and any proof gist, but restarts the retry budget —
|
|
45
|
+
// the user just asked again, so a previous give-up no longer applies.
|
|
46
|
+
export function withVerifyIntent(userCfg, now = Date.now()) {
|
|
47
|
+
const profile = userCfg.profile || {};
|
|
48
|
+
const prev = profile.verifyPending && typeof profile.verifyPending === 'object' ? profile.verifyPending : {};
|
|
49
|
+
const since = Number.isFinite(prev.since) ? Math.min(prev.since, now) : now;
|
|
50
|
+
userCfg.profile = {
|
|
51
|
+
...profile,
|
|
52
|
+
verifyPending: { since, ...(prev.gistId ? { gistId: prev.gistId } : {}) },
|
|
53
|
+
};
|
|
54
|
+
return userCfg;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Should the daemon try a background verification now? { go } or { go:false, why }.
|
|
58
|
+
export function resumeDecision(profile, now = Date.now()) {
|
|
59
|
+
const p = profile || {};
|
|
60
|
+
const vp = p.verifyPending;
|
|
61
|
+
if (!vp || typeof vp !== 'object') return { go: false, why: 'none' };
|
|
62
|
+
if (p.verified) return { go: false, why: 'verified' };
|
|
63
|
+
if (vp.gaveUpAt) return { go: false, why: 'gave-up' };
|
|
64
|
+
// Publishing turned off (or no handle) → the row this would verify isn't
|
|
65
|
+
// being kept; wait rather than burn the retry window.
|
|
66
|
+
if (!profileIsPublishable(p)) return { go: false, why: 'profile-off' };
|
|
67
|
+
if (vp.firstTryAt && now - vp.firstTryAt > RESUME_WINDOW_MS) return { go: false, why: 'expired' };
|
|
68
|
+
if (vp.nextAt && now < vp.nextAt) return { go: false, why: 'backoff' };
|
|
69
|
+
return { go: true };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// One pass of the dance: worker token → public proof gist → worker check.
|
|
73
|
+
// Never throws and never prints (onStep narrates for the CLI). Returns
|
|
74
|
+
// { ok: true, githubUser, merged, handle, gistId }
|
|
75
|
+
// { ok: false, stage: 'config'|'start'|'gist'|'check', status, error, gistId }
|
|
76
|
+
// `gistId` reuses the proof gist of an earlier attempt (edited, not
|
|
77
|
+
// re-created). `resumed` + `pendingSince` tell the worker this completes an
|
|
78
|
+
// earlier consent so it can back-date the verification to it.
|
|
79
|
+
export async function runGistVerify(cfg, {
|
|
80
|
+
fetchImpl = globalThis.fetch,
|
|
81
|
+
publish = null,
|
|
82
|
+
onStep = () => {},
|
|
83
|
+
gistId = null,
|
|
84
|
+
pendingSince = null,
|
|
85
|
+
resumed = false,
|
|
86
|
+
} = {}) {
|
|
87
|
+
const instanceId = cfg?.community?.instanceId;
|
|
88
|
+
const endpoint = (cfg?.community?.endpoint || '').replace(/\/+$/, '');
|
|
89
|
+
if (!instanceId) return { ok: false, stage: 'config', status: 0, error: 'no-instance-id' };
|
|
90
|
+
if (!endpoint) return { ok: false, stage: 'config', status: 0, error: 'no-endpoint' };
|
|
91
|
+
|
|
92
|
+
const post = async (path, body) => {
|
|
93
|
+
try {
|
|
94
|
+
const res = await fetchImpl(endpoint + path, {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
headers: { 'Content-Type': 'application/json' },
|
|
97
|
+
body: JSON.stringify(body),
|
|
98
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
99
|
+
});
|
|
100
|
+
return { status: res.status, json: await res.json().catch(() => ({})) };
|
|
101
|
+
} catch (e) {
|
|
102
|
+
return { status: 0, json: { error: e.message } };
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const errOf = (r) => r.json?.error || (r.status ? `http-${r.status}` : 'network');
|
|
106
|
+
|
|
107
|
+
onStep('requesting a verification token');
|
|
108
|
+
const start = await post('/verify/start', { instanceId, githubUser: cfg.profile?.githubUser || null });
|
|
109
|
+
if (!start.json?.token) return { ok: false, stage: 'start', status: start.status, error: errOf(start), gistId };
|
|
110
|
+
|
|
111
|
+
onStep('publishing a public proof gist');
|
|
112
|
+
let gist;
|
|
113
|
+
try {
|
|
114
|
+
const pub = publish || (await import('./gist.js')).publishGistFile;
|
|
115
|
+
const content = `claude-rpc leaderboard verification\n${start.json.token}\n`;
|
|
116
|
+
const fresh = () => pub({ svg: content, filename: PROOF_FILENAME, description: 'claude-rpc profile verification', isPublic: true });
|
|
117
|
+
if (gistId) {
|
|
118
|
+
// A deleted or foreign gist can't be edited; fall back to a new one.
|
|
119
|
+
try { gist = await pub({ svg: content, filename: PROOF_FILENAME, gistId }); }
|
|
120
|
+
catch { gist = await fresh(); }
|
|
121
|
+
} else {
|
|
122
|
+
gist = await fresh();
|
|
123
|
+
}
|
|
124
|
+
} catch (e) {
|
|
125
|
+
return { ok: false, stage: 'gist', status: 0, error: e.message, gistId };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The worker fetches THAT gist by id (no gist-list lag) and takes its real
|
|
129
|
+
// owner as the verified identity.
|
|
130
|
+
onStep('confirming with the server');
|
|
131
|
+
const check = await post('/verify/check', {
|
|
132
|
+
instanceId,
|
|
133
|
+
gistId: gist.id,
|
|
134
|
+
...(resumed ? { resumed: true } : {}),
|
|
135
|
+
...(Number.isFinite(pendingSince) ? { pendingSince } : {}),
|
|
136
|
+
});
|
|
137
|
+
if (!check.json?.verified) return { ok: false, stage: 'check', status: check.status, error: errOf(check), gistId: gist.id };
|
|
138
|
+
return {
|
|
139
|
+
ok: true,
|
|
140
|
+
githubUser: check.json.githubUser || gist.owner || cfg.profile?.githubUser || null,
|
|
141
|
+
merged: !!check.json.merged,
|
|
142
|
+
handle: check.json.handle || null,
|
|
143
|
+
gistId: gist.id,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Read-modify-write the raw user config (never the defaults-merged view), via
|
|
148
|
+
// tmp + rename so the daemon's config watcher can't read a half-written file.
|
|
149
|
+
// `mutate` returns false to skip the write.
|
|
150
|
+
export function updateUserConfig(mutate, { path = CONFIG_PATH } = {}) {
|
|
151
|
+
let cfg = {};
|
|
152
|
+
if (existsSync(path)) {
|
|
153
|
+
try { cfg = JSON.parse(readFileSync(path, 'utf8')); } catch { return false; } // unreadable → don't clobber it
|
|
154
|
+
}
|
|
155
|
+
if (!cfg || typeof cfg !== 'object') return false;
|
|
156
|
+
if (mutate(cfg) === false) return false;
|
|
157
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
158
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
159
|
+
writeFileSync(tmp, JSON.stringify(cfg, null, 2) + '\n');
|
|
160
|
+
renameSyncRetry(tmp, path);
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Persist a confirmed verification locally: authoritative login + verified
|
|
165
|
+
// marker, pending intent cleared.
|
|
166
|
+
export function markVerifiedLocally(githubUser, { path = CONFIG_PATH } = {}) {
|
|
167
|
+
return updateUserConfig((cfg) => {
|
|
168
|
+
const { verifyPending: _done, ...rest } = cfg.profile || {};
|
|
169
|
+
cfg.profile = { ...rest, ...(githubUser ? { githubUser } : {}), verified: true };
|
|
170
|
+
}, { path });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Merge fields into an existing pending marker; no-op if it's gone (another
|
|
174
|
+
// process verified or cleared it meanwhile).
|
|
175
|
+
function patchPending(fields, path) {
|
|
176
|
+
return updateUserConfig((cfg) => {
|
|
177
|
+
const vp = cfg.profile?.verifyPending;
|
|
178
|
+
if (!vp || typeof vp !== 'object' || cfg.profile.verified) return false;
|
|
179
|
+
cfg.profile.verifyPending = { ...vp, ...fields };
|
|
180
|
+
}, { path });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Daemon entry point — called after a successful profile flush, so the row
|
|
184
|
+
// being verified exists. At most one attempt per call; the schedule lives in
|
|
185
|
+
// the pending marker itself. Returns { attempted, ok?, why? }.
|
|
186
|
+
export async function resumePendingVerify(config, {
|
|
187
|
+
log = () => {},
|
|
188
|
+
now = Date.now,
|
|
189
|
+
ghLoginImpl = null,
|
|
190
|
+
fetchImpl = globalThis.fetch,
|
|
191
|
+
publish = null,
|
|
192
|
+
configPath = CONFIG_PATH,
|
|
193
|
+
} = {}) {
|
|
194
|
+
// The schedule and outcome live on disk and the daemon's in-memory config
|
|
195
|
+
// only catches up via its file watcher — read the marker fresh so a missed
|
|
196
|
+
// watch event can't re-run an attempt inside its backoff (or after success).
|
|
197
|
+
let disk = {};
|
|
198
|
+
try { disk = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf8')) : {}; } catch { /* fall back to memory */ }
|
|
199
|
+
const profile = { ...(config?.profile || {}), ...(disk?.profile || {}) };
|
|
200
|
+
if (disk?.profile) profile.verifyPending = disk.profile.verifyPending; // cleared on disk = cleared
|
|
201
|
+
const t = now();
|
|
202
|
+
const decision = resumeDecision(profile, t);
|
|
203
|
+
if (!decision.go) {
|
|
204
|
+
if (decision.why === 'expired') {
|
|
205
|
+
patchPending({ gaveUpAt: t }, configPath);
|
|
206
|
+
log('profile: gave up retrying GitHub verification after 14 days — run `claude-rpc profile verify` to try again');
|
|
207
|
+
}
|
|
208
|
+
return { attempted: false, why: decision.why };
|
|
209
|
+
}
|
|
210
|
+
const vp = profile.verifyPending;
|
|
211
|
+
|
|
212
|
+
// The gist owner becomes the verified identity, so only ever prove the
|
|
213
|
+
// account the user connected: if gh has since been switched to another
|
|
214
|
+
// account, wait instead of verifying that one.
|
|
215
|
+
const want = normalizeGithubUser(profile.githubUser);
|
|
216
|
+
let have = null;
|
|
217
|
+
try { have = normalizeGithubUser((ghLoginImpl || (await import('./gist.js')).ghLogin)()); } catch { /* treated as logged out */ }
|
|
218
|
+
let r;
|
|
219
|
+
if (!have) r = { ok: false, stage: 'gh', error: 'gh CLI missing or logged out' };
|
|
220
|
+
else if (want && want.toLowerCase() !== have.toLowerCase()) r = { ok: false, stage: 'gh', error: `gh is logged in as @${have}, not @${want}` };
|
|
221
|
+
else r = await runGistVerify({ ...config, profile }, { fetchImpl, publish, gistId: vp.gistId || null, pendingSince: vp.since ?? null, resumed: true });
|
|
222
|
+
|
|
223
|
+
if (r.ok) {
|
|
224
|
+
markVerifiedLocally(r.githubUser, { path: configPath });
|
|
225
|
+
log(`profile: finished the GitHub verification that didn't go through earlier — verified as @${r.githubUser}${r.merged ? ` (merged into @${r.handle})` : ''}`);
|
|
226
|
+
return { attempted: true, ok: true };
|
|
227
|
+
}
|
|
228
|
+
const attempts = (vp.attempts || 0) + 1;
|
|
229
|
+
const wait = resumeBackoffMs(attempts);
|
|
230
|
+
patchPending({
|
|
231
|
+
attempts,
|
|
232
|
+
firstTryAt: vp.firstTryAt || t,
|
|
233
|
+
nextAt: t + wait,
|
|
234
|
+
lastError: `${r.stage}: ${r.error}`.slice(0, 200),
|
|
235
|
+
...(r.gistId ? { gistId: r.gistId } : {}),
|
|
236
|
+
}, configPath);
|
|
237
|
+
log(`profile: GitHub verification retry ${attempts} failed (${r.stage}: ${r.error}) — next try in ${Math.round(wait / 60_000)} min`);
|
|
238
|
+
return { attempted: true, ok: false, stage: r.stage };
|
|
239
|
+
}
|