Skip to content

Commit 1c37bc7

Browse files
anandgupta42claude
andcommitted
fix: keep session warehouse credentials across instance disposal
- disposal closes a project's connectors and tunnels but keeps its configs - `remove()` writes the global file before touching state, so a failed write changes nothing - a dropped connect reports the superseded retry error, not its closed tunnel's error Refs #1566 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent feb06f3 commit 1c37bc7

2 files changed

Lines changed: 54 additions & 15 deletions

File tree

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ function detachCached(s: State, name: string): () => Promise<void> {
9999
}
100100

101101
let disposerRegistered = false
102-
/** Close a project's connectors and tunnels when its instance is disposed (`/instance/dispose`, reload). */
102+
/**
103+
* Close a project's connectors, in-flight connects and tunnels when its instance is disposed (`/instance/dispose`,
104+
* config updates, reload). Its configs stay: they may hold session-only credentials from `warehouse_add` (no OS
105+
* credential store), which must last for the process, and a populated registry is never re-read anyway.
106+
*/
103107
function ensureDisposer(): void {
104108
if (disposerRegistered) return
105109
disposerRegistered = true
106110
registerDisposer(async (directory) => {
107-
const key = path.resolve(directory)
108-
const s = states.get(key)
109-
if (!s) return
110-
states.delete(key)
111-
await closeState(s)
111+
const s = states.get(path.resolve(directory))
112+
if (s) await closeState(s)
112113
})
113114
}
114115

@@ -724,7 +725,11 @@ export async function get(name: string): Promise<Connector> {
724725
})
725726
} catch {}
726727
return connector
727-
} catch (e) {
728+
} catch (caught) {
729+
// altimate_change start — an attempt dropped by reload/remove/add/dispose fails with whatever its closed tunnel
730+
// or socket raised; report it as superseded (with the retry hint), not as a broken connection
731+
const e = current() ? caught : SUPERSEDED(name)
732+
// altimate_change end
728733
fileLog("WARN", "warehouse-connect", "connect failed", {
729734
name,
730735
type: config?.type,
@@ -943,19 +948,19 @@ export async function remove(name: string): Promise<{ success: boolean; error?:
943948
// later reconnect on that account be put down to another connection that shares it.
944949
const inflight = s.pending.has(name)
945950
const projectConfigPath = localConfigPath()
946-
// One synchronous step before any await: the in-flight connect is invalidated (and its tunnel closed), the
947-
// config is gone from memory and from the global file, so no connect or empty-registry re-read can bring it back,
948-
// and the cached connector and tunnel are detached.
949-
dropAttempt(s, name)
950-
s.configs.delete(name)
951-
const release = detachCached(s, name)
951+
// One synchronous step before any await. The global file is written first, so a failed write leaves this
952+
// project's state untouched. Then the in-flight connect is invalidated (and its tunnel closed), the config is
953+
// gone so no connect or empty-registry re-read can bring it back, and the cached connector and tunnel are
954+
// detached. Only a file that holds the entry is rewritten (a missing global directory is not an error).
952955
const globalPath = globalConfigPath()
953956
const existing = loadFromFile(globalPath)
954-
// Only a file that holds the entry is rewritten (a missing global directory is not an error).
955957
if (Object.hasOwn(existing, name)) {
956958
delete existing[name]
957959
fs.writeFileSync(globalPath, JSON.stringify(existing, null, 2), "utf-8")
958960
}
961+
dropAttempt(s, name)
962+
s.configs.delete(name)
963+
const release = detachCached(s, name)
959964
await release()
960965
if (!inflight) ReconnectLog.forget(name)
961966
// altimate_change end

‎packages/opencode/test/altimate/registry-per-project.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,45 @@ describe("instance disposal", () => {
348348
await expect(a.execute("SELECT 1")).rejects.toThrow()
349349
expect((await b.execute("SELECT 1 AS one")).rows).toEqual([[1]])
350350

351-
// reopening A loads a fresh state
351+
// reopening A connects again with a new connector
352352
const a2 = await inProject(projectA, () => Registry.get("store"))
353353
expect(a2).not.toBe(a)
354354
expect((await a2.execute("SELECT 1 AS one")).rows).toEqual([[1]])
355355
})
356+
357+
test("session-only credentials from warehouse_add survive disposal", async () => {
358+
// The test preload disables the OS credential store, so the password exists only in memory.
359+
await inProject(projectA, () => Registry.add("pg", { type: "postgres", host: "h", password: "s3cret-test" } as any))
360+
await disposeInstance(projectA)
361+
expect(inProject(projectA, () => Registry.getConfig("pg")?.password)).toBe("s3cret-test")
362+
})
363+
364+
test("a connect dropped while its tunnel is opening fails as superseded, with the retry hint", async () => {
365+
writeLocal(projectA, { store: sshStore("held-dispose") })
366+
const pending = inProject(projectA, () => Registry.get("store"))
367+
await sshConnected("held-dispose")
368+
await disposeInstance(projectA)
369+
await expect(pending).rejects.toThrow("reloaded or removed while it was connecting. Retry the request.")
370+
})
371+
})
372+
373+
describe("remove()", () => {
374+
test("a failed global file write leaves the project's connection usable", async () => {
375+
writeGlobal({ g: { type: "sqlite", path: path.join(projectA, "g.sqlite"), create: true } })
376+
const connector = await inProject(projectA, () => Registry.get("g"))
377+
const globalDir = path.join(home, ".altimate-code")
378+
fs.chmodSync(globalDir, 0o500)
379+
fs.chmodSync(path.join(globalDir, "connections.json"), 0o400)
380+
try {
381+
const result = await inProject(projectA, () => Registry.remove("g"))
382+
expect(result.success).toBe(false)
383+
expect(inProject(projectA, () => Registry.names())).toContain("g")
384+
expect(await inProject(projectA, () => Registry.get("g"))).toBe(connector)
385+
} finally {
386+
fs.chmodSync(globalDir, 0o700)
387+
fs.chmodSync(path.join(globalDir, "connections.json"), 0o600)
388+
}
389+
})
356390
})
357391

358392
describe("credentials of a removed connection", () => {

0 commit comments

Comments
 (0)