Skip to content

The agent that grows with you

fix(desktop): resolve home-relative attachment refs in preview/download

Tyler Lyonc7f793527224
diff --git a/apps/desktop/electron/hardening.test.ts b/apps/desktop/electron/hardening.test.ts
index aa2cdfc0301..48c2c1b3121 100644
--- a/apps/desktop/electron/hardening.test.ts
+++ b/apps/desktop/electron/hardening.test.ts
@@ -14,6 +14,7 @@ import {
DEFAULT_FETCH_TIMEOUT_MS,
enableBasicPasswordStoreEncryption,
encryptDesktopSecret,
+ homeRelativeAttachmentCandidates,
readFileDataUrlForIpc,
resolveDirectoryForIpc,
resolvePersistedRemoteToken,
@@ -1045,3 +1046,57 @@ test('saveRegistryConnection republishes the registry to renderers on EVERY succ
'a non-dial-material save must republish the registry snapshot (#95393)'
)
})
+
+// ---------------------------------------------------------------------------
+// homeRelativeAttachmentCandidates (#115609)
+// ---------------------------------------------------------------------------
+
+test('homeRelativeAttachmentCandidates tries the home dir and the HERMES_HOME attachments dir', () => {
+ const candidates = homeRelativeAttachmentCandidates(
+ 'AppData/Local/hermes/attachments/foo.xlsx',
+ '/Users/alice',
+ '/Users/alice/AppData/Local/hermes'
+ )
+
+ assert.deepEqual(candidates, [
+ path.join('/Users/alice', 'AppData/Local/hermes/attachments/foo.xlsx'),
+ path.join('/Users/alice/AppData/Local/hermes', 'attachments', 'foo.xlsx')
+ ])
+})
+
+test('homeRelativeAttachmentCandidates normalizes Windows backslashes before joining', () => {
+ const candidates = homeRelativeAttachmentCandidates(
+ 'AppData\\Local\\hermes\\attachments\\foo.xlsx',
+ '/Users/alice',
+ '/Users/alice/.hermes'
+ )
+
+ assert.equal(candidates[0], path.join('/Users/alice', 'AppData/Local/hermes/attachments/foo.xlsx'))
+})
+
+test('homeRelativeAttachmentCandidates returns nothing for an absolute path', () => {
+ assert.deepEqual(homeRelativeAttachmentCandidates('/already/absolute/foo.xlsx', '/Users/alice', '/Users/alice/.hermes'), [])
+})
+
+test('homeRelativeAttachmentCandidates returns nothing for a file: URL', () => {
+ assert.deepEqual(
+ homeRelativeAttachmentCandidates('file:///already/resolved/foo.xlsx', '/Users/alice', '/Users/alice/.hermes'),
+ []
+ )
+})
+
+test('homeRelativeAttachmentCandidates returns nothing for empty input', () => {
+ assert.deepEqual(homeRelativeAttachmentCandidates('', '/Users/alice', '/Users/alice/.hermes'), [])
+ assert.deepEqual(homeRelativeAttachmentCandidates(' ', '/Users/alice', '/Users/alice/.hermes'), [])
+})
+
+test('homeRelativeAttachmentCandidates second candidate falls back to basename only', () => {
+ // A ref that lost its directory prefix entirely still has a shot via the
+ // well-known attachments dir + basename, matching the reported repro shape.
+ const candidates = homeRelativeAttachmentCandidates('foo.xlsx', '/Users/alice', '/Users/alice/.hermes')
+
+ assert.deepEqual(candidates, [
+ path.join('/Users/alice', 'foo.xlsx'),
+ path.join('/Users/alice/.hermes', 'attachments', 'foo.xlsx')
+ ])
+})
diff --git a/apps/desktop/electron/hardening.ts b/apps/desktop/electron/hardening.ts
index 4ea4bc125e2..b75dcad55ae 100644
--- a/apps/desktop/electron/hardening.ts
+++ b/apps/desktop/electron/hardening.ts
@@ -395,6 +395,29 @@ function resolveRequestedPathForIpc(filePath, options: { purpose?: string; baseD
return resolvedPath
}
+/**
+ * Candidate absolute paths to retry when a preview/download target could not
+ * be resolved against the agent's working directory. Attachment references
+ * stored in chat history are frequently HOME-relative (e.g.
+ * "AppData/Local/hermes/attachments/foo.xlsx" on Windows, or
+ * ".hermes/attachments/foo.xlsx" elsewhere) rather than relative to cwd
+ * (#115609). Pure and DI-testable: takes `home`/`hermesHome` as arguments
+ * instead of reaching for `app.getPath('home')` / a module-level constant.
+ * Returns an empty array for absolute paths, `file:` URLs, and empty input —
+ * those already had their one real resolution attempt upstream.
+ */
+function homeRelativeAttachmentCandidates(raw, home, hermesHome) {
+ const trimmed = String(raw || '').trim()
+
+ if (!trimmed || /^file:/i.test(trimmed) || path.isAbsolute(trimmed)) {
+ return []
+ }
+
+ const normalized = trimmed.replace(/\\/g, '/')
+
+ return [path.join(home, normalized), path.join(hermesHome, 'attachments', path.basename(normalized))]
+}
+
async function statForIpc(fsImpl: { promises: { stat: typeof fs.promises.stat } }, resolvedPath, purpose, typeLabel) {
try {
return await fsImpl.promises.stat(resolvedPath)
@@ -538,6 +561,7 @@ export {
DEFAULT_FETCH_TIMEOUT_MS,
enableBasicPasswordStoreEncryption,
encryptDesktopSecret,
+ homeRelativeAttachmentCandidates,
readFileDataUrlForIpc,
rejectUnsafePathSyntax,
resolveDirectoryForIpc,
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 532901b1834..5b983d119f2 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -235,6 +235,7 @@ import {
DEFAULT_FETCH_TIMEOUT_MS,
enableBasicPasswordStoreEncryption,
encryptDesktopSecret as encryptDesktopSecretStrict,
+ homeRelativeAttachmentCandidates,
readFileDataUrlForIpc,
resolvePersistedRemoteToken,
resolveReadableFileForIpc,
@@ -6437,6 +6438,21 @@ async function previewFileTarget(rawTarget, baseDir) {
purpose: 'Preview target'
})
+ // Attachment references stored in chat history are frequently HOME-relative
+ // (e.g. "AppData/Local/hermes/attachments/foo.xlsx" on Windows, or
+ // ".hermes/attachments/foo.xlsx" elsewhere) rather than relative to the
+ // agent's working directory. The primary resolution above only tries
+ // `base` (the working dir), so such a ref never exists there and the
+ // preview/download 404s even though the file is present on disk (#115609).
+ if (!fileExists(resolved) && !directoryExists(resolved)) {
+ for (const candidate of homeRelativeAttachmentCandidates(raw, app.getPath('home'), HERMES_HOME)) {
+ if (fileExists(candidate)) {
+ resolved = candidate
+ break
+ }
+ }
+ }
+
if (directoryExists(resolved)) {
resolved = path.join(resolved, 'index.html')
}