@@ -262,6 +262,68 @@ func TestPushMessageFlagsRewriteRegression(t *testing.T) {
262262 "private chain of thought" )
263263}
264264
265+ func TestPushMessageNanosecondTimestampNoRewriteRegression (t * testing.T ) {
266+ pgURL := testPGURL (t )
267+
268+ const schema = "agentsview_push_msgtime_nanos_test"
269+ pg , err := Open (pgURL , schema , true )
270+ require .NoError (t , err , "Open" )
271+ defer pg .Close ()
272+
273+ ctx := context .Background ()
274+ _ , err = pg .Exec (`DROP SCHEMA IF EXISTS ` + schema + ` CASCADE` )
275+ require .NoError (t , err , "drop schema" )
276+ require .NoError (t , EnsureSchema (ctx , pg , schema ), "EnsureSchema" )
277+
278+ localDB , err := db .Open (filepath .Join (t .TempDir (), "local.db" ))
279+ require .NoError (t , err , "db.Open" )
280+ defer localDB .Close ()
281+
282+ sync := & Sync {
283+ pg : pg ,
284+ local : localDB ,
285+ machine : "test-machine" ,
286+ schema : schema ,
287+ schemaDone : true ,
288+ }
289+
290+ const sessID = "message-nanotime-rewrite-001"
291+ sess := db.Session {
292+ ID : sessID ,
293+ Project : "test-proj" ,
294+ Machine : "test-machine" ,
295+ Agent : "shelley" ,
296+ MessageCount : 1 ,
297+ UserMessageCount : 1 ,
298+ CreatedAt : "2026-01-01T00:00:00Z" ,
299+ }
300+ require .NoError (t , localDB .UpsertSession (sess ), "UpsertSession" )
301+ require .NoError (t , localDB .InsertMessages ([]db.Message {{
302+ SessionID : sessID ,
303+ Ordinal : 1 ,
304+ Role : "user" ,
305+ Content : "question" ,
306+ ContentLength : len ("question" ),
307+ Timestamp : "2026-01-01T00:00:00.123456789Z" ,
308+ }}), "InsertMessages" )
309+
310+ _ , err = sync .Push (ctx , false , nil )
311+ require .NoError (t , err , "Push first timestamp" )
312+ assertPGMessageTimestamp (t , pg , sessID , 1 ,
313+ "2026-01-01T00:00:00.123456Z" )
314+
315+ ctidBefore := pgMessageCTID (t , pg , sessID , 1 )
316+ require .NoError (t , localDB .SetSyncState ("last_push_at" , "" ),
317+ "clearing last_push_at" )
318+ require .NoError (t , localDB .SetSyncState (lastPushBoundaryStateKey , "" ),
319+ "clearing boundary state" )
320+
321+ _ , err = sync .Push (ctx , false , nil )
322+ require .NoError (t , err , "Push same timestamp" )
323+ assert .Equal (t , ctidBefore , pgMessageCTID (t , pg , sessID , 1 ),
324+ "microsecond-equivalent timestamps should hit the fast path" )
325+ }
326+
265327// TestPushSessionTerminationStatus verifies that pushSession round-trips
266328// the termination_status column to PG: a non-nil value writes the string,
267329// and a subsequent push with nil clears the column back to NULL via the
@@ -547,6 +609,40 @@ func assertPGMessageThinking(
547609 assert .Equal (t , wantThinkingText , gotThinkingText )
548610}
549611
612+ func assertPGMessageTimestamp (
613+ t * testing.T ,
614+ pg * sql.DB ,
615+ sessionID string ,
616+ ordinal int ,
617+ want string ,
618+ ) {
619+ t .Helper ()
620+ var gotTime sql.NullTime
621+ require .NoError (t , pg .QueryRow (
622+ `SELECT timestamp FROM messages
623+ WHERE session_id = $1 AND ordinal = $2` ,
624+ sessionID , ordinal ,
625+ ).Scan (& gotTime ), "read pg message timestamp" )
626+ require .True (t , gotTime .Valid , "timestamp should be non-NULL" )
627+ assert .Equal (t , want , FormatISO8601 (gotTime .Time ))
628+ }
629+
630+ func pgMessageCTID (
631+ t * testing.T ,
632+ pg * sql.DB ,
633+ sessionID string ,
634+ ordinal int ,
635+ ) string {
636+ t .Helper ()
637+ var ctid string
638+ require .NoError (t , pg .QueryRow (
639+ `SELECT ctid::text FROM messages
640+ WHERE session_id = $1 AND ordinal = $2` ,
641+ sessionID , ordinal ,
642+ ).Scan (& ctid ), "read pg message ctid" )
643+ return ctid
644+ }
645+
550646// TestPushMessagesSanitizesNULBytes verifies that a message whose
551647// model and source fields carry NUL bytes (observed in production:
552648// the Antigravity gen_metadata heuristic persisted a raw protobuf
0 commit comments