From 7e3ae9c9fb853ef6e71fdd222810db4e132bdb8c Mon Sep 17 00:00:00 2001 From: "fable-5.2" Date: Fri, 20 Nov 2026 09:27:54 -0600 Subject: [PATCH] fix(web,mobile): recovering a conversation no longer shows a handoff from a model to itself Co-Authored-By: stefan --- .../features/threads/thread-handoff-row.tsx | 3 +- apps/mobile/src/i18n/translations/de.json | 1 + apps/mobile/src/i18n/translations/en.json | 1 + apps/mobile/src/i18n/translations/es.json | 1 + apps/mobile/src/i18n/translations/fr.json | 1 + apps/mobile/src/i18n/translations/ja.json | 1 + apps/mobile/src/i18n/translations/ko.json | 1 + apps/mobile/src/i18n/translations/pt-BR.json | 1 + apps/mobile/src/i18n/translations/ru.json | 1 + apps/mobile/src/i18n/translations/sv.json | 1 + apps/mobile/src/i18n/translations/zh.json | 1 + .../src/components/chat/V2LifecycleRow.tsx | 12 ++++++- apps/web/src/i18n/translations/de.json | 1 + apps/web/src/i18n/translations/en.json | 1 + apps/web/src/i18n/translations/es.json | 1 + apps/web/src/i18n/translations/fr.json | 1 + apps/web/src/i18n/translations/ja.json | 1 + apps/web/src/i18n/translations/ko.json | 1 + apps/web/src/i18n/translations/pt-BR.json | 1 + apps/web/src/i18n/translations/ru.json | 1 + apps/web/src/i18n/translations/sv.json | 1 + apps/web/src/i18n/translations/zh.json | 1 + packages/client-runtime/src/handoff.test.ts | 15 +++++++++ packages/client-runtime/src/handoff.ts | 31 +++++++++++++++++-- 24 files changed, 77 insertions(+), 4 deletions(-) diff --git a/apps/mobile/src/features/threads/thread-handoff-row.tsx b/apps/mobile/src/features/threads/thread-handoff-row.tsx index 7158a9968bcc6..eef5d1cc504ab 100644 --- a/apps/mobile/src/features/threads/thread-handoff-row.tsx +++ b/apps/mobile/src/features/threads/thread-handoff-row.tsx @@ -2,6 +2,7 @@ import * as Dialog from "../../components/ui/Dialog"; import { useAtomValue } from "@effect/atom-react"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { + isRecoveryHandoff, resolveHandoffEndpoints, resolveModelSwitchChange, type HandoffEndpoints, @@ -100,7 +101,7 @@ export function ThreadHandoffRow(props: { endpoints={endpoints} iconColor={color} surfaceColor={props.surfaceColor} - label={gt("Context handoff")} + label={isRecoveryHandoff(item) ? gt("Recovered context") : gt("Context handoff")} failed={item.status === "failed"} /> ); diff --git a/apps/mobile/src/i18n/translations/de.json b/apps/mobile/src/i18n/translations/de.json index 1b7bd82051d2e..c6315ff4305ce 100644 --- a/apps/mobile/src/i18n/translations/de.json +++ b/apps/mobile/src/i18n/translations/de.json @@ -4694,6 +4694,7 @@ "97ad838fc2717591": "Ausgabe wird geladen…", "b2de62817c6ab53d": "Keine Ausgabe.", "346e42415ac6d374": "Frühere Ausgabe nicht angezeigt", + "b5a8acfa085f459a": "Wiederhergestellter Kontext", "4d765811334b8edf": "Kontextübergabe", "0e7207133e1d9277": "Anbieterkonto anzeigen", "3747414e652cca55": "Thread maximieren", diff --git a/apps/mobile/src/i18n/translations/en.json b/apps/mobile/src/i18n/translations/en.json index 9e14fe9b7a221..e6371983ecc3c 100644 --- a/apps/mobile/src/i18n/translations/en.json +++ b/apps/mobile/src/i18n/translations/en.json @@ -4695,6 +4695,7 @@ "97ad838fc2717591": "Loading output…", "b2de62817c6ab53d": "No output.", "346e42415ac6d374": "Earlier output not shown", + "b5a8acfa085f459a": "Recovered context", "4d765811334b8edf": "Context handoff", "0e7207133e1d9277": "Show provider account", "3747414e652cca55": "Maximize thread", diff --git a/apps/mobile/src/i18n/translations/es.json b/apps/mobile/src/i18n/translations/es.json index e1a9c5cf1b5a3..b049dbffaf2ab 100644 --- a/apps/mobile/src/i18n/translations/es.json +++ b/apps/mobile/src/i18n/translations/es.json @@ -4687,6 +4687,7 @@ "97ad838fc2717591": "Cargando la salida…", "b2de62817c6ab53d": "Sin salida.", "346e42415ac6d374": "No se muestra la salida anterior", + "b5a8acfa085f459a": "Contexto recuperado", "4d765811334b8edf": "Traspaso de contexto", "0e7207133e1d9277": "Mostrar cuenta del proveedor", "3747414e652cca55": "Maximizar hilo", diff --git a/apps/mobile/src/i18n/translations/fr.json b/apps/mobile/src/i18n/translations/fr.json index ef1ee0912d582..efa301b65f734 100644 --- a/apps/mobile/src/i18n/translations/fr.json +++ b/apps/mobile/src/i18n/translations/fr.json @@ -4688,6 +4688,7 @@ "97ad838fc2717591": "Chargement de la sortie…", "b2de62817c6ab53d": "Aucune sortie.", "346e42415ac6d374": "Sortie antérieure non affichée", + "b5a8acfa085f459a": "Contexte récupéré", "4d765811334b8edf": "Transfert de contexte", "0e7207133e1d9277": "Afficher le compte du fournisseur", "3747414e652cca55": "Agrandir le fil", diff --git a/apps/mobile/src/i18n/translations/ja.json b/apps/mobile/src/i18n/translations/ja.json index dd7f638cfca7a..dfc2ac08a331c 100644 --- a/apps/mobile/src/i18n/translations/ja.json +++ b/apps/mobile/src/i18n/translations/ja.json @@ -4669,6 +4669,7 @@ "97ad838fc2717591": "出力を読み込んでいます…", "b2de62817c6ab53d": "出力はありません。", "346e42415ac6d374": "以前の出力は表示されていません", + "b5a8acfa085f459a": "復元されたコンテキスト", "4d765811334b8edf": "コンテキストの引き継ぎ", "0e7207133e1d9277": "プロバイダーのアカウントを表示", "3747414e652cca55": "スレッドを最大化", diff --git a/apps/mobile/src/i18n/translations/ko.json b/apps/mobile/src/i18n/translations/ko.json index cbc39fa7feabb..25d8a766cd123 100644 --- a/apps/mobile/src/i18n/translations/ko.json +++ b/apps/mobile/src/i18n/translations/ko.json @@ -4674,6 +4674,7 @@ "97ad838fc2717591": "출력을 불러오는 중…", "b2de62817c6ab53d": "출력이 없습니다.", "346e42415ac6d374": "이전 출력은 표시되지 않음", + "b5a8acfa085f459a": "복구된 컨텍스트", "4d765811334b8edf": "컨텍스트 인계", "0e7207133e1d9277": "제공자 계정 표시", "3747414e652cca55": "스레드 최대화", diff --git a/apps/mobile/src/i18n/translations/pt-BR.json b/apps/mobile/src/i18n/translations/pt-BR.json index fc2ebadd1255f..51b674a56203b 100644 --- a/apps/mobile/src/i18n/translations/pt-BR.json +++ b/apps/mobile/src/i18n/translations/pt-BR.json @@ -4688,6 +4688,7 @@ "97ad838fc2717591": "Carregando a saída…", "b2de62817c6ab53d": "Sem saída.", "346e42415ac6d374": "Saída anterior não mostrada", + "b5a8acfa085f459a": "Contexto recuperado", "4d765811334b8edf": "Transferência de contexto", "0e7207133e1d9277": "Mostrar conta do provedor", "3747414e652cca55": "Maximizar thread", diff --git a/apps/mobile/src/i18n/translations/ru.json b/apps/mobile/src/i18n/translations/ru.json index b3c35b2d78daa..cce3ac401bfa5 100644 --- a/apps/mobile/src/i18n/translations/ru.json +++ b/apps/mobile/src/i18n/translations/ru.json @@ -4766,6 +4766,7 @@ "97ad838fc2717591": "Загрузка вывода…", "b2de62817c6ab53d": "Нет вывода.", "346e42415ac6d374": "Более ранний вывод не показан", + "b5a8acfa085f459a": "Контекст восстановлен", "4d765811334b8edf": "Передача контекста", "0e7207133e1d9277": "Показать аккаунт провайдера", "3747414e652cca55": "Развернуть тред", diff --git a/apps/mobile/src/i18n/translations/sv.json b/apps/mobile/src/i18n/translations/sv.json index 50bf58982de2c..fe2e02f0d4bb2 100644 --- a/apps/mobile/src/i18n/translations/sv.json +++ b/apps/mobile/src/i18n/translations/sv.json @@ -4694,6 +4694,7 @@ "97ad838fc2717591": "Läser in utdata…", "b2de62817c6ab53d": "Inga utdata.", "346e42415ac6d374": "Tidigare utdata visas inte", + "b5a8acfa085f459a": "Återställd kontext", "4d765811334b8edf": "Kontextöverlämning", "0e7207133e1d9277": "Visa leverantörskonto", "3747414e652cca55": "Maximera tråden", diff --git a/apps/mobile/src/i18n/translations/zh.json b/apps/mobile/src/i18n/translations/zh.json index ec960fce61b75..68c38ad89b3c2 100644 --- a/apps/mobile/src/i18n/translations/zh.json +++ b/apps/mobile/src/i18n/translations/zh.json @@ -4677,6 +4677,7 @@ "97ad838fc2717591": "正在加载输出…", "b2de62817c6ab53d": "没有输出。", "346e42415ac6d374": "更早的输出未显示", + "b5a8acfa085f459a": "已恢复上下文", "4d765811334b8edf": "上下文交接", "0e7207133e1d9277": "显示提供方账户", "3747414e652cca55": "最大化会话", diff --git a/apps/web/src/components/chat/V2LifecycleRow.tsx b/apps/web/src/components/chat/V2LifecycleRow.tsx index 940e741b9c486..6cddb48befee5 100644 --- a/apps/web/src/components/chat/V2LifecycleRow.tsx +++ b/apps/web/src/components/chat/V2LifecycleRow.tsx @@ -16,6 +16,7 @@ import { MiddleTruncate } from "../ui/middle-truncate"; import * as DateTime from "effect/DateTime"; import { WorkLogRow } from "./WorkLog"; import { + isRecoveryHandoff, resolveHandoffEndpoints, type HandoffEndpoints, type HandoffTimelineRun, @@ -96,6 +97,7 @@ export function V2LifecycleRow(props: { ); @@ -624,6 +626,7 @@ function EnvironmentHandoffRow({ }: { readonly environmentId: EnvironmentId; readonly endpoints: HandoffEndpoints; + readonly recovery: boolean; readonly failed: boolean; }) { const providers = @@ -666,6 +669,7 @@ export function HandoffRow(props: { readonly endpoints: HandoffEndpoints; readonly providers: ReadonlyArray; readonly pending?: boolean; + readonly recovery?: boolean; readonly failed?: boolean; }) { const gt = useGT(); @@ -675,7 +679,13 @@ export function HandoffRow(props: { const entries = settings ? applyProviderInstanceSettings(derived, settings, null) : derived; return ( { resolveHandoffEndpoints(item, [run("target", 2, from, "wrong-model")]).to.model, ).toBeUndefined(); }); + + it("treats a provider conversation restarted in a fresh session of itself as a recovery", () => { + const sameModel = { fromModelSelections: [{ instanceId: to, model: "same" }], toModel: "same" }; + const recovery = { ...item, ...sameModel, fromProviderThreadIds: [item.toProviderThreadId] }; + expect(isRecoveryHandoff(recovery)).toBe(true); + expect(resolveHandoffEndpoints(recovery, []).from).toEqual([]); + // A fork or merge-back between two conversations on one model is still a handoff. + const fork = { ...item, ...sameModel }; + expect(isRecoveryHandoff(fork)).toBe(false); + expect(resolveHandoffEndpoints(fork, []).from).toEqual(sameModel.fromModelSelections); + }); }); const projection = { diff --git a/packages/client-runtime/src/handoff.ts b/packages/client-runtime/src/handoff.ts index 48a1227429312..ce8b5f1ff4efe 100644 --- a/packages/client-runtime/src/handoff.ts +++ b/packages/client-runtime/src/handoff.ts @@ -52,7 +52,13 @@ export function resolvePendingHandoff( export function resolveHandoffEndpoints( item: Pick< Extract, - "runId" | "fromModelSelections" | "fromProviderInstanceIds" | "toModel" | "toProviderInstanceId" + | "runId" + | "fromProviderThreadIds" + | "toProviderThreadId" + | "fromModelSelections" + | "fromProviderInstanceIds" + | "toModel" + | "toProviderInstanceId" >, runs: ReadonlyArray, ) { @@ -72,7 +78,28 @@ export function resolveHandoffEndpoints( instanceId, model: latestRunModelBefore(runs, instanceId, handoffRun?.ordinal), })); - return { from: fromEndpoints, to: { instanceId: item.toProviderInstanceId, model: toModel } }; + return { + from: isRecoveryHandoff(item) ? [] : fromEndpoints, + to: { instanceId: item.toProviderInstanceId, model: toModel }, + }; +} + +/** + * Whether every source of the handoff is its own target conversation, as when a provider + * conversation restarts in a fresh session of itself after a failed resume or a rollback it + * cannot truncate. Nothing changed hands, so it has no source. Matching on the model instead + * would also catch a fork or a merge-back between two conversations that happen to share one. + */ +export function isRecoveryHandoff( + item: Pick< + Extract, + "fromProviderThreadIds" | "toProviderThreadId" + >, +): boolean { + return ( + item.fromProviderThreadIds.length > 0 && + item.fromProviderThreadIds.every((id) => id === item.toProviderThreadId) + ); } function latestRunModelBefore(