Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): compare shared assets against full archive transcripts, not truncated content

Adam Eivy052db41e1b40
diff --git a/server/services/chatgptImport.js b/server/services/chatgptImport.js
index 693ba918d5..8f3cb88edc 100644
--- a/server/services/chatgptImport.js
+++ b/server/services/chatgptImport.js
@@ -344,6 +344,24 @@ export const extractAssetFileNames = (content) => {
return names;
};
+/**
+ * Every asset filename an import memory references — across BOTH its (possibly
+ * truncated) `content` AND its full archived transcript. The memory `content`
+ * is capped at MAX_MEMORY_CONTENT, so an asset linked only past the truncation
+ * point is invisible in `content` but present in the archive; unioning the two
+ * catches it. Reads the archive only when the memory carries a `sourceRef`.
+ */
+const assetNamesForMemory = async (record) => {
+ const names = extractAssetFileNames(record?.content);
+ if (record?.sourceRef) {
+ const archive = await readArchivedConversation(record.sourceRef);
+ if (typeof archive?.transcript === 'string') {
+ for (const name of extractAssetFileNames(archive.transcript)) names.add(name);
+ }
+ }
+ return names;
+};
+
/**
* Delete the on-disk assets + archived transcript that belonged to a deleted
* `chatgpt-import` memory, leaving no orphans under `data/brain/imports/`.
@@ -357,7 +375,9 @@ export const extractAssetFileNames = (content) => {
* shares its `sourceRef`.
* - An asset id can likewise recur across conversations (ChatGPT reuses a
* `file-service://` id), so an asset is only unlinked when NO survivor
- * still references it.
+ * still references it — compared via the FULL archived transcript, not the
+ * truncated `content`, so an asset linked only in a survivor's truncated
+ * tail isn't wrongly pulled out from under it.
*
* Best-effort: a missing file is a no-op (already gone), and any unlink failure
* is logged but never thrown — cleanup must not turn a successful delete into a
@@ -366,6 +386,10 @@ export const extractAssetFileNames = (content) => {
export async function deleteMemoryAssets(record, survivors = []) {
if (!record || record.source !== 'chatgpt-import') return;
+ // Resolve the deleted record's full asset set BEFORE unlinking its transcript
+ // below (the transcript is one of the sources we read asset links from).
+ const referenced = await assetNamesForMemory(record);
+
// 1. Archived transcript — remove only when no surviving import shares it (a
// re-import of the same conversation reuses the same `<id>.json` archive).
const ref = String(record.sourceRef || '');
@@ -376,20 +400,13 @@ export async function deleteMemoryAssets(record, survivors = []) {
});
}
- // 2. Served assets — only those no surviving import memory still references.
- // Both sides are compared via the SAME truncated `content` (not the full
- // archived transcript), which keeps the comparison symmetric: an asset
- // referenced only in the truncated tail of an over-long conversation is
- // invisible on both the deleted record AND its survivors, so it's
- // conservatively KEPT (a harmless orphan) rather than wrongly deleted out
- // from under a survivor that still uses it. Expanding to the full transcript
- // would require reading every surviving import's archive on each delete to
- // stay symmetric — not worth it for the tail-asset edge case.
- const referenced = extractAssetFileNames(record.content);
+ // 2. Served assets — only those no surviving import memory still references
+ // (in its content OR its full archive). Short-circuit when this memory had no
+ // assets so an asset-free conversation never reads a single survivor archive.
if (referenced.size === 0) return;
const stillUsed = new Set();
for (const m of survivors) {
- for (const name of extractAssetFileNames(m?.content)) stillUsed.add(name);
+ for (const name of await assetNamesForMemory(m)) stillUsed.add(name);
}
const orphaned = [...referenced].filter((name) => !stillUsed.has(name));
await Promise.all(orphaned.map((name) =>
diff --git a/server/services/chatgptImport.test.js b/server/services/chatgptImport.test.js
index 59ce94c62f..2e0d1af41a 100644
--- a/server/services/chatgptImport.test.js
+++ b/server/services/chatgptImport.test.js
@@ -343,6 +343,30 @@ describe('chatgptImport service', () => {
expect(existsSync(join(ASSETS_DIR, 'file-solo.png'))).toBe(false);
});
+ it('keeps an asset a survivor references only in its full archive transcript (beyond truncated content)', async () => {
+ await writeAsset('file-tail.png');
+ // Write a survivor's archive whose TRANSCRIPT references the asset, while
+ // the survivor's stored `content` (passed below) does NOT — simulating a
+ // link that fell past the memory's truncation point.
+ await importConversations(parseExport([sampleConversation({
+ id: 'survivor-conv',
+ mapping: {
+ n1: { id: 'n1', parent: null, message: { id: 'm-a', author: { role: 'user' }, content: { parts: ['![tail](/data/brain-imports/file-tail.png)'] }, create_time: 1700000000 } },
+ },
+ current_node: 'n1',
+ })]));
+
+ await deleteMemoryAssets({
+ id: 'mem-del',
+ source: 'chatgpt-import',
+ sourceRef: 'gone.json',
+ content: '![t](/data/brain-imports/file-tail.png)',
+ }, [{ id: 'mem-survivor', source: 'chatgpt-import', sourceRef: 'survivor-conv.json', content: 'no asset link here' }]);
+
+ // The survivor's archive still links the asset, so it must NOT be unlinked.
+ expect(existsSync(join(ASSETS_DIR, 'file-tail.png'))).toBe(true);
+ });
+
it('keeps the archived transcript when a surviving memory shares its sourceRef (same export imported twice)', async () => {
await importConversations(parseExport([sampleConversation()])); // writes conv-1.json archive
expect(existsSync(join(TMP, 'imports', 'chatgpt', 'conv-1.json'))).toBe(true);