lazycodex icon indicating copy to clipboard operation
lazycodex copied to clipboard

Bound Codex bootstrap thread limits for token control

Open johnnyisme opened this issue 4 months ago • 2 comments

Problem Situation

Codex token optimization lowered local agent thread limits to 4, but LazyCodex/OMO bootstrap provisioning could restore high defaults. The shipped OMO bundles still set CODEX_SUBAGENT_THREAD_LIMIT = 1000 and CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 16, so a session-start/worker provisioning pass can undo local token-control settings.

Reproduction Logs

RED regression before the fix, from a fresh code-yeongyu/lazycodex worktree at 895b70c:

node --test test/codex-token-thread-limits.test.mjs
fails because shipped bundles do not contain `var CODEX_SUBAGENT_THREAD_LIMIT = 4;` or `var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 4;`.

Manual smoke before completing the fix also showed the bootstrap worker lowered [agents].max_threads but left existing [features.multi_agent_v2].max_concurrent_threads_per_session = 16, because the helper returned early when that setting already existed.

Root Cause

The generated OMO bootstrap/install helper has two token-cost problems:

  • It ships high concurrency defaults: CODEX_SUBAGENT_THREAD_LIMIT = 1000 and CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 16.
  • It skips rewriting features.multi_agent_v2.max_concurrent_threads_per_session when the setting already exists, so existing installs can remain at 16 even after lowering the constant.

Verified Fix

Lower both shipped defaults to 4, remove the early return so existing features.multi_agent_v2.max_concurrent_threads_per_session values are replaced with the bounded value, and add a Node test that runs the bootstrap worker against a temporary Codex home with 1000/16 configured.

diff --git a/plugins/omo/components/bootstrap/dist/cli.js b/plugins/omo/components/bootstrap/dist/cli.js
index 43fc26a..6fa7ac1 100755
--- a/plugins/omo/components/bootstrap/dist/cli.js
+++ b/plugins/omo/components/bootstrap/dist/cli.js
@@ -3006,8 +3006,8 @@ import { dirname as dirname7, isAbsolute as isAbsolute5, join as join17 } from "
 var CODEX_AGENTS_HEADER = "agents";
 var CODEX_MULTI_AGENT_V2_HEADER = "features.multi_agent_v2";
 var CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY = `${CODEX_MULTI_AGENT_V2_HEADER}.max_concurrent_threads_per_session`;
