Skip to content

fix: resolve auth cascade errors and optimize sync performance - #2

Open
zwolenik wants to merge 4 commits into
LinkLeong:mainfrom
zwolenik:fix/auth-and-sync-optimization
Open

fix: resolve auth cascade errors and optimize sync performance#2
zwolenik wants to merge 4 commits into
LinkLeong:mainfrom
zwolenik:fix/auth-and-sync-optimization

Conversation

@zwolenik

@zwolenik zwolenik commented Jul 3, 2026

Copy link
Copy Markdown

Problem

After Obsidian restart or token expiry, every sync operation generates a cascade of "Authentication required" errors — one per file in the vault. With hundreds of files, this produces dozens of red error notices.

Root cause: performSync() doesn't verify authentication before starting, and ensureValidToken() returns false (instead of attempting login) when isAuthenticated is false.

Additionally, needsSync() calls getFileStatsV2() individually for each file (N+1 problem), generating hundreds of API requests per sync.

Changes

1. ensureValidToken() — public with auto-login fallback

  • Changed from private to public so SyncManager can call it
  • Instead of returning false when not authenticated, attempts auto-login
  • Prevents the cascade: one login call replaces N error messages

2. Auth check at start of performSync()

  • Calls ensureValidToken() before any sync operations
  • If auth fails, shows a single notice and aborts cleanly
  • No more per-file "Authentication required" errors

3. Batch needsSync() (N+1 fix)

  • Collects all candidate file paths first
  • Sends a single getFileStatsV2() batch request
  • Maps results locally to determine which files need sync
  • Reduces API calls from ~N to 1

4. Proactive token refresh timer

  • Checks token expiry every 5 minutes
  • Refreshes proactively when token expires within 10 minutes
  • Prevents auth failures during auto-sync cycles

Testing

Verified locally:

  • Before: 25+ "Authentication required" errors per sync
  • After: 0 errors, sync completes cleanly
  • Token refreshes automatically before expiry
  • Batch file stats returns 200 OK

Related

Partially addresses sync reliability concerns mentioned in issue discussions.

zwolenik added 4 commits July 3, 2026 09:40
- Make ensureValidToken() public with auto-login fallback
  Previously, if isAuthenticated was false (e.g. after restart),
  every API call would fail with 'Authentication required', generating
  a cascade of errors - one per file in the vault.

- Add auth check at start of performSync()
  Ensures authentication is valid before beginning any sync operations.
  If auth fails, shows a notice and aborts cleanly instead of
  generating N errors.

- Batch needsSync() checks into single API call (N+1 fix)
  Previously, getFileStatsV2() was called individually for each file,
  generating hundreds of API requests. Now all file paths are sent in
  a single batch request, with results mapped locally.

- Add proactive token refresh timer in main plugin
  Checks token expiry every 5 minutes and refreshes proactively when
  token expires within 10 minutes, preventing auth failures during
  auto-sync cycles.

Fixes LinkLeong#1 (indirectly - improves sync reliability)
- Simplify getFilesToSync() to exclusion-only filter (no API calls)
  The mediainfo API does not return modification timestamps, making
  per-file needsSync() checks wasteful. Time comparison is done
  properly in compareFiles() using the file listing API.

- Fix needsSync() to handle mediainfo response format correctly
  Updated to match actual API response structure and provide
  clear documentation of its limitations.

- Add finally blocks to performAutoSync and performManualSync
  Guarantees syncInProgress is always reset, preventing the flag
  from getting stuck after errors in Notice/logError.

- Sync syncInProgress via notifyStatusUpdate callback
  Ensures main.ts and sync-manager.ts stay in sync on the flag.

- Skip directories in remote-only download operations
  getAllFilesRecursive returns both files and directories. The
  compareFiles method was creating download operations for remote
  directories that don't exist locally, causing 400 errors on
  the download API endpoint.
Security:
- Remove plaintext password from login request logs
- Gate window.ozsyncPlugin behind debugMode setting
- Remove duplicate Notice popup from log() method (showErrorNotice is
  the single source for user-facing error popups)

Binary file handling:
- Change downloadFile to use responseType arraybuffer instead of text
  This prevents silent corruption of images, PDFs, and other binary
  files that were previously decoded as UTF-8 text.
- Expand text extension detection: md, txt, csv, json, yaml, html,
  css, js, canvas, xml, ts, tsx, jsx, py, and more are now read as
  text on both upload and download. All other extensions use binary
  read/write paths.
- Fix cloud-browser download and preview to handle ArrayBuffer data
Text files decode correctly after parsing JSON envelope.
Binary files have documented limitation due to JSON UTF-8 serialization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant