Skip to content

fix(provisioner): state-sync silently failing — Python NameError on JSON booleans - #284

Closed
samxu01 wants to merge 1 commit into
mainfrom
fix/state-sync-python-json-literal
Closed

samxu01 wants to merge 1 commit into
mainfrom
fix/state-sync-python-json-literal

Conversation

@samxu01

@samxu01 samxu01 commented May 3, 2026

Copy link
Copy Markdown
Contributor

Root cause

Every `syncAccountToStateMoltbot` call from the JS provisioner has been silently failing since `2a4bc1314d` (2026-05-02). Result: the gateway PVC `/state/moltbot.json` ignored every config update — including the new `defaults.model.primary = nemotron` that should have stopped the Codex burn at the source.

The bug:
```ts
`agents_defaults = ${JSON.stringify(agentsDefaults)}`, // emits Python source
```
produces lowercase `true` / `false` / `null` — valid JSON, not valid Python (Python wants `True`/`False`/`None`). The first embedded boolean (`memorySearch.enabled: true`) raised `NameError: name 'true' is not defined`, python exited 1, the kubelet returned "command terminated with non-zero exit code", and `execInPod` rejected without surfacing the python stderr that explained why.

Why it took so long to find

`execInPod` only included the kubelet's generic message in the rejected error. Python's NameError sat in the captured stderr buffer but was thrown away. So the user saw 250+ "Failed to sync account" log lines with no clue what python actually did. Hour-long debugging chased every wrong direction (websocket flake, race conditions, set-eu) before noticing the lowercase booleans.

Fix

  1. Double-encode JSON through `JSON.stringify` — the result is a valid Python string literal, parsed at runtime via `json.loads`. Works for booleans, null, nested objects, unicode without manual escaping.
  2. Include captured stderr in `execInPod` error messages. Truncate to last 2KB. Next time a python-in-a-pod script breaks, the actual error will show up in backend logs instead of being silently swallowed.

Test plan

  • Deploy Dev runs green
  • After deploy: trigger `reprovision-all`. Backend logs show `[k8s-provisioner] synced X to /state/moltbot.json` for every account, zero "Failed to sync account" lines.
  • PVC `/state/moltbot.json`:
    • `agents.defaults.model.primary == openrouter/nvidia/nemotron-3-super-120b-a12b:free` (currently sticky from manual patch — this PR proves it lands automatically)
    • `models.providers.openrouter` populated
    • No trinity references anywhere
  • Trigger a community-agent heartbeat (fakesam/liz/x-curator). LiteLLM logs show nemotron call succeeding. Codex weekly counters do not advance.

🤖 Generated with Claude Code

…ds, not Python literals

Root cause for the silent state-sync failures during reprovision-all:
JSON.stringify(...) interpolated directly into the python heredoc produced
lowercase `true` / `false` / `null` — valid JSON but **not valid Python**
literals (Python wants `True` / `False` / `None`). The first embedded
boolean (e.g. `agents_defaults.memorySearch.enabled: true`) raised
NameError, python exited 1, and every state-sync since 2a4bc13 failed
silently. Result: the gateway PVC `/state/moltbot.json` never got the new
`defaults.model.primary = nemotron`, never got the openrouter provider
catalog, and community agents kept routing to the stale gpt-5.4-mini
default — burning Codex.

Fix: double-encode through JSON.stringify so each embedded value becomes
a valid Python string literal, and parse it at runtime via `json.loads`.
This works for any JSON-shaped payload (booleans, null, nested objects,
unicode) without manual escaping.

Also: include captured stderr in the execInPod error message. Kubelet's
"command terminated with non-zero exit code" alone is useless; the python
NameError was sitting in stderr the whole time and was hidden by the
wrapper. With this change, the next instance of the same class of bug
will be visible from the first failed call.

After this PR + Deploy Dev + reprovision-all, the gateway PVC will reflect
the in-code defaults (nemotron primary, OR provider with nemotron-only
catalog) on every account sync.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@samxu01

samxu01 commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

Squash-merged locally to preserve Sam Xu attribution. Closing.

samxu01 added a commit that referenced this pull request May 3, 2026
…SON booleans (#284)

JSON.stringify(...) interpolated into the python heredoc produced lowercase
true/false/null — valid JSON but invalid Python. The first embedded boolean
(agents_defaults.memorySearch.enabled: true) raised NameError, python exited
1, every state-sync silently failed since 2a4bc13. Result: gateway PVC
ignored config updates including the nemotron default that should have
stopped the Codex burn.

Two changes:
1. Double-encode each embedded value through JSON.stringify so it becomes a
   valid Python string literal, parse at runtime via json.loads.
2. Include captured stderr in execInPod error messages — the python stderr
   was the smoking gun all along but was being thrown away.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@samxu01 samxu01 closed this May 3, 2026
@samxu01
samxu01 deleted the fix/state-sync-python-json-literal branch May 3, 2026 22:46
samxu01 added a commit that referenced this pull request May 4, 2026
… clarified (#289)

Update Agent Runtime quick-rule for community/dev model routing to match
the post-burn-fix state (PRs #282, #284, #285):

- Drop trinity reference (model dead at OpenRouter)
- Add aria to dev-agent list
- Note hard guardrail in applyOpenClawModelDefaults blocking codex from
  community fallback chain
- Document GEMINI_API_KEY's wrong-project status (946211286881) so
  future debugging doesn't waste time
- Document new retry policy: num_retries=1 + retry_after=1 lets
  codex-auth-rotator swap auth.json before retry reads it

Pure docs change, no test deltas.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant