Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
feat(hosts): multi-host init orchestrator (detect/explicit/all selection)
Shrish Dwivedic16bc3c0232c
diff --git a/src/hosts/init.ts b/src/hosts/init.tsnew file mode 100644index 00000000..02d67ca6--- /dev/null+++ b/src/hosts/init.ts@@ -0,0 +1,63 @@+/**+ * Multi-host init: write each selected host's instruction file.+ * Selection = explicit ids > all > detected. Claude Code is handled+ * separately by src/claude/init.ts (hooks + statusline + skill).+ */+import { statSync, writeFileSync, readFileSync, mkdirSync, existsSync } from 'node:fs';+import { dirname, join } from 'node:path';+import { homedir } from 'node:os';+import { HOSTS, detectHosts, type DetectProbe, type HostTarget } from './registry.js';+import { upsertSection } from './sections.js';++export interface HostsInitResult {+ written: { id: string; path: string; action: string }[];+ skipped: string[];+ unknown: string[];+}++function probeFor(home: string, repo: string): DetectProbe {+ return {+ home, repo,+ dirExists: (p) => { try { return statSync(p).isDirectory(); } catch { return false; } },+ };+}++function writeOwned(path: string, content: string): string {+ if (existsSync(path) && readFileSync(path, 'utf8') === content) return 'unchanged';+ mkdirSync(dirname(path), { recursive: true });+ const existed = existsSync(path);+ writeFileSync(path, content);+ return existed ? 'replaced' : 'created';+}++export function runHostsInit(+ repo: string,+ opts: { agents?: string[]; all?: boolean; home?: string } = {},+): HostsInitResult {+ const home = opts.home ?? homedir();+ const probe = probeFor(home, repo);++ let selected: HostTarget[];+ let unknown: string[] = [];+ if (opts.agents?.length) {+ const byId = new Map(HOSTS.map((h) => [h.id, h]));+ selected = opts.agents.flatMap((id) => byId.get(id) ?? []);+ unknown = opts.agents.filter((id) => !byId.has(id));+ } else if (opts.all) {+ selected = HOSTS;+ } else {+ selected = detectHosts(probe);+ }++ const written: HostsInitResult['written'] = [];+ for (const host of selected) {+ const path = join(repo, host.relPath);+ const action =+ host.kind === 'owned'+ ? writeOwned(path, host.content())+ : upsertSection(path, host.content()).action;+ written.push({ id: host.id, path, action });+ }+ const skipped = HOSTS.filter((h) => !selected.includes(h)).map((h) => h.id);+ return { written, skipped, unknown };+}diff --git a/test/hosts-init.test.ts b/test/hosts-init.test.tsnew file mode 100644index 00000000..a167bdf3--- /dev/null+++ b/test/hosts-init.test.ts@@ -0,0 +1,48 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { runHostsInit } from '../src/hosts/init.js';++function fresh(): string { return mkdtempSync(join(tmpdir(), 'graft-hostsinit-')); }++test('writes only detected hosts by default', () => {+ const home = fresh(); const repo = fresh();+ mkdirSync(join(home, '.cursor'));+ const r = runHostsInit(repo, { home });+ assert.deepEqual(r.written.map((w) => w.id), ['cursor']);+ const mdc = readFileSync(join(repo, '.cursor', 'rules', 'graft.mdc'), 'utf8');+ assert.match(mdc, /alwaysApply: true/);+ assert.ok(!existsSync(join(repo, 'AGENTS.md')));+});++test('explicit agents list overrides detection and flags unknown ids', () => {+ const home = fresh(); const repo = fresh();+ const r = runHostsInit(repo, { home, agents: ['gemini', 'nope'] });+ assert.deepEqual(r.written.map((w) => w.id), ['gemini']);+ assert.deepEqual(r.unknown, ['nope']);+ assert.ok(readFileSync(join(repo, 'GEMINI.md'), 'utf8').includes('graft ask'));+});++test('all writes every host and re-run converges (idempotent)', () => {+ const home = fresh(); const repo = fresh();+ const first = runHostsInit(repo, { home, all: true });+ assert.equal(first.written.length, 6);+ const second = runHostsInit(repo, { home, all: true });+ assert.ok(second.written.every((w) => w.action === 'unchanged'));+ const agents = readFileSync(join(repo, 'AGENTS.md'), 'utf8');+ assert.equal(agents.match(/graft:start/g)!.length, 1);+});++test('preserves user content around the fenced section', () => {+ const home = fresh(); const repo = fresh();+ const target = join(repo, '.github', 'copilot-instructions.md');+ mkdirSync(join(repo, '.github'), { recursive: true });+ writeFileSync(target, '# House rules\n');+ const r = runHostsInit(repo, { home });+ assert.deepEqual(r.written.map((w) => w.id), ['copilot']);+ const text = readFileSync(target, 'utf8');+ assert.ok(text.startsWith('# House rules'));+ assert.ok(text.includes('graft ask'));+});