summaryrefslogtreecommitdiffhomepage
path: root/packages/api/src/agent-manager.ts
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-05-30 23:14:55 +0900
committerAdam Malczewski <[email protected]>2026-05-30 23:14:55 +0900
commit624b808da0f2f8bbad8a4fbbcca3f82f24ecfc47 (patch)
tree869d34092345344ff13953398f876c8b38c8116a /packages/api/src/agent-manager.ts
parentb19f1aafc43141a865ecd40a813ed3212e77d95e (diff)
downloaddispatch-624b808da0f2f8bbad8a4fbbcca3f82f24ecfc47.tar.gz
dispatch-624b808da0f2f8bbad8a4fbbcca3f82f24ecfc47.zip
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.
Diffstat (limited to 'packages/api/src/agent-manager.ts')
-rw-r--r--packages/api/src/agent-manager.ts13
1 files changed, 13 insertions, 0 deletions
diff --git a/packages/api/src/agent-manager.ts b/packages/api/src/agent-manager.ts
index f7975d1..5f2027d 100644
--- a/packages/api/src/agent-manager.ts
+++ b/packages/api/src/agent-manager.ts
@@ -799,6 +799,9 @@ export class AgentManager {
if (tabAgent.currentAssistantId) {
snap.currentAssistantId = tabAgent.currentAssistantId;
}
+ if (tabAgent.currentTurnId) {
+ snap.currentTurnId = tabAgent.currentTurnId;
+ }
}
result[tabId] = snap;
}
@@ -1119,6 +1122,10 @@ export class AgentManager {
// chunk rows — shares one `turn_id`.
const turnId = crypto.randomUUID();
tabAgent.currentTurnId = turnId;
+ // Announce the turn so the frontend can tag its live chunks with this
+ // turn_id (stable render keys → flicker-free reconcile when the turn
+ // seals). Emitted before any content delta.
+ this.emit({ type: "turn-start", turnId }, tabId);
appendChunks(tabId, explodeUserText(turnId, message));
// Store agent models on the tab if provided (defines fallback order)
@@ -1284,6 +1291,12 @@ export class AgentManager {
this.emit({ type: "status", status: "error" }, tabId);
break;
}
+ // Turn fully settled and its chunks are now persisted (flushAssistant ran
+ // above). Signal the frontend that the turn's rows — with real seqs — are
+ // durable so it can fold its live representation into the sealed log.
+ // Emitted AFTER status:idle/error (which fire before the DB write).
+ this.emit({ type: "turn-sealed", turnId }, tabId);
+
// Turn fully settled — clear the shared turn id.
tabAgent.currentTurnId = null;