fix(plugins): stabilize plugin integration

This commit is contained in:
Jinpy authored and GitHub committed 2026-09-14 21:03:47 +08:00
1 parent b70d6da227
commit 3d1b84831f
76 files changed
+3846 -431

No files matched your search

+114 -23
View File
@@ -3398,7 +3398,16 @@ export const useQueryStore = defineStore("query", () => {
if (!options.forceNew) {
const existing = tabs.value.find((tab) => tab.mode === "plugin-workbench" && tab.pluginWorkbench?.pluginId === pluginId && tab.pluginWorkbench?.contributionId === contributionId && tab.connectionId === (options.connectionId || ""));
if (existing) {
if (options.context) existing.pluginWorkbench = { ...existing.pluginWorkbench!, context: snapshotPluginWorkbenchContext(options.context) };
// Reopening surfaces the existing tab as-is. Replacing the context
// here (openPluginConnection mints a fresh workbenchId per click)
// would deep-reload the plugin webview — a full flash plus losing
// the sidecar session binding on the old workbench id.
// A tab created by an older build (or otherwise unregistered) may
// still be ownerless; land it in the workspace or the group-rendered
// tab strips can never show it.
if (!groupForTab(existing.id)) {
ensureTabInWorkspace(existing.id);
}
switchTab(existing.id);
return existing.id;
}
@@ -3421,6 +3430,9 @@ export const useQueryStore = defineStore("query", () => {
context: options.context ? snapshotPluginWorkbenchContext(options.context) : undefined,
},
};
// The split workspace renders strips from group membership — always go
// through registerOpenTab so the tab joins the focused group (raw push
// left it ownerless and invisible in every tab strip).
return registerOpenTab(tab);
}
@@ -3429,6 +3441,9 @@ export const useQueryStore = defineStore("query", () => {
const existing = tabs.value.find((tab) => tab.mode === "plugin-filesystem" && tab.pluginFilesystem?.pluginId === pluginId && tab.pluginFilesystem?.providerId === providerId && tab.connectionId === (options.connectionId || ""));
if (existing) {
if (options.currentUri) existing.pluginFilesystem = { ...existing.pluginFilesystem!, currentUri: options.currentUri };
if (!groupForTab(existing.id)) {
ensureTabInWorkspace(existing.id);
}
switchTab(existing.id);
return existing.id;
}
@@ -3475,6 +3490,15 @@ export const useQueryStore = defineStore("query", () => {
connectionId,
providerId,
connectionType: connection.plugin_connection_type,
workbenchId: crypto.randomUUID(),
connection: {
id: connection.id,
name: connection.name,
host: connection.host,
port: connection.port,
username: connection.username,
readOnly: false,
},
},
});
}
@@ -3482,12 +3506,44 @@ export const useQueryStore = defineStore("query", () => {
const filesystem = filesystemProviderId ? registry.listFilesystemProviders().find((entry) => entry.plugin.manifest.id === pluginId && entry.contribution.id === filesystemProviderId) : undefined;
if (!filesystem) throw new Error(`Plugin connection provider '${pluginId}/${providerId}' does not declare a workbench or filesystem provider`);
return openPluginFilesystem(pluginId, filesystem.contribution.id, {
title: connection.name,
title: `${connection.name} · SFTP`,
connectionId,
rootUri: filesystem.contribution.root_uri,
});
}
/**
* Boot-time tab restore only replays tab metadata: after a host restart the
* sidecar's in-memory connection registry starts empty (credentials are
* injected per connect via `connection/connect`), so a restored plugin
* workbench/filesystem tab fails its first session/open with "Connection is
* not active" until the user reopens the connection from the sidebar. Replay
* the connect lifecycle for every distinct restored plugin connection —
* fire-and-forget so an interactive password prompt or a slow sidecar spawn
* never blocks startup; per-connection failures surface in the tab's own UI.
*/
async function reconnectRestoredPluginTabs() {
const connectionStore = useConnectionStore();
const activeConnectionId = tabs.value.find((tab) => tab.id === activeTabId.value)?.connectionId;
const orderedIds: string[] = [];
for (const tab of tabs.value) {
if (tab.mode !== "plugin-workbench" && tab.mode !== "plugin-filesystem") continue;
if (!tab.connectionId || orderedIds.includes(tab.connectionId)) continue;
if (tab.connectionId === activeConnectionId) orderedIds.unshift(tab.connectionId);
else orderedIds.push(tab.connectionId);
}
for (const connectionId of orderedIds) {
if (connectionStore.getConfig(connectionId)?.db_type !== "plugin") continue;
try {
// activate:false keeps the boot restore from overriding the last
// active connection already chosen by restoreActiveConnectionContext().
await connectionStore.ensureConnected(connectionId, { activate: false });
} catch (error) {
console.warn("[DBX][plugin-tab-restore:reconnect]", connectionId, error);
}
}
}
function applyTableStructureInitialTab(tab: QueryTab, initialTab?: TableInfoTab, initialTarget?: TableStructureEditorTarget) {
if (!initialTab && !initialTarget?.name) return;
if (initialTab) tab.structureInitialTab = initialTab;
@@ -3790,6 +3846,39 @@ export const useQueryStore = defineStore("query", () => {
continuePendingBatchClose();
}
const pluginReleaseInFlight = new Set<string>();
/**
* Closing the last plugin tab (workbench or filesystem) bound to a plugin
* connection ends that connection: the sidecar session has no remaining
* owner, so disconnecting tears the pool down and drops the sidebar's
* "connected" dot. Fire-and-forget; concurrent releases for the same
* connection coalesce, and a reopen racing the release wins.
*/
function releasePluginConnectionsAfterClose(closedTabs: ReadonlyArray<QueryTab>) {
const connectionIds = new Set(closedTabs.filter((tab) => (tab.mode === "plugin-workbench" || tab.mode === "plugin-filesystem") && tab.connectionId).map((tab) => tab.connectionId));
for (const connectionId of connectionIds) {
if (pluginReleaseInFlight.has(connectionId)) continue;
pluginReleaseInFlight.add(connectionId);
void (async () => {
try {
// Yield one microtask so a same-tick reopen registers its
// replacement plugin tab before we tear the connection down.
await Promise.resolve();
if (tabs.value.some((tab) => (tab.mode === "plugin-workbench" || tab.mode === "plugin-filesystem") && tab.connectionId === connectionId)) return;
const connectionStore = useConnectionStore();
// A user-initiated disconnect that closed these tabs already owns
// the teardown — don't stack a second one on top of it.
if (connectionStore.hasDisconnectInFlight(connectionId)) return;
await connectionStore.disconnect(connectionId);
} catch (error) {
console.warn("[DBX][plugin-tab-close:disconnect]", connectionId, error);
} finally {
pluginReleaseInFlight.delete(connectionId);
}
})();
}
}
function closeTab(id: string, { force = false }: { force?: boolean } = {}) {
const tab = tabs.value.find((t) => t.id === id);
if (!tab) return;
@@ -3822,6 +3911,7 @@ export const useQueryStore = defineStore("query", () => {
const wasGlobalActive = activeTabId.value === id;
tabs.value.splice(idx, 1);
releasePluginConnectionsAfterClose([tab]);
if (tab.externalSqlPath) refreshExternalSqlFileTitles();
if (owner) {
@@ -4161,29 +4251,29 @@ export const useQueryStore = defineStore("query", () => {
const closingIds = new Set(tabs.value.filter((tab) => predicate(tab)).map((tab) => tab.id));
if (closingIds.size === 0) return;
tabs.value
.filter((tab) => closingIds.has(tab.id))
.forEach((tab) => {
if (tab.mode === "sqlserver-trace") void disposeSqlServerActivityTrace(tab.id);
clearDataGridPendingSnapshotsForTab(tab.id);
beginClosingDataGridViewSnapshotsForTab(tab.id);
beginClosingBrowserState(tab.id);
clearDataGridStructuredFilterStatesForTab(tab.id);
clearDataGridSearchStatesForTab(tab.id);
if (tab.txnSessionId) void rollbackTransaction(tab.id);
if (tab.isExecuting) void cancelTabExecution(tab.id);
if (tab.isExplaining) void cancelTabExplain(tab.id);
void closeResultSession(tab);
void closeClientConnectionSession(tab);
clearResultRunSnapshots(tab);
void deleteTabResultSnapshot(tabResultCacheKey(tab.id));
releaseTabResultObjectPayloads(tab);
clearResultRuns(tab);
clearResultPayload(tab);
});
const closingTabs = tabs.value.filter((tab) => closingIds.has(tab.id));
closingTabs.forEach((tab) => {
if (tab.mode === "sqlserver-trace") void disposeSqlServerActivityTrace(tab.id);
clearDataGridPendingSnapshotsForTab(tab.id);
beginClosingDataGridViewSnapshotsForTab(tab.id);
beginClosingBrowserState(tab.id);
clearDataGridStructuredFilterStatesForTab(tab.id);
clearDataGridSearchStatesForTab(tab.id);
if (tab.txnSessionId) void rollbackTransaction(tab.id);
if (tab.isExecuting) void cancelTabExecution(tab.id);
if (tab.isExplaining) void cancelTabExplain(tab.id);
void closeResultSession(tab);
void closeClientConnectionSession(tab);
clearResultRunSnapshots(tab);
void deleteTabResultSnapshot(tabResultCacheKey(tab.id));
releaseTabResultObjectPayloads(tab);
clearResultRuns(tab);
clearResultPayload(tab);
});
const activeClosingIndex = tabs.value.findIndex((tab) => tab.id === activeTabId.value && closingIds.has(tab.id));
tabs.value = tabs.value.filter((tab) => !closingIds.has(tab.id));
releasePluginConnectionsAfterClose(closingTabs);
// Resolve the fallback against the POST-filter array: the survivor at the
// closing tab's index, not the closing tab itself.
const fallbackTabId = activeClosingIndex >= 0 ? tabs.value[Math.min(activeClosingIndex, tabs.value.length - 1)]?.id : undefined;
@@ -8492,10 +8582,11 @@ export const useQueryStore = defineStore("query", () => {
openMqAdmin,
openMqttAdmin,
openNacosAdmin,
clearNacosNavigationTarget,
openPluginWorkbench,
openPluginFilesystem,
reconnectRestoredPluginTabs,
openPluginConnection,
clearNacosNavigationTarget,
openTableStructure,
linkSavedSql,
linkExternalSqlPath,