@devries-cameron20 / t3code Mirror · read-only / commits / a6a921c763
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 07:00 UTC

perf(client): streamed row updates no longer rescan question history (#29105)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Andrei committed Nov 3, 2026 at 09:41 UTC a6a921c76335cff0a7869b84d05293ac4a50a048
2 files changed +255 -40
packages/client-runtime/src/state/threadRequests.test.ts
+207
@@ -2,6 +2,8 @@ import { presentThreadShell } from "./models.ts";
2 import { threadKey } from "./entities.ts";
3 import {
4 EnvironmentId,
5 + EventId,
6 + MessageId,
7 ThreadId,
8 type ScopedThreadRef,
9 NodeId,
@@ -13,6 +15,7 @@ import {
15 import { describe, expect, it, vi } from "vite-plus/test";
16 import { Atom, AtomRegistry, AsyncResult } from "effect/reactivity";
17 import * as Cause from "effect/Cause";
18 +import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
19 import { v2Now, v2Projection, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
20 import {
21 createQuestionHistoryProjector,
@@ -360,6 +363,210 @@ it("restores old text answers without mutating history or replacing unchanged ro
363 expect(project({ ...answered, visibleTurnItems: [...rows] })[0]).toBe(result[0]);
364 });
365
366 +type QuestionItem = Extract<
367 + OrchestrationV2ThreadProjection["turnItems"][number],
368 + { readonly type: "user_input_request" }
369 +>;
370 +
371 +describe("question history through exact row replacements", () => {
372 + const baseItem = {
373 + threadId: v2Projection.thread.id,
374 + runId: null,
375 + nodeId,
376 + providerThreadId: null,
377 + providerTurnId: null,
378 + nativeItemRef: null,
379 + parentItemId: null,
380 + status: "running" as const,
381 + title: null,
382 + startedAt: v2Now,
383 + completedAt: null,
384 + updatedAt: v2Now,
385 + };
386 + const assistantItem = (ordinal: number, text = "Hel") => ({
387 + ...baseItem,
388 + id: TurnItemId.make(`assistant-${ordinal}`),
389 + ordinal,
390 + type: "assistant_message" as const,
391 + messageId: MessageId.make(`message-${ordinal}`),
392 + text,
393 + streaming: true,
394 + });
395 + const localRow = (
396 + item: OrchestrationV2ThreadProjection["turnItems"][number],
397 + position: number,
398 + ) => ({
399 + item,
400 + position,
401 + sourceItemId: item.id,
402 + sourceThreadId: item.threadId,
403 + visibility: "local" as const,
404 + });
405 + const updated = (
406 + current: OrchestrationV2ThreadProjection,
407 + item: OrchestrationV2ThreadProjection["turnItems"][number],
408 + ) =>
409 + applyOrchestrationV2ProjectionEvent(current, {
410 + id: EventId.make(`event-${item.id}`),
411 + type: "turn-item.updated",
412 + threadId: current.thread.id,
413 + occurredAt: v2Now,
414 + payload: item,
415 + })!;
416 +
417 + it("reads only the replaced row and leaves the request index alone", () => {
418 + const size = 1_000;
419 + const counts = { items: 0, requestIds: 0 };
420 + const items = Array.from({ length: size }, (_, index) => assistantItem(index));
421 + const countedRows = items.map((item, index) => {
422 + const row = localRow(item, index);
423 + return Object.defineProperty(row, "item", {
424 + enumerable: true,
425 + get: () => {
426 + counts.items += 1;
427 + return item;
428 + },
429 + });
430 + });
431 + const countedRequests = Array.from({ length: 400 }, (_, index) => {
432 + const request = {
433 + ...projection.runtimeRequests[0]!,
434 + id: RuntimeRequestId.make(`request-${index}`),
435 + };
436 + return Object.defineProperty(request, "id", {
437 + enumerable: true,
438 + get: () => {
439 + counts.requestIds += 1;
440 + return RuntimeRequestId.make(`request-${index}`);
441 + },
442 + });
443 + });
444 + const initial: OrchestrationV2ThreadProjection = {
445 + ...v2Projection,
446 + turnItems: items,
447 + visibleTurnItems: countedRows,
448 + runtimeRequests: countedRequests,
449 + };
450 + const project = createQuestionHistoryProjector();
451 + const scan = (current: OrchestrationV2ThreadProjection) => {
452 + counts.items = 0;
453 + counts.requestIds = 0;
454 + const result = project(current);
455 + return { result, ...counts };
456 + };
457 + expect(scan(initial)).toMatchObject({ items: size, requestIds: 400 });
458 +
459 + const adjacent = updated(initial, assistantItem(size - 1, "Hello"));
460 + const hinted = scan(adjacent);
461 + expect(hinted.result).toBe(adjacent.visibleTurnItems);
462 + expect(hinted).toMatchObject({ items: 0, requestIds: 0 });
463 +
464 + // A skipped delta has no hint for this exact pair, so it scans again.
465 + const skipped = updated(
466 + updated(adjacent, assistantItem(size - 1, "Hello t")),
467 + assistantItem(size - 1, "Hello there"),
468 + );
469 + const fallback = scan(skipped);
470 + expect(fallback.result).toBe(skipped.visibleTurnItems);
471 + expect(fallback).toMatchObject({ items: size - 1, requestIds: 0 });
472 + });
473 +
474 + it("matches a full projection through streamed rows, request changes and answers", () => {
475 + const question = projection.turnItems[0] as QuestionItem;
476 + const resolved = (answers: Record<string, string>) => ({
477 + ...projection.runtimeRequests[0]!,
478 + status: "resolved" as const,
479 + answers,
480 + });
481 + const explicitRequestId = RuntimeRequestId.make("explicit-question");
482 + const explicitAnswer = {
483 + requestId: explicitRequestId,
484 + answers: { next: "Explicit" },
485 + attachmentsByQuestionId: {},
486 + questionTextById: { next: "What should happen next?" },
487 + };
488 + const unanswered: QuestionItem = {
489 + ...question,
490 + id: TurnItemId.make("explicit-question-item"),
491 + ordinal: 1,
492 + requestId: explicitRequestId,
493 + };
494 + const explicit: QuestionItem = { ...unanswered, questionAnswer: explicitAnswer };
495 + const explicitRequest = {
496 + ...resolved({ next: "From request" }),
497 + id: explicitRequestId,
498 + };
499 + const stream = assistantItem(2);
500 + const inherited = {
501 + ...localRow({ ...question, id: TurnItemId.make("inherited-question-item") }, 3),
502 + visibility: "inherited" as const,
503 + sourceThreadId: ThreadId.make("parent-thread"),
504 + };
505 + const turnItems = [question, explicit, stream];
506 + let current: OrchestrationV2ThreadProjection = {
507 + ...v2Projection,
508 + turnItems,
509 + visibleTurnItems: [...turnItems.map(localRow), inherited],
510 + runtimeRequests: [projection.runtimeRequests[0]!, explicitRequest],
511 + };
512 + const project = createQuestionHistoryProjector();
513 + const step = (next: OrchestrationV2ThreadProjection) => {
514 + current = next;
515 + const result = project(next);
516 + expect(result).toEqual(createQuestionHistoryProjector()(next));
517 + return result;
518 + };
519 + const answerOf = (rows: ReadonlyArray<{ readonly item: unknown }>, index: number) =>
520 + (rows[index]!.item as { readonly questionAnswer?: { readonly answers: unknown } })
521 + .questionAnswer?.answers;
522 +
523 + expect(step(current)).toBe(current.visibleTurnItems);
524 + expect(step(updated(current, assistantItem(2, "Hello")))).toBe(current.visibleTurnItems);
525 +
526 + // The last duplicate request wins, and every row asking it gets the answer.
527 + const answered = step({
528 + ...current,
529 + runtimeRequests: [
530 + projection.runtimeRequests[0]!,
531 + resolved({ next: "Continue" }),
532 + explicitRequest,
533 + ],
534 + });
535 + expect(answerOf(answered, 0)).toEqual({ next: "Continue" });
536 + expect(answerOf(answered, 1)).toEqual({ next: "Explicit" });
537 + expect(answerOf(answered, 3)).toEqual({ next: "Continue" });
538 +
539 + const whileOverlaid = step(updated(current, assistantItem(2, "Hello there")));
540 + expect(whileOverlaid[0]).toBe(answered[0]);
541 + expect(whileOverlaid[3]).toBe(answered[3]);
542 +
543 + const changedAnswer = step({
544 + ...current,
545 + runtimeRequests: [resolved({ next: "Stop" }), explicitRequest],
546 + });
547 + expect(answerOf(changedAnswer, 0)).toEqual({ next: "Stop" });
548 +
549 + const reopened = step({
550 + ...current,
551 + runtimeRequests: [projection.runtimeRequests[0]!, explicitRequest],
552 + });
553 + expect(reopened).toBe(current.visibleTurnItems);
554 +
555 + // Dropping an explicit answer falls back to its resolved request.
556 + const restored = step(updated(current, unanswered));
557 + expect(answerOf(restored, 1)).toEqual({ next: "From request" });
558 +
559 + const retitled: QuestionItem = {
560 + ...unanswered,
561 + questions: [{ ...question.questions[0]!, question: "Now what?" }],
562 + };
563 + const retitledRows = step(updated(current, retitled));
564 + expect(retitledRows[1]!.item).toMatchObject({
565 + questionAnswer: { questionTextById: { next: "Now what?" } },
566 + });
567 + });
568 +});
569 +
570 it("loads only child approvals and retains their destination across environments", () => {
571 const local = EnvironmentId.make("local");
572 const remote = EnvironmentId.make("remote");
packages/client-runtime/src/state/threadRequests.ts
+48 -40
@@ -13,6 +13,7 @@ import type {
13 } from "@t3tools/contracts";
14 import { Atom } from "effect/reactivity";
15 import { parseThreadKey, threadKey } from "./entities.ts";
16 +import { getProjectionArrayReplacementIndex } from "./orchestrationV2Projection.ts";
17 import type { EnvironmentThreadShell } from "./models.ts";
18 import * as DateTime from "effect/DateTime";
19 import type { AtomCommandResult } from "./runtime.ts";
@@ -292,54 +293,61 @@ export function createQuestionHistoryProjector<
293 let previousRows: Rows | undefined;
294 let previousRequests: Requests | undefined;
295 let previousResult: Rows | undefined;
296 + let byId = new Map<RuntimeRequestId, Requests[number]>();
297 const cache = new WeakMap<Row, { request: Requests[number]; row: Row }>();
296 - return (projection: {
297 - readonly visibleTurnItems: Rows;
298 - readonly runtimeRequests: Requests;
299 - }): Rows => {
300 - const { visibleTurnItems: rows, runtimeRequests: requests } = projection;
301 - if (rows === previousRows && requests === previousRequests && previousResult)
302 - return previousResult;
303 - const byId = new Map(requests.map((request) => [request.id, request]));
298 + const projectRow = (row: Row): Row => {
299 + const item = row.item;
300 + if (item.type !== "user_input_request" || item.questionAnswer) return row;
301 + const request = byId.get(item.requestId);
302 + if (request?.status !== "resolved" || request.answers === undefined) return row;
303 + const cached = cache.get(row);
304 + const next: Row =
305 + cached?.request === request
306 + ? cached.row
307 + : {
308 + ...row,
309 + item: {
310 + ...item,
311 + questionAnswer: {
312 + requestId: request.id,
313 + answers: request.answers,
314 + attachmentsByQuestionId: {},
315 + questionTextById: Object.fromEntries(
316 + item.questions.map((question) => [question.id, question.question]),
317 + ),
318 + },
319 + },
320 + };
321 + cache.set(row, { request, row: next });
322 + return next;
323 + };
324 + const projectRows = (rows: Rows): Rows => {
325 let result: Row[] | undefined;
326 for (const [index, row] of rows.entries()) {
306 - const item = row.item;
307 - const request =
308 - item.type === "user_input_request" && !item.questionAnswer
309 - ? byId.get(item.requestId)
310 - : undefined;
311 - let next = row;
312 - if (
313 - item.type === "user_input_request" &&
314 - request?.status === "resolved" &&
315 - request.answers !== undefined
316 - ) {
317 - const cached = cache.get(row);
318 - next =
319 - cached?.request === request
320 - ? cached.row
321 - : {
322 - ...row,
323 - item: {
324 - ...item,
325 - questionAnswer: {
326 - requestId: request.id,
327 - answers: request.answers,
328 - attachmentsByQuestionId: {},
329 - questionTextById: Object.fromEntries(
330 - item.questions.map((question) => [question.id, question.question]),
331 - ),
332 - },
333 - },
334 - };
335 - cache.set(row, { request, row: next });
336 - }
327 + const next = projectRow(row);
328 if (next !== row) result ??= rows.slice(0, index);
329 result?.push(next);
330 }
331 + return result ?? rows;
332 + };
333 + return (projection: {
334 + readonly visibleTurnItems: Rows;
335 + readonly runtimeRequests: Requests;
336 + }): Rows => {
337 + const { visibleTurnItems: rows, runtimeRequests: requests } = projection;
338 + const sameRequests = requests === previousRequests;
339 + if (rows === previousRows && sameRequests && previousResult) return previousResult;
340 + if (!sameRequests) byId = new Map(requests.map((request) => [request.id, request]));
341 + // Nothing was overlaid and requests are unchanged, so only an exactly replaced row can differ.
342 + const replacedIndex =
343 + sameRequests && previousRows !== undefined && previousResult === previousRows
344 + ? getProjectionArrayReplacementIndex(previousRows, rows)
345 + : undefined;
346 + const unchanged =
347 + replacedIndex !== undefined && projectRow(rows[replacedIndex]!) === rows[replacedIndex];
348 previousRows = rows;
349 previousRequests = requests;
342 - previousResult = result ?? rows;
350 + previousResult = unchanged ? rows : projectRows(rows);
351 return previousResult;
352 };
353 }