feat(providers): let providers report session and turn metrics

Providers know things about a session and a turn that Paseo cannot derive:
timing, token flow, cache use, cost, the model that answered. They can now
report them as label/value rows that Paseo carries to clients unchanged, so
each provider decides which metrics make sense and how they read.

Session metrics live on the agent record and snapshot. Turn metrics are
stamped on the turn's footer row (last assistant message, else last tool
call) so they survive reload, pagination and a daemon restart, and so the
live turn_completed event and a later timeline fetch agree. Provider history
replay restores both.

Metrics are sanitized once where they enter the agent manager. The wire
schemas validate whole messages, so one malformed row from a provider would
otherwise make clients drop an entire snapshot, turn event or timeline page.

A live completed turn with no Model metric gets the agent's model, so every
provider shows model attribution before it is instrumented. Replayed turns do
not: they may have run on a different model than the agent has now.

Plugin providers publish session metrics behind a negotiated
`session.metrics` capability because an older daemon closes a connection
that sends an event type it does not know. Turn metrics ride an existing
event that older daemons strip, so they need no gate.

The wire additions are optional fields on existing schemas only.
This commit is contained in:
Mohamed Boudra
2026-11-22 08:25:42 +01:00
parent 08ad7c7271
commit eda4597aab
33 changed files with 1616 additions and 62 deletions
+62 -1
View File
@@ -1,5 +1,12 @@
import { describe, expect, it } from "vitest";
import { ProviderInputSchema, type ProviderInput } from "./provider.js";
import {
ProviderEventSchema,
ProviderInputSchema,
negotiateProviderCapabilities,
type ProviderEvent,
type ProviderInput,
type ProviderMetric,
} from "./provider.js";
type ReviewMode = "uncommitted" | "base" | "all";
type Delivery = "auto" | "steer";
@@ -64,3 +71,57 @@ describe("Provider review attachments", () => {
},
);
});
describe("Provider metrics", () => {
const metrics: ProviderMetric[] = [
{ label: "Model", value: "agent-large" },
{ label: "Cache", value: "82%", tone: "ok" },
];
it("publishes the session's metrics as one replaceable list", () => {
const event: ProviderEvent = { type: "session.metrics", sessionId: "session-1", metrics };
expect(ProviderEventSchema.parse(event)).toEqual(event);
});
it("attaches metrics to the turn they describe when it completes", () => {
const event: ProviderEvent = {
type: "session.turn",
sessionId: "session-1",
turnId: "turn-1",
state: "completed",
metrics,
};
expect(ProviderEventSchema.parse(event)).toEqual(event);
});
it("keeps completing turns without metrics", () => {
const event: ProviderEvent = {
type: "session.turn",
sessionId: "session-1",
turnId: "turn-1",
state: "completed",
};
expect(ProviderEventSchema.parse(event)).toEqual(event);
});
it("tells a provider whether this Paseo shows session metrics", () => {
const supported = ["prompt.message", "session.metrics"] as const;
expect(
negotiateProviderCapabilities(["prompt.message", "session.metrics"], supported),
).toContain("session.metrics");
// A Paseo from before session metrics never offers them.
expect(negotiateProviderCapabilities(["prompt.message"], supported)).toEqual([
"prompt.message",
]);
});
it("refuses a metric whose value is not text Paseo can show as given", () => {
expect(
ProviderEventSchema.safeParse({
type: "session.metrics",
sessionId: "session-1",
metrics: [{ label: "Cost", value: 1.23 }],
}).success,
).toBe(false);
});
});
+38
View File
@@ -13,6 +13,7 @@ export const PROVIDER_CAPABILITIES = [
"session.archive",
"session.configure",
"session.list",
"session.metrics",
"session.persistence",
"session.revert.both",
"session.revert.conversation",
@@ -549,6 +550,17 @@ export interface ProviderUsage {
contextWindowUsedTokens?: number;
}
/**
* One row of provider-owned detail about a session or a turn. Format `value` for
* reading: Paseo shows it as given.
*/
export interface ProviderMetric {
label: string;
value: string;
/** Colors the value. Omit it for an ordinary value. */
tone?: "default" | "ok" | "warning" | "danger";
}
interface ProviderTimelineIdentity {
id: string;
revertToken?: JsonValue;
@@ -724,8 +736,19 @@ export type ProviderEvent =
turnId: string;
state: "started" | "completed" | "failed" | "canceled";
error?: ProviderError;
/** Shown on the turn's footer. Read when `state` is `"completed"`. */
metrics?: readonly ProviderMetric[];
}
| { type: "session.usage"; sessionId: string; turnId?: string; usage: ProviderUsage }
| {
type: "session.metrics";
sessionId: string;
/**
* Replaces the metrics shown for the session. An empty list clears them. Publish only when
* `session.metrics` was negotiated: a Paseo that does not offer it rejects this event.
*/
metrics: readonly ProviderMetric[];
}
| { type: "session.config"; sessionId: string; config: ProviderConfigState }
| { type: "session.commands"; sessionId: string; commands: ProviderCommand[] }
| { type: "session.permission"; sessionId: string; request: ProviderPermissionRequest }
@@ -1232,6 +1255,13 @@ const usageSchema = z
contextWindowUsedTokens: z.number().optional(),
})
.strip();
const metricSchema = z
.object({
label: z.string(),
value: z.string(),
tone: z.enum(["default", "ok", "warning", "danger"]).optional(),
})
.strip();
const worktreeCommandSchema = z
.object({
index: z.number().int().positive(),
@@ -1552,6 +1582,7 @@ export const ProviderEventSchema: z.ZodType<ProviderEvent> = z.discriminatedUnio
turnId: idSchema,
state: z.enum(["started", "completed", "failed", "canceled"]),
error: providerErrorSchema.optional(),
metrics: z.array(metricSchema).optional(),
})
.strip(),
z
@@ -1562,6 +1593,13 @@ export const ProviderEventSchema: z.ZodType<ProviderEvent> = z.discriminatedUnio
usage: usageSchema,
})
.strip(),
z
.object({
type: z.literal("session.metrics"),
sessionId: idSchema,
metrics: z.array(metricSchema),
})
.strip(),
z
.object({ type: z.literal("session.config"), sessionId: idSchema, config: configStateSchema })
.strip(),