Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(ui): a revision replaces an edit whose save failed

Sanil66116e7a8a44
diff --git a/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx b/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx
index ee77010abb..c163ccaa8f 100644
--- a/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx
+++ b/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx
@@ -1,14 +1,14 @@
import { configureStore } from '@reduxjs/toolkit';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { Provider } from 'react-redux';
-import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { callCoreRpc } from '../../../services/coreRpcClient';
import chatRuntimeReducer, {
openArtifact,
upsertArtifactReadyForThread,
} from '../../../store/chatRuntimeSlice';
-import { ArtifactPreviewPanel } from './ArtifactPreviewPanel';
+import { ArtifactPreviewPanel, AUTOSAVE_DELAY_MS } from './ArtifactPreviewPanel';
vi.mock('../../../services/coreRpcClient', () => ({ callCoreRpc: vi.fn() }));
vi.mock('../../../lib/i18n/I18nContext', () => ({
@@ -113,3 +113,45 @@ describe('ArtifactPreviewPanel — resize handle', () => {
expect(width()).toBe(Number(handle.getAttribute('aria-valuemax')));
});
});
+
+describe('ArtifactPreviewPanel — a failed save, then a revision', () => {
+ beforeEach(() => rpc.mockReset());
+ afterEach(() => vi.useRealTimers());
+
+ it('keeps the revision and never re-sends the failed edit', async () => {
+ vi.useFakeTimers({ shouldAdvanceTime: true });
+ const saves = () =>
+ rpc.mock.calls.filter(([req]) => req?.method === 'openhuman.ai_save_artifact_version');
+ rpc.mockImplementation(async req => {
+ if (req?.method === 'openhuman.ai_save_artifact_version') throw new Error('core restarting');
+ return body('# first', 1);
+ });
+ const store = setup();
+ await screen.findByRole('heading', { level: 1 });
+ fireEvent.mouseDown(screen.getByRole('tab', { name: /chat.artifact.view.code/ }));
+ fireEvent.change(screen.getByRole('textbox'), { target: { value: '# my edit' } });
+
+ // The save fails; a retry is pending.
+ await act(async () => vi.advanceTimersByTime(AUTOSAVE_DELAY_MS));
+ await waitFor(() => expect(saves()).toHaveLength(1));
+
+ // The agent saves a revision.
+ rpc.mockImplementation(async req => {
+ if (req?.method === 'openhuman.ai_save_artifact_version') throw new Error('core restarting');
+ return body('# revised by the agent', 2);
+ });
+ await act(async () => {
+ vi.advanceTimersByTime(2);
+ store.dispatch(ready());
+ });
+ await waitFor(() =>
+ expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe(
+ '# revised by the agent'
+ )
+ );
+
+ // The old edit's retry is gone.
+ await act(async () => vi.advanceTimersByTime(AUTOSAVE_DELAY_MS * 5));
+ expect(saves()).toHaveLength(1);
+ });
+});
diff --git a/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx b/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
index 719324af04..aac707ab20 100644
--- a/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
+++ b/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
@@ -507,9 +507,19 @@ export function useArtifactAutosave(
// Save a pending edit when switching artifacts or closing the panel.
useEffect(() => () => void save(), [save]);
+ // Drop a held edit and its retry: a newer version saved elsewhere replaced
+ // it.
+ const discard = useCallback(() => {
+ if (timer.current) clearTimeout(timer.current);
+ timer.current = null;
+ pending.current = null;
+ retries.current = 0;
+ setStatus('saved');
+ }, []);
+
return useMemo(
- () => ({ status, version, schedule, flush: save }),
- [status, version, schedule, save]
+ () => ({ status, version, schedule, flush: save, discard }),
+ [status, version, schedule, save, discard]
);
}
@@ -726,12 +736,18 @@ function EditableTextViewer({
const autosave = useArtifactAutosave(artifact.artifactId, body, version, editable);
// Take a new body (another artifact, or a version saved elsewhere) unless
- // there is an unsaved edit here: that edit is saved as the next version.
- const hasLocalEdit = autosave.status === 'pending' || autosave.status === 'saving';
- const hasLocalEditRef = useRef(hasLocalEdit);
- hasLocalEditRef.current = hasLocalEdit;
+ // an edit here is still on its way: that edit is saved as the next version.
+ // An edit whose save failed is not on its way: the new version replaces it,
+ // and its retry is dropped so it cannot overwrite that version later.
+ const statusRef = useRef(autosave.status);
+ statusRef.current = autosave.status;
+ const discardRef = useRef(autosave.discard);
+ discardRef.current = autosave.discard;
useEffect(() => {
- if (!hasLocalEditRef.current) setEditedBody(body);
+ const status = statusRef.current;
+ if (status === 'pending' || status === 'saving') return;
+ if (status === 'error') discardRef.current();
+ setEditedBody(body);
}, [body]);
const onEdit = useCallback(