claude-rpc 1.4.2 → 1.4.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/SECURITY.md +17 -0
- package/package.json +1 -1
- package/src/cli.js +66 -60
- package/src/daemon.js +7 -0
- package/src/gist.js +7 -2
- package/src/install.js +12 -0
- package/src/verify.js +239 -0
- package/src/version.js +1 -1
package/SECURITY.md
CHANGED
|
@@ -218,6 +218,23 @@ non-interactive setups are never asked. There is no private identity tier:
|
|
|
218
218
|
the only way claude-rpc ever learns a GitHub identity is this same public,
|
|
219
219
|
consented profile flow.
|
|
220
220
|
|
|
221
|
+
**Background retry of a consented verification (v1.4.3).** Answering **y**
|
|
222
|
+
(or running `profile verify`) records a local `profile.verifyPending` marker
|
|
223
|
+
before any network step. If the server can't confirm the verification right
|
|
224
|
+
then, the daemon retries the *same* gist flow in the background after its
|
|
225
|
+
profile flush — backing off from 30 minutes to 12 hours, reusing one proof
|
|
226
|
+
gist rather than publishing new ones, and going quiet after 14 days (the
|
|
227
|
+
marker keeps `gaveUpAt`; `profile verify` restarts it). It only ever proves the
|
|
228
|
+
GitHub login you connected: if the `gh` CLI is logged out or has switched to a
|
|
229
|
+
different account, it waits and sends nothing. `profile off` pauses it;
|
|
230
|
+
`link` or a successful verify clears it. Upgrading from 1.4.0–1.4.2 infers the
|
|
231
|
+
marker only for installs whose config shows setup's yes-path (asked, a GitHub
|
|
232
|
+
login stored, publishing on, never verified) — the installs whose connect
|
|
233
|
+
failed during the 2026-08-20 → 09-11 server outage. The retry adds two
|
|
234
|
+
optional fields to `/verify/check` — `resumed: true` and `pendingSince` (the
|
|
235
|
+
local consent timestamp) — which the worker uses only to back-date an
|
|
236
|
+
internal, non-public `verifiedAt` stamp.
|
|
237
|
+
|
|
221
238
|
**Claude Wrapped (opt-in one-shot, never automatic).** `claude-rpc wrapped
|
|
222
239
|
--publish` publishes a year-in-review blob under your profile handle to
|
|
223
240
|
`<endpoint>/wrapped`, rendered at `claude-rpc.com/wrapped/<handle>`. It never
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -1680,8 +1680,9 @@ async function doLink(argv) {
|
|
|
1680
1680
|
// local handle or an explicit enabled:false is always respected.
|
|
1681
1681
|
const userCfg = readJson(CONFIG_PATH, {});
|
|
1682
1682
|
const prevProfile = userCfg.profile || {};
|
|
1683
|
+
const { verifyPending: _linked, ...prevRest } = prevProfile; // a link completes any pending gist verification
|
|
1683
1684
|
userCfg.profile = {
|
|
1684
|
-
...
|
|
1685
|
+
...prevRest,
|
|
1685
1686
|
githubUser: r.json.githubUser,
|
|
1686
1687
|
verified: true,
|
|
1687
1688
|
...(prevProfile.handle ? {} : { handle: r.json.handle }),
|
|
@@ -1902,6 +1903,10 @@ function profileStatus() {
|
|
|
1902
1903
|
// for terminals with no browser nearby.
|
|
1903
1904
|
if (!steps[2].done) {
|
|
1904
1905
|
lines.push('');
|
|
1906
|
+
const vp = p.verifyPending;
|
|
1907
|
+
if (vp && typeof vp === 'object' && !vp.gaveUpAt) {
|
|
1908
|
+
lines.push(`${c.yellow}…${c.reset} ${c.dim}a GitHub verification you started is pending — the daemon retries it automatically${vp.lastError ? ` (last: ${vp.lastError})` : ''}${c.reset}`);
|
|
1909
|
+
}
|
|
1905
1910
|
lines.push(`${c.dim}the code comes from${c.reset} ${c.cyan}claude-rpc link${c.reset} ${c.dim}on a machine you already verified${c.reset}`);
|
|
1906
1911
|
lines.push(`${c.dim}first machine? log in at${c.reset} ${c.cyan}${LINK_PAGE}${c.reset} ${c.dim}— or no browser:${c.reset} ${c.cyan}claude-rpc profile verify${c.reset}`);
|
|
1907
1912
|
}
|
|
@@ -2003,19 +2008,21 @@ async function profilePublish() {
|
|
|
2003
2008
|
|
|
2004
2009
|
// The gist-verification dance shared by `profile verify` and setup's optional
|
|
2005
2010
|
// GitHub-connect question: worker token → public proof gist → worker check →
|
|
2006
|
-
// persist the verified identity locally
|
|
2007
|
-
//
|
|
2008
|
-
//
|
|
2009
|
-
//
|
|
2010
|
-
//
|
|
2011
|
+
// persist the verified identity locally (core in src/verify.js, shared with the
|
|
2012
|
+
// daemon's background resume). Prints its own progress lines (they read the
|
|
2013
|
+
// same in both contexts) and returns { ok, who } on success. It never exits:
|
|
2014
|
+
// hard failures come back as { ok: false, fatal: {label, hint, code} } so
|
|
2015
|
+
// profileVerify can fail() loudly while setup — where the whole step is
|
|
2016
|
+
// optional — shrugs and finishes the install. Callers record consent
|
|
2017
|
+
// (withVerifyIntent) first, so a failure here leaves a pending marker the
|
|
2018
|
+
// daemon keeps retrying.
|
|
2011
2019
|
async function gistVerifyDance(cfg) {
|
|
2012
2020
|
const profile = cfg.profile || {};
|
|
2013
2021
|
const community = cfg.community || {};
|
|
2014
2022
|
if (!community.instanceId) {
|
|
2015
2023
|
return { ok: false, fatal: { label: 'enable the profile first', hint: 'claude-rpc profile on', code: EX_BAD_STATE } };
|
|
2016
2024
|
}
|
|
2017
|
-
|
|
2018
|
-
if (!endpoint) {
|
|
2025
|
+
if (!(community.endpoint || '').replace(/\/+$/, '')) {
|
|
2019
2026
|
return { ok: false, fatal: {
|
|
2020
2027
|
label: 'no community endpoint configured',
|
|
2021
2028
|
hint: 'config.json is missing community.endpoint — re-run `claude-rpc setup` to restore the default',
|
|
@@ -2023,67 +2030,57 @@ async function gistVerifyDance(cfg) {
|
|
|
2023
2030
|
} };
|
|
2024
2031
|
}
|
|
2025
2032
|
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
body: JSON.stringify(body),
|
|
2031
|
-
});
|
|
2032
|
-
return { status: res.status, json: await res.json().catch(() => ({})) };
|
|
2033
|
-
};
|
|
2034
|
-
|
|
2035
|
-
try {
|
|
2036
|
-
// Make sure the profile row exists server-side before we verify it, so
|
|
2037
|
-
// verification works regardless of whether `profile publish` was run first.
|
|
2038
|
-
if (lb.profileIsPublishable(profile)) {
|
|
2033
|
+
// Make sure the profile row exists server-side before we verify it, so
|
|
2034
|
+
// verification works regardless of whether `profile publish` was run first.
|
|
2035
|
+
if (lb.profileIsPublishable(profile)) {
|
|
2036
|
+
try {
|
|
2039
2037
|
const { flushProfile } = await import('./community.js');
|
|
2040
2038
|
await flushProfile(cfg);
|
|
2039
|
+
} catch { /* the check below reports a missing row */ }
|
|
2040
|
+
}
|
|
2041
|
+
const { runGistVerify, markVerifiedLocally, updateUserConfig } = await import('./verify.js');
|
|
2042
|
+
const pending = readJson(CONFIG_PATH, {}).profile?.verifyPending;
|
|
2043
|
+
const r = await runGistVerify(cfg, {
|
|
2044
|
+
onStep: (msg) => console.log(` ${c.dim}${msg}…${c.reset}`),
|
|
2045
|
+
gistId: pending?.gistId || null,
|
|
2046
|
+
pendingSince: pending?.since ?? null,
|
|
2047
|
+
resumed: !!pending,
|
|
2048
|
+
});
|
|
2049
|
+
if (r.ok) {
|
|
2050
|
+
const who = r.githubUser;
|
|
2051
|
+
markVerifiedLocally(who);
|
|
2052
|
+
console.log(` ${c.green}✓${c.reset} verified as ${c.cyan}@${who}${c.reset} — you'll show the ✓ on the board`);
|
|
2053
|
+
if (who && profile.githubUser && who.toLowerCase() !== profile.githubUser.toLowerCase()) {
|
|
2054
|
+
console.log(` ${c.dim}(your gist is owned by @${who}, so the profile now uses that account)${c.reset}`);
|
|
2041
2055
|
}
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
console.log(` ${c.dim}publishing a public proof gist…${c.reset}`);
|
|
2051
|
-
const gist = await publishGistFile({
|
|
2052
|
-
svg: `claude-rpc leaderboard verification\n${token}\n`,
|
|
2053
|
-
filename: 'claude-rpc-verify.txt',
|
|
2054
|
-
description: 'claude-rpc profile verification',
|
|
2055
|
-
isPublic: true,
|
|
2056
|
+
return { ok: true, who };
|
|
2057
|
+
}
|
|
2058
|
+
// Remember the proof gist so a retry (ours or the daemon's) edits it
|
|
2059
|
+
// instead of publishing another one.
|
|
2060
|
+
if (r.gistId) {
|
|
2061
|
+
updateUserConfig((u) => {
|
|
2062
|
+
if (!u.profile?.verifyPending) return false;
|
|
2063
|
+
u.profile.verifyPending = { ...u.profile.verifyPending, gistId: r.gistId };
|
|
2056
2064
|
});
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
// gist-list lag) and reads the real owner — instant, and the owner becomes
|
|
2060
|
-
// the verified identity regardless of what --github was set to.
|
|
2061
|
-
console.log(` ${c.dim}confirming with the server…${c.reset}`);
|
|
2062
|
-
const check = await post('/verify/check', { instanceId: community.instanceId, gistId: gist.id });
|
|
2063
|
-
if (check.json?.verified) {
|
|
2064
|
-
const who = check.json.githubUser || gist.owner || profile.githubUser;
|
|
2065
|
-
// Persist the authoritative owner + a local verified marker so the
|
|
2066
|
-
// profile checklist and future publishes match what got verified.
|
|
2067
|
-
const userCfg = readJson(CONFIG_PATH, {});
|
|
2068
|
-
userCfg.profile = { ...(userCfg.profile || {}), ...(who ? { githubUser: who } : {}), verified: true };
|
|
2069
|
-
writeUserConfig(userCfg);
|
|
2070
|
-
console.log(` ${c.green}✓${c.reset} verified as ${c.cyan}@${who}${c.reset} — you'll show the ✓ on the board`);
|
|
2071
|
-
if (who && profile.githubUser && who.toLowerCase() !== profile.githubUser.toLowerCase()) {
|
|
2072
|
-
console.log(` ${c.dim}(your gist is owned by @${who}, so the profile now uses that account)${c.reset}`);
|
|
2073
|
-
}
|
|
2074
|
-
return { ok: true, who };
|
|
2075
|
-
}
|
|
2065
|
+
}
|
|
2066
|
+
if (r.stage === 'check' && r.status >= 400 && r.status < 500) {
|
|
2076
2067
|
// Soft outcome (matches historical behavior): the message tells the user
|
|
2077
2068
|
// how to finish, and the process still exits 0.
|
|
2078
|
-
console.log(` ${c.yellow}!${c.reset} not confirmed: ${
|
|
2069
|
+
console.log(` ${c.yellow}!${c.reset} not confirmed: ${r.error}`);
|
|
2079
2070
|
console.log(` ${c.dim}↳ make sure the gist is public, then re-run ${c.reset}${c.cyan}claude-rpc profile verify${c.reset}`);
|
|
2080
2071
|
return { ok: false };
|
|
2081
|
-
}
|
|
2072
|
+
}
|
|
2073
|
+
if (r.stage === 'gist') {
|
|
2082
2074
|
return { ok: false, fatal: {
|
|
2083
|
-
label: `verification failed: ${
|
|
2075
|
+
label: `verification failed: ${r.error}`,
|
|
2084
2076
|
hint: 'needs `gh` logged in or GH_TOKEN with gist scope', code: EX_SYS_ERROR,
|
|
2085
2077
|
} };
|
|
2086
2078
|
}
|
|
2079
|
+
return { ok: false, fatal: {
|
|
2080
|
+
label: `${r.stage === 'start' ? 'verify/start' : 'verify/check'} failed: ${r.error}`,
|
|
2081
|
+
hint: 'the server may be having trouble — the daemon retries this automatically in the background',
|
|
2082
|
+
code: EX_SYS_ERROR,
|
|
2083
|
+
} };
|
|
2087
2084
|
}
|
|
2088
2085
|
|
|
2089
2086
|
// GitHub verification: ask the worker for a one-time token, publish it in a
|
|
@@ -2097,7 +2094,12 @@ async function profileVerify() {
|
|
|
2097
2094
|
if (!cfg.profile?.githubUser) {
|
|
2098
2095
|
console.log(` ${c.dim}no --github set — your verified identity will be the account that owns the proof gist${c.reset}`);
|
|
2099
2096
|
}
|
|
2100
|
-
|
|
2097
|
+
if (cfg.community?.instanceId) {
|
|
2098
|
+
// Consent to verify; if the server can't finish it now, the daemon retries.
|
|
2099
|
+
const { withVerifyIntent } = await import('./verify.js');
|
|
2100
|
+
writeUserConfig(withVerifyIntent(readJson(CONFIG_PATH, {})));
|
|
2101
|
+
}
|
|
2102
|
+
const r = await gistVerifyDance(loadConfig());
|
|
2101
2103
|
if (r.fatal) return fail(r.fatal.label, { hint: r.fatal.hint || '', code: r.fatal.code });
|
|
2102
2104
|
}
|
|
2103
2105
|
|
|
@@ -2149,8 +2151,12 @@ async function setupGhConnect() {
|
|
|
2149
2151
|
console.log(` ${c.yellow}!${c.reset} couldn't derive a handle from @${login} — run ${c.cyan}claude-rpc profile set --handle <you>${c.reset} then ${c.cyan}claude-rpc profile verify${c.reset}`);
|
|
2150
2152
|
return;
|
|
2151
2153
|
}
|
|
2154
|
+
// The y above is consent to the whole flow, so record it before any network
|
|
2155
|
+
// step: if the server can't finish it now, the daemon retries in the
|
|
2156
|
+
// background (src/verify.js) instead of the install sitting unverified.
|
|
2152
2157
|
userCfg.profile = { ...userCfg.profile, handle, githubUser: login, enabled: true };
|
|
2153
|
-
|
|
2158
|
+
const { withVerifyIntent } = await import('./verify.js');
|
|
2159
|
+
writeUserConfig(withVerifyIntent(userCfg));
|
|
2154
2160
|
console.log(` ${c.dim}connecting as ${c.reset}${c.cyan}@${login}${c.reset}${c.dim} — handle ${c.reset}${c.cyan}${handle}${c.reset}`);
|
|
2155
2161
|
|
|
2156
2162
|
// A fresh install has no aggregate yet; publish real totals, not zeros.
|
|
@@ -2164,7 +2170,7 @@ async function setupGhConnect() {
|
|
|
2164
2170
|
console.log(` ${c.green}✓${c.reset} you're live at ${c.cyan}https://claude-rpc.com/u/${encodeURIComponent(handle)}${c.reset}`);
|
|
2165
2171
|
} else {
|
|
2166
2172
|
if (r.fatal) console.log(` ${c.yellow}!${c.reset} ${r.fatal.label}`);
|
|
2167
|
-
console.log(` ${c.dim}↳ setup itself is done —
|
|
2173
|
+
console.log(` ${c.dim}↳ setup itself is done — the daemon keeps retrying this in the background, or run ${c.reset}${c.cyan}claude-rpc profile verify${c.reset}`);
|
|
2168
2174
|
}
|
|
2169
2175
|
}
|
|
2170
2176
|
|
package/src/daemon.js
CHANGED
|
@@ -943,6 +943,13 @@ async function runCommunityFlush() {
|
|
|
943
943
|
} else if (!pr.ok && pr.reason !== 'rate-limited' && pr.reason !== 'disabled') {
|
|
944
944
|
log(`profile: ${pr.reason}${pr.error ? ' (' + pr.error + ')' : ''}`);
|
|
945
945
|
}
|
|
946
|
+
// A GitHub connect the user said yes to but the server never confirmed
|
|
947
|
+
// (e.g. the Aug–Sep 2026 KV write-cap outage) — finish it now that the
|
|
948
|
+
// row just published. Self-scheduling and a no-op without a pending marker.
|
|
949
|
+
if (pr.ok && config.profile?.verifyPending) {
|
|
950
|
+
const { resumePendingVerify } = await import('./verify.js');
|
|
951
|
+
await resumePendingVerify(config, { log });
|
|
952
|
+
}
|
|
946
953
|
}
|
|
947
954
|
} catch (e) {
|
|
948
955
|
log('community/profile flush threw:', e.message);
|
package/src/gist.js
CHANGED
|
@@ -26,12 +26,17 @@ const WIN = process.platform === 'win32';
|
|
|
26
26
|
function ghQuote(a) {
|
|
27
27
|
return /[\s"]/.test(a) ? `"${String(a).replace(/"/g, '""')}"` : a;
|
|
28
28
|
}
|
|
29
|
+
// gh can block on a network stall or a locked keyring, and the daemon now
|
|
30
|
+
// calls it too (background verification resume) — a spawnSync with no timeout
|
|
31
|
+
// would freeze its event loop, so every call is capped. A timeout surfaces as
|
|
32
|
+
// a non-zero/null status, which every caller already treats as failure.
|
|
33
|
+
const GH_TIMEOUT_MS = 30_000;
|
|
29
34
|
function gh(args, opts = {}) {
|
|
30
35
|
// windowsHide so the gh.exe/cmd shim doesn't flash a console window on
|
|
31
36
|
// Windows (shell:true routes through cmd.exe; hide that too). No-op elsewhere.
|
|
32
37
|
return WIN
|
|
33
|
-
? spawnSync('gh', args.map(ghQuote), { ...opts, shell: true, windowsHide: true })
|
|
34
|
-
: spawnSync('gh', args, { ...opts, windowsHide: true });
|
|
38
|
+
? spawnSync('gh', args.map(ghQuote), { timeout: GH_TIMEOUT_MS, ...opts, shell: true, windowsHide: true })
|
|
39
|
+
: spawnSync('gh', args, { timeout: GH_TIMEOUT_MS, ...opts, windowsHide: true });
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
// Bare fetch has no total timeout; a stalled GitHub endpoint would hang the
|
package/src/install.js
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
import { DEFAULT_CONFIG } from './default-config.js';
|
|
21
21
|
import { VERSION } from './version.js';
|
|
22
22
|
import { c, SYM_OK, SYM_WARN, SYM_FAIL, SYM_INFO, hintLine } from './ui.js';
|
|
23
|
+
import { inferLegacyVerifyIntent } from './verify.js';
|
|
23
24
|
|
|
24
25
|
const STARTUP_KEY = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run';
|
|
25
26
|
const STARTUP_VALUE = 'ClaudeRPC';
|
|
@@ -534,6 +535,17 @@ export function migrateConfig({ silent = false } = {}) {
|
|
|
534
535
|
added.push('presence.byStatus.thinking.state');
|
|
535
536
|
}
|
|
536
537
|
|
|
538
|
+
// v1.4.3: setup's "connect GitHub?" y could die on a server error (the
|
|
539
|
+
// 2026-08-20 → 09-11 KV write-cap outage 500'd /verify/start most of every
|
|
540
|
+
// day) and was never asked again. 1.4.0–1.4.2 recorded no intent, so infer
|
|
541
|
+
// it from the yes-path's footprint; the daemon then finishes the verification
|
|
542
|
+
// in the background (src/verify.js). Undated — the worker back-dates to the
|
|
543
|
+
// profile's first successful write instead.
|
|
544
|
+
if (inferLegacyVerifyIntent(cfg.profile)) {
|
|
545
|
+
cfg.profile.verifyPending = { since: null };
|
|
546
|
+
added.push('profile.verifyPending (resume GitHub connect)');
|
|
547
|
+
}
|
|
548
|
+
|
|
537
549
|
// v0.7: community.enabled flipped to true in DEFAULT_CONFIG. For users
|
|
538
550
|
// upgrading from a version without a community block, we must NOT
|
|
539
551
|
// silently turn telemetry on — write an explicit `enabled: false` so
|
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
|
+
}
|