codearts 0.3.12: an IDE rewrite in the same mtime tick is read again (#1007)
Cause: ideReader kept the last read of CodeArts Agent's state.vscdb
under a stamp of size:mtimeMs:ino (plus -wal/-journal size and mtime).
An in-place SQLite write keeps the inode and page count, so two writes
within one tick of the file system's clock left the stamp unchanged and
the old credential came back. The test's rewriteIde hit exactly that:
two rewrites a few ms apart, each with mtime Date.now()+5000, so when
both landed in one millisecond the renewal test saw IDEAK2's cached read
and said "hasn't renewed" instead of "another account" (~1 run in 3).
Fix:
- ideReader stamps the inode plus a sha1 of the bytes of the database,
its -wal and its -journal, read in 1MB pieces (no copy, no SQLite
open; it runs only on an IDE sign-in's renewal). A file that can't be
read, or is past IDE_DB_MAX_BYTES, gives no stamp, so it is read again
rather than taken as unchanged.
- rewriteIde gives each rewrite a strictly later mtime (>= 1s after the
previous one).
- New test: the file rewritten at a fixed mtime with the same size and
inode is read again; a removed file is not the last read.
Ran (mote, Bun 1.4.2, HOME=$(mktemp -d), at 1e5b1c83 + this change):
- bun test packages/codearts 8 times: 202 pass, 0 fail each.
- bun scripts/check.mjs: exit 0.
- root bun test under lockf ~/suite.lock: 8370 pass, 20 skip, 0 fail.
- Windows (xiaomi-gpu, temp HOME/USERPROFILE): bun test packages/codearts
twice, 202 pass, 0 fail each.
Broken by hand: the old size:mtimeMs:ino stamp back in ideReader turns
the new test red (1 fail). rewriteIde with a fixed time
(new Date(2000000000000)) and the new reader: the renewal test passes
3 of 3.
Not tried: a real CodeArts Agent writing its state.vscdb; no Huawei API
was contacted.
This commit is contained in:
@@ -2881,10 +2881,47 @@ async function readIdeCredential(file, { tmp = os.tmpdir() } = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A digest of `file`'s bytes, read in pieces: `""` when there is no such file,
|
||||
* `null` when it couldn't be read (so nothing is taken as unchanged).
|
||||
*/
|
||||
function fileDigest(file) {
|
||||
let fd
|
||||
try {
|
||||
fd = fs.openSync(file, "r")
|
||||
} catch (e) {
|
||||
return e?.code === "ENOENT" ? "" : null
|
||||
}
|
||||
try {
|
||||
const hash = createHash("sha1")
|
||||
const buf = Buffer.allocUnsafe(1 << 20)
|
||||
let total = 0
|
||||
for (;;) {
|
||||
const n = fs.readSync(fd, buf, 0, buf.length, null)
|
||||
if (n === 0) break
|
||||
total += n
|
||||
// past the size a read would copy: not read, so not known to be the same
|
||||
if (total > IDE_DB_MAX_BYTES) return null
|
||||
hash.update(n === buf.length ? buf : buf.subarray(0, n))
|
||||
}
|
||||
return `${total}:${hash.digest("hex")}`
|
||||
} catch {
|
||||
return null
|
||||
} finally {
|
||||
try {
|
||||
fs.closeSync(fd)
|
||||
} catch {
|
||||
/* closed */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A reader of the IDE's sign-in at `file`, read again only when the file
|
||||
* changed (by its size, time and inode): a renewal that finds the IDE hasn't
|
||||
* renewed yet asks again every half minute.
|
||||
* changed: a renewal that finds the IDE hasn't renewed yet asks again every
|
||||
* half minute. "Changed" is by its bytes and those of its -wal and -journal,
|
||||
* not only their size, time and inode: an IDE that writes twice within one
|
||||
* tick of the file system's clock leaves those as they were.
|
||||
*/
|
||||
function ideReader(file) {
|
||||
let last = null
|
||||
@@ -2892,16 +2929,17 @@ function ideReader(file) {
|
||||
let stamp = ""
|
||||
try {
|
||||
const st = fs.statSync(file)
|
||||
stamp = `${st.size}:${st.mtimeMs}:${st.ino}`
|
||||
if (!st.isFile()) throw new Error("not a file")
|
||||
const main = fileDigest(file)
|
||||
if (!main) throw new Error("unread")
|
||||
stamp = `${st.ino}:${main}`
|
||||
for (const suffix of ["-wal", "-journal"]) {
|
||||
try {
|
||||
const j = fs.statSync(file + suffix)
|
||||
stamp += `|${suffix}:${j.size}:${j.mtimeMs}`
|
||||
} catch {
|
||||
/* none */
|
||||
}
|
||||
const d = fileDigest(file + suffix)
|
||||
if (d === null) throw new Error("unread")
|
||||
if (d) stamp += `|${suffix}:${d}`
|
||||
}
|
||||
} catch {
|
||||
// unknown: read it again
|
||||
stamp = ""
|
||||
}
|
||||
if (stamp && last?.stamp === stamp) return last.result
|
||||
|
||||
@@ -4538,10 +4538,38 @@ async function rewriteIde(file, value) {
|
||||
const db = await x.openSqlite(file)
|
||||
sqlRun(db, "INSERT INTO ItemTable (key, value) VALUES (?, ?)", x.IDE_STATE_KEY, value)
|
||||
db.close()
|
||||
// a clock coarse enough to give the same mtime still sees the size or inode move
|
||||
const t = new Date(Date.now() + 5000)
|
||||
// each rewrite strictly later than the one before: two within one
|
||||
// millisecond would otherwise share an mtime
|
||||
lastIdeWrite = Math.max(Date.now() + 5000, lastIdeWrite + 1000)
|
||||
const t = new Date(lastIdeWrite)
|
||||
fs.utimesSync(file, t, t)
|
||||
}
|
||||
let lastIdeWrite = 0
|
||||
|
||||
test("the IDE's file rewritten in one clock tick, at the same size, is read again", async () => {
|
||||
const fx = await ideFixture({ value: ideValue(ideOrigin()) })
|
||||
// a clock that gives every write the same time
|
||||
const t = new Date(2000000000000)
|
||||
fs.utimesSync(fx.file, t, t)
|
||||
const before = fs.statSync(fx.file)
|
||||
const read = x.ideReader(fx.file)
|
||||
assert.equal((await read()).cred?.access_key_id, "IDEAK1")
|
||||
// the same file, the same time: no read
|
||||
assert.equal((await read()).cred?.access_key_id, "IDEAK1")
|
||||
// the IDE writes a key of the same length into the same page
|
||||
const db = await x.openSqlite(fx.file)
|
||||
sqlRun(db, "INSERT INTO ItemTable (key, value) VALUES (?, ?)", x.IDE_STATE_KEY, ideValue(ideOrigin({ accessKey: "IDEAK2" })))
|
||||
db.close()
|
||||
fs.utimesSync(fx.file, t, t)
|
||||
const after = fs.statSync(fx.file)
|
||||
assert.equal(after.size, before.size)
|
||||
assert.equal(after.mtimeMs, before.mtimeMs)
|
||||
assert.equal(after.ino, before.ino)
|
||||
assert.equal((await read()).cred?.access_key_id, "IDEAK2")
|
||||
// a file that can't be read is unknown, never the last read
|
||||
fs.rmSync(fx.file)
|
||||
assert.equal((await read()).cred, undefined)
|
||||
})
|
||||
|
||||
test("an international account chats, reads and renews on its own station only", async () => {
|
||||
const fx = await ideFixture({ value: ideValue(ideOrigin()) })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-codearts-auth",
|
||||
"version": "0.3.11",
|
||||
"version": "0.3.12",
|
||||
"description": "Huawei Cloud in OpenCode and magpie: CodeArts (码道) and OfficeAce (AgentArts), each from its own Huawei Cloud OAuth sign-in — models, credit rates, credit usage and the daily check-in (unofficial)",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
Reference in New Issue
Block a user