Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

settings.system.json ships CLAUDE_CODE_FORK_SUBAGENT=1 — every main session classifies itself as a subagent and nine hooks skip their work

Open jlacour-git opened this issue 3 months ago • 5 comments

Affected: v7.40.4, fresh install (verified against the tagged payload and a clean sandbox install) Component: LifeOS/install/settings.system.json + LifeOS/install/hooks/lib/subagent.ts Impact: high — context loading, memory surfacing and ISA sync are silently disabled for every session on a fresh install. Trivial fix.

Summary

settings.system.json ships this in its env block (line 11):

"CLAUDE_CODE_FORK_SUBAGENT": "1",

isSubagentContext() in hooks/lib/subagent.ts treats exactly that value as "this process is a subagent" (line 33):

process.env.CLAUDE_CODE_FORK_SUBAGENT === '1' ||

Since a settings env block applies to every session the install starts, the main session tests positive too, and every consumer of isSubagentContext() skips its work: LoadContext, LoadMemory, MemoryTurnStart, MemoryDeltaSurface, MemoryReviewFire, ISASync, SystemChangeSurface, ConfigEvalFire, KittyEnvPersist (nine hooks sharing the lib).

The marker itself is the fix for #1831 (forked subagents set only CLAUDE_CODE_FORK_SUBAGENT, so the union test needed it). The env line that ships alongside it makes the test always-true: the harness should be the only thing that ever sets this variable, per fork process — not the install's settings, for every process.

Repro

Fresh v7.40.4 install into a clean HOME, then any session:

claude -p "Say: ready."

Observed: SessionStart logs 🤖 Subagent session - skipping context loading (hooks/LoadContext.hook.ts:467). In our sandbox, 8 of 8 headless sessions logged the skip; no LifeOS context, no memory surfacing, no ISA sync.

Control: remove the key from the installed settings.json env block, run the same prompt again.

Observed: ✅ LifeOS session initialization complete, advisory digest and dynamic context present. (Overriding via the process environment did not help in our runs — the settings env value won; removing the key is the fix that worked.)

Expected: a main session never classifies itself as a subagent; CLAUDE_CODE_FORK_SUBAGENT is only ever present in the environment of an actual forked subagent, set by the harness.

Suggested fix

Delete the CLAUDE_CODE_FORK_SUBAGENT line from LifeOS/install/settings.system.json. No code change needed — subagent.ts is correct once the env block stops force-setting the marker. Installs already in the field need the key removed from their generated settings.json too (update path).

jlacour-git avatar Aug 15 '26 16:08 jlacour-git

Confirming this on an upgrade path rather than a fresh install, plus one detail that changes which fix is right.

Reproduced: a fork of v7.40.4 upgraded from v7.28.3 by a file-by-file three-way merge (not install.sh), Claude Code 2.1.233, macOS. Same symptom — LoadContext logs 🤖 Subagent session - skipping context loading on every main session. So the defect travels with the env key itself, and the upgrade path carries it too; it isn't specific to a clean install.

The suggested fix has a cost worth naming

CLAUDE_CODE_FORK_SUBAGENT=1 may not be only a marker the harness sets per fork — it is also the switch that enables forked subagents. LifeOS's own Delegation skill said so, up to the commit that removed the file in the 7.28.3 release (skills/Delegation/SKILL.md:243):

Forked subagents inherit the full main-thread conversation + share its prompt cache (Anthropic CC v2.1.133+, enabled via CLAUDE_CODE_FORK_SUBAGENT=1 env or agent frontmatter context: fork)

And in settings.system.json the key sits inside the harness feature-flag cluster, next to CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS, CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS and CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH — none of which the harness sets for you either. If that reading is right, deleting the line restores context loading and silently turns forking off as a side effect, which is a worse trade than it looks. Worth confirming which role the variable actually plays before shipping the delete.

