shadok-ai 0.17.78 → 0.18.0

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/README.md CHANGED
@@ -193,7 +193,11 @@ Enter to skip; you can add it later from the web UI).
193
193
  at Anthropic API list prices, a way to compare agents rather than an invoice. It
194
194
  also shows how long the agent has been **up** (session age, which survives
195
195
  restarts) and when it **last respawned** (the running process's start). Scope is
196
- the current session's transcript, read once, so opening it never drags.
196
+ the current session's transcript, read once, so opening it never drags. The
197
+ header **⋯** menu has the **instance-wide total** — the same windows summed over
198
+ every agent you can see (a member → their groups, admin → all). Running agents
199
+ are summed from memory and a dormant agent's transcript is read once and cached,
200
+ so the fleet total doesn't stall the cockpit either.
197
201
  - **Agents that report back** — an agent that launches other agents is told when
198
202
  each one finishes, blocks on a question, or dies. It hears about **its own**
199
203
  children and nothing else, so a busy channel never wakes it. What arrives is
package/dist/server.js CHANGED
@@ -44,7 +44,7 @@ import { openBrowser } from "./open-browser.js";
44
44
  import { parseSkillMeta, prepromptParts, prepromptFingerprint, stalenessReasons, LEGACY_FINGERPRINT, } from "./preprompt.js";
45
45
  import { ensureSpawnHelperExecutable } from "./node-pty-fix.js";
46
46
  import { TmuxPilot, tmuxAvailable, tmuxHasSession, tmuxKillSession, tmuxPaneCwd, tmuxSessionCreated } from "./tmux.js";
47
- import { scanUsageSamples, sessionFilePath, tailSession, clearTailPos, seedTailPos, isNothingToShow, newestTranscriptById } from "./tail.js";
47
+ import { scanUsageSamples, scanUsageSamplesAsync, sessionFilePath, tailSession, clearTailPos, seedTailPos, isNothingToShow, newestTranscriptById } from "./tail.js";
48
48
  import { windowStats } from "./token-stats.js";
49
49
  import { strandedWorktreePlan } from "./worktree-relocate.js";
50
50
  import { detectFork, rootIdOfFile } from "./forktrace.js";
@@ -2552,6 +2552,55 @@ app.get("/channels/stats", (req, res) => {
2552
2552
  const windows = windowStats(samples, Date.now());
2553
2553
  res.json({ windows, upSince, lastRespawn, model });
2554
2554
  });
2555
+ // The instance-wide total, for the header ⋯ menu's "Instance stats". A dormant
2556
+ // agent's scan is cached by (mtime,size), so re-opening only stat()s unchanged
2557
+ // files; a LIVE agent is read from memory. Invalidated when the transcript grows.
2558
+ const statsScanCache = new Map();
2559
+ // Bounded on purpose: the entries hold one object PER MESSAGE of a transcript,
2560
+ // the server runs for weeks, and nothing ever removed a file that stopped
2561
+ // existing (a closed agent, a pruned worktree) — so an unbounded map here grows
2562
+ // with every agent that ever lived on the instance. Insertion order gives a
2563
+ // plain FIFO eviction; an evicted file is simply re-scanned next time.
2564
+ const STATS_CACHE_MAX = 64;
2565
+ async function channelStatsSamples(ch) {
2566
+ const live = sessions.get(ch.sessionId);
2567
+ if (live)
2568
+ return live.usageSamples.values(); // in memory — no I/O
2569
+ const file = sessionFilePath(ch.cwd, ch.sessionId, channelStateHome(ch));
2570
+ let st;
2571
+ try {
2572
+ st = await fs.promises.stat(file);
2573
+ }
2574
+ catch {
2575
+ return [];
2576
+ }
2577
+ const cached = statsScanCache.get(file);
2578
+ if (cached && cached.mtime === st.mtimeMs && cached.size === st.size)
2579
+ return cached.samples.values();
2580
+ const { samples } = await scanUsageSamplesAsync(file);
2581
+ statsScanCache.delete(file); // re-insert, so a refreshed file is the newest entry
2582
+ if (statsScanCache.size >= STATS_CACHE_MAX) {
2583
+ const oldest = statsScanCache.keys().next();
2584
+ if (!oldest.done)
2585
+ statsScanCache.delete(oldest.value);
2586
+ }
2587
+ statsScanCache.set(file, { mtime: st.mtimeMs, size: st.size, samples });
2588
+ return samples.values();
2589
+ }
2590
+ // Sum of every agent the caller may SEE (a member → their groups; admin → all) —
2591
+ // so the total means the same thing per viewer. Reads happen async and one agent
2592
+ // at a time, so the single-threaded server stays responsive even on a big fleet;
2593
+ // the per-file cache makes a repeat open cheap.
2594
+ app.get("/instance/stats", async (req, res) => {
2595
+ const box = restrictedBoxOf(req);
2596
+ const visible = visibleChannels(loadChannels(), loadGroups(), viewerFor(currentAccount(req))).filter((ch) => !box || ownedByPeer(ch, box));
2597
+ const all = [];
2598
+ for (const ch of visible) {
2599
+ for (const s of await channelStatsSamples(ch))
2600
+ all.push(s);
2601
+ }
2602
+ res.json({ windows: windowStats(all, Date.now()), agents: visible.length });
2603
+ });
2555
2604
  // ── Crons (scheduled channel prompts) ───────────────────────────────────────
2556
2605
  // Scoped like every other per-session read (invariant 60): a cron carries its
2557
2606
  // prompt and its guard command, i.e. another group's monitoring logic.