TheYuchen commited on
Commit
1cd2cb9
·
1 Parent(s): 130ee8d

Provenance state: no false forks, no stale pins, one-shot board replicates

Browse files

- clearGraph / loadPreset now clear restoredFrom, pinnedSeed,
precisionTarget and editorContext (mirroring the New-configuration
bridge, which also clears precisionTarget now): a preset or cleared
canvas is not a fork of whatever was restored before, and must not
replay an unrelated pinned seed.
- restoredFrom is consumed after the FIRST post-restore run archives:
only that run is the fork; later runs no longer inherit a false
forked_from lineage.
- The restore consumer writes the pin unconditionally — including
null — so board "Open" clears a stale pin from an unrelated replay.
- Board "+3 replicates" no longer mutates global toolbar state: a new
one-shot replicateOnce field rides the pendingRestore -> auto-run
bridge and is consumed by that single run; replicateCount is never
written.
- Palette-add appends nodes only: when the canvas already has edges
(hand-drawn or restored) it no longer recomputes the whole edge set
(which silently destroyed hand wiring) — it says "wire it in
manually or press Auto-connect". setEdges/setNotice hoisted out of
the setNodes updater (StrictMode double-invoke).

frontend/src/components/FlowCanvas.tsx CHANGED
@@ -315,9 +315,21 @@ export function FlowCanvas() {
315
  };
316
  });
317
 
318
- setNodes((ns) => {
319
- const merged = [...ns, ...newNodes];
320
- const result = autoConnect(merged, [], useApp.getState().plugins);
 
 
 
 
 
 
 
 
 
 
 
 
321
  if (result.connected) {
322
  setEdges(result.edges);
323
  setNotice(
@@ -333,8 +345,7 @@ export function FlowCanvas() {
333
  },
334
  );
335
  }
336
- return merged;
337
- });
338
 
339
  requestAnimationFrame(() => fitView({ padding: 0.2, duration: 300, maxZoom: 1 }));
340
  }, [pendingBlockKinds]); // eslint-disable-line react-hooks/exhaustive-deps