-var CODEX_SUBAGENT_THREAD_LIMIT = 1000;
-var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 16;
+var CODEX_SUBAGENT_THREAD_LIMIT = 4;
+var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 4;
 function ensureCodexMultiAgentV2Config(config, options = {}) {
   const featureFlag = removeFeatureFlagSetting(config, "multi_agent_v2");
   const v2Preferred = options.multiAgentVersion === "v2";
@@ -3015,8 +3015,6 @@ function ensureCodexMultiAgentV2Config(config, options = {}) {
   const agentsConfig = v2Preferred ? removeAgentsMaxThreads(featureFlag.config) : modelKnown ? ensureAgentsMaxThreads(featureFlag.config) : raiseExistingAgentsMaxThreads(featureFlag.config);
   const preserveDisable = featureFlag.value === false && !v2Preferred;
   const featureConfig = preserveDisable ? setMultiAgentV2Disable(agentsConfig) : v2Preferred ? removeMultiAgentV2Disable(agentsConfig) : agentsConfig;
-  if (hasTomlSetting(featureConfig, CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY))
-    return featureConfig;
   const section = findTomlSection(featureConfig, CODEX_MULTI_AGENT_V2_HEADER);
   if (!section) {
     const enabledSetting = preserveDisable ? `enabled = false
diff --git a/plugins/omo/dist/cli-node/index.js b/plugins/omo/dist/cli-node/index.js
index c41f475..e971294 100755
--- a/plugins/omo/dist/cli-node/index.js
+++ b/plugins/omo/dist/cli-node/index.js
@@ -98168,8 +98168,8 @@ import { dirname as dirname14, isAbsolute as isAbsolute7, join as join38 } from
 var CODEX_AGENTS_HEADER = "agents";
 var CODEX_MULTI_AGENT_V2_HEADER = "features.multi_agent_v2";
 var CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY = `${CODEX_MULTI_AGENT_V2_HEADER}.max_concurrent_threads_per_session`;
-var CODEX_SUBAGENT_THREAD_LIMIT = 1000;
-var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 16;
+var CODEX_SUBAGENT_THREAD_LIMIT = 4;
+var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 4;
 function ensureCodexMultiAgentV2Config(config3, options = {}) {
   const featureFlag = removeFeatureFlagSetting(config3, "multi_agent_v2");
   const v2Preferred = options.multiAgentVersion === "v2";
@@ -98177,8 +98177,6 @@ function ensureCodexMultiAgentV2Config(config3, options = {}) {
   const agentsConfig = v2Preferred ? removeAgentsMaxThreads(featureFlag.config) : modelKnown ? ensureAgentsMaxThreads(featureFlag.config) : raiseExistingAgentsMaxThreads(featureFlag.config);
   const preserveDisable = featureFlag.value === false && !v2Preferred;
   const featureConfig = preserveDisable ? setMultiAgentV2Disable(agentsConfig) : v2Preferred ? removeMultiAgentV2Disable(agentsConfig) : agentsConfig;
-  if (hasTomlSetting(featureConfig, CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY))
-    return featureConfig;
   const section = findTomlSection(featureConfig, CODEX_MULTI_AGENT_V2_HEADER);
   if (!section) {
     const enabledSetting = preserveDisable ? `enabled = false
diff --git a/plugins/omo/dist/cli/index.js b/plugins/omo/dist/cli/index.js
index a2e6bbb..a128de5 100755
--- a/plugins/omo/dist/cli/index.js
+++ b/plugins/omo/dist/cli/index.js
@@ -98112,8 +98112,8 @@ import { dirname as dirname14, isAbsolute as isAbsolute7, join as join38 } from
 var CODEX_AGENTS_HEADER = "agents";
 var CODEX_MULTI_AGENT_V2_HEADER = "features.multi_agent_v2";
 var CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY = `${CODEX_MULTI_AGENT_V2_HEADER}.max_concurrent_threads_per_session`;
-var CODEX_SUBAGENT_THREAD_LIMIT = 1000;
-var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 16;
+var CODEX_SUBAGENT_THREAD_LIMIT = 4;
+var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 4;
 function ensureCodexMultiAgentV2Config(config3, options = {}) {
   const featureFlag = removeFeatureFlagSetting(config3, "multi_agent_v2");
   const v2Preferred = options.multiAgentVersion === "v2";
@@ -98121,8 +98121,6 @@ function ensureCodexMultiAgentV2Config(config3, options = {}) {
   const agentsConfig = v2Preferred ? removeAgentsMaxThreads(featureFlag.config) : modelKnown ? ensureAgentsMaxThreads(featureFlag.config) : raiseExistingAgentsMaxThreads(featureFlag.config);
   const preserveDisable = featureFlag.value === false && !v2Preferred;
   const featureConfig = preserveDisable ? setMultiAgentV2Disable(agentsConfig) : v2Preferred ? removeMultiAgentV2Disable(agentsConfig) : agentsConfig;
-  if (hasTomlSetting(featureConfig, CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY))
-    return featureConfig;
   const section = findTomlSection(featureConfig, CODEX_MULTI_AGENT_V2_HEADER);
   if (!section) {
     const enabledSetting = preserveDisable ? `enabled = false
diff --git a/test/codex-token-thread-limits.test.mjs b/test/codex-token-thread-limits.test.mjs
new file mode 100644
index 0000000..a13311b
--- /dev/null
+++ b/test/codex-token-thread-limits.test.mjs
@@ -0,0 +1,71 @@
+import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
+import assert from "node:assert/strict";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { spawnSync } from "node:child_process";
+import { describe, test } from "node:test";
+
+const repoRoot = new URL("..", import.meta.url).pathname;
+
+const shippedBundles = [
+	"plugins/omo/components/bootstrap/dist/cli.js",
+	"plugins/omo/dist/cli/index.js",
+	"plugins/omo/dist/cli-node/index.js",
+];
+
+describe("Codex token-control thread limits", () => {
+	for (const relativePath of shippedBundles) {
+		test(`${relativePath} keeps bootstrap thread limits bounded`, () => {
+			const source = readFileSync(join(repoRoot, relativePath), "utf8");
+
+			assert.match(source, /var CODEX_SUBAGENT_THREAD_LIMIT = 4;/);
+			assert.match(source, /var CODEX_MULTI_AGENT_V2_THREAD_LIMIT = 4;/);
+		});
+	}
+
+	test("bootstrap worker lowers existing Codex thread limits", () => {
+		const smokeRoot = mkdtempSync(join(tmpdir(), "lazycodex-thread-limit-"));
+		const codexHome = join(smokeRoot, "codex");
+		const home = join(smokeRoot, "home");
+		mkdirSync(codexHome);
+		mkdirSync(home);
+		writeFileSync(
+			join(codexHome, "config.toml"),
+			[
+				'model = "gpt-5.5"',
+				'model_provider = "ly-chatai"',
+				"",
+				"[features]",
+				"multi_agent = true",
+				"",
+				"[agents]",
+				"enabled = true",
+				"max_threads = 1000",
+				"",
+				"[features.multi_agent_v2]",
+				"max_concurrent_threads_per_session = 16",
+				"",
+			].join("\n"),
+		);
+
+		const result = spawnSync(
+			process.execPath,
+			[
+				join(repoRoot, "plugins/omo/components/bootstrap/dist/cli.js"),
+				"worker",
+				"--codex-home",
+				codexHome,
+				"--once",
+			],
+			{
+				encoding: "utf8",
+				env: { ...process.env, HOME: home },
+			},
+		);
+
+		assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
+		const config = readFileSync(join(codexHome, "config.toml"), "utf8");
+		assert.match(config, /^max_threads = 4$/m);
+		assert.match(config, /^max_concurrent_threads_per_session = 4$/m);
+	});
+});

Verification

  • RED: node --test test/codex-token-thread-limits.test.mjs failed before the fix because all three shipped bundles still used high thread limits.
  • GREEN focused: node --test test/codex-token-thread-limits.test.mjs -> 4 tests passed.
  • GREEN suite: npm test -> 13 tests passed.
  • Manual QA: node plugins/omo/components/bootstrap/dist/cli.js worker --codex-home <temp-codex-home> --once rewrote a temporary config from agents.max_threads = 1000 and features.multi_agent_v2.max_concurrent_threads_per_session = 16 to 4 and 4.

This fix was debugged, implemented, and verified with LazyCodex. Tag: lazycodex-generated

johnnyisme avatar Jul 29 '26 04:07 johnnyisme

The unsafe limit is still shipping in 4.19.4 and was active during two real runaway incidents

This issue must not be treated as a theoretical token-optimization request. The current released plugin still carries the unsafe configuration, and that configuration was active during repeated production incidents involving catastrophic agent multiplication and rapid quota loss. The evidence establishes an unsafe contributing condition; it does not claim that the plugin was the sole cause of every runtime behavior.

Released artifact and live configuration

Installed OMO/LazyCodex plugin: 4.19.4.

The installed source still contains:

const CODEX_SUBAGENT_THREAD_LIMIT = "1000";

and the migration still preserves an existing MultiAgentV2 limit instead of lowering it:

if (hasTomlSetting(config, CODEX_MULTI_AGENT_V2_THREAD_LIMIT_KEY)) return config;

The current upstream code-yeongyu/lazycodex HEAD (10f95587d3aeacf208cc1fee88a91315962d31e8) still contains both behaviors in plugins/omo/scripts/migrate-codex-config/subagent-limit-guard.mjs.

More importantly, the live Codex configuration regenerated on 2026-09-02 21:13 KST contains:

[features.multi_agent_v2]
max_concurrent_threads_per_session = 1000

[agents]
max_threads = 1000

The configuration even carries the “Managed by LazyCodex” marker. A supposed safety/migration layer is therefore leaving the effective blast radius at one thousand agents.

Verified real-world damage

The local Codex state database provides authoritative parent/child edges. Sanitized reconstruction shows:

  • On 2026-08-29 22:47–23:36 KST, one bounded content-research worker created only four direct children, but one of those children recursively created 1,088 descendants.
  • The full subtree reached 1,218 unique descendants, maximum depth 14, in under 50 minutes.
  • That subtree records approximately 5.03 billion tokens across the root and descendants. This is local recorded-token accounting, not a claim that every recorded token was billed.
  • The user contemporaneously experienced the UI/aggregate proliferation as more than 1,500 agents or execution threads. Because that exact UI count was not preserved, this is reported as a user observation; the independently persisted minimum is 1,218 descendants in one bounded event and 1,400 execution threads across the two bounded incident trees described here.
  • A second user-launched Local Work session on 2026-09-02 produced 182 execution threads total (one root plus 181 descendants) at depth 8 from only 12 direct children.
  • During the September burst, the root's seven-day usage indicator moved from 46% to 54% in approximately 11 minutes 43 seconds. This is UI rate-limit evidence; it is separate from duplicated local token telemetry.
  • The account's weekly-usage display fell by more than 50 percentage points during one of the runaway events, according to the user's contemporaneous observation. The user had to stop the work and reboot the PC to prevent further consumption.

This happened twice. There was no automatic circuit breaker, no meaningful warning before the quota damage, and no automatic usage restoration or compensation path for the paid Pro allowance.

Why the proposed fix in this issue is not delivered

The issue body already proposes the correct direction: lower the shipped limits to 4 and rewrite unsafe existing values. Yet the released 4.19.4 artifacts and current repository HEAD still ship 1000, and existing max_concurrent_threads_per_session values are still preserved. A “verified fix” that is not merged, released, and migrated on existing installations is not a fix for users.

Required action

This needs an urgent safety release:

  1. Replace the 1000 defaults with a conservative bounded value.
  2. Never raise a user's lower configured cap.
  3. Rewrite known-unsafe legacy values (1000, and the old high V2 defaults) downward during migration, with an explicit notice.
  4. Default to depth 1: workers must not create workers unless the user explicitly opts in.
  5. Add a parent-wide descendant budget and a hard circuit breaker for spawn count, depth, token growth, and repeated review/test loops.
  6. Expose the effective limit, its configuration source, active descendants, and accumulated subagent usage in the UI.
  7. Publish a remediation command for already affected installations and identify the first safe release.

This is a production-safety defect. Shipping a limit of one thousand while marketing autonomous multi-agent work transfers an unreasonable and hidden financial risk to the user. Until a bounded release and migration exist, the affected configuration should be treated as unsafe by default.


This issue or PR was generated by LazyCodex. Tag: lazycodex-generated

doormatwin-svg avatar Sep 02 '26 13:09 doormatwin-svg

[sisyphus-bot] Thanks for the report! 🙏

I'm closing this because the behaviour it describes is already fixed: the bundles no longer set any thread cap — CODEX_SUBAGENT_THREAD_LIMIT was removed entirely and the guard never inserts or raises a limit, so local token-control settings survive a bootstrap pass.

How I checked: git grep -c CODEX_SUBAGENT_THREAD_LIMIT origin/main → 0 matches; subagent-limit-guard.mjs line 14: "Never insert or raise either cap."

If you can still reproduce this, please leave a comment — I'm happy to reopen right away. Thanks again for taking the time to file it! 🙇

code-yeongyu avatar Sep 21 '26 03:09 code-yeongyu