openclaw - 💡(How to fix) Fix [Bug]: post-compaction context injection uses process.cwd() instead of run.workspaceDir (agent-runner.runtime line 3312) [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#70541Fetched 2026-04-24 05:56:42
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

readPostCompactionContext is called with process.cwd() instead of the run's configured workspaceDir inside the auto-compaction post-hook for follow-up runs (agent-runner.runtime-CH0aH7T6.js line 3312). For agents that run via a CLI provider subprocess (e.g. openai-codex), process.cwd() inside the subprocess is typically $HOME, so the gateway repeatedly tries to read $HOME/AGENTS.md and logs Warning: Could not read $HOME/AGENTS.md: Error: EISDIR... (or ENOENT depending on what happens to be there).

Error Message

readPostCompactionContext is called with process.cwd() instead of the run's configured workspaceDir inside the auto-compaction post-hook for follow-up runs (agent-runner.runtime-CH0aH7T6.js line 3312). For agents that run via a CLI provider subprocess (e.g. openai-codex), process.cwd() inside the subprocess is typically $HOME, so the gateway repeatedly tries to read $HOME/AGENTS.md and logs Warning: Could not read $HOME/AGENTS.md: Error: EISDIR... (or ENOENT depending on what happens to be there). Low severity — the warning is non-fatal because the downstream .catch(() => {}) swallows the error, so no user-facing breakage. But:

Root Cause

Not reproducible with provider runtimes that don't spawn a CLI subprocess with $HOME as cwd (e.g. Anthropic, Gemini direct), because those go through the other code path that uses the configured workspaceDir.

Code Example

if (sessionKey) readPostCompactionContext(process.cwd(), cfg).then((contextContent) => {
    if (contextContent) enqueueSystemEvent(contextContent, { sessionKey });
}).catch(() => {});

---

const refreshPrompt = await readPostCompactionContext(params.followupRun.run.workspaceDir, params.cfg);

---

"agents": {
     "defaults": {
       "heartbeat": { "model": "openai-codex/gpt-5.4", "every": "30m" }
     }
   }

---

- if (sessionKey) readPostCompactionContext(process.cwd(), cfg).then((contextContent) => {
+ if (sessionKey) readPostCompactionContext(followupRun.run.workspaceDir ?? process.cwd(), cfg).then((contextContent) => {
RAW_BUFFERClick to expand / collapse

Summary

readPostCompactionContext is called with process.cwd() instead of the run's configured workspaceDir inside the auto-compaction post-hook for follow-up runs (agent-runner.runtime-CH0aH7T6.js line 3312). For agents that run via a CLI provider subprocess (e.g. openai-codex), process.cwd() inside the subprocess is typically $HOME, so the gateway repeatedly tries to read $HOME/AGENTS.md and logs Warning: Could not read $HOME/AGENTS.md: Error: EISDIR... (or ENOENT depending on what happens to be there).

Location

/opt/homebrew/lib/node_modules/openclaw/dist/agent-runner.runtime-CH0aH7T6.js (v2026.4.14), line 3312:

if (sessionKey) readPostCompactionContext(process.cwd(), cfg).then((contextContent) => {
    if (contextContent) enqueueSystemEvent(contextContent, { sessionKey });
}).catch(() => {});

A few hundred lines earlier, line 1409 in the same file does the correct thing:

const refreshPrompt = await readPostCompactionContext(params.followupRun.run.workspaceDir, params.cfg);

followupRun.run.workspaceDir is in scope at line 3312 too — line 3290 already uses followupRun.run.sessionId. So the two call sites are inconsistent for no clear reason.

Reproduce

  1. Configure a default heartbeat in openclaw.json:
    "agents": {
      "defaults": {
        "heartbeat": { "model": "openai-codex/gpt-5.4", "every": "30m" }
      }
    }
  2. Let the heartbeat fire a few times. When auto-compaction triggers on the heartbeat follow-up run and the Codex CLI subprocess's cwd is $HOME, the gateway starts logging Warning: Could not read $HOME/AGENTS.md on every occurrence.
  3. The problem is especially visible if something happens to exist at $HOME/AGENTS.md (directory or file). If nothing is there, the warning is still emitted (noisy but benign).

Not reproducible with provider runtimes that don't spawn a CLI subprocess with $HOME as cwd (e.g. Anthropic, Gemini direct), because those go through the other code path that uses the configured workspaceDir.

Expected

Post-compaction context injection should load AGENTS.md from the agent's configured workspaceDir — the same file the pre-compaction refresh reads — not from whatever cwd the runner subprocess happens to have.

Suggested fix

One-line change to line 3312:

- if (sessionKey) readPostCompactionContext(process.cwd(), cfg).then((contextContent) => {
+ if (sessionKey) readPostCompactionContext(followupRun.run.workspaceDir ?? process.cwd(), cfg).then((contextContent) => {

The ?? process.cwd() fallback keeps existing behavior as a defensive default if a caller ever omits workspaceDir. Alternatively, match line 1409 exactly and drop the fallback.

Impact

Low severity — the warning is non-fatal because the downstream .catch(() => {}) swallows the error, so no user-facing breakage. But:

  • Log noise: ~48 warnings/day per affected agent (every 30 min heartbeat tick)
  • Post-compaction context injection does not fire for affected agents when the warning triggers, so the agent loses the intended "re-read Session Startup / Red Lines" nudge after compaction — which is exactly the scenario the feature was designed to cover
  • Obscures the useful signal in gateway.err.log when actually investigating a problem

Environment

  • OpenClaw 2026.4.14
  • macOS 26.3.0 / Apple Silicon
  • Heartbeat provider: openai-codex/gpt-5.4
  • Agent without explicit workspace override (inherits agents.defaults.workspace)

Related / not-dupes

  • #37398 (closed) — "Inject configurable workspace files into post-compaction context" — adjacent feature, not this bug
  • #25392 (closed) — "Default AGENTS.md template headings mismatch compaction code" — different bug in the same area
  • #25969 (closed) — "support after_compaction internal hooks + followup turn" — the PR that likely introduced this code path

Happy to submit a PR if helpful.

extent analysis

TL;DR

The issue can be fixed by changing the readPostCompactionContext call to use the followupRun.run.workspaceDir instead of process.cwd().

Guidance

  • The problem is caused by the inconsistency in using process.cwd() and followupRun.run.workspaceDir in the readPostCompactionContext calls.
  • To fix this, update the readPostCompactionContext call at line 3312 to use followupRun.run.workspaceDir as suggested in the issue.
  • Verify that the fix works by checking the logs for the absence of the Warning: Could not read $HOME/AGENTS.md message.
  • Test the fix by running the heartbeat a few times and checking that the post-compaction context injection works as expected.

Example

The suggested fix is to change the line 3312 to:

- if (sessionKey) readPostCompactionContext(process.cwd(), cfg).then((contextContent) => {
+ if (sessionKey) readPostCompactionContext(followupRun.run.workspaceDir ?? process.cwd(), cfg).then((contextContent) => {

Alternatively, match line 1409 exactly and drop the fallback:

if (sessionKey) readPostCompactionContext(followupRun.run.workspaceDir, cfg).then((contextContent) => {

Notes

The issue is specific to agents that run via a CLI provider subprocess, such as openai-codex, and may not affect other providers.

Recommendation

Apply the suggested workaround by changing the readPostCompactionContext call to use followupRun.run.workspaceDir. This fix is low-risk and should resolve the log noise and post-compaction context injection issues.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug]: post-compaction context injection uses process.cwd() instead of run.workspaceDir (agent-runner.runtime line 3312) [1 participants]