@@ -741,6 +752,15 @@ export function FlowCanvas() {
741
  setEdges(g.edges);
742
  setRun(null);
743
  setNotice(null);
 
 
 
 
 
 
 
 
 
744
  // Sample selection: an explicit override beats the preset's
745
  // defaultCircuit, which beats keeping the current sample. Skip the network round-trip if we're already on
746
  // the requested sample.
@@ -771,6 +791,13 @@ export function FlowCanvas() {
771
  setEdges([]);
772
  setRun(null);
773
  setNotice(null);
 
 
 
 
 
 
 
774
  };
775
 
776
  // Share flow for the toolbar's ⋯ menu (Share lives there at every
@@ -930,6 +957,7 @@ export function FlowCanvas() {
930
  setEdges([]);
931
  setRun(null);
932
  setPinnedSeed(null);
 
933
  useApp.getState().setRestoredFrom(null);
934
  setNotice({
935
  text: "New configuration — drag blocks from the strip above or pick a preset, then Run to archive its first evidence.",
@@ -954,6 +982,7 @@ export function FlowCanvas() {
954
  sourceRunId,
955
  precisionTarget: restoredTarget,
956
  autoRunAfter,
 
957
  } = pendingRestore;
958
  useApp.getState().clearRestore();
959
 
@@ -1008,7 +1037,10 @@ export function FlowCanvas() {
1008
  }, 1400);
1009
  setRun(null);
1010
  useApp.getState().setRestoredFrom(sourceRunId);
1011
- if (pinSeed != null) setPinnedSeed(pinSeed);
 
 
 
1012
  // The optional-stopping target is part of the restored run's
1013
  // configuration in BOTH modes: a pinned replay of an early-stopped
1014
  // run must re-send it to reproduce the stopping point bit-exactly,
@@ -1026,7 +1058,10 @@ export function FlowCanvas() {
1026
  // Scenario boot: the graph is on the canvas and the right
1027
  // circuit is loaded — NOW it is safe to request the auto
1028
  // run (see the pendingAutoRun consumer below).
1029
- useApp.getState().requestAutoRun(sk);
 
 
 
1030
  return;
1031
  }
1032
  setNotice({
@@ -1052,7 +1087,7 @@ export function FlowCanvas() {
1052
  // eslint-disable-next-line react-hooks/exhaustive-deps
1053
  }, [pendingRestore]);
1054
 
1055
- const runPipeline = async () => {
1056
  if (!circuit) {
1057
  // Open the left pane so the user can see where to pick a circuit.
1058
  useApp.getState().bumpHintExpandLeftPane();
@@ -1160,6 +1195,11 @@ export function FlowCanvas() {
1160
  precisionTarget: useApp.getState().precisionTarget,
1161
  });
1162
  await saveRun(record);
 
 
 
 
 
1163
  useApp.getState().setLastConfigHash(record.config_hash);
1164
  useApp.getState().bumpHistoryVersion();
1165
  } catch (e) {
@@ -1169,7 +1209,7 @@ export function FlowCanvas() {
1169
 
1170
  // Replicates only make sense for fresh draws; under a pinned seed
1171
  // every repeat would return the identical numbers.
1172
- const count = pinnedSeed != null ? 1 : replicateCount;
1173
  try {
1174
  for (let i = 0; i < count; i++) {
1175
  if (count > 1) {
@@ -1236,8 +1276,9 @@ export function FlowCanvas() {
1236
  )
1237
  return;
1238
  autoRunFiredRef.current = true;
 
1239
  useApp.getState().clearAutoRun();
1240
- void runPipeline();
1241
  // eslint-disable-next-line react-hooks/exhaustive-deps -- runPipeline is stable-enough (reads live state via closures/store)
1242
  }, [pendingAutoRun, circuit, sampleKey, nodes, running]);
1243
 
@@ -1495,7 +1536,7 @@ export function FlowCanvas() {
1495
  md-gates, so no hidden/md:flex here — on mobile the
1496
  bottom-right Run FAB takes over. */}
1497
  <button
1498
- onClick={runPipeline}
1499
  disabled={running || !circuit}
1500
  className="btn-primary h-8 disabled:opacity-50 disabled:cursor-not-allowed"
1501
  aria-label={running ? "Running" : "Run pipeline"}
@@ -1589,7 +1630,7 @@ export function FlowCanvas() {
1589
  so it stays clear of the iOS home indicator. */}
1590
  <button
1591
  type="button"
1592
- onClick={runPipeline}
1593
  disabled={running || !circuit}
1594
  className="md:hidden absolute right-4 z-20 flex items-center gap-2 px-4 py-3 rounded-full bg-accent text-canvas shadow-lg disabled:opacity-50 disabled:cursor-not-allowed font-medium text-sm active:scale-95 transition-transform"
1595
  style={{
 
315
  };
316
  });
317
 
318
+ // Append only — never touch existing nodes/edges inside the
319
+ // updater. Side effects (setEdges/setNotice) are hoisted OUT of
320
+ // the setNodes updater: StrictMode double-invokes updaters, so a
321
+ // setEdges inside one fires twice.
322
+ setNodes((ns) => [...ns, ...newNodes]);
323
+ if (edges.length > 0) {
324
+ // The canvas already has wiring (hand-drawn or restored).
325
+ // Recomputing the whole edge set would silently destroy it —
326
+ // leave the new blocks unwired and say so.
327
+ setNotice({
328
+ text: `Added ${kinds.length} block${kinds.length > 1 ? "s" : ""} — wire ${kinds.length > 1 ? "them" : "it"} in manually or press Auto-connect.`,
329
+ tone: "ok",
330
+ });
331
+ } else {
332
+ const result = autoConnect([...nodes, ...newNodes], [], plugins);
333
  if (result.connected) {
334
  setEdges(result.edges);
335
  setNotice(
 
345
  },
346
  );
347
  }
348
+ }
 
349
 
350
  requestAnimationFrame(() => fitView({ padding: 0.2, duration: 300, maxZoom: 1 }));
351
  }, [pendingBlockKinds]); // eslint-disable-line react-hooks/exhaustive-deps
 
752
  setEdges(g.edges);
753
  setRun(null);
754
  setNotice(null);
755
+ // Provenance hygiene: a preset is a NEW definition, not a fork of
756
+ // whatever was restored before. Drop the restored parentage, the
757
+ // pinned seed, the stopping rule and the editor framing, or the
758
+ // next archived run would claim a false lineage / replay a stale
759
+ // seed that has nothing to do with this graph.
760
+ setPinnedSeed(null);
761
+ setPrecisionTarget(null);
762
+ useApp.getState().setRestoredFrom(null);
763
+ useApp.getState().setEditorContext(null);
764
  // Sample selection: an explicit override beats the preset's
765
  // defaultCircuit, which beats keeping the current sample. Skip the network round-trip if we're already on
766
  // the requested sample.
 
791
  setEdges([]);
792
  setRun(null);
793
  setNotice(null);
794
+ // Same provenance hygiene as loadPreset: a cleared canvas is not a
795
+ // fork of anything and carries no pinned seed / stopping rule /
796
+ // card identity (mirrors the New-configuration bridge below).
797
+ setPinnedSeed(null);
798
+ setPrecisionTarget(null);
799
+ useApp.getState().setRestoredFrom(null);
800
+ useApp.getState().setEditorContext(null);
801
  };
802
 
803
  // Share flow for the toolbar's ⋯ menu (Share lives there at every
 
957
  setEdges([]);
958
  setRun(null);
959
  setPinnedSeed(null);
960
+ setPrecisionTarget(null);
961
  useApp.getState().setRestoredFrom(null);
962
  setNotice({
963
  text: "New configuration — drag blocks from the strip above or pick a preset, then Run to archive its first evidence.",
 
982
  sourceRunId,
983
  precisionTarget: restoredTarget,
984
  autoRunAfter,
985
+ replicateOnce,
986
  } = pendingRestore;
987
  useApp.getState().clearRestore();
988
 
 
1037
  }, 1400);
1038
  setRun(null);
1039
  useApp.getState().setRestoredFrom(sourceRunId);
1040
+ // Always write the pin — INCLUDING null: restoring a fresh-draw
1041
+ // run (board Open) must clear any stale pin left by an unrelated
1042
+ // earlier replay, or the next run silently replays the wrong seed.
1043
+ setPinnedSeed(pinSeed);
1044
  // The optional-stopping target is part of the restored run's
1045
  // configuration in BOTH modes: a pinned replay of an early-stopped
1046
  // run must re-send it to reproduce the stopping point bit-exactly,
 
1058
  // Scenario boot: the graph is on the canvas and the right
1059
  // circuit is loaded — NOW it is safe to request the auto
1060
  // run (see the pendingAutoRun consumer below).
1061
+ useApp.getState().requestAutoRun(
1062
+ sk,
1063
+ replicateOnce != null ? { replicateOnce } : undefined,
1064
+ );
1065
  return;
1066
  }
