fix: red-team round 3 hardening across desktop, mobile, core and server

Batch of red-team r3 fixes that were in the working tree before the
2026-09-28 design overhaul, committed as one unit with their tests.

- desktop main: STT timeouts and sidecar, voice recording store, sync
  (credentials, audio, knowledge reindex, push gates), runtime
  provisioner, update policy, AltGr keybindings, voice-command policy,
  dictionary file codec/limits, meeting transcript condensing and a
  local recording ledger so interrupted-session recovery only closes
  meetings this device recorded (a phone's live meeting is left alone).
- mobile: login CSRF via implicit token callbacks rejected, account
  deletion/retention, durable queue retention, knowledge realtime
  without unfiltered DELETE, meeting re-record failure paths, cloud STT
  client, preferences store/resync.
- core: text chunking splits long unbroken transcripts to fit, template
  field policy, dictionary limits, meeting markdown inline handling.
- server: payple webhook policy and cancellation order scope, meeting
  document generation quota, team RPC null-role guard, unified LLM
  quota in-flight accounting, knowledge chunk vector index, meeting
  re-record failure paths (migrations 20260929*).
- ci: portable/runtime feed gates, update-policy schema, Forgejo file
  delete and alias planning.

Four older tests are updated to the new contracts rather than the old
behavior: token-pair auth callbacks are rejected, knowledge realtime no
longer subscribes to DELETE, long transcript lines are split, and
meeting recovery requires the local recording ledger for empty rows.
This commit is contained in:
Yun Chan 2026-09-28 20:45:52 +09:00
parent 2428ede03d
commit ba9ef9741e
161 changed files with 17056 additions and 2379 deletions

View file

@ -0,0 +1,122 @@
// src/features/account/account-deletion-service.ts — account deletion use case.
//
// The screen only renders; the server contract (account-delete Edge Function),
// the failure mapping and the side-effect ordering live here so they can be
// tested without rendering UI.
import {
edgeFailureField,
invokeEdgeFunction,
type EdgeFunctionFailure,
type EdgeFunctionInvoker,
} from '../../lib/edge-functions'
export type AccountDeletionFailureCode =
| 'SERVER_ENDPOINT_UNAVAILABLE'
| 'REAUTHENTICATION_REQUIRED'
| 'ACTIVE_SUBSCRIPTION'
| 'REQUEST_FAILED'
export type AccountDeletionResult =
| { ok: true }
| { ok: false; code: AccountDeletionFailureCode; message?: string }
/** Server-owned boundary for deleting auth.users and its cascaded account data. */
export interface AccountDeletionService {
deleteCurrentAccount(): Promise<AccountDeletionResult>
}
export const ACCOUNT_DELETE_FUNCTION = 'account-delete'
export const ACCOUNT_DELETE_CONFIRMATION = 'DELETE_MY_ACCOUNT'
/**
* account-delete puts the machine code in `code` and an English diagnostic in
* `error`. The diagnostic is kept for logs only; UI copy comes from i18n.
*/
export function mapAccountDeletionFailure(failure: EdgeFunctionFailure): AccountDeletionResult {
const serverCode = edgeFailureField(failure, 'code')
const message = edgeFailureField(failure, 'error') ?? failure.message ?? undefined
if (serverCode === 'REAUTHENTICATION_REQUIRED') {
return { ok: false, code: 'REAUTHENTICATION_REQUIRED', message }
}
if (serverCode === 'ACTIVE_SUBSCRIPTION') {
return { ok: false, code: 'ACTIVE_SUBSCRIPTION', message }
}
if (failure.status === 404) {
return { ok: false, code: 'SERVER_ENDPOINT_UNAVAILABLE', message }
}
return { ok: false, code: 'REQUEST_FAILED', message }
}
export function createEdgeAccountDeletionService(
invoke: EdgeFunctionInvoker = invokeEdgeFunction,
): AccountDeletionService {
return Object.freeze({
async deleteCurrentAccount(): Promise<AccountDeletionResult> {
const result = await invoke<{ success?: unknown; error?: unknown }>(
ACCOUNT_DELETE_FUNCTION,
{ confirmation: ACCOUNT_DELETE_CONFIRMATION },
)
if (!result.ok) return mapAccountDeletionFailure(result)
if (result.data?.success !== true) {
const message = typeof result.data?.error === 'string' ? result.data.error : undefined
return { ok: false, code: 'REQUEST_FAILED', message }
}
return { ok: true }
},
})
}
export const edgeAccountDeletionService: AccountDeletionService = createEdgeAccountDeletionService()
export type AccountDeletionFailureMessageKey =
| 'mobile.account.deleteUnavailable'
| 'mobile.account.reauthenticationRequired'
| 'mobile.account.deleteFailed'
/** i18n key for a deletion failure. Raw server/SDK text is never shown. */
export function accountDeletionFailureMessageKey(
code: AccountDeletionFailureCode,
): AccountDeletionFailureMessageKey {
switch (code) {
case 'SERVER_ENDPOINT_UNAVAILABLE':
return 'mobile.account.deleteUnavailable'
case 'REAUTHENTICATION_REQUIRED':
return 'mobile.account.reauthenticationRequired'
default:
return 'mobile.account.deleteFailed'
}
}
export interface AccountDeletionWorkflow {
deletionService: AccountDeletionService
/**
* Detaches this device's push registration. Runs only after the server has
* confirmed deletion, so a refused deletion (re-auth, active subscription)
* leaves the still-live account's notifications intact.
*/
detachPushRegistration: () => Promise<void>
/** Removes every account-owned local artifact and the secure session. */
purgeLocalSession: () => Promise<void>
}
/**
* Deletes the current account. Order: server deletion → push detach
* (best-effort, needs the session) → local purge. A failed deletion performs
* no side effect. A purge failure propagates to the caller.
*/
export async function runAccountDeletion(
workflow: AccountDeletionWorkflow,
): Promise<AccountDeletionResult> {
const result = await workflow.deletionService.deleteCurrentAccount()
if (!result.ok) return result
// The account no longer exists, so a push-detach failure must not block the
// local privacy purge; detachPushRegistrationForLogout already tolerates a
// single-boundary failure and only throws when both boundaries fail.
try {
await workflow.detachPushRegistration()
} catch {
// Best-effort by design; see above.
}
await workflow.purgeLocalSession()
return result
}

View file

@ -21,16 +21,20 @@ import {
countTranscriptWords,
decodeBase64,
} from './audio-validation';
import { createMultipartAudioBody } from './multipart-audio';
import { createLinkedDeadline } from './linked-deadline';
import { transcribeAudioLocally } from './local-whisper-transcription';
import { uploadAudioResumably } from './resumable-audio-upload';
import {
beginMeetingProcessing,
completeMeetingProcessing,
} from '../meetings/meetings-service';
import { transcribeCloud } from './stt-cloud-client';
import { transcribeWithLocalFallback } from './stt-engine';
import { createLogger } from '../../lib/logger';
const UPLOAD_TIMEOUT_MS = 120_000;
const TRANSCRIPTION_TIMEOUT_MS = 300_000;
const log = createLogger('AudioPipeline');
interface OperationState {
audioFile: AudioFile | null;
@ -41,12 +45,6 @@ interface OperationState {
processingJobId: string | null;
}
interface LinkedDeadline {
signal: AbortSignal;
timedOut: () => boolean;
dispose: () => void;
}
function database(): D3roSupabaseClient {
return supabase as unknown as D3roSupabaseClient;
}
@ -78,31 +76,6 @@ async function requireCurrentSession(
return session;
}
function createLinkedDeadline(
parentSignal: AbortSignal,
timeoutMs: number,
): LinkedDeadline {
const controller = new AbortController();
let didTimeOut = false;
const abortFromParent = (): void => controller.abort();
parentSignal.addEventListener('abort', abortFromParent, { once: true });
const timeout = setTimeout(() => {
didTimeOut = true;
controller.abort();
}, timeoutMs);
if (parentSignal.aborted) controller.abort();
return {
signal: controller.signal,
timedOut: () => didTimeOut,
dispose: () => {
clearTimeout(timeout);
parentSignal.removeEventListener('abort', abortFromParent);
},
};
}
function encodedStoragePath(storageKey: string): string {
return storageKey.split('/').map(encodeURIComponent).join('/');
}
@ -259,122 +232,6 @@ async function uploadAudio(
}
}
function parseTranscriptionResponse(
value: unknown,
latencyMs: number,
): MobileTranscriptionResult {
if (typeof value !== 'object' || value === null) {
throw new AudioPipelineError(
'transcription',
'The transcription service returned an invalid response',
);
}
const candidate = value as Record<string, unknown>;
if (
typeof candidate.transcript !== 'string' ||
typeof candidate.confidence !== 'number' ||
!Number.isFinite(candidate.confidence) ||
typeof candidate.language_code !== 'string' ||
candidate.language_code.trim() === '' ||
typeof candidate.duration_seconds !== 'number' ||
!Number.isFinite(candidate.duration_seconds) ||
candidate.duration_seconds < 0 ||
typeof candidate.provider !== 'string' ||
candidate.provider.trim() === ''
) {
throw new AudioPipelineError(
'transcription',
'The transcription service returned incomplete audio results',
);
}
return {
text: candidate.transcript,
confidence: Math.max(0, Math.min(1, candidate.confidence)),
language: candidate.language_code,
durationSeconds: candidate.duration_seconds,
provider: candidate.provider,
latencyMs,
};
}
async function transcribeAudioFile(
input: LocalAudioInput,
audioBytes: Uint8Array,
session: Session,
languageCode: string,
parentSignal: AbortSignal,
): Promise<MobileTranscriptionResult> {
const multipart = createMultipartAudioBody(input, audioBytes, languageCode);
const deadline = createLinkedDeadline(parentSignal, TRANSCRIPTION_TIMEOUT_MS);
const startedAt = Date.now();
try {
const response = await fetch(`${SUPABASE_URL}/functions/v1/stt-proxy`, {
method: 'POST',
headers: {
apikey: SUPABASE_ANON_KEY,
Authorization: `Bearer ${session.access_token}`,
'Content-Type': `multipart/form-data; boundary=${multipart.boundary}`,
},
body: multipart.body,
signal: deadline.signal,
});
if (!response.ok) {
if (response.status === 401 || response.status === 403) {
throw new AudioPipelineError(
'auth',
'The transcription session is no longer authorized',
);
}
if (response.status === 413) {
throw new AudioPipelineError(
'file-too-large',
'The transcription service rejected the file size',
);
}
if (response.status === 429) {
throw new AudioPipelineError(
'quota',
'Your transcription quota has been reached',
);
}
if (response.status === 503) {
throw new AudioPipelineError(
'provider-unavailable',
'No transcription provider is currently available',
);
}
throw new AudioPipelineError(
'transcription',
`Transcription failed with status ${response.status}`,
);
}
const body: unknown = await response.json();
return parseTranscriptionResponse(body, Date.now() - startedAt);
} catch (error) {
if (error instanceof AudioPipelineError) throw error;
if (parentSignal.aborted) {
throw new AudioPipelineError('cancelled', 'Transcription was cancelled');
}
if (deadline.timedOut()) {
throw new AudioPipelineError(
'provider-unavailable',
'The transcription service timed out',
error,
);
}
throw new AudioPipelineError(
'transcription',
'The transcription service could not be reached',
error,
);
} finally {
deadline.dispose();
}
}
function historyTitle(fileName: string): string {
const withoutExtension = fileName.replace(/\.[^.]+$/, '').trim();
return (withoutExtension === '' ? 'Audio transcription' : withoutExtension).slice(0, 160);
@ -741,52 +598,28 @@ async function processCore(
options.signal,
);
state.lastAccessToken = transcriptionSession.access_token;
let transcription: MobileTranscriptionResult;
try {
transcription = await transcribeAudioFile(
const transcription: MobileTranscriptionResult = await transcribeWithLocalFallback({
input,
languageCode: options.languageCode,
signal: options.signal,
cloud: () => transcribeCloud({
input,
audioBytes,
transcriptionSession,
options.languageCode,
options.signal,
);
} catch (cloudError) {
if (
!(cloudError instanceof AudioPipelineError) ||
(cloudError.code !== 'provider-unavailable' &&
cloudError.code !== 'transcription')
) {
throw cloudError;
}
audioBytes = new Uint8Array(0);
try {
transcription = await transcribeAudioLocally(
input,
options.languageCode,
options.signal,
bytes: audioBytes,
accessToken: transcriptionSession.access_token,
languageCode: options.languageCode,
signal: options.signal,
timeoutMs: TRANSCRIPTION_TIMEOUT_MS,
}),
local: transcribeAudioLocally,
onFallback: cloudError => {
// Release the uploaded bytes before on-device Whisper decodes the file.
audioBytes = new Uint8Array(0);
log.warn(
'cloud STT failed, falling back to on-device whisper:',
`${cloudError.code}: ${cloudError.message}`,
);
} catch (localError) {
if (
localError instanceof AudioPipelineError &&
localError.code === 'cancelled'
) {
throw localError;
}
if (localError instanceof AudioPipelineError) {
throw new AudioPipelineError(
localError.code,
localError.message,
{ cloudError, localError },
);
}
throw new AudioPipelineError(
'transcription',
'Cloud and on-device transcription both failed',
{ cloudError, localError },
);
}
}
},
});
options.onPhase?.('saving');
await requireCurrentSession(options.expectedUserId, options.signal);

View file

@ -0,0 +1,35 @@
/**
* An AbortSignal that fires when either the parent signal aborts or the
* deadline elapses. `timedOut()` tells the two apart so callers can map a
* deadline to a retryable provider error and a parent abort to cancellation.
*/
export interface LinkedDeadline {
signal: AbortSignal;
timedOut: () => boolean;
dispose: () => void;
}
export function createLinkedDeadline(
parentSignal: AbortSignal,
timeoutMs: number,
): LinkedDeadline {
const controller = new AbortController();
let didTimeOut = false;
const abortFromParent = (): void => controller.abort();
parentSignal.addEventListener('abort', abortFromParent, { once: true });
const timeout = setTimeout(() => {
didTimeOut = true;
controller.abort();
}, timeoutMs);
if (parentSignal.aborted) controller.abort();
return {
signal: controller.signal,
timedOut: () => didTimeOut,
dispose: () => {
clearTimeout(timeout);
parentSignal.removeEventListener('abort', abortFromParent);
},
};
}

View file

@ -0,0 +1,170 @@
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '@d3ro/core/supabase-config';
import {
AudioPipelineError,
type LocalAudioInput,
type MobileTranscriptionResult,
} from './audio-import-types';
import { createLinkedDeadline } from './linked-deadline';
import { createMultipartAudioBody } from './multipart-audio';
/**
* The one mobile client for the `stt-proxy` edge function. Talk and the
* file/meeting pipeline both go through here so the HTTP contract (status
* table, response shape, timeout/cancel mapping) is defined exactly once.
*/
export const STT_PROXY_ENDPOINT = `${SUPABASE_URL}/functions/v1/stt-proxy`;
const MAX_BODY_SNIPPET = 160;
export interface CloudSttRequest {
input: LocalAudioInput;
bytes: Uint8Array;
accessToken: string;
languageCode: string;
signal: AbortSignal;
/** Talk waits 120 s, the file/meeting pipeline 300 s. */
timeoutMs: number;
}
function bodySnippet(bodyText: string): string {
return bodyText.replace(/\s+/g, ' ').trim().slice(0, MAX_BODY_SNIPPET);
}
function describe(status: number, bodyText: string): string {
const snippet = bodySnippet(bodyText);
return snippet.length === 0 ? `HTTP ${status}` : `HTTP ${status}: ${snippet}`;
}
/**
* Maps an stt-proxy HTTP status (and its body, for diagnostics) to the
* pipeline error it stands for. Returns null for a 2xx answer.
*
* stt-proxy contract (server/supabase/functions/stt-proxy/handler.ts):
* - 401/403 auth rejected -> 'auth'
* - 413 audio_too_large -> 'file-too-large'
* - 422 stt_no_speech (quota already billed) -> 'no-speech'
* - 429 quota_exceeded -> 'quota'
* - 502 stt_upstream_failed / 503 stt_provider_unavailable / 504 gateway
* -> 'provider-unavailable'
* - anything else (400, 415, 500, ...) -> 'transcription'
*/
export function classifySttResponse(
status: number,
bodyText = '',
): AudioPipelineError | null {
if (status >= 200 && status < 300) return null;
const detail = describe(status, bodyText);
switch (status) {
case 401:
case 403:
return new AudioPipelineError('auth', `The speech session is no longer authorized (${detail})`);
case 413:
return new AudioPipelineError('file-too-large', `The speech service rejected the file size (${detail})`);
case 422:
return new AudioPipelineError('no-speech', `No speech was detected in the recording (${detail})`);
case 429:
return new AudioPipelineError('quota', `The speech transcription quota is exhausted (${detail})`);
case 502:
case 503:
case 504:
return new AudioPipelineError('provider-unavailable', `Cloud STT unavailable (${detail})`);
default:
return new AudioPipelineError('transcription', `Speech transcription failed (${detail})`);
}
}
function nonEmptyString(value: unknown): value is string {
return typeof value === 'string' && value.trim().length > 0;
}
/**
* Parses a 2xx stt-proxy body. `confidence` is optional (not every provider
* reports one) and an empty transcript is rejected: stt-proxy reports silence
* as 422, so an empty 2xx transcript is an unusable response, not a result.
*/
export function parseSttResult(value: unknown, latencyMs: number): MobileTranscriptionResult {
if (typeof value !== 'object' || value === null) {
throw new AudioPipelineError('transcription', 'The speech service returned invalid data');
}
const body = value as Record<string, unknown>;
if (
!nonEmptyString(body.transcript)
|| !nonEmptyString(body.language_code)
|| typeof body.duration_seconds !== 'number'
|| !Number.isFinite(body.duration_seconds)
|| body.duration_seconds < 0
|| !nonEmptyString(body.provider)
) {
throw new AudioPipelineError('transcription', 'The speech service returned incomplete data');
}
const confidence = typeof body.confidence === 'number' && Number.isFinite(body.confidence)
? Math.max(0, Math.min(1, body.confidence))
: null;
return {
text: body.transcript.trim(),
confidence,
language: body.language_code.trim(),
durationSeconds: body.duration_seconds,
provider: body.provider.trim(),
latencyMs,
};
}
/** Rejects a missing bearer token before any audio leaves the device. */
export function requireSttAccessToken(accessToken: string): string {
const token = accessToken.trim();
if (token.length === 0) {
throw new AudioPipelineError('auth', 'Sign in to transcribe speech');
}
return token;
}
async function readBodyText(response: Response): Promise<string> {
try {
return typeof response.text === 'function' ? await response.text() : '';
} catch {
return '';
}
}
export async function transcribeCloud(request: CloudSttRequest): Promise<MobileTranscriptionResult> {
const token = requireSttAccessToken(request.accessToken);
if (request.signal.aborted) {
throw new AudioPipelineError('cancelled', 'Speech transcription was cancelled');
}
const multipart = createMultipartAudioBody(request.input, request.bytes, request.languageCode);
const deadline = createLinkedDeadline(request.signal, request.timeoutMs);
const startedAt = Date.now();
try {
const response = await fetch(STT_PROXY_ENDPOINT, {
method: 'POST',
headers: {
apikey: SUPABASE_ANON_KEY,
Authorization: `Bearer ${token}`,
'Content-Type': `multipart/form-data; boundary=${multipart.boundary}`,
},
body: multipart.body,
signal: deadline.signal,
});
if (!response.ok) {
const failure = classifySttResponse(response.status, await readBodyText(response))
?? new AudioPipelineError('transcription', `Speech transcription failed (HTTP ${response.status})`);
throw failure;
}
return parseSttResult(await response.json(), Date.now() - startedAt);
} catch (error) {
if (error instanceof AudioPipelineError) throw error;
if (request.signal.aborted) {
throw new AudioPipelineError('cancelled', 'Speech transcription was cancelled', error);
}
if (deadline.timedOut()) {
throw new AudioPipelineError('provider-unavailable', 'Cloud speech recognition timed out', error);
}
throw new AudioPipelineError('transcription', 'Cloud speech recognition could not be reached', error);
} finally {
deadline.dispose();
}
}

View file

@ -0,0 +1,74 @@
import {
AudioPipelineError,
type LocalAudioInput,
type MobileTranscriptionResult,
} from './audio-import-types';
import { shouldFallBackToLocal } from './stt-policy';
/**
* Port for anything that turns a local audio file into a transcript. The
* on-device Whisper runner (`transcribeAudioLocally`) satisfies it directly.
*/
export type SttEngine = (
input: LocalAudioInput,
languageCode: string,
signal: AbortSignal,
) => Promise<MobileTranscriptionResult>;
export interface FallbackTranscriptionRequest {
input: LocalAudioInput;
languageCode: string;
signal: AbortSignal;
/** The primary (cloud) attempt, already bound to its credentials and bytes. */
cloud: () => Promise<MobileTranscriptionResult>;
/** The secondary engine, used only when stt-policy allows it. */
local: SttEngine;
/** Observes the cloud failure right before the local engine runs. */
onFallback?: (cloudError: AudioPipelineError) => void;
}
/**
* Shared "cloud first, on-device second" orchestration for Talk and the
* file/meeting pipeline:
*
* 1. Any cloud error that stt-policy does not mark as fallback-eligible is
* rethrown untouched (auth, quota, no-speech, cancelled, ...).
* 2. A local failure keeps its own code and message and carries both errors
* as its cause; a local cancellation is rethrown as-is.
* 3. An empty local transcript is 'no-speech', never a completed result.
*/
export async function transcribeWithLocalFallback(
request: FallbackTranscriptionRequest,
): Promise<MobileTranscriptionResult> {
let cloudError: AudioPipelineError;
try {
return await request.cloud();
} catch (error) {
if (!(error instanceof AudioPipelineError) || !shouldFallBackToLocal(error.code)) {
throw error;
}
cloudError = error;
}
request.onFallback?.(cloudError);
let result: MobileTranscriptionResult;
try {
result = await request.local(request.input, request.languageCode, request.signal);
} catch (localError) {
if (localError instanceof AudioPipelineError) {
if (localError.code === 'cancelled') throw localError;
throw new AudioPipelineError(localError.code, localError.message, { cloudError, localError });
}
throw new AudioPipelineError(
'transcription',
'Cloud and on-device speech recognition both failed',
{ cloudError, localError },
);
}
if (result.text.trim().length === 0) {
throw new AudioPipelineError('no-speech', 'No speech was detected in the recording', cloudError);
}
return result;
}

View file

@ -0,0 +1,45 @@
import type { AudioPipelineErrorCode } from './audio-import-types';
/**
* Single source of truth for how the mobile STT pipelines react to an
* AudioPipelineError code. Pure and dependency-free so the durable queue, the
* file/meeting pipeline and Talk can all share it without pulling in IO.
*
* The two predicates are deliberately independent: an expired session
* ('auth') is worth retrying later from the queue, but must never be
* "rescued" by running on-device Whisper, which would bypass authentication
* and the server quota.
*/
/**
* Cloud failures that may be answered by on-device Whisper instead.
*
* - 'provider-unavailable': no provider configured, upstream outage, timeout.
* - 'transcription': unreachable service or an unusable response.
*
* Everything else is authoritative and must surface as-is. In particular
* 'no-speech' (stt-proxy 422 stt_no_speech) is a real, already billed answer:
* re-running a local model on silent audio only produces an empty or
* hallucinated transcript.
*/
const LOCAL_FALLBACK_CODES: ReadonlySet<AudioPipelineErrorCode> = new Set<AudioPipelineErrorCode>([
'provider-unavailable',
'transcription',
]);
/** Failures a durable queue item may be attempted again for. */
const RETRYABLE_CODES: ReadonlySet<AudioPipelineErrorCode> = new Set<AudioPipelineErrorCode>([
'upload',
'provider-unavailable',
'transcription',
'persist',
'auth',
]);
export function shouldFallBackToLocal(code: AudioPipelineErrorCode): boolean {
return LOCAL_FALLBACK_CODES.has(code);
}
export function isRetryable(code: AudioPipelineErrorCode): boolean {
return RETRYABLE_CODES.has(code);
}

View file

@ -0,0 +1,110 @@
import { AppState } from 'react-native'
/**
* Realtime policy for the mobile Knowledge screen.
*
* Supabase Postgres Changes does not apply RLS to DELETE events and cannot
* filter them, so an unfiltered DELETE subscription would deliver every
* account's deleted primary keys to every subscriber and make each client
* refetch on every deletion anywhere (N-client fan-out). Only INSERT/UPDATE are
* subscribed (RLS-scoped, optionally narrowed to the owner), and deletions are
* reconciled by foreground polling, mirroring use-history-sync.ts.
*/
export type KnowledgeRealtimeEvent = 'INSERT' | 'UPDATE'
export interface KnowledgeRealtimeBinding {
event: KnowledgeRealtimeEvent
schema: 'public'
table: 'knowledge_documents'
filter?: string
}
export const KNOWLEDGE_DELETION_RECONCILE_INTERVAL_MS = 45_000
const SUBSCRIBED_EVENTS: readonly KnowledgeRealtimeEvent[] = ['INSERT', 'UPDATE']
/**
* Pure: the postgres_changes bindings the Knowledge screen may open. DELETE is
* intentionally never included. `ownerId` must already be validated by the
* caller because it is interpolated into the realtime filter.
*/
export function knowledgeRealtimeBindings(ownerId?: string): KnowledgeRealtimeBinding[] {
return SUBSCRIBED_EVENTS.map((event) => ({
event,
schema: 'public',
table: 'knowledge_documents',
...(ownerId === undefined ? {} : { filter: `user_id=eq.${ownerId}` }),
}))
}
/** Port: whether the app is in the foreground, and changes to that state. */
export interface ForegroundStatePort {
isActive: () => boolean
onChange: (listener: (active: boolean) => void) => { remove: () => void }
}
/** Port: interval scheduling, injectable for tests. */
export interface IntervalPort {
set: (callback: () => void, ms: number) => ReturnType<typeof setInterval>
clear: (handle: ReturnType<typeof setInterval>) => void
}
export interface ForegroundReconciliationDeps {
foreground?: ForegroundStatePort
interval?: IntervalPort
intervalMs?: number
}
export interface ForegroundReconciliation {
stop: () => void
}
export const reactNativeForegroundState: ForegroundStatePort = {
isActive: () => AppState.currentState === 'active',
onChange: (listener) => {
const subscription = AppState.addEventListener('change', (state) => {
listener(state === 'active')
})
return { remove: () => subscription.remove() }
},
}
const systemInterval: IntervalPort = {
set: (callback, ms) => setInterval(callback, ms),
clear: (handle) => clearInterval(handle),
}
/**
* Calls `onReconcile` periodically while the app is in the foreground and once
* whenever it returns to the foreground, so remote deletions are picked up
* without a DELETE realtime subscription.
*/
export function startForegroundReconciliation(
onReconcile: () => void,
deps: ForegroundReconciliationDeps = {},
): ForegroundReconciliation {
const foreground = deps.foreground ?? reactNativeForegroundState
const interval = deps.interval ?? systemInterval
const intervalMs = deps.intervalMs ?? KNOWLEDGE_DELETION_RECONCILE_INTERVAL_MS
let stopped = false
let active = foreground.isActive()
const stateSubscription = foreground.onChange((nextActive) => {
const resumed = nextActive && !active
active = nextActive
if (resumed && !stopped) onReconcile()
})
const handle = interval.set(() => {
if (active && !stopped) onReconcile()
}, intervalMs)
return {
stop: () => {
if (stopped) return
stopped = true
interval.clear(handle)
stateSubscription.remove()
},
}
}

View file

@ -7,6 +7,11 @@ import type {
import { SUPABASE_URL } from '@d3ro/core/supabase-config'
import { supabase } from '../../lib/supabase'
import { createUuidV4 } from '../../lib/random-id'
import {
type ForegroundReconciliationDeps,
knowledgeRealtimeBindings,
startForegroundReconciliation,
} from './knowledge-realtime'
export type MobileKnowledgeFileType = Extract<KnowledgeFileType, 'txt' | 'md'>
@ -607,31 +612,40 @@ export async function searchKnowledge(
}
}
export interface KnowledgeDocumentSubscriptionOptions {
/**
* Owner whose documents the list shows. When given, INSERT/UPDATE events are
* narrowed to that owner so teammates' shared-document writes do not trigger
* list refetches for a list that only shows owned documents.
*/
userId?: string
/** Injectable foreground/interval ports for deletion reconciliation. */
reconciliation?: ForegroundReconciliationDeps
}
/**
* Live updates for the Knowledge list. Only INSERT/UPDATE are subscribed:
* Postgres Changes cannot filter DELETE events and does not apply RLS to them,
* so a DELETE subscription would leak other accounts' document ids and fan out
* a refetch to every client on every deletion. Deletions are reconciled by
* foreground polling instead (same policy as use-history-sync.ts).
*/
export function subscribeToKnowledgeDocuments(
onChanged: () => void,
onStatus: (connected: boolean) => void,
options: KnowledgeDocumentSubscriptionOptions = {},
): KnowledgeDocumentSubscription {
const channel: RealtimeChannel = supabase
.channel(`mobile-knowledge-${createUuidV4()}`)
.on('postgres_changes', {
event: 'INSERT',
schema: 'public',
table: 'knowledge_documents',
}, onChanged)
.on('postgres_changes', {
event: 'UPDATE',
schema: 'public',
table: 'knowledge_documents',
}, onChanged)
.on('postgres_changes', {
event: 'DELETE',
schema: 'public',
table: 'knowledge_documents',
}, onChanged)
.subscribe((status) => onStatus(status === 'SUBSCRIBED'))
if (options.userId !== undefined) requireUuid(options.userId, 'knowledge owner id')
let channel: RealtimeChannel = supabase.channel(`mobile-knowledge-${createUuidV4()}`)
for (const binding of knowledgeRealtimeBindings(options.userId)) {
channel = channel.on('postgres_changes', binding, onChanged)
}
channel = channel.subscribe((status) => onStatus(status === 'SUBSCRIBED'))
const reconciliation = startForegroundReconciliation(onChanged, options.reconciliation)
return {
unsubscribe: async () => {
reconciliation.stop()
await supabase.removeChannel(channel)
},
}

View file

@ -0,0 +1,39 @@
import type { Meeting, MeetingStatus } from '@d3ro/api-client'
/**
* Meeting states the server may answer with for each recording lifecycle
* transition. Pure policy: the RPC wrappers in meetings-service only decide
* whether a returned row confirms the transition they asked for.
*
* A capture that ends without a new transcript (cancelled, failed after
* upload, discarded while queued) leaves a meeting that still holds content
* from an earlier recording 'completed' (migrations 20260929002700,
* 20260929040000); only a meeting without earlier content becomes 'error'.
*/
export type MeetingRecordingTransition =
| 'recording-started'
| 'recording-queued'
| 'processing-completed'
| 'capture-abandoned'
const EXPECTED_STATES: Readonly<Record<MeetingRecordingTransition, readonly MeetingStatus[]>> = {
'recording-started': ['recording'],
'recording-queued': ['processing'],
'processing-completed': ['completed'],
'capture-abandoned': ['error', 'completed'],
}
export function expectedMeetingStates(
transition: MeetingRecordingTransition,
): readonly MeetingStatus[] {
return EXPECTED_STATES[transition]
}
/** True when `meeting` belongs to `userId` and is in a state `transition` may produce. */
export function confirmsMeetingTransition(
meeting: Pick<Meeting, 'user_id' | 'status'>,
userId: string,
transition: MeetingRecordingTransition,
): boolean {
return meeting.user_id === userId && EXPECTED_STATES[transition].includes(meeting.status)
}

View file

@ -17,6 +17,10 @@ import {
type LinkedAudioRef,
type LinkedAudioStore,
} from '../audio/linked-audio-cleanup'
import {
confirmsMeetingTransition,
type MeetingRecordingTransition,
} from './meeting-recording-state-policy'
export interface MeetingListOptions {
userId: string
@ -267,6 +271,18 @@ function assertMeetingRow(row: Meeting | null, expectedId?: string): Meeting {
return row
}
function assertMeetingTransition(
meeting: Meeting,
userId: string,
transition: MeetingRecordingTransition,
message: string,
): Meeting {
if (!confirmsMeetingTransition(meeting, userId, transition)) {
throw new MeetingServiceError('server', message)
}
return meeting
}
function assertProcessingJobRow(row: ProcessingJob | null): ProcessingJob {
if (row === null) {
throw new MeetingServiceError('server', 'Processing job was not returned')
@ -801,11 +817,12 @@ export async function beginMeetingRecording(
'mobile_begin_meeting_recording',
{ p_meeting_id: meetingId },
)
const meeting = assertMeetingRow(row, meetingId)
if (meeting.user_id !== userId || meeting.status !== 'recording') {
throw new MeetingServiceError('server', 'Meeting recording state was not confirmed')
}
return meeting
return assertMeetingTransition(
assertMeetingRow(row, meetingId),
userId,
'recording-started',
'Meeting recording state was not confirmed',
)
} catch (error) {
throw toMeetingServiceError(error)
}
@ -859,11 +876,12 @@ export async function queueMeetingRecording(
'mobile_queue_meeting_recording',
{ p_meeting_id: meetingId, p_duration_ms: Math.round(durationMs) },
)
const meeting = assertMeetingRow(row, meetingId)
if (meeting.user_id !== userId || meeting.status !== 'processing') {
throw new MeetingServiceError('server', 'Queued meeting state was not confirmed')
}
return meeting
return assertMeetingTransition(
assertMeetingRow(row, meetingId),
userId,
'recording-queued',
'Queued meeting state was not confirmed',
)
} catch (error) {
throw toMeetingServiceError(error)
}
@ -899,11 +917,12 @@ export async function completeMeetingProcessing(
p_stt_latency_ms: Math.round(result.sttLatencyMs),
},
)
const meeting = assertMeetingRow(row, meetingId)
if (meeting.user_id !== userId || meeting.status !== 'completed') {
throw new MeetingServiceError('server', 'Meeting completion was not confirmed')
}
return meeting
return assertMeetingTransition(
assertMeetingRow(row, meetingId),
userId,
'processing-completed',
'Meeting completion was not confirmed',
)
} catch (error) {
throw toMeetingServiceError(error)
}
@ -952,11 +971,13 @@ export async function cancelMeetingRecording(
'mobile_cancel_meeting_recording',
{ p_meeting_id: meetingId },
)
const meeting = assertMeetingRow(row, meetingId)
if (meeting.user_id !== userId || meeting.status !== 'error') {
throw new MeetingServiceError('server', 'Meeting cancellation was not confirmed')
}
return meeting
// A re-record of a meeting with earlier content returns to 'completed'.
return assertMeetingTransition(
assertMeetingRow(row, meetingId),
userId,
'capture-abandoned',
'Meeting cancellation was not confirmed',
)
} catch (error) {
throw toMeetingServiceError(error)
}
@ -978,11 +999,13 @@ export async function failMeetingRecording(
'mobile_fail_meeting_recording',
{ p_meeting_id: meetingId, p_error_message: safeMessage },
)
const meeting = assertMeetingRow(row, meetingId)
if (meeting.user_id !== userId || meeting.status !== 'error') {
throw new MeetingServiceError('server', 'Meeting error state was not confirmed')
}
return meeting
// A re-record of a meeting with earlier content returns to 'completed'.
return assertMeetingTransition(
assertMeetingRow(row, meetingId),
userId,
'capture-abandoned',
'Meeting error state was not confirmed',
)
} catch (error) {
throw toMeetingServiceError(error)
}

View file

@ -77,6 +77,12 @@ const listeners = new Set<(event: DurableQueueEvent) => void>();
const runningControllers = new Map<string, AbortController>();
const runningItemPromises = new Map<string, Promise<AudioPipelineResult>>();
const discardedItems = new Set<string>();
/**
* Owners whose session was lost while their items stay parked on the device.
* Timer-driven passes skip them so a signed-out owner's retries are not spent
* on certain auth failures; resumeQueuedAudioForUser (sign-in) lifts it.
*/
const suspendedUsers = new Set<string>();
function emit(event: DurableQueueEvent): void {
for (const listener of listeners) listener(event);
@ -458,11 +464,13 @@ export async function processQueuedAudioNow(
* overwritten from a stale snapshot.
*/
async function runResumePass(userId: string): Promise<void> {
if (suspendedUsers.has(userId)) return;
const order = (await readQueue())
.filter(item => item.userId === userId)
.sort((left, right) => left.createdAtMs - right.createdAtMs)
.map(item => item.id);
for (const itemId of order) {
if (suspendedUsers.has(userId)) return;
const item = await repository.get(itemId);
if (item === undefined || item.userId !== userId) continue;
if (item.status === 'terminal') continue;
@ -493,6 +501,7 @@ async function runResumePass(userId: string): Promise<void> {
* pass after it, and the returned promise settles when that pass is done.
*/
export function resumeQueuedAudioForUser(userId: string): Promise<void> {
suspendedUsers.delete(userId);
return scheduler.requestRun(userId);
}
@ -545,6 +554,28 @@ export async function clearQueuedAudioForUser(userId: string): Promise<void> {
if (firstRemovalError !== null) throw firstRemovalError;
}
/**
* Involuntary session loss: keeps every item of `ownerUserId` (files and
* entries, whatever their status) and suspends its retries until that owner
* signs back in; items of any other account are discarded as usual.
*/
export async function retainQueuedAudioOnlyForUser(ownerUserId: string): Promise<void> {
suspendedUsers.add(ownerUserId);
scheduler.cancel(ownerUserId);
const otherUserIds = [...new Set((await readQueue())
.map(item => item.userId)
.filter(userId => userId !== ownerUserId))];
let firstError: unknown = null;
for (const userId of otherUserIds) {
try {
await clearQueuedAudioForUser(userId);
} catch (error) {
firstError ??= error;
}
}
if (firstError !== null) throw firstError;
}
export async function clearAllQueuedAudio(): Promise<void> {
const userIds = [...new Set((await readQueue()).map(item => item.userId))];
let firstError: unknown = null;

View file

@ -1,15 +1,16 @@
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '@d3ro/core/supabase-config'
import { FileSystem } from 'react-native-file-access'
import type { RecordedAudio } from '../../lib/audio-recorder'
import { createLogger } from '../../lib/logger'
import {
AudioPipelineError,
type LocalAudioInput,
type MobileTranscriptionResult,
} from '../import/audio-import-types'
import { decodeBase64 } from '../import/audio-validation'
import { createMultipartAudioBody } from '../import/multipart-audio'
import { prepareRecordedAudio } from '../import/recorded-audio-input'
import { transcribeAudioLocally } from '../import/local-whisper-transcription'
import { requireSttAccessToken, transcribeCloud } from '../import/stt-cloud-client'
import { transcribeWithLocalFallback } from '../import/stt-engine'
interface TalkTranscriptionOptions {
accessToken: string
@ -20,32 +21,7 @@ interface TalkTranscriptionOptions {
}
const DEFAULT_STT_TIMEOUT_MS = 120_000
interface LinkedDeadline {
signal: AbortSignal
didTimeOut: () => boolean
dispose: () => void
}
function linkedDeadline(parent: AbortSignal, timeoutMs: number): LinkedDeadline {
const controller = new AbortController()
let timedOut = false
const abortFromParent = (): void => controller.abort()
parent.addEventListener('abort', abortFromParent, { once: true })
if (parent.aborted) controller.abort()
const timer = setTimeout(() => {
timedOut = true
controller.abort()
}, timeoutMs)
return {
signal: controller.signal,
didTimeOut: () => timedOut,
dispose: () => {
clearTimeout(timer)
parent.removeEventListener('abort', abortFromParent)
},
}
}
const log = createLogger('Talk')
function ensureNotCancelled(signal: AbortSignal): void {
if (signal.aborted) {
@ -53,38 +29,8 @@ function ensureNotCancelled(signal: AbortSignal): void {
}
}
function parseCloudResult(value: unknown, latencyMs: number): MobileTranscriptionResult {
if (typeof value !== 'object' || value === null) {
throw new AudioPipelineError('transcription', 'The speech service returned invalid data')
}
const body = value as Record<string, unknown>
if (
typeof body.transcript !== 'string'
|| body.transcript.trim().length === 0
|| typeof body.language_code !== 'string'
|| body.language_code.trim().length === 0
|| typeof body.duration_seconds !== 'number'
|| !Number.isFinite(body.duration_seconds)
|| body.duration_seconds < 0
|| typeof body.provider !== 'string'
|| body.provider.trim().length === 0
) {
throw new AudioPipelineError('transcription', 'The speech service returned incomplete data')
}
const confidence = typeof body.confidence === 'number' && Number.isFinite(body.confidence)
? Math.max(0, Math.min(1, body.confidence))
: null
return {
text: body.transcript.trim(),
confidence,
language: body.language_code.trim(),
durationSeconds: body.duration_seconds,
provider: body.provider.trim(),
latencyMs,
}
}
// Talk reads the recording itself so the "file changed under us" size check
// stays next to the read, before any byte is sent to the cloud.
async function readBytes(input: LocalAudioInput): Promise<Uint8Array> {
try {
const bytes = decodeBase64(await FileSystem.readFile(input.path, 'base64'))
@ -98,70 +44,22 @@ async function readBytes(input: LocalAudioInput): Promise<Uint8Array> {
}
}
async function transcribeCloud(
async function transcribeTalkCloud(
input: LocalAudioInput,
accessToken: string,
languageCode: string,
signal: AbortSignal,
timeoutMs: number,
options: TalkTranscriptionOptions,
): Promise<MobileTranscriptionResult> {
const token = accessToken.trim()
if (token.length === 0) throw new AudioPipelineError('auth', 'Sign in to transcribe speech')
ensureNotCancelled(signal)
const accessToken = requireSttAccessToken(options.accessToken)
ensureNotCancelled(options.signal)
const bytes = await readBytes(input)
ensureNotCancelled(signal)
const multipart = createMultipartAudioBody(input, bytes, languageCode)
const deadline = linkedDeadline(signal, timeoutMs)
const startedAt = Date.now()
try {
const response = await fetch(`${SUPABASE_URL}/functions/v1/stt-proxy`, {
method: 'POST',
headers: {
apikey: SUPABASE_ANON_KEY,
Authorization: `Bearer ${token}`,
'Content-Type': `multipart/form-data; boundary=${multipart.boundary}`,
},
body: multipart.body,
signal: deadline.signal,
})
if (!response.ok) {
if (response.status === 401 || response.status === 403) {
throw new AudioPipelineError('auth', 'The speech session is no longer authorized')
}
if (response.status === 413) {
throw new AudioPipelineError('file-too-large', 'The recording is too large')
}
if (response.status === 429) {
throw new AudioPipelineError('quota', 'The speech transcription quota is exhausted')
}
if (response.status === 502 || response.status === 503 || response.status === 504) {
// 진단: 엣지함수 본문(stt_provider_unavailable=시크릿 없음 vs
// stt_upstream_failed=제공자 실패)을 로그에서 구분할 수 있게 남긴다.
const bodyText = await response.text().catch(() => '')
throw new AudioPipelineError(
'provider-unavailable',
`Cloud STT unavailable (HTTP ${response.status}: ${bodyText.replace(/\s+/g, ' ').slice(0, 160)})`,
)
}
throw new AudioPipelineError(
'transcription',
`Speech transcription failed with status ${response.status}`,
)
}
return parseCloudResult(await response.json(), Date.now() - startedAt)
} catch (error) {
if (error instanceof AudioPipelineError) throw error
if (signal.aborted) {
throw new AudioPipelineError('cancelled', 'Talk transcription was cancelled', error)
}
if (deadline.didTimeOut()) {
throw new AudioPipelineError('provider-unavailable', 'Cloud speech recognition timed out', error)
}
throw new AudioPipelineError('transcription', 'Cloud speech recognition could not be reached', error)
} finally {
deadline.dispose()
}
ensureNotCancelled(options.signal)
return transcribeCloud({
input,
bytes,
accessToken,
languageCode: options.languageCode,
signal: options.signal,
timeoutMs: options.timeoutMs ?? DEFAULT_STT_TIMEOUT_MS,
})
}
export async function transcribeTalkRecording(
@ -172,59 +70,18 @@ export async function transcribeTalkRecording(
let primaryError: unknown = null
try {
try {
return await transcribeCloud(
input,
options.accessToken,
options.languageCode,
options.signal,
options.timeoutMs ?? DEFAULT_STT_TIMEOUT_MS,
)
} catch (cloudError) {
if (
!(cloudError instanceof AudioPipelineError)
|| cloudError.code === 'auth'
|| cloudError.code === 'cancelled'
|| cloudError.code === 'quota'
|| cloudError.code === 'file-too-large'
) {
throw cloudError
}
return await transcribeWithLocalFallback({
input,
languageCode: options.languageCode,
signal: options.signal,
cloud: () => transcribeTalkCloud(input, options),
local: transcribeAudioLocally,
// 진단: 클라우드가 실패해 기기 whisper로 폴백하는 이유를 남긴다.
console.warn(
'[Talk] cloud STT failed, falling back to on-device whisper:',
`${(cloudError as AudioPipelineError).code}: ${cloudError.message}`,
)
try {
const result = await transcribeAudioLocally(
input,
options.languageCode,
options.signal,
)
if (result.text.trim().length === 0) {
// 기기 whisper는 실행됐지만 한국어 등을 인식하지 못한 경우 —
// "모두 실패"가 아니다. 클라우드 실패 원인도 원인 체인에 남긴다.
throw new AudioPipelineError(
'no-speech',
'No speech was detected in the recording',
cloudError,
)
}
return result
} catch (localError) {
if (localError instanceof AudioPipelineError && localError.code === 'cancelled') {
throw localError
}
throw new AudioPipelineError(
localError instanceof AudioPipelineError ? localError.code : 'transcription',
localError instanceof Error
? localError.message
: 'Cloud and on-device speech recognition both failed',
{ cloudError, localError },
)
}
}
onFallback: cloudError => log.warn(
'cloud STT failed, falling back to on-device whisper:',
`${cloudError.code}: ${cloudError.message}`,
),
})
} catch (error) {
primaryError = error
throw error

View file

@ -2,6 +2,7 @@ import type { RealtimeChannel } from '@supabase/supabase-js'
import type { Meeting, Team, TeamRole } from '@d3ro/api-client'
import { PUBLIC_SITE_ORIGIN, SITE_URLS } from '@d3ro/core/web-urls'
import { supabase } from '../../lib/supabase'
import { invokeEdgeFunction, type EdgeFunctionFailure } from '../../lib/edge-functions'
export interface TeamSummary extends Team {
role: TeamRole
@ -407,23 +408,26 @@ async function invokeRpc(name: string, params: Record<string, unknown>): Promise
}
}
async function readFunctionFailure(error: unknown): Promise<TeamServiceError> {
const context = (error as { context?: unknown })?.context
if (context instanceof Response) {
try {
const payload: unknown = await context.clone().json()
if (isRecord(payload)) {
const code = typeof payload.error === 'string' ? payload.error : ''
const message = typeof payload.message === 'string'
? payload.message
: code || `Team function returned ${context.status}`
return toTeamServiceError({ code, message })
}
} catch {
return toTeamServiceError({ message: `Team function returned ${context.status}` })
/**
* Team Edge Functions put the machine code in `error` and a human string in
* `message`. The transport decoding lives in lib/edge-functions; this keeps
* only the team-specific field selection and fallbacks.
*/
function readFunctionFailure(failure: EdgeFunctionFailure): TeamServiceError {
if (failure.status !== null && failure.body !== null) {
if (!failure.body.readable) {
return toTeamServiceError({ message: `Team function returned ${failure.status}` })
}
const payload = failure.body.payload
if (isRecord(payload)) {
const code = typeof payload.error === 'string' ? payload.error : ''
const message = typeof payload.message === 'string'
? payload.message
: code || `Team function returned ${failure.status}`
return toTeamServiceError({ code, message })
}
}
return toTeamServiceError(error)
return toTeamServiceError(failure.cause)
}
export async function listTeams(userId: string): Promise<TeamSummary[]> {
@ -705,10 +709,9 @@ export async function createTeamInvite(
throw new TeamServiceError('validation', 'Invite role is invalid')
}
try {
const { data, error } = await supabase.functions.invoke('team-invite', {
body: { team_id: teamId, email, role },
})
if (error !== null) throw await readFunctionFailure(error)
const result = await invokeEdgeFunction('team-invite', { team_id: teamId, email, role })
if (!result.ok) throw readFunctionFailure(result)
const data = result.data
if (!isRecord(data)) {
throw new TeamServiceError('invalid-response', 'Invite response is not an object')
}
@ -746,10 +749,9 @@ export async function cancelTeamInvite(teamId: string, inviteId: string): Promis
export async function acceptTeamInvite(tokenValue: string): Promise<TeamAcceptResult> {
const token = normalizeInviteToken(tokenValue)
try {
const { data, error } = await supabase.functions.invoke('team-accept', {
body: { token },
})
if (error !== null) throw await readFunctionFailure(error)
const result = await invokeEdgeFunction('team-accept', { token })
if (!result.ok) throw readFunctionFailure(result)
const data = result.data
if (!isRecord(data)) {
throw new TeamServiceError('invalid-response', 'Accept response is not an object')
}

View file

@ -1,11 +1,21 @@
import { clearAllActionHistories } from '../features/actions/action-service'
import { clearAllHistoryCaches } from '../features/history/history-cache'
import { deleteNativePushRegistration } from '../features/notifications/notification-native'
import { clearAllQueuedAudio } from '../features/recording/durable-processing-queue'
import {
clearAllQueuedAudio,
retainQueuedAudioOnlyForUser,
} from '../features/recording/durable-processing-queue'
import { clearEveryGenerationIdempotencyKey } from '../features/templates'
import { audioRecorder } from './audio-recorder'
import {
DISCARD_UNSYNCED_WORK,
unsyncedWorkDispositionKey,
type UnsyncedWorkDisposition,
} from './auth-transition-policy'
import { clearAllEntitlementCaches } from './entitlement-context'
import { clearAllUserPreferenceCaches } from './preferences-context'
import { stopLiveCaptureKeepingFile } from './retain-live-capture'
import { retainedAccountWork } from './retained-account-work'
export class AccountLocalDataPurgeError extends Error {
readonly code = 'account_local_data_purge_failed'
@ -16,34 +26,88 @@ export class AccountLocalDataPurgeError extends Error {
}
}
let purgeInFlight: Promise<void> | null = null
type PurgeTask = () => Promise<void>
/** Account-scoped caches: always removed, they can be rebuilt from the server. */
const ACCOUNT_CACHE_TASKS: readonly PurgeTask[] = [
clearAllHistoryCaches,
clearAllEntitlementCaches,
clearAllUserPreferenceCaches,
clearAllActionHistories,
clearEveryGenerationIdempotencyKey,
deleteNativePushRegistration,
]
/**
* Removes every account-scoped local artifact before an auth boundary opens.
* Unsynced user-owned work (the live capture and durable-queue recordings) is
* the only copy of that audio. It is deleted only for a discard boundary; on a
* retain boundary the microphone is stopped, the owner's files are kept and
* the owner is recorded so a different account can still never see them.
*/
function unsyncedWorkTasks(disposition: UnsyncedWorkDisposition): PurgeTask[] {
if (disposition.kind === 'discard') {
return [
() => audioRecorder.cancel(),
clearAllQueuedAudio,
() => retainedAccountWork.release(),
]
}
const owner = disposition.ownerUserId
return [
() => retainedAccountWork.retain(owner),
() => stopLiveCaptureKeepingFile(audioRecorder),
() => retainQueuedAudioOnlyForUser(owner),
]
}
function startTask(task: PurgeTask): Promise<void> {
try {
return Promise.resolve(task())
} catch (error) {
return Promise.reject(error)
}
}
async function runPurge(disposition: UnsyncedWorkDisposition): Promise<void> {
const results = await Promise.allSettled(
[...ACCOUNT_CACHE_TASKS, ...unsyncedWorkTasks(disposition)].map(startTask),
)
if (results.some((result) => result.status === 'rejected')) {
throw new AccountLocalDataPurgeError()
}
}
interface PurgeRun {
key: string
promise: Promise<void>
}
let purgeInFlight: PurgeRun | null = null
/**
* Removes account-scoped local artifacts before an auth boundary opens.
* All tasks are allowed to settle so an early failure cannot skip deletion of
* unrelated caches or raw audio; callers receive only a stable public code.
*
* Concurrent calls with the same disposition share one run; a call with a
* different disposition (for example a sign-out immediately replaced by
* another account) runs after the current one so its stricter cleanup is
* never swallowed by the earlier, weaker one.
*/
export function purgeAllAccountLocalData(): Promise<void> {
if (purgeInFlight !== null) return purgeInFlight
export function purgeAllAccountLocalData(
unsyncedWork: UnsyncedWorkDisposition = DISCARD_UNSYNCED_WORK,
): Promise<void> {
const key = unsyncedWorkDispositionKey(unsyncedWork)
const previous = purgeInFlight
if (previous !== null && previous.key === key) return previous.promise
const operation = (async (): Promise<void> => {
const results = await Promise.allSettled([
clearAllHistoryCaches(),
clearAllEntitlementCaches(),
clearAllUserPreferenceCaches(),
audioRecorder.cancel(),
clearAllQueuedAudio(),
clearAllActionHistories(),
clearEveryGenerationIdempotencyKey(),
deleteNativePushRegistration(),
])
if (results.some((result) => result.status === 'rejected')) {
throw new AccountLocalDataPurgeError()
}
})().finally(() => {
if (purgeInFlight === operation) purgeInFlight = null
const run = previous === null
? runPurge(unsyncedWork)
: previous.promise.catch(() => undefined).then(() => runPurge(unsyncedWork))
const promise: Promise<void> = run.finally(() => {
if (purgeInFlight?.promise === promise) purgeInFlight = null
})
purgeInFlight = operation
return operation
purgeInFlight = { key, promise }
return promise
}

View file

@ -14,6 +14,8 @@ import { supabase, isSupabaseConfigured } from './supabase'
import { completeAuthRedirect, isAuthRedirectUrl } from './auth-redirect'
import { clearAllSecureAuthStorage } from './secure-auth-storage'
import { purgeAllAccountLocalData } from './account-local-data'
import { DISCARD_UNSYNCED_WORK, planAuthTransition } from './auth-transition-policy'
import { retainedAccountWork } from './retained-account-work'
export type AuthPrivacyCleanupState = 'ready' | 'purging' | 'failed'
@ -106,12 +108,19 @@ export function AuthProvider({ children }: { children: ReactNode }): React.React
): Promise<boolean> => {
const generation = ++transitionGenerationRef.current
const previousUserId = committedSessionRef.current?.user.id ?? null
const nextUserId = transition.session?.user.id ?? null
const needsPurge = transition.forcePurge
|| privacyCleanupStateRef.current !== 'ready'
|| (previousUserId !== null && previousUserId !== nextUserId)
const plan = planAuthTransition({
previousUserId,
nextUserId: transition.session?.user.id ?? null,
retainedOwnerUserId: retainedAccountWork.current(),
forcePurge: transition.forcePurge,
cleanupPending: privacyCleanupStateRef.current !== 'ready',
explicit: transition.explicit,
})
if (!needsPurge) {
if (!plan.purge) {
// The owner of work kept by an earlier session loss is back: the queue
// hook and Record screen resume it, so the retention marker can go.
if (plan.releaseRetainedWork) void retainedAccountWork.release().catch(() => undefined)
pendingTransitionRef.current = null
commitSession(transition.session, transition.event)
updatePrivacyCleanupState('ready')
@ -122,7 +131,7 @@ export function AuthProvider({ children }: { children: ReactNode }): React.React
updatePrivacyCleanupState('purging')
try {
await purgeAllAccountLocalData()
await purgeAllAccountLocalData(plan.unsyncedWork)
if (generation !== transitionGenerationRef.current) return false
if (transition.clearSecureAuth) {
@ -130,21 +139,31 @@ export function AuthProvider({ children }: { children: ReactNode }): React.React
// A SIGNED_OUT notification can race with a direct A -> B account
// replacement. Preserve a newer different-account session that the
// auth client has already committed to secure storage.
let replacementSession: Session | null = null
try {
const { data } = await supabase.auth.getSession()
if (generation !== transitionGenerationRef.current) return false
const latestSession = data.session
const latestUserId = latestSession?.user.id ?? null
if (latestSession !== null && latestUserId !== previousUserId) {
pendingTransitionRef.current = null
commitSession(latestSession, 'SIGNED_IN')
updatePrivacyCleanupState('ready')
return true
replacementSession = latestSession
}
} catch {
// If the auth client cannot prove that a newer session exists,
// fail closed by removing the signed-out account's secure data.
}
if (replacementSession !== null) {
if (plan.unsyncedWork.kind === 'retain') {
// Work kept for the signed-out account must not stay on a
// device that another account has already taken over.
await purgeAllAccountLocalData(DISCARD_UNSYNCED_WORK)
if (generation !== transitionGenerationRef.current) return false
}
pendingTransitionRef.current = null
commitSession(replacementSession, 'SIGNED_IN')
updatePrivacyCleanupState('ready')
return true
}
}
try {
@ -228,6 +247,9 @@ export function AuthProvider({ children }: { children: ReactNode }): React.React
}
try {
// The retained-work owner must be known before the first boundary
// decides between keeping and discarding unsynced recordings.
await retainedAccountWork.load().catch(() => null)
const initialUrl = await Linking.getInitialURL()
if (initialUrl !== null) await handleUrl(initialUrl)
const { data: { session: restoredSession }, error } = await supabase.auth.getSession()

View file

@ -6,22 +6,19 @@ export const AUTH_REDIRECT_URL = 'd3ro-voice://auth-callback'
export type AuthRedirectType = 'recovery' | 'signed-in' | 'cancelled' | 'ignored'
/**
* The only auth operation a callback may drive. A session is created solely by
* a PKCE code exchange, which the auth server binds to the code verifier this
* device stored when it started the flow.
*/
type AuthRedirectOperations = {
exchangeCodeForSession: typeof supabase.auth.exchangeCodeForSession
setSession: typeof supabase.auth.setSession
}
type ParsedAuthRedirect =
| { kind: 'ignored' }
| { kind: 'cancelled' }
| { kind: 'code'; code: string; recovery: boolean; replayKey: string }
| {
kind: 'tokens'
accessToken: string
refreshToken: string
recovery: boolean
replayKey: string
}
export class AuthRedirectError extends Error {
readonly code: 'invalid_callback' | 'provider_error'
@ -46,8 +43,8 @@ function hasAuthMaterial(parsedUrl: URL): boolean {
|| fragment.has('error_description')
}
function replayFingerprint(kind: 'code' | 'tokens', secret: string): string {
return `${kind}:${bytesToHex(sha256(utf8ToBytes(secret)))}`
function replayFingerprint(secret: string): string {
return `code:${bytesToHex(sha256(utf8ToBytes(secret)))}`
}
export function isAuthRedirectUrl(url: string): boolean {
@ -89,34 +86,23 @@ function parseAuthRedirect(url: string): ParsedAuthRedirect {
throw new AuthRedirectError('provider_error')
}
const code = query.get('code')
const accessToken = fragment.get('access_token')
const refreshToken = fragment.get('refresh_token')
const hasCode = typeof code === 'string' && code.length > 0
const hasAccessToken = typeof accessToken === 'string' && accessToken.length > 0
const hasRefreshToken = typeof refreshToken === 'string' && refreshToken.length > 0
// Implicit-flow session material (#access_token/#refresh_token) is never
// accepted. The client runs every flow as PKCE, so a legitimate callback
// carries only ?code=. A bare token pair is bound to nothing this device
// started: any web page or app could open it and sign the device into
// another account (login CSRF), which would also trigger the account-switch
// purge of this user's local data.
if (fragment.has('access_token') || fragment.has('refresh_token')) {
throw new AuthRedirectError('invalid_callback')
}
if (hasCode && (hasAccessToken || hasRefreshToken)) {
throw new AuthRedirectError('invalid_callback')
}
if (hasAccessToken !== hasRefreshToken) {
throw new AuthRedirectError('invalid_callback')
}
if (hasCode) {
const code = query.get('code')
if (typeof code === 'string' && code.length > 0) {
return {
kind: 'code',
code,
recovery: query.get('type') === 'recovery',
replayKey: replayFingerprint('code', code),
}
}
if (hasAccessToken && hasRefreshToken) {
return {
kind: 'tokens',
accessToken,
refreshToken,
recovery: fragment.get('type') === 'recovery',
replayKey: replayFingerprint('tokens', `${accessToken}\u0000${refreshToken}`),
replayKey: replayFingerprint(code),
}
}
return { kind: 'ignored' }
@ -146,21 +132,12 @@ export function createAuthRedirectHandler(
if (existing !== undefined) return existing
const operation = (async (): Promise<AuthRedirectType> => {
if (parsed.kind === 'code') {
const { data, error } = await auth.exchangeCodeForSession(parsed.code)
if (error !== null) throw error
const redirectData = data as typeof data & { redirectType?: string | null }
return parsed.recovery || redirectData.redirectType === 'recovery'
? 'recovery'
: 'signed-in'
}
const { error } = await auth.setSession({
access_token: parsed.accessToken,
refresh_token: parsed.refreshToken,
})
const { data, error } = await auth.exchangeCodeForSession(parsed.code)
if (error !== null) throw error
return parsed.recovery ? 'recovery' : 'signed-in'
const redirectData = data as typeof data & { redirectType?: string | null }
return parsed.recovery || redirectData.redirectType === 'recovery'
? 'recovery'
: 'signed-in'
})()
inFlight.set(parsed.replayKey, operation)

View file

@ -0,0 +1,69 @@
/**
* Pure policy for the mobile auth privacy boundary: given who was signed in,
* who is signing in, and whose unsynced work is parked on the device, decide
* whether local account data must be purged and what happens to the user's
* unsynced recordings (durable-queue items and the live capture file).
*
* Caches are always purged when the boundary runs. Unsynced work is different:
* it is the only copy of the user's audio, and queue items are already keyed
* by their owner, so it is kept across an involuntary session loss (a revoked
* refresh token, a password change on another device, a cold boot without a
* session) and deleted only when another account takes the device or the user
* signs out on purpose.
*/
export type UnsyncedWorkDisposition =
| { readonly kind: 'discard' }
| { readonly kind: 'retain'; readonly ownerUserId: string }
export const DISCARD_UNSYNCED_WORK: UnsyncedWorkDisposition = Object.freeze({ kind: 'discard' })
export interface AuthTransitionFacts {
/** Account whose session is currently committed to the UI. */
previousUserId: string | null
/** Account the transition would commit, or null for a signed-out state. */
nextUserId: string | null
/** Account whose unsynced work was kept by an earlier session loss. */
retainedOwnerUserId: string | null
/** The caller requires the boundary even for an unchanged account. */
forcePurge: boolean
/** An earlier boundary has not completed yet. */
cleanupPending: boolean
/** The user asked to sign out (logout, account deletion, device revoke). */
explicit: boolean
}
export type AuthTransitionPlan =
| {
readonly purge: false
/** The retained work's owner is back; the retention marker can go. */
readonly releaseRetainedWork: boolean
}
| { readonly purge: true; readonly unsyncedWork: UnsyncedWorkDisposition }
export function planAuthTransition(facts: AuthTransitionFacts): AuthTransitionPlan {
const workOwner = facts.previousUserId ?? facts.retainedOwnerUserId
const accountLeaves = facts.previousUserId !== null && facts.previousUserId !== facts.nextUserId
const otherAccountArrives = facts.nextUserId !== null
&& workOwner !== null
&& workOwner !== facts.nextUserId
if (!facts.forcePurge && !facts.cleanupPending && !accountLeaves && !otherAccountArrives) {
return {
purge: false,
releaseRetainedWork: facts.retainedOwnerUserId !== null
&& facts.retainedOwnerUserId === facts.nextUserId,
}
}
const involuntarySessionLoss = facts.nextUserId === null && !facts.explicit
if (involuntarySessionLoss && workOwner !== null) {
return { purge: true, unsyncedWork: { kind: 'retain', ownerUserId: workOwner } }
}
return { purge: true, unsyncedWork: DISCARD_UNSYNCED_WORK }
}
/** Stable key so concurrent boundaries with the same disposition share one run. */
export function unsyncedWorkDispositionKey(disposition: UnsyncedWorkDisposition): string {
return disposition.kind === 'retain' ? `retain:${disposition.ownerUserId}` : 'discard'
}

View file

@ -27,7 +27,12 @@ import {
type EntitlementSnapshot,
type MobileSubscriptionTier,
} from './entitlement-context'
import { supabase } from './supabase'
import {
invokeEdgeFunction,
readEdgeFunctionHttpFailure,
type EdgeFunctionFailure,
type EdgeFunctionHttpBody,
} from './edge-functions'
import { getMobileRuntimeConfig } from './native-config'
export const GOOGLE_PLAY_SUBSCRIPTION_IDS = [
@ -256,21 +261,37 @@ export async function restoreGooglePlayPurchaseSet(
return refreshed
}
async function functionErrorCode(candidate: unknown): Promise<string> {
if (candidate && typeof candidate === 'object' && 'context' in candidate) {
const context = (candidate as { context?: unknown }).context
if (context instanceof Response) {
try {
const body = await context.clone().json() as { error?: unknown }
if (typeof body.error === 'string') return body.error
} catch {
return 'purchase_verification_failed'
}
}
const DEFAULT_VERIFICATION_ERROR = 'purchase_verification_failed'
/**
* Billing Edge Functions put the machine code in `error`. Returns null when
* the body carries no usable code so callers fall back to the Error message.
*/
function billingCodeFromBody(body: EdgeFunctionHttpBody): string | null {
if (!body.readable || body.payload === null) return DEFAULT_VERIFICATION_ERROR
const payload = body.payload
if (typeof payload === 'object') {
const code = (payload as { error?: unknown }).error
if (typeof code === 'string') return code
}
return null
}
function errorMessageOrDefault(candidate: unknown): string {
return candidate instanceof Error && candidate.message
? candidate.message
: 'purchase_verification_failed'
: DEFAULT_VERIFICATION_ERROR
}
function billingFailureCode(failure: EdgeFunctionFailure): string {
const code = failure.body === null ? null : billingCodeFromBody(failure.body)
return code ?? errorMessageOrDefault(failure.cause)
}
async function functionErrorCode(candidate: unknown): Promise<string> {
const http = await readEdgeFunctionHttpFailure(candidate)
const code = http === null ? null : billingCodeFromBody(http.body)
return code ?? errorMessageOrDefault(candidate)
}
export function BillingProvider({ children }: { children: ReactNode }): React.ReactElement {
@ -306,14 +327,13 @@ export function BillingProvider({ children }: { children: ReactNode }): React.Re
setOperation(purchase.purchaseState === 'pending' ? 'pending' : 'verifying')
setErrorCode(null)
try {
const { data, error } = await supabase.functions.invoke<IapVerifyResult>('iap-verify', {
body: {
platform: 'google_play',
productId: purchase.productId,
purchaseToken: purchase.purchaseToken,
},
const result = await invokeEdgeFunction<IapVerifyResult>('iap-verify', {
platform: 'google_play',
productId: purchase.productId,
purchaseToken: purchase.purchaseToken,
})
if (error) throw new Error(await functionErrorCode(error))
if (!result.ok) throw new Error(billingFailureCode(result))
const data = result.data
const verifiedProduct = verifiedRestorableProduct(data, purchase.productId)
const verification = data && isRecord(data.verification) ? data.verification : null
const refreshed = await entitlement.refresh()
@ -509,14 +529,13 @@ export function BillingProvider({ children }: { children: ReactNode }): React.Re
restoredPurchases,
async (restoredPurchase) => {
if (userIdRef.current !== currentUserId) throw new Error('restore_session_changed')
const { data, error } = await supabase.functions.invoke<IapVerifyResult>('iap-verify', {
body: {
platform: 'google_play',
productId: restoredPurchase.productId,
purchaseToken: restoredPurchase.purchaseToken,
},
const result = await invokeEdgeFunction<IapVerifyResult>('iap-verify', {
platform: 'google_play',
productId: restoredPurchase.productId,
purchaseToken: restoredPurchase.purchaseToken,
})
if (error) throw new Error(await functionErrorCode(error))
if (!result.ok) throw new Error(billingFailureCode(result))
const data = result.data
return data
},
async () => {

View file

@ -0,0 +1,129 @@
// src/lib/edge-functions.ts — typed transport adapter for Supabase Edge Functions.
//
// functions-js returns `{ data: null, error: FunctionsHttpError }` for every
// non-2xx response and keeps the server's JSON body only inside
// `error.context` (a fetch Response). Callers must never inspect `data` to
// find a failure code. This adapter decodes that transport detail once so
// feature services only see a discriminated result.
//
// Edge functions do not share one error-body convention (team/billing put the
// code in `error`, account-delete puts it in `code` and a human string in
// `error`), so the adapter deliberately does NOT pick a code field. It hands
// back the parsed payload and each caller keeps its own field selection.
import { supabase } from './supabase'
/** Decoded body of a non-2xx Edge Function response. */
export type EdgeFunctionHttpBody =
| { readable: true; payload: unknown }
| { readable: false }
export interface EdgeFunctionHttpFailure {
status: number
body: EdgeFunctionHttpBody
}
export interface EdgeFunctionSuccess<T> {
ok: true
data: T | null
}
export interface EdgeFunctionFailure {
ok: false
/** HTTP status when the function responded; null for fetch/relay failures. */
status: number | null
/** Parsed response body when the function responded; null otherwise. */
body: EdgeFunctionHttpBody | null
/** Untranslated transport message. For diagnostics only, never for UI. */
message: string | null
/** The original SDK error (or thrown value), for callers that map it further. */
cause: unknown
}
export type EdgeFunctionResult<T> = EdgeFunctionSuccess<T> | EdgeFunctionFailure
/** Minimal port of the Supabase client this adapter depends on. */
export interface EdgeFunctionsClient {
functions: {
invoke(
name: string,
options: { body: Record<string, unknown> },
): Promise<{ data: unknown; error: unknown }>
}
}
/** Invocation port that feature services depend on instead of the SDK. */
export type EdgeFunctionInvoker = <T = unknown>(
name: string,
body: Record<string, unknown>,
) => Promise<EdgeFunctionResult<T>>
function errorMessage(error: unknown): string | null {
if (error instanceof Error) return error.message || null
if (typeof error === 'object' && error !== null && 'message' in error) {
const message = (error as { message?: unknown }).message
return typeof message === 'string' && message ? message : null
}
return null
}
/**
* Reads the HTTP response carried by a FunctionsHttpError. Returns null when
* the error has no Response context (fetch/relay errors, thrown values).
*/
export async function readEdgeFunctionHttpFailure(
error: unknown,
): Promise<EdgeFunctionHttpFailure | null> {
if (typeof error !== 'object' || error === null || !('context' in error)) return null
const context = (error as { context?: unknown }).context
if (!(context instanceof Response)) return null
try {
const payload: unknown = await context.clone().json()
return { status: context.status, body: { readable: true, payload } }
} catch {
return { status: context.status, body: { readable: false } }
}
}
/** Converts an SDK `{ data, error }` pair (or thrown value) into a typed result. */
export async function toEdgeFunctionResult<T>(
data: unknown,
error: unknown,
): Promise<EdgeFunctionResult<T>> {
if (error === null || error === undefined) {
return { ok: true, data: (data ?? null) as T | null }
}
const http = await readEdgeFunctionHttpFailure(error)
return {
ok: false,
status: http?.status ?? null,
body: http?.body ?? null,
message: errorMessage(error),
cause: error,
}
}
export async function invokeEdgeFunction<T = unknown>(
name: string,
body: Record<string, unknown>,
client: EdgeFunctionsClient = supabase,
): Promise<EdgeFunctionResult<T>> {
let response: { data: unknown; error: unknown }
try {
response = await client.functions.invoke(name, { body })
} catch (thrown) {
return toEdgeFunctionResult<T>(null, thrown ?? new Error('edge_function_invoke_failed'))
}
return toEdgeFunctionResult<T>(response.data, response.error)
}
/** Reads a string field from a decoded failure payload, if present. */
export function edgeFailureField(
failure: Pick<EdgeFunctionFailure, 'body'>,
field: string,
): string | null {
if (failure.body === null || !failure.body.readable) return null
const payload = failure.body.payload
if (typeof payload !== 'object' || payload === null || Array.isArray(payload)) return null
const value = (payload as Record<string, unknown>)[field]
return typeof value === 'string' ? value : null
}

View file

@ -0,0 +1,26 @@
/**
* Minimal scoped logger for the React Native app. It is the single sink that
* talks to the JS console (Metro / logcat / Xcode), so feature code never
* calls `console.*` directly and the output format stays consistent.
*/
export interface Logger {
info: (message: string, ...details: unknown[]) => void;
warn: (message: string, ...details: unknown[]) => void;
error: (message: string, ...details: unknown[]) => void;
}
type LogLevel = keyof Logger;
type ConsoleSink = Pick<Console, LogLevel>;
export function createLogger(scope: string, sink: ConsoleSink = console): Logger {
const prefix = `[${scope}]`;
const write = (level: LogLevel) => (message: string, ...details: unknown[]): void => {
sink[level](`${prefix} ${message}`, ...details);
};
return {
info: write('info'),
warn: write('warn'),
error: write('error'),
};
}

View file

@ -0,0 +1,81 @@
// Production adapters for the preferences store ports (AsyncStorage + Supabase).
import AsyncStorage from '@react-native-async-storage/async-storage'
import type { SupabaseClient } from '@supabase/supabase-js'
import { supabase } from './supabase'
import {
USER_SETTINGS_COLUMNS,
type PreferencesRemote,
type PreferencesStorage,
} from './preferences-store'
const UNIQUE_VIOLATION = '23505'
export const asyncStoragePreferencesStorage: PreferencesStorage = {
multiGet: (keys) => AsyncStorage.multiGet(keys),
setItem: (key, value) => AsyncStorage.setItem(key, value),
multiSet: (pairs) => AsyncStorage.multiSet(pairs),
getAllKeys: () => AsyncStorage.getAllKeys(),
multiRemove: (keys) => AsyncStorage.multiRemove(keys),
}
export function createSupabasePreferencesRemote(
client: SupabaseClient = supabase,
): PreferencesRemote {
return {
async fetchRow(userId) {
const { data, error } = await client
.from('user_settings')
.select(USER_SETTINGS_COLUMNS)
.eq('user_id', userId)
.maybeSingle()
if (error) throw error
return data
},
async insertRow(row) {
const { data, error } = await client
.from('user_settings')
.insert(row)
.select(USER_SETTINGS_COLUMNS)
.single()
if (error?.code === UNIQUE_VIOLATION) return { status: 'duplicate' }
if (error) throw error
return { status: 'inserted', row: data }
},
async updateRowAtRevision(row, expectedRevision) {
const { data, error } = await client
.from('user_settings')
.update(row)
.eq('user_id', row.user_id)
.eq('revision', expectedRevision)
.select(USER_SETTINGS_COLUMNS)
.maybeSingle()
if (error) throw error
return data
},
}
}
/** Subscribes to UPDATEs of the user's settings row; returns an unsubscribe function. */
export function subscribeToUserSettingsChanges(
userId: string,
onRow: (row: unknown) => void,
client: SupabaseClient = supabase,
): () => void {
const channel = client
.channel(`mobile-user-settings-${userId}`)
.on(
'postgres_changes',
{
event: 'UPDATE',
schema: 'public',
table: 'user_settings',
filter: `user_id=eq.${userId}`,
},
(payload) => onRow(payload.new),
)
.subscribe()
return () => {
void client.removeChannel(channel)
}
}

View file

@ -1,4 +1,3 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import {
AppState,
useColorScheme,
@ -9,84 +8,49 @@ import {
useContext,
useEffect,
useMemo,
useRef,
useState,
useSyncExternalStore,
} from 'react'
import type { ReactNode } from 'react'
import { useAuth } from './auth-context'
import { supabase } from './supabase'
import {
asyncStoragePreferencesStorage,
createSupabasePreferencesRemote,
subscribeToUserSettingsChanges,
} from './preferences-adapters'
import {
CURRENT_ONBOARDING_VERSION,
clearUserPreferenceCaches,
createPreferencesStore,
preferencesOwnerFor,
type MobilePreferences,
type MobilePreferencesPatch,
type PreferenceMutationResult,
type PreferencesErrorCode,
type PreferencesStore,
type PreferencesSyncStatus,
} from './preferences-store'
import {
getMobileThemePalette,
isMobileThemeMode,
resolveEffectiveTheme,
type EffectiveMobileTheme,
type MobileThemeMode,
type MobileThemePalette,
} from '../theme/mobile-theme'
export const CURRENT_ONBOARDING_VERSION = 1
const CACHE_SCHEMA_VERSION = 1
const INSTALLATION_CACHE_KEY = '@d3ro/mobile/preferences/installation-v1'
const USER_CACHE_PREFIX = '@d3ro/mobile/preferences/user-v1/'
const USER_SETTINGS_COLUMNS = [
'user_id',
'theme_mode',
'locale',
'haptic_enabled',
'auto_polish_enabled',
'preferred_stt_model',
'preferred_llm_model',
'onboarding_version',
'tutorial_completed_at',
'revision',
'updated_at',
].join(',')
export type SupportedMobileLocale = 'ko' | 'en'
export type PreferencesSyncStatus =
| 'loading'
| 'local'
| 'saving'
| 'synced'
| 'offline'
| 'error'
export type PreferencesErrorCode =
| 'CACHE_READ_FAILED'
| 'CACHE_WRITE_FAILED'
| 'SYNC_FAILED'
| 'SYNC_CONFLICT'
export interface MobilePreferences {
themeMode: MobileThemeMode
locale: SupportedMobileLocale
hapticEnabled: boolean
autoPolishEnabled: boolean
preferredSttModel: string | null
preferredLlmModel: string | null
onboardingVersion: number
tutorialCompletedAt: string | null
revision: number
updatedAt: string | null
}
export type MobilePreferencesPatch = Partial<Pick<
export {
CURRENT_ONBOARDING_VERSION,
applyPreferencesPatch,
normalizePreferences,
normalizePreferencesPatch,
} from './preferences-store'
export type {
MobilePreferences,
| 'themeMode'
| 'locale'
| 'hapticEnabled'
| 'autoPolishEnabled'
| 'preferredSttModel'
| 'preferredLlmModel'
| 'onboardingVersion'
| 'tutorialCompletedAt'
>>
export interface PreferenceMutationResult {
localSaved: boolean
serverSynced: boolean
}
MobilePreferencesPatch,
PreferenceMutationResult,
PreferencesErrorCode,
PreferencesSyncStatus,
SupportedMobileLocale,
} from './preferences-store'
interface PreferencesContextValue {
preferences: MobilePreferences
@ -104,758 +68,70 @@ interface PreferencesContextValue {
retrySync: () => Promise<void>
}
interface CacheEnvelope {
schemaVersion: number
preferences: MobilePreferences
pendingPatch: MobilePreferencesPatch
lastSyncedAt: string | null
}
interface SettingsRow {
user_id: string
theme_mode: unknown
locale: unknown
haptic_enabled: unknown
auto_polish_enabled: unknown
preferred_stt_model: unknown
preferred_llm_model: unknown
onboarding_version: unknown
tutorial_completed_at: unknown
revision: unknown
updated_at: unknown
}
interface ProviderSnapshot {
ownerKey: string
preferences: MobilePreferences
loading: boolean
syncStatus: PreferencesSyncStatus
errorCode: PreferencesErrorCode | null
lastSyncedAt: string | null
}
const DEFAULT_PREFERENCES: MobilePreferences = Object.freeze({
themeMode: 'system',
locale: 'ko',
hapticEnabled: true,
autoPolishEnabled: true,
preferredSttModel: null,
preferredLlmModel: null,
onboardingVersion: 0,
tutorialCompletedAt: null,
revision: 1,
updatedAt: null,
})
const PreferencesContext = createContext<PreferencesContextValue | null>(null)
export async function clearAllUserPreferenceCaches(): Promise<void> {
const keys = await AsyncStorage.getAllKeys()
const userKeys = keys.filter((key) => key.startsWith(USER_CACHE_PREFIX))
if (userKeys.length > 0) await AsyncStorage.multiRemove(userKeys)
export function clearAllUserPreferenceCaches(): Promise<void> {
return clearUserPreferenceCaches(asyncStoragePreferencesStorage)
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function normalizeLocale(value: unknown): SupportedMobileLocale {
return value === 'en' ? 'en' : 'ko'
}
function normalizeNullableString(value: unknown): string | null {
return typeof value === 'string' && value.trim().length > 0
? value.trim()
: null
}
function normalizeNonNegativeInteger(value: unknown, fallback: number): number {
const numeric = typeof value === 'number' ? value : Number(value)
return Number.isSafeInteger(numeric) && numeric >= 0 ? numeric : fallback
}
function normalizePositiveInteger(value: unknown, fallback: number): number {
const numeric = typeof value === 'number' ? value : Number(value)
return Number.isSafeInteger(numeric) && numeric > 0 ? numeric : fallback
}
function normalizeIsoDate(value: unknown): string | null {
if (typeof value !== 'string') return null
const timestamp = Date.parse(value)
return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : null
}
export function normalizePreferences(
value: unknown,
fallback: MobilePreferences = DEFAULT_PREFERENCES,
): MobilePreferences {
if (!isRecord(value)) return { ...fallback }
return {
themeMode: isMobileThemeMode(value.themeMode) ? value.themeMode : fallback.themeMode,
locale: normalizeLocale(value.locale ?? fallback.locale),
hapticEnabled: typeof value.hapticEnabled === 'boolean'
? value.hapticEnabled
: fallback.hapticEnabled,
autoPolishEnabled: typeof value.autoPolishEnabled === 'boolean'
? value.autoPolishEnabled
: fallback.autoPolishEnabled,
preferredSttModel: value.preferredSttModel === undefined
? fallback.preferredSttModel
: normalizeNullableString(value.preferredSttModel),
preferredLlmModel: value.preferredLlmModel === undefined
? fallback.preferredLlmModel
: normalizeNullableString(value.preferredLlmModel),
onboardingVersion: normalizeNonNegativeInteger(
value.onboardingVersion,
fallback.onboardingVersion,
),
tutorialCompletedAt: value.tutorialCompletedAt === undefined
? fallback.tutorialCompletedAt
: normalizeIsoDate(value.tutorialCompletedAt),
revision: normalizePositiveInteger(value.revision, fallback.revision),
updatedAt: value.updatedAt === undefined
? fallback.updatedAt
: normalizeIsoDate(value.updatedAt),
}
}
export function normalizePreferencesPatch(value: unknown): MobilePreferencesPatch {
if (!isRecord(value)) return {}
const patch: MobilePreferencesPatch = {}
if (isMobileThemeMode(value.themeMode)) patch.themeMode = value.themeMode
if (value.locale === 'ko' || value.locale === 'en') patch.locale = value.locale
if (typeof value.hapticEnabled === 'boolean') patch.hapticEnabled = value.hapticEnabled
if (typeof value.autoPolishEnabled === 'boolean') {
patch.autoPolishEnabled = value.autoPolishEnabled
}
if (value.preferredSttModel === null || typeof value.preferredSttModel === 'string') {
patch.preferredSttModel = normalizeNullableString(value.preferredSttModel)
}
if (value.preferredLlmModel === null || typeof value.preferredLlmModel === 'string') {
patch.preferredLlmModel = normalizeNullableString(value.preferredLlmModel)
}
if (value.onboardingVersion !== undefined) {
patch.onboardingVersion = normalizeNonNegativeInteger(value.onboardingVersion, 0)
}
if (value.tutorialCompletedAt === null || typeof value.tutorialCompletedAt === 'string') {
patch.tutorialCompletedAt = normalizeIsoDate(value.tutorialCompletedAt)
}
return patch
}
export function applyPreferencesPatch(
preferences: MobilePreferences,
patch: MobilePreferencesPatch,
): MobilePreferences {
return normalizePreferences({ ...preferences, ...normalizePreferencesPatch(patch) }, preferences)
}
function parseSettingsRow(value: unknown): MobilePreferences | null {
if (!isRecord(value) || typeof value.user_id !== 'string') return null
const row = value as unknown as SettingsRow
return normalizePreferences({
themeMode: row.theme_mode,
locale: row.locale,
hapticEnabled: row.haptic_enabled,
autoPolishEnabled: row.auto_polish_enabled,
preferredSttModel: row.preferred_stt_model,
preferredLlmModel: row.preferred_llm_model,
onboardingVersion: row.onboarding_version,
tutorialCompletedAt: row.tutorial_completed_at,
revision: row.revision,
updatedAt: row.updated_at,
function createDefaultStore(): PreferencesStore {
return createPreferencesStore({
storage: asyncStoragePreferencesStorage,
remote: createSupabasePreferencesRemote(),
})
}
function parseCacheEnvelope(raw: string | null): CacheEnvelope | null {
if (raw === null) return null
const parsed: unknown = JSON.parse(raw)
if (!isRecord(parsed) || parsed.schemaVersion !== CACHE_SCHEMA_VERSION) {
throw new Error('Unsupported mobile preferences cache schema')
}
return {
schemaVersion: CACHE_SCHEMA_VERSION,
preferences: normalizePreferences(parsed.preferences),
pendingPatch: normalizePreferencesPatch(parsed.pendingPatch),
lastSyncedAt: normalizeIsoDate(parsed.lastSyncedAt),
}
}
function createEnvelope(
preferences: MobilePreferences,
pendingPatch: MobilePreferencesPatch,
lastSyncedAt: string | null,
): CacheEnvelope {
return {
schemaVersion: CACHE_SCHEMA_VERSION,
preferences,
pendingPatch: normalizePreferencesPatch(pendingPatch),
lastSyncedAt,
}
}
function toInstallationPreferences(
preferences: MobilePreferences,
): MobilePreferences {
return {
...DEFAULT_PREFERENCES,
themeMode: preferences.themeMode,
locale: preferences.locale,
hapticEnabled: preferences.hapticEnabled,
onboardingVersion: preferences.onboardingVersion,
tutorialCompletedAt: preferences.tutorialCompletedAt,
}
}
function getUserCacheKey(userId: string): string {
return `${USER_CACHE_PREFIX}${userId}`
}
function serializeEnvelope(envelope: CacheEnvelope): string {
return JSON.stringify(envelope)
}
async function writeCaches(
userId: string | null,
preferences: MobilePreferences,
userPendingPatch: MobilePreferencesPatch,
lastSyncedAt: string | null,
installationPendingPatch: MobilePreferencesPatch = {},
): Promise<void> {
const installationEnvelope = createEnvelope(
toInstallationPreferences(preferences),
installationPendingPatch,
lastSyncedAt,
)
if (userId === null) {
await AsyncStorage.setItem(
INSTALLATION_CACHE_KEY,
serializeEnvelope(installationEnvelope),
)
return
}
const userEnvelope = createEnvelope(
preferences,
userPendingPatch,
lastSyncedAt,
)
await AsyncStorage.multiSet([
[INSTALLATION_CACHE_KEY, serializeEnvelope(installationEnvelope)],
[getUserCacheKey(userId), serializeEnvelope(userEnvelope)],
])
}
function toRowMutation(
userId: string,
preferences: MobilePreferences,
): Record<string, unknown> {
return {
user_id: userId,
theme_mode: preferences.themeMode,
locale: preferences.locale,
haptic_enabled: preferences.hapticEnabled,
auto_polish_enabled: preferences.autoPolishEnabled,
preferred_stt_model: preferences.preferredSttModel,
preferred_llm_model: preferences.preferredLlmModel,
onboarding_version: preferences.onboardingVersion,
tutorial_completed_at: preferences.tutorialCompletedAt,
revision: preferences.revision,
}
}
async function fetchServerPreferences(userId: string): Promise<MobilePreferences | null> {
const { data, error } = await supabase
.from('user_settings')
.select(USER_SETTINGS_COLUMNS)
.eq('user_id', userId)
.maybeSingle()
if (error) throw error
return parseSettingsRow(data)
}
async function writeServerPreferences(
userId: string,
fallback: MobilePreferences,
patch: MobilePreferencesPatch,
): Promise<MobilePreferences> {
for (let attempt = 0; attempt < 3; attempt += 1) {
const remote = await fetchServerPreferences(userId)
if (remote === null) {
const desired = applyPreferencesPatch(fallback, patch)
const insertValue: MobilePreferences = {
...desired,
revision: Math.max(1, desired.revision),
}
const { data, error } = await supabase
.from('user_settings')
.insert(toRowMutation(userId, insertValue))
.select(USER_SETTINGS_COLUMNS)
.single()
if (error?.code === '23505') continue
if (error) throw error
const inserted = parseSettingsRow(data)
if (inserted === null) throw new Error('Invalid user settings insert response')
return inserted
}
const desired: MobilePreferences = {
...applyPreferencesPatch(remote, patch),
revision: remote.revision + 1,
}
const { data, error } = await supabase
.from('user_settings')
.update(toRowMutation(userId, desired))
.eq('user_id', userId)
.eq('revision', remote.revision)
.select(USER_SETTINGS_COLUMNS)
.maybeSingle()
if (error) throw error
const updated = parseSettingsRow(data)
if (updated !== null) return updated
}
const conflict = new Error('User settings changed repeatedly on another device')
conflict.name = 'PreferencesSyncConflictError'
throw conflict
}
function isPatchEmpty(patch: MobilePreferencesPatch): boolean {
return Object.keys(patch).length === 0
}
function removeCommittedPatch(
current: MobilePreferencesPatch,
committed: MobilePreferencesPatch,
): MobilePreferencesPatch {
const remaining: MobilePreferencesPatch = { ...current }
for (const key of Object.keys(committed) as Array<keyof MobilePreferencesPatch>) {
if (remaining[key] === committed[key]) {
delete remaining[key]
}
}
return remaining
}
function errorCodeForSync(error: unknown): PreferencesErrorCode {
return error instanceof Error && error.name === 'PreferencesSyncConflictError'
? 'SYNC_CONFLICT'
: 'SYNC_FAILED'
}
export function MobilePreferencesProvider({
children,
store: injectedStore,
}: {
children: ReactNode
/** Test seam: defaults to the AsyncStorage + Supabase backed store. */
store?: PreferencesStore
}): React.ReactElement {
const { user } = useAuth()
const systemScheme = useColorScheme()
const userId = user?.id ?? null
const ownerKey = userId === null ? 'installation' : `user:${userId}`
const ownerKey = preferencesOwnerFor(userId).ownerKey
const [snapshot, setSnapshot] = useState<ProviderSnapshot>({
ownerKey: 'uninitialized',
preferences: { ...DEFAULT_PREFERENCES },
loading: true,
syncStatus: 'loading',
errorCode: null,
lastSyncedAt: null,
})
const ownerKeyRef = useRef(ownerKey)
const userIdRef = useRef<string | null>(userId)
const preferencesRef = useRef<MobilePreferences>({ ...DEFAULT_PREFERENCES })
const pendingPatchRef = useRef<MobilePreferencesPatch>({})
const lastSyncedAtRef = useRef<string | null>(null)
const loadGenerationRef = useRef(0)
const serverQueueRef = useRef<Promise<void>>(Promise.resolve())
const localQueueRef = useRef<Promise<void>>(Promise.resolve())
ownerKeyRef.current = ownerKey
userIdRef.current = userId
const flushPendingPatch = useCallback(async (targetUserId: string): Promise<boolean> => {
if (userIdRef.current !== targetUserId) return false
const committedPatch = { ...pendingPatchRef.current }
if (isPatchEmpty(committedPatch)) return true
setSnapshot((current) => current.ownerKey === `user:${targetUserId}`
? { ...current, syncStatus: 'saving', errorCode: null }
: current)
try {
const saved = await writeServerPreferences(
targetUserId,
preferencesRef.current,
committedPatch,
)
if (userIdRef.current !== targetUserId) return false
const remainingPatch = removeCommittedPatch(
pendingPatchRef.current,
committedPatch,
)
const nextPreferences = applyPreferencesPatch(saved, remainingPatch)
const syncedAt = new Date().toISOString()
preferencesRef.current = nextPreferences
pendingPatchRef.current = remainingPatch
lastSyncedAtRef.current = syncedAt
try {
await writeCaches(
targetUserId,
nextPreferences,
remainingPatch,
syncedAt,
)
setSnapshot({
ownerKey: `user:${targetUserId}`,
preferences: nextPreferences,
loading: false,
syncStatus: isPatchEmpty(remainingPatch) ? 'synced' : 'saving',
errorCode: null,
lastSyncedAt: syncedAt,
})
} catch {
setSnapshot({
ownerKey: `user:${targetUserId}`,
preferences: nextPreferences,
loading: false,
syncStatus: 'error',
errorCode: 'CACHE_WRITE_FAILED',
lastSyncedAt: syncedAt,
})
}
return isPatchEmpty(remainingPatch)
} catch (error) {
if (userIdRef.current !== targetUserId) return false
setSnapshot((current) => current.ownerKey === `user:${targetUserId}`
? {
...current,
loading: false,
syncStatus: 'offline',
errorCode: errorCodeForSync(error),
}
: current)
return false
}
}, [])
const queueServerFlush = useCallback((targetUserId: string): Promise<boolean> => {
const run = serverQueueRef.current.then(
() => flushPendingPatch(targetUserId),
() => flushPendingPatch(targetUserId),
)
serverQueueRef.current = run.then(() => undefined, () => undefined)
return run
}, [flushPendingPatch])
const loadPreferences = useCallback(async (
targetOwnerKey: string,
targetUserId: string | null,
): Promise<void> => {
const generation = ++loadGenerationRef.current
setSnapshot((current) => ({
...current,
ownerKey: targetOwnerKey,
loading: true,
syncStatus: 'loading',
errorCode: null,
}))
let installationEnvelope: CacheEnvelope | null = null
let userEnvelope: CacheEnvelope | null = null
let cacheReadFailed = false
let cacheWriteFailed = false
try {
const keys = targetUserId === null
? [INSTALLATION_CACHE_KEY]
: [INSTALLATION_CACHE_KEY, getUserCacheKey(targetUserId)]
const entries = await AsyncStorage.multiGet(keys)
installationEnvelope = parseCacheEnvelope(entries[0]?.[1] ?? null)
if (targetUserId !== null) {
userEnvelope = parseCacheEnvelope(entries[1]?.[1] ?? null)
}
} catch {
cacheReadFailed = true
}
if (generation !== loadGenerationRef.current || ownerKeyRef.current !== targetOwnerKey) {
return
}
if (targetUserId === null) {
const localPreferences = installationEnvelope?.preferences ?? { ...DEFAULT_PREFERENCES }
const localPending = installationEnvelope?.pendingPatch ?? {}
preferencesRef.current = localPreferences
pendingPatchRef.current = localPending
lastSyncedAtRef.current = installationEnvelope?.lastSyncedAt ?? null
setSnapshot({
ownerKey: targetOwnerKey,
preferences: localPreferences,
loading: false,
syncStatus: cacheReadFailed ? 'error' : 'local',
errorCode: cacheReadFailed ? 'CACHE_READ_FAILED' : null,
lastSyncedAt: installationEnvelope?.lastSyncedAt ?? null,
})
return
}
const cachedPreferences = userEnvelope?.preferences
?? installationEnvelope?.preferences
?? { ...DEFAULT_PREFERENCES }
const stagedPatch: MobilePreferencesPatch = {
...(userEnvelope?.pendingPatch ?? {}),
...(installationEnvelope?.pendingPatch ?? {}),
}
const cachedWithPending = applyPreferencesPatch(cachedPreferences, stagedPatch)
preferencesRef.current = cachedWithPending
pendingPatchRef.current = stagedPatch
lastSyncedAtRef.current = userEnvelope?.lastSyncedAt
?? installationEnvelope?.lastSyncedAt
?? null
setSnapshot({
ownerKey: targetOwnerKey,
preferences: cachedWithPending,
loading: false,
syncStatus: 'loading',
errorCode: cacheReadFailed ? 'CACHE_READ_FAILED' : null,
lastSyncedAt: userEnvelope?.lastSyncedAt ?? installationEnvelope?.lastSyncedAt ?? null,
})
try {
await writeCaches(
targetUserId,
cachedWithPending,
stagedPatch,
userEnvelope?.lastSyncedAt ?? null,
)
} catch {
cacheWriteFailed = true
}
let resolved: MobilePreferences
try {
const remote = await fetchServerPreferences(targetUserId)
if (remote === null || !isPatchEmpty(stagedPatch)) {
resolved = await writeServerPreferences(
targetUserId,
remote ?? cachedWithPending,
stagedPatch,
)
} else {
resolved = remote
}
if (generation !== loadGenerationRef.current || ownerKeyRef.current !== targetOwnerKey) {
return
}
} catch (error) {
if (generation !== loadGenerationRef.current || ownerKeyRef.current !== targetOwnerKey) {
return
}
setSnapshot({
ownerKey: targetOwnerKey,
preferences: cachedWithPending,
loading: false,
syncStatus: 'offline',
errorCode: cacheReadFailed
? 'CACHE_READ_FAILED'
: cacheWriteFailed
? 'CACHE_WRITE_FAILED'
: errorCodeForSync(error),
lastSyncedAt: userEnvelope?.lastSyncedAt ?? installationEnvelope?.lastSyncedAt ?? null,
})
return
}
if (generation !== loadGenerationRef.current || ownerKeyRef.current !== targetOwnerKey) {
return
}
const syncedAt = new Date().toISOString()
try {
await writeCaches(targetUserId, resolved, {}, syncedAt)
} catch {
cacheWriteFailed = true
}
preferencesRef.current = resolved
pendingPatchRef.current = {}
lastSyncedAtRef.current = syncedAt
setSnapshot({
ownerKey: targetOwnerKey,
preferences: resolved,
loading: false,
syncStatus: cacheReadFailed || cacheWriteFailed ? 'error' : 'synced',
errorCode: cacheReadFailed
? 'CACHE_READ_FAILED'
: cacheWriteFailed
? 'CACHE_WRITE_FAILED'
: null,
lastSyncedAt: syncedAt,
})
}, [])
const [store] = useState<PreferencesStore>(() => injectedStore ?? createDefaultStore())
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot)
// Owner change → first load (the store alone decides initial vs background).
useEffect(() => {
void loadPreferences(ownerKey, userId)
}, [loadPreferences, ownerKey, userId])
void store.load(preferencesOwnerFor(userId))
}, [store, userId])
// Realtime rows from other devices.
useEffect(() => {
if (userId === null) return undefined
return subscribeToUserSettingsChanges(userId, (row) => {
store.applyRemote(userId, row)
})
}, [store, userId])
const channel = supabase
.channel(`mobile-user-settings-${userId}`)
.on(
'postgres_changes',
{
event: 'UPDATE',
schema: 'public',
table: 'user_settings',
filter: `user_id=eq.${userId}`,
},
(payload) => {
if (userIdRef.current !== userId || !isPatchEmpty(pendingPatchRef.current)) return
const remote = parseSettingsRow(payload.new)
if (remote === null) return
// Foreground → background resync; never re-enters the initial-loading phase.
useEffect(() => {
const subscription = AppState.addEventListener('change', (state) => {
if (state !== 'active' || !store.needsForegroundResync()) return
void store.resync()
})
return () => subscription.remove()
}, [store])
const syncedAt = new Date().toISOString()
preferencesRef.current = remote
lastSyncedAtRef.current = syncedAt
setSnapshot({
ownerKey: `user:${userId}`,
preferences: remote,
loading: false,
syncStatus: 'synced',
errorCode: null,
lastSyncedAt: syncedAt,
})
void writeCaches(userId, remote, {}, syncedAt).catch(() => {
if (userIdRef.current !== userId) return
setSnapshot((current) => ({
...current,
syncStatus: 'error',
errorCode: 'CACHE_WRITE_FAILED',
}))
})
},
)
.subscribe()
return () => {
void supabase.removeChannel(channel)
}
}, [userId])
const commitPatch = useCallback(async (
requestedPatch: MobilePreferencesPatch,
): Promise<PreferenceMutationResult> => {
const targetOwnerKey = ownerKeyRef.current
const targetUserId = userIdRef.current
const patch = normalizePreferencesPatch(requestedPatch)
if (isPatchEmpty(patch)) {
return {
localSaved: true,
serverSynced: targetUserId === null || isPatchEmpty(pendingPatchRef.current),
}
}
const nextPreferences = applyPreferencesPatch(preferencesRef.current, patch)
const nextPendingPatch = {
...pendingPatchRef.current,
...patch,
}
try {
await writeCaches(
targetUserId,
nextPreferences,
nextPendingPatch,
lastSyncedAtRef.current,
targetUserId === null ? nextPendingPatch : {},
)
} catch {
if (ownerKeyRef.current === targetOwnerKey) {
setSnapshot((current) => ({
...current,
syncStatus: 'error',
errorCode: 'CACHE_WRITE_FAILED',
}))
}
return { localSaved: false, serverSynced: false }
}
if (ownerKeyRef.current !== targetOwnerKey) {
return { localSaved: true, serverSynced: false }
}
preferencesRef.current = nextPreferences
pendingPatchRef.current = nextPendingPatch
setSnapshot((current) => ({
...current,
ownerKey: targetOwnerKey,
preferences: nextPreferences,
loading: false,
syncStatus: targetUserId === null ? 'local' : 'saving',
errorCode: null,
}))
if (targetUserId === null) {
return { localSaved: true, serverSynced: false }
}
const serverSynced = await queueServerFlush(targetUserId)
return { localSaved: true, serverSynced }
}, [queueServerFlush])
const updatePreferences = useCallback((
patch: MobilePreferencesPatch,
): Promise<PreferenceMutationResult> => {
const run = localQueueRef.current.then(
() => commitPatch(patch),
() => commitPatch(patch),
)
localQueueRef.current = run.then(() => undefined, () => undefined)
return run
}, [commitPatch])
const updatePreferences = useCallback(
(patch: MobilePreferencesPatch): Promise<PreferenceMutationResult> => store.commit(patch),
[store],
)
const completeOnboarding = useCallback((
outcome: 'completed' | 'skipped',
): Promise<PreferenceMutationResult> => updatePreferences({
): Promise<PreferenceMutationResult> => store.commit({
onboardingVersion: CURRENT_ONBOARDING_VERSION,
tutorialCompletedAt: outcome === 'completed' ? new Date().toISOString() : null,
}), [updatePreferences])
}), [store])
const retrySync = useCallback(async (): Promise<void> => {
const targetOwnerKey = ownerKeyRef.current
const targetUserId = userIdRef.current
if (targetUserId !== null && !isPatchEmpty(pendingPatchRef.current)) {
await queueServerFlush(targetUserId)
return
}
await loadPreferences(targetOwnerKey, targetUserId)
}, [loadPreferences, queueServerFlush])
useEffect(() => {
const subscription = AppState.addEventListener('change', (state) => {
if (state !== 'active' || userIdRef.current === null) return
if (isPatchEmpty(pendingPatchRef.current) && snapshot.syncStatus !== 'offline') return
void retrySync()
})
return () => subscription.remove()
}, [retrySync, snapshot.syncStatus])
const retrySync = useCallback((): Promise<void> => store.resync(), [store])
const effectiveTheme = resolveEffectiveTheme(
snapshot.preferences.themeMode,
@ -865,7 +141,7 @@ export function MobilePreferencesProvider({
() => getMobileThemePalette(effectiveTheme),
[effectiveTheme],
)
const loading = snapshot.loading || snapshot.ownerKey !== ownerKey
const loading = snapshot.phase === 'initial-loading' || snapshot.ownerKey !== ownerKey
const contextValue = useMemo<PreferencesContextValue>(() => ({
preferences: snapshot.preferences,

View file

@ -0,0 +1,898 @@
// Mobile preferences store — framework-free sync core.
//
// Owns the cache envelope codec, pending-patch bookkeeping, the revision-checked
// server writer and the owner/generation guards. IO is injected through the
// PreferencesStorage and PreferencesRemote ports so the whole sync flow can be
// unit-tested without React, AsyncStorage or Supabase. The React provider only
// subscribes to snapshots and forwards triggers (owner change, foreground,
// realtime rows, Settings retry).
import { isMobileThemeMode, type MobileThemeMode } from '../theme/mobile-theme'
export const CURRENT_ONBOARDING_VERSION = 1
const CACHE_SCHEMA_VERSION = 1
export const INSTALLATION_CACHE_KEY = '@d3ro/mobile/preferences/installation-v1'
export const USER_CACHE_PREFIX = '@d3ro/mobile/preferences/user-v1/'
export const USER_SETTINGS_COLUMNS = [
'user_id',
'theme_mode',
'locale',
'haptic_enabled',
'auto_polish_enabled',
'preferred_stt_model',
'preferred_llm_model',
'onboarding_version',
'tutorial_completed_at',
'revision',
'updated_at',
].join(',')
const MAX_WRITE_ATTEMPTS = 3
export type SupportedMobileLocale = 'ko' | 'en'
export type PreferencesSyncStatus =
| 'loading'
| 'local'
| 'saving'
| 'synced'
| 'offline'
| 'error'
export type PreferencesErrorCode =
| 'CACHE_READ_FAILED'
| 'CACHE_WRITE_FAILED'
| 'SYNC_FAILED'
| 'SYNC_CONFLICT'
export interface MobilePreferences {
themeMode: MobileThemeMode
locale: SupportedMobileLocale
hapticEnabled: boolean
autoPolishEnabled: boolean
preferredSttModel: string | null
preferredLlmModel: string | null
onboardingVersion: number
tutorialCompletedAt: string | null
revision: number
updatedAt: string | null
}
export type MobilePreferencesPatch = Partial<Pick<
MobilePreferences,
| 'themeMode'
| 'locale'
| 'hapticEnabled'
| 'autoPolishEnabled'
| 'preferredSttModel'
| 'preferredLlmModel'
| 'onboardingVersion'
| 'tutorialCompletedAt'
>>
export interface PreferenceMutationResult {
localSaved: boolean
serverSynced: boolean
}
/**
* 'initial-loading' is entered only when the store starts serving a new owner.
* Background resyncs of an already-loaded owner keep the phase at 'ready' so
* the app tree that depends on preferences never unmounts.
*/
export type PreferencesPhase = 'initial-loading' | 'ready'
export interface PreferencesOwner {
ownerKey: string
userId: string | null
}
export interface PreferencesStoreSnapshot {
readonly ownerKey: string
readonly phase: PreferencesPhase
readonly preferences: MobilePreferences
readonly syncStatus: PreferencesSyncStatus
readonly errorCode: PreferencesErrorCode | null
readonly lastSyncedAt: string | null
}
export type UserSettingsRowMutation = {
user_id: string
theme_mode: MobileThemeMode
locale: SupportedMobileLocale
haptic_enabled: boolean
auto_polish_enabled: boolean
preferred_stt_model: string | null
preferred_llm_model: string | null
onboarding_version: number
tutorial_completed_at: string | null
revision: number
}
/** Local key-value persistence port (AsyncStorage in production). */
export interface PreferencesStorage {
multiGet(keys: readonly string[]): Promise<ReadonlyArray<readonly [string, string | null]>>
setItem(key: string, value: string): Promise<void>
multiSet(pairs: Array<[string, string]>): Promise<void>
getAllKeys(): Promise<readonly string[]>
multiRemove(keys: readonly string[]): Promise<void>
}
export type RemoteInsertResult =
| { status: 'inserted', row: unknown }
| { status: 'duplicate' }
/** user_settings persistence port (Supabase in production). Every method throws on failure. */
export interface PreferencesRemote {
/** Returns the raw row, or null when the user has no settings row yet. */
fetchRow(userId: string): Promise<unknown>
/** Inserts a row; a unique-violation race is reported as 'duplicate'. */
insertRow(row: UserSettingsRowMutation): Promise<RemoteInsertResult>
/** Updates only when the stored revision still equals expectedRevision; null otherwise. */
updateRowAtRevision(row: UserSettingsRowMutation, expectedRevision: number): Promise<unknown>
}
export interface PreferencesStoreDeps {
storage: PreferencesStorage
remote: PreferencesRemote
now?: () => string
}
export interface PreferencesStore {
getSnapshot(): PreferencesStoreSnapshot
subscribe(listener: () => void): () => void
/** Switches to (or re-reads) an owner. Only an owner change enters 'initial-loading'. */
load(owner: PreferencesOwner): Promise<void>
/** Background resync of the current owner. Never re-enters 'initial-loading'. */
resync(): Promise<void>
/** Whether returning to the foreground should trigger resync(). */
needsForegroundResync(): boolean
/** Serialized local-first commit followed by a server flush for signed-in owners. */
commit(patch: MobilePreferencesPatch): Promise<PreferenceMutationResult>
/** Applies a realtime user_settings row unless local edits are still pending. */
applyRemote(userId: string, row: unknown): void
}
interface CacheEnvelope {
schemaVersion: number
preferences: MobilePreferences
pendingPatch: MobilePreferencesPatch
lastSyncedAt: string | null
}
interface SettingsRow {
user_id: string
theme_mode: unknown
locale: unknown
haptic_enabled: unknown
auto_polish_enabled: unknown
preferred_stt_model: unknown
preferred_llm_model: unknown
onboarding_version: unknown
tutorial_completed_at: unknown
revision: unknown
updated_at: unknown
}
export const DEFAULT_PREFERENCES: MobilePreferences = Object.freeze({
themeMode: 'system',
locale: 'ko',
hapticEnabled: true,
autoPolishEnabled: true,
preferredSttModel: null,
preferredLlmModel: null,
onboardingVersion: 0,
tutorialCompletedAt: null,
revision: 1,
updatedAt: null,
})
// ── Pure helpers ─────────────────────────────────────────────────────────────
export function preferencesOwnerFor(userId: string | null): PreferencesOwner {
return {
ownerKey: userId === null ? 'installation' : `user:${userId}`,
userId,
}
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function normalizeLocale(value: unknown): SupportedMobileLocale {
return value === 'en' ? 'en' : 'ko'
}
function normalizeNullableString(value: unknown): string | null {
return typeof value === 'string' && value.trim().length > 0
? value.trim()
: null
}
function normalizeNonNegativeInteger(value: unknown, fallback: number): number {
const numeric = typeof value === 'number' ? value : Number(value)
return Number.isSafeInteger(numeric) && numeric >= 0 ? numeric : fallback
}
function normalizePositiveInteger(value: unknown, fallback: number): number {
const numeric = typeof value === 'number' ? value : Number(value)
return Number.isSafeInteger(numeric) && numeric > 0 ? numeric : fallback
}
function normalizeIsoDate(value: unknown): string | null {
if (typeof value !== 'string') return null
const timestamp = Date.parse(value)
return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : null
}
export function normalizePreferences(
value: unknown,
fallback: MobilePreferences = DEFAULT_PREFERENCES,
): MobilePreferences {
if (!isRecord(value)) return { ...fallback }
return {
themeMode: isMobileThemeMode(value.themeMode) ? value.themeMode : fallback.themeMode,
locale: normalizeLocale(value.locale ?? fallback.locale),
hapticEnabled: typeof value.hapticEnabled === 'boolean'
? value.hapticEnabled
: fallback.hapticEnabled,
autoPolishEnabled: typeof value.autoPolishEnabled === 'boolean'
? value.autoPolishEnabled
: fallback.autoPolishEnabled,
preferredSttModel: value.preferredSttModel === undefined
? fallback.preferredSttModel
: normalizeNullableString(value.preferredSttModel),
preferredLlmModel: value.preferredLlmModel === undefined
? fallback.preferredLlmModel
: normalizeNullableString(value.preferredLlmModel),
onboardingVersion: normalizeNonNegativeInteger(
value.onboardingVersion,
fallback.onboardingVersion,
),
tutorialCompletedAt: value.tutorialCompletedAt === undefined
? fallback.tutorialCompletedAt
: normalizeIsoDate(value.tutorialCompletedAt),
revision: normalizePositiveInteger(value.revision, fallback.revision),
updatedAt: value.updatedAt === undefined
? fallback.updatedAt
: normalizeIsoDate(value.updatedAt),
}
}
export function normalizePreferencesPatch(value: unknown): MobilePreferencesPatch {
if (!isRecord(value)) return {}
const patch: MobilePreferencesPatch = {}
if (isMobileThemeMode(value.themeMode)) patch.themeMode = value.themeMode
if (value.locale === 'ko' || value.locale === 'en') patch.locale = value.locale
if (typeof value.hapticEnabled === 'boolean') patch.hapticEnabled = value.hapticEnabled
if (typeof value.autoPolishEnabled === 'boolean') {
patch.autoPolishEnabled = value.autoPolishEnabled
}
if (value.preferredSttModel === null || typeof value.preferredSttModel === 'string') {
patch.preferredSttModel = normalizeNullableString(value.preferredSttModel)
}
if (value.preferredLlmModel === null || typeof value.preferredLlmModel === 'string') {
patch.preferredLlmModel = normalizeNullableString(value.preferredLlmModel)
}
if (value.onboardingVersion !== undefined) {
patch.onboardingVersion = normalizeNonNegativeInteger(value.onboardingVersion, 0)
}
if (value.tutorialCompletedAt === null || typeof value.tutorialCompletedAt === 'string') {
patch.tutorialCompletedAt = normalizeIsoDate(value.tutorialCompletedAt)
}
return patch
}
export function applyPreferencesPatch(
preferences: MobilePreferences,
patch: MobilePreferencesPatch,
): MobilePreferences {
return normalizePreferences({ ...preferences, ...normalizePreferencesPatch(patch) }, preferences)
}
export function parseSettingsRow(value: unknown): MobilePreferences | null {
if (!isRecord(value) || typeof value.user_id !== 'string') return null
const row = value as unknown as SettingsRow
return normalizePreferences({
themeMode: row.theme_mode,
locale: row.locale,
hapticEnabled: row.haptic_enabled,
autoPolishEnabled: row.auto_polish_enabled,
preferredSttModel: row.preferred_stt_model,
preferredLlmModel: row.preferred_llm_model,
onboardingVersion: row.onboarding_version,
tutorialCompletedAt: row.tutorial_completed_at,
revision: row.revision,
updatedAt: row.updated_at,
})
}
function parseCacheEnvelope(raw: string | null): CacheEnvelope | null {
if (raw === null) return null
const parsed: unknown = JSON.parse(raw)
if (!isRecord(parsed) || parsed.schemaVersion !== CACHE_SCHEMA_VERSION) {
throw new Error('Unsupported mobile preferences cache schema')
}
return {
schemaVersion: CACHE_SCHEMA_VERSION,
preferences: normalizePreferences(parsed.preferences),
pendingPatch: normalizePreferencesPatch(parsed.pendingPatch),
lastSyncedAt: normalizeIsoDate(parsed.lastSyncedAt),
}
}
function serializeEnvelope(
preferences: MobilePreferences,
pendingPatch: MobilePreferencesPatch,
lastSyncedAt: string | null,
): string {
const envelope: CacheEnvelope = {
schemaVersion: CACHE_SCHEMA_VERSION,
preferences,
pendingPatch: normalizePreferencesPatch(pendingPatch),
lastSyncedAt,
}
return JSON.stringify(envelope)
}
function toInstallationPreferences(
preferences: MobilePreferences,
): MobilePreferences {
return {
...DEFAULT_PREFERENCES,
themeMode: preferences.themeMode,
locale: preferences.locale,
hapticEnabled: preferences.hapticEnabled,
onboardingVersion: preferences.onboardingVersion,
tutorialCompletedAt: preferences.tutorialCompletedAt,
}
}
export function getUserCacheKey(userId: string): string {
return `${USER_CACHE_PREFIX}${userId}`
}
export function toRowMutation(
userId: string,
preferences: MobilePreferences,
): UserSettingsRowMutation {
return {
user_id: userId,
theme_mode: preferences.themeMode,
locale: preferences.locale,
haptic_enabled: preferences.hapticEnabled,
auto_polish_enabled: preferences.autoPolishEnabled,
preferred_stt_model: preferences.preferredSttModel,
preferred_llm_model: preferences.preferredLlmModel,
onboarding_version: preferences.onboardingVersion,
tutorial_completed_at: preferences.tutorialCompletedAt,
revision: preferences.revision,
}
}
export function isPatchEmpty(patch: MobilePreferencesPatch): boolean {
return Object.keys(patch).length === 0
}
export function removeCommittedPatch(
current: MobilePreferencesPatch,
committed: MobilePreferencesPatch,
): MobilePreferencesPatch {
const remaining: MobilePreferencesPatch = { ...current }
for (const key of Object.keys(committed) as Array<keyof MobilePreferencesPatch>) {
if (remaining[key] === committed[key]) {
delete remaining[key]
}
}
return remaining
}
export function errorCodeForSync(error: unknown): PreferencesErrorCode {
return error instanceof Error && error.name === 'PreferencesSyncConflictError'
? 'SYNC_CONFLICT'
: 'SYNC_FAILED'
}
/** Cache failures outrank sync failures: CACHE_READ_FAILED > CACHE_WRITE_FAILED > sync. */
function resolveErrorCode(
cacheReadFailed: boolean,
cacheWriteFailed: boolean,
syncError: PreferencesErrorCode | null,
): PreferencesErrorCode | null {
if (cacheReadFailed) return 'CACHE_READ_FAILED'
if (cacheWriteFailed) return 'CACHE_WRITE_FAILED'
return syncError
}
// ── IO helpers over the ports ────────────────────────────────────────────────
export async function clearUserPreferenceCaches(storage: PreferencesStorage): Promise<void> {
const keys = await storage.getAllKeys()
const userKeys = keys.filter((key) => key.startsWith(USER_CACHE_PREFIX))
if (userKeys.length > 0) await storage.multiRemove(userKeys)
}
async function writeCaches(
storage: PreferencesStorage,
userId: string | null,
preferences: MobilePreferences,
userPendingPatch: MobilePreferencesPatch,
lastSyncedAt: string | null,
installationPendingPatch: MobilePreferencesPatch = {},
): Promise<void> {
const installationValue = serializeEnvelope(
toInstallationPreferences(preferences),
installationPendingPatch,
lastSyncedAt,
)
if (userId === null) {
await storage.setItem(INSTALLATION_CACHE_KEY, installationValue)
return
}
await storage.multiSet([
[INSTALLATION_CACHE_KEY, installationValue],
[getUserCacheKey(userId), serializeEnvelope(preferences, userPendingPatch, lastSyncedAt)],
])
}
async function fetchServerPreferences(
remote: PreferencesRemote,
userId: string,
): Promise<MobilePreferences | null> {
return parseSettingsRow(await remote.fetchRow(userId))
}
/**
* Revision-checked write: re-reads the row, applies the patch on top and
* updates only if nobody else bumped the revision in between. Retries a few
* times, then reports a PreferencesSyncConflictError.
*/
export async function writeServerPreferences(
remote: PreferencesRemote,
userId: string,
fallback: MobilePreferences,
patch: MobilePreferencesPatch,
): Promise<MobilePreferences> {
for (let attempt = 0; attempt < MAX_WRITE_ATTEMPTS; attempt += 1) {
const current = await fetchServerPreferences(remote, userId)
if (current === null) {
const desired = applyPreferencesPatch(fallback, patch)
const insertValue: MobilePreferences = {
...desired,
revision: Math.max(1, desired.revision),
}
const result = await remote.insertRow(toRowMutation(userId, insertValue))
if (result.status === 'duplicate') continue
const inserted = parseSettingsRow(result.row)
if (inserted === null) throw new Error('Invalid user settings insert response')
return inserted
}
const desired: MobilePreferences = {
...applyPreferencesPatch(current, patch),
revision: current.revision + 1,
}
const updated = parseSettingsRow(
await remote.updateRowAtRevision(toRowMutation(userId, desired), current.revision),
)
if (updated !== null) return updated
}
const conflict = new Error('User settings changed repeatedly on another device')
conflict.name = 'PreferencesSyncConflictError'
throw conflict
}
interface CacheReadResult {
installation: CacheEnvelope | null
user: CacheEnvelope | null
failed: boolean
}
async function readCaches(
storage: PreferencesStorage,
userId: string | null,
): Promise<CacheReadResult> {
try {
const keys = userId === null
? [INSTALLATION_CACHE_KEY]
: [INSTALLATION_CACHE_KEY, getUserCacheKey(userId)]
const entries = await storage.multiGet(keys)
return {
installation: parseCacheEnvelope(entries[0]?.[1] ?? null),
user: userId === null ? null : parseCacheEnvelope(entries[1]?.[1] ?? null),
failed: false,
}
} catch {
return { installation: null, user: null, failed: true }
}
}
// ── Store ────────────────────────────────────────────────────────────────────
type RefreshMode = 'initial' | 'background'
export function createPreferencesStore(deps: PreferencesStoreDeps): PreferencesStore {
const { storage, remote } = deps
const now = deps.now ?? (() => new Date().toISOString())
let owner: PreferencesOwner = { ownerKey: 'uninitialized', userId: null }
let snapshot: PreferencesStoreSnapshot = Object.freeze({
ownerKey: 'uninitialized',
phase: 'initial-loading',
preferences: { ...DEFAULT_PREFERENCES },
syncStatus: 'loading',
errorCode: null,
lastSyncedAt: null,
})
let preferences: MobilePreferences = { ...DEFAULT_PREFERENCES }
let pendingPatch: MobilePreferencesPatch = {}
let lastSyncedAt: string | null = null
let loadGeneration = 0
let serverQueue: Promise<void> = Promise.resolve()
let localQueue: Promise<void> = Promise.resolve()
let resyncInFlight: Promise<void> | null = null
const listeners = new Set<() => void>()
function setSnapshot(
next:
| PreferencesStoreSnapshot
| ((current: PreferencesStoreSnapshot) => PreferencesStoreSnapshot),
): void {
const resolved = typeof next === 'function' ? next(snapshot) : next
if (resolved === snapshot) return
snapshot = Object.freeze({ ...resolved })
for (const listener of [...listeners]) listener()
}
function isCurrentLoad(generation: number, ownerKey: string): boolean {
return generation === loadGeneration && owner.ownerKey === ownerKey
}
function enqueue<T>(
queue: 'server' | 'local',
task: () => Promise<T>,
): Promise<T> {
const tail = queue === 'server' ? serverQueue : localQueue
const run = tail.then(task, task)
const settled = run.then(() => undefined, () => undefined)
if (queue === 'server') serverQueue = settled
else localQueue = settled
return run
}
async function flushPendingPatch(targetUserId: string): Promise<boolean> {
if (owner.userId !== targetUserId) return false
const committedPatch = { ...pendingPatch }
if (isPatchEmpty(committedPatch)) return true
const targetOwnerKey = preferencesOwnerFor(targetUserId).ownerKey
setSnapshot((current) => current.ownerKey === targetOwnerKey
? { ...current, syncStatus: 'saving', errorCode: null }
: current)
try {
const saved = await writeServerPreferences(
remote,
targetUserId,
preferences,
committedPatch,
)
if (owner.userId !== targetUserId) return false
const remainingPatch = removeCommittedPatch(pendingPatch, committedPatch)
const nextPreferences = applyPreferencesPatch(saved, remainingPatch)
const syncedAt = now()
preferences = nextPreferences
pendingPatch = remainingPatch
lastSyncedAt = syncedAt
try {
await writeCaches(storage, targetUserId, nextPreferences, remainingPatch, syncedAt)
setSnapshot({
ownerKey: targetOwnerKey,
phase: 'ready',
preferences: nextPreferences,
syncStatus: isPatchEmpty(remainingPatch) ? 'synced' : 'saving',
errorCode: null,
lastSyncedAt: syncedAt,
})
} catch {
setSnapshot({
ownerKey: targetOwnerKey,
phase: 'ready',
preferences: nextPreferences,
syncStatus: 'error',
errorCode: 'CACHE_WRITE_FAILED',
lastSyncedAt: syncedAt,
})
}
return isPatchEmpty(remainingPatch)
} catch (error) {
if (owner.userId !== targetUserId) return false
setSnapshot((current) => current.ownerKey === targetOwnerKey
? {
...current,
phase: 'ready',
syncStatus: 'offline',
errorCode: errorCodeForSync(error),
}
: current)
return false
}
}
function queueServerFlush(targetUserId: string): Promise<boolean> {
return enqueue('server', () => flushPendingPatch(targetUserId))
}
async function refresh(target: PreferencesOwner, mode: RefreshMode): Promise<void> {
if (mode === 'background' && owner.ownerKey !== target.ownerKey) return
const targetOwnerKey = target.ownerKey
const targetUserId = target.userId
const generation = ++loadGeneration
setSnapshot((current) => {
if (mode === 'initial') {
return {
...current,
ownerKey: targetOwnerKey,
phase: 'initial-loading',
syncStatus: 'loading',
errorCode: null,
}
}
// Background: keep the phase so dependent UI stays mounted.
return current.ownerKey === targetOwnerKey
? { ...current, syncStatus: 'loading', errorCode: null }
: current
})
const cache = await readCaches(storage, targetUserId)
const cacheReadFailed = cache.failed
let cacheWriteFailed = false
if (!isCurrentLoad(generation, targetOwnerKey)) return
if (targetUserId === null) {
const localPreferences = cache.installation?.preferences ?? { ...DEFAULT_PREFERENCES }
preferences = localPreferences
pendingPatch = cache.installation?.pendingPatch ?? {}
lastSyncedAt = cache.installation?.lastSyncedAt ?? null
setSnapshot({
ownerKey: targetOwnerKey,
phase: 'ready',
preferences: localPreferences,
syncStatus: cacheReadFailed ? 'error' : 'local',
errorCode: cacheReadFailed ? 'CACHE_READ_FAILED' : null,
lastSyncedAt,
})
return
}
const cachedPreferences = cache.user?.preferences
?? cache.installation?.preferences
?? { ...DEFAULT_PREFERENCES }
const stagedPatch: MobilePreferencesPatch = {
...(cache.user?.pendingPatch ?? {}),
...(cache.installation?.pendingPatch ?? {}),
}
const cachedWithPending = applyPreferencesPatch(cachedPreferences, stagedPatch)
const cachedSyncedAt = cache.user?.lastSyncedAt ?? cache.installation?.lastSyncedAt ?? null
preferences = cachedWithPending
pendingPatch = stagedPatch
lastSyncedAt = cachedSyncedAt
setSnapshot({
ownerKey: targetOwnerKey,
phase: 'ready',
preferences: cachedWithPending,
syncStatus: 'loading',
errorCode: cacheReadFailed ? 'CACHE_READ_FAILED' : null,
lastSyncedAt: cachedSyncedAt,
})
try {
await writeCaches(
storage,
targetUserId,
cachedWithPending,
stagedPatch,
cache.user?.lastSyncedAt ?? null,
)
} catch {
cacheWriteFailed = true
}
let resolved: MobilePreferences
try {
const server = await fetchServerPreferences(remote, targetUserId)
resolved = server === null || !isPatchEmpty(stagedPatch)
? await writeServerPreferences(
remote,
targetUserId,
server ?? cachedWithPending,
stagedPatch,
)
: server
} catch (error) {
if (!isCurrentLoad(generation, targetOwnerKey)) return
setSnapshot({
ownerKey: targetOwnerKey,
phase: 'ready',
preferences: cachedWithPending,
syncStatus: 'offline',
errorCode: resolveErrorCode(cacheReadFailed, cacheWriteFailed, errorCodeForSync(error)),
lastSyncedAt: cachedSyncedAt,
})
return
}
if (!isCurrentLoad(generation, targetOwnerKey)) return
// Edits committed while this refresh was in flight stay pending on top of
// the server value instead of being dropped.
const remainingPatch = removeCommittedPatch(pendingPatch, stagedPatch)
const nextPreferences = applyPreferencesPatch(resolved, remainingPatch)
const syncedAt = now()
preferences = nextPreferences
pendingPatch = remainingPatch
lastSyncedAt = syncedAt
try {
await writeCaches(storage, targetUserId, nextPreferences, remainingPatch, syncedAt)
} catch {
cacheWriteFailed = true
}
if (!isCurrentLoad(generation, targetOwnerKey)) return
// Read the in-memory state again: a commit may have landed during the cache write.
const errorCode = resolveErrorCode(cacheReadFailed, cacheWriteFailed, null)
setSnapshot({
ownerKey: targetOwnerKey,
phase: 'ready',
preferences,
syncStatus: errorCode !== null
? 'error'
: isPatchEmpty(pendingPatch) ? 'synced' : 'saving',
errorCode,
lastSyncedAt: syncedAt,
})
}
function load(target: PreferencesOwner): Promise<void> {
const sameOwnerReady = snapshot.ownerKey === target.ownerKey && snapshot.phase === 'ready'
owner = { ...target }
return refresh(owner, sameOwnerReady ? 'background' : 'initial')
}
function resync(): Promise<void> {
const target = owner
if (target.userId !== null && !isPatchEmpty(pendingPatch)) {
return queueServerFlush(target.userId).then(() => undefined)
}
if (resyncInFlight !== null) return resyncInFlight
const run = enqueue('server', () => refresh(target, 'background'))
const tracked = run.finally(() => {
if (resyncInFlight === tracked) resyncInFlight = null
})
resyncInFlight = tracked
return tracked
}
function needsForegroundResync(): boolean {
if (owner.userId === null) return false
return !isPatchEmpty(pendingPatch) || snapshot.syncStatus === 'offline'
}
async function commitNow(
requestedPatch: MobilePreferencesPatch,
): Promise<PreferenceMutationResult> {
const targetOwnerKey = owner.ownerKey
const targetUserId = owner.userId
const patch = normalizePreferencesPatch(requestedPatch)
if (isPatchEmpty(patch)) {
return {
localSaved: true,
serverSynced: targetUserId === null || isPatchEmpty(pendingPatch),
}
}
const nextPreferences = applyPreferencesPatch(preferences, patch)
const nextPendingPatch = { ...pendingPatch, ...patch }
try {
await writeCaches(
storage,
targetUserId,
nextPreferences,
nextPendingPatch,
lastSyncedAt,
targetUserId === null ? nextPendingPatch : {},
)
} catch {
if (owner.ownerKey === targetOwnerKey) {
setSnapshot((current) => ({
...current,
syncStatus: 'error',
errorCode: 'CACHE_WRITE_FAILED',
}))
}
return { localSaved: false, serverSynced: false }
}
if (owner.ownerKey !== targetOwnerKey) {
return { localSaved: true, serverSynced: false }
}
preferences = nextPreferences
pendingPatch = nextPendingPatch
setSnapshot((current) => ({
...current,
ownerKey: targetOwnerKey,
phase: 'ready',
preferences: nextPreferences,
syncStatus: targetUserId === null ? 'local' : 'saving',
errorCode: null,
}))
if (targetUserId === null) {
return { localSaved: true, serverSynced: false }
}
const serverSynced = await queueServerFlush(targetUserId)
return { localSaved: true, serverSynced }
}
function commit(patch: MobilePreferencesPatch): Promise<PreferenceMutationResult> {
return enqueue('local', () => commitNow(patch))
}
function applyRemote(userId: string, row: unknown): void {
if (owner.userId !== userId || !isPatchEmpty(pendingPatch)) return
const parsed = parseSettingsRow(row)
if (parsed === null) return
const syncedAt = now()
preferences = parsed
lastSyncedAt = syncedAt
setSnapshot({
ownerKey: preferencesOwnerFor(userId).ownerKey,
phase: 'ready',
preferences: parsed,
syncStatus: 'synced',
errorCode: null,
lastSyncedAt: syncedAt,
})
void writeCaches(storage, userId, parsed, {}, syncedAt).catch(() => {
if (owner.userId !== userId) return
setSnapshot((current) => ({
...current,
syncStatus: 'error',
errorCode: 'CACHE_WRITE_FAILED',
}))
})
}
return {
getSnapshot: () => snapshot,
subscribe(listener) {
listeners.add(listener)
return () => {
listeners.delete(listener)
}
},
load,
resync,
needsForegroundResync,
commit,
applyRemote,
}
}

View file

@ -0,0 +1,57 @@
import { RecorderBusyError } from './recorder/recorder-errors'
import type {
RecorderOwner,
RecorderSession,
RecordingRuntimeSnapshot,
} from './recorder/recorder-types'
/** The part of the shared recorder facade this policy needs. */
export interface CaptureRecorderPort {
acquire(owner: RecorderOwner): Promise<RecorderSession>
}
function isLive(snapshot: RecordingRuntimeSnapshot): boolean {
return snapshot.state === 'recording' || snapshot.state === 'paused'
}
function isReattachable(snapshot: RecordingRuntimeSnapshot): boolean {
return (snapshot.state === 'stopped' || snapshot.state === 'recoverable')
&& snapshot.recording !== null
}
/**
* Stops the microphone when the signed-in session is lost involuntarily, but
* keeps the meeting capture's file so its owner can process it after signing
* back in (Record restores a stopped capture as a recoverable recording).
*
* - A Talk turn is transient conversation input, not durable work: discarded.
* - A capture the backend cannot hand back after the screen unmounts (the
* in-process iOS recorder reports no reattachable snapshot) is discarded as
* before, because a stranded stopped capture would block every new
* recording.
*/
export async function stopLiveCaptureKeepingFile(recorder: CaptureRecorderPort): Promise<void> {
let session: RecorderSession
try {
session = await recorder.acquire('record')
} catch (error) {
if (!(error instanceof RecorderBusyError)) throw error
const holder = await recorder.acquire(error.holder)
await holder.cancel()
return
}
try {
await session.stop()
} catch {
// Nothing is driven by this JS process (idle, already stopped, or a
// foreground-service capture that outlived a restart): inspect it below.
}
let snapshot = await session.restore()
if (isLive(snapshot)) {
await session.stop()
snapshot = await session.restore()
}
if (isReattachable(snapshot)) return
await session.cancel()
}

View file

@ -0,0 +1,77 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
const STORAGE_KEY = '@d3ro/account-retained-work/v1'
const MAX_USER_ID_LENGTH = 128
/** Storage port; AsyncStorage in the app, an in-memory fake in tests. */
export interface RetainedWorkStoragePort {
getItem(key: string): Promise<string | null>
setItem(key: string, value: string): Promise<void>
removeItem(key: string): Promise<void>
}
/**
* Persisted marker naming the account whose unsynced recordings were kept on
* this device when its session was lost involuntarily. It survives a restart
* so the next boundary can tell "the owner came back" (keep and resume) from
* "another account arrived" (discard before that account sees the device).
*
* `current()` is synchronous so the auth boundary can decide without an extra
* await; `load()` must have settled once before the first decision.
*/
export interface RetainedWorkOwnerStore {
load(): Promise<string | null>
current(): string | null
retain(userId: string): Promise<void>
release(): Promise<void>
}
function parseOwner(raw: string | null): string | null {
if (raw === null) return null
const trimmed = raw.trim()
return trimmed.length > 0 && trimmed.length <= MAX_USER_ID_LENGTH ? trimmed : null
}
export function createRetainedWorkOwnerStore(
storage: RetainedWorkStoragePort,
): RetainedWorkOwnerStore {
let cached: string | null = null
let version = 0
let loading: Promise<string | null> | null = null
return {
load(): Promise<string | null> {
if (loading !== null) return loading
const loadVersion = version
const operation = storage.getItem(STORAGE_KEY).then((raw) => {
// A retain/release issued while the read was in flight is newer.
if (version === loadVersion) cached = parseOwner(raw)
return cached
})
loading = operation
operation.catch(() => {
if (loading === operation) loading = null
})
return operation
},
current(): string | null {
return cached
},
async retain(userId: string): Promise<void> {
const owner = parseOwner(userId)
if (owner === null) throw new Error('retained_work_owner_invalid')
version += 1
cached = owner
await storage.setItem(STORAGE_KEY, owner)
},
async release(): Promise<void> {
version += 1
cached = null
await storage.removeItem(STORAGE_KEY)
},
}
}
export const retainedAccountWork: RetainedWorkOwnerStore = createRetainedWorkOwnerStore(AsyncStorage)
export const retainedAccountWorkTestContract = { storageKey: STORAGE_KEY }

View file

@ -41,6 +41,21 @@ import {
import { useMobilePreferences } from '../lib/preferences-context'
import { useDevice } from '../lib/device-context'
import { detachPushRegistrationForLogout } from '../features/notifications/notification-service'
import {
accountDeletionFailureMessageKey,
edgeAccountDeletionService,
runAccountDeletion,
type AccountDeletionService,
} from '../features/account/account-deletion-service'
// Public surface kept for existing importers; the use case now lives in
// features/account so it is testable without rendering this screen.
export {
edgeAccountDeletionService,
type AccountDeletionFailureCode,
type AccountDeletionResult,
type AccountDeletionService,
} from '../features/account/account-deletion-service'
const PROFILE_COLUMNS = 'id, name, avatar_url, locale, tier, created_at, updated_at'
const HTTP_URL_PATTERN = /^https?:\/\//i
@ -57,42 +72,6 @@ type Profile = {
updated_at: string
}
export type AccountDeletionFailureCode =
| 'SERVER_ENDPOINT_UNAVAILABLE'
| 'REAUTHENTICATION_REQUIRED'
| 'REQUEST_FAILED'
export type AccountDeletionResult =
| { ok: true }
| { ok: false; code: AccountDeletionFailureCode; message?: string }
/** Server-owned boundary for deleting auth.users and its cascaded account data. */
export interface AccountDeletionService {
deleteCurrentAccount(): Promise<AccountDeletionResult>
}
export const edgeAccountDeletionService: AccountDeletionService = Object.freeze({
async deleteCurrentAccount(): Promise<AccountDeletionResult> {
const { data, error } = await supabase.functions.invoke('account-delete', {
body: { confirmation: 'DELETE_MY_ACCOUNT' },
})
const response = data as { success?: boolean; code?: string; error?: string } | null
if (error || response?.success !== true) {
if (response?.code === 'REAUTHENTICATION_REQUIRED') {
return { ok: false, code: 'REAUTHENTICATION_REQUIRED', message: response.error }
}
return {
ok: false,
code: 'REQUEST_FAILED',
message: response?.error ?? error?.message,
}
}
return { ok: true }
},
})
export interface AccountScreenProps {
deletionService?: AccountDeletionService
identityService?: IdentityManagementService
@ -413,26 +392,25 @@ export default function AccountScreen({
setErrorMessage(null)
setSuccessMessage(null)
const userId = user?.id ?? null
const deviceId = currentDevice?.id ?? null
try {
if (user?.id && currentDevice?.id) {
await detachPushRegistrationForLogout(user.id, currentDevice.id).catch(() => undefined)
}
const result = await deletionService.deleteCurrentAccount()
// Push is detached only after the server confirms auth.users deletion,
// then the central privacy boundary removes every account-owned local
// artifact and the secure Supabase session before Login can render.
const result = await runAccountDeletion({
deletionService,
detachPushRegistration: async () => {
if (userId && deviceId) await detachPushRegistrationForLogout(userId, deviceId)
},
purgeLocalSession,
})
if (!result.ok) {
const message = result.code === 'SERVER_ENDPOINT_UNAVAILABLE'
? t('mobile.account.deleteUnavailable')
: result.code === 'REAUTHENTICATION_REQUIRED'
? t('mobile.account.reauthenticationRequired')
: result.message ?? t('mobile.account.deleteFailed')
const message = t(accountDeletionFailureMessageKey(result.code))
setErrorMessage(message)
Alert.alert(t('mobile.auth.errorTitle'), message)
return
}
// Only reachable after the server confirms auth.users deletion. The
// central privacy boundary removes every account-owned local artifact
// and the secure Supabase session before Login can render.
await purgeLocalSession()
} catch {
const message = t('mobile.auth.localDataCleanupBody')
setErrorMessage(message)