Skip to content

v6.5.0 — read the screen, not the bytes

Choose a tag to compare

@moerasermax moerasermax released this 06 Oct 06:37
· 175 commits to master since this release
claude mcp add ai-cli -s user -- npx -y @tkflyc/ai-cli-mcp

Read the screen, not the bytes

query_usage runs each vendor's interactive TUI in a pseudo-terminal and parses what it prints. The parser used to strip the escape codes and read what was left. That stopped working, because these TUIs only redraw the cells that changed: ESC[row;colH moves to a row, ESC[nX and ESC[K erase, and ESC[nC skips n cells and keeps what was already there — it is not a space. Strip the codes and Codex's Pro␛[1C100 becomes Pro100. Treat the skip as a space and Claude's Current sess␛[1Con — where the i was skipped because it hadn't changed — becomes Current sess on.

The new renderTerminal() plays the output into a small screen buffer, following terminal rules (cursor moves, erases, line feeds and scrolling, double-width CJK characters), and parses what is actually visible. All three vendors now go through it, and it fixed three bugs at once:

  • Claude returned ok with every field null. Current session had been squashed into Currentsession, so no pattern matched. The panel now parses — session 7% used and the week 44% in the live check — and a result with no percentages at all is an error. Claude's percentages are marked percentBasis: "used", the opposite direction from Codex's percentRemaining.
  • agy's remaining quota was reported as used. The panel says "Weekly Limit Remaining 99.86%"; the old parser put 99.86 in percentUsed, which reads as nearly exhausted. Labels that say Remaining or Used are now converted accordingly, with both directions and a basis, and model is the group name (GEMINI MODELS, CLAUDE AND GPT MODELS).
  • Codex model and account were always null. Model: had been glued onto the end of the previous line. Codex 0.160 also no longer shows an email, so account is returned exactly as displayed (for example Pro 100).

Quotas beyond the main ones — Codex's Luna Reserve Weekly limit, Claude's Current week (Fable) — used to be dropped silently. They are listed in additionalLimits.

Codex quota without opening a TUI

Codex writes rate_limits into its session files at the end of every turn. When one is less than 10 minutes old and its window has not reset, query_usage reads it instead: about 0.1 seconds versus 8 or more for the TUI, and immune to elevation, daemon, or hook-trust prompts. Results carry source: "session-file" and asOf. refresh=true always uses the TUI. Session files have no model or account, so those are null on this path.

Polling stopped flooding the caller

While a Claude job was running it had no reply yet, so wait fell back to returning the entire raw stdout — about 90 KB of stream-json, mostly the init event — on every poll, straight into the caller's context. That is ai-cli's own cost, not the vendor's. Running jobs now return only liveness; use peek for live events. A finished job with no parsed reply returns the last 4,096 characters, marked with stdoutTruncated / stderrTruncated. With verbose, a job without a parsed reply still returns everything.

Usage reporting, completed

agy and direct-api jobs now return agentOutput.usage in the same shape as Codex and Claude. agy reports no cache figures, so none are invented. For direct-api, the last usage record of a response wins instead of summing, so providers that send cumulative counts in every chunk are not double-counted. ai-cli run on the CLI now returns the same high-effort warnings as MCP.

Releases check themselves

While publishing 6.4.0, an agent had written backups and logs into dist/. dist is git-ignored, so nothing showed up in git status — but files shipped all of dist, and the first publish attempt carried 19 extra files. It only failed because npm wasn't logged in. Now files ships only dist/**/*.js and .js.map, and tools/verify-release.mjs runs as prepublishOnly: clean working tree, HEAD equal to the version tag, every packed file traced back to a source file, NOTICE present. Any failure stops npm publish.

Compatibility

Three visible changes, all corrections:

  • agy rows labelled "Limit Remaining": percentUsed now really means used. Read percentRemaining and check basis.
  • Claude with no readable percentage: status is now "error" instead of "ok" with nulls.
  • Running jobs: compact wait / get_result results no longer include stdout. Use verbose: true or peek.

Two independent audits found nothing of high severity. Every confirmed finding was fixed, and the ones judged invalid are recorded in the CHANGELOG with the reason.

Full details: CHANGELOG.


繁體中文

讀畫面,不是讀位元組

query_usage 會在偽終端機(PTY)裡開各家的互動式 TUI,再解析它印出來的內容。原本的做法是刪掉跳脫碼、讀剩下的文字,但這招不管用了,因為這些 TUI 只重畫有變動的格子:ESC[row;colH 移到指定的列與欄,ESC[nX 和 ESC[K 清除內容,ESC[nC 是跳過 n 格、保留原本的內容,不是空格。刪掉跳脫碼,codex 的 Pro␛[1C100 就變成 Pro100;把「跳過」當成空格,claude 的 Current sess␛[1Con(那個 i 沒變所以被跳過)又會變成 Current sess on。

新的 renderTerminal() 照終端機的規則把輸出播放到一個小型畫面緩衝區(游標移動、清除、換行與捲動、中日韓的雙寬字元),再解析畫面上實際看得到的內容。三家都改走它,一次修好三個 bug:

  • claude 回 ok,所有欄位卻都是 null。 Current session 被擠成 Currentsession,一個正規式都對不上。現在面板解析得出來(實測 session 已用 7%、本週 44%),而且一個百分比都讀不到時會回 error。claude 的百分比標上 percentBasis: "used",方向跟 codex 的 percentRemaining 相反。
  • agy 的剩餘額度被當成已用量。 面板寫的是「Weekly Limit Remaining 99.86%」,舊的解析器卻把 99.86 放進 percentUsed,看起來像快用完了。現在標籤寫明 Remaining 或 Used 的,會照標籤換算,同時給出兩個方向和 basis;model 改成群組名稱(GEMINI MODELS、CLAUDE AND GPT MODELS)。
  • codex 的 model 和 account 永遠是 null。 Model: 被黏到上一行的尾巴。codex 0.160 也不再顯示 email,所以 account 照畫面原樣回傳(例如 Pro 100)。

主額度以外的額度,例如 codex 的 Luna Reserve Weekly limit、claude 的 Current week (Fable),以前會被默默丟掉,現在列在 additionalLimits。

查 Codex 額度不用開 TUI

codex 每個 turn 結束時都會把 rate_limits 寫進 session 檔。只要最新那筆距今未滿 10 分鐘、而且額度週期還沒重置,query_usage 就直接讀它:約 0.1 秒,開 TUI 要 8 秒以上,也不受系統管理員權限、daemon 或 hook 信任畫面影響。結果帶 source: "session-file" 和 asOf。refresh=true 一律走 TUI。session 檔裡沒有模型和帳號,所以這條路徑的這兩個欄位是 null。

輪詢不再把原始輸出灌給呼叫端

claude job 執行中還沒有回覆時,wait 會退回去傳整份原始 stdout,大約 90 KB 的 stream-json,大部分是開頭的 init 事件。每輪詢一次,就有這麼多內容進到呼叫端的 context。這是 ai-cli 自己造成的成本,不是 vendor 的。現在執行中的 job 只回傳 liveness,要看即時事件用 peek。已結束但沒有解析出回覆的 job,只回傳最後 4,096 個字元,並標上 stdoutTruncated / stderrTruncated。沒有解析出回覆時,verbose 仍會回傳完整內容。

用量回報補齊

agy 和 direct-api 的 job 現在也會回傳 agentOutput.usage,形狀跟 codex、claude 相同。agy 不回報快取數字,就不捏造。direct-api 同一個回應的用量取最後一筆,不再加總,所以每個 chunk 都送累計值的供應商不會被重複計算。CLI 的 ai-cli run 也會回傳跟 MCP 相同的高成本 effort warnings。

發版會自己檢查

發 6.4.0 時,一個 agent 把備份和 log 寫進了 dist/。dist 被 git 忽略,git status 看不出來,但 files 會打包整個 dist,第一次嘗試發佈時多帶了 19 個檔案,只是因為 npm 剛好沒登入才失敗。現在 files 只打包 dist/**/*.js 和 .js.map,而且 tools/verify-release.mjs 會在 prepublishOnly 自動執行:工作樹要乾淨、HEAD 要等於版本 tag、每個打包的檔案都要對得到原始碼、NOTICE 要在套件裡。任何一項不過,npm publish 就會被擋下。

相容性

有三處看得到的變化,都是修正:

  • agy 標示「Limit Remaining」的那幾列:percentUsed 現在真的是已用量。請改讀 percentRemaining 並看 basis。
  • claude 一個百分比都讀不到時:status 改回 "error",不再是 "ok" 加上一排 null。
  • 執行中的 job:精簡的 wait / get_result 結果不再包含 stdout。請改用 verbose: true 或 peek。

兩輪獨立稽核都沒有發現高嚴重度問題。確認成立的項目全部修正,判定不成立的項目連同理由都記在 CHANGELOG。

完整內容:CHANGELOG。