Skip to content

Instantly share code, notes, and snippets.

@syabro
Last active January 25, 2026 06:22
Show Gist options
  • Select an option

  • Save syabro/0ca27bdfebf2ca1592808e0617a1ae92 to your computer and use it in GitHub Desktop.

Select an option

Save syabro/0ca27bdfebf2ca1592808e0617a1ae92 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
/**
* Claude Code Swarm Mode Patcher
*
* Enables native multi-agent features (TeammateTool, delegate mode, swarm spawning)
* by patching the tengu_brass_pebble statsig gate to always return true.
*
* Got the main code from https://x.com/NicerInPerson/status/2014989679796347375
*
* Usage:
* node patch-swarm.mjs
*
* WORKS ONLY FOR NODE CLAUDE VERSION!
* If you have standalone installed remove it and `npm install -g @anthropic-ai/claude-code`
*/
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
import readline from 'node:readline';
// === Swarm Mode Patch Logic ===
const SWARM_GATE_MARKER = /tengu_brass_pebble/;
const SWARM_GATE_FN_RE =
/function\s+([a-zA-Z_$][\w$]*)\(\)\{if\([\w$]+\(process\.env\.CLAUDE_CODE_AGENT_SWARMS\)\)return!1;return\s*[\w$]+\("tengu_brass_pebble",!1\)\}/;
const findSwarmGateFunction = (content) => {
if (!SWARM_GATE_MARKER.test(content)) return null;
const match = content.match(SWARM_GATE_FN_RE);
if (!match) return null;
return { fnName: match[1], fullMatch: match[0] };
};
const detectSwarmModeState = (content) => {
const gate = findSwarmGateFunction(content);
if (gate) return 'disabled';
if (!SWARM_GATE_MARKER.test(content)) {
const hasSwarmCode = /TeammateTool|teammate_mailbox|launchSwarm/.test(content);
if (hasSwarmCode) return 'enabled';
return 'unknown';
}
return 'unknown';
};
const setSwarmModeEnabled = (content) => {
const gate = findSwarmGateFunction(content);
if (!gate) {
const currentState = detectSwarmModeState(content);
return { content, changed: false, state: currentState };
}
const patched = content.replace(gate.fullMatch, `function ${gate.fnName}(){return!0}`);
return { content: patched, changed: true, state: 'enabled' };
};
// === CLI Discovery ===
const findClaudePath = () => {
try {
const claudePath = execSync('which claude', { encoding: 'utf8' }).trim();
if (!claudePath) return null;
return fs.realpathSync(claudePath);
} catch {
return null;
}
};
const findCliJs = (claudePath) => {
// claude binary -> resolve symlinks -> find cli.js in the package
let dir = path.dirname(claudePath);
// Walk up to find node_modules/@anthropic-ai/claude-code/cli.js
for (let i = 0; i < 10; i++) {
const cliJs = path.join(dir, 'cli.js');
if (fs.existsSync(cliJs)) {
const content = fs.readFileSync(cliJs, 'utf8');
if (content.includes('@anthropic-ai/claude-code') || content.includes('TeammateTool') || content.includes('tengu_brass_pebble')) {
return cliJs;
}
}
// Check if we're in node_modules structure
const packageJson = path.join(dir, 'package.json');
if (fs.existsSync(packageJson)) {
try {
const pkg = JSON.parse(fs.readFileSync(packageJson, 'utf8'));
if (pkg.name === '@anthropic-ai/claude-code') {
const cliPath = path.join(dir, 'cli.js');
if (fs.existsSync(cliPath)) return cliPath;
}
} catch {}
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
};
// === User Interaction ===
const askQuestion = (question) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim().toLowerCase());
});
});
};
// === Main ===
const main = async () => {
console.log('Claude Code Swarm Mode Patcher\n');
// Step 1: Find claude
console.log('Looking for claude...');
const claudePath = findClaudePath();
if (!claudePath) {
console.error('Error: claude not found. Make sure it\'s installed and in PATH.');
process.exit(1);
}
console.log(`Found claude: ${claudePath}`);
// Step 2: Find cli.js
console.log('\nLooking for cli.js...');
const cliJsPath = findCliJs(claudePath);
if (!cliJsPath) {
console.error('Error: cli.js not found in claude installation.');
process.exit(1);
}
console.log(`Found cli.js: ${cliJsPath}`);
// Step 3: Check current state
console.log('\nAnalyzing swarm mode state...');
const content = fs.readFileSync(cliJsPath, 'utf8');
const state = detectSwarmModeState(content);
console.log(`Current state: ${state}`);
if (state === 'enabled') {
console.log('\nSwarm mode is already enabled. Nothing to do.');
process.exit(0);
}
if (state === 'unknown') {
console.error('\nError: Cannot detect swarm gate. This version may not be supported (need 2.1.16+).');
process.exit(1);
}
// Step 4: Ask user
console.log('\nSwarm mode enables:');
console.log(' - TeammateTool for team coordination');
console.log(' - Delegate mode for Task tool');
console.log(' - Swarm spawning via ExitPlanMode');
console.log(' - Teammate mailbox/messaging');
console.log(' - Task ownership and claiming\n');
const answer = await askQuestion('Patch cli.js to enable swarm mode? (y/N): ');
if (answer !== 'y' && answer !== 'yes') {
console.log('Aborted.');
process.exit(0);
}
// Step 5: Create backup and patch
const backupPath = `${cliJsPath}.backup`;
if (!fs.existsSync(backupPath)) {
console.log(`\nCreating backup: ${backupPath}`);
fs.copyFileSync(cliJsPath, backupPath);
}
console.log('Applying patch...');
const result = setSwarmModeEnabled(content);
if (!result.changed) {
console.error('Error: Patch failed - no changes made.');
process.exit(1);
}
fs.writeFileSync(cliJsPath, result.content);
// Step 6: Verify
const verifyContent = fs.readFileSync(cliJsPath, 'utf8');
const verifyState = detectSwarmModeState(verifyContent);
if (verifyState !== 'enabled') {
console.error('Error: Patch verification failed.');
process.exit(1);
}
console.log('\nSwarm mode enabled successfully!');
console.log(`Backup saved at: ${backupPath}`);
};
main().catch((err) => {
console.error('Error:', err.message);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment