An open-source, faster Devin alternative
TypeScript
Start new orgs on the work-minute trial
Mohamed Habib via Computerf21dae9ec0d3
diff --git a/cloudflare-workers/api-edge/migrations/0011_orgs_trial_started_at.sql b/cloudflare-workers/api-edge/migrations/0011_orgs_trial_started_at.sqlnew file mode 100644index 00000000..7352592e--- /dev/null+++ b/cloudflare-workers/api-edge/migrations/0011_orgs_trial_started_at.sql@@ -0,0 +1,6 @@+-- orgs.trial_started_at — when the org's work-minute trial began, in unix+-- seconds (serverless-agents-ws 086). Autumn attaches the `trial` plan to every+-- customer created at signup, so it is set with the org row; the edge ends the+-- trial on day 7. NULL for orgs that never had one (legacy billing, or created+-- before the trial launched, which get a backdated start at launch).+ALTER TABLE orgs ADD COLUMN trial_started_at INTEGER;diff --git a/cloudflare-workers/api-edge/src/autumn_webhook.ts b/cloudflare-workers/api-edge/src/autumn_webhook.tsindex 5d176ada..046bce9b 100644--- a/cloudflare-workers/api-edge/src/autumn_webhook.ts+++ b/cloudflare-workers/api-edge/src/autumn_webhook.ts@@ -117,6 +117,8 @@ export interface AutumnCustomer {// "auto-recharge will fire" signal (Autumn exposes no payment-method field).purchases?: Array<{ plan_id: string }>;balances?: Record<string, AutumnBalance>;+ // Boolean features the customer's plans grant, by feature id.+ flags?: Record<string, unknown>;billing_controls?: { auto_topups?: AutumnAutoTopup[] };}@@ -601,7 +603,7 @@ function maxConcurrency(subs: AutumnSubscription[]): number {}// createAutumnCustomer provisions an Autumn customer (id = our org UUID) →-// Autumn auto-attaches the `base` plan + grants the $5 signup credit. POSTing an+// Autumn auto-attaches the `trial` plan (600 work minutes, $10). POSTing an// existing id returns the existing customer, so it's idempotent/retry-safe.export async function createAutumnCustomer(env: AutumnApiEnv,diff --git a/cloudflare-workers/api-edge/src/cli_auth.test.ts b/cloudflare-workers/api-edge/src/cli_auth.test.tsindex afb8e756..cca61fd3 100644--- a/cloudflare-workers/api-edge/src/cli_auth.test.ts+++ b/cloudflare-workers/api-edge/src/cli_auth.test.ts@@ -661,12 +661,35 @@ describe("CLI device authorization edge contract", () => {expect(userInsert?.sql).toContain("infrastructure_enabled");expect(userInsert?.sql).toMatch(/VALUES\s*\(\?1, \?2, \?3, \?4, \?5, 0, 0\)/);expect(orgInsert?.args[3]).toBe("azure-us-east-2-a");+ // Legacy billing (no Autumn customer) has no trial.+ expect(orgInsert?.sql).toContain("trial_started_at");+ expect(orgInsert?.args[8]).toBeNull();expect(membershipInsert?.args[0]).toBe(body.org.id);expect(membershipInsert?.args[1]).toBe(body.user.id);expect(keyInsert?.args[1]).toBe(body.org.id);expect(keyInsert?.args[2]).toBe(body.user.id);});+ it("starts the trial for an org created with an Autumn customer", async () => {+ const db = new FakeDB([], null);+ const fetchMock = vi.fn(async (_url: RequestInfo | URL, _init?: RequestInit) => Response.json({+ user: { id: "new-workos-user", email: "[email protected]", first_name: "New" },+ }));+ vi.stubGlobal("fetch", fetchMock);+ const resp = await worker.fetch(request("/auth/cli/device/exchange", {+ method: "POST",+ headers: { "content-type": "application/json", "cf-ipcountry": "US" },+ body: JSON.stringify({ device_code: "opaque", credential_name: "oc CLI" }),+ }), { ...testEnv(db), AUTUMN_NEW_ORGS: "true", AUTUMN_SECRET_KEY: "autumn-test" }, ctx);+ expect(resp.status).toBe(200);++ expect(fetchMock.mock.calls.some(([url]) => String(url).endsWith("/customers"))).toBe(true);+ const orgInsert = db.executed.find((entry) => entry.sql.includes("INSERT INTO orgs"));+ expect(orgInsert?.args[6]).toBe("autumn");+ expect(orgInsert?.args[8]).toBe(orgInsert?.args[5]);+ expect(typeof orgInsert?.args[8]).toBe("number");+ });+it("rejects extra exchange fields before contacting WorkOS", async () => {const providerFetch = vi.fn();vi.stubGlobal("fetch", providerFetch);diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.tsindex e581e0d4..5232e04c 100644--- a/cloudflare-workers/api-edge/src/index.ts+++ b/cloudflare-workers/api-edge/src/index.ts@@ -4058,8 +4058,8 @@ async function provisionWorkOSIdentity(}}await env.OPENCOMPUTER_DB.prepare(- `INSERT INTO orgs (id, name, slug, plan, home_cell, is_personal, owner_user_id, billing_provider, max_concurrent_sandboxes, created_at, updated_at)- VALUES (?1, ?2, ?3, 'free', ?4, 1, ?5, ?7, ?8, ?6, ?6)`,+ `INSERT INTO orgs (id, name, slug, plan, home_cell, is_personal, owner_user_id, billing_provider, max_concurrent_sandboxes, trial_started_at, created_at, updated_at)+ VALUES (?1, ?2, ?3, 'free', ?4, 1, ?5, ?7, ?8, ?9, ?6, ?6)`,).bind(orgID,@@ -4070,6 +4070,8 @@ async function provisionWorkOSIdentity(nowSec,provider,DEFAULT_MAX_CONCURRENT_SANDBOXES,+ // A new Autumn customer starts on the trial plan (autoEnable).+ provider === "autumn" ? nowSec : null,).run();await env.OPENCOMPUTER_DB.prepare(diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.tsindex acfe6297..67877ff6 100644--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts@@ -736,6 +736,29 @@ describe("managed agents proxy", () => {).resolves.toBe(true);});+ it("allows BYOK on the trial through its byo_model_subscriptions flag", async () => {+ const access = async (customer: Record<string, unknown>) => {+ vi.stubGlobal("fetch", vi.fn(async () => Response.json({ id: "org_test", ...customer })));+ const autumnDB = {+ prepare: () => ({+ bind: () => ({ first: async () => ({ plan: "free", billing_provider: "autumn" }) }),+ }),+ } as unknown as D1Database;+ return hasBYOKPlanAccess(+ { OPENCOMPUTER_DB: autumnDB, AUTUMN_SECRET_KEY: "autumn-test", AUTUMN_BASE_URL: "https://autumn.test/v1" },+ "org_test",+ );+ };+ const trial = [{ plan_id: "trial", add_on: false, status: "active" }];+ await expect(+ access({+ subscriptions: trial,+ flags: { byo_model_subscriptions: { feature_id: "byo_model_subscriptions", plan_id: "trial" } },+ }),+ ).resolves.toBe(true);+ await expect(access({ subscriptions: trial, flags: {} })).resolves.toBe(false);+ });+it("returns the public model-access OAuth receipt without custody fields", async () => {vi.stubGlobal("fetch",diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.tsindex b48dd23d..1550f96f 100644--- a/cloudflare-workers/api-edge/src/managed_agents.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.ts@@ -83,6 +83,9 @@ export async function hasBYOKPlanAccess(},orgID,);+ // Every plan that allows BYOK grants this feature (trial, pro, max);+ // the plan check covers a customer read before the flag existed.+ if (customer?.flags?.byo_model_subscriptions) return true;return (customer?.subscriptions?.some((subscription) =>diff --git a/scripts/autumn/autumn.config.ts b/scripts/autumn/autumn.config.tsindex b344e358..f9f4e354 100644--- a/scripts/autumn/autumn.config.ts+++ b/scripts/autumn/autumn.config.ts@@ -128,7 +128,6 @@ export const credits = feature({export const base = plan({id: "base",name: "base",- autoEnable: true,items: [item({featureId: credits.id,@@ -218,13 +217,14 @@ export const max = plan({],});-// Work-minute trial (serverless-agents-ws 086), attached at signup; the-// day-7 end is enforced by the API edge, not by Autumn. Credits come from-// top_up in $10 / $50 / $200 packs.+// Work-minute trial (serverless-agents-ws 086). Autumn attaches it to every+// new customer, in place of base; the edge records the start and enforces+// the day-7 end. Credits come from top_up in $10 / $50 / $200 packs.export const trial = plan({id: "trial",name: "Trial",+ autoEnable: true,description: "7 days, 600 work minutes and $10 of model credits.",group: "usage_plan",items: [