Repository navigation
fix(transcription): budget speaker-linking by audio length, degrade on timeout #84
Changes from all commits
1f4b02d
5f3a212
5c7f17b
a5aaaad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,44 @@ export class SpeakerLinkingUnavailableError extends Error { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wall-clock budget for the acoustic worker. pyannote runs at roughly 3.4x realtime on | ||
| * this class of machine (expected time ~ duration / 3.4), so a flat cap silently fails | ||
| * every recording longer than about cap x 3.4: on 2026-09-15 a device backlog of 30 | ||
| * recordings was imported at once and the 19 longer than ~34 minutes all died with | ||
| * "timed out after 600 seconds" (the 600 s default) while every shorter one completed. | ||
| * The budget is the configured floor or 0.75x the audio length, whichever is larger: | ||
| * about 2.5x the expected time, enough for a loaded machine without letting one stuck | ||
| * recording hold the serialized queue for hours (1.5x would have granted 3.5 h to a | ||
| * 2h21m file). Unknown, zero, negative or non-finite lengths keep the floor. | ||
| */ | ||
| export const SPEAKER_LINKING_BUDGET_PER_AUDIO_SECOND = 0.75 | ||
|
|
||
| export function speakerLinkingTimeoutMs( | ||
| configuredSeconds: number, | ||
| audioDurationSeconds?: number | null | ||
| ): number { | ||
| const floor = Math.max(30, Number.isFinite(configuredSeconds) ? configuredSeconds : 0) | ||
| const scaled = typeof audioDurationSeconds === 'number' && Number.isFinite(audioDurationSeconds) && audioDurationSeconds > 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Wrap the changed lines that exceed 120 characters. These lines exceed the configured line-length limit. Split the duration predicate into a named boolean. Split the As per coding guidelines: “Keep line length at 120 characters.” Also applies to: 110-110 🧰 Tools🪛 ast-grep (0.45.3)[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec. (detect-child-process-typescript) 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| ? Math.ceil(audioDurationSeconds * SPEAKER_LINKING_BUDGET_PER_AUDIO_SECOND) | ||
| : 0 | ||
| return Math.max(floor, scaled) * 1000 | ||
| } | ||
|
|
||
| /** | ||
| * Kill the worker and everything it spawned. On Windows `child.kill()` only ends the | ||
| * Python process: the FFmpeg it started through subprocess.run() keeps decoding the file | ||
| * until it notices the broken pipe, outside any lifecycle the queue knows about. | ||
| */ | ||
| function killWorkerTree(child: ReturnType<typeof spawn>): void { | ||
| if (child.exitCode !== null || child.signalCode !== null) return | ||
| if (process.platform === 'win32' && child.pid) { | ||
| spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' }).on('error', () => child.kill()) | ||
| return | ||
| } | ||
| child.kill('SIGKILL') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '70,130p' apps/electron/electron/main/services/speaker-linking.ts
sed -n '175,280p' apps/electron/electron/main/services/speaker-linking.ts
rg -n 'spawn\(|ffmpeg|subprocess|Popen|runWorker|python' apps/electron/electron/main services scripts packages 2>/dev/null | head -200Repository: sgeraldes/hidock-next Length of output: 27788 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- worker candidates ---'
find . -type f \( -name 'worker.py' -o -iname '*speaker*link*' \) -not -path '*/node_modules/*' -not -path '*/dist/*' -not -path '*/build/*' | sort
printf '%s\n' '--- runWorker caller ---'
sed -n '430,515p' apps/electron/electron/main/services/speaker-linking.ts
printf '%s\n' '--- worker subprocess references ---'
rg -n -C 4 'subprocess|ffmpeg|FFMPEG_PATH|torchaudio|Audio|audio' --glob '*.py' --glob '!scripts/**' --glob '!**/node_modules/**' .Repository: sgeraldes/hidock-next Length of output: 50378 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- worker files ---'
workers=$(find . -type f -name 'worker.py' -not -path '*/node_modules/*' -not -path '*/dist/*' -not -path '*/build/*' -print)
printf '%s\n' "$workers"
printf '%s\n' '--- runWorker caller ---'
sed -n '430,515p' apps/electron/electron/main/services/speaker-linking.ts
printf '%s\n' '--- worker process calls ---'
for file in $workers; do
printf '%s\n' "--- $file ---"
rg -n -C 5 'subprocess|Popen|run\(|ffmpeg|FFMPEG_PATH|torchaudio|Audio' "$file" || true
doneRepository: sgeraldes/hidock-next Length of output: 5349 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- preflight call sites ---'
rg -n -C 12 'runSpeakerLinkingPreflight|provider.*diar|diariz|SpeakerLinkingUnavailableError|clearAcousticEvidence' apps/electron/electron/main/services --glob '*.ts'Repository: sgeraldes/hidock-next Length of output: 50378 Terminate the POSIX worker process group. Proposed fix const child = spawn(python, args, {
+ detached: process.platform !== 'win32',
windowsHide: true,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
@@
- child.kill('SIGKILL')
+ try {
+ if (child.pid) {
+ process.kill(-child.pid, 'SIGKILL')
+ } else {
+ child.kill('SIGKILL')
+ }
+ } catch {
+ child.kill('SIGKILL')
+ }🧰 Tools🪛 ast-grep (0.45.3)[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec. (detect-child-process-typescript) 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| export function normalizeEmbedding(values: number[]): number[] { | ||
| if (!values.length || values.some((value) => !Number.isFinite(value))) return [] | ||
| const magnitude = Math.sqrt(values.reduce((sum, value) => sum + value * value, 0)) | ||
|
|
@@ -147,7 +185,8 @@ function resolveWorkerPath(configured: string): string { | |
|
|
||
| function runWorker( | ||
| audioPath: string, | ||
| shouldContinue: () => boolean | ||
| shouldContinue: () => boolean, | ||
| audioDurationSeconds?: number | null | ||
| ): Promise<AcousticWorkerResult> { | ||
| const config = getConfig().transcription | ||
| const workerPath = resolveWorkerPath(config.speakerLinkingWorkerPath) | ||
|
|
@@ -183,10 +222,21 @@ function runWorker( | |
| let stdout = '' | ||
| let stderr = '' | ||
| const cap = 25 * 1024 * 1024 | ||
| const timeoutMs = Math.max(30, config.speakerLinkingTimeoutSeconds) * 1000 | ||
| const timeoutMs = speakerLinkingTimeoutMs(config.speakerLinkingTimeoutSeconds, audioDurationSeconds) | ||
| // A timeout means the worker could not serve THIS recording in budget, not that the | ||
| // audio is bad: degrade to provider-managed diarization instead of failing the | ||
| // transcript (the transcript is the product, voice linking is the enhancement). The | ||
| // rejection waits for the process tree to be gone, so the provider run never overlaps | ||
| // a worker still decoding the same file; a grace timer covers a kill that hangs. | ||
| let timedOut = false | ||
| let grace: NodeJS.Timeout | null = null | ||
| const rejectTimedOut = () => reject(new SpeakerLinkingUnavailableError( | ||
| `speaker-linking timed out after ${Math.round(timeoutMs / 1000)} seconds` | ||
| )) | ||
| const timeout = setTimeout(() => { | ||
| child.kill() | ||
| reject(new Error(`speaker-linking timed out after ${Math.round(timeoutMs / 1000)} seconds`)) | ||
| timedOut = true | ||
| killWorkerTree(child) | ||
| grace = setTimeout(rejectTimedOut, 10_000) | ||
|
Comment on lines
+225
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '100,125p' apps/electron/electron/main/services/speaker-linking.ts
sed -n '220,245p' apps/electron/electron/main/services/speaker-linking.ts
rg -n 'taskkill|killWorkerTree|TIMEOUT_KILL_GRACE|rejectTimedOut' apps/electron/electron/main/services apps/electron/electron/main/services/__tests__Repository: sgeraldes/hidock-next Length of output: 7059 🏁 Script executed: sed -n '1,35p' apps/electron/electron/main/services/speaker-linking.ts
sed -n '245,280p' apps/electron/electron/main/services/speaker-linking.ts
sed -n '319,372p' apps/electron/electron/main/services/brains/cli-runner.tsRepository: sgeraldes/hidock-next Length of output: 5118 Confirm worker-tree termination before provider fallback.
Keep the bounded timeout, but trigger provider fallback only after confirmed tree termination. If cleanup is not confirmed, use a failure path that does not start provider-managed diarization. 🧰 Tools🪛 ast-grep (0.45.3)[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec. (detect-child-process-typescript) 🤖 Prompt for AI Agents |
||
| }, timeoutMs) | ||
| const cancellation = setInterval(() => { | ||
| if (!shouldContinue()) { | ||
|
|
@@ -212,6 +262,11 @@ function runWorker( | |
| child.on('close', (code) => { | ||
| clearTimeout(timeout) | ||
| clearInterval(cancellation) | ||
| if (timedOut) { | ||
| if (grace) clearTimeout(grace) | ||
| rejectTimedOut() | ||
| return | ||
| } | ||
| if (code !== 0) { | ||
| const detail = stderr.trim().split('\n').slice(-12).join('\n') || `speaker-linking exited with code ${code}` | ||
| if (/ModuleNotFoundError|No module named|GatedRepo|401|403|not authorized|cannot access gated/i.test(detail)) { | ||
|
|
@@ -287,6 +342,16 @@ function removeExistingRecordingEvidence(recordingId: string): void { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * A degraded run (worker unavailable or out of budget) must not leave the evidence of an | ||
| * earlier successful run behind: the transcript is about to carry provider labels, and | ||
| * `recording_voice_clusters` / `voice_cluster_observations` from the old run would keep | ||
| * exposing a "Voice ABCDEF" that no longer exists in it, and could be re-bound later. | ||
| */ | ||
| export function clearAcousticEvidence(recordingId: string): void { | ||
| runInTransaction(() => removeExistingRecordingEvidence(recordingId)) | ||
| } | ||
|
|
||
| function persistMatches(recordingId: string, result: AcousticWorkerResult): VoiceMatch[] { | ||
| const config = getConfig().transcription | ||
| return runInTransaction(() => { | ||
|
|
@@ -386,7 +451,8 @@ function persistMatches(recordingId: string, result: AcousticWorkerResult): Voic | |
| export async function runSpeakerLinkingPreflight( | ||
| recordingId: string, | ||
| audioPath: string, | ||
| shouldContinue: () => boolean | ||
| shouldContinue: () => boolean, | ||
| audioDurationSeconds?: number | null | ||
| ): Promise<SpeakerLinkingResult> { | ||
| const config = getConfig().transcription | ||
| if (!config.speakerLinkingEnabled) { | ||
|
|
@@ -400,7 +466,13 @@ export async function runSpeakerLinkingPreflight( | |
| reason: 'disabled in transcription settings' | ||
| } | ||
| } | ||
| const result = await runWorker(audioPath, shouldContinue) | ||
| let result: AcousticWorkerResult | ||
| try { | ||
| result = await runWorker(audioPath, shouldContinue, audioDurationSeconds) | ||
| } catch (error) { | ||
| if (error instanceof SpeakerLinkingUnavailableError) clearAcousticEvidence(recordingId) | ||
| throw error | ||
| } | ||
| if (!shouldContinue()) throw new Error('speaker-linking cancelled because recording became ineligible') | ||
| const matches = persistMatches(recordingId, result) | ||
| return { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Wrap the changed SQL and assertion lines.
These lines exceed the configured 120-character limit. Use multiline query strings and assign repeated queries to local constants.
As per coding guidelines: “Keep line length at 120 characters.”
Also applies to: 85-85, 89-89, 93-94
🤖 Prompt for AI Agents
Source: Coding guidelines