Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/parallel-execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,39 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

/**
* Check if file is a native binary (Mach-O, ELF, etc.) vs JavaScript
* @param {string} filePath - Path to file
* @returns {boolean} true if native binary
*/
function isNativeBinary(filePath) {
try {
const magic = Buffer.alloc(4);
const fd = fs.openSync(filePath, 'r');
fs.readSync(fd, magic, 0, 4, 0);
fs.closeSync(fd);
const hex = magic.toString('hex');
return hex === 'cffaedfe' || hex === 'cafebabe' || hex === '7f454c46';
} catch {
return false;
}
}

/**
* Detect if TeammateTool is available in current Claude Code installation
* Auto-enables if possible
*/
export function detectTeammateTool() {
try {
// Find Claude Code installation
const claudePath = findClaudeCodeBinary();
if (!claudePath) {
return { available: false, reason: 'Claude Code binary not found' };
}

// Read CLI content
if (isNativeBinary(claudePath)) {
return { available: false, reason: 'Native binary detected - patching not supported', isNative: true };
}

const cliContent = fs.readFileSync(claudePath, 'utf8');

// Check for TeammateTool presence
Expand Down Expand Up @@ -130,6 +150,10 @@ export function enableTeammateTool() {
throw new Error('Claude Code binary not found');
}

if (isNativeBinary(claudePath)) {
return { success: false, error: 'Native binary detected - patching not supported. TeammateTool patching only works with npm-installed Claude Code.' };
}

// Backup original
const backupPath = `${claudePath}.backup-${Date.now()}`;
fs.copyFileSync(claudePath, backupPath);
Expand Down
59 changes: 59 additions & 0 deletions test/unit/parallel-execution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ describe('Parallel Execution', () => {
expect(result.reason).toContain('binary not found');
});

it('should detect native Mach-O binary and skip patching', () => {
execSync.mockImplementation((cmd) => {
if (cmd.includes('npm root -g')) {
return '/usr/local/lib/node_modules\n';
}
if (cmd.includes('which') || cmd.includes('where')) {
return '/usr/local/bin/claude\n';
}
throw new Error('Unexpected command');
});

fs.existsSync.mockReturnValue(true);
fs.realpathSync.mockImplementation((p) => p);
fs.openSync.mockReturnValue(42);
fs.readSync.mockImplementation((fd, buffer) => {
buffer[0] = 0xcf;
buffer[1] = 0xfa;
buffer[2] = 0xed;
buffer[3] = 0xfe;
return 4;
});
fs.closeSync.mockReturnValue(undefined);

const result = detectTeammateTool();

expect(result.available).toBe(false);
expect(result.reason).toContain('Native binary');
expect(result.isNative).toBe(true);
});

it('should detect when TeammateTool code is not present (old version)', () => {
// Mock execSync completely - no real calls
execSync.mockImplementation((cmd) => {
Expand Down Expand Up @@ -127,6 +157,35 @@ describe('Parallel Execution', () => {
});

describe('enableTeammateTool', () => {
it('should reject native Mach-O binary', () => {
execSync.mockImplementation((cmd) => {
if (cmd.includes('npm root -g')) {
return '/usr/local/lib/node_modules\n';
}
if (cmd.includes('which') || cmd.includes('where')) {
return '/usr/local/bin/claude\n';
}
throw new Error('Unexpected command');
});

fs.existsSync.mockReturnValue(true);
fs.realpathSync.mockImplementation((p) => p);
fs.openSync.mockReturnValue(42);
fs.readSync.mockImplementation((fd, buffer) => {
buffer[0] = 0xcf;
buffer[1] = 0xfa;
buffer[2] = 0xed;
buffer[3] = 0xfe;
return 4;
});
fs.closeSync.mockReturnValue(undefined);

const result = enableTeammateTool();

expect(result.success).toBe(false);
expect(result.error).toContain('Native binary');
});

it('should create backup before patching', () => {
execSync.mockReturnValue('/usr/local/lib/node_modules');
fs.existsSync.mockReturnValue(true);
Expand Down