@songsid/agend 2.1.2-beta.4 → 2.1.2-beta.40

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.
Files changed (166) hide show
  1. package/dist/access-path.js +15 -6
  2. package/dist/access-path.js.map +1 -1
  3. package/dist/agent-cli-instructions.md +1 -0
  4. package/dist/agent-cli.js +14 -0
  5. package/dist/agent-cli.js.map +1 -1
  6. package/dist/agent-endpoint.d.ts +2 -0
  7. package/dist/agent-endpoint.js +25 -1
  8. package/dist/agent-endpoint.js.map +1 -1
  9. package/dist/backend/antigravity.d.ts +20 -0
  10. package/dist/backend/antigravity.js +51 -4
  11. package/dist/backend/antigravity.js.map +1 -1
  12. package/dist/backend/claude-code.d.ts +59 -0
  13. package/dist/backend/claude-code.js +67 -1
  14. package/dist/backend/claude-code.js.map +1 -1
  15. package/dist/backend/codex.d.ts +27 -0
  16. package/dist/backend/codex.js +93 -0
  17. package/dist/backend/codex.js.map +1 -1
  18. package/dist/backend/grok.d.ts +25 -0
  19. package/dist/backend/grok.js +51 -0
  20. package/dist/backend/grok.js.map +1 -1
  21. package/dist/backend/kiro.d.ts +22 -0
  22. package/dist/backend/kiro.js +79 -1
  23. package/dist/backend/kiro.js.map +1 -1
  24. package/dist/backend/types.d.ts +58 -0
  25. package/dist/backend/types.js +13 -1
  26. package/dist/backend/types.js.map +1 -1
  27. package/dist/channel/adapters/discord.d.ts +15 -0
  28. package/dist/channel/adapters/discord.js +100 -2
  29. package/dist/channel/adapters/discord.js.map +1 -1
  30. package/dist/channel/adapters/telegram.d.ts +21 -0
  31. package/dist/channel/adapters/telegram.js +86 -0
  32. package/dist/channel/adapters/telegram.js.map +1 -1
  33. package/dist/channel/ipc-bridge.d.ts +9 -1
  34. package/dist/channel/ipc-bridge.js +12 -3
  35. package/dist/channel/ipc-bridge.js.map +1 -1
  36. package/dist/channel/ipc-timeouts.d.ts +46 -0
  37. package/dist/channel/ipc-timeouts.js +65 -0
  38. package/dist/channel/ipc-timeouts.js.map +1 -0
  39. package/dist/channel/mcp-server.js +30 -14
  40. package/dist/channel/mcp-server.js.map +1 -1
  41. package/dist/channel/mcp-tools.js +30 -1
  42. package/dist/channel/mcp-tools.js.map +1 -1
  43. package/dist/channel/reconnect-backoff.d.ts +17 -0
  44. package/dist/channel/reconnect-backoff.js +21 -0
  45. package/dist/channel/reconnect-backoff.js.map +1 -0
  46. package/dist/channel/types.d.ts +29 -0
  47. package/dist/classic-channel-manager.js +1 -5
  48. package/dist/classic-channel-manager.js.map +1 -1
  49. package/dist/cli.js +204 -82
  50. package/dist/cli.js.map +1 -1
  51. package/dist/completion.d.ts +27 -0
  52. package/dist/completion.js +121 -0
  53. package/dist/completion.js.map +1 -0
  54. package/dist/config-validator.js +21 -0
  55. package/dist/config-validator.js.map +1 -1
  56. package/dist/config.js +1 -0
  57. package/dist/config.js.map +1 -1
  58. package/dist/cost-guard.d.ts +3 -1
  59. package/dist/cost-guard.js +3 -1
  60. package/dist/cost-guard.js.map +1 -1
  61. package/dist/daemon.d.ts +260 -14
  62. package/dist/daemon.js +991 -309
  63. package/dist/daemon.js.map +1 -1
  64. package/dist/event-log.d.ts +31 -0
  65. package/dist/event-log.js +96 -0
  66. package/dist/event-log.js.map +1 -1
  67. package/dist/fleet-context.d.ts +9 -0
  68. package/dist/fleet-lock.d.ts +28 -0
  69. package/dist/fleet-lock.js +130 -0
  70. package/dist/fleet-lock.js.map +1 -0
  71. package/dist/fleet-manager.d.ts +315 -2
  72. package/dist/fleet-manager.js +1410 -108
  73. package/dist/fleet-manager.js.map +1 -1
  74. package/dist/general-knowledge/skills/cross-instance-messaging/SKILL.md +22 -0
  75. package/dist/general-knowledge/skills/model-discovery/SKILL.md +14 -23
  76. package/dist/general-knowledge/skills/session-management/SKILL.md +15 -34
  77. package/dist/hang-detector.d.ts +19 -13
  78. package/dist/hang-detector.js +19 -49
  79. package/dist/hang-detector.js.map +1 -1
  80. package/dist/instance-lifecycle.d.ts +44 -3
  81. package/dist/instance-lifecycle.js +165 -48
  82. package/dist/instance-lifecycle.js.map +1 -1
  83. package/dist/instructions.d.ts +5 -0
  84. package/dist/instructions.js +9 -11
  85. package/dist/instructions.js.map +1 -1
  86. package/dist/locale.js +5 -1
  87. package/dist/locale.js.map +1 -1
  88. package/dist/logger.js +14 -0
  89. package/dist/logger.js.map +1 -1
  90. package/dist/mcp-liveness.d.ts +21 -0
  91. package/dist/mcp-liveness.js +27 -0
  92. package/dist/mcp-liveness.js.map +1 -0
  93. package/dist/outbound-handlers.d.ts +16 -0
  94. package/dist/outbound-handlers.js +151 -27
  95. package/dist/outbound-handlers.js.map +1 -1
  96. package/dist/outbound-schemas.d.ts +11 -3
  97. package/dist/outbound-schemas.js +16 -2
  98. package/dist/outbound-schemas.js.map +1 -1
  99. package/dist/pane-write-lock.d.ts +48 -0
  100. package/dist/pane-write-lock.js +73 -0
  101. package/dist/pane-write-lock.js.map +1 -0
  102. package/dist/process-memory.d.ts +31 -0
  103. package/dist/process-memory.js +79 -0
  104. package/dist/process-memory.js.map +1 -0
  105. package/dist/quickstart.js +17 -16
  106. package/dist/quickstart.js.map +1 -1
  107. package/dist/reply-dedup.d.ts +41 -0
  108. package/dist/reply-dedup.js +0 -0
  109. package/dist/reply-dedup.js.map +1 -0
  110. package/dist/scheduler/db.js +3 -0
  111. package/dist/scheduler/db.js.map +1 -1
  112. package/dist/sd-notify.d.ts +27 -0
  113. package/dist/sd-notify.js +33 -1
  114. package/dist/sd-notify.js.map +1 -1
  115. package/dist/secret-file.d.ts +33 -0
  116. package/dist/secret-file.js +36 -0
  117. package/dist/secret-file.js.map +1 -0
  118. package/dist/service-installer.d.ts +16 -0
  119. package/dist/service-installer.js +56 -1
  120. package/dist/service-installer.js.map +1 -1
  121. package/dist/setup-wizard.js +9 -7
  122. package/dist/setup-wizard.js.map +1 -1
  123. package/dist/tmux-control.d.ts +58 -5
  124. package/dist/tmux-control.js +102 -14
  125. package/dist/tmux-control.js.map +1 -1
  126. package/dist/tmux-manager.d.ts +31 -1
  127. package/dist/tmux-manager.js +40 -11
  128. package/dist/tmux-manager.js.map +1 -1
  129. package/dist/topic-commands.d.ts +49 -9
  130. package/dist/topic-commands.js +267 -105
  131. package/dist/topic-commands.js.map +1 -1
  132. package/dist/tui-glyphs.d.ts +5 -1
  133. package/dist/tui-glyphs.js +6 -2
  134. package/dist/tui-glyphs.js.map +1 -1
  135. package/dist/types.d.ts +12 -2
  136. package/dist/ui/view.html +123 -2
  137. package/dist/update-marker.d.ts +15 -0
  138. package/dist/update-marker.js +69 -0
  139. package/dist/update-marker.js.map +1 -0
  140. package/dist/usage/format-rich.d.ts +11 -0
  141. package/dist/usage/format-rich.js +121 -0
  142. package/dist/usage/format-rich.js.map +1 -0
  143. package/dist/usage/provider-alerts.d.ts +33 -0
  144. package/dist/usage/provider-alerts.js +57 -0
  145. package/dist/usage/provider-alerts.js.map +1 -0
  146. package/dist/usage/providers.d.ts +130 -0
  147. package/dist/usage/providers.js +1063 -0
  148. package/dist/usage/providers.js.map +1 -0
  149. package/dist/usage/statusline-usage.d.ts +20 -0
  150. package/dist/usage/statusline-usage.js +120 -0
  151. package/dist/usage/statusline-usage.js.map +1 -0
  152. package/dist/usage/usage-api.d.ts +50 -0
  153. package/dist/usage/usage-api.js +197 -0
  154. package/dist/usage/usage-api.js.map +1 -0
  155. package/dist/view-api.d.ts +3 -0
  156. package/dist/view-api.js +11 -2
  157. package/dist/view-api.js.map +1 -1
  158. package/dist/web-api.js +5 -2
  159. package/dist/web-api.js.map +1 -1
  160. package/package.json +4 -1
  161. package/dist/channel/tool-tracker.d.ts +0 -13
  162. package/dist/channel/tool-tracker.js +0 -58
  163. package/dist/channel/tool-tracker.js.map +0 -1
  164. package/dist/daemon-entry.d.ts +0 -1
  165. package/dist/daemon-entry.js +0 -30
  166. package/dist/daemon-entry.js.map +0 -1
