Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
plugin: plugin-auth.json is parsed once per version of the file, not on every request
yetone8f37ed57d2a5
diff --git a/internal/plugin/auth_read_once_test.go b/internal/plugin/auth_read_once_test.gonew file mode 100644index 000000000..da08ab1cd--- /dev/null+++ b/internal/plugin/auth_read_once_test.go@@ -0,0 +1,81 @@+package plugin++import (+ "bytes"+ "context"+ "os/exec"+ "path/filepath"+ "testing"+ "time"+)++// TestAuthReadOncePerVersion: host.js reads and parses plugin-auth.json+// once per version of the file, not on every request (its account, its+// renewal and the plugin's getAuth each read it). A file rewritten with+// the same size inside one tick of the file system's clock, which leaves+// the same time, is still read again, and a plugin's copy is its own.+func TestAuthReadOncePerVersion(t *testing.T) {+ node, err := exec.LookPath("node")+ if err != nil {+ t.Skip("no node on PATH")+ }+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)+ defer cancel()+ cmd := exec.CommandContext(ctx, node, "-e", authReadOnceJS, filepath.Join(t.TempDir(), "plugin-auth.json"))+ cmd.Stdin = bytes.NewReader(hostJS)+ if out, err := cmd.CombinedOutput(); err != nil {+ t.Fatalf("%v\n%s", err, out)+ }+}++const authReadOnceJS = `+const assert = require('node:assert/strict')+const realFs = require('node:fs')+const vm = require('node:vm')+const source = realFs.readFileSync(0, 'utf8')+const section = (from, to) => {+ const start = source.indexOf(from)+ const end = source.indexOf(to, start)+ assert.ok(start >= 0 && end > start, 'production section exists')+ return source.slice(start, end)+}+const authPath = process.argv[1]+let reads = 0+const fs = { ...realFs, readFileSync: (p, ...rest) => {+ if (p === authPath) reads+++ return realFs.readFileSync(p, ...rest)+} }+const context = vm.createContext({ fs, authPath, Buffer, structuredClone, Date, toErr: () => {}, send: () => {}, pause: () => {} })+const run = code => vm.runInContext(code, context)+run(section('function parseAuth(', '// writeDurable'))+const past = new Date(Date.now() - 60e3)+const write = (v, at) => {+ realFs.writeFileSync(authPath, JSON.stringify(v))+ if (at) realFs.utimesSync(authPath, at, at)+}++write({ a: { type: 'api', key: 'k1' } }, past)+for (let i = 0; i < 100; i++) assert.equal(run('readAuth()').a.key, 'k1')+assert.equal(reads, 1, 'a settled file is read once for 100 reads, read ' + reads + ' times')++// written again: read again+write({ a: { type: 'api', key: 'k2' }, b: { type: 'api', key: 'k3' } }, new Date(past.getTime() + 1000))+assert.equal(run('readAuth()').a.key, 'k2')++// a file written just now, rewritten with the same size and time (two+// writes inside one tick of the clock): its bytes tell them apart+const now = new Date()+write({ a: { type: 'api', key: 'x1' } }, now)+assert.equal(run('readAuth()').a.key, 'x1')+write({ a: { type: 'api', key: 'x2' } }, now)+assert.equal(run('readAuth()').a.key, 'x2', 'a same-size rewrite in one tick is read')++// what a plugin is handed is its own to change+const copy = run('authCopy("a")')+copy.key = 'changed'+assert.equal(run('readAuth()').a.key, 'x2')+const all = run('authCopy()')+all.a.key = 'changed'+assert.equal(run('readAuth()').a.key, 'x2')+console.log('ok')+`diff --git a/internal/plugin/host.js b/internal/plugin/host.jsindex 8656dd20d..ca025abba 100644--- a/internal/plugin/host.js+++ b/internal/plugin/host.js@@ -716,11 +716,10 @@ let authUnread = false// readAuthState is the sign-ins and whether they are known: a file not// there is none, known; one that doesn't read is its .bak's, known, or// none, unknown.-function readAuthState() {- const buf = readFileSteady(authPath)+function readAuthState(buf = readFileSteady(authPath)) {if (buf === null) return { all: {}, known: true }const v = parseAuth(buf.toString("utf8"))- if (v) return { all: v, known: true }+ if (v) return { all: v, known: true, own: true }const why = describeBad(buf)let bak = nulltry {@@ -741,8 +740,41 @@ function readAuthState() {return { all: {}, known: false }}+// authSeen is plugin-auth.json as last parsed, kept while the file has the+// same time, size and inode: every request read and parsed it two or three+// times (its account, its renewal, the plugin's getAuth), ~0.6 ms each for+// a dozen sign-ins. While the file's time is too recent for a second write+// to stamp a later one (Linux's clock is a tick coarse), its bytes are kept+// and compared too, as Go's filememo does.+let authSeen = null // {mtimeMs, size, ino, raw, all}+const AUTH_SETTLED = 2000++// readAuth is the sign-ins, shared: the caller must not change them, and+// hands a plugin a copy (authCopy).function readAuth() {- return readAuthState().all+ let st = null+ try {+ st = fs.statSync(authPath)+ } catch {}+ const e = authSeen+ const same = st && e && e.mtimeMs === st.mtimeMs && e.size === st.size && e.ino === st.ino+ if (same && !e.raw) return e.all+ const buf = readFileSteady(authPath)+ if (same && buf && buf.equals(e.raw)) {+ if (Date.now() - e.mtimeMs > AUTH_SETTLED) e.raw = null+ return e.all+ }+ const s = readAuthState(buf)+ authSeen = st && s.own ? { mtimeMs: st.mtimeMs, size: st.size, ino: st.ino, raw: Date.now() - st.mtimeMs > AUTH_SETTLED ? null : buf, all: s.all } : null+ return s.all+}++// authCopy is readAuth's sign-ins, or the one at key, the plugin's own to+// change.+function authCopy(key) {+ const all = readAuth()+ const v = key === undefined ? all : all[key]+ return v === undefined ? undefined : structuredClone(v)}// writeDurable replaces p with data through a temp file flushed to the@@ -1172,7 +1204,7 @@ async function loadPlugins(list) {}const pis = pi ? list.filter((p) => pi.isPi(p.target)) : []if (pis.length) {- const h = { readAuth, changeAuth, keyFor, send, directory }+ const h = { readAuth: authCopy, changeAuth, keyFor, send, directory }const got = await pi.load(h, pis).catch((e) => pis.map((p) => ({ spec: p.spec, error: String(e?.stack ?? e) })))for (const r of got) {for (const x of r.hooks ?? []) hooks.push(x)@@ -1692,7 +1724,7 @@ async function info(id, key, strict) {try {const given = JSON.parse(JSON.stringify(out))const l = { tried: false, lastOk: false }- const next = await listing.run(l, () => inScope(id, k, () => ph.models(given, { auth: all[k] })))+ const next = await listing.run(l, () => inScope(id, k, () => ph.models(given, { auth: authCopy(k) })))// a hook that asked its vendor, got no list and gave back the one it// was given fell back: magpie keeps the list it had, as a built-in// whose fetch failed keeps the one it fetched last; so does one that@@ -2162,7 +2194,7 @@ async function options(provider, key) {let opts = {}if (a?.loader && stored) {const p = await infoOf(provider, key)- opts = (await inScope(provider, key, () => a.loader(async () => readAuth()[key], JSON.parse(JSON.stringify(p))))) ?? {}+ opts = (await inScope(provider, key, () => a.loader(async () => authCopy(key), JSON.parse(JSON.stringify(p))))) ?? {}}if (!opts.apiKey && stored?.type === "api") opts = { ...opts, apiKey: stored.key }loaders.set(key, opts)@@ -2219,7 +2251,7 @@ async function usageOf(provider, account) {if (!readAuth()[key]) throw new Error("not signed in")await fresh(provider, key)const p = await infoOf(provider, key)- const u = (await inScope(provider, key, () => a.usage(async () => readAuth()[key], JSON.parse(JSON.stringify(p))))) ?? {}+ const u = (await inScope(provider, key, () => a.usage(async () => authCopy(key), JSON.parse(JSON.stringify(p))))) ?? {}const when = (v) => {if (v === undefined || v === null || v === "") return ""const d = new Date(typeof v === "number" && v < 1e11 ? v * 1000 : v)@@ -2294,7 +2326,7 @@ async function checkinOf(provider, account) {if (!readAuth()[key]) throw new Error("not signed in")await fresh(provider, key)const p = await infoOf(provider, key)- const r = (await inScope(provider, key, () => a.checkin(async () => readAuth()[key], JSON.parse(JSON.stringify(p))))) ?? {}+ const r = (await inScope(provider, key, () => a.checkin(async () => authCopy(key), JSON.parse(JSON.stringify(p))))) ?? {}const num = (v) => (typeof v === "number" && isFinite(v) ? Math.max(0, v) : 0)const message = typeof r.message === "string" ? r.message : ""if (!checkinOutcomes.includes(r.outcome)) {