devin 0.1.22: organizations under one email are kept apart, each with its own quota (#296)
Two Devin organizations under one email both answered GetUserStatus with
the same email, so the plugin saved both sign-ins as the same
metadata.email and magpie's host (settle, which matches on it) put the
second over the first. The account is now named "email · <organization>"
when Devin names an organization (devinInfo.accountDisplayName) that isn't
the email, with orgId and teamId in its metadata. The usage hook brings an
older sign-in's name and ids up to date through client.auth.set, which the
host scopes to that account. `devin auth status`'s Team ID must match
before the CLI is taken to answer for a key. An account with no
organization in its status is left as it was and nothing is written.
Existing users: on the next usage poll, a sign-in whose status names an
organization is renamed "email · <org>" once; its slot, key and plan stay.
metadata.orgId and teamId aren't read by magpie's host today; the slot
split comes from the distinct name. The magpie side (a CLI account's label
overwritten by `devin auth status`) is yetone/magpie#3198.
Run at the reviewed head 7d589ab45323ee46a03af0f4b496c2dc94e67ba4
(base af360a5), on macOS, bun 1.4.2:
- HOME=$(mktemp -d) bun test: 2051 pass, 0 fail; bun scripts/check.mjs ok.
- Red without the change: the two new tests (identity, usage) fail against
main's index.mjs.
- Ad-hoc test (not committed): a status with no organization writes
nothing through auth.set and keeps the name; an organization named as
the email adds no suffix.
- With #297's change on the same tree: 2052 pass, check ok.
Not tried here: real Devin accounts (the author reports two live Devin Max
organizations under one email on Windows 11).
This commit is contained in:
@@ -19,7 +19,7 @@ two ways to get one:
|
||||
read, never changed.
|
||||
|
||||
The plugin names the account as Devin does when asked with the token
|
||||
(`GetUserStatus`: its email and tier). When Devin can't tell, and the CLI
|
||||
(`GetUserStatus`: its email, organization when named, and tier). When Devin can't tell, and the CLI
|
||||
is installed, it asks `devin auth status`, run with a data folder of the
|
||||
plugin's own, `~/.cache/opencode-devin-auth/<hash of the token>/`, so the
|
||||
CLI's own sign-in is never touched. A CLI that reads another
|
||||
@@ -28,7 +28,7 @@ CLI's own sign-in is never touched. A CLI that reads another
|
||||
its own account. Otherwise the account is named "Devin".
|
||||
|
||||
The token is kept where OpenCode keeps sign-ins (`auth.json`; in magpie,
|
||||
`plugin-auth.json`) as `{ "type": "api", "key", "metadata": { "email", "plan" } }`.
|
||||
`plugin-auth.json`) as `{ "type": "api", "key", "metadata": { "email", "plan", "orgId", "teamId" } }`.
|
||||
Devin doesn't expire it on a schedule, so there is nothing to refresh;
|
||||
when it is refused, requests fail with a 401 saying to sign in again.
|
||||
|
||||
|
||||
@@ -8,9 +8,14 @@ import { join } from "node:path"
|
||||
import { _internal } from "./index.mjs"
|
||||
|
||||
const was = process.env.XDG_DATA_HOME
|
||||
const wasAppData = process.env.APPDATA
|
||||
const data = mkdtempSync(join(tmpdir(), "devin-data-"))
|
||||
process.env.XDG_DATA_HOME = data
|
||||
afterAll(() => (was === undefined ? delete process.env.XDG_DATA_HOME : (process.env.XDG_DATA_HOME = was)))
|
||||
process.env.APPDATA = data
|
||||
afterAll(() => {
|
||||
was === undefined ? delete process.env.XDG_DATA_HOME : (process.env.XDG_DATA_HOME = was)
|
||||
wasAppData === undefined ? delete process.env.APPDATA : (process.env.APPDATA = wasAppData)
|
||||
})
|
||||
|
||||
test("the CLI's account follows credentials.toml; a browser sign-in stays", async () => {
|
||||
const cli = { type: "api", key: "saved", metadata: { cli: true, server: "https://s.example" } }
|
||||
|
||||
@@ -92,6 +92,43 @@ test.if(unix)("a second account is named as Devin names its key, not as the CLI'
|
||||
expect((await _internal.success("key-a", "https://server.codeium.com")).metadata.email).toBe("a@x.dev")
|
||||
}, SLOW)
|
||||
|
||||
test("two organizations under the same email are named and keyed apart", async () => {
|
||||
const orgStatus = (orgName, orgId, teamId) =>
|
||||
Response.json({
|
||||
userStatus: {
|
||||
name: "sky",
|
||||
email: "m@x.dev",
|
||||
teamId,
|
||||
teamsTier: "TEAMS_TIER_DEVIN_MAX",
|
||||
planStatus: {
|
||||
planInfo: {
|
||||
planName: "Max",
|
||||
devinInfo: { orgId, accountDisplayName: orgName },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
devin((key) =>
|
||||
key === "key-org-1"
|
||||
? orgStatus("opt", "org_1", "devin-team$account-1")
|
||||
: orgStatus("lab", "org_2", "devin-team$account-2"),
|
||||
)
|
||||
const r1 = await _internal.success("key-org-1", "https://server.codeium.com")
|
||||
const r2 = await _internal.success("key-org-2", "https://server.codeium.com")
|
||||
expect(r1).toEqual({
|
||||
type: "success",
|
||||
provider: "devin",
|
||||
key: "key-org-1",
|
||||
metadata: { email: "m@x.dev · opt", plan: "Devin Max", orgId: "org_1", teamId: "devin-team$account-1" },
|
||||
})
|
||||
expect(r2).toEqual({
|
||||
type: "success",
|
||||
provider: "devin",
|
||||
key: "key-org-2",
|
||||
metadata: { email: "m@x.dev · lab", plan: "Devin Max", orgId: "org_2", teamId: "devin-team$account-2" },
|
||||
})
|
||||
})
|
||||
|
||||
test.if(unix)("a key Devin refuses fails the sign-in, whatever the CLI says", async () => {
|
||||
devin(() => Response.json({ code: "unauthenticated", message: "invalid api key" }, { status: 401 }))
|
||||
await expect(_internal.success("key-refused", "https://server.codeium.com")).rejects.toThrow("Devin didn't take the sign-in")
|
||||
|
||||
+68
-16
@@ -680,11 +680,12 @@ function statusField(out, key) {
|
||||
}
|
||||
|
||||
// parseStatus reads `devin auth status`'s report: its User's Email (or
|
||||
// Name), its Account's Tier (or Plan).
|
||||
// Name), its Account's Tier (or Plan), and its Team ID when one is shown.
|
||||
function parseStatus(out) {
|
||||
if (!out.includes("Logged in")) return null
|
||||
const field = (key) => statusField(out, key)
|
||||
return { user: field("Email") || field("Name"), plan: field("Tier") || field("Plan") }
|
||||
const teamId = field("Team ID")
|
||||
return { user: field("Email") || field("Name"), plan: field("Tier") || field("Plan"), ...(teamId ? { teamId } : {}) }
|
||||
}
|
||||
|
||||
// inside says file is in dir, whichever way either is spelled.
|
||||
@@ -736,9 +737,32 @@ const tierName = (t) =>
|
||||
.map((w) => w[0] + w.slice(1).toLowerCase())
|
||||
.join(" ")
|
||||
|
||||
// whoOfStatus names the account GetUserStatus describes: its email (or
|
||||
// handle), and for a seat in an organization the organization's display
|
||||
// name too ("email · org"), so two organizations under one email are kept
|
||||
// apart as two accounts with their own quota pools.
|
||||
function whoOfStatus(u = {}) {
|
||||
const base = String(u.email || u.name || "").trim()
|
||||
if (!base) return undefined
|
||||
const info = u.planStatus?.planInfo ?? {}
|
||||
const devin = info.devinInfo ?? {}
|
||||
const org = String(devin.accountDisplayName || info.teamName || u.teamName || "").trim()
|
||||
const orgId = String(devin.orgId || u.teamId || "").trim()
|
||||
const teamId = String(u.teamId || "").trim()
|
||||
const named = org && org.toLowerCase() !== base.toLowerCase()
|
||||
const user = named ? `${base} · ${org}` : base
|
||||
return {
|
||||
user,
|
||||
...(named ? { email: base } : {}),
|
||||
plan: tierName(u.teamsTier || info.teamsTier) || info.planName || "",
|
||||
...(orgId ? { orgId } : {}),
|
||||
...(teamId ? { teamId } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
// whoByKey is whose key it is, as GetUserStatus (usage's) says, asked with
|
||||
// the key itself: {user, plan}, null when Devin refuses the key, undefined
|
||||
// when it can't tell.
|
||||
// the key itself: {user, plan, orgId?, teamId?}, null when Devin refuses
|
||||
// the key, undefined when it can't tell.
|
||||
async function whoByKey(key, server) {
|
||||
let res, text
|
||||
try {
|
||||
@@ -755,10 +779,7 @@ async function whoByKey(key, server) {
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
const user = u.email || u.name
|
||||
if (!user) return undefined
|
||||
const info = u.planStatus?.planInfo ?? {}
|
||||
return { user, plan: tierName(u.teamsTier || info.teamsTier) || info.planName || "" }
|
||||
return whoOfStatus(u)
|
||||
}
|
||||
|
||||
// cliAnswersFor says whether the CLI, run with home as its data folder,
|
||||
@@ -775,11 +796,12 @@ async function cliAnswersFor(key, server, home) {
|
||||
return undefined
|
||||
}
|
||||
const own = readsHome(out, home)
|
||||
const cli = parseStatus(out)?.user
|
||||
if (!cli) return own
|
||||
const st = parseStatus(out)
|
||||
if (!st?.user) return own
|
||||
const who = await whoByKey(key, server)
|
||||
if (!who?.user) return own
|
||||
return who.user.toLowerCase() === cli.toLowerCase()
|
||||
if (who.teamId && st.teamId && who.teamId !== st.teamId) return false
|
||||
return (who.email || who.user).toLowerCase() === st.user.toLowerCase()
|
||||
}
|
||||
|
||||
// Each account's model list as the CLI last gave it, kept FRESH; one past
|
||||
@@ -877,6 +899,9 @@ async function success(key, server) {
|
||||
if (who === null) throw new Error("Devin didn't take the sign-in: nobody is signed in with the token it gave")
|
||||
const metadata = { email: who?.user || "Devin" }
|
||||
if (who?.plan) metadata.plan = who.plan
|
||||
const orgId = who?.orgId || who?.teamId
|
||||
if (orgId) metadata.orgId = orgId
|
||||
if (who?.teamId) metadata.teamId = who.teamId
|
||||
if (server && server !== SERVER) metadata.server = server
|
||||
return { type: "success", provider: ID, key, metadata }
|
||||
}
|
||||
@@ -1758,13 +1783,15 @@ function userStatus(key, server) {
|
||||
// built-in Devin account showed none of this. The day and the week don't
|
||||
// count the models Devin says are free (notModels): with them used up,
|
||||
// SWE-2 still answers, and magpie sends it to the account.
|
||||
async function usage(key, server) {
|
||||
async function usage(key, server, onStatus) {
|
||||
const res = await userStatus(key, server)
|
||||
const text = await res.text()
|
||||
if (res.status === 401 || failure(res.status, text).status === 401)
|
||||
return { error: "Devin's sign-in has expired — sign in again", windows: [] }
|
||||
if (!res.ok) return { error: failure(res.status, text).message, windows: [] }
|
||||
const st = JSON.parse(text || "{}")?.userStatus?.planStatus ?? {}
|
||||
const u = JSON.parse(text || "{}")?.userStatus ?? {}
|
||||
await onStatus?.(u)
|
||||
const st = u.planStatus ?? {}
|
||||
const info = st.planInfo ?? {}
|
||||
const num = (v) => (typeof v === "number" ? v : typeof v === "string" && v.trim() && !isNaN(Number(v)) ? Number(v) : 0)
|
||||
// no plan: the row keeps the one `devin auth status` gave the sign-in
|
||||
@@ -1830,7 +1857,7 @@ async function live(auth) {
|
||||
return { key: auth?.key, server: serverOf(auth) }
|
||||
}
|
||||
|
||||
export async function DevinAuthPlugin() {
|
||||
export async function DevinAuthPlugin({ client } = {}) {
|
||||
return {
|
||||
auth: {
|
||||
provider: ID,
|
||||
@@ -1869,7 +1896,32 @@ export async function DevinAuthPlugin() {
|
||||
const auth = await getAuth()
|
||||
if (auth?.type !== "api" || !auth.key) return { error: "Devin isn't signed in", signIn: "kept" }
|
||||
const { key, server } = await live(auth)
|
||||
return { ...(await usage(key, server)), signIn: "kept" }
|
||||
const syncOrg = async (u) => {
|
||||
if (!client?.auth?.set) return
|
||||
const who = whoOfStatus(u)
|
||||
if (!who) return
|
||||
const md = { ...(auth.metadata ?? {}) }
|
||||
let changed = key !== auth.key
|
||||
if (who.user && md.email !== who.user) {
|
||||
md.email = who.user
|
||||
changed = true
|
||||
}
|
||||
const orgId = who.orgId || who.teamId
|
||||
if (orgId && md.orgId !== orgId) {
|
||||
md.orgId = orgId
|
||||
changed = true
|
||||
}
|
||||
if (who.teamId && md.teamId !== who.teamId) {
|
||||
md.teamId = who.teamId
|
||||
changed = true
|
||||
}
|
||||
if (changed) {
|
||||
try {
|
||||
await client.auth.set({ path: { id: ID }, body: { ...auth, key, metadata: md } })
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
return { ...(await usage(key, server, syncOrg)), signIn: "kept" }
|
||||
},
|
||||
},
|
||||
async config(config) {
|
||||
@@ -1902,4 +1954,4 @@ export async function DevinAuthPlugin() {
|
||||
}
|
||||
|
||||
// for tests
|
||||
export const _internal = { parseFree, parsePrices, geminiSchema, freeIDs, outID, inID, objectRoot, seesImages, imagesFor, forgetSaid: () => said.clear(), parseModelConfigs, parseConfigFamilies, SNAPSHOT_IMAGES, withImages, forgetImages: () => (seen = null), effortOf, runtimeModel, configModel, live, build, failure, complete, parseFamilies, listed, variantFor, familiesOf, SNAPSHOT, parseStatus, readsHome, tierName, whoByKey, success, familiesFor, readCredentials, credentials, fields, frame, PB, events, frames }
|
||||
export const _internal = { parseFree, parsePrices, geminiSchema, freeIDs, outID, inID, objectRoot, seesImages, imagesFor, forgetSaid: () => said.clear(), parseModelConfigs, parseConfigFamilies, SNAPSHOT_IMAGES, withImages, forgetImages: () => (seen = null), effortOf, runtimeModel, configModel, live, build, failure, complete, parseFamilies, listed, variantFor, familiesOf, SNAPSHOT, parseStatus, readsHome, tierName, whoOfStatus, whoByKey, success, familiesFor, readCredentials, credentials, fields, frame, PB, events, frames }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-devin-auth",
|
||||
"version": "0.1.21",
|
||||
"version": "0.1.22",
|
||||
"description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -94,6 +94,78 @@ test("a plan without quotas has no windows, and leaves the row's plan be", async
|
||||
expect((await run(auth, () => Response.json(s))).u).toEqual({ windows: [], signIn: "kept" })
|
||||
})
|
||||
|
||||
test("usage enriches an older sign-in with its organization and keeps each org's quota pool separate", async () => {
|
||||
const saved = []
|
||||
const hooks = await DevinAuthPlugin({
|
||||
client: {
|
||||
auth: {
|
||||
set: async (opts) => {
|
||||
saved.push(opts)
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
globalThis.fetch = async (_, init) => {
|
||||
const apiKey = JSON.parse(init.body).metadata.apiKey
|
||||
if (apiKey === "key-org-1") {
|
||||
return Response.json({
|
||||
userStatus: {
|
||||
name: "sky",
|
||||
email: "m@x.dev",
|
||||
teamId: "devin-team$account-1",
|
||||
teamsTier: "TEAMS_TIER_DEVIN_MAX",
|
||||
planStatus: {
|
||||
planInfo: {
|
||||
planName: "Max",
|
||||
teamsTier: "TEAMS_TIER_DEVIN_MAX",
|
||||
billingStrategy: "BILLING_STRATEGY_QUOTA",
|
||||
devinInfo: { orgId: "org_1", accountDisplayName: "opt" },
|
||||
},
|
||||
dailyQuotaRemainingPercent: 80,
|
||||
weeklyQuotaRemainingPercent: 60,
|
||||
dailyQuotaResetAtUnix: "1790841600",
|
||||
weeklyQuotaResetAtUnix: "1791100800",
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
return Response.json({
|
||||
userStatus: {
|
||||
name: "sky",
|
||||
email: "m@x.dev",
|
||||
teamId: "devin-team$account-2",
|
||||
teamsTier: "TEAMS_TIER_DEVIN_MAX",
|
||||
planStatus: {
|
||||
planInfo: {
|
||||
planName: "Max",
|
||||
teamsTier: "TEAMS_TIER_DEVIN_MAX",
|
||||
billingStrategy: "BILLING_STRATEGY_QUOTA",
|
||||
devinInfo: { orgId: "org_2", accountDisplayName: "lab" },
|
||||
},
|
||||
dailyQuotaRemainingPercent: 25,
|
||||
weeklyQuotaRemainingPercent: 10,
|
||||
dailyQuotaResetAtUnix: "1790841600",
|
||||
weeklyQuotaResetAtUnix: "1791100800",
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
const u1 = await hooks.auth.usage(async () => ({ type: "api", key: "key-org-1", metadata: { email: "m@x.dev", plan: "Devin Max" } }))
|
||||
const u2 = await hooks.auth.usage(async () => ({ type: "api", key: "key-org-2", metadata: { email: "m@x.dev · lab", plan: "Devin Max", orgId: "org_2", teamId: "devin-team$account-2" } }))
|
||||
expect(u1.windows.map((w) => w.used)).toEqual([20, 40])
|
||||
expect(u2.windows.map((w) => w.used)).toEqual([75, 90])
|
||||
expect(saved).toEqual([
|
||||
{
|
||||
path: { id: "devin" },
|
||||
body: {
|
||||
type: "api",
|
||||
key: "key-org-1",
|
||||
metadata: { email: "m@x.dev · opt", plan: "Devin Max", orgId: "org_1", teamId: "devin-team$account-1" },
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
// the built-in read no usage, so none marked the account: a refused key
|
||||
// says to sign in again, and says the account is kept
|
||||
test("a refused key says to sign in again, the account kept", async () => {
|
||||
|
||||
Reference in New Issue
Block a user