Skip to content

fix(attachments): restore missing cached files#358

Open
ThunderTr77 wants to merge 3 commits into
TouchAI-org:mainfrom
ThunderTr77:fix/attachment-cache-restore
Open

fix(attachments): restore missing cached files#358
ThunderTr77 wants to merge 3 commits into
TouchAI-org:mainfrom
ThunderTr77:fix/attachment-cache-restore

Conversation

@ThunderTr77

Copy link
Copy Markdown
Contributor

Summary

  • Restore the attachment cache file before reusing an existing attachment hash record.
  • Keep draft attachment paths valid after cache cleanup so attachment materialization can still read the file.

Related issue or RFC

AI assistance disclosure

  • Tool(s) used: Local development assistant.
  • Scope of assistance: Identified the cache reuse failure, implemented the fix, and ran validation.
  • Human review or rewrite performed: Reviewed the diff and test coverage before submission.
  • Architecture or boundary impact: No new boundary or schema change.

Testing evidence

  • corepack pnpm --dir apps/desktop exec vitest run --configLoader runner tests/services/AgentService/infrastructure/attachment-storage.test.ts passed.
  • corepack pnpm --dir apps/desktop exec eslint src/services/AgentService/infrastructure/attachments/storage.ts tests/services/AgentService/infrastructure/attachment-storage.test.ts passed.
  • corepack pnpm --dir apps/desktop exec prettier --check src/services/AgentService/infrastructure/attachments/storage.ts tests/services/AgentService/infrastructure/attachment-storage.test.ts passed.
  • corepack pnpm --dir apps/desktop run test:typecheck passed.
  • git diff --check passed.
  • pnpm test:pr was not run locally; targeted checks cover the changed attachment storage path and CI should run the full suite.

Did you follow TDD (test-first) for feature and fix work? A regression test was added with the fix.

corepack pnpm --dir apps/desktop exec vitest run --configLoader runner tests/services/AgentService/infrastructure/attachment-storage.test.ts
corepack pnpm --dir apps/desktop exec eslint src/services/AgentService/infrastructure/attachments/storage.ts tests/services/AgentService/infrastructure/attachment-storage.test.ts
corepack pnpm --dir apps/desktop exec prettier --check src/services/AgentService/infrastructure/attachments/storage.ts tests/services/AgentService/infrastructure/attachment-storage.test.ts
corepack pnpm --dir apps/desktop run test:typecheck
git diff --check

Risk notes

  • AgentService, runtime, MCP, or schema impact: Attachment storage behavior only; no transport, runtime, MCP, or schema change.
  • database baseline or migration impact: None.
  • release or packaging impact: None.

Screenshots or recordings

Not UI-facing.

Checklist

  • The PR title follows Conventional Commits and is valid for squash merge.
  • This PR is either ready for review or explicitly marked as a Draft PR.
  • I did not use [WIP] or similar title prefixes.
  • If AI materially assisted this PR, I disclosed the tools and scope and I personally reviewed every affected change.
  • I can explain the why, what, and how of this change without relying on an AI tool.
  • If this touches AgentService, runtime, MCP, or schema boundaries, there is an accepted RFC. (N/A: no boundary change.)
  • If this changes architecture or adds a new cross-boundary abstraction, there is an accepted RFC. (N/A.)
  • I ran pnpm test:pr for this code PR, or this is a docs-only change.
  • If I changed Rust behavior or tests, I reviewed pnpm test:coverage:rust or relied on CI coverage evidence. (N/A.)
  • If I changed desktop startup/window/search/popup/settings/E2E paths, I ran pnpm test:e2e locally or documented why CI is the first valid proof. (N/A.)
  • I added tests or explained why tests are not appropriate.
  • I updated docs when behavior changed. (N/A.)

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @ThunderTr77, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@github-actions github-actions Bot added the area:agent-service AgentService and conversation runtime changes label May 31, 2026
@coderabbitai

coderabbitai Bot commented May 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: df1ff231-3732-4bcd-8a9e-0b6a5d6d4375

📥 Commits

Reviewing files that changed from the base of the PR and between be14da6 and 1b06d83.

