diff --git a/packages/codearts/index.mjs b/packages/codearts/index.mjs index 92fcd1b..88eb856 100644 --- a/packages/codearts/index.mjs +++ b/packages/codearts/index.mjs @@ -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 diff --git a/packages/codearts/index.test.mjs b/packages/codearts/index.test.mjs index e666a49..f1cb16c 100644 --- a/packages/codearts/index.test.mjs +++ b/packages/codearts/index.test.mjs @@ -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()) }) diff --git a/packages/codearts/package.json b/packages/codearts/package.json index d7c9d93..1e2c94e 100644 --- a/packages/codearts/package.json +++ b/packages/codearts/package.json @@ -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",