Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import { SqliteResourceStore } from "./store/sqliteResourceStore.ts";
import { SqliteRunStore } from "./store/sqliteRunStore.ts";
import { SqliteSessionStore } from "./store/sqliteSessionStore.ts";
import { SqliteSharedFolderStore } from "./store/sqliteSharedFolderStore.ts";
import { SqliteUsageStore } from "./store/sqliteUsageStore.ts";

// Opening the DB applies pending drizzle-kit migrations on startup. The single
// shared connection backs every store: session metadata for the REST routes and
Expand Down Expand Up @@ -217,10 +218,14 @@ reactors.useSuperviseCitation(async (sessionId, cause, supervisorId) => {
// previous process are settled once here: nothing can run before we start.
// Constructed before the router so a correlation can be keyed by its asker's
// open run.
const runs = new RunRegistry(new SqliteRunStore(db));
const runStore = new SqliteRunStore(db);
const runs = new RunRegistry(runStore);
void runs.failStaleRuns().then((failed) => {
if (failed > 0) console.log(`[runs] settled ${failed} stale run(s)`);
});
// Per-message token/cost accounting, appended as each turn finalizes and rolled
// up by the usage route into per-session / per-agent / per-run totals.
const usage = new SqliteUsageStore(db);
// Holds the outstanding request/reply correlations posted Messages open and
// resolve, so "who is blocked on whom" is a listable fact and a timeout is
// engine policy rather than a connector's private table. Empty until a Message
Expand Down Expand Up @@ -308,6 +313,7 @@ const pi = new PiAgentManager(
agentHandlers,
memoryResolver,
runs,
usage,
hostResourcePreamble,
(sessionId) => store.selectorsOf(sessionId),
);
Expand Down Expand Up @@ -479,6 +485,8 @@ app.use(
provokes: false,
});
},
usage,
runStore,
bundleKb,
),
);
Expand Down
77 changes: 76 additions & 1 deletion apps/server/src/pi/piAgentManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import type { AgentRole } from "../memory/types.ts";
import { RunRegistry } from "../runs/runRegistry.ts";
import { InMemoryRunStore } from "../store/inMemoryRunStore.ts";
import { InMemoryUsageStore } from "../store/inMemoryUsageStore.ts";
import type { SessionAgent } from "../store/sessionStore.ts";
import type { ResolvedSessionConfig } from "./agentConfig.ts";
import {
Expand Down Expand Up @@ -133,7 +134,7 @@
}

/** Builds a manager wired to a fake launcher; returns it plus the spawn log. */
function makeManager(options?: {

Check warning on line 137 in apps/server/src/pi/piAgentManager.test.ts

View workflow job for this annotation

GitHub Actions / Linting

Function 'makeManager' has too many lines (67). Maximum allowed is 60

Check warning on line 137 in apps/server/src/pi/piAgentManager.test.ts

View workflow job for this annotation

GitHub Actions / Linting

Function 'makeManager' has too many lines (67). Maximum allowed is 60
shouldFailSpawn?: () => boolean;
memory?: MemoryResolver;
selectors?: string[];
Expand All @@ -145,6 +146,7 @@
statuses: SessionStatusPayload[];
runs: RunRegistry;
runStore: InMemoryRunStore;
usageStore: InMemoryUsageStore;
} {
const spawns: SpawnRecord[] = [];
const rosterUpdates: { id: string; status: string }[] = [];
Expand Down Expand Up @@ -177,16 +179,27 @@

const runStore = new InMemoryRunStore();
const runs = new RunRegistry(runStore);
const usageStore = new InMemoryUsageStore();
const pi = new PiAgentManager(
handlers,
memory,
runs,
usageStore,
undefined,
() => options?.selectors ?? [],
fakeSpawn,
);
runs.useOnSettled((run, settlement) => pi.recordSettled(run, settlement));
return { pi, spawns, rosterUpdates, events, statuses, runs, runStore };
return {
pi,
spawns,
rosterUpdates,
events,
statuses,
runs,
runStore,
usageStore,
};
}

/** A persisted roster row with sane defaults, overridable per field. */
Expand Down Expand Up @@ -456,6 +469,68 @@
return new Promise((resolve) => setImmediate(resolve));
}

/** Drives a turn whose assistant `message_end` carries token/cost usage. */
function runTurnWithUsage(
child: FakeChild,
text: string,
usage: Record<string, unknown>,
): void {
emitPi(child, { type: "agent_start" });
emitPi(child, { type: "message_start", message: { role: "assistant" } });
emitPi(child, {
type: "message_update",
assistantMessageEvent: { type: "text_delta", delta: text },
});
emitPi(child, {
type: "message_end",
message: { role: "assistant", content: text, usage },
});
emitPi(child, { type: "agent_end" });
}

