Skip to content

Commit 1ee2d31

Browse files
anandgupta42claude
andcommitted
fix: keep warehouse connection registry state per project directory (#1567)
* fix: keep warehouse connection registry state per project directory - `registry.ts`: configs, connectors, in-flight connects and the load latch live in a `Map<projectDir, State>`; global file and env stay shared sources - SSH tunnel keys are project-scoped; stale in-flight connects are not cached after reload/remove/add - `setConfigs()` injects for all projects until `reset()`/`load()` - drop `projectConfigApplies()` Closes #1566 Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix: close the reload and reset windows for in-flight warehouse connects - `reload()` detaches and reloads before awaiting connector closes - `reset()` invalidates pending connects before dropping states Refs #1566 Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix: give each warehouse connect attempt its own SSH tunnel and dispose per project - `ssh-tunnel.ts`: unkeyed `openTunnel()` handles, closable while opening - registry: attempts close only their own tunnel; add/remove/reload/reset detach synchronously - instance disposal closes that project's connectors and tunnels - keep keychain secrets while a served project's local config names the connection Refs #1566 Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix: close remaining warehouse teardown gaps from review - `remove()` drops the global entry before awaiting cleanup; rewrites only a file holding it - `closeState()` closes all tunnels before awaiting connector closes - a clean SSH disconnect ends the tunnel - credential retention reads served projects after the awaits Refs #1566 Co-Authored-By: Claude Opus 5.5 <[email protected]> * 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]> * fix: keep shared credentials for untouched open projects and handle SSH forwarding errors - `remove()` also checks the local connections file of every open instance (`Instance.directories()`), not only projects that already loaded the registry. - SSH forwarding attaches `error` handlers to the local socket and remote stream and destroys both, so a reset no longer throws an uncaught exception. - Tests: `registry-per-project.test.ts` (untouched open project, socket error). Co-Authored-By: Claude Opus 5.5 <[email protected]> * test: isolate `warehouse_list` hint tests from the shared global connections file `Registry.add()` in `telemetry-safety.test.ts` and `adversarial.test.ts` persists `test_add` and `evil` to the test-home global `connections.json`. When CI ran those files first in the same process, the empty-registry cases in `warehouse-list-hint.test.ts` listed them and failed. The hint tests now set that file aside in `beforeEach` and restore it in `afterEach`. Verified by seeding a global file before the suite: 5 failures before, 9/9 pass after. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit 9137a1d) Branch port: `loadFile` (`mcp serve --connections`) now installs the file through `setConfigs()`, so an explicit file is the one registry every project uses and is never re-read from project sources. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0028a4b commit 1ee2d31

8 files changed

Lines changed: 984 additions & 276 deletions

File tree

‎packages/opencode/src/altimate/native/connections/credential-store.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ export async function deleteCredential(
167167
* Entries are keyed by connection name only. Only `saveConnection` writes them, for the saved (global) connection
168168
* of that name; a project's own connections file never stores a secret here. A project file that defines a
169169
* connection of the same name without its secret was reading the saved connection's secret, and loses it when that
170-
* saved connection is removed or re-saved without it.
170+
* saved connection is removed or re-saved without it. The registry keeps the entries on removal while an env var or
171+
* the local config of a project this process currently serves still names the connection; projects it does not
172+
* serve (closed, never opened, or in another process) are not consulted.
171173
*/
172174
export async function forgetCredentials(name: string, keep?: ConnectionConfig): Promise<string[]> {
173175
const store = await getBackend()

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

Lines changed: 291 additions & 143 deletions
Large diffs are not rendered by default.

‎packages/opencode/src/altimate/native/connections/ssh-tunnel.ts‎

Lines changed: 133 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@
33
*
44
* Uses the `ssh2` package (dynamic import). If ssh2 is not installed,
55
* throws a clear error message.
6+
*
7+
* Tunnels are not keyed by connection name. Each `openTunnel()` call returns a
8+
* handle owned by the caller (the connection registry's connect attempt), and
9+
* only that handle closes it: an attempt that was superseded can never register
10+
* over, or close, a newer attempt's tunnel.
611
*/
712

813
import type { ConnectionConfig } from "@altimateai/drivers"
14+
import type { Server, Socket } from "net"
15+
import type { Duplex } from "stream"
916

1017
export interface TunnelInfo {
1118
localPort: number
1219
close(): void
1320
}
1421

22+
/** A tunnel being opened. `close()` works before or after `ready` settles and is idempotent. */
23+
export interface TunnelHandle {
24+
/** Resolves once the local forwarding port listens; rejects on failure or when closed first. */
25+
readonly ready: Promise<TunnelInfo>
26+
close(): void
27+
}
28+
1529
export interface SshConfig {
1630
ssh_host: string
1731
ssh_port?: number
@@ -22,161 +36,165 @@ export interface SshConfig {
2236
port: number
2337
}
2438

25-
/** Active tunnels keyed by connection name. */
26-
const activeTunnels = new Map<string, TunnelInfo>()
39+
/** Every tunnel opened and not yet closed, for process-exit cleanup and `closeAllTunnels()`. */
40+
const openTunnels = new Set<TunnelHandle>()
2741

2842
/** Clean up all tunnels on process exit. */
2943
let cleanupRegistered = false
3044
function ensureCleanup(): void {
3145
if (cleanupRegistered) return
3246
cleanupRegistered = true
33-
const cleanup = () => {
34-
for (const [, tunnel] of activeTunnels) {
35-
try {
36-
tunnel.close()
37-
} catch {
38-
// best-effort cleanup
39-
}
40-
}
41-
activeTunnels.clear()
42-
}
43-
process.once("exit", cleanup)
47+
process.once("exit", closeAllTunnels)
4448
process.once("SIGINT", () => {
45-
cleanup()
49+
closeAllTunnels()
4650
process.exit(0)
4751
})
4852
process.once("SIGTERM", () => {
49-
cleanup()
53+
closeAllTunnels()
5054
process.exit(0)
5155
})
5256
}
5357

5458
/**
55-
* Start an SSH tunnel for a connection.
56-
* Returns a TunnelInfo with the local port to connect to.
59+
* Open an SSH tunnel that forwards a local port to `config.host:config.port`.
60+
*
61+
* One terminal state covers every path: an error, a close before `ready`, or a
62+
* late `ready`/listen callback after either closes the client, the local server
63+
* and forwarded sockets, and never publishes a tunnel.
5764
*/
58-
export async function startTunnel(
59-
name: string,
60-
config: SshConfig,
61-
): Promise<TunnelInfo> {
62-
// Close existing tunnel for this name
63-
const existing = activeTunnels.get(name)
64-
if (existing) {
65-
existing.close()
66-
activeTunnels.delete(name)
67-
}
68-
69-
let ssh2: any
70-
try {
71-
// @ts-expect-error — optional dependency
72-
ssh2 = await import("ssh2")
73-
} catch {
74-
throw new Error(
75-
"SSH tunnel requires the ssh2 package. Run: bun add ssh2 @types/ssh2",
76-
)
65+
export function openTunnel(config: SshConfig): TunnelHandle {
66+
let closed = false
67+
let client: any
68+
let server: Server | undefined
69+
const sockets = new Set<Socket>()
70+
let rejectReady: ((e: Error) => void) | undefined
71+
72+
const close = () => {
73+
if (closed) return
74+
closed = true
75+
openTunnels.delete(handle)
76+
for (const socket of sockets) socket.destroy()
77+
try {
78+
server?.close()
79+
} catch {
80+
// ignore
81+
}
82+
try {
83+
client?.end()
84+
} catch {
85+
// ignore
86+
}
87+
rejectReady?.(new Error("SSH tunnel was closed before it was ready"))
7788
}
7889

79-
ensureCleanup()
80-
81-
const net = await import("net")
82-
83-
return new Promise<TunnelInfo>((resolve, reject) => {
84-
const client = new ssh2.Client()
85-
86-
client.on("ready", () => {
87-
// Create a local TCP server that forwards to remote host:port via SSH
88-
const server = net.createServer((localSocket) => {
89-
client.forwardOut(
90-
"127.0.0.1",
91-
0,
92-
config.host,
93-
config.port,
94-
(err: Error | undefined, stream: any) => {
95-
if (err) {
90+
const ready = (async (): Promise<TunnelInfo> => {
91+
let ssh2: any
92+
try {
93+
// @ts-expect-error — optional dependency
94+
ssh2 = await import("ssh2")
95+
} catch {
96+
throw new Error("SSH tunnel requires the ssh2 package. Run: bun add ssh2 @types/ssh2")
97+
}
98+
const net = await import("net")
99+
if (closed) throw new Error("SSH tunnel was closed before it was ready")
100+
ensureCleanup()
101+
102+
return new Promise<TunnelInfo>((resolve, reject) => {
103+
rejectReady = reject
104+
client = new ssh2.Client()
105+
106+
client.on("ready", () => {
107+
if (closed) return
108+
// Create a local TCP server that forwards to remote host:port via SSH
109+
server = net.createServer((localSocket) => {
110+
let remoteStream: Duplex | undefined
111+
localSocket.on("error", () => {
112+
remoteStream?.destroy()
113+
localSocket.destroy()
114+
})
115+
if (closed) {
116+
localSocket.destroy()
117+
return
118+
}
119+
sockets.add(localSocket)
120+
localSocket.on("close", () => sockets.delete(localSocket))
121+
client.forwardOut("127.0.0.1", 0, config.host, config.port, (err: Error | undefined, stream: any) => {
122+
remoteStream = stream
123+
stream?.on("error", () => {
124+
localSocket.destroy()
125+
stream.destroy()
126+
})
127+
if (err || closed || localSocket.destroyed) {
128+
stream?.destroy()
96129
localSocket.destroy()
97130
return
98131
}
99132
localSocket.pipe(stream).pipe(localSocket)
100-
},
101-
)
133+
})
134+
})
135+
136+
server.on("error", (err: Error) => {
137+
reject(new Error(`SSH tunnel local server error: ${err.message}`))
138+
close()
139+
})
140+
141+
server.listen(0, "127.0.0.1", () => {
142+
if (closed) return
143+
const addr = server?.address()
144+
const localPort = typeof addr === "object" && addr ? addr.port : 0
145+
resolve({ localPort, close })
146+
})
102147
})
103148

104-
server.listen(0, "127.0.0.1", () => {
105-
const addr = server.address()
106-
const localPort =
107-
typeof addr === "object" && addr ? addr.port : 0
108-
109-
const tunnelInfo: TunnelInfo = {
110-
localPort,
111-
close() {
112-
try {
113-
server.close()
114-
} catch {
115-
// ignore
116-
}
117-
try {
118-
client.end()
119-
} catch {
120-
// ignore
121-
}
122-
activeTunnels.delete(name)
123-
},
124-
}
125-
126-
activeTunnels.set(name, tunnelInfo)
127-
resolve(tunnelInfo)
149+
client.on("error", (err: Error) => {
150+
reject(new Error(`SSH connection error: ${err.message}`))
151+
close()
128152
})
129153

130-
server.on("error", (err: Error) => {
131-
client.end()
132-
reject(new Error(`SSH tunnel local server error: ${err.message}`))
154+
// A clean remote disconnect ends the tunnel too: before ready it rejects, after it closes the listener.
155+
client.on("close", () => {
156+
reject(new Error("SSH connection closed"))
157+
close()
133158
})
134-
})
135-
136-
client.on("error", (err: Error) => {
137-
reject(new Error(`SSH connection error: ${err.message}`))
138-
})
139-
140-
const connectOptions: Record<string, unknown> = {
141-
host: config.ssh_host,
142-
port: config.ssh_port ?? 22,
143-
username: config.ssh_user ?? "root",
144-
}
145159

146-
if (config.ssh_private_key) {
147-
connectOptions.privateKey = config.ssh_private_key
148-
} else if (config.ssh_password) {
149-
connectOptions.password = config.ssh_password
150-
}
160+
const connectOptions: Record<string, unknown> = {
161+
host: config.ssh_host,
162+
port: config.ssh_port ?? 22,
163+
username: config.ssh_user ?? "root",
164+
}
151165

152-
client.connect(connectOptions)
153-
})
154-
}
166+
if (config.ssh_private_key) {
167+
connectOptions.privateKey = config.ssh_private_key
168+
} else if (config.ssh_password) {
169+
connectOptions.password = config.ssh_password
170+
}
155171

156-
/** Get an active tunnel by connection name. */
157-
export function getActiveTunnel(name: string): TunnelInfo | undefined {
158-
return activeTunnels.get(name)
159-
}
172+
client.connect(connectOptions)
173+
})
174+
})()
160175

161-
/** Close a specific tunnel by name. */
162-
export function closeTunnel(name: string): void {
163-
const tunnel = activeTunnels.get(name)
164-
if (tunnel) {
165-
tunnel.close()
166-
activeTunnels.delete(name)
167-
}
176+
const handle: TunnelHandle = { ready, close }
177+
openTunnels.add(handle)
178+
// Any failure ends in the terminal state; the caller still observes the rejection through `ready`.
179+
ready.catch(() => close())
180+
return handle
168181
}
169182

170-
/** Close all active tunnels. */
183+
/** Close every open tunnel (process teardown and tests). */
171184
export function closeAllTunnels(): void {
172-
for (const [, tunnel] of activeTunnels) {
185+
for (const tunnel of [...openTunnels]) {
173186
try {
174187
tunnel.close()
175188
} catch {
176189
// best-effort
177190
}
178191
}
179-
activeTunnels.clear()
192+
openTunnels.clear()
193+
}
194+
195+
/** Number of tunnels currently open or opening (diagnostics and tests). */
196+
export function openTunnelCount(): number {
197+
return openTunnels.size
180198
}
181199

182200
/**

‎packages/opencode/src/altimate/tools/warehouse-list.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as Precedence from "../workspace/precedence"
88
import { discoverCatalog } from "./warehouse-discovery"
99

1010
// altimate_change start — point at the locations the registry actually reads
11-
import { CONN_ENV_PREFIX, globalConfigPath, localConfigPath, projectConfigApplies } from "../native/connections/registry"
11+
import { CONN_ENV_PREFIX, globalConfigPath, localConfigPath } from "../native/connections/registry"
1212

1313
/** The no-connections hint. Paths come from the registry's own helpers so it cannot drift from what is loaded. */
1414
export function noConnectionsMessage(): string {
@@ -19,8 +19,8 @@ export function noConnectionsMessage(): string {
1919
"",
2020
"Alternatively, connections are read from these sources (later ones override earlier ones):",
2121
` 1. ${globalConfigPath()} (global)`,
22-
...(projectConfigApplies() ? [` 2. ${localConfigPath()} (this project)`] : []),
23-
` ${projectConfigApplies() ? 3 : 2}. ${CONN_ENV_PREFIX}<NAME> environment variables, each holding one connection's config as JSON (set them in the shell before starting altimate-code; a running process cannot see variables exported later)`,
22+
` 2. ${localConfigPath()} (this project)`,
23+
` 3. ${CONN_ENV_PREFIX}<NAME> environment variables, each holding one connection's config as JSON (set them in the shell before starting altimate-code; a running process cannot see variables exported later)`,
2424
"",
2525
"After adding the first connection file, call warehouse_list again. Edits to an existing connections.json take effect after a restart. Example connections.json:",
2626
"{",

‎packages/opencode/src/project/instance.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export const Instance = {
8181
return input.fn()
8282
})
8383
},
84+
// altimate_change start — enumerate open projects even before their warehouse registry is loaded
85+
directories(): string[] {
86+
return [...cache.keys()]
87+
},
88+
// altimate_change end
8489
get current() {
8590
return context.use()
8691
},

0 commit comments

Comments
 (0)