π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(tasks): discard the list-mode task draft once emptied (#20972)
Rdmclin2aa8da805e404
diff --git a/src/features/AgentTasks/AgentTaskList/CreateTaskInlineEntry.tsx b/src/features/AgentTasks/AgentTaskList/CreateTaskInlineEntry.tsxindex cfd0a981800..9be8146af1e 100644--- a/src/features/AgentTasks/AgentTaskList/CreateTaskInlineEntry.tsx+++ b/src/features/AgentTasks/AgentTaskList/CreateTaskInlineEntry.tsx@@ -44,6 +44,11 @@ import { taskDetailPath } from '../shared/taskDetailPath';import { useAgentDisplayMeta } from '../shared/useAgentDisplayMeta';import { useAgentVisibility } from '../shared/useAgentVisibility';import { useUserDisplayMeta } from '../shared/useUserDisplayMeta';+import {+ clearTaskCreateDraft,+ persistTaskCreateDraft,+ readTaskCreateDraft,+} from './taskCreateDraft';import {answeredClarifications,buildConfirmedDraft,@@ -180,6 +185,8 @@ const CreateTaskInlineEntry = memo<CreateTaskInlineEntryProps>((props) => {// State (instead of a ref) keeps the persistence effect from writing the old// scope's member into the new key during the same effect flush as the reset.const [draftHydratedKey, setDraftHydratedKey] = useState<string | null>(null);+ // Whether the composer has held text since that hydration; see persistTaskCreateDraft.+ const draftHasTextRef = useRef(false);const assigneeMeta = useAgentDisplayMeta(assigneeAgentId);const memberMeta = useUserDisplayMeta(assigneeUserId);@@ -235,6 +242,7 @@ const CreateTaskInlineEntry = memo<CreateTaskInlineEntryProps>((props) => {if (activeWorkspaceId && isMembersLoading) return;if (draftHydratedKey === draftStorageKey) return;setDraftHydratedKey(draftStorageKey);+ draftHasTextRef.current = false;// Reset to baseline for the new scope before hydrating. The run location is// part of that baseline: a device or directory picked for the previous@@ -248,22 +256,9 @@ const CreateTaskInlineEntry = memo<CreateTaskInlineEntryProps>((props) => {if (!lockAssignee) setAssigneeAgentId(agentId);setAssigneeUserId(undefined);- let raw: string | null;- try {- raw = localStorage.getItem(draftStorageKey);- } catch {- raw = null;- }- if (!raw) return;+ const draft = readTaskCreateDraft(draftStorageKey);+ if (!draft) return;try {- const draft = JSON.parse(raw) as {- assigneeAgentId?: string;- assigneeUserId?: string;- execution?: unknown;- markdown?: string;- priority?: number;- visibility?: 'private' | 'public';- };if (draft.markdown) editor.setDocument?.('markdown', draft.markdown);if (typeof draft.priority === 'number') setPriority(draft.priority);if (!lockAssignee && draft.assigneeAgentId) setAssigneeAgentId(draft.assigneeAgentId);@@ -294,32 +289,28 @@ const CreateTaskInlineEntry = memo<CreateTaskInlineEntryProps>((props) => {]);// Back the draft to storage on every change. Gated behind the restore pass so- // the initial render can't clobber a just-read draft. Write-only on non-empty:- // the key is cleared only on a successful submit (below), never here β so a+ // the initial render can't clobber a just-read draft. Emptying the editor+ // discards the draft, but only after it has held text since hydration β so a// `setDocument`-timing gap right after restore can't wipe a valid draft.useEffect(() => {if (!draftStorageKey || draftHydratedKey !== draftStorageKey || !editor) return;const markdown = String(editor.getDocument?.('markdown') ?? '').trim();- if (!markdown) return;- try {- localStorage.setItem(- draftStorageKey,- JSON.stringify({- assigneeAgentId: lockAssignee ? undefined : assigneeAgentId,- // `lockAssignee` locks only the scoped Agent. The responsible- // member remains an independent draft field and must survive reloads.- assigneeUserId,- // `undefined` (the whole selection is `undefined`) drops the key, so- // an untouched composer writes the same draft shape as before.- execution,- markdown,- priority,- visibility,- }),- );- } catch {- /* storage unavailable / quota β persistence is best-effort */- }+ draftHasTextRef.current = persistTaskCreateDraft(+ draftStorageKey,+ {+ assigneeAgentId: lockAssignee ? undefined : assigneeAgentId,+ // `lockAssignee` locks only the scoped Agent. The responsible+ // member remains an independent draft field and must survive reloads.+ assigneeUserId,+ // `undefined` (the whole selection is `undefined`) drops the key, so+ // an untouched composer writes the same draft shape as before.+ execution,+ markdown,+ priority,+ visibility,+ },+ draftHasTextRef.current,+ );}, [assigneeAgentId,assigneeUserId,@@ -365,13 +356,7 @@ const CreateTaskInlineEntry = memo<CreateTaskInlineEntryProps>((props) => {setAnalysis(null);setIntentAnswers({});editor?.cleanDocument?.();- if (draftStorageKey) {- try {- localStorage.removeItem(draftStorageKey);- } catch {- /* ignore */- }- }+ if (draftStorageKey) clearTaskCreateDraft(draftStorageKey);}, [agentId, defaultVisibility, draftStorageKey, editor]);/** What the composer currently holds, or null when there is nothing to create. */diff --git a/src/features/AgentTasks/AgentTaskList/taskCreateDraft.test.ts b/src/features/AgentTasks/AgentTaskList/taskCreateDraft.test.tsnew file mode 100644index 00000000000..405d1a55971--- /dev/null+++ b/src/features/AgentTasks/AgentTaskList/taskCreateDraft.test.ts@@ -0,0 +1,46 @@+/**+ * @vitest-environment happy-dom+ */+import { beforeEach, describe, expect, it } from 'vitest';++import { persistTaskCreateDraft, readTaskCreateDraft } from './taskCreateDraft';++const KEY = 'lobehub:task-create-draft:workspace-1:all';++describe('taskCreateDraft', () => {+ beforeEach(() => {+ localStorage.clear();+ });++ it('writes a draft that holds text', () => {+ expect(persistTaskCreateDraft(KEY, { markdown: 'Write a plan', priority: 2 }, false)).toBe(+ true,+ );++ expect(readTaskCreateDraft(KEY)).toEqual({ markdown: 'Write a plan', priority: 2 });+ });++ it('discards the saved draft once the user empties a composer that held text', () => {+ // Deleting everything used to leave the last text on disk, so it came back+ // on every reload or tab switch and could never be cleared.+ persistTaskCreateDraft(KEY, { markdown: 'Write a plan' }, false);++ expect(persistTaskCreateDraft(KEY, { markdown: '' }, true)).toBe(false);+ expect(readTaskCreateDraft(KEY)).toBeNull();+ });++ it('keeps the saved draft on an empty read before the composer has held text', () => {+ // Right after a restore the editor can still read empty until `setDocument`+ // lands; that read must not wipe the draft being restored.+ localStorage.setItem(KEY, JSON.stringify({ markdown: 'Write a plan' }));++ expect(persistTaskCreateDraft(KEY, { markdown: '' }, false)).toBe(false);+ expect(readTaskCreateDraft(KEY)).toEqual({ markdown: 'Write a plan' });+ });++ it('reads a malformed draft as no draft', () => {+ localStorage.setItem(KEY, '{not json');++ expect(readTaskCreateDraft(KEY)).toBeNull();+ });+});diff --git a/src/features/AgentTasks/AgentTaskList/taskCreateDraft.ts b/src/features/AgentTasks/AgentTaskList/taskCreateDraft.tsnew file mode 100644index 00000000000..5e0d8e2638b--- /dev/null+++ b/src/features/AgentTasks/AgentTaskList/taskCreateDraft.ts@@ -0,0 +1,58 @@+/**+ * The composer's in-progress draft as stored in the browser. Every field is+ * optional because the value is a browser string that a previous release (or+ * another tab) may have written in another shape.+ */+export interface TaskCreateDraft {+ assigneeAgentId?: string;+ assigneeUserId?: string;+ execution?: unknown;+ markdown?: string;+ priority?: number;+ visibility?: 'private' | 'public';+}++/** The saved draft for a scope, or null when there is none or it is unreadable. */+export const readTaskCreateDraft = (key: string): TaskCreateDraft | null => {+ try {+ const raw = localStorage.getItem(key);+ return raw ? (JSON.parse(raw) as TaskCreateDraft) : null;+ } catch {+ return null;+ }+};++export const clearTaskCreateDraft = (key: string) => {+ try {+ localStorage.removeItem(key);+ } catch {+ /* storage unavailable β persistence is best-effort */+ }+};++/**+ * Mirrors the composer into storage and returns whether it now holds text.+ *+ * An empty composer clears the saved draft only once it has held text since it+ * was hydrated (`hasHeldText`): emptying the editor is the user discarding the+ * draft, and leaving the old text on disk brings it back on every reload. Before+ * that, an empty read is the gap right after a restore, before `setDocument`+ * lands, and must not wipe the draft being restored.+ */+export const persistTaskCreateDraft = (+ key: string,+ draft: TaskCreateDraft & { markdown: string },+ hasHeldText: boolean,+): boolean => {+ if (!draft.markdown) {+ if (hasHeldText) clearTaskCreateDraft(key);+ return false;+ }++ try {+ localStorage.setItem(key, JSON.stringify(draft));+ } catch {+ /* storage unavailable / quota β persistence is best-effort */+ }+ return true;+};