@quantiya/codevibe-gemini-plugin 1.0.19 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/codevibe-gemini +175 -7
  2. package/package.json +1 -1
@@ -43,6 +43,65 @@ done
43
43
  SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
44
44
  PLUGIN_DIR="$(dirname "$SCRIPT_DIR")"
45
45
 
46
+ # ─── Wrapper telemetry (GA4 Measurement Protocol) ─────────────────────
47
+ # Diagnoses agent CLI failures: pre-flight bailouts, fast-die patterns,
48
+ # whether SessionStart hook fired, exit code. Background curl, fail
49
+ # silently, no PII (hashed hostname + per-run random id only). Honors
50
+ # CODEVIBE_TELEMETRY_SOURCE=test for internal testing.
51
+ _CV_MID="G-GS74YEQTB8"
52
+ _CV_SEC="lAfOF6OxRzSQ-NsLBRjhAg"
53
+ _CV_CID="$(echo "$(uname -n)-$(id -u)" | (sha256sum 2>/dev/null || shasum -a 256 2>/dev/null || echo "anonymous-fallback ") | cut -c1-36)"
54
+ _CV_RUN_ID="$(head -c 16 /dev/urandom 2>/dev/null | od -An -tx1 | tr -d ' \n' | cut -c1-32)"
55
+ [ -z "$_CV_RUN_ID" ] && _CV_RUN_ID="fallback-$(date +%s)-$$"
56
+ _CV_AGENT="gemini"
57
+ _CV_SOURCE="${CODEVIBE_TELEMETRY_SOURCE:-production}"
58
+ _CV_STARTED_AT="$(date +%s)"
59
+ _CV_EXITED="" # set by terminal events; suppresses trap double-fire
60
+ _CV_PLUGIN_VERSION="$(node -p "require('$PLUGIN_DIR/package.json').version" 2>/dev/null || echo unknown)"
61
+ _CV_MCP_LOG="${CODEVIBE_TMPDIR}/codevibe-gemini-mcp.log"
62
+ _CV_MCP_LOG_BASELINE=0
63
+ if [ -f "$_CV_MCP_LOG" ]; then
64
+ _CV_MCP_LOG_BASELINE=$(wc -l < "$_CV_MCP_LOG" 2>/dev/null | tr -d ' ')
65
+ [ -z "$_CV_MCP_LOG_BASELINE" ] && _CV_MCP_LOG_BASELINE=0
66
+ fi
67
+ _CV_TMUX_STARTED="false"
68
+ _CV_AGENT_INVOKED="false"
69
+ _CV_AGENT_STARTED_AT=0
70
+ _CV_GEMINI_EXIT_FILE="${CODEVIBE_TMPDIR}/codevibe-gemini-exit-$$"
71
+
72
+ # Strip an arbitrary string down to a JSON-safe identifier alphabet.
73
+ # Removes anything that could break the hand-built JSON payload below
74
+ # (quotes, backslashes, ANSI escapes, control bytes, tabs, newlines).
75
+ # Truncates to 40 chars to bound the impact of pathological CLI version
76
+ # output. Caller is responsible for emptiness check after sanitize.
77
+ cv_sanitize() {
78
+ printf '%s' "$1" | LC_ALL=C tr -cd 'A-Za-z0-9._\- ' | cut -c1-40
79
+ }
80
+
81
+ # Sanitize trusted-but-still-string values that go into the payload
82
+ # (plugin version, source label) so future schema additions can't
83
+ # accidentally reintroduce a JSON-injection path.
84
+ _CV_PLUGIN_VERSION="$(cv_sanitize "$_CV_PLUGIN_VERSION")"
85
+ [ -z "$_CV_PLUGIN_VERSION" ] && _CV_PLUGIN_VERSION="unknown"
86
+ _CV_SOURCE="$(cv_sanitize "$_CV_SOURCE")"
87
+ [ -z "$_CV_SOURCE" ] && _CV_SOURCE="production"
88
+
89
+ cv_telem() {
90
+ local event="$1"; shift
91
+ local params="$*"
92
+ curl -s -X POST \
93
+ "https://www.google-analytics.com/mp/collect?measurement_id=${_CV_MID}&api_secret=${_CV_SEC}" \
94
+ -H "Content-Type: application/json" \
95
+ -d "{\"client_id\":\"${_CV_CID}\",\"events\":[{\"name\":\"${event}\",\"params\":{\"agent\":\"${_CV_AGENT}\",\"plugin_version\":\"${_CV_PLUGIN_VERSION}\",\"source\":\"${_CV_SOURCE}\",\"run_id\":\"${_CV_RUN_ID}\"${params:+,$params}}}]}" \
96
+ </dev/null >/dev/null 2>&1 &
97
+ }
98
+
99
+ cv_failed() {
100
+ [ -n "$_CV_EXITED" ] && return 0
101
+ _CV_EXITED="failed"
102
+ cv_telem "wrapper_failed" "\"reason\":\"$1\",\"lifetime_seconds\":$(( $(date +%s) - _CV_STARTED_AT ))"
103
+ }
104
+
46
105
  # Handle auth commands (login, logout, status, reset-device)
