summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Vincent <jesse@fsck.com>2026-01-17 18:35:37 -0800
committerJesse Vincent <jesse@fsck.com>2026-01-17 18:35:37 -0800
commit70c2d06c4e792d0c5e4c896e70765ddf2a5affa7 (patch)
tree1f39257de34d3a1622feef265aa986cd7a5c7ae4
parentb98afbd74fc346b3fa293d9e638716d3823dc1c1 (diff)
feat: add show-and-wait.sh helper, fix race condition
- New show-and-wait.sh combines write + wait into one command - Uses polling instead of tail -f (which hangs on macOS) - Docs updated: start watcher BEFORE writing screen to avoid race - Reduces terminal noise by consolidating operations
-rwxr-xr-xlib/brainstorm-server/show-and-wait.sh32
-rw-r--r--skills/brainstorming/SKILL.md6
-rw-r--r--skills/brainstorming/visual-companion.md12
3 files changed, 41 insertions, 9 deletions
diff --git a/lib/brainstorm-server/show-and-wait.sh b/lib/brainstorm-server/show-and-wait.sh
new file mode 100755
index 0000000..02519f0
--- /dev/null
+++ b/lib/brainstorm-server/show-and-wait.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+# Write HTML to screen and wait for user feedback
+# Usage: show-and-wait.sh <screen_dir> < html_content
+#
+# Reads HTML from stdin, writes to screen_file, waits for feedback.
+# Outputs the feedback JSON when user sends from browser.
+
+SCREEN_DIR="${1:?Usage: show-and-wait.sh <screen_dir>}"
+SCREEN_FILE="${SCREEN_DIR}/screen.html"
+LOG_FILE="${SCREEN_DIR}/.server.log"
+
+if [[ ! -d "$SCREEN_DIR" ]]; then
+ echo '{"error": "Screen directory not found"}' >&2
+ exit 1
+fi
+
+# Write HTML from stdin to screen file
+cat > "$SCREEN_FILE"
+
+# Record current position in log file
+LOG_POS=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)
+
+# Poll for new lines containing the event
+while true; do
+ # Check for new matching lines since our starting position
+ RESULT=$(tail -n +$((LOG_POS + 1)) "$LOG_FILE" 2>/dev/null | grep -m 1 "send-to-claude")
+ if [[ -n "$RESULT" ]]; then
+ echo "$RESULT"
+ exit 0
+ fi
+ sleep 0.2
+done
diff --git a/skills/brainstorming/SKILL.md b/skills/brainstorming/SKILL.md
index c5d0e36..2659dda 100644
--- a/skills/brainstorming/SKILL.md
+++ b/skills/brainstorming/SKILL.md
@@ -112,9 +112,9 @@ When the user clicks Send in the browser, the watcher exits and TaskOutput retur
### The Loop
-1. Write screen HTML to `screen_file`
-2. Start watcher (background bash)
-3. Call TaskOutput(task_id, block=true) to wait
+1. Start watcher (background bash) - must be FIRST to avoid race condition
+2. Write screen HTML to `screen_file`
+3. Call TaskOutput(task_id, block=true, timeout=600000) to wait
4. TaskOutput returns with feedback
5. Respond with new screen
6. Repeat until done
diff --git a/skills/brainstorming/visual-companion.md b/skills/brainstorming/visual-companion.md
index c6a2756..4ca1260 100644
--- a/skills/brainstorming/visual-companion.md
+++ b/skills/brainstorming/visual-companion.md
@@ -21,18 +21,18 @@ ${CLAUDE_PLUGIN_ROOT}/lib/brainstorm-server/start-server.sh
# "screen_dir":"/tmp/brainstorm-12345-1234567890",
# "screen_file":"/tmp/brainstorm-12345-1234567890/screen.html"}
-# 2. Write screen to the session's screen_file
+# 2. Start watcher FIRST (before writing screen - avoids race condition)
+${CLAUDE_PLUGIN_ROOT}/lib/brainstorm-server/wait-for-event.sh $SCREEN_DIR/.server.log
+
+# 3. Write screen to the session's screen_file
# Use Bash with heredoc to write HTML
-# 3. Wait for feedback using TaskOutput with block=true
-# Start watcher in background:
-${CLAUDE_PLUGIN_ROOT}/lib/brainstorm-server/wait-for-event.sh $SCREEN_DIR/.server.log
-# Then call TaskOutput(task_id, block=true, timeout=600000) to wait
+# 4. Wait for feedback - call TaskOutput(task_id, block=true, timeout=600000)
# If timeout, call TaskOutput again (watcher still running)
# After 3 timeouts (30 min), say "Let me know when you want to continue"
# Returns: {"choice":"a","feedback":"user notes"}
-# 4. Clean up when done (pass screen_dir as argument)
+# 5. Clean up when done (pass screen_dir as argument)
${CLAUDE_PLUGIN_ROOT}/lib/brainstorm-server/stop-server.sh $SCREEN_DIR
```