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([story-builder-sse-progress]): coalesce step runs by request signature, not just op

[._.]/ Adam Eivy5b52a678bf8c
diff --git a/client/src/pages/StoryBuilder.jsx b/client/src/pages/StoryBuilder.jsx
index cb7d751f5e..5a57d9e829 100644
--- a/client/src/pages/StoryBuilder.jsx
+++ b/client/src/pages/StoryBuilder.jsx
@@ -429,12 +429,12 @@ function useStepStream(sessionId, stepId) {
const res = await kickoff().catch((err) => { handlers.onError?.(err); return null; });
setStarting(false);
if (!res) { setPhase(''); setOp(null); return; }
- // The kickoff coalesced onto a different-op run already in flight for this
- // step (e.g. a refine landing on an in-flight generate). Both ops persist to
- // the same records, so binding THIS button's success handler to that run's
- // terminal frame would misreport ("Refined" when a generate finished). Don't
- // subscribe — report the conflict and leave the live run untouched.
- if (res.alreadyRunning && res.op && res.op !== nextOp) {
+ // The kickoff collided with a DIFFERENT in-flight request for this step (a
+ // different op, or a refine of a different target/note). That run persists to
+ // the same records, so binding THIS button's success handler to its terminal
+ // frame would misreport. Don't subscribe — report it and leave the run alone.
+ // (A same-work re-click returns alreadyRunning without conflict and re-attaches.)
+ if (res.conflict) {
setPhase(''); setOp(null);
handlers.onError?.(new Error('Another operation is already running for this step — try again once it finishes.'));
return;
diff --git a/server/services/storyBuilderRunner.js b/server/services/storyBuilderRunner.js
index cdff72963e..826aebdc19 100644
--- a/server/services/storyBuilderRunner.js
+++ b/server/services/storyBuilderRunner.js
@@ -55,22 +55,32 @@ function broadcast(key, payload) {
* model, fromDownstream for generate; feedback, entryId, providerId, model for
* refine) plus an `onProgress` callback the conductor uses to emit phase frames.
*/
+// Identity of a kickoff request: what work it would actually do. Two clicks
+// with the same signature run the same work, so the second can safely attach to
+// the first's in-flight run; a different signature would do different work, so
+// it must NOT bind onto the running one (else its success handler fires off
+// another request's terminal frame — e.g. refining character B while character
+// A's refine is in flight would toast "Refined B" for A's run).
+const requestSig = (op, { entryId, feedback, fromDownstream } = {}) =>
+ JSON.stringify({ op, entryId: entryId ?? null, feedback: feedback ?? null, fromDownstream: !!fromDownstream });
+
export function startStepRun(sessionId, stepId, { op = 'generate', ...options } = {}) {
const key = runKey(sessionId, stepId);
- // Coalesce a second click onto the in-flight run, but surface that run's `op`
- // so the caller can tell a same-op re-click (reload mid-run — safe to attach)
- // from a different-op collision (a refine landing on an in-flight generate).
- // The two ops persist to the same records, so the client must NOT bind a
- // refine's success handler to a generate's terminal frame.
+ const sig = requestSig(op, options);
+ // Coalesce a second click onto the in-flight run only when it's doing the SAME
+ // work (matching signature) — a reload mid-run re-attaching to its own request.
+ // A different signature (different op, or a refine of a different target/note)
+ // would do different work, so we refuse to coalesce and report `conflict` so
+ // the client doesn't bind its success handler to the running request's frame.
//
- // Only coalesce onto a run that's still WORKING. A completed run lingers in
- // `runs` for the SSE replay grace window (closeJobAfterDelay); coalescing onto
- // it would re-run no work and replay the stale terminal frame as a false
- // success. A fresh kickoff in that window starts a new run that replaces the
- // lingering record under the same key.
+ // Only a still-WORKING run blocks. A completed run lingers in `runs` for the
+ // SSE replay grace window (closeJobAfterDelay); coalescing onto it would re-run
+ // no work and replay the stale terminal frame as a false success. A fresh
+ // kickoff in that window starts a new run that replaces the lingering record.
const inFlight = runs.get(key);
if (inFlight && !inFlight.done) {
- return { runId: inFlight.runId, alreadyRunning: true, op: inFlight.op };
+ if (inFlight.sig === sig) return { runId: inFlight.runId, alreadyRunning: true };
+ return { runId: inFlight.runId, alreadyRunning: true, conflict: true, op: inFlight.op };
}
const runId = randomUUID();
@@ -81,6 +91,7 @@ export function startStepRun(sessionId, stepId, { op = 'generate', ...options }
startedAt: new Date().toISOString(),
stepId,
op,
+ sig,
done: false,
};
runs.set(key, record);
diff --git a/server/services/storyBuilderRunner.test.js b/server/services/storyBuilderRunner.test.js
index 620c81c5a9..c4b49ae365 100644
--- a/server/services/storyBuilderRunner.test.js
+++ b/server/services/storyBuilderRunner.test.js
@@ -76,21 +76,39 @@ describe('storyBuilderRunner', () => {
await flush();
});
- it('surfaces the in-flight op when a different op coalesces onto the same step', async () => {
+ it('flags a conflict when a different-op request collides with an in-flight run', async () => {
let resolve;
conductor.generateStep.mockImplementation(() => new Promise((r) => { resolve = () => r({}); }));
startStepRun('stb-1', 'plotArc', { op: 'generate' });
// A refine click lands on the same step while the generate is in flight: it
- // coalesces but must report the live op so the client refuses to bind its
+ // must report `conflict` + the live op so the client refuses to bind its
// "Refined" handler to the generate's terminal frame.
const collision = startStepRun('stb-1', 'plotArc', { op: 'refine', feedback: 'x' });
expect(collision.alreadyRunning).toBe(true);
+ expect(collision.conflict).toBe(true);
expect(collision.op).toBe('generate');
expect(conductor.refineStep).not.toHaveBeenCalled();
resolve();
await flush();
});
+ it('flags a conflict when a refine of a DIFFERENT target collides, but re-attaches the same one', async () => {
+ let resolve;
+ conductor.refineStep.mockImplementation(() => new Promise((r) => { resolve = () => r({}); }));
+ const first = startStepRun('stb-1', 'characters', { op: 'refine', entryId: 'char-A' });
+ // Same target + op → same work → safe re-attach (no conflict, same runId).
+ const same = startStepRun('stb-1', 'characters', { op: 'refine', entryId: 'char-A' });
+ expect(same.alreadyRunning).toBe(true);
+ expect(same.conflict).toBeUndefined();
+ expect(same.runId).toBe(first.runId);
+ // Different target → different work → conflict, must not bind onto char-A's run.
+ const other = startStepRun('stb-1', 'characters', { op: 'refine', entryId: 'char-B' });
+ expect(other.conflict).toBe(true);
+ expect(conductor.refineStep).toHaveBeenCalledTimes(1);
+ resolve();
+ await flush();
+ });
+
it('surfaces a refine failure as an error frame', async () => {
conductor.refineStep.mockRejectedValue(new Error('LLM down'));
startStepRun('stb-2', 'universeAesthetic', { op: 'refine', feedback: 'tighter' });