shadok-ai 0.17.79 → 0.18.1

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
@@ -192,12 +192,16 @@ Enter to skip; you can add it later from the web UI).
192
192
  API, so nothing is billed per token; the figure is what those tokens would cost
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
- 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. 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.
195
+ restarts) and when it **last respawned** (the running process's start). The
196
+ figures are the **real** per-message counts the API reported (only the *cost* is
197
+ notional), and they include the agent's **subagents** — the Task-tool children
198
+ write their own transcripts, so a delegating agent's true total is mostly there;
199
+ it is attributed back to the parent. Scope is each agent's current session (not
200
+ earlier resume files). The header **⋯** menu has the **instance-wide total** —
201
+ the same windows summed over every agent you can see (a member → their groups,
202
+ admin → all). Live agents are summed from memory; a dormant agent's transcript
203
+ (and its subagents') is read once and cached by modification time, so neither
204
+ opening a panel nor the fleet total stalls the cockpit.
201
205
  - **Agents that report back** — an agent that launches other agents is told when
202
206
  each one finishes, blocks on a question, or dies. It hears about **its own**
203
207
  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, scanUsageSamplesAsync, sessionFilePath, tailSession, clearTailPos, seedTailPos, isNothingToShow, newestTranscriptById } from "./tail.js";
47
+ import { scanUsageSamples, scanUsageSamplesAsync, subagentDirFor, 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";
@@ -2519,37 +2519,44 @@ app.get("/channels/glance", async (req, res) => {
2519
2519
  // agent is loaded; otherwise scanned once from its transcript — scope stays the
2520
2520
  // CURRENT session file either way, which is why it cannot drag (no cross-session
2521
2521
  // sweep). Same per-session ACL as /crons.
2522
- app.get("/channels/stats", (req, res) => {
2522
+ app.get("/channels/stats", async (req, res) => {
2523
2523
  const id = String(req.query.id ?? "");
2524
2524
  if (!id)
2525
2525
  return res.status(400).json({ error: "id required" });
2526
2526
  if (!mayActFor(req, id))
2527
2527
  return res.status(403).json({ error: "forbidden" });
2528
2528
  const live = sessions.get(id);
2529
- let samples;
2529
+ let mainSamples;
2530
2530
  let upSince;
2531
2531
  let model;
2532
2532
  let lastRespawn;
2533
+ let mainFile;
2533
2534
  if (live) {
2534
- samples = live.usageSamples.values();
2535
+ mainSamples = [...live.usageSamples.values()];
2535
2536
  upSince = live.firstMessageAt;
2536
2537
  model = live.model ?? "";
2537
2538
  // A tmux pane's own creation time survives a server restart (true process
2538
2539
  // age); node-pty dies with the server, so the Live's build time is right.
2539
2540
  lastRespawn =
2540
2541
  live.pilot instanceof TmuxPilot ? (tmuxSessionCreated("sk-" + id) ?? live.spawnedAt) : live.spawnedAt;
2542
+ mainFile = sessionFilePath(live.cwd, id, live.transcriptHome);
2541
2543
  }
2542
2544
  else {
2543
2545
  const ch = loadChannels().find((c) => c.sessionId === id);
2544
2546
  if (!ch)
2545
2547
  return res.status(404).json({ error: "unknown session" });
2546
- const scan = scanUsageSamples(sessionFilePath(ch.cwd, id, channelStateHome(ch)));
2547
- samples = scan.samples.values();
2548
+ mainFile = sessionFilePath(ch.cwd, id, channelStateHome(ch));
2549
+ const scan = await scanUsageSamplesAsync(mainFile);
2550
+ mainSamples = [...scan.samples.values()];
2548
2551
  upSince = scan.firstAt;
2549
2552
  model = ch.model ?? "";
2550
2553
  lastRespawn = null; // not running — there is no live process to date
2551
2554
  }
2552
- const windows = windowStats(samples, Date.now());
2555
+ // A subagent (Task tool) writes its own transcript with its own usage, never
2556
+ // folded into the parent's — so add it here, attributed to this agent, or a
2557
+ // delegating agent reads far cheaper than it was.
2558
+ const subs = await subagentSamples(mainFile);
2559
+ const windows = windowStats([...mainSamples, ...subs], Date.now());
2553
2560
  res.json({ windows, upSince, lastRespawn, model });
2554
2561
  });
2555
2562
  // The instance-wide total, for the header ⋯ menu's "Instance stats". A dormant
@@ -2562,11 +2569,11 @@ const statsScanCache = new Map();
2562
2569
  // with every agent that ever lived on the instance. Insertion order gives a
2563
2570
  // plain FIFO eviction; an evicted file is simply re-scanned next time.
2564
2571
  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));
2572
+ // A transcript's samples, async and cached by (mtime,size): a file that has not
2573
+ // changed since last read is never re-parsed, only stat()ed. Used for dormant
2574
+ // main transcripts AND subagent ones (a finished subagent's file is static, so
2575
+ // it is read once ever).
2576
+ async function cachedFileSamples(file) {
2570
2577
  let st;
2571
2578
  try {
2572
2579
  st = await fs.promises.stat(file);
@@ -2576,7 +2583,7 @@ async function channelStatsSamples(ch) {
2576
2583
  }
2577
2584
  const cached = statsScanCache.get(file);
2578
2585
  if (cached && cached.mtime === st.mtimeMs && cached.size === st.size)
2579
- return cached.samples.values();
2586
+ return [...cached.samples.values()];
2580
2587
  const { samples } = await scanUsageSamplesAsync(file);
2581
2588
  statsScanCache.delete(file); // re-insert, so a refreshed file is the newest entry
2582
2589
  if (statsScanCache.size >= STATS_CACHE_MAX) {
@@ -2585,7 +2592,37 @@ async function channelStatsSamples(ch) {
2585
2592
  statsScanCache.delete(oldest.value);
2586
2593
  }
2587
2594
  statsScanCache.set(file, { mtime: st.mtimeMs, size: st.size, samples });
2588
- return samples.values();
2595
+ return [...samples.values()];
2596
+ }
2597
+ // Every subagent transcript's samples, merged — the Task-tool children whose
2598
+ // usage is theirs alone. Missing dir (no subagents) → none. One file at a time
2599
+ // so a deep tree still yields the event loop.
2600
+ async function subagentSamples(mainFile) {
2601
+ const dir = subagentDirFor(mainFile);
2602
+ let names;
2603
+ try {
2604
+ names = await fs.promises.readdir(dir);
2605
+ }
2606
+ catch {
2607
+ return [];
2608
+ }
2609
+ const out = [];
2610
+ for (const name of names) {
2611
+ if (!name.endsWith(".jsonl"))
2612
+ continue;
2613
+ out.push(...(await cachedFileSamples(path.join(dir, name))));
2614
+ }
2615
+ return out;
2616
+ }
2617
+ async function channelStatsSamples(ch) {
2618
+ const live = sessions.get(ch.sessionId);
2619
+ const home = live ? live.transcriptHome : channelStateHome(ch);
2620
+ const cwd = live ? live.cwd : ch.cwd;
2621
+ const mainFile = sessionFilePath(cwd, ch.sessionId, home);
2622
+ // A live agent's MAIN usage is in memory (no I/O); its subagents are on disk.
2623
+ const main = live ? [...live.usageSamples.values()] : await cachedFileSamples(mainFile);
2624
+ const subs = await subagentSamples(mainFile);
2625
+ return [...main, ...subs];
2589
2626
  }
2590
2627
  // Sum of every agent the caller may SEE (a member → their groups; admin → all) —
2591
2628
  // so the total means the same thing per viewer. Reads happen async and one agent