@@ -1,12 +1,13 @@
1
1
  import { readFileSync, existsSync } from "node:fs";
2
- import { exec } from "node:child_process";
2
+ import { exec, execFileSync } from "node:child_process";
3
3
  import { promisify } from "node:util";
4
4
  import { join, basename } from "node:path";
5
+ import { release } from "node:os";
5
6
  import { createRequire } from "node:module";
6
7
  const execAsync = promisify(exec);
7
8
  import { DEFAULT_INSTANCE_CONFIG } from "./config.js";
8
9
  import { formatCents } from "./cost-guard.js";
9
- import { getAgendHome } from "./paths.js";
10
+ import { getAgendHome, getTmuxSocketName, getTmuxSessionName } from "./paths.js";
10
11
  import { t } from "./locale.js";
11
12
  import { PIE_PERCENT_RE } from "./tui-glyphs.js";
12
13
  /** Sanitize a directory name into a valid instance name. Keeps Unicode letters (incl. CJK). */
@@ -48,6 +49,32 @@ export function parsePauseWakeCommand(text) {
48
49
  * Antigravity (agy) has NO summarizing compact — its only manual context-reset
49
50
  * is "/clear" (a full reset; it also auto-summarizes at a token threshold).
50
51
  */
52
+ /** os.release(), guarded — purely informational, must never break /sysinfo. */
53
+ function osRelease() {
54
+ try {
55
+ return release();
56
+ }
57
+ catch {
58
+ return "?";
59
+ }
60
+ }
61
+ /**
62
+ * `tmux -V`, memoised. One short synchronous exec for the lifetime of the
63
+ * process — /sysinfo is on-demand and rare, and the version cannot change under
64
+ * a running fleet (the server would have to restart with it).
65
+ */
66
+ let cachedTmuxVersion = null;
67
+ function tmuxVersion() {
68
+ if (cachedTmuxVersion === null) {
69
+ try {
70
+ cachedTmuxVersion = execFileSync("tmux", ["-V"], { encoding: "utf-8", timeout: 3000 }).trim();
71
+ }
72
+ catch {
73
+ cachedTmuxVersion = "not found";
74
+ }
75
+ }
76
+ return cachedTmuxVersion;
77
+ }
51
78
  export function compactCommandForBackend(backend) {
52
79
  if (backend === "antigravity")
53
80
  return "/clear";
@@ -101,6 +128,115 @@ export function parseContextPercent(pane) {
101
128
  }
102
129
  return null;
103
130
  }
131
+ /** Claude Code statusline.json context used % (null if missing / unreadable). */
132
+ export function readStatuslineContextPct(dataDir, instanceName) {
133
+ try {
134
+ const statusFile = join(dataDir, "instances", instanceName, "statusline.json");
135
+ if (!existsSync(statusFile))
136
+ return null;
137
+ const data = JSON.parse(readFileSync(statusFile, "utf-8"));
138
+ const pct = data.context_window?.used_percentage;
139
+ return typeof pct === "number" && Number.isFinite(pct) ? pct : null;
140
+ }
141
+ catch {
142
+ return null;
143
+ }
144
+ }
145
+ /** Live-pane scrape used by /ctx, /status Ctx, and /view sidebar. */
146
+ export function scrapePaneContext(instanceName, backend) {
147
+ try {
148
+ const socketName = getTmuxSocketName();
149
+ // Scrollback (-S -60) so a recent footer/statusline is kept even mid-output.
150
+ const baseArgs = ["capture-pane", "-t", `${getTmuxSessionName()}:${instanceName}`, "-p", "-S", "-60"];
151
+ const tmuxArgs = socketName ? ["-L", socketName, ...baseArgs] : baseArgs;
152
+ const pane = execFileSync("tmux", tmuxArgs, {
153
+ encoding: "utf-8",
154
+ timeout: 2000,
155
+ stdio: ["pipe", "pipe", "pipe"],
156
+ });
157
+ const tokenRatio = backend === "grok" ? parseTokenContextRatio(pane) : null;
158
+ const context = tokenRatio?.percentage ?? parseContextPercent(pane);
159
+ return { context, tokenRatio };
160
+ }
161
+ catch {
162
+ return { context: null, tokenRatio: null };
163
+ }
164
+ }
165
+ const paneContextCache = new Map();
166
+ // Below the dashboard's 10s SSE tick on purpose: at the previous 12s the cache was
167
+ // guaranteed to be stale on roughly every other tick, which is what made the
168
+ // blocking scrape fire so often. Now a tick either hits the cache or triggers a
169
+ // background refresh, never a synchronous capture.
170
+ const PANE_CONTEXT_CACHE_MS = 8_000;
171
+ /** Instances with a background scrape in flight, so polls don't pile up captures. */
172
+ const paneScrapeInFlight = new Set();
173
+ /** Async twin of scrapePaneContext — same parsers, no blocking. */
174
+ async function scrapePaneContextAsync(instanceName, backend) {
175
+ try {
176
+ const socketName = getTmuxSocketName();
177
+ const baseArgs = ["capture-pane", "-t", `${getTmuxSessionName()}:${instanceName}`, "-p", "-S", "-60"];
178
+ const tmuxArgs = socketName ? ["-L", socketName, ...baseArgs] : baseArgs;
179
+ const { promisify } = await import("node:util");
180
+ const { execFile } = await import("node:child_process");
181
+ const { stdout } = await promisify(execFile)("tmux", tmuxArgs, { encoding: "utf-8", timeout: 2000 });
182
+ const pane = stdout.toString();
183
+ const tokenRatio = backend === "grok" ? parseTokenContextRatio(pane) : null;
184
+ return { context: tokenRatio?.percentage ?? parseContextPercent(pane), tokenRatio };
185
+ }
186
+ catch {
187
+ return { context: null, tokenRatio: null };
188
+ }
189
+ }
190
+ /** Refresh one instance's cached context in the background (deduped per instance). */
191
+ function refreshPaneContext(instanceName, backend) {
192
+ if (paneScrapeInFlight.has(instanceName))
193
+ return;
194
+ paneScrapeInFlight.add(instanceName);
195
+ void scrapePaneContextAsync(instanceName, backend)
196
+ .then(scraped => { paneContextCache.set(instanceName, { at: Date.now(), ...scraped }); })
197
+ .finally(() => { paneScrapeInFlight.delete(instanceName); });
198
+ }
199
+ /** Forget a deleted instance's cached context so the map can't grow forever. */
200
+ export function forgetInstanceContext(instanceName) {
201
+ paneContextCache.delete(instanceName);
202
+ }
203
+ /**
204
+ * Single source of truth for instance context % across /ctx, /status, and View.
205
+ * Claude-code prefers statusline.json (authoritative, no TUI scrape); everyone
206
+ * else scrapes the live pane with the same parsers /ctx uses.
207
+ *
208
+ * Non-blocking by default (stale-while-revalidate): a fresh cache entry is
209
+ * returned as-is; a stale or missing one is returned immediately anyway while a
210
+ * background refresh runs. This used to scrape synchronously with `execFileSync`
211
+ * (2s timeout) on a cache miss, and the 12s TTL is LONGER than the dashboard's
212
+ * 10s poll — so roughly every other tick did N blocking captures. With ten
213
+ * non-claude-code instances and a slow tmux that froze the entire fleet event
214
+ * loop for up to 20s per tick: no IPC, no message delivery, no watchdog ping.
215
+ * Three open browser tabs ran three independent polls.
216
+ *
217
+ * Pass `bypassCache` for a synchronous, authoritative read — used by `/ctx`,
218
+ * where a user is asking right now and 2s of blocking is the correct trade.
219
+ */
220
+ export function resolveInstanceContext(dataDir, instanceName, backend, opts) {
221
+ if (backend === "claude-code") {
222
+ const fromFile = readStatuslineContextPct(dataDir, instanceName);
223
+ if (fromFile != null)
224
+ return { context: fromFile, tokenRatio: null };
225
+ }
226
+ if (opts?.bypassCache) {
227
+ const scraped = scrapePaneContext(instanceName, backend);
228
+ paneContextCache.set(instanceName, { at: Date.now(), ...scraped });
229
+ return scraped;
230
+ }
231
+ const hit = paneContextCache.get(instanceName);
232
+ if (hit && Date.now() - hit.at < PANE_CONTEXT_CACHE_MS) {
233
+ return { context: hit.context, tokenRatio: hit.tokenRatio };
234
+ }
235
+ // Stale or absent: kick off the refresh and answer with what we have. A brand-new
236
+ // instance reads as "no data" for one tick rather than blocking the fleet.
237
+ refreshPaneContext(instanceName, backend);
238
+ return hit ? { context: hit.context, tokenRatio: hit.tokenRatio } : { context: null, tokenRatio: null };
239
+ }
104
240
  export class TopicCommands {
105
241
  ctx;
106
242
  constructor(ctx) {
@@ -135,6 +271,10 @@ export class TopicCommands {
135
271
  await this.handleDoctorCommand(msg);
136
272
  return true;
137
273
  }
274
+ if (text === "/usage" || text.startsWith("/usage@")) {
275
+ await this.handleUsageCommand(msg);
276
+ return true;
277
+ }
138
278
  if (text === "/update" || text.startsWith("/update@")) {
139
279
  await this.handleUpdateCommand(msg);
140
280
  return true;
@@ -230,6 +370,28 @@ export class TopicCommands {
230
370
  : t("collab.off"), { threadId: msg.threadId });
231
371
  return true;
232
372
  }
373
+ if (text === "/effort" || text.startsWith("/effort ") || text.startsWith("/effort@")) {
374
+ const adapter = this.getReplyAdapter(msg);
375
+ if (!adapter)
376
+ return false;
377
+ if (!this.ctx.isFleetAdmin(msg.userId, msg.adapterId)) {
378
+ await adapter.sendText(msg.chatId, t("permission.denied"), { threadId: msg.threadId });
379
+ return true;
380
+ }
381
+ const level = text.replace(/^\/effort(@\S+)?/, "").trim();
382
+ if (level) {
383
+ const reply = await this.ctx.applyEffort?.(instanceName, level)
384
+ ?? "Effort switching is unavailable.";
385
+ await adapter.sendText(msg.chatId, reply, { threadId: msg.threadId });
386
+ }
387
+ else if (this.ctx.promptEffortMenu) {
388
+ // No arg → inline keyboard menu (TG), same shape as /model.
389
+ const fallback = await this.ctx.promptEffortMenu(instanceName, msg.userId, msg.threadId ?? msg.chatId, adapter, msg.chatId, msg.threadId);
390
+ if (fallback)
391
+ await adapter.sendText(msg.chatId, fallback, { threadId: msg.threadId });
392
+ }
393
+ return true;
394
+ }
233
395
  if (text === "/model" || text.startsWith("/model ") || text.startsWith("/model@")) {
234
396
  const adapter = this.getReplyAdapter(msg);
235
397
  if (!adapter)
@@ -307,50 +469,22 @@ export class TopicCommands {
307
469
  return t(`${action}.failed`, instanceName, err.message);
308
470
  }
309
471
  }
310
- /** Get context usage text for an instance (shared by TG + DC) */
311
- async getCtxText(instanceName) {
312
- // Classic instances live in classicBot.yaml, not fleet.yaml → consult the
313
- // classic channel manager for those so we don't mis-report defaults.backend.
314
- // getBackendByInstance always returns a string (its own fallback), so only
315
- // use it when the instance is ACTUALLY classic — otherwise a fleet instance
316
- // that inherits its backend would wrongly pick up the classic default.
472
+ /** Resolve the effective backend name for fleet or classic instances. */
473
+ effectiveBackend(instanceName) {
317
474
  const classicBackend = this.ctx.classicChannels?.getChannelIdByInstance(instanceName)
318
- ? this.ctx.classicChannels.getBackendByInstance(instanceName)
475
+ ? this.ctx.classicChannels.getBackendByInstance(instanceName, this.ctx.fleetConfig?.defaults?.backend)
319
476
  : undefined;
320
- const backend = this.ctx.fleetConfig?.instances[instanceName]?.backend
477
+ return this.ctx.fleetConfig?.instances[instanceName]?.backend
321
478
  ?? classicBackend
322
479
  ?? this.ctx.fleetConfig?.defaults?.backend ?? "claude-code";
323
- let context = null;
324
- let tokenRatio = null;
325
- // Only claude-code writes statusline.json. Reading it for other backends
326
- // risks a stale value left over from a previous backend (e.g. after
327
- // switching claude-code kiro-cli), so those go straight to capture-pane.
328
- if (backend === "claude-code") {
329
- try {
330
- const statusFile = join(this.ctx.dataDir, "instances", instanceName, "statusline.json");
331
- if (existsSync(statusFile)) {
332
- const d = JSON.parse(readFileSync(statusFile, "utf-8"));
333
- context = d.context_window?.used_percentage ?? null;
334
- }
335
- }
336
- catch { /* ignore */ }
337
- }
338
- if (context == null) {
339
- try {
340
- const { execFileSync } = await import("node:child_process");
341
- const { getTmuxSocketName, getTmuxSessionName } = await import("./paths.js");
342
- const socketName = getTmuxSocketName();
343
- // Include scrollback (-S -60) so a recent prompt is captured even when
344
- // the CLI is mid-output and the bottom of the visible pane has no prompt.
345
- const baseArgs = ["capture-pane", "-t", `${getTmuxSessionName()}:${instanceName}`, "-p", "-S", "-60"];
346
- const tmuxArgs = socketName ? ["-L", socketName, ...baseArgs] : baseArgs;
347
- const pane = execFileSync("tmux", tmuxArgs, { encoding: "utf-8", timeout: 2000, stdio: ["pipe", "pipe", "pipe"] });
348
- if (backend === "grok")
349
- tokenRatio = parseTokenContextRatio(pane);
350
- context = tokenRatio?.percentage ?? parseContextPercent(pane);
351
- }
352
- catch { /* ignore */ }
353
- }
480
+ }
481
+ /** Get context usage text for an instance (shared by TG + DC) */
482
+ async getCtxText(instanceName) {
483
+ // Classic instances live in classicBot.yaml, not fleet.yaml consult the
484
+ // classic channel manager for those so we don't mis-report defaults.backend.
485
+ const backend = this.effectiveBackend(instanceName);
486
+ // Fresh scrape for /ctx (user is asking right now) — bypass short cache.
487
+ const { context, tokenRatio } = resolveInstanceContext(this.ctx.dataDir, instanceName, backend, { bypassCache: true });
354
488
  const contextLine = context == null ? null : formatContextUsageLine(context, tokenRatio);
355
489
  // Effective model (resolves per-instance → fleet default → classic channel →
356
490
  // the CLI's own default) via the shared resolver, so /ctx and /model agree.
@@ -364,7 +498,11 @@ export class TopicCommands {
364
498
  async sendCompact(instanceName) {
365
499
  const ipc = this.ctx.instanceIpcClients.get(instanceName);
366
500
  if (ipc?.connected) {
501
+ const classicBackend = this.ctx.classicChannels?.getChannelIdByInstance(instanceName)
502
+ ? this.ctx.classicChannels.getBackendByInstance(instanceName, this.ctx.fleetConfig?.defaults?.backend)
503
+ : undefined;
367
504
  const backend = this.ctx.fleetConfig?.instances[instanceName]?.backend
505
+ ?? classicBackend
368
506
  ?? this.ctx.fleetConfig?.defaults?.backend ?? "claude-code";
369
507
  const cmd = compactCommandForBackend(backend);
370
508
  ipc.send({ type: "raw_paste", content: cmd });
@@ -374,7 +512,11 @@ export class TopicCommands {
374
512
  }
375
513
  /** Send a backend-appropriate session-save command to a fleet-topic instance. */
376
514
  async sendSave(instanceName, filename) {
515
+ const classicBackend = this.ctx.classicChannels?.getChannelIdByInstance(instanceName)
516
+ ? this.ctx.classicChannels.getBackendByInstance(instanceName, this.ctx.fleetConfig?.defaults?.backend)
517
+ : undefined;
377
518
  const backend = this.ctx.fleetConfig?.instances[instanceName]?.backend
519
+ ?? classicBackend
378
520
  ?? this.ctx.fleetConfig?.defaults?.backend ?? "claude-code";
379
521
  const cmd = saveCommandForBackend(backend, filename);
380
522
  if (!cmd)
@@ -400,10 +542,44 @@ export class TopicCommands {
400
542
  await adapter.sendText(chatId, t("restart.graceful"), { threadId });
401
543
  process.kill(process.pid, "SIGUSR2");
402
544
  }
545
+ /**
546
+ * `/usage` — AI subscription usage for every CLI backend logged in on this
547
+ * machine, one compact message. Same permission level as /ctx (none): whoever
548
+ * can talk to the fleet may see how much headroom it has left. Vendor rate
549
+ * limits are protected by the shared 5-minute cache, not by gating callers.
550
+ */
551
+ async handleUsageCommand(msg) {
552
+ const adapter = this.getReplyAdapter(msg);
553
+ if (!adapter)
554
+ return;
555
+ try {
556
+ const { getUsageSnapshot } = await import("./usage/usage-api.js");
557
+ const { renderUsageHtml, renderUsageMarkdown } = await import("./usage/format-rich.js");
558
+ const payload = await getUsageSnapshot();
559
+ // Each platform gets its native rich format: Telegram needs parse_mode
560
+ // HTML; Discord renders Markdown in plain content. Anything else falls back
561
+ // to Markdown, which degrades to readable text.
562
+ if (adapter.type === "telegram") {
563
+ await adapter.sendText(msg.chatId, renderUsageHtml(payload), { threadId: msg.threadId, format: "html" });
564
+ }
565
+ else {
566
+ await adapter.sendText(msg.chatId, renderUsageMarkdown(payload), { threadId: msg.threadId });
567
+ }
568
+ }
569
+ catch (err) {
570
+ await adapter.sendText(msg.chatId, `⚠️ Usage fetch failed: ${err.message}`, { threadId: msg.threadId });
571
+ }
572
+ }
403
573
  async handleStatusCommand(msg) {
404
574
  const adapter = this.getReplyAdapter(msg);
405
575
  if (!adapter || !this.ctx.fleetConfig)
406
576
  return;
577
+ // Admin-gated: with the /sysinfo instance table folded in, /status now shows
578
+ // the whole fleet's per-instance costs and IPC health in one message.
579
+ if (!this.ctx.isFleetAdmin(msg.userId, msg.adapterId)) {
580
+ await adapter.sendText(msg.chatId, t("cmd.admin_required", "/status"), { threadId: msg.threadId });
581
+ return;
582
+ }
407
583
  const text = await this.getStatusText();
408
584
  await adapter.sendText(msg.chatId, text, { threadId: msg.threadId });
409
585
  }
@@ -438,18 +614,16 @@ export class TopicCommands {
438
614
  const costPaused = this.ctx.costGuard?.isLimited(name);
439
615
  if (status === "paused")
440
616
  pausedCount++;
441
- let contextStr = "-";
442
- try {
443
- const data = JSON.parse(readFileSync(join(this.ctx.dataDir, "instances", name, "statusline.json"), "utf-8"));
444
- if (data.context_window?.used_percentage != null) {
445
- contextStr = `${Math.round(data.context_window.used_percentage)}%`;
446
- }
447
- }
448
- catch { /* file may not exist yet */ }
449
- const costCents = this.ctx.costGuard?.getDailyCostCents(name) ?? 0;
450
617
  const backend = classic
451
618
  ? this.ctx.classicChannels?.getBackendByInstance(name, this.ctx.fleetConfig.defaults?.backend) ?? "-"
452
619
  : this.ctx.fleetConfig.instances[name]?.backend ?? this.ctx.fleetConfig.defaults?.backend ?? "-";
620
+ // Same source as /ctx (statusline for claude-code, pane scrape otherwise).
621
+ // Without this, kiro/grok/codex always showed "-" in the Ctx column.
622
+ const { context } = backend === "-"
623
+ ? { context: null }
624
+ : resolveInstanceContext(this.ctx.dataDir, name, backend);
625
+ const contextStr = context == null ? "-" : `${Math.round(context)}%`;
626
+ const costCents = this.ctx.costGuard?.getDailyCostCents(name) ?? 0;
453
627
  let icon;
454
628
  if (costPaused || status === "paused")
455
629
  icon = "⏸";
@@ -465,15 +639,21 @@ export class TopicCommands {
465
639
  : executionState === "paused" ? "⏸ paused"
466
640
  : "—";
467
641
  const displayName = this.shortInstanceName(name);
468
- rows.push(`| ${displayName} | ${backend} | ${contextStr} | ${formatCents(costCents)} | ${icon} | ${stateLabel} |`);
642
+ // IPC reachability moved here from /sysinfo: it is per-instance health, and
643
+ // /status is now the one place that shows the fleet per instance.
644
+ const ipc = this.ctx.instanceIpcClients.has(name) ? "✓" : "✗";
645
+ // "-" distinguishes "backend has no effort setting" from an unset one:
646
+ // an empty cell would read as missing data rather than not-applicable.
647
+ const effort = this.ctx.resolveInstanceEffort?.(name).effort ?? "-";
648
+ rows.push(`| ${displayName} | ${backend} | ${contextStr} | ${effort} | ${formatCents(costCents)} | ${icon} | ${stateLabel} | ${ipc} |`);
469
649
  }
470
650
  if (rows.length === 0)
471
651
  return "No instances configured.";
472
652
  const lines = [
473
653
  "## Fleet Status",
474
654
  "",
475
- "| Instance | Backend | Ctx | Cost | Status | State |",
476
- "|----------|---------|-----|------|--------|-------|",
655
+ "| Instance | Backend | Ctx | Effort | Cost | Status | State | IPC |",
656
+ "|----------|---------|-----|--------|------|--------|-------|-----|",
477
657
  ...rows,
478
658
  "",
479
659
  `Paused instances: ${pausedCount}`,
@@ -504,63 +684,33 @@ export class TopicCommands {
504
684
  const text = this.getSysInfoText();
505
685
  await adapter.sendText(msg.chatId, text, { threadId: msg.threadId });
506
686
  }
507
- /** Get system info as markdown text (shared by TG + DC) */
687
+ /**
688
+ * Get system info as markdown text (shared by TG + DC).
689
+ *
690
+ * System-level only. The per-instance table (state/IPC/cost) moved to /status,
691
+ * which was already the fleet-per-instance view — the two tables overlapped on
692
+ * everything but the IPC column, and each command answered half of "is the
693
+ * machine fine and are the instances fine".
694
+ */
508
695
  getSysInfoText() {
509
696
  const info = this.ctx.getSysInfo();
510
697
  const upHours = Math.floor(info.uptime_seconds / 3600);
511
698
  const upMins = Math.floor((info.uptime_seconds % 3600) / 60);
512
699
  const require = createRequire(import.meta.url);
513
700
  const agendVersion = require("../package.json").version ?? "unknown";
514
- const lines = [
701
+ return [
515
702
  "## System Info",
516
703
  "",
517
704
  "| Metric | Value |",
518
705
  "|--------|-------|",
519
706
  `| AgEnD | v${agendVersion} |`,
707
+ `| OS | ${process.platform} ${osRelease()} (${process.arch}) |`,
708
+ `| Node | ${process.version} |`,
709
+ `| tmux | ${tmuxVersion()} |`,
520
710
  `| Uptime | ${upHours}h ${upMins}m |`,
521
711
  `| Memory | ${info.memory_mb.rss} MB RSS |`,
522
712
  `| Heap | ${info.memory_mb.heapUsed} / ${info.memory_mb.heapTotal} MB |`,
523
- "",
524
- "## Instances",
525
- "",
526
- "| Name | State | IPC | Cost | Rate |",
527
- "|------|-------|-----|------|------|",
528
- ];
529
- const fleetNames = new Set(info.instances.map(inst => inst.name));
530
- const classicInstances = (this.ctx.classicChannels?.getAll() ?? [])
531
- .filter(ch => !fleetNames.has(ch.instanceName))
532
- .map(ch => ({
533
- name: ch.instanceName,
534
- status: this.ctx.getInstanceStatus(ch.instanceName),
535
- state: this.ctx.getInstanceExecutionState?.(ch.instanceName) ?? null,
536
- ipc: this.ctx.instanceIpcClients.has(ch.instanceName),
537
- costCents: this.ctx.costGuard?.getDailyCostCents(ch.instanceName) ?? 0,
538
- rateLimits: null,
539
- classic: true,
540
- }));
541
- const instances = [
542
- ...info.instances.map(inst => ({ ...inst, classic: false })),
543
- ...classicInstances,
544
- ];
545
- for (const inst of instances) {
546
- const executionState = inst.state;
547
- const displayState = inst.status === "paused" ? "paused"
548
- : inst.status === "stopped" || inst.status === "crashed" ? inst.status
549
- : executionState ?? "running";
550
- const icon = displayState === "idle" ? "🟢"
551
- : displayState === "working" ? "🔵"
552
- : displayState === "stuck" || displayState === "crashed" ? "🔴"
553
- : displayState === "paused" ? "⏸"
554
- : displayState === "stopped" ? "⚪" : "🟢";
555
- const ipc = inst.ipc ? "✓" : "✗";
556
- const rate = inst.rateLimits ? `5h:${inst.rateLimits.five_hour_pct}%` : "-";
557
- const displayName = this.shortInstanceName(inst.name);
558
- lines.push(`| ${icon} ${displayName} | ${displayState} | ${ipc} | ${formatCents(inst.costCents)} | ${rate} |`);
559
- }
560
- if (info.fleet_cost_limit_cents > 0) {
561
- lines.push("", `Fleet cost: ${formatCents(info.fleet_cost_cents)} / ${formatCents(info.fleet_cost_limit_cents)} daily`);
562
- }
563
- return lines.join("\n");
713
+ ].join("\n");
564
714
  }
565
715
  async handleUpdateCommand(msg) {
566
716
  const adapter = this.getReplyAdapter(msg);
@@ -597,17 +747,26 @@ export class TopicCommands {
597
747
  return;
598
748
  }
599
749
  await adapter.sendText(chatId, t("doctor.running"), { threadId });
750
+ // Async, and execFile rather than a shell string: as execSync this froze the
751
+ // whole fleet event loop for up to 30s — no IPC, no delivery, no WATCHDOG
752
+ // ping — and any allowlisted user could trigger it with /doctor.
753
+ const stripAnsi = (s) => s.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "");
754
+ const backend = this.ctx.fleetConfig?.defaults?.backend || "claude-code";
755
+ let output;
600
756
  try {
601
- const { execSync } = await import("node:child_process");
602
- const backend = this.ctx.fleetConfig?.defaults?.backend || "claude-code";
603
- const result = execSync(`agend backend doctor ${backend}`, { timeout: 30_000, encoding: "utf-8" });
604
- const clean = result.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "");
605
- await adapter.sendText(chatId, clean || "No output", { threadId });
757
+ const { execFile } = await import("node:child_process");
758
+ const { promisify } = await import("node:util");
759
+ const { stdout } = await promisify(execFile)("agend", ["backend", "doctor", backend], {
760
+ timeout: 30_000,
761
+ encoding: "utf-8",
762
+ });
763
+ output = stripAnsi(stdout) || "No output";
606
764
  }
607
765
  catch (err) {
608
- const output = (err.stdout ?? err.message ?? "Doctor failed").replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "");
609
- await adapter.sendText(chatId, output, { threadId });
766
+ const e = err;
767
+ output = stripAnsi(e.stdout ?? e.message ?? "Doctor failed");
610
768
  }
769
+ await adapter.sendText(chatId, output, { threadId });
611
770
  }
612
771
  /** Reply with redirect when message arrives in an unbound topic */
613
772
  async handleUnboundTopic(msg) {
@@ -726,18 +885,20 @@ export class TopicCommands {
726
885
  headers: { "Content-Type": "application/json" },
727
886
  body: JSON.stringify({
728
887
  commands: [
729
- { command: "status", description: "Show fleet status and costs" },
888
+ { command: "status", description: "🔒 Fleet status, per-instance costs and health" },
730
889
  { command: "sysinfo", description: "System diagnostics" },
731
890
  { command: "dashboard", description: "🔒 Get dashboard URLs" },
732
891
  { command: "ctx", description: "Show context usage" },
733
892
  { command: "compact", description: "Compact agent context" },
734
893
  { command: "model", description: "🔒 Switch model (admin only)" },
894
+ { command: "effort", description: "🔒 Set reasoning effort (admin only)" },
735
895
  { command: "pause", description: "🔒 Pause an idle instance" },
736
896
  { command: "wake", description: "🔒 Wake a paused instance" },
737
897
  { command: "restart", description: "🔒 Graceful restart all instances" },
738
898
  { command: "collab", description: "🔒 Toggle bot/webhook mode" },
739
899
  { command: "update", description: "🔒 Update AgEnD to latest" },
740
900
  { command: "doctor", description: "🔒 Run health diagnostics" },
901
+ { command: "usage", description: "AI subscription usage" },
741
902
  ],
742
903
  scope: { type: "chat", chat_id: ch.group_id },
743
904
  }),
@@ -752,6 +913,7 @@ export class TopicCommands {
752
913
  { command: "stop", description: "🔒 Stop the agent" },
753
914
  { command: "compact", description: "🔒 Compact agent context" },
754
915
  { command: "model", description: "🔒 Switch model (admin only)" },
916
+ { command: "effort", description: "🔒 Set reasoning effort (admin only)" },
755
917
  { command: "pause", description: "🔒 Pause the agent" },
756
918
  { command: "wake", description: "🔒 Wake the agent" },
757
919
  { command: "ctx", description: "Show context usage" },