diff --git a/server/src/services/chat-channels.ts b/server/src/services/chat-channels.ts index 18f7263c073..29f64ab0c5b 100644 --- a/server/src/services/chat-channels.ts +++ b/server/src/services/chat-channels.ts @@ -3540,7 +3540,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { await invalidateRuntime(endpointId); return "settled"; }, - reconcileProviderResourceRows, reconcileDiscordCommands, runtimeFor, runtimeContextForRecord, + reconcileProviderResourceRows, reconcileDiscordCommands, runtimeFor: photonExternalRuntimeOwner.configurationRuntimeFor, runtimeContextForRecord, stageTelegramMaintenance, processTelegramMaintenance, recordTelegramStopSubscription, fetchImpl, redactError, runtimeGeneration, leasedChatProvider, PROVIDER_LABELS, SUPPLIED_CREDENTIAL_KEYS, RECONNECT_CREDENTIAL_KEYS, PROVIDER_CREDENTIAL_CHECK_TIMEOUT_MS, isSupportedTelegramWebhookBaseUrl, diff --git a/server/src/services/chat-channels/imessage-photon/external-runtime.ts b/server/src/services/chat-channels/imessage-photon/external-runtime.ts index f6122737a10..8588cfbbd18 100644 --- a/server/src/services/chat-channels/imessage-photon/external-runtime.ts +++ b/server/src/services/chat-channels/imessage-photon/external-runtime.ts @@ -68,7 +68,7 @@ export function createPhotonExternalRuntimeOwner(db: Db, options: { broker(): PhotonExternalBroker | undefined; withReceiverLease(record: EndpointRecord, work: (lease: { token: string }) => Promise): Promise; endpointRecord(id: string): Promise; - runtimeFor(endpoint: EndpointRecord["endpoint"], input: { requireDiscordOwnership?: boolean }): Promise; + runtimeFor(endpoint: EndpointRecord["endpoint"], input: { requireDiscordOwnership?: boolean; waitForDiscordOwnership?: boolean }): Promise; context(runtime: ChatEndpointRuntime): RuntimeContext | undefined; ownership(id: string): DiscordGatewayOwnership | undefined; release(ownership: DiscordGatewayOwnership): Promise; @@ -97,6 +97,35 @@ export function createPhotonExternalRuntimeOwner(db: Db, options: { if (record.endpoint.provider !== "imessage-photon") return null; return selected(record); } + /** Configuration needs a ready sender, not an idle renewable receiver lease. + * Recover unfinished custody through the existing finite lease owner before + * asking the normal Runtime registry to construct an external-only sender. */ + async function configurationRuntimeFor(endpoint: EndpointRecord["endpoint"], + input: { requireDiscordOwnership?: boolean; waitForDiscordOwnership?: boolean } = {}): Promise { + if (endpoint.provider !== "imessage-photon") return options.runtimeFor(endpoint, input); + const record = await options.endpointRecord(endpoint.id); + if (!record) return options.runtimeFor(endpoint, input); + const prior = await store.read({ companyId: record.endpoint.companyId, endpointId: endpoint.id }); + const connector = options.broker()?.instanceIdentity(); + let recoveredOriginalHandoff = false; + if (prior && (prior.phase === "enrolling" || prior.phase === "joining") + && prior.identity.subject === (record.endpoint.setup as InternalSetupState).photonExternalSubject + && connector && prior.binding.instanceId === connector.instanceId && prior.binding.environment === connector.environment) { + if (!isDeepStrictEqual(await store.inspect(prior.identity), prior)) + throw conflict("Photon original handoff changed during configuration; retry with current authority"); + // Normal configuration has already joined and invalidated its old Runtime. + // reconcile retains all current lease/binding and physical-debt fences. + await reconcile(endpoint.id); + recoveredOriginalHandoff = true; + } + const current = await options.endpointRecord(endpoint.id); + if (current && await placement(current)) { + return options.runtimeFor(current.endpoint, { ...input, requireDiscordOwnership: false, waitForDiscordOwnership: false }); + } + if (recoveredOriginalHandoff) + throw conflict("Photon original handoff has no current external placement; retry with current authority"); + return options.runtimeFor(endpoint, input); + } async function reserveLocalRuntime(record: EndpointRecord, context: RuntimeContext, lease: { token: string }, config: ResolvedPhotonChatConfig): Promise { await options.joinConstructionCleanup?.(record.endpoint.id); const subject = (record.endpoint.setup as InternalSetupState).photonExternalSubject, broker = options.broker(); @@ -513,7 +542,7 @@ export function createPhotonExternalRuntimeOwner(db: Db, options: { return { actionId, state: "scheduled" as const }; } ingress.set(db, receive); - return { reserveLocalRuntime, attachLocalRuntime, reserveSenderRuntime, attachSenderRuntime, senderRuntimeClosed, runtimeConstructionAborted, reconcile, placement, senderDebt, retire, retained, cleanupFailure, retry, close() { if (ingress.get(db) === receive) ingress.delete(db); return Promise.all([...active.values()]).then(() => undefined); } }; + return { configurationRuntimeFor, reserveLocalRuntime, attachLocalRuntime, reserveSenderRuntime, attachSenderRuntime, senderRuntimeClosed, runtimeConstructionAborted, reconcile, placement, senderDebt, retire, retained, cleanupFailure, retry, close() { if (ingress.get(db) === receive) ingress.delete(db); return Promise.all([...active.values()]).then(() => undefined); } }; } /** Called by normal configuration SQL with its actual authorized actor. Existing