fix(speko): require saved audio evidence before cleanup completes

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-09 21:32:23 -05:00
1 parent faf217940c
commit 3b3a9c1af9
4 files changed
+73 -12

No files matched your search

@@ -80,7 +80,7 @@ Latest UI review repair: voice start, inspection, and hangup requests now have t
Runtime follow-up review repairs preserve pending questions across reconnect, allow canonical authenticated task-page answers to continue the original voice request, and retain accepted guest tasks in their saved sandbox when future-call settings change. Current permission, credential, isolation, sandbox availability, and company ownership checks remain enforced. All 85 voice persistence tests and server typecheck passed. Five focused native/legacy signed-tool and task-page continuation/authority regressions passed; the other 145 cases were intentionally unselected in that focused run.
Harness review repairs: successful local cleanup operations are tracked individually; failed operations remain retryable and End stays available until local and provider cleanup complete. Six cleanup/redaction tests pass. The live browser harness attempts provider hangup independently of evidence-write failures. README links now point to the included report and runbook instead of absent historical private reports.
Harness review repairs: successful local cleanup operations are tracked individually; failed operations remain retryable and End stays available until local and provider cleanup complete. Eight cleanup/redaction tests pass. Audio cleanup awaits finalized MediaRecorder data and a successful upload; a failed upload retains the recording for retry and keeps End available. Recording finalization and upload have bounded deadlines. No real audio session was run for this repair. The live browser harness attempts provider hangup independently of evidence-write failures. README links now point to the included report and runbook instead of absent historical private reports.
Interrupted setup qualification: safe secret-presence serialization and production-component recovery regressions pass, alongside UI/server typecheck and token gates. The 155-story build and all 32 affected provider-setup interaction/accessibility checks passed across both themes and desktop/mobile widths.
+24 -10
View File
@@ -1,5 +1,5 @@
import { VoiceConversation } from '@spekoai/client';
import { createCleanup } from './cleanup.mjs';
import { createCleanup, createAudioSave } from './cleanup.mjs';
const config = JSON.parse(document.querySelector('#config').textContent);
const $ = (id) => document.getElementById(id);
const events = [];
@@ -8,7 +8,7 @@ const tracks = [];
const cleanupFailures = [];
const began = performance.now();
let conversation, context, destination, muted = false, running = false, ended = false;
let speaking = false, notificationSent = false, sessionId, interval, recorder;
let speaking = false, notificationSent = false, sessionId, interval, recorder, audioEvidence;
const event = (kind, fields = {}) => { events.push({ kind, elapsedMs: Math.round(performance.now() - began), ...fields }); };
const status = (text) => { $('status').textContent = text; };
async function api(path, body = {}) {
@@ -23,13 +23,27 @@ const observer = new MutationObserver(() => {
recorder = new MediaRecorder(stream);
const chunks = [];
recorder.ondataavailable = (e) => { if (e.data.size) chunks.push(e.data); };
recorder.onstop = async () => {
try {
const result = await fetch('/audio', { method: 'POST', headers: { 'content-type': 'audio/webm', 'x-proof-csrf': config.csrf }, body: new Blob(chunks, { type: 'audio/webm' }) });
event('received_audio_saved', { ok: result.ok });
await saveReport();
} catch { event('received_audio_save_failed'); }
};
const recorded = new Promise((resolve, reject) => {
recorder.onstop = () => resolve(new Blob(chunks, {type: 'audio/webm'}));
recorder.onerror = () => reject(new Error('Recording failed'));
});
recorded.catch(() => {}); // Cleanup observes the original rejected promise.
audioEvidence = createAudioSave({
stop: async () => {
if (recorder.state !== 'inactive') recorder.stop();
let timer;
try {
return await Promise.race([recorded, new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error('Recording did not finish')), 10_000);
})]);
} finally { clearTimeout(timer); }
},
upload: async blob => {
const response = await fetch('/audio', {method: 'POST', headers: {'content-type': 'audio/webm', 'x-proof-csrf': config.csrf}, body: blob, signal: AbortSignal.timeout(20_000)});
if (!response.ok) throw new Error('Received audio could not be saved');
event('received_audio_saved', {ok: true});
},
});
recorder.start(); event('received_audio_recording_started');
});
observer.observe(document.body, { childList: true, subtree: true });
@@ -78,7 +92,7 @@ const cleanup = createCleanup({
local: async (attempt) => {
ended = true; clearInterval(interval); observer.disconnect();
$('scenario').disabled = $('mute').disabled = true;
await attempt('recorder.stop', () => { if (recorder?.state === 'recording') recorder.stop(); });
await attempt('recording.save', () => audioEvidence?.save());
await attempt('sdk.end', () => conversation?.endSession());
event('sdk_cleanup', { inputTracksEnded: tracks.every((t) => t.readyState === 'ended'), audioElements: document.querySelectorAll('audio').length });
await attempt('tracks.stop', () => { destination?.stream.getTracks().forEach((t) => t.stop()); tracks.forEach((t) => t.stop()); });
+17
View File
@@ -35,3 +35,20 @@ export function createCleanup({ local, provider, report, failed }) {
},
};
}
/** Keep the finalized recording for retry until its upload is acknowledged. */
export function createAudioSave({stop, upload}) {
let blob, saved = false, pending;
return {
save() {
if (saved) return Promise.resolve();
if (pending) return pending;
pending = (async () => {
blob ??= await stop();
await upload(blob);
saved = true;
})().finally(() => { pending = undefined; });
return pending;
},
};
}
+31 -1
View File
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createCleanup } from './cleanup.mjs';
import { createCleanup, createAudioSave } from './cleanup.mjs';
test('failed hangup preserves evidence and retries only the unconfirmed provider operation', async () => {
let locals = 0, ends = 0;
@@ -66,3 +66,33 @@ test('retries failed local operations and keeps End available until all media cl
assert.equal(cleanup.complete, true);
assert.deepEqual({tracks, sdk, ends, reports}, {tracks: 1, sdk: 2, ends: 1, reports: 2});
});
test('failed audio upload is retryable with the same finalized recording and blocks cleanup completion', async () => {
const blob = new Blob(['synthetic-audio']);
let stops = 0, uploads = 0, ends = 0;
const evidence = createAudioSave({stop: async () => { stops++; return blob; }, upload: async value => {
assert.equal(value, blob); if (++uploads === 1) throw new Error('HTTP failure');
}});
const failures = [];
const cleanup = createCleanup({local: async attempt => { await attempt('recording.save', () => evidence.save()); },
provider: async () => { ends++; }, report: async () => {}, failed: operation => failures.push(operation)});
assert.equal(await cleanup.end(), false);
assert.equal(cleanup.complete, false);
assert.deepEqual(failures, ['recording.save']);
assert.equal(await cleanup.end(), true);
assert.equal(cleanup.complete, true);
assert.deepEqual({stops, uploads, ends}, {stops: 1, uploads: 2, ends: 1});
});
test('audio evidence waits for final recording data before upload and coalesces concurrent saves', async () => {
let finish, uploads = 0;
const stopped = new Promise(resolve => { finish = resolve; });
const evidence = createAudioSave({stop: () => stopped, upload: async () => { uploads++; }});
const first = evidence.save(), second = evidence.save();
assert.equal(first, second);
await Promise.resolve(); assert.equal(uploads, 0);
finish(new Blob(['synthetic-audio']));
await first; await evidence.save();
assert.equal(uploads, 1);
});