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.

🔎 chore(runtime): add runtime logs (#18361)

Rylan Cai3cc8dbe8c7fe
diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
index d2b98dbf45c..0fe67a8ac0c 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
@@ -70,6 +70,7 @@ import {
isSuccessLikeCompletionReason,
normalizeCompletionMessages,
} from './CompletionLifecycle';
+import { logToolCallPc } from './formalObservation';
import { hookDispatcher } from './hooks';
import { HumanInterventionHandler } from './HumanInterventionHandler';
import { OperationTraceRecorder } from './OperationTraceRecorder';
@@ -1113,6 +1114,10 @@ export class AgentRuntimeService {
// Execute step (skipped when force-finishing a parked supervisor op).
const startAt = Date.now();
+ logToolCallPc(operationId, stepIndex, 'post.runtime_step_entered', () => ({
+ forcedFinish: Boolean(forcedFinishState),
+ stateStatus: currentState.status,
+ }));
const stepResult = forcedFinishState
? { events: [], newState: forcedFinishState, nextContext: undefined }
: await runtime.step(currentState, currentContext);
@@ -1125,10 +1130,19 @@ export class AgentRuntimeService {
if (stepResult.newState.error) {
stepResult.newState.error = formatErrorForState(stepResult.newState.error);
}
+ logToolCallPc(operationId, stepIndex, 'post.runtime_step_returned', () => ({
+ errorCategory: stepResult.newState.error?.category ?? null,
+ errorRetryable: stepResult.newState.error?.retryable ?? null,
+ nextContextPresent: stepResult.nextContext !== undefined,
+ stateStatus: stepResult.newState.status,
+ }));
// Check if the operation was interrupted while the step was executing
// (e.g., user clicked abort during a long LLM call)
const latestState = await this.coordinator.loadAgentState(operationId);
+ logToolCallPc(operationId, stepIndex, 'post.latest_state_loaded', () => ({
+ interrupted: latestState?.status === 'interrupted',
+ }));
if (latestState?.status === 'interrupted') {
stepResult.newState.status = 'interrupted';
stepResult.newState.lastModified = new Date().toISOString();
@@ -1161,6 +1175,7 @@ export class AgentRuntimeService {
// per-(op, file) versions at pre-approval content.
if (isSuccessLikeCompletionReason(preSaveReason)) {
await this.completionLifecycle.registerFileWorks(operationId, stepResult.newState);
+ logToolCallPc(operationId, stepIndex, 'post.file_works_registered', () => ({}));
}
}
@@ -1170,6 +1185,10 @@ export class AgentRuntimeService {
executionTime: Date.now() - startAt,
stepIndex, // placeholder
});
+ logToolCallPc(operationId, stepIndex, 'post.step_result_saved', () => ({
+ stateStatus: stepResult.newState.status,
+ stateStepCount: stepResult.newState.stepCount,
+ }));
let nextStepScheduled = false;
@@ -1183,6 +1202,7 @@ export class AgentRuntimeService {
stepIndex,
type: 'step_complete',
});
+ logToolCallPc(operationId, stepIndex, 'post.step_complete_published', () => ({}));
await this.publishSubAgentProgress(stepResult.newState, stepIndex);
@@ -1297,6 +1317,13 @@ export class AgentRuntimeService {
const hasAfterStepHooks = stepResult.newState.metadata?._hooks?.some(
(h: { type: string }) => h.type === 'afterStep',
);
+ logToolCallPc(operationId, stepIndex, 'post.trace_appended', () => ({}));
+ logToolCallPc(operationId, stepIndex, 'post.route_selected', () => ({
+ hasAfterStepHooks,
+ nextContextPresent: stepResult.nextContext !== undefined,
+ queueAvailable: Boolean(this.queueService),
+ shouldContinue,
+ }));
if (hasAfterStepHooks && stepResult.newState.metadata) {
const prevTracking = stepResult.newState.metadata._stepTracking || {};
const newTotalToolCalls =
@@ -1318,6 +1345,7 @@ export class AgentRuntimeService {
// Persist tracking state for next step
stepResult.newState.metadata._stepTracking = updatedTracking;
await this.coordinator.saveAgentState(operationId, stepResult.newState);
+ logToolCallPc(operationId, stepIndex, 'post.step_tracking_saved', () => ({}));
}
if (shouldContinue && stepResult.nextContext && this.queueService) {
@@ -1342,6 +1370,9 @@ export class AgentRuntimeService {
stepIndex: nextStepIndex,
});
nextStepScheduled = true;
+ logToolCallPc(operationId, stepIndex, 'post.next_step_scheduled', () => ({
+ nextStepIndex,
+ }));
log('[%s][%d] Scheduled next step %d', operationId, stepIndex, nextStepIndex);
}
@@ -1369,9 +1400,11 @@ export class AgentRuntimeService {
stepResult.newState,
reason,
);
+ logToolCallPc(operationId, stepIndex, 'post.completion_signals', () => ({ reason }));
// Dispatch completion hooks
await this.completionLifecycle.dispatchHooks(operationId, stepResult.newState, reason);
+ logToolCallPc(operationId, stepIndex, 'post.completion_hooks', () => ({ reason }));
// Park-time self-check: sub-agents are dispatched mid-step, so a
// fast child can complete BEFORE this op's parked state/row were
@@ -1418,6 +1451,7 @@ export class AgentRuntimeService {
: undefined,
state: stepResult.newState,
});
+ logToolCallPc(operationId, stepIndex, 'post.trace_finalized', () => ({ reason }));
}
return {
@@ -1493,9 +1527,11 @@ export class AgentRuntimeService {
}
await this.completionLifecycle.emitSignalEvents(operationId, finalStateWithError, 'error');
+ logToolCallPc(operationId, stepIndex, 'post.completion_signals', () => ({ reason: 'error' }));
// Dispatch onComplete + onError hooks
await this.completionLifecycle.dispatchHooks(operationId, finalStateWithError, 'error');
+ logToolCallPc(operationId, stepIndex, 'post.completion_hooks', () => ({ reason: 'error' }));
// Finalize the partial snapshot into the canonical S3 path so the
// failed op is observable in the same place as a successful run.
@@ -1531,6 +1567,7 @@ export class AgentRuntimeService {
},
state: finalStateWithError,
});
+ logToolCallPc(operationId, stepIndex, 'post.trace_finalized', () => ({ reason: 'error' }));
throw error;
} finally {
diff --git a/apps/server/src/services/agentRuntime/formalObservation.ts b/apps/server/src/services/agentRuntime/formalObservation.ts
new file mode 100644
index 00000000000..b30ef4dcf44
--- /dev/null
+++ b/apps/server/src/services/agentRuntime/formalObservation.ts
@@ -0,0 +1,26 @@
+import debug from 'debug';
+
+const debugLog = debug('lobe-server:agent-runtime:tool-call-stability');
+
+export const logToolCallPc = (
+ operationId: string,
+ stepIndex: number,
+ pc: string,
+ getObs: () => Record<string, unknown>,
+) => {
+ if (!debugLog.enabled) return;
+
+ const formatObservation = (obs: Record<string, unknown>): string => {
+ try {
+ return `op=${operationId} step=${stepIndex} pc=${pc} obs=${JSON.stringify(obs)}`;
+ } catch {
+ return `op=${operationId} step=${stepIndex} pc=${pc}`;
+ }
+ };
+
+ try {
+ debugLog('%s', formatObservation(getObs()));
+ } catch (error) {
+ debugLog('%s', formatObservation({ error: String(error) }));
+ }
+};