opencode-statboard 2.9.0 → 2.9.2

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/attr-worker.js CHANGED
@@ -1,16 +1,10 @@
1
1
  const { parentPort, workerData } = require('worker_threads');
2
- const fs = require('fs');
3
2
  const srv = require('./server.js');
4
- const dbg = (s) => fs.appendFileSync('C:\\Users\\CPXru\\Desktop\\thumb\\大拇哥實驗室\\Projects\\oc-stats\\worker-debug.txt', s + '\n');
5
- dbg('WORKER STARTED; BACKUP_SRC=' + (srv.BACKUP_SRC || 'NOT EXPORTED') + ' env.OPENCODE_DATA=' + (process.env.OPENCODE_DATA || '(unset)'));
6
3
 
7
4
  (async () => {
8
- dbg('calling buildAttrBundle...');
9
5
  const bundle = await srv.buildAttrBundle(workerData.now || Date.now());
10
- dbg('bundle done: messages=' + bundle.totals.messages + ' input=' + bundle.totals.input);
11
6
  bundle.builtAt = workerData.now || Date.now();
12
7
  const counts = srv.scanToolCounts(workerData.toolsStartMs || 0);
13
- dbg('tools done: ' + Object.keys(counts).length);
14
8
  parentPort.postMessage({
15
9
  ok: true,
16
10
  builtAt: bundle.builtAt,
@@ -18,8 +12,6 @@ dbg('WORKER STARTED; BACKUP_SRC=' + (srv.BACKUP_SRC || 'NOT EXPORTED') + ' env.O
18
12
  json: srv.serializeBundle(bundle, { embedded: srv.getEmbeddedToolsFlag(), startMs: workerData.toolsStartMs || 0, counts }),
19
13
  tools: { embedded: srv.getEmbeddedToolsFlag(), startMs: workerData.toolsStartMs || 0, counts },
20
14
  });
21
- dbg('posted');
22
15
  })().catch((e) => {
23
- dbg('CATCH: ' + (e && e.stack ? e.stack : e));
24
16
  parentPort.postMessage({ ok: false, error: (e && e.message) || String(e) });
25
17
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.9.0",
3
+ "version": "2.9.2",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
package/server.js CHANGED
@@ -20,9 +20,13 @@ const TREND_MS = 60 * 1000;
20
20
  const REPORT_TTL = 10 * 60 * 1000;
21
21
  const TOOLS_TTL = 5 * 60 * 1000;
22
22
  const STATS_TTL = 10 * 60 * 1000;
23
- const CACHE_CLEANUP_MS = 5 * 60 * 1000;
23
+ // Backup interval: default 1 hour (was 5 min — far too aggressive for 3GB DB).
24
+ // Configurable via OPENCODE_BACKUP_INTERVAL_MS env var.
25
+ const CACHE_CLEANUP_MS = parseInt(process.env.OPENCODE_BACKUP_INTERVAL_MS || String(60 * 60 * 1000), 10);
24
26
  const BACKUP_DIR = path.join(__dirname, 'backups');
25
27
  const BACKUP_RETENTION = parseInt(process.env.OPENCODE_BACKUP_RETENTION || '3', 10) || 3;
28
+ // Set OPENCODE_BACKUP_INTERVAL_MS=0 to disable auto backup entirely
29
+ const BACKUP_DISABLED = process.env.OPENCODE_BACKUP_INTERVAL_MS === '0';
26
30
  const BACKUP_SRC = process.env.OPENCODE_DATA
27
31
  ? path.join(process.env.OPENCODE_DATA, 'opencode.db')
28
32
  : path.join(process.env.USERPROFILE || process.env.HOME || '', '.local', 'share', 'opencode', 'opencode.db');
@@ -336,6 +340,11 @@ function marketOf(tok, provider, model) {
336
340
  // message count, output, reasoning, active ms, wall span on the MESSAGE clock)
337
341
  // and msgAgentAgg, so reports need no extra queries.
338
342
  const ATTR_TTL = 300000;
343
+ // Minimum interval between full scans, regardless of DB revision changes.
344
+ // Prevents excessive scanning when opencode is actively writing (WAL checkpoints
345
+ // change DB mtime/size constantly, making revision appear changed every time).
346
+ // Default 15 minutes — configurable via OPENCODE_MIN_SCAN_INTERVAL_MS.
347
+ const MIN_SCAN_INTERVAL = parseInt(process.env.OPENCODE_MIN_SCAN_INTERVAL_MS || String(15 * 60 * 1000), 10);
339
348
  // Tool counts change slowly but the embedded-content scan costs ~2.8s on a
340
349
  // 2.7GB live DB, so it is computed by the stats WORKER (see spawnAttrWorker)
341
350
  // and only ever READ here. `fromWorker` marks worker-supplied data, which is
@@ -562,6 +571,9 @@ async function attributeUsage(startMs, endMs, periodStartsMs) {
562
571
  let attrAllCache = null;
563
572
  let attrBundlePending = false;
564
573
  let attrLastSave = 0;
574
+ let attrLastScan = 0; // timestamp of last scan start
575
+ // Set OPENCODE_ATTR_SCAN_DISABLED=1 to disable background attr scans entirely
576
+ const ATTR_SCAN_DISABLED = process.env.OPENCODE_ATTR_SCAN_DISABLED === '1';
565
577
  const EMPTY_ATTR = {
566
578
  perDay: {}, perModel: {}, perAgent: {}, perSession: {},
567
579
  totals: { sessions: 0, messages: 0, input: 0, output: 0, reasoning: 0, cacheRead: 0, cacheWrite: 0, cost: 0 },
@@ -722,16 +734,21 @@ function refreshAttrBundle() {
722
734
  if (attrBundlePending) return attrAllCache ? attrAllCache.a : null;
723
735
  const now = Date.now();
724
736
  if (attrAllCache && now - attrAllCache.at < ATTR_TTL) return attrAllCache.a;
737
+ // Enforce minimum scan interval to prevent excessive scanning when DB
738
+ // revision changes frequently (e.g., due to WAL checkpoints during active
739
+ // opencode usage). This is independent of TTL and revision checks.
740
+ if (attrLastScan && now - attrLastScan < MIN_SCAN_INTERVAL) {
741
+ return attrAllCache ? attrAllCache.a : null;
742
+ }
725
743
  const rev = dbRevision();
726
744
  // DB hasn't changed since the last scan: just extend the TTL, no scan.
727
- // This is the common case on a machine where opencode is idle (which is most
728
- // of the time), and it turns a 30-60s rebuild into a stat() call.
729
745
  if (attrAllCache && rev && attrAllCache.dbRevision === rev) {
730
746
  attrAllCache.at = now;
731
747
  return attrAllCache.a;
732
748
  }
733
749
  attrBundlePending = true;
734
750
  attrWorkerActive = true;
751
+ attrLastScan = now;
735
752
  spawnAttrWorker(now, rev);
736
753
  return attrAllCache ? attrAllCache.a : null;
737
754
  }
package/worker-debug.txt DELETED
@@ -1,20 +0,0 @@
1
- WORKER STARTED; BACKUP_SRC=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v28\opencode.db env.OPENCODE_DATA=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v28
2
- calling buildAttrBundle...
3
- bundle done: messages=0 input=0
4
- tools done: 0
5
- posted
6
- WORKER STARTED; BACKUP_SRC=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v28\opencode.db env.OPENCODE_DATA=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v28
7
- calling buildAttrBundle...
8
- bundle done: messages=0 input=0
9
- tools done: 0
10
- posted
11
- WORKER STARTED; BACKUP_SRC=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v28\opencode.db env.OPENCODE_DATA=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v28
12
- calling buildAttrBundle...
13
- bundle done: messages=0 input=0
14
- tools done: 0
15
- posted
16
- WORKER STARTED; BACKUP_SRC=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v30\opencode.db env.OPENCODE_DATA=C:\Users\CPXru\AppData\Local\Temp\opencode\snap-v30
17
- calling buildAttrBundle...
18
- bundle done: messages=98508 input=1041461624.0000005
19
- tools done: 52
20
- posted