Skip to content

Commit a3cefd2

Browse files
ThinkOffAppclaude
andcommitted
nudge: reserve the injection atomically, stamp only after a real send (codex review of #89)
Two processes could pass the early debounce, both wait out the idle guard, and both inject; and the AppleScript path stamped before it knew it had sent - a 'human active' abort even returned success. Now: mkdir reservation held across the injection with a stamp re-check inside it (stale holder after 10 min), stamp written only after cliclick or AppleScript succeeds, AppleScript aborts raise (non-zero exit). Test: a held reservation debounces right before injection with the lock/idle checks stubbed; positive control shows the same stubs reach the injection, and a failed send leaves no stamp and releases the lock. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent ec43d73 commit a3cefd2

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎test/poller-health.test.mjs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Issue #86: a nudge must not fire while the room poller is down, and the
33
// supervisor must say so once (and once more when it is back).
44
import assert from 'node:assert/strict';
5-
import { mkdtempSync, rmSync, writeFileSync, existsSync, utimesSync, readFileSync, chmodSync, statSync } from 'node:fs';
5+
import { mkdtempSync, rmSync, writeFileSync, existsSync, utimesSync, readFileSync, chmodSync, statSync, mkdirSync } from 'node:fs';
66
import { tmpdir } from 'node:os';
77
import path from 'node:path';
88
import { spawnSync, execFile } from 'node:child_process';
@@ -151,3 +151,37 @@ test('codex_gui_nudge debounces a second nudge inside the window, and only a rea
151151
assert.equal(statSync(stamp).mtimeMs, before, 'abort paths never write the stamp');
152152
} finally { rmSync(dir, { recursive: true, force: true }); }
153153
});
154+
155+
test('codex_gui_nudge: a reservation held by another nudge debounces this one right before injection', () => {
156+
const dir = mkdtempSync(path.join(tmpdir(), 'iak-reserve-'));
157+
try {
158+
const log = path.join(dir, 'nudge.log');
159+
const hb = path.join(dir, 'hb'); writeHeartbeat(hb);
160+
const stamp = path.join(dir, 'last');
161+
mkdirSync(stamp + '.lock'); // the other process is injecting right now
162+
// stubs so the run passes the screen-lock and human-idle checks and reaches
163+
// the reservation: ioreg (idle guard), a fake python that reports
164+
// "unlocked" and "idle", and an osascript that must never be reached
165+
const stubDir = path.join(dir, 'bin'); mkdirSync(stubDir);
166+
const mk = (name, body) => { const f = path.join(stubDir, name); writeFileSync(f, body); chmodSync(f, 0o755); return f; };
167+
mk('ioreg', '#!/bin/sh\necho \'| "HIDIdleTime" = 999999999999\'\n');
168+
const py = mk('fakepy', '#!/bin/sh\n[ "$1" = "-" ] && echo unlocked\nexit 0\n');
169+
mk('osascript', '#!/bin/sh\necho "MUST NOT RUN" >&2; exit 99\n');
170+
const idleCheck = mk('idle.py', '#!/bin/sh\nexit 0\n');
171+
const r = spawnSync('bash', [nudge], { encoding: 'utf8', env: { ...process.env, PATH: `${stubDir}:${process.env.PATH}`, IAK_POLLER_HEARTBEAT: hb, IAK_NUDGE_STAMP: stamp, IAK_CODEX_NUDGE_LOG: log, IAK_CLICLICK_BIN: '/nonexistent', IAK_PYTHON_BIN: py, IAK_HUMAN_IDLE_CHECK: idleCheck } });
172+
const out = readFileSync(log, 'utf8');
173+
assert.equal(r.status, 0, r.stderr);
174+
assert.ok(!/MUST NOT RUN/.test(r.stderr), 'no injection while another nudge holds the reservation');
175+
assert.match(out, /debounced \(another nudge is injecting right now\)/);
176+
assert.ok(existsSync(stamp + '.lock'), 'a reservation held by another process is not released by us');
177+
assert.ok(!existsSync(stamp), 'no stamp written by a debounced run');
178+
// positive control: with no reservation the same stubs DO reach the
179+
// injection (the osascript stub fails), and a failed send leaves no stamp
180+
rmSync(stamp + '.lock', { recursive: true, force: true });
181+
const r2 = spawnSync('bash', [nudge], { encoding: 'utf8', env: { ...process.env, PATH: `${stubDir}:${process.env.PATH}`, IAK_POLLER_HEARTBEAT: hb, IAK_NUDGE_STAMP: stamp, IAK_CODEX_NUDGE_LOG: log, IAK_CLICLICK_BIN: '/nonexistent', IAK_PYTHON_BIN: py, IAK_HUMAN_IDLE_CHECK: idleCheck } });
182+
assert.match(r2.stderr, /MUST NOT RUN/, 'control: the stubs reach the injection point');
183+
assert.equal(r2.status, 1, 'a failed injection exits 1');
184+
assert.ok(!existsSync(stamp), 'a failed injection writes no stamp');
185+
assert.ok(!existsSync(stamp + '.lock'), 'the reservation is released on exit');
186+
} finally { rmSync(dir, { recursive: true, force: true }); }
187+
});

