- apps/cli/ is now the canonical CLI (was apps/cli-v2/). - apps/cli/ legacy v0 archived as branch 'legacy-cli-archive' and tag 'cli-v0-legacy-final' before deletion; git history preserves it too. - .github/workflows/release-cli.yml paths updated. - pnpm-lock.yaml regenerated. Broker-side peer-grant enforcement (spec: 2026-04-15-per-peer-capabilities): - 0020_peer-grants.sql adds peer_grants jsonb + GIN index on mesh.member. - handleSend in broker fetches recipient grant maps once per send, drops messages silently when sender lacks the required capability. - POST /cli/mesh/:slug/grants to update from CLI; broker_messages_dropped_by_grant_total metric. - CLI grant/revoke/block now mirror to broker via syncToBroker. Auto-migrate on broker startup: - apps/broker/src/migrate.ts runs drizzle migrate with pg_advisory_lock before the HTTP server binds. Exits non-zero on failure so Coolify healthcheck fails closed. - Dockerfile copies packages/db/migrations into /app/migrations. - postgres 3.4.5 added as direct broker dep. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
90 lines
2.9 KiB
TypeScript
90 lines
2.9 KiB
TypeScript
/**
|
|
* `claudemesh sync` — re-sync meshes from dashboard account.
|
|
*
|
|
* Opens browser for OAuth, receives sync token, calls broker /cli-sync,
|
|
* merges new meshes into local config.
|
|
*/
|
|
|
|
import { createInterface } from "node:readline";
|
|
import { hostname } from "node:os";
|
|
import { readConfig, writeConfig } from "~/services/config/facade.js";
|
|
import { startCallbackListener, generatePairingCode, syncWithBroker } from "~/services/auth/facade.js";
|
|
import { openBrowser } from "~/services/spawn/facade.js";
|
|
import { generateKeypair } from "~/services/crypto/facade.js";
|
|
|
|
export async function runSync(args: { force?: boolean }): Promise<void> {
|
|
const useColor = !process.env.NO_COLOR && process.env.TERM !== "dumb" && process.stdout.isTTY;
|
|
const dim = (s: string): string => (useColor ? `\x1b[2m${s}\x1b[22m` : s);
|
|
const green = (s: string): string => (useColor ? `\x1b[32m${s}\x1b[39m` : s);
|
|
|
|
const config = readConfig();
|
|
|
|
const code = generatePairingCode();
|
|
const listener = await startCallbackListener();
|
|
const url = `https://claudemesh.com/cli-auth?port=${listener.port}&code=${code}&action=sync`;
|
|
|
|
console.log(`Opening browser to sync meshes...`);
|
|
console.log(dim(`Visit: ${url}`));
|
|
await openBrowser(url);
|
|
|
|
// Race: localhost callback vs manual paste vs timeout
|
|
const manualPromise = new Promise<string>((resolve) => {
|
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
rl.question("Paste sync token (or wait for browser): ", (answer) => {
|
|
rl.close();
|
|
if (answer.trim()) resolve(answer.trim());
|
|
});
|
|
});
|
|
|
|
const timeoutPromise = new Promise<null>((resolve) => {
|
|
setTimeout(() => resolve(null), 15 * 60_000);
|
|
});
|
|
|
|
const syncToken = await Promise.race([
|
|
listener.token,
|
|
manualPromise,
|
|
timeoutPromise,
|
|
]);
|
|
|
|
listener.close();
|
|
|
|
if (!syncToken) {
|
|
console.error("Timed out waiting for sign-in.");
|
|
process.exit(1);
|
|
}
|
|
|
|
// Use existing keypair from first mesh, or generate new
|
|
const keypair = config.meshes.length > 0
|
|
? { publicKey: config.meshes[0]!.pubkey, secretKey: config.meshes[0]!.secretKey }
|
|
: await generateKeypair();
|
|
|
|
const displayName = config.displayName ?? `${hostname()}-${process.pid}`;
|
|
|
|
const result = await syncWithBroker(syncToken, keypair.publicKey, displayName);
|
|
|
|
// Merge: add new meshes, skip duplicates
|
|
let added = 0;
|
|
for (const m of result.meshes) {
|
|
if (config.meshes.some(existing => existing.meshId === m.mesh_id)) continue;
|
|
config.meshes.push({
|
|
meshId: m.mesh_id,
|
|
memberId: m.member_id,
|
|
slug: m.slug,
|
|
name: m.slug,
|
|
pubkey: keypair.publicKey,
|
|
secretKey: keypair.secretKey,
|
|
brokerUrl: m.broker_url,
|
|
joinedAt: new Date().toISOString(),
|
|
});
|
|
added++;
|
|
}
|
|
config.accountId = result.account_id;
|
|
writeConfig(config);
|
|
|
|
if (added > 0) {
|
|
console.log(green(`✓ Added ${added} new mesh(es)`));
|
|
} else {
|
|
console.log(`Already up to date (${config.meshes.length} meshes)`);
|
|
}
|
|
}
|