From 155288c969e063569082bd76010fb319f806891a Mon Sep 17 00:00:00 2001 From: Evgeny Shurakov Date: Mon, 11 May 2026 21:23:11 +0200 Subject: [PATCH] fix(cloud-agent): fallback to preparedAt in cleanupIdleKiloServer when no executions exist --- .../src/persistence/CloudAgentSession.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/services/cloud-agent-next/src/persistence/CloudAgentSession.ts b/services/cloud-agent-next/src/persistence/CloudAgentSession.ts index ed5f60e16b..e3fe98910d 100644 --- a/services/cloud-agent-next/src/persistence/CloudAgentSession.ts +++ b/services/cloud-agent-next/src/persistence/CloudAgentSession.ts @@ -1702,14 +1702,22 @@ export class CloudAgentSession extends DurableObject { return; } + const metadata = await this.getMetadata(); + if (!metadata) { + return; + } + const executions = await this.executionQueries.getAll(); const latestExecution = executions[executions.length - 1]; - if (!latestExecution) { + + const lastActivity = latestExecution + ? (latestExecution.lastHeartbeat ?? latestExecution.completedAt ?? latestExecution.startedAt) + : (metadata.preparedAt ?? metadata.timestamp); + + if (!lastActivity) { return; } - const lastActivity = - latestExecution.lastHeartbeat ?? latestExecution.completedAt ?? latestExecution.startedAt; const idleMs = now - lastActivity; const idleTimeoutMs = this.getKiloServerIdleTimeoutMs(); @@ -1717,11 +1725,6 @@ export class CloudAgentSession extends DurableObject { return; } - const metadata = await this.getMetadata(); - if (!metadata) { - return; - } - logger .withFields({ sessionId: this.sessionId,