Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: Question asked between turns gets no card on a partially loaded thread until reload #49550

Openristoxy(GitHub profile, opens in a new tab) opened this issue 0 comments

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/web

Steps to reproduce

  1. Open a long thread, so the client loads it as a bounded window (partialTimeline).
  2. While no turn is running, have the provider ask a question (a provider that asks a question between turns, e.g. Pi extension UI).
  3. Keep the thread open in the live client.

Minimal repro against the client reducer, no network. Save as packages/client-runtime/src/state/partialQuestion.repro.test.ts and run vp test run src/state/partialQuestion.repro.test.ts in packages/client-runtime:

import {
  EventId,
  NodeId,
  ProviderSessionId,
  RuntimeRequestId,
  TurnItemId,
  type OrchestrationV2DomainEvent,
  type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
import { v2MessageRow, v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
import { derivePendingThreadRequests } from "./threadRequests.ts";

const requestId = RuntimeRequestId.make("between-turns-question");
const nodeId = NodeId.make("between-turns-question-node");

// A long thread loaded as a bounded window: only its latest turn is local.
const rows = [v2MessageRow("latest-user", 99_000_001), v2MessageRow("latest-reply", 99_000_005)];
const window: OrchestrationV2ThreadProjection = {
  ...v2Projection,
  turnItems: rows.map((row) => row.item),
  visibleTurnItems: rows.map((row, position) => ({ ...row, position })),
};
const partial = { partialTimeline: true, latestLocalTurnOrdinal: 99_000_005 };

// A provider asks a question while no turn runs: no run, no provider turn, low ordinal.
const events: OrchestrationV2DomainEvent[] = [
  {
    id: EventId.make("event-request"),
    type: "runtime-request.updated",
    threadId: v2ThreadId,
    occurredAt: v2Now,
    payload: {
      id: requestId,
      nodeId,
      providerTurnId: null,
      nativeRequestRef: null,
      kind: "user_input",
      status: "pending",
      responseCapability: { type: "live", providerSessionId: ProviderSessionId.make("session") },
      createdAt: v2Now,
      resolvedAt: null,
    },
  },
  {
    id: EventId.make("event-question-item"),
    type: "turn-item.updated",
    threadId: v2ThreadId,
    occurredAt: v2Now,
    payload: {
      id: TurnItemId.make("between-turns-question-item"),
      threadId: v2ThreadId,
      runId: null,
      nodeId,
      providerThreadId: null,
      providerTurnId: null,
      nativeItemRef: null,
      parentItemId: null,
      ordinal: 0,
      status: "waiting",
      title: "Pick one",
      startedAt: v2Now,
      completedAt: null,
      updatedAt: v2Now,
      type: "user_input_request",
      requestId,
      questions: [
        {
          id: "q",
          header: "Question",
          question: "Pick one",
          required: true,
          allowCustomAnswer: false,
          options: [{ label: "A", description: "A" }],
        },
      ],
    },
  },
];

const replay = (options?: typeof partial) =>
  events.reduce<OrchestrationV2ThreadProjection>(
    (projection, event) => applyOrchestrationV2ProjectionEvent(projection, event, options)!,
    window,
  );

describe("question asked between turns, live on a partially loaded thread", () => {
  it("shows the question card when the whole thread is loaded", () => {
    const projection = replay();
    expect(projection.runtimeRequests.map((request) => request.status)).toEqual(["pending"]);
    expect(derivePendingThreadRequests(projection).userInputs.map((input) => input.requestId)).toEqual([
      requestId,
    ]);
  });

  it("shows the question card when only the latest turns are loaded", () => {
    const projection = replay(partial);
    expect(projection.runtimeRequests.map((request) => request.status)).toEqual(["pending"]);
    expect(projection.turnItems.map((item) => item.type)).toContain("user_input_request");
    expect(derivePendingThreadRequests(projection).userInputs.map((input) => input.requestId)).toEqual([
      requestId,
    ]);
  });
});

Expected behavior

The pending question shows its card on the live thread, as it does when the whole thread is loaded.

Actual behavior

The sidebar shows Input, but the thread shows no question card. It appears only after a reload or after switching threads.

The question arrives as a turn-item.updated with runId: null, providerTurnId: null and a low ordinal (the Pi adapter uses ordinal: 0 when no turn is active, packages/provider-pi/src/server/adapter.ts:1946). On a partial timeline the reducer treats it as unloaded history and drops it:

  • packages/client-runtime/src/state/orchestrationV2Projection.ts:156: shouldDropMissingPartialTurnItem sends every runId === null item to isBelowPartialWindow.
  • isBelowPartialWindow (:181) returns true because item.ordinal <= latestLocalTurnOrdinal.
  • turn-item.updated (:466) then returns the projection unchanged, so the item never reaches turnItems.
  • derivePendingThreadRequests (threadRequests.ts:508) finds the pending runtimeRequests entry but no matching user_input_request item and skips it.

On reopen, hasMissingPendingQuestionCard (threads.ts:232) discards the cached projection and the fresh snapshot contains the item, which is why a reload helps.

#17764 kept items of unfinished runs and states that "items with no run" are still dropped; #16437 was closed as superseded by it. #26435 (for #25806) keeps pending questions in bounded snapshots and history paging, but not on this live path.

Possible fix (the repro and the neighbouring orchestrationV2Projection, threadRequests, threads-sync and threadHistoryMerge tests pass with it, vp run typecheck in packages/client-runtime exits 0). It assumes the runtime-request.updated arrives before the item, as it does for this adapter:

   if (item.runId === null) {
+    // A request asked between turns is new, not unloaded history, while it waits for an answer.
+    if (
+      (item.type === "user_input_request" || item.type === "approval_request") &&
+      projection.runtimeRequests.some(
+        (request) => request.id === item.requestId && request.status === "pending",
+      )
+    ) {
+      return false;
+    }
     return isBelowPartialWindow(projection, item, latestLocalTurnOrdinal);
   }

Impact

Major degradation or frequent failure

Version or commit

main @ 54702074 (reducer repro)

Environment

Linux, Vite+ 1.9.0 (vp test, Vitest 5.1.1)

Logs or stack traces

 ❯ src/state/partialQuestion.repro.test.ts (2 tests | 1 failed)
   × shows the question card when only the latest turns are loaded

AssertionError: expected [ 'user_message', 'user_message' ] to include 'user_input_request'

 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)

Workaround

Reload the page or switch to another thread and back.

Prepared by Opus 5.5, reviewed by human before posting.

  1. tanstack-source added labelsbug

Sign in to join this conversation.