feat(config): apply ~/.gsd/defaults.json as fallback for pre-project commands (#1738)
* feat(config): apply ~/.gsd/defaults.json as fallback for pre-project commands (#1683) When .planning/config.json is missing (e.g., running GSD commands outside a project), loadConfig() now checks ~/.gsd/defaults.json before returning hardcoded defaults. This lets users set preferred model_profile, context_window, subagent_timeout, and other settings globally. Only whitelisted keys are merged — unknown keys in defaults.json are silently ignored. If defaults.json is missing or contains invalid JSON, the hardcoded defaults are returned as before. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(config): scope defaults.json fallback to pre-project context only Only consult ~/.gsd/defaults.json when .planning/ does not exist (truly pre-project). When .planning/ exists but config.json is missing, return hardcoded defaults — avoids interference with tests and initialized projects. Use GSD_HOME env var for test isolation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -365,7 +365,36 @@ function loadConfig(cwd) {
|
||||
response_language: get('response_language') || null,
|
||||
};
|
||||
} catch {
|
||||
return defaults;
|
||||
// Fall back to ~/.gsd/defaults.json only for truly pre-project contexts (#1683)
|
||||
// If .planning/ exists, the project is initialized — just missing config.json
|
||||
if (fs.existsSync(planningDir(cwd))) {
|
||||
return defaults;
|
||||
}
|
||||
try {
|
||||
const home = process.env.GSD_HOME || os.homedir();
|
||||
const globalDefaultsPath = path.join(home, '.gsd', 'defaults.json');
|
||||
const raw = fs.readFileSync(globalDefaultsPath, 'utf-8');
|
||||
const globalDefaults = JSON.parse(raw);
|
||||
return {
|
||||
...defaults,
|
||||
model_profile: globalDefaults.model_profile ?? defaults.model_profile,
|
||||
commit_docs: globalDefaults.commit_docs ?? defaults.commit_docs,
|
||||
research: globalDefaults.research ?? defaults.research,
|
||||
plan_checker: globalDefaults.plan_checker ?? defaults.plan_checker,
|
||||
verifier: globalDefaults.verifier ?? defaults.verifier,
|
||||
nyquist_validation: globalDefaults.nyquist_validation ?? defaults.nyquist_validation,
|
||||
parallelization: globalDefaults.parallelization ?? defaults.parallelization,
|
||||
text_mode: globalDefaults.text_mode ?? defaults.text_mode,
|
||||
resolve_model_ids: globalDefaults.resolve_model_ids ?? defaults.resolve_model_ids,
|
||||
context_window: globalDefaults.context_window ?? defaults.context_window,
|
||||
subagent_timeout: globalDefaults.subagent_timeout ?? defaults.subagent_timeout,
|
||||
model_overrides: globalDefaults.model_overrides || null,
|
||||
agent_skills: globalDefaults.agent_skills || {},
|
||||
response_language: globalDefaults.response_language || null,
|
||||
};
|
||||
} catch {
|
||||
return defaults;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
143
tests/defaults-json-fallback.test.cjs
Normal file
143
tests/defaults-json-fallback.test.cjs
Normal file
@@ -0,0 +1,143 @@
|
||||
/**
|
||||
* GSD Tools Tests — ~/.gsd/defaults.json fallback (#1683)
|
||||
*
|
||||
* When .planning/ does not exist (pre-project context), loadConfig() should
|
||||
* consult ~/.gsd/defaults.json before returning hardcoded defaults.
|
||||
* When .planning/ exists but config.json is missing, hardcoded defaults are used.
|
||||
*/
|
||||
|
||||
const { test, describe } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const { cleanup } = require('./helpers.cjs');
|
||||
|
||||
const { loadConfig } = require('../get-shit-done/bin/lib/core.cjs');
|
||||
|
||||
/** Create a bare temp dir (no .planning/) to simulate pre-project context */
|
||||
function createBareTmpDir() {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-test-'));
|
||||
}
|
||||
|
||||
describe('loadConfig ~/.gsd/defaults.json fallback (#1683)', () => {
|
||||
test('pre-project, no defaults.json → hardcoded defaults', (t) => {
|
||||
const tmpDir = createBareTmpDir();
|
||||
process.env.GSD_HOME = tmpDir;
|
||||
t.after(() => { delete process.env.GSD_HOME; cleanup(tmpDir); });
|
||||
|
||||
const config = loadConfig(tmpDir);
|
||||
assert.strictEqual(config.model_profile, 'balanced');
|
||||
assert.strictEqual(config.context_window, 200000);
|
||||
assert.strictEqual(config.research, true);
|
||||
assert.strictEqual(config.subagent_timeout, 300000);
|
||||
});
|
||||
|
||||
test('pre-project, defaults.json exists → merges with hardcoded defaults', (t) => {
|
||||
const tmpDir = createBareTmpDir();
|
||||
|
||||
// Create ~/.gsd/defaults.json under fake GSD_HOME
|
||||
const gsdDir = path.join(tmpDir, '.gsd');
|
||||
fs.mkdirSync(gsdDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(gsdDir, 'defaults.json'),
|
||||
JSON.stringify({ model_profile: 'quality', context_window: 1000000 })
|
||||
);
|
||||
|
||||
process.env.GSD_HOME = tmpDir;
|
||||
t.after(() => { delete process.env.GSD_HOME; cleanup(tmpDir); });
|
||||
|
||||
const config = loadConfig(tmpDir);
|
||||
// Values from defaults.json
|
||||
assert.strictEqual(config.model_profile, 'quality');
|
||||
assert.strictEqual(config.context_window, 1000000);
|
||||
// Hardcoded defaults for keys not in defaults.json
|
||||
assert.strictEqual(config.research, true);
|
||||
assert.strictEqual(config.subagent_timeout, 300000);
|
||||
assert.strictEqual(config.parallelization, true);
|
||||
});
|
||||
|
||||
test('.planning/ exists but no config.json → hardcoded defaults (not defaults.json)', (t) => {
|
||||
const tmpDir = createBareTmpDir();
|
||||
// Create .planning/ without config.json
|
||||
fs.mkdirSync(path.join(tmpDir, '.planning'), { recursive: true });
|
||||
|
||||
// Create defaults.json — should NOT be consulted
|
||||
const gsdDir = path.join(tmpDir, '.gsd');
|
||||
fs.mkdirSync(gsdDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(gsdDir, 'defaults.json'),
|
||||
JSON.stringify({ model_profile: 'quality', context_window: 1000000 })
|
||||
);
|
||||
|
||||
process.env.GSD_HOME = tmpDir;
|
||||
t.after(() => { delete process.env.GSD_HOME; cleanup(tmpDir); });
|
||||
|
||||
const config = loadConfig(tmpDir);
|
||||
// Hardcoded defaults — NOT defaults.json values
|
||||
assert.strictEqual(config.model_profile, 'balanced');
|
||||
assert.strictEqual(config.context_window, 200000);
|
||||
});
|
||||
|
||||
test('project config exists → project config wins', (t) => {
|
||||
const tmpDir = createBareTmpDir();
|
||||
fs.mkdirSync(path.join(tmpDir, '.planning'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, '.planning', 'config.json'),
|
||||
JSON.stringify({ model_profile: 'budget' })
|
||||
);
|
||||
|
||||
// Also write defaults.json with a different value
|
||||
const gsdDir = path.join(tmpDir, '.gsd');
|
||||
fs.mkdirSync(gsdDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(gsdDir, 'defaults.json'),
|
||||
JSON.stringify({ model_profile: 'quality', context_window: 1000000 })
|
||||
);
|
||||
|
||||
process.env.GSD_HOME = tmpDir;
|
||||
t.after(() => { delete process.env.GSD_HOME; cleanup(tmpDir); });
|
||||
|
||||
const config = loadConfig(tmpDir);
|
||||
assert.strictEqual(config.model_profile, 'budget');
|
||||
assert.strictEqual(config.context_window, 200000);
|
||||
});
|
||||
|
||||
test('defaults.json with unknown keys → unknown keys NOT passed through', (t) => {
|
||||
const tmpDir = createBareTmpDir();
|
||||
|
||||
const gsdDir = path.join(tmpDir, '.gsd');
|
||||
fs.mkdirSync(gsdDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(gsdDir, 'defaults.json'),
|
||||
JSON.stringify({
|
||||
model_profile: 'quality',
|
||||
unknown_key: 'should_not_appear',
|
||||
another_unknown: 42,
|
||||
})
|
||||
);
|
||||
|
||||
process.env.GSD_HOME = tmpDir;
|
||||
t.after(() => { delete process.env.GSD_HOME; cleanup(tmpDir); });
|
||||
|
||||
const config = loadConfig(tmpDir);
|
||||
assert.strictEqual(config.model_profile, 'quality');
|
||||
assert.strictEqual(config.unknown_key, undefined);
|
||||
assert.strictEqual(config.another_unknown, undefined);
|
||||
});
|
||||
|
||||
test('defaults.json with invalid JSON → returns hardcoded defaults', (t) => {
|
||||
const tmpDir = createBareTmpDir();
|
||||
|
||||
const gsdDir = path.join(tmpDir, '.gsd');
|
||||
fs.mkdirSync(gsdDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(gsdDir, 'defaults.json'), '{ not valid json !!!');
|
||||
|
||||
process.env.GSD_HOME = tmpDir;
|
||||
t.after(() => { delete process.env.GSD_HOME; cleanup(tmpDir); });
|
||||
|
||||
const config = loadConfig(tmpDir);
|
||||
assert.strictEqual(config.model_profile, 'balanced');
|
||||
assert.strictEqual(config.context_window, 200000);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user