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
+1 -1
View File
@@ -3,7 +3,7 @@
This plugin implements `ProviderRegistration` directly. It demonstrates connection negotiation,
catalog and session listing, session creation, prompt and command disposition, option controls
with icons (`controls` and `providerOptions`),
persistence and replay, archive actions, and an ordinary child session.
persistence and replay, archive actions, turn and session metrics, and an ordinary child session.
The provider emits versioned `provider-result` timeline items for its root and child sessions.
`index.client.tsx` registers the Zod schema and React Native renderer for those items. The server
@@ -47,6 +47,7 @@ describe("direct provider example", () => {
"prompt.message",
"prompt.command",
"session.configure",
"session.metrics",
"session.persistence",
"session.subsession",
"timeline.plugin",
@@ -165,6 +166,54 @@ describe("direct provider example", () => {
}),
);
expect(events).toContainEqual(expect.objectContaining({ type: "session.persistence" }));
expect(events).toContainEqual({
type: "session.turn",
sessionId: "session-complete",
turnId: "turn-1",
state: "completed",
metrics: [
{ label: "Model", value: "example-1" },
{ label: "Reply", value: "11 characters" },
],
});
expect(events).toContainEqual({
type: "session.metrics",
sessionId: "session-complete",
metrics: [{ label: "Turns", value: "1" }],
});
await connection.close();
});
it("keeps session metrics from a Paseo that does not offer them", async () => {
const connection = await createDirectExampleProvider().connect({
versions: [1],
capabilities: ["prompt.message"],
});
const events: ProviderEvent[] = [];
connection.onEvent((event) => events.push(event));
await connection.send({
type: "session.open",
requestId: "open-older",
sessionId: "session-older",
config: { cwd: "/repo", env: {}, mcpServers: {}, settings: {}, persist: false },
history: "skip",
});
await connection.send({
type: "session.prompt",
sessionId: "session-older",
prompt: {
clientMessageId: "message-older",
delivery: "auto",
input: { type: "message", content: [{ type: "text", text: "hello" }] },
},
});
await new Promise<void>((resolve) => queueMicrotask(resolve));
expect(events).toContainEqual(
expect.objectContaining({ type: "session.turn", state: "completed" }),
);
expect(events.filter((event) => event.type === "session.metrics")).toEqual([]);
await connection.close();
});
@@ -21,6 +21,7 @@ const CAPABILITIES = [
"session.archive",
"session.configure",
"session.list",
"session.metrics",
"session.persistence",
"session.subsession",
"session.unarchive",
@@ -309,10 +310,11 @@ function promptSession(
result: { type: "turn", turnId },
});
state.emit({ type: "session.turn", sessionId: input.sessionId, turnId, state: "started" });
const reply = `Echo: ${text}`;
state.emit({
type: "timeline.item",
sessionId: input.sessionId,
item: { type: "assistant_message", id: `assistant-${session.turn}`, text: `Echo: ${text}` },
item: { type: "assistant_message", id: `assistant-${session.turn}`, text: reply },
});
state.emit({
type: "timeline.item",
@@ -336,7 +338,25 @@ function promptSession(
sessionId: input.sessionId,
persistence: session.persistence,
});
state.emit({ type: "session.turn", sessionId: input.sessionId, turnId, state: "completed" });
// Values are formatted here: Paseo shows each one as given.
state.emit({
type: "session.turn",
sessionId: input.sessionId,
turnId,
state: "completed",
metrics: [
{ label: "Model", value: session.config.model ?? "example-1" },
{ label: "Reply", value: `${reply.length} characters` },
],
});
// An older Paseo does not offer session metrics and rejects the event.
if (state.capabilities.includes("session.metrics")) {
state.emit({
type: "session.metrics",
sessionId: input.sessionId,
metrics: [{ label: "Turns", value: String(session.turn) }],
});
}
session.activeTurnId = null;
}