1067
  setNotice({
 
1087
  // eslint-disable-next-line react-hooks/exhaustive-deps
1088
  }, [pendingRestore]);
1089
 
1090
+ const runPipeline = async (opts?: { replicateOnce?: number }) => {
1091
  if (!circuit) {
1092
  // Open the left pane so the user can see where to pick a circuit.
1093
  useApp.getState().bumpHintExpandLeftPane();
 
1195
  precisionTarget: useApp.getState().precisionTarget,
1196
  });
1197
  await saveRun(record);
1198
+ // forked_from is a one-shot claim: only the FIRST run archived
1199
+ // after a restore descends from the restored run. Consume it so
1200
+ // later runs of this canvas don't inherit a false lineage.
1201
+ if (useApp.getState().restoredFrom != null)
1202
+ useApp.getState().setRestoredFrom(null);
1203
  useApp.getState().setLastConfigHash(record.config_hash);
1204
  useApp.getState().bumpHistoryVersion();
1205
  } catch (e) {
 
1209
 
1210
  // Replicates only make sense for fresh draws; under a pinned seed
1211
  // every repeat would return the identical numbers.
1212
+ const count = pinnedSeed != null ? 1 : (opts?.replicateOnce ?? replicateCount);
1213
  try {
1214
  for (let i = 0; i < count; i++) {
1215
  if (count > 1) {
 
1276
  )
1277
  return;
1278
  autoRunFiredRef.current = true;
1279
+ const { replicateOnce } = pendingAutoRun;
1280
  useApp.getState().clearAutoRun();
1281
+ void runPipeline(replicateOnce != null ? { replicateOnce } : undefined);
1282
  // eslint-disable-next-line react-hooks/exhaustive-deps -- runPipeline is stable-enough (reads live state via closures/store)
1283
  }, [pendingAutoRun, circuit, sampleKey, nodes, running]);
1284
 
 
1536
  md-gates, so no hidden/md:flex here — on mobile the
1537
  bottom-right Run FAB takes over. */}