📒 Files selected for processing (1)
  • apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Frontend Tests
  • GitHub Check: Frontend Quality
  • GitHub Check: Conventional Commits
🔇 Additional comments (1)
apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts (1)

170-174: LGTM!


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Ensured attachment source files are reliably copied into the local cache even when a matching database record already exists, preventing missing cached files.
  • Tests

    • Added tests validating cache persistence and restoration behavior, covering cases where cached files are absent or present alongside existing records.

Walkthrough

This PR ensures attachment cache files are present when reusing database records by adding a conditional cache-restoration helper and refactoring record persistence; tests verify both restoration when missing and reuse when present.

Changes

Attachment cache restoration when reusing existing database records

Layer / File(s) Summary
Cache file restoration helper
apps/desktop/src/services/AgentService/infrastructure/attachments/storage.ts
New ensureAttachmentStorageFile helper that copies a source file to a cache target path only if the target doesn't already exist.
Refactored attachment record persistence with cache guarantee
apps/desktop/src/services/AgentService/infrastructure/attachments/storage.ts
ensureAttachmentRecord now computes cache targetPath before checking for existing DB records and calls ensureAttachmentStorageFile to restore missing cache files even when reusing existing records.
Test infrastructure and cache restoration validation
apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts
Comprehensive test suite with hoisted mocks for DB, filesystem, Tauri, and crypto APIs. Validates restoration: when cache file is missing but DB record exists, the source file is copied to cache and the existing record is reused without creating a new one.
Cache reuse validation
apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts
Validates the reuse case: when both cache file and DB record exist, the record is reused and copyFile is not called.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through code with nimble paws,
Found vanished files and fixed the cause.
If cache is gone, I copy it back,
Reuse the row, keep nothing slack.
Now attachments sing — no missing docs!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'fix(attachments): restore missing cached files' follows conventional commits format, uses a clear fix type, and accurately describes the core change of restoring missing attachment cache files.
Description check ✅ Passed The PR description follows the template structure with all required sections completed: summary, related issue link, AI disclosure, testing evidence, risk notes, and checklist. Sections are appropriately filled with relevant details.
Linked Issues check ✅ Passed The code changes directly address #357's requirement: the new ensureAttachmentStorageFile helper and refactored ensureAttachmentRecord logic now restore missing cache files before reusing existing hash records, matching the expected behavior outlined in the issue.
Out of Scope Changes check ✅ Passed All changes are scoped to attachment storage behavior: the helper function, refactored persistence logic, and regression test directly support the cache restoration objective with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts`:
- Around line 157-173: The test should assert that the existing attachment
record was actually reused: call ensurePersistedAttachmentIndex and capture its
return value, then assert the returned id equals the mocked record id (43) and
also assert createAttachmentRecord (or its mock, e.g.,
createAttachmentRecordMock) was not called, in addition to the existing
expect(copyFileMock).not.toHaveBeenCalled(); this makes reuse explicit—update
the test using the existing mocks findAttachmentByHashMock, copyFileMock, and
createAttachmentRecordMock and verify returned id and lack of
createAttachmentRecord invocation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d1cc5dfb-668e-4d51-b9a5-938b8a030957

📥 Commits

Reviewing files that changed from the base of the PR and between a48361d and be14da6.

📒 Files selected for processing (2)
  • apps/desktop/src/services/AgentService/infrastructure/attachments/storage.ts
  • apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: Desktop E2E Smoke (Windows)
  • GitHub Check: CodeQL (rust)
  • GitHub Check: CodeQL (javascript-typescript)
  • GitHub Check: Frontend Quality
  • GitHub Check: Rust Checks
  • GitHub Check: Frontend Tests
🔇 Additional comments (4)
apps/desktop/src/services/AgentService/infrastructure/attachments/storage.ts (2)

121-127: LGTM!


142-149: LGTM!

apps/desktop/tests/services/AgentService/infrastructure/attachment-storage.test.ts (2)

6-126: LGTM!


128-155: LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent-service AgentService and conversation runtime changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attachment cache record can point at a missing local copy

1 participant