Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(agent-tracing): align DB trace_s3_key with .json.zst suffix (#14860)

Arvin Xuff259bdc5172
diff --git a/src/server/modules/AgentTracing/S3SnapshotStore.ts b/src/server/modules/AgentTracing/S3SnapshotStore.ts
index 46d1291a654..60bc4f63e24 100644
--- a/src/server/modules/AgentTracing/S3SnapshotStore.ts
+++ b/src/server/modules/AgentTracing/S3SnapshotStore.ts
@@ -16,6 +16,19 @@ const SNAPSHOT_SUFFIX = '.json.zst';
const LEGACY_SUFFIX = '.json';
const ZSTD_CONTENT_TYPE = 'application/zstd';
+/**
+ * Canonical S3 key for a finalized operation snapshot. Single source of truth
+ * for the layout β€” both this store's `save()` and the DB-persistence path
+ * (CompletionLifecycle) build keys through this helper so the value written to
+ * `agent_operations.trace_s3_key` stays aligned with the object actually put
+ * in S3.
+ */
+export const buildFinalSnapshotKey = (
+ agentId: string,
+ topicId: string,
+ operationId: string,
+): string => `${TRACE_PREFIX}/${agentId}/${topicId}/${operationId}${SNAPSHOT_SUFFIX}`;
+
/**
* S3-backed snapshot store for production agent trace persistence.
*
@@ -61,7 +74,7 @@ export class S3SnapshotStore implements ISnapshotStore {
async save(snapshot: ExecutionSnapshot): Promise<void> {
const agentId = snapshot.agentId ?? 'unknown';
const topicId = snapshot.topicId ?? 'unknown';
- const key = `${TRACE_PREFIX}/${agentId}/${topicId}/${snapshot.operationId}${SNAPSHOT_SUFFIX}`;
+ const key = buildFinalSnapshotKey(agentId, topicId, snapshot.operationId);
log('Saving snapshot to S3: %s', key);
const compressed = await this.encodeSnapshot(snapshot);
diff --git a/src/server/modules/AgentTracing/index.ts b/src/server/modules/AgentTracing/index.ts
index f09f7978312..d4ee83a9f05 100644
--- a/src/server/modules/AgentTracing/index.ts
+++ b/src/server/modules/AgentTracing/index.ts
@@ -1 +1 @@
-export { S3SnapshotStore } from './S3SnapshotStore';
+export { buildFinalSnapshotKey, S3SnapshotStore } from './S3SnapshotStore';
diff --git a/src/server/services/agentRuntime/AgentRuntimeService.ts b/src/server/services/agentRuntime/AgentRuntimeService.ts
index 1c2ebfa769d..888741d2c75 100644
--- a/src/server/services/agentRuntime/AgentRuntimeService.ts
+++ b/src/server/services/agentRuntime/AgentRuntimeService.ts
@@ -951,8 +951,8 @@ export class AgentRuntimeService {
// failed op is observable in the same place as a successful run.
// Without this, propagated errors (e.g. markPersistFatal from
// RuntimeExecutors) leave the partial as an orphan at
- // `_partial/<op>.json` and the canonical
- // `agent-traces/<agentId>/<topicId>/<op>.json` returns 404 β€” see
+ // `_partial/<op>.json.zst` and the canonical
+ // `agent-traces/<agentId>/<topicId>/<op>.json.zst` returns 404 β€” see
// LOBE-8533.
//
// `failedStep` synthesizes a step record for the failure because the
diff --git a/src/server/services/agentRuntime/CompletionLifecycle.ts b/src/server/services/agentRuntime/CompletionLifecycle.ts
index 954a65464a4..78e69a04d72 100644
--- a/src/server/services/agentRuntime/CompletionLifecycle.ts
+++ b/src/server/services/agentRuntime/CompletionLifecycle.ts
@@ -6,6 +6,7 @@ import {
} from '@/database/models/agentOperation';
import { MessageModel } from '@/database/models/message';
import { type LobeChatDatabase } from '@/database/type';
+import { buildFinalSnapshotKey } from '@/server/modules/AgentTracing';
import { emitAgentSignalSourceEvent } from '@/server/services/agentSignal';
import { toAgentSignalTraceEvents } from '@/server/services/agentSignal/observability/traceEvents';
@@ -99,7 +100,7 @@ export class CompletionLifecycle {
const agentId = metadata?.agentId;
const topicId = metadata?.topicId;
const traceS3Key =
- agentId && topicId ? `agent-traces/${agentId}/${topicId}/${operationId}.json` : null;
+ agentId && topicId ? buildFinalSnapshotKey(agentId, topicId, operationId) : null;
const processingTimeMs = state?.createdAt
? Date.now() - new Date(state.createdAt).getTime()