Skip to content

The agent that grows with you

fix(install-e2e): writeEnvFile rewrites keys in place instead of moving them

ethernet0117a28b4465
diff --git a/tests-js/scripts/desktop-smoke.test.ts b/tests-js/scripts/desktop-smoke.test.ts
index dd4a5e6cc1e..756e1f95a1c 100644
--- a/tests-js/scripts/desktop-smoke.test.ts
+++ b/tests-js/scripts/desktop-smoke.test.ts
@@ -289,3 +289,24 @@ test('writeEnvFile without a URL keeps the journey provider endpoint', (): void
expect(after).toContain('MOCK_API_KEY=e2e-mock-key')
} finally { fs.rmSync(home, { recursive: true, force: true }) }
})
+
+test('re-writing the same provider config is byte-identical', (): void => {
+ // Filtering the keys out and re-appending them moved a journey's own .env
+ // entries on every call: same keys, same values, different bytes -- which the
+ // user-state verifier reported as "0 deleted, 1 modified ... no key differs".
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'smoke-env-order-'))
+ try {
+ // The key is deliberately NOT last, with a comment after it: filtering the
+ // managed keys out and re-appending them moved them past the journey's own
+ // entries, which is the "no key differs, comments/order/blanks only" the
+ // user-state verifier reported.
+ const before = 'OPENAI_BASE_URL=http://127.0.0.1:9000/v1\n# keep me\nOTHER_TEST_VALUE=kept\n'
+ fs.writeFileSync(path.join(home, '.env'), before)
+
+ writeEnvFile(home, 'e2e-mock-key', 'http://127.0.0.1:9000')
+
+ expect(fs.readFileSync(path.join(home, '.env'), 'utf8')).toBe(
+ 'OPENAI_BASE_URL=http://127.0.0.1:9000/v1\n# keep me\nOTHER_TEST_VALUE=kept\n'
+ + 'MOCK_API_KEY=e2e-mock-key\nOPENAI_API_KEY=e2e-mock-key\n')
+ } finally { fs.rmSync(home, { recursive: true, force: true }) }
+})
diff --git a/tests-js/scripts/mock-provider-config.ts b/tests-js/scripts/mock-provider-config.ts
index 6dabf7d8c5f..9773fa64aec 100644
--- a/tests-js/scripts/mock-provider-config.ts
+++ b/tests-js/scripts/mock-provider-config.ts
@@ -79,21 +79,36 @@ export function writeEnvFile(hermesHome: string, apiKey = 'e2e-mock-key', mockUr
}
const envPath = path.join(hermesHome, '.env')
const prior = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : ''
- // Only drop keys this call actually REPLACES. With no mockUrl it writes
- // MOCK_API_KEY alone, and stripping the portable pair then would delete a
- // journey's provider endpoint: the install e2e's snapshot missed OPENAI_BASE_URL
- // after this smoke ran, and the upgrade's own .env sync put it back -- reported
- // as "the upgrade changed the user's own state".
- const replaced = mockUrl
- ? /^\s*(?:export\s+)?(?:MOCK_API_KEY|OPENAI_BASE_URL|OPENAI_API_KEY)\s*=/
- : /^\s*(?:export\s+)?MOCK_API_KEY\s*=/
- const lines = prior.split(/\r?\n/).filter((line: string): boolean => !replaced.test(line))
- // OPENAI_BASE_URL + OPENAI_API_KEY is how EVERY vintage reaches an external
- // OpenAI-compatible endpoint ("custom"); a bare MOCK_API_KEY only means
- // something to a tree that knows a provider named `mock`, which older refs
- // do not (their resolve_provider accepts only openrouter/custom/registry).
- const portable = mockUrl ? `\nOPENAI_BASE_URL=${mockUrl}/v1\nOPENAI_API_KEY=${apiKey}` : ''
- fs.writeFileSync(envPath, `${lines.join('\n').trimEnd()}\nMOCK_API_KEY=${apiKey}${portable}\n`, { mode: 0o600 })
+ const reps = new Map<string, string>([['MOCK_API_KEY', `MOCK_API_KEY=${apiKey}`]])
+ if (mockUrl) {
+ reps.set('OPENAI_BASE_URL', `OPENAI_BASE_URL=${mockUrl}/v1`)
+ reps.set('OPENAI_API_KEY', `OPENAI_API_KEY=${apiKey}`)
+ }
+ // Rewrite each key IN PLACE and append only the ones that are missing. Filtering
+ // the keys out and re-appending them at the end moved a journey's own entries on
+ // every call: same keys, same values, different bytes -- which the user-state
+ // verifier reported as "0 deleted, 1 modified ... no key differs". Writing the
+ // same pair twice is byte-identical now.
+ const seen = new Set<string>()
+ const kept: string[] = []
+ for (const line of prior.split(/\r?\n/)) {
+ const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line)?.[1]
+ if (!key || !reps.has(key)) {
+ kept.push(line)
+ continue
+ }
+ if (!seen.has(key)) {
+ kept.push(reps.get(key)!)
+ seen.add(key)
+ }
+ }
+ // Drop the prior file's trailing blank lines BEFORE appending: trimEnd() runs
+ // after the appended keys, so it cannot reach a blank line that they now follow.
+ while (kept.length > 0 && kept[kept.length - 1].trim() === '') kept.pop()
+ for (const [key, line] of reps) {
+ if (!seen.has(key)) kept.push(line)
+ }
+ fs.writeFileSync(envPath, `${kept.join('\n').trimEnd()}\n`, { mode: 0o600 })
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {