The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | import { changeRequestTermKind } from "../lib/changeRequestTerm"; |
| 3 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 4 | import * as Dialog from "../components/ui/Dialog"; |
| 5 | import { |
| 6 | type EnvironmentProject, |
| 7 | type EnvironmentThreadShell, |
| 8 | } from "@t3tools/client-runtime/state/shell"; |
| 9 | import { |
| 10 | type AtomCommand, |
| 11 | type AtomCommandResult, |
| 12 | reportAtomCommandResult, |
| 13 | } from "@t3tools/client-runtime/state/runtime"; |
| 14 | import { threadFamilyRead, type ThreadFamilyRead } from "@t3tools/client-runtime/state/threads"; |
| 15 | import { deriveThreadTitleSeed } from "@t3tools/client-runtime/operations"; |
| 16 | import { managedRuntimeModeDenial } from "@t3tools/client-runtime/contract-labels"; |
| 17 | import { |
| 18 | AuthOrchestrationOperateScope, |
| 19 | CommandId, |
| 20 | DEFAULT_PROVIDER_INTERACTION_MODE, |
| 21 | DEFAULT_RUNTIME_MODE, |
| 22 | MessageId, |
| 23 | PROVIDER_SEND_TURN_MAX_ATTACHMENTS, |
| 24 | type EnvironmentId, |
| 25 | } from "@t3tools/contracts"; |
| 26 | import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git"; |
| 27 | import * as Cause from "effect/Cause"; |
| 28 | import * as Clock from "effect/Clock"; |
| 29 | import * as Effect from "effect/Effect"; |
| 30 | import * as Exit from "effect/Exit"; |
| 31 | import * as Fiber from "effect/Fiber"; |
| 32 | import * as Latch from "effect/Latch"; |
| 33 | import * as Schema from "effect/Schema"; |
| 34 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 35 | import { msg } from "gt-react-native"; |
| 36 | import { m } from "../i18n/messages"; |
| 37 | |
| 38 | import { createDebugLogger } from "../lib/debugLog"; |
| 39 | import type { DraftComposerAttachment } from "../lib/composerImages"; |
| 40 | import { scopedThreadKey } from "../lib/scopedEntities"; |
| 41 | import { buildProjectThreadStartTurnInput } from "../lib/projectThreadStartTurn"; |
| 42 | import { serializeComposerMessageForServer, uploadedComposerContext } from "../lib/composerContext"; |
| 43 | import { prepareTurnAttachments } from "../lib/attachmentUpload"; |
| 44 | import { randomHex } from "../lib/uuid"; |
| 45 | import { |
| 46 | describeUnavailableModelSelection, |
| 47 | isModelSelectionUnavailable, |
| 48 | } from "../lib/modelOptions"; |
| 49 | import { |
| 50 | retainAcknowledgedThreadMessage, |
| 51 | forgetAcknowledgedThreadMessage, |
| 52 | } from "./acknowledged-thread-messages"; |
| 53 | import { appAtomRegistry } from "./atom-registry"; |
| 54 | import { restoredNewTaskDraftKey } from "./new-task-draft-key"; |
| 55 | import { |
| 56 | clearPendingThreadCreationOutcome, |
| 57 | pendingThreadCreationOutcomesAtom, |
| 58 | recordPendingThreadCreationOutcome, |
| 59 | } from "./pending-thread-creation"; |
| 60 | import { environmentProjects } from "./projects"; |
| 61 | import { environmentServerConfigsAtom, serverEnvironment } from "./server"; |
| 62 | import { uploadComposerAttachmentShared } from "./composer-attachment-uploads"; |
| 63 | import { gitEnvironment } from "./git"; |
| 64 | import { environmentSession, readEnvironmentScope, sessionHasScope } from "./session"; |
| 65 | import { |
| 66 | confirmThreadOutboxMessageQueued, |
| 67 | threadOutboxManager, |
| 68 | threadOutboxRevision, |
| 69 | updateThreadOutboxMessage, |
| 70 | } from "./thread-outbox"; |
| 71 | import { removeThreadOutboxMessage } from "./thread-outbox-removal"; |
| 72 | import { |
| 73 | isQueuedThreadCreationSendable, |
| 74 | resolveThreadOutboxDeliveryAction, |
| 75 | resolveThreadOutboxDispatchStep, |
| 76 | resolveThreadOutboxFailureAction, |
| 77 | resolveQueuedThreadSettings, |
| 78 | shouldRetryThreadOutboxDelivery, |
| 79 | threadOutboxRetryDelayMs, |
| 80 | type QueuedThreadCreation, |
| 81 | type QueuedThreadMessage, |
| 82 | type ThreadOutboxCommandStage, |
| 83 | type ThreadOutboxFailureAction, |
| 84 | } from "./thread-outbox-model"; |
| 85 | import { environmentThreadShells, threadEnvironment, threadFamilies } from "./threads"; |
| 86 | import { |
| 87 | appendComposerDraftAttachments, |
| 88 | composerDraftsAtom, |
| 89 | flushComposerDrafts, |
| 90 | type ComposerDraft, |
| 91 | getComposerDraftSnapshot, |
| 92 | mergeComposerDraftContent, |
| 93 | replaceComposerDraftAttachments, |
| 94 | removeDeliveredCloudQueuedMessage, |
| 95 | undoComposerDraftMerge, |
| 96 | updateComposerDraftSettings, |
| 97 | waitForComposerDraftsLoaded, |
| 98 | } from "./use-composer-drafts"; |
| 99 | import { |
| 100 | dispatchingQueuedMessageIdsAtom, |
| 101 | editingQueuedMessageIdsAtom, |
| 102 | retryingQueuedMessageIdsAtom, |
| 103 | threadOutboxShellStatusesAtom, |
| 104 | } from "./use-thread-outbox"; |
| 105 | import { clearThreadComposerError, setThreadComposerError } from "./thread-composer-error"; |
| 106 | import { workspaceConnections } from "./workspace"; |
| 107 | |
| 108 | const threadOutboxDebug = createDebugLogger("thread-outbox"); |
| 109 | |
| 110 | function isRpcClientDecodeDefect(error: unknown): boolean { |
| 111 | if ( |
| 112 | typeof error !== "object" || |
| 113 | error === null || |
| 114 | !("_tag" in error) || |
| 115 | error._tag !== "RpcClientError" |
| 116 | ) { |
| 117 | return false; |
| 118 | } |
| 119 | const reason: unknown = (error as { readonly reason?: unknown }).reason; |
| 120 | return ( |
| 121 | typeof reason === "object" && |
| 122 | reason !== null && |
| 123 | "_tag" in reason && |
| 124 | reason._tag === "RpcClientDefect" |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | function isOrdinaryThreadOutboxTransportFailure(error: unknown): boolean { |
| 129 | return shouldRetryThreadOutboxDelivery(error) && !isRpcClientDecodeDefect(error); |
| 130 | } |
| 131 | |
| 132 | function logThreadOutboxDeliveryFailure(input: { |
| 133 | readonly stage: ThreadOutboxCommandStage; |
| 134 | readonly error: unknown; |
| 135 | readonly interrupted: boolean; |
| 136 | readonly context: Record<string, unknown>; |
| 137 | }): ThreadOutboxFailureAction { |
| 138 | const action = resolveThreadOutboxFailureAction({ |
| 139 | stage: input.stage, |
| 140 | error: input.error, |
| 141 | interrupted: input.interrupted, |
| 142 | }); |
| 143 | const details = { ...input.context, stage: input.stage, action }; |
| 144 | const ordinaryTransportRetry = |
| 145 | action === "retry" && |
| 146 | !isRpcClientDecodeDefect(input.error) && |
| 147 | (input.interrupted || |
| 148 | input.stage !== "settings-sync" || |
| 149 | shouldRetryThreadOutboxDelivery(input.error)); |
| 150 | if (ordinaryTransportRetry) { |
| 151 | threadOutboxDebug.log("queued message delivery failed", details); |
| 152 | } else { |
| 153 | console.warn("[thread-outbox] queued message delivery failed", details); |
| 154 | } |
| 155 | return action; |
| 156 | } |
| 157 | |
| 158 | function logThreadOutboxUploadFailure(queuedMessage: QueuedThreadMessage, error: unknown): void { |
| 159 | const context = { |
| 160 | environmentId: queuedMessage.environmentId, |
| 161 | threadId: queuedMessage.threadId, |
| 162 | messageId: queuedMessage.messageId, |
| 163 | }; |
| 164 | if (isOrdinaryThreadOutboxTransportFailure(error)) { |
| 165 | threadOutboxDebug.log("attachment upload failed; retrying", { ...context, error }); |
| 166 | } else { |
| 167 | console.warn("[thread-outbox] failed to upload attachments", { ...context, error }); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function setQueuedMessageRetrying(queuedMessageId: MessageId, retrying: boolean): void { |
| 172 | const { [queuedMessageId]: current, ...others } = appAtomRegistry.get( |
| 173 | retryingQueuedMessageIdsAtom, |
| 174 | ); |
| 175 | if ((current === true) === retrying) return; |
| 176 | appAtomRegistry.set( |
| 177 | retryingQueuedMessageIdsAtom, |
| 178 | retrying ? { ...others, [queuedMessageId]: true } : others, |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | function beginDispatchingQueuedMessage(queuedMessageId: MessageId): void { |
| 183 | // An attempt in flight is sending, even while its retry count is kept. |
| 184 | setQueuedMessageRetrying(queuedMessageId, false); |
| 185 | appAtomRegistry.set(dispatchingQueuedMessageIdsAtom, { |
| 186 | ...appAtomRegistry.get(dispatchingQueuedMessageIdsAtom), |
| 187 | [queuedMessageId]: true, |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | function finishDispatchingQueuedMessage(queuedMessageId: MessageId): void { |
| 192 | const { [queuedMessageId]: _finished, ...others } = appAtomRegistry.get( |
| 193 | dispatchingQueuedMessageIdsAtom, |
| 194 | ); |
| 195 | appAtomRegistry.set(dispatchingQueuedMessageIdsAtom, others); |
| 196 | } |
| 197 | |
| 198 | function findThread( |
| 199 | threads: ReadonlyArray<EnvironmentThreadShell>, |
| 200 | message: QueuedThreadMessage, |
| 201 | ): EnvironmentThreadShell | undefined { |
| 202 | return threads.find( |
| 203 | (candidate) => |
| 204 | candidate.environmentId === message.environmentId && candidate.id === message.threadId, |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | function findCreationProject( |
| 209 | projects: ReadonlyArray<EnvironmentProject>, |
| 210 | message: QueuedThreadMessage, |
| 211 | ): EnvironmentProject | undefined { |
| 212 | return projects.find( |
| 213 | (candidate) => |
| 214 | candidate.environmentId === message.environmentId && |
| 215 | candidate.id === message.creation?.projectId, |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | function settingsCommandId(message: QueuedThreadMessage, setting: string): CommandId { |
| 220 | return CommandId.make(`${message.commandId}:${setting}`); |
| 221 | } |
| 222 | |
| 223 | /** Outbox or draft storage refused a step; the step decides what that means for the message. */ |
| 224 | class ThreadOutboxStorageError extends Schema.TaggedError<ThreadOutboxStorageError>()( |
| 225 | "ThreadOutboxStorageError", |
| 226 | { |
| 227 | operation: Schema.Literals([ |
| 228 | "forget-sign-out-snapshot", |
| 229 | "remove-delivered", |
| 230 | "remove-acknowledged", |
| 231 | "remove-unsendable", |
| 232 | "hand-back-to-draft", |
| 233 | "restore-to-draft", |
| 234 | "undo-draft-merge", |
| 235 | "load-drafts", |
| 236 | "flush-drafts", |
| 237 | "remove-recovered", |
| 238 | "remove-restored", |
| 239 | ]), |
| 240 | messageId: Schema.String, |
| 241 | cause: Schema.Defect(), |
| 242 | }, |
| 243 | ) { |
| 244 | override get message(): string { |
| 245 | return `Queued message ${this.messageId} could not ${this.operation.replaceAll("-", " ")}.`; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | const storage = <A>( |
| 250 | operation: ThreadOutboxStorageError["operation"], |
| 251 | messageId: MessageId, |
| 252 | write: () => Promise<A>, |
| 253 | ) => |
| 254 | Effect.tryPromise({ |
| 255 | try: write, |
| 256 | catch: (cause) => new ThreadOutboxStorageError({ operation, messageId, cause }), |
| 257 | }); |
| 258 | |
| 259 | /** The head was not handled; back off and try it again. */ |
| 260 | export class DeliveryRetry extends Schema.TaggedError<DeliveryRetry>()("DeliveryRetry", {}) {} |
| 261 | |
| 262 | const retryDelivery = Effect.fail(new DeliveryRetry()); |
| 263 | |
| 264 | const isEditingQueuedMessage = (messageId: MessageId): boolean => |
| 265 | appAtomRegistry.get(editingQueuedMessageIdsAtom)[messageId] === true; |
| 266 | |
| 267 | const confirmQueued = (message: QueuedThreadMessage) => |
| 268 | Effect.promise(() => confirmThreadOutboxMessageQueued(message)); |
| 269 | |
| 270 | const abandoned = { status: "abandoned" } as const; |
| 271 | |
| 272 | export const prepareQueuedMessageAttachments = Effect.fn("ThreadOutbox.prepareAttachments")( |
| 273 | function* (queuedMessage: QueuedThreadMessage) { |
| 274 | if (!(yield* confirmQueued(queuedMessage))) return abandoned; |
| 275 | if (!readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope)) { |
| 276 | return abandoned; |
| 277 | } |
| 278 | const revision = threadOutboxRevision(queuedMessage.messageId); |
| 279 | if (!isQueuedMessagePayloadCurrent(queuedMessage, revision)) return abandoned; |
| 280 | let persistedMessage = queuedMessage; |
| 281 | let deliveryRevision = revision; |
| 282 | const result = yield* prepareTurnAttachments({ |
| 283 | environmentId: queuedMessage.environmentId, |
| 284 | attachments: queuedMessage.attachments, |
| 285 | supportsImageUploads: true, |
| 286 | uploadAttachment: (attachment) => |
| 287 | uploadComposerAttachmentShared(queuedMessage.environmentId, attachment), |
| 288 | persistUploadedReferences: Effect.fn("ThreadOutbox.persistUploadedReferences")(function* ( |
| 289 | draftAttachments: ReadonlyArray<DraftComposerAttachment>, |
| 290 | ) { |
| 291 | if (isEditingQueuedMessage(queuedMessage.messageId)) return "abandon" as const; |
| 292 | const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; |
| 293 | if (!(yield* Effect.promise(() => updateThreadOutboxMessage(updatedMessage, revision)))) { |
| 294 | return "abandon" as const; |
| 295 | } |
| 296 | persistedMessage = updatedMessage; |
| 297 | deliveryRevision = revision + 1; |
| 298 | return "persisted" as const; |
| 299 | }), |
| 300 | }); |
| 301 | if ( |
| 302 | result.status === "abandoned" || |
| 303 | !isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) |
| 304 | ) { |
| 305 | return abandoned; |
| 306 | } |
| 307 | return { status: "ready", prepared: result, persistedMessage, deliveryRevision } as const; |
| 308 | }, |
| 309 | ); |
| 310 | |
| 311 | function isQueuedMessagePayloadCurrent( |
| 312 | message: QueuedThreadMessage, |
| 313 | expectedRevision: number, |
| 314 | ): boolean { |
| 315 | return ( |
| 316 | threadOutboxRevision(message.messageId) === expectedRevision && |
| 317 | Object.values(appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom)) |
| 318 | .flat() |
| 319 | .some((candidate) => candidate === message) |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | const forgetCloudQueuedMessage = (queuedMessage: QueuedThreadMessage) => |
| 324 | storage("forget-sign-out-snapshot", queuedMessage.messageId, () => |
| 325 | removeDeliveredCloudQueuedMessage(queuedMessage), |
| 326 | ).pipe( |
| 327 | Effect.catch((error) => |
| 328 | Effect.logWarning("[thread-outbox] could not update sign-out snapshot after delivery", { |
| 329 | messageId: queuedMessage.messageId, |
| 330 | error: error.cause, |
| 331 | }), |
| 332 | ), |
| 333 | ); |
| 334 | |
| 335 | export const completeQueuedMessageDelivery = Effect.fn("ThreadOutbox.completeDelivery")(function* ( |
| 336 | queuedMessage: QueuedThreadMessage, |
| 337 | deliveryRevision: number, |
| 338 | sequence?: number, |
| 339 | ) { |
| 340 | clearThreadComposerError( |
| 341 | scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId), |
| 342 | queuedMessage.messageId, |
| 343 | ); |
| 344 | yield* forgetCloudQueuedMessage(queuedMessage); |
| 345 | // Editors can acquire entries during startTurn without bumping revisions; CAS alone would lose unsaved edits. |
| 346 | if (isEditingQueuedMessage(queuedMessage.messageId)) return "edited" as const; |
| 347 | retainAcknowledgedThreadMessage( |
| 348 | sequence === undefined ? queuedMessage : { ...queuedMessage, sequence }, |
| 349 | ); |
| 350 | const removed = yield* storage("remove-delivered", queuedMessage.messageId, () => |
| 351 | removeThreadOutboxMessage( |
| 352 | queuedMessage, |
| 353 | deliveryRevision, |
| 354 | () => !isEditingQueuedMessage(queuedMessage.messageId), |
| 355 | ), |
| 356 | ).pipe( |
| 357 | Effect.catch((error) => |
| 358 | Effect.logWarning("[thread-outbox] failed to remove delivered queued message", { |
| 359 | environmentId: queuedMessage.environmentId, |
| 360 | threadId: queuedMessage.threadId, |
| 361 | messageId: queuedMessage.messageId, |
| 362 | error: error.cause, |
| 363 | }).pipe(Effect.as(null)), |
| 364 | ), |
| 365 | ); |
| 366 | if (removed === true) return "removed" as const; |
| 367 | forgetAcknowledgedThreadMessage(queuedMessage); |
| 368 | if (removed === null) return "failed" as const; |
| 369 | threadOutboxDebug.log("delivered message was edited before cleanup", { |
| 370 | environmentId: queuedMessage.environmentId, |
| 371 | threadId: queuedMessage.threadId, |
| 372 | messageId: queuedMessage.messageId, |
| 373 | }); |
| 374 | return "edited" as const; |
| 375 | }); |
| 376 | |
| 377 | export const removeAcknowledgedExistingThreadMessage = Effect.fn( |
| 378 | "ThreadOutbox.removeAcknowledgedMessage", |
| 379 | )(function* (queuedMessage: QueuedThreadMessage, acknowledgedMessageIds: Set<MessageId>) { |
| 380 | yield* forgetCloudQueuedMessage(queuedMessage); |
| 381 | const removed = yield* storage("remove-acknowledged", queuedMessage.messageId, () => |
| 382 | removeThreadOutboxMessage(queuedMessage), |
| 383 | ).pipe( |
| 384 | Effect.catch((error) => |
| 385 | Effect.logWarning("[thread-outbox] failed to remove acknowledged queued message", { |
| 386 | environmentId: queuedMessage.environmentId, |
| 387 | threadId: queuedMessage.threadId, |
| 388 | messageId: queuedMessage.messageId, |
| 389 | error: error.cause, |
| 390 | }).pipe(Effect.as(false)), |
| 391 | ), |
| 392 | ); |
| 393 | if (!removed) return yield* retryDelivery; |
| 394 | acknowledgedMessageIds.delete(queuedMessage.messageId); |
| 395 | }); |
| 396 | |
| 397 | /** Hands a new task edited after its launch back to the composer, then drops it from the queue. */ |
| 398 | export const recoverEditedCreationAfterDelivery = Effect.fn("ThreadOutbox.recoverEditedCreation")( |
| 399 | function* (queuedMessage: QueuedThreadMessage) { |
| 400 | const kept = Object.values( |
| 401 | appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), |
| 402 | ) |
| 403 | .flat() |
| 404 | .find((candidate) => candidate.messageId === queuedMessage.messageId); |
| 405 | if (!kept || isEditingQueuedMessage(kept.messageId)) return; |
| 406 | const keptRevision = threadOutboxRevision(kept.messageId); |
| 407 | const draftKey = scopedThreadKey(kept.environmentId, kept.threadId); |
| 408 | const handedBack = yield* Effect.gen(function* () { |
| 409 | // Merge before removal to retain file ownership; allow overflow and let send guards require trimming. |
| 410 | yield* storage("hand-back-to-draft", kept.messageId, () => |
| 411 | mergeComposerDraftContent(draftKey, { |
| 412 | text: kept.text, |
| 413 | context: kept.context, |
| 414 | attachments: [], |
| 415 | }), |
| 416 | ); |
| 417 | if (isEditingQueuedMessage(kept.messageId)) return "edited" as const; |
| 418 | if (threadOutboxRevision(kept.messageId) !== keptRevision) return "changed" as const; |
| 419 | const existingAttachmentIds = new Set( |
| 420 | getComposerDraftSnapshot(draftKey).attachments.map((attachment) => attachment.id), |
| 421 | ); |
| 422 | appendComposerDraftAttachments( |
| 423 | draftKey, |
| 424 | kept.attachments.filter((attachment) => !existingAttachmentIds.has(attachment.id)), |
| 425 | { restore: true }, |
| 426 | ); |
| 427 | updateComposerDraftSettings(draftKey, { |
| 428 | ...(kept.modelSelection !== undefined ? { modelSelection: kept.modelSelection } : {}), |
| 429 | ...(kept.runtimeMode !== undefined ? { runtimeMode: kept.runtimeMode } : {}), |
| 430 | ...(kept.interactionMode !== undefined ? { interactionMode: kept.interactionMode } : {}), |
| 431 | }); |
| 432 | // Flush before removal: append only schedules persistence, leaving the queue as the sole durable copy. |
| 433 | yield* storage("flush-drafts", kept.messageId, () => flushComposerDrafts()); |
| 434 | return "handed-back" as const; |
| 435 | }).pipe( |
| 436 | Effect.catch((error) => |
| 437 | Effect.logWarning( |
| 438 | "[thread-outbox] could not hand an edited pending task to the composer", |
| 439 | error.cause, |
| 440 | ).pipe(Effect.as("failed" as const)), |
| 441 | ), |
| 442 | ); |
| 443 | if (handedBack === "edited") return; |
| 444 | if (handedBack !== "handed-back") return yield* retryDelivery; |
| 445 | if (isEditingQueuedMessage(kept.messageId)) return; |
| 446 | const removed = yield* storage("remove-recovered", kept.messageId, () => |
| 447 | removeThreadOutboxMessage(kept, keptRevision, () => !isEditingQueuedMessage(kept.messageId)), |
| 448 | ).pipe( |
| 449 | Effect.catch((error) => |
| 450 | Effect.logWarning( |
| 451 | "[thread-outbox] could not remove recovered pending task", |
| 452 | error.cause, |
| 453 | ).pipe(Effect.as(false)), |
| 454 | ), |
| 455 | ); |
| 456 | if (!removed) return yield* retryDelivery; |
| 457 | }, |
| 458 | ); |
| 459 | |
| 460 | /** |
| 461 | * Hands an undeliverable message back to its draft with the reason. "blocked" means the draft |
| 462 | * cannot take it yet (attachment cap); "deferred" means an editor or newer write owns it. |
| 463 | */ |
| 464 | export const restoreRejectedQueuedMessage = Effect.fn("ThreadOutbox.restoreRejectedMessage")( |
| 465 | function* (queuedMessage: QueuedThreadMessage, message: string) { |
| 466 | const draftKey = recoveryDraftKey(queuedMessage); |
| 467 | const threadKey = scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId); |
| 468 | const editing = () => isEditingQueuedMessage(queuedMessage.messageId); |
| 469 | // Until the queue drops the message, a failure undoes the merge, keeping concurrent edits. |
| 470 | let rollback: { readonly snapshot: ComposerDraft; readonly merged: ComposerDraft } | null = |
| 471 | null; |
| 472 | const undo = (snapshot: ComposerDraft, merged: ComposerDraft) => |
| 473 | storage("undo-draft-merge", queuedMessage.messageId, () => |
| 474 | undoComposerDraftMerge(draftKey, snapshot, merged), |
| 475 | ); |
| 476 | return yield* Effect.gen(function* () { |
| 477 | if (editing() || !(yield* confirmQueued(queuedMessage)) || editing()) { |
| 478 | return "deferred" as const; |
| 479 | } |
| 480 | const revision = threadOutboxRevision(queuedMessage.messageId); |
| 481 | if (queuedMessage.restoreToDraft === false) { |
| 482 | const removed = yield* storage("remove-unsendable", queuedMessage.messageId, () => |
| 483 | removeThreadOutboxMessage(queuedMessage, revision, () => !editing()), |
| 484 | ); |
| 485 | if (!removed) return "deferred" as const; |
| 486 | setThreadComposerError(threadKey, message, queuedMessage.messageId); |
| 487 | return "restored" as const; |
| 488 | } |
| 489 | yield* storage("load-drafts", queuedMessage.messageId, () => waitForComposerDraftsLoaded()); |
| 490 | if (editing()) return "deferred" as const; |
| 491 | const originalDraft = getComposerDraftSnapshot(draftKey); |
| 492 | const existingAttachmentIds = new Set( |
| 493 | originalDraft.attachments.map((attachment) => attachment.id), |
| 494 | ); |
| 495 | const addedAttachmentCount = queuedMessage.attachments.filter( |
| 496 | (attachment) => !existingAttachmentIds.has(attachment.id), |
| 497 | ).length; |
| 498 | if (existingAttachmentIds.size + addedAttachmentCount > PROVIDER_SEND_TURN_MAX_ATTACHMENTS) { |
| 499 | setThreadComposerError( |
| 500 | threadKey, |
| 501 | m( |
| 502 | msg( |
| 503 | "Remove attachments from the draft before restoring this message. Messages can contain at most {max} attachments.", |
| 504 | { max: PROVIDER_SEND_TURN_MAX_ATTACHMENTS }, |
| 505 | ), |
| 506 | ), |
| 507 | queuedMessage.messageId, |
| 508 | ); |
| 509 | return "blocked" as const; |
| 510 | } |
| 511 | |
| 512 | const withdraw = (merged: ComposerDraft) => |
| 513 | Effect.sync(() => clearThreadComposerError(threadKey, queuedMessage.messageId)).pipe( |
| 514 | Effect.andThen(undo(originalDraft, merged)), |
| 515 | Effect.as("deferred" as const), |
| 516 | ); |
| 517 | if (!queuedMessage.creation) { |
| 518 | setThreadComposerError(threadKey, message, queuedMessage.messageId); |
| 519 | } |
| 520 | stampRecoveryDraftProject(queuedMessage, draftKey); |
| 521 | // The merge publishes even when persisting it fails, so the rollback is captured either way. |
| 522 | yield* storage("restore-to-draft", queuedMessage.messageId, () => |
| 523 | mergeComposerDraftContent(draftKey, { |
| 524 | text: queuedMessage.text, |
| 525 | context: queuedMessage.context, |
| 526 | attachments: queuedMessage.attachments, |
| 527 | }), |
| 528 | ).pipe( |
| 529 | Effect.ensuring( |
| 530 | Effect.sync(() => { |
| 531 | rollback = { snapshot: originalDraft, merged: getComposerDraftSnapshot(draftKey) }; |
| 532 | }), |
| 533 | ), |
| 534 | ); |
| 535 | if (editing()) return yield* withdraw(getComposerDraftSnapshot(draftKey)); |
| 536 | updateComposerDraftSettings(draftKey, { |
| 537 | ...(queuedMessage.modelSelection ? { modelSelection: queuedMessage.modelSelection } : {}), |
| 538 | ...(queuedMessage.runtimeMode ? { runtimeMode: queuedMessage.runtimeMode } : {}), |
| 539 | ...(queuedMessage.interactionMode |
| 540 | ? { interactionMode: queuedMessage.interactionMode } |
| 541 | : {}), |
| 542 | ...(queuedMessage.freshContext ? { freshContext: true } : {}), |
| 543 | ...(queuedMessage.creation |
| 544 | ? { |
| 545 | workspaceSelection: { |
| 546 | mode: queuedMessage.creation.workspaceMode, |
| 547 | branch: queuedMessage.creation.branch, |
| 548 | worktreePath: queuedMessage.creation.worktreePath, |
| 549 | pullRequestReference: queuedMessage.creation.pullRequestReference, |
| 550 | ...(queuedMessage.creation.startFromOrigin !== undefined |
| 551 | ? { startFromOrigin: queuedMessage.creation.startFromOrigin } |
| 552 | : {}), |
| 553 | }, |
| 554 | } |
| 555 | : {}), |
| 556 | }); |
| 557 | const restoredDraft = getComposerDraftSnapshot(draftKey); |
| 558 | rollback = { snapshot: originalDraft, merged: restoredDraft }; |
| 559 | yield* storage("flush-drafts", queuedMessage.messageId, () => flushComposerDrafts()); |
| 560 | if (editing() || !(yield* confirmQueued(queuedMessage)) || editing()) { |
| 561 | return yield* withdraw(restoredDraft); |
| 562 | } |
| 563 | const removed = yield* storage("remove-restored", queuedMessage.messageId, () => |
| 564 | removeThreadOutboxMessage(queuedMessage, revision, () => !editing()), |
| 565 | ); |
| 566 | if (!removed) return yield* withdraw(restoredDraft); |
| 567 | // After queue removal, the draft owns content and must never roll back. |
| 568 | rollback = null; |
| 569 | if (queuedMessage.creation) { |
| 570 | clearThreadComposerError(threadKey, queuedMessage.messageId); |
| 571 | recordPendingThreadCreationOutcome({ |
| 572 | kind: "failed", |
| 573 | message: queuedMessage, |
| 574 | reason: message, |
| 575 | }); |
| 576 | } |
| 577 | return "restored" as const; |
| 578 | }).pipe( |
| 579 | Effect.catch((error) => |
| 580 | Effect.gen(function* () { |
| 581 | const pending: typeof rollback = rollback; |
| 582 | if (pending !== null) { |
| 583 | yield* undo(pending.snapshot, pending.merged).pipe( |
| 584 | Effect.catch((undoError) => |
| 585 | Effect.logWarning( |
| 586 | "[thread-outbox] failed to persist a recovery rollback", |
| 587 | undoError.cause, |
| 588 | ), |
| 589 | ), |
| 590 | ); |
| 591 | } |
| 592 | yield* Effect.logWarning( |
| 593 | "[thread-outbox] failed to restore an undeliverable message", |
| 594 | error.cause, |
| 595 | ); |
| 596 | setThreadComposerError( |
| 597 | threadKey, |
| 598 | errorDisplayMessage(error.cause, m) ?? |
| 599 | m(msg("The unsent message could not be restored.")), |
| 600 | queuedMessage.messageId, |
| 601 | ); |
| 602 | return "retry" as const; |
| 603 | }), |
| 604 | ), |
| 605 | ); |
| 606 | }, |
| 607 | ); |
| 608 | |
| 609 | /** Derive keys from message ids so failed recovery retries reuse the same draft. */ |
| 610 | function recoveryDraftKey(queuedMessage: QueuedThreadMessage): string { |
| 611 | return queuedMessage.creation |
| 612 | ? restoredNewTaskDraftKey(queuedMessage.messageId) |
| 613 | : scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId); |
| 614 | } |
| 615 | |
| 616 | function stampRecoveryDraftProject(queuedMessage: QueuedThreadMessage, draftKey: string): void { |
| 617 | if (!queuedMessage.creation) { |
| 618 | return; |
| 619 | } |
| 620 | updateComposerDraftSettings(draftKey, { |
| 621 | project: { |
| 622 | environmentId: queuedMessage.environmentId, |
| 623 | projectId: queuedMessage.creation.projectId, |
| 624 | createdAt: queuedMessage.createdAt, |
| 625 | }, |
| 626 | }); |
| 627 | } |
| 628 | |
| 629 | const preserveUploadedAttachmentsForEditor = Effect.fn( |
| 630 | "ThreadOutbox.preserveUploadedAttachmentsForEditor", |
| 631 | )(function* (originalMessage: QueuedThreadMessage, uploadedMessage: QueuedThreadMessage) { |
| 632 | if (!originalMessage.creation) return; |
| 633 | const draftKey = `pending-task:${originalMessage.messageId}`; |
| 634 | const draft = getComposerDraftSnapshot(draftKey); |
| 635 | const uploadedById = new Map( |
| 636 | uploadedMessage.attachments.map((attachment) => [attachment.id, attachment] as const), |
| 637 | ); |
| 638 | let changed = false; |
| 639 | const nextAttachments = draft.attachments.map((attachment) => { |
| 640 | const uploaded = uploadedById.get(attachment.id); |
| 641 | if ( |
| 642 | !uploaded?.uploadedAttachmentId || |
| 643 | uploaded.uploadEnvironmentId !== originalMessage.environmentId || |
| 644 | (attachment.uploadedAttachmentId === uploaded.uploadedAttachmentId && |
| 645 | attachment.uploadEnvironmentId === uploaded.uploadEnvironmentId) |
| 646 | ) { |
| 647 | return attachment; |
| 648 | } |
| 649 | changed = true; |
| 650 | return { |
| 651 | ...attachment, |
| 652 | uploadedAttachmentId: uploaded.uploadedAttachmentId, |
| 653 | uploadEnvironmentId: uploaded.uploadEnvironmentId, |
| 654 | }; |
| 655 | }); |
| 656 | if (!changed) return; |
| 657 | replaceComposerDraftAttachments(draftKey, nextAttachments); |
| 658 | yield* Effect.promise(() => flushComposerDrafts()); |
| 659 | }); |
| 660 | |
| 661 | // Everything delivery decides from. Each queued environment's session is read |
| 662 | // here so a granted or revoked operate scope wakes delivery. |
| 663 | |
| 664 | const deliveryInputsAtom = Atom.make((get) => { |
| 665 | const connectedEnvironments = get(workspaceConnections.environmentsAtom); |
| 666 | const operableEnvironments = new Set<EnvironmentId>(); |
| 667 | for (const { environmentId } of connectedEnvironments) { |
| 668 | if ( |
| 669 | sessionHasScope( |
| 670 | get(environmentSession.sessionStateAtom(environmentId)), |
| 671 | AuthOrchestrationOperateScope, |
| 672 | ) |
| 673 | ) { |
| 674 | operableEnvironments.add(environmentId); |
| 675 | } |
| 676 | } |
| 677 | return { |
| 678 | editingQueuedMessageIds: get(editingQueuedMessageIdsAtom), |
| 679 | queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom), |
| 680 | shellStatuses: get(threadOutboxShellStatusesAtom), |
| 681 | threads: get(environmentThreadShells.threadShellsAtom), |
| 682 | projects: get(environmentProjects.projectsAtom), |
| 683 | serverConfigs: get(environmentServerConfigsAtom), |
| 684 | connectedEnvironments, |
| 685 | operableEnvironments, |
| 686 | }; |
| 687 | }).pipe(Atom.withLabel("mobile:thread-outbox:delivery-inputs")); |
| 688 | |
| 689 | // Read through the shell atom, not once: the shell often lands before the |
| 690 | // outcome is recorded, and that entry must still be collected when it does. |
| 691 | const settledCreationOutcomeKeysAtom = Atom.make((get): ReadonlyArray<string> => { |
| 692 | const outcomes = get(pendingThreadCreationOutcomesAtom); |
| 693 | const threads = get(environmentThreadShells.threadShellsAtom); |
| 694 | return Object.entries(outcomes).flatMap(([threadKey, outcome]) => |
| 695 | outcome.kind === "delivered" && |
| 696 | threads.some( |
| 697 | (thread) => |
| 698 | scopedThreadKey(thread.environmentId, thread.id) === threadKey && |
| 699 | (thread.latestRun !== null || |
| 700 | thread.runtime?.status === "failed" || |
| 701 | thread.runtime?.status === "cancelled" || |
| 702 | thread.runtime?.status === "interrupted"), |
| 703 | ) |
| 704 | ? [threadKey] |
| 705 | : [], |
| 706 | ); |
| 707 | }).pipe(Atom.withLabel("mobile:thread-outbox:settled-creation-outcomes")); |
| 708 | |
| 709 | function deliveredSequence( |
| 710 | result: AtomCommandResult<{ readonly sequence?: number }, unknown>, |
| 711 | ): number | undefined { |
| 712 | return AsyncResult.isSuccess(result) ? result.value.sequence : undefined; |
| 713 | } |
| 714 | |
| 715 | const deliveryCommand = <W, A, E>( |
| 716 | command: AtomCommand<W, A, E>, |
| 717 | input: W, |
| 718 | ): Effect.Effect<AtomCommandResult<A, E>> => |
| 719 | command.run(appAtomRegistry, input).pipe( |
| 720 | Effect.exit, |
| 721 | Effect.map((exit) => { |
| 722 | const result = AsyncResult.fromExit(exit); |
| 723 | reportAtomCommandResult(result, { label: command.label, reportFailure: false }); |
| 724 | return result; |
| 725 | }), |
| 726 | ); |
| 727 | |
| 728 | function reportCommandFailure( |
| 729 | queuedMessage: QueuedThreadMessage, |
| 730 | commandResult: AtomCommandResult<unknown, unknown>, |
| 731 | stage: ThreadOutboxCommandStage, |
| 732 | ): { readonly action: "retry" | "restore"; readonly message: string } | null { |
| 733 | if (!AsyncResult.isFailure(commandResult)) { |
| 734 | return null; |
| 735 | } |
| 736 | const error = Cause.squash(commandResult.cause); |
| 737 | const action = logThreadOutboxDeliveryFailure({ |
| 738 | stage, |
| 739 | error, |
| 740 | interrupted: Cause.hasInterruptsOnly(commandResult.cause), |
| 741 | context: { |
| 742 | environmentId: queuedMessage.environmentId, |
| 743 | threadId: queuedMessage.threadId, |
| 744 | messageId: queuedMessage.messageId, |
| 745 | cause: commandResult.cause, |
| 746 | }, |
| 747 | }); |
| 748 | return { |
| 749 | action, |
| 750 | message: errorDisplayMessage(error, m) ?? m(msg("The message could not be sent.")), |
| 751 | }; |
| 752 | } |
| 753 | |
| 754 | interface ThreadOutboxRetry { |
| 755 | readonly attempt: number; |
| 756 | readonly notBefore: number; |
| 757 | } |
| 758 | |
| 759 | /** A thread's delivery fiber; `retiring` once interrupted, until the fiber has exited. */ |
| 760 | interface ThreadFiber { |
| 761 | readonly wake: Latch.Latch; |
| 762 | fiber: Fiber.Fiber<never> | undefined; |
| 763 | retiring: boolean; |
| 764 | } |
| 765 | |
| 766 | /** What a thread's delivery fiber does next with the head of its queue. */ |
| 767 | type ThreadDeliveryStep = |
| 768 | /** Nothing to send until an input changes, or until `until` when a backoff is running. */ |
| 769 | | { readonly _tag: "Wait"; readonly until?: number } |
| 770 | /** The head failed without a send; back off before trying it again. */ |
| 771 | | { readonly _tag: "Backoff"; readonly messageId: MessageId } |
| 772 | | { |
| 773 | readonly _tag: "Dispatch"; |
| 774 | readonly messageId: MessageId; |
| 775 | /** Succeeds once the message is handled (sent, removed or handed back). */ |
| 776 | readonly run: Effect.Effect<void, DeliveryRetry>; |
| 777 | }; |
| 778 | |
| 779 | /** |
| 780 | * Delivers queued thread messages, one fiber per thread so a slow send in one thread never |
| 781 | * holds another. Each fiber sends its thread's head message in order, backs off after a |
| 782 | * failure, and otherwise waits until an input changes (outbox, locks, shells, projects, |
| 783 | * configs, connections, scopes). |
| 784 | */ |
| 785 | export function createThreadOutboxDelivery(): { |
| 786 | readonly drain: () => void; |
| 787 | readonly dispose: () => void; |
| 788 | } { |
| 789 | let disposed = false; |
| 790 | const retries = new Map<MessageId, ThreadOutboxRetry>(); |
| 791 | let readyEnvironmentIds = new Set<EnvironmentId>(); |
| 792 | // Existing-thread turns the server accepted but whose outbox removal has |
| 793 | // not landed; the next pass retries only the removal, never the send. |
| 794 | const acknowledgedExistingThreadMessageIds = new Set<MessageId>(); |
| 795 | // Revision and workspace each new task's launch carried. Launch replays by command id, so an |
| 796 | // unedited task whose reply was lost is resent in that workspace, not handed back. |
| 797 | const launchedCreations = new Map< |
| 798 | MessageId, |
| 799 | { readonly revision: number; readonly workspace: QueuedThreadCreation } |
| 800 | >(); |
| 801 | // Targets the shell may have left out hold their thread family until they leave the queue, so |
| 802 | // the row stays readable through dispatch. |
| 803 | const familyMounts = new Map<MessageId, () => void>(); |
| 804 | const blockedRecoveries = new Map< |
| 805 | MessageId, |
| 806 | { readonly message: QueuedThreadMessage; readonly unsubscribe: () => void } |
| 807 | >(); |
| 808 | |
| 809 | const retryNotBefore = (messageId: MessageId): number => retries.get(messageId)?.notBefore ?? 0; |
| 810 | |
| 811 | const scheduleRetry = (messageId: MessageId): void => { |
| 812 | if (disposed) return; |
| 813 | const attempt = (retries.get(messageId)?.attempt ?? 0) + 1; |
| 814 | retries.set(messageId, { |
| 815 | attempt, |
| 816 | notBefore: Date.now() + threadOutboxRetryDelayMs(attempt), |
| 817 | }); |
| 818 | setQueuedMessageRetrying(messageId, true); |
| 819 | }; |
| 820 | |
| 821 | const clearRetry = (messageId: MessageId): void => { |
| 822 | if (!retries.delete(messageId)) return; |
| 823 | setQueuedMessageRetrying(messageId, false); |
| 824 | }; |
| 825 | |
| 826 | // An over-cap draft blocks recovery until the user changes its attachments; |
| 827 | // parking on the draft avoids hammering the restore with backoff retries. |
| 828 | const restoreQueuedMessage = Effect.fn("ThreadOutbox.restoreQueuedMessage")(function* ( |
| 829 | queuedMessage: QueuedThreadMessage, |
| 830 | message: string, |
| 831 | ) { |
| 832 | const result = yield* restoreRejectedQueuedMessage(queuedMessage, message); |
| 833 | if (result === "retry") return yield* retryDelivery; |
| 834 | if (result !== "blocked" || disposed || blockedRecoveries.has(queuedMessage.messageId)) { |
| 835 | return; |
| 836 | } |
| 837 | const draftKey = recoveryDraftKey(queuedMessage); |
| 838 | const editorDraftKey = queuedMessage.creation |
| 839 | ? `pending-task:${queuedMessage.messageId}` |
| 840 | : null; |
| 841 | const currentDrafts = appAtomRegistry.get(composerDraftsAtom); |
| 842 | const blockedAttachments = currentDrafts[draftKey]?.attachments; |
| 843 | const editorAttachments = |
| 844 | editorDraftKey === null ? undefined : currentDrafts[editorDraftKey]?.attachments; |
| 845 | const unsubscribe = appAtomRegistry.subscribe(composerDraftsAtom, (drafts) => { |
| 846 | if ( |
| 847 | drafts[draftKey]?.attachments === blockedAttachments && |
| 848 | (editorDraftKey === null || drafts[editorDraftKey]?.attachments === editorAttachments) |
| 849 | ) { |
| 850 | return; |
| 851 | } |
| 852 | const active = blockedRecoveries.get(queuedMessage.messageId); |
| 853 | if (!active) { |
| 854 | return; |
| 855 | } |
| 856 | blockedRecoveries.delete(queuedMessage.messageId); |
| 857 | active.unsubscribe(); |
| 858 | wakeThreads(); |
| 859 | }); |
| 860 | blockedRecoveries.set(queuedMessage.messageId, { message: queuedMessage, unsubscribe }); |
| 861 | }); |
| 862 | |
| 863 | const sendQueuedMessage = Effect.fn("ThreadOutbox.sendQueuedMessage")(function* ( |
| 864 | queuedMessage: QueuedThreadMessage, |
| 865 | thread: EnvironmentThreadShell, |
| 866 | ) { |
| 867 | const hasAccess = () => |
| 868 | readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope); |
| 869 | if (!hasAccess()) return; |
| 870 | const serverConfig = appAtomRegistry.get( |
| 871 | serverEnvironment.configValueAtom(queuedMessage.environmentId), |
| 872 | ); |
| 873 | if (!serverConfig) return yield* retryDelivery; |
| 874 | const settings = resolveQueuedThreadSettings(queuedMessage, thread, serverConfig.providers); |
| 875 | if (isModelSelectionUnavailable(serverConfig, settings.modelSelection, thread.projectId)) { |
| 876 | const { title, detail } = describeUnavailableModelSelection( |
| 877 | serverConfig, |
| 878 | settings.modelSelection, |
| 879 | thread.projectId, |
| 880 | ); |
| 881 | return yield* restoreQueuedMessage(queuedMessage, `${title}. ${detail}`); |
| 882 | } |
| 883 | // The server would refuse this mode in settings sync, which retries forever; stop first. |
| 884 | const runtimeModeDenial = managedRuntimeModeDenial( |
| 885 | serverConfig.managedPolicy, |
| 886 | settings.runtimeMode, |
| 887 | m, |
| 888 | ); |
| 889 | if (runtimeModeDenial !== undefined) { |
| 890 | return yield* restoreQueuedMessage(queuedMessage, runtimeModeDenial); |
| 891 | } |
| 892 | |
| 893 | if (settings.runtimeMode !== thread.runtimeMode) { |
| 894 | if (!hasAccess()) return; |
| 895 | const runtimeResult = yield* deliveryCommand(threadEnvironment.setRuntimeMode, { |
| 896 | environmentId: queuedMessage.environmentId, |
| 897 | input: { |
| 898 | commandId: settingsCommandId(queuedMessage, "runtime-mode"), |
| 899 | threadId: queuedMessage.threadId, |
| 900 | runtimeMode: settings.runtimeMode, |
| 901 | createdAt: queuedMessage.createdAt, |
| 902 | }, |
| 903 | }); |
| 904 | if (AsyncResult.isFailure(runtimeResult)) { |
| 905 | reportCommandFailure(queuedMessage, runtimeResult, "settings-sync"); |
| 906 | return yield* retryDelivery; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (settings.interactionMode !== thread.interactionMode) { |
| 911 | if (!hasAccess()) return; |
| 912 | const interactionResult = yield* deliveryCommand(threadEnvironment.setInteractionMode, { |
| 913 | environmentId: queuedMessage.environmentId, |
| 914 | input: { |
| 915 | commandId: settingsCommandId(queuedMessage, "interaction-mode"), |
| 916 | threadId: queuedMessage.threadId, |
| 917 | interactionMode: settings.interactionMode, |
| 918 | createdAt: queuedMessage.createdAt, |
| 919 | }, |
| 920 | }); |
| 921 | if (AsyncResult.isFailure(interactionResult)) { |
| 922 | reportCommandFailure(queuedMessage, interactionResult, "settings-sync"); |
| 923 | return yield* retryDelivery; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (!hasAccess()) return; |
| 928 | // Waiting on the shared upload is the one part of a send that may be cut short. |
| 929 | const preparedExit = yield* Effect.exit( |
| 930 | Effect.interruptible(prepareQueuedMessageAttachments(queuedMessage)), |
| 931 | ); |
| 932 | if (Exit.isFailure(preparedExit)) { |
| 933 | if (Cause.hasInterruptsOnly(preparedExit.cause)) return yield* Effect.interrupt; |
| 934 | const error = Cause.squash(preparedExit.cause); |
| 935 | if (!hasAccess()) return; |
| 936 | logThreadOutboxUploadFailure(queuedMessage, error); |
| 937 | if (!shouldRetryThreadOutboxDelivery(error)) { |
| 938 | return yield* restoreQueuedMessage( |
| 939 | queuedMessage, |
| 940 | errorDisplayMessage(error, m) ?? m(msg("An attachment could not upload.")), |
| 941 | ); |
| 942 | } |
| 943 | return yield* retryDelivery; |
| 944 | } |
| 945 | if (preparedExit.value.status === "abandoned") return; |
| 946 | const { prepared, persistedMessage, deliveryRevision } = preparedExit.value; |
| 947 | if (isEditingQueuedMessage(queuedMessage.messageId)) { |
| 948 | yield* preserveUploadedAttachmentsForEditor(queuedMessage, persistedMessage); |
| 949 | return; |
| 950 | } |
| 951 | if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) { |
| 952 | return; |
| 953 | } |
| 954 | const currentConfig = appAtomRegistry.get( |
| 955 | serverEnvironment.configValueAtom(queuedMessage.environmentId), |
| 956 | ); |
| 957 | if (!currentConfig) return yield* retryDelivery; |
| 958 | if (isModelSelectionUnavailable(currentConfig, settings.modelSelection, thread.projectId)) { |
| 959 | const { title, detail } = describeUnavailableModelSelection( |
| 960 | currentConfig, |
| 961 | settings.modelSelection, |
| 962 | thread.projectId, |
| 963 | ); |
| 964 | return yield* restoreQueuedMessage(persistedMessage, `${title}. ${detail}`); |
| 965 | } |
| 966 | const sendSettings = resolveQueuedThreadSettings( |
| 967 | queuedMessage, |
| 968 | settings, |
| 969 | currentConfig.providers, |
| 970 | ); |
| 971 | if (queuedMessage.compactBeforeSend === true) { |
| 972 | if (!hasAccess()) return; |
| 973 | // Queued, since steering a running turn with a compaction is rejected. |
| 974 | const compactionResult = yield* deliveryCommand(threadEnvironment.startTurn, { |
| 975 | environmentId: queuedMessage.environmentId, |
| 976 | input: { |
| 977 | commandId: settingsCommandId(queuedMessage, "compact"), |
| 978 | creationSource: "mobile", |
| 979 | threadId: queuedMessage.threadId, |
| 980 | message: { |
| 981 | messageId: MessageId.make(`${queuedMessage.messageId}:compact`), |
| 982 | role: "user", |
| 983 | text: "/compact", |
| 984 | attachments: [], |
| 985 | idleCompaction: "onReturn", |
| 986 | }, |
| 987 | modelSelection: sendSettings.modelSelection, |
| 988 | runtimeMode: sendSettings.runtimeMode, |
| 989 | interactionMode: sendSettings.interactionMode, |
| 990 | createdAt: queuedMessage.createdAt, |
| 991 | dispatchMode: "queue", |
| 992 | resendAfterSocketLoss: false, |
| 993 | }, |
| 994 | }); |
| 995 | if (AsyncResult.isFailure(compactionResult) && !hasAccess()) return; |
| 996 | const compactionFailure = reportCommandFailure(queuedMessage, compactionResult, "start-turn"); |
| 997 | if (compactionFailure?.action === "retry") return yield* retryDelivery; |
| 998 | if (compactionFailure?.action === "restore") { |
| 999 | return yield* restoreQueuedMessage(persistedMessage, compactionFailure.message); |
| 1000 | } |
| 1001 | if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) return; |
| 1002 | } |
| 1003 | if (!hasAccess()) return; |
| 1004 | const deliveryResult = yield* deliveryCommand(threadEnvironment.startTurn, { |
| 1005 | environmentId: queuedMessage.environmentId, |
| 1006 | input: { |
| 1007 | commandId: queuedMessage.commandId, |
| 1008 | creationSource: "mobile", |
| 1009 | threadId: queuedMessage.threadId, |
| 1010 | message: { |
| 1011 | messageId: queuedMessage.messageId, |
| 1012 | role: "user", |
| 1013 | ...serializeComposerMessageForServer( |
| 1014 | queuedMessage.text, |
| 1015 | uploadedComposerContext( |
| 1016 | queuedMessage.context, |
| 1017 | queuedMessage.attachments, |
| 1018 | prepared.attachments, |
| 1019 | ), |
| 1020 | ), |
| 1021 | attachments: prepared.attachments, |
| 1022 | }, |
| 1023 | modelSelection: sendSettings.modelSelection, |
| 1024 | titleSeed: deriveThreadTitleSeed( |
| 1025 | { text: queuedMessage.text, attachments: queuedMessage.attachments }, |
| 1026 | m, |
| 1027 | ), |
| 1028 | runtimeMode: sendSettings.runtimeMode, |
| 1029 | interactionMode: sendSettings.interactionMode, |
| 1030 | createdAt: queuedMessage.createdAt, |
| 1031 | dispatchMode: queuedMessage.dispatchMode ?? "start", |
| 1032 | ...(queuedMessage.freshContext ? { freshContext: true } : {}), |
| 1033 | resendAfterSocketLoss: false, |
| 1034 | }, |
| 1035 | }); |
| 1036 | if (AsyncResult.isFailure(deliveryResult) && !hasAccess()) return; |
| 1037 | const failure = reportCommandFailure(queuedMessage, deliveryResult, "start-turn"); |
| 1038 | if (failure?.action === "retry") { |
| 1039 | return yield* retryDelivery; |
| 1040 | } |
| 1041 | if (failure?.action === "restore") { |
| 1042 | return yield* restoreQueuedMessage(persistedMessage, failure.message); |
| 1043 | } |
| 1044 | acknowledgedExistingThreadMessageIds.add(persistedMessage.messageId); |
| 1045 | const outcome = yield* completeQueuedMessageDelivery( |
| 1046 | persistedMessage, |
| 1047 | deliveryRevision, |
| 1048 | deliveredSequence(deliveryResult), |
| 1049 | ); |
| 1050 | if (outcome !== "removed") return yield* retryDelivery; |
| 1051 | acknowledgedExistingThreadMessageIds.delete(persistedMessage.messageId); |
| 1052 | }); |
| 1053 | |
| 1054 | const sendQueuedCreation = Effect.fn("ThreadOutbox.sendQueuedCreation")(function* ( |
| 1055 | queuedMessage: QueuedThreadMessage, |
| 1056 | creation: QueuedThreadCreation, |
| 1057 | projectCwd: string, |
| 1058 | ) { |
| 1059 | const hasAccess = () => |
| 1060 | readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope); |
| 1061 | if (!hasAccess()) return; |
| 1062 | const modelSelection = queuedMessage.modelSelection; |
| 1063 | if (modelSelection === undefined) { |
| 1064 | return yield* retryDelivery; |
| 1065 | } |
| 1066 | const serverConfig = appAtomRegistry.get( |
| 1067 | serverEnvironment.configValueAtom(queuedMessage.environmentId), |
| 1068 | ); |
| 1069 | if (!serverConfig) return yield* retryDelivery; |
| 1070 | const settings = resolveQueuedThreadSettings( |
| 1071 | queuedMessage, |
| 1072 | { |
| 1073 | modelSelection, |
| 1074 | runtimeMode: DEFAULT_RUNTIME_MODE, |
| 1075 | interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, |
| 1076 | }, |
| 1077 | serverConfig.providers, |
| 1078 | ); |
| 1079 | if (isModelSelectionUnavailable(serverConfig, settings.modelSelection, creation.projectId)) { |
| 1080 | const { title, detail } = describeUnavailableModelSelection( |
| 1081 | serverConfig, |
| 1082 | settings.modelSelection, |
| 1083 | creation.projectId, |
| 1084 | ); |
| 1085 | return yield* restoreQueuedMessage(queuedMessage, `${title}. ${detail}`); |
| 1086 | } |
| 1087 | const runtimeModeDenial = managedRuntimeModeDenial( |
| 1088 | serverConfig.managedPolicy, |
| 1089 | settings.runtimeMode, |
| 1090 | m, |
| 1091 | ); |
| 1092 | if (runtimeModeDenial !== undefined) { |
| 1093 | return yield* restoreQueuedMessage(queuedMessage, runtimeModeDenial); |
| 1094 | } |
| 1095 | // Waiting on the shared upload is the one part of a send that may be cut short. |
| 1096 | const preparedExit = yield* Effect.exit( |
| 1097 | Effect.interruptible(prepareQueuedMessageAttachments(queuedMessage)), |
| 1098 | ); |
| 1099 | if (Exit.isFailure(preparedExit)) { |
| 1100 | if (Cause.hasInterruptsOnly(preparedExit.cause)) return yield* Effect.interrupt; |
| 1101 | const error = Cause.squash(preparedExit.cause); |
| 1102 | if (!hasAccess()) return; |
| 1103 | logThreadOutboxUploadFailure(queuedMessage, error); |
| 1104 | if (!shouldRetryThreadOutboxDelivery(error)) { |
| 1105 | return yield* restoreQueuedMessage( |
| 1106 | queuedMessage, |
| 1107 | errorDisplayMessage(error, m) ?? m(msg("An attachment could not upload.")), |
| 1108 | ); |
| 1109 | } |
| 1110 | return yield* retryDelivery; |
| 1111 | } |
| 1112 | if (preparedExit.value.status === "abandoned") return; |
| 1113 | const { prepared, persistedMessage, deliveryRevision } = preparedExit.value; |
| 1114 | if (isEditingQueuedMessage(queuedMessage.messageId)) { |
| 1115 | yield* preserveUploadedAttachmentsForEditor(queuedMessage, persistedMessage); |
| 1116 | return; |
| 1117 | } |
| 1118 | if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) { |
| 1119 | return; |
| 1120 | } |
| 1121 | const currentConfig = appAtomRegistry.get( |
| 1122 | serverEnvironment.configValueAtom(queuedMessage.environmentId), |
| 1123 | ); |
| 1124 | if (!currentConfig) return yield* retryDelivery; |
| 1125 | if (isModelSelectionUnavailable(currentConfig, settings.modelSelection, creation.projectId)) { |
| 1126 | const { title, detail } = describeUnavailableModelSelection( |
| 1127 | currentConfig, |
| 1128 | settings.modelSelection, |
| 1129 | creation.projectId, |
| 1130 | ); |
| 1131 | return yield* restoreQueuedMessage(persistedMessage, `${title}. ${detail}`); |
| 1132 | } |
| 1133 | const sendSettings = resolveQueuedThreadSettings( |
| 1134 | queuedMessage, |
| 1135 | settings, |
| 1136 | currentConfig.providers, |
| 1137 | ); |
| 1138 | if (!hasAccess()) return; |
| 1139 | let workspace = creation; |
| 1140 | const launched = launchedCreations.get(persistedMessage.messageId); |
| 1141 | if (launched?.revision === deliveryRevision) { |
| 1142 | workspace = launched.workspace; |
| 1143 | } else if (creation.pullRequestReference) { |
| 1144 | const checkout = yield* deliveryCommand(gitEnvironment.preparePullRequestThread, { |
| 1145 | environmentId: queuedMessage.environmentId, |
| 1146 | input: { cwd: projectCwd, reference: creation.pullRequestReference, mode: "worktree" }, |
| 1147 | }); |
| 1148 | if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) || !hasAccess()) |
| 1149 | return; |
| 1150 | const failure = reportCommandFailure(queuedMessage, checkout, "start-turn"); |
| 1151 | if (failure?.action === "retry") return yield* retryDelivery; |
| 1152 | if (failure?.action === "restore") |
| 1153 | return yield* restoreQueuedMessage(persistedMessage, failure.message); |
| 1154 | if (!AsyncResult.isSuccess(checkout)) return yield* retryDelivery; |
| 1155 | if (!checkout.value.worktreePath || !checkout.value.isOnPullRequestHead) { |
| 1156 | const term = changeRequestTermKind( |
| 1157 | ( |
| 1158 | sourceControlClients.findByChangeRequestUrl(creation.pullRequestReference) ?? |
| 1159 | sourceControlClients.get(undefined) |
| 1160 | ).changeRequest, |
| 1161 | ); |
| 1162 | return yield* restoreQueuedMessage( |
| 1163 | persistedMessage, |
| 1164 | m( |
| 1165 | msg( |
| 1166 | "{term, select, mr {The MR worktree has local changes and could not update to the MR head. Resolve them before trying again.} cr {The change request worktree has local changes and could not update to the change request head. Resolve them before trying again.} other {The PR worktree has local changes and could not update to the PR head. Resolve them before trying again.}}", |
| 1167 | { term }, |
| 1168 | ), |
| 1169 | ), |
| 1170 | ); |
| 1171 | } |
| 1172 | workspace = { |
| 1173 | ...creation, |
| 1174 | workspaceMode: "local", |
| 1175 | branch: checkout.value.branch, |
| 1176 | worktreePath: checkout.value.worktreePath, |
| 1177 | startFromOrigin: false, |
| 1178 | }; |
| 1179 | } |
| 1180 | launchedCreations.set(persistedMessage.messageId, { revision: deliveryRevision, workspace }); |
| 1181 | const deliveryResult = yield* deliveryCommand(threadEnvironment.startTurn, { |
| 1182 | environmentId: queuedMessage.environmentId, |
| 1183 | input: buildProjectThreadStartTurnInput({ |
| 1184 | projectId: creation.projectId, |
| 1185 | projectCwd, |
| 1186 | threadId: queuedMessage.threadId, |
| 1187 | commandId: queuedMessage.commandId, |
| 1188 | messageId: queuedMessage.messageId, |
| 1189 | createdAt: queuedMessage.createdAt, |
| 1190 | titleText: queuedMessage.text, |
| 1191 | ...serializeComposerMessageForServer( |
| 1192 | queuedMessage.text.trim(), |
| 1193 | uploadedComposerContext( |
| 1194 | queuedMessage.context, |
| 1195 | queuedMessage.attachments, |
| 1196 | prepared.attachments, |
| 1197 | ), |
| 1198 | ), |
| 1199 | uploadedAttachments: prepared.attachments, |
| 1200 | modelSelection: sendSettings.modelSelection, |
| 1201 | runtimeMode: sendSettings.runtimeMode, |
| 1202 | interactionMode: sendSettings.interactionMode, |
| 1203 | workspaceMode: workspace.workspaceMode, |
| 1204 | branch: workspace.branch, |
| 1205 | worktreePath: workspace.worktreePath, |
| 1206 | startFromOrigin: workspace.startFromOrigin ?? false, |
| 1207 | worktreeBranchName: buildTemporaryWorktreeBranchName(randomHex), |
| 1208 | }), |
| 1209 | }); |
| 1210 | if (AsyncResult.isFailure(deliveryResult) && !hasAccess()) return; |
| 1211 | const failure = reportCommandFailure(queuedMessage, deliveryResult, "start-turn"); |
| 1212 | if (failure?.action === "retry") { |
| 1213 | return yield* retryDelivery; |
| 1214 | } |
| 1215 | if (failure?.action === "restore") { |
| 1216 | return yield* restoreQueuedMessage(persistedMessage, failure.message); |
| 1217 | } |
| 1218 | recordPendingThreadCreationOutcome({ kind: "delivered", message: persistedMessage }); |
| 1219 | const outcome = yield* completeQueuedMessageDelivery( |
| 1220 | persistedMessage, |
| 1221 | deliveryRevision, |
| 1222 | deliveredSequence(deliveryResult), |
| 1223 | ); |
| 1224 | if (outcome === "edited") { |
| 1225 | if (isEditingQueuedMessage(queuedMessage.messageId)) return; |
| 1226 | return yield* recoverEditedCreationAfterDelivery(persistedMessage); |
| 1227 | } |
| 1228 | if (outcome !== "removed") return yield* retryDelivery; |
| 1229 | }); |
| 1230 | |
| 1231 | /** The head of one thread's queue, as the inputs stand now. */ |
| 1232 | function nextStep( |
| 1233 | threadKey: string, |
| 1234 | queuedMessages: ReadonlyArray<QueuedThreadMessage>, |
| 1235 | inputs: Atom.Type<typeof deliveryInputsAtom>, |
| 1236 | ): ThreadDeliveryStep { |
| 1237 | const { |
| 1238 | editingQueuedMessageIds, |
| 1239 | shellStatuses, |
| 1240 | threads, |
| 1241 | projects, |
| 1242 | serverConfigs, |
| 1243 | connectedEnvironments, |
| 1244 | operableEnvironments, |
| 1245 | } = inputs; |
| 1246 | const nextQueuedMessage = queuedMessages[0]; |
| 1247 | if (!nextQueuedMessage) { |
| 1248 | return { _tag: "Wait" }; |
| 1249 | } |
| 1250 | const messageId = nextQueuedMessage.messageId; |
| 1251 | if ( |
| 1252 | nextQueuedMessage.creation === undefined && |
| 1253 | acknowledgedExistingThreadMessageIds.has(messageId) |
| 1254 | ) { |
| 1255 | if (retryNotBefore(messageId) > Date.now()) { |
| 1256 | return { _tag: "Wait", until: retryNotBefore(messageId) }; |
| 1257 | } |
| 1258 | return { |
| 1259 | _tag: "Dispatch", |
| 1260 | messageId, |
| 1261 | run: removeAcknowledgedExistingThreadMessage( |
| 1262 | nextQueuedMessage, |
| 1263 | acknowledgedExistingThreadMessageIds, |
| 1264 | ), |
| 1265 | }; |
| 1266 | } |
| 1267 | if (editingQueuedMessageIds[messageId]) { |
| 1268 | return { _tag: "Wait" }; |
| 1269 | } |
| 1270 | const blockedRecovery = blockedRecoveries.get(messageId); |
| 1271 | if (blockedRecovery) { |
| 1272 | if (blockedRecovery.message === nextQueuedMessage) { |
| 1273 | return { _tag: "Wait" }; |
| 1274 | } |
| 1275 | blockedRecoveries.delete(messageId); |
| 1276 | blockedRecovery.unsubscribe(); |
| 1277 | } |
| 1278 | if (retryNotBefore(messageId) > Date.now()) { |
| 1279 | return { _tag: "Wait", until: retryNotBefore(messageId) }; |
| 1280 | } |
| 1281 | let thread = findThread(threads, nextQueuedMessage); |
| 1282 | if (thread && scopedThreadKey(thread.environmentId, thread.id) !== threadKey) { |
| 1283 | return { _tag: "Wait" }; |
| 1284 | } |
| 1285 | |
| 1286 | const creation = nextQueuedMessage.creation; |
| 1287 | const environment = connectedEnvironments.find( |
| 1288 | (candidate) => candidate.environmentId === nextQueuedMessage.environmentId, |
| 1289 | ); |
| 1290 | const shellStatus = shellStatuses.get(nextQueuedMessage.environmentId) ?? "empty"; |
| 1291 | const serverConfig = serverConfigs.get(nextQueuedMessage.environmentId); |
| 1292 | let familyRead: ThreadFamilyRead | undefined; |
| 1293 | if ( |
| 1294 | thread === undefined && |
| 1295 | creation === undefined && |
| 1296 | serverConfig?.shellThreadFamilies === true |
| 1297 | ) { |
| 1298 | // The family keeps the row readable through dispatch once it loads; the current shell |
| 1299 | // generation's read decides whether the thread is gone. |
| 1300 | if (!familyMounts.has(nextQueuedMessage.messageId)) { |
| 1301 | familyMounts.set( |
| 1302 | nextQueuedMessage.messageId, |
| 1303 | threadFamilies.mount( |
| 1304 | appAtomRegistry, |
| 1305 | nextQueuedMessage.environmentId, |
| 1306 | nextQueuedMessage.threadId, |
| 1307 | ), |
| 1308 | ); |
| 1309 | } |
| 1310 | familyRead = threadFamilyRead( |
| 1311 | appAtomRegistry.get( |
| 1312 | threadFamilies.familyAtom(nextQueuedMessage.environmentId, nextQueuedMessage.threadId), |
| 1313 | ), |
| 1314 | ); |
| 1315 | if (familyRead === "settled") { |
| 1316 | // The rows read at the start of this pass can lag the family that just settled; |
| 1317 | // decide from the merged rows as they are now. |
| 1318 | thread = findThread( |
| 1319 | appAtomRegistry.get(environmentThreadShells.threadShellsAtom), |
| 1320 | nextQueuedMessage, |
| 1321 | ); |
| 1322 | } else if (familyRead === "failed") { |
| 1323 | threadFamilies.refresh( |
| 1324 | appAtomRegistry, |
| 1325 | nextQueuedMessage.environmentId, |
| 1326 | nextQueuedMessage.threadId, |
| 1327 | ); |
| 1328 | } |
| 1329 | } |
| 1330 | const resendLaunch = |
| 1331 | creation !== undefined && |
| 1332 | launchedCreations.get(messageId)?.revision === threadOutboxRevision(messageId); |
| 1333 | const deliveryAction = resolveThreadOutboxDeliveryAction({ |
| 1334 | isCreation: creation !== undefined, |
| 1335 | // A resend goes through the same gates as the first launch. |
| 1336 | threadExists: thread !== undefined && !resendLaunch, |
| 1337 | shellStatus, |
| 1338 | environmentConnected: environment?.connectionState === "connected", |
| 1339 | ...(familyRead === undefined ? {} : { familyRead }), |
| 1340 | }); |
| 1341 | if ( |
| 1342 | deliveryAction === "wait" && |
| 1343 | shellStatus === "live" && |
| 1344 | familyRead !== undefined && |
| 1345 | familyRead !== "settled" |
| 1346 | ) { |
| 1347 | // An empty family read changes no merged row, so nothing else would wake the thread. |
| 1348 | return { _tag: "Backoff", messageId }; |
| 1349 | } |
| 1350 | if (deliveryAction === "send" && !operableEnvironments.has(nextQueuedMessage.environmentId)) { |
| 1351 | return { _tag: "Wait" }; |
| 1352 | } |
| 1353 | const dispatchStep = resolveThreadOutboxDispatchStep({ |
| 1354 | deliveryAction, |
| 1355 | fileAttachments: nextQueuedMessage.attachments.filter( |
| 1356 | (attachment) => attachment.type === "file", |
| 1357 | ), |
| 1358 | serverConfig: serverConfig |
| 1359 | ? { |
| 1360 | maxFileUploadBytes: |
| 1361 | serverConfig.environment.capabilities.fileAttachments?.maxUploadBytes, |
| 1362 | } |
| 1363 | : null, |
| 1364 | }); |
| 1365 | if (dispatchStep.step === "wait") { |
| 1366 | return { _tag: "Wait" }; |
| 1367 | } |
| 1368 | if (dispatchStep.step === "retry") { |
| 1369 | return { _tag: "Backoff", messageId }; |
| 1370 | } |
| 1371 | if (dispatchStep.step === "restore") { |
| 1372 | const attachmentError = dispatchStep.reason; |
| 1373 | return { |
| 1374 | _tag: "Dispatch", |
| 1375 | messageId, |
| 1376 | run: Effect.gen(function* () { |
| 1377 | if (!(yield* confirmQueued(nextQueuedMessage)) || isEditingQueuedMessage(messageId)) { |
| 1378 | return; |
| 1379 | } |
| 1380 | yield* restoreQueuedMessage(nextQueuedMessage, attachmentError); |
| 1381 | }), |
| 1382 | }; |
| 1383 | } |
| 1384 | const creationProjectCwd = |
| 1385 | creation !== undefined |
| 1386 | ? (findCreationProject(projects, nextQueuedMessage)?.workspaceRoot ?? |
| 1387 | creation.projectCwd ?? |
| 1388 | null) |
| 1389 | : null; |
| 1390 | if (deliveryAction === "send" && creation !== undefined) { |
| 1391 | if (!isQueuedThreadCreationSendable(nextQueuedMessage)) { |
| 1392 | return { _tag: "Wait" }; |
| 1393 | } |
| 1394 | if (creationProjectCwd === null && shellStatus !== "live") { |
| 1395 | return { _tag: "Wait" }; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | const removeQueuedMessage = (warning: string) => |
| 1400 | storage("remove-unsendable", messageId, () => |
| 1401 | removeThreadOutboxMessage(nextQueuedMessage), |
| 1402 | ).pipe( |
| 1403 | Effect.catch((error) => |
| 1404 | Effect.logWarning(warning, { |
| 1405 | environmentId: nextQueuedMessage.environmentId, |
| 1406 | threadId: nextQueuedMessage.threadId, |
| 1407 | messageId, |
| 1408 | error: error.cause, |
| 1409 | }).pipe(Effect.andThen(retryDelivery)), |
| 1410 | ), |
| 1411 | Effect.asVoid, |
| 1412 | ); |
| 1413 | // Confirm durability before sending; optimistic enqueue can still roll back on write failure. |
| 1414 | const run = Effect.gen(function* () { |
| 1415 | if (!(yield* confirmQueued(nextQueuedMessage))) return; |
| 1416 | // An editor that took the message after confirmation owns it; that is not a failure. |
| 1417 | if (isEditingQueuedMessage(messageId)) return; |
| 1418 | if (deliveryAction === "send") { |
| 1419 | const liveThread = findThread( |
| 1420 | appAtomRegistry.get(environmentThreadShells.threadShellsAtom), |
| 1421 | nextQueuedMessage, |
| 1422 | ); |
| 1423 | const liveDeliveryAction = resolveThreadOutboxDeliveryAction({ |
| 1424 | isCreation: creation !== undefined, |
| 1425 | threadExists: liveThread !== undefined && !resendLaunch, |
| 1426 | shellStatus, |
| 1427 | environmentConnected: environment?.connectionState === "connected", |
| 1428 | }); |
| 1429 | if (liveDeliveryAction !== "send") return; |
| 1430 | } |
| 1431 | if (deliveryAction === "remove") { |
| 1432 | // Edited since launch, or launched before a restart that lost the revision: hand it back. |
| 1433 | return yield* creation !== undefined |
| 1434 | ? recoverEditedCreationAfterDelivery(nextQueuedMessage) |
| 1435 | : removeQueuedMessage("[thread-outbox] failed to remove message for a missing thread"); |
| 1436 | } |
| 1437 | if (creation !== undefined) { |
| 1438 | return yield* creationProjectCwd !== null |
| 1439 | ? sendQueuedCreation(nextQueuedMessage, creation, creationProjectCwd) |
| 1440 | : removeQueuedMessage("[thread-outbox] dropped pending task for a missing project"); |
| 1441 | } |
| 1442 | return yield* thread !== undefined |
| 1443 | ? sendQueuedMessage(nextQueuedMessage, thread) |
| 1444 | : retryDelivery; |
| 1445 | }); |
| 1446 | return { _tag: "Dispatch", messageId, run }; |
| 1447 | } |
| 1448 | |
| 1449 | // One fiber per thread with queued messages. Opening a thread's latch wakes its fiber to |
| 1450 | // reconsider its head; the fiber closes it before reading the inputs, so no wake is lost. |
| 1451 | // An entry outlives its interrupted fiber until that fiber exits, so a thread never has two. |
| 1452 | const threadFibers = new Map<string, ThreadFiber>(); |
| 1453 | |
| 1454 | const deliverThread = (threadKey: string, wake: Latch.Latch) => |
| 1455 | Effect.forever( |
| 1456 | Effect.gen(function* () { |
| 1457 | yield* wake.close; |
| 1458 | const inputs = appAtomRegistry.get(deliveryInputsAtom); |
| 1459 | const step = nextStep(threadKey, inputs.queuedMessagesByThreadKey[threadKey] ?? [], inputs); |
| 1460 | if (step._tag === "Backoff") { |
| 1461 | scheduleRetry(step.messageId); |
| 1462 | return; |
| 1463 | } |
| 1464 | if (step._tag === "Wait") { |
| 1465 | if (step.until === undefined) { |
| 1466 | yield* wake.await; |
| 1467 | return; |
| 1468 | } |
| 1469 | const now = yield* Clock.currentTimeMillis; |
| 1470 | yield* wake.await.pipe( |
| 1471 | Effect.timeoutOrElse({ |
| 1472 | duration: Math.max(0, step.until - now), |
| 1473 | orElse: () => Effect.void, |
| 1474 | }), |
| 1475 | ); |
| 1476 | return; |
| 1477 | } |
| 1478 | // A send that started finishes, even when its thread leaves the queue meanwhile; only |
| 1479 | // its wait on a shared upload stops early. |
| 1480 | beginDispatchingQueuedMessage(step.messageId); |
| 1481 | const sent = yield* step.run.pipe( |
| 1482 | Effect.as(true), |
| 1483 | Effect.catchTags({ DeliveryRetry: () => Effect.succeed(false) }), |
| 1484 | // A send that throws backs off like any failure, not straight back into the send. |
| 1485 | Effect.catchDefect((error) => |
| 1486 | Effect.logWarning("[thread-outbox] queued message delivery threw", { |
| 1487 | messageId: step.messageId, |
| 1488 | error, |
| 1489 | }).pipe(Effect.as(false)), |
| 1490 | ), |
| 1491 | Effect.ensuring(Effect.sync(() => finishDispatchingQueuedMessage(step.messageId))), |
| 1492 | Effect.uninterruptible, |
| 1493 | ); |
| 1494 | if (sent) clearRetry(step.messageId); |
| 1495 | else scheduleRetry(step.messageId); |
| 1496 | }), |
| 1497 | ); |
| 1498 | |
| 1499 | function wakeThreads(): void { |
| 1500 | for (const { wake } of threadFibers.values()) wake.openUnsafe(); |
| 1501 | } |
| 1502 | |
| 1503 | /** Starts a fiber for each thread with queued messages, stops the rest, and wakes them all. */ |
| 1504 | function onInputs(): void { |
| 1505 | if (disposed) return; |
| 1506 | const { queuedMessagesByThreadKey, serverConfigs, connectedEnvironments } = |
| 1507 | appAtomRegistry.get(deliveryInputsAtom); |
| 1508 | const queued = Object.values(queuedMessagesByThreadKey).flat(); |
| 1509 | // Failures while an environment was unreachable say nothing about it now; a backoff |
| 1510 | // started then would hold a message for up to its full delay after reconnecting. |
| 1511 | const ready = new Set( |
| 1512 | connectedEnvironments |
| 1513 | .filter( |
| 1514 | (environment) => |
| 1515 | environment.connectionState === "connected" && |
| 1516 | serverConfigs.has(environment.environmentId), |
| 1517 | ) |
| 1518 | .map((environment) => environment.environmentId), |
| 1519 | ); |
| 1520 | for (const message of queued) { |
| 1521 | if (ready.has(message.environmentId) && !readyEnvironmentIds.has(message.environmentId)) { |
| 1522 | clearRetry(message.messageId); |
| 1523 | } |
| 1524 | } |
| 1525 | readyEnvironmentIds = ready; |
| 1526 | |
| 1527 | const queuedMessageIds = new Set(queued.map((message) => message.messageId)); |
| 1528 | for (const messageId of acknowledgedExistingThreadMessageIds) { |
| 1529 | if (!queuedMessageIds.has(messageId)) acknowledgedExistingThreadMessageIds.delete(messageId); |
| 1530 | } |
| 1531 | for (const messageId of launchedCreations.keys()) { |
| 1532 | if (!queuedMessageIds.has(messageId)) launchedCreations.delete(messageId); |
| 1533 | } |
| 1534 | for (const [messageId, release] of familyMounts) { |
| 1535 | if (!queuedMessageIds.has(messageId)) { |
| 1536 | release(); |
| 1537 | familyMounts.delete(messageId); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | for (const [threadKey, entry] of threadFibers) { |
| 1542 | if ((queuedMessagesByThreadKey[threadKey]?.length ?? 0) > 0 || entry.retiring) continue; |
| 1543 | entry.retiring = true; |
| 1544 | if (entry.fiber !== undefined) Effect.runFork(Fiber.interrupt(entry.fiber)); |
| 1545 | } |
| 1546 | for (const [threadKey, messages] of Object.entries(queuedMessagesByThreadKey)) { |
| 1547 | if (messages.length === 0 || threadFibers.has(threadKey)) continue; |
| 1548 | // Registered before the fiber starts: its first step runs synchronously and can re-enter here. |
| 1549 | const entry: ThreadFiber = { |
| 1550 | wake: Latch.makeUnsafe(true), |
| 1551 | fiber: undefined, |
| 1552 | retiring: false, |
| 1553 | }; |
| 1554 | threadFibers.set(threadKey, entry); |
| 1555 | const fiber = Effect.runFork(deliverThread(threadKey, entry.wake)); |
| 1556 | entry.fiber = fiber; |
| 1557 | fiber.addObserver(() => { |
| 1558 | if (threadFibers.get(threadKey) !== entry) return; |
| 1559 | threadFibers.delete(threadKey); |
| 1560 | // A message queued while this fiber finished its send starts the next fiber now. |
| 1561 | if (!disposed) onInputs(); |
| 1562 | }); |
| 1563 | if (entry.retiring) Effect.runFork(Fiber.interrupt(fiber)); |
| 1564 | } |
| 1565 | wakeThreads(); |
| 1566 | } |
| 1567 | |
| 1568 | const stopDelivering = appAtomRegistry.subscribe(deliveryInputsAtom, onInputs, { |
| 1569 | immediate: true, |
| 1570 | }); |
| 1571 | const stopCollectingOutcomes = appAtomRegistry.subscribe( |
| 1572 | settledCreationOutcomeKeysAtom, |
| 1573 | (threadKeys) => { |
| 1574 | for (const threadKey of threadKeys) clearPendingThreadCreationOutcome(threadKey); |
| 1575 | }, |
| 1576 | { immediate: true }, |
| 1577 | ); |
| 1578 | |
| 1579 | return { |
| 1580 | drain: wakeThreads, |
| 1581 | dispose: () => { |
| 1582 | disposed = true; |
| 1583 | stopDelivering(); |
| 1584 | stopCollectingOutcomes(); |
| 1585 | for (const { fiber } of threadFibers.values()) { |
| 1586 | if (fiber !== undefined) Effect.runFork(Fiber.interrupt(fiber)); |
| 1587 | } |
| 1588 | threadFibers.clear(); |
| 1589 | retries.clear(); |
| 1590 | appAtomRegistry.set(retryingQueuedMessageIdsAtom, {}); |
| 1591 | for (const blocked of blockedRecoveries.values()) blocked.unsubscribe(); |
| 1592 | blockedRecoveries.clear(); |
| 1593 | for (const release of familyMounts.values()) release(); |
| 1594 | familyMounts.clear(); |
| 1595 | }, |
| 1596 | }; |
| 1597 | } |
| 1598 | |
| 1599 | function loadThreadOutbox(isActive: () => boolean): void { |
| 1600 | void threadOutboxManager.load().then((loaded) => { |
| 1601 | if (loaded || !isActive()) return; |
| 1602 | Dialog.alert( |
| 1603 | m(msg("Some queued messages could not be loaded")), |
| 1604 | m( |
| 1605 | msg( |
| 1606 | "Unreadable records and attachment files are still saved. Other messages can still be sent.", |
| 1607 | ), |
| 1608 | ), |
| 1609 | [ |
| 1610 | { text: m(msg("Dismiss")), style: "cancel" }, |
| 1611 | { text: m(msg("Retry")), onPress: () => loadThreadOutbox(isActive) }, |
| 1612 | ], |
| 1613 | ); |
| 1614 | }); |
| 1615 | } |
| 1616 | |
| 1617 | /** App-lifetime: loads the persisted outbox and delivers queued messages. */ |
| 1618 | export function installThreadOutboxDelivery(): () => void { |
| 1619 | let active = true; |
| 1620 | loadThreadOutbox(() => active); |
| 1621 | const delivery = createThreadOutboxDelivery(); |
| 1622 | return () => { |
| 1623 | active = false; |
| 1624 | delivery.dispose(); |
| 1625 | }; |
| 1626 | } |