[object Object]

← back to Uspto Trtyrap

Fix bg-job state parser (double-decode openclaw output)

fd59c5cecc85eb96d98342020aebf918b86e12c3 · 2026-08-06 16:02:12 -0700 · Steve

Files touched

Diff

commit fd59c5cecc85eb96d98342020aebf918b86e12c3
Author: Steve <steve@designerwallcoverings.com>
Date:   Thu Aug 6 16:02:12 2026 -0700

    Fix bg-job state parser (double-decode openclaw output)
---
 parse_state.py       | 15 +++++++++++++++
 poll-and-download.sh |  8 +++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/parse_state.py b/parse_state.py
new file mode 100644
index 0000000..27aa409
--- /dev/null
+++ b/parse_state.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+# Reads openclaw evaluate output (often double-JSON-encoded) from stdin,
+# prints "STATE\tKEY" (KEY empty if none). Robust to quoting.
+import json, sys
+raw = sys.stdin.read().strip()
+o = {}
+try:
+    v = json.loads(raw)
+    if isinstance(v, str):
+        v = json.loads(v)
+    if isinstance(v, dict):
+        o = v
+except Exception:
+    pass
+print(f"{o.get('state','PARSE_ERR')}\t{o.get('key','')}")
diff --git a/poll-and-download.sh b/poll-and-download.sh
index e62cbbf..803d58b 100755
--- a/poll-and-download.sh
+++ b/poll-and-download.sh
@@ -38,14 +38,16 @@ click_generate(){
 log "=== poll-and-download started (pid $$) ==="
 for i in $(seq 1 $MAX); do
   raw=$(extract_state)
-  state=$(echo "$raw" | sed -E 's/.*"state":"([^"]*)".*/\1/')
+  parsed=$(printf '%s' "$raw" | python3 parse_state.py)
+  state=${parsed%%$'\t'*}; key=${parsed#*$'\t'}
   log "poll $i/$MAX -> $state"
   if [ "$state" = "CAN_GENERATE" ]; then
-    log "clicking Generate API key"; click_generate; raw=$(extract_state); state=$(echo "$raw" | sed -E 's/.*"state":"([^"]*)".*/\1/')
+    log "clicking Generate API key"; click_generate; raw=$(extract_state)
+    parsed=$(printf '%s' "$raw" | python3 parse_state.py)
+    state=${parsed%%$'\t'*}; key=${parsed#*$'\t'}
     log "post-generate -> $state"
   fi
   if [ "$state" = "KEY_PRESENT" ]; then
-    key=$(echo "$raw" | python3 -c "import json,sys;print(json.load(sys.stdin).get('key',''))" 2>/dev/null)
     if [ -n "$key" ]; then
       printf '%s' "$key" > .apikey; chmod 600 .apikey
       log "KEY captured (…${key: -4}); starting download"

← 8e805d9 Add detached poll-and-download bg job  ·  back to Uspto Trtyrap  ·  auto-data-snapshot: 2026-08-06T16:03:07 (1 data files) — .ap feb6987 →