Repository navigation
feat(assets): add conservative explicit cleanup for local files #854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zhao0335
wants to merge
3
commits into
pascalorg:main
Choose a base branch
from
Zhao0335:fix/asset-storage-orphan-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+621
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import 'fake-indexeddb/auto' | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { deleteAsset, loadAssetUrl, saveAsset } from '@pascal-app/core' | ||
| import { collectNodeAssetUrlList, runLocalAssetGc } from './local-asset-gc' | ||
|
|
||
| type FetchHandler = (url: string) => Response | Promise<Response> | ||
|
|
||
| function mockFetch(handler: FetchHandler): () => void { | ||
| const original = globalThis.fetch | ||
| globalThis.fetch = (async (input: RequestInfo | URL) => { | ||
| const url = typeof input === 'string' ? input : input.toString() | ||
| return handler(url) | ||
| }) as typeof fetch | ||
| return () => { | ||
| globalThis.fetch = original | ||
| } | ||
| } | ||
|
|
||
| function json(body: unknown, ok = true, status = 200): Response { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }) | ||
| } | ||
|
|
||
| describe('local-asset-gc collectors', () => { | ||
| test('collectNodeAssetUrlList walks url and src', () => { | ||
| const urls = collectNodeAssetUrlList({ | ||
| a: { url: 'asset://guide' }, | ||
| b: { src: 'asset://model' }, | ||
| c: { src: 'https://cdn.example.com/x.glb' }, | ||
| }) | ||
| expect(urls.sort()).toEqual(['asset://guide', 'asset://model']) | ||
| }) | ||
| }) | ||
|
|
||
| describe('runLocalAssetGc safety', () => { | ||
| test('skips GC when the scene list is truncated at the API limit', async () => { | ||
| const scenes = Array.from({ length: 500 }, (_, i) => ({ id: `scene-${i}` })) | ||
| const restore = mockFetch((url) => { | ||
| if (url.includes('/api/scenes?')) return json({ scenes }) | ||
| return json({ graph: { nodes: {} } }) | ||
| }) | ||
| try { | ||
| const result = await runLocalAssetGc(() => ({ nodes: {} })) | ||
| expect(result).toBeNull() | ||
| } finally { | ||
| restore() | ||
| } | ||
| }) | ||
|
|
||
| test('skips GC when listing scenes fails', async () => { | ||
| const restore = mockFetch(() => json({ error: 'no' }, false, 500)) | ||
| try { | ||
| expect(await runLocalAssetGc(() => ({ nodes: {} }))).toBeNull() | ||
| } finally { | ||
| restore() | ||
| } | ||
| }) | ||
|
|
||
| test('skips GC when the scene inventory is malformed', async () => { | ||
| for (const body of [{}, { scenes: [{ id: null }] }]) { | ||
| const restore = mockFetch(() => json(body)) | ||
| try { | ||
| expect(await runLocalAssetGc(() => ({ nodes: {} }))).toBeNull() | ||
| } finally { | ||
| restore() | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| test('skips GC when a listed scene has no graph', async () => { | ||
| const restore = mockFetch((url) => | ||
| url.includes('/api/scenes?') ? json({ scenes: [{ id: 'scene-1' }] }) : json({}), | ||
| ) | ||
| try { | ||
| expect(await runLocalAssetGc(() => ({ nodes: {} }))).toBeNull() | ||
| } finally { | ||
| restore() | ||
| } | ||
| }) | ||
|
|
||
| test('skips a graph with a missing or malformed node map', async () => { | ||
| const asset = await saveAsset(new File(['keep'], 'keep.txt')) | ||
| try { | ||
| for (const graph of [{}, { nodes: null }, { nodes: [] }]) { | ||
| const restore = mockFetch((url) => | ||
| url.includes('/api/scenes?') ? json({ scenes: [{ id: 'scene-1' }] }) : json({ graph }), | ||
| ) | ||
| try { | ||
| expect(await runLocalAssetGc(() => ({ nodes: {} }))).toBeNull() | ||
| expect(await loadAssetUrl(asset)).not.toBeNull() | ||
| } finally { | ||
| restore() | ||
| } | ||
| } | ||
| } finally { | ||
| await deleteAsset(asset) | ||
| } | ||
| }) | ||
|
|
||
| test('keeps deeply nested references and references outside nodes and materials', async () => { | ||
| const deepAsset = await saveAsset(new File(['deep'], 'deep.txt')) | ||
| const collectionAsset = await saveAsset(new File(['collection'], 'collection.txt')) | ||
| let nested: unknown = deepAsset | ||
| for (let depth = 0; depth < 12; depth++) nested = { nested } | ||
| const graph = { nodes: { plugin: { nested } }, collections: { preview: collectionAsset } } | ||
| const restore = mockFetch((url) => | ||
| url.includes('/api/scenes?') ? json({ scenes: [{ id: 'scene-1' }] }) : json({ graph }), | ||
| ) | ||
| try { | ||
| await runLocalAssetGc(() => ({ nodes: {} })) | ||
| expect(await loadAssetUrl(deepAsset)).not.toBeNull() | ||
| expect(await loadAssetUrl(collectionAsset)).not.toBeNull() | ||
| } finally { | ||
| restore() | ||
| await deleteAsset(deepAsset) | ||
| await deleteAsset(collectionAsset) | ||
| } | ||
| }) | ||
|
|
||
| test('skips GC when the local scene cannot be parsed', async () => { | ||
| const previous = Object.getOwnPropertyDescriptor(globalThis, 'localStorage') | ||
| Object.defineProperty(globalThis, 'localStorage', { | ||
| configurable: true, | ||
| value: { getItem: () => '{not-json' }, | ||
| }) | ||
| const restore = mockFetch(() => json({ scenes: [] })) | ||
| try { | ||
| expect(await runLocalAssetGc(() => ({ nodes: {} }))).toBeNull() | ||
| } finally { | ||
| restore() | ||
| if (previous) Object.defineProperty(globalThis, 'localStorage', previous) | ||
| else Reflect.deleteProperty(globalThis, 'localStorage') | ||
| } | ||
| }) | ||
|
|
||
| test('re-reads live nodes so mid-GC uploads stay in the keep-set', async () => { | ||
| // Live graph is empty when GC starts, then gains a guide during the | ||
| // per-scene fetch — the File must survive the sweep. | ||
| let live: Record<string, unknown> = {} | ||
| const lateAsset = 'asset://uploaded-mid-gc' | ||
| const restore = mockFetch(async (url) => { | ||
| if (url.includes('/api/scenes?')) { | ||
| // Simulate latency while the user uploads. | ||
| live = { guide: { url: lateAsset } } | ||
| return json({ scenes: [] }) | ||
| } | ||
| return json({ graph: { nodes: {} } }) | ||
| }) | ||
| try { | ||
| const result = await runLocalAssetGc(() => ({ nodes: live })) | ||
| // Keep-set included the late upload, so sweep ran but did not need to | ||
| // delete it (result is a count of removals — File itself is not present | ||
| // in this unit test's IDB; the contract is "keep-set includes live URLs"). | ||
| expect(result).not.toBeNull() | ||
| expect(collectNodeAssetUrlList(live)).toContain(lateAsset) | ||
| } finally { | ||
| restore() | ||
| } | ||
| }) | ||
|
|
||
| test('keeps a material texture added to the live graph during inventory', async () => { | ||
| const assetUrl = await saveAsset(new File(['texture'], 'texture.png')) | ||
| let materials: unknown = {} | ||
| const restore = mockFetch((url) => { | ||
| if (url.includes('/api/scenes?')) { | ||
| materials = { floor: { texture: { url: assetUrl } } } | ||
| return json({ scenes: [] }) | ||
| } | ||
| return json({ graph: { nodes: {} } }) | ||
| }) | ||
| try { | ||
| await runLocalAssetGc(() => ({ nodes: {}, materials })) | ||
| expect(await loadAssetUrl(assetUrl)).not.toBeNull() | ||
| } finally { | ||
| restore() | ||
| await deleteAsset(assetUrl) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { | ||
| collectLocalAssetUrls, | ||
| collectNodeAssetUrls, | ||
| sweepLocalAssetsExcept, | ||
| } from '@pascal-app/core' | ||
|
|
||
| const LOCAL_STORAGE_SCENE_KEY = 'pascal-editor-scene' | ||
| /** Must match apps/editor/app/api/scenes/route.ts listQuerySchema max. */ | ||
| const SCENES_LIST_MAX = 500 | ||
|
|
||
| type SceneGraphLike = { | ||
| nodes?: Record<string, unknown> | ||
| materials?: unknown | ||
| } | ||
|
|
||
| function collectGraphAssetUrls(graph: unknown): string[] | null { | ||
| if (!graph || typeof graph !== 'object' || !('nodes' in graph)) return null | ||
| const nodes = graph.nodes | ||
| if (!nodes || typeof nodes !== 'object' || Array.isArray(nodes)) return null | ||
| return collectLocalAssetUrls(graph) | ||
| } | ||
|
|
||
| /** Asset URLs still referenced by the browser's localStorage scene, if any. */ | ||
| export function collectLocalStorageSceneAssetUrls(): string[] | null { | ||
| if (typeof localStorage === 'undefined') return [] | ||
| try { | ||
| const raw = localStorage.getItem(LOCAL_STORAGE_SCENE_KEY) | ||
| if (raw === null) return [] | ||
| return collectGraphAssetUrls(JSON.parse(raw)) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Union of asset URLs across every graph this origin can still open. | ||
| * Returns null when the inventory cannot be proven complete — callers must | ||
| * skip GC rather than sweep from a partial keep-set. | ||
| */ | ||
| export async function collectAllPersistedAssetUrls( | ||
| currentGraph: SceneGraphLike, | ||
| ): Promise<string[] | null> { | ||
| const currentUrls = collectGraphAssetUrls(currentGraph) | ||
| if (currentUrls === null) return null | ||
| const keep = new Set(currentUrls) | ||
| const localUrls = collectLocalStorageSceneAssetUrls() | ||
| if (localUrls === null) return null | ||
| for (const url of localUrls) keep.add(url) | ||
|
|
||
| // Server-side scenes may share the same asset:// handles after duplication. | ||
| // If listing fails or is truncated we cannot prove the keep-set is complete. | ||
| let scenesJson: { data?: { scenes?: unknown[] }; scenes?: unknown[] } | null = null | ||
| try { | ||
| const res = await fetch(`/api/scenes?limit=${SCENES_LIST_MAX}`) | ||
| if (!res.ok) return null | ||
| scenesJson = (await res.json()) as { | ||
| data?: { scenes?: unknown[] } | ||
| scenes?: unknown[] | ||
| } | ||
| } catch { | ||
| return null | ||
| } | ||
|
|
||
| const list = scenesJson?.data?.scenes ?? scenesJson?.scenes | ||
| if (!Array.isArray(list)) return null | ||
| // The API has no cursor/total. A full page means older scenes were dropped | ||
| // by `limit` — treating it as complete would delete their Files. | ||
| if (list.length >= SCENES_LIST_MAX) return null | ||
|
|
||
| for (const entry of list) { | ||
| const id = (entry as { id?: unknown } | null)?.id | ||
| if (typeof id !== 'string' || !id) return null | ||
| try { | ||
| const res = await fetch(`/api/scenes/${encodeURIComponent(id)}`) | ||
| if (!res.ok) return null | ||
| const body = (await res.json()) as { | ||
| data?: { graph?: SceneGraphLike; scene?: { graph?: SceneGraphLike } } | ||
| graph?: SceneGraphLike | ||
| } | ||
| const graph = body.data?.graph ?? body.data?.scene?.graph ?? body.graph ?? null | ||
| const urls = collectGraphAssetUrls(graph) | ||
| if (urls === null) return null | ||
| for (const url of urls) keep.add(url) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| return [...keep] | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Explicit GC: delete IndexedDB Files that no persisted scene references. | ||
| * | ||
| * **Do not call this from scene mount / load.** Automatic mount-time GC races | ||
| * keepalive flushes, other tabs, and uploads that have hit IndexedDB but are | ||
| * not yet on a node; a sequential GET per scene also burns the shared API rate | ||
| * bucket used by autosave (#733 review). Call only when the host can prove: | ||
| * no in-flight writes, a complete scene inventory, and spare rate budget. | ||
| * Pass references from any other recoverable snapshots in extraKeepUrls. | ||
| * | ||
| * `getLiveGraph` is re-read immediately before sweeping so both nodes and | ||
| * unsaved material texture references survive. | ||
| * No-ops when the full keep-set cannot be built (never partial-sweeps). | ||
| */ | ||
| export async function runLocalAssetGc( | ||
| getLiveGraph: () => SceneGraphLike, | ||
| extraKeepUrls: Iterable<string> = [], | ||
| ): Promise<number | null> { | ||
| const keep = await collectAllPersistedAssetUrls(getLiveGraph()) | ||
| if (keep === null) return null | ||
|
|
||
| const finalKeep = new Set<string>(keep) | ||
| const liveUrls = collectGraphAssetUrls(getLiveGraph()) | ||
| if (liveUrls === null) return null | ||
| for (const url of liveUrls) { | ||
| finalKeep.add(url) | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| for (const url of extraKeepUrls) { | ||
| if (typeof url === 'string' && url.startsWith('asset://')) finalKeep.add(url) | ||
| } | ||
| return sweepLocalAssetsExcept(finalKeep) | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /** For tests: collect urls from an arbitrary node map. */ | ||
| export function collectNodeAssetUrlList(nodes: Record<string, unknown>): string[] { | ||
| const urls: string[] = [] | ||
| for (const node of Object.values(nodes)) { | ||
| urls.push(...collectNodeAssetUrls(node as never)) | ||
| } | ||
| return urls | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.