Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cyclops): restore production usage reporting (#7103)
r33drichardsd1f1903a4d38
diff --git a/libs/fleet/backend/database/migrations/000004_filter_invalid_usage_sandbox_events.sql b/libs/fleet/backend/database/migrations/000004_filter_invalid_usage_sandbox_events.sqlnew file mode 100644index 0000000000..eabd9a454c--- /dev/null+++ b/libs/fleet/backend/database/migrations/000004_filter_invalid_usage_sandbox_events.sql@@ -0,0 +1,83 @@+SET LOCAL ROLE k8s_reporting_owner;+CREATE OR REPLACE FUNCTION k8s_reporting.usage_sandbox_events(+ p_capsule_tenant text,+ p_start timestamptz,+ p_end timestamptz+)+RETURNS TABLE (+ event_id uuid,+ namespace text,+ sandbox_name text,+ sandbox_uid text,+ pool_name text,+ runtime text,+ vm_name text,+ event_type text,+ observed_at timestamptz+)+LANGUAGE plpgsql+STABLE+SECURITY DEFINER+SET search_path = k8s_state, pg_catalog+AS $$+BEGIN+ IF p_capsule_tenant IS NULL OR p_capsule_tenant = '' THEN+ RAISE EXCEPTION 'capsule tenant is required';+ END IF;+ IF p_start IS NULL OR p_end IS NULL OR p_end <= p_start OR p_end - p_start > interval '31 days' THEN+ RAISE EXCEPTION 'usage window must be positive and at most 31 days';+ END IF;++ RETURN QUERY+ WITH tenant_events AS (+ SELECT event.*+ FROM k8s_state.resource_event_outbox AS event+ WHERE event.capsule_tenant = p_capsule_tenant+ AND event.api_group = 'osgym.cua.ai'+ AND event.resource = 'osgymsandboxes'+ AND event.observed_at < p_end+ AND event.uid IS NOT NULL+ AND event.uid <> ''+ AND coalesce(+ coalesce(+ event.object -> 'metadata' -> 'labels' ->> 'osgym.cua.ai/warmpool',+ event.object -> 'metadata' -> 'annotations' ->> 'osgym.cua.ai/origin-warmpool'+ ),+ event.object -> 'metadata' -> 'annotations' ->> 'osgym.cua.ai/origin-warmpool'+ ) <> ''+ AND event.object -> 'spec' -> 'vmTemplate' ->> 'runtime' <> ''+ AND event.object -> 'status' ->> 'vmName' <> ''+ ), baseline AS (+ SELECT DISTINCT ON (event.namespace, event.uid) event.*+ FROM tenant_events AS event+ WHERE event.observed_at < p_start+ ORDER BY event.namespace, event.uid, event.observed_at DESC, event.observed_sequence DESC, event.event_id DESC+ ), window_events AS (+ SELECT event.*+ FROM tenant_events AS event+ WHERE event.observed_at >= p_start+ )+ SELECT+ event.event_id,+ event.namespace,+ event.name,+ event.uid,+ coalesce(+ event.object -> 'metadata' -> 'labels' ->> 'osgym.cua.ai/warmpool',+ event.object -> 'metadata' -> 'annotations' ->> 'osgym.cua.ai/origin-warmpool'+ ),+ event.object -> 'spec' -> 'vmTemplate' ->> 'runtime',+ event.object -> 'status' ->> 'vmName',+ event.event_type,+ event.observed_at+ FROM (+ SELECT * FROM baseline+ UNION ALL+ SELECT * FROM window_events+ ) AS event+ ORDER BY event.namespace, event.uid, event.observed_at, event.observed_sequence, event.event_id;+END+$$;+REVOKE ALL ON FUNCTION k8s_reporting.usage_sandbox_events(text, timestamptz, timestamptz) FROM PUBLIC;+GRANT EXECUTE ON FUNCTION k8s_reporting.usage_sandbox_events(text, timestamptz, timestamptz) TO cyclops_usage_reader;+RESET ROLE;diff --git a/libs/fleet/backend/database/migrator_pg_test.go b/libs/fleet/backend/database/migrator_pg_test.goindex 13db60384a..3f18a9d148 100644--- a/libs/fleet/backend/database/migrator_pg_test.go+++ b/libs/fleet/backend/database/migrator_pg_test.go@@ -131,14 +131,14 @@ func TestRunUpgradesVersionOneAndThenNoOps(t *testing.T) {upgrade := captureRunSummary(t, func() error {return Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials})})- if upgrade.Current != 1 || upgrade.Target != 3 || upgrade.Pending != 2 || upgrade.Applied != 2 || upgrade.Skipped != 1 || upgrade.Result != "success" {+ if upgrade.Current != 1 || upgrade.Target != 4 || upgrade.Pending != 3 || upgrade.Applied != 3 || upgrade.Skipped != 1 || upgrade.Result != "success" {t.Fatalf("version-one upgrade summary = %+v", upgrade)}noOp := captureRunSummary(t, func() error {return Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials})})- if noOp.Current != 3 || noOp.Target != 3 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 3 || noOp.Result != "success" {+ if noOp.Current != 4 || noOp.Target != 4 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 4 || noOp.Result != "success" {t.Fatalf("post-upgrade no-op summary = %+v", noOp)}}@@ -1533,8 +1533,8 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []lif err := rows.Err(); err != nil {t.Fatal("iterate migration ledger")}- if len(ledger) != 3 {- t.Fatalf("migration ledger row count = %d, want 3", len(ledger))+ if len(ledger) != 4 {+ t.Fatalf("migration ledger row count = %d, want 4", len(ledger))}for index, want := range []struct {version int64@@ -1543,6 +1543,7 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []l{1, "000001_initial_schema.sql"},{2, "000002_usage_sandbox_events.sql"},{3, "000003_usage_claimed_sandbox_pool.sql"},+ {4, "000004_filter_invalid_usage_sandbox_events.sql"},} {if ledger[index].Version != want.version || ledger[index].ApplicationOrder != int64(index+1) || ledger[index].Filename != want.filename {t.Fatalf("migration ledger row %d = version:%d order:%d filename:%q", index, ledger[index].Version, ledger[index].ApplicationOrder, ledger[index].Filename)@@ -1872,7 +1873,7 @@ func assertRuntimeLedgerAccess(t *testing.T, ctx context.Context, credentials Crvar count interr := connection.QueryRow(ctx, `select count(*) from cyclops_migrations.applied_migrations`).Scan(&count)connection.Close(ctx)- if err != nil || count != 3 {+ if err != nil || count != 4 {t.Errorf("%s ledger select = count:%d err:%v", role, count, err)}assertStatementFails(t, ctx, databaseURL, `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values (99, 'invalid.sql', 'invalid')`)@@ -1925,7 +1926,8 @@ func assertUsageReaderBoundary(t *testing.T, ctx context.Context, inspectionURL,('10000000-0000-0000-0000-000000000017', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-b', 'user-alice', 'sandbox-uid-b', 'schema', 'ADDED', 2, 107, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-b-old"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-b-old"}}', '2026-08-17T20:00:00Z'),('10000000-0000-0000-0000-000000000018', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-b', 'user-alice', 'sandbox-uid-b', 'schema', 'MODIFIED', 2, 108, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-b"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-b"}}', '2026-08-17T22:00:00Z'),('10000000-0000-0000-0000-000000000019', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-other', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'ADDED', 2, 109, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-other-old"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-other-old"}}', '2026-08-17T19:00:00Z'),- ('10000000-0000-0000-0000-000000000020', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-other', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'MODIFIED', 2, 110, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-other"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-other"}}', '2026-08-17T21:00:00Z')`)+ ('10000000-0000-0000-0000-000000000020', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-other', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'MODIFIED', 2, 110, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-other"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-other"}}', '2026-08-17T21:00:00Z'),+ ('10000000-0000-0000-0000-000000000021', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'legacy-unlabeled', 'user-alice', 'legacy-unlabeled-uid', 'schema', 'ADDED', 1, 1, '{"metadata":{"labels":{}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"legacy-vm"}}', '2026-08-18T10:00:00Z')`)if err != nil {t.Fatal("seed usage sandbox events")}diff --git a/libs/fleet/backend/database/migrator_test.go b/libs/fleet/backend/database/migrator_test.goindex 78d9cd2590..ff3973dd0e 100644--- a/libs/fleet/backend/database/migrator_test.go+++ b/libs/fleet/backend/database/migrator_test.go@@ -520,8 +520,8 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {if err != nil {t.Fatal(err)}- if len(files) != 3 {- t.Fatalf("expected exactly three migrations, got %d", len(files))+ if len(files) != 4 {+ t.Fatalf("expected exactly four migrations, got %d", len(files))}initial := files[0]if initial.Version != 1 || initial.Name != "000001_initial_schema.sql" {@@ -594,6 +594,21 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {t.Errorf("claimed sandbox pool migration is missing contract %q", expected)}}++ legacyFilter := files[3]+ if legacyFilter.Version != 4 || legacyFilter.Name != "000004_filter_invalid_usage_sandbox_events.sql" {+ t.Fatalf("expected version 4 usage filter migration, got version=%d name=%q", legacyFilter.Version, legacyFilter.Name)+ }+ for _, expected := range []string{+ "CREATE OR REPLACE FUNCTION k8s_reporting.usage_sandbox_events",+ "event.object -> 'metadata' -> 'annotations' ->> 'osgym.cua.ai/origin-warmpool'",+ "event.object -> 'spec' -> 'vmTemplate' ->> 'runtime' <> ''",+ "event.object -> 'status' ->> 'vmName' <> ''",+ } {+ if !strings.Contains(legacyFilter.SQL, expected) {+ t.Errorf("usage filter migration is missing contract %q", expected)+ }+ }}func TestAppliedMigrationLedgerUsesApplicationOrder(t *testing.T) {