@app-vitals/agent-runtime 1.92.0 → 1.93.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/boot.ts CHANGED
@@ -100,6 +100,7 @@ import { ensureAgentHome, installPlugins, runMiseStartup } from "./setup.ts";
100
100
  import { SCRIPTS_BIN } from "./scripts-bin.ts";
101
101
  import { checkCredentialHelper, setupGitHubAuth } from "./setup-github-auth.ts";
102
102
  import { HttpShipwrightRuntimeClient } from "./shipwright-runtime-client.ts";
103
+ import { singleFlight } from "./single-flight.ts";
103
104
  import { createSlackApp, hasSlackCredentials } from "./slack.ts";
104
105
  import {
105
106
  type StartableSlackApp,
@@ -699,7 +700,16 @@ export async function startAgent(
699
700
 
700
701
  // Await first sync so ANTHROPIC_API_KEY is set before Slack starts
701
702
  await syncConfig();
702
- setInterval(() => void syncConfig(), 60_000);
703
+ // A tick installs plugins by spawning `claude plugin …` subprocesses, so
704
+ // one that outlasts the 60s interval must not overlap the next: the
705
+ // overlapping ticks starve each other until the health probe times out.
706
+ const syncConfigTick = singleFlight(syncConfig, {
707
+ onSkip: (inFlightMs) =>
708
+ console.warn(
709
+ `[config-sync] tick skipped: the previous sync has been running for ${inFlightMs}ms`,
710
+ ),
711
+ });
712
+ setInterval(() => void syncConfigTick(), 60_000);
703
713
  console.log("[agent] config sync started (60s interval)");
704
714
  }
705
715
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@app-vitals/agent-runtime",
3
- "version": "1.92.0",
3
+ "version": "1.93.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -41,8 +41,8 @@
41
41
  "./workspace-paths": "./workspace-paths.ts"
42
42
  },
