buddy-reroll 0.2.0 → 0.3.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/lib/wyhash.js ADDED
@@ -0,0 +1,88 @@
1
+ // Wyhash v4.2 — Pure JavaScript implementation
2
+ // Ported from @pencroff-lab/wyhash-ts (Apache-2.0)
3
+ // https://github.com/pencroff-lab/wyhash-ts
4
+ //
5
+ // Produces identical output to Bun.hash() for string inputs.
6
+
7
+ const secret = [
8
+ 0xa0761d6478bd642fn,
9
+ 0xe7037ed1a0b428dbn,
10
+ 0x8ebc6af09c88c6e3n,
11
+ 0x589965cc75374cc3n,
12
+ ];
13
+
14
+ function read(data, offset, bytes) {
15
+ let result = 0n;
16
+ for (let i = 0; i < bytes && offset + i < data.length; i++) {
17
+ result |= BigInt(data[offset + i]) << (BigInt(i) * 8n);
18
+ }
19
+ return BigInt.asUintN(64, result);
20
+ }
21
+
22
+ function mum(a, b) {
23
+ const x = a * b;
24
+ return [BigInt.asUintN(64, x), BigInt.asUintN(64, x >> 64n)];
25
+ }
26
+
27
+ function mix(a, b) {
28
+ const [aMul, bMul] = mum(a, b);
29
+ return aMul ^ bMul;
30
+ }
31
+
32
+ function sum64(seed, input) {
33
+ let a, b;
34
+ let state0 = seed ^ mix(seed ^ secret[0], secret[1]);
35
+ const len = input.length;
36
+
37
+ if (len <= 16) {
38
+ if (len >= 4) {
39
+ const end = len - 4;
40
+ const quarter = (len >> 3) << 2;
41
+ a = (read(input, 0, 4) << 32n) | read(input, quarter, 4);
42
+ b = (read(input, end, 4) << 32n) | read(input, end - quarter, 4);
43
+ } else if (len > 0) {
44
+ a = (BigInt(input[0]) << 16n) | (BigInt(input[len >> 1]) << 8n) | BigInt(input[len - 1]);
45
+ b = 0n;
46
+ } else {
47
+ a = 0n;
48
+ b = 0n;
49
+ }
50
+ } else {
51
+ const state = [state0, state0, state0];
52
+ let i = 0;
53
+
54
+ if (len >= 48) {
55
+ while (i + 48 < len) {
56
+ for (let j = 0; j < 3; j++) {
57
+ const aRound = read(input, i + 8 * (2 * j), 8);
58
+ const bRound = read(input, i + 8 * (2 * j + 1), 8);
59
+ state[j] = mix(aRound ^ secret[j + 1], bRound ^ state[j]);
60
+ }
61
+ i += 48;
62
+ }
63
+ state[0] ^= state[1] ^ state[2];
64
+ }
65
+
66
+ const remaining = input.subarray(i);
67
+ let k = 0;
68
+ while (k + 16 < remaining.length) {
69
+ state[0] = mix(read(remaining, k, 8) ^ secret[1], read(remaining, k + 8, 8) ^ state[0]);
70
+ k += 16;
71
+ }
72
+
73
+ a = read(input, len - 16, 8);
74
+ b = read(input, len - 8, 8);
75
+ state0 = state[0];
76
+ }
77
+
78
+ a ^= secret[1];
79
+ b ^= state0;
80
+ [a, b] = mum(a, b);
81
+ return mix(a ^ secret[0] ^ BigInt(len), b ^ secret[1]);
82
+ }
83
+
84
+ const encoder = new TextEncoder();
85
+
86
+ export function wyhash(seed, key) {
87
+ return sum64(BigInt.asUintN(64, seed), encoder.encode(key));
88
+ }
@@ -0,0 +1,50 @@
1
+ import { describe, it, expect } from "bun:test";
2
+ import { wyhash } from "./wyhash.js";
3
+
4
+ describe("wyhash", () => {
5
+ it("matches Bun.hash for empty string", () => {
6
+ expect(wyhash(0n, "")).toBe(BigInt(Bun.hash("")));
7
+ });
8
+
9
+ it("matches Bun.hash for short strings", () => {
10
+ const cases = ["a", "ab", "abc", "hello", "hello world"];
11
+ for (const s of cases) {
12
+ expect(wyhash(0n, s)).toBe(BigInt(Bun.hash(s)));
13
+ }
14
+ });
15
+
16
+ it("matches Bun.hash for companion-realistic inputs", () => {
17
+ const salts = ["friend-2026-401", "friend-2026-abc", "xxxxxxxxxxxxxxx"];
18
+ const userIds = ["anon", "fd50b3fd-1234-5678-9abc-def012345678"];
19
+ for (const uid of userIds) {
20
+ for (const salt of salts) {
21
+ const key = uid + salt;
22
+ expect(wyhash(0n, key)).toBe(BigInt(Bun.hash(key)));
23
+ }
24
+ }
25
+ });
26
+
27
+ it("matches Bun.hash for long strings (>48 bytes)", () => {
28
+ const long = "a".repeat(100);
29
+ expect(wyhash(0n, long)).toBe(BigInt(Bun.hash(long)));
30
+ });
31
+
32
+ it("matches Bun.hash for unicode strings", () => {
33
+ const cases = ["한글", "αβγδ", "🎮🐉✨"];
34
+ for (const s of cases) {
35
+ expect(wyhash(0n, s)).toBe(BigInt(Bun.hash(s)));
36
+ }
37
+ });
38
+
39
+ it("returns bigint", () => {
40
+ expect(typeof wyhash(0n, "test")).toBe("bigint");
41
+ });
42
+
43
+ it("is deterministic", () => {
44
+ expect(wyhash(0n, "test")).toBe(wyhash(0n, "test"));
45
+ });
46
+
47
+ it("produces different output for different inputs", () => {
48
+ expect(wyhash(0n, "hello")).not.toBe(wyhash(0n, "world"));
49
+ });
50
+ });
package/package.json CHANGED
@@ -1,18 +1,28 @@
1
1
  {
2
2
  "name": "buddy-reroll",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Reroll your Claude Code buddy companion to any species/rarity/eye/hat/shiny combo",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "buddy-reroll": "index.js"
8
8
  },
