Skip to content

Commit 20bff59

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(desktop): track concurrent agent turns up to the harness maximum (#2882)
Desktop's active-turn store capped tracked concurrent turns per agent at `4` while the harness runs `DEFAULT_AGENT_PARALLELISM = 24` parallel agent subprocesses and accepts up to `32` (`--agents` / `BUZZ_ACP_AGENTS`, `value_parser range(1..=32)`). Turns above the cap were silently evicted, so a genuinely-running turn lost its working badge in the sidebar and the agents popover. The eviction also caused the badge set to rotate indefinitely. Evicted turns are still alive, so their hosts keep emitting `turn_liveness` every 10s; `recordActivity` can't find the evicted turn, `resurrectTurn` recreates it, and that eviction drops one of the surviving turns. With two live turns above the cap the visible set churned every 10 seconds. `MAX_TURNS_PER_AGENT` exists only to bound map growth, so it now sits at the harness's hard upper bound of `32` — unreachable for any legitimately-configured agent while still keeping the per-agent map bounded. `MAX_TERMINAL_TOMBSTONES` derives from it (`* 4`), so the tombstone cap moves from 16 to 128. Two regressions cover the reported symptom: a default-parallelism agent working in 24 channels keeps all 24 badges, and the tracked channel set stays stable as `turn_liveness` arrives for turns that previously would have been evicted. Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
1 parent c0d7b52 commit 20bff59

2 files changed

Lines changed: 88 additions & 20 deletions

File tree

‎desktop/src/features/agents/activeAgentTurnsStore.test.mjs‎

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,88 @@ describe("activeAgentTurnsStore", () => {
154154
});
155155

156156
describe("eviction at MAX_TURNS_PER_AGENT", () => {
157-
it("evicts oldest turn when exceeding 4 concurrent turns", () => {
157+
// Mirrors the store's private cap: the harness's hard upper bound on
158+
// parallel agent subprocesses (`--agents` accepts 1..=32).
159+
const CAP = 32;
160+
/** Desktop's default harness parallelism (DEFAULT_AGENT_PARALLELISM). */
161+
const DEFAULT_PARALLELISM = 24;
162+
const EPOCH = Date.parse("2024-01-01T00:00:00Z");
163+
const at = (ms) => new Date(EPOCH + ms).toISOString();
164+
165+
/** One turn_started per channel, minute apart so start order is unambiguous. */
166+
function startTurns(count, firstSeq = 1) {
158167
const events = [];
159-
for (let i = 1; i <= 5; i++) {
168+
for (let i = 1; i <= count; i++) {
160169
events.push(
161170
makeEvent({
162-
seq: i,
171+
seq: firstSeq + i - 1,
163172
turnId: `t${i}`,
164173
channelId: `c${i}`,
165-
timestamp: `2024-01-01T00:0${i}:00Z`,
174+
timestamp: at(i * 60_000),
166175
}),
167176
);
168177
}
169-
syncAgentTurnsFromEvents(AGENT, events);
178+
return events;
179+
}
180+
181+
it("evicts oldest turn when exceeding the cap", () => {
182+
syncAgentTurnsFromEvents(AGENT, startTurns(CAP + 1));
170183
const channels = channelIdsOf(getActiveTurnsForAgent(AGENT));
171-
// Should have evicted c1 (oldest) to make room for c5
172-
assert.equal(channels.size, 4);
184+
// c1 (oldest) evicted to make room for the 33rd turn.
185+
assert.equal(channels.size, CAP);
173186
assert.ok(!channels.has("c1"), "oldest turn should be evicted");
174187
assert.ok(channels.has("c2"));
175-
assert.ok(channels.has("c5"));
188+
assert.ok(channels.has(`c${CAP + 1}`));
189+
});
190+
191+
it("tracks every turn of a default-parallelism agent working in 24 channels", () => {
192+
// DEFAULT_AGENT_PARALLELISM is 24, so a single agent legitimately runs 24
193+
// concurrent turns. Every one must keep its working badge.
194+
syncAgentTurnsFromEvents(AGENT, startTurns(DEFAULT_PARALLELISM));
195+
const channels = channelIdsOf(getActiveTurnsForAgent(AGENT));
196+
assert.equal(
197+
channels.size,
198+
DEFAULT_PARALLELISM,
199+
"all 24 concurrently-worked channels must be tracked",
200+
);
201+
for (let i = 1; i <= DEFAULT_PARALLELISM; i++) {
202+
assert.ok(channels.has(`c${i}`), `c${i} must be tracked`);
203+
}
204+
});
205+
206+
it("keeps the tracked channel set stable as liveness arrives for every turn", () => {
207+
// The flicker: with the cap below real parallelism, turns above it are
208+
// evicted while still alive, and their 10s turn_liveness frames land on
209+
// resurrectTurn — which evicts one of the survivors to make room. The set
210+
// then rotates forever. Under a cap at the harness maximum, liveness for
211+
// any live turn is a plain refresh and the set never moves.
212+
const TURNS = 6;
213+
syncAgentTurnsFromEvents(AGENT, startTurns(TURNS));
214+
const expected = channelIdsOf(getActiveTurnsForAgent(AGENT));
215+
216+
// Liveness for the two earliest-started turns — the first to be evicted
217+
// under the old cap, hence the first to trigger a resurrection swap.
218+
for (const [i, turnId] of ["t1", "t2"].entries()) {
219+
syncAgentTurnsFromEvents(AGENT, [
220+
makeEvent({
221+
seq: TURNS + i + 1,
222+
kind: "turn_liveness",
223+
turnId,
224+
channelId: turnId.replace("t", "c"),
225+
timestamp: at((TURNS + i + 1) * 60_000),
226+
}),
227+
]);
228+
assert.deepEqual(
229+
[...channelIdsOf(getActiveTurnsForAgent(AGENT))].sort(),
230+
[...expected].sort(),
231+
`liveness for ${turnId} must not change the tracked channel set`,
232+
);
233+
}
234+
assert.equal(
235+
expected.size,
236+
TURNS,
237+
"all six live turns must be tracked, not a rotating subset",
238+
);
176239
});
177240
});
178241

@@ -1343,16 +1406,17 @@ describe("activeAgentTurnsStore", () => {
13431406
});
13441407

13451408
it("evicts the oldest tombstone once past the cap so the map stays bounded", () => {
1346-
// The tombstone map is capped at MAX_TERMINAL_TOMBSTONES (16). Complete
1347-
// 18 distinct turns so eviction fires twice, dropping the two oldest by
1348-
// insertion order (t0, t1). Probe via the ONE behavior a tombstone gates
1349-
// that a strictly-newer frame cannot mask: an EQUAL-timestamp liveness
1350-
// (frameAt == terminalAt). All completions share timestamp T with rising
1351-
// seq, so the probe clears the per-agent watermark on the seq tiebreak
1352-
// (compareObserverEvents is timestamp-primary, seq-secondary) yet stays
1353-
// equal to the recorded terminal — reaching resurrectTurn's tombstone
1354-
// check rather than being shadowed by the watermark.
1355-
const CAP = 16;
1409+
// The tombstone map is capped at MAX_TERMINAL_TOMBSTONES (MAX_TURNS_PER_AGENT
1410+
// * 4 = 128). Complete 130 distinct turns so eviction fires twice, dropping
1411+
// the two oldest by insertion order (t0, t1). Probe via the ONE behavior a
1412+
// tombstone gates that a strictly-newer frame cannot mask: an
1413+
// EQUAL-timestamp liveness (frameAt == terminalAt). All completions share
1414+
// timestamp T with rising seq, so the probe clears the per-agent watermark
1415+
// on the seq tiebreak (compareObserverEvents is timestamp-primary,
1416+
// seq-secondary) yet stays equal to the recorded terminal — reaching
1417+
// resurrectTurn's tombstone check rather than being shadowed by the
1418+
// watermark.
1419+
const CAP = 128;
13561420
const TOTAL = CAP + 2;
13571421
const T = at(0);
13581422
const completions = [];

‎desktop/src/features/agents/activeAgentTurnsStore.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ const REMOVE_AFTER_MS = LIVENESS_INTERVAL_MS * 2.5;
2323
const FRAME_GAP_PAUSE_MS = LIVENESS_INTERVAL_MS * 2;
2424
/** A silent agent is treated as dead after this bounded prune pause. */
2525
const PRUNE_PAUSE_MAX_MS = 3 * 60_000;
26-
/** Maximum concurrent active turns tracked per agent (matches pool size). */
27-
const MAX_TURNS_PER_AGENT = 4;
26+
/** Maximum concurrent active turns tracked per agent. Purely an unbounded-growth
27+
* guard, so it sits at the harness's hard upper bound for parallel agent
28+
* subprocesses (`--agents` / `BUZZ_ACP_AGENTS` accepts `1..=32`) rather than the
29+
* Desktop default of 24 — any lower value silently evicts a live turn, dropping
30+
* its working badge. */
31+
const MAX_TURNS_PER_AGENT = 32;
2832
/** Cap on per-agent terminal tombstones (A's resurrection guard). Only the
2933
* most recently completed turns can be raced by a late liveness frame; older
3034
* ones are already below the watermark, so a small multiple of the live cap is

0 commit comments

Comments
 (0)