fix(openclaw): keep the Python launcher fallback off the working directory (#4843)

## Description

`ProxyManager.buildLaunchSpecs()`
(`plugins/openclaw/src/proxy-manager.ts`) has a Python fallback: it
probes with `python -c "...find_spec('headroom')..."` and starts the
proxy with `python -m headroom.cli proxy ...`. Both put the current
directory first on `sys.path`, and the spawned process inherits
OpenClaw's working directory, so a `headroom/` folder there was found
and run instead of the installed package.

Both the probe and the launch now pass `-P` (Python 3.11+: do not
prepend the cwd to `sys.path`). Interpreters older than 3.11 reject
`-P`, so their probe fails and the launcher moves on; `headroom-ai`
requires 3.11+ so they could not hold a real install. `-P` rather than
`-I` keeps `PYTHONPATH` and user-site installs working.

Companion to the same change in
`plugins/headroom-agent-hooks/bin/headroom-hook.sh` (#4842); kept
separate because this plugin has its own test suite.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `plugins/openclaw/src/proxy-manager.ts`: `-P` on the configured-python
and fallback-python launch and probe args.
- `plugins/openclaw/test/proxy-manager.test.ts`: updated expected args;
new test that runs each Python spec's probe with a real Python 3.11+
(site-packages disabled) from a directory containing a `headroom/`
package and expects "not found".

## Testing

- [x] Unit tests pass (`vitest`)
- [ ] Linting passes (`ruff check .`) (no Python changed)
- [x] Type checking passes (`npm run typecheck`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
# new test on origin/main (fix stashed), python3 = 3.12
$ npx vitest run test/proxy-manager.test.ts -t "working directory"
AssertionError: expected +0 to be 1 // Object.is equality
      Tests  1 failed | 34 skipped (35)

# with the fix
$ npx vitest run test/proxy-manager.test.ts
      Tests  35 passed (35)
$ npx vitest run
      Tests  2 failed | 161 passed (163)
# the 2 failures are test/advancement-key-store.test.ts "...two separate OS processes..."; they fail identically on origin/main (2 failed | 160 passed) and are unrelated.
$ npm run typecheck
(no output, exit 0)
```

## Real Behavior Proof

- Environment: macOS, Node + vitest from the plugin's lockfile, Python
3.12.
- Exact command / steps: in a temp dir containing
`headroom/__init__.py`, run each Python launch spec's probe as `python3
-S <checkArgs>` (`-S` so no installed headroom can be seen).
- Observed result: main exits 0 (the folder in the cwd is accepted as
headroom); this branch exits 1 (not found), so the launcher would move
to the next spec.
- Not tested: Windows `py` launcher (it forwards `-P` to the
interpreter).

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: the Python fallback only starts an
installed headroom; a source checkout reachable only through the cwd is
no longer picked up (set `pythonPath`/`PYTHONPATH`, or put `headroom` on
PATH).
- Kill switch / disable path: a `headroom` executable on PATH or an
explicit `proxyUrl` bypasses the fallback.
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this PR

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

## Additional Notes

No docs change needed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4843"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

Reported by @JoaoMarcos44. Thank you for your contribution! 🩵

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
Tejas Chopra
2026-11-17 19:08:42 +05:30
committed by GitHub
co-authored by Claude Opus 5.5 JerrettDavis
parent 18067b1d19
commit acebf20b47
2 changed files with 41 additions and 5 deletions
+10 -4
View File
@@ -57,6 +57,12 @@ interface LaunchSpec {
const HEADROOM_MODULE_DISCOVERY_SNIPPET =
"import importlib.util, sys; sys.exit(0 if importlib.util.find_spec('headroom') else 1)";
// `python -m` / `python -c` put the current directory first on sys.path, so a
// `headroom/` folder in whatever directory OpenClaw runs from would be used
// instead of the installed package. -P (Python 3.11+, which headroom-ai
// requires) leaves it off; older interpreters reject -P and are skipped.
const PYTHON_SAFE_PATH_FLAG = "-P";
export class ProxyManager {
private config: ProxyManagerConfig;
private logger: ProxyManagerLogger;
@@ -230,9 +236,9 @@ export class ProxyManager {
specs.push({
label: `Configured Python: ${configuredPython} -m headroom.cli`,
command: configuredPython,
args: ["-m", "headroom.cli", ...commonArgs],
args: [PYTHON_SAFE_PATH_FLAG, "-m", "headroom.cli", ...commonArgs],
checkCommand: configuredPython,
checkArgs: ["-c", HEADROOM_MODULE_DISCOVERY_SNIPPET],
checkArgs: [PYTHON_SAFE_PATH_FLAG, "-c", HEADROOM_MODULE_DISCOVERY_SNIPPET],
});
}
@@ -325,9 +331,9 @@ export class ProxyManager {
specs.push({
label: `Python: ${pyCmd} -m headroom.cli`,
command: pyCmd,
args: ["-m", "headroom.cli", ...commonArgs],
args: [PYTHON_SAFE_PATH_FLAG, "-m", "headroom.cli", ...commonArgs],
checkCommand: pyCmd,
checkArgs: ["-c", HEADROOM_MODULE_DISCOVERY_SNIPPET],
checkArgs: [PYTHON_SAFE_PATH_FLAG, "-c", HEADROOM_MODULE_DISCOVERY_SNIPPET],
});
}
+31 -1
View File
@@ -5,6 +5,10 @@ import {
isLocalProxyUrl,
probeHeadroomProxy,
} from "../src/proxy-manager.js";
import { spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
const retrieveStatsBody = JSON.stringify({ store: { entry_count: 0 }, recent_retrievals: [] });
const proxyStatsBody = JSON.stringify({ proxy_inbound: { total: 1 } });
@@ -347,7 +351,7 @@ describe("ProxyManager launch internals", () => {
expect(specs[0]?.label).toContain("Configured Python:");
expect(specs[0]?.command).toBe("C:\\Python311\\python.exe");
expect(specs[0]?.args).toEqual(["-m", "headroom.cli", "proxy", "--host", "127.0.0.1", "--port", "8787"]);
expect(specs[0]?.args).toEqual(["-P", "-m", "headroom.cli", "proxy", "--host", "127.0.0.1", "--port", "8787"]);
});
it("uses lightweight PATH checks instead of booting the headroom CLI", () => {
@@ -407,11 +411,37 @@ describe("ProxyManager launch internals", () => {
expect(pythonSpec).toBeDefined();
expect(pythonSpec?.checkArgs).toEqual([
"-P",
"-c",
"import importlib.util, sys; sys.exit(0 if importlib.util.find_spec('headroom') else 1)",
]);
});
it("never resolves headroom from the working directory in python fallbacks", () => {
const manager = new ProxyManager({ pythonPath: "/opt/py/bin/python3" });
const specs = (manager as any).buildLaunchSpecs("127.0.0.1", "8787") as Array<Record<string, any>>;
const pythonSpecs = specs.filter((spec) => spec.args?.includes("headroom.cli"));
expect(pythonSpecs.length).toBeGreaterThan(1);
const probe = spawnSync("python3", ["-c", "import sys; sys.exit(sys.version_info < (3, 11))"]);
const project = mkdtempSync(join(tmpdir(), "openclaw-cwd-"));
try {
mkdirSync(join(project, "headroom"));
writeFileSync(join(project, "headroom", "__init__.py"), "");
for (const spec of pythonSpecs) {
if (probe.status === 0) {
// -S and -E hide any installed or PYTHONPATH headroom, so only the
// project folder could match.
const check = spawnSync("python3", ["-S", "-E", ...spec.checkArgs], { cwd: project });
expect(check.status).toBe(1);
}
expect(spec.args[0]).toBe("-P");
}
} finally {
rmSync(project, { recursive: true, force: true });
}
});
it("uses first available launcher from provided specs", async () => {
const manager = new ProxyManager({});
(manager as any).buildLaunchSpecs = () => [