Skip to content

fix(activity): detect git commits via tool_result event instead of command.executed#58

Open
TestardR wants to merge 1 commit into
DEVtheOPS:mainfrom
TestardR:fix/cost-histogram-buckets-and-commit-detection
Open

fix(activity): detect git commits via tool_result event instead of command.executed#58
TestardR wants to merge 1 commit into
DEVtheOPS:mainfrom
TestardR:fix/cost-histogram-buckets-and-commit-detection

Conversation

@TestardR
Copy link
Copy Markdown

@TestardR TestardR commented May 25, 2026

Problem

opencode.commit.count never increments, regardless of how many git commit invocations a session makes.

Root cause

The old handleCommandExecuted listens to command.executed events filtered by name === "bash". But command.executed in opencode is for slash commands (e.g. /clear), not for bash tool calls — it fires at session.idle and never carries name === "bash". The handler was wired to the wrong event.

Fix

Bash tool completions surface as EventMessagePartUpdated with part.type === "tool" and state.status === "completed". Move commit detection there as handleToolResult(toolName, toolInput, sessionID, ctx), called from handleMessagePartUpdated when success === true.

  • Match against both command and description fields of the bash tool input.
  • Counter increments once per matching tool call (no double-count between fields).
  • Removes the dead handleCommandExecuted handler, its switch case in src/index.ts, and the unused EventCommandExecuted import.

Strictly a bug fix — commits that previously went uncounted will now be counted.


Companion PR: #59

Summary by CodeRabbit

  • Refactor
    • Updated commit tracking detection to use tool result events instead of command execution events, improving event handling consistency.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: af259059-c787-49c9-9433-8c12adde2310

📥 Commits

Reviewing files that changed from the base of the PR and between e50a9ed and 6d0dd14.

📒 Files selected for processing (6)
  • src/handlers/activity.ts
  • src/handlers/message.ts
  • src/index.ts
  • tests/handlers/activity.test.ts
  • tests/handlers/disabled-metrics.test.ts
  • tests/handlers/disabled-signals.test.ts

📝 Walkthrough

Walkthrough

This PR migrates commit instrumentation from event-driven bash command detection to tool-result-driven detection. The new handleToolResult function detects git commit invocations in bash tool inputs, increments commit counters, and emits log events. Event routing is updated to dispatch session.diff events, and tool completion handling integrates the new commit detection logic. Test suites are updated to validate the new behavior across normal and disabled scenarios.

Changes

Commit Detection Refactor

Layer / File(s) Summary
handleToolResult implementation
src/handlers/activity.ts
Replaces handleCommandExecuted with handleToolResult, which detects git commit invocations in bash tool inputs by regex-matching command and description fields, increments commit.count metrics when enabled, and emits commit log events with sessionID.
Event dispatch wiring update
src/index.ts
Updates plugin event routing to dispatch session.diff events via handleSessionDiff and removes command.executed event handling. Adjusts SDK type imports from EventCommandExecuted to EventSessionDiff.
Tool completion handler integration
src/handlers/message.ts
Adds call to handleToolResult when tool parts complete successfully, passing tool name, input, session ID, and handler context before existing logging behavior.
Handler test validations
tests/handlers/activity.test.ts, tests/handlers/disabled-metrics.test.ts, tests/handlers/disabled-signals.test.ts
Replaces handleCommandExecuted tests with handleToolResult test suite covering git commit detection patterns (including --amend and echo-embedded cases), non-bash tool ignoring, double-count prevention, and log emission. Updates disabled-metrics and disabled-signals scenarios to use handleToolResult with bash tool helpers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • DEVtheOPS/opencode-plugin-otel#8: Directly overlaps with this PR's refactor of git-commit instrumentation in src/handlers/activity.ts by replacing handleCommandExecuted with handleToolResult for commit detection, counting, and logging.

Suggested reviewers

  • dialupdisaster
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: moving git commit detection from command.executed events to tool_result events, which is the core fix implemented across all modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

…mmand.executed

The commit counter never incremented in production because the handler

listened on command.executed and gated on name === "bash". In opencode,

command.executed is emitted for slash commands only (e.g. /commit, /clear) and

is published at session.idle time — it is never fired for bash tool

invocations, so the regex match could never run.

Move detection into the tool-completion branch of handleMessagePartUpdated,

where bash tool calls actually surface (part.type === "tool" with

state.status === "completed"). The new handleToolResult helper checks both

the bash command input (literal shell command) and the description input

(agent-provided short description) against the same git commit regex.

Only successful tool calls are counted; errored bash invocations do not bump

the counter, matching the prior intent that only completed commits are

observable.

The dead handleCommandExecuted handler and its command.executed switch case

are removed along with the now-unused EventCommandExecuted import.
@TestardR TestardR force-pushed the fix/cost-histogram-buckets-and-commit-detection branch from b04baac to 6d0dd14 Compare May 25, 2026 08:05
@TestardR TestardR changed the title fix: cents-scale cost histogram buckets + fix commit detection event source fix(activity): detect git commits via tool_result event instead of command.executed May 25, 2026
@TestardR TestardR marked this pull request as ready for review May 25, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant