2626import { DATAMATE_KEY } from "@/altimate/datamate-transport"
2727import { MCP } from "@/mcp"
2828import { Config } from "@/config/config"
29- import { currentDirectory , isEnabled , isHeadless , isServe , log , syncInternals } from "./engine-seams"
29+ import {
30+ currentDirectory ,
31+ isEnabled ,
32+ isHeadless ,
33+ isServe ,
34+ log ,
35+ syncInternals ,
36+ type ScopedBinding ,
37+ } from "./engine-seams"
3038import { declaredBounded , notify , printLine , resolveBinding , versionOf , which } from "./engine-probes"
3139import {
3240 ENGINE_BINARY ,
@@ -95,7 +103,10 @@ export function invalidateProbe(): void {
95103
96104type Overlay = {
97105 directory : string
98- workspace : { id : string ; name : string }
106+ /** `key` is the workspace's identity across accounts — ids are tenant-local,
107+ * so the same number in another tenant is another workspace, and a session
108+ * that switched accounts must not keep the old one's engine or inventory. */
109+ workspace : { id : string ; name : string ; key : string }
99110 /** The derived entry, or null when the engine is unusable. */
100111 entry : LocalMcpConfig | null
101112 refusal : Extract < Outcome , { kind : "engine-missing" | "engine-too-old" } > | null
@@ -138,6 +149,12 @@ function stateFor(directory: string): DirectoryState {
138149 return state
139150}
140151
152+ /** Identity of the workspace a binding names: the credential scope it was
153+ * read under plus the tenant-local id. */
154+ function workspaceKey ( binding : ScopedBinding ) : string {
155+ return `${ binding . scope ?? "" } |${ binding . datamateId } `
156+ }
157+
141158function sameEntry ( a : LocalMcpConfig | null , b : LocalMcpConfig | null ) : boolean {
142159 return ! ! a && ! ! b && a . command . join ( "\0" ) === b . command . join ( "\0" )
143160}
@@ -176,7 +193,11 @@ export async function overlay(
176193 state . current = null
177194 return
178195 }
179- const workspace = { id : String ( binding . datamateId ) , name : binding . datamateName }
196+ const workspace = {
197+ id : String ( binding . datamateId ) ,
198+ name : binding . datamateName ,
199+ key : workspaceKey ( binding ) ,
200+ }
180201 const probe = await probeEngine ( )
181202 if ( probe . kind === "ok" ) {
182203 const entry = engineEntry ( workspace . id )
@@ -213,7 +234,8 @@ export async function overlay(
213234 * the key and say why, instead of replacing the engine underneath a turn. */
214235export function managedWorkspace ( directory : string | null = currentDirectory ( ) ) : { id : string ; name : string } | null {
215236 if ( ! directory ) return null
216- return directories . get ( directory ) ?. current ?. workspace ?? null
237+ const workspace = directories . get ( directory ) ?. current ?. workspace
238+ return workspace ? { id : workspace . id , name : workspace . name } : null
217239}
218240
219241/** `managedWorkspace` once the overlay has run for this instance. The overlay
@@ -287,11 +309,13 @@ async function releaseKey(loaded: { mcp?: Record<string, unknown> } | undefined,
287309 }
288310}
289311
290- async function declaredFor ( workspaceId : string ) : Promise < Declared | null > {
291- const cached = declaredCache . get ( workspaceId )
312+ async function declaredFor ( workspace : { id : string ; key : string } ) : Promise < Declared | null > {
313+ // Cached per workspace identity, not per id: the same id in another tenant
314+ // is another allowlist.
315+ const cached = declaredCache . get ( workspace . key )
292316 if ( cached && ( cached . value || now ( ) - cached . at < DECLARED_RETRY_MS ) ) return cached . value
293- const value = await declaredBounded ( workspaceId )
294- declaredCache . set ( workspaceId , { value, at : now ( ) } )
317+ const value = await declaredBounded ( workspace . id )
318+ declaredCache . set ( workspace . key , { value, at : now ( ) } )
295319 return value
296320}
297321
@@ -389,12 +413,13 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
389413 record ( sessionID , { kind : "unbound" } )
390414 return
391415 }
392- const workspaceId = String ( binding . datamateId )
416+ const boundKey = workspaceKey ( binding )
393417
394- // Reload the overlay when the binding moved, or when a refused engine may
395- // have appeared since (the probe memo bounds how often that is asked).
418+ // Reload the overlay when the binding moved — to another workspace, or the
419+ // same id under another account — or when a refused engine may have
420+ // appeared since (the probe memo bounds how often that is asked).
396421 let reload = state . current
397- ? state . current . workspace . id !== workspaceId
422+ ? state . current . workspace . key !== boundKey
398423 : state . failedAt === undefined || now ( ) - state . failedAt >= FAILED_PROBE_TTL_MS
399424 if ( ! reload && state . current && ! state . current . entry ) {
400425 const probe = await probeEngine ( )
@@ -410,17 +435,21 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
410435 // last applied for this same workspace: a running engine is not released
411436 // over a fault in the probe. After a relink nothing is kept — workspace A's
412437 // engine must not serve a directory now bound to B.
413- const retained = state . failedAt !== undefined && state . applied ?. workspace . id === workspaceId ? state . applied : null
438+ const retained = state . failedAt !== undefined && state . applied ?. workspace . key === boundKey ? state . applied : null
414439 const overlayNow = state . current ?? retained
415440 if ( ! overlayNow ) {
416- if ( state . applied ) await releaseKey ( loaded , ! ! state . applied . entry )
417- state . applied = null
418441 if ( state . failedAt === undefined ) {
442+ if ( state . applied ) await releaseKey ( loaded , ! ! state . applied . entry )
443+ state . applied = null
419444 record ( sessionID , { kind : "unbound" } )
420445 return
421446 }
422- // Bound, but the overlay could not be derived: say so, once, rather than
423- // settling a bound directory as unbound in silence.
447+ // Bound, but the overlay could not be derived. Whatever runs under the key
448+ // is dropped and nothing is handed back: the reloaded config may carry a
449+ // raw IDE or hosted entry, and that must not answer for this workspace.
450+ if ( state . applied ?. entry || DATAMATE_KEY in ( await mcp ( ) . status ( ) ) ) await mcp ( ) . remove ( DATAMATE_KEY )
451+ state . applied = null
452+ // Say so, once, rather than settling a bound directory as unbound in silence.
424453 const outcome : Outcome = { kind : "connect-failed" , error : "the workspace engine could not be checked" }
425454 record ( sessionID , outcome )
426455 await announceRefusal ( sessionID , outcome , {
@@ -435,7 +464,12 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
435464 // Bring MCP in line with the overlay: start or replace the engine when the
436465 // derived entry changed, drop it when there is none any more.
437466 if ( overlayNow . entry ) {
438- if ( ! sameEntry ( state . applied ?. entry ?? null , overlayNow . entry ) ) await mcp ( ) . add ( DATAMATE_KEY , overlayNow . entry )
467+ // The argv is the same for the same id in another tenant; the engine
468+ // reads its credentials when it starts, so it is replaced on identity, not
469+ // only on argv.
470+ const replaced =
471+ ! sameEntry ( state . applied ?. entry ?? null , overlayNow . entry ) || state . applied ?. workspace . key !== workspace . key
472+ if ( replaced ) await mcp ( ) . add ( DATAMATE_KEY , overlayNow . entry )
439473 } else if ( state . applied ?. entry || DATAMATE_KEY in ( await mcp ( ) . status ( ) ) ) {
440474 // Ours to drop — or a client that predates the link, which MCP bootstrapped
441475 // from an IDE or hosted entry while the directory was unbound. With the
@@ -447,7 +481,7 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
447481 if ( ! overlayNow . entry ) {
448482 const refusal = overlayNow . refusal ?? { kind : "engine-missing" as const }
449483 if ( refusal . kind === "engine-missing" ) {
450- const declared = await declaredFor ( workspace . id )
484+ const declared = await declaredFor ( workspace )
451485 const count = declared ?. keys . length
452486 const outcome : Outcome =
453487 count === undefined ? { kind : "engine-missing" } : { kind : "engine-missing" , declared : count }
@@ -474,7 +508,7 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
474508
475509 // The engine is configured. The first status call boots MCP, which awaits
476510 // the engine's handshake; the allowlist lookup overlaps with it.
477- const [ statusMap , declared ] = await Promise . all ( [ mcp ( ) . status ( ) , declaredFor ( workspace . id ) ] )
511+ const [ statusMap , declared ] = await Promise . all ( [ mcp ( ) . status ( ) , declaredFor ( workspace ) ] )
478512 let status = statusMap [ DATAMATE_KEY ]
479513 const session = sessions . get ( sessionID )
480514 if ( status ?. status !== "connected" && ! session ?. retried ) {
0 commit comments