Skip to content

fix(cli): size the sandbox LLM gateway's memory to its capacity - #5080

Closed
tale-code-bot wants to merge 2 commits into
mainfrom
fix/sandbox-llm-gateway-memory
Closed

tale-code-bot wants to merge 2 commits into
mainfrom
fix/sandbox-llm-gateway-memory

Conversation

@tale-code-bot

@tale-code-bot tale-code-bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Task: TALE-229 on the Development board. The plan is the task's plan comment (Coder run e115d270), and it still holds.

What was wrong

tale deploy gave the sandbox LLM gateway's container a fixed mem_limit: 512m (tools/cli/src/lib/compose/services/create-sandbox-llm-gateway-service.ts), and compose.yml did the same, whatever capacity the deployment allowed. The gateway's memory grows with the agent sessions that run at once. On a deployment with SANDBOX_MAX_SESSIONS=320, the kernel OOM-killed it about every 40 s from about 80 sessions, and agent starts failed while it was down. Its health check passed between the kills, so nothing reported them.

What changed (two commits)

  • fix(cli): size the sandbox LLM gateway's memory to its capacity
    • SANDBOX_LLM_GATEWAY_MEMORY sets the limit when it is present. It takes a size such as 4g or 4096m, parsed the way the spawner parses its own sizes (sizeEnv in services/sandbox/src/config.ts). It must be at least 512m and is emitted as whole MiB.
    • Otherwise the builder derives the limit from SANDBOX_MAX_SESSIONS, parsed the way the spawner's numEnv parses it: 256 MiB plus 12 MiB per session, at least 512 MiB.
      • 8 sessions, the default, keep 512m.
      • 80 sessions get 1216m.
      • 320 sessions get 4096m, the value that fixed the reported deployment.
    • An unset capacity gets 3328m.
      • That is 256 sessions, the most a Docker host sizes itself to. The CLI cannot see the host when it generates the file.
      • The limit is a cap, not a reservation, so a small host pays nothing for the headroom.
    • A malformed value of either variable fails compose generation (tale deploy, tale dev) before any container is recreated.
    • compose.yml now reads mem_limit: ${SANDBOX_LLM_GATEWAY_MEMORY:-3328m}, because compose interpolation cannot derive one variable from another. A parity test pins this default to the builder's default for an unset capacity.
    • .env.example documents the variable.
  • docs: explain how the model gateway's memory scales with sessions
    • EN, DE and FR environment reference:
      • a row for SANDBOX_LLM_GATEWAY_MEMORY;
      • a sizing paragraph and the apply-a-change steps under the capacity section;
      • notes at SANDBOX_MAX_SESSIONS.
    • The recovery steps for a gateway killed at boot now note the container's current limits and restore them. They used to name a fixed 2g and 512m, which would now lower a larger limit.
    • The Kubernetes page (raise the gateway Deployment's limits.memory by the same rule; the manifest keeps 512Mi for 8 sessions), the own-compose page and the gateway's README.
    • The DE and FR "Sandbox agent turns" headings get an explicit {#sandbox-agent-turns} id, because the new links point there. No page linked to the old FR slug.

Why 12 MiB per session.

  • 512 MiB ran out at about 80 sessions, roughly 6.4 MiB per session with the base counted in.
  • The image sets no GOMEMLIMIT, so Go's collector lets the heap grow to about twice what is live.
  • 12 MiB is about twice the observed cost, and it reproduces the 4 GiB that held 320 sessions.
  • The figure rests on one incident; the variable is the escape hatch, and the docs say so.

Why provider concurrency is not in the formula.

  • SANDBOX_LLM_GATEWAY_[CUSTOM_]PROVIDER_CONCURRENCY sizes the worker pool of each provider record.
  • Those pools grow with organizations × models, not with sessions.
  • The docs say to set the variable when the pools are raised or when many organizations' custom models are in use.

Checklist

  • bun run check passes, in CI on this head. In Checks run 40917140820, these lanes pass: Format, Lint, Knip, Type check, Unit (platform 1/2), Unit (platform 2/2) and Unit (workspaces). The workspaces lane's turbo run test covers @tale/cli and @tale/docs, with 16 of 16 tasks successful. The UI and browser lanes skip on pull requests and run in the merge queue. It was not run as one command on the authoring worker (see What was not tested).
  • bun run lint:sast passes, in CI on this head: Opengrep passed in SAST run 40917141031. It was not run locally, because opengrep is not cached on the worker and the pre-commit scan skipped.
  • Translations: N/A. No UI string or message catalog changed.
  • Docs updated in docs/{en,de,fr}/: the environment reference, Kubernetes and own-compose pages, in all three locales.
  • README.md / README.de.md / README.fr.md: N/A. The root READMEs do not cover gateway memory or SANDBOX_MAX_SESSIONS; the gateway's own README (services/sandbox-llm-gateway/README.md) is updated.

How it was tested

Every command ran on the Coder's worker with the GitHub credentials removed from the environment, at head ad9233abd91a9f74204de0706df9e32d3f14baaf, which is the head of this pull request.

  • Regression test against main.
    • bun test src/lib/compose/services/create-sandbox-llm-gateway-service.test.ts src/lib/compose/services/compose-parity.test.ts (in tools/cli): 133 pass, 0 fail.
    • With the builder from main 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2 swapped back in: 127 pass, 6 fail. The failures are the six new tests:
      • derived limits for 80 and 320 sessions;
      • the unset default;
      • the override in MiB;
      • refusal of malformed or too-small sizes;
      • refusal of an invalid capacity;
      • compose.yml parity.
  • Compose. docker compose -f compose.yml config (Compose v5.6.0) resolves the gateway's mem_limit to:
    • unset: 3489660928 bytes (3328 MiB);
    • 4g and 4096m: 4294967296 bytes.
  • Docs.
    • bun run --filter @tale/docs test: 38 files, 716 tests passed.
    • bun run lint:links: every link lands (502 pages).
    • bun run --filter @tale/docs lint: passes.
  • CLI.
    • bun run --filter @tale/cli typecheck passes after bun run --filter @tale/cli generate.
    • bun run --filter @tale/cli lint: passes.
    • Full bun run --filter @tale/cli test: 3827 pass, 35 skip, 9 fail. The nine failures are explained under What was not tested.
  • Repo-wide.
    • oxfmt --check: all 8615 matched files use the correct format.
    • bun run lint:conflicts: passes.
  • Pre-commit hooks. They ran on both commits: conflict markers and oxfmt. Opengrep skipped because it is not cached.
  • CI on this head. Checked once at 17:08 UTC: 23 checks pass, 40 skip, none pending or failing.
    • Checks 40917140820: Format, Lint, Knip, Type check, Unit (platform 1/2), Unit (platform 2/2) and Unit (workspaces). The last ran the full @tale/cli suite and the @tale/docs tests.
    • SAST 40917141031: Opengrep.
    • Commitlint 40917140838: Lint commits.
    • The UI, browser, build and e2e lanes skip on pull requests.

What was not tested

  • The full bun run check, as one command on the authoring worker. The worker fails 9 @tale/cli tests whatever the change, all in files that touch no compose code:

    • The release-snapshot and historical-rebuild tests need git archive --mtime, which the worker's git 2.39.5 lacks.
    • scripts/action-archive.test.ts hits the worker's 512 MiB file-size limit (SIGXFSZ).
    • tests/smoke-fixture.test.ts passes once CLAUDECODE is unset.

    In CI, the full @tale/cli suite passes on this head (Unit (workspaces) above). The other workspaces' lint, typecheck and tests ran only in CI.

  • The UI, browser and e2e lanes. They skip on pull requests and run in the merge queue. This change touches no UI.

  • ruff format --check. uvx is not installed on the worker. No Python file changed.

  • Opengrep, locally. See the checklist.

  • A live deployment. Nobody ran tale deploy against a Docker host or measured the gateway under 80+ sessions.

  • Out of scope, per triage: showing gateway restarts on the Sandboxes page or the health endpoint (the issue's optional item).

  • Possible follow-up: setting GOMEMLIMIT from the container's limit, so the collector respects the cap.

Closes #5074

The gateway's container had a fixed 512m limit whatever capacity the
deployment allowed. Its memory grows with the agent sessions that run at
once, so a deployment with SANDBOX_MAX_SESSIONS=320 had it OOM-killed about
every 40 s from about 80 sessions, and agent starts failed while it was
down.

The compose builder now takes SANDBOX_LLM_GATEWAY_MEMORY when it is set (a
size such as 4g or 4096m, read like the spawner's own sizes, at least
512m) and otherwise gives the gateway 256 MiB plus 12 MiB for each session
of SANDBOX_MAX_SESSIONS, at least 512m: 512m for the default 8 sessions,
4096m for 320. An unset capacity counts as the 256 sessions a Docker host
may size itself to (3328m), which compose.yml uses as its default because
compose cannot derive one variable from another. A malformed value fails
the compose generation, before any container is recreated.
The environment reference documents SANDBOX_LLM_GATEWAY_MEMORY and the
rule `tale deploy` sizes the gateway by, and the capacity guidance says
when to set it: with the shipped compose files above 256 sessions, and on
Kubernetes by raising the gateway Deployment's limit. The recovery steps
for a gateway killed at boot note the container's current limits and
restore them instead of naming a fixed 512m and 2g, which would now lower
a larger limit. The Kubernetes and own-compose pages and the gateway's
README say the same, in English, German and French.

@tale-review-bot tale-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at head ad9233abd91a9f74204de0706df9e32d3f14baaf (base main 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2).

Scope. The PR does what TALE-229's triage and plan asked, and nothing else:

  • The gateway's mem_limit is SANDBOX_LLM_GATEWAY_MEMORY when set; otherwise 256 MiB plus 12 MiB per session of SANDBOX_MAX_SESSIONS, at least 512m.
  • An unset capacity gives 3328m, sized for the spawner's 256-session ceiling.
  • compose.yml defaults to the same value, and a parity test holds the two together.
  • .env.example, the gateway README and the EN/DE/FR environment reference, Kubernetes and own-compose pages are swept in. DE and FR carry the same figures as EN.
  • Restart visibility stays out of scope, as the triage set it.

Correctness.

  • Inputs: loadEnv folds the project .env into process.env before compose generation, as the other builders rely on. The size grammar is the spawner's sizeEnv regex, byte for byte. sessionCapacity mirrors numEnv('SANDBOX_MAX_SESSIONS', 8, { min: 1 }), rounding up, and AUTO_MAX_SESSIONS = 256 matches the unset default.
  • No swap conflict: neither the builder nor compose.yml sets memswap_limit, so a larger mem_limit doesn't collide with a fixed swap.
  • Recovery steps: they now note and restore the container's own limits instead of the old 512m/1g, which would have lowered a derived limit.

What I ran at this head (credentials removed; the install's lockfile and manifests are unchanged from main):

  • bun test on the builder and parity test files: 133 pass, 0 fail.
  • The same two files against main's builder: 127 pass, 6 fail, all six of them new. The seventh new test, the 8-session 512m control, passes on both.

Checks. All seven CI ready checks passed on this head: Checks 40917140820, SAST 40917141031, Commitlint 40917140838, CLI 40917140990, Build 40917140874, E2E 40917140735 and Security 40917140573. The Checks run includes Format (lint:links), Type check, Knip and Unit (workspaces), which runs the full @tale/cli and @tale/docs suites. There are no review threads.

Not run: a live tale deploy with 80 or more sessions, and the queue-only tier, which the merge queue runs.

Non-blocking:

  • The 12 MiB per session rests on one incident. The override variable, which the docs name, is the escape hatch.
  • Setting GOMEMLIMIT from the limit is a possible follow-up.

@tale-review-bot
tale-review-bot added this pull request to the merge queue Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 29, 2026
@tale-review-bot
tale-review-bot added this pull request to the merge queue Oct 29, 2026
@yannickmonney
yannickmonney removed this pull request from the merge queue due to a manual request Oct 29, 2026
@yannickmonney yannickmonney reopened this Oct 29, 2026
@tale-review-bot

Copy link
Copy Markdown
Collaborator

Not landed yet: its CI must re-run under the new landing rule. (Reviewer tale-review-bot, run 1f1d8573, 20:27 UTC.)

  • What changed. main no longer lands through a merge queue: commit 0da08205, and since about 20:25 UTC the ruleset holds only the seven required CI ready (…) contexts. This PR left the queue without merging. It is not merged, and auto-merge is off.
  • The new rule. .agents/repo.md ("Each PR lands on its own") and .github/CI.md#landing-on-main say a PR's own CI runs every suite its paths owe, the heavy ones included, and the PR squash-merges once the seven contexts pass on its head.
  • Why I'm not landing it now. The seven green contexts on the approved head ad9233abd91a9f74204de0706df9e32d3f14baaf ran under the pre-0da08205 workflows. Those skipped Build, E2E, CLI, UI, Browser, Performance and backend integration on pull requests and left them to the queue, which never ran them for this PR. Enabling auto-merge now would merge it at once without those suites. A skipped check is not a pass, so I am not landing it on that evidence.
  • Next. Re-run this PR's CI on the same head; any new pull-request run uses the new workflows. Once all seven CI ready contexts pass on that run, the Reviewer lands it with gh pr merge 5080 --repo tale-project/tale --squash --auto --match-head-commit ad9233abd91a9f74204de0706df9e32d3f14baaf. The approval stands while the head is unchanged.

@tale-review-bot

Copy link
Copy Markdown
Collaborator

Reviewer: the CI on this head since the 20:13 UTC reopen can't be used to land it (head ad9233abd91a9f74204de0706df9e32d3f14baaf, still the approved head).

It ran the workflows from before 0da08205. Its checkout merged this head into the old base 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2 (job 123705524985: HEAD is now at 840feb4 Merge ad9233abd91a9f74204de0706df9e32d3f14baaf into 9be5cadac…). As a result:

  • E2E's PR scope logged e2e runs only in the merge queue; this pull request skips it (job 123705521249).
  • These runs skipped every heavy job:
    • Checks 40947184859: UI shards, UI, Browser, Build, Performance and Backend integration;
    • Build 40947184905: all 13 jobs;
    • CLI 40947184781: all 5 jobs;
    • E2E 40947184761: all of its Playwright and bundle jobs.

Without a merge queue, those suites never run before landing. So these runs' CI ready contexts don't show what the contract now requires: "A pull request runs every suite its paths owe, the heavy ones included" (.github/CI.md, Landing on main). I won't enable auto-merge on them, even once they are green. The SAST, Commitlint and Security workflows didn't change in 0da08205.

Next. This PR's merge ref is now based on 0da08205 (merge commit d548c4f4f4f8). A new pull_request run on the same head, for example closing and reopening the PR as at 20:13, would run the current workflows. Once that run's seven CI ready contexts pass on ad9233abd91a9f74204de0706df9e32d3f14baaf, the Reviewer lands it with gh pr merge 5080 --repo tale-project/tale --squash --auto --match-head-commit ad9233abd91a9f74204de0706df9e32d3f14baaf.

@yannickmonney

yannickmonney commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Reopened so the checks run again. The 20:13–20:15 UTC reopen started its runs from a test merge into 9be5cada (before 0da08205), because GitHub had not refreshed it yet. This reopen's runs start from the test merge fd07716cb4, into 0da08205. The head is unchanged (no new commit).

@yannickmonney

Copy link
Copy Markdown
Contributor

Landed on train as c96e897: squashed at the approved head after the fast gate (formatting, typecheck, build). It reaches main with the merge train's pull request, so this pull request closes without merging.

yannickmonney pushed a commit that referenced this pull request Oct 30, 2026
Reviewed and approved in pull request #5080 at ad9233a.

Closes #5074
Task: TALE-229
@yannickmonney
yannickmonney deleted the fix/sandbox-llm-gateway-memory branch October 30, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(cli): the sandbox LLM gateway's fixed 512m limit OOMs at about 80 agent sessions

3 participants