47
106
  # Delegate to codevibe-core CLI (shared auth across all plugins)
48
107
  case "$1" in
@@ -53,14 +112,42 @@ case "$1" in
53
112
  CORE_CLI="$PLUGIN_DIR/../codevibe-core/bin/codevibe.js"
54
113
  fi
55
114
  if [ -f "$CORE_CLI" ]; then
115
+ cv_telem "wrapper_started" "\"invocation\":\"auth_$1\",\"os\":\"$(uname -s | cv_sanitize)\",\"arch\":\"$(uname -m | cv_sanitize)\""
56
116
  exec node "$CORE_CLI" "$1"
57
117
  else
58
118
  echo "Error: codevibe-core not found. Try reinstalling: npm install -g @quantiya/codevibe"
119
+ cv_failed "core_not_found"
120
+ sleep 1
59
121
  exit 1
60
122
  fi
61
123
  ;;
62
124
  esac
63
125
 
126
+ # Capture environment facts for the session-flow wrapper_started event.
127
+ # Each probe is non-fatal — if a CLI is missing we record "missing" rather
128
+ # than aborting; pre-flight checks below still gate execution. Every
129
+ # string that lands in the JSON payload goes through cv_sanitize so an
130
+ # agent CLI emitting ANSI escapes or quotes in `--version` can't break
131
+ # the hand-built payload.
132
+ _CV_GEMINI_VER="missing"
133
+ command -v gemini >/dev/null 2>&1 && _CV_GEMINI_VER="$(gemini --version 2>/dev/null | cv_sanitize)"
134
+ [ -z "$_CV_GEMINI_VER" ] && _CV_GEMINI_VER="unknown"
135
+ _CV_NODE_VER="missing"
136
+ command -v node >/dev/null 2>&1 && _CV_NODE_VER="$(node -v 2>/dev/null | cv_sanitize)"
137
+ [ -z "$_CV_NODE_VER" ] && _CV_NODE_VER="unknown"
138
+ _CV_TMUX_VER="missing"
139
+ command -v tmux >/dev/null 2>&1 && _CV_TMUX_VER="$(tmux -V 2>/dev/null | cv_sanitize)"
140
+ [ -z "$_CV_TMUX_VER" ] && _CV_TMUX_VER="unknown"
141
+ _CV_OS_VER="$(uname -s | cv_sanitize)"
142
+ [ -z "$_CV_OS_VER" ] && _CV_OS_VER="unknown"
143
+ _CV_ARCH_VER="$(uname -m | cv_sanitize)"
144
+ [ -z "$_CV_ARCH_VER" ] && _CV_ARCH_VER="unknown"
145
+ _CV_GEMINI_AUTH="false"; [ -f "$HOME/.gemini/oauth_creds.json" ] && _CV_GEMINI_AUTH="true"
146
+ _CV_GEMINI_SETTINGS="false"; [ -f "$HOME/.gemini/settings.json" ] && _CV_GEMINI_SETTINGS="true"
147
+ _CV_INSIDE_TMUX="false"; [ -n "$TMUX" ] && _CV_INSIDE_TMUX="true"
148
+ _CV_IS_TTY="false"; { [ -t 0 ] && [ -t 1 ]; } && _CV_IS_TTY="true"
149
+ cv_telem "wrapper_started" "\"invocation\":\"session\",\"os\":\"$_CV_OS_VER\",\"arch\":\"$_CV_ARCH_VER\",\"gemini_version\":\"$_CV_GEMINI_VER\",\"node_version\":\"$_CV_NODE_VER\",\"tmux_version\":\"$_CV_TMUX_VER\",\"gemini_auth_present\":$_CV_GEMINI_AUTH,\"gemini_settings_present\":$_CV_GEMINI_SETTINGS,\"inside_tmux\":$_CV_INSIDE_TMUX,\"is_terminal\":$_CV_IS_TTY"
150
+
64
151
  # Export hooks directory for hook scripts to use
65
152
  export CODEVIBE_HOOKS_DIR="$PLUGIN_DIR/hooks"
66
153
 
