Files
loki/scripts/test/agent-session-limit.ts
CatoandClaude Opus 5.5 43e1782b22 fix(builder): five agent sessions at once, not eight; idle sealed sessions can be stopped to free memory (#1687)
* docs(handoff): #1678 merged; the box still drops requests and Loki's deploys do not land

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* docs(handoff): memory ceiling set at 10 GiB; Gemini spending cap in OrangeCat #1672

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): five agent sessions at once, not eight; idle sealed sessions can be stopped to free memory

Measured on the box on 2026-11-12: a sealed session holds about 1 GiB, not
the 0.4 GB the cap of eight was sized at. With seven or eight open the
builder held 6.3 to 8.3 GiB, the box had 0.6 GiB left, swap was full, and
the kernel had stopped 37 processes for memory, live sites and the gateway
among them.

- AGENT_MAX_OPEN_DEFAULT is 5. LOKI_AGENT_MAX_OPEN raises it where there is
  room.
- sealed-builds-ctl.sh stop-idle (sealed-builds.yml): stops the sealed
  sessions that relayed less than 512 bytes over two minutes, the line the
  idle rule already uses for "no work". A working agent prints far more
  (its spinner alone). runs=dry lists without stopping. idle_sessions is
  pinned by scripts/hetzner/test-sealed-builds-memory.sh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(spend): the platform's Gemini key is not used where nothing caps it

The audit after the Gemini spend (2026-11-12) found the same platform key in
three places with no cap that refuses a call:

- Loki's own chat chain called Google whenever GEMINI_API_KEY was in its
  env. Google is left out of CHAT_CHAIN (UNCAPPED_PAID_PROVIDERS) until Loki
  has a cap that refuses, like OrangeCat's reserve_vendor_call.
- install-builder-agents.sh copied OrangeCat's key into the builder's env
  for the Antigravity CLI. An agent CLI cannot be capped in code; the
  builder now uses Gemini only with a key of its own, from a Google project
  with a per-day request quota.
- openclaw-media.yml fell back to OrangeCat's key for the Telegram gateway's
  image and video reading. No fallback: with no key of its own, media
  understanding stays off.

Keys already copied on the box stop mattering when the platform key is
replaced; the new key is never copied anywhere.

scripts/test/chat-chain.ts pins all three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): stop-idle stops only sessions whose project has no open run

Bytes are not the signal: on 2026-11-12 eight sealed sessions at their
prompt each relayed 3 to 10 KB a minute through podman, so a 512-byte line
would have stopped none, and an agent waiting on CI prints nothing at all.
A session whose project (or lane) has no open run in Loki's database is
between tasks; one that does is kept, and so is any session under fifteen
minutes old. between_tasks is pinned by test-sealed-builds-memory.sh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): between_tasks reads its list without a pipe into grep -q

scripts/test/no-grep-q-in-a-pipe.ts: grep -q exits at the first match and
can cut off the writer. A here-string has no writer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* docs(handoff): the box recovered; spend caps; the owner's key steps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 16:39:19 +01:00

45 lines
2.0 KiB
TypeScript

// The cloud builder keeps a limited number of agent sessions open
// (lib/agent-session-limit): 27 at once took every site down on 2026-11-11.
import assert from "node:assert/strict";
import {
AGENT_MAX_OPEN_DEFAULT,
agentLimitRefusal,
agentMaxOpen,
openAgentCount,
} from "../../src/lib/agent-session-limit";
import { shellTabFromHex } from "../../src/lib/shell-session";
const shell = shellTabFromHex("deadbeef");
const tabs = (n: number): string[] => Array.from({ length: n }, (_, i) => `project-${i}`);
assert.equal(agentMaxOpen({}), AGENT_MAX_OPEN_DEFAULT);
// About 1 GiB a sealed session on a 15 GiB box shared with the sites.
assert.ok(AGENT_MAX_OPEN_DEFAULT <= 5, "the default leaves the sites their memory");
assert.equal(agentMaxOpen({ LOKI_AGENT_MAX_OPEN: "4" }), 4);
assert.equal(agentMaxOpen({ LOKI_AGENT_MAX_OPEN: "0" }), AGENT_MAX_OPEN_DEFAULT);
assert.equal(agentMaxOpen({ LOKI_AGENT_MAX_OPEN: "many" }), AGENT_MAX_OPEN_DEFAULT);
// Shells are counted by their own cap, not this one.
assert.equal(openAgentCount([...tabs(2), shell]), 2);
// Room left: allowed.
assert.equal(agentLimitRefusal({ channel: "cloud", tab: "new", openTabs: tabs(7), max: 8 }), null);
// Full: refused, in words that say what to do.
const refusal = agentLimitRefusal({ channel: "cloud", tab: "new", openTabs: tabs(8), max: 8 });
assert.match(refusal ?? "", /^8 agent sessions are already open on the cloud builder/);
assert.match(refusal ?? "", /Close one/);
// Full of shells is not full of agents.
assert.equal(
agentLimitRefusal({ channel: "cloud", tab: "new", openTabs: [...tabs(7), shell], max: 8 }),
null,
);
// A tab that is already open is re-attached, not a ninth session.
assert.equal(
agentLimitRefusal({ channel: "cloud", tab: "Project-3", openTabs: tabs(8), max: 8 }),
null,
);
// A person's own computer is not limited.
assert.equal(agentLimitRefusal({ channel: "local", tab: "new", openTabs: tabs(30), max: 8 }), null);
assert.equal(agentLimitRefusal({ channel: null, tab: "new", openTabs: tabs(30), max: 8 }), null);
console.log("✓ agent-session-limit");