refactor(#1679): ADR-1239 Phase B — collapse getConfigDirFromHome chain into getGlobalConfigHomeFragment [AC2 slice 2/6] (#1801)

* refactor(#1679): ADR-1239 Phase B — collapse getConfigDirFromHome chain into getGlobalConfigHomeFragment

AC2 slice 2. Collapses the 14-branch runtime->global-config-home source-fragment
chain in bin/install.js getConfigDirFromHome (the hook path.join() codegen mapping)
into a single getGlobalConfigHomeFragment(runtime) lookup in runtime-name-policy.cts,
sibling to getDirName / getRuntimeLabel.

The antigravity branch stays dynamic in the caller (resolveAntigravityGlobalDir +
path.relative — env-overridable, multi-segment); the prior inner unreachable
`if (!isGlobal) return "'agents'"` (dead: !isGlobal returns at the fn top) is dropped.

Behavior: byte-identical. Fragments preserved verbatim in the table.
- claude/unknown/empty -> default '.claude' fragment
- 14 runtimes (copilot..kimi) -> table lookup
- antigravity -> dynamic (unchanged)

Verification:
- TDD: tests/global-config-home-fragment.test.cjs (golden map + 2 drift guards +
  fallbacks). Red->green.
- 16-runtime golden install parity: byte-identical (hook codegen output unchanged)
- eslint + test-file-count + regression-names clean
- runtime === count in install.js: 115 -> 101 (-14)

* chore(changeset): add Changed fragment for getConfigDirFromHome collapse (#1679)
This commit is contained in:
Tom Boucher
2026-06-28 00:26:13 -04:00
committed by GitHub
parent ad79e99fc9
commit 4810bfe4df
4 changed files with 159 additions and 22 deletions

View File

@@ -0,0 +1,7 @@
---
type: Changed
pr: 1801
---
**Internal: the installer's runtime → global-config-home hook-pathogen fragment is now a single `getGlobalConfigHomeFragment` lookup** — the 14-branch `if (runtime === 'x') return "'...'"` chain in `getConfigDirFromHome` (`bin/install.js`, the hook `path.join()` codegen mapping) is collapsed into one table in `runtime-name-policy.cts`, sibling to `getRuntimeLabel` (ADR-1239 Phase B, #1679 AC2 slice 2). Generated hook output is byte-identical for all 16 runtimes (golden-parity asserted); antigravity's dynamic env-overridable resolution is preserved in the caller. No user-facing change.
<!-- docs-exempt: internal refactor; hook codegen path-fragment table, no user-facing doc surface -->

View File

@@ -37,7 +37,7 @@ const {
// installer to the runtime-name-policy leaf (ADR-1508 / #1510 Phase 1) so the
// conversion module's rewrite engine can consume it without importing
// bin/install.js. Re-exported below for back-compat consumers/tests.
const { getDirName, getRuntimeLabel } = require('../gsd-core/bin/lib/runtime-name-policy.cjs');
const { getDirName, getRuntimeLabel, getGlobalConfigHomeFragment } = require('../gsd-core/bin/lib/runtime-name-policy.cjs');
const {
applyWorktreeBaseRef,
readBaseRefFromSettings,
@@ -510,18 +510,11 @@ function getConfigDirFromHome(runtime, isGlobal) {
// Local installs use the same dir name pattern
return `'${getDirName(runtime)}'`;
}
// Global installs - OpenCode uses XDG path structure
if (runtime === 'copilot') return "'.copilot'";
if (runtime === 'opencode') {
// OpenCode: ~/.config/opencode -> '.config', 'opencode'
// Return as comma-separated for path.join() replacement
return "'.config', 'opencode'";
}
if (runtime === 'gemini') return "'.gemini'";
if (runtime === 'kilo') return "'.config', 'kilo'";
if (runtime === 'codex') return "'.codex'";
// Global installs. antigravity's home is resolved dynamically (env-overridable,
// multi-segment via resolveAntigravityGlobalDir + path.relative) — not a table
// entry. (The prior inner `if (!isGlobal) return "'.agents'"` was unreachable:
// !isGlobal returns at the top of this function.)
if (runtime === 'antigravity') {
if (!isGlobal) return "'.agents'";
const antigravityDir = resolveAntigravityGlobalDir();
const rel = path.relative(os.homedir(), antigravityDir);
const segments = rel.split(path.sep).filter(Boolean);
@@ -532,16 +525,9 @@ function getConfigDirFromHome(runtime, isGlobal) {
// stable legacy template so generated path.join() calls remain valid.
return "'.gemini', 'antigravity'";
}
if (runtime === 'cursor') return "'.cursor'";
if (runtime === 'windsurf') return "'.windsurf'";
if (runtime === 'augment') return "'.augment'";
if (runtime === 'trae') return "'.trae'";
if (runtime === 'qwen') return "'.qwen'";
if (runtime === 'hermes') return "'.hermes'";
if (runtime === 'codebuddy') return "'.codebuddy'";
if (runtime === 'cline') return "'.cline'";
if (runtime === 'kimi') return "'.config', 'agents'";
return "'.claude'";
// All other runtimes: single source-of-truth fragment table (ADR-1239 Phase B,
// #1679). claude/unknown fall through to the table's default '.claude'.
return getGlobalConfigHomeFragment(runtime);
}
/**

View File

@@ -208,3 +208,52 @@ export function getRuntimeLabel(runtime: string): string {
const label = RUNTIME_LABELS[runtime];
return typeof label === 'string' && label.length > 0 ? label : 'Claude Code';
}
/**
* Source-string fragments for the runtime → global config-home path, used by
* `getConfigDirFromHome` in bin/install.js to template `path.join()` calls in
* generated hook scripts. Each value is a JS-source snippet (embedded quotes /
* commas are intentional — it is spliced into generated code as path.join args).
*
* Collapses the prior 14-branch `if (runtime === 'x') return "'...'"` chain in
* bin/install.js (ADR-1239 Phase B / #1679, AC2 slice 2) — the add-a-host tax:
* a new runtime meant remembering to add a branch here. Values are preserved
* BYTE-FOR-BYTE from the prior chain; golden install parity asserts generated
* hook output is unchanged across all 16 runtimes.
*
* Two runtimes are intentionally absent (handled by the caller, NOT this table):
* - `claude` → the default; falls through to `DEFAULT_FRAGMENT`.
* - `antigravity`→ resolved dynamically via resolveAntigravityGlobalDir +
* path.relative (multi-segment, env-overridable).
*
* Unknown/empty ids fall back to the default (`.claude`).
*/
const DEFAULT_CONFIG_HOME_FRAGMENT = "'.claude'";
const GLOBAL_CONFIG_HOME_FRAGMENTS: Readonly<Record<string, string>> = {
copilot: "'.copilot'",
opencode: "'.config', 'opencode'",
gemini: "'.gemini'",
kilo: "'.config', 'kilo'",
codex: "'.codex'",
cursor: "'.cursor'",
windsurf: "'.windsurf'",
augment: "'.augment'",
trae: "'.trae'",
qwen: "'.qwen'",
hermes: "'.hermes'",
codebuddy: "'.codebuddy'",
cline: "'.cline'",
kimi: "'.config', 'agents'",
};
/**
* Return the global config-home path-fragment source snippet for a runtime
* (for hook path.join() codegen). `claude`/unknown/empty → the default
* `'.claude'` fragment. `antigravity` is NOT handled here (caller resolves it
* dynamically). Pure: no I/O. Sibling to `getDirName` / `getRuntimeLabel`.
*/
export function getGlobalConfigHomeFragment(runtime: string): string {
if (!runtime) return DEFAULT_CONFIG_HOME_FRAGMENT;
const frag = GLOBAL_CONFIG_HOME_FRAGMENTS[runtime];
return typeof frag === 'string' && frag.length > 0 ? frag : DEFAULT_CONFIG_HOME_FRAGMENT;
}

View File

@@ -0,0 +1,95 @@
'use strict';
/**
* Drift-guard + collapse: getGlobalConfigHomeFragment must be the SINGLE source
* of truth for the runtime → global config-home path-fragment mapping used by
* `getConfigDirFromHome` in bin/install.js for hook path.join() codegen.
*
* Collapses the prior 14-branch `if (runtime === 'x') return "'...'"` chain
* (install.js ~514-543) into one table — ADR-1239 Phase B / #1679, AC2 slice 2.
*
* Invariants pinned here:
* 1. Each of the 14 table runtimes returns its exact verbatim source fragment
* (byte-identical to the prior chain — golden install parity asserts the
* generated hook output is unchanged).
* 2. claude + unknown + empty fall back to the default "'.claude'" fragment.
* 3. antigravity is intentionally NOT in the table (handled dynamically by the
* caller via resolveAntigravityGlobalDir).
* 4. Drift guard: every registry runtime EXCEPT {claude, antigravity} has a
* table entry — so adding a runtime forces a deliberate fragment decision
* here (the add-a-host tax this collapse removes).
*
* ADR-1239 Phase B (#1679). Behavioral tests only.
*/
const { test } = require('node:test');
const assert = require('node:assert/strict');
const runtimeNamePolicy = require('../gsd-core/bin/lib/runtime-name-policy.cjs');
const registry = require('../gsd-core/bin/lib/capability-registry.cjs');
const { getGlobalConfigHomeFragment } = runtimeNamePolicy;
// Golden oracle: the exact source-string fragments the prior install.js chain
// emitted, preserved verbatim. These are path.join() arg-source snippets (the
// embedded quotes/commas are intentional — they get spliced into generated hook
// scripts). Pinned here as the test oracle.
const GOLDEN_FRAGMENT_MAP = {
copilot: "'.copilot'",
opencode: "'.config', 'opencode'",
gemini: "'.gemini'",
kilo: "'.config', 'kilo'",
codex: "'.codex'",
cursor: "'.cursor'",
windsurf: "'.windsurf'",
augment: "'.augment'",
trae: "'.trae'",
qwen: "'.qwen'",
hermes: "'.hermes'",
codebuddy: "'.codebuddy'",
cline: "'.cline'",
kimi: "'.config', 'agents'",
};
// Runtimes intentionally NOT in the table: claude is the default; antigravity is
// resolved dynamically by the caller (resolveAntigravityGlobalDir + path.relative).
const SPECIAL_CASED = new Set(['claude', 'antigravity']);
test('getGlobalConfigHomeFragment: golden map matches for all 14 table runtimes', () => {
for (const [id, expected] of Object.entries(GOLDEN_FRAGMENT_MAP)) {
const actual = getGlobalConfigHomeFragment(id);
assert.strictEqual(
actual,
expected,
`getGlobalConfigHomeFragment('${id}') diverged from golden.\n` +
` actual: ${JSON.stringify(actual)}\n` +
` expected: ${JSON.stringify(expected)}`,
);
}
});
test('getGlobalConfigHomeFragment fallback: claude/unknown/empty return the default fragment', () => {
assert.strictEqual(getGlobalConfigHomeFragment('claude'), "'.claude'",
'claude must return the default fragment (it is special-cased as the default)');
assert.strictEqual(getGlobalConfigHomeFragment('unknown'), "'.claude'",
'unknown runtime must return the default fragment');
assert.strictEqual(getGlobalConfigHomeFragment(''), "'.claude'",
'empty input must return the default fragment');
});
test('drift guard: every registry runtime except {claude, antigravity} has a table entry (add-a-host tax removed)', () => {
// A newly-added registry runtime that is NOT claude/antigravity MUST get a
// deliberate fragment entry here — otherwise it silently falls through to the
// '.claude' default. claude (default) and antigravity (caller-dynamic) are the
// only legitimate absences.
const tableIds = new Set(Object.keys(GOLDEN_FRAGMENT_MAP));
const missing = Object.keys(registry.runtimes)
.filter((id) => !SPECIAL_CASED.has(id) && !tableIds.has(id));
assert.deepEqual(missing, [],
`registry runtimes missing a fragment table entry (add one to GOLDEN_FRAGMENT_MAP + the module table, or add to SPECIAL_CASED if caller-dynamic): ${missing.join(', ')}`);
});
test('drift guard: table keys are a subset of the registry (no stale entries)', () => {
const registryIds = new Set(Object.keys(registry.runtimes));
const stale = Object.keys(GOLDEN_FRAGMENT_MAP).filter((id) => !registryIds.has(id));
assert.deepEqual(stale, [],
`fragment table references runtimes not in the registry (stale entries): ${stale.join(', ')}`);
});