mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 05:52:10 +00:00
🧷 refactor: Split useResumeOnLoad by Responsibility (#17134)
* refactor: Split useResumeOnLoad by Responsibility * refactor: Name the Resume Hooks' Modules With Single Words
This commit is contained in:
parent
dfedc3d873
commit
856ba7ffa0
4 changed files with 735 additions and 580 deletions
191
client/src/hooks/SSE/announcements.ts
Normal file
191
client/src/hooks/SSE/announcements.ts
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
import { useRef, useEffect } from 'react';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { Constants, QueryKeys } from 'librechat-data-provider';
|
||||
import type { TSubmission } from 'librechat-data-provider';
|
||||
import type { MutableRefObject } from 'react';
|
||||
import type { OwedSuccessorState } from './successor';
|
||||
import { resumeRequestsAtom } from '~/hooks/Chat/resume';
|
||||
import { streamStatusQueryKey } from '~/data-provider';
|
||||
|
||||
/**
|
||||
* Matches the active-job list's own poll interval: answering an announcement
|
||||
* faster than the list can change it would only re-read the same snapshot.
|
||||
*/
|
||||
const ACTIVE_JOB_REARM_INTERVAL_MS = 5_000;
|
||||
|
||||
/**
|
||||
* Re-arms the resume check when a run this pane did not start is announced (the active-job
|
||||
* list, an owed queued turn) or when `resumeStream` asks for it, forcing the status and
|
||||
* history reads it is about to make fresh first.
|
||||
*/
|
||||
export default function useResumeAnnouncements({
|
||||
conversationId,
|
||||
resumableEnabled,
|
||||
owed,
|
||||
processedConvoRef,
|
||||
answeredActiveJobRef,
|
||||
setExternalRunArm,
|
||||
setSubmission,
|
||||
routeConversationLoaded,
|
||||
hasActiveSubmissionForThisConvo,
|
||||
hasLiveSubmissionForThisConvo,
|
||||
attachedGenerationCreatedAt,
|
||||
}: {
|
||||
conversationId: string | undefined;
|
||||
resumableEnabled: boolean;
|
||||
owed: OwedSuccessorState;
|
||||
processedConvoRef: MutableRefObject<string | null>;
|
||||
answeredActiveJobRef: MutableRefObject<{ conversationId: string; at: number } | null>;
|
||||
setExternalRunArm: (update: (arm: number) => number) => void;
|
||||
setSubmission: (submission: TSubmission | null) => void;
|
||||
routeConversationLoaded: boolean;
|
||||
hasActiveSubmissionForThisConvo: boolean;
|
||||
hasLiveSubmissionForThisConvo: boolean;
|
||||
attachedGenerationCreatedAt: number | null;
|
||||
}): void {
|
||||
const queryClient = useQueryClient();
|
||||
const { successorOwed, hasActiveJobForThisConvo, activeJobsUpdatedAt, receiptSignature } = owed;
|
||||
/** Receipt state at the last arm, so history is refetched on a transition
|
||||
* rather than on every heartbeat of an unchanged queued turn. */
|
||||
const lastArmedReceiptSignatureRef = useRef<string | null>(null);
|
||||
|
||||
/**
|
||||
* Answer an active-job announcement for the conversation on screen.
|
||||
*
|
||||
* The announcement means "your history may have moved", not merely "re-open
|
||||
* the status query", so both reads this pane is about to make are forced
|
||||
* fresh first. Toggling `enabled` alone would not do it: an inactive status
|
||||
* answered less than `staleTime` ago is still fresh, and React Query would
|
||||
* hand back that cached "nothing running" and let the effect above record the
|
||||
* announcement as handled without ever attaching.
|
||||
*
|
||||
* History is invalidated for the same reason, and it matters whichever way
|
||||
* the status read lands. An external client may have completed whole turns
|
||||
* this pane never saw before starting the one now running: the resume
|
||||
* submission and `finalHandler` both build on this snapshot, so attaching
|
||||
* without it grafts the live turn onto a hole. And when the run turns out to
|
||||
* have already finished, the refetch is the entire repair: the messages
|
||||
* query disables refetch on focus, mount and reconnect, so nothing else would
|
||||
* ever collect those turns. The refetch also re-gates `messagesLoaded`, which
|
||||
* defers the effect above until the authoritative history has landed.
|
||||
*/
|
||||
/**
|
||||
* Two announcement sources, one arm. The active-job list is the general one.
|
||||
* An owed queued turn is the specific one, and it must be able to arm on its
|
||||
* own: the run it announces can start and finish between two list polls, and
|
||||
* the status read this arm enables is the authoritative answer either way:
|
||||
* active attaches, inactive means the history refetch below is the repair.
|
||||
*/
|
||||
const successorAnnounced = hasActiveJobForThisConvo || successorOwed;
|
||||
useEffect(() => {
|
||||
if (!successorAnnounced) {
|
||||
answeredActiveJobRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (!resumableEnabled || !conversationId || hasLiveSubmissionForThisConvo) {
|
||||
return;
|
||||
}
|
||||
|
||||
const answered = answeredActiveJobRef.current;
|
||||
const now = Date.now();
|
||||
if (
|
||||
answered != null &&
|
||||
answered.conversationId === conversationId &&
|
||||
now - answered.at < ACTIVE_JOB_REARM_INTERVAL_MS
|
||||
) {
|
||||
return;
|
||||
}
|
||||
answeredActiveJobRef.current = { conversationId, at: now };
|
||||
|
||||
console.log('[ResumeOnLoad] Active job announced without an attachment', { conversationId });
|
||||
/**
|
||||
* A finished submission still installed here is the one thing standing
|
||||
* between this announcement and the attachment: it is what the check below
|
||||
* reads as "already attached", and what disables the status query. The run
|
||||
* it describes is over, so release it; a server-side continuation such as
|
||||
* a background tool dispatch finishing is a different generation, and the
|
||||
* resume path is what owns building the submission for it.
|
||||
*/
|
||||
if (hasActiveSubmissionForThisConvo) {
|
||||
setSubmission(null);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: streamStatusQueryKey(conversationId) });
|
||||
/**
|
||||
* History is refetched only when this arm can actually change what the
|
||||
* pane shows: a run is listed (attachment is imminent and must not be
|
||||
* grafted onto a stale snapshot), or the receipts just transitioned (the
|
||||
* turn was admitted, or dropped after finishing inside a poll gap). A
|
||||
* long wait behind an unadmitted turn re-arms status on the heartbeat but
|
||||
* must not download the conversation again each time.
|
||||
*/
|
||||
const receiptsTransitioned = lastArmedReceiptSignatureRef.current !== receiptSignature;
|
||||
lastArmedReceiptSignatureRef.current = receiptSignature;
|
||||
if (hasActiveJobForThisConvo || receiptsTransitioned) {
|
||||
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, conversationId] });
|
||||
}
|
||||
processedConvoRef.current = null;
|
||||
setExternalRunArm((arm) => arm + 1);
|
||||
}, [
|
||||
conversationId,
|
||||
resumableEnabled,
|
||||
successorAnnounced,
|
||||
hasActiveJobForThisConvo,
|
||||
hasActiveSubmissionForThisConvo,
|
||||
hasLiveSubmissionForThisConvo,
|
||||
attachedGenerationCreatedAt,
|
||||
activeJobsUpdatedAt,
|
||||
receiptSignature,
|
||||
setSubmission,
|
||||
queryClient,
|
||||
processedConvoRef,
|
||||
answeredActiveJobRef,
|
||||
setExternalRunArm,
|
||||
]);
|
||||
|
||||
/**
|
||||
* An explicit `resumeStream` request takes the announcement's path: the
|
||||
* status read decides whether anything is running, and the effect above
|
||||
* builds the resume submission that `useResumableSSE` attaches through the
|
||||
* host transport. The request is consumed either way: one made while this
|
||||
* pane is already attached is answered by that attachment.
|
||||
*/
|
||||
const [resumeRequests, setResumeRequests] = useAtom(resumeRequestsAtom);
|
||||
const resumeRequested = !!conversationId && resumeRequests.has(conversationId);
|
||||
useEffect(() => {
|
||||
if (!resumeRequested || !conversationId || !routeConversationLoaded) {
|
||||
return;
|
||||
}
|
||||
setResumeRequests((pending) => {
|
||||
const next = new Set(pending);
|
||||
next.delete(conversationId);
|
||||
return next;
|
||||
});
|
||||
if (!resumableEnabled || conversationId === Constants.NEW_CONVO) {
|
||||
return;
|
||||
}
|
||||
if (hasLiveSubmissionForThisConvo) {
|
||||
return;
|
||||
}
|
||||
/** A finished submission still installed reads as attached to the check above. */
|
||||
if (hasActiveSubmissionForThisConvo) {
|
||||
setSubmission(null);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: streamStatusQueryKey(conversationId) });
|
||||
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, conversationId] });
|
||||
processedConvoRef.current = null;
|
||||
setExternalRunArm((arm) => arm + 1);
|
||||
}, [
|
||||
conversationId,
|
||||
resumeRequested,
|
||||
routeConversationLoaded,
|
||||
setResumeRequests,
|
||||
resumableEnabled,
|
||||
hasActiveSubmissionForThisConvo,
|
||||
hasLiveSubmissionForThisConvo,
|
||||
setSubmission,
|
||||
queryClient,
|
||||
processedConvoRef,
|
||||
setExternalRunArm,
|
||||
]);
|
||||
}
|
||||
122
client/src/hooks/SSE/finished.ts
Normal file
122
client/src/hooks/SSE/finished.ts
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
import { QueryKeys } from 'librechat-data-provider';
|
||||
import { resolveDetachedRunEnd } from '@librechat/chat';
|
||||
import {
|
||||
revealedQueuedTurnFamily,
|
||||
isDetachedRunManual,
|
||||
detachedRunByConvoId,
|
||||
pendingRunEndByConvoId,
|
||||
} from '@librechat/chat/react';
|
||||
import type { StreamStatusResponse, GenerationProtocolVersion } from '@librechat/chat';
|
||||
import type { TMessage, TAgentQueuedTurnReceipt } from 'librechat-data-provider';
|
||||
import type { QueryClient } from '@tanstack/react-query';
|
||||
import type { useStore } from 'jotai';
|
||||
import type { SteerState } from './steer';
|
||||
import { isQueuedTurnSuccessorOwed } from '~/data-provider';
|
||||
import { dedupeSteersById } from '~/utils';
|
||||
|
||||
/**
|
||||
* Settles what a run that ended while this pane was away left behind: the reveal guard of a
|
||||
* queued turn that already landed, steers a terminal drain parked, stale local chips, and the
|
||||
* end of a run the pane left mid-stream, read from the history the server persisted.
|
||||
*/
|
||||
export function settleFinishedRun({
|
||||
jotaiStore,
|
||||
queryClient,
|
||||
steers,
|
||||
conversationId,
|
||||
streamStatus,
|
||||
streamStatusUpdatedAt,
|
||||
queuedTurnReceipts,
|
||||
getMessages,
|
||||
generationProtocolVersion,
|
||||
}: {
|
||||
jotaiStore: ReturnType<typeof useStore>;
|
||||
queryClient: QueryClient;
|
||||
steers: Pick<SteerState, 'seedSteerChips' | 'settleAppliedSteerParts' | 'convertSteersToQueued'>;
|
||||
conversationId: string;
|
||||
streamStatus: StreamStatusResponse;
|
||||
streamStatusUpdatedAt: number;
|
||||
queuedTurnReceipts: TAgentQueuedTurnReceipt[] | undefined;
|
||||
getMessages: () => TMessage[] | undefined;
|
||||
generationProtocolVersion: GenerationProtocolVersion;
|
||||
}): void {
|
||||
const revealFamily = revealedQueuedTurnFamily(conversationId);
|
||||
const pendingReveal = jotaiStore.get(revealFamily);
|
||||
/** A remounted pane may have missed attachment and the quiet-window
|
||||
* timer. An expired job has no epoch; require a fresh inactive read
|
||||
* after restored history before retiring its surviving handoff guard. */
|
||||
const historyUpdatedAt = queryClient.getQueryState([
|
||||
QueryKeys.messages,
|
||||
conversationId,
|
||||
])?.dataUpdatedAt;
|
||||
if (
|
||||
pendingReveal != null &&
|
||||
streamStatus.active === false &&
|
||||
streamStatus.createdAt == null &&
|
||||
historyUpdatedAt != null &&
|
||||
streamStatusUpdatedAt >= historyUpdatedAt &&
|
||||
streamStatusUpdatedAt > Date.parse(pendingReveal.revealedAt) &&
|
||||
!isQueuedTurnSuccessorOwed(queuedTurnReceipts) &&
|
||||
(getMessages() ?? []).some(
|
||||
(message) => message.parentMessageId === pendingReveal.parentMessageId,
|
||||
)
|
||||
) {
|
||||
jotaiStore.set(revealFamily, null);
|
||||
}
|
||||
|
||||
// A terminal drain may have parked acknowledged steers no subscriber
|
||||
// received (tab closed / reload racing the final event); the status
|
||||
// claim returns them exactly once; restore as queued follow-up chips.
|
||||
// An expired pendingAction can report inactive BEFORE the sweeper parks
|
||||
// the steer queue: those steers still ride resumeState.pendingSteers,
|
||||
// so convert both lists (id-deduped) before the empty seed clears chips.
|
||||
const leftoverSteers = dedupeSteersById(
|
||||
streamStatus.unrecoveredSteers,
|
||||
streamStatus.resumeState?.pendingSteers,
|
||||
);
|
||||
if (conversationId && leftoverSteers.length > 0) {
|
||||
steers.convertSteersToQueued(conversationId, leftoverSteers, {
|
||||
generationProtocolVersion,
|
||||
});
|
||||
}
|
||||
// The run is terminal, so any remaining local pending chip is stale:
|
||||
// its steer either applied (inline part in the saved message) or rode
|
||||
// `unrecoveredSteers` above, the same empty-list reconcile as the resume path.
|
||||
steers.settleAppliedSteerParts(conversationId, getMessages());
|
||||
steers.seedSteerChips(conversationId, []);
|
||||
/** A run this pane stopped watching when the user left has ended on the server, which
|
||||
* deleted the job. Its persisted response tells how it ended; parking that end lets the
|
||||
* queue drain send a follow-up queued during the run, as an attached run would have.
|
||||
* The cached history still holds the optimistic response the pane left, which lacks the
|
||||
* persisted stop and error flags, so only a successful refetch of history may resolve it;
|
||||
* when that fails or predates the saved response, the next visit tries again. */
|
||||
const detachedFamily = detachedRunByConvoId(conversationId);
|
||||
const detachedRun = jotaiStore.get(detachedFamily);
|
||||
const parkDetachedEnd = () => {
|
||||
if (detachedRun == null || jotaiStore.get(detachedFamily) !== detachedRun) {
|
||||
return;
|
||||
}
|
||||
if (isDetachedRunManual(jotaiStore.get, conversationId, detachedRun)) {
|
||||
jotaiStore.set(detachedFamily, null);
|
||||
return;
|
||||
}
|
||||
const end = resolveDetachedRunEnd(conversationId, detachedRun, getMessages());
|
||||
if (end == null) {
|
||||
return;
|
||||
}
|
||||
jotaiStore.set(detachedFamily, null);
|
||||
jotaiStore.set(pendingRunEndByConvoId(conversationId), end);
|
||||
};
|
||||
/** A run left for a manual send is retired without a read instead of refetching on every
|
||||
* visit; the check repeats after the read, since a receipt can settle meanwhile. */
|
||||
if (detachedRun != null && isDetachedRunManual(jotaiStore.get, conversationId, detachedRun)) {
|
||||
jotaiStore.set(detachedFamily, null);
|
||||
} else if (detachedRun != null) {
|
||||
queryClient
|
||||
.refetchQueries(
|
||||
{ queryKey: [QueryKeys.messages, conversationId], exact: true },
|
||||
{ throwOnError: true },
|
||||
)
|
||||
.then(parkDetachedEnd, () => undefined);
|
||||
}
|
||||
}
|
||||
382
client/src/hooks/SSE/successor.ts
Normal file
382
client/src/hooks/SSE/successor.ts
Normal file
|
|
@ -0,0 +1,382 @@
|
|||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useStore } from 'jotai';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { Constants, QueryKeys } from 'librechat-data-provider';
|
||||
import { revealedQueuedTurnFamily } from '@librechat/chat/react';
|
||||
import type { TAgentQueuedTurnReceipt } from 'librechat-data-provider';
|
||||
import {
|
||||
useActiveJobs,
|
||||
useAgentQueuedTurns,
|
||||
extendActiveJobsGrace,
|
||||
isQueuedTurnSuccessorOwed,
|
||||
ACTIVE_JOBS_SUCCESSOR_GRACE_MS,
|
||||
} from '~/data-provider';
|
||||
import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';
|
||||
|
||||
/** What the pane knows about runs it did not start for the conversation on screen. */
|
||||
export type OwedSuccessorState = {
|
||||
/** The server's receipts for the conversation's queued turns. */
|
||||
queuedTurnReceipts: TAgentQueuedTurnReceipt[] | undefined;
|
||||
/** The receipts reduced to turn and status, which changes only on a transition. */
|
||||
receiptSignature: string;
|
||||
/** Whether the backend owes this conversation a run this pane has not attached to. */
|
||||
successorOwed: boolean;
|
||||
/** Whether the active-job list names this conversation. */
|
||||
hasActiveJobForThisConvo: boolean;
|
||||
/** When the active-job list was last fetched, the heartbeat an announcement re-arms on. */
|
||||
activeJobsUpdatedAt: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Tracks runs the server owes a conversation (a queued turn it took ownership of, or a job
|
||||
* the active-job list names) until this pane attaches to them or a quiet window expires.
|
||||
*/
|
||||
export default function useOwedSuccessor({
|
||||
conversationId,
|
||||
resumableEnabled,
|
||||
attachedGenerationCreatedAt,
|
||||
hasLiveSubmissionForThisConvo,
|
||||
}: {
|
||||
conversationId: string | undefined;
|
||||
resumableEnabled: boolean;
|
||||
attachedGenerationCreatedAt: number | null;
|
||||
hasLiveSubmissionForThisConvo: boolean;
|
||||
}): OwedSuccessorState {
|
||||
const jotaiStore = useStore();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
/**
|
||||
* A run this pane did not start (another tab, another device, a scheduled
|
||||
* trigger) announces itself only through the user-scoped active-job list,
|
||||
* which already polls while anything is running and refetches on focus. The
|
||||
* status query is the one thing that could turn that into an attachment, and
|
||||
* it switches off for the rest of this conversation's mount the moment it has
|
||||
* answered inactive once. Without a re-arm the pane sits on history it cannot
|
||||
* see has moved on, and a send from here forks a branch off a stale tail.
|
||||
*
|
||||
* Consumed once per run rather than held open: a job stays listed for its
|
||||
* whole lifetime, and re-opening the query on every poll would turn a
|
||||
* five-second heartbeat into a five-second status read.
|
||||
*/
|
||||
/**
|
||||
* `dataUpdatedAt` is the trigger, not `activeJobsData`. React Query keeps the
|
||||
* previous reference when a refetch is deep-equal, and a run that stays
|
||||
* listed produces exactly that, so the derived boolean below never changes
|
||||
* and an effect keyed on it would run once and never again. The stamp moves
|
||||
* on every fetch, which is the heartbeat this needs.
|
||||
*/
|
||||
/**
|
||||
* Positive evidence that the backend owes this conversation a run: a queued
|
||||
* turn it has taken ownership of. The client never builds a submission for
|
||||
* one (`useQueueDrain` declines the boundary), so the resulting run has to
|
||||
* be recognised from the active-job list, and finishing the previous run is
|
||||
* what empties that list. Saying so outright keeps the listening exact
|
||||
* instead of betting on how long admission takes.
|
||||
*
|
||||
* Read from the server's own receipts rather than the chip projection, so
|
||||
* admission semantics stay beside the queue's own predicate instead of being
|
||||
* restated here. `useSteering` owns the fetch; subscribing with
|
||||
* `enabled: false` observes that cache without competing for it, and an
|
||||
* unpopulated cache simply falls back to the handover window.
|
||||
*/
|
||||
const { data: queuedTurnReceipts, dataUpdatedAt: queuedTurnsObservedAt } = useAgentQueuedTurns(
|
||||
conversationId ?? '',
|
||||
false,
|
||||
);
|
||||
/**
|
||||
* Receipts whose successor this pane has already attached to, per turn and
|
||||
* per conversation. An `admitted` receipt stays in the cache for a fetch
|
||||
* after delivery and still reads as owed; without remembering it was
|
||||
* delivered, the latch would clear on the attachment and be re-recorded from
|
||||
* the same receipt on the next render, synchronously, until the cache moved
|
||||
* on. Tracked per turn rather than per receipt set: two turns queued behind
|
||||
* the same generation deliver one at a time, and marking the whole set on
|
||||
* the first would let the still-attached first successor pass as delivery of
|
||||
* the second. Entries are pruned once the conversation's receipts no longer
|
||||
* mention them.
|
||||
*/
|
||||
const [deliveredTurnIdsByConvo, setDeliveredTurnIdsByConvo] = useState<
|
||||
ReadonlyMap<string, ReadonlySet<string>>
|
||||
>(() => new Map());
|
||||
/**
|
||||
* What the receipts say, reduced to the two facts this hook keys on. The
|
||||
* signature drives re-arming: a `queued` receipt is refetched every two
|
||||
* seconds during a long wait and advances `dataUpdatedAt` without changing,
|
||||
* and re-arming on that heartbeat would invalidate status and history every
|
||||
* throttle interval while nothing has happened. Only a transition (a status
|
||||
* change, a receipt appearing or being dropped) is an announcement.
|
||||
*
|
||||
* The boundary is the generation the owed turn waits behind, which the
|
||||
* receipt knows better than this pane does: `expectedPredecessorCreatedAt`
|
||||
* is stamped from the live epoch at enqueue, and `effectivePredecessorCreatedAt`
|
||||
* is the boundary admission actually consumed once turns chain. It is what
|
||||
* lets delivery be judged by generation rather than by "some run exists".
|
||||
*/
|
||||
const receiptSignature = Array.isArray(queuedTurnReceipts)
|
||||
? queuedTurnReceipts
|
||||
.map((receipt) => `${receipt.queuedTurnId}:${receipt.status}`)
|
||||
.sort()
|
||||
.join('|')
|
||||
: '';
|
||||
const deliveredTurnIds =
|
||||
conversationId != null ? deliveredTurnIdsByConvo.get(conversationId) : undefined;
|
||||
const owedReceipts = useMemo(
|
||||
() =>
|
||||
Array.isArray(queuedTurnReceipts)
|
||||
? queuedTurnReceipts.filter(
|
||||
(receipt) =>
|
||||
!deliveredTurnIds?.has(receipt.queuedTurnId) && isQueuedTurnSuccessorOwed([receipt]),
|
||||
)
|
||||
: [],
|
||||
[queuedTurnReceipts, deliveredTurnIds],
|
||||
);
|
||||
const successorOwedByReceipt = owedReceipts.length > 0;
|
||||
/** A turn still `queued`/`claimed` has not produced a run: whatever generation
|
||||
* is live right now is its predecessor, including a sibling turn's
|
||||
* successor that this pane is attached to. The boundary it enqueued with
|
||||
* names the root of the chain, not the link it now waits behind. */
|
||||
const owedReceiptStillWaiting = owedReceipts.some(
|
||||
(receipt) => receipt.status === 'queued' || receipt.status === 'claimed',
|
||||
);
|
||||
const receiptBoundary = owedReceipts.reduce<number | null>((boundary, receipt) => {
|
||||
const candidate =
|
||||
receipt.effectivePredecessorCreatedAt ?? receipt.expectedPredecessorCreatedAt ?? null;
|
||||
return candidate != null && (boundary == null || candidate > boundary) ? candidate : boundary;
|
||||
}, null);
|
||||
|
||||
/**
|
||||
* The latch remembers the generation the owed turn waits behind, because
|
||||
* the successor is usually announced while that predecessor is still live:
|
||||
* attached here, or merely listed because another client owns it. Neither
|
||||
* is delivery. The active-job list carries no generation identity at all, so
|
||||
* "the conversation is listed" can never prove the successor exists; only
|
||||
* attachment to a generation other than the predecessor's can. Judging
|
||||
* delivery by anything weaker clears the latch in exactly the window it is
|
||||
* for, and a successor that starts and finishes between two list polls is
|
||||
* then never seen.
|
||||
*
|
||||
* `quietSince` is the absolute start of the expiry window. It begins when
|
||||
* the receipt stops reporting the turn (or when its conversation leaves the
|
||||
* screen, where no receipt is observed at all) and is never pushed back by
|
||||
* a remount, so returning later cannot reopen a spent window.
|
||||
*/
|
||||
type OwedSuccessor = { predecessorCreatedAt: number | null; quietSince: number | null };
|
||||
/** Keyed by conversation: navigating to a conversation that also owes a run
|
||||
* must not evict another's latch, or its expiry (and the history repair it
|
||||
* performs) is lost. */
|
||||
const [owedSuccessors, setOwedSuccessors] = useState<ReadonlyMap<string, OwedSuccessor>>(
|
||||
() => new Map(),
|
||||
);
|
||||
const owedSuccessor =
|
||||
conversationId != null ? (owedSuccessors.get(conversationId) ?? null) : null;
|
||||
const successorOwed =
|
||||
!hasLiveSubmissionForThisConvo && (successorOwedByReceipt || owedSuccessor != null);
|
||||
const { data: activeJobsData, dataUpdatedAt: activeJobsUpdatedAt } = useActiveJobs(
|
||||
resumableEnabled,
|
||||
successorOwed,
|
||||
);
|
||||
const hasActiveJobForThisConvo =
|
||||
!!conversationId &&
|
||||
conversationId !== Constants.NEW_CONVO &&
|
||||
activeJobsData?.activeJobIds?.includes(conversationId) === true;
|
||||
/**
|
||||
* The predecessor a turn actually waits behind is the LATEST link known:
|
||||
* receipts can still name the chain's root long after a sibling's successor
|
||||
* became the live link, and the latch may have learned that link while the
|
||||
* turn was waiting. Generations are timestamps, so "latest" is the max of
|
||||
* what the receipt says, what the latch learned, and, while the turn still
|
||||
* waits, whatever is live right now.
|
||||
*/
|
||||
const knownPredecessorCreatedAt = [
|
||||
owedReceiptStillWaiting ? attachedGenerationCreatedAt : null,
|
||||
receiptBoundary,
|
||||
owedSuccessor?.predecessorCreatedAt ?? null,
|
||||
].reduce<number | null>(
|
||||
(latest, candidate) =>
|
||||
candidate != null && (latest == null || candidate > latest) ? candidate : latest,
|
||||
null,
|
||||
);
|
||||
/** Delivery needs a boundary to compare against. With none known, a live
|
||||
* generation could as easily be the predecessor as the successor, and
|
||||
* guessing "delivered" would silence the poll while the run is still owed;
|
||||
* the expiry window is the bounded fallback for that ambiguity. */
|
||||
const attachedToSuccessor =
|
||||
owedSuccessor != null &&
|
||||
attachedGenerationCreatedAt != null &&
|
||||
!owedReceiptStillWaiting &&
|
||||
knownPredecessorCreatedAt != null &&
|
||||
attachedGenerationCreatedAt !== knownPredecessorCreatedAt;
|
||||
|
||||
useEffect(() => {
|
||||
if (!conversationId) {
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(queuedTurnReceipts)) {
|
||||
const stillMentioned = new Set(queuedTurnReceipts.map((receipt) => receipt.queuedTurnId));
|
||||
setDeliveredTurnIdsByConvo((current) => {
|
||||
const delivered = current.get(conversationId);
|
||||
if (!delivered) {
|
||||
return current;
|
||||
}
|
||||
const kept = [...delivered].filter((turnId) => stillMentioned.has(turnId));
|
||||
if (kept.length === delivered.size) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
if (kept.length === 0) {
|
||||
next.delete(conversationId);
|
||||
} else {
|
||||
next.set(conversationId, new Set(kept));
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
if (attachedToSuccessor) {
|
||||
setDeliveredTurnIdsByConvo((current) => {
|
||||
const next = new Map(current);
|
||||
const merged = new Set(current.get(conversationId) ?? []);
|
||||
for (const receipt of owedReceipts) {
|
||||
merged.add(receipt.queuedTurnId);
|
||||
}
|
||||
next.set(conversationId, merged);
|
||||
return next;
|
||||
});
|
||||
/**
|
||||
* The list may never have seen this run (it can start and finish
|
||||
* between two polls), so its "recently active" clock is stale and would
|
||||
* leave nothing polling once the latch clears. A live generation was
|
||||
* just observed; restart the handover window from it so an unpredicted
|
||||
* continuation after this run is still discovered.
|
||||
*/
|
||||
extendActiveJobsGrace();
|
||||
setOwedSuccessors((current) => {
|
||||
if (!current.has(conversationId)) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.delete(conversationId);
|
||||
return next;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!successorOwedByReceipt) {
|
||||
return;
|
||||
}
|
||||
setOwedSuccessors((current) => {
|
||||
const existing = current.get(conversationId);
|
||||
if (existing == null) {
|
||||
/** The receipt's boundary when it has one; otherwise whatever is live
|
||||
* right now: a turn that is still owed cannot have started, so a live
|
||||
* generation at the moment it is first seen is its predecessor. */
|
||||
const next = new Map(current);
|
||||
next.set(conversationId, {
|
||||
predecessorCreatedAt: knownPredecessorCreatedAt ?? attachedGenerationCreatedAt,
|
||||
quietSince: null,
|
||||
});
|
||||
return next;
|
||||
}
|
||||
/** Still reporting. A boundary learned later (admission consumed it, or a
|
||||
* sibling's successor is now the live link) supersedes what was
|
||||
* guessed. The window is reopened only by an observation newer than the
|
||||
* moment it started: a remount first exposes the cached receipt from
|
||||
* before the turn went quiet, and that stale data must not extend an
|
||||
* expiry that is meant to be absolute. */
|
||||
const refined = knownPredecessorCreatedAt ?? existing.predecessorCreatedAt;
|
||||
const reopen = existing.quietSince != null && queuedTurnsObservedAt > existing.quietSince;
|
||||
if (!reopen && refined === existing.predecessorCreatedAt) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.set(conversationId, {
|
||||
predecessorCreatedAt: refined,
|
||||
quietSince: reopen ? null : existing.quietSince,
|
||||
});
|
||||
return next;
|
||||
});
|
||||
}, [
|
||||
conversationId,
|
||||
queuedTurnReceipts,
|
||||
owedReceipts,
|
||||
successorOwedByReceipt,
|
||||
attachedToSuccessor,
|
||||
knownPredecessorCreatedAt,
|
||||
attachedGenerationCreatedAt,
|
||||
queuedTurnsObservedAt,
|
||||
]);
|
||||
|
||||
/** Only a server observation newer than the window's start counts as the
|
||||
* turn still reporting; a remount's cached receipt predates it. Reporting
|
||||
* is observable only for the conversation on screen. */
|
||||
const owedIsReporting =
|
||||
owedSuccessor != null &&
|
||||
successorOwedByReceipt &&
|
||||
(owedSuccessor.quietSince == null || queuedTurnsObservedAt > owedSuccessor.quietSince);
|
||||
useEffect(() => {
|
||||
const now = Date.now();
|
||||
const timers: ReturnType<typeof setTimeout>[] = [];
|
||||
let needsQuietStamp = false;
|
||||
for (const [owedConversationId, latch] of owedSuccessors) {
|
||||
if (owedConversationId === conversationId && owedIsReporting) {
|
||||
continue;
|
||||
}
|
||||
if (latch.quietSince == null) {
|
||||
needsQuietStamp = true;
|
||||
continue;
|
||||
}
|
||||
const remaining = Math.max(0, ACTIVE_JOBS_SUCCESSOR_GRACE_MS - (now - latch.quietSince));
|
||||
timers.push(
|
||||
setTimeout(() => {
|
||||
setOwedSuccessors((current) => {
|
||||
if (!current.has(owedConversationId)) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.delete(owedConversationId);
|
||||
return next;
|
||||
});
|
||||
/** The window closed without the successor being seen. If it ran
|
||||
* and finished inside a poll gap, its turns are on the server and
|
||||
* nowhere else; one refetch is the whole repair, and marking an
|
||||
* off-screen conversation stale costs nothing until it is opened. */
|
||||
const revealFamily = revealedQueuedTurnFamily(owedConversationId);
|
||||
const pendingReveal = jotaiStore.get(revealFamily);
|
||||
if (
|
||||
pendingReveal != null &&
|
||||
Date.parse(pendingReveal.revealedAt) <= latch.quietSince! &&
|
||||
!isQueuedTurnSuccessorOwed(
|
||||
queryClient.getQueryData(agentQueuedTurnsQueryKey(owedConversationId)),
|
||||
)
|
||||
) {
|
||||
jotaiStore.set(revealFamily, null);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, owedConversationId] });
|
||||
}, remaining),
|
||||
);
|
||||
}
|
||||
if (needsQuietStamp) {
|
||||
setOwedSuccessors((current) => {
|
||||
const next = new Map(current);
|
||||
for (const [owedConversationId, latch] of current) {
|
||||
const reporting = owedConversationId === conversationId && owedIsReporting;
|
||||
if (!reporting && latch.quietSince == null) {
|
||||
next.set(owedConversationId, { ...latch, quietSince: now });
|
||||
}
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
return () => {
|
||||
for (const timer of timers) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
}, [owedSuccessors, owedIsReporting, conversationId, queryClient, jotaiStore]);
|
||||
|
||||
return {
|
||||
queuedTurnReceipts,
|
||||
receiptSignature,
|
||||
successorOwed,
|
||||
hasActiveJobForThisConvo,
|
||||
activeJobsUpdatedAt,
|
||||
};
|
||||
}
|
||||
|
|
@ -1,21 +1,13 @@
|
|||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { resolveDetachedRunEnd } from '@librechat/chat';
|
||||
import { useStore, useAtom, useSetAtom, useAtomValue } from 'jotai';
|
||||
import {
|
||||
isDetachedRunManual,
|
||||
pendingRunEndByConvoId,
|
||||
detachedRunByConvoId,
|
||||
} from '@librechat/chat/react';
|
||||
import { useStore, useSetAtom, useAtomValue } from 'jotai';
|
||||
import {
|
||||
Constants,
|
||||
QueryKeys,
|
||||
isAssistantsEndpoint,
|
||||
isForcedTemporaryRetention,
|
||||
} from 'librechat-data-provider';
|
||||
import {
|
||||
revealedQueuedTurnFamily,
|
||||
activeGenerationCreatedAtByConvoId,
|
||||
useIsSubmitting,
|
||||
submissionStartFamily,
|
||||
|
|
@ -32,30 +24,16 @@ import {
|
|||
buildSubmissionFromResumeState,
|
||||
} from '@librechat/chat';
|
||||
import type { TMessage, Agents } from 'librechat-data-provider';
|
||||
import {
|
||||
useStreamStatus,
|
||||
useActiveJobs,
|
||||
useAgentQueuedTurns,
|
||||
useGetStartupConfig,
|
||||
streamStatusQueryKey,
|
||||
isQueuedTurnSuccessorOwed,
|
||||
extendActiveJobsGrace,
|
||||
ACTIVE_JOBS_SUCCESSOR_GRACE_MS,
|
||||
} from '~/data-provider';
|
||||
import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';
|
||||
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
|
||||
import { dedupeSteersById, getBranchSiblingIndexesForTarget } from '~/utils';
|
||||
import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';
|
||||
import { resumeRequestsAtom } from '~/hooks/Chat/resume';
|
||||
import { useStreamStatus, useGetStartupConfig } from '~/data-provider';
|
||||
import { getBranchSiblingIndexesForTarget } from '~/utils';
|
||||
import useResumeAnnouncements from './announcements';
|
||||
import { settleFinishedRun } from './finished';
|
||||
import useOwedSuccessor from './successor';
|
||||
import useSteerState from './steer';
|
||||
import store from '~/store';
|
||||
|
||||
/**
|
||||
* Matches the active-job list's own poll interval: answering an announcement
|
||||
* faster than the list can change it would only re-read the same snapshot.
|
||||
*/
|
||||
const ACTIVE_JOB_REARM_INTERVAL_MS = 5_000;
|
||||
|
||||
/**
|
||||
* Hook to resume streaming if navigating to a conversation with active generation.
|
||||
* Checks stream status via React Query and sets submission if active job found.
|
||||
|
|
@ -100,9 +78,6 @@ export default function useResumeOnLoad(
|
|||
* repeatable, and still one status read per interval at worst.
|
||||
*/
|
||||
const answeredActiveJobRef = useRef<{ conversationId: string; at: number } | null>(null);
|
||||
/** Receipt state at the last arm, so history is refetched on a transition
|
||||
* rather than on every heartbeat of an unchanged queued turn. */
|
||||
const lastArmedReceiptSignatureRef = useRef<string | null>(null);
|
||||
/**
|
||||
* Bumped when an announcement is answered. Clearing `processedConvoRef` alone
|
||||
* cannot restart the check below: a ref mutation neither schedules a render
|
||||
|
|
@ -131,17 +106,10 @@ export default function useResumeOnLoad(
|
|||
[jotaiStore],
|
||||
);
|
||||
|
||||
/** Restore pending-steer chips for steers the server still has queued
|
||||
* (injected ones already live inside the resumed aggregatedContent). Always
|
||||
* reconcile against the server's still-queued list (mirrors the sync-path
|
||||
* re-seed in useResumableSSE): a steer applied while this client was away is
|
||||
* absent there, so an EMPTY list must clear stale local pending chips. */
|
||||
const {
|
||||
seedSteerChips: restoreSteerChips,
|
||||
settleAppliedSteerParts,
|
||||
convertSteersToQueued,
|
||||
updateActiveGenerationCreatedAt,
|
||||
} = useSteerState();
|
||||
/** Pending-steer chips for steers the server still has queued (injected ones
|
||||
* already live inside the resumed aggregatedContent). */
|
||||
const steerState = useSteerState();
|
||||
const { seedSteerChips, settleAppliedSteerParts, updateActiveGenerationCreatedAt } = steerState;
|
||||
|
||||
// Check for active stream when conversation changes
|
||||
const submissionConvoId = currentSubmission?.conversation?.conversationId;
|
||||
|
|
@ -172,332 +140,13 @@ export default function useResumeOnLoad(
|
|||
const hasLiveSubmissionForThisConvo =
|
||||
hasActiveSubmissionForThisConvo && (isSubmitting || attachedGenerationCreatedAt != null);
|
||||
|
||||
/**
|
||||
* A run this pane did not start — another tab, another device, a scheduled
|
||||
* trigger — announces itself only through the user-scoped active-job list,
|
||||
* which already polls while anything is running and refetches on focus. The
|
||||
* status query is the one thing that could turn that into an attachment, and
|
||||
* it switches off for the rest of this conversation's mount the moment it has
|
||||
* answered inactive once. Without a re-arm the pane sits on history it cannot
|
||||
* see has moved on, and a send from here forks a branch off a stale tail.
|
||||
*
|
||||
* Consumed once per run rather than held open: a job stays listed for its
|
||||
* whole lifetime, and re-opening the query on every poll would turn a
|
||||
* five-second heartbeat into a five-second status read.
|
||||
*/
|
||||
/**
|
||||
* `dataUpdatedAt` is the trigger, not `activeJobsData`. React Query keeps the
|
||||
* previous reference when a refetch is deep-equal, and a run that stays
|
||||
* listed produces exactly that — so the derived boolean below never changes
|
||||
* and an effect keyed on it would run once and never again. The stamp moves
|
||||
* on every fetch, which is the heartbeat this needs.
|
||||
*/
|
||||
/**
|
||||
* Positive evidence that the backend owes this conversation a run: a queued
|
||||
* turn it has taken ownership of. The client never builds a submission for
|
||||
* one — `useQueueDrain` declines the boundary — so the resulting run has to
|
||||
* be recognised from the active-job list, and finishing the previous run is
|
||||
* what empties that list. Saying so outright keeps the listening exact
|
||||
* instead of betting on how long admission takes.
|
||||
*
|
||||
* Read from the server's own receipts rather than the chip projection, so
|
||||
* admission semantics stay beside the queue's own predicate instead of being
|
||||
* restated here. `useSteering` owns the fetch; subscribing with
|
||||
* `enabled: false` observes that cache without competing for it, and an
|
||||
* unpopulated cache simply falls back to the handover window.
|
||||
*/
|
||||
const { data: queuedTurnReceipts, dataUpdatedAt: queuedTurnsObservedAt } = useAgentQueuedTurns(
|
||||
conversationId ?? '',
|
||||
false,
|
||||
);
|
||||
/**
|
||||
* Receipts whose successor this pane has already attached to, per turn and
|
||||
* per conversation. An `admitted` receipt stays in the cache for a fetch
|
||||
* after delivery and still reads as owed; without remembering it was
|
||||
* delivered, the latch would clear on the attachment and be re-recorded from
|
||||
* the same receipt on the next render, synchronously, until the cache moved
|
||||
* on. Tracked per turn rather than per receipt set: two turns queued behind
|
||||
* the same generation deliver one at a time, and marking the whole set on
|
||||
* the first would let the still-attached first successor pass as delivery of
|
||||
* the second. Entries are pruned once the conversation's receipts no longer
|
||||
* mention them.
|
||||
*/
|
||||
const [deliveredTurnIdsByConvo, setDeliveredTurnIdsByConvo] = useState<
|
||||
ReadonlyMap<string, ReadonlySet<string>>
|
||||
>(() => new Map());
|
||||
/**
|
||||
* What the receipts say, reduced to the two facts this hook keys on. The
|
||||
* signature drives re-arming: a `queued` receipt is refetched every two
|
||||
* seconds during a long wait and advances `dataUpdatedAt` without changing,
|
||||
* and re-arming on that heartbeat would invalidate status and history every
|
||||
* throttle interval while nothing has happened. Only a transition — a status
|
||||
* change, a receipt appearing or being dropped — is an announcement.
|
||||
*
|
||||
* The boundary is the generation the owed turn waits behind, which the
|
||||
* receipt knows better than this pane does: `expectedPredecessorCreatedAt`
|
||||
* is stamped from the live epoch at enqueue, and `effectivePredecessorCreatedAt`
|
||||
* is the boundary admission actually consumed once turns chain. It is what
|
||||
* lets delivery be judged by generation rather than by "some run exists".
|
||||
*/
|
||||
const receiptSignature = Array.isArray(queuedTurnReceipts)
|
||||
? queuedTurnReceipts
|
||||
.map((receipt) => `${receipt.queuedTurnId}:${receipt.status}`)
|
||||
.sort()
|
||||
.join('|')
|
||||
: '';
|
||||
const deliveredTurnIds =
|
||||
conversationId != null ? deliveredTurnIdsByConvo.get(conversationId) : undefined;
|
||||
const owedReceipts = useMemo(
|
||||
() =>
|
||||
Array.isArray(queuedTurnReceipts)
|
||||
? queuedTurnReceipts.filter(
|
||||
(receipt) =>
|
||||
!deliveredTurnIds?.has(receipt.queuedTurnId) && isQueuedTurnSuccessorOwed([receipt]),
|
||||
)
|
||||
: [],
|
||||
[queuedTurnReceipts, deliveredTurnIds],
|
||||
);
|
||||
const successorOwedByReceipt = owedReceipts.length > 0;
|
||||
/** A turn still `queued`/`claimed` has not produced a run: whatever generation
|
||||
* is live right now is its predecessor — including a sibling turn's
|
||||
* successor that this pane is attached to. The boundary it enqueued with
|
||||
* names the root of the chain, not the link it now waits behind. */
|
||||
const owedReceiptStillWaiting = owedReceipts.some(
|
||||
(receipt) => receipt.status === 'queued' || receipt.status === 'claimed',
|
||||
);
|
||||
const receiptBoundary = owedReceipts.reduce<number | null>((boundary, receipt) => {
|
||||
const candidate =
|
||||
receipt.effectivePredecessorCreatedAt ?? receipt.expectedPredecessorCreatedAt ?? null;
|
||||
return candidate != null && (boundary == null || candidate > boundary) ? candidate : boundary;
|
||||
}, null);
|
||||
|
||||
/**
|
||||
* The latch remembers the generation the owed turn waits behind, because
|
||||
* the successor is usually announced while that predecessor is still live —
|
||||
* attached here, or merely listed because another client owns it. Neither
|
||||
* is delivery. The active-job list carries no generation identity at all, so
|
||||
* "the conversation is listed" can never prove the successor exists; only
|
||||
* attachment to a generation other than the predecessor's can. Judging
|
||||
* delivery by anything weaker clears the latch in exactly the window it is
|
||||
* for, and a successor that starts and finishes between two list polls is
|
||||
* then never seen.
|
||||
*
|
||||
* `quietSince` is the absolute start of the expiry window. It begins when
|
||||
* the receipt stops reporting the turn — or when its conversation leaves the
|
||||
* screen, where no receipt is observed at all — and is never pushed back by
|
||||
* a remount, so returning later cannot reopen a spent window.
|
||||
*/
|
||||
type OwedSuccessor = { predecessorCreatedAt: number | null; quietSince: number | null };
|
||||
/** Keyed by conversation: navigating to a conversation that also owes a run
|
||||
* must not evict another's latch, or its expiry — and the history repair it
|
||||
* performs — is lost. */
|
||||
const [owedSuccessors, setOwedSuccessors] = useState<ReadonlyMap<string, OwedSuccessor>>(
|
||||
() => new Map(),
|
||||
);
|
||||
const owedSuccessor =
|
||||
conversationId != null ? (owedSuccessors.get(conversationId) ?? null) : null;
|
||||
const successorOwed =
|
||||
!hasLiveSubmissionForThisConvo && (successorOwedByReceipt || owedSuccessor != null);
|
||||
const { data: activeJobsData, dataUpdatedAt: activeJobsUpdatedAt } = useActiveJobs(
|
||||
resumableEnabled,
|
||||
successorOwed,
|
||||
);
|
||||
const hasActiveJobForThisConvo =
|
||||
!!conversationId &&
|
||||
conversationId !== Constants.NEW_CONVO &&
|
||||
activeJobsData?.activeJobIds?.includes(conversationId) === true;
|
||||
/**
|
||||
* The predecessor a turn actually waits behind is the LATEST link known:
|
||||
* receipts can still name the chain's root long after a sibling's successor
|
||||
* became the live link, and the latch may have learned that link while the
|
||||
* turn was waiting. Generations are timestamps, so "latest" is the max of
|
||||
* what the receipt says, what the latch learned, and — while the turn still
|
||||
* waits — whatever is live right now.
|
||||
*/
|
||||
const knownPredecessorCreatedAt = [
|
||||
owedReceiptStillWaiting ? attachedGenerationCreatedAt : null,
|
||||
receiptBoundary,
|
||||
owedSuccessor?.predecessorCreatedAt ?? null,
|
||||
].reduce<number | null>(
|
||||
(latest, candidate) =>
|
||||
candidate != null && (latest == null || candidate > latest) ? candidate : latest,
|
||||
null,
|
||||
);
|
||||
/** Delivery needs a boundary to compare against. With none known, a live
|
||||
* generation could as easily be the predecessor as the successor, and
|
||||
* guessing "delivered" would silence the poll while the run is still owed;
|
||||
* the expiry window is the bounded fallback for that ambiguity. */
|
||||
const attachedToSuccessor =
|
||||
owedSuccessor != null &&
|
||||
attachedGenerationCreatedAt != null &&
|
||||
!owedReceiptStillWaiting &&
|
||||
knownPredecessorCreatedAt != null &&
|
||||
attachedGenerationCreatedAt !== knownPredecessorCreatedAt;
|
||||
|
||||
useEffect(() => {
|
||||
if (!conversationId) {
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(queuedTurnReceipts)) {
|
||||
const stillMentioned = new Set(queuedTurnReceipts.map((receipt) => receipt.queuedTurnId));
|
||||
setDeliveredTurnIdsByConvo((current) => {
|
||||
const delivered = current.get(conversationId);
|
||||
if (!delivered) {
|
||||
return current;
|
||||
}
|
||||
const kept = [...delivered].filter((turnId) => stillMentioned.has(turnId));
|
||||
if (kept.length === delivered.size) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
if (kept.length === 0) {
|
||||
next.delete(conversationId);
|
||||
} else {
|
||||
next.set(conversationId, new Set(kept));
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
if (attachedToSuccessor) {
|
||||
setDeliveredTurnIdsByConvo((current) => {
|
||||
const next = new Map(current);
|
||||
const merged = new Set(current.get(conversationId) ?? []);
|
||||
for (const receipt of owedReceipts) {
|
||||
merged.add(receipt.queuedTurnId);
|
||||
}
|
||||
next.set(conversationId, merged);
|
||||
return next;
|
||||
});
|
||||
/**
|
||||
* The list may never have seen this run — it can start and finish
|
||||
* between two polls — so its "recently active" clock is stale and would
|
||||
* leave nothing polling once the latch clears. A live generation was
|
||||
* just observed; restart the handover window from it so an unpredicted
|
||||
* continuation after this run is still discovered.
|
||||
*/
|
||||
extendActiveJobsGrace();
|
||||
setOwedSuccessors((current) => {
|
||||
if (!current.has(conversationId)) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.delete(conversationId);
|
||||
return next;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!successorOwedByReceipt) {
|
||||
return;
|
||||
}
|
||||
setOwedSuccessors((current) => {
|
||||
const existing = current.get(conversationId);
|
||||
if (existing == null) {
|
||||
/** The receipt's boundary when it has one; otherwise whatever is live
|
||||
* right now — a turn that is still owed cannot have started, so a live
|
||||
* generation at the moment it is first seen is its predecessor. */
|
||||
const next = new Map(current);
|
||||
next.set(conversationId, {
|
||||
predecessorCreatedAt: knownPredecessorCreatedAt ?? attachedGenerationCreatedAt,
|
||||
quietSince: null,
|
||||
});
|
||||
return next;
|
||||
}
|
||||
/** Still reporting. A boundary learned later (admission consumed it, or a
|
||||
* sibling's successor is now the live link) supersedes what was
|
||||
* guessed. The window is reopened only by an observation newer than the
|
||||
* moment it started: a remount first exposes the cached receipt from
|
||||
* before the turn went quiet, and that stale data must not extend an
|
||||
* expiry that is meant to be absolute. */
|
||||
const refined = knownPredecessorCreatedAt ?? existing.predecessorCreatedAt;
|
||||
const reopen = existing.quietSince != null && queuedTurnsObservedAt > existing.quietSince;
|
||||
if (!reopen && refined === existing.predecessorCreatedAt) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.set(conversationId, {
|
||||
predecessorCreatedAt: refined,
|
||||
quietSince: reopen ? null : existing.quietSince,
|
||||
});
|
||||
return next;
|
||||
});
|
||||
}, [
|
||||
const owed = useOwedSuccessor({
|
||||
conversationId,
|
||||
queuedTurnReceipts,
|
||||
owedReceipts,
|
||||
successorOwedByReceipt,
|
||||
attachedToSuccessor,
|
||||
knownPredecessorCreatedAt,
|
||||
resumableEnabled,
|
||||
attachedGenerationCreatedAt,
|
||||
queuedTurnsObservedAt,
|
||||
]);
|
||||
|
||||
/** Only a server observation newer than the window's start counts as the
|
||||
* turn still reporting; a remount's cached receipt predates it. Reporting
|
||||
* is observable only for the conversation on screen. */
|
||||
const owedIsReporting =
|
||||
owedSuccessor != null &&
|
||||
successorOwedByReceipt &&
|
||||
(owedSuccessor.quietSince == null || queuedTurnsObservedAt > owedSuccessor.quietSince);
|
||||
useEffect(() => {
|
||||
const now = Date.now();
|
||||
const timers: ReturnType<typeof setTimeout>[] = [];
|
||||
let needsQuietStamp = false;
|
||||
for (const [owedConversationId, latch] of owedSuccessors) {
|
||||
if (owedConversationId === conversationId && owedIsReporting) {
|
||||
continue;
|
||||
}
|
||||
if (latch.quietSince == null) {
|
||||
needsQuietStamp = true;
|
||||
continue;
|
||||
}
|
||||
const remaining = Math.max(0, ACTIVE_JOBS_SUCCESSOR_GRACE_MS - (now - latch.quietSince));
|
||||
timers.push(
|
||||
setTimeout(() => {
|
||||
setOwedSuccessors((current) => {
|
||||
if (!current.has(owedConversationId)) {
|
||||
return current;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.delete(owedConversationId);
|
||||
return next;
|
||||
});
|
||||
/** The window closed without the successor being seen. If it ran
|
||||
* and finished inside a poll gap, its turns are on the server and
|
||||
* nowhere else; one refetch is the whole repair, and marking an
|
||||
* off-screen conversation stale costs nothing until it is opened. */
|
||||
const revealFamily = revealedQueuedTurnFamily(owedConversationId);
|
||||
const pendingReveal = jotaiStore.get(revealFamily);
|
||||
if (
|
||||
pendingReveal != null &&
|
||||
Date.parse(pendingReveal.revealedAt) <= latch.quietSince! &&
|
||||
!isQueuedTurnSuccessorOwed(
|
||||
queryClient.getQueryData(agentQueuedTurnsQueryKey(owedConversationId)),
|
||||
)
|
||||
) {
|
||||
jotaiStore.set(revealFamily, null);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, owedConversationId] });
|
||||
}, remaining),
|
||||
);
|
||||
}
|
||||
if (needsQuietStamp) {
|
||||
setOwedSuccessors((current) => {
|
||||
const next = new Map(current);
|
||||
for (const [owedConversationId, latch] of current) {
|
||||
const reporting = owedConversationId === conversationId && owedIsReporting;
|
||||
if (!reporting && latch.quietSince == null) {
|
||||
next.set(owedConversationId, { ...latch, quietSince: now });
|
||||
}
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
return () => {
|
||||
for (const timer of timers) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
}, [owedSuccessors, owedIsReporting, conversationId, queryClient, jotaiStore]);
|
||||
hasLiveSubmissionForThisConvo,
|
||||
});
|
||||
const { queuedTurnReceipts } = owed;
|
||||
|
||||
const shouldCheck =
|
||||
resumableEnabled &&
|
||||
|
|
@ -624,85 +273,17 @@ export default function useResumeOnLoad(
|
|||
|
||||
if (!streamStatus.active || !streamStatus.streamId) {
|
||||
console.log('[ResumeOnLoad] No active job to resume for:', conversationId);
|
||||
const revealFamily = revealedQueuedTurnFamily(conversationId);
|
||||
const pendingReveal = jotaiStore.get(revealFamily);
|
||||
/** A remounted pane may have missed attachment and the quiet-window
|
||||
* timer. An expired job has no epoch; require a fresh inactive read
|
||||
* after restored history before retiring its surviving handoff guard. */
|
||||
const historyUpdatedAt = queryClient.getQueryState([
|
||||
QueryKeys.messages,
|
||||
settleFinishedRun({
|
||||
jotaiStore,
|
||||
queryClient,
|
||||
steers: steerState,
|
||||
conversationId,
|
||||
])?.dataUpdatedAt;
|
||||
if (
|
||||
pendingReveal != null &&
|
||||
streamStatus.active === false &&
|
||||
streamStatus.createdAt == null &&
|
||||
historyUpdatedAt != null &&
|
||||
streamStatusUpdatedAt >= historyUpdatedAt &&
|
||||
streamStatusUpdatedAt > Date.parse(pendingReveal.revealedAt) &&
|
||||
!isQueuedTurnSuccessorOwed(queuedTurnReceipts) &&
|
||||
(getMessages() ?? []).some(
|
||||
(message) => message.parentMessageId === pendingReveal.parentMessageId,
|
||||
)
|
||||
) {
|
||||
jotaiStore.set(revealFamily, null);
|
||||
}
|
||||
|
||||
// A terminal drain may have parked acknowledged steers no subscriber
|
||||
// received (tab closed / reload racing the final event) — the status
|
||||
// claim returns them exactly once; restore as queued follow-up chips.
|
||||
// An expired pendingAction can report inactive BEFORE the sweeper parks
|
||||
// the steer queue: those steers still ride resumeState.pendingSteers,
|
||||
// so convert both lists (id-deduped) before the empty seed clears chips.
|
||||
const leftoverSteers = dedupeSteersById(
|
||||
streamStatus.unrecoveredSteers,
|
||||
streamStatus.resumeState?.pendingSteers,
|
||||
);
|
||||
if (conversationId && leftoverSteers.length > 0) {
|
||||
convertSteersToQueued(conversationId, leftoverSteers, {
|
||||
generationProtocolVersion,
|
||||
});
|
||||
}
|
||||
// The run is terminal, so any remaining local pending chip is stale:
|
||||
// its steer either applied (inline part in the saved message) or rode
|
||||
// `unrecoveredSteers` above — same empty-list reconcile as the resume path.
|
||||
settleAppliedSteerParts(conversationId, getMessages());
|
||||
restoreSteerChips(conversationId, undefined);
|
||||
/** A run this pane stopped watching when the user left has ended on the server, which
|
||||
* deleted the job. Its persisted response tells how it ended; parking that end lets the
|
||||
* queue drain send a follow-up queued during the run, as an attached run would have.
|
||||
* The cached history still holds the optimistic response the pane left, which lacks the
|
||||
* persisted stop and error flags, so only a successful refetch of history may resolve it;
|
||||
* when that fails or predates the saved response, the next visit tries again. */
|
||||
const detachedFamily = detachedRunByConvoId(conversationId);
|
||||
const detachedRun = jotaiStore.get(detachedFamily);
|
||||
const parkDetachedEnd = () => {
|
||||
if (detachedRun == null || jotaiStore.get(detachedFamily) !== detachedRun) {
|
||||
return;
|
||||
}
|
||||
if (isDetachedRunManual(jotaiStore.get, conversationId, detachedRun)) {
|
||||
jotaiStore.set(detachedFamily, null);
|
||||
return;
|
||||
}
|
||||
const end = resolveDetachedRunEnd(conversationId, detachedRun, getMessages());
|
||||
if (end == null) {
|
||||
return;
|
||||
}
|
||||
jotaiStore.set(detachedFamily, null);
|
||||
jotaiStore.set(pendingRunEndByConvoId(conversationId), end);
|
||||
};
|
||||
/** A run left for a manual send is retired without a read instead of refetching on every
|
||||
* visit; the check repeats after the read, since a receipt can settle meanwhile. */
|
||||
if (detachedRun != null && isDetachedRunManual(jotaiStore.get, conversationId, detachedRun)) {
|
||||
jotaiStore.set(detachedFamily, null);
|
||||
} else if (detachedRun != null) {
|
||||
queryClient
|
||||
.refetchQueries(
|
||||
{ queryKey: [QueryKeys.messages, conversationId], exact: true },
|
||||
{ throwOnError: true },
|
||||
)
|
||||
.then(parkDetachedEnd, () => undefined);
|
||||
}
|
||||
streamStatus,
|
||||
streamStatusUpdatedAt,
|
||||
queuedTurnReceipts,
|
||||
getMessages,
|
||||
generationProtocolVersion,
|
||||
});
|
||||
processedConvoRef.current = conversationId;
|
||||
return;
|
||||
}
|
||||
|
|
@ -753,9 +334,9 @@ export default function useResumeOnLoad(
|
|||
// Build submission from resume state if available
|
||||
if (streamStatus.resumeState) {
|
||||
restoreResumeBranch(streamStatus.resumeState, messages, conversationId);
|
||||
restoreSteerChips(
|
||||
seedSteerChips(
|
||||
conversationId,
|
||||
streamStatus.resumeState.pendingSteers,
|
||||
streamStatus.resumeState.pendingSteers ?? [],
|
||||
streamStatus.createdAt,
|
||||
generationProtocolVersion,
|
||||
);
|
||||
|
|
@ -807,9 +388,9 @@ export default function useResumeOnLoad(
|
|||
setSubmission,
|
||||
setSubmissionStart,
|
||||
restoreResumeBranch,
|
||||
restoreSteerChips,
|
||||
steerState,
|
||||
seedSteerChips,
|
||||
settleAppliedSteerParts,
|
||||
convertSteersToQueued,
|
||||
updateActiveGenerationCreatedAt,
|
||||
jotaiStore,
|
||||
externalRunArm,
|
||||
|
|
@ -831,140 +412,19 @@ export default function useResumeOnLoad(
|
|||
}
|
||||
}, [conversationId]);
|
||||
|
||||
/**
|
||||
* Answer an active-job announcement for the conversation on screen.
|
||||
*
|
||||
* The announcement means "your history may have moved", not merely "re-open
|
||||
* the status query", so both reads this pane is about to make are forced
|
||||
* fresh first. Toggling `enabled` alone would not do it: an inactive status
|
||||
* answered less than `staleTime` ago is still fresh, and React Query would
|
||||
* hand back that cached "nothing running" and let the effect above record the
|
||||
* announcement as handled without ever attaching.
|
||||
*
|
||||
* History is invalidated for the same reason, and it matters whichever way
|
||||
* the status read lands. An external client may have completed whole turns
|
||||
* this pane never saw before starting the one now running: the resume
|
||||
* submission and `finalHandler` both build on this snapshot, so attaching
|
||||
* without it grafts the live turn onto a hole. And when the run turns out to
|
||||
* have already finished, the refetch is the entire repair — the messages
|
||||
* query disables refetch on focus, mount and reconnect, so nothing else would
|
||||
* ever collect those turns. The refetch also re-gates `messagesLoaded`, which
|
||||
* defers the effect above until the authoritative history has landed.
|
||||
*/
|
||||
/**
|
||||
* Two announcement sources, one arm. The active-job list is the general one.
|
||||
* An owed queued turn is the specific one, and it must be able to arm on its
|
||||
* own: the run it announces can start and finish between two list polls, and
|
||||
* the status read this arm enables is the authoritative answer either way —
|
||||
* active attaches, inactive means the history refetch below is the repair.
|
||||
*/
|
||||
const successorAnnounced = hasActiveJobForThisConvo || successorOwed;
|
||||
useEffect(() => {
|
||||
if (!successorAnnounced) {
|
||||
answeredActiveJobRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (!resumableEnabled || !conversationId || hasLiveSubmissionForThisConvo) {
|
||||
return;
|
||||
}
|
||||
|
||||
const answered = answeredActiveJobRef.current;
|
||||
const now = Date.now();
|
||||
if (
|
||||
answered != null &&
|
||||
answered.conversationId === conversationId &&
|
||||
now - answered.at < ACTIVE_JOB_REARM_INTERVAL_MS
|
||||
) {
|
||||
return;
|
||||
}
|
||||
answeredActiveJobRef.current = { conversationId, at: now };
|
||||
|
||||
console.log('[ResumeOnLoad] Active job announced without an attachment', { conversationId });
|
||||
/**
|
||||
* A finished submission still installed here is the one thing standing
|
||||
* between this announcement and the attachment: it is what the check below
|
||||
* reads as "already attached", and what disables the status query. The run
|
||||
* it describes is over, so release it — a server-side continuation such as
|
||||
* a background tool dispatch finishing is a different generation, and the
|
||||
* resume path is what owns building the submission for it.
|
||||
*/
|
||||
if (hasActiveSubmissionForThisConvo) {
|
||||
setSubmission(null);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: streamStatusQueryKey(conversationId) });
|
||||
/**
|
||||
* History is refetched only when this arm can actually change what the
|
||||
* pane shows: a run is listed (attachment is imminent and must not be
|
||||
* grafted onto a stale snapshot), or the receipts just transitioned (the
|
||||
* turn was admitted, or dropped after finishing inside a poll gap). A
|
||||
* long wait behind an unadmitted turn re-arms status on the heartbeat but
|
||||
* must not download the conversation again each time.
|
||||
*/
|
||||
const receiptsTransitioned = lastArmedReceiptSignatureRef.current !== receiptSignature;
|
||||
lastArmedReceiptSignatureRef.current = receiptSignature;
|
||||
if (hasActiveJobForThisConvo || receiptsTransitioned) {
|
||||
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, conversationId] });
|
||||
}
|
||||
processedConvoRef.current = null;
|
||||
setExternalRunArm((arm) => arm + 1);
|
||||
}, [
|
||||
/** The route can name a conversation before this pane has loaded it; until then the endpoint
|
||||
* that decides resumability is the previous conversation's, so the request waits. */
|
||||
useResumeAnnouncements({
|
||||
conversationId,
|
||||
resumableEnabled,
|
||||
successorAnnounced,
|
||||
hasActiveJobForThisConvo,
|
||||
owed,
|
||||
processedConvoRef,
|
||||
answeredActiveJobRef,
|
||||
setExternalRunArm,
|
||||
setSubmission,
|
||||
routeConversationLoaded: currentConversation?.conversationId === conversationId,
|
||||
hasActiveSubmissionForThisConvo,
|
||||
hasLiveSubmissionForThisConvo,
|
||||
attachedGenerationCreatedAt,
|
||||
activeJobsUpdatedAt,
|
||||
receiptSignature,
|
||||
setSubmission,
|
||||
queryClient,
|
||||
]);
|
||||
|
||||
/**
|
||||
* An explicit `resumeStream` request takes the announcement's path: the
|
||||
* status read decides whether anything is running, and the effect above
|
||||
* builds the resume submission that `useResumableSSE` attaches through the
|
||||
* host transport. The request is consumed either way: one made while this
|
||||
* pane is already attached is answered by that attachment.
|
||||
*/
|
||||
const [resumeRequests, setResumeRequests] = useAtom(resumeRequestsAtom);
|
||||
const resumeRequested = !!conversationId && resumeRequests.has(conversationId);
|
||||
/** The route can name a conversation before this pane has loaded it; until then the endpoint
|
||||
* that decides resumability is the previous conversation's, so the request waits. */
|
||||
const routeConversationLoaded = currentConversation?.conversationId === conversationId;
|
||||
useEffect(() => {
|
||||
if (!resumeRequested || !conversationId || !routeConversationLoaded) {
|
||||
return;
|
||||
}
|
||||
setResumeRequests((pending) => {
|
||||
const next = new Set(pending);
|
||||
next.delete(conversationId);
|
||||
return next;
|
||||
});
|
||||
if (!resumableEnabled || conversationId === Constants.NEW_CONVO) {
|
||||
return;
|
||||
}
|
||||
if (hasLiveSubmissionForThisConvo) {
|
||||
return;
|
||||
}
|
||||
/** A finished submission still installed reads as attached to the check above. */
|
||||
if (hasActiveSubmissionForThisConvo) {
|
||||
setSubmission(null);
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: streamStatusQueryKey(conversationId) });
|
||||
queryClient.invalidateQueries({ queryKey: [QueryKeys.messages, conversationId] });
|
||||
processedConvoRef.current = null;
|
||||
setExternalRunArm((arm) => arm + 1);
|
||||
}, [
|
||||
conversationId,
|
||||
resumeRequested,
|
||||
routeConversationLoaded,
|
||||
setResumeRequests,
|
||||
resumableEnabled,
|
||||
hasActiveSubmissionForThisConvo,
|
||||
hasLiveSubmissionForThisConvo,
|
||||
setSubmission,
|
||||
queryClient,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue