Skip to content
Draft
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
24 changes: 18 additions & 6 deletions src/features/agents/activity-transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ export function activityTranscript(
return transcript;
}

// Each new record replaces the array, so a transcript is rebuilt from every
// retained record. Frozen records never change: parse each one once.
const unparsable = Symbol("unparsable");
const parsedPlaintext = new WeakMap<object, unknown>();
function parsed(record: Source) {
if (parsedPlaintext.has(record)) return parsedPlaintext.get(record);
let raw: unknown;
try {
raw = JSON.parse(record.plaintext);
} catch {
raw = unparsable;
}
if (Object.isFrozen(record)) parsedPlaintext.set(record, raw);
return raw;
}

function buildTranscript(
records: readonly Source[],
scope: TranscriptScope,
Expand All @@ -359,12 +375,8 @@ function buildTranscript(
const events: { id: string; at: number; order: number; event: Json }[] = [];
for (const record of records) {
if (record.agent !== scope.agent) continue;
let raw: unknown;
try {
raw = JSON.parse(record.plaintext);
} catch {
continue;
}
const raw = parsed(record);
if (raw === unparsable) continue;
const envelope = object(raw);
const children =
envelope?.kind === "batch" ? object(envelope.payload)?.events : [raw];
Expand Down
29 changes: 29 additions & 0 deletions src/features/agents/request-activity.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, it, vi } from "vitest";
import { activityTranscript } from "./activity-transcript";
import { requestActivity } from "./request-activity";

const agent = "a".repeat(64);
Expand Down Expand Up @@ -52,6 +53,34 @@ it("parses only newly appended records when the records array changes", () => {
}
});

it("lets transcripts of later selections reuse earlier record parses", () => {
const first = [
record(1, [
event("turn_started", "t", 1, { triggeringEventIds: [trigger] }),
event("step", "t", 2),
]),
];
const scope = { agent, channelId: "c" };
activityTranscript(
requestActivity(first, agent, "c", trigger).records,
scope,
);
const next = [...first, record(2, [event("step", "t", 3)])];
const parse = vi.spyOn(JSON, "parse");
try {
const { records } = requestActivity(next, agent, "c", trigger);
parse.mockClear();
const transcript = activityTranscript(records, scope);
// Only the newly selected projection is parsed.
expect(parse).toHaveBeenCalledTimes(1);
expect(transcript).toEqual(
activityTranscript(structuredClone(records), scope),
);
} finally {
parse.mockRestore();
}
});

it("gives a steered message the records of the turn it joined, from that turn's start", () => {
const steered = "d".repeat(64);
const records = [
Expand Down
5 changes: 4 additions & 1 deletion src/features/agents/request-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ function parseRecord(
value.turnId.length > 256
)
return [];
return [{ record: projected, value }];
// A frozen projection lets transcripts reuse its parse by identity.
return [
{ record: byScope ? Object.freeze(projected) : projected, value },
];
} catch {
return [];
}
Expand Down
26 changes: 23 additions & 3 deletions src/features/agents/request-work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ export function isSettledWork(entry: Pick<RequestWork, "state">): boolean {
return ["done", "ended", "superseded", "stopped"].includes(entry.state);
}

// Snapshot record arrays are frozen and replaced on change, and every mounted
// row asks about the same channels: index each array's agents once.
const recordAgents = new WeakMap<object, Map<string, string[]>>();
function channelAgents(records: Snapshot["records"], channelId: string) {
if (!Object.isFrozen(records))
return records
.filter((record) => record.channelIds.includes(channelId))
.map((record) => record.agent);
let byChannel = recordAgents.get(records);
if (!byChannel) {
byChannel = new Map();
for (const record of records)
for (const id of record.channelIds) {
const agents = byChannel.get(id);
if (!agents) byChannel.set(id, [record.agent]);
else if (!agents.includes(record.agent)) agents.push(record.agent);
}
recordAgents.set(records, byChannel);
}
return byChannel.get(channelId) ?? [];
}

/** Only explicit triggering IDs associate work with a message. Typing is not
* message evidence, and a sibling request must never inherit the same bubble. */
export function requestWork(
Expand All @@ -39,9 +61,7 @@ export function requestWork(
(request) => request.channelId === channelId && ids.has(request.messageId),
);
const agents = new Set([
...records
.filter((record) => record.channelIds.includes(channelId))
.map((record) => record.agent),
...channelAgents(records, channelId),
...requests.map((request) => request.agent),
...snapshot.turns
.filter(
Expand Down
Loading