Line data Source code
1 : /**
2 : * V2 Workflow Draft Store
3 : *
4 : * Uses per-draft keys in localStorage instead of a single blob.
5 : * Handles LRU eviction and quota management.
6 : */
7 :
8 : import { defineStore } from 'pinia'
9 : import { ref } from 'vue'
10 :
11 : import { reportError } from '@comfyorg/telemetry/reportError'
12 : import { useApp } from '@/scripts/appInstance'
13 :
14 : import type {
15 : DraftIndexV2,
16 : DraftLineage,
17 : DraftPayloadV2
18 : } from '@/platform/workflow/persistence/base/draftTypes'
19 : import { MAX_DRAFTS } from '@/platform/workflow/persistence/base/draftTypes'
20 : import {
21 : createEmptyIndex,
22 : getEntryByPath,
23 : getMostRecentKey,
24 : moveEntry,
25 : removeEntry,
26 : removeOrphanedEntries,
27 : touchOrder,
28 : upsertEntry
29 : } from '@/platform/workflow/persistence/base/draftCacheV2'
30 : import { hashPath } from '@/platform/workflow/persistence/base/hashUtil'
31 : import { getWorkspaceId } from '@/platform/workflow/persistence/base/storageKeys'
32 : import {
33 : deleteOrphanPayloads,
34 : deletePayload,
35 : deletePayloads,
36 : getPayloadKeys,
37 : isStorageAvailable,
38 : markStorageUnavailable,
39 : readIndex,
40 : readPayload,
41 : writeIndex,
42 : writePayload
43 : } from '@/platform/workflow/persistence/base/storageIO'
44 :
45 : interface DraftMeta {
46 : name: string
47 : isTemporary: boolean
48 : }
49 :
50 : interface LoadPersistedWorkflowOptions {
51 : preferredPath?: string | null
52 : fallbackToLatestDraft?: boolean
53 : isCurrent?: () => boolean
54 : }
55 :
56 2356 : export const useWorkflowDraftStoreV2 = defineStore('workflowDraftV2', () => {
57 : // In-memory cache of the index per workspace (synced with localStorage)
58 : // Key is workspaceId, value is the cached index
59 2355 : const indexCacheByWorkspace = ref<Record<string, DraftIndexV2>>({})
60 2355 : const lineageByDraft = new Map<string, DraftLineage>()
61 2355 : const authSubject = ref<string | null | undefined>(undefined)
62 :
63 : /**
64 : * Gets the current workspace ID fresh (not cached).
65 : * This ensures operations use the correct workspace after switches.
66 : */
67 553382 : function currentWorkspaceId(): string {
68 553382 : const workspaceId = getWorkspaceId()
69 553382 : const subject = authSubject.value
70 553382 : if (subject === undefined) return workspaceId
71 553382 : return `${encodeURIComponent(subject ?? 'unauthenticated')}:${workspaceId}`
72 : }
73 :
74 2355 : function setAuthSubject(subject: string | null | undefined): void {
75 2355 : authSubject.value = subject
76 : }
77 :
78 5786 : function lineageKey(workspaceId: string, draftKey: string): string {
79 5786 : return `${workspaceId}:${draftKey}`
80 : }
81 :
82 5524 : function deleteLineages(workspaceId: string, draftKeys: string[]): void {
83 5524 : for (const draftKey of draftKeys)
84 0 : lineageByDraft.delete(lineageKey(workspaceId, draftKey))
85 : }
86 :
87 5524 : function payloadFor(
88 : workspaceId: string,
89 : draftKey: string,
90 : data: string,
91 : updatedAt: number
92 : ): DraftPayloadV2 {
93 5524 : const lineage =
94 : lineageByDraft.get(lineageKey(workspaceId, draftKey)) ??
95 : readPayload(workspaceId, draftKey)?.lineage
96 5524 : return { data, updatedAt, ...(lineage && { lineage }) }
97 : }
98 :
99 : /**
100 : * Loads the index from localStorage or creates empty.
101 : */
102 274945 : function loadIndex(): DraftIndexV2 {
103 274945 : const workspaceId = currentWorkspaceId()
104 :
105 274945 : const cached = getCachedIndex(workspaceId)
106 274945 : if (cached) return cached
107 :
108 2354 : const stored = readIndex(workspaceId)
109 264070 : if (stored) {
110 : // Clean up any index/payload drift
111 : const payloadKeys = new Set(getPayloadKeys(workspaceId))
112 98 : const cleaned = removeOrphanedEntries(stored, payloadKeys)
113 98 : indexCacheByWorkspace.value[workspaceId] = cleaned
114 :
115 : // Also clean up orphan payloads
116 98 : const indexKeys = new Set(cleaned.order)
117 98 : deleteOrphanPayloads(workspaceId, indexKeys)
118 :
119 98 : return cleaned
120 : }
121 :
122 2256 : const emptyIndex = createEmptyIndex()
123 2256 : indexCacheByWorkspace.value[workspaceId] = emptyIndex
124 2256 : return emptyIndex
125 : }
126 :
127 274945 : function getCachedIndex(workspaceId: string): DraftIndexV2 | null {
128 274945 : return indexCacheByWorkspace.value[workspaceId]
129 : }
130 :
131 : /**
132 : * Persists the current index to localStorage.
133 : */
134 5869 : function persistIndex(index: DraftIndexV2): boolean {
135 5869 : const workspaceId = currentWorkspaceId()
136 5869 : indexCacheByWorkspace.value[workspaceId] = index
137 5869 : return writeIndex(workspaceId, index)
138 : }
139 :
140 : /**
141 : * Saves a draft (data + metadata).
142 : * Primes index cache, writes payload, then persists updated index.
143 : */
144 5524 : function saveDraft(path: string, data: string, meta: DraftMeta): boolean {
145 5524 : if (!isStorageAvailable()) return false
146 :
147 5524 : const workspaceId = currentWorkspaceId()
148 5524 : const draftKey = hashPath(path)
149 5524 : const now = Date.now()
150 :
151 5524 : const index = loadIndex()
152 :
153 : const payloadWritten = writePayload(
154 : workspaceId,
155 : draftKey,
156 : payloadFor(workspaceId, draftKey, data, now)
157 : )
158 :
159 5524 : if (!payloadWritten) {
160 0 : return handleQuotaExceeded(path, data, meta)
161 : }
162 5524 : const { index: newIndex, evicted } = upsertEntry(
163 : index,
164 : path,
165 : { ...meta, updatedAt: now },
166 : MAX_DRAFTS
167 : )
168 :
169 5524 : if (!persistIndex(newIndex)) {
170 0 : deletePayload(workspaceId, draftKey)
171 0 : persistIndex(index)
172 0 : return false
173 : }
174 :
175 5524 : deletePayloads(workspaceId, evicted)
176 5524 : deleteLineages(workspaceId, evicted)
177 5524 : return true
178 : }
179 :
180 : /**
181 : * Handles quota exceeded by evicting oldest drafts until write succeeds.
182 : *
183 : * Tolerates index/payload desync: orphaned `order` keys with no matching
184 : * entry in `entries` are stripped in-place and the loop continues, rather
185 : * than bailing out and leaving evictable drafts behind.
186 : *
187 : * Recovery writes (`persistIndex(currentIndex)` after a failed write) are
188 : * best-effort; their return value is intentionally ignored because there
189 : * is no useful action to take when the recovery itself also fails — the
190 : * caller will already see `false` and surface the toast. A subsequent
191 : * `saveDraft` will re-converge the index via `removeOrphanedEntries`.
192 : */
193 0 : function handleQuotaExceeded(
194 : path: string,
195 : data: string,
196 : meta: DraftMeta
197 : ): boolean {
198 0 : const workspaceId = currentWorkspaceId()
199 0 : const draftKey = hashPath(path)
200 :
201 0 : let currentIndex = loadIndex()
202 0 : let evictedCount = 0
203 :
204 0 : while (currentIndex.order.length > 0) {
205 0 : const oldestKey = currentIndex.order.find((key) => key !== draftKey)
206 0 : if (!oldestKey) break
207 :
208 0 : const oldestEntry = currentIndex.entries[oldestKey]
209 0 : if (!getIndexEntry(currentIndex, oldestKey)) {
210 0 : currentIndex = stripOrderKey(currentIndex, oldestKey)
211 0 : continue
212 : }
213 :
214 0 : const result = removeEntry(currentIndex, oldestEntry.path)
215 0 : currentIndex = result.index
216 0 : if (result.removedKey) {
217 0 : deletePayload(workspaceId, result.removedKey)
218 0 : deleteLineages(workspaceId, [result.removedKey])
219 0 : evictedCount++
220 : }
221 :
222 0 : const now = Date.now()
223 0 : if (
224 : writePayload(
225 : workspaceId,
226 : draftKey,
227 : payloadFor(workspaceId, draftKey, data, now)
228 : )
229 : ) {
230 0 : const { index: finalIndex } = upsertEntry(
231 : currentIndex,
232 : path,
233 : { ...meta, updatedAt: now },
234 : MAX_DRAFTS
235 : )
236 0 : if (!persistIndex(finalIndex)) {
237 0 : deletePayload(workspaceId, draftKey)
238 0 : persistIndex(currentIndex)
239 0 : return false
240 : }
241 0 : return true
242 : }
243 : }
244 :
245 0 : persistIndex(currentIndex)
246 0 : reportQuotaExhausted(currentIndex, evictedCount, payloadByteSize(data))
247 0 : markStorageUnavailable()
248 0 : return false
249 : }
250 :
251 40 : function getIndexEntry(
252 : index: DraftIndexV2,
253 : key: string
254 : ): DraftIndexV2['entries'][string] | undefined {
255 40 : return index.entries[key]
256 : }
257 :
258 : /**
259 : * Approximates the UTF-8 byte size of the envelope `writePayload` actually
260 : * stores. We hard-code `updatedAt: 0` rather than the real timestamp because
261 : * the missing ~12 bytes are noise compared to the kilobyte-scale workflow
262 : * payload this telemetry exists to measure.
263 : */
264 0 : function payloadByteSize(data: string): number {
265 0 : return new TextEncoder().encode(JSON.stringify({ data, updatedAt: 0 }))
266 : .length
267 : }
268 :
269 0 : function stripOrderKey(index: DraftIndexV2, orphanKey: string): DraftIndexV2 {
270 0 : return {
271 : ...index,
272 : updatedAt: Date.now(),
273 : order: index.order.filter((key) => key !== orphanKey)
274 : }
275 : }
276 :
277 0 : function reportQuotaExhausted(
278 : finalIndex: DraftIndexV2,
279 : evicted: number,
280 : payloadBytes: number
281 : ): void {
282 0 : reportError(
283 : new Error('localStorage quota exhausted after full draft eviction'),
284 : {
285 : surface: 'graph',
286 : errorType: 'storage_quota_exhausted',
287 : level: 'warning',
288 : tags: { store: 'workflowDraftStoreV2' },
289 : context: {
290 : evictedDrafts: evicted,
291 : remainingDrafts: finalIndex.order.length,
292 : incomingPayloadBytes: payloadBytes
293 : }
294 : }
295 : )
296 : }
297 :
298 : /**
299 : * Removes a draft.
300 : */
301 326 : function removeDraft(path: string): void {
302 326 : const workspaceId = currentWorkspaceId()
303 : const index = loadIndex()
304 326 : const { index: newIndex, removedKey } = removeEntry(index, path)
305 :
306 326 : if (removedKey) {
307 261 : deletePayload(workspaceId, removedKey)
308 261 : lineageByDraft.delete(lineageKey(workspaceId, removedKey))
309 261 : persistIndex(newIndex)
310 : }
311 : }
312 :
313 : /**
314 : * Moves a draft from one path to another (rename).
315 : */
316 100 : function moveDraft(oldPath: string, newPath: string, name: string): void {
317 100 : const workspaceId = currentWorkspaceId()
318 : const index = loadIndex()
319 100 : const result = moveEntry(index, oldPath, newPath, name)
320 :
321 100 : if (result) {
322 : const isRenameOntoSelf = result.oldKey === result.newKey
323 2 : if (isRenameOntoSelf) {
324 1 : persistIndex(result.index)
325 1 : return
326 : }
327 1 : const oldPayload = readPayload(workspaceId, result.oldKey)
328 1 : if (oldPayload) {
329 : const written = writePayload(workspaceId, result.newKey, oldPayload)
330 1 : if (!written) return
331 :
332 1 : if (!persistIndex(result.index)) {
333 0 : deletePayload(workspaceId, result.newKey)
334 0 : return
335 : }
336 1 : deletePayload(workspaceId, result.oldKey)
337 1 : const lineage = lineageByDraft.get(
338 : lineageKey(workspaceId, result.oldKey)
339 : )
340 1 : if (lineage) {
341 0 : lineageByDraft.set(lineageKey(workspaceId, result.newKey), lineage)
342 0 : lineageByDraft.delete(lineageKey(workspaceId, result.oldKey))
343 : }
344 : }
345 : }
346 : }
347 :
348 : /**
349 : * Gets draft data by path.
350 : */
351 266618 : function getDraft(path: string): {
352 : data: string
353 : name: string
354 : isTemporary: boolean
355 : updatedAt: number
356 : lineage?: DraftLineage
357 : } | null {
358 266618 : const workspaceId = currentWorkspaceId()
359 : const index = loadIndex()
360 266618 : const entry = getEntryByPath(index, path)
361 266618 : if (!entry) return null
362 :
363 232 : const draftKey = hashPath(path)
364 232 : const payload = readPayload(workspaceId, draftKey)
365 29121 : if (!payload) {
366 0 : removeDraft(path)
367 0 : return null
368 : }
369 13334 : if (payload.lineage)
370 0 : lineageByDraft.set(lineageKey(workspaceId, draftKey), payload.lineage)
371 :
372 232 : return {
373 : data: payload.data,
374 : name: entry.name,
375 : isTemporary: entry.isTemporary,
376 : updatedAt: payload.updatedAt,
377 : lineage: payload.lineage
378 : }
379 : }
380 :
381 0 : function setLineage(path: string, lineage: DraftLineage): void {
382 0 : const workspaceId = currentWorkspaceId()
383 0 : const draftKey = hashPath(path)
384 0 : lineageByDraft.set(lineageKey(workspaceId, draftKey), lineage)
385 0 : const payload = readPayload(workspaceId, draftKey)
386 0 : if (
387 : payload &&
388 : (payload.lineage?.workflowId !== lineage.workflowId ||
389 : payload.lineage.lineageSeq !== lineage.lineageSeq)
390 : )
391 0 : writePayload(workspaceId, draftKey, { ...payload, lineage })
392 : }
393 :
394 : /**
395 : * Marks a draft as recently used without rewriting its payload.
396 : */
397 82 : function markDraftUsed(path: string): void {
398 82 : const index = loadIndex()
399 : const entry = getEntryByPath(index, path)
400 82 : if (!entry) return
401 :
402 82 : const draftKey = hashPath(path)
403 82 : persistIndex({
404 : ...index,
405 : updatedAt: Date.now(),
406 : order: touchOrder(index.order, draftKey)
407 : })
408 : }
409 :
410 : /**
411 : * Gets the most recent draft path.
412 : */
413 2295 : function getMostRecentPath(): string | null {
414 2295 : const index = loadIndex()
415 2295 : const key = getMostRecentKey(index)
416 2295 : if (!key) return null
417 :
418 2295 : return getIndexEntry(index, key)?.path ?? null
419 : }
420 :
421 44 : async function tryLoadGraph(
422 : payload: string | null,
423 : workflowName: string | null,
424 : isCurrent?: () => boolean,
425 : onFailure?: () => void
426 : ): Promise<boolean> {
427 44 : if (!payload) return false
428 44 : try {
429 44 : const workflow = JSON.parse(payload)
430 44 : if (isCurrent?.() === false) return false
431 44 : await useApp().loadGraphData(workflow, true, true, workflowName)
432 44 : return true
433 : } catch (err) {
434 0 : console.error('Failed to load persisted workflow', err)
435 0 : onFailure?.()
436 0 : return false
437 : }
438 : }
439 :
440 44 : async function loadDraft(
441 : path: string,
442 : isCurrent?: () => boolean
443 : ): Promise<boolean> {
444 44 : const draft = getDraft(path)
445 44 : if (!draft) return false
446 :
447 44 : const loaded = await tryLoadGraph(draft.data, draft.name, isCurrent, () => {
448 0 : removeDraft(path)
449 : })
450 44 : if (loaded) {
451 44 : markDraftUsed(path)
452 : }
453 :
454 44 : return loaded
455 : }
456 :
457 2299 : async function loadPersistedWorkflow(
458 : options: LoadPersistedWorkflowOptions
459 : ): Promise<boolean> {
460 2299 : const { preferredPath, fallbackToLatestDraft = false, isCurrent } = options
461 :
462 2299 : if (preferredPath && (await loadDraft(preferredPath, isCurrent))) {
463 4 : return true
464 : }
465 :
466 2299 : if (fallbackToLatestDraft && isCurrent?.() !== false) {
467 2295 : const mostRecent = getMostRecentPath()
468 2295 : if (mostRecent && (await loadDraft(mostRecent, isCurrent))) {
469 40 : return true
470 : }
471 : }
472 :
473 2255 : return false
474 : }
475 :
476 : /**
477 : * Resets the store (clears in-memory cache for current workspace).
478 : */
479 0 : function reset(): void {
480 0 : const workspaceId = currentWorkspaceId()
481 0 : delete indexCacheByWorkspace.value[workspaceId]
482 0 : for (const key of lineageByDraft.keys()) {
483 0 : if (key.startsWith(`${workspaceId}:`)) lineageByDraft.delete(key)
484 : }
485 : }
486 :
487 2355 : return {
488 : setAuthSubject,
489 : saveDraft,
490 : removeDraft,
491 : moveDraft,
492 : markDraftUsed,
493 : getDraft,
494 : setLineage,
495 : getMostRecentPath,
496 : loadPersistedWorkflow,
497 : reset
498 : }
499 : })
|