fix(mobile): harden recorder, queue, context, and screen async lifecycles

This commit is contained in:
Yun Chan 2026-11-14 15:10:18 +09:00
parent 3f9e412257
commit bd3c1a98d9
25 changed files with 2279 additions and 68 deletions

View file

@ -406,6 +406,9 @@ async function requestAdmin(path: string, options: RequestOptions): Promise<unkn
options.signal?.addEventListener('abort', abortFromCaller, { once: true })
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
try {
if (options.signal?.aborted === true) {
throw new AdminServiceError('network', null, false)
}
const response = await fetch(`${SUPABASE_URL}/functions/v1/${path}`, {
method: options.method ?? 'GET',
headers: {

View file

@ -100,6 +100,9 @@ async function sendChatRequest(
const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? 45_000)
try {
if (options.signal?.aborted === true) {
throw new ChatServiceError('CANCELLED', false)
}
const headers: Record<string, string> = {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',

View file

@ -87,6 +87,14 @@ const DEVICE_COLUMNS = [
export async function registerCurrentDevice(
userId: string,
config: MobileRuntimeConfig,
): Promise<RegisteredDevice> {
return registerCurrentDeviceAttempt(userId, config, true)
}
async function registerCurrentDeviceAttempt(
userId: string,
config: MobileRuntimeConfig,
allowConflictRetry: boolean,
): Promise<RegisteredDevice> {
requireUserId(userId)
const existing = await supabase
@ -130,9 +138,11 @@ export async function registerCurrentDevice(
.select(DEVICE_COLUMNS)
.single()
if (inserted.error) {
// A simultaneous bootstrap may have won the unique key. Fetching again is
// safe and never clears a remote revocation.
if (inserted.error.code === '23505') return registerCurrentDevice(userId, config)
// A simultaneous bootstrap may have won the unique key. Retry once safely
// while preventing infinite recursion when conflict errors persist.
if (inserted.error.code === '23505' && allowConflictRetry) {
return registerCurrentDeviceAttempt(userId, config, false)
}
throw new DeviceServiceError('server')
}
return normalizeRegisteredDevice(inserted.data)

View file

@ -209,6 +209,7 @@ export function useHistorySync(): HistorySyncModel {
try {
const cached = await loadHistoryCache(userId)
if (currentRequest !== requestId.current) return
if (cached !== null) {
commitWindow({
entries: filterCachedHistory(cached.entries, filter, debouncedSearch),
@ -222,6 +223,7 @@ export function useHistorySync(): HistorySyncModel {
setOffline(normalized.code === 'network')
}
} catch (cacheError) {
if (currentRequest !== requestId.current) return
setCacheWarning(true)
setError(normalized.code === 'network' ? normalized : syncError(cacheError))
setOffline(normalized.code === 'network')
@ -378,6 +380,7 @@ export function useHistorySync(): HistorySyncModel {
entry.revision,
{ is_favorite: optimisticFavorite },
)
if (!isCurrentScope()) return
const replacement: HistoryListEntry = { ...entry, ...updated }
updateEntries((current) => {
if (filter === 'favorites' && !updated.is_favorite) {
@ -387,9 +390,11 @@ export function useHistorySync(): HistorySyncModel {
})
await patchCachedEntry(entry.id, replacement, isCurrentScope)
} catch (mutationError) {
updateEntries((current) => current.map((item) => (
item.id === entry.id ? entry : item
)))
if (isCurrentScope()) {
updateEntries((current) => current.map((item) => (
item.id === entry.id ? entry : item
)))
}
throw mutationError
} finally {
endMutation(entry.id)
@ -404,9 +409,12 @@ export function useHistorySync(): HistorySyncModel {
updateEntries((current) => current.filter((item) => item.id !== entry.id))
try {
await deleteHistoryRevisionSafe(userId, entry.id, entry.revision)
if (!isCurrentScope()) return
await patchCachedEntry(entry.id, null, isCurrentScope)
} catch (mutationError) {
updateEntries((current) => mergeHistoryEntries(current, [entry]))
if (isCurrentScope()) {
updateEntries((current) => mergeHistoryEntries(current, [entry]))
}
throw mutationError
} finally {
endMutation(entry.id)

View file

@ -438,7 +438,14 @@ async function processItemCore(item: DurableQueueItem): Promise<AudioPipelineRes
};
await replaceItem(current);
await reportMeetingFailure(current, error, !canRetry);
if (canRetry) scheduler.arm(current.userId, current.nextAttemptAtMs);
if (discardedItems.has(current.id)) {
discardedItems.delete(current.id);
await removeItem(current).catch(() => undefined);
throw new AudioPipelineError('cancelled', 'Queued audio processing was cancelled', error);
}
if (canRetry && !suspendedUsers.has(current.userId)) {
scheduler.arm(current.userId, current.nextAttemptAtMs);
}
throw canRetry ? new QueuedAudioPendingError(current) : error;
} finally {
runningControllers.delete(item.id);
@ -523,6 +530,11 @@ export async function retryQueuedAudio(userId: string, itemId: string): Promise<
const items = await readQueue();
const item = items.find(candidate => candidate.id === itemId && candidate.userId === userId);
if (item === undefined) throw new AudioPipelineError('file-read', 'Queued audio was not found');
if (item.status === 'running' || runningItemPromises.has(item.id)) return;
if (item.status === 'cleanup') {
await removeItem(item);
return;
}
await replaceItem({
...item,
status: 'retry',

View file

@ -280,6 +280,7 @@ export async function streamTalkResponse(
const generationId = response.headers.get('X-D3RO-Generation-Id')?.trim() ?? ''
if (!isUuid(generationId)) {
await response.body?.cancel?.().catch(() => undefined)
throw new ChatServiceError('INVALID_RESPONSE', true)
}
const text = await consumeStreamingBody(response, options.onTextDelta)