@@ -29,7 +29,9 @@ import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
2929import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments" ;
3030import { nextPastedTextFileName , pastedTextDisposition } from "@t3tools/client-runtime/text-paste" ;
3131import {
32+ EMPTY_ENVIRONMENT_THREAD_STATE ,
3233 parseCodexFeedbackCommand ,
34+ queuedRunEditOwnership ,
3335 submitCodexFeedback ,
3436 type CodexFeedbackSubmission ,
3537} from "@t3tools/client-runtime/state/threads" ;
@@ -112,6 +114,7 @@ import { useAtomCommand } from "./use-atom-command";
112114const EMPTY_QUEUE_WORKFLOW_ATOM = Atom . make < null > ( null ) . pipe (
113115 Atom . withLabel ( "mobile-thread-queue-workflow:empty" ) ,
114116) ;
117+ const EMPTY_THREAD_STATE_ATOM = Atom . make ( EMPTY_ENVIRONMENT_THREAD_STATE ) ;
115118
116119export function appendReviewCommentToDraft ( input : {
117120 readonly environmentId : EnvironmentId ;
@@ -183,6 +186,14 @@ export function useThreadComposerState() {
183186 selectedEnvironmentRuntime,
184187 } = useThreadSelection ( ) ;
185188 const selectedThreadProjection = useSelectedThreadProjection ( ) ;
189+ const selectedThreadState = useAtomValue (
190+ selectedThreadShell === null
191+ ? EMPTY_THREAD_STATE_ATOM
192+ : environmentThreadDetails . stateAtom ( {
193+ environmentId : selectedThreadShell . environmentId ,
194+ threadId : selectedThreadShell . id ,
195+ } ) ,
196+ ) ;
186197 const selectedThreadVisibleTurnItems = useSelectedThreadVisibleTurnItems ( ) ;
187198 const composerDrafts = useAtomValue ( composerDraftsAtom ) ;
188199 const acknowledgedMessages = useAtomValue ( acknowledgedThreadMessagesAtom ) ;
@@ -430,19 +441,14 @@ export function useThreadComposerState() {
430441 // Another client can start or cancel the run, or end or take over its edit,
431442 // while its message is open. Leave edit mode rather than saving into a run the
432443 // server will refuse, and keep whatever was typed if there is room for it.
433- const selectedThreadRuns = selectedThreadProjection ?. projection . runs ;
434444 const editedRunId = queuedRunEdit ?. runId ?? null ;
435- const editedRunEditId = queuedRunEdit ?. editId ?? null ;
445+ const editOwnership =
446+ queuedRunEdit === null ? null : queuedRunEditOwnership ( selectedThreadState , queuedRunEdit ) ;
436447 useEffect ( ( ) => {
437- if ( selectedThreadKey === null || editedRunId === null || selectedThreadRuns === undefined ) {
448+ if ( selectedThreadKey === null || editedRunId === null || editOwnership !== "lost" ) {
438449 return ;
439450 }
440451 if ( isSavingQueuedEdit || savingQueuedEditRef . current ) return ;
441- const stillOwned = selectedThreadRuns . some (
442- ( run ) =>
443- run . id === editedRunId && run . status === "queued" && run . queueEditId === editedRunEditId ,
444- ) ;
445- if ( stillOwned ) return ;
446452 const editDraftKey = queuedEditDraftKey ( selectedThreadKey , editedRunId ) ;
447453 const editDraft = getComposerDraftSnapshot ( editDraftKey ) ;
448454 const threadDraft = getComposerDraftSnapshot ( selectedThreadKey ) ;
@@ -464,7 +470,7 @@ export function useThreadComposerState() {
464470 ? "That queued message edit ended. Your edit is back in the composer."
465471 : "That queued message edit ended, so the edit was discarded." ,
466472 ) ;
467- } , [ editedRunId , editedRunEditId , isSavingQueuedEdit , selectedThreadKey , selectedThreadRuns ] ) ;
473+ } , [ editedRunId , editOwnership , isSavingQueuedEdit , selectedThreadKey ] ) ;
468474
469475 const activeThreadBusy = threadRuntimeIsActive ( selectedThreadRuntime ) ;
470476 const interruptibleRunId = threadRuntimeHasInterruptibleRun ( selectedThreadRuntime )
@@ -478,9 +484,8 @@ export function useThreadComposerState() {
478484 savingQueuedEditRef . current = true ;
479485 setIsSavingQueuedEdit ( true ) ;
480486 try {
481- const currentRun = selectedThreadRuns ?. find ( ( run ) => run . id === edit . runId ) ;
482487 const result =
483- currentRun !== undefined && currentRun . queueEditId !== edit . editId
488+ queuedRunEditOwnership ( selectedThreadState , edit ) === "lost"
484489 ? null
485490 : await cancelQueuedEdit ( {
486491 environmentId : selectedThreadShell . environmentId ,
@@ -491,7 +496,7 @@ export function useThreadComposerState() {
491496 savingQueuedEditRef . current = false ;
492497 setIsSavingQueuedEdit ( false ) ;
493498 }
494- } , [ cancelQueuedEdit , selectedThreadKey , selectedThreadRuns , selectedThreadShell ] ) ;
499+ } , [ cancelQueuedEdit , selectedThreadKey , selectedThreadState , selectedThreadShell ] ) ;
495500
496501 const onRemoveQueuedEditAttachment = useCallback (
497502 ( attachmentId : string ) => {
0 commit comments