perf(orchestration): ask the pull-loop question in one statement without a temporary set (STA-15102) (#42122)

The per-server check from #41834 used `state IN ('open', 'cancel_owed')`, which makes SQLite
build a small temporary set on every call, and asked about home conversations in a separate
statement. A server with one live link and no home conversation paid more per pull than the
old global scan did.

It is now one statement: a home-conversation seek, then one equality seek per live link state
on the same covering index, stopping at the first row. Closed-link history still never enters
the check. The answer, the read-error fallback and global discovery are unchanged.
This commit is contained in:
Jinjing
2026-11-13 13:20:18 -08:00
committed by GitHub
parent 54590ba35e
commit 3d13314d86
2 changed files with 13 additions and 28 deletions
@@ -75,17 +75,6 @@ export function listHomePeerMailEnvironments(this: OrchestrationDb): string[] {
return rows.map((row) => row.environment_id)
}
/** One server's check, for its pull loop: a seek on that server's home rows, not every server's. */
export function hasHomePeerMailEnvironment(this: OrchestrationDb, environmentId: string): boolean {
return (
this.db
.prepare(
`SELECT 1 FROM peer_mail_conversations WHERE role = 'home' AND environment_id = ? LIMIT 1`
)
.get(environmentId) !== undefined
)
}
/** Conversations a paired Orca opened here that hold replies it has not acknowledged yet. */
export function listPeerConversationIdsWithPendingReplies(
this: OrchestrationDb,
@@ -189,7 +178,6 @@ export type PeerMailStoreMethods = {
openHomePeerMailConversation: typeof openHomePeerMailConversation
getPeerMailConversation: typeof getPeerMailConversation
listHomePeerMailEnvironments: typeof listHomePeerMailEnvironments
hasHomePeerMailEnvironment: typeof hasHomePeerMailEnvironment
listPeerConversationIdsWithPendingReplies: typeof listPeerConversationIdsWithPendingReplies
getPeerMailMessage: typeof getPeerMailMessage
isPeerMailSenderLabel: typeof isPeerMailSenderLabel
@@ -201,7 +189,6 @@ export function attachPeerMailStore(ctor: { prototype: object }): void {
openHomePeerMailConversation,
getPeerMailConversation,
listHomePeerMailEnvironments,
hasHomePeerMailEnvironment,
listPeerConversationIdsWithPendingReplies,
getPeerMailMessage,
isPeerMailSenderLabel,
@@ -6,24 +6,22 @@ export function peerMailRelayEnvironments(db: OrchestrationDb): Set<string> {
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. Runs
* every pull, so it asks about this one server only: closed links' history never enters it. */
// Every pull asks about one server only, so closed links' history never enters it: an index seek
// for a home conversation, then one per live link state, stopping at the first row. One equality
// per state, because an IN list builds a temporary set on every call.
const RELAY_WANTS_SQL = `
SELECT 1 FROM peer_mail_conversations WHERE role = 'home' AND environment_id = ?1
UNION ALL
SELECT 1 FROM remote_run_links WHERE environment_id = ?1 AND state = 'open'
UNION ALL
SELECT 1 FROM remote_run_links WHERE environment_id = ?1 AND state = 'cancel_owed'
LIMIT 1`
/** 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 db.hasHomePeerMailEnvironment(environmentId) || hasLiveRemoteRunLink(db, environmentId)
return db.db.prepare(RELAY_WANTS_SQL).get(environmentId) !== undefined
} catch {
return true
}
}
/** A seek on this server's own (environment_id, state) index rows, however many closed links it keeps. */
function hasLiveRemoteRunLink(db: OrchestrationDb, environmentId: string): boolean {
return (
db.db
.prepare(
`SELECT 1 FROM remote_run_links
WHERE environment_id = ? AND state IN ('open', 'cancel_owed') LIMIT 1`
)
.get(environmentId) !== undefined
)
}