@@ -43,7 +43,7 @@ func New(d db.Store, engine *sync.Engine) *Watcher {
4343}
4444
4545// Events polls the database for session changes and signals the
46- // returned channel when the message count changes. This is
46+ // returned channel when the session version changes. This is
4747// decoupled from file I/O — the file watcher handles syncing
4848// files to the database, and this monitor detects the resulting
4949// DB changes.
@@ -60,15 +60,15 @@ func (w *Watcher) Events(
6060 defer close (ch )
6161
6262 // Seed initial state from the database.
63- lastCount , lastDBMtime , _ := w .db .GetSessionVersion (
63+ lastCount , lastDBVersion , _ := w .db .GetSessionVersion (
6464 sessionID ,
6565 )
6666
6767 if w .engine == nil {
6868 // PG read mode: poll GetSessionVersion only,
6969 // no file watching or fallback sync.
7070 w .pollDBOnly (ctx , ch , sessionID ,
71- lastCount , lastDBMtime )
71+ lastCount , lastDBVersion )
7272 return
7373 }
7474
@@ -91,7 +91,7 @@ func (w *Watcher) Events(
9191 changed := w .checkDBForChanges (
9292 sessionID ,
9393 & lastCount ,
94- & lastDBMtime ,
94+ & lastDBVersion ,
9595 & sourcePath ,
9696 & lastFileMtime ,
9797 & fileMtimeChangedAt ,
@@ -114,7 +114,7 @@ func (w *Watcher) Events(
114114// no sync engine or file watcher.
115115func (w * Watcher ) pollDBOnly (
116116 ctx context.Context , ch chan <- struct {},
117- sessionID string , lastCount int , lastDBMtime int64 ,
117+ sessionID string , lastCount int , lastDBVersion int64 ,
118118) {
119119 ticker := time .NewTicker (PollInterval )
120120 defer ticker .Stop ()
@@ -124,10 +124,10 @@ func (w *Watcher) pollDBOnly(
124124 case <- ctx .Done ():
125125 return
126126 case <- ticker .C :
127- count , dbMtime , ok := w .db .GetSessionVersion (sessionID )
128- if ok && (count != lastCount || dbMtime != lastDBMtime ) {
127+ count , dbVersion , ok := w .db .GetSessionVersion (sessionID )
128+ if ok && (count != lastCount || dbVersion != lastDBVersion ) {
129129 lastCount = count
130- lastDBMtime = dbMtime
130+ lastDBVersion = dbVersion
131131 select {
132132 case ch <- struct {}{}:
133133 case <- ctx .Done ():
@@ -138,28 +138,25 @@ func (w *Watcher) pollDBOnly(
138138 }
139139}
140140
141- // checkDBForChanges polls the database for a session's
142- // message_count and file_mtime. If either changed, it
143- // returns true. As a fallback, it monitors source file
144- // mtime and triggers a direct sync when the watcher
145- // hasn't updated the DB.
141+ // checkDBForChanges polls the database for a session version change.
142+ // As a fallback, it monitors source file mtime and triggers a direct
143+ // sync when the watcher hasn't updated the DB.
146144func (w * Watcher ) checkDBForChanges (
147145 sessionID string ,
148146 lastCount * int ,
149- lastDBMtime * int64 ,
147+ lastDBVersion * int64 ,
150148 sourcePath * string ,
151149 lastFileMtime * int64 ,
152150 fileMtimeChangedAt * time.Time ,
153151) bool {
154- // Primary: check if the DB has new data (message count
155- // or file_mtime changed, covering both message appends
156- // and metadata-only updates like progress events).
157- if count , dbMtime , ok := w .db .GetSessionVersion (
152+ // Primary: check if the DB has new data. The version marker covers
153+ // message appends and metadata/content-only updates.
154+ if count , dbVersion , ok := w .db .GetSessionVersion (
158155 sessionID ,
159156 ); ok && (count != * lastCount ||
160- dbMtime != * lastDBMtime ) {
157+ dbVersion != * lastDBVersion ) {
161158 * lastCount = count
162- * lastDBMtime = dbMtime
159+ * lastDBVersion = dbVersion
163160 // DB was updated; clear any pending fallback.
164161 * fileMtimeChangedAt = time.Time {}
165162 return true
@@ -208,12 +205,12 @@ func (w *Watcher) checkDBForChanges(
208205 return false
209206 }
210207 // Re-check the DB after syncing.
211- if count , dbMtime , ok := w .db .GetSessionVersion (
208+ if count , dbVersion , ok := w .db .GetSessionVersion (
212209 sessionID ,
213210 ); ok && (count != * lastCount ||
214- dbMtime != * lastDBMtime ) {
211+ dbVersion != * lastDBVersion ) {
215212 * lastCount = count
216- * lastDBMtime = dbMtime
213+ * lastDBVersion = dbVersion
217214 return true
218215 }
219216 }
0 commit comments