test("an assistant turn's usage is recorded against its agent and run", async () => {
const h = makeManager();
await h.pi.ensure("s1", "/tmp/s1");

h.pi.sendToAgent({ sessionId: "s1", agentId: PRIME_AGENT_ID, text: "hi" });
const runId = h.runs.current("s1", PRIME_AGENT_ID)?.id;

runTurnWithUsage(h.spawns[0].child, "done", {
input: 100,
output: 20,
cacheRead: 5,
cacheWrite: 0,
totalTokens: 125,
cost: { total: 0.0021 },
});
await flush();

const summary = await h.usageStore.summarize("s1");
assert.equal(summary.total.totalTokens, 125);
assert.equal(summary.total.inputTokens, 100);
assert.equal(summary.total.cacheReadTokens, 5);
assert.equal(summary.total.costUsd, 0.0021);
assert.deepEqual(
summary.byAgent.map((agent) => agent.agentId),
[PRIME_AGENT_ID],
);
assert.equal(summary.byRun[0]?.runId, runId);
assert.equal(summary.byRun[0]?.totals.totalTokens, 125);
});

test("a turn whose message carries no usage records nothing", async () => {
const h = makeManager();
await h.pi.ensure("s1", "/tmp/s1");

h.pi.sendToAgent({ sessionId: "s1", agentId: PRIME_AGENT_ID, text: "hi" });
runTurn(h.spawns[0].child, "no usage here");
await flush();

const summary = await h.usageStore.summarize("s1");
assert.equal(summary.total.totalTokens, 0);
assert.equal(summary.byAgent.length, 0);
});

