From 624b808da0f2f8bbad8a4fbbcca3f82f24ecfc47 Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Sat, 30 May 2026 23:14:55 +0900 Subject: feat(chunks): chunk-native frontend store with turn-sealed reconcile + per-chunk eviction Replace the stored ChatMessage[] with a chunk-native model: tab.chunks (sealed ChunkRow[]) + tab.live (transient in-flight turn buffer) + derived tab.renderGroups. This enables per-chunk eviction (trimming WITHIN a large turn) and raw-chunk pagination (loadOlderChunks), removing the whole-message eviction limitation. Backend: - Emit turn-start/turn-sealed around each turn; expose currentTurnId in the status snapshot. turn-sealed fires after the durable write (status:idle fires before it). - New GET /tabs/:id/chunks raw paginated endpoint (limit/before). - Wrap appendChunks in a single SQLite transaction. Frontend: - turn-sealed drives a turn-aware reconcile that folds the sealed turn into chunks while preserving a concurrent newer in-flight turn and pending queued messages; deferred while the user is scrolled up. - Stable turn-scoped render keys (${turnId}:${role}:${n}) avoid remount/flash. Reconcile correctness (three review passes): - preserve a concurrent newer turn when an earlier deferred reconcile flushes; - keep optimistic queued user messages (no loss); - turn-start backfill skips pending queued rows and tags only the turn initiator; - bind consumed interrupt messages to the in-flight turn so they collapse on seal (no lingering/duplicated bubble). Tests: chat-store reconcile/eviction/pagination suite; api chunks endpoint + events. --- packages/api/tests/agent-manager.test.ts | 32 ++++++++++++++++++++++++++++++-- packages/api/tests/routes.test.ts | 13 +++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'packages/api/tests') diff --git a/packages/api/tests/agent-manager.test.ts b/packages/api/tests/agent-manager.test.ts index 6b016db..4415bbb 100644 --- a/packages/api/tests/agent-manager.test.ts +++ b/packages/api/tests/agent-manager.test.ts @@ -340,15 +340,43 @@ describe("AgentManager", () => { await manager.processMessage("tab-1", "test"); expect(events.length).toBeGreaterThan(0); - expect(events[0]).toMatchObject({ type: "status", status: "running" }); + // A turn now opens with `turn-start`, immediately followed by the + // agent's `status: running`. + expect(events[0]).toMatchObject({ type: "turn-start" }); + expect(events[1]).toMatchObject({ type: "status", status: "running" }); + // A turn now closes with `turn-sealed` (emitted after the DB write, which + // is after the agent's final `status: idle`). const lastEvent = events[events.length - 1]; - expect(lastEvent).toMatchObject({ type: "status", status: "idle" }); + expect(lastEvent).toMatchObject({ type: "turn-sealed" }); + expect(events.some((e) => e.type === "status" && e.status === "idle")).toBe(true); const doneEvent = events.find((e) => e.type === "done"); expect(doneEvent).toBeDefined(); }); + it("emits a turn-start with a turnId before any content event", async () => { + const manager = new AgentManager(); + const events: AgentEvent[] = []; + manager.onEvent((event) => { + events.push(event); + }); + + await manager.processMessage("tab-turnstart", "go"); + + const turnStartIdx = events.findIndex((e) => e.type === "turn-start"); + expect(turnStartIdx).toBeGreaterThanOrEqual(0); + const turnStart = events[turnStartIdx] as Extract; + expect(typeof turnStart.turnId).toBe("string"); + expect(turnStart.turnId.length).toBeGreaterThan(0); + + // Must precede the first content delta. + const firstContentIdx = events.findIndex( + (e) => e.type === "text-delta" || e.type === "reasoning-delta", + ); + expect(firstContentIdx).toBeGreaterThan(turnStartIdx); + }); + it("emits text-delta events during processMessage", async () => { const manager = new AgentManager(); const events: AgentEvent[] = []; diff --git a/packages/api/tests/routes.test.ts b/packages/api/tests/routes.test.ts index f4de845..4b8dd40 100644 --- a/packages/api/tests/routes.test.ts +++ b/packages/api/tests/routes.test.ts @@ -335,6 +335,19 @@ describe("POST /chat", () => { }); }); +describe("GET /tabs/:id/chunks", () => { + it("returns the raw chunk window shape { chunks, total, oldestSeq }", async () => { + const res = await app.request("/tabs/tab-x/chunks?limit=50"); + expect(res.status).toBe(200); + const body = await res.json(); + // Mocked getChunksForTab returns [] → empty window, null cursor. + expect(Array.isArray(body.chunks)).toBe(true); + expect(body.chunks).toEqual([]); + expect(body.total).toBe(0); + expect(body.oldestSeq).toBeNull(); + }); +}); + describe("POST /chat/stop", () => { it("returns 200 with success: true for valid tabId", async () => { const res = await app.request("/chat/stop", { -- cgit v1.2.3