local uv = require('luv') local M = {} local key = 'panto-antigravity:private-profile' -- all configured aliases share ACP's one private profile local max_bytes, refresh_ms, stale_s = 32768, 60000, 300 local function name(s) return #s <= 100 and not s:find('[^ -~]') and s:find('%S') end -- agy 1.5.18 bundled changelog and static inspection of printmode.quotaRows, -- bucketRemaining, bucketReset and headless.writeRows: piped /usage emits -- group name, bucket name, remaining (%.0f%%, %d or disabled), UTC RFC3339 or empty. -- There is no header; integer amounts do not establish a denominator. function M.parse(body, reset_time) if type(body) ~= 'string' or #body > max_bytes then return nil end local windows, seen, count = {}, {}, 0 if body == '' then return windows end if body:sub(-1) ~= '\n' then return nil end -- the writer terminates every record for line in body:gmatch('([^\n]*)\n') do local group, bucket, remaining, reset = line:match('^([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)$') if not group or (group ~= '' and not name(group)) or not name(bucket) then return nil end local id = group .. '\t' .. bucket count = count + 1 if count > 64 or seen[id] then return nil end seen[id] = true local pct = tonumber(remaining:match('^(%d%d?%d?)%%$')) local at if reset ~= '' then if not reset:match('^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%dZ$') then return nil end at = reset_time(reset) if not at or at < 0 then return nil end end if pct then if pct > 100 or tostring(pct) .. '%' ~= remaining then return nil end elseif remaining == 'disabled' then if at then return nil end elseif not remaining:match('^%-?%d+$') then return nil end if pct or at then local label = group ~= '' and group ~= bucket and (group .. ' / ' .. bucket) or bucket windows[#windows + 1] = { label = pct and label:gsub(' Limit Remaining$', ' Limit Used') or label, used_percent = pct and 100 - pct or nil, resets_at = at, } end end return windows end local function close(handle) if handle and not handle:is_closing() then handle:close() end end function M.activate(ext, footer, acp) local state = footer.install(ext) if not state or state.closed then return function() end end if state.antigravity_refresh then return state.antigravity_refresh end local alive, pending, next_run = true, nil, 0 local function publish(windows) if not alive then return end local function section(rows, why) return footer.section(ext, 'antigravity', rows, { status = why }) end local render if windows and #windows > 0 then local expires = os.time() + stale_s local stale = section({}, 'stale') local cached, deadline = nil, 0 render = function(now) if now >= expires then return stale(now) end if not cached or now >= deadline then local current = {} deadline = expires for _, w in ipairs(windows) do if not w.resets_at or w.resets_at > now then current[#current + 1] = w if w.resets_at then deadline = math.min(deadline, w.resets_at) end end end cached = section(current, #current < #windows and 'stale' or nil) end return cached(now) end else render = section({}, windows and 'unknown' or 'unavailable') end state.sections[key] = nil -- never leave a previous snapshot on a failed publication pcall(footer.publish, ext, key, render) end -- The caller supplies direct-turn provenance. A rendered footer also gates -- headless use. Drop duplicate calls in flight or inside the cooldown; no polling. local function refresh() if not alive or not state.handle or pending then return end local now = uv.hrtime() / 1000000 if now < next_run then return end next_run = now + refresh_ms local req, chunks, bytes = {}, {}, 0 pending = req local function finish(windows) if req.done then return end req.done, chunks = true, nil if req.cancel then pcall(req.cancel); req.cancel = nil end close(req.timer); close(req.stdout) if not windows and req.pid then pcall(uv.kill, -req.pid, 'sigkill') end if not req.child then pending = nil end -- don't launch over a child awaiting reap publish(windows) end req.stop = function() finish(nil) end local function complete() if not req.done and req.eof and req.exited then finish(M.parse(table.concat(chunks), footer.reset_time)) end end local ok = pcall(function() req.timer = assert(uv.new_timer()) local cancel = acp.probe(function(bin, profile, env) if req.done or req.stdout then return end req.cancel = nil -- verification has completed assert(req.timer:start(15000, 0, req.stop)) req.stdout = assert(uv.new_pipe(false)) -- Detached (no controlling tty) + closed input. The CLI's -- print-mode auth path returns without interactive login in this case. req.child, req.pid = uv.spawn(bin, { args = {'--gemini_dir', profile, '--sandbox', '-p', '/usage'}, cwd = profile, env = env, detached = true, stdio = {nil, req.stdout, nil}, }, function(code, sig) close(req.child); req.child, req.pid = nil, nil if req.done then pending = nil; return end if code ~= 0 or sig ~= 0 then req.stop(); return end req.exited = true complete() end) if not req.child then req.pid = nil; req.stop(); return end assert(req.stdout:read_start(function(err, data) if req.done then return end if err then req.stop(); return end if not data then req.eof = true; close(req.stdout); complete(); return end bytes = bytes + #data if bytes > max_bytes then req.stop(); return end chunks[#chunks + 1] = data end)) end, req.stop) if not req.done and not req.stdout then req.cancel = cancel end end) if not ok then req.stop() end end -- Install the row immediately without touching the account. Only the human -- turn callback below may replace the placeholder with an actual reading. pcall(footer.publish,ext,key,footer.section(ext,'antigravity',{}, {status='awaiting first turn'})) state.antigravity_refresh = refresh ext.on('session_end', function() alive = false if pending then pending.stop() end state.antigravity_refresh, state.sections[key] = nil, nil end) return refresh end return M