Skip to content

Commit 8079d35

Browse files
committed
fix: observer fails closed on confirmation/permission prompts (issue anomalyco#400)
1 parent da4db99 commit 8079d35

1 file changed

Lines changed: 50 additions & 26 deletions

File tree

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

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ resolve_python_cmd() {
3333
return 0
3434
fi
3535

36+
# FIX: Windows Git Bash — check known Python install paths directly
37+
# because `command -v python` triggers the Microsoft Store alias instead
38+
for win_py in \
39+
"/c/Users/$USER/AppData/Local/Programs/Python/Python311/python" \
40+
"/c/Users/$USER/AppData/Local/Programs/Python/Python312/python" \
41+
"/c/Users/$USER/AppData/Local/Programs/Python/Python310/python"; do
42+
if [ -x "$win_py" ]; then
43+
printf '%s\n' "$win_py"
44+
return 0
45+
fi
46+
done
47+
3648
if command -v python3 >/dev/null 2>&1; then
3749
printf '%s\n' python3
3850
return 0
@@ -93,11 +105,22 @@ CONFIG_DIR="${HOME}/.claude/homunculus"
93105
OBSERVATIONS_FILE="${PROJECT_DIR}/observations.jsonl"
94106
MAX_FILE_SIZE_MB=10
95107

96-
# Skip if disabled
108+
# FIX: SENTINEL_FILE must be defined AFTER PROJECT_DIR is set by detect-project.sh
109+
# Previously it was defined at the top before PROJECT_DIR existed, making it empty/broken
110+
SENTINEL_FILE="${PROJECT_DIR}/.observer.lock"
111+
112+
# Skip if disabled globally
97113
if [ -f "$CONFIG_DIR/disabled" ]; then
98114
exit 0
99115
fi
100116

117+
# FIX: Skip if a previous run already aborted due to confirmation/permission prompt
118+
# This is the circuit-breaker — stops retrying after a non-interactive failure
119+
if [ -f "$SENTINEL_FILE" ]; then
120+
echo "[observe] Skipping: previous run aborted due to confirmation/permission prompt. Remove ${SENTINEL_FILE} to re-enable." >&2
121+
exit 0
122+
fi
123+
101124
# Auto-purge observation files older than 30 days (runs once per session)
102125
PURGE_MARKER="${PROJECT_DIR}/.last-purge"
103126
if [ ! -f "$PURGE_MARKER" ] || [ "$(find "$PURGE_MARKER" -mtime +1 2>/dev/null)" ]; then
@@ -190,46 +213,47 @@ if [ -f "$OBSERVATIONS_FILE" ]; then
190213
fi
191214
fi
192215

216+
# FIX: Detect confirmation/permission prompts in observer output and fail closed.
217+
# A non-interactive background observer must never ask for user confirmation.
218+
# If detected: log once, write sentinel to suppress all future retries, exit non-zero.
219+
if echo "$PARSED" | grep -E -i -q "Can you confirm|requires permission|Awaiting|confirm I should proceed|once granted access|grant.*access"; then
220+
echo "[observe] OBSERVER_ABORT: Confirmation or permission prompt detected in observer output. This observer run is non-actionable." >&2
221+
echo "[observe] Writing sentinel to suppress retries: ${SENTINEL_FILE}" >&2
222+
echo "$PARSED" > "$SENTINEL_FILE"
223+
exit 2
224+
fi
225+
193226
# Build and write observation (now includes project context)
194-
# Scrub common secret patterns from tool I/O before persisting
195227
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
196-
197228
export PROJECT_ID_ENV="$PROJECT_ID"
198229
export PROJECT_NAME_ENV="$PROJECT_NAME"
199230
export TIMESTAMP="$timestamp"
200-
201231
echo "$PARSED" | "$PYTHON_CMD" -c '
202232
import json, sys, os, re
203-
204233
parsed = json.load(sys.stdin)
205234
observation = {
206-
"timestamp": os.environ["TIMESTAMP"],
207-
"event": parsed["event"],
208-
"tool": parsed["tool"],
209-
"session": parsed["session"],
210-
"project_id": os.environ.get("PROJECT_ID_ENV", "global"),
211-
"project_name": os.environ.get("PROJECT_NAME_ENV", "global")
235+
"timestamp": os.environ["TIMESTAMP"],
236+
"event": parsed["event"],
237+
"tool": parsed["tool"],
238+
"session": parsed["session"],
239+
"project_id": os.environ.get("PROJECT_ID_ENV", "global"),
240+
"project_name": os.environ.get("PROJECT_NAME_ENV", "global")
212241
}
213-
214242
# Scrub secrets: match common key=value, key: value, and key"value patterns
215-
# Includes optional auth scheme (e.g., "Bearer", "Basic") before token
216243
_SECRET_RE = re.compile(
217-
r"(?i)(api[_-]?key|token|secret|password|authorization|credentials?|auth)"
218-
r"""(["'"'"'\s:=]+)"""
219-
r"([A-Za-z]+\s+)?"
220-
r"([A-Za-z0-9_\-/.+=]{8,})"
244+
r"(?i)(api[_-]?key|token|secret|password|authorization|credentials?|auth)"
245+
r"""(["'"'"'\s:=]+)"""
246+
r"([A-Za-z]+\s+)?"
247+
r"([A-Za-z0-9_\-/.+=]{8,})"
221248
)
222-
223249
def scrub(val):
224-
if val is None:
225-
return None
226-
return _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", str(val))
227-
250+
if val is None:
251+
return None
252+
return _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", str(val))
228253
if parsed["input"]:
229-
observation["input"] = scrub(parsed["input"])
254+
observation["input"] = scrub(parsed["input"])
230255
if parsed["output"] is not None:
231-
observation["output"] = scrub(parsed["output"])
232-
256+
observation["output"] = scrub(parsed["output"])
233257
print(json.dumps(observation))
234258
' >> "$OBSERVATIONS_FILE"
235259

@@ -243,4 +267,4 @@ for pid_file in "${PROJECT_DIR}/.observer.pid" "${CONFIG_DIR}/.observer.pid"; do
243267
fi
244268
done
245269

246-
exit 0
270+
exit 0

0 commit comments

Comments
 (0)