43
43
  "dependencies": {
44
- "@app-vitals/agent-admin": "1.92.0",
45
- "@app-vitals/agent-lib": "1.92.0",
44
+ "@app-vitals/agent-admin": "1.93.0",
45
+ "@app-vitals/agent-lib": "1.93.0",
46
46
  "@octokit/auth-app": "^8.2.0",
47
47
  "@sentry/bun": "^10.63.0",
48
48
  "@slack/bolt": "^5.0.0",
@@ -26,6 +26,7 @@ import {
26
26
  installPlugins,
27
27
  isNewWorkspace,
28
28
  loadState,
29
+ MISE_INSTALL_TIMEOUT_MS,
29
30
  runMiseStartup,
30
31
  saveState,
31
32
  seedFile,
@@ -654,6 +655,24 @@ describe("runMiseStartup", () => {
654
655
  expect(process.env.CARGO_HOME).toBe(before);
655
656
  });
656
657
 
658
+ it("gives mise install its own longer timeout, not the default subprocess cap", async () => {
659
+ mkdirSync(join(testHome, "workspace"), { recursive: true });
660
+ writeFileSync(join(testHome, "workspace", "mise.toml"), "[tools]\n");
661
+ const calls: Array<{ args: string[]; timeoutMs?: number }> = [];
662
+ const mockExec = async (
663
+ _cmd: string,
664
+ args: string[],
665
+ opts: { cwd: string; timeoutMs?: number },
666
+ ) => {
667
+ calls.push({ args, timeoutMs: opts.timeoutMs });
668
+ return { stdout: "", exitCode: 0 };
669
+ };
670
+ await runMiseStartup(testHome, mockExec);
671
+ const install = calls.find((c) => c.args[0] === "install");
672
+ expect(install?.timeoutMs).toBe(MISE_INSTALL_TIMEOUT_MS);
673
+ expect(MISE_INSTALL_TIMEOUT_MS).toBeGreaterThan(5 * 60 * 1000);
674
+ });
675
+
657
676
  it("skips mise install when mise.toml absent", async () => {
658
677
  mkdirSync(join(testHome, "workspace"), { recursive: true });
659
678
  const calls: Array<{ cmd: string; args: string[] }> = [];
package/setup.ts CHANGED
@@ -334,13 +334,32 @@ export function discoverBakedMarketplaces(conventionRoot: string): string[] {
334
334
  type ExecFn = (
335
335
  cmd: string,
336
336
  args: string[],
337
- opts: { cwd: string; env?: Record<string, string | undefined> },
337
+ opts: {
338
+ cwd: string;
339
+ env?: Record<string, string | undefined>;
340
+ timeoutMs?: number;
341
+ },
338
342
  ) => Promise<{ stdout: string; exitCode: number }>;
339
343
 
340
- async function defaultExec(
344
+ /** Max wall time for one subprocess before it is killed (5 minutes). */
345
+ const DEFAULT_EXEC_TIMEOUT_MS = 5 * 60 * 1000;
346
+
347
+ /**
348
+ * Max wall time for the boot-time `mise install` (30 minutes). A cold install
349
+ * can compile tools such as python or ruby from source, which outlasts the
350
+ * default cap; killing it would leave the workspace's tools missing on every
351
+ * boot that cannot finish in one run.
352
+ */
353
+ export const MISE_INSTALL_TIMEOUT_MS = 30 * 60 * 1000;
354
+
355
+ export async function defaultExec(
341
356
  cmd: string,
342
357
  args: string[],
343
- opts: { cwd: string; env?: Record<string, string | undefined> },
358
+ opts: {
359
+ cwd: string;
360
+ env?: Record<string, string | undefined>;
361
+ timeoutMs?: number;
362
+ },
344
363
  ): Promise<{ stdout: string; exitCode: number }> {
345
364
  // env: process.env is required — Bun.spawn otherwise snapshots env at Bun
346
365
  // startup and misses runtime mutations (MISE_CACHE_DIR, etc.).
@@ -350,9 +369,20 @@ async function defaultExec(
350
369
  stdout: "pipe",
351
370
  stderr: "pipe",
352
371
  });
353
- const stdout = await new Response(proc.stdout).text();
354
- await proc.exited;
355
- return { stdout, exitCode: proc.exitCode ?? 1 };
372
+ // Bound the run: a hung subprocess must not block callers (e.g. the
373
+ // single-flight config sync) forever.
374
+ let timedOut = false;
375
+ const timer = setTimeout(() => {
376
+ timedOut = true;
377
+ proc.kill("SIGKILL");
378
+ }, opts.timeoutMs ?? DEFAULT_EXEC_TIMEOUT_MS);
379
+ try {
380
+ const stdout = await new Response(proc.stdout).text();
381
+ await proc.exited;
382
+ return { stdout, exitCode: timedOut ? 124 : (proc.exitCode ?? 1) };
383
+ } finally {
384
+ clearTimeout(timer);
385
+ }
356
386
  }
357
387
 
358
388
  /**
@@ -1189,7 +1219,10 @@ export async function runMiseStartup(
1189
1219
  // Run mise install (idempotent — no-op when tools already cached)
1190
1220
  let install: { stdout: string; exitCode: number };
1191
1221
  try {
1192
- install = await execFn("mise", ["install"], { cwd: workspaceDir });
1222
+ install = await execFn("mise", ["install"], {
1223
+ cwd: workspaceDir,
1224
+ timeoutMs: MISE_INSTALL_TIMEOUT_MS,
1225
+ });
1193
1226
  } catch (err) {
1194
1227
  console.warn(
1195
1228
  `[agent] mise install failed — tools may be unavailable: ${(err as Error).message}`,
@@ -16,6 +16,7 @@ import {
16
16
  import { tmpdir } from "node:os";
17
17
  import { join } from "node:path";
18
18
  import {
19
+ defaultExec,
19
20
  discoverBakedMarketplaces,
20
21
  installPlugins,
21
22
  resolveShipwrightRepoRoot,
@@ -599,3 +600,20 @@ describe("installPlugins — remote marketplace owner hint (TMK-1.1)", () => {
599
600
  });
600
601
  }
601
602
  });
603
+
604
+ describe("defaultExec timeout", () => {
605
+ it("kills a hung subprocess and returns a nonzero exit code", async () => {
606
+ const start = Date.now();
607
+ const res = await defaultExec("sleep", ["30"], {
608
+ cwd: tmpdir(),
609
+ timeoutMs: 100,
610
+ });
611
+ expect(res.exitCode).not.toBe(0);
612
+ expect(Date.now() - start).toBeLessThan(10_000);
613
+ });
614
+
615
+ it("returns stdout and exit code for a fast command", async () => {
616
+ const res = await defaultExec("echo", ["hi"], { cwd: tmpdir() });
617
+ expect(res).toEqual({ stdout: "hi\n", exitCode: 0 });
618
+ });
619
+ });
@@ -0,0 +1,46 @@
1
+ /**
2
+ * runtime/single-flight.ts
3
+ *
4
+ * Wraps an async tick so a new call is dropped while the previous one is still
5
+ * running. `setInterval` fires on the wall clock whether or not the last tick
6
+ * finished, so a tick that outlasts its interval otherwise overlaps the next
7
+ * one — and when the tick spawns CPU-heavy subprocesses, each overlap slows
8
+ * every in-flight tick further until the process can no longer answer its
9
+ * health probe.
10
+ */
11
+
12
+ export interface SingleFlightOptions {
13
+ /**
14
+ * Called when a call is dropped because the previous one is still running.
15
+ * Receives how long that run has been in flight, in milliseconds.
16
+ */
17
+ onSkip?: (inFlightMs: number) => void;
18
+ /** Injected for tests; defaults to `Date.now`. */
19
+ now?: () => number;
20
+ }
21
+
22
+ /**
23
+ * Returns a function that runs `fn` unless a previous run is still in flight,
24
+ * in which case it calls `onSkip` and resolves without running `fn`. A run
25
+ * that rejects still releases the guard, and the rejection is passed through.
26
+ */
27
+ export function singleFlight(
28
+ fn: () => Promise<void>,
29
+ options: SingleFlightOptions = {},
30
+ ): () => Promise<void> {
31
+ const now = options.now ?? Date.now;
32
+ let startedAt: number | null = null;
33
+
34
+ return async () => {
35
+ if (startedAt !== null) {
36
+ options.onSkip?.(now() - startedAt);
37
+ return;
38
+ }
39
+ startedAt = now();
40
+ try {
41
+ await fn();
42
+ } finally {
43
+ startedAt = null;
44
+ }
45
+ };
46
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * runtime/single-flight.unit.test.ts
3
+ *
4
+ * Unit tests for singleFlight() — the re-entry guard on boot.ts's syncConfig()
5
+ * interval. Pure logic with an injected clock and hand-resolved promises; no
6
+ * timers, no I/O.
7
+ */
8
+
9
+ import { describe, expect, test } from "bun:test";
10
+ import { singleFlight } from "./single-flight.ts";
11
+
12
+ /** A promise whose settlement the test controls. */
13
+ function deferred() {
14
+ let resolve!: () => void;
15
+ let reject!: (err: Error) => void;
16
+ const promise = new Promise<void>((res, rej) => {
17
+ resolve = res;
18
+ reject = rej;
19
+ });
20
+ return { promise, resolve, reject };
21
+ }
22
+
23
+ describe("singleFlight", () => {
24
+ test("runs the function when nothing is in flight", async () => {
25
+ let runs = 0;
26
+ const guarded = singleFlight(async () => {
27
+ runs++;
28
+ });
29
+
30
+ await guarded();
31
+ await guarded();
32
+
33
+ expect(runs).toBe(2);
34
+ });
35
+
36
+ test("drops a call made while the previous run is still in flight", async () => {
37
+ const gate = deferred();
38
+ let runs = 0;
39
+ const guarded = singleFlight(async () => {
40
+ runs++;
41
+ await gate.promise;
42
+ });
43
+
44
+ const first = guarded();
45
+ await guarded();
46
+ await guarded();
47
+ expect(runs).toBe(1);
48
+
49
+ gate.resolve();
50
+ await first;
51
+ await guarded();
52
+ expect(runs).toBe(2);
53
+ });
54
+
55
+ test("reports how long the in-flight run has been going on each skip", async () => {
56
+ const gate = deferred();
57
+ let clock = 1_000;
58
+ const skips: number[] = [];
59
+ const guarded = singleFlight(() => gate.promise, {
60
+ now: () => clock,
61
+ onSkip: (ms) => skips.push(ms),
62
+ });
63
+
64
+ const first = guarded();
65
+ clock = 61_000;
66
+ await guarded();
67
+ clock = 121_000;
68
+ await guarded();
69
+
70
+ expect(skips).toEqual([60_000, 120_000]);
71
+
72
+ gate.resolve();
73
+ await first;
74
+ });
75
+
76
+ test("does not call onSkip when no run is in flight", async () => {
77
+ const skips: number[] = [];
78
+ const guarded = singleFlight(async () => {}, {
79
+ onSkip: (ms) => skips.push(ms),
80
+ });
81
+
82
+ await guarded();
83
+ await guarded();
84
+
85
+ expect(skips).toEqual([]);
86
+ });
87
+
88
+ test("releases the guard and passes the rejection through when a run fails", async () => {
89
+ let runs = 0;
90
+ const guarded = singleFlight(async () => {
91
+ runs++;
92
+ if (runs === 1) throw new Error("boom");
93
+ });
94
+
95
+ await expect(guarded()).rejects.toThrow("boom");
96
+ await guarded();
97
+
98
+ expect(runs).toBe(2);
99
+ });
100
+ });