Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: make task run log pairing robust to equal timestamps

Dannyf0cffa001e08
diff --git a/backend/api/v1/rollout_service_converter.go b/backend/api/v1/rollout_service_converter.go
index 7dc7bf349c..56f1dc0964 100644
--- a/backend/api/v1/rollout_service_converter.go
+++ b/backend/api/v1/rollout_service_converter.go
@@ -354,6 +354,23 @@ func convertToTaskRunLog(parent string, logs []*store.TaskRunLog) *v1pb.TaskRunL
func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntry {
var entries []*v1pb.TaskRunLogEntry
+ // task_run_log rows have no per-row sequence and can share a created_at
+ // microsecond, so an end/response event may not directly follow its start
+ // entry. Attach each one to the nearest entry of the matching type that is
+ // still awaiting its end instead of requiring strict adjacency.
+ unpaired := map[v1pb.TaskRunLogEntry_Type][]int{}
+ push := func(t v1pb.TaskRunLogEntry_Type) {
+ unpaired[t] = append(unpaired[t], len(entries)-1)
+ }
+ pop := func(t v1pb.TaskRunLogEntry_Type) *v1pb.TaskRunLogEntry {
+ stack := unpaired[t]
+ if len(stack) == 0 {
+ return nil
+ }
+ i := stack[len(stack)-1]
+ unpaired[t] = stack[:len(stack)-1]
+ return entries[i]
+ }
for _, l := range logs {
switch l.Payload.Type {
case storepb.TaskRunLog_SCHEMA_DUMP_START:
@@ -365,13 +382,11 @@ func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntr
StartTime: timestamppb.New(l.T),
},
})
+ push(v1pb.TaskRunLogEntry_SCHEMA_DUMP)
case storepb.TaskRunLog_SCHEMA_DUMP_END:
- if len(entries) == 0 {
- continue
- }
- prev := entries[len(entries)-1]
- if prev == nil || prev.Type != v1pb.TaskRunLogEntry_SCHEMA_DUMP {
+ prev := pop(v1pb.TaskRunLogEntry_SCHEMA_DUMP)
+ if prev == nil {
continue
}
prev.SchemaDump.EndTime = timestamppb.New(l.T)
@@ -388,13 +403,11 @@ func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntr
Statement: l.Payload.CommandExecute.Statement,
},
})
+ push(v1pb.TaskRunLogEntry_COMMAND_EXECUTE)
case storepb.TaskRunLog_COMMAND_RESPONSE:
- if len(entries) == 0 {
- continue
- }
- prev := entries[len(entries)-1]
- if prev == nil || prev.Type != v1pb.TaskRunLogEntry_COMMAND_EXECUTE {
+ prev := pop(v1pb.TaskRunLogEntry_COMMAND_EXECUTE)
+ if prev == nil {
continue
}
prev.CommandExecute.Response = &v1pb.TaskRunLogEntry_CommandExecute_CommandResponse{
@@ -413,13 +426,11 @@ func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntr
StartTime: timestamppb.New(l.T),
},
})
+ push(v1pb.TaskRunLogEntry_DATABASE_SYNC)
case storepb.TaskRunLog_DATABASE_SYNC_END:
- if len(entries) == 0 {
- continue
- }
- prev := entries[len(entries)-1]
- if prev == nil || prev.Type != v1pb.TaskRunLogEntry_DATABASE_SYNC {
+ prev := pop(v1pb.TaskRunLogEntry_DATABASE_SYNC)
+ if prev == nil {
continue
}
prev.DatabaseSync.EndTime = timestamppb.New(l.T)
@@ -445,13 +456,11 @@ func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntr
StartTime: timestamppb.New(l.T),
},
})
+ push(v1pb.TaskRunLogEntry_PRIOR_BACKUP)
case storepb.TaskRunLog_PRIOR_BACKUP_END:
- if len(entries) == 0 {
- continue
- }
- prev := entries[len(entries)-1]
- if prev == nil || prev.Type != v1pb.TaskRunLogEntry_PRIOR_BACKUP {
+ prev := pop(v1pb.TaskRunLogEntry_PRIOR_BACKUP)
+ if prev == nil {
continue
}
prev.PriorBackup.EndTime = timestamppb.New(l.T)
@@ -467,13 +476,11 @@ func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntr
StartTime: timestamppb.New(l.T),
},
})
+ push(v1pb.TaskRunLogEntry_COMPUTE_DIFF)
case storepb.TaskRunLog_COMPUTE_DIFF_END:
- if len(entries) == 0 {
- continue
- }
- prev := entries[len(entries)-1]
- if prev == nil || prev.Type != v1pb.TaskRunLogEntry_COMPUTE_DIFF {
+ prev := pop(v1pb.TaskRunLogEntry_COMPUTE_DIFF)
+ if prev == nil {
continue
}
prev.ComputeDiff.EndTime = timestamppb.New(l.T)
@@ -511,13 +518,11 @@ func convertToTaskRunLogEntries(logs []*store.TaskRunLog) []*v1pb.TaskRunLogEntr
StartTime: timestamppb.New(l.T),
},
})
+ push(v1pb.TaskRunLogEntry_GHOST_MIGRATION)
case storepb.TaskRunLog_GHOST_MIGRATION_END:
- if len(entries) == 0 {
- continue
- }
- prev := entries[len(entries)-1]
- if prev == nil || prev.Type != v1pb.TaskRunLogEntry_GHOST_MIGRATION {
+ prev := pop(v1pb.TaskRunLogEntry_GHOST_MIGRATION)
+ if prev == nil {
continue
}
prev.GhostMigration.EndTime = timestamppb.New(l.T)
diff --git a/backend/api/v1/rollout_service_converter_test.go b/backend/api/v1/rollout_service_converter_test.go
index cbdd9cab19..94b400bd94 100644
--- a/backend/api/v1/rollout_service_converter_test.go
+++ b/backend/api/v1/rollout_service_converter_test.go
@@ -68,3 +68,76 @@ func TestConvertToTaskRunLogEntries_GhostMigration(t *testing.T) {
require.Equal(t, end.Unix(), entry.GhostMigration.EndTime.AsTime().Unix())
require.Equal(t, "copy failed", entry.GhostMigration.Error)
}
+
+// task_run_log rows can share a created_at microsecond and carry no per-row
+// sequence, so end/response events may not directly follow their start entry.
+func TestConvertToTaskRunLogEntries_TieOrderPairing(t *testing.T) {
+ at := time.Date(2026, 8, 6, 10, 0, 0, 0, time.UTC)
+ execute := func(statement string) *store.TaskRunLog {
+ return &store.TaskRunLog{T: at, Payload: &storepb.TaskRunLog{
+ Type: storepb.TaskRunLog_COMMAND_EXECUTE,
+ CommandExecute: &storepb.TaskRunLog_CommandExecute{Statement: statement},
+ }}
+ }
+ response := func(affectedRows int64) *store.TaskRunLog {
+ return &store.TaskRunLog{T: at, Payload: &storepb.TaskRunLog{
+ Type: storepb.TaskRunLog_COMMAND_RESPONSE,
+ CommandResponse: &storepb.TaskRunLog_CommandResponse{AffectedRows: affectedRows},
+ }}
+ }
+
+ t.Run("response attaches across an interleaved entry", func(t *testing.T) {
+ entries := convertToTaskRunLogEntries([]*store.TaskRunLog{
+ execute("SELECT 1"),
+ {T: at, Payload: &storepb.TaskRunLog{
+ Type: storepb.TaskRunLog_TRANSACTION_CONTROL,
+ TransactionControl: &storepb.TaskRunLog_TransactionControl{Type: storepb.TaskRunLog_TransactionControl_COMMIT},
+ }},
+ response(7),
+ })
+
+ require.Len(t, entries, 2)
+ require.Equal(t, v1pb.TaskRunLogEntry_COMMAND_EXECUTE, entries[0].Type)
+ require.NotNil(t, entries[0].CommandExecute.Response)
+ require.Equal(t, int64(7), entries[0].CommandExecute.Response.AffectedRows)
+ })
+
+ t.Run("crossed responses pair with the nearest unpaired execute", func(t *testing.T) {
+ entries := convertToTaskRunLogEntries([]*store.TaskRunLog{
+ execute("SELECT 1"),
+ execute("SELECT 2"),
+ response(1),
+ response(2),
+ })
+
+ require.Len(t, entries, 2)
+ require.NotNil(t, entries[0].CommandExecute.Response)
+ require.NotNil(t, entries[1].CommandExecute.Response)
+ require.Equal(t, int64(1), entries[1].CommandExecute.Response.AffectedRows)
+ require.Equal(t, int64(2), entries[0].CommandExecute.Response.AffectedRows)
+ })
+
+ t.Run("orphan response is dropped", func(t *testing.T) {
+ entries := convertToTaskRunLogEntries([]*store.TaskRunLog{response(1)})
+ require.Empty(t, entries)
+ })
+
+ t.Run("schema dump end attaches across an interleaved entry", func(t *testing.T) {
+ entries := convertToTaskRunLogEntries([]*store.TaskRunLog{
+ {T: at, Payload: &storepb.TaskRunLog{
+ Type: storepb.TaskRunLog_SCHEMA_DUMP_START,
+ SchemaDumpStart: &storepb.TaskRunLog_SchemaDumpStart{},
+ }},
+ execute("SELECT 1"),
+ {T: at.Add(time.Second), Payload: &storepb.TaskRunLog{
+ Type: storepb.TaskRunLog_SCHEMA_DUMP_END,
+ SchemaDumpEnd: &storepb.TaskRunLog_SchemaDumpEnd{Error: "boom"},
+ }},
+ })
+
+ require.Len(t, entries, 2)
+ require.Equal(t, v1pb.TaskRunLogEntry_SCHEMA_DUMP, entries[0].Type)
+ require.NotNil(t, entries[0].SchemaDump.EndTime)
+ require.Equal(t, "boom", entries[0].SchemaDump.Error)
+ })
+}
diff --git a/backend/store/task_run_log.go b/backend/store/task_run_log.go
index aa5a0dbd9f..ba6defc6f2 100644
--- a/backend/store/task_run_log.go
+++ b/backend/store/task_run_log.go
@@ -58,13 +58,16 @@ func (s *Store) CreateTaskRunLog(ctx context.Context, projectID string, taskRunU
}
func (s *Store) ListTaskRunLogs(ctx context.Context, projectID string, taskRunUID int64) ([]*TaskRunLog, error) {
+ // created_at can tie across entries (no per-row sequence exists); ctid
+ // breaks ties in insertion order, which is stable because the table is
+ // append-only and rows are never updated.
q := qb.Q().Space(`
SELECT
created_at,
payload
FROM task_run_log
WHERE task_run_log.project = ? AND task_run_log.task_run_id = ?
- ORDER BY created_at
+ ORDER BY created_at, ctid
`, projectID, taskRunUID)
sql, args, err := q.ToSQL()