diff options
| author | Enrico Carlesso <enricocarlesso@gmail.com> | 2026-07-24 12:05:20 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-24 22:05:20 +0300 |
| commit | e9b22084e03880f43d1f1daaf5d9f45c717b77c4 (patch) | |
| tree | 343c3099fcb0b935f857b7eb00df1e4d9c83d609 /src/integration/assets | |
| parent | b56baee9d60a002b5d8c0fe74d5032afad27ef30 (diff) | |
feat: add grok cli integration for native session restore (#1807)
* feat: add grok cli integration for native session restore
tl;dr: adds `herdr integration install grok` (session-identity tier: a
SessionStart hook reports the grok session id for native restore; state
stays screen-detected via the bundled manifest).
Grok merges every ~/.grok/hooks/*.json, so install writes a
self-contained hooks/herdr.json registering a SessionStart hook next to
hooks/herdr-agent-state.sh, and never touches other hook files. The hook
prefers $GROK_SESSION_ID (injected into every grok hook process) and
reports through the socket API; herdr:grok is a reserved-native-state
source so screen detection stays authoritative. Restore uses
`grok --resume <id>`. Additive JSON API change; no protocol bump; the
schema artifact is regenerated.
refs #1800
* fix: honor GROK_HOME and validate grok hook config in status
Review follow-ups: grok_dir now resolves GROK_CONFIG_DIR (herdr test
seam) before GROK_HOME (the grok CLI's own config-home override) before
~/.grok, so hook installs land where grok actually looks. Integration
status now also validates the herdr-owned hooks/herdr.json next to the
hook script — missing, corrupt, or no-longer-referencing configs report
the install as outdated instead of masking a hook grok never invokes.
refs #1800
* fix: validate the exact grok session hook command in status
The config validator matched on a filename substring, so a SessionStart
command like `echo herdr-agent-state.sh`, or one invoking the script
without the `session` action, still reported the integration as current
while grok never delivered a session id. Status now compares against the
exact command the installer writes, via a shared builder, and the
degradation tests cover both nonfunctional shapes.
refs #1800
* fix: require a command-type grok session hook in status validation
A config entry carrying the exact generated command under a non-command
hook type (e.g. "http") is never executed by grok, so it must not report
the integration as current. The validator now also requires
type = "command", with a degradation test for the mismatched-type shape.
refs #1800
* fix: validate the complete grok hook config
refs #1800
---------
Co-authored-by: Ogulcan Celik <ogulcancelik@gmail.com>
Diffstat (limited to 'src/integration/assets')
| -rw-r--r-- | src/integration/assets/grok/herdr-agent-state.sh | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/integration/assets/grok/herdr-agent-state.sh b/src/integration/assets/grok/herdr-agent-state.sh new file mode 100644 index 00000000..bdf0f671 --- /dev/null +++ b/src/integration/assets/grok/herdr-agent-state.sh @@ -0,0 +1,99 @@ +#!/bin/sh +# installed by herdr +# managed by herdr; reinstalling or updating the integration overwrites this file. +# add custom hooks beside this file instead of editing it. +# HERDR_INTEGRATION_ID=grok +# HERDR_INTEGRATION_VERSION=1 + +set -eu + +action="${1:-}" +hook_input_file="$(mktemp "${TMPDIR:-/tmp}/herdr-grok-hook.XXXXXX")" || exit 0 +trap 'rm -f "$hook_input_file"' EXIT HUP INT TERM +cat >"$hook_input_file" 2>/dev/null || true + +case "$action" in + session) ;; + *) exit 0 ;; +esac + +[ "${HERDR_ENV:-}" = "1" ] || exit 0 +[ -n "${HERDR_SOCKET_PATH:-}" ] || exit 0 +[ -n "${HERDR_PANE_ID:-}" ] || exit 0 +command -v python3 >/dev/null 2>&1 || exit 0 + +HERDR_ACTION="$action" HERDR_HOOK_INPUT_FILE="$hook_input_file" python3 - <<'PY' +import json +import os +import random +import socket +import time + +source = "herdr:grok" +pane_id = os.environ.get("HERDR_PANE_ID") +socket_path = os.environ.get("HERDR_SOCKET_PATH") +hook_input_file = os.environ.get("HERDR_HOOK_INPUT_FILE") + +if not pane_id or not socket_path: + raise SystemExit(0) + +hook_input = {} +if hook_input_file: + try: + with open(hook_input_file, encoding="utf-8") as handle: + content = handle.read() + if content.strip(): + hook_input = json.loads(content) + except Exception: + hook_input = {} + + +def first_text(*keys): + for key in keys: + value = hook_input.get(key) + if isinstance(value, str) and value: + return value + return None + + +# Backstop: only report on session-start payloads. Grok emits +# "session_start" and accepts the Claude/Cursor spellings, so tolerate all +# three; a missing field is allowed for forward compatibility. +hook_event_name = first_text("hook_event_name", "hookEventName") +if hook_event_name not in (None, "session_start", "SessionStart", "sessionStart"): + raise SystemExit(0) + +# Grok injects GROK_SESSION_ID into every hook process; prefer it and fall +# back to the event payload's session id fields. +session_id = os.environ.get("GROK_SESSION_ID") or first_text("session_id", "sessionId") +agent_session_id = session_id if isinstance(session_id, str) and session_id else None +if not agent_session_id: + raise SystemExit(0) + +request_id = f"{source}:{int(time.time() * 1000)}:{random.randrange(1_000_000):06d}" +report_seq = time.time_ns() +request = { + "id": request_id, + "method": "pane.report_agent_session", + "params": { + "pane_id": pane_id, + "source": source, + "agent": "grok", + "seq": report_seq, + "agent_session_id": agent_session_id, + }, +} + +try: + client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + client.settimeout(0.5) + client.connect(socket_path) + client.sendall((json.dumps(request) + "\n").encode()) + try: + client.recv(4096) + except Exception: + pass + client.close() +except Exception: + pass +PY |
