Skip to content

Commit b57b573

Browse files
fix(continuous-learning-v2): add lazy-start observer logic (anomalyco#508)
* feat(continuous-learning-v2): add lazy-start observer logic Auto-starts observer when observer.enabled: true in config and no .observer.pid exists. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(continuous-learning-v2): address PR review concerns - Use flock for atomic check-then-act to prevent race conditions - Check both project-scoped AND global PID files before starting - Support CLV2_CONFIG override for config file path - Check disabled file in lazy-start logic - Use double-check pattern after acquiring lock Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(observe.sh): address PR review comments - Add stale PID cleanup via _CHECK_OBSERVER_RUNNING function - Add macOS fallback using lockfile when flock unavailable - Fix CLV2_CONFIG override: use EFFECTIVE_CONFIG for both check and read - Use proper Python context manager (with open() as f) - Deduplicate signaled PIDs to avoid duplicate USR1 signals Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(observe.sh): wrap macOS lockfile fallback in subshell with trap - Wrap lockfile block in subshell so exit 0 only terminates that block - Add trap for EXIT to clean up lock file on script interruption - Add -l 30 (30 second expiry) to prevent permanent lock file stuck Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(observe.sh): address remaining PR review comments - Validate PID is a positive integer before kill calls to prevent signaling invalid targets (e.g. -1 could signal all processes) - Pass config path via env var instead of interpolating shell variable into Python -c string to prevent injection/breakage on special paths - Check CLV2_CONFIG-derived directory for disabled file so disable guard respects the same config source as lazy-start Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
1 parent 01ed1b3 commit b57b573

1 file changed

Lines changed: 103 additions & 3 deletions

File tree

‎skills/continuous-learning-v2/hooks/observe.sh‎

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ fi
8383

8484
CONFIG_DIR="${HOME}/.claude/homunculus"
8585

86-
# Skip if disabled
86+
# Skip if disabled (check both default and CLV2_CONFIG-derived locations)
8787
if [ -f "$CONFIG_DIR/disabled" ]; then
8888
exit 0
8989
fi
90+
if [ -n "${CLV2_CONFIG:-}" ] && [ -f "$(dirname "$CLV2_CONFIG")/disabled" ]; then
91+
exit 0
92+
fi
9093

9194
# Prevent observe.sh from firing on non-human sessions to avoid:
9295
# - ECC observing its own Haiku observer sessions (self-loop)
@@ -275,12 +278,109 @@ if parsed["output"] is not None:
275278
print(json.dumps(observation))
276279
' >> "$OBSERVATIONS_FILE"
277280

278-
# Signal observer if running (check both project-scoped and global observer)
281+
# Lazy-start observer if enabled but not running (first-time setup)
282+
# Use flock for atomic check-then-act to prevent race conditions
283+
# Fallback for macOS (no flock): use lockfile or skip
284+
LAZY_START_LOCK="${PROJECT_DIR}/.observer-start.lock"
285+
_CHECK_OBSERVER_RUNNING() {
286+
local pid_file="$1"
287+
if [ -f "$pid_file" ]; then
288+
local pid
289+
pid=$(cat "$pid_file" 2>/dev/null)
290+
# Validate PID is a positive integer (>1) to prevent signaling invalid targets
291+
case "$pid" in
292+
''|*[!0-9]*|0|1)
293+
rm -f "$pid_file" 2>/dev/null || true
294+
return 1
295+
;;
296+
esac
297+
if kill -0 "$pid" 2>/dev/null; then
298+
return 0 # Process is alive
299+
fi
300+
# Stale PID file - remove it
301+
rm -f "$pid_file" 2>/dev/null || true
302+
fi
303+
return 1 # No PID file or process dead
304+
}
305+
306+
if [ -f "${CONFIG_DIR}/disabled" ]; then
307+
OBSERVER_ENABLED=false
308+
else
309+
OBSERVER_ENABLED=false
310+
CONFIG_FILE="${SKILL_ROOT}/config.json"
311+
# Allow CLV2_CONFIG override
312+
if [ -n "${CLV2_CONFIG:-}" ]; then
313+
CONFIG_FILE="$CLV2_CONFIG"
314+
fi
315+
# Use effective config path for both existence check and reading
316+
EFFECTIVE_CONFIG="$CONFIG_FILE"
317+
if [ -f "$EFFECTIVE_CONFIG" ] && [ -n "$PYTHON_CMD" ]; then
318+
_enabled=$(CLV2_CONFIG_PATH="$EFFECTIVE_CONFIG" "$PYTHON_CMD" -c "
319+
import json, os
320+
with open(os.environ['CLV2_CONFIG_PATH']) as f:
321+
cfg = json.load(f)
322+
print(str(cfg.get('observer', {}).get('enabled', False)).lower())
323+
" 2>/dev/null || echo "false")
324+
if [ "$_enabled" = "true" ]; then
325+
OBSERVER_ENABLED=true
326+
fi
327+
fi
328+
fi
329+
330+
# Check both project-scoped AND global PID files (with stale PID recovery)
331+
if [ "$OBSERVER_ENABLED" = "true" ]; then
332+
# Clean up stale PID files first
333+
_CHECK_OBSERVER_RUNNING "${PROJECT_DIR}/.observer.pid" || true
334+
_CHECK_OBSERVER_RUNNING "${CONFIG_DIR}/.observer.pid" || true
335+
336+
# Check if observer is now running after cleanup
337+
if [ ! -f "${PROJECT_DIR}/.observer.pid" ] && [ ! -f "${CONFIG_DIR}/.observer.pid" ]; then
338+
# Use flock if available (Linux), fallback for macOS
339+
if command -v flock >/dev/null 2>&1; then
340+
(
341+
flock -n 9 || exit 0
342+
# Double-check PID files after acquiring lock
343+
_CHECK_OBSERVER_RUNNING "${PROJECT_DIR}/.observer.pid" || true
344+
_CHECK_OBSERVER_RUNNING "${CONFIG_DIR}/.observer.pid" || true
345+
if [ ! -f "${PROJECT_DIR}/.observer.pid" ] && [ ! -f "${CONFIG_DIR}/.observer.pid" ]; then
346+
nohup "${SKILL_ROOT}/agents/start-observer.sh" start >/dev/null 2>&1 &
347+
fi
348+
) 9>"$LAZY_START_LOCK"
349+
else
350+
# macOS fallback: use lockfile if available, otherwise skip
351+
if command -v lockfile >/dev/null 2>&1; then
352+
# Use subshell to isolate exit and add trap for cleanup
353+
(
354+
trap 'rm -f "$LAZY_START_LOCK" 2>/dev/null || true' EXIT
355+
lockfile -r 1 -l 30 "$LAZY_START_LOCK" 2>/dev/null || exit 0
356+
_CHECK_OBSERVER_RUNNING "${PROJECT_DIR}/.observer.pid" || true
357+
_CHECK_OBSERVER_RUNNING "${CONFIG_DIR}/.observer.pid" || true
358+
if [ ! -f "${PROJECT_DIR}/.observer.pid" ] && [ ! -f "${CONFIG_DIR}/.observer.pid" ]; then
359+
nohup "${SKILL_ROOT}/agents/start-observer.sh" start >/dev/null 2>&1 &
360+
fi
361+
rm -f "$LAZY_START_LOCK" 2>/dev/null || true
362+
)
363+
fi
364+
fi
365+
fi
366+
fi
367+
368+
# Signal observer if running (check both project-scoped and global observer, deduplicate)
369+
signaled_pids=" "
279370
for pid_file in "${PROJECT_DIR}/.observer.pid" "${CONFIG_DIR}/.observer.pid"; do
280371
if [ -f "$pid_file" ]; then
281-
observer_pid=$(cat "$pid_file")
372+
observer_pid=$(cat "$pid_file" 2>/dev/null || true)
373+
# Validate PID is a positive integer (>1)
374+
case "$observer_pid" in
375+
''|*[!0-9]*|0|1) rm -f "$pid_file" 2>/dev/null || true; continue ;;
376+
esac
377+
# Deduplicate: skip if already signaled this pass
378+
case "$signaled_pids" in
379+
*" $observer_pid "*) continue ;;
380+
esac
282381
if kill -0 "$observer_pid" 2>/dev/null; then
283382
kill -USR1 "$observer_pid" 2>/dev/null || true
383+
signaled_pids="${signaled_pids}${observer_pid} "
284384
fi
285385
fi
286386
done

0 commit comments

Comments
 (0)