Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(client-usage): preserve activity bounds across out-of-order reports (#10375)

Frezc4b517c651581
diff --git a/server/internal/handler/client_usage_test.go b/server/internal/handler/client_usage_test.go
index 58e4b0006a..51ea85ff97 100644
--- a/server/internal/handler/client_usage_test.go
+++ b/server/internal/handler/client_usage_test.go
@@ -6,7 +6,10 @@ import (
"net/http/httptest"
"testing"
+ "github.com/google/uuid"
+ "github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/middleware"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
)
func ptrInt32(value int32) *int32 { return &value }
@@ -89,3 +92,55 @@ func TestUpsertClientUsageKeepsRuntimeSnapshotOnActivityRefresh(t *testing.T) {
t.Fatalf("daily row = count %d runtime %d online %d", rowCount, runtimeCount, onlineCount)
}
}
+
+func TestUpsertClientUsageOutOfOrderTransactions(t *testing.T) {
+ installID := uuid.New()
+ t.Cleanup(func() {
+ dbfx.Exec(t, "DELETE FROM client_usage_daily WHERE user_id = $1 AND install_id = $2", testUserID, installID)
+ })
+ ctx := t.Context()
+ params := db.UpsertClientUsageDailyParams{
+ UserID: parseUUID(testUserID), InstallID: pgtype.UUID{Bytes: installID, Valid: true},
+ ClientType: "web", ClientVersion: "0.1.0", Os: "linux",
+ }
+
+ older, err := testPool.Begin(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer older.Rollback(ctx)
+ var first pgtype.Timestamptz
+ if err := older.QueryRow(ctx, "SELECT CURRENT_TIMESTAMP FROM pg_sleep(0.01)").Scan(&first); err != nil {
+ t.Fatal(err)
+ }
+
+ // A newer transaction inserts and commits while the older one is still open.
+ inserted, err := testHandler.Queries.UpsertClientUsageDaily(ctx, params)
+ if err != nil {
+ t.Fatal(err)
+ }
+ updated, err := testHandler.Queries.WithTx(older).UpsertClientUsageDaily(ctx, params)
+ if err != nil {
+ t.Fatalf("older transaction could not update the daily row: %v", err)
+ }
+ if err := older.Commit(ctx); err != nil {
+ t.Fatal(err)
+ }
+ if !updated.FirstActiveAt.Time.Equal(first.Time) || !updated.LastActiveAt.Time.Equal(inserted.LastActiveAt.Time) ||
+ !updated.FirstActiveAt.Time.Before(updated.LastActiveAt.Time) {
+ t.Fatalf("daily row must span both reports: first=%v last=%v, want %v to %v",
+ updated.FirstActiveAt.Time, updated.LastActiveAt.Time, first.Time, inserted.LastActiveAt.Time)
+ }
+
+ refreshed, err := testHandler.Queries.UpsertClientUsageDaily(ctx, params)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !refreshed.FirstActiveAt.Time.Equal(first.Time) || !refreshed.LastActiveAt.Time.After(updated.LastActiveAt.Time) {
+ t.Fatalf("subsequent report must advance only the latest activity: first=%v last=%v",
+ refreshed.FirstActiveAt.Time, refreshed.LastActiveAt.Time)
+ }
+ if count := dbfx.Count(t, "SELECT count(*) FROM client_usage_daily WHERE user_id = $1 AND install_id = $2", testUserID, installID); count != 1 {
+ t.Fatalf("got %d daily rows, want one", count)
+ }
+}
diff --git a/server/pkg/db/generated/client_usage.sql.go b/server/pkg/db/generated/client_usage.sql.go
index 1f28ca4c02..bbcc330e47 100644
--- a/server/pkg/db/generated/client_usage.sql.go
+++ b/server/pkg/db/generated/client_usage.sql.go
@@ -49,7 +49,8 @@ ON CONFLICT (user_id, client_type, install_id, activity_date) DO UPDATE SET
workspace_id = COALESCE(EXCLUDED.workspace_id, client_usage_daily.workspace_id),
client_version = EXCLUDED.client_version,
os = EXCLUDED.os,
- last_active_at = EXCLUDED.last_active_at,
+ first_active_at = LEAST(client_usage_daily.first_active_at, EXCLUDED.first_active_at),
+ last_active_at = GREATEST(client_usage_daily.last_active_at, EXCLUDED.last_active_at),
runtime_probed_at = CASE WHEN $7::boolean THEN EXCLUDED.runtime_probed_at ELSE client_usage_daily.runtime_probed_at END,
probe_result = CASE WHEN $7::boolean THEN EXCLUDED.probe_result ELSE client_usage_daily.probe_result END,
runtime_count = CASE WHEN $7::boolean THEN EXCLUDED.runtime_count ELSE client_usage_daily.runtime_count END,
diff --git a/server/pkg/db/queries/client_usage.sql b/server/pkg/db/queries/client_usage.sql
index dfd66219d0..185cb4f3f9 100644
--- a/server/pkg/db/queries/client_usage.sql
+++ b/server/pkg/db/queries/client_usage.sql
@@ -36,7 +36,8 @@ ON CONFLICT (user_id, client_type, install_id, activity_date) DO UPDATE SET
workspace_id = COALESCE(EXCLUDED.workspace_id, client_usage_daily.workspace_id),
client_version = EXCLUDED.client_version,
os = EXCLUDED.os,
- last_active_at = EXCLUDED.last_active_at,
+ first_active_at = LEAST(client_usage_daily.first_active_at, EXCLUDED.first_active_at),
+ last_active_at = GREATEST(client_usage_daily.last_active_at, EXCLUDED.last_active_at),
runtime_probed_at = CASE WHEN sqlc.arg('has_runtime_probe')::boolean THEN EXCLUDED.runtime_probed_at ELSE client_usage_daily.runtime_probed_at END,
probe_result = CASE WHEN sqlc.arg('has_runtime_probe')::boolean THEN EXCLUDED.probe_result ELSE client_usage_daily.probe_result END,
runtime_count = CASE WHEN sqlc.arg('has_runtime_probe')::boolean THEN EXCLUDED.runtime_count ELSE client_usage_daily.runtime_count END,