agent-yes 1.164.0 → 1.166.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/dist/{SUPPORTED_CLIS-DAf9N7Wn.js → SUPPORTED_CLIS-DCwX8lvW.js} +2 -2
- package/dist/SUPPORTED_CLIS-DsYijbWx.js +8 -0
- package/dist/cli.js +14 -5
- package/dist/index.js +2 -2
- package/dist/{remotes-DkNuq417.js → remotes-ClT1bq16.js} +1 -1
- package/dist/{remotes-DBCvpp3B.js → remotes-T6nf0t3K.js} +2 -2
- package/dist/{schedule-mKjjuT1M.js → schedule-BkUFuCvW.js} +5 -5
- package/dist/{serve-CMNGOlQ3.js → serve-BtJDjoPO.js} +12 -9
- package/dist/{setup-NJ4_GFXO.js → setup-CK6lH0UM.js} +3 -3
- package/dist/{share-Bq_tDXQU.js → share-BjqQBWM-.js} +1 -1
- package/dist/spawnGate-B_VDMXYL.js +107 -0
- package/dist/spawnGate-UH73I2le.js +5 -0
- package/dist/{subcommands-XvqBWxtm.js → subcommands-CuuiDKr0.js} +230 -20
- package/dist/subcommands-lQ3cyReU.js +9 -0
- package/dist/{tray-BMzpUSfa.js → tray-CZarCA2Q.js} +1 -1
- package/dist/{ts-Bjfd09LQ.js → ts-YJSzO6hM.js} +2 -2
- package/dist/{versionChecker-DiNfz39j.js → versionChecker-DwkNjj7n.js} +2 -2
- package/dist/{webrtcRemote-Ccdzmuc-.js → webrtcRemote-GAgF5K45.js} +1 -1
- package/dist/{workspaceConfig-B0Q9-q2B.js → workspaceConfig-D3OH7and.js} +41 -2
- package/package.json +1 -1
- package/ts/cli.ts +21 -0
- package/ts/needsInput.spec.ts +42 -1
- package/ts/needsInput.ts +54 -0
- package/ts/serve.ts +8 -0
- package/ts/spawnGate.spec.ts +155 -0
- package/ts/spawnGate.ts +123 -0
- package/ts/subcommands.spec.ts +33 -1
- package/ts/subcommands.ts +308 -31
- package/ts/workspaceConfig.spec.ts +85 -0
- package/ts/workspaceConfig.ts +64 -0
- package/dist/SUPPORTED_CLIS-BJ8hXe7M.js +0 -8
- package/dist/subcommands-CNDyinaw.js +0 -9
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "fs";
|
|
3
|
+
import { tmpdir } from "os";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import {
|
|
6
|
+
memAvailableMb,
|
|
7
|
+
spawnGateEnabled,
|
|
8
|
+
spawnRejectionReason,
|
|
9
|
+
waitForSpawnCapacity,
|
|
10
|
+
} from "./spawnGate.ts";
|
|
11
|
+
|
|
12
|
+
// Build a minimal live pid record. Using the test runner's own pid guarantees
|
|
13
|
+
// `isProcessAlive` (process.kill(pid, 0)) succeeds, so the record counts as live.
|
|
14
|
+
const liveRecord = (pid: number) =>
|
|
15
|
+
JSON.stringify({
|
|
16
|
+
pid,
|
|
17
|
+
cli: "claude",
|
|
18
|
+
prompt: null,
|
|
19
|
+
cwd: "/tmp",
|
|
20
|
+
log_file: null,
|
|
21
|
+
status: "active",
|
|
22
|
+
exit_code: null,
|
|
23
|
+
exit_reason: null,
|
|
24
|
+
started_at: 0,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("spawnGate", () => {
|
|
28
|
+
let original: string | undefined;
|
|
29
|
+
let tmp: string;
|
|
30
|
+
const writePids = (lines: string[]) =>
|
|
31
|
+
writeFileSync(path.join(tmp, "pids.jsonl"), lines.join("\n") + "\n");
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
original = process.env.AGENT_YES_HOME;
|
|
35
|
+
tmp = mkdtempSync(path.join(tmpdir(), "ay-gate-"));
|
|
36
|
+
process.env.AGENT_YES_HOME = tmp;
|
|
37
|
+
});
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
if (original === undefined) delete process.env.AGENT_YES_HOME;
|
|
40
|
+
else process.env.AGENT_YES_HOME = original;
|
|
41
|
+
delete process.env.AGENT_YES_MAX_AGENTS;
|
|
42
|
+
delete process.env.AGENT_YES_MIN_FREE_MB;
|
|
43
|
+
delete process.env.AGENT_YES_SPAWN_WAIT_MS;
|
|
44
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("memAvailableMb", () => {
|
|
48
|
+
it("returns a positive number on this host", async () => {
|
|
49
|
+
const mb = await memAvailableMb();
|
|
50
|
+
expect(mb).not.toBeNull();
|
|
51
|
+
expect(mb!).toBeGreaterThan(0);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("spawnGateEnabled", () => {
|
|
56
|
+
it("is false when nothing is configured", () => {
|
|
57
|
+
expect(spawnGateEnabled()).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
it("is true when a cap or floor is set", () => {
|
|
60
|
+
process.env.AGENT_YES_MAX_AGENTS = "5";
|
|
61
|
+
expect(spawnGateEnabled()).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("spawnRejectionReason", () => {
|
|
66
|
+
it("returns null when no limits are configured", async () => {
|
|
67
|
+
expect(await spawnRejectionReason()).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("rejects when live agents reach the cap", async () => {
|
|
71
|
+
writePids([liveRecord(process.pid)]);
|
|
72
|
+
process.env.AGENT_YES_MAX_AGENTS = "1";
|
|
73
|
+
expect(await spawnRejectionReason()).toMatch(/too many agents running \(1\/1\)/);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("admits when live agents are under the cap", async () => {
|
|
77
|
+
writePids([liveRecord(process.pid)]);
|
|
78
|
+
process.env.AGENT_YES_MAX_AGENTS = "5";
|
|
79
|
+
expect(await spawnRejectionReason()).toBeNull();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("ignores dead pids when counting live agents", async () => {
|
|
83
|
+
// pid 2^31-1 is effectively never a live process → not counted.
|
|
84
|
+
writePids([liveRecord(2147483646)]);
|
|
85
|
+
process.env.AGENT_YES_MAX_AGENTS = "1";
|
|
86
|
+
expect(await spawnRejectionReason()).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("rejects when free memory is below the floor", async () => {
|
|
90
|
+
process.env.AGENT_YES_MIN_FREE_MB = String(Number.MAX_SAFE_INTEGER);
|
|
91
|
+
expect(await spawnRejectionReason()).toMatch(/host is low on memory/);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("admits when free memory is above the floor", async () => {
|
|
95
|
+
process.env.AGENT_YES_MIN_FREE_MB = "1";
|
|
96
|
+
expect(await spawnRejectionReason()).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("waitForSpawnCapacity", () => {
|
|
101
|
+
it("returns immediately (no sleep) when the gate is disabled", async () => {
|
|
102
|
+
const sleeps: number[] = [];
|
|
103
|
+
await waitForSpawnCapacity({ sleep: async (ms) => void sleeps.push(ms) });
|
|
104
|
+
expect(sleeps).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("backs off with φ growth and fails open at the deadline", async () => {
|
|
108
|
+
writePids([liveRecord(process.pid)]);
|
|
109
|
+
process.env.AGENT_YES_MAX_AGENTS = "1"; // always over → never admits
|
|
110
|
+
|
|
111
|
+
let t = 0;
|
|
112
|
+
const sleeps: number[] = [];
|
|
113
|
+
let proceededAfter: number | null = null;
|
|
114
|
+
await waitForSpawnCapacity({
|
|
115
|
+
maxWaitMs: 5000,
|
|
116
|
+
now: () => t,
|
|
117
|
+
sleep: async (ms) => {
|
|
118
|
+
sleeps.push(ms);
|
|
119
|
+
t += ms;
|
|
120
|
+
},
|
|
121
|
+
onProceedAnyway: (_r, waited) => {
|
|
122
|
+
proceededAfter = waited;
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// 1000, then ×φ (1618), then clamped to the remaining 2382ms before deadline.
|
|
127
|
+
expect(sleeps[0]).toBe(1000);
|
|
128
|
+
expect(sleeps[1]).toBe(1618);
|
|
129
|
+
expect(sleeps.reduce((a, b) => a + b, 0)).toBe(5000);
|
|
130
|
+
expect(proceededAfter).toBe(5000);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("admits as soon as capacity frees, without proceeding-anyway", async () => {
|
|
134
|
+
writePids([liveRecord(process.pid)]);
|
|
135
|
+
process.env.AGENT_YES_MAX_AGENTS = "1";
|
|
136
|
+
|
|
137
|
+
const sleeps: number[] = [];
|
|
138
|
+
let proceeded = false;
|
|
139
|
+
await waitForSpawnCapacity({
|
|
140
|
+
maxWaitMs: 600_000,
|
|
141
|
+
sleep: async (ms) => {
|
|
142
|
+
sleeps.push(ms);
|
|
143
|
+
// free up capacity after two backoff cycles
|
|
144
|
+
if (sleeps.length >= 2) process.env.AGENT_YES_MAX_AGENTS = "10";
|
|
145
|
+
},
|
|
146
|
+
onProceedAnyway: () => {
|
|
147
|
+
proceeded = true;
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(sleeps.length).toBe(2);
|
|
152
|
+
expect(proceeded).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
package/ts/spawnGate.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spawn admission control — keep an unbounded fan-out of agents from exhausting
|
|
3
|
+
* host RAM and tripping the kernel OOM-killer (which, on a box with no cgroup
|
|
4
|
+
* memory limit, hangs the whole machine). Two opt-in limits:
|
|
5
|
+
* - {@link getMaxAgents} — max concurrently-live agents
|
|
6
|
+
* - {@link getMinFreeMb} — minimum free memory before admitting a spawn
|
|
7
|
+
*
|
|
8
|
+
* Two entry points share one instantaneous check ({@link spawnRejectionReason}):
|
|
9
|
+
* - the `ay serve` daemon HARD-REJECTS (`/api/spawn` → 429) so the caller retries;
|
|
10
|
+
* - the CLI path BLOCKS-AND-WAITS ({@link waitForSpawnCapacity}) with φ-backoff,
|
|
11
|
+
* failing open after a timeout so recursive `ay <cli>` spawns get spaced out
|
|
12
|
+
* (the actual cause of the burst storms) without ever deadlocking a workflow.
|
|
13
|
+
*/
|
|
14
|
+
import { readFile } from "fs/promises";
|
|
15
|
+
import { readGlobalPids } from "./globalPidIndex.ts";
|
|
16
|
+
import { getMaxAgents, getMinFreeMb, getSpawnWaitMs } from "./workspaceConfig.ts";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* System available memory in MB. Prefers Linux `/proc/meminfo` `MemAvailable`
|
|
20
|
+
* (the kernel's own estimate of what's reclaimable without swapping — far more
|
|
21
|
+
* accurate than free RAM), falling back to `os.freemem()` on other platforms or
|
|
22
|
+
* if the file can't be parsed. Returns null when nothing usable is available.
|
|
23
|
+
*/
|
|
24
|
+
export async function memAvailableMb(): Promise<number | null> {
|
|
25
|
+
try {
|
|
26
|
+
const txt = await readFile("/proc/meminfo", "utf-8");
|
|
27
|
+
const m = /^MemAvailable:\s+(\d+)\s*kB/m.exec(txt);
|
|
28
|
+
if (m) return Math.floor(Number(m[1]) / 1024);
|
|
29
|
+
} catch {
|
|
30
|
+
/* not Linux / unreadable — fall through */
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const { freemem } = await import("os");
|
|
34
|
+
return Math.floor(freemem() / (1024 * 1024));
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** True when at least one spawn limit is configured (else the gate is a no-op). */
|
|
41
|
+
export function spawnGateEnabled(): boolean {
|
|
42
|
+
return getMaxAgents() !== undefined || getMinFreeMb() !== undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Instantaneous capacity check. Returns a human-readable reason string when a
|
|
47
|
+
* new spawn should be held back (cap reached / memory too low), or null when
|
|
48
|
+
* there's capacity. Both limits are opt-in — unset means no check.
|
|
49
|
+
*
|
|
50
|
+
* NOTE — the `maxAgents` count is BEST-EFFORT, not a hard barrier: the check is
|
|
51
|
+
* non-atomic (check here → register in `pids.jsonl` later, in another process),
|
|
52
|
+
* so a simultaneous burst can briefly overshoot the cap before the new wrappers
|
|
53
|
+
* appear in the live count. That's acceptable because (a) the CLI path's
|
|
54
|
+
* φ-backoff desynchronizes retries, spreading a burst out, and (b) `minFreeMb`
|
|
55
|
+
* is the HARD OOM guard — it's re-evaluated against live `/proc/meminfo` on every
|
|
56
|
+
* attempt, so once RAM actually drops, further spawns are held regardless of the
|
|
57
|
+
* count. Exact admission would need a cross-process reservation+lock (a TTL'd
|
|
58
|
+
* reservation file); deliberately deferred to avoid stale reservations wedging
|
|
59
|
+
* spawns, which would be worse than a transient overshoot.
|
|
60
|
+
*/
|
|
61
|
+
export async function spawnRejectionReason(): Promise<string | null> {
|
|
62
|
+
const maxAgents = getMaxAgents();
|
|
63
|
+
if (maxAgents !== undefined) {
|
|
64
|
+
// Live agents already running (this wrapper hasn't registered itself yet),
|
|
65
|
+
// so "live >= cap" admits exactly `maxAgents` concurrent agents.
|
|
66
|
+
const live = (await readGlobalPids({ liveOnly: true })).length;
|
|
67
|
+
if (live >= maxAgents)
|
|
68
|
+
// Phrased for the end user who clicked "Spawn" (the console shows this
|
|
69
|
+
// string verbatim), with a host-admin hint appended in parentheses.
|
|
70
|
+
return `too many agents running (${live}/${maxAgents}) — this agent wasn't started. Please wait for one to finish and try again. (host: raise "maxAgents" in ~/.agent-yes/config.json or AGENT_YES_MAX_AGENTS)`;
|
|
71
|
+
}
|
|
72
|
+
const minFreeMb = getMinFreeMb();
|
|
73
|
+
if (minFreeMb !== undefined) {
|
|
74
|
+
const avail = await memAvailableMb();
|
|
75
|
+
if (avail !== null && avail < minFreeMb)
|
|
76
|
+
return `the host is low on memory (${avail}MB free, needs ${minFreeMb}MB) — this agent wasn't started, to avoid crashing the machine. Please try again in a moment. (host: adjust "minFreeMb" in ~/.agent-yes/config.json or AGENT_YES_MIN_FREE_MB)`;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const PHI = 1.618; // golden-ratio backoff base (snomiao global pref)
|
|
82
|
+
const BACKOFF_CAP_MS = 60_000;
|
|
83
|
+
const BACKOFF_BASE_MS = 1_000;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Block until there's spawn capacity, polling with φ-backoff (1s × φⁿ, capped at
|
|
87
|
+
* 60s — there is no OS event for "memory freed", so polling is unavoidable here).
|
|
88
|
+
* Fails open after {@link getSpawnWaitMs} ms (default 10 min): on timeout it logs
|
|
89
|
+
* and proceeds, so a set of mutually-waiting recursive spawns can never deadlock
|
|
90
|
+
* permanently. A no-op (returns immediately) when no limit is configured, so it
|
|
91
|
+
* adds ~zero overhead to the spawn hot path for users who haven't opted in.
|
|
92
|
+
*
|
|
93
|
+
* `sleep`/`now` are injectable for tests. `onWait` fires once per backoff cycle.
|
|
94
|
+
*/
|
|
95
|
+
export async function waitForSpawnCapacity(opts?: {
|
|
96
|
+
maxWaitMs?: number;
|
|
97
|
+
onWait?: (reason: string, waitedMs: number) => void;
|
|
98
|
+
onProceedAnyway?: (reason: string, waitedMs: number) => void;
|
|
99
|
+
sleep?: (ms: number) => Promise<void>;
|
|
100
|
+
now?: () => number;
|
|
101
|
+
}): Promise<void> {
|
|
102
|
+
if (!spawnGateEnabled()) return; // fast path: nothing configured
|
|
103
|
+
const maxWaitMs = opts?.maxWaitMs ?? getSpawnWaitMs();
|
|
104
|
+
// Monotonic clock for the deadline — `Date.now()` can step backward (NTP /
|
|
105
|
+
// suspend) and stretch the fail-open wait far past `maxWaitMs`.
|
|
106
|
+
const now = opts?.now ?? (() => performance.now());
|
|
107
|
+
const sleep = opts?.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
|
|
108
|
+
const start = now();
|
|
109
|
+
let delay = BACKOFF_BASE_MS;
|
|
110
|
+
for (;;) {
|
|
111
|
+
const reason = await spawnRejectionReason();
|
|
112
|
+
if (!reason) return; // capacity available — admit
|
|
113
|
+
const waited = now() - start;
|
|
114
|
+
if (waited >= maxWaitMs) {
|
|
115
|
+
opts?.onProceedAnyway?.(reason, waited);
|
|
116
|
+
return; // fail open — never deadlock a workflow
|
|
117
|
+
}
|
|
118
|
+
opts?.onWait?.(reason, waited);
|
|
119
|
+
// Don't oversleep past the deadline.
|
|
120
|
+
await sleep(Math.min(delay, BACKOFF_CAP_MS, maxWaitMs - waited));
|
|
121
|
+
delay = Math.min(delay * PHI, BACKOFF_CAP_MS);
|
|
122
|
+
}
|
|
123
|
+
}
|
package/ts/subcommands.spec.ts
CHANGED
|
@@ -43,6 +43,18 @@ describe("subcommands.controlCodeFromName", () => {
|
|
|
43
43
|
expect(controlCodeFromName("")).toBe("");
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
+
it("maps navigation keys to their ANSI sequences (for ay key / ay select)", async () => {
|
|
47
|
+
const { controlCodeFromName } = await loadModule();
|
|
48
|
+
expect(controlCodeFromName("up")).toBe("\x1b[A");
|
|
49
|
+
expect(controlCodeFromName("down")).toBe("\x1b[B");
|
|
50
|
+
expect(controlCodeFromName("right")).toBe("\x1b[C");
|
|
51
|
+
expect(controlCodeFromName("left")).toBe("\x1b[D");
|
|
52
|
+
expect(controlCodeFromName("space")).toBe(" ");
|
|
53
|
+
expect(controlCodeFromName("backspace")).toBe("\x7f");
|
|
54
|
+
expect(controlCodeFromName("pageup")).toBe("\x1b[5~");
|
|
55
|
+
expect(controlCodeFromName("pagedown")).toBe("\x1b[6~");
|
|
56
|
+
});
|
|
57
|
+
|
|
46
58
|
it("supports raw:0xNN escape", async () => {
|
|
47
59
|
const { controlCodeFromName } = await loadModule();
|
|
48
60
|
expect(controlCodeFromName("raw:0x03")).toBe("\x03");
|
|
@@ -51,7 +63,27 @@ describe("subcommands.controlCodeFromName", () => {
|
|
|
51
63
|
|
|
52
64
|
it("throws on unknown code names", async () => {
|
|
53
65
|
const { controlCodeFromName } = await loadModule();
|
|
54
|
-
expect(() => controlCodeFromName("nope")).toThrow(/unknown
|
|
66
|
+
expect(() => controlCodeFromName("nope")).toThrow(/unknown key\/code/);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("subcommands.menuSelectKeys (ay select cursor arithmetic)", () => {
|
|
71
|
+
it("sends Downs + Enter to reach an option below the cursor", async () => {
|
|
72
|
+
const { menuSelectKeys } = await loadModule();
|
|
73
|
+
expect(menuSelectKeys(1, 3)).toEqual(["down", "down", "enter"]);
|
|
74
|
+
});
|
|
75
|
+
it("sends Ups + Enter to reach an option above the cursor", async () => {
|
|
76
|
+
const { menuSelectKeys } = await loadModule();
|
|
77
|
+
expect(menuSelectKeys(3, 1)).toEqual(["up", "up", "enter"]);
|
|
78
|
+
});
|
|
79
|
+
it("sends only Enter when the cursor already sits on the target", async () => {
|
|
80
|
+
const { menuSelectKeys } = await loadModule();
|
|
81
|
+
expect(menuSelectKeys(2, 2)).toEqual(["enter"]);
|
|
82
|
+
});
|
|
83
|
+
it("encodes to the exact ANSI byte stream via controlCodeFromName", async () => {
|
|
84
|
+
const { menuSelectKeys, controlCodeFromName } = await loadModule();
|
|
85
|
+
const bytes = menuSelectKeys(2, 4).map((k) => controlCodeFromName(k));
|
|
86
|
+
expect(bytes.join("")).toBe("\x1b[B\x1b[B\r"); // down, down, enter
|
|
55
87
|
});
|
|
56
88
|
});
|
|
57
89
|
|