Skip to content

Commit f27bdc6

Browse files
anandgupta42claude
andcommitted
fix: keep a refreshed connection config authoritative over stale in-memory state
Review of the reload change found two ways an old target could survive a config edit: a connection added in-process was restored over a later edit of its global entry, and a connect already in flight could cache a connector built from the previous config. - `add()` now remembers the sanitized form it wrote; the in-memory copy (with credentials) is restored only while the file still holds exactly that. - Refresh bumps a config generation; a connect that finished against an obsolete config is closed and fails with a retry message instead of being cached. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a3c7274 commit f27bdc6

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎packages/opencode/src/altimate/native/connections/registry.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,13 @@ export function load(): void {
233233
let injected = false
234234
/** Fingerprint of the sources behind the last load. */
235235
let loadedSignature = ""
236-
/** Connections added in this process, with the credentials the sanitized file no longer holds. */
237-
const sessionAdded = new Map<string, ConnectionConfig>()
236+
/** Bumped whenever a refresh changes a connection's config, so an in-flight connect can tell it is obsolete. */
237+
let configGeneration = 0
238+
/**
239+
* Connections added in this process: the full config (with credentials the sanitized file no longer holds) and
240+
* the sanitized form that was written, so a later edit of the file is not overridden by the older in-memory copy.
241+
*/
242+
const sessionAdded = new Map<string, { full: ConnectionConfig; written: ConnectionConfig }>()
238243

239244
function statSignature(filePath: string): string {
240245
try {
@@ -257,10 +262,15 @@ function sourceSignature(): string {
257262
function refresh(): void {
258263
const previous = new Map(configs)
259264
load()
260-
for (const [name, config] of sessionAdded) {
261-
// A project file or env var defining the name outranks what this process added to the global file.
265+
for (const [name, { full, written }] of sessionAdded) {
266+
// Only while the file still holds what add() wrote: a project file or env var defining the name outranks it,
267+
// and a later edit of the global entry must win over the older in-memory copy.
262268
const overridden = Object.hasOwn(loadFromFile(localConfigPath()), name) || Object.hasOwn(loadFromEnv(), name)
263-
if (!overridden && configs.has(name)) configs.set(name, config)
269+
if (!overridden && JSON.stringify(configs.get(name)) === JSON.stringify(written)) configs.set(name, full)
270+
else if (!overridden) sessionAdded.delete(name)
271+
}
272+
for (const name of new Set([...previous.keys(), ...configs.keys()])) {
273+
if (JSON.stringify(configs.get(name)) !== JSON.stringify(previous.get(name))) configGeneration++
264274
}
265275
for (const [name, connector] of [...connectors]) {
266276
if (JSON.stringify(configs.get(name)) === JSON.stringify(previous.get(name))) continue
@@ -600,6 +610,9 @@ export async function get(name: string): Promise<Connector> {
600610
})
601611
} catch {}
602612
// altimate_change end
613+
// altimate_change start
614+
const generation = configGeneration
615+
// altimate_change end
603616
const promise = (async () => {
604617
try {
605618
const connector = await createConnector(name, config)
@@ -610,6 +623,13 @@ export async function get(name: string): Promise<Connector> {
610623
closeTunnel(name)
611624
throw connectErr
612625
}
626+
// altimate_change start — the config changed while this connected; caching it would pin the old target
627+
if (generation !== configGeneration && JSON.stringify(configs.get(name)) !== JSON.stringify(config)) {
628+
await connector.close().catch(() => {})
629+
closeTunnel(name)
630+
throw new Error(`Connection "${name}" changed while it was connecting; retry.`)
631+
}
632+
// altimate_change end
613633
connectors.set(name, connector)
614634
fileLog("INFO", "warehouse-connect", "connected", { name, type: config.type, auth: authMethod, duration_ms: Date.now() - startTime })
615635
try {
@@ -799,7 +819,7 @@ export async function add(
799819
// file uses the sanitized version (credentials stripped).
800820
configs.set(name, normalized)
801821
// altimate_change start — our own write is not an external edit, and the credentials stay in memory
802-
sessionAdded.set(name, normalized)
822+
sessionAdded.set(name, { full: normalized, written: sanitized })
803823
loadedSignature = sourceSignature()
804824
// altimate_change end
805825

‎packages/opencode/test/altimate/warehouse-list-hint.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,19 @@ describe("warehouse_list no-connections hint", () => {
103103
expect(Registry.names()).toEqual(["injected"])
104104
expect(Registry.names()).toEqual(["injected"])
105105
})
106+
107+
test("a connection added in-process yields to a later edit of its global entry", async () => {
108+
const added = await Registry.add("edit-me", { type: "postgres", host: "old-host", database: "d" })
109+
expect(added.success).toBe(true)
110+
try {
111+
expect(Registry.getConfig("edit-me")?.host).toBe("old-host")
112+
const globalPath = Registry.globalConfigPath()
113+
const onDisk = JSON.parse(fs.readFileSync(globalPath, "utf-8"))
114+
onDisk["edit-me"].host = "new-host"
115+
fs.writeFileSync(globalPath, JSON.stringify(onDisk))
116+
expect(Registry.getConfig("edit-me")?.host).toBe("new-host")
117+
} finally {
118+
await Registry.remove("edit-me")
119+
}
120+
})
106121
})

0 commit comments

Comments
 (0)