From 600c078cd35008c9a83a4133b501cc137dfccaf5 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Wed, 25 Feb 2026 15:04:02 -0500 Subject: [PATCH] Prefer parent torch-config in host-repo torch subdir --- src/torch-config.mjs | 22 ++++++++++++++++++++-- test/torch-config.test.mjs | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) 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', () => {