mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2025-12-05 18:20:39 -08:00
* feat: Skip restart if file system watch is enabled - the watcher will handle file changes automatically * fix: sometimes change sync interval not working fixes #310 * fix: Return false on sync failure - no successful changes were made fixes #558 * fix: step that is wrong * feat: monitoring subwiki * AI added waitForSSEReady * Revert "AI added waitForSSEReady" This reverts commit983b1c623c. * fix: error on frontend loading worker thread * fix * Update wiki.ts * auto reload view and click subwiki icon * Refactor sync echo prevention and improve logging Removed frontend-side echo prevention logic in ipcSyncAdaptor, relying solely on backend file exclusion for echo prevention. Improved console log wrappers to preserve native behavior and added a log statement to setupSSE. Updated test steps and file modification logic to better simulate external edits without modifying timestamps. Added internal documentation on sync architecture. * feat: deboucne and prevent data race when write file * Update watch-filesystem-adaptor.ts * rename camelcase * Update filesystemPlugin.feature * Fix sync interval timezone handling and add tests Refactored syncDebounceInterval logic in Sync.tsx to be timezone-independent, ensuring correct interval storage and display across all timezones. Added comprehensive tests in Sync.timezone.test.ts to verify correct behavior and document previous timezone-related bugs. fixes #310 * i18n for notification * Update index.tsx * fix: potential symlinks problem of subwiki * Update Sync.timezone.test.ts * lint * Implement backoff for file existence check Refactor file existence check to use backoff strategy and add directory tree retrieval for error reporting. * Update BACKOFF_OPTIONS with new configuration * Update wiki.ts * remove log * Update wiki.ts * fix: draft not move to sub * Update filesystemPlugin.feature * fix: routing tw logger to file * Update filesystemPlugin.feature * test: use id to check view load and sse load * Optimize test steps and screenshot logic Removed unnecessary short waits in filesystemPlugin.feature and increased wait time for tiddler state to settle. Updated application.ts to skip screenshots for wait steps, reducing redundant screenshots during test execution. * Check if the WebContents is actually loaded and remove fake webContentsViewHelper.new.ts created by AI * Update view.ts * fix: prevent echo by exclude title * test: Then file "Draft of '新条目'.tid" should not exist in "{tmpDir}/wiki/tiddlers" * Revert "fix: prevent echo by exclude title" This reverts commit86aa838d24. * fix: when move file to subwiki, delete old file * fix: prevent ipc echo change back to frontend * test: view might take longer to load * fix: minor issues * test: fix cleanup timeout * Update cleanup.ts * feat: capture webview screenshot * Update filesystemPlugin.feature * Update SyncArchitecture.md * rename * test: add some time to easy failed steps * Separate logs by test scenario for easier debugging * Update selectors for add and confirm buttons in tests Changed the CSS selectors for the add tiddler and confirm buttons in the filesystem plugin feature tests to use :has() with icon classes. This improves selector robustness and aligns with UI changes. * Ensure window has focus and is ready * Update window.ts * fix: webview screenshot capture prevent mini window to close * fix: Failed to take screenshot: Error: ENAMETOOLONG: name too long, open '/home/runner/work/TidGi-Desktop/TidGi-Desktop/userData-test/logs/screenshots/Agent workflow - Create notes- update embeddings- then search/2025-10-30T11-46-28-891Z-I type -在 wiki 工作区创建一个名为 AI Agent Guide 的笔记-内容是-智能体是一种可以执行任务的AI系统-它可以使用工具-搜索信息并与用户交互- in -chat input- element with selec-PASSED-page.png' * Update window.ts * feat: remove deprecated symlink subwiki approach * Update wiki.ts * fix: remove AI buggy bring window to front cause mini window test to fail * lint * Adjust wait time for draft saving in filesystemPlugin Increased wait time for file system plugin to save draft. * Adjust wait time for tiddler state stabilization Increased wait time to ensure tiddler state settles properly. * Refactor release workflow to simplify dependency installation Removed installation steps for x64 and arm64 dependencies, and adjusted the build process for plugins and native modules. * Enhance wait for IPC in filesystemPlugin feature Added a wait time to improve reliability of content update verification in CI.
10 KiB
10 KiB
Sync Architecture: IPC and Watch-FS Plugins
This document describes how the tidgi-ipc-syncadaptor (frontend) and watch-filesystem-adaptor (backend) plugins work together to provide real-time bidirectional synchronization between the TiddlyWiki in-memory store and the file system.
Architecture Overview
Frontend (Browser) Backend (Node.js Worker)
┌─────────────────┐ ┌──────────────────────┐
│ TiddlyWiki │ │ TiddlyWiki (Server) │
│ In-Memory Store │◄──────►│ File System Adaptor │
└────────┬────────┘ └──────────┬───────────┘
│ │
│ IPC Sync │ Watch-FS
│ Adaptor │ Adaptor
│ │
├─ Save to FS ──────────────►│
│ │
│◄───── SSE Events ──────────┤
│ (File Changes) │
│ │
│ ├─ nsfw Watcher
│ │ (File System)
└────────────────────────────┘
Key Design Principles
1. Single Source of Truth: File System
- Backend watch-fs monitors the file system using
nsfwlibrary - All file changes (external edits, saves from frontend) flow through file system
- Backend wiki state reflects file system state
2. Two-Layer Echo Prevention
IPC Layer (First Defense):
ipcServerRoutes.tstracks recently saved tiddlers inrecentlySavedTiddlersSet- When
wiki.addTiddler()triggers change event, filter out tiddlers in the Set - Prevents frontend from receiving its own save operations as change notifications
Watch-FS Layer (Second Defense):
- When saving, watch-fs temporarily excludes file from monitoring for 200ms
- Prevents watcher from detecting the file write operation
- Combined with IPC filtering, ensures no echo even for rapid successive saves
3. SSE-like Change Notification (IPC Observable)
- Backend sends change events to frontend via IPC (not real SSE, but Observable pattern via
ipc-cat) - Frontend subscribes to
getWikiChangeObserver$observable fromwindow.observables.wiki - Change events trigger tw's
syncFromServer()to pull updates from backend, it will in return call ourgetUpdatedTiddlers
Plugin Responsibilities
Frontend: tidgi-ipc-syncadaptor
Purpose: Bridge between frontend TiddlyWiki and backend file system
IPC Communication Flow:
- Frontend plugin calls
window.service.wikimethods (provided by preload script) - Preload script uses
setupIpcServerRoutesHandlers.ts(runs in view context) to set up IPC protocol handlers - These handlers forward requests to
ipcServerRoutes.tsin wiki worker via IPC - Uses
tidgi://custom protocol for RESTful-like API
Key Functions:
saveTiddler(): Send tiddler to backend via IPC →putTiddlerinipcServerRoutes.tsloadTiddler(): Request tiddler from backend via IPC →getTiddlerinipcServerRoutes.tsdeleteTiddler(): Request deletion via IPC →deleteTiddlerinipcServerRoutes.tssetupSSE(): Subscribe to file change events from backend (via IPC Observable, not real SSE)getUpdatedTiddlers(): Provide list of changed tiddlers to syncer
Echo Prevention Strategy:
Frontend plugin does NOT implement echo detection because:
- Cannot distinguish between "save to fs and watch fs echo back" and "external user text edit with unchanged 'modified' timestamp metadata"
- Echo prevention is handled by backend at two levels:
- IPC level:
ipcServerRoutes.tsfilters out change events from IPC saves usingrecentlySavedTiddlersSet - Watch-FS level:
watch-filesystem-adaptortemporarily excludes files during save operations
- IPC level:
Backend: watch-filesystem-adaptor
Purpose: Monitor file system and maintain wiki state
Key Functions:
saveTiddler(): Write to file system with temporary exclusiondeleteTiddler(): Remove file with temporary exclusioninitializeFileWatching(): Setupnsfwwatcher for main wiki and sub-wikishandleFileAddOrChange(): Load changed files into wikihandleFileDelete(): Remove deleted tiddlers from wiki
Two-Layer Echo Prevention:
Layer 1: IPC Level (in ipcServerRoutes.ts)
async putTiddler(title, fields) {
// Mark as recently saved to prevent echo
this.recentlySavedTiddlers.add(title);
// This triggers 'change' event synchronously
this.wikiInstance.wiki.addTiddler(new Tiddler(fields));
// Change event handler filters it out and removes the mark
}
// Change event handler
wiki.addEventListener('change', (changes) => {
const filteredChanges = {};
for (const title in changes) {
if (this.recentlySavedTiddlers.has(title)) {
// Filter out echo from IPC save
this.recentlySavedTiddlers.delete(title);
continue;
}
filteredChanges[title] = changes[title];
}
// Only send filtered changes to frontend
observer.next(filteredChanges);
});
Layer 2: Watch-FS Level (in WatchFileSystemAdaptor.ts)
async saveTiddler(tiddler) {
const filepath = await this.getTiddlerFileInfo(tiddler);
// 1. Exclude file BEFORE saving
await this.excludeFile(filepath);
// 2. Save to file system (calls parent saveTiddler)
await super.saveTiddler(tiddler);
// 3. Re-include after delay
this.scheduleFileInclusion(filepath);
// After 200ms: includeFile(filepath) removes exclusion
}
File Change Flow:
handleNsfwEvents(events) {
events.forEach(event => {
const filepath = path.join(event.directory, event.file);
// Skip if file is excluded (being saved)
if (this.excludedFiles.has(filepath)) return;
// Load changed file into wiki
const tiddler = $tw.loadTiddlersFromFile(filepath);
$tw.syncadaptor.wiki.addTiddler(tiddler);
// Wiki change event fires → SSE sends to frontend
});
}
Data Flow Examples
Example 1: User Edits in Frontend
1. User clicks save in browser
├─► Frontend: saveTiddler() called
│
2. IPC call to backend
├─► Backend: receives putTiddler request
│
3. Backend IPC layer: Mark tiddler in recentlySavedTiddlers
├─► ipcServerRoutes.ts adds title to Set
│
4. Backend: wiki.addTiddler(tiddler)
├─► Triggers 'change' event
├─► Event handler checks recentlySavedTiddlers
├─► Filters out this change (IPC echo prevention)
├─► Removes title from recentlySavedTiddlers
│
5. Backend Watch-FS layer: excludeFile(filepath)
├─► File added to excludedFiles set
│
6. Backend: write to file system
├─► File content updated on disk
│
7. nsfw detects file change
├─► But file is in excludedFiles
├─► Change event ignored (Watch-FS echo prevention)
│
8. After 200ms delay
├─► includeFile(filepath) removes exclusion
Example 2: External Editor Modifies File
1. User edits file in VSCode/Vim
├─► File content changes on disk
│
2. nsfw detects file change
├─► File NOT in excludedFiles (not being saved)
│
3. handleFileAddOrChange() called
├─► Load tiddler from file
├─► wiki.addTiddler(tiddler)
│
4. Wiki fires 'change' event
├─► ipcServerRoutes.ts checks recentlySavedTiddlers
├─► Title NOT in Set (external edit, not IPC save)
├─► Change passes through filter
├─► IPC Observable sends event to frontend (via ipc-cat)
│
5. Frontend receives change event
├─► Adds to updatedTiddlers.modifications
├─► Triggers syncFromServer()
│
6. Frontend: loadTiddler() via IPC
├─► Gets latest tiddler from backend
├─► Updates frontend wiki
├─► UI re-renders with new content
Example 3: Sub-Wiki Synchronization
1. Main wiki has sub-wiki folder linked by tag
├─► watch-fs detects sub-wiki in tiddlywiki.info
│
2. watch-fs starts additional watcher
├─► Monitors SubWiki/ folder
│
3. User saves tiddler with tag in frontend
├─► Backend determines file should go to SubWiki/
├─► Saves to SubWiki/Tiddler.tid
│
4. Sub-wiki watcher detects new file
├─► (Excluded during save, so no echo)
│
5. External edit in SubWiki/Tiddler.tid
├─► Sub-wiki watcher detects change
├─► Updates main wiki's in-memory store
├─► IPC Observable notifies frontend
├─► Frontend syncs and displays update
Key Configuration
File Exclusion
FILE_EXCLUSION_CLEANUP_DELAY_MS = 200: Time to keep file excluded after save- Prevents echo while allowing quick re-detection of external changes
SSE-like Debouncing (IPC Observable)
debounce(syncFromServer, 500): Batch multiple file changes- Reduces unnecessary sync operations
Syncer Polling
pollTimerInterval = 2_147_483_647: Effectively disable polling- All updates come via IPC Observable (event-driven, not polling)
Troubleshooting
Changes Not Appearing in Frontend
- Check IPC Observable connection: Look for
[test-id-SSE_READY]in logs - Verify watch-fs is running: Look for
[test-id-WATCH_FS_STABILIZED] - Check file exclusion: Should see
[WATCH_FS_EXCLUDE]and[WATCH_FS_INCLUDE]
Echo/Duplicate Updates
- Verify exclusion timing: 200ms should be sufficient
- Check for multiple watchers on same path
- Ensure frontend isn't doing its own timestamp-based filtering
Sub-Wiki Not Syncing
- Check sub-wiki detection: Look for
[WATCH_FS_SUBWIKI]logs - Verify tiddlywiki.info has correct configuration
- Check workspace
subWikiFolderssetting
Not Recommended
- ❌ Timestamp-based echo detection (already tried, unreliable)
- ❌ Frontend-side file watching (duplicates backend effort)
- ❌ Polling-based synchronization (SSE is better)