Repository navigation
feat(server): export how long runs take and how they end #22094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+454
−20
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
39d493f
feat(server): export how long runs take and how they end
juliusmarminge 2e14866
fix(server): a rollback no longer counts a finished run again
juliusmarminge 646eaf9
fix(server): break the RunMetrics import cycle and record before publ…
juliusmarminge c4933db
test(server): takeover-tests for run outcome metrics
juliusmarminge 5c7ce57
refactor(server): run outcome metrics record atomically
juliusmarminge c757603
Merge remote-tracking branch 'origin/main' into t3/run-duration-metric
juliusmarminge a2218be
test(server): add reproducible run metrics evidence drivers
juliusmarminge 4dc39ad
fix(server): label a subagent thread's first run as delegated
juliusmarminge dc42a03
Merge remote-tracking branch 'origin/main' into t3/run-duration-metric
juliusmarminge d221e15
test(server): capture run metric lineage boundaries
juliusmarminge 7500a9d
fix(server): read run lineage inside the write transaction
juliusmarminge 52b942a
fix(server): prepare every finished run so the 4096-run FIFO bound holds
juliusmarminge 8cde869
test(server): capture staged run metric preparation boundaries
juliusmarminge d30d014
test(server): retain run projection and metadata rollback evidence
juliusmarminge 365c045
test(server): distinguish prepared duplicate timer winners
juliusmarminge 7071694
test(server): trim run metrics tests and drop evidence scripts
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
| import { | ||
| EventId, | ||
| ProviderInstanceId, | ||
| RunId, | ||
| ThreadId, | ||
| type OrchestrationV2Run, | ||
| type OrchestrationV2StoredEvent, | ||
| } from "@t3tools/contracts"; | ||
| import * as DateTime from "effect/DateTime"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Metric from "effect/Metric"; | ||
|
|
||
| import { makeRunOutcomeRecorder } from "./RunMetrics.ts"; | ||
|
|
||
| const at = (iso: string) => DateTime.makeUnsafe(iso); | ||
|
|
||
| const runUpdated = ( | ||
| sequence: number, | ||
| run: Pick<OrchestrationV2Run, "id" | "status" | "startedAt" | "completedAt">, | ||
| provider: string, | ||
| ): OrchestrationV2StoredEvent => ({ | ||
| sequence, | ||
| commandId: null, | ||
| event: { | ||
| type: "run.updated", | ||
| id: EventId.make(`event:${sequence}`), | ||
| threadId: ThreadId.make("thread:metrics"), | ||
| occurredAt: at("2026-10-19T12:00:00.000Z"), | ||
| payload: { | ||
| threadId: ThreadId.make("thread:metrics"), | ||
| ordinal: 1, | ||
| providerInstanceId: ProviderInstanceId.make(provider), | ||
| modelSelection: { instanceId: ProviderInstanceId.make(provider), model: "claude-opus-5-5" }, | ||
| requestedAt: at("2026-10-19T12:00:00.000Z"), | ||
| ...run, | ||
| } as OrchestrationV2Run, | ||
| }, | ||
| }); | ||
|
|
||
| const runSnapshots = (provider: string) => | ||
| Metric.snapshot.pipe( | ||
| Effect.map((snapshots) => | ||
| snapshots.filter( | ||
| (snapshot) => | ||
| snapshot.id.startsWith("t3_run") && snapshot.attributes?.["provider"] === provider, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| describe("makeRunOutcomeRecorder", () => { | ||
| it.effect("records a finished run once, with how long it waited and ran", () => | ||
| Effect.gen(function* () { | ||
| const record = makeRunOutcomeRecorder(); | ||
| const id = RunId.make("run:metrics:once"); | ||
| const started = { startedAt: at("2026-10-19T12:00:30.000Z") }; | ||
|
|
||
| yield* record([ | ||
| runUpdated(1, { id, status: "running", ...started, completedAt: null }, "p1"), | ||
| ]); | ||
| const finished = { id, status: "completed" as const, ...started }; | ||
| yield* record([ | ||
| runUpdated(2, { ...finished, completedAt: at("2026-10-19T12:10:30.000Z") }, "p1"), | ||
| ]); | ||
| // A later update to the finished run, such as its checkpoint, is not a second run. | ||
| yield* record([ | ||
| runUpdated(3, { ...finished, completedAt: at("2026-10-19T12:10:30.000Z") }, "p1"), | ||
| ]); | ||
|
|
||
| const snapshots = yield* runSnapshots("p1"); | ||
| const counter = snapshots.find((snapshot) => snapshot.id === "t3_runs_total"); | ||
| assert.strictEqual(counter?.type === "Counter" ? counter.state.count : undefined, 1); | ||
| assert.strictEqual(counter?.attributes?.["outcome"], "completed"); | ||
| assert.strictEqual(counter?.attributes?.["modelFamily"], "claude"); | ||
| const histogram = (id: string) => { | ||
| const snapshot = snapshots.find((candidate) => candidate.id === id); | ||
| return snapshot?.type === "Histogram" ? snapshot.state : undefined; | ||
| }; | ||
| assert.strictEqual(histogram("t3_run_duration")?.sum, 600_000); | ||
| assert.strictEqual(histogram("t3_run_queue_wait")?.sum, 30_000); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("does not count a rollback of a run that already finished", () => | ||
| Effect.gen(function* () { | ||
| // A fresh recorder stands in for a restart, which forgets the runs already counted. | ||
| const record = makeRunOutcomeRecorder(); | ||
| yield* record([ | ||
| runUpdated( | ||
| 1, | ||
| { | ||
| id: RunId.make("run:metrics:rolled-back"), | ||
| status: "rolled_back", | ||
| startedAt: at("2026-10-19T12:00:30.000Z"), | ||
| completedAt: at("2026-10-20T09:00:00.000Z"), | ||
| }, | ||
| "p3", | ||
| ), | ||
| ]); | ||
|
|
||
| assert.deepStrictEqual(yield* runSnapshots("p3"), []); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("counts a run cancelled before it started without timing it", () => | ||
| Effect.gen(function* () { | ||
| const record = makeRunOutcomeRecorder(); | ||
| yield* record([ | ||
| runUpdated( | ||
| 1, | ||
| { | ||
| id: RunId.make("run:metrics:cancelled"), | ||
| status: "cancelled", | ||
| startedAt: null, | ||
| completedAt: at("2026-10-19T12:01:00.000Z"), | ||
| }, | ||
| "p2", | ||
| ), | ||
| ]); | ||
|
|
||
| const snapshots = yield* runSnapshots("p2"); | ||
| assert.deepStrictEqual( | ||
| snapshots.map((snapshot) => snapshot.id), | ||
| ["t3_runs_total"], | ||
| ); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import type { OrchestrationV2Run, OrchestrationV2StoredEvent } from "@t3tools/contracts"; | ||
| import * as DateTime from "effect/DateTime"; | ||
| import * as Duration from "effect/Duration"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Metric from "effect/Metric"; | ||
|
|
||
| import { normalizeModelMetricLabel } from "../observability/Attributes.ts"; | ||
| import { | ||
| metricAttributes, | ||
| runDuration, | ||
| runQueueWait, | ||
| runsTotal, | ||
| } from "../observability/Metrics.ts"; | ||
|
|
||
| const REMEMBERED_RUNS = 4096; | ||
| // How a run can end. `rolled_back` is left out (see below). Declared here rather than taken from | ||
| // ThreadManagementService, which imports back into EventSink. | ||
| const OUTCOMES: ReadonlySet<OrchestrationV2Run["status"]> = new Set([ | ||
| "completed", | ||
| "failed", | ||
| "cancelled", | ||
| "interrupted", | ||
| ]); | ||
|
|
||
| /** | ||
| * Records each run once, when a committed event first shows it finished. A finished run's row is | ||
| * updated again later (its checkpoint, a settlement), so the runs already counted are remembered. | ||
| * A rollback is not how a run ended: it rewrites runs that already finished, stamping the rollback | ||
| * time as their completion, so it would count them again with a duration that runs to the | ||
| * rollback. | ||
| */ | ||
| export const makeRunOutcomeRecorder = () => { | ||
| const counted = new Set<string>(); | ||
| // Untraced: it runs for every committed batch inside the EventSink publish step, whose | ||
| // operation already owns the span. | ||
| return Effect.fnUntraced(function* (events: ReadonlyArray<OrchestrationV2StoredEvent>) { | ||
| for (const { event } of events) { | ||
| if (event.type !== "run.updated") continue; | ||
| const run = event.payload; | ||
| if (!OUTCOMES.has(run.status) || counted.has(run.id)) continue; | ||
| counted.add(run.id); | ||
| if (counted.size > REMEMBERED_RUNS) { | ||
| const oldest = counted.values().next(); | ||
| if (!oldest.done) counted.delete(oldest.value); | ||
| } | ||
| const attributes = metricAttributes({ | ||
| provider: run.providerInstanceId, | ||
| modelFamily: normalizeModelMetricLabel(run.modelSelection.model), | ||
| outcome: run.status, | ||
| delegated: run.delegatedTaskId !== undefined, | ||
| }); | ||
| yield* Metric.update(Metric.withAttributes(runsTotal, attributes), 1); | ||
| if (run.startedAt === null) continue; | ||
| yield* Metric.update( | ||
| Metric.withAttributes(runQueueWait, attributes), | ||
| Duration.millis( | ||
| DateTime.toEpochMillis(run.startedAt) - DateTime.toEpochMillis(run.requestedAt), | ||
| ), | ||
| ); | ||
| if (run.completedAt !== null) { | ||
| yield* Metric.update( | ||
| Metric.withAttributes(runDuration, attributes), | ||
| Duration.millis( | ||
| DateTime.toEpochMillis(run.completedAt) - DateTime.toEpochMillis(run.startedAt), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Protect the recording step from interruption.
If the publishing fiber is interrupted after
counted.add(run.id)but before allMetric.updatecalls finish, the committed run can retain incomplete metrics. A later update skips that ID. MakerecordRunOutcomes(events)uninterruptible as one recording step before publication. Effect documents that interruption can stop an executing fiber. (effect.website)Proposed change
📝 Committable suggestion
🤖 Prompt for AI Agents