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]): reject different-op kickoff coalescing onto an in-flight step run

[._.]/ Adam Eivy185363c71bd0
diff --git a/client/src/pages/StoryBuilder.jsx b/client/src/pages/StoryBuilder.jsx
index 7c7935e929..5fa8668eb3 100644
--- a/client/src/pages/StoryBuilder.jsx
+++ b/client/src/pages/StoryBuilder.jsx
@@ -420,6 +420,16 @@ 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) {
+ setPhase(''); setOp(null);
+ handlers.onError?.(new Error('Another operation is already running for this step — try again once it finishes.'));
+ return;
+ }
handlersRef.current = handlers;
setActive(true); // enable the SSE subscription now that the run is registered
}, [starting, active]);
diff --git a/server/services/storyBuilderRunner.js b/server/services/storyBuilderRunner.js
index 9305758a4e..f854724c1c 100644
--- a/server/services/storyBuilderRunner.js
+++ b/server/services/storyBuilderRunner.js
@@ -57,7 +57,15 @@ function broadcast(key, payload) {
*/
export function startStepRun(sessionId, stepId, { op = 'generate', ...options } = {}) {
const key = runKey(sessionId, stepId);
- if (runs.has(key)) return { runId: runs.get(key).runId, alreadyRunning: true };
+ // 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.
+ if (runs.has(key)) {
+ const existing = runs.get(key);
+ return { runId: existing.runId, alreadyRunning: true, op: existing.op };
+ }
const runId = randomUUID();
const record = {
diff --git a/server/services/storyBuilderRunner.test.js b/server/services/storyBuilderRunner.test.js
index 46ffa7e15f..540ef4d88b 100644
--- a/server/services/storyBuilderRunner.test.js
+++ b/server/services/storyBuilderRunner.test.js
@@ -76,6 +76,21 @@ describe('storyBuilderRunner', () => {
await flush();
});
+ it('surfaces the in-flight op when a different op coalesces onto the same step', 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
+ // "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.op).toBe('generate');
+ expect(conductor.refineStep).not.toHaveBeenCalled();
+ 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' });