@marimo-team/islands 0.25.3-dev388 → 0.25.3-dev391

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.
Files changed (101) hide show
  1. package/dist/{ErrorBoundary-_mFP3LkM.js → ErrorBoundary-DNRG0xHi.js} +1 -1
  2. package/dist/{chat-ui-BKSYhGMh.js → chat-ui-CWVNPm2l.js} +769 -769
  3. package/dist/{code-visibility-DkMNjdNe.js → code-visibility-Ca8sDptK.js} +10 -10
  4. package/dist/{common-Ces9ooG_.js → common-B3NYpak4.js} +4910 -4902
  5. package/dist/{data-grid-overlay-editor-uNMslVKY.js → data-grid-overlay-editor-Cz7uMmtm.js} +1 -1
  6. package/dist/{error-entries-rtPWQkJk.js → error-entries-CnXf4kpP.js} +821 -686
  7. package/dist/{feedback-button-i960qUnq.js → feedback-button-BQbnZsf5.js} +7 -7
  8. package/dist/{glide-data-editor-_EEhlGzS.js → glide-data-editor-Bxc30klU.js} +3 -3
  9. package/dist/{globals-DKkjIolp.js → globals-CDatnx0Y.js} +1 -1
  10. package/dist/{html-to-image-B5nTOeMI.js → html-to-image-Bu_-vH_Y.js} +4782 -4765
  11. package/dist/main.js +6233 -6172
  12. package/dist/{number-overlay-editor-D5U7jbrh.js → number-overlay-editor-ChGu5C-s.js} +1 -1
  13. package/dist/{process-output-DNxpsd4n.js → process-output-Rbqr1aYD.js} +2 -2
  14. package/dist/{reveal-component-BM9TozVQ.js → reveal-component-CSxWJFAP.js} +32 -32
  15. package/dist/style.css +1 -1
  16. package/package.json +1 -1
  17. package/src/__mocks__/executions.ts +48 -8
  18. package/src/__mocks__/notebook.ts +20 -42
  19. package/src/components/app-config/__tests__/add-model-discovery.test.tsx +255 -0
  20. package/src/components/app-config/__tests__/model-discovery.test.tsx +183 -0
  21. package/src/components/app-config/ai-config.tsx +20 -24
  22. package/src/components/app-config/model-discovery.tsx +118 -184
  23. package/src/components/app-config/model-provider-config.ts +25 -0
  24. package/src/components/editor/Output.tsx +5 -4
  25. package/src/components/editor/__tests__/data-attributes.test.tsx +1 -6
  26. package/src/components/editor/__tests__/dynamic-favicon.test.tsx +11 -16
  27. package/src/components/editor/__tests__/output-persistence.test.tsx +6 -24
  28. package/src/components/editor/actions/__tests__/useSetOutputsExpanded.test.ts +6 -8
  29. package/src/components/editor/actions/useSetOutputsExpanded.ts +12 -4
  30. package/src/components/editor/cell/cell-context-menu.tsx +3 -2
  31. package/src/components/editor/errors/__tests__/send-error-report-button.test.tsx +89 -72
  32. package/src/components/editor/notebook-cell.tsx +15 -10
  33. package/src/components/editor/output/console/process-output.ts +4 -2
  34. package/src/components/editor/renderers/cells-renderer.tsx +3 -1
  35. package/src/components/editor/renderers/grid-layout/__tests__/grid-layout.test.tsx +5 -11
  36. package/src/components/editor/renderers/grid-layout/grid-layout.tsx +9 -8
  37. package/src/components/editor/renderers/types.ts +2 -2
  38. package/src/components/editor/renderers/vertical-layout/utils.ts +4 -4
  39. package/src/components/editor/renderers/vertical-layout/vertical-layout.tsx +5 -8
  40. package/src/components/scratchpad/scratchpad.tsx +6 -2
  41. package/src/components/slides/__tests__/cell-output-block.test.tsx +7 -8
  42. package/src/components/slides/__tests__/minimap-actions.test.tsx +3 -3
  43. package/src/components/slides/__tests__/slide-cell-view.test.tsx +1 -0
  44. package/src/components/slides/slide-cell-view.tsx +1 -3
  45. package/src/components/slides/slide.tsx +3 -3
  46. package/src/core/ai/context/__tests__/registry.test.ts +7 -11
  47. package/src/core/ai/context/providers/__tests__/cell-output.test.ts +113 -35
  48. package/src/core/ai/context/providers/__tests__/error.test.ts +74 -191
  49. package/src/core/ai/context/providers/cell-output.ts +11 -6
  50. package/src/core/cells/__tests__/__snapshots__/cells.test.ts.snap +8 -103
  51. package/src/core/cells/__tests__/cells.test.ts +40 -292
  52. package/src/core/cells/__tests__/clear-outputs.test.ts +151 -0
  53. package/src/core/cells/__tests__/harness.ts +14 -12
  54. package/src/core/cells/__tests__/layout.test.ts +20 -26
  55. package/src/core/cells/__tests__/notebook-outline.test.ts +49 -43
  56. package/src/core/cells/__tests__/session.test.ts +26 -146
  57. package/src/core/cells/actions.ts +49 -9
  58. package/src/core/cells/cell.ts +0 -13
  59. package/src/core/cells/cells.ts +53 -62
  60. package/src/core/cells/focus.ts +6 -1
  61. package/src/core/cells/layout.ts +2 -3
  62. package/src/core/cells/outputs.ts +2 -2
  63. package/src/core/cells/session.ts +3 -38
  64. package/src/core/cells/types.ts +6 -8
  65. package/src/core/cells/utils.ts +4 -3
  66. package/src/core/codemirror/ai/__tests__/resources.test.ts +7 -11
  67. package/src/core/errors/__tests__/error-entries.test.ts +52 -18
  68. package/src/core/errors/error-entries.ts +21 -11
  69. package/src/core/executions/__tests__/freshness.test.ts +7 -4
  70. package/src/core/executions/__tests__/hooks.test.tsx +5 -1
  71. package/src/core/executions/__tests__/interrupt.test.ts +2 -0
  72. package/src/core/executions/__tests__/output-lifecycle.test.ts +201 -0
  73. package/src/core/executions/__tests__/output.test.ts +87 -3
  74. package/src/core/executions/__tests__/select.test.ts +17 -10
  75. package/src/core/executions/atoms.ts +73 -15
  76. package/src/core/executions/freshness.ts +1 -1
  77. package/src/core/executions/hooks.ts +18 -8
  78. package/src/core/executions/index.ts +6 -0
  79. package/src/core/executions/output.ts +122 -38
  80. package/src/core/executions/select.ts +2 -2
  81. package/src/core/export/__tests__/hooks.test.ts +20 -29
  82. package/src/core/export/hooks.ts +7 -9
  83. package/src/core/islands/__tests__/iframe-resizer.test.ts +91 -0
  84. package/src/core/islands/__tests__/main.test.ts +17 -0
  85. package/src/core/islands/components/__tests__/web-components.test.tsx +13 -15
  86. package/src/core/islands/components/output-wrapper.tsx +15 -20
  87. package/src/core/islands/iframe-resizer.ts +38 -0
  88. package/src/core/islands/main.ts +3 -0
  89. package/src/core/lifecycle/render-policy.ts +7 -4
  90. package/src/core/network/__tests__/ai.test.ts +41 -0
  91. package/src/core/network/ai.ts +12 -0
  92. package/src/core/notebook/document/__tests__/codemirror.test.ts +2 -0
  93. package/src/core/notebook/replica/__tests__/replica.test.ts +19 -0
  94. package/src/core/notebook/replica/__tests__/results.test.ts +45 -16
  95. package/src/core/notebook/replica/index.ts +1 -1
  96. package/src/core/notebook/replica/replica.ts +19 -4
  97. package/src/core/notebook/replica/resources/executions.ts +100 -9
  98. package/src/core/notebook/replica/resources/results.ts +46 -31
  99. package/src/core/transports/__tests__/static.test.ts +5 -3
  100. package/src/stories/cell.stories.tsx +40 -30
  101. package/src/stories/layout/vertical/one-column.stories.tsx +15 -7
