Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: chain manuscript section saves instead of awaiting the in-flight one (#3399)

[._.]/ Adam Eivy4b34051eee4e
diff --git a/client/src/pages/PipelineManuscriptEditor.jsx b/client/src/pages/PipelineManuscriptEditor.jsx
index 8aca3e0915..517036951c 100644
--- a/client/src/pages/PipelineManuscriptEditor.jsx
+++ b/client/src/pages/PipelineManuscriptEditor.jsx
@@ -430,21 +430,18 @@ export default function PipelineManuscriptEditor() {
const setSectionContent = (issueId, content) => patchSection(issueId, { content });
- // Saves in flight, keyed by section. Two paths can target the same section at
- // once — the onBlur save and the flush below (clicking the format switcher
- // blurs the textarea, then immediately asks to swap formats) — and firing both
- // PATCHes would snapshot a redundant version. Queuing behind the in-flight
- // save lets the second call re-read the baseline and no-op once the first
- // persisted that same text.
+ // Tail of each section's save chain, keyed by section. Two paths can target
+ // the same section at once — the onBlur save and the flush below (clicking the
+ // format switcher blurs the textarea, then immediately asks to swap formats) —
+ // and firing both PATCHes would snapshot a redundant version.
const pendingSaves = useRef(new Map());
- const saveSectionContent = async (section, content) => {
- const key = baselineKey(section);
- const prior = pendingSaves.current.get(key);
- if (prior) await prior;
+ const persistSection = async (section, key, content) => {
+ // Re-checked HERE, not before queueing: by the time this link runs, the save
+ // ahead of it may have already persisted this exact text.
if (baselineRef.current.get(key) === content) return;
setSaveState((prev) => ({ ...prev, [section.issueId]: 'saving' }));
- const run = savePipelineManuscriptSection(
+ const result = await savePipelineManuscriptSection(
seriesId,
section.issueId,
{ stageId: section.stageId, output: content },
@@ -453,9 +450,6 @@ export default function PipelineManuscriptEditor() {
toast.error(err.message || 'Failed to save manuscript edit');
return null;
});
- pendingSaves.current.set(key, run);
- const result = await run;
- if (pendingSaves.current.get(key) === run) pendingSaves.current.delete(key);
if (result?.section) {
setBaseline(key, result.section.content);
patchSection(section.issueId, { versions: result.section.versions });
@@ -464,6 +458,23 @@ export default function PipelineManuscriptEditor() {
return result;
};
+ // Chain onto the section's tail rather than awaiting the in-flight save: two
+ // callers that both wait on the SAME in-flight save would each wake up seeing
+ // a stale baseline and fire their own PATCH for identical text. Each call
+ // extends the chain, so every link re-checks against what the link before it
+ // persisted. `link` swallows rejections so one failure can't poison the tail.
+ const saveSectionContent = (section, content) => {
+ const key = baselineKey(section);
+ const prior = pendingSaves.current.get(key) || Promise.resolve();
+ const run = prior.then(() => persistSection(section, key, content));
+ const link = run.catch(() => null);
+ pendingSaves.current.set(key, link);
+ link.then(() => {
+ if (pendingSaves.current.get(key) === link) pendingSaves.current.delete(key);
+ });
+ return run;
+ };
+
// Persist every pending onBlur edit before an action that swaps `sections`
// out from under them. Returns false when a save FAILED, so the caller aborts
// rather than discarding the user's text — `undefined` means the save was a
diff --git a/client/src/pages/PipelineManuscriptEditor.test.jsx b/client/src/pages/PipelineManuscriptEditor.test.jsx
index e36563fd4c..016030b01a 100644
--- a/client/src/pages/PipelineManuscriptEditor.test.jsx
+++ b/client/src/pages/PipelineManuscriptEditor.test.jsx
@@ -308,6 +308,38 @@ describe('PipelineManuscriptEditor', () => {
expect(api.savePipelineManuscriptSection).toHaveBeenCalledTimes(1);
});
+ it('does not double-PATCH text edited while an earlier save is still in flight (#3399)', async () => {
+ mockBothFormats();
+ let resolveFirst;
+ const echoSave = (_s, _i, body) => Promise.resolve({
+ section: { issueId: 'iss-1', number: 1, title: 'One', stageId: 'prose', content: body.output, versions: [] },
+ });
+ api.savePipelineManuscriptSection
+ .mockImplementationOnce(() => new Promise((res) => { resolveFirst = res; }))
+ .mockImplementation(echoSave);
+ renderEditor();
+ const ta = await screen.findByDisplayValue('The hero walked in. She left.');
+
+ // Save A goes out and stays in flight…
+ fireEvent.change(ta, { target: { value: 'first edit' } });
+ fireEvent.blur(ta);
+ await waitFor(() => expect(api.savePipelineManuscriptSection).toHaveBeenCalledTimes(1));
+
+ // …while the user types more and then switches formats. Both the blur save
+ // and the switch's flush queue behind A with the SAME text — waiting on A
+ // instead of chaining would wake both with a stale baseline and PATCH twice.
+ fireEvent.change(ta, { target: { value: 'second edit' } });
+ fireEvent.blur(ta);
+ fireEvent.click(screen.getByText('Teleplay'));
+
+ await act(async () => { resolveFirst(await echoSave(null, null, { output: 'first edit' })); });
+ expect(await screen.findByDisplayValue('INT. ROOM - DAY')).toBeInTheDocument();
+ expect(api.savePipelineManuscriptSection).toHaveBeenCalledTimes(2); // A, then one for 'second edit'
+ expect(api.savePipelineManuscriptSection).toHaveBeenLastCalledWith(
+ 'ser-1', 'iss-1', { stageId: 'prose', output: 'second edit' }, { silent: true },
+ );
+ });
+
it('aborts a format switch when the pending edit fails to save (#3399)', async () => {
api.savePipelineManuscriptSection.mockResolvedValue(null);
renderEditor();