Keep empty orchestration reads lightweight without hiding inserts

This commit is contained in:
Brennan Benson
2026-11-09 16:14:26 -08:00
parent dee68b6d71
commit ff553c2baa
3 changed files with 22 additions and 4 deletions
@@ -41,11 +41,15 @@ export function readCapturedOrchestrationFleetAgentStatusSnapshot(
): readonly FleetAgentStatusEvidence[] {
const projection = new RuntimeAgentOrchestrationProjection(dependencies)
const raw = dependencies.getDb()
const db = raw?.db ? orchestrationProjectionFacts(raw).view : raw
const sourceFacts = raw?.db ? orchestrationProjectionFacts(raw) : undefined
const db = sourceFacts?.view ?? raw
let scope: ReturnType<typeof createOrchestrationProjectionScope> | undefined
return readOrchestrationFleetAgentStatusSnapshot({
...source,
getAgentStatusOrchestrationContextForPaneKey: (paneKey) => {
if (raw?.db && !sourceFacts) {
return undefined
}
const handle = source.getAgentStatusTerminalHandleForPaneKey?.(paneKey)
if (!handle) {
return undefined
@@ -128,7 +128,7 @@ export class OrchestrationProjectionFacts {
}
this.candidates.refresh(now, dirty, this.cold)
this.cold = false
if (!this.transient) {
if (!this.transient && dirty.length > 0) {
clearProjectionDirtyKeys(this.db)
}
}
@@ -252,13 +252,17 @@ export class OrchestrationProjectionFacts {
export function orchestrationProjectionFacts(
db: OrchestrationDb,
now = Date.now()
): OrchestrationProjectionFacts {
): OrchestrationProjectionFacts | undefined {
const view = views.get(db)
if (view) {
return view
}
let source = db.db.isTransaction ? undefined : sources.get(db)
if (!source) {
// An uncached probe also sees raw/local and external inserts after an empty read.
if (!db.db.prepare('SELECT 1 FROM dispatch_contexts LIMIT 1').get()) {
return undefined
}
source = new OrchestrationProjectionFacts(db, db.db.isTransaction)
if (!db.db.isTransaction) {
sources.set(db, source)
@@ -58,6 +58,9 @@ export class RuntimeAgentOrchestrationProjection {
buildByPaneKey(): Record<string, AgentStatusOrchestrationContext> | undefined {
const raw = this.deps.getDb()
const source = raw?.db ? orchestrationProjectionFacts(raw) : undefined
if (raw?.db && !source) {
return undefined
}
const db = source?.view ?? raw
if (!db || (source ? source.rows().next().done : db.hasAnyDispatchContexts?.() === false)) {
return undefined
@@ -162,7 +165,11 @@ export class RuntimeAgentOrchestrationProjection {
if (!raw) {
return undefined
}
const db = raw.db ? orchestrationProjectionFacts(raw).view : raw
const source = raw.db ? orchestrationProjectionFacts(raw) : undefined
if (raw.db && !source) {
return undefined
}
const db = source?.view ?? raw
const scope = createOrchestrationProjectionScope(db, this.deps)
const statuses =
this.deps.getAgentStatusForPaneKey?.(paneKey, (key) =>
@@ -233,6 +240,9 @@ export class RuntimeAgentOrchestrationProjection {
scope = createOrchestrationProjectionScope(db, this.deps)
): AgentStatusOrchestrationContext | undefined {
const facts = db?.db ? orchestrationProjectionFacts(db, scope.now) : undefined
if (db?.db && !facts) {
return undefined
}
if (facts && facts.view !== db) {
db = facts.view
scope = createOrchestrationProjectionScope(db, this.deps, scope.now)