From f287e7721826f0923fe337ccc84ea23968cec431 Mon Sep 17 00:00:00 2001 From: InTheta <17220028+InTheta@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:36:51 +0000 Subject: [PATCH 1/3] Show read-only managed run details in Delivery --- .../web/src/components/ManagedRunEvidence.tsx | 66 +++++++++++++++++++ apps/web/src/lib/managedRunDelivery.test.ts | 62 +++++++++++++++++ apps/web/src/lib/managedRunDelivery.ts | 60 +++++++++++++++++ apps/web/src/routes/_chat.delivery.tsx | 64 +++++++++++++++--- docs/user/delivery.md | 8 +++ 5 files changed, 250 insertions(+), 10 deletions(-) create mode 100644 apps/web/src/components/ManagedRunEvidence.tsx create mode 100644 apps/web/src/lib/managedRunDelivery.test.ts create mode 100644 apps/web/src/lib/managedRunDelivery.ts diff --git a/apps/web/src/components/ManagedRunEvidence.tsx b/apps/web/src/components/ManagedRunEvidence.tsx new file mode 100644 index 0000000000000..e99c352346e77 --- /dev/null +++ b/apps/web/src/components/ManagedRunEvidence.tsx @@ -0,0 +1,66 @@ +import { Link } from "@tanstack/react-router"; + +import type { ManagedRun } from "~/lib/managedRunDelivery"; + +function formatTokens(value: number | null): string { + return value === null ? "Unknown" : value.toLocaleString(); +} + +export function ManagedRunEvidence({ run }: { run: ManagedRun }) { + const { brief, budget } = run; + const fields = [ + ["Run ID", run.runId], + ["Status", run.status], + ["Brief revision", run.revision], + ["Workspace", brief.workspaceId], + ["Repository", brief.repository], + ["Base commit", brief.baseSha], + ["Scope", brief.scope], + ["Brief tokens", formatTokens(brief.tokens)], + ["Reserved tokens", formatTokens(budget.reserved)], + ["Used tokens", formatTokens(budget.used)], + ["Remaining tokens", formatTokens(budget.remaining)], + ]; + return ( +
+

Managed run · {run.ticket}

+

+ Read only. Approval and dispatch controls are unavailable here. +

+

Brief and budget

+
+ {fields.map(([label, value]) => ( +
+
{label}
+
{value}
+
+ ))} +
+

Stages

+ {run.stages.length === 0 ? ( +

No stages recorded.

+ ) : ( + + )} +
+ + Provider Settings + + + Usage + +
+
+ ); +} diff --git a/apps/web/src/lib/managedRunDelivery.test.ts b/apps/web/src/lib/managedRunDelivery.test.ts new file mode 100644 index 0000000000000..7a79da34582c1 --- /dev/null +++ b/apps/web/src/lib/managedRunDelivery.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it, vi } from "vite-plus/test"; + +import { parseManagedRun, parseManagedRunId, readManagedRun } from "./managedRunDelivery"; + +const runId = "547c0437-b190-4c3f-a703-ae2c3d839e27"; +const otherId = "547c0437-b190-4c3f-a703-ae2c3d839e28"; +const run = { + version: 1, + runId, + ticket: "T3-03", + revision: "a".repeat(64), + status: "approved", + brief: { + workspaceId: "isolated", + repository: "InTheta/t3code", + baseSha: "b".repeat(40), + scope: "Delivery", + tokens: 1000, + }, + budget: { reserved: 1000, used: null, remaining: null }, + stages: [ + { + stageId: `${runId}:lead`, + role: "lead", + status: "queued", + providerInstanceId: null, + threadId: null, + }, + ], +}; + +describe("managed-run Delivery", () => { + it("normalizes UUIDs and rejects values that could change the endpoint path", () => { + expect(parseManagedRunId(` ${runId.toUpperCase()} `)).toBe(runId); + expect(parseManagedRunId(`${runId}/extra`)).toBeUndefined(); + }); + + it("preserves unknown metrics and accepts additive sidecar fields", () => { + expect(parseManagedRun({ ...run, nextVersionField: null }, runId)).toMatchObject(run); + }); + + it("rejects mismatched identity and malformed evidence", () => { + expect(parseManagedRun(run, otherId)).toBeNull(); + expect(parseManagedRun({ ...run, version: 2 }, runId)).toBeNull(); + expect(parseManagedRun({ ...run, budget: { ...run.budget, used: -1 } }, runId)).toBeNull(); + expect(parseManagedRun({ ...run, stages: [{ status: "queued" }] }, runId)).toBeNull(); + }); + + it("reads fresh evidence and treats a disabled sidecar as unavailable", async () => { + const { signal } = new AbortController(); + using fetch = vi + .spyOn(globalThis, "fetch") + .mockResolvedValueOnce(Response.json(run)) + .mockResolvedValueOnce(new Response(null, { status: 404 })); + await expect(readManagedRun(runId, signal)).resolves.toMatchObject(run); + expect(fetch).toHaveBeenCalledWith(`/delivery-sidecar/v1/managed-runs/${runId}`, { + cache: "no-store", + signal, + }); + await expect(readManagedRun(runId, signal)).resolves.toBeNull(); + }); +}); diff --git a/apps/web/src/lib/managedRunDelivery.ts b/apps/web/src/lib/managedRunDelivery.ts new file mode 100644 index 0000000000000..16b90626d9faf --- /dev/null +++ b/apps/web/src/lib/managedRunDelivery.ts @@ -0,0 +1,60 @@ +import * as Schema from "effect/Schema"; + +const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; +const Tokens = Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER })); +const ManagedRun = Schema.Struct({ + version: Schema.Literal(1), + runId: Schema.String.check(Schema.isPattern(uuid)), + ticket: Schema.String, + revision: Schema.String.check(Schema.isPattern(/^[0-9a-f]{64}$/)), + status: Schema.String, + brief: Schema.Struct({ + workspaceId: Schema.String, + repository: Schema.String, + baseSha: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/)), + scope: Schema.String, + tokens: Tokens, + }), + budget: Schema.Struct({ + reserved: Tokens, + used: Schema.NullOr(Tokens), + remaining: Schema.NullOr(Tokens), + }), + stages: Schema.Array( + Schema.Struct({ + stageId: Schema.String, + role: Schema.String, + status: Schema.String, + providerInstanceId: Schema.NullOr(Schema.String), + threadId: Schema.NullOr(Schema.String), + }), + ), +}); + +export type ManagedRun = typeof ManagedRun.Type; +const isManagedRun = Schema.is(ManagedRun); + +export function parseManagedRunId(input: unknown): string | undefined { + if (typeof input !== "string") return undefined; + const normalized = input.trim().toLowerCase(); + return uuid.test(normalized) ? normalized : undefined; +} + +export function parseManagedRun(input: unknown, requestedRunId: string): ManagedRun | null { + return isManagedRun(input) && input.runId === requestedRunId ? input : null; +} + +export async function readManagedRun( + runId: string, + signal: AbortSignal, +): Promise { + try { + const response = await fetch(`/delivery-sidecar/v1/managed-runs/${encodeURIComponent(runId)}`, { + cache: "no-store", + signal, + }); + return response.ok ? parseManagedRun(await response.json(), runId) : null; + } catch { + return null; + } +} diff --git a/apps/web/src/routes/_chat.delivery.tsx b/apps/web/src/routes/_chat.delivery.tsx index b91976696e732..ac90014af87bc 100644 --- a/apps/web/src/routes/_chat.delivery.tsx +++ b/apps/web/src/routes/_chat.delivery.tsx @@ -1,6 +1,8 @@ import { createFileRoute, useNavigate, useRouter, useRouterState } from "@tanstack/react-router"; +import { ManagedRunEvidence } from "~/components/ManagedRunEvidence"; import { parseDeliveryPullRequestUrl } from "~/lib/deliveryPullRequest"; +import { parseManagedRunId, readManagedRun } from "~/lib/managedRunDelivery"; interface Delivery { version: 1; @@ -140,8 +142,8 @@ function PullRequestEvidence({ delivery }: { delivery: PullRequestDelivery }) { function DeliveryPage() { const router = useRouter(); const navigate = useNavigate({ from: Route.fullPath }); - const { ticket, pr } = Route.useSearch(); - const { delivery, pullRequestDelivery } = Route.useLoaderData(); + const { ticket, pr, run } = Route.useSearch(); + const { delivery, pullRequestDelivery, managedRun } = Route.useLoaderData(); const loading = useRouterState({ select: (state) => state.isLoading }); const ciStatus = delivery?.ci.status === "passed" && delivery.ci.head !== delivery.pullRequest.head @@ -233,7 +235,46 @@ function DeliveryPage() { Open Delivery - {pr && (loading || !pullRequestDelivery) && ( +
{ + event.preventDefault(); + const value = parseManagedRunId(new FormData(event.currentTarget).get("run")); + if (value) { + void navigate({ search: { run: value } }); + } else { + const field = event.currentTarget.elements.namedItem("run"); + if (field instanceof HTMLInputElement) { + field.setCustomValidity("Enter a managed run UUID."); + field.reportValidity(); + } + } + }} + > + + +
+ {run && (loading || !managedRun) && ( +

+ {loading + ? "Reading managed run…" + : "Managed run unavailable. Check the run ID and sidecar connection."} +

+ )} + {managedRun && !loading && } + {!run && pr && (loading || !pullRequestDelivery) && (

{loading ? "Reading pull request evidence…" @@ -241,7 +282,7 @@ function DeliveryPage() {

)} {pullRequestDelivery && !loading && } - {ticket && (loading || !delivery) && ( + {!run && ticket && (loading || !delivery) && (

{loading ? "Reading delivery evidence…" : "Delivery sidecar unavailable."}

@@ -276,7 +317,7 @@ function DeliveryPage() { export const Route = createFileRoute("/_chat/delivery")({ validateSearch: ( search: Record, - ): { ticket?: string | undefined; pr?: string | undefined } => ({ + ): { ticket?: string | undefined; pr?: string | undefined; run?: string | undefined } => ({ ticket: typeof search.ticket === "string" && validTicket.test(search.ticket) ? search.ticket @@ -285,14 +326,17 @@ export const Route = createFileRoute("/_chat/delivery")({ typeof search.pr === "string" && parseDeliveryPullRequestUrl(search.pr) ? search.pr : undefined, + run: parseManagedRunId(search.run), }), - loaderDeps: ({ search }) => ({ ticket: search.ticket, pr: search.pr }), + loaderDeps: ({ search }) => ({ ticket: search.ticket, pr: search.pr, run: search.run }), loader: async ({ abortController, deps }) => ({ delivery: - !deps.pr && deps.ticket ? await readDelivery(deps.ticket, abortController.signal) : null, - pullRequestDelivery: deps.pr - ? await readPullRequestDelivery(deps.pr, abortController.signal) - : null, + !deps.run && !deps.pr && deps.ticket + ? await readDelivery(deps.ticket, abortController.signal) + : null, + pullRequestDelivery: + !deps.run && deps.pr ? await readPullRequestDelivery(deps.pr, abortController.signal) : null, + managedRun: deps.run ? await readManagedRun(deps.run, abortController.signal) : null, }), component: DeliveryPage, }); diff --git a/docs/user/delivery.md b/docs/user/delivery.md index 11abca26aa70d..77fe019fa2b26 100644 --- a/docs/user/delivery.md +++ b/docs/user/delivery.md @@ -21,3 +21,11 @@ A version 1 response contains `version: 1`, `ticket`, `owner`, and `stage`, plus For a Vite development session, set T3CODE_DELIVERY_PROXY_URL to the URL of your read service. For a deployed frontend, configure the same-origin route in your hosting or reverse proxy setup. + +### Managed runs + +Enter a managed run UUID in Delivery, or open `/delivery?run=`. The isolated sidecar +must expose `GET /delivery-sidecar/v1/managed-runs/` through the existing Delivery proxy. +Delivery shows the recorded brief revision, repository, base commit, scope, reservation, and +stages. Unknown token usage stays unknown. This view has no approval or dispatch controls. +Use Provider Settings and Usage to inspect providers in the current T3 environment. From 638baa89998a2810b74d2ed0c0f466aef7a9c383 Mon Sep 17 00:00:00 2001 From: InTheta <17220028+InTheta@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:54:13 +0000 Subject: [PATCH 2/3] Show per-stage tool call evidence in Delivery --- apps/web/src/components/ManagedRunEvidence.tsx | 11 ++++++----- apps/web/src/lib/managedRunDelivery.test.ts | 12 ++++++++++++ apps/web/src/lib/managedRunDelivery.ts | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/ManagedRunEvidence.tsx b/apps/web/src/components/ManagedRunEvidence.tsx index e99c352346e77..31e80c66a67e2 100644 --- a/apps/web/src/components/ManagedRunEvidence.tsx +++ b/apps/web/src/components/ManagedRunEvidence.tsx @@ -2,7 +2,7 @@ import { Link } from "@tanstack/react-router"; import type { ManagedRun } from "~/lib/managedRunDelivery"; -function formatTokens(value: number | null): string { +function formatCount(value: number | null): string { return value === null ? "Unknown" : value.toLocaleString(); } @@ -16,10 +16,10 @@ export function ManagedRunEvidence({ run }: { run: ManagedRun }) { ["Repository", brief.repository], ["Base commit", brief.baseSha], ["Scope", brief.scope], - ["Brief tokens", formatTokens(brief.tokens)], - ["Reserved tokens", formatTokens(budget.reserved)], - ["Used tokens", formatTokens(budget.used)], - ["Remaining tokens", formatTokens(budget.remaining)], + ["Brief tokens", formatCount(brief.tokens)], + ["Reserved tokens", formatCount(budget.reserved)], + ["Used tokens", formatCount(budget.used)], + ["Remaining tokens", formatCount(budget.remaining)], ]; return (
@@ -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, ]), );