Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

Move agent thread metrics into the runtime module.

debajyoti-truefoundry485e8d7c54ee
diff --git a/packages/harness/src/agentSession/TurnHandle.ts b/packages/harness/src/agentSession/TurnHandle.ts
index 7c7290522..4b63b4c31 100644
--- a/packages/harness/src/agentSession/TurnHandle.ts
+++ b/packages/harness/src/agentSession/TurnHandle.ts
@@ -4,7 +4,6 @@
import type { MCPAuthRequiredEvent, ModelMessageDeltaEvent, ThreadDoneEvent } from '../core/events/schema';
import { EventType as HarnessEventType, newEventId } from '../core/events/schema';
import { getEmptyUsage } from '../core/llm/LLMTypes';
-import { getEmptyMetric } from '../core/llm/metrics';
import {
InternalEventType,
type AgentThreadExecutionEvent,
@@ -13,6 +12,7 @@ import {
type InternalThreadDoneEvent,
} from '../core/runtime/AgentThread.types';
import type { AgentThreadOrchestrator } from '../core/runtime/AgentThreadOrchestrator';
+import { createEmptyAgentThreadMetrics } from '../core/runtime/metrics';
import type { ITurnResourceResolver } from './ITurnResourceResolver';
import type { TurnRecord } from './models/TurnRecord';
import { EventType, type PersistedTurnEvent, type TurnCreatedEvent, type TurnDoneEvent } from './schemas/events';
@@ -211,7 +211,7 @@ export class TurnHandle<TTurnCustom extends object = Record<string, never>> {
const emptyResult: AgentThreadExecutionResult = {
output: null,
required_actions: [],
- metrics: { total: getEmptyMetric() },
+ metrics: { total: createEmptyAgentThreadMetrics() },
};
await generator.return(emptyResult);
}
diff --git a/packages/harness/src/core/index.ts b/packages/harness/src/core/index.ts
index 13e377779..34e32dcf5 100644
--- a/packages/harness/src/core/index.ts
+++ b/packages/harness/src/core/index.ts
@@ -76,9 +76,9 @@ export {
ToolResponseRequiredEventSchema,
newEventId,
} from './events/schema';
-export type { AgentThreadMetric } from './llm/metrics';
export { InternalEventType } from './runtime/AgentThread.types';
export type { AgentThreadSendBatch, ContextMessage } from './runtime/AgentThread.types';
+export type { AgentThreadMetrics } from './runtime/metrics';
export type { SandboxInfo } from './sandbox/Sandbox';
// Tracing
diff --git a/packages/harness/src/core/runtime/AgentThread.ts b/packages/harness/src/core/runtime/AgentThread.ts
index 0e17cd585..f8eda6e05 100644
--- a/packages/harness/src/core/runtime/AgentThread.ts
+++ b/packages/harness/src/core/runtime/AgentThread.ts
@@ -50,7 +50,6 @@ import {
type LLMUserMessage,
type RawAssistantMessage,
} from '../llm/LLMTypes';
-import type { AgentThreadMetric } from '../llm/metrics';
import { toOpenAIResponseFormat } from '../llm/responseFormat';
import { toOpenAIChatMessage } from '../llm/toOpenAIChatMessage';
import { estimateTokensForString, mergeUsage } from '../llm/usage';
@@ -91,6 +90,7 @@ import {
toToolCallInfo,
} from './contextUtils';
import { DeferredTool } from './DeferredTool';
+import type { AgentThreadMetrics } from './metrics';
import { getClosableOpenToolCallIds, OpenToolCallCloser } from './OpenToolCallCloser';
import { isEmptyMessageContent, processAgentUserInput, type AgentInputUserMessage } from './UserInputMessage';
@@ -751,7 +751,7 @@ export class AgentThread {
return base;
}
- public getAgentThreadMetrics(): AgentThreadMetric {
+ public getAgentThreadMetrics(): AgentThreadMetrics {
return {
iterations: this._iterations,
total_tool_calls: this._totalToolCalls,
diff --git a/packages/harness/src/core/runtime/AgentThread.types.ts b/packages/harness/src/core/runtime/AgentThread.types.ts
index 39a42071a..5980af58c 100644
--- a/packages/harness/src/core/runtime/AgentThread.types.ts
+++ b/packages/harness/src/core/runtime/AgentThread.types.ts
@@ -31,10 +31,10 @@ import type {
LLMToolMessage,
LLMUserMessage,
} from '../llm/LLMTypes';
-import type { AgentThreadMetrics } from '../llm/metrics';
import type { Sandbox } from '../sandbox/Sandbox';
import type { AgentTracing } from '../tracing/AgentTracing';
import type { AgentDefinition } from './AgentDefinition';
+import type { AgentThreadMetrics } from './metrics';
export type { AgentInfo, AgentParent };
@@ -141,7 +141,7 @@ export interface AgentThreadExecutionResult {
output: ModelMessageEvent | null;
required_actions: ActionRequiredEvent[];
root_agent_error?: Pick<ThreadStateError, 'error' | 'output'> | undefined;
- metrics: AgentThreadMetrics;
+ metrics: { total: AgentThreadMetrics };
}
/** Public send items plus internal LLM tool messages (child→parent delivery). */
diff --git a/packages/harness/src/core/runtime/AgentThreadOrchestrator.ts b/packages/harness/src/core/runtime/AgentThreadOrchestrator.ts
index 6ecd84230..4df2ca105 100644
--- a/packages/harness/src/core/runtime/AgentThreadOrchestrator.ts
+++ b/packages/harness/src/core/runtime/AgentThreadOrchestrator.ts
@@ -9,7 +9,6 @@ import {
type UserToolApprovalMessage,
type UserToolResponseMessage,
} from '../events/schema';
-import { addMetrics, getEmptyMetric } from '../llm/metrics';
import type { AgentExecutionTrace, AgentTracing } from '../tracing/AgentTracing';
import { onSignalAbort } from '../util/abort';
import { mergeAsyncGenerators } from '../util/promiseUtils';
@@ -33,6 +32,7 @@ import {
isInternalThreadDoneError,
} from './contextUtils';
import type { CreateDynamicSubAgentThread } from './CreateDynamicSubAgentThread';
+import { addAgentThreadMetrics, createEmptyAgentThreadMetrics } from './metrics';
const MAX_PARALLEL_SUB_AGENTS = 5;
@@ -400,7 +400,7 @@ export class AgentThreadOrchestrator {
const requiredActions: ActionRequiredEvent[] = [];
let rootAgentError: AgentThreadExecutionResult['root_agent_error'];
const pendingAuthEvents: InternalMCPAuthRequiredEvent[] = [];
- const totalMetrics = getEmptyMetric();
+ const totalMetrics = createEmptyAgentThreadMetrics();
const mainThread = [...agentThreads.values()].find(e => !e.parent);
if (!mainThread) {
throw new Error('Unreachable: no root thread found');
@@ -436,7 +436,7 @@ export class AgentThreadOrchestrator {
if (chunk.parent) {
const completedThread = agentThreads.get(chunk.thread_id);
if (completedThread) {
- addMetrics(totalMetrics, completedThread.getAgentThreadMetrics());
+ addAgentThreadMetrics(totalMetrics, completedThread.getAgentThreadMetrics());
}
} else {
// Root-thread only — sub-agent AGENT_DONE would overwrite root trace output /
@@ -469,7 +469,7 @@ export class AgentThreadOrchestrator {
caughtError = error;
throw error;
} finally {
- addMetrics(totalMetrics, mainThread.getAgentThreadMetrics());
+ addAgentThreadMetrics(totalMetrics, mainThread.getAgentThreadMetrics());
rootSpan.finalize(mainThread, caughtError);
rootSpan.end();
}
diff --git a/packages/harness/src/core/llm/metrics.ts b/packages/harness/src/core/runtime/metrics.ts
similarity index 63%
rename from packages/harness/src/core/llm/metrics.ts
rename to packages/harness/src/core/runtime/metrics.ts
index 397830f48..08c509398 100644
--- a/packages/harness/src/core/llm/metrics.ts
+++ b/packages/harness/src/core/runtime/metrics.ts
@@ -1,8 +1,8 @@
-import type { CompletionUsage } from './LLMTypes';
-import { getEmptyUsage } from './LLMTypes';
-import { mergeUsage } from './usage';
+import type { CompletionUsage } from '../llm/LLMTypes';
+import { getEmptyUsage } from '../llm/LLMTypes';
+import { mergeUsage } from '../llm/usage';
-export interface AgentThreadMetric {
+export interface AgentThreadMetrics {
iterations: number;
total_tool_calls: number;
total_summarizations: number;
@@ -10,11 +10,7 @@ export interface AgentThreadMetric {
total_sub_agents: number;
}
-export interface AgentThreadMetrics {
- total: AgentThreadMetric;
-}
-
-export function getEmptyMetric(): AgentThreadMetric {
+export function createEmptyAgentThreadMetrics(): AgentThreadMetrics {
return {
iterations: 0,
total_tool_calls: 0,
@@ -24,7 +20,7 @@ export function getEmptyMetric(): AgentThreadMetric {
};
}
-export function addMetrics(target: AgentThreadMetric, source: AgentThreadMetric): void {
+export function addAgentThreadMetrics(target: AgentThreadMetrics, source: AgentThreadMetrics): void {
target.iterations += source.iterations;
target.total_tool_calls += source.total_tool_calls;
target.total_summarizations += source.total_summarizations;
diff --git a/packages/harness/src/core/tracing/AgentTracing.ts b/packages/harness/src/core/tracing/AgentTracing.ts
index 3beffc529..cf7ff00b3 100644
--- a/packages/harness/src/core/tracing/AgentTracing.ts
+++ b/packages/harness/src/core/tracing/AgentTracing.ts
@@ -1,10 +1,10 @@
-import type { AgentThreadMetric } from '../llm/metrics';
+import type { AgentThreadMetrics } from '../runtime/metrics';
export interface AgentExecutionTrace {
runInContext<T>(operation: () => T): T;
startSubAgent(name: string, input: string): AgentExecutionTrace;
setOutput(output: string): void;
- setMetrics(metrics: AgentThreadMetric): void;
+ setMetrics(metrics: AgentThreadMetrics): void;
setError(error: unknown): void;
setSuccess(): void;
end(): void;