9
+ "scripts": {
10
+ "test": "bun test lib/",
11
+ "help": "bun run index.js --help",
12
+ "list": "bun run index.js --list",
13
+ "verify:runtime": "bun run scripts/verify-runtime.js",
14
+ "verify": "bun run test && bun run help && bun run list && bun run verify:runtime && npm pack --dry-run"
15
+ },
9
16
  "files": [
10
17
  "index.js",
18
+ "lib",
19
+ "scripts",
11
20
  "ui.jsx",
21
+ "ui-fallback.js",
12
22
  "sprites.js"
13
23
  ],
14
24
  "engines": {
15
- "bun": ">=1.0.0"
25
+ "node": ">=20.0.0"
16
26
  },
17
27
  "license": "MIT",
18
28
  "repository": {
@@ -27,6 +37,7 @@
27
37
  "reroll"
28
38
  ],
29
39
  "dependencies": {
40
+ "@inquirer/prompts": "^8.3.2",
30
41
  "chalk": "^5.6.2",
31
42
  "ink": "^6.8.0",
32
43
  "react": "^19.2.4"
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { execFileSync } from "child_process";
4
+ import { fileURLToPath } from "url";
5
+ import { dirname, join } from "path";
6
+ import { formatDoctorReport, getDoctorReport } from "../lib/doctor.js";
7
+
8
+ const rootDir = dirname(dirname(fileURLToPath(import.meta.url)));
9
+ const entrypoint = join(rootDir, "index.js");
10
+
11
+ function log(message) {
12
+ process.stdout.write(`${message}\n`);
13
+ }
14
+
15
+ function runCurrentSmokeCheck() {
16
+ execFileSync("bun", [entrypoint, "--current"], {
17
+ cwd: rootDir,
18
+ stdio: "inherit",
19
+ });
20
+ }
21
+
22
+ function main() {
23
+ const report = getDoctorReport();
24
+ log(formatDoctorReport(report, "Runtime verification"));
25
+
26
+ if (!report.canRunCurrent) {
27
+ log(" Skipping --current smoke check because Claude Code runtime was not fully discovered on this machine.");
28
+ process.exit(0);
29
+ }
30
+
31
+ runCurrentSmokeCheck();
32
+ }
33
+
34
+ main();
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env node
2
+ import { rollFrom, matches } from "../lib/companion.js";
3
+
4
+ const SALT_LEN = 15;
5
+ const CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
6
+ const REPORT_INTERVAL = 100_000;
7
+
8
+ function randomSalt() {
9
+ let s = "";
10
+ for (let i = 0; i < SALT_LEN; i++) {
11
+ s += CHARSET[(Math.random() * CHARSET.length) | 0];
12
+ }
13
+ return s;
14
+ }
15
+
16
+ const userId = process.argv[2];
17
+ const target = JSON.parse(process.argv[3]);
18
+
19
+ if (!userId || !target) {
20
+ process.stderr.write("Usage: worker.js <userId> '<targetJSON>'\n");
21
+ process.exit(1);
22
+ }
23
+
24
+ const start = Date.now();
25
+ let attempts = 0;
26
+
27
+ for (;;) {
28
+ attempts++;
29
+ const salt = randomSalt();
30
+ const result = rollFrom(salt, userId);
31
+
32
+ if (matches(result, target)) {
33
+ process.stdout.write(JSON.stringify({ salt, attempts, elapsed: Date.now() - start }));
34
+ process.exit(0);
35
+ }
36
+
37
+ if (attempts % REPORT_INTERVAL === 0) {
38
+ process.stderr.write(JSON.stringify({ attempts, elapsed: Date.now() - start }) + "\n");
39
+ }
40
+ }
package/ui-fallback.js ADDED
@@ -0,0 +1,181 @@
1
+ import { select, confirm } from "@inquirer/prompts";
2
+ import { existsSync, copyFileSync } from "fs";
3
+ import chalk from "chalk";
4
+ import { renderSprite, colorizeSprite, RARITY_STARS, RARITY_COLORS } from "./sprites.js";
5
+
6
+ function printSprite(roll) {
7
+ const sprite = renderSprite({ species: roll.species, eye: roll.eye, hat: roll.hat });
8
+ const colored = colorizeSprite(sprite, roll.rarity);
9
+ const colorFn = chalk[RARITY_COLORS[roll.rarity]] ?? chalk.white;
10
+ const stars = RARITY_STARS[roll.rarity] ?? "";
11
+
12
+ console.log("");
13
+ for (const line of colored) console.log(` ${line}`);
14
+ console.log(` ${chalk.bold(roll.species)} / ${colorFn(roll.rarity)}${roll.shiny ? " / ✦shiny" : ""}`);
15
+ console.log(` eye:${roll.eye} hat:${roll.hat} ${stars}`);
16
+
17
+ if (roll.stats) {
18
+ console.log("");
19
+ for (const [k, v] of Object.entries(roll.stats)) {
20
+ const filled = Math.min(10, Math.max(0, Math.round(v / 10)));
21
+ const bar = colorFn("█".repeat(filled)) + chalk.dim("░".repeat(10 - filled));
22
+ console.log(` ${k.padEnd(10)} ${bar} ${String(v).padStart(3)}`);
23
+ }
24
+ }
25
+ console.log("");
26
+ }
27
+
28
+ export async function runInteractiveUI(opts) {
29
+ const {
30
+ currentRoll, currentSalt, binaryPath, configPath, userId,
31
+ bruteForce, patchBinary, resignBinary, clearCompanion, getPatchability, isClaudeRunning,
32
+ rollFrom, matches, SPECIES, RARITIES, RARITY_LABELS, EYES, HATS, STAT_NAMES,
33
+ } = opts;
34
+
35
+ console.log(chalk.bold.dim("\n buddy-reroll\n"));
36
+ printSprite(currentRoll);
37
+
38
+ const action = await select({
39
+ message: "What would you like to do?",
40
+ choices: [
41
+ { name: "Pick a new buddy", value: "reroll" },
42
+ { name: "Go back to default", value: "restore" },
43
+ { name: "See my buddy", value: "current" },
44
+ ],
45
+ });
46
+
47
+ if (action === "current") {
48
+ console.log(chalk.green("✓ That's your buddy!"));
49
+ return;
50
+ }
51
+
52
+ if (action === "restore") {
53
+ const patchability = getPatchability(binaryPath);
54
+ if (!patchability.ok) {
55
+ console.log(chalk.red(`✗ ${patchability.message}`));
56
+ return;
57
+ }
58
+ const { backupPath } = patchability;
59
+ if (!existsSync(backupPath)) {
60
+ console.log(chalk.yellow("No backup found. Nothing to restore."));
61
+ return;
62
+ }
63
+ try {
64
+ copyFileSync(backupPath, binaryPath);
65
+ resignBinary(binaryPath);
66
+ clearCompanion(configPath);
67
+ console.log(chalk.green("✓ Restored! Restart Claude Code and say /buddy to see your original friend."));
68
+ } catch (err) {
69
+ console.log(chalk.red(`✗ ${err.message}`));
70
+ }
71
+ return;
72
+ }
73
+
74
+ const species = await select({
75
+ message: "Species",
76
+ choices: SPECIES.map((s) => ({ name: s, value: s })),
77
+ default: currentRoll.species,
78
+ });
79
+
80
+ const rarity = await select({
81
+ message: "Rarity",
82
+ choices: RARITIES.map((r) => ({ name: RARITY_LABELS[r], value: r })),
83
+ default: currentRoll.rarity,
84
+ });
85
+
86
+ const eye = await select({
87
+ message: "Eye",
88
+ choices: EYES.map((e) => ({ name: e, value: e })),
89
+ default: currentRoll.eye,
90
+ });
91
+
92
+ let hat = "none";
93
+ if (rarity !== "common") {
94
+ hat = await select({
95
+ message: "Hat",
96
+ choices: HATS.map((h) => ({ name: h, value: h })),
97
+ default: currentRoll.hat === "none" ? "crown" : currentRoll.hat,
98
+ });
99
+ }
100
+
101
+ const shiny = await confirm({ message: "Shiny?", default: false });
102
+
103
+ let peak = null;
104
+ let dump = null;
105
+ if (STAT_NAMES) {
106
+ const wantStats = await confirm({ message: "Choose your buddy's strong and weak stats?", default: false });
107
+ if (wantStats) {
108
+ peak = await select({
109
+ message: "Best at",
110
+ choices: [
111
+ { name: "Surprise me!", value: null },
112
+ ...STAT_NAMES.map((s) => ({ name: s, value: s })),
113
+ ],
114
+ });
115
+ if (peak) {
116
+ dump = await select({
117
+ message: "Worst at",
118
+ choices: [
119
+ { name: "Surprise me!", value: null },
120
+ ...STAT_NAMES.filter((s) => s !== peak).map((s) => ({ name: s, value: s })),
121
+ ],
122
+ });
123
+ }
124
+ }
125
+ }
126
+
127
+ const preview = { species, rarity, eye, hat, shiny, stats: null };
128
+ printSprite(preview);
129
+
130
+ const target = { species, rarity, eye, hat: rarity === "common" ? "none" : hat };
131
+ if (shiny) target.shiny = true;
132
+ if (peak) target.peak = peak;
133
+ if (dump) target.dump = dump;
134
+
135
+ if (matches(currentRoll, target)) {
136
+ console.log(chalk.green("✓ Your buddy already looks like that!"));
137
+ return;
138
+ }
139
+
140
+ const patchability = getPatchability(binaryPath);
141
+ if (!patchability.ok) {
142
+ console.log(chalk.red(`✗ ${patchability.message}`));
143
+ return;
144
+ }
145
+
146
+ if (isClaudeRunning()) {
147
+ console.log(chalk.yellow("⚠ Claude Code is still running — close it first so the changes stick."));
148
+ }
149
+
150
+ const proceed = await confirm({ message: "Let's go?", default: true });
151
+ if (!proceed) return;
152
+
153
+ console.log(" Looking for your buddy...");
154
+ const found = await bruteForce(userId, target, (checked, elapsed) => {
155
+ process.stdout.write(`\r ${(checked / 1e6).toFixed(1)}M tries (${(elapsed / 1000).toFixed(1)}s)`);
156
+ });
157
+
158
+ if (!found) {
159
+ console.log(chalk.red("\n✗ Couldn't find a match. Try being less picky!"));
160
+ return;
161
+ }
162
+
163
+ console.log(chalk.green(`\n✓ Found your buddy! (${found.checked.toLocaleString()} tries, ${(found.elapsed / 1000).toFixed(1)}s)`));
164
+ printSprite(found.result);
165
+
166
+ const { backupPath } = patchability;
167
+ try {
168
+ if (!existsSync(backupPath)) {
169
+ copyFileSync(binaryPath, backupPath);
170
+ console.log(` Backup saved to ${backupPath}`);
171
+ }
172
+ patchBinary(binaryPath, currentSalt, found.salt);
173
+ console.log(" Applied ✓");
174
+ if (resignBinary(binaryPath)) console.log(" Re-signed for macOS ✓");
175
+ clearCompanion(configPath);
176
+ console.log(" Cleaned up old buddy data ✓");
177
+ console.log(chalk.bold("\n All set! Restart Claude Code and say /buddy to meet your new friend.\n"));
178
+ } catch (err) {
179
+ console.log(chalk.red(`\n✗ ${err.message}`));
180
+ }
181
+ }