test("a prompt opens one run and every event in the turn carries its id", async () => {
const h = makeManager();
await h.pi.ensure("s1", "/tmp/s1");
Expand Down
37 changes: 37 additions & 0 deletions apps/server/src/pi/piAgentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type {
Settlement,
} from "../runs/runRegistry.ts";
import type { SessionAgent } from "../store/sessionStore.ts";
import type { UsageStore } from "../store/usageStore.ts";
import {
type AgentConfig,
getPrimeAgentConfig,
Expand Down Expand Up @@ -86,6 +87,7 @@ import {
toolActivityLabel,
toSubagentInfo,
TRIGGERS_EXTENSION,
usageFromMessage,
} from "./utils.ts";

// Re-exported so existing consumers (sockets, routes, index) keep importing
Expand Down Expand Up @@ -1041,6 +1043,11 @@ export class PiAgentManager {
* settles them rather than having them inferred from the event stream.
*/
private readonly runs: RunRegistry;
/**
* Sink for per-message token/cost accounting, written as each assistant turn
* finalizes. The authority that feeds the usage route's rollups.
*/
private readonly usage: UsageStore;
/**
* Spawn-time projection of each session's host resources, appended to every
* agent's preamble. Optional so a bare manager (e.g. a test) skips it.
Expand Down Expand Up @@ -1082,6 +1089,7 @@ export class PiAgentManager {
handlers: ConversationEventSink,
memory: MemoryResolver,
runs: RunRegistry,
usage: UsageStore,
hostResources?: HostResourcePreamble,
selectorsOf?: SessionSelectors,
spawnProcess: typeof spawn = spawn,
Expand All @@ -1090,6 +1098,7 @@ export class PiAgentManager {
this.handlers = handlers;
this.memory = memory;
this.runs = runs;
this.usage = usage;
this.hostResources = hostResources;
this.selectorsOf = selectorsOf;
}
Expand Down Expand Up @@ -2370,6 +2379,8 @@ export class PiAgentManager {
assistantTextFromMessage(event.message) || agent.accum || "";
if (content.trim()) agent.lastFinalContent = content;

this.recordUsage(sessionId, agent, event);

this.emit(sessionId, descriptor, {
type: "end",
messageId: agent.currentMessageId as string,
Expand All @@ -2379,6 +2390,32 @@ export class PiAgentManager {
});
}

/**
* Appends the finalized message's token/cost accounting, attributed to the
* agent and whatever Run it is working under. Fire-and-forget: a usage write
* must never block or fail the turn it measures. A message Pi sent with no
* `usage` (an older build, or a provider that omits it) records nothing.
*/
private recordUsage(
sessionId: string,
agent: AgentProcess,
event: PiStdoutEvent,
): void {
const usage = usageFromMessage(event.message);
if (!usage) return;
void this.usage
.record({
sessionId,
agentId: agent.agentId,
runId: this.runs.current(sessionId, agent.agentId)?.id,
model: agent.config.model,
usage,
})
.catch((err: unknown) => {
console.error(`[pi:${sessionId}:${agent.agentId}] usage write`, err);
});
}

/**
* Surfaces a starting tool call as the ephemeral activity indicator. The
* descriptive label deliberately persists past the tool's `tool_execution_end`
Expand Down
29 changes: 28 additions & 1 deletion apps/server/src/pi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ export interface AssistantDelta {
delta?: string;
}

/**
* The token/cost accounting Pi attaches to a finalized assistant message. Token
* counts are that model call's own; `cost` is already in US dollars, computed by
* Pi from its per-model rate table. Flat and all-optional because it comes
* straight from JSON and an older Pi (or a provider that omits a field) may not
* carry every key.
*/
export interface MessageUsage {
input?: number;
output?: number;
cacheRead?: number;
cacheWrite?: number;
totalTokens?: number;
cost?: {
input?: number;
output?: number;
cacheRead?: number;
cacheWrite?: number;
total?: number;
};
}

/**
* A parsed line from a Pi process's stdout (JSONL RPC stream). Kept as a flat
* shape (rather than a discriminated union) because the `type` field comes
Expand All @@ -254,7 +276,12 @@ export interface PiStdoutEvent {
* Present on `message_start` / `message_update` / `message_end` events. An
* assistant message's `stopReason` is `error` when the model call failed.
*/
message?: { role?: string; content?: unknown; stopReason?: string };
message?: {
role?: string;
content?: unknown;
stopReason?: string;
usage?: MessageUsage;
};
/** Present on `tool_execution_*` events. */
toolName?: string;
/** Present on `tool_execution_start` / `tool_execution_update` events. */
Expand Down
49 changes: 49 additions & 0 deletions apps/server/src/pi/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import assert from "node:assert/strict";
import { test } from "node:test";

import { usageFromMessage } from "./utils.ts";

test("usageFromMessage lifts a full usage object onto the wire totals", () => {
const usage = usageFromMessage({
usage: {
input: 100,
output: 20,
cacheRead: 5,
cacheWrite: 2,
totalTokens: 127,
cost: { input: 0.1, output: 0.2, total: 0.0031 },
},
});
assert.deepEqual(usage, {
inputTokens: 100,
outputTokens: 20,
cacheReadTokens: 5,
cacheWriteTokens: 2,
totalTokens: 127,
costUsd: 0.0031,
});
});

test("usageFromMessage returns undefined when the message carries no usage", () => {
assert.equal(usageFromMessage(undefined), undefined);
assert.equal(usageFromMessage({}), undefined);
});

test("usageFromMessage tolerates partial usage, defaulting missing fields to 0", () => {
const usage = usageFromMessage({ usage: { input: 10, output: 5 } });
assert.deepEqual(usage, {
inputTokens: 10,
outputTokens: 5,
cacheReadTokens: 0,
cacheWriteTokens: 0,
totalTokens: 15,
costUsd: 0,
});
});

test("usageFromMessage falls back to summing tokens when totalTokens is absent", () => {
const usage = usageFromMessage({
usage: { input: 100, output: 20, cacheRead: 5, cacheWrite: 5 },
});
assert.equal(usage?.totalTokens, 130);
});
39 changes: 38 additions & 1 deletion apps/server/src/pi/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import path from "node:path";
import { StringDecoder } from "node:string_decoder";

import { connectorFor, type SubagentInfo } from "@tangent/shared/contracts.ts";
import {
connectorFor,
type SubagentInfo,
type UsageTotals,
} from "@tangent/shared/contracts.ts";

import type {
AgentDescriptor,
AgentProcess,
AssistantDelta,
MessageUsage,
PiStdoutEvent,
} from "./types.ts";

Expand Down Expand Up @@ -233,6 +238,38 @@ export function assistantTextFromMessage(message: unknown): string | undefined {
return assistantText(message);
}

/** A finite number, or 0 — so a missing/NaN field never poisons a sum. */
function num(value: unknown): number {
return typeof value === "number" && Number.isFinite(value) ? value : 0;
}

/**
* Lifts the token/cost accounting off a finalized assistant message into the
* wire {@link UsageTotals}. Returns `undefined` when the message carries no
* `usage` at all (the signal not to record anything), but tolerates partial
* usage — any absent field reads as 0.
*/
export function usageFromMessage(
message: { usage?: MessageUsage } | undefined,
): UsageTotals | undefined {
const usage = message?.usage;
if (!usage || typeof usage !== "object") return undefined;
const input = num(usage.input);
const output = num(usage.output);
const cacheRead = num(usage.cacheRead);
const cacheWrite = num(usage.cacheWrite);
const totalTokens =
num(usage.totalTokens) || input + output + cacheRead + cacheWrite;
return {
inputTokens: input,
outputTokens: output,
cacheReadTokens: cacheRead,
cacheWriteTokens: cacheWrite,
totalTokens,
costUsd: num(usage.cost?.total),
};
}

/** Max length of an arg hint before it is ellipsized in the activity label. */
const MAX_HINT_LENGTH = 80;

Expand Down
Loading
Loading