opencode-statboard 2.8.3 → 2.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +17 -6
- package/attr-worker.js +1 -0
- package/package.json +1 -1
- package/server.js +73 -21
package/AGENTS.md
CHANGED
|
@@ -16,24 +16,34 @@ opencode-dashboard
|
|
|
16
16
|
- `attr-worker.js` — Stats worker (`worker_threads`). Runs the heavy scan off the request thread and posts back the serialized bundle + tool counts. `server.js` is `require`-able (it guards all side effects behind `require.main === module`) so the worker reuses the exact same scan code instead of a copy.
|
|
17
17
|
- `public/index.html` — Single-file frontend (vanilla JS/CSS/HTML, no build step). Report charts (`#speed-bars`, `#agent-bars`, `#session-bars`) render from `/api/report` data via `renderReportCharts`; `check-frontend-smoke.js` executes the page's real script against live API data with a stub DOM.
|
|
18
18
|
- `backups/` — Gzipped DB backups (auto-rotated, `OPENCODE_BACKUP_RETENTION`)
|
|
19
|
-
- `.attr-bundle.json` — Persisted attribution bundle (git/npm-ignored). Keyed by DB path+size+mtime, so pointing `OPENCODE_DATA` elsewhere never reuses it.
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
Runtime state lives OUTSIDE the package directory:
|
|
21
|
+
|
|
22
|
+
| State | Location |
|
|
23
|
+
|---|---|
|
|
24
|
+
| Attribution bundle cache | `~/.cache/opencode-statboard/attr-bundle-<sha1(db path)>.json` |
|
|
25
|
+
| DB backups | `<package>/backups/` (wiped by `npm i -g` — acceptable, they are re-creatable) |
|
|
26
|
+
|
|
27
|
+
One cache file per database path (sha1 of the lowercased path). Two servers pointed at different DBs used to overwrite each other's entry and never get a cache hit. The pre-2.8.4 in-package `.attr-bundle.json` is still read once and then migrated.
|
|
28
|
+
|
|
29
|
+
## Startup & latency (measured 2026-10-12 / 2026-10-16, live 3GB WAL DB)
|
|
22
30
|
|
|
23
31
|
The message table is ~1GB of JSON; scanning it with `json_extract` costs **43s**, fetching the raw `data` blob and calling `JSON.parse` once per row costs **~2.5s**. That scan is still synchronous `better-sqlite3`, so it runs in `attr-worker.js`:
|
|
24
32
|
|
|
25
33
|
| Stage | Before | After |
|
|
26
34
|
|---|---|---|
|
|
27
|
-
| Cold start to first data (cache present) | 28.5s | **0.
|
|
28
|
-
| Cold start, no cache (worker scan runs meanwhile) | 28.5s | 6
|
|
29
|
-
| Report request while a scan is in flight | blocked 3–
|
|
35
|
+
| Cold start to first data (cache present) | 28.5s | **0.56s** |
|
|
36
|
+
| Cold start, no cache (worker scan runs meanwhile) | 28.5s | 6–58s, UI responsive throughout, reports `loading` |
|
|
37
|
+
| Report request while a scan is in flight | blocked 3–58s | ~1ms |
|
|
30
38
|
| First switch to an unvisited scope | up to 6.3s | ~130ms, ~0ms after the frontend's idle prefetch |
|
|
31
39
|
|
|
32
40
|
Rules that keep it fast:
|
|
33
41
|
|
|
34
42
|
- **Nothing on the request path may run a full scan.** `toolCountsFor()` returns the worker/cached counts while a worker round is in flight; only if the worker is unavailable does it fall back to an inline scan.
|
|
35
|
-
- **The bundle cache
|
|
43
|
+
- **The bundle cache lives in the user cache dir, not the package dir.** `npm install -g` replaces the package directory, so a cache stored next to `server.js` was deleted on every upgrade and each upgrade paid a full 30–60s scan before showing anything.
|
|
44
|
+
- **The bundle cache is keyed on the DB PATH only** (in the filename). Keying it on size/mtime looked safer, but the live DB grows on every message, so the fingerprint changed constantly and the "instant" cold start silently degraded back to a full rebuild on a busy machine.
|
|
36
45
|
- **Reports refuse to build without a bundle** (`refreshReport` returns `ready:false`), otherwise an all-zero report sits in the 10-minute cache and blanks the dashboard.
|
|
46
|
+
- **With no bundle at all**, `refresh()` keeps `state.loading = true` and `/api/stats` reports `ok:false`, so the UI shows "updating" instead of an all-zero payload it cannot tell apart from real numbers.
|
|
37
47
|
- The bundle is persisted with its tool counts, so a cold start paints complete numbers before the first worker round lands. `settleBundle` repaints and prewarms the default scope report.
|
|
38
48
|
- The frontend `warmOtherScopes()` fetches the scopes the user is NOT looking at, on idle, so a scope click is an in-memory swap.
|
|
39
49
|
|
|
@@ -149,6 +159,7 @@ npm install -g opencode-statboard # after the Actions run is green
|
|
|
149
159
|
- `better-sqlite3` is a native addon; must match Node.js ABI
|
|
150
160
|
- `attributeUsage` does a full DB scan (~62k messages, ~1GB JSON) — always in the worker thread; the result is memoized for `ATTR_TTL` and persisted to `.attr-bundle.json`
|
|
151
161
|
- `check-frontend-smoke.js` compares the rendered DOM against the report for the scope the page is showing; it is scope-aware and pre-fetches every scope the page can visit, because a stub that answers every `/api/report` with one scope makes the "repainted for new scope" checks meaningless
|
|
162
|
+
- The smoke harness must not capture `/api/stats` before the payload is real: it polls until `ok && trendReady && input > 0 && tools.length`, because a server that just booted (or one mid-rebuild) answers with zeros and no trend, and every DOM assertion would then compare against an empty page
|
|
152
163
|
- Trend buckets are sliced from the same `getAttrAll()` attribution the report uses — never a separate windowed call (windowed calls inflate in-window days via residual growth; fixed 2026-09-29 after measuring +0.25–1.7M input/day). Remaining panel differences are cache ages only (trend ≤60s, report ≤10min, both timestamps shown)
|
|
153
164
|
- V2 tool counts use `json_each` over `session_message` content (~1.5s full scan), cached 5min via `toolsCache` (the 15s refresh serves cache; verification accepts api<=expected so staleness never fails checks); legacy V1 DBs scan `part` instead
|
|
154
165
|
- OpenCode V2 writes `cost = 0` for free/subscription providers, so $ totals only reflect paid (per-token billed) usage — same field, not a schema issue
|
package/attr-worker.js
CHANGED
|
@@ -18,6 +18,7 @@ const srv = require('./server.js');
|
|
|
18
18
|
parentPort.postMessage({
|
|
19
19
|
ok: true,
|
|
20
20
|
builtAt: now,
|
|
21
|
+
dbRevision: srv.dbRevision(),
|
|
21
22
|
json: srv.serializeBundle(bundle, { embedded: srv.getEmbeddedToolsFlag(), startMs: workerData.toolsStartMs || 0, counts }),
|
|
22
23
|
tools: { embedded: srv.getEmbeddedToolsFlag(), startMs: workerData.toolsStartMs || 0, counts },
|
|
23
24
|
});
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -5,6 +5,7 @@ const fs = require('fs');
|
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const zlib = require('zlib');
|
|
7
7
|
const { Worker } = require('worker_threads');
|
|
8
|
+
const crypto = require('crypto');
|
|
8
9
|
const { pipeline } = require('stream');
|
|
9
10
|
const { promisify } = require('util');
|
|
10
11
|
const pipelineAsync = promisify(pipeline);
|
|
@@ -583,7 +584,15 @@ async function buildAttrBundle(now) {
|
|
|
583
584
|
// Scanning the live DB costs 5–26s (WAL + concurrent opencode writes), so the
|
|
584
585
|
// computed bundle is persisted and loaded on startup; a stale bundle still
|
|
585
586
|
// serves every request while a fresh one is rebuilt in the background.
|
|
586
|
-
|
|
587
|
+
// The bundle cache is RUNTIME state, not package content: `npm install -g`
|
|
588
|
+
// replaces the whole package directory, which used to delete the cache and
|
|
589
|
+
// force every upgrade to pay a full 30-60s scan before showing anything.
|
|
590
|
+
// One file per database path: two servers pointed at different DBs (tests,
|
|
591
|
+
// snapshots) would otherwise keep overwriting each other's entry and never
|
|
592
|
+
// get a cache hit.
|
|
593
|
+
const BUNDLE_DIR = path.join(process.env.USERPROFILE || process.env.HOME || process.cwd(), '.cache', 'opencode-statboard');
|
|
594
|
+
const BUNDLE_FILE = path.join(BUNDLE_DIR, `attr-bundle-${crypto.createHash('sha1').update(BACKUP_SRC.toLowerCase()).digest('hex').slice(0, 12)}.json`);
|
|
595
|
+
const BUNDLE_FILE_LEGACY = path.join(__dirname, '.attr-bundle.json');
|
|
587
596
|
const BUNDLE_SAVE_MS = 60000; // don't hammer the disk while chatting
|
|
588
597
|
// Cache is bound to the DATABASE PATH it was computed from, so pointing
|
|
589
598
|
// OPENCODE_DATA at another DB (tests, a restored copy) never reuses it.
|
|
@@ -593,6 +602,16 @@ const BUNDLE_SAVE_MS = 60000; // don't hammer the disk while chatting
|
|
|
593
602
|
function dbFingerprint() {
|
|
594
603
|
return BACKUP_SRC;
|
|
595
604
|
}
|
|
605
|
+
// Cheap revision check for the REBUILD decision (distinct from the cache
|
|
606
|
+
// fingerprint above, which is intentionally path-only). Two servers pointed at
|
|
607
|
+
// the same DB see the same revision, so a DB that has not changed since the
|
|
608
|
+
// last scan is never scanned again.
|
|
609
|
+
function dbRevision() {
|
|
610
|
+
try {
|
|
611
|
+
const st = fs.statSync(BACKUP_SRC);
|
|
612
|
+
return `${BACKUP_SRC}|${st.size}|${Math.round(st.mtimeMs)}`;
|
|
613
|
+
} catch (_) { return BACKUP_SRC; }
|
|
614
|
+
}
|
|
596
615
|
function serializeBundle(a, tools) {
|
|
597
616
|
// MUST NOT mutate: the caller serializes the LIVE bundle, and swapping a
|
|
598
617
|
// bucket's `sessions` Set for an array in place would break every later
|
|
@@ -614,7 +633,7 @@ function serializeBundle(a, tools) {
|
|
|
614
633
|
perAgentPeriod: setToArr(sl.perAgentPeriod),
|
|
615
634
|
};
|
|
616
635
|
return JSON.stringify({
|
|
617
|
-
v: 2, builtAt: a.builtAt, db: dbFingerprint(), byPeriodStart: a.byPeriodStart,
|
|
636
|
+
v: 2, builtAt: a.builtAt, db: dbFingerprint(), dbRevision: dbRevision(), byPeriodStart: a.byPeriodStart,
|
|
618
637
|
tools: tools ? { embedded: tools.embedded, startMs: tools.startMs, counts: tools.counts } : null,
|
|
619
638
|
perDay: days,
|
|
620
639
|
perModel: setToArr(a.perModel), perAgent: setToArr(a.perAgent),
|
|
@@ -653,22 +672,35 @@ function reviveBundle(o) {
|
|
|
653
672
|
return a;
|
|
654
673
|
}
|
|
655
674
|
function saveBundleCache(a, tools) {
|
|
656
|
-
try {
|
|
675
|
+
try {
|
|
676
|
+
fs.mkdirSync(BUNDLE_DIR, { recursive: true });
|
|
677
|
+
fs.writeFileSync(BUNDLE_FILE, serializeBundle(a, tools));
|
|
678
|
+
} catch (_) {}
|
|
657
679
|
}
|
|
658
680
|
function loadBundleCache() {
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
681
|
+
for (const file of [BUNDLE_FILE, BUNDLE_FILE_LEGACY]) {
|
|
682
|
+
try {
|
|
683
|
+
const raw = fs.readFileSync(file, 'utf8');
|
|
684
|
+
const o = JSON.parse(raw);
|
|
685
|
+
if (o && o.v === 2 && o.db === dbFingerprint() && o.periods && Object.keys(o.periods).length) {
|
|
686
|
+
console.log(`attr bundle loaded from cache (built ${Math.round((Date.now() - o.builtAt) / 60000)} min ago)`);
|
|
687
|
+
// Tool counts ride along so a cold start paints complete numbers before
|
|
688
|
+
// the first worker round lands.
|
|
689
|
+
if (o.tools && o.tools.counts) {
|
|
690
|
+
toolsCache = { at: o.builtAt, embedded: o.tools.embedded, startMs: 0, counts: o.tools.counts, fromWorker: true };
|
|
691
|
+
}
|
|
692
|
+
// Migrate the pre-2.8.4 in-package location on first load.
|
|
693
|
+
if (file !== BUNDLE_FILE) {
|
|
694
|
+
try {
|
|
695
|
+
fs.mkdirSync(BUNDLE_DIR, { recursive: true });
|
|
696
|
+
fs.writeFileSync(BUNDLE_FILE, raw);
|
|
697
|
+
console.log('attr bundle cache migrated to ' + BUNDLE_DIR);
|
|
698
|
+
} catch (_) {}
|
|
699
|
+
}
|
|
700
|
+
return { at: o.builtAt, a: reviveBundle(o) };
|
|
668
701
|
}
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
} catch (_) {}
|
|
702
|
+
} catch (_) {}
|
|
703
|
+
}
|
|
672
704
|
return null;
|
|
673
705
|
}
|
|
674
706
|
function getAttrAll() {
|
|
@@ -689,9 +721,17 @@ function refreshAttrBundle() {
|
|
|
689
721
|
if (attrBundlePending) return attrAllCache ? attrAllCache.a : null;
|
|
690
722
|
const now = Date.now();
|
|
691
723
|
if (attrAllCache && now - attrAllCache.at < ATTR_TTL) return attrAllCache.a;
|
|
724
|
+
const rev = dbRevision();
|
|
725
|
+
// DB hasn't changed since the last scan: just extend the TTL, no scan.
|
|
726
|
+
// This is the common case on a machine where opencode is idle (which is most
|
|
727
|
+
// of the time), and it turns a 30-60s rebuild into a stat() call.
|
|
728
|
+
if (attrAllCache && rev && attrAllCache.dbRevision === rev) {
|
|
729
|
+
attrAllCache.at = now;
|
|
730
|
+
return attrAllCache.a;
|
|
731
|
+
}
|
|
692
732
|
attrBundlePending = true;
|
|
693
733
|
attrWorkerActive = true;
|
|
694
|
-
spawnAttrWorker(now);
|
|
734
|
+
spawnAttrWorker(now, rev);
|
|
695
735
|
return attrAllCache ? attrAllCache.a : null;
|
|
696
736
|
}
|
|
697
737
|
|
|
@@ -706,7 +746,7 @@ function waitForBundle(timeoutMs) {
|
|
|
706
746
|
});
|
|
707
747
|
}
|
|
708
748
|
|
|
709
|
-
function spawnAttrWorker(now) {
|
|
749
|
+
function spawnAttrWorker(now, rev) {
|
|
710
750
|
// Failure path: unblock waiters and, on a truly cold start, build inline so
|
|
711
751
|
// the dashboard still shows numbers.
|
|
712
752
|
function failBundle(msg) {
|
|
@@ -726,7 +766,7 @@ function spawnAttrWorker(now) {
|
|
|
726
766
|
}
|
|
727
767
|
let w;
|
|
728
768
|
try {
|
|
729
|
-
w = new Worker(ATTR_WORKER, { workerData: { now, toolsStartMs: 0 } });
|
|
769
|
+
w = new Worker(ATTR_WORKER, { workerData: { now, toolsStartMs: 0, dbRevision: rev } });
|
|
730
770
|
} catch (e) {
|
|
731
771
|
// Worker unavailable (very old Node / restricted env): build inline.
|
|
732
772
|
console.error('attr worker spawn failed, building inline:', e.message);
|
|
@@ -742,7 +782,8 @@ function spawnAttrWorker(now) {
|
|
|
742
782
|
try {
|
|
743
783
|
const a = reviveBundle(JSON.parse(m.json));
|
|
744
784
|
a.builtAt = m.builtAt;
|
|
745
|
-
|
|
785
|
+
a.dbRevision = m.dbRevision || rev;
|
|
786
|
+
attrAllCache = { at: m.builtAt, a, dbRevision: a.dbRevision };
|
|
746
787
|
if (m.tools) {
|
|
747
788
|
toolsCache = { at: m.builtAt, embedded: m.tools.embedded, startMs: 0, counts: m.tools.counts, fromWorker: true };
|
|
748
789
|
}
|
|
@@ -932,19 +973,29 @@ async function refresh() {
|
|
|
932
973
|
if (state.busy) return;
|
|
933
974
|
state.busy = true;
|
|
934
975
|
try {
|
|
976
|
+
// No bundle yet (first run on this machine, or right after `npm i -g`
|
|
977
|
+
// wiped the package dir): the worker is still scanning — which can take
|
|
978
|
+
// 30-60s on a 3GB DB. Publish "loading" instead of an all-zero payload
|
|
979
|
+
// the UI cannot tell apart from real numbers.
|
|
980
|
+
if (!attrAllCache) {
|
|
981
|
+
refreshAttrBundle();
|
|
982
|
+
state.loading = true;
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
935
985
|
const p = getStatsFromDb(0);
|
|
936
986
|
if (p && p.ok) {
|
|
937
987
|
state.data = p;
|
|
938
988
|
state.error = null;
|
|
939
989
|
state.updatedAt = Date.now();
|
|
990
|
+
state.loading = false;
|
|
940
991
|
} else {
|
|
941
992
|
state.error = state.error || 'failed to read database';
|
|
942
993
|
}
|
|
943
994
|
} catch (e) {
|
|
944
995
|
state.error = e.message;
|
|
996
|
+
} finally {
|
|
997
|
+
state.busy = false;
|
|
945
998
|
}
|
|
946
|
-
state.loading = false;
|
|
947
|
-
state.busy = false;
|
|
948
999
|
}
|
|
949
1000
|
|
|
950
1001
|
function getModelsFromDb() {
|
|
@@ -1416,6 +1467,7 @@ if (require.main !== module) {
|
|
|
1416
1467
|
serializeBundle,
|
|
1417
1468
|
reviveBundle,
|
|
1418
1469
|
scanToolCounts,
|
|
1470
|
+
dbRevision,
|
|
1419
1471
|
getEmbeddedToolsFlag: () => schema.embeddedTools,
|
|
1420
1472
|
};
|
|
1421
1473
|
return;
|