Repository navigation
Expand file tree
/
Copy pathstep-usage.mjs
More file actions
345 lines (322 loc) · 12 KB
/
Copy pathstep-usage.mjs
File metadata and controls
345 lines (322 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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 including thinking, 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 }
})
}