@openparachute/vault 0.7.3-rc.11 → 0.7.3-rc.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/transcription-worker.test.ts +151 -0
- package/src/transcription-worker.ts +113 -52
package/package.json
CHANGED
|
@@ -1627,3 +1627,154 @@ describe("transcription worker — legacy in-body memo content safety (finding F
|
|
|
1627
1627
|
expect(note!.content).toContain("retry text with $& and $0 and $$ literal");
|
|
1628
1628
|
});
|
|
1629
1629
|
});
|
|
1630
|
+
|
|
1631
|
+
// ---------------------------------------------------------------------------
|
|
1632
|
+
// voice W2 — segmented recordings. The app slices a long recording into
|
|
1633
|
+
// ~10-min segments, each linked as its own attachment on ONE note carrying a
|
|
1634
|
+
// client-set `segment_index` (0-based). The legacy in-body path targets that
|
|
1635
|
+
// part's markers — `_Transcript pending (part N)._` / `_Transcription
|
|
1636
|
+
// unavailable (part N)._`, N = segment_index + 1 — so each transcript lands in
|
|
1637
|
+
// its own pre-allocated slot regardless of completion order. Marker strings
|
|
1638
|
+
// are a BYTE-EXACT cross-door + cross-repo contract (the cloud worker ships
|
|
1639
|
+
// the identical text), so these assertions pin the exact bytes.
|
|
1640
|
+
// ---------------------------------------------------------------------------
|
|
1641
|
+
|
|
1642
|
+
describe("transcription worker — segmented recordings (voice W2)", () => {
|
|
1643
|
+
test("three parts completing OUT OF ORDER (2 ok, 0 fails, 1 ok) each land in their own slot", async () => {
|
|
1644
|
+
// One note, three pre-allocated part slots + the shared stub opt-in.
|
|
1645
|
+
const body =
|
|
1646
|
+
"# 🎙️ Voice memo\n\n" +
|
|
1647
|
+
"_Transcript pending (part 1)._\n\n" +
|
|
1648
|
+
"_Transcript pending (part 2)._\n\n" +
|
|
1649
|
+
"_Transcript pending (part 3)._\n";
|
|
1650
|
+
await store.createNote(body, { id: "seg-note", metadata: { transcribe_stub: true } });
|
|
1651
|
+
|
|
1652
|
+
seedAudio("memos/seg0.webm");
|
|
1653
|
+
seedAudio("memos/seg1.webm");
|
|
1654
|
+
seedAudio("memos/seg2.webm");
|
|
1655
|
+
const seg0 = await store.addAttachment("seg-note", "memos/seg0.webm", "audio/webm", {
|
|
1656
|
+
transcribe_status: "pending",
|
|
1657
|
+
segment_index: 0,
|
|
1658
|
+
});
|
|
1659
|
+
const seg1 = await store.addAttachment("seg-note", "memos/seg1.webm", "audio/webm", {
|
|
1660
|
+
transcribe_status: "pending",
|
|
1661
|
+
segment_index: 1,
|
|
1662
|
+
});
|
|
1663
|
+
const seg2 = await store.addAttachment("seg-note", "memos/seg2.webm", "audio/webm", {
|
|
1664
|
+
transcribe_status: "pending",
|
|
1665
|
+
segment_index: 2,
|
|
1666
|
+
});
|
|
1667
|
+
|
|
1668
|
+
// Complete part 3 (segment_index 2) FIRST — success.
|
|
1669
|
+
const w2 = makeWorker({ fetchImpl: mkFetchMock([{ text: "part three text" }]) });
|
|
1670
|
+
try { await w2.kick("default", seg2); } finally { await w2.stop(); }
|
|
1671
|
+
|
|
1672
|
+
// Then part 1 (segment_index 0) — terminal failure (maxAttempts=1).
|
|
1673
|
+
const w0 = makeWorker({
|
|
1674
|
+
fetchImpl: mkFetchMock([{ error: "scribe down", status: 500 }]),
|
|
1675
|
+
maxAttempts: 1,
|
|
1676
|
+
});
|
|
1677
|
+
try { await w0.kick("default", seg0); } finally { await w0.stop(); }
|
|
1678
|
+
|
|
1679
|
+
// Finally part 2 (segment_index 1) — success.
|
|
1680
|
+
const w1 = makeWorker({ fetchImpl: mkFetchMock([{ text: "part two text" }]) });
|
|
1681
|
+
try { await w1.kick("default", seg1); } finally { await w1.stop(); }
|
|
1682
|
+
|
|
1683
|
+
const note = await store.getNote("seg-note");
|
|
1684
|
+
// Each part landed in its own slot: part 1 → failure marker, part 2/3 →
|
|
1685
|
+
// their transcripts, despite completing 3 → 1 → 2.
|
|
1686
|
+
expect(note!.content).toBe(
|
|
1687
|
+
"# 🎙️ Voice memo\n\n" +
|
|
1688
|
+
"_Transcription unavailable (part 1)._\n\n" +
|
|
1689
|
+
"part two text\n\n" +
|
|
1690
|
+
"part three text\n",
|
|
1691
|
+
);
|
|
1692
|
+
// Shared stub SURVIVES — sibling parts each needed the gate open; a
|
|
1693
|
+
// per-part clear (the un-segmented behavior) would have blocked parts 2 & 3.
|
|
1694
|
+
expect((note!.metadata as any)?.transcribe_stub).toBe(true);
|
|
1695
|
+
|
|
1696
|
+
// Attachment rows reflect their individual outcomes.
|
|
1697
|
+
const atts = await store.getAttachments("seg-note");
|
|
1698
|
+
const byIndex = Object.fromEntries(atts.map((a) => [a.metadata?.segment_index, a]));
|
|
1699
|
+
expect(byIndex[0]!.metadata?.transcribe_status).toBe("failed");
|
|
1700
|
+
expect(byIndex[1]!.metadata?.transcribe_status).toBe("done");
|
|
1701
|
+
expect(byIndex[1]!.metadata?.transcript).toBe("part two text");
|
|
1702
|
+
expect(byIndex[2]!.metadata?.transcribe_status).toBe("done");
|
|
1703
|
+
expect(byIndex[2]!.metadata?.transcript).toBe("part three text");
|
|
1704
|
+
});
|
|
1705
|
+
|
|
1706
|
+
test("un-segmented attachment → BARE markers, one-shot stub cleared (regression pin)", async () => {
|
|
1707
|
+
// No `segment_index` → byte-for-byte the pre-W2 behavior: replace the bare
|
|
1708
|
+
// placeholder, clear the stub. A stray `(part N)` marker in the body is
|
|
1709
|
+
// left untouched (the bare path never targets part markers).
|
|
1710
|
+
await store.createNote(
|
|
1711
|
+
"# 🎙️ Voice memo\n\n_Transcript pending._\n\n_Transcript pending (part 2)._\n",
|
|
1712
|
+
{ id: "unseg-note", metadata: { transcribe_stub: true } },
|
|
1713
|
+
);
|
|
1714
|
+
seedAudio("memos/unseg.webm");
|
|
1715
|
+
const att = await store.addAttachment("unseg-note", "memos/unseg.webm", "audio/webm", {
|
|
1716
|
+
transcribe_status: "pending",
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1719
|
+
const worker = makeWorker({ fetchImpl: mkFetchMock([{ text: "bare transcript" }]) });
|
|
1720
|
+
try { await worker.kick("default", att); } finally { await worker.stop(); }
|
|
1721
|
+
|
|
1722
|
+
const note = await store.getNote("unseg-note");
|
|
1723
|
+
// Bare marker replaced; the part-2 marker is NOT touched by the bare path.
|
|
1724
|
+
expect(note!.content).toBe(
|
|
1725
|
+
"# 🎙️ Voice memo\n\nbare transcript\n\n_Transcript pending (part 2)._\n",
|
|
1726
|
+
);
|
|
1727
|
+
// One-shot stub cleared, exactly as today.
|
|
1728
|
+
expect((note!.metadata as any)?.transcribe_stub).toBeUndefined();
|
|
1729
|
+
});
|
|
1730
|
+
|
|
1731
|
+
test("malformed segment_index falls back to bare markers (contract: integer ≥ 0)", async () => {
|
|
1732
|
+
// A non-integer / negative `segment_index` must NOT fabricate a `(part N)`
|
|
1733
|
+
// marker — it degrades to the bare path (fully backward compatible).
|
|
1734
|
+
await store.createNote(
|
|
1735
|
+
"# 🎙️ Voice memo\n\n_Transcript pending._\n",
|
|
1736
|
+
{ id: "seg-bad", metadata: { transcribe_stub: true } },
|
|
1737
|
+
);
|
|
1738
|
+
seedAudio("memos/segbad.webm");
|
|
1739
|
+
const att = await store.addAttachment("seg-bad", "memos/segbad.webm", "audio/webm", {
|
|
1740
|
+
transcribe_status: "pending",
|
|
1741
|
+
segment_index: -1, // invalid → bare path
|
|
1742
|
+
});
|
|
1743
|
+
|
|
1744
|
+
const worker = makeWorker({ fetchImpl: mkFetchMock([{ text: "fallback transcript" }]) });
|
|
1745
|
+
try { await worker.kick("default", att); } finally { await worker.stop(); }
|
|
1746
|
+
|
|
1747
|
+
const note = await store.getNote("seg-bad");
|
|
1748
|
+
expect(note!.content).toBe("# 🎙️ Voice memo\n\nfallback transcript\n");
|
|
1749
|
+
expect((note!.metadata as any)?.transcribe_stub).toBeUndefined();
|
|
1750
|
+
});
|
|
1751
|
+
|
|
1752
|
+
test("segmented part whose marker was edited away → transcript appended (graceful fallback)", async () => {
|
|
1753
|
+
// The user rewrote part 2's slot by hand, removing its pending marker.
|
|
1754
|
+
// Mirror the un-segmented replace-or-append policy scoped to the part:
|
|
1755
|
+
// with neither of part 2's markers present, append the transcript
|
|
1756
|
+
// (no `(part N)` prefix) rather than destroying the user's edit.
|
|
1757
|
+
const editedBody =
|
|
1758
|
+
"# 🎙️ Voice memo\n\n" +
|
|
1759
|
+
"_Transcript pending (part 1)._\n\n" +
|
|
1760
|
+
"User rewrote part two by hand.\n";
|
|
1761
|
+
await store.createNote(editedBody, { id: "seg-edit", metadata: { transcribe_stub: true } });
|
|
1762
|
+
seedAudio("memos/segedit.webm");
|
|
1763
|
+
const seg1 = await store.addAttachment("seg-edit", "memos/segedit.webm", "audio/webm", {
|
|
1764
|
+
transcribe_status: "pending",
|
|
1765
|
+
segment_index: 1, // part 2
|
|
1766
|
+
});
|
|
1767
|
+
|
|
1768
|
+
const worker = makeWorker({ fetchImpl: mkFetchMock([{ text: "the real part two" }]) });
|
|
1769
|
+
try { await worker.kick("default", seg1); } finally { await worker.stop(); }
|
|
1770
|
+
|
|
1771
|
+
const note = await store.getNote("seg-edit");
|
|
1772
|
+
// Part 2's transcript appended; the user's edit + part 1's pending slot
|
|
1773
|
+
// both survive untouched.
|
|
1774
|
+
expect(note!.content).toBe(`${editedBody}\n\nthe real part two`);
|
|
1775
|
+
expect(note!.content).toContain("User rewrote part two by hand.");
|
|
1776
|
+
expect(note!.content).toContain("_Transcript pending (part 1)._");
|
|
1777
|
+
// Segmented → shared stub preserved (part 1 still needs the gate open).
|
|
1778
|
+
expect((note!.metadata as any)?.transcribe_stub).toBe(true);
|
|
1779
|
+
});
|
|
1780
|
+
});
|
|
@@ -64,41 +64,57 @@ import {
|
|
|
64
64
|
} from "../core/src/transcription/provider.ts";
|
|
65
65
|
import { ScribeHttpProvider } from "./transcription/providers/scribe-http.ts";
|
|
66
66
|
|
|
67
|
-
/** Placeholder pattern written by the voice-memo capture stub. */
|
|
68
|
-
const TRANSCRIPT_PLACEHOLDER = /_Transcript pending\._/;
|
|
69
|
-
|
|
70
67
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
68
|
+
* The in-body transcription markers.
|
|
69
|
+
*
|
|
70
|
+
* The BARE markers are the un-segmented default; voice W2 (segmented
|
|
71
|
+
* recordings) targets per-part variants built by `markersFor`. Both are a
|
|
72
|
+
* BYTE-EXACT cross-door + cross-repo contract — the cloud Workers-AI
|
|
73
|
+
* transcription path ships the identical strings, and the notes-ui status
|
|
74
|
+
* chip (parachute-surface TranscriptionStatus.tsx) keys off the failure
|
|
75
|
+
* marker's exact copy. Don't change any of this text without a coordinated
|
|
76
|
+
* change in both places. A friendlier "retry available" copy + chip
|
|
77
|
+
* affordance is a tracked parachute-surface follow-up.
|
|
76
78
|
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* tracked parachute-surface follow-up.
|
|
79
|
+
* Owning the failure marker here (it used to be written by Lens's now-removed
|
|
80
|
+
* scribe client) means a failed upload stops reading "Transcript pending"
|
|
81
|
+
* forever regardless of which client uploaded the audio.
|
|
81
82
|
*/
|
|
82
|
-
const
|
|
83
|
+
const BARE_PENDING = "_Transcript pending._";
|
|
84
|
+
const BARE_UNAVAILABLE = "_Transcription unavailable._";
|
|
85
|
+
|
|
86
|
+
/** Escape a literal string for safe embedding in a `RegExp`. */
|
|
87
|
+
function escapeRegExp(literal: string): string {
|
|
88
|
+
return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
89
|
+
}
|
|
83
90
|
|
|
84
91
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* the `_Recorded …_` line, the header).
|
|
92
|
-
*
|
|
93
|
-
* Deliberately NO `/g` flag — `.replace` swaps only the FIRST match. A
|
|
94
|
-
* canonical capture body holds exactly one marker, so first-match is the
|
|
95
|
-
* correct target. `applyFailureMarker`'s includes-guard (no-op when the
|
|
96
|
-
* marker is already present) prevents markers accumulating across repeated
|
|
97
|
-
* terminal failures, so the body never carries two of the same marker. A
|
|
98
|
-
* hand-edited body that somehow contains both markers patches only the
|
|
99
|
-
* first — accepted (degenerate, operator-induced).
|
|
92
|
+
* The pending + terminal-failure markers for an attachment, honoring
|
|
93
|
+
* `segment_index` (voice W2 — segmented recordings). `undefined` yields the
|
|
94
|
+
* bare markers (fully backward compatible — un-segmented flows are byte-
|
|
95
|
+
* unchanged). An integer ≥ 0 yields this part's markers, with the human-
|
|
96
|
+
* facing part number N = segment_index + 1 (1-based, decimal):
|
|
97
|
+
* `_Transcript pending (part N)._` / `_Transcription unavailable (part N)._`
|
|
100
98
|
*/
|
|
101
|
-
|
|
99
|
+
function markersFor(segmentIndex: number | undefined): { pending: string; unavailable: string } {
|
|
100
|
+
if (segmentIndex === undefined) return { pending: BARE_PENDING, unavailable: BARE_UNAVAILABLE };
|
|
101
|
+
const n = segmentIndex + 1;
|
|
102
|
+
return {
|
|
103
|
+
pending: `_Transcript pending (part ${n})._`,
|
|
104
|
+
unavailable: `_Transcription unavailable (part ${n})._`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A valid segment index (integer ≥ 0) off attachment metadata, else
|
|
110
|
+
* `undefined` — the un-segmented path. Client-set at link time; anything that
|
|
111
|
+
* isn't a non-negative integer falls back to the bare markers rather than
|
|
112
|
+
* fabricating a `(part N)`.
|
|
113
|
+
*/
|
|
114
|
+
function segmentIndexOf(meta: { segment_index?: unknown }): number | undefined {
|
|
115
|
+
const raw = meta.segment_index;
|
|
116
|
+
return typeof raw === "number" && Number.isInteger(raw) && raw >= 0 ? raw : undefined;
|
|
117
|
+
}
|
|
102
118
|
|
|
103
119
|
/**
|
|
104
120
|
* Default sweep cadence (ms). The sweep is the safety net for backoff-
|
|
@@ -195,6 +211,14 @@ interface PendingMeta {
|
|
|
195
211
|
* worker preserves the original stub-patching behavior (Lens flow).
|
|
196
212
|
*/
|
|
197
213
|
transcribe_origin?: "auto" | "legacy";
|
|
214
|
+
/**
|
|
215
|
+
* Voice W2 (segmented recordings): a client-set 0-based index marking this
|
|
216
|
+
* attachment as one segment of a longer recording sliced into ~10-min parts,
|
|
217
|
+
* all linked on ONE note. When present, the legacy in-body path targets this
|
|
218
|
+
* part's markers (`… (part N)._`, N = segment_index + 1) rather than the bare
|
|
219
|
+
* ones — making per-part ordering structurally guaranteed. See `markersFor`.
|
|
220
|
+
*/
|
|
221
|
+
segment_index?: number;
|
|
198
222
|
[k: string]: unknown;
|
|
199
223
|
}
|
|
200
224
|
|
|
@@ -357,17 +381,28 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
357
381
|
* attachment failure we're trying to record.
|
|
358
382
|
*
|
|
359
383
|
* Body policy (finding F — never destroy content):
|
|
360
|
-
* -
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
* -
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
384
|
+
* - Pending marker PRESENT → surgical replace of the pending marker with
|
|
385
|
+
* the failure marker. The `![[memo]]` embed + any surrounding text
|
|
386
|
+
* survive. For a segmented attachment (`segment_index` set) this is the
|
|
387
|
+
* per-part `_Transcript pending (part N)._`; otherwise the bare marker.
|
|
388
|
+
* - Failure marker ALREADY PRESENT → no-op (idempotent; a double-terminal-
|
|
389
|
+
* failure must not stack markers).
|
|
390
|
+
* - Otherwise (pending marker absent — the user edited the note while it
|
|
391
|
+
* was pending) → APPEND `\n\n` + failure marker to the existing content.
|
|
392
|
+
* The old code full-replaced the body here, destroying the embed AND the
|
|
393
|
+
* user's edits. We append instead so nothing is lost. If the content is
|
|
394
|
+
* empty, the marker alone becomes the body (avoids a leading blank line).
|
|
369
395
|
*/
|
|
370
|
-
async function applyFailureMarker(
|
|
396
|
+
async function applyFailureMarker(
|
|
397
|
+
store: Store,
|
|
398
|
+
noteId: string,
|
|
399
|
+
segmentIndex: number | undefined,
|
|
400
|
+
): Promise<void> {
|
|
401
|
+
// Bare markers by default; this segment's `(part N)` markers when the
|
|
402
|
+
// attachment carries a `segment_index` (voice W2). String-search replace
|
|
403
|
+
// targets the FIRST occurrence (a canonical body holds exactly one), and
|
|
404
|
+
// the includes-guard below keeps a repeated terminal failure from stacking.
|
|
405
|
+
const { pending, unavailable } = markersFor(segmentIndex);
|
|
371
406
|
// OC-guarded (vault#435): the read-transform-write below is re-run against
|
|
372
407
|
// fresh content on a conflict so a concurrent user edit isn't clobbered.
|
|
373
408
|
// The transform is pure w.r.t. the note it's handed; the stub-set and
|
|
@@ -381,17 +416,24 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
381
416
|
if (noteMeta.transcribe_stub !== true) return null;
|
|
382
417
|
|
|
383
418
|
let body: string;
|
|
384
|
-
if (
|
|
385
|
-
|
|
386
|
-
|
|
419
|
+
if (note.content.includes(pending)) {
|
|
420
|
+
// Function replacer so the search string is treated literally and the
|
|
421
|
+
// (fixed) failure marker is inserted verbatim.
|
|
422
|
+
body = note.content.replace(pending, () => unavailable);
|
|
423
|
+
} else if (note.content.includes(unavailable)) {
|
|
387
424
|
// Marker already present — nothing to do. Clear the stub and
|
|
388
425
|
// return without rewriting the body so we don't stack markers.
|
|
389
426
|
body = note.content;
|
|
390
427
|
} else {
|
|
391
428
|
body = note.content.length > 0
|
|
392
|
-
? `${note.content}\n\n${
|
|
393
|
-
:
|
|
429
|
+
? `${note.content}\n\n${unavailable}`
|
|
430
|
+
: unavailable;
|
|
394
431
|
}
|
|
432
|
+
// Segmented: the stub is SHARED across this note's parts — keep it set
|
|
433
|
+
// so sibling parts still resolve their own slots. Return content only
|
|
434
|
+
// (leave note metadata untouched). Un-segmented: clear the one-shot
|
|
435
|
+
// stub as before (byte-unchanged).
|
|
436
|
+
if (segmentIndex !== undefined) return { content: body };
|
|
395
437
|
const { transcribe_stub: _drop, ...restMeta } = noteMeta;
|
|
396
438
|
return { content: body, metadata: restMeta };
|
|
397
439
|
},
|
|
@@ -449,6 +491,12 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
449
491
|
// vs. the legacy stub-patching path (Lens flow). Auto-write notes also
|
|
450
492
|
// surface failures so the user can retry from the transcript note.
|
|
451
493
|
const isAutoOrigin = meta.transcribe_origin === "auto";
|
|
494
|
+
// Voice W2: when this attachment is one segment of a longer recording
|
|
495
|
+
// (client-set `segment_index`), the legacy in-body path targets this
|
|
496
|
+
// part's markers instead of the bare ones. Undefined for un-segmented
|
|
497
|
+
// attachments — byte-unchanged behavior. Only the legacy path consults it;
|
|
498
|
+
// the auto/transcript-note path is untouched (segments are a memo concern).
|
|
499
|
+
const segmentIndex = segmentIndexOf(meta);
|
|
452
500
|
|
|
453
501
|
// Honor backoff — we re-check here in case another tick queued this
|
|
454
502
|
// attachment between the listing and now.
|
|
@@ -469,7 +517,7 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
469
517
|
if (isAutoOrigin) {
|
|
470
518
|
await writeFailureTranscriptNote(store, attachment, "audio file not found", undefined, undefined);
|
|
471
519
|
} else {
|
|
472
|
-
await applyFailureMarker(store, attachment.noteId);
|
|
520
|
+
await applyFailureMarker(store, attachment.noteId, segmentIndex);
|
|
473
521
|
}
|
|
474
522
|
return;
|
|
475
523
|
}
|
|
@@ -527,7 +575,7 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
527
575
|
if (isAutoOrigin) {
|
|
528
576
|
await writeFailureTranscriptNote(store, attachment, errMsg, apiErr?.code, undefined);
|
|
529
577
|
} else {
|
|
530
|
-
await applyFailureMarker(store, attachment.noteId);
|
|
578
|
+
await applyFailureMarker(store, attachment.noteId, segmentIndex);
|
|
531
579
|
}
|
|
532
580
|
// retention=never drops the audio on any terminal state, including
|
|
533
581
|
// failure. The user opted in to "I don't want the audio kept around
|
|
@@ -578,6 +626,16 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
578
626
|
// before the transcript arrives opts out of the overwrite. OC-guarded
|
|
579
627
|
// (vault#435): re-applied against fresh content on a conflict so a
|
|
580
628
|
// concurrent user edit isn't clobbered.
|
|
629
|
+
//
|
|
630
|
+
// Success replaces whichever of THIS part's markers is present (bare, or
|
|
631
|
+
// `(part N)` when segmented). Built with no `/g` flag so `.replace`
|
|
632
|
+
// swaps only the FIRST match — a canonical capture body holds exactly
|
|
633
|
+
// one marker per part; alternation preserves positional-first semantics
|
|
634
|
+
// (a retried success replaces the failure marker where a first-try
|
|
635
|
+
// success replaced the pending one) so byte-for-byte matching today's
|
|
636
|
+
// un-segmented behavior.
|
|
637
|
+
const { pending, unavailable } = markersFor(segmentIndex);
|
|
638
|
+
const successTarget = new RegExp(`${escapeRegExp(pending)}|${escapeRegExp(unavailable)}`);
|
|
581
639
|
await applyNoteTransformWithOC(
|
|
582
640
|
store,
|
|
583
641
|
attachment.noteId,
|
|
@@ -586,28 +644,31 @@ export function startTranscriptionWorker(opts: TranscriptionWorkerOpts): Transcr
|
|
|
586
644
|
const noteMeta = (note.metadata as Record<string, unknown> | undefined) ?? {};
|
|
587
645
|
if (noteMeta.transcribe_stub !== true) return null;
|
|
588
646
|
// Body policy (finding F — never destroy content):
|
|
589
|
-
// -
|
|
590
|
-
//
|
|
591
|
-
// unavailable._` marker, landing exactly where a first-try
|
|
592
|
-
// success would). The embed + surrounding capture body survive.
|
|
647
|
+
// - pending OR failure marker present → surgical replace in place.
|
|
648
|
+
// The embed + surrounding capture body survive.
|
|
593
649
|
// - neither present (user edited the note while pending) → APPEND
|
|
594
650
|
// the transcript instead of full-replacing the body, so the
|
|
595
651
|
// user's edits + the `![[memo]]` embed are preserved. The old
|
|
596
652
|
// code full-replaced here, which destroyed both.
|
|
597
653
|
let body: string;
|
|
598
|
-
if (
|
|
654
|
+
if (successTarget.test(note.content)) {
|
|
599
655
|
// Function replacer, NOT a string — speech-to-text is arbitrary
|
|
600
656
|
// user content, and String.replace treats `$&`, `$\``, `$'`,
|
|
601
657
|
// `$1`-`$9` as special patterns in a string replacement. A
|
|
602
658
|
// transcript containing `$&` would otherwise inject the matched
|
|
603
659
|
// marker text into the body. `() => transcript` returns the text
|
|
604
660
|
// verbatim.
|
|
605
|
-
body = note.content.replace(
|
|
661
|
+
body = note.content.replace(successTarget, () => transcript);
|
|
606
662
|
} else {
|
|
607
663
|
body = note.content.length > 0
|
|
608
664
|
? `${note.content}\n\n${transcript}`
|
|
609
665
|
: transcript;
|
|
610
666
|
}
|
|
667
|
+
// Segmented: the stub is SHARED across this note's parts — keep it
|
|
668
|
+
// set so sibling parts still resolve their own slots. Return content
|
|
669
|
+
// only (leave note metadata untouched). Un-segmented: clear the
|
|
670
|
+
// one-shot stub as before (byte-unchanged).
|
|
671
|
+
if (segmentIndex !== undefined) return { content: body };
|
|
611
672
|
const { transcribe_stub: _drop, ...restMeta } = noteMeta;
|
|
612
673
|
return { content: body, metadata: restMeta };
|
|
613
674
|
},
|