@@ -130,7 +217,52 @@ log() {
130
217
 
131
218
  # Cleanup function to kill MCP server when wrapper exits
132
219
  cleanup() {
220
+ local wrapper_exit_code=$?
133
221
  log "Cleanup triggered"
222
+
223
+ # Fire wrapper_exited telemetry BEFORE killing the server so the MCP
224
+ # log is intact when we grep for SessionStart. cv_failed sets
225
+ # _CV_EXITED on pre-flight failures so this block won't double-fire.
226
+ if [ -z "$_CV_EXITED" ]; then
227
+ _CV_EXITED="exited"
228
+ local gemini_exit="unknown"
229
+ if [ -f "$_CV_GEMINI_EXIT_FILE" ]; then
230
+ gemini_exit="$(cat "$_CV_GEMINI_EXIT_FILE" 2>/dev/null | head -c 10 | tr -d '\n\r ')"
231
+ [ -z "$gemini_exit" ] && gemini_exit="unknown"
232
+ fi
233
+ local lifetime=$(( $(date +%s) - _CV_STARTED_AT ))
234
+ local gemini_lifetime=0
235
+ if [ "$_CV_AGENT_STARTED_AT" -gt 0 ] 2>/dev/null; then
236
+ gemini_lifetime=$(( $(date +%s) - _CV_AGENT_STARTED_AT ))
237
+ fi
238
+ local hook_fired="false"
239
+ if [ -f "$_CV_MCP_LOG" ]; then
240
+ if tail -n "+$((_CV_MCP_LOG_BASELINE + 1))" "$_CV_MCP_LOG" 2>/dev/null \
241
+ | grep -q "SessionStart" 2>/dev/null; then
242
+ hook_fired="true"
243
+ fi
244
+ fi
245
+ # Outcome priority: SIGINT/SIGTERM beats everything (user intent).
246
+ # Then "we never got far enough to invoke gemini" — distinct from
247
+ # "we invoked gemini via passthrough but never started a tmux of
248
+ # our own" (the latter is a normal direct-run, not an abort).
249
+ local outcome
250
+ if [ "$wrapper_exit_code" = "130" ] || [ "$wrapper_exit_code" = "143" ]; then
251
+ outcome="interrupted"
252
+ elif [ "$_CV_AGENT_INVOKED" = "false" ]; then
253
+ outcome="pre_invoke_abort"
254
+ elif [ "$gemini_exit" != "unknown" ] && [ "$gemini_exit" != "0" ]; then
255
+ outcome="error_exit"
256
+ elif [ "$gemini_lifetime" -lt 5 ] 2>/dev/null; then
257
+ outcome="early_exit"
258
+ elif [ "$gemini_lifetime" -lt 60 ] 2>/dev/null; then
259
+ outcome="clean_short"
260
+ else
261
+ outcome="clean_long"
262
+ fi
263
+ cv_telem "wrapper_exited" "\"exit_code\":$wrapper_exit_code,\"lifetime_seconds\":$lifetime,\"gemini_exit_code\":\"$gemini_exit\",\"gemini_lifetime_seconds\":$gemini_lifetime,\"tmux_session_started\":$_CV_TMUX_STARTED,\"agent_invoked\":$_CV_AGENT_INVOKED,\"session_start_hook_fired\":$hook_fired,\"terminal_outcome\":\"$outcome\""
264
+ fi
265
+
134
266
  if [ -n "$MCP_PID" ] && kill -0 "$MCP_PID" 2>/dev/null; then
135
267
  log "Stopping MCP server (PID: $MCP_PID)"
136
268
  kill "$MCP_PID" 2>/dev/null || true
@@ -138,6 +270,7 @@ cleanup() {
138
270
  fi
139
271
  # Remove PID file
140
272
  rm -f "${CODEVIBE_TMPDIR}/codevibe-gemini-mcp-$$.pid"
273
+ rm -f "$_CV_GEMINI_EXIT_FILE"
141
274
  }
142
275
 
143
276
  # Set up trap for cleanup
@@ -147,6 +280,8 @@ trap cleanup EXIT INT TERM
147
280
  if ! command -v tmux &> /dev/null; then
148
281
  echo "Error: tmux is required but not installed."
149
282
  echo "Install with: brew install tmux"
283
+ cv_failed "tmux_missing"
284
+ sleep 1
150
285
  exit 1
151
286
  fi
152
287
 
@@ -154,18 +289,24 @@ fi
154
289
  if ! command -v gemini &> /dev/null; then
155
290
  echo "Error: gemini CLI is not installed."
156
291
  echo "Install from: https://github.com/google-gemini/gemini-cli"
292
+ cv_failed "gemini_missing"
293
+ sleep 1
157
294
  exit 1
158
295
  fi
159
296
 
160
297
  # Check if node is installed
161
298
  if ! command -v node &> /dev/null; then
162
299
  echo "Error: Node.js is required but not installed."
300
+ cv_failed "node_missing"
301
+ sleep 1
163
302
  exit 1
164
303
  fi
165
304
 
166
305
  # Check if MCP server is built
167
306
  if [ ! -f "$PLUGIN_DIR/dist/server.js" ]; then
168
307
  echo "Error: MCP server not built. Run 'npm run build' in the plugin directory first."
308
+ cv_failed "server_not_built"
309
+ sleep 1
169
310
  exit 1
170
311
  fi
171
312
 
@@ -177,17 +318,36 @@ log "Starting codevibe-gemini with session: $SESSION_NAME"
177
318
  log "Working directory: $WORKING_DIR"
178
319
  log "Arguments: $*"
179
320
 
180
- # Check if we're already inside tmux
321
+ # Check if we're already inside tmux.
322
+ # We deliberately do NOT `exec` here — running gemini as a child process
323
+ # lets the EXIT trap fire after it returns so wrapper_exited still gets
324
+ # emitted on these direct-run paths. Behaviorally identical for the user
325
+ # (gemini remains the foreground process for the duration).
181
326
  if [ -n "$TMUX" ]; then
182
327
  log "Already inside tmux, running gemini directly"
183
- # Already in tmux, just run gemini
184
- exec gemini "$@"
328
+ _CV_AGENT_INVOKED="true"
329
+ _CV_AGENT_STARTED_AT="$(date +%s)"
330
+ # `|| _CV_RC=$?` is load-bearing: with `set -e`, a non-zero exit
331
+ # from gemini would abort the wrapper before we capture the exit
332
+ # code, leaving wrapper_exited with gemini_exit_code="unknown". The
333
+ # `||` form catches non-zero without triggering set -e, while exit
334
+ # 0 leaves _CV_RC at its 0 default. printf's `|| true` keeps a
335
+ # disk-full failure from clobbering diagnostics.
336
+ _CV_RC=0
337
+ gemini "$@" || _CV_RC=$?
338
+ printf '%s' "$_CV_RC" > "$_CV_GEMINI_EXIT_FILE" 2>/dev/null || true
339
+ exit "$_CV_RC"
185
340
  fi
186
341
 
187
- # Check if running in a terminal
342
+ # Check if running in a terminal — same direct-run treatment as above.
188
343
  if [ ! -t 0 ] || [ ! -t 1 ]; then
189
344
  log "Not running in a terminal, running gemini directly"
190
- exec gemini "$@"
345
+ _CV_AGENT_INVOKED="true"
346
+ _CV_AGENT_STARTED_AT="$(date +%s)"
347
+ _CV_RC=0
348
+ gemini "$@" || _CV_RC=$?
349
+ printf '%s' "$_CV_RC" > "$_CV_GEMINI_EXIT_FILE" 2>/dev/null || true
350
+ exit "$_CV_RC"
191
351
  fi
192
352
 
193
353
  # Start MCP server in background BEFORE launching Gemini
@@ -214,6 +374,8 @@ if ! kill -0 "$MCP_PID" 2>/dev/null; then
214
374
  tail -3 "$MCP_LOG_FILE" 2>/dev/null | grep -v '^\[' | head -1
215
375
  echo ""
216
376
  echo "Server failed to start. Check $MCP_LOG_FILE for details."
377
+ cv_failed "server_died_on_startup"
378
+ sleep 1
217
379
  exit 1
218
380
  fi
219
381
 
@@ -232,10 +394,16 @@ done
232
394
  # We use a wrapper that:
233
395
  # 1. Exports the session name so prompts can find it
234
396
  # 2. Runs gemini
235
- # 3. Exits the tmux session when gemini exits
397
+ # 3. Captures gemini's exit code to $_CV_GEMINI_EXIT_FILE for the
398
+ # wrapper's cleanup trap (tmux's own attach exit code is independent
399
+ # of the inner process exit, so the file is the only reliable signal)
400
+ # 4. Exits the tmux session when gemini exits
236
401
 
237
402
  tmux new-session -d -s "$SESSION_NAME" -x "$(tput cols)" -y "$(tput lines)" \
238
- "export CODEVIBE_GEMINI_TMUX_SESSION='$SESSION_NAME'; export ENVIRONMENT='$ENVIRONMENT'; $GEMINI_CMD; exit"
403
+ "export CODEVIBE_GEMINI_TMUX_SESSION='$SESSION_NAME'; export ENVIRONMENT='$ENVIRONMENT'; $GEMINI_CMD; printf '%s' \"\$?\" > '$_CV_GEMINI_EXIT_FILE'; exit"
404
+ _CV_TMUX_STARTED="true"
405
+ _CV_AGENT_INVOKED="true"
406
+ _CV_AGENT_STARTED_AT="$(date +%s)"
239
407
 
240
408
  # Enable mouse support for scrolling
241
409
  tmux set-option -t "$SESSION_NAME" -g mouse on
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantiya/codevibe-gemini-plugin",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "Control Gemini CLI from your iPhone and Android — real-time sync, approve file edits, send prompts by voice. Part of CodeVibe.",
5
5
  "main": "dist/server.js",
6
6
  "bin": {