A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, performance] Cache the branch array on the per-turn hot path (#2414)
Seth Karten0498deb439b4
diff --git a/packages/coding-agent/.changes/perf-branch-cache.md b/packages/coding-agent/.changes/perf-branch-cache.mdnew file mode 100644index 0000000000..31bc5dd965--- /dev/null+++ b/packages/coding-agent/.changes/perf-branch-cache.md@@ -0,0 +1,2 @@+- Cached the active branch path in the session manager, so per-turn compaction checks and context-usage updates no longer rebuild the whole leaf-to-root path after every assistant message.+- `SessionManager.getBranch()` now returns the live shared branch array (public API): repeated reads return the same array object and straight-line appends extend it in place. Callers must treat it as read-only and take `.slice()` for a snapshot; `session_before_compact` already passes a snapshot.diff --git a/packages/coding-agent/docs/extensions.md b/packages/coding-agent/docs/extensions.mdindex fd6e61d380..0491b984b8 100644--- a/packages/coding-agent/docs/extensions.md+++ b/packages/coding-agent/docs/extensions.md@@ -411,7 +411,7 @@ Do cleanup work in `session_shutdown`, then reestablish any in-memory state in `#### session_before_compact / session_compact-Fired on compaction. See [compaction.md](compaction.md) for details.+Fired on compaction. See [compaction.md](compaction.md) for details. `branchEntries` is a read-only snapshot of the active branch taken before compaction, so it does not include entries appended while your handler runs and must not be mutated.```typescriptpi.on("session_before_compact", async (event, ctx) => {diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.tsindex 8a67c7c550..c23dd977aa 100644--- a/packages/coding-agent/src/core/agent-session.ts+++ b/packages/coding-agent/src/core/agent-session.ts@@ -8732,7 +8732,9 @@ export class AgentSession {const result = (await this._extensionRunner.emit({type: "session_before_compact",preparation,- branchEntries: pathEntries,+ // slice: getBranch() returns the live leaf-branch cache, which appends+ // extend in place, so the awaited handler gets a snapshot.+ branchEntries: pathEntries.slice(),customInstructions,signal,})) as SessionBeforeCompactResult | undefined;diff --git a/packages/coding-agent/src/core/session-manager.ts b/packages/coding-agent/src/core/session-manager.tsindex e04815427b..e1220af2a5 100644--- a/packages/coding-agent/src/core/session-manager.ts+++ b/packages/coding-agent/src/core/session-manager.ts@@ -1749,6 +1749,12 @@ export class SessionManager {private labelsById: Map<string, string> = new Map();private labelTimestampsById: Map<string, string> = new Map();private leafId: string | null = null;+ // Cache of the live leaf's branch path, so the per-turn compaction check and the+ // context-usage state build get an O(1) lookup instead of a leaf-to-root walk.+ // Appends extend the cached array in place; every other leafId write drops the+ // cache. The array is handed out as-is, so callers must treat it as read-only+ // and must not hold it across an append.+ private leafBranchCache: { leafId: string | null; entries: SessionEntry[] } | null = null;private persistListeners = new Set<SessionPersistListener>();private constructor(@@ -1865,6 +1871,7 @@ export class SessionManager {this.labelsById.clear();this.labelTimestampsById.clear();this.leafId = null;+ this.leafBranchCache = null;this.flushed = false;if (this.persist) {@@ -1885,6 +1892,7 @@ export class SessionManager {this.labelsById.clear();this.labelTimestampsById.clear();this.leafId = null;+ this.leafBranchCache = null;for (const entry of this.fileEntries) {if (entry.type === "session") continue;this.byId.set(entry.id, entry);@@ -2047,6 +2055,21 @@ export class SessionManager {this.byId.set(entry.id, entry);this.leafId = entry.id;this._persist(entry);+ // After _persist: a throwing persist rolls the append back, and the cache must+ // not grow past a leaf that never survived it.+ this._extendLeafBranchCache(entry);+ }++ // Appends only ever extend the live branch path, so the leaf branch cache grows+ // in place. Anything other than a straight append off the cached leaf drops it.+ private _extendLeafBranchCache(entry: SessionEntry): void {+ const cache = this.leafBranchCache;+ if (cache && cache.leafId === entry.parentId) {+ cache.leafId = entry.id;+ cache.entries.push(entry);+ } else {+ this.leafBranchCache = null;+ }}appendMessage(message: Message | CustomMessage | BashExecutionMessage): string {@@ -2333,10 +2356,18 @@ export class SessionManager {} catch (error) {// The append indexes the entry before persisting it; undo exactly that.if (this.leafId !== null && this.leafId !== previousLeafId) {- this.byId.delete(this.leafId);+ const rolledBackId = this.leafId;+ this.byId.delete(rolledBackId);this.fileEntries.pop();this._refreshHasAssistantEntry();+ // A cache extended by the append is live, so drop the rolled-back entry from+ // the array a caller may already hold before invalidating the cache.+ const cache = this.leafBranchCache;+ if (cache && cache.entries[cache.entries.length - 1]?.id === rolledBackId) {+ cache.entries.pop();+ }this.leafId = previousLeafId;+ this.leafBranchCache = null;// The failed append may have left a torn line on disk. Restore the file// from the rolled-back entries now; if that also fails (e.g. the disk is// still full), fall back to forcing the next persist to rewrite.@@ -2400,7 +2431,18 @@ export class SessionManager {return entry.id;}+ /**+ * Entries on the active branch, root to leaf. A leaf read returns the live+ * leaf-branch cache: treat the result as read-only and do not hold it across+ * appends.+ */getBranch(fromId?: string): SessionEntry[] {+ // Leaf-path reads are the per-turn hot path (compaction checks, context+ // usage); a read for another entry walks the chain instead.+ const isLeafPath = fromId === undefined || fromId === this.leafId;+ if (isLeafPath && this.leafBranchCache?.leafId === this.leafId) {+ return this.leafBranchCache.entries;+ }// push+reverse, not unshift-per-entry: unshift is O(n), which makes this O(n^2) on long sessions.const path: SessionEntry[] = [];const startId = fromId ?? this.leafId;@@ -2410,6 +2452,9 @@ export class SessionManager {current = current.parentId ? this.byId.get(current.parentId) : undefined;}path.reverse();+ if (isLeafPath) {+ this.leafBranchCache = { leafId: this.leafId, entries: path };+ }return path;}@@ -2480,10 +2525,12 @@ export class SessionManager {throw new Error(`Entry ${branchFromId} not found`);}this.leafId = branchFromId;+ this.leafBranchCache = null;}resetLeaf(): void {this.leafId = null;+ this.leafBranchCache = null;}branchWithSummary(@@ -2497,6 +2544,7 @@ export class SessionManager {throw new Error(`Entry ${branchFromId} not found`);}this.leafId = branchFromId;+ this.leafBranchCache = null;const entry: BranchSummaryEntry = {type: "branch_summary",id: generateId(this.byId),diff --git a/packages/coding-agent/test/session-manager/leaf-branch-cache.test.ts b/packages/coding-agent/test/session-manager/leaf-branch-cache.test.tsnew file mode 100644index 0000000000..4163038a8c--- /dev/null+++ b/packages/coding-agent/test/session-manager/leaf-branch-cache.test.ts@@ -0,0 +1,158 @@+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it, vi } from "vitest";+import { SessionManager } from "../../src/core/session-manager.js";+import { assistantMsg, userMsg } from "../utilities.js";++// SessionManager caches the live leaf's branch path (getBranch() reads) because+// agent-session re-reads it on every assistant message end. These tests pin the+// cache's contract: identical reads share the array, appends extend it, every+// other leaf move drops it, and non-leaf reads never poison it.+describe("SessionManager leaf branch cache", () => {+ const tempDirs: string[] = [];++ afterEach(() => {+ while (tempDirs.length > 0) {+ rmSync(tempDirs.pop()!, { recursive: true, force: true });+ }+ });++ function persistedSession(): SessionManager {+ const dir = mkdtempSync(join(tmpdir(), "pi-leaf-branch-cache-"));+ tempDirs.push(dir);+ return SessionManager.create(dir, join(dir, "sessions"));+ }++ const ids = (session: SessionManager) => session.getBranch().map((entry) => entry.id);++ // Walks the served path and checks it against the byId parent chain, so a+ // stale or half-extended cache cannot pass by returning the right ids only.+ function expectChainMatches(session: SessionManager, expected: string[]): void {+ const branch = session.getBranch();+ expect(branch.map((entry) => entry.id)).toEqual(expected);+ for (let index = 1; index < branch.length; index++) {+ expect(branch[index]!.parentId).toBe(branch[index - 1]!.id);+ expect(session.getEntry(branch[index]!.id)).toBe(branch[index]);+ }+ }++ function seedTwo() {+ const session = SessionManager.inMemory();+ const firstId = session.appendMessage(userMsg("one"));+ const secondId = session.appendMessage(assistantMsg("two"));+ return { session, firstId, secondId };+ }++ it("serves one shared cached array that appends extend in place", () => {+ const { session, firstId, secondId } = seedTwo();+ const first = session.getBranch();+ expect(session.getBranch()).toBe(first); // repeated leaf reads share the array+ expect(first.map((entry) => entry.id)).toEqual([firstId, secondId]);++ const thirdId = session.appendCustomMessageEntry("note", "hello", false);+ expectChainMatches(session, [firstId, secondId, thirdId]);+ // An append extends the same array the earlier read handed out.+ expect(session.getBranch()).toBe(first);++ const fourthId = session.appendMessage(userMsg("four"));+ expectChainMatches(session, [firstId, secondId, thirdId, fourthId]);+ });++ it("drops the cache on every leaf-changing operation and re-caches the new path", () => {+ const { session, firstId, secondId } = seedTwo();+ expect(ids(session)).toEqual([firstId, secondId]); // populates the cache++ // branch(): the abandoned path must not leak, and a sibling append re-roots.+ session.branch(firstId);+ expectChainMatches(session, [firstId]);+ const siblingId = session.appendMessage(userMsg("sibling"));+ expectChainMatches(session, [firstId, siblingId]);+ // Branching back to the abandoned entry must not resurrect the old cache.+ session.branch(secondId);+ expectChainMatches(session, [firstId, secondId]);++ // resetLeaf(): an empty branch, then the next append re-roots the tree.+ session.resetLeaf();+ expect(session.getLeafId()).toBeNull();+ expect(session.getBranch()).toEqual([]);+ const rootId = session.appendMessage(userMsg("root again"));+ expect(session.getEntry(rootId)?.parentId).toBeNull();+ expectChainMatches(session, [rootId]);++ // branchWithSummary(): the summary entry becomes the new leaf, and the+ // compaction appended after it lands on the summarized branch.+ const summaryId = session.branchWithSummary(secondId, "summary of the abandoned path");+ expectChainMatches(session, [firstId, secondId, summaryId]);+ const compactionId = session.appendCompaction("compaction summary", rootId, 1234, {+ readFiles: [],+ modifiedFiles: [],+ });+ expectChainMatches(session, [firstId, secondId, summaryId, compactionId]);+ });++ it("returns the correct suffix for a mid-branch read without poisoning the leaf cache", () => {+ const session = SessionManager.inMemory();+ const firstId = session.appendMessage(userMsg("one"));+ const secondId = session.appendMessage(assistantMsg("two"));+ const thirdId = session.appendMessage(userMsg("three"));+ const fourthId = session.appendMessage(assistantMsg("four"));++ const leafPath = session.getBranch();+ expect(ids(session)).toEqual([firstId, secondId, thirdId, fourthId]);++ const suffix = session.getBranch(secondId);+ expect(suffix.map((entry) => entry.id)).toEqual([firstId, secondId]);+ expect(suffix).not.toBe(leafPath);++ // The mid-branch read must not have replaced the cached leaf path...+ expect(session.getBranch()).toBe(leafPath);+ // ...nor cached a suffix as if it were the leaf path.+ const fifthId = session.appendMessage(userMsg("five"));+ expectChainMatches(session, [firstId, secondId, thirdId, fourthId, fifthId]);+ });++ it("drops the cached branch when a failed append is rolled back", () => {+ const session = persistedSession();+ const firstId = session.appendMessage(userMsg("one"));+ expect(ids(session)).toEqual([firstId]); // populates the cache++ vi.spyOn(session, "_persist").mockImplementationOnce(() => {+ throw new Error("disk full");+ });+ expect(() => session.appendCustomMessageEntryWithRollback("note", "unsaved", false)).toThrow("disk full");++ expect(session.getLeafId()).toBe(firstId);+ expectChainMatches(session, [firstId]);+ // The next append starts from the rolled-back leaf and stays coherent.+ const secondId = session.appendCustomMessageEntry("note", "saved", false);+ expectChainMatches(session, [firstId, secondId]);+ });++ it("never leaves a rolled-back append in a branch array a caller already holds", () => {+ const session = persistedSession();+ const firstId = session.appendMessage(userMsg("one"));+ const held = session.getBranch(); // the live cached array++ // The append persists, so it reaches the cached array in place...+ vi.spyOn(session, "flushNow").mockImplementationOnce(() => {+ throw new Error("flush failed");+ });+ expect(() => session.appendCustomMessageEntryWithRollback("note", "unsaved", false)).toThrow("flush failed");++ // ...and the rollback takes it back out of that same array.+ expect(held.map((entry) => entry.id)).toEqual([firstId]);+ expect(session.getLeafId()).toBe(firstId);+ expectChainMatches(session, [firstId]);++ // A failing persist never extends the cache in the first place.+ const heldAgain = session.getBranch();+ vi.spyOn(session, "_persist").mockImplementationOnce(() => {+ throw new Error("disk full");+ });+ expect(() => session.appendCustomMessageEntryWithRollback("note", "unsaved again", false)).toThrow("disk full");++ expect(heldAgain.map((entry) => entry.id)).toEqual([firstId]);+ expectChainMatches(session, [firstId]);+ });+});diff --git a/packages/coding-agent/test/session-manager/tree-traversal.test.ts b/packages/coding-agent/test/session-manager/tree-traversal.test.tsindex f7a2d68524..d3a861a797 100644--- a/packages/coding-agent/test/session-manager/tree-traversal.test.ts+++ b/packages/coding-agent/test/session-manager/tree-traversal.test.ts@@ -2,156 +2,87 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSyncimport { tmpdir } from "os";import { join } from "path";import { describe, expect, it } from "vitest";-import { type CustomEntry, loadEntriesFromFile, SessionManager } from "../../src/core/session-manager.js";+import { loadEntriesFromFile, type SessionEntry, SessionManager } from "../../src/core/session-manager.js";import { assistantMsg, userMsg } from "../utilities.js";describe("SessionManager append and tree traversal", () => {describe("append operations", () => {- it("appendMessage creates entry with correct parentId chain", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("first"));- const id2 = session.appendMessage(assistantMsg("second"));- const id3 = session.appendMessage(userMsg("third"));-- const entries = session.getEntries();- expect(entries).toHaveLength(3);-- expect(entries[0].id).toBe(id1);- expect(entries[0].parentId).toBeNull();- expect(entries[0].type).toBe("message");-- expect(entries[1].id).toBe(id2);- expect(entries[1].parentId).toBe(id1);-- expect(entries[2].id).toBe(id3);- expect(entries[2].parentId).toBe(id2);- });-- it("appendThinkingLevelChange integrates into tree", () => {- const session = SessionManager.inMemory();-- const msgId = session.appendMessage(userMsg("hello"));- const thinkingId = session.appendThinkingLevelChange("high");- const _msg2Id = session.appendMessage(assistantMsg("response"));-- const entries = session.getEntries();- expect(entries).toHaveLength(3);-- const thinkingEntry = entries.find((e) => e.type === "thinking_level_change");- expect(thinkingEntry).toBeDefined();- expect(thinkingEntry!.id).toBe(thinkingId);- expect(thinkingEntry!.parentId).toBe(msgId);-- expect(entries[2].parentId).toBe(thinkingId);- });-- it("appendModelChange integrates into tree", () => {- const session = SessionManager.inMemory();-- const msgId = session.appendMessage(userMsg("hello"));- const modelId = session.appendModelChange("openai", "gpt-4");- const _msg2Id = session.appendMessage(assistantMsg("response"));-- const entries = session.getEntries();- const modelEntry = entries.find((e) => e.type === "model_change");- expect(modelEntry).toBeDefined();- expect(modelEntry?.id).toBe(modelId);- expect(modelEntry?.parentId).toBe(msgId);- if (modelEntry?.type === "model_change") {- expect(modelEntry.provider).toBe("openai");- expect(modelEntry.modelId).toBe("gpt-4");- }-- expect(entries[2].parentId).toBe(modelId);- });-- it("appendCompaction integrates into tree", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const id2 = session.appendMessage(assistantMsg("2"));- const compactionId = session.appendCompaction("summary", id1, 1000);- const _id3 = session.appendMessage(userMsg("3"));-- const entries = session.getEntries();- const compactionEntry = entries.find((e) => e.type === "compaction");- expect(compactionEntry).toBeDefined();- expect(compactionEntry?.id).toBe(compactionId);- expect(compactionEntry?.parentId).toBe(id2);- if (compactionEntry?.type === "compaction") {- expect(compactionEntry.summary).toBe("summary");- expect(compactionEntry.firstKeptEntryId).toBe(id1);- expect(compactionEntry.tokensBefore).toBe(1000);- }-- expect(entries[3].parentId).toBe(compactionId);- });-- it("appendCompaction persists customInstructions", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- session.appendMessage(assistantMsg("2"));- session.appendCompaction("summary", id1, 1000, undefined, undefined, "focus on xyz");-- const compactionEntry = session.getEntries().find((e) => e.type === "compaction");- if (compactionEntry?.type === "compaction") {- expect(compactionEntry.customInstructions).toBe("focus on xyz");- } else {- expect.fail("compaction entry not found");- }- });-- it("appendCustomEntry integrates into tree", () => {+ const cases: Array<{+ name: string;+ seed: (session: SessionManager) => string[];+ append: (session: SessionManager) => string;+ type: string;+ expectEntry?: (entry: SessionEntry, seedIds: string[]) => void;+ }> = [+ {+ name: "appendMessage creates entry with correct parentId chain",+ seed: (session) => [session.appendMessage(userMsg("first"))],+ append: (session) => session.appendMessage(assistantMsg("second")),+ type: "message",+ },+ {+ name: "appendThinkingLevelChange integrates into tree",+ seed: (session) => [session.appendMessage(userMsg("hello"))],+ append: (session) => session.appendThinkingLevelChange("high"),+ type: "thinking_level_change",+ expectEntry: (entry) => expect(entry).toMatchObject({ thinkingLevel: "high" }),+ },+ {+ name: "appendModelChange integrates into tree",+ seed: (session) => [session.appendMessage(userMsg("hello"))],+ append: (session) => session.appendModelChange("openai", "gpt-4"),+ type: "model_change",+ expectEntry: (entry) => expect(entry).toMatchObject({ provider: "openai", modelId: "gpt-4" }),+ },+ {+ name: "appendCompaction integrates into tree and persists customInstructions",+ seed: (session) => {+ const firstId = session.appendMessage(userMsg("1"));+ return [firstId, session.appendMessage(assistantMsg("2"))];+ },+ append: (session) =>+ session.appendCompaction(+ "summary",+ session.getEntries()[0]!.id,+ 1000,+ undefined,+ undefined,+ "focus on xyz",+ ),+ type: "compaction",+ expectEntry: (entry, seedIds) =>+ expect(entry).toMatchObject({+ summary: "summary",+ firstKeptEntryId: seedIds[0],+ tokensBefore: 1000,+ customInstructions: "focus on xyz",+ }),+ },+ {+ name: "appendCustomEntry integrates into tree",+ seed: (session) => [session.appendMessage(userMsg("hello"))],+ append: (session) => session.appendCustomEntry("my_data", { key: "value" }),+ type: "custom",+ expectEntry: (entry) => expect(entry).toMatchObject({ customType: "my_data", data: { key: "value" } }),+ },+ ];+ it.each(cases)("$name", ({ seed, append, type, expectEntry }) => {const session = SessionManager.inMemory();-- const msgId = session.appendMessage(userMsg("hello"));- const customId = session.appendCustomEntry("my_data", { key: "value" });- const _msg2Id = session.appendMessage(assistantMsg("response"));+ const seedIds = seed(session);+ const appendedId = append(session);+ const followingId = session.appendMessage(userMsg("after"));const entries = session.getEntries();- const customEntry = entries.find((e) => e.type === "custom") as CustomEntry;- expect(customEntry).toBeDefined();- expect(customEntry.id).toBe(customId);- expect(customEntry.parentId).toBe(msgId);- expect(customEntry.customType).toBe("my_data");- expect(customEntry.data).toEqual({ key: "value" });-- expect(entries[2].parentId).toBe(customId);- });-- it("leaf pointer advances after each append", () => {- const session = SessionManager.inMemory();-- expect(session.getLeafId()).toBeNull();-- const id1 = session.appendMessage(userMsg("1"));- expect(session.getLeafId()).toBe(id1);-- const id2 = session.appendMessage(assistantMsg("2"));- expect(session.getLeafId()).toBe(id2);-- const id3 = session.appendThinkingLevelChange("high");- expect(session.getLeafId()).toBe(id3);+ const appended = entries.find((e) => e.id === appendedId)!;+ expect(appended.type).toBe(type);+ expect(appended.parentId).toBe(seedIds.at(-1) ?? null);+ expectEntry?.(appended, seedIds);+ // The entry after the appended one parents to it, pinning the chain.+ expect(entries.find((e) => e.id === followingId)?.parentId).toBe(appendedId);});});describe("getPath", () => {- it("returns empty array for empty session", () => {- const session = SessionManager.inMemory();- expect(session.getBranch()).toEqual([]);- });-- it("returns single entry path", () => {- const session = SessionManager.inMemory();- const id = session.appendMessage(userMsg("hello"));-- const path = session.getBranch();- expect(path).toHaveLength(1);- expect(path[0].id).toBe(id);- });-it("returns full path from root to leaf", () => {const session = SessionManager.inMemory();@@ -164,19 +95,6 @@ describe("SessionManager append and tree traversal", () => {expect(path).toHaveLength(4);expect(path.map((e) => e.id)).toEqual([id1, id2, id3, id4]);});-- it("returns path from specified entry to root", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const id2 = session.appendMessage(assistantMsg("2"));- const _id3 = session.appendMessage(userMsg("3"));- const _id4 = session.appendMessage(assistantMsg("4"));-- const path = session.getBranch(id2);- expect(path).toHaveLength(2);- expect(path.map((e) => e.id)).toEqual([id1, id2]);- });});describe("getTree", () => {@@ -185,25 +103,6 @@ describe("SessionManager append and tree traversal", () => {expect(session.getTree()).toEqual([]);});- it("returns single root for linear session", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const id2 = session.appendMessage(assistantMsg("2"));- const id3 = session.appendMessage(userMsg("3"));-- const tree = session.getTree();- expect(tree).toHaveLength(1);-- const root = tree[0];- expect(root.entry.id).toBe(id1);- expect(root.children).toHaveLength(1);- expect(root.children[0].entry.id).toBe(id2);- expect(root.children[0].children).toHaveLength(1);- expect(root.children[0].children[0].entry.id).toBe(id3);- expect(root.children[0].children[0].children).toHaveLength(0);- });-it("returns tree with branches after branch", () => {const session = SessionManager.inMemory();@@ -213,6 +112,8 @@ describe("SessionManager append and tree traversal", () => {session.branch(id2);const id4 = session.appendMessage(userMsg("4-branch"));+ session.branch(id2);+ const id5 = session.appendMessage(userMsg("5-branch"));const tree = session.getTree();expect(tree).toHaveLength(1);@@ -223,34 +124,10 @@ describe("SessionManager append and tree traversal", () => {const node2 = root.children[0];expect(node2.entry.id).toBe(id2);- expect(node2.children).toHaveLength(2); // id3 and id4 are siblings+ expect(node2.children).toHaveLength(3); // id3, id4, and id5 are siblingsconst childIds = node2.children.map((c) => c.entry.id).sort();- expect(childIds).toEqual([id3, id4].sort());- });-- it("handles multiple branches at same point", () => {- const session = SessionManager.inMemory();-- const _id1 = session.appendMessage(userMsg("root"));- const id2 = session.appendMessage(assistantMsg("response"));-- session.branch(id2);- const idA = session.appendMessage(userMsg("branch-A"));-- session.branch(id2);- const idB = session.appendMessage(userMsg("branch-B"));-- session.branch(id2);- const idC = session.appendMessage(userMsg("branch-C"));-- const tree = session.getTree();- const node2 = tree[0].children[0];- expect(node2.entry.id).toBe(id2);- expect(node2.children).toHaveLength(3);-- const branchIds = node2.children.map((c) => c.entry.id).sort();- expect(branchIds).toEqual([idA, idB, idC].sort());+ expect(childIds).toEqual([id3, id4, id5].sort());});it("handles deep branching", () => {@@ -284,62 +161,15 @@ describe("SessionManager append and tree traversal", () => {});describe("branch", () => {- it("moves leaf pointer to specified entry", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const _id2 = session.appendMessage(assistantMsg("2"));- const id3 = session.appendMessage(userMsg("3"));-- expect(session.getLeafId()).toBe(id3);-- session.branch(id1);- expect(session.getLeafId()).toBe(id1);- });-it("throws for non-existent entry", () => {const session = SessionManager.inMemory();session.appendMessage(userMsg("hello"));expect(() => session.branch("nonexistent")).toThrow("Entry nonexistent not found");});-- it("new appends become children of branch point", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const _id2 = session.appendMessage(assistantMsg("2"));-- session.branch(id1);- const id3 = session.appendMessage(userMsg("branched"));-- const entries = session.getEntries();- const branchedEntry = entries.find((e) => e.id === id3)!;- expect(branchedEntry.parentId).toBe(id1); // sibling of id2- });});describe("branchWithSummary", () => {- it("inserts branch summary and advances leaf", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const _id2 = session.appendMessage(assistantMsg("2"));- const _id3 = session.appendMessage(userMsg("3"));-- const summaryId = session.branchWithSummary(id1, "Summary of abandoned work");-- expect(session.getLeafId()).toBe(summaryId);-- const entries = session.getEntries();- const summaryEntry = entries.find((e) => e.type === "branch_summary");- expect(summaryEntry).toBeDefined();- expect(summaryEntry?.parentId).toBe(id1);- if (summaryEntry?.type === "branch_summary") {- expect(summaryEntry.summary).toBe("Summary of abandoned work");- }- });-it("throws for non-existent entry", () => {const session = SessionManager.inMemory();session.appendMessage(userMsg("hello"));@@ -371,26 +201,6 @@ describe("SessionManager append and tree traversal", () => {const session = SessionManager.inMemory();expect(session.getEntry("nonexistent")).toBeUndefined();});-- it("returns entry by id", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("first"));- const id2 = session.appendMessage(assistantMsg("second"));-- const entry1 = session.getEntry(id1);- expect(entry1).toBeDefined();- expect(entry1?.type).toBe("message");- if (entry1?.type === "message" && entry1.message.role === "user") {- expect(entry1.message.content).toBe("first");- }-- const entry2 = session.getEntry(id2);- expect(entry2).toBeDefined();- if (entry2?.type === "message" && entry2.message.role === "assistant") {- expect((entry2.message.content as any)[0].text).toBe("second");- }- });});describe("buildSessionContext with branches", () => {@@ -423,28 +233,6 @@ describe("createBranchedSession", () => {expect(() => session.createBranchedSession("nonexistent")).toThrow("Entry nonexistent not found");});- it("creates new session with path to specified leaf (in-memory)", () => {- const session = SessionManager.inMemory();-- const id1 = session.appendMessage(userMsg("1"));- const id2 = session.appendMessage(assistantMsg("2"));- const id3 = session.appendMessage(userMsg("3"));- session.appendMessage(assistantMsg("4"));-- // Branch from 3: 3 -> 5- session.branch(id3);- const _id5 = session.appendMessage(userMsg("5"));-- // Create branched session from id2 (should only have 1 -> 2)- const result = session.createBranchedSession(id2);- expect(result).toBeUndefined(); // in-memory returns null-- const entries = session.getEntries();- expect(entries).toHaveLength(2);- expect(entries[0].id).toBe(id1);- expect(entries[1].id).toBe(id2);- });-it("extracts correct path from branched tree", () => {const session = SessionManager.inMemory();@@ -456,8 +244,8 @@ describe("createBranchedSession", () => {const id4 = session.appendMessage(userMsg("4"));const id5 = session.appendMessage(assistantMsg("5"));- // Create branched session from id5 (should have 1 -> 2 -> 4 -> 5)- session.createBranchedSession(id5);+ // In-memory branching rebuilds this session (no new file): the entries become the path to id5.+ expect(session.createBranchedSession(id5)).toBeUndefined();const entries = session.getEntries();expect(entries).toHaveLength(4);diff --git a/packages/coding-agent/test/suite/agent-session-compaction.test.ts b/packages/coding-agent/test/suite/agent-session-compaction.test.tsindex 119a41a3bb..c2f5073b15 100644--- a/packages/coding-agent/test/suite/agent-session-compaction.test.ts+++ b/packages/coding-agent/test/suite/agent-session-compaction.test.ts@@ -17,7 +17,7 @@ import type { AgentSession } from "../../src/core/agent-session.js";import type { ExtensionFactory } from "../../src/core/extensions/types.js";import { convertToLlm } from "../../src/core/messages.js";import { getLocalHarnessStateDir, loadHarnessState, saveHarnessState } from "../../src/core/refinement/index.js";-import { SessionManager } from "../../src/core/session-manager.js";+import { type SessionEntry, SessionManager } from "../../src/core/session-manager.js";import { assistantMsg, userMsg } from "../utilities.js";import { createHarness, getMessageText, type Harness, type HarnessOptions } from "./harness.js";import { createDeferred } from "./scheduling.js";@@ -218,6 +218,55 @@ describe("AgentSession compaction", () => {expect(harness.session.messages[0]?.role).toBe("compactionSummary");});+ it("hands session_before_compact a branch snapshot that later appends do not change", async () => {+ let capturedBranchEntries: SessionEntry[] | undefined;+ let capturedLength = -1;+ let appendedEntryId: string | undefined;+ let harness: Harness;+ harness = await createHarness({+ settings: { compaction: { keepRecentTokens: 1 } },+ extensionFactories: [+ (pi) => {+ pi.on("session_before_compact", async (event) => {+ // getBranch() serves the live leaf-branch cache, so the event must+ // carry a snapshot: an append while this handler is awaited must+ // not show up in the branch the extension received.+ capturedBranchEntries = event.branchEntries;+ capturedLength = event.branchEntries.length;+ appendedEntryId = harness.sessionManager.appendCustomMessageEntry(+ "branch_snapshot_probe",+ "appended during compaction",+ false,+ );+ return {+ compaction: {+ summary: "summary from extension",+ firstKeptEntryId: event.preparation.firstKeptEntryId,+ tokensBefore: event.preparation.tokensBefore,+ details: { source: "extension" },+ },+ };+ });+ },+ ],+ });+ harnesses.push(harness);++ await harness.session.prompt("one");+ await harness.session.prompt("two");+ await harness.session.compact();++ expect(appendedEntryId).toBeDefined();+ // The snapshot never grew in place when the append landed...+ expect(capturedBranchEntries).toHaveLength(capturedLength);+ expect(capturedBranchEntries?.map((entry) => entry.id)).not.toContain(appendedEntryId);+ // ...while the live branch did grow: the probe entry and the compaction are on it.+ const liveBranch = harness.sessionManager.getBranch();+ expect(liveBranch).not.toBe(capturedBranchEntries);+ expect(liveBranch.map((entry) => entry.id)).toContain(appendedEntryId);+ expect(liveBranch.length).toBeGreaterThan(capturedBranchEntries!.length);+ });+it("compacts through the model summarizer, persists metadata, emits events, and remains usable", async () => {const harness = await createHarness({settings: { compaction: { keepRecentTokens: 1 } },