diff --git a/src/torch-config.mjs b/src/torch-config.mjs index a759051..2e40b76 100644 --- a/src/torch-config.mjs +++ b/src/torch-config.mjs @@ -69,9 +69,27 @@ export function getTorchConfigPath() { if (explicitPath) return path.resolve(process.cwd(), explicitPath); const localPath = path.resolve(process.cwd(), DEFAULT_CONFIG_PATH); - if (fs.existsSync(localPath)) return localPath; - const parentPath = path.resolve(process.cwd(), '..', DEFAULT_CONFIG_PATH); + const cwdBaseName = path.basename(process.cwd()); + const parentPkgPath = path.resolve(process.cwd(), '..', 'package.json'); + let parentLooksLikeHostRepo = false; + + try { + if (fs.existsSync(parentPkgPath)) { + const parentPkgRaw = fs.readFileSync(parentPkgPath, 'utf8'); + const parentPkg = JSON.parse(parentPkgRaw); + parentLooksLikeHostRepo = parentPkg && typeof parentPkg.name === 'string' && parentPkg.name !== 'torch-lock'; + } + } catch { + parentLooksLikeHostRepo = false; + } + + // Host-repo mode: when running from "/torch", prefer parent config. + if (cwdBaseName === 'torch' && parentLooksLikeHostRepo && fs.existsSync(parentPath)) { + return parentPath; + } + + if (fs.existsSync(localPath)) return localPath; if (fs.existsSync(parentPath)) return parentPath; return localPath; diff --git a/test/torch-config.test.mjs b/test/torch-config.test.mjs index 2ea4728..e0c6144 100644 --- a/test/torch-config.test.mjs +++ b/test/torch-config.test.mjs @@ -19,10 +19,12 @@ import { DEFAULT_RELAYS } from '../src/constants.mjs'; describe('torch-config', () => { describe('getTorchConfigPath', () => { const originalEnv = process.env.TORCH_CONFIG_PATH; + const originalCwd = process.cwd(); after(() => { if (originalEnv === undefined) delete process.env.TORCH_CONFIG_PATH; else process.env.TORCH_CONFIG_PATH = originalEnv; + process.chdir(originalCwd); }); it('returns default path when env var is not set', () => { @@ -34,6 +36,24 @@ describe('torch-config', () => { process.env.TORCH_CONFIG_PATH = 'custom-config.json'; assert.strictEqual(getTorchConfigPath(), path.resolve(process.cwd(), 'custom-config.json')); }); + + it('prefers parent torch-config.json in host-repo mode when cwd is /torch', () => { + delete process.env.TORCH_CONFIG_PATH; + + const fixtureRoot = fs.mkdtempSync(path.join(process.cwd(), 'test-torch-config-host-')); + const hostPkgPath = path.join(fixtureRoot, 'package.json'); + const parentConfigPath = path.join(fixtureRoot, 'torch-config.json'); + const torchDir = path.join(fixtureRoot, 'torch'); + const localConfigPath = path.join(torchDir, 'torch-config.json'); + + fs.mkdirSync(torchDir, { recursive: true }); + fs.writeFileSync(hostPkgPath, JSON.stringify({ name: 'seedpass', version: '1.0.0' }), 'utf8'); + fs.writeFileSync(parentConfigPath, JSON.stringify({ nostrLock: { namespace: 'parent' } }), 'utf8'); + fs.writeFileSync(localConfigPath, JSON.stringify({ nostrLock: { namespace: 'local' } }), 'utf8'); + + process.chdir(torchDir); + assert.strictEqual(getTorchConfigPath(), path.resolve(parentConfigPath)); + }); }); describe('parseTorchConfig', () => {