feat(timeline): record the provider that wrote each row

An agent's history can span providers once it switches, so a row served with
the agent's current provider would be rendered as the wrong one. Live writes,
provider history imports, opening notices and subagent rows now store their
provider in the row; the timeline page and replay serve it, falling back to
the agent's provider for rows written before this change. SQLite keeps it in
the row JSON, so the schema is unchanged.
This commit is contained in:
Mohamed Boudra
2026-11-25 16:31:25 +01:00
parent 573e46cf20
commit beccad5ede
10 changed files with 120 additions and 13 deletions
@@ -954,6 +954,52 @@ test("Reload agent keeps the history and its identity, and adds only what the pr
}
});
test("records the provider that wrote each row, live or from provider history", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-row-provider-"));
class HistorySession extends TestAgentSession {
override async *streamHistory(): AsyncGenerator<AgentStreamEvent> {
yield {
type: "timeline",
provider: "codex",
item: { type: "user_message", text: "continue", messageId: "provider-1" },
};
}
}
class HistoryClient extends TestAgentClient {
override async createSession(config: AgentSessionConfig): Promise<AgentSession> {
return new HistorySession(config);
}
override async resumeSession(
_handle: AgentPersistenceHandle,
config?: Partial<AgentSessionConfig>,
): Promise<AgentSession> {
return new HistorySession({ provider: "codex", cwd: config?.cwd ?? workdir });
}
}
const manager = new AgentManager({
clients: { codex: new HistoryClient() },
timeline: new AgentTimeline(new DeferredTimelineBackend()),
logger,
});
let agentId: string | null = null;
try {
const agent = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, {
workspaceId: undefined,
});
agentId = agent.id;
expect(await reloadAgent(manager, agent.id)).toEqual({ kind: "appended" });
await manager.appendTimelineItem(agent.id, { type: "assistant_message", text: "live" });
await manager.flush();
const page = await manager.fetchTimeline(agent.id);
expect(page.rows.map((row) => row.provider)).toEqual(["codex", "codex"]);
} finally {
if (agentId) await manager.closeAgent(agentId).catch(() => undefined);
rmSync(workdir, { recursive: true, force: true });
}
});
test("Reload agent adds the provider children of new turns and leaves the children Paseo shows alone", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-timeline-reload-children-"));
const providerHistory: AgentStreamEvent[] = [
@@ -7457,6 +7503,7 @@ test("getTimelineRows falls back to the in-memory timeline when no durable store
sourceSeqRanges: [{ startSeq: 1, endSeq: 2 }],
collapsed: ["assistant_merge"],
timestamp: expect.any(String),
provider: "codex",
item: { type: "assistant_message", text: "row onerow two" },
},
]);
@@ -802,7 +802,11 @@ function validateAgentId(agentId: string, source: string): string {
}
function toTimelineEntry(event: Extract<AgentStreamEvent, { type: "timeline" }>): TimelineEntry {
return event.timestamp ? { item: event.item, timestamp: event.timestamp } : { item: event.item };
return {
item: event.item,
provider: event.provider,
...(event.timestamp ? { timestamp: event.timestamp } : {}),
};
}
/** The live event that publishes a row committed from provider history. */
@@ -816,6 +820,7 @@ function historyEvent(provider: AgentProvider, commit: TimelineCommit): AgentStr
}
function projectImportedTimelineEntries(
provider: AgentProvider,
entries: readonly ImportedTimelineEntry[],
): TimelineEntry[] {
const projected: TimelineEntry[] = [];
@@ -823,9 +828,11 @@ function projectImportedTimelineEntries(
const projection = new TimelineProjection();
for (const entry of entries) {
for (const item of projectAgentMessages(entry.item)) {
const timelineEntry: TimelineEntry = entry.timestamp
? { item, timestamp: entry.timestamp }
: { item };
const timelineEntry: TimelineEntry = {
item,
provider,
...(entry.timestamp ? { timestamp: entry.timestamp } : {}),
};
projected.push(timelineEntry);
projection.append({
seq: projected.length,
@@ -1968,7 +1975,7 @@ export class AgentManager {
}),
{ resolveDefaultModel: false },
);
const importedEntries = projectImportedTimelineEntries(imported.timeline);
const importedEntries = projectImportedTimelineEntries(input.provider, imported.timeline);
const initialTitle = resolveImportedAgentTitle(importedConfig, importedEntries);
handedToRegistration = true;
@@ -3282,7 +3289,7 @@ export class AgentManager {
const write = this.timeline
.ingest(
scope,
{ item: event.item },
{ item: event.item, provider: event.provider },
{ publish: (commit) => this.publishTimelineEvent(agent.id, event, commit) },
)
.then((result) => this.reportUnpublished(agent.id, result));
@@ -3346,7 +3353,7 @@ export class AgentManager {
const event: AgentStreamEvent = { type: "timeline", item, provider: agent.provider };
const result = await this.timeline.ingest(
{ agentId },
{ item },
{ item, provider: agent.provider },
{ publish: (commit) => this.publishTimelineEvent(agentId, event, commit) },
);
if (result.kind === "dropped") {
@@ -4852,7 +4859,7 @@ export class AgentManager {
if (timelineInit.kind === "created") {
await this.timeline.append(
{ agentId: managed.id },
projectImportedTimelineEntries(session.initialTimeline),
projectImportedTimelineEntries(managed.provider, session.initialTimeline),
);
}
this.refreshSessionPersistence(managed);
@@ -5990,6 +5997,7 @@ export class AgentManager {
{ agentId },
{
item,
provider,
...(turnId !== undefined ? { turnId } : {}),
...(options?.providerMessageId ? { providerMessageId: options.providerMessageId } : {}),
},
@@ -1,10 +1,12 @@
import type { ProjectedTimelineRow, TimelineRetirement } from "./timeline-projection.js";
import type { AgentMetric, AgentTimelineItem } from "./agent-sdk-types.js";
import type { AgentMetric, AgentProvider, AgentTimelineItem } from "./agent-sdk-types.js";
export interface AgentTimelineRow {
seq: number;
timestamp: string;
item: AgentTimelineItem;
/** The provider that wrote the row; an agent's history can span providers. */
readonly provider?: AgentProvider;
readonly turnId?: string;
readonly providerMessageId?: string;
/** Metrics of the turn this row carries the footer for. */
@@ -95,7 +95,7 @@ export class ProviderSubagentStore {
if (event.type === "timeline") {
const commits = await this.timeline.recordChildEntries(scope, [
{ item: event.item, timestamp: event.timestamp },
{ item: event.item, provider, timestamp: event.timestamp },
]);
if (!commits) return null;
const [{ row, epoch }] = commits;
@@ -1,5 +1,10 @@
import { continuesTimelineTurn, isTimelineTurnTrigger } from "@getpaseo/protocol/timeline-turn";
import type { AgentMetric, AgentTimelineItem, ToolCallDetail } from "./agent-sdk-types.js";
import type {
AgentMetric,
AgentProvider,
AgentTimelineItem,
ToolCallDetail,
} from "./agent-sdk-types.js";
import { timelineItemIdentity } from "@getpaseo/protocol/timeline-identity";
import type { AgentTimelineRow } from "./agent-timeline-store-types.js";
@@ -20,6 +25,7 @@ export type TimelineLimitDirection = "tail" | "before" | "after";
export interface TimelineProjectionEntry {
item: AgentTimelineItem;
provider?: AgentProvider;
turnId?: string;
providerMessageId?: string;
turnMetrics?: AgentMetric[];
@@ -135,6 +141,7 @@ function makeCanonicalEntries(rows: readonly AgentTimelineRow[]): WorkingEntry[]
return {
item: row.item,
timestamp: row.timestamp,
...(row.provider ? { provider: row.provider } : {}),
...(row.turnId ? { turnId: row.turnId } : {}),
...(row.providerMessageId ? { providerMessageId: row.providerMessageId } : {}),
...(row.turnMetrics ? { turnMetrics: row.turnMetrics } : {}),
@@ -64,6 +64,24 @@ describe.each(
expect(published).toEqual([1, 2, 3]);
});
it("keeps the provider that wrote each row, and a merged row the one it started with", async () => {
const timeline = createTimeline();
await timeline.create(root("a"), { epoch: "e" });
await timeline.append(root("a"), [
{ item: { type: "user_message", text: "prompt" }, provider: "codex" },
{ item: { type: "assistant_message", text: "A" }, provider: "claude" },
{ item: { type: "assistant_message", text: "B" }, provider: "claude" },
{ item: { type: "user_message", text: "unattributed" } },
]);
const page = await timeline.fetch(root("a"), { direction: "tail", limit: 0 });
expect(page.rows.map((row) => [row.item.type, row.provider])).toEqual([
["user_message", "codex"],
["assistant_message", "claude"],
["user_message", undefined],
]);
});
it("publishes each commit before the next operation starts", async () => {
const timeline = createTimeline();
await timeline.create(root("a"), { epoch: "e" });
@@ -1,5 +1,6 @@
import type {
AgentMetric,
AgentProvider,
AgentTimelineItem,
ConversationRewindCut,
} from "../../agent-sdk-types.js";
@@ -19,6 +20,8 @@ export interface TimelineScope {
export interface TimelineEntry {
item: AgentTimelineItem;
/** The provider that wrote the entry. */
provider?: AgentProvider;
timestamp?: string;
turnId?: string;
providerMessageId?: string;
@@ -11,6 +11,7 @@ export function buildSourceRow(
seq,
timestamp: entry.timestamp ?? timestamp,
item: entry.item,
...(entry.provider ? { provider: entry.provider } : {}),
...(entry.turnId ? { turnId: entry.turnId } : {}),
...(entry.providerMessageId ? { providerMessageId: entry.providerMessageId } : {}),
...(entry.turnMetrics ? { turnMetrics: entry.turnMetrics } : {}),
+3 -2
View File
@@ -5608,7 +5608,7 @@ export class Session {
}
const event = serializeAgentStreamEvent({
type: "timeline",
provider,
provider: row.provider ?? provider,
item: row.item,
...(row.turnId ? { turnId: row.turnId } : {}),
timestamp: row.timestamp,
@@ -8775,7 +8775,8 @@ export class Session {
: {}),
entries: entries.map((entry) => {
const payloadEntry = {
provider: agentPayload.provider,
// A row keeps the provider that wrote it; older rows have none.
provider: entry.provider ?? agentPayload.provider,
item: entry.item,
timestamp: entry.timestamp,
seqStart: entry.seqStart,
@@ -458,6 +458,26 @@ describe("wire compatibility", () => {
expect(currentParsed.payload.entries[0]?.collapsed).toContain("reasoning_merge");
});
test("serves each entry with the provider that wrote it, the agent's when the row has none", async () => {
const response = await emitTimelineResponse({
rows: [
{
seq: 1,
timestamp: "2026-05-02T00:00:00.000Z",
provider: "claude",
item: { type: "user_message", text: "prompt" },
},
{
seq: 2,
timestamp: "2026-05-02T00:00:01.000Z",
item: { type: "assistant_message", text: "done" },
},
],
});
expect(response.payload.entries.map((entry) => entry.provider)).toEqual(["claude", "codex"]);
});
test("carries canonical turn IDs to new clients while legacy schemas ignore them", async () => {
const response = await emitTimelineResponse({
rows: [