Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(app): stop Canvas proposal save loops

Louis Beaumontd8621a141791
diff --git a/apps/screenpipe-app-tauri/components/settings/__tests__/brain-overview.test.tsx b/apps/screenpipe-app-tauri/components/settings/__tests__/brain-overview.test.tsx
index 37ec3ab12..64d962dc7 100644
--- a/apps/screenpipe-app-tauri/components/settings/__tests__/brain-overview.test.tsx
+++ b/apps/screenpipe-app-tauri/components/settings/__tests__/brain-overview.test.tsx
@@ -1844,6 +1844,88 @@ describe("BrainOverview", () => {
expect(await screen.findByTestId("overview-undo")).toBeTruthy();
});
+ it("never persists preview-only Block positions while reviewing an added Block", async () => {
+ mocks.listBrainViews.mockResolvedValue({
+ status: "ok",
+ data: [populatedView],
+ });
+ mocks.generateLiveViewWithPi.mockResolvedValue({
+ title: populatedView.title,
+ timeRange: populatedView.timeRange,
+ periodPolicy: populatedView.periodPolicy,
+ note: "Added habit signals.",
+ blocks: [
+ {
+ id: "focus-time",
+ title: "Focus time",
+ intent: "Calculate focused work time",
+ component: "metric.v1",
+ width: 6,
+ pipeName: "daily-summary",
+ },
+ {
+ id: "habit-signals",
+ title: "Habit signals",
+ intent: "Show recurring work habits.",
+ component: "list.v1",
+ width: 6,
+ pipeName: "daily-summary",
+ },
+ ],
+ });
+ mocks.saveBrainViewCanvas.mockImplementation(async (request) => {
+ if (
+ request.blocks.some(
+ (block: { slotId: string }) => block.slotId === "habit-signals",
+ )
+ ) {
+ return {
+ status: "error" as const,
+ error: "canvas contains more Block positions than the Live View",
+ };
+ }
+ return {
+ status: "ok" as const,
+ data: {
+ schema: "live-view-canvas.v1" as const,
+ ...request,
+ revision: (request.expectedRevision ?? 0) + 1,
+ updatedAt: "2026-07-27T18:00:00Z",
+ },
+ };
+ });
+ render(<BrainOverview />);
+
+ fireEvent.change(await screen.findByTestId("live-view-ai-prompt"), {
+ target: { value: "add habit signals" },
+ });
+ fireEvent.click(screen.getByTestId("live-view-ai-generate"));
+
+ expect(await screen.findByTestId("live-view-ai-review")).toBeTruthy();
+ expect(screen.getByTestId("canvas-block-habit-signals")).toBeTruthy();
+ mocks.saveBrainViewCanvas.mockClear();
+ fireEvent.click(screen.getByTestId("canvas-tools-toggle"));
+ fireEvent.click(screen.getByTestId("canvas-fit"));
+
+ await waitFor(() =>
+ expect(mocks.saveBrainViewCanvas).toHaveBeenCalledTimes(1),
+ );
+ expect(mocks.saveBrainViewCanvas).toHaveBeenCalledWith(
+ expect.objectContaining({
+ viewId: populatedView.id,
+ blocks: [expect.objectContaining({ slotId: "focus-time" })],
+ }),
+ );
+ expect(
+ mocks.saveBrainViewCanvas.mock.calls[0][0].blocks.some(
+ (block: { slotId: string }) => block.slotId === "habit-signals",
+ ),
+ ).toBe(false);
+ expect(mocks.toast).not.toHaveBeenCalledWith(
+ expect.objectContaining({ title: "canvas changes were not saved" }),
+ );
+ });
+
it("rejects a staged Block edit without writing the dashboard", async () => {
mocks.listBrainViews.mockResolvedValue({
status: "ok",
diff --git a/apps/screenpipe-app-tauri/components/settings/brain-overview.tsx b/apps/screenpipe-app-tauri/components/settings/brain-overview.tsx
index 8cbae2b4d..44bf575f2 100644
--- a/apps/screenpipe-app-tauri/components/settings/brain-overview.tsx
+++ b/apps/screenpipe-app-tauri/components/settings/brain-overview.tsx
@@ -574,6 +574,7 @@ export function BrainOverview({
const canvasLoadTokenRef = useRef(0);
const canvasLatestRef = useRef<BrainViewCanvasDocument | null>(null);
const canvasServerRevisionsRef = useRef(new Map<string, number>());
+ const canvasSaveErrorsRef = useRef(new Map<string, string>());
const pendingCanvasSavesRef = useRef(
new Map<string, BrainViewCanvasDocument>(),
);
@@ -666,14 +667,17 @@ export function BrainOverview({
canvasServerRevisionsRef.current.get(viewId) ?? null;
const result = await commands.saveBrainViewCanvas(request);
if (result.status === "error") {
- setCanvasError(result.error);
- toast({
- title: "canvas changes were not saved",
- description: result.error,
- variant: "destructive",
- });
+ if (canvasSaveErrorsRef.current.get(viewId) !== result.error) {
+ canvasSaveErrorsRef.current.set(viewId, result.error);
+ toast({
+ title: "canvas changes were not saved",
+ description: result.error,
+ variant: "destructive",
+ });
+ }
continue;
}
+ canvasSaveErrorsRef.current.delete(viewId);
canvasServerRevisionsRef.current.set(viewId, result.data.revision);
setCanvasError(null);
if (canvasLatestRef.current?.viewId === viewId) {
@@ -716,6 +720,23 @@ export function BrainOverview({
[pumpCanvasSaves],
);
+ const changeVisibleCanvasDocument = useCallback(
+ (next: BrainViewCanvasDocument, options: { persist: boolean }) => {
+ if (!view || aiBlockProposals.length === 0) {
+ changeCanvasDocument(next, options);
+ return;
+ }
+
+ // AI review renders a preview that can contain Blocks which do not exist
+ // in the persisted Live View yet. Canvas interactions must never enqueue
+ // those preview-only positions for the backend. Keep safe viewport and
+ // annotation edits, but reconcile Block positions and arrows against the
+ // currently saved definition until the proposals are applied.
+ changeCanvasDocument(reconcileCanvasDocument(view, next), options);
+ },
+ [aiBlockProposals.length, changeCanvasDocument, view],
+ );
+
useEffect(() => {
if (!view) {
canvasLatestRef.current = null;
@@ -3030,7 +3051,7 @@ export function BrainOverview({
timeRange={view.timeRange}
refreshingSlotIds={refreshingSlotIds}
aiEditingSlotId={aiEditingSlotId}
- onChange={changeCanvasDocument}
+ onChange={changeVisibleCanvasDocument}
onFeedback={recordCardFeedback}
onRegenerate={(slot) =>
void refreshConnectedPipes(view, [slot], "card_regenerated")