agy-cli 0.1.20: each step's usage from agy's own store, and 1.5.1+ inputTokens read as uncached (#983)
agy 1.5.1+ reports ACP `inputTokens` as uncached prompt tokens only; `promptUsage()` tells the exclusive shape from the inclusive one by `totalTokens`, so cache reads are no longer clipped and the learned cache ratio no longer pins to 1.0. Each HTTP step of a multi-step tool loop is settled from agy's own per-call rows in `conversations/<id>.db` (`gen_metadata` on 1.5.1+, `steps` type 15 on 1.4.x-1.5.0) when they reconcile with ACP's total; same-generation tool continuations settle at 0, a cancelled or fallen-back turn stops trusting the store, and a clipped cold-start replay bounds its fallback estimate. The reader (`step-usage.mjs`) never opens a store read-write. Under Bun it opens with `SQLITE_OPEN_READONLY | SQLITE_OPEN_URI` (Bun's SQLite on Linux and Windows parses `file:` URIs only with that flag), finalizes statements and waits on no busy timeout. A checkpointed WAL store with no `-wal`/`-shm` is read through `?immutable=1` and gains no side files; if `-wal` appears during that read it is read again live. Store paths are resolved to absolute first, and Windows `\\?\` and UNC paths get their own `file:` form. On an uncheckpointed store the only file a reader touches is SQLite's `-shm` WAL index. Reviewed head 9054f47295d51d6f83f8585dd550312ebff8448a (base dba24067; merges cleanly onto main7c44d027). What ran at that head, Bun 1.4.2 everywhere, fresh temp HOME (and USERPROFILE on Windows): - macOS (mote): `bun test packages/agy-cli` 14/14; step-usage 10/10, usage-settlement 33/33; `node --test test/step-usage.mjs` (Node 22.22) 10/10; `bun scripts/check.mjs` ok. - Linux (WSL Ubuntu 24.04): `bun test packages/agy-cli` 14/14; step-usage 10/10, usage-settlement 33/33; Node 22.23 10/10; check.mjs ok. - Windows 11: step-usage 10/10 (the loopback UNC share was reachable, so its branch ran), usage-settlement 33/33, Node 10/10, check.mjs ok. `bun test packages/agy-cli` 13/14: smoke's one red is "no call to the loaded tool reaches agy's validator in the old prompt", the known #973 (1 of 334 checks), not touched here. - Root `bun test` on mica, merged onto origin/main7c44d027, under suite.lock: 8407 pass, 20 skip, 0 fail across 593 files (fresh temp HOME). Broken by hand (restored after each): - Linux: SQLITE_OPEN_URI dropped -> step-usage 3 red, usage-settlement 2 red; `path.resolve` dropped -> the URI/relative-path test red (Bun and Node); the "no -wal after a failed immutable read returns null" guard dropped -> the mid-read test red (Bun and Node). - macOS: the re-read after `-wal` appears removed, and the failed-immutable-read path returning null outright -> the mid-read test red under Bun and Node (at 262145f4; the code is unchanged since). - Windows: the `\\` branch of `immutableUri` disabled -> the URI/UNC test red. Not tried: a real agy session or account (fake agy and fixture stores only); Bun versions other than 1.4.2; a remote (non-loopback) UNC share. Co-authored-by: tungloong <19512489+tungloong@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,344 @@
|
||||
// Per-model-call usage from agy's local conversation store.
|
||||
//
|
||||
// ACP reports usage once, accumulated over a whole session/prompt. Prefer
|
||||
// `gen_metadata`, written when generation ends, before client tools return.
|
||||
// Older stores record each completed model call in table `steps`, with
|
||||
// step_type 15 and status 3 (done), the counts in Protobuf field 9 of
|
||||
// `metadata` (2 uncached input, 3 output, 4 cache write, 5 cache read,
|
||||
// 9 thinking). Summed over a prompt they equal the ACP total exactly.
|
||||
// Protobuf layout first decoded by lili-luo/dsh-plugin-bundle
|
||||
// (dsh-llm-antigravity-acp, MIT); SQLite reader adapted from
|
||||
// DylanLIiii/opencode-antigravity-bridge (MIT). Any failure here returns null
|
||||
// and the caller keeps its estimate.
|
||||
import { createRequire } from "node:module"
|
||||
import fs from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { pathToFileURL } from "node:url"
|
||||
|
||||
const MODEL_STEP = 15
|
||||
const STEP_DONE = 3
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
let opener
|
||||
|
||||
// Prefer bun:sqlite under Bun (Bun's node:sqlite keeps an unfinalized statement's
|
||||
// file handle open until GC, locking the store on Windows); fall back to
|
||||
// node:sqlite on Node >= 22.5. Never open a read-write handle or wait on locks.
|
||||
function loadOpener() {
|
||||
if (opener !== undefined) return opener
|
||||
opener = null
|
||||
try {
|
||||
const { Database, constants } = require("bun:sqlite")
|
||||
if (typeof Database === "function") {
|
||||
// Bun's bundled SQLite on Linux/Windows disables URI filenames unless SQLITE_OPEN_URI is passed.
|
||||
const flags = constants.SQLITE_OPEN_READONLY | constants.SQLITE_OPEN_URI
|
||||
opener = (target) => {
|
||||
const db = new Database(target, flags)
|
||||
return {
|
||||
all: (sql, ...args) => {
|
||||
const stmt = db.prepare(sql)
|
||||
try {
|
||||
return stmt.all(...args)
|
||||
} finally {
|
||||
stmt.finalize()
|
||||
}
|
||||
},
|
||||
close: () => db.close(),
|
||||
}
|
||||
}
|
||||
return opener
|
||||
}
|
||||
} catch {}
|
||||
try {
|
||||
const { DatabaseSync } = require("node:sqlite")
|
||||
if (typeof DatabaseSync === "function") {
|
||||
// Only plain Node reaches this fallback; Node's DatabaseSync.close() finalizes prepared statements.
|
||||
opener = (target) => {
|
||||
const db = new DatabaseSync(target, { readOnly: true, timeout: 0 })
|
||||
return {
|
||||
all: (sql, ...args) => db.prepare(sql).all(...args),
|
||||
close: () => db.close(),
|
||||
}
|
||||
}
|
||||
return opener
|
||||
}
|
||||
} catch {}
|
||||
return opener
|
||||
}
|
||||
|
||||
export function conversationsDir(env = process.env) {
|
||||
return env.AGY_BRIDGE_CONVERSATIONS_DIR ||
|
||||
path.join(os.homedir(), ".gemini", "antigravity-cli", "conversations")
|
||||
}
|
||||
|
||||
export function sessionDbPath(sessionId, env = process.env) {
|
||||
if (!sessionId || !/^[A-Za-z0-9._-]+$/.test(String(sessionId))) return null
|
||||
return path.join(conversationsDir(env), `${sessionId}.db`)
|
||||
}
|
||||
|
||||
function varint(buf, at) {
|
||||
let value = 0
|
||||
let scale = 1
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const byte = buf[at + i]
|
||||
if (byte === undefined) return null
|
||||
value += (byte & 0x7f) * scale
|
||||
if ((byte & 0x80) === 0) return { value, next: at + i + 1 }
|
||||
scale *= 128
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Calls fn(field, wire, value | bytes) for each field; false on malformed input.
|
||||
function walk(buf, fn) {
|
||||
let at = 0
|
||||
while (at < buf.length) {
|
||||
const tag = varint(buf, at)
|
||||
if (!tag || tag.value < 8) return false
|
||||
at = tag.next
|
||||
const field = Math.floor(tag.value / 8)
|
||||
const wire = tag.value % 8
|
||||
if (wire === 0) {
|
||||
const item = varint(buf, at)
|
||||
if (!item) return false
|
||||
at = item.next
|
||||
fn(field, wire, item.value)
|
||||
} else if (wire === 2) {
|
||||
const len = varint(buf, at)
|
||||
if (!len || len.next + len.value > buf.length) return false
|
||||
fn(field, wire, buf.subarray(len.next, len.next + len.value))
|
||||
at = len.next + len.value
|
||||
} else if (wire === 1 || wire === 5) {
|
||||
at += wire === 1 ? 8 : 4
|
||||
if (at > buf.length) return false
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const count = (value) => Number.isSafeInteger(value) && value > 0 ? value : 0
|
||||
|
||||
function decodeUsage(buf) {
|
||||
const usage = { input: 0, output: 0, write: 0, read: 0, thinking: 0 }
|
||||
const ok = walk(buf, (field, wire, n) => {
|
||||
if (wire !== 0) return
|
||||
if (field === 2) usage.input = count(n)
|
||||
else if (field === 3) usage.output = count(n)
|
||||
else if (field === 4) usage.write = count(n)
|
||||
else if (field === 5) usage.read = count(n)
|
||||
else if (field === 9) usage.thinking = count(n)
|
||||
})
|
||||
return ok ? usage : null
|
||||
}
|
||||
|
||||
export function decodeStepUsage(metadata) {
|
||||
const buf = metadata instanceof Uint8Array ? metadata
|
||||
: ArrayBuffer.isView(metadata) ? new Uint8Array(metadata.buffer, metadata.byteOffset, metadata.byteLength)
|
||||
: null
|
||||
if (!buf) return null
|
||||
let stats = null
|
||||
const ok = walk(buf, (field, wire, value) => {
|
||||
if (field !== 9 || wire !== 2) return
|
||||
stats = decodeUsage(value)
|
||||
})
|
||||
return ok ? stats : null
|
||||
}
|
||||
|
||||
// gen_metadata.data contains the same usage message at field 1 -> 4.
|
||||
// Its pre-send local context estimate is separate, at 1 -> 9 -> 10 -> 1.
|
||||
// A context estimate alone is not a completed, billed model call.
|
||||
export function decodeGenerationUsage(data) {
|
||||
const buf = data instanceof Uint8Array ? data
|
||||
: ArrayBuffer.isView(data) ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
||||
: null
|
||||
if (!buf) return null
|
||||
let stats = null
|
||||
let context = null
|
||||
let valid = true
|
||||
const ok = walk(buf, (field, wire, chat) => {
|
||||
if (field !== 1 || wire !== 2) return
|
||||
valid = walk(chat, (sub, subWire, value) => {
|
||||
if (subWire !== 2) return
|
||||
if (sub === 4) {
|
||||
stats = decodeUsage(value)
|
||||
if (!stats) valid = false
|
||||
} else if (sub === 9) {
|
||||
if (!walk(value, (key, kind, start) => {
|
||||
if (key !== 10 || kind !== 2) return
|
||||
if (!walk(start, (k, w, n) => {
|
||||
if (k === 1 && w === 0) context = count(n)
|
||||
})) valid = false
|
||||
})) valid = false
|
||||
}
|
||||
}) && valid
|
||||
})
|
||||
return ok && valid && stats ? { ...stats, ...(context !== null ? { context } : {}) } : null
|
||||
}
|
||||
|
||||
function runReadOnly(open, target, fn) {
|
||||
let db
|
||||
try {
|
||||
db = open(target)
|
||||
return fn(db)
|
||||
} finally {
|
||||
try { db?.close() } catch {}
|
||||
}
|
||||
}
|
||||
|
||||
function tableQuery(db, sql, ...args) {
|
||||
try {
|
||||
return db.all(sql, ...args)
|
||||
} catch (error) {
|
||||
if (/no such table|no such column/i.test(String(error?.message || ""))) return null
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function isCheckpointedWal(file) {
|
||||
if (fs.existsSync(`${file}-wal`) || fs.existsSync(`${file}-shm`) || fs.existsSync(`${file}-journal`)) return false
|
||||
let fd
|
||||
try {
|
||||
fd = fs.openSync(file, "r")
|
||||
const header = new Uint8Array(20)
|
||||
if (fs.readSync(fd, header, 0, 20, 0) < 20) return false
|
||||
return header[18] === 2 && header[19] === 2
|
||||
} catch {
|
||||
return false
|
||||
} finally {
|
||||
if (fd !== undefined) {
|
||||
try { fs.closeSync(fd) } catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resolveStorePath(file) {
|
||||
if (typeof file !== "string" || !file || file.includes("\0")) return null
|
||||
return path.resolve(file)
|
||||
}
|
||||
|
||||
// SQLite rejects non-localhost URI authorities (`file://server/share/...`) unless
|
||||
// compiled with SQLITE_ALLOW_URI_AUTHORITY, strips `localhost` from `\\localhost\...`,
|
||||
// and pathToFileURL rejects `\\?\...` extended paths. Prefixing Windows `\\...` paths
|
||||
// directly after `file:` (with `%`, `?`, `#`, space percent-encoded) avoids any
|
||||
// `//authority` segment so sqlite3ParseUri hands the exact `\\...` path to winOpen.
|
||||
function immutableUri(resolved) {
|
||||
if (process.platform === "win32" && resolved.startsWith("\\\\")) {
|
||||
return `file:${resolved.replace(/[%?# ]/g, (ch) => `%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`)}?immutable=1`
|
||||
}
|
||||
return `${pathToFileURL(resolved).href}?immutable=1`
|
||||
}
|
||||
|
||||
// When a WAL-flagged store (header bytes 18..19 = 2, 2) has neither -wal nor
|
||||
// -shm on disk, no writer holds it open, yet SQLITE_OPEN_READONLY fails with
|
||||
// SQLITE_CANTOPEN under Apple's libsqlite3 on macOS and creates -wal/-shm
|
||||
// sidecars under upstream SQLite. Opening with ?immutable=1 keeps that closed
|
||||
// WAL read strictly side-effect-free; if -wal appears concurrently, re-read
|
||||
// once against the resolved live file path (never a relative `file:...` string).
|
||||
function withStore(file, fn) {
|
||||
const resolved = resolveStorePath(file)
|
||||
if (!resolved || !fs.existsSync(resolved)) return null
|
||||
const open = loadOpener()
|
||||
if (!open) return null
|
||||
if (isCheckpointedWal(resolved)) {
|
||||
try {
|
||||
const result = runReadOnly(open, immutableUri(resolved), fn)
|
||||
if (!fs.existsSync(`${resolved}-wal`)) return result
|
||||
} catch {
|
||||
if (!fs.existsSync(`${resolved}-wal`)) return null
|
||||
}
|
||||
}
|
||||
try {
|
||||
return runReadOnly(open, resolved, fn)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function maxCompletedStep(db) {
|
||||
const rows = tableQuery(db,
|
||||
`SELECT COALESCE(MAX(idx), -1) AS idx FROM steps WHERE step_type = ${MODEL_STEP} AND status = ${STEP_DONE}`)
|
||||
const idx = Number(rows?.[0]?.idx)
|
||||
return Number.isSafeInteger(idx) ? idx : null
|
||||
}
|
||||
|
||||
function stepsAfter(db, afterIdx) {
|
||||
const rows = tableQuery(db,
|
||||
`SELECT idx, metadata FROM steps WHERE step_type = ${MODEL_STEP} AND status = ${STEP_DONE} AND idx > ? ORDER BY idx`,
|
||||
afterIdx)
|
||||
if (!rows) return null
|
||||
const steps = []
|
||||
for (const row of rows) {
|
||||
const idx = Number(row.idx)
|
||||
const usage = decodeStepUsage(row.metadata)
|
||||
if (!Number.isSafeInteger(idx) || !usage) continue
|
||||
steps.push({ idx, ...usage })
|
||||
}
|
||||
return steps
|
||||
}
|
||||
|
||||
// Highest completed model-call index, -1 for an empty or missing store, null
|
||||
// when the store exists but cannot be read.
|
||||
export function lastModelStep(file) {
|
||||
const resolved = resolveStorePath(file)
|
||||
if (!resolved) return null
|
||||
if (!fs.existsSync(resolved)) return -1
|
||||
return withStore(resolved, (db) => maxCompletedStep(db))
|
||||
}
|
||||
|
||||
// Completed model calls after `afterIdx`, oldest first: [{ idx, input, output, write, read, thinking }].
|
||||
export function modelStepsAfter(file, afterIdx) {
|
||||
return withStore(file, (db) => stepsAfter(db, afterIdx))
|
||||
}
|
||||
|
||||
// Pin a turn to one table. Reading both tables for a turn would bill the same
|
||||
// generation twice. A new store is discovered after its first generation.
|
||||
export function modelUsageCursor(file) {
|
||||
const resolved = resolveStorePath(file)
|
||||
if (!resolved) return null
|
||||
if (!fs.existsSync(resolved)) return { source: null, idx: -1, generationIdx: -1 }
|
||||
return withStore(resolved, (db) => {
|
||||
const genRows = tableQuery(db, "SELECT COALESCE(MAX(idx), -1) AS idx FROM gen_metadata")
|
||||
const generationIdx = Number(genRows?.[0]?.idx)
|
||||
const step = maxCompletedStep(db)
|
||||
if (Number.isSafeInteger(generationIdx)) {
|
||||
return step === null ? { source: "gen_metadata", idx: generationIdx }
|
||||
: { source: null, idx: step, generationIdx }
|
||||
}
|
||||
return step === null ? null : { source: "steps", idx: step }
|
||||
})
|
||||
}
|
||||
|
||||
export function modelUsageAfter(file, cursor) {
|
||||
if (!cursor) return null
|
||||
return withStore(file, (db) => {
|
||||
if (cursor.source !== "steps") {
|
||||
const after = cursor.source === "gen_metadata" ? cursor.idx : cursor.generationIdx
|
||||
const rows = tableQuery(db, "SELECT idx, data FROM gen_metadata WHERE idx > ? ORDER BY idx", after)
|
||||
if (rows) {
|
||||
const steps = []
|
||||
let pending = false
|
||||
for (const row of rows) {
|
||||
const idx = Number(row.idx)
|
||||
const usage = decodeGenerationUsage(row.data)
|
||||
if (!Number.isSafeInteger(idx)) return null
|
||||
// Failed generations and pre-send estimates carry no billing counters.
|
||||
if (!usage || !(usage.input + usage.read + usage.write + usage.output + usage.thinking)) {
|
||||
pending = true
|
||||
continue
|
||||
}
|
||||
pending = false
|
||||
steps.push({ idx, ...usage })
|
||||
}
|
||||
if (steps.length || pending || cursor.source === "gen_metadata") {
|
||||
return { source: "gen_metadata", steps, ...(pending ? { pending: true } : {}) }
|
||||
}
|
||||
}
|
||||
if (cursor.source === "gen_metadata") return null
|
||||
}
|
||||
const steps = stepsAfter(db, cursor.idx)
|
||||
return steps === null ? null : { source: "steps", steps }
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user