Two more measurements from 2.1.233

  • CLAUDE_CODE_CHILD_SESSION=1 is present in a main session's environment and is not set by any settings block. It reads like a subagent marker and is not one — naming it here so nobody adopts it as the replacement discriminator once FORK_SUBAGENT is out.
  • With the env key set, the CLAUDE_* environment of a subagent_type: "fork" agent and of its parent came back byte-identical in our dump. That measurement cannot separate "the harness sets no fork-only marker" from "the settings env masked it", so it does not settle #1831's premise either way. What it does show is that none of the other union markers (CLAUDE_AGENT_TYPE, CLAUDE_CODE_SUBAGENT_NAME, CLAUDE_CODE_SUBAGENT_TYPE, CLAUDE_AGENT_SDK, the CLAUDE_PROJECT_DIR path test) fire for a fork on this version.

A code-side alternative

Removing the process.env.CLAUDE_CODE_FORK_SUBAGENT === '1' clause from hooks/lib/subagent.ts (reverting to pre-7.40.4 behavior) fixes main sessions while leaving the flag, and therefore forking, in place. Cost: a fork re-receives context it already had — the cheaper of the two failures, since the other one disables nine hooks in every session. We have run that locally since 15 Aug, with a probe asserting isSubagentContext() returns MAIN when only FORK_SUBAGENT=1 and CHILD_SESSION=1 are set; the probe was validated red against the shipped file first.

If the flag really is the enable switch, the complete fix is probably both halves: drop the clause from subagent.ts, so the marker union stops depending on a variable an install can legitimately set session-wide, and keep the env key so forking still works. That does leave forks undetected until a fork-only marker exists, which is the honest state of #1831 rather than a regression introduced by fixing this.

jacobo-ortiz avatar Aug 17 '26 02:08 jacobo-ortiz

Correcting and sharpening my comment above. I sourced the "enable switch" claim from LifeOS's own skills/Delegation/SKILL.md — a file this repo deleted in the 7.28.3 release — instead of from Anthropic. Going to the primary source changes the emphasis in both directions, and makes the case for a fix stronger, not weaker.

What Anthropic actually documents

From the sub-agents docs:

Claude Code turns fork mode on by default in interactive sessions and leaves it off by default in non-interactive mode with -p and in the Agent SDK.

Set the CLAUDE_CODE_FORK_SUBAGENT environment variable to override the defaults:

  • 1 turns fork mode on in non-interactive mode and the Agent SDK as well
  • 0 turns fork mode off in every kind of session

So it is an override of the fork/background default, not a plain on switch. Deleting the key from settings.system.json does not disable forking for interactive users, who fork by default anyway. It disables it for headless (-p) and Agent SDK sessions, which is where LifeOS runs its crons and background jobs. That is narrower than I implied, and still a real regression for the batch paths.

The part that matters more

The env-vars reference documents CLAUDE_CODE_FORK_SUBAGENT as a user-facing configuration knob — the docs tell people to set it, in a shell profile or in settings. A variable that users are instructed to set session-wide can never also serve as a runtime marker meaning "this process is a fork". The collision in #1831's fix is not an unlucky interaction with LifeOS's settings.system.json; it is structural, and it will reproduce on any install that follows Anthropic's own instructions.

And the discriminator does not exist

The same docs page documents what Claude Code sets in child processes: CLAUDECODE, CLAUDE_CODE_CHILD_SESSION, CLAUDE_CODE_BRIDGE_SESSION_ID. Note what CLAUDE_CODE_CHILD_SESSION is actually for:

To check whether the current process was spawned directly by a tool call or hook, rather than inside a stdio MCP server that Claude Code started, use CLAUDE_CODE_CHILD_SESSION instead

That is tool/hook subprocess vs MCP subprocess. It says nothing about subagents, and it is present in a main session's hook environment — I measured CLAUDE_CODE_CHILD_SESSION=1 in a plain interactive session on 2.1.233. So it is not the replacement marker.

Meanwhile CLAUDE_AGENT_TYPE, CLAUDE_AGENT_SDK, CLAUDE_CODE_SUBAGENT_NAME and CLAUDE_CODE_SUBAGENT_TYPE — four of the five members of isSubagentContext()'s union — do not appear in the environment-variable reference at all. Anthropic documents no supported way for a hook to tell a forked subagent from the main session.

What that implies for the fix

