src/vs/workbench/contrib/terminal/browser/terminalService.ts/TerminalService/handleNewRegisteredBackend
class TerminalService:	handleNewRegisteredBackend(backend: ITerminalBackend) {
		if (backend.remoteAuthority === this._environmentService.remoteAuthority) {
			this._primaryBackend = backend;
			const enableTerminalReconnection = this.configHelper.config.enablePersistentSessions;

			// Connect to the extension host if it's there, set the connection state to connected when
			// it's done. This should happen even when there is no extension host.
			this._connectionState = TerminalConnectionState.Connecting;

			const isPersistentRemote = !!this._environmentService.remoteAuthority && enableTerminalReconnection;

			if (isPersistentRemote) {
				this._remoteTerminalsInitPromise = this._reconnectToRemoteTerminals();
			} else if (enableTerminalReconnection) {
				this._localTerminalsInitPromise = this._reconnectToLocalTerminals();
			} else {
				this._connectionState = TerminalConnectionState.Connected;
			}

			backend.onDidRequestDetach(async (e) => {
				const instanceToDetach = this.getInstanceFromResource(getTerminalUri(e.workspaceId, e.instanceId));
				if (instanceToDetach) {
					const persistentProcessId = instanceToDetach?.persistentProcessId;
					if (persistentProcessId && !instanceToDetach.shellLaunchConfig.isFeatureTerminal && !instanceToDetach.shellLaunchConfig.customPtyImplementation) {
						if (instanceToDetach.target === TerminalLocation.Editor) {
							this._terminalEditorService.detachInstance(instanceToDetach);
						} else {
							this._terminalGroupService.getGroupForInstance(instanceToDetach)?.removeInstance(instanceToDetach);
						}
						await instanceToDetach.detachFromProcess();
						await this._primaryBackend?.acceptDetachInstanceReply(e.requestId, persistentProcessId);
					} else {
						// will get rejected without a persistentProcessId to attach to
						await this._primaryBackend?.acceptDetachInstanceReply(e.requestId, undefined);
					}
				}
			});
		}
	}