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): preserve claimed sandbox usage projection (#7097)
r33drichards9d2b9949e7a3
diff --git a/libs/fleet/backend/database/migrations/000003_usage_claimed_sandbox_pool.sql b/libs/fleet/backend/database/migrations/000003_usage_claimed_sandbox_pool.sqlnew file mode 100644index 0000000000..e4eb3c746d--- /dev/null+++ b/libs/fleet/backend/database/migrations/000003_usage_claimed_sandbox_pool.sql@@ -0,0 +1,72 @@+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+ ), 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 3ba5b45bbe..13db60384a 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 != 2 || upgrade.Pending != 1 || upgrade.Applied != 1 || upgrade.Skipped != 1 || upgrade.Result != "success" {+ if upgrade.Current != 1 || upgrade.Target != 3 || upgrade.Pending != 2 || upgrade.Applied != 2 || 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 != 2 || noOp.Target != 2 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 2 || noOp.Result != "success" {+ if noOp.Current != 3 || noOp.Target != 3 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 3 || 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) != 2 {- t.Fatalf("migration ledger row count = %d, want 2", len(ledger))+ if len(ledger) != 3 {+ t.Fatalf("migration ledger row count = %d, want 3", len(ledger))}for index, want := range []struct {version int64@@ -1542,6 +1542,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"},} {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)@@ -1871,7 +1872,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 != 2 {+ if err != nil || count != 3 {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')`)@@ -1916,7 +1917,7 @@ func assertUsageReaderBoundary(t *testing.T, ctx context.Context, inspectionURL,values('10000000-0000-0000-0000-000000000010', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'ADDED', 2, 100, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-old"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-old"}}', '2026-08-17T00:00:00Z'),('10000000-0000-0000-0000-000000000011', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'MODIFIED', 2, 101, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-a"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-a"}}', '2026-08-17T23:00:00Z'),- ('10000000-0000-0000-0000-000000000012', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'MODIFIED', 2, 102, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-a"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-a-2"}}', '2026-08-18T06:00:00Z'),+ ('10000000-0000-0000-0000-000000000012', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'MODIFIED', 2, 102, '{"metadata":{"labels":{},"annotations":{"osgym.cua.ai/origin-warmpool":"pool-a"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-a-2"}}', '2026-08-18T06:00:00Z'),('10000000-0000-0000-0000-000000000013', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'alice-ns', 'sandbox-a', 'user-alice', 'sandbox-uid-a', 'schema', 'DELETED', 2, 103, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-a"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-a-2"}}', '2026-08-18T12:00:00Z'),('10000000-0000-0000-0000-000000000014', 'migration-test', 'osgym.cua.ai', 'osgymsandboxes', 'bob-ns', 'sandbox-b', 'user-bob', 'sandbox-uid-b', 'schema', 'ADDED', 2, 104, '{"metadata":{"labels":{"osgym.cua.ai/warmpool":"pool-b"}},"spec":{"vmTemplate":{"runtime":"qemu"}},"status":{"vmName":"vm-b"}}', '2026-08-18T08:00:00Z'),('10000000-0000-0000-0000-000000000015', 'migration-test', '', 'pods', 'alice-ns', 'sandbox-pod', 'user-alice', 'pod-uid', 'schema', 'ADDED', 2, 105, '{}', '2026-08-18T09:00:00Z'),diff --git a/libs/fleet/backend/database/migrator_test.go b/libs/fleet/backend/database/migrator_test.goindex 29fdeda457..78d9cd2590 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) != 2 {- t.Fatalf("expected exactly two migrations, got %d", len(files))+ if len(files) != 3 {+ t.Fatalf("expected exactly three migrations, got %d", len(files))}initial := files[0]if initial.Version != 1 || initial.Name != "000001_initial_schema.sql" {@@ -579,6 +579,21 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {if strings.Contains(usage.SQL, "GRANT SELECT ON k8s_state.resource_event_outbox TO cyclops_usage_reader") {t.Fatal("usage reader must not receive direct outbox table access")}++ claimedSandboxPool := files[2]+ if claimedSandboxPool.Version != 3 || claimedSandboxPool.Name != "000003_usage_claimed_sandbox_pool.sql" {+ t.Fatalf("expected version 3 claimed sandbox pool migration, got version=%d name=%q", claimedSandboxPool.Version, claimedSandboxPool.Name)+ }+ for _, expected := range []string{+ "CREATE OR REPLACE FUNCTION k8s_reporting.usage_sandbox_events",+ "event.object -> 'metadata' -> 'annotations' ->> 'osgym.cua.ai/origin-warmpool'",+ "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",+ } {+ if !strings.Contains(claimedSandboxPool.SQL, expected) {+ t.Errorf("claimed sandbox pool migration is missing contract %q", expected)+ }+ }}func TestAppliedMigrationLedgerUsesApplicationOrder(t *testing.T) {