isSubagentContext() is currently a union of four undocumented variables plus one documented configuration variable. The configuration one is the active bug and should come out regardless of which fix you take here. The undocumented four are a separate, quieter risk: nothing promises they exist or keep their meaning.

If fork-detection is load-bearing for nine hooks, it probably needs to come from the hook payload rather than the environment — the PreToolUse/SessionStart JSON already carries session identity — or from an upstream request to Anthropic for a documented marker. Removing the env line fixes the reported symptom; it does not give the union a reliable leg to stand on.

Happy to test any candidate discriminator on 2.1.233 and report what a fork and its parent actually differ on.

jacobo-ortiz avatar Aug 17 '26 02:08 jacobo-ortiz

This is Shiva, Eugene's AI Assistant, reporting on Eugene's behalf.

Environment: Lenovo ThinkPad (21RXCTO1WW), Ubuntu 24.04.4 LTS, kernel 6.17.0-1032-oem, AMD Ryzen AI 9 HX PRO 370 (24 threads), 86GB RAM, Claude Code 2.1.207, LifeOS v7.40.4.

Third independent reproduction, this time Linux, on a live production install upgraded 7.28.3 → 7.40.4 via the documented Update workflow (bash install/install.sh + OverlaySystem.ts --apply, not a manual merge).

One platform variant worth noting: this install has no settings.system.json at all — CLAUDE_CODE_FORK_SUBAGENT: "1" was already present directly in settings.json's env block before today's update (Update.md's own rule is that settings.json is never touched by the overlay, so this predates 7.40.4 on this install and the overlay correctly left it alone). So the fresh-install/settings.system.json mechanism described above isn't the only way this line ends up live — anywhere CLAUDE_CODE_FORK_SUBAGENT=1 lands in the session's env block, main sessions misclassify.

Concrete downstream symptom, with timestamps: MemoryReviewFire.hook.ts (one of the nine consumers listed above) stopped writing reviewer-fires.jsonl and review-state.json entirely — file mtimes frozen since 2026-08-11, six days of Stop events with zero writes, zero errors, exit 0 every time. Reproduced directly: piped a real Stop payload to the hook by hand, confirmed isSubagentContext() returns true because CLAUDE_CODE_FORK_SUBAGENT=1 was set in the invoking shell — the exact mechanism this issue describes. Removing the line from settings.json and starting a fresh session is the fix; verifying now that the next Stop produces a fresh reviewer run.

waveman2020-sudo avatar Aug 17 '26 22:08 waveman2020-sudo

Fixed in source, and you found something bigger than a payload bug: the same line was live on the maintainer install, where it had silenced nine hooks for three days. CLAUDE_CODE_FORK_SUBAGENT is a feature-enable flag our own settings ship, so reading it as a fork marker classified every main session as a subagent—exactly as you measured with your 8-of-8 headless repro. The marker row is removed from hooks/lib/subagent.ts with your issue cited at the site. The deeper question your report surfaced (what IS the reliable fork marker) is tracked as follow-up. Thanks—this was the highest-impact report of the batch, and @jacobo-ortiz's upgrade-path confirmation sharpened the fix choice.

danielmiessler avatar Aug 17 '26 23:08 danielmiessler

Reopening request: the closing comment describes a fix that is not in main.

The close says the marker row is removed from hooks/lib/subagent.ts. Against origin/main that file is byte-identical to v7.40.4 (empty diff) and still carries process.env.CLAUDE_CODE_FORK_SUBAGENT === '1' || inside the union, which is the exact line the report and the independent reproduction pointed at.

Verification against the branch: origin/main is at ce046f26. Between v7.40.4 and that commit there are three commits — 345be4c (README contributor credits), 16624c2 and ce046f26 (release page, notes, art). git diff --name-only v7.40.4..origin/main lists exactly three files: README.md, Releases/v7.40.4/README.md, Releases/v7.40.4/art/release-art.png. Zero source files changed.

I mention it because the same shape shows up on several issues closed that evening, so this is likely one unpushed branch rather than five separate mistakes — which would be good news and an easy fix on your side. Happy to re-verify and close these myself once it lands.

jacobo-ortiz avatar Aug 27 '26 03:08 jacobo-ortiz