1538
  <button
1539
+ onClick={() => void runPipeline()}
1540
  disabled={running || !circuit}
1541
  className="btn-primary h-8 disabled:opacity-50 disabled:cursor-not-allowed"
1542
  aria-label={running ? "Running" : "Run pipeline"}
 
1630
  so it stays clear of the iOS home indicator. */}
1631
  <button
1632
  type="button"
1633
+ onClick={() => void runPipeline()}
1634
  disabled={running || !circuit}
1635
  className="md:hidden absolute right-4 z-20 flex items-center gap-2 px-4 py-3 rounded-full bg-accent text-canvas shadow-lg disabled:opacity-50 disabled:cursor-not-allowed font-medium text-sm active:scale-95 transition-transform"
1636
  style={{
frontend/src/components/MultiverseBoard.tsx CHANGED
@@ -605,8 +605,6 @@ export function MultiverseBoard() {
605
  const setWorkspaceMode = useApp((s) => s.setWorkspaceMode);
606
  const setEditorContext = useApp((s) => s.setEditorContext);
607
  const requestNewConfig = useApp((s) => s.requestNewConfig);
608
- const setReplicateCount = useApp((s) => s.setReplicateCount);
609
- const setPinnedSeed = useApp((s) => s.setPinnedSeed);
610
  const compareIds = useApp((s) => s.compareIds);
611
  const toggleCompare = useApp((s) => s.toggleCompare);
612
 
@@ -715,10 +713,10 @@ export function MultiverseBoard() {
715
  const runThreeReplicates = (g: ConfigGroup) => {
716
  const src = g.latestOk;
717
  if (!src || src.sample_key == null) return;
718
- // Fresh draws: clear any stale pinned seed (a pin forces ×1 and
719
- // identical numbers) and ask for 3 replicates.
720
- setPinnedSeed(null);
721
- setReplicateCount(3);
722
  requestRestore({
723
  graph: src.graph,
724
  sampleKey: src.sample_key,
@@ -726,6 +724,7 @@ export function MultiverseBoard() {
726
  sourceRunId: src.run_id,
727
  precisionTarget: src.precision_target ?? null,
728
  autoRunAfter: true,
 
729
  });
730
  };
731
 
 
605
  const setWorkspaceMode = useApp((s) => s.setWorkspaceMode);
606
  const setEditorContext = useApp((s) => s.setEditorContext);
607
  const requestNewConfig = useApp((s) => s.requestNewConfig);
 
 
608
  const compareIds = useApp((s) => s.compareIds);
609
  const toggleCompare = useApp((s) => s.toggleCompare);
610
 
 
713
  const runThreeReplicates = (g: ConfigGroup) => {
714
  const src = g.latestOk;
715
  if (!src || src.sample_key == null) return;
716
+ // Fresh draws, one-shot: pinSeed:null clears any stale pin via
717
+ // the restore bridge, and replicateOnce asks THIS auto-run for 3
718
+ // replicates without mutating the global toolbar replicateCount —
719
+ // a board quick action must not rewrite the user's Run settings.
720
  requestRestore({
721
  graph: src.graph,
722
  sampleKey: src.sample_key,
 
724
  sourceRunId: src.run_id,
725
  precisionTarget: src.precision_target ?? null,
726
  autoRunAfter: true,
727
+ replicateOnce: 3,
728
  });
729
  };
730
 
frontend/src/lib/store.ts CHANGED
@@ -229,6 +229,10 @@ interface AppState {
229
  * restore consumer, so it can't race the boot-time default-
230
  * circuit load). */
231
  autoRunAfter?: boolean;
 
 
 
 
232
  } | null;
233
  requestRestore: (r: NonNullable<AppState["pendingRestore"]>) => void;
234
  clearRestore: () => void;
@@ -334,8 +338,11 @@ interface AppState {
334
  * boot-time default circuit is still loaded (restore's sample load is
335
  * async and races the default bell_state auto-load).
336
  */
337
- pendingAutoRun: { sampleKey: string | null } | null;
338
- requestAutoRun: (sampleKey: string | null) => void;
 
 
 
339
  clearAutoRun: () => void;
340
 
341
  /** Scenario bridge: which Evidence tab ResultsPane should switch to.
@@ -510,7 +517,8 @@ export const useApp = create<AppState>((set) => ({
510
  pendingTouchDrop: null,
511
  setPendingTouchDrop: (v) => set({ pendingTouchDrop: v }),
512
  pendingAutoRun: null,
513
- requestAutoRun: (sampleKey) => set({ pendingAutoRun: { sampleKey } }),
 
514
  clearAutoRun: () => set({ pendingAutoRun: null }),
515
  pendingEvidenceTab: null,
516
  setPendingEvidenceTab: (t) => set({ pendingEvidenceTab: t }),
 
229
  * restore consumer, so it can't race the boot-time default-
230
  * circuit load). */
231
  autoRunAfter?: boolean;
232
+ /** One-shot replicate count for the requested auto-run (board
233
+ * quick action "+3 replicates"). Consumed by that single run
234
+ * only — never written into the global toolbar replicateCount. */
235
+ replicateOnce?: number;
236
  } | null;
237
  requestRestore: (r: NonNullable<AppState["pendingRestore"]>) => void;
238
  clearRestore: () => void;
 
338
  * boot-time default circuit is still loaded (restore's sample load is
339
  * async and races the default bell_state auto-load).
340
  */
341
+ pendingAutoRun: { sampleKey: string | null; replicateOnce?: number } | null;
342
+ requestAutoRun: (
343
+ sampleKey: string | null,
344
+ opts?: { replicateOnce?: number },
345
+ ) => void;
346
  clearAutoRun: () => void;
347
 
348
  /** Scenario bridge: which Evidence tab ResultsPane should switch to.
 
517
  pendingTouchDrop: null,
518
  setPendingTouchDrop: (v) => set({ pendingTouchDrop: v }),
519
  pendingAutoRun: null,
520
+ requestAutoRun: (sampleKey, opts) =>
521
+ set({ pendingAutoRun: { sampleKey, ...(opts ?? {}) } }),
522
  clearAutoRun: () => set({ pendingAutoRun: null }),
523
  pendingEvidenceTab: null,
524
  setPendingEvidenceTab: (t) => set({ pendingEvidenceTab: t }),