d3ro-voice/apps/mobile-rn/src/lib/retain-live-capture.ts
Yun Chan ba9ef9741e 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.
2026-09-28 20:45:52 +09:00

57 lines
1.9 KiB
TypeScript

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()
}