diff --git a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay-environments.ts b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay-environments.ts new file mode 100644 index 00000000000..8998d0a362c --- /dev/null +++ b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay-environments.ts @@ -0,0 +1,16 @@ +import type { OrchestrationDb } from '../../db/orchestration-db' +import { listLiveRemoteRunLinkEnvironments } from '../../db/remote-owned/remote-run-links' + +/** Servers this Orca opened conversations with, or has a link to that still needs reading or a cancel. */ +export function peerMailRelayEnvironments(db: OrchestrationDb): Set { + return new Set([...db.listHomePeerMailEnvironments(), ...listLiveRemoteRunLinkEnvironments(db)]) +} + +/** Whether a server's pull loop still has a reason to run; a store read error keeps it running. */ +export function peerMailRelayWants(db: OrchestrationDb, environmentId: string): boolean { + try { + return peerMailRelayEnvironments(db).has(environmentId) + } catch { + return true + } +} diff --git a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.test.ts b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.test.ts new file mode 100644 index 00000000000..e294e963da3 --- /dev/null +++ b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.test.ts @@ -0,0 +1,53 @@ +import { afterEach, expect, it, vi } from 'vitest' +import { OrchestrationDb } from '../../db/orchestration-db' +import { closeRemoteRunLink, openRemoteRunLink } from '../../db/remote-owned/remote-run-links' +import type { OrchestrationWorkerServer } from '../environment-transport' +import { PeerMailRelay } from './peer-mail-relay' + +const SERVER: OrchestrationWorkerServer = { + environmentId: 'env_b', + name: 'devB', + peerFingerprint: 'host_b', + homePeerFingerprint: 'cred_a', + pairingRevision: 1 +} + +let relay: PeerMailRelay | null = null +afterEach(() => relay?.stop()) + +it("stops pulling from a server once this Orca's last link to it closes", async () => { + const db = new OrchestrationDb(':memory:') + const run = db.createRun({ + objective: 'remote work', + coordinatorHandle: 'term_coordinator', + coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111' + }) + const link = openRemoteRunLink(db, run.id, SERVER) + let pulls = 0 + const runtime = { + getOrchestrationDb: () => db, + resolveOrchestrationWorkerServer: () => SERVER, + notifyMessageArrived: vi.fn(), + callOrchestrationWorkerServer: async (_environmentId: string, method: string) => { + if (method === 'orchestration.peerMailPull') { + pulls += 1 + if (pulls === 2) { + closeRemoteRunLink(db, link.conversation_id, 'The Run ended.') + } + // An idle pull answers empty after its wait. + await new Promise((resolve) => setImmediate(resolve)) + } + return { items: [] } + } + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the relay reads only these runtime members. + relay = new PeerMailRelay(runtime as never, () => ({}) as never) + + relay.ensure() + await vi.waitFor(() => expect(pulls).toBeGreaterThanOrEqual(2)) + for (let i = 0; i < 20; i++) { + await new Promise((resolve) => setImmediate(resolve)) + } + + expect(pulls).toBe(2) +}) diff --git a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts index dbd37b0e756..a22ca7e02ae 100644 --- a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts +++ b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts @@ -8,6 +8,7 @@ import { federationRelayRetryDelayMs } from '../relay/federation-relay-scheduler import { HeldPeerConversations } from './held-peer-conversations' import type { OrchestrationEnvironmentTransport } from '../environment-transport' import { peerMailLocalRecipient } from './peer-mail-delivery' +import { peerMailRelayEnvironments, peerMailRelayWants } from './peer-mail-relay-environments' import { presentImportedPeerItem } from '../../mailbox/formatting/imported-peer-item-presentation' import { importRemoteOwnedItem } from '../../db/remote-owned/remote-owned-import' import { @@ -15,7 +16,6 @@ import { closeRemoteRunLinksOfRemovedServer, dueRemoteResendsNow, getRemoteRunLink, - listLiveRemoteRunLinkEnvironments, noteRemoteRunLinkError, remoteServerPin } from '../../db/remote-owned/remote-run-links' @@ -71,11 +71,7 @@ export class PeerMailRelay { return } const db = this.runtime.getOrchestrationDb() - const environments = new Set([ - ...db.listHomePeerMailEnvironments(), - ...listLiveRemoteRunLinkEnvironments(db) - ]) - for (const environmentId of environments) { + for (const environmentId of peerMailRelayEnvironments(db)) { const current = this.loops.get(environmentId) if (current && !current.stopped && current.db === db) { continue @@ -109,7 +105,8 @@ export class PeerMailRelay { let failures = 0 // Named while it is still paired, so a removal can say which server went. const serverName = remoteServerName(this.runtime, environmentId) - while (this.isCurrent(environmentId, loop)) { + // Ends with its last reason to pull; a later conversation or link starts a new loop through ensure(). + while (this.isCurrent(environmentId, loop) && peerMailRelayWants(loop.db, environmentId)) { try { await this.pullOnce(environmentId, loop) if (failures > 0) {