import { c as isRecord } from "./record-coerce-DItp3I4t.js";
import { l as isOperatorScope, t as ADMIN_SCOPE } from "./operator-scopes-Dw7Gu2cA.js";
import { t as ErrorCodes } from "./gateway-error-details-16gBJ-B1.js";
import { i as gatewayStartupUnavailableDetails } from "./startup-unavailable-D0-EeFjq.js";
import { d as getActivePluginRegistry, l as getActivePluginHttpRouteRegistry } from "./runtime-D0VWfhor.js";
import { i as getPluginRuntimeGatewayRequestScope, s as withPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-Bj2ffrHm.js";
import { Sd as GATEWAY_SUSPEND_UNAVAILABLE_REASON, xd as GATEWAY_RESTART_UNAVAILABLE_REASON } from "./src-CwDR-7yt.js";
import { d as isCoreGatewayMethodClassified, n as authorizeOperatorScopesForMethod, p as listCoreGatewayHandlerMethodNames, r as authorizeOperatorScopesForRequiredScope, s as resolveLeastPrivilegeOperatorScopesForMethod, u as createCoreGatewayMethodDescriptors } from "./method-scopes-CuEAYEiZ.js";
import { C as tryBeginGatewayRootWorkAdmission, l as isGatewayRestartDraining, s as getGatewaySuspendAdmissionPhase, x as tryBeginGatewayPreparedRestartRootWorkAdmission } from "./gateway-work-admission-Cq7eCT39.js";
import { o as sessionMutationTargetFields, r as resolveDirectIncognitoTargets, v as SessionMutationAuthorizationChangedError } from "./session-sharing-target-input-CxrGyR08.js";
import { d as errorShape, f as missingScopeErrorShape } from "./error-codes-DEczInWy.js";
import { f as authenticatedProfileUnavailableError } from "./operator-role-policy-Blv6BRJs.js";
import { m as resolveSessionMutationAuthorization } from "./session-sharing-CkBLRjIT.js";
import { n as resolveControlPlaneActor, t as formatControlPlaneActor } from "./control-plane-audit-CN8L3SYx.js";
import { n as consumeControlPlaneWriteBudget, t as CONTROL_PLANE_RATE_LIMIT_WINDOW_MS } from "./control-plane-rate-limit-BtKY9m7Q.js";
import { n as createGatewayMethodRegistry, r as createPluginGatewayMethodDescriptors, t as createGatewayMethodDescriptorsFromHandlers } from "./registry-DKlEtulk.js";
import { n as parseGatewayRole, t as isRoleAuthorizedForMethod } from "./role-policy-BUa93-QC.js";
import { t as isTargetedNonSafeGatewayRestartRequest } from "./restart-request-cVXXa5KE.js";
import { n as classifyGatewayStaleInstall } from "./stale-install-BYIlxkPn.js";
//#region src/gateway/server-methods/lazy-core-handlers.ts
function lazyHandlerModule(loadModule, selectHandlers) {
	let handlersPromise = null;
	return () => handlersPromise ??= loadModule().then(selectHandlers);
}
function createLazyCoreHandlers(params) {
	return Object.fromEntries(params.methods.map((method) => [method, async (opts) => {
		const handler = (await params.loadHandlers())[method];
		if (!handler) throw new Error(`lazy gateway handler not found: ${method}`);
		await handler(opts);
	}]));
}
//#endregion
//#region src/gateway/server-methods.ts
const CORE_GATEWAY_HANDLER_MODULES = {
	agent: () => import("./agent-DaMqhk43.js").then((module) => module.agentHandlers),
	"agent-identity": () => import("./agent-identity-n1T85guV.js").then((module) => module.agentIdentityHandlers),
	agents: () => import("./agents-D54RhKOZ.js").then((module) => module.agentsHandlers),
	"agents-workspace": () => import("./agents-workspace-CXRNWSSM.js").then((module) => module.agentsWorkspaceHandlers),
	artifacts: () => import("./artifacts-BdM1H0hf.js").then((module) => module.artifactsHandlers),
	board: () => import("./board-CnGvSHvH.js").then((module) => module.boardHandlers),
	audit: () => import("./audit-Dvdx-WP9.js").then((module) => module.auditHandlers),
	users: () => import("./users-CfYOXm5Y.js").then((module) => module.usersHandlers),
	attach: () => import("./attach-BUlt7BFq.js").then((module) => module.attachHandlers),
	channels: () => import("./channels-CGajiUb1.js").then((module) => module.channelsHandlers),
	"channel-pairing": () => import("./channel-pairing-BwjUEa2y.js").then((module) => module.channelPairingHandlers),
	chat: () => import("./chat-Ck4tH4M2.js").then((module) => module.chatHandlers),
	"chat-abort": () => import("./chat-abort-handler-BMe3vkd2.js").then((module) => ({ "chat.abort": module.handleChatAbortRequest })),
	commands: () => import("./commands-BJDCRe5L.js").then((module) => module.commandsHandlers),
	config: () => import("./config-Bb8EyV26.js").then((module) => module.configHandlers),
	conversations: () => import("./conversations-CKaNTpUx.js").then((module) => module.conversationHandlers),
	connect: () => import("./connect-D7OaorII.js").then((module) => module.connectHandlers),
	"control-ui": () => import("./control-ui-Dk_md1yT.js").then((module) => module.controlUiHandlers),
	cron: () => import("./cron-BLYIMAMC.js").then((module) => module.cronHandlers),
	devices: () => import("./devices-jCuA1qAN.js").then((module) => module.deviceHandlers),
	"device-pair-setup": () => import("./device-pair-setup-A_mjtrFe.js").then((module) => module.devicePairSetupHandlers),
	diagnostics: () => import("./diagnostics-DOyRD5f_.js").then((module) => module.diagnosticsHandlers),
	doctor: () => import("./doctor-BrEE92JD.js").then((module) => module.createDoctorHandlers()),
	environments: () => import("./environments-CKcy1Op9.js").then((module) => module.environmentsHandlers),
	worktrees: () => import("./worktrees-GT6wvuip.js").then((module) => module.worktreesHandlers),
	"exec-approvals": () => import("./exec-approvals-CxTQeOVi.js").then((module) => module.execApprovalsHandlers),
	fs: () => import("./fs-DyoZoC_4.js").then((module) => module.fsHandlers),
	health: () => import("./health-CJZZs_Nq.js").then((module) => module.healthHandlers),
	logs: () => import("./logs-BlAv0bjK.js").then((module) => module.logsHandlers),
	"memory-search": () => import("./memory-search-DzCI2zXO.js").then((module) => module.memorySearchHandlers),
	terminal: () => import("./terminal-0vOMWxeA.js").then((module) => module.terminalHandlers),
	"ui-command": () => import("./ui-command-DZwV7wDK.js").then((module) => module.uiCommandHandlers),
	"models-auth-status": () => import("./models-auth-status-CY1Urs5B.js").then((module) => module.modelsAuthStatusHandlers),
	models: () => import("./models-DblDP4pO.js").then((module) => module.modelsHandlers),
	"models-probe": () => import("./models-probe-SqbtcDg8.js").then((module) => module.modelsProbeHandlers),
	"native-hook-relay": () => import("./native-hook-relay-Ba6A6Gt7.js").then((module) => module.nativeHookRelayHandlers),
	"nodes-pending": () => import("./nodes.pending-work-CvNigYBl.js").then((module) => module.nodePendingWorkHandlers),
	nodes: () => import("./nodes-BVDMvQ0n.js").then((module) => module.nodeHandlers),
	"plugin-host-hooks": () => import("./plugin-host-hooks-CvrRV2Vx.js").then((module) => module.pluginHostHookHandlers),
	plugins: () => import("./plugins-10zmYF6O.js").then((module) => module.pluginsHandlers),
	projects: () => import("./projects--J5ZNEEz.js").then((module) => module.projectsHandlers),
	portals: () => import("./portals-DDisiuQ4.js").then((module) => module.portalHandlers),
	"progress-card": () => import("./progress-card-M8eg67-8.js").then((module) => module.progressCardHandlers),
	migrations: () => import("./migrations--aj0jN9C.js").then((module) => module.migrationsHandlers),
	push: () => import("./push-D_ZB8vmf.js").then((module) => module.pushHandlers),
	restart: () => import("./restart-Bh-58dbV.js").then((module) => module.restartHandlers),
	suspend: () => import("./suspend-BX4B4Znr.js").then((module) => module.suspendHandlers),
	send: () => import("./send-CcjRWFvK.js").then((module) => module.sendHandlers),
	"sessions-files": () => import("./sessions-files-Bq5nLT7G.js").then((module) => module.sessionsFilesHandlers),
	"sessions-github": () => import("./sessions-github-CcQRyBNN.js").then((module) => module.sessionsGitHubHandlers),
	"sessions-diff": () => import("./sessions-diff-C7Rdz-B6.js").then((module) => module.sessionsDiffHandlers),
	"sessions-abort": () => import("./sessions-abort-DOczVFCs.js").then((module) => module.sessionAbortHandlers),
	"sessions-compact": () => import("./sessions-compact-kQDAh7th.js").then((module) => module.sessionCompactHandlers),
	"sessions-compaction-checkpoints": () => import("./sessions-compaction-checkpoints-BjKMtrUa.js").then((module) => module.sessionCheckpointHandlers),
	"sessions-compaction-queries": () => import("./sessions-compaction-queries-DiLqTqH7.js").then((module) => module.sessionCheckpointQueryHandlers),
	"sessions-create": () => import("./sessions-create-CokPa9ie.js").then((module) => module.sessionCreateHandlers),
	"sessions-recover": () => import("./sessions-recover-kns5rp-C.js").then((module) => module.sessionRecoverHandlers),
	"sessions-delete": () => import("./sessions-delete-Db-SmYbo.js").then((module) => module.sessionDeleteHandlers),
	"sessions-dispatch": () => import("./sessions-dispatch-BbgHOBOv.js").then((module) => module.sessionDispatchHandlers),
	"sessions-groups": () => import("./sessions-groups-BN3OVgOm.js").then((module) => module.sessionGroupHandlers),
	"sessions-goal": () => import("./sessions-goal-B_7_9KAm.js").then((module) => module.sessionGoalHandlers),
	"sessions-messaging": () => import("./sessions-messaging-BVPW0aew.js").then((module) => module.sessionMessagingHandlers),
	"sessions-mutations": () => import("./sessions-mutations-CDs_egBg.js").then((module) => module.sessionMutationHandlers),
	"sessions-read": () => import("./sessions-read-BlAiIR3i.js").then((module) => module.sessionReadHandlers),
	"sessions-rewind": () => import("./sessions-rewind-B7B88-BE.js").then((module) => module.sessionRewindHandlers),
	"sessions-sharing": () => import("./sessions-sharing-Cl42Wtrn.js").then((module) => module.sessionSharingHandlers),
	"sessions-subscriptions": () => import("./sessions-subscriptions-Cy6KKBrW.js").then((module) => module.sessionSubscriptionHandlers),
	"sessions-suggestions": () => import("./sessions-suggestions-EsFnW6iC.js").then((module) => module.sessionSuggestionHandlers),
	"session-catalog": () => import("./session-catalog-CEg1Wm6_.js").then((module) => module.sessionCatalogHandlers),
	"session-discussion": () => import("./session-discussion-CAjuv_AI.js").then((module) => module.sessionDiscussionHandlers),
	"session-observer-rpc": () => import("./session-observer-rpc-KDEhVuEK.js").then((module) => module.sessionObserverHandlers),
	"session-companion-rpc": () => import("./session-companion-rpc-FMS7TsPA.js").then((module) => module.sessionCompanionHandlers),
	"hooks-status": () => import("./hooks-status-B_d8Rcw1.js").then((module) => module.hooksStatusHandlers),
	skills: () => import("./skills-DfxABu8r.js").then((module) => module.skillsHandlers),
	system: () => import("./system-BiW8SOkU.js").then((module) => module.systemHandlers),
	talk: () => import("./talk-0q3z2sKN.js").then((module) => module.talkHandlers),
	tasks: () => import("./tasks-GVEtfAv3.js").then((module) => module.tasksHandlers),
	"task-suggestions": () => import("./task-suggestions-CiHQ8ANT.js").then((module) => module.taskSuggestionsHandlers),
	"tools-catalog": () => import("./tools-catalog-B15-XNoN.js").then((module) => module.toolsCatalogHandlers),
	"tools-github": () => import("./tools-github-DkA5dnKS.js").then((module) => module.toolsGitHubHandlers),
	"tools-effective": () => import("./tools-effective-CxrJHei0.js").then((module) => module.toolsEffectiveHandlers),
	"tools-invoke": () => import("./tools-invoke-DRST7r-G.js").then((module) => module.toolsInvokeHandlers),
	"mcp-app": () => import("./mcp-app-BB_3pL1J.js").then((module) => module.mcpAppHandlers),
	tts: () => import("./tts-CMX9_zpk.js").then((module) => module.ttsHandlers),
	update: () => import("./update-Dgp8tVxY.js").then((module) => module.updateHandlers),
	usage: () => import("./usage-DCXoJwIm.js").then((module) => module.usageHandlers),
	"voicewake-routing": () => import("./voicewake-routing-dw1JHncA.js").then((module) => module.voicewakeRoutingHandlers),
	voicewake: () => import("./voicewake-C-Ux_401.js").then((module) => module.voicewakeHandlers),
	web: () => import("./web-Bo6I1FHb.js").then((module) => module.webHandlers),
	"system-agent": () => import("./system-agent-Di80Mpd_.js").then((module) => module.systemAgentHandlers),
	"system-changes": () => import("./system-changes-DJ0TUdfr.js").then((module) => module.systemChangesHandlers),
	wizard: () => import("./wizard-bbb0tBVW.js").then((module) => module.wizardHandlers)
};
function authorizeGatewayMethod(method, client, params, methodRegistry) {
	if (!client?.connect || method === "health") return null;
	const roleRaw = client.connect.role ?? "operator";
	const role = parseGatewayRole(roleRaw);
	if (!role) return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${roleRaw}`);
	const scopes = client.connect.scopes ?? [];
	if (!isRoleAuthorizedForMethod(role, method)) return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
	if (role === "node") return null;
	if (method === "device.scopes.requestUpgrade" || method === "device.scopes.waitUpgrade") return null;
	if (scopes.includes("operator.admin")) return null;
	const registeredScope = methodRegistry.getScope(method);
	const scopeAuth = isOperatorScope(registeredScope) ? authorizeOperatorScopesForRequiredScope(registeredScope, scopes) : authorizeOperatorScopesForMethod(method, scopes, params);
	if (!scopeAuth.allowed) {
		const resolvedRequiredScopes = isOperatorScope(registeredScope) ? [registeredScope] : resolveLeastPrivilegeOperatorScopesForMethod(method, params);
		return missingScopeErrorShape({
			missingScope: scopeAuth.missingScope,
			requiredScopes: resolvedRequiredScopes.length > 0 ? resolvedRequiredScopes : [scopeAuth.missingScope]
		});
	}
	return null;
}
const SUSPEND_CONTROL_METHODS = /* @__PURE__ */ new Set([
	"gateway.suspend.prepare",
	"gateway.suspend.status",
	"gateway.suspend.resume"
]);
function isGatewayMethodAllowedDuringSuspension(method) {
	return SUSPEND_CONTROL_METHODS.has(method);
}
function runGatewayPendingWorkContinuation(params) {
	if (!isRecord(params.requestParams)) return null;
	const request = params.requestParams;
	if (params.client?.connect.role === "node") {
		if (getGatewaySuspendAdmissionPhase() !== "draining" && !isGatewayRestartDraining()) return null;
		const invokeId = params.method === "node.invoke.progress" ? request.invokeId : params.method === "node.invoke.result" ? request.id : void 0;
		if (typeof invokeId !== "string" || typeof request.nodeId !== "string") return null;
		return params.context.nodeRegistry.runPendingInvokeContinuation({
			invokeId,
			nodeId: request.nodeId,
			connId: params.client.connId,
			run: params.run
		});
	}
	if (getGatewaySuspendAdmissionPhase() !== "draining" || params.client?.connect.role !== "operator" || typeof request.id !== "string") return null;
	if (params.method === "question.resolve" || params.method === "question.get") return params.context.questionManager?.runPendingContinuation(request.id, params.run) ?? null;
	return (params.method === "exec.approval.resolve" ? params.context.execApprovalManager : params.method === "plugin.approval.resolve" ? params.context.pluginApprovalManager : params.method === "approval.resolve" ? request.kind === "exec" ? params.context.execApprovalManager : request.kind === "plugin" ? params.context.pluginApprovalManager : request.kind === "system-agent" ? params.context.systemAgentApprovalManager : void 0 : void 0)?.runPendingContinuation(request.id, params.run) ?? null;
}
async function authorizeAuthenticatedProfileForMethod(params) {
	const sync = params.client?.authenticatedGitHubIdentitySync;
	if (!sync || params.client?.authenticatedUserProfile?.profileId.trim()) return null;
	if (!(params.methodRegistry.requiresAuthenticatedProfile(params.method) || resolveDirectIncognitoTargets(params.method, params.requestParams).length > 0 || sessionMutationTargetFields(params.method).length > 0 && params.context.getRuntimeConfig().gateway?.roles !== void 0)) return null;
	try {
		await sync();
	} catch {
		return authenticatedProfileUnavailableError();
	}
	return params.client?.authenticatedUserProfile?.profileId.trim() ? null : authenticatedProfileUnavailableError();
}
const coreGatewayHandlerMethodNames = listCoreGatewayHandlerMethodNames();
const coreGatewayHandlerModules = Object.entries(CORE_GATEWAY_HANDLER_MODULES);
const coreGatewayHandlers = Object.fromEntries(coreGatewayHandlerModules.flatMap(([family, loadModule]) => Object.entries(createLazyCoreHandlers({
	methods: coreGatewayHandlerMethodNames.get(family) ?? [],
	loadHandlers: lazyHandlerModule(loadModule, (handlers) => handlers)
}))));
/** Builds the per-request method registry from core, plugin, and explicit extra handlers. */
function createRequestGatewayMethodRegistry(extraHandlers) {
	const gatewayPluginRegistry = getActivePluginHttpRouteRegistry();
	const gatewayPluginHandlers = gatewayPluginRegistry?.gatewayHandlers ?? {};
	const extraHandlerEntries = Object.entries(extraHandlers ?? {});
	const pluginMethodNames = new Set(Object.keys(gatewayPluginHandlers));
	const coreDescriptorHandlers = { ...coreGatewayHandlers };
	for (const [method, extraHandler] of extraHandlerEntries) if (!pluginMethodNames.has(method) && isCoreGatewayMethodClassified(method)) coreDescriptorHandlers[method] = extraHandler;
	const coreDescriptors = createCoreGatewayMethodDescriptors(coreDescriptorHandlers);
	for (const descriptor of coreDescriptors) {
		const extraHandler = extraHandlers?.[descriptor.name];
		if (extraHandler && !pluginMethodNames.has(descriptor.name)) descriptor.handler = extraHandler;
	}
	const coreMethodNames = new Set(coreDescriptors.map((descriptor) => descriptor.name));
	const auxHandlers = Object.fromEntries(extraHandlerEntries.filter(([method]) => !pluginMethodNames.has(method) && !coreMethodNames.has(method)));
	return createGatewayMethodRegistry([
		...coreDescriptors,
		...gatewayPluginRegistry ? createPluginGatewayMethodDescriptors(gatewayPluginRegistry) : [],
		...createGatewayMethodDescriptorsFromHandlers({
			handlers: auxHandlers,
			owner: {
				kind: "aux",
				area: "gateway-extra"
			},
			defaultScope: ADMIN_SCOPE
		})
	], gatewayPluginRegistry ?? void 0);
}
/** Applies the router-owned authorization fence before any transport or typed dispatch. */
async function authorizeGatewayRequestPreDispatch(params) {
	const authError = authorizeGatewayMethod(params.method, params.client, params.requestParams, params.methodRegistry);
	if (authError) return { error: authError };
	const profileError = await authorizeAuthenticatedProfileForMethod(params);
	if (profileError) return { error: profileError };
	if (params.context.unavailableGatewayMethods?.has(params.method)) return { error: errorShape(ErrorCodes.UNAVAILABLE, `${params.method} unavailable during gateway startup`, {
		retryable: true,
		retryAfterMs: 500,
		details: {
			...gatewayStartupUnavailableDetails(),
			method: params.method
		}
	}) };
	const sessionMutation = resolveSessionMutationAuthorization({
		client: params.client ?? null,
		method: params.method,
		requestParams: params.requestParams,
		context: params.context
	});
	if (sessionMutation.error) return { error: sessionMutation.error };
	if (params.client?.connect.role === "node" && (!params.client.connId || !await params.context.nodeRegistry.isConnectionCurrentPairingState(params.client.connId))) return { error: errorShape(ErrorCodes.UNAVAILABLE, "node pairing changed before request dispatch", {
		retryable: true,
		details: { code: "PAIRING_CHANGED" }
	}) };
	return {
		error: null,
		...sessionMutation.authorization ? { sessionMutationAuthorization: sessionMutation.authorization } : {}
	};
}
/** Runs admitted Gateway work inside the shared root and plugin request scopes. */
async function runWithGatewayRequestEnvelope(method, client, fn, options) {
	const rejectRateLimitedControlPlaneWrite = () => {
		if (!options.methodRegistry.isControlPlaneWrite(method)) return;
		const budget = consumeControlPlaneWriteBudget({
			client,
			method
		});
		if (budget.allowed) return;
		const actor = resolveControlPlaneActor(client);
		options.context.logGateway.warn(`control-plane write rate-limited method=${method} ${formatControlPlaneActor(actor)} retryAfterMs=${budget.retryAfterMs} key=${budget.key}`);
		return errorShape(ErrorCodes.UNAVAILABLE, `rate limit exceeded for ${method}; retry after ${Math.ceil(budget.retryAfterMs / 1e3)}s`, {
			retryable: true,
			retryAfterMs: budget.retryAfterMs,
			details: {
				method,
				limit: `30 per ${CONTROL_PLANE_RATE_LIMIT_WINDOW_MS / 1e3}s`
			}
		});
	};
	const isSuspendPrepare = method === "gateway.suspend.prepare";
	const preAdmissionRateLimitError = isSuspendPrepare ? rejectRateLimitedControlPlaneWrite() : void 0;
	if (preAdmissionRateLimitError) return await options.reject(preAdmissionRateLimitError);
	const rootWorkAdmission = tryBeginGatewayRootWorkAdmission() ?? (method === "gateway.restart.request" && isTargetedNonSafeGatewayRestartRequest(options.requestParams) ? tryBeginGatewayPreparedRestartRootWorkAdmission() : null);
	if (!rootWorkAdmission) {
		const continuation = runGatewayPendingWorkContinuation({
			method,
			client,
			requestParams: options.requestParams,
			context: options.context,
			run: invokeWithRequestScope
		});
		if (continuation) return await continuation;
	}
	if (isSuspendPrepare && rootWorkAdmission && !rootWorkAdmission.ownsRoot) return await options.reject(errorShape(ErrorCodes.UNAVAILABLE, "gateway suspension cannot begin from a nested request", {
		retryable: true,
		retryAfterMs: 1e3,
		details: {
			method,
			reason: "nested-gateway-request"
		}
	}));
	if (!rootWorkAdmission && !isGatewayMethodAllowedDuringSuspension(method)) {
		const restartDraining = isGatewayRestartDraining();
		return await options.reject(errorShape(ErrorCodes.UNAVAILABLE, `${method} unavailable during gateway ${restartDraining ? "restart" : "suspension"}`, {
			retryable: true,
			retryAfterMs: 1e3,
			details: {
				method,
				reason: restartDraining ? GATEWAY_RESTART_UNAVAILABLE_REASON : GATEWAY_SUSPEND_UNAVAILABLE_REASON,
				phase: getGatewaySuspendAdmissionPhase()
			}
		}));
	}
	async function invokeWithRequestScope() {
		const postAdmissionRateLimitError = isSuspendPrepare ? void 0 : rejectRateLimitedControlPlaneWrite();
		if (postAdmissionRateLimitError) return await options.reject(postAdmissionRateLimitError);
		try {
			const pluginRegistry = options.methodRegistry.pluginRegistry ?? getPluginRuntimeGatewayRequestScope()?.pluginRegistry ?? getActivePluginRegistry() ?? void 0;
			return await withPluginRuntimeGatewayRequestScope({
				context: options.context,
				resolveGatewayContext: options.context.resolveGatewayContext,
				client,
				isWebchatConnect: options.isWebchatConnect,
				...client?.internal?.nodeInvokeStream ? { invokeWithSessionNodeAuthority: getPluginRuntimeGatewayRequestScope()?.invokeWithSessionNodeAuthority } : {},
				...pluginRegistry ? { pluginRegistry } : {}
			}, fn);
		} catch (error) {
			if (error instanceof SessionMutationAuthorizationChangedError) return await options.reject(error.error);
			const staleInstall = classifyGatewayStaleInstall(error);
			if (staleInstall) return await options.reject(staleInstall.error);
			throw error;
		}
	}
	if (!rootWorkAdmission) return await invokeWithRequestScope();
	try {
		return await rootWorkAdmission.run(invokeWithRequestScope);
	} finally {
		rootWorkAdmission.release();
	}
}
/** Authorizes and dispatches one gateway JSON-RPC-style request. */
async function handleGatewayRequest(opts) {
	const { req, respond, client, isWebchatConnect, context, signal } = opts;
	const methodRegistry = opts.methodRegistry?.getHandler(req.method) !== void 0 ? opts.methodRegistry : createRequestGatewayMethodRegistry(opts.extraHandlers);
	const authorization = await authorizeGatewayRequestPreDispatch({
		method: req.method,
		requestParams: req.params,
		client,
		context,
		methodRegistry
	});
	if (authorization.error) {
		respond(false, void 0, authorization.error);
		return;
	}
	const handler = methodRegistry.getHandler(req.method);
	if (!handler) {
		respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unknown method: ${req.method}`));
		return;
	}
	const invokeHandler = () => handler({
		req,
		params: req.params ?? {},
		client,
		isWebchatConnect,
		respond,
		context,
		...signal ? { signal } : {},
		...opts.sessionMutationCommitGuard ? { sessionMutationCommitGuard: opts.sessionMutationCommitGuard } : {},
		...authorization.sessionMutationAuthorization ? { sessionMutationAuthorization: authorization.sessionMutationAuthorization } : {}
	});
	await runWithGatewayRequestEnvelope(req.method, client, invokeHandler, {
		context,
		isWebchatConnect,
		methodRegistry,
		requestParams: req.params,
		reject: (error) => respond(false, void 0, error)
	});
}
//#endregion
export { runWithGatewayRequestEnvelope as a, handleGatewayRequest as i, coreGatewayHandlers as n, createRequestGatewayMethodRegistry as r, authorizeGatewayRequestPreDispatch as t };
