|
| 1 | +#!/usr/bin/env node |
| 2 | +// Measures what a saved Zoo Code task is made of, to see what the webview has to hold: sizes only, never prints message content. |
| 3 | +// |
| 4 | +// node scripts/gray-screen/analyze-session.mjs [--storage <globalStorage>] [--top 15] [--task <id>] [--include-generated true] |
| 5 | +// |
| 6 | +// Per task: file size, message counts, bytes per message kind and per tool, bytes per JSON field of tool payloads |
| 7 | +// (originalContent / content / diff ...), share of strings V8 stores as 2 bytes/char (any char above U+00FF), and the |
| 8 | +// longest run of consecutive file-edit asks that ChatView's batchNearby would merge into one message. |
| 9 | +import fs from "node:fs" |
| 10 | +import os from "node:os" |
| 11 | +import path from "node:path" |
| 12 | + |
| 13 | +const args = Object.fromEntries( |
| 14 | + process.argv.slice(2).reduce((acc, cur, i, all) => { |
| 15 | + if (cur.startsWith("--")) acc.push([cur.slice(2), all[i + 1]]) |
| 16 | + return acc |
| 17 | + }, []), |
| 18 | +) |
| 19 | +const storage = |
| 20 | + args["storage"] ?? path.join(os.homedir(), ".vscode-server", "data", "User", "globalStorage", "codemate.zoo-code") |
| 21 | +const top = Number(args["top"] ?? 15) |
| 22 | +const includeGenerated = args["include-generated"] === "true" |
| 23 | +const tasksDir = path.join(storage, "tasks") |
| 24 | + |
| 25 | +const EDIT_TOOLS = new Set(["editedExistingFile", "appliedDiff", "newFileCreated", "insertContent", "searchAndReplace"]) |
| 26 | +const BOUNDARY_SAY = new Set(["user_feedback", "user_feedback_diff", "completion_result", "checkpoint_saved", "error", "condense_context", "codebase_search_result"]) |
| 27 | +const NON_LATIN1 = /[^\u0000-ÿ]/ |
| 28 | +const mb = (n) => (n / 1048576).toFixed(1) |
| 29 | + |
| 30 | +const isIgnorable = (m) => m.type === "say" && (m.say === "api_req_started" || (m.say === "text" && !m.text?.trim()) || m.say === "reasoning") |
| 31 | +const isBoundary = (m) => m.type === "say" && (BOUNDARY_SAY.has(m.say) || (m.say === "text" && !!m.text?.trim())) |
| 32 | + |
| 33 | +function analyze(taskId) { |
| 34 | + const file = path.join(tasksDir, taskId, "ui_messages.json") |
| 35 | + const raw = fs.readFileSync(file, "utf8") |
| 36 | + const messages = JSON.parse(raw) |
| 37 | + const r = { taskId, fileBytes: Buffer.byteLength(raw), count: messages.length, kinds: {}, tools: {}, fields: {}, imageBytes: 0, twoByteBytes: 0, latin1Bytes: 0, maxRun: 0, maxRunBytes: 0, runs2plus: 0 } |
| 38 | + |
| 39 | + const add = (obj, key, bytes) => { |
| 40 | + const e = (obj[key] ??= { n: 0, bytes: 0 }) |
| 41 | + e.n++ |
| 42 | + e.bytes += bytes |
| 43 | + } |
| 44 | + const classify = (str) => { |
| 45 | + if (NON_LATIN1.test(str)) r.twoByteBytes += str.length * 2 |
| 46 | + else r.latin1Bytes += str.length |
| 47 | + } |
| 48 | + |
| 49 | + for (const m of messages) { |
| 50 | + const text = typeof m.text === "string" ? m.text : "" |
| 51 | + add(r.kinds, `${m.type}:${m.say ?? m.ask ?? "?"}`, text.length) |
| 52 | + if (text) classify(text) |
| 53 | + if (Array.isArray(m.images)) for (const img of m.images) r.imageBytes += typeof img === "string" ? img.length : 0 |
| 54 | + if (m.type === "ask" && m.ask === "tool" && text) { |
| 55 | + try { |
| 56 | + const t = JSON.parse(text) |
| 57 | + add(r.tools, String(t.tool), text.length) |
| 58 | + for (const [k, v] of Object.entries(t)) add(r.fields, k, typeof v === "string" ? v.length : JSON.stringify(v)?.length ?? 0) |
| 59 | + } catch {} |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // longest run of consecutive edit asks that batchNearby merges (same rules as batchNearby.ts) |
| 64 | + const isEditAsk = (m) => { |
| 65 | + if (m.type !== "ask" || m.ask !== "tool" || !m.text) return false |
| 66 | + try { |
| 67 | + const t = JSON.parse(m.text) |
| 68 | + return EDIT_TOOLS.has(t.tool) && !t.batchDiffs |
| 69 | + } catch { |
| 70 | + return false |
| 71 | + } |
| 72 | + } |
| 73 | + const items = messages.slice(1) |
| 74 | + for (let i = 0; i < items.length; ) { |
| 75 | + if (isBoundary(items[i])) { i++; continue } |
| 76 | + if (!isEditAsk(items[i])) { i++; continue } |
| 77 | + let j = i + 1, len = 1, bytes = items[i].text.length |
| 78 | + while (j < items.length) { |
| 79 | + if (isBoundary(items[j])) break |
| 80 | + if (isEditAsk(items[j])) { len++; bytes += items[j].text.length; j++ } |
| 81 | + else if (isIgnorable(items[j])) j++ |
| 82 | + else break |
| 83 | + } |
| 84 | + if (len > 1) r.runs2plus++ |
| 85 | + if (len > r.maxRun) { r.maxRun = len; r.maxRunBytes = bytes } |
| 86 | + i = j |
| 87 | + } |
| 88 | + return r |
| 89 | +} |
| 90 | + |
| 91 | +const taskMeta = (id) => { |
| 92 | + try { return JSON.parse(fs.readFileSync(path.join(tasksDir, id, "history_item.json"), "utf8")) } catch { return {} } |
| 93 | +} |
| 94 | +const ids = args["task"] ? [args["task"]] : fs.readdirSync(tasksDir).filter((id) => fs.existsSync(path.join(tasksDir, id, "ui_messages.json"))) |
| 95 | +const sizes = ids.map((id) => ({ id, size: fs.statSync(path.join(tasksDir, id, "ui_messages.json")).size, generated: (taskMeta(id).task ?? "").startsWith("[LOAD TEST]") })) |
| 96 | +const real = sizes.filter((s) => includeGenerated || !s.generated) |
| 97 | + |
| 98 | +const sorted = real.map((s) => s.size).sort((a, b) => a - b) |
| 99 | +const pct = (p) => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * p))] ?? 0 |
| 100 | +console.log(`storage: ${storage}`) |
| 101 | +console.log(`tasks: ${ids.length} (excluded ${sizes.length - real.length} generated [LOAD TEST] tasks${includeGenerated ? " - included" : ""}); analyzed set: ${real.length}`) |
| 102 | +console.log(`ui_messages.json size: p50=${mb(pct(0.5))}MB p90=${mb(pct(0.9))}MB p99=${mb(pct(0.99))}MB max=${mb(sorted.at(-1) ?? 0)}MB; over 10MB: ${sorted.filter((s) => s > 10 * 1048576).length}, over 5MB: ${sorted.filter((s) => s > 5 * 1048576).length}`) |
| 103 | + |
| 104 | +const results = real.sort((a, b) => b.size - a.size).slice(0, top).map((s) => analyze(s.id)) |
| 105 | +console.log(`\nTop ${results.length} tasks by ui_messages.json size (sizes only, no content):`) |
| 106 | +console.log("size(MB) msgs toolTxt(MB) editTools topField(share) 2byte% imgMB maxBatchRun(bytes MB) task") |
| 107 | +for (const r of results) { |
| 108 | + const toolBytes = Object.values(r.tools).reduce((a, b) => a + b.bytes, 0) |
| 109 | + const editCount = Object.entries(r.tools).filter(([k]) => EDIT_TOOLS.has(k)).reduce((a, [, v]) => a + v.n, 0) |
| 110 | + const fieldTotal = Object.values(r.fields).reduce((a, b) => a + b.bytes, 0) || 1 |
| 111 | + const [topKey, topVal] = Object.entries(r.fields).sort((a, b) => b[1].bytes - a[1].bytes)[0] ?? ["-", { bytes: 0 }] |
| 112 | + const text = r.latin1Bytes + r.twoByteBytes || 1 |
| 113 | + const meta = taskMeta(r.taskId) |
| 114 | + console.log( |
| 115 | + `${mb(r.fileBytes).padStart(7)} ${String(r.count).padStart(5)} ${mb(toolBytes).padStart(9)} ${String(editCount).padStart(9)} ${`${topKey} ${(100 * topVal.bytes / fieldTotal).toFixed(0)}%`.padEnd(30)} ${(100 * r.twoByteBytes / text).toFixed(0).padStart(5)}% ${mb(r.imageBytes).padStart(5)} ${`${r.maxRun} (${mb(r.maxRunBytes)})`.padEnd(20)} ${(meta.task ?? "").slice(0, 28).replace(/\s+/g, " ")} [${r.taskId.slice(0, 8)}]`, |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +const biggest = results[0] |
| 120 | +if (biggest) { |
| 121 | + console.log(`\nBreakdown of the largest analyzed task [${biggest.taskId.slice(0, 8)}], ${mb(biggest.fileBytes)}MB, ${biggest.count} messages:`) |
| 122 | + const show = (title, obj, n = 8) => { |
| 123 | + console.log(` ${title}`) |
| 124 | + for (const [k, v] of Object.entries(obj).sort((a, b) => b[1].bytes - a[1].bytes).slice(0, n)) console.log(` ${k.padEnd(28)} n=${String(v.n).padStart(5)} ${mb(v.bytes).padStart(7)}MB`) |
| 125 | + } |
| 126 | + show("by message kind (text bytes):", biggest.kinds) |
| 127 | + show("by tool (payload bytes):", biggest.tools) |
| 128 | + show("by tool payload field (string chars):", biggest.fields) |
| 129 | +} |
0 commit comments