Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(skills): separate forever lifetime from cycle timeout

cyhcad92141ad88
diff --git a/skills/background-watch-hook/SKILL.md b/skills/background-watch-hook/SKILL.md
index 5ae524e45..fddd79487 100644
--- a/skills/background-watch-hook/SKILL.md
+++ b/skills/background-watch-hook/SKILL.md
@@ -1,219 +1,90 @@
---
name: background-watch-hook
slug: background-watch-hook
-description: Run a blocking watcher in the background and send a `vibe hook` back to the current IM session when it finishes. Use for GitHub reviews, CI, file completion, log matches, and process-exit follow-ups.
-version: 0.3.0
+description: Start a background waiter that returns to the same conversation later. Use when the agent needs to wait for reviews, CI, files, logs, or process completion without blocking the current turn.
+version: 0.4.0
---
# Background Watch Hook
-Use this skill when the user wants the agent to:
+Use this skill when the job is "wait now, continue later in the same conversation".
-- wait for an external event in the background
-- stop polling manually
-- automatically wake up the current IM session when the event happens
+What it gives the agent:
-This is a reusable pattern, not a product feature. Do not build a new scheduler or event subsystem when this pattern is enough.
+- the ability to stop manual polling
+- a reusable way to come back to the same channel or thread later
+- a small pattern that works for many external events, not just GitHub
-## Core Idea
+Good trigger scenarios:
-The pattern has three parts:
+- PR reviews or comments may arrive later
+- CI, deployments, or exports need time to finish
+- a file, log line, or process exit should wake the agent up later
-1. A blocking waiter command that exits only when the watched condition becomes true.
-2. An async wrapper that starts the waiter in the background and returns immediately.
-3. A `vibe hook send` call that fires only after the waiter exits successfully.
+Do not build a new scheduler or event system when this pattern is enough.
-The waiter prints its final event summary to `stdout`. The wrapper captures that output, builds a prompt file, and queues one hook back into the current session.
-
-Use this pattern for any one-off "wait in the background, then come back here" task, for example:
-
-- GitHub PR reviews or comments
-- CI finishing
-- a file or export being generated
-- a log line appearing
-- a long-running process exiting
-
-## Hard Rules
-
-1. Do not use `watcher & && vibe hook send ...`. That runs the hook immediately after background launch, not after the watcher finishes.
-2. Let the wrapper own detaching. Do not make every caller hand-roll `nohup`.
-3. Keep waiter `stdout` clean. Final event summary goes to `stdout`; debug logs go to `stderr`.
-4. Prefer `--prompt-file` over inline `--prompt` for hook payloads.
-5. Use a timeout-aware waiter. Infinite waits are fine only when the user explicitly wants them.
-
-## Files In This Skill
+## Main Tools
- `scripts/watch_then_hook.sh`
- Main entrypoint. Starts a waiter in the background by default, captures `stdout`, then calls `vibe hook send`.
+ Main entrypoint. Starts a waiter in the background by default, captures its final `stdout`, and sends one follow-up back to the target session.
- `scripts/watch_github_pr_then_hook.sh`
- Thin convenience wrapper for the common GitHub PR case. It forwards GitHub-specific flags to the bundled waiter and generic delivery flags to the main wrapper.
+ Thin convenience wrapper for one common case: GitHub PR review activity.
- `scripts/wait_for_github_pr_activity.py`
- Included example waiter. Polls a GitHub PR until new review activity appears, then prints a concise summary.
-
-## Session Targeting
-
-You usually already know the current delivery target from Vibe Remote's prompt context:
+ Bundled waiter example used by the GitHub wrapper.
-- use the current `session_key` if the follow-up should return to the same channel
-- append `::thread::<thread_id>` when the follow-up must land in the current thread
-- use `--post-to channel` only when the user wants to preserve thread context but publish in the parent channel
+## Core Pattern
-If the current turn does not expose a usable session key, stop and ask for the exact delivery target instead of guessing.
+Use the generic wrapper first. Most tasks only need:
-## Wrapper First
-
-`scripts/watch_then_hook.sh` is the main reusable tool in this skill. Most of the time you do not need to invent a new shell pattern. You only need:
-
-1. a correct `session_key`
+1. the target `session_key`
2. a short action-oriented prefix
-3. a waiter command that follows the contract below
+3. a blocking waiter command
-The wrapper interface is:
+Generic shape:
```bash
scripts/watch_then_hook.sh \
--session-key "<session-key>" \
- [--prefix "<hook prefix>"] \
- [--timeout 21600] \
- [--log-file "/tmp/watch.log"] \
- [--foreground] \
- [--post-to channel] \
- [--deliver-key "<delivery-session-key>"] \
- [--hook-bin vibe] \
- [--hook-cmd "uv run python -m vibe"] \
- [--timeout-exit-code 124] \
- -- <waiter command ...>
-```
-
-- `--session-key`
- Required. Delivery target for the follow-up hook.
-- `--prefix`
- Optional. Prepended to the waiter output inside the generated prompt file.
-- `--timeout`
- Optional. Overall wait timeout in seconds. Default is `21600` (6 hours). Use `0` for no timeout.
-- `--log-file`
- Optional. Background log destination. If omitted, the wrapper picks a file under `/tmp/`.
-- `--foreground`
- Optional. Run inline. Use this only when you explicitly want the low-level synchronous primitive for testing or composition.
-- `--post-to`
- Optional. Use only when the follow-up should post to the parent channel while keeping thread context.
-- `--deliver-key`
- Optional. Explicit delivery target key. Use this only when the follow-up should be delivered to a different target.
-- `--hook-bin`
- Optional. Explicit hook executable override.
-- `--hook-cmd`
- Optional. Full hook command override. Useful when the local `vibe` on `PATH` is a stub and the real CLI should run through something like `uv run python -m vibe`.
-- `--timeout-exit-code`
- Optional. The wrapper treats this exit code as a timeout and still sends a timeout hook summary.
-
-`--post-to` and `--deliver-key` are mutually exclusive. Fail fast if both are set.
-
-## Preferred Execution Pattern
-
-The core reusable move is still the generic wrapper. Start there:
-
-```bash
-scripts/watch_then_hook.sh \
- --session-key "slack::channel::C123::thread::171717.123" \
- --prefix "The background condition was met. Inspect the result and continue the task." \
+ --prefix "<what the next turn should do>" \
-- \
- bash -lc 'sleep 120; echo "Waiter finished after 120 seconds."'
+ <waiter command ...>
```
-That command returns immediately, leaves the watcher running in the background, and sends the hook only after the waiter exits with success.
-
-If you need the old low-level synchronous primitive, add `--foreground`.
-
-If the waiter does not finish within the wrapper timeout, the wrapper still sends a hook with a timeout summary instead of failing silently.
-
-When running inside the Vibe Remote repo or worktree, the wrapper will auto-fallback to `uv run python -m vibe` if the `vibe` executable on `PATH` is not the real CLI.
-
-Use a thin convenience wrapper like `watch_github_pr_then_hook.sh` only when it materially shortens a common case.
-
-## Generic Workflow
-
-### 1. Pick the waiter
-
-For other use cases, either:
-
-- write a new small waiter in the task workspace, or
-- reuse another existing blocking command that prints a final summary to `stdout`
-
-Use `scripts/wait_for_github_pr_activity.py` only when the thing being watched is GitHub PR review activity. Use `scripts/watch_github_pr_then_hook.sh` if you want that common path pre-wired.
-
-By default, the GitHub convenience wrapper is still one-shot: it waits for the next matching event, sends one hook, and exits. Use `--forever` only when you explicitly want a long-lived watcher that keeps re-arming itself after each detected event.
-
-If the watcher should immediately surface activity that already exists at startup, add `--catch-up`. Without it, the included GitHub waiter snapshots current activity as the baseline and waits only for newer events.
-
-The included GitHub waiter expects GitHub authentication by default. It will use `GITHUB_TOKEN`, `GH_TOKEN`, or `gh auth token`. Only use `--allow-unauthenticated` for slower best-effort polling when authentication is not available.
-
-### 2. Decide the session target
+Default behavior:
-Use the current thread when the user wants the result to continue in context.
+- returns immediately
+- keeps the waiter in the background
+- sends a follow-up only after the waiter succeeds
+- sends a timeout follow-up if the waiter times out
-### 3. Build a short hook prefix
-
-The prefix should tell the next turn what to do with the watcher result. Good prefixes are action-oriented:
-
-- `GitHub PR has new review activity. Fetch the latest comments and continue resolving them.`
-- `CI finished. Inspect the failure and propose the next fix.`
-- `The export completed. Summarize the result and share the artifact details.`
-
-### 4. Start the wrapper
-
-Run `scripts/watch_then_hook.sh ...`. It detaches by default.
-
-### 5. Tell the user what was started
-
-Report:
-
-- what is being watched
-- where the hook will be delivered
-- where the background log is going
+Add `--foreground` only when you explicitly want the low-level synchronous primitive.
## Waiter Contract
-Design waiters to follow this contract:
+Write waiters to follow this contract:
- `exit 0`: event detected; final summary printed to `stdout`
-- `exit 124`: timeout; the wrapper sends a timeout hook with an error summary
-- wrapper timeout:
- by default the wrapper itself times out the waiter after 6 hours and emits the same timeout hook path
-- any other non-zero exit: failure; wrapper should exit without sending a hook
-
-Good waiter output is compact and already useful to the next turn, for example:
-
-```text
-GitHub PR activity detected for cyhhao/vibe-remote#151
-- review_comment #3025433621 by chatgpt-codex-connector[bot] on modules/agents/opencode/server.py
- Avoid terminating active OpenCode server during auth refresh
- https://github.com/cyhhao/vibe-remote/pull/151#discussion_r3025433621
-```
-
-## Writing New Waiters
+- `exit 124`: timeout; still send a timeout follow-up
+- other non-zero: failure; no follow-up
-Keep new waiters small and boring. A good waiter:
+Keep the output split clean:
-- blocks until a single condition becomes true
-- prints one final summary to `stdout`
-- keeps polling logs and diagnostics on `stderr`
-- exits with `124` on timeout when a timeout matters
-
-The wrapper is the reusable part. Waiters should stay disposable and task-specific.
+- `stdout`: final summary for the next turn
+- `stderr`: polling logs and diagnostics
## Generic Examples
-### Delayed Follow-up
+Delay:
```bash
scripts/watch_then_hook.sh \
--session-key "slack::channel::C123::thread::171717.123" \
- --prefix "The delayed check completed. Continue from the waiter output below." \
+ --prefix "The delayed check completed. Continue from the result below." \
-- \
bash -lc 'sleep 120; echo "Timer finished after 120 seconds."'
```
-### File Appears
+File appears:
```bash
scripts/watch_then_hook.sh \
@@ -223,7 +94,7 @@ scripts/watch_then_hook.sh \
bash -lc 'while [ ! -f /tmp/export.json ]; do sleep 10; done; echo "Detected /tmp/export.json"'
```
-### Log Pattern
+Log match:
```bash
scripts/watch_then_hook.sh \
@@ -233,72 +104,76 @@ scripts/watch_then_hook.sh \
bash -lc 'tail -Fn0 /tmp/app.log | while read -r line; do case "$line" in *READY*) echo "$line"; break;; esac; done'
```
-## GitHub Example
-`scripts/watch_github_pr_then_hook.sh` is a convenience wrapper for one specific waiter pair: `watch_then_hook.sh` plus `wait_for_github_pr_activity.py`.
+## Session Targeting
+
+Use the current Vibe Remote context:
+
+- current channel if the follow-up can return there
+- current thread if the follow-up must stay in thread context
+- `--post-to channel` only when the user wants to keep thread context but publish in the parent channel
+
+If the current turn does not expose a usable target, ask instead of guessing.
-Its default timeout is 6 hours. Override it with `--timeout <seconds>` when the watch should end sooner or run longer.
+## Timeout And Lifecycle
+
+For the generic wrapper:
+
+- `--timeout` is the waiter timeout
+- default is `21600` seconds
+- `0` means no timeout
+
+For the GitHub convenience wrapper:
+
+- `--timeout` is still the timeout for one waiter cycle
+- `--forever` means re-arm after each detected event
+- `--lifetime-timeout` limits the whole long-running watch; default is `0` meaning run until killed
+
+This separation matters: a forever watcher can still use a bounded timeout for each polling cycle.
+
+## GitHub Convenience Wrapper
+
+Use the GitHub wrapper only when the watched thing is PR review activity.
+
+One-shot watch:
```bash
scripts/watch_github_pr_then_hook.sh \
--session-key "slack::channel::C123::thread::171717.123" \
--repo cyhhao/vibe-remote \
--pr 151 \
- --prefix "PR review changed. Check the latest GitHub review results and continue the review loop." \
- --interval 60 \
- --timeout 14400
+ --interval 60
```
-To catch up on comments or reviews that already exist before the watcher starts, add `--catch-up`:
+Catch up on existing activity first:
```bash
scripts/watch_github_pr_then_hook.sh \
--session-key "slack::channel::C123::thread::171717.123" \
--repo cyhhao/vibe-remote \
--pr 151 \
- --prefix "PR review already has activity. Pull the current review state and continue the thread." \
--catch-up
```
-If authentication is unavailable and a quick one-off best-effort watch is still acceptable, you may opt in explicitly:
-
-```bash
- scripts/watch_github_pr_then_hook.sh \
- --session-key "slack::channel::C123::thread::171717.123" \
- --repo cyhhao/vibe-remote \
- --pr 151 \
- --prefix "Best-effort unauthenticated GitHub watch fired. Inspect the latest PR state." \
- --allow-unauthenticated \
- --interval 180
-```
-
-To keep watching a PR continuously instead of exiting after the next event, use `--forever`:
+Stay armed for future activity:
```bash
scripts/watch_github_pr_then_hook.sh \
--session-key "slack::channel::C123::thread::171717.123" \
--repo cyhhao/vibe-remote \
--pr 151 \
- --forever
+ --forever \
+ --timeout 21600 \
+ --lifetime-timeout 86400
```
-`--forever` is intentionally long-lived:
-
-- it keeps the watcher process alive until you stop it
-- it advances its cursors after each detected event and waits for the next one
-- transient cycle failures are logged and retried instead of killing the watcher immediately
-- it must run with `--timeout 0`, so omit `--timeout` or pass `0` explicitly
-- always mention the lifecycle to the user and surface the PID/log path from the startup message
-
-## Failure Handling
+GitHub-specific notes:
-- If the waiter requires credentials, verify them before starting the background process.
-- If `vibe hook send` is not on `PATH`, stop and fix the environment instead of launching a broken watcher.
-- If the user wants a durable recurring watch shared across sessions, this pattern is not enough by itself; explain that it is best for one-off background waiting tied to the current task.
+- `--catch-up` reports activity that already exists at startup
+- without `--catch-up`, the waiter snapshots current PR activity as the baseline
+- authentication is preferred; unauthenticated polling is slower and more fragile
-## Common Mistakes
+## Practical Advice
-- launching the waiter in the background and the hook in the foreground
-- letting the waiter print noisy polling logs to `stdout`
-- hardcoding the wrong session target
-- forgetting that a channel follow-up and a thread follow-up use different `session_key` values
-- omitting a timeout when the user expects eventual cleanup
+- Keep prefixes action-oriented. Tell the next turn what to do with the waiter result.
+- Prefer the generic wrapper in the skill body. Treat GitHub as just one example.
+- If the execution host reaps detached children, run `--foreground` inside a long-lived terminal session instead of relying on background detaching.
diff --git a/skills/background-watch-hook/scripts/watch_github_pr_then_hook.sh b/skills/background-watch-hook/scripts/watch_github_pr_then_hook.sh
index 17fd36f98..c52be5d19 100755
--- a/skills/background-watch-hook/scripts/watch_github_pr_then_hook.sh
+++ b/skills/background-watch-hook/scripts/watch_github_pr_then_hook.sh
@@ -17,7 +17,8 @@ Options:
--pr <number> Required. Pull request number.
--prefix <value> Optional. Hook prefix. If omitted, a sensible default is used.
--interval <seconds> Optional. Polling interval passed to the waiter.
- --timeout <seconds> Optional. Overall waiter timeout. Default: 21600 (6 hours).
+ --timeout <seconds> Optional. Per-cycle waiter timeout. Default: 21600 (6 hours).
+ --lifetime-timeout <seconds> Optional. Overall lifetime for --forever. Default: 0 (run until killed).
--forever Optional. Keep watching and re-arm after each detected event.
--event-limit <count> Optional. Maximum rendered events.
--catch-up Optional. Treat existing PR activity as pending immediately.
@@ -42,7 +43,7 @@ pr=""
prefix=""
interval=""
timeout=""
-timeout_set=0
+lifetime_timeout="0"
forever=0
event_limit=""
catch_up=0
@@ -82,7 +83,10 @@ while [[ $# -gt 0 ]]; do
;;
--timeout)
timeout="${2:-}"
- timeout_set=1
+ shift 2
+ ;;
+ --lifetime-timeout)
+ lifetime_timeout="${2:-}"
shift 2
;;
--forever)
@@ -164,6 +168,11 @@ if [[ -n "$timeout_exit_code" && "$timeout_exit_code" != "124" ]]; then
exit 2
fi
+if [[ "$forever" -ne 1 && "$lifetime_timeout" != "0" ]]; then
+ echo "--lifetime-timeout requires --forever" >&2
+ exit 2
+fi
+
if [[ ! -x "$WRAPPER" ]]; then
echo "Wrapper not found or not executable: $WRAPPER" >&2
exit 127
@@ -174,13 +183,7 @@ if [[ ! -x "$WAITER" ]]; then
exit 127
fi
-if [[ "$forever" -eq 1 ]]; then
- if [[ "$timeout_set" -eq 1 && "$timeout" != "0" ]]; then
- echo "--forever watcher must use --timeout 0; omit --timeout or pass 0 explicitly" >&2
- exit 2
- fi
- timeout="0"
-elif [[ -z "$timeout" ]]; then
+if [[ -z "$timeout" ]]; then
timeout="21600"
fi
@@ -202,6 +205,7 @@ if [[ "$forever" -eq 1 && "$foreground" -ne 1 ]]; then
--pr "$pr"
--prefix "$prefix"
--timeout "$timeout"
+ --lifetime-timeout "$lifetime_timeout"
)
if [[ -n "$interval" ]]; then
@@ -248,9 +252,39 @@ if [[ "$forever" -eq 1 && "$foreground" -ne 1 ]]; then
exit 0
fi
+send_status_hook() {
+ local status_prefix="$1"
+ local status_message="$2"
+
+ local -a status_args=(
+ "$WRAPPER"
+ --foreground
+ --session-key "$session_key"
+ --prefix "$status_prefix"
+ --timeout 0
+ )
+
+ if [[ -n "$post_to" ]]; then
+ status_args+=(--post-to "$post_to")
+ fi
+ if [[ -n "$deliver_key" ]]; then
+ status_args+=(--deliver-key "$deliver_key")
+ fi
+ if [[ -n "$hook_bin" ]]; then
+ status_args+=(--hook-bin "$hook_bin")
+ fi
+ if [[ -n "$hook_cmd" ]]; then
+ status_args+=(--hook-cmd "$hook_cmd")
+ fi
+
+ status_args+=(-- python3 -c 'import sys; print(sys.argv[1])' "$status_message")
+ "${status_args[@]}"
+}
+
if [[ "$forever" -eq 1 ]]; then
cursor_file="$(mktemp)"
retry_delay_seconds=30
+ lifetime_started_at="$(date +%s)"
cleanup() {
rm -f "$cursor_file"
}
@@ -262,6 +296,54 @@ if [[ "$forever" -eq 1 ]]; then
current_catch_up="$catch_up"
while true; do
+ cycle_timeout="$timeout"
+ if [[ "$lifetime_timeout" != "0" ]]; then
+ lifetime_window="$(
+ python3 - "$lifetime_started_at" "$lifetime_timeout" <<'PY'
+import sys
+import time
+
+started_at = float(sys.argv[1])
+lifetime_timeout = float(sys.argv[2])
+remaining = lifetime_timeout - (time.time() - started_at)
+print(max(0.0, remaining))
+PY
+ )"
+
+ if python3 - "$lifetime_window" <<'PY'
+import sys
+
+raise SystemExit(0 if float(sys.argv[1]) <= 0 else 1)
+PY
+ then
+ elapsed_seconds="$(
+ python3 - "$lifetime_started_at" <<'PY'
+import sys
+import time
+
+print(int(max(0, time.time() - float(sys.argv[1]))))
+PY
+ )"
+ send_status_hook \
+ "GitHub PR watch stopped after reaching its lifetime timeout." \
+ "Lifetime timeout reached after ${elapsed_seconds} second(s) for ${repo}#${pr}. Last cursors: review=${current_since_review_id:-0} review_comment=${current_since_review_comment_id:-0} issue_comment=${current_since_issue_comment_id:-0}."
+ exit 0
+ fi
+
+ cycle_timeout="$(
+ python3 - "$timeout" "$lifetime_window" <<'PY'
+import sys
+
+cycle_timeout = float(sys.argv[1])
+remaining_lifetime = float(sys.argv[2])
+if cycle_timeout <= 0:
+ print(remaining_lifetime)
+else:
+ print(min(cycle_timeout, remaining_lifetime))
+PY
+ )"
+ fi
+
rm -f "$cursor_file"
cycle_wrapper_args=(
@@ -269,7 +351,7 @@ if [[ "$forever" -eq 1 ]]; then
--foreground
--session-key "$session_key"
--prefix "$prefix"
- --timeout "$timeout"
+ --timeout "$cycle_timeout"
)
if [[ -n "$post_to" ]]; then
@@ -289,7 +371,7 @@ if [[ "$forever" -eq 1 ]]; then
"$WAITER"
--repo "$repo"
--pr "$pr"
- --timeout "$timeout"
+ --timeout "$cycle_timeout"
--cursor-output "$cursor_file"
)