diff --git a/src/main/runtime/orchestration/addressing/orchestration-fleet-agent-status-snapshot.ts b/src/main/runtime/orchestration/addressing/orchestration-fleet-agent-status-snapshot.ts index 91ebf189bf6..8b04deb002d 100644 --- a/src/main/runtime/orchestration/addressing/orchestration-fleet-agent-status-snapshot.ts +++ b/src/main/runtime/orchestration/addressing/orchestration-fleet-agent-status-snapshot.ts @@ -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 | undefined return readOrchestrationFleetAgentStatusSnapshot({ ...source, getAgentStatusOrchestrationContextForPaneKey: (paneKey) => { + if (raw?.db && !sourceFacts) { + return undefined + } const handle = source.getAgentStatusTerminalHandleForPaneKey?.(paneKey) if (!handle) { return undefined diff --git a/src/main/runtime/orchestration/crew/orchestration-projection-facts.ts b/src/main/runtime/orchestration/crew/orchestration-projection-facts.ts index 65b5c04bc83..bec5b7b73ee 100644 --- a/src/main/runtime/orchestration/crew/orchestration-projection-facts.ts +++ b/src/main/runtime/orchestration/crew/orchestration-projection-facts.ts @@ -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) diff --git a/src/main/runtime/orchestration/crew/runtime-agent-orchestration-projection.ts b/src/main/runtime/orchestration/crew/runtime-agent-orchestration-projection.ts index 5627055bf5e..d95968e8a85 100644 --- a/src/main/runtime/orchestration/crew/runtime-agent-orchestration-projection.ts +++ b/src/main/runtime/orchestration/crew/runtime-agent-orchestration-projection.ts @@ -58,6 +58,9 @@ export class RuntimeAgentOrchestrationProjection { buildByPaneKey(): Record | 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)