@@ -49,6 +49,7 @@ export function ManagedRunEvidence({ run }: { run: ManagedRun }) {
Stage ID {stage.stageId}
Provider instance {stage.providerInstanceId ?? "Unknown"}
Thread {stage.threadId ?? "Unknown"}
+ Tool calls {formatCount(stage.toolCalls ?? null)}
))}
diff --git a/apps/web/src/lib/managedRunDelivery.test.ts b/apps/web/src/lib/managedRunDelivery.test.ts
index 7a79da34582c1..c73bfd4d558c0 100644
--- a/apps/web/src/lib/managedRunDelivery.test.ts
+++ b/apps/web/src/lib/managedRunDelivery.test.ts
@@ -39,6 +39,18 @@ describe("managed-run Delivery", () => {
expect(parseManagedRun({ ...run, nextVersionField: null }, runId)).toMatchObject(run);
});
+ it("distinguishes an observed zero from missing tool usage", () => {
+ expect(parseManagedRun(run, runId)?.stages[0]?.toolCalls).toBeUndefined();
+ const withUsage = (toolCalls: number | null) => ({
+ ...run,
+ stages: [{ ...run.stages[0], toolCalls }],
+ });
+ expect(parseManagedRun(withUsage(null), runId)?.stages[0]?.toolCalls).toBeNull();
+ expect(parseManagedRun(withUsage(0), runId)?.stages[0]?.toolCalls).toBe(0);
+ expect(parseManagedRun(withUsage(3), runId)?.stages[0]?.toolCalls).toBe(3);
+ expect(parseManagedRun(withUsage(-1), runId)).toBeNull();
+ });
+
it("rejects mismatched identity and malformed evidence", () => {
expect(parseManagedRun(run, otherId)).toBeNull();
expect(parseManagedRun({ ...run, version: 2 }, runId)).toBeNull();
diff --git a/apps/web/src/lib/managedRunDelivery.ts b/apps/web/src/lib/managedRunDelivery.ts
index 16b90626d9faf..439d90e1128df 100644
--- a/apps/web/src/lib/managedRunDelivery.ts
+++ b/apps/web/src/lib/managedRunDelivery.ts
@@ -27,6 +27,7 @@ const ManagedRun = Schema.Struct({
status: Schema.String,
providerInstanceId: Schema.NullOr(Schema.String),
threadId: Schema.NullOr(Schema.String),
+ toolCalls: Schema.optionalKey(Schema.NullOr(Tokens)),
}),
),
});
From 491176d8f668775ff828cf7fda8338fe3bd47f67 Mon Sep 17 00:00:00 2001
From: InTheta <17220028+InTheta@users.noreply.github.com>
Date: Tue, 17 Nov 2026 09:14:01 +0000
Subject: [PATCH 3/3] Export exact native turn tool counts from T3
---
apps/server/src/cli/thread.test.ts | 39 +++++++++++++-
apps/server/src/cli/thread.ts | 87 ++++++++++++++++++++++++++++++
2 files changed, 125 insertions(+), 1 deletion(-)
diff --git a/apps/server/src/cli/thread.test.ts b/apps/server/src/cli/thread.test.ts
index 5252d81b2a22f..6f9864c1d77e4 100644
--- a/apps/server/src/cli/thread.test.ts
+++ b/apps/server/src/cli/thread.test.ts
@@ -1,7 +1,7 @@
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
-import { AuthSessionId, ProjectId, RunId, ThreadId } from "@t3tools/contracts";
+import { AuthSessionId, ProjectId, RunId, ThreadId, TurnItemId } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NetService from "@t3tools/shared/Net";
import * as ConfigProvider from "effect/ConfigProvider";
@@ -23,6 +23,7 @@ import {
persistServerRuntimeState,
} from "../serverRuntimeState.ts";
import {
+ countObservedToolCalls,
readLatestMessages,
resolveProject,
threadCommand,
@@ -528,3 +529,39 @@ it.live("resolves a path inside a git worktree of the project's repository to th
assert.include(ambiguous.message, "feature (project-feature)");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
+
+it("counts each native tool item once on the exact local turn", () => {
+ const tool = {
+ visibility: "local" as const,
+ sourceThreadId: threadId,
+ runId,
+ type: "command_execution",
+ itemId: TurnItemId.make("command"),
+ };
+ const otherItems = [
+ {
+ ...tool,
+ itemId: TurnItemId.make(
+ `turn-item:provider:codex:native-item:${encodeURIComponent(`workspace-preparation:${runId}`)}`,
+ ),
+ },
+ { ...tool, visibility: "inherited" as const },
+ { ...tool, runId: RunId.make("another-run") },
+ { ...tool, sourceThreadId: ThreadId.make("another-thread") },
+ { ...tool, type: "assistant_message" },
+ ];
+ assert.equal(countObservedToolCalls(otherItems, threadId, runId), 0);
+ assert.equal(
+ countObservedToolCalls(
+ [
+ ...otherItems,
+ tool,
+ tool,
+ { ...tool, type: "dynamic_tool", itemId: TurnItemId.make("read") },
+ ],
+ threadId,
+ runId,
+ ),
+ 2,
+ );
+});
diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts
index 98d18a6c9d240..08e7e622d101b 100644
--- a/apps/server/src/cli/thread.ts
+++ b/apps/server/src/cli/thread.ts
@@ -1,4 +1,5 @@
import {
+ isToolLifecycleItemType,
OrchestratorMcpFailure,
OrchestratorMcpThreadListResult,
OrchestratorMcpThreadReadResult,
@@ -896,6 +897,91 @@ const threadSendCommand = Command.make("send", {
),
);
+export function countObservedToolCalls(
+ items: ReadonlyArray<
+ Pick<
+ OrchestratorMcpThreadTimelineItem,
+ "visibility" | "sourceThreadId" | "runId" | "type" | "itemId"
+ >
+ >,
+ threadId: ThreadId,
+ runId: RunId,
+): number {
+ // The runtime emits workspace preparation as a synthetic command_execution.
+ const workspacePreparationItem = `:native-item:${encodeURIComponent(`workspace-preparation:${runId}`)}`;
+ return new Set(
+ items
+ .filter(
+ (item) =>
+ item.visibility === "local" &&
+ item.sourceThreadId === threadId &&
+ item.runId === runId &&
+ !item.itemId.endsWith(workspacePreparationItem) &&
+ (item.type === "dynamic_tool" || isToolLifecycleItemType(item.type)),
+ )
+ .map((item) => item.itemId),
+ ).size;
+}
+
+const threadToolUsageCommand = Command.make("tool-usage", {
+ ...projectLocationFlags,
+ thread: threadArgument,
+ run: Argument.String("run"),
+}).pipe(
+ Command.withDescription("Print the count observed in the native Work Log for a completed run."),
+ Command.withHandler((flags) =>
+ Effect.gen(function* () {
+ const threadId = yield* decodeThreadId(flags.thread);
+ yield* withThreadTools({ ...flags, json: true }, (tools) =>
+ Effect.gen(function* () {
+ let cursor: number | undefined;
+ const items: Array = [];
+ for (;;) {
+ const page = yield* tools.call(
+ "t3_thread_read",
+ {
+ threadId,
+ view: "activity",
+ limit: READ_PAGE_LIMIT,
+ maxCharsPerItem: 1,
+ runLimit: 1,
+ ...(cursor === undefined ? {} : { afterPosition: cursor }),
+ },
+ OrchestratorMcpThreadReadResult,
+ );
+ const run = page.recentRuns[0];
+ if (
+ page.thread.threadId !== threadId ||
+ run?.runId !== flags.run ||
+ run.status !== "completed" ||
+ page.thread.activeRunId !== null
+ ) {
+ return yield* new ThreadUsageError({
+ detail: "Tool evidence requires the latest completed run of an idle thread.",
+ });
+ }
+ items.push(...page.items);
+ if (!page.hasMore) {
+ return yield* printJson({
+ source: "t3-work-log",
+ threadId,
+ turnId: run.runId,
+ toolCalls: countObservedToolCalls(items, threadId, run.runId),
+ });
+ }
+ if (page.nextPosition === null || page.nextPosition <= (cursor ?? -1)) {
+ return yield* new ThreadUsageError({
+ detail: "Tool evidence timeline is incomplete.",
+ });
+ }
+ cursor = page.nextPosition;
+ }
+ }),
+ );
+ }).pipe(reportThreadErrors),
+ ),
+);
+
const threadShowCommand = Command.make("show", {
...projectLocationFlags,
thread: threadArgument,
@@ -957,5 +1043,6 @@ export const threadCommand = Command.make("thread").pipe(
threadStartCommand,
threadSendCommand,
threadShowCommand,
+ threadToolUsageCommand,
]),
);