@@ -3,7 +3,12 @@
3
3
  /** The active notebook's executions and runtime as the editor reads them. */
4
4
 
5
5
  import type * as Protocol from "@marimo-team/client/protocol";
6
+ import { dequal } from "dequal";
6
7
  import { atom, type createStore } from "jotai";
8
+ import { atomFamily, selectAtom } from "jotai/utils";
9
+ import type { CellId } from "@/core/cells/ids";
10
+ import type { Outline } from "@/core/cells/outline";
11
+ import { parseOutline } from "@/core/dom/outline";
7
12
  import type { CellMessage } from "@/core/kernel/messages";
8
13
  import {
9
14
  ConnectionState,
@@ -13,7 +18,13 @@ import {
13
18
  } from "@/core/notebook/replica";
14
19
  import { activeNotebookReplicaAtom } from "@/core/session/active-notebook";
15
20
  import { type CellFreshnessInputs, freshnessOf } from "./freshness";
16
- import type { CellOutputInputs } from "./output";
21
+ import {
22
+ type CellOutput,
23
+ type CellOutputInputs,
24
+ type ClearedOutput,
25
+ cellOutputsView,
26
+ shownExecution,
27
+ } from "./output";
17
28
  import {
18
29
  anyRunning,
19
30
  type CellExecutionInputs,
@@ -26,14 +37,15 @@ const NO_EXECUTIONS: Executions = {
26
37
  runs: new Map(),
27
38
  executions: new Map(),
28
39
  latest: new Map(),
40
+ results: new Map(),
41
+ scratch: null,
29
42
  };
30
43
  const NO_RESULTS: Results = new Map();
31
44
  const NO_CELLS: ReadonlyMap<Protocol.CellId, Protocol.Cell> = new Map();
32
45
  const NO_REQUESTS: ReadonlySet<RunRequest> = new Set();
33
46
  const NO_REQUESTED: ReadonlySet<Protocol.CellId> = new Set();
34
47
  const NO_BLOCKED: ReadonlySet<Protocol.CellId> = new Set();
35
- const NO_CLEARED: ReadonlyMap<Protocol.CellId, Protocol.ExecutionId> =
36
- new Map();
48
+ const NO_CLEARED: ReadonlyMap<Protocol.CellId, ClearedOutput> = new Map();
37
49
 
38
50
  /** The notebook a replica is showing, which the editor's marks belong to. */
39
51
  interface ShownNotebook {
@@ -97,7 +109,7 @@ function current<T>(
97
109
  : empty;
98
110
  }
99
111
 
100
- function shownNotebook(store: Store): ShownNotebook | null {
112
+ function shownNotebook(store: Pick<Store, "get">): ShownNotebook | null {
101
113
  const replica = store.get(activeNotebookReplicaAtom);
102
114
  const notebook = replica === null ? null : store.get(replica.notebookAtom);
103
115
  return replica === null || notebook === null
@@ -105,7 +117,7 @@ function shownNotebook(store: Store): ShownNotebook | null {
105
117
  : { replica, id: notebook.id };
106
118
  }
107
119
 
108
- function mark<T>(store: Store, value: T): Marks<T> {
120
+ function mark<T>(store: Pick<Store, "get">, value: T): Marks<T> {
109
121
  const notebook = shownNotebook(store);
110
122
  return notebook === null ? null : { notebook, value };
111
123
  }
@@ -173,36 +185,82 @@ export const cellFreshnessAtom = atom((get) =>
173
185
  freshnessOf(get(cellFreshnessInputsAtom)),
174
186
  );
175
187
 
176
- /** Results this browser cleared, by execution id, until a newer one lands. */
188
+ /** What each cell showed when this browser cleared it. */
177
189
  const clearedOutputsAtom =
178
- atom<Marks<ReadonlyMap<Protocol.CellId, Protocol.ExecutionId>>>(null);
190
+ atom<Marks<ReadonlyMap<Protocol.CellId, ClearedOutput>>>(null);
191
+
192
+ const displayedNotebookAtom = atom((get): ShownNotebook | null => {
193
+ const replica = get(activeNotebookReplicaAtom);
194
+ const document = replica === null ? null : get(replica.shownDocumentAtom);
195
+ return replica === null || document === null
196
+ ? null
197
+ : { replica, id: document.revision.notebook_id };
198
+ });
179
199
 
180
200
  /** What every cell's output derives from. Pair with `cellOutputView`. */
181
201
  export const cellOutputInputsAtom = atom((get): CellOutputInputs => {
182
202
  const replica = get(activeNotebookReplicaAtom);
183
- const notebookId =
184
- replica === null ? null : (get(replica.notebookAtom)?.id ?? null);
203
+ const notebookId = get(displayedNotebookAtom)?.id ?? null;
185
204
  return {
186
205
  executions: replica === null ? NO_EXECUTIONS : get(replica.executionsAtom),
187
206
  results: replica === null ? NO_RESULTS : get(replica.resultsAtom),
188
207
  cleared: current(get(clearedOutputsAtom), replica, notebookId, NO_CLEARED),
208
+ cells:
209
+ replica === null
210
+ ? NO_CELLS
211
+ : (get(replica.documentAtom)?.cells ?? NO_CELLS),
189
212
  };
190
213
  });
191
214
 
192
- /** Hide what `cellIds` show now, until each has a newer result. */
215
+ /** What every cell shows, by cell id. Pair with `cellOutputAtom` for one cell. */
216
+ export const cellOutputsAtom = atom((get) =>
217
+ cellOutputsView(get(cellOutputInputsAtom)),
218
+ );
219
+
220
+ /**
221
+ * One cell's output, compared by value so its readers are left alone by
222
+ * other cells' runs. Released with the cell by `releaseCellOutputAtoms`.
223
+ */
224
+ export const cellOutputAtom = atomFamily((cellId: CellId) =>
225
+ selectAtom(
226
+ cellOutputsAtom,
227
+ (outputs): CellOutput | null => outputs.get(cellId) ?? null,
228
+ dequal,
229
+ ),
230
+ );
231
+
232
+ /** The headings in a cell's output, for the table of contents and folding. */
233
+ export const cellOutlineAtom = atomFamily((cellId: CellId) =>
234
+ atom((get): Outline | null => parseOutline(get(cellOutputAtom(cellId)))),
235
+ );
236
+
237
+ /** Drop the cached atoms of a cell that left the document. */
238
+ export function releaseCellOutputAtoms(cellId: CellId): void {
239
+ cellOutputAtom.remove(cellId);
240
+ cellOutlineAtom.remove(cellId);
241
+ }
242
+
243
+ /** Hide what `cellIds` show now, until each shows something new. */
193
244
  export function clearCellOutputs(
194
- store: Store,
245
+ store: Pick<Store, "get" | "set">,
195
246
  cellIds: readonly Protocol.CellId[],
196
247
  ): void {
197
248
  const inputs = store.get(cellOutputInputsAtom);
198
249
  const next = new Map(inputs.cleared);
199
250
  for (const cellId of cellIds) {
200
- const result = inputs.results.get(cellId);
201
- if (result !== null && result !== undefined) {
202
- next.set(cellId, result.id);
251
+ const shown = shownExecution(inputs, cellId);
252
+ if (shown?.record.outcome != null) {
253
+ next.set(cellId, {
254
+ execution: shown.record.id,
255
+ version: shown.outputVersion,
256
+ });
203
257
  }
204
258
  }
205
- store.set(clearedOutputsAtom, mark(store, next));
259
+ const notebook = store.get(displayedNotebookAtom);
260
+ store.set(
261
+ clearedOutputsAtom,
262
+ notebook === null ? null : { notebook, value: next },
263
+ );
206
264
  }
207
265
 
208
266
  /** The runtime's status, null while the notebook has none or the stream is not ready. */
@@ -163,7 +163,7 @@ export function freshnessOf(inputs: CellFreshnessInputs): CellFreshnessLookup {
163
163
  }
164
164
 
165
165
  return (id) => {
166
- const result = inputs.results.get(id);
166
+ const result = inputs.results.get(id)?.record;
167
167
  return result === null || result === undefined
168
168
  ? CURRENT
169
169
  : ofRecord(result, new Set());
@@ -5,14 +5,17 @@ import { dequal } from "dequal";
5
5
  import { useAtomValue } from "jotai";
6
6
  import { selectAtom } from "jotai/utils";
7
7
  import { useMemo } from "react";
8
- import { parseOutline } from "@/core/dom/outline";
8
+ import type { CellId } from "@/core/cells/ids";
9
+ import type { Outline } from "@/core/cells/outline";
9
10
  import {
10
11
  cellExecutionInputsAtom,
11
12
  cellFreshnessAtom,
13
+ cellOutlineAtom,
14
+ cellOutputAtom,
12
15
  cellOutputInputsAtom,
13
16
  } from "./atoms";
14
17
  import type { CellFreshness } from "./freshness";
15
- import { type CellOutput, cellOutputView, scratchOutputView } from "./output";
18
+ import { type CellOutput, scratchOutputView } from "./output";
16
19
  import { type CellExecutionView, cellExecutionView } from "./select";
17
20
 
18
21
  /**
@@ -52,13 +55,21 @@ export function useCellFreshness(id: Protocol.CellId): CellFreshness {
52
55
  }
53
56
 
54
57
  /** What the cell shows: its running value so far, else its result's outcome. */
55
- export function useCellOutput(id: Protocol.CellId): CellOutput | null {
58
+ export function useCellOutput(id: CellId): CellOutput | null {
59
+ return useAtomValue(cellOutputAtom(id));
60
+ }
61
+
62
+ /**
63
+ * Whether the cell has completed an execution, even one that shows nothing.
64
+ *
65
+ * Tells a cell that ran and has no output from one that has not run yet.
66
+ */
67
+ export function useCellHasResult(id: Protocol.CellId): boolean {
56
68
  const cellAtom = useMemo(
57
69
  () =>
58
70
  selectAtom(
59
71
  cellOutputInputsAtom,
60
- (inputs) => cellOutputView(inputs, id),
61
- dequal,
72
+ (inputs) => inputs.results.get(id) != null,
62
73
  ),
63
74
  [id],
64
75
  );
@@ -66,9 +77,8 @@ export function useCellOutput(id: Protocol.CellId): CellOutput | null {
66
77
  }
67
78
 
68
79
  /** The headings in the cell's output, for the table of contents. */
69
- export function useCellOutline(id: Protocol.CellId) {
70
- const output = useCellOutput(id);
71
- return useMemo(() => parseOutline(output), [output]);
80
+ export function useCellOutline(id: CellId): Outline | null {
81
+ return useAtomValue(cellOutlineAtom(id));
72
82
  }
73
83
 
74
84
  /** What the scratchpad shows, from its latest scratch execution. */
@@ -12,11 +12,15 @@
12
12
  export {
13
13
  cellExecutionInputsAtom,
14
14
  cellFreshnessAtom,
15
+ cellOutlineAtom,
16
+ cellOutputAtom,
15
17
  cellOutputInputsAtom,
18
+ cellOutputsAtom,
16
19
  clearCellOutputs,
17
20
  notebookIsRunningAtom,
18
21
  notebookQueuedOrRunningCountAtom,
19
22
  observeCellMessage,
23
+ releaseCellOutputAtoms,
20
24
  runtimeStatusAtom,
21
25
  waitForRuntimeRunning,
22
26
  } from "./atoms";
@@ -27,6 +31,7 @@ export { runCells, syncEdits } from "./run";
27
31
  export {
28
32
  useCellExecution,
29
33
  useCellFreshness,
34
+ useCellHasResult,
30
35
  useCellOutline,
31
36
  useCellOutput,
32
37
  useScratchOutput,
@@ -34,6 +39,7 @@ export {
34
39
  export {
35
40
  type CellError,
36
41
  type CellOutput,
42
+ cellOutputsView,
37
43
  cellOutputView,
38
44
  type HostDiagnostic,
39
45
  scratchOutputView,
@@ -12,7 +12,7 @@
12
12
  import type * as Protocol from "@marimo-team/client/protocol";
13
13
  import type { CellId } from "@/core/cells/ids";
14
14
  import type { MarimoError, OutputMessage } from "@/core/kernel/messages";
15
- import type { Executions, Results } from "@/core/notebook/replica";
15
+ import type { Executions, Result, Results } from "@/core/notebook/replica";
16
16
 
17
17
  /** A host error shown as its message and the cells involved. */
18
18
  export interface HostDiagnostic {
@@ -33,14 +33,23 @@ export type CellOutput = Omit<OutputMessage, "data"> & {
33
33
  readonly data: OutputMessage["data"] | readonly CellError[];
34
34
  };
35
35
 
36
- /** What every cell's output derives from. */
36
+ /** The execution and display revision hidden by this browser. */
37
+ export interface ClearedOutput {
38
+ readonly execution: Protocol.ExecutionId;
39
+ readonly version: number;
40
+ }
41
+
37
42
  export interface CellOutputInputs {
38
43
  readonly executions: Executions;
39
44
  readonly results: Results;
40
- /** Results this browser cleared, by execution id, until a newer one lands. */
41
- readonly cleared: ReadonlyMap<Protocol.CellId, Protocol.ExecutionId>;
45
+ /** What each cell showed when this browser cleared it. */
46
+ readonly cleared: ReadonlyMap<Protocol.CellId, ClearedOutput>;
47
+ /** The confirmed document's cells, for the source an execution ran. */
48
+ readonly cells: ReadonlyMap<Protocol.CellId, Protocol.Cell>;
42
49
  }
43
50
 
51
+ const NO_CELLS: ReadonlyMap<Protocol.CellId, Protocol.Cell> = new Map();
52
+
44
53
  const ERROR_MIMETYPE = "application/vnd.marimo+error";
45
54
 
46
55
  /** The protocol's ids are plain strings; the editor brands them. */
@@ -89,9 +98,9 @@ function errorOutput(
89
98
  };
90
99
  }
91
100
 
92
- /** The cell an exception was blamed on, when the host names one. */
101
+ /** The cell an exception was blamed on, which is the one cell the host relates to it. */
93
102
  function raisingCell(error: Protocol.Error): CellId | null {
94
- const raised = error.related.find((cell) => cell.message === "raised");
103
+ const raised = error.related.at(0);
95
104
  return raised === undefined ? null : brand(raised.cell_id);
96
105
  }
97
106
 
@@ -170,8 +179,39 @@ function skippedOutput(
170
179
  ]);
171
180
  }
172
181
 
173
- /** What an execution's outcome shows, or null for an empty value. */
174
- export function outcomeOutput(record: Protocol.Execution): CellOutput | null {
182
+ /**
183
+ * The source an execution ran, while the confirmed cell still has it.
184
+ *
185
+ * Fingerprints are opaque, so the editor cannot take one of its draft. The
186
+ * confirmed cell's fingerprint names the source the host holds, which is
187
+ * the execution's own until an edit is confirmed.
188
+ */
189
+ function sourceOf(
190
+ record: Protocol.Execution,
191
+ cells: ReadonlyMap<Protocol.CellId, Protocol.Cell>,
192
+ ): string | undefined {
193
+ const cell = record.cell_id === null ? undefined : cells.get(record.cell_id);
194
+ return cell !== undefined &&
195
+ cell.code !== null &&
196
+ cell.code_fingerprint === record.code_fingerprint
197
+ ? cell.code
198
+ : undefined;
199
+ }
200
+
201
+ /**
202
+ * What an execution's outcome shows, or null for an empty value, with the
203
+ * source it ran when `cells` still holds it.
204
+ */
205
+ export function outcomeOutput(
206
+ record: Protocol.Execution,
207
+ cells: ReadonlyMap<Protocol.CellId, Protocol.Cell> = NO_CELLS,
208
+ ): CellOutput | null {
209
+ const output = outcomeDisplay(record);
210
+ const code = sourceOf(record, cells);
211
+ return output === null || code === undefined ? output : { ...output, code };
212
+ }
213
+
214
+ function outcomeDisplay(record: Protocol.Execution): CellOutput | null {
175
215
  const outcome = record.outcome;
176
216
  if (outcome === null) {
177
217
  return null;
@@ -191,52 +231,96 @@ export function outcomeOutput(record: Protocol.Execution): CellOutput | null {
191
231
  }
192
232
 
193
233
  /**
194
- * What a cell shows: its running value so far, else its result's outcome.
234
+ * The execution whose outcome a cell shows: its running one once that has
235
+ * shown something, else its result.
195
236
  *
196
237
  * A running execution's outcome is null until it shows something, so the
197
238
  * result stays up until then. Once it has shown, an empty display means
198
- * the cell cleared its output, and nothing shows.
239
+ * the cell cleared its output, and nothing shows. A result that says the
240
+ * cell did not run comes down as soon as it does run.
199
241
  */
200
- export function cellOutputView(
201
- inputs: CellOutputInputs,
242
+ export function shownExecution(
243
+ inputs: Pick<CellOutputInputs, "executions" | "results">,
202
244
  id: Protocol.CellId,
203
- ): CellOutput | null {
245
+ ): Result | null {
204
246
  const latestId = inputs.executions.latest.get(id);
205
247
  const latest =
206
248
  latestId === undefined
207
249
  ? undefined
208
- : inputs.executions.executions.get(latestId)?.record;
209
- if (latest !== undefined && latest.outcome !== null) {
210
- if (latest.status !== "completed") {
211
- return outcomeOutput(latest);
212
- }
250
+ : inputs.executions.executions.get(latestId);
251
+ if (
252
+ latest !== undefined &&
253
+ latest.record.outcome !== null &&
254
+ latest.record.status !== "completed"
255
+ ) {
256
+ return latest;
213
257
  }
214
- const result = inputs.results.get(id);
215
- if (result === null || result === undefined) {
258
+ const result = inputs.results.get(id) ?? null;
259
+ if (
260
+ latest?.record.status === "running" &&
261
+ result?.record.outcome?.kind === "skipped"
262
+ ) {
216
263
  return null;
217
264
  }
218
- if (inputs.cleared.get(id) === result.id) {
265
+ return result;
266
+ }
267
+
268
+ /**
269
+ * Whether `record` still shows what was cleared.
270
+ *
271
+ * A running execution that shows something new after the clear comes back,
272
+ * and so does a newer execution.
273
+ */
274
+ function stillCleared(
275
+ cleared: ClearedOutput | undefined,
276
+ shown: Result,
277
+ ): boolean {
278
+ return (
279
+ cleared?.execution === shown.record.id &&
280
+ cleared.version === shown.outputVersion
281
+ );
282
+ }
283
+
284
+ /** What a cell shows, unless this browser cleared it. */
285
+ export function cellOutputView(
286
+ inputs: CellOutputInputs,
287
+ id: Protocol.CellId,
288
+ ): CellOutput | null {
289
+ const shown = shownExecution(inputs, id);
290
+ if (shown === null || stillCleared(inputs.cleared.get(id), shown)) {
219
291
  return null;
220
292
  }
221
- return outcomeOutput(result);
293
+ return outcomeOutput(shown.record, inputs.cells);
222
294
  }
223
295
 
224
- /** What the scratchpad shows, from its latest scratch execution. */
225
- export function scratchOutputView(executions: Executions): CellOutput | null {
226
- let scratch: Protocol.Execution | undefined;
227
- for (const { record } of executions.executions.values()) {
228
- if (record.cell_id !== null) {
229
- continue;
230
- }
231
- if (
232
- scratch === undefined ||
233
- seconds(record.started_at) >= seconds(scratch.started_at)
234
- ) {
235
- scratch = record;
296
+ /**
297
+ * What each cell with a result or a running execution shows, by cell id.
298
+ *
299
+ * Cells showing nothing are left out, so a reader can tell "no output"
300
+ * from "has a cell" without the document.
301
+ */
302
+ export function cellOutputsView(
303
+ inputs: CellOutputInputs,
304
+ ): ReadonlyMap<CellId, CellOutput> {
305
+ const outputs = new Map<CellId, CellOutput>();
306
+ const cellIds = new Set([
307
+ ...inputs.results.keys(),
308
+ ...inputs.executions.latest.keys(),
309
+ ]);
310
+ for (const cellId of cellIds) {
311
+ const output = cellOutputView(inputs, cellId);
312
+ if (output !== null) {
313
+ outputs.set(brand(cellId), output);
236
314
  }
237
315
  }
238
- if (scratch === undefined) {
239
- return null;
240
- }
241
- return outcomeOutput(scratch);
316
+ return outputs;
317
+ }
318
+
319
+ /**
320
+ * The latest scratch display, retained until a newer execution shows one.
321
+ */
322
+ export function scratchOutputView(executions: Executions): CellOutput | null {
323
+ return executions.scratch === null
324
+ ? null
325
+ : outcomeOutput(executions.scratch.record);
242
326
  }
@@ -96,7 +96,7 @@ function awaited(inputs: CellExecutionInputs, id: Protocol.CellId): boolean {
96
96
  }
97
97
  const planned =
98
98
  latestOf(inputs.executions, id)?.record.run_id ??
99
- inputs.results.get(id)?.run_id;
99
+ inputs.results.get(id)?.record.run_id;
100
100
  for (const run of inputs.executions.runs.values()) {
101
101
  if (
102
102
  run.status !== "completed" &&
@@ -114,7 +114,7 @@ export function cellExecutionView(
114
114
  id: Protocol.CellId,
115
115
  ): CellExecutionView {
116
116
  const latest = latestOf(inputs.executions, id);
117
- const result = inputs.results.get(id) ?? null;
117
+ const result = inputs.results.get(id)?.record ?? null;
118
118
  let status: CellRunStatus = "idle";
119
119
  let startedAt = result === null ? null : millis(result.started_at);
120
120
  let rendered = false;
@@ -1,15 +1,16 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
2
 
3
+ import type * as Protocol from "@marimo-team/client/protocol";
3
4
  import { renderHook } from "@testing-library/react";
4
5
  import { createStore, Provider } from "jotai";
5
6
  import type { ReactNode } from "react";
6
7
  import * as React from "react";
7
8
  import { beforeEach, describe, expect, it, vi } from "vitest";
8
9
  import { MockModules, Mocks } from "@/__mocks__/common";
10
+ import { MockExecutions } from "@/__mocks__/executions";
9
11
  import { cellId } from "@/__tests__/branded";
10
- import type { CellId } from "@/core/cells/ids";
11
12
  import { CellOutputId } from "@/core/cells/ids";
12
- import type { CellRuntimeState } from "@/core/cells/types";
13
+ import type { CellOutput } from "@/core/executions";
13
14
  import { ProgressState } from "@/utils/progress";
14
15
  import {
15
16
  captureTracker,
@@ -26,19 +27,10 @@ vi.mock("@/utils/Logger", () => ({ Logger: Mocks.quietLogger() }));
26
27
 
27
28
  vi.mock("@/components/ui/use-toast", () => MockModules.toast());
28
29
 
29
- // Mock cellsRuntimeAtom - must be defined inline in the factory function
30
- vi.mock("@/core/cells/cells", async () => {
31
- const { atom } = await import("jotai");
32
- return {
33
- cellsRuntimeAtom: atom({}),
34
- };
35
- });
36
-
37
30
  const progress = ProgressState.indeterminate();
38
31
 
39
32
  import { toPng } from "html-to-image";
40
33
  import { toast } from "@/components/ui/use-toast";
41
- import { cellsRuntimeAtom } from "@/core/cells/cells";
42
34
  import { Logger } from "@/utils/Logger";
43
35
 
44
36
  describe("useEnrichCellOutputs", () => {
@@ -53,26 +45,25 @@ describe("useEnrichCellOutputs", () => {
53
45
  const wrapper = ({ children }: { children: ReactNode }) =>
54
46
  React.createElement(Provider, { store }, children);
55
47
 
56
- // Helper to set the mocked atom (cast to any to work around type mismatch)
57
- const setCellsRuntime = (value: Record<CellId, CellRuntimeState>) => {
58
- // oxlint-disable-next-line typescript/no-explicit-any
59
- store.set(cellsRuntimeAtom as any, value);
48
+ /** Make the host report `records` as the cells' results. */
49
+ const setCellsRuntime = (records: Protocol.Execution[]) => {
50
+ MockExecutions.seed(store, records);
60
51
  };
61
52
 
53
+ /** A completed execution per cell that shows its `output`. */
62
54
  const createMockCellRuntimes = (
63
- cells: Record<string, Partial<CellRuntimeState>>,
64
- ): Record<CellId, CellRuntimeState> => {
65
- return Object.fromEntries(
66
- Object.entries(cells).map(([cellId, cell]) => [
67
- cellId,
68
- {
69
- output: cell.output || null,
70
- stallTime: null as unknown as number,
71
- ...cell,
72
- } as CellRuntimeState,
73
- ]),
74
- ) as Record<CellId, CellRuntimeState>;
75
- };
55
+ cells: Record<string, { output: CellOutput | null }>,
56
+ ): Protocol.Execution[] =>
57
+ Object.entries(cells).flatMap(([cellId, { output }]) =>
58
+ output === null
59
+ ? []
60
+ : [
61
+ MockExecutions.shown(cellId, {
62
+ mimetype: output.mimetype,
63
+ data: String(output.data),
64
+ }),
65
+ ],
66
+ );
76
67
 
77
68
  it("should return empty object when no cells need screenshots", async () => {
78
69
  vi.spyOn(document, "getElementById");
@@ -502,7 +493,7 @@ describe("useEnrichCellOutputs", () => {
502
493
  output: null,
503
494
  },
504
495
  "cell-2": {
505
- output: undefined,
496
+ output: null,
506
497
  },
507
498
  }),
508
499
  );
@@ -9,8 +9,8 @@ import { getImageDataUrlForCell } from "@/utils/download";
9
9
  import { Logger } from "@/utils/Logger";
10
10
  import { Objects } from "@/utils/objects";
11
11
  import { ProgressState } from "@/utils/progress";
12
- import { cellsRuntimeAtom } from "../cells/cells";
13
12
  import type { CellId } from "../cells/ids";
13
+ import { cellOutputsAtom } from "../executions";
14
14
  import { getExportLayout } from "./layout";
15
15
  import { connectionAtom } from "../network/connection";
16
16
  import { useRequestClient } from "../network/requests";
@@ -122,16 +122,15 @@ interface UseEnrichCellOutputsOptions {
122
122
  export function useEnrichCellOutputs(): (
123
123
  opts: UseEnrichCellOutputsOptions,
124
124
  ) => Promise<ScreenshotResults> {
125
- const cellRuntimes = useAtomValue(cellsRuntimeAtom);
125
+ const outputs = useAtomValue(cellOutputsAtom);
126
126
 
127
127
  return async (
128
128
  opts: UseEnrichCellOutputsOptions,
129
129
  ): Promise<ScreenshotResults> => {
130
130
  const { progress } = opts;
131
131
 
132
- // Prune tracked state for cells that no longer exist
133
- const currentCellIds = new Set(Objects.keys(cellRuntimes));
134
- captureTracker.prune(currentCellIds);
132
+ // Prune tracked state for cells that no longer show anything
133
+ captureTracker.prune(new Set(outputs.keys()));
135
134
 
136
135
  const cellsToCaptureScreenshot: [CellId, unknown][] = [];
137
136
  const inFlightWaiters: {
@@ -139,11 +138,10 @@ export function useEnrichCellOutputs(): (
139
138
  promise: Promise<ScreenshotResult | undefined>;
140
139
  }[] = [];
141
140
 
142
- for (const [cellId, runtime] of Objects.entries(cellRuntimes)) {
143
- const outputData = runtime.output?.data;
141
+ for (const [cellId, output] of outputs) {
142
+ const outputData = output.data;
144
143
  if (
145
- runtime.output?.mimetype &&
146
- MIME_TYPES_TO_CAPTURE_SCREENSHOTS.has(runtime.output.mimetype) &&
144
+ MIME_TYPES_TO_CAPTURE_SCREENSHOTS.has(output.mimetype) &&
147
145
  outputData
148
146
  ) {
149
147
  if (captureTracker.needsCapture(cellId, outputData)) {