‎tools/codex_gui_nudge.sh‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ if [ -f "$NUDGE_STAMP" ]; then
5555
fi ;;
5656
esac
5757
fi
58+
# Atomic reservation held across the injection (codex review of PR #89):
59+
# two nudge processes can both pass the early check, both wait out the idle
60+
# guard, and both inject. mkdir is atomic; the holder re-checks the stamp
61+
# inside the reservation, so a nudge that landed while we waited debounces
62+
# this one. A reservation older than 10 min is a crashed holder.
63+
NUDGE_LOCK="$NUDGE_STAMP.lock"
64+
release_nudge() { rmdir "$NUDGE_LOCK" 2>/dev/null || true; }
65+
reserve_nudge() {
66+
if [ -d "$NUDGE_LOCK" ]; then
67+
LOCK_MTIME=$(stat -c %Y "$NUDGE_LOCK" 2>/dev/null || stat -f %m "$NUDGE_LOCK" 2>/dev/null || echo 0)
68+
case "$LOCK_MTIME" in ''|*[!0-9]*) LOCK_MTIME=0 ;; esac
69+
if [ $(( $(date +%s) - LOCK_MTIME )) -gt 600 ]; then rmdir "$NUDGE_LOCK" 2>/dev/null || true; fi
70+
fi
71+
if ! mkdir "$NUDGE_LOCK" 2>/dev/null; then
72+
printf '[%s] codex_gui_nudge: debounced (another nudge is injecting right now)\n' "$(date -u +%FT%TZ)" >>"$NUDGE_LOG_EARLY"
73+
exit 0
74+
fi
75+
trap release_nudge EXIT
76+
if [ -f "$NUDGE_STAMP" ]; then
77+
LAST_MTIME=$(stat -c %Y "$NUDGE_STAMP" 2>/dev/null || stat -f %m "$NUDGE_STAMP" 2>/dev/null || echo "")
78+
case "$LAST_MTIME" in
79+
''|*[!0-9]*) ;;
80+
*)
81+
SINCE=$(( $(date +%s) - LAST_MTIME ))
82+
if [ "$SINCE" -lt "$NUDGE_DEBOUNCE" ]; then
83+
printf '[%s] codex_gui_nudge: debounced at injection (%ss since a nudge that landed while we waited)\n' "$(date -u +%FT%TZ)" "$SINCE" >>"$NUDGE_LOG_EARLY"
84+
exit 0
85+
fi ;;
86+
esac
87+
fi
88+
}
5889
if ! "$REPO_ROOT/tools/human-idle-guard.sh" --wait 300; then
5990
echo "nudge aborted: human continuously active" >&2
6091
exit 1
@@ -169,8 +200,9 @@ PY
169200
CLICK_X=$((WIN_X + WIN_W / 2))
170201
CLICK_Y=$((WIN_Y + WIN_H - 72))
171202
require_human_idle "before cliclick injection" || exit 1
172-
touch "$NUDGE_STAMP" 2>/dev/null || true
203+
reserve_nudge
173204
if "$CLICLICK_BIN" -r -w 20 "c:${CLICK_X},${CLICK_Y}" "t:${PROMPT_TEXT}" kp:return; then
205+
touch "$NUDGE_STAMP" 2>/dev/null || true
174206
printf '[%s] codex_gui_nudge: sent via cliclick x=%s y=%s\n' "$(date -u +%FT%TZ)" "$CLICK_X" "$CLICK_Y" >>"$LOG_FILE"
175207
exit 0
176208
fi
@@ -179,9 +211,8 @@ PY
179211
fi
180212

181213
require_human_idle "before AppleScript wake" || exit 0
182-
touch "$NUDGE_STAMP" 2>/dev/null || true
183-
184-
osascript - "$APP_NAME" "$PROMPT_TEXT" "$LOG_FILE" "$LOCK_PY" "$HUMAN_IDLE_CHECK" "$HUMAN_IDLE_SEC" <<'APPLESCRIPT'
214+
reserve_nudge
215+
if osascript - "$APP_NAME" "$PROMPT_TEXT" "$LOG_FILE" "$LOCK_PY" "$HUMAN_IDLE_CHECK" "$HUMAN_IDLE_SEC" <<'APPLESCRIPT'
185216
on run argv
186217
set appName to item 1 of argv
187218
set promptText to item 2 of argv
@@ -242,7 +273,7 @@ on run argv
242273
243274
if not my humanIsIdle(pythonBin, idleCheck, idleThreshold) then
244275
my writeLog(logFile, "ABORT human active or idle state unknown before AppleScript injection")
245-
return
276+
error "human active before injection" number 1003
246277
end if
247278
248279
try
@@ -293,3 +324,12 @@ on writeLog(logFile, msg)
293324
do shell script "printf '[%s] %s\\n' \"$(date -u +%FT%TZ)\" " & quoted form of ("codex_gui_nudge: " & msg) & " >> " & quoted form of logFile
294325
end writeLog
295326
APPLESCRIPT
327+
then
328+
touch "$NUDGE_STAMP" 2>/dev/null || true
329+
printf '[%s] codex_gui_nudge: sent via AppleScript\n' "$(date -u +%FT%TZ)" >>"$LOG_FILE"
330+
exit 0
331+
else
332+
# AppleScript aborts and failures raise an error (non-zero exit): nothing
333+
# was delivered, no stamp, so the next wake is not debounced.
334+
exit 1
335+
fi

0 commit comments

Comments
 (0)