Skip to content

fix(mcp): await PostHog flush for local tool-call telemetry - #8733

Closed
joaovictor91123 wants to merge 1 commit into
JSONbored:mainfrom
joaovictor91123:fix/mcp-local-telemetry-flush-8690
Closed

fix(mcp): await PostHog flush for local tool-call telemetry#8733
joaovictor91123 wants to merge 1 commit into
JSONbored:mainfrom
joaovictor91123:fix/mcp-local-telemetry-flush-8690

Conversation

@joaovictor91123

Copy link
Copy Markdown
Contributor

Summary

  • Make local recordMcpToolCall async and await client.flush() before returning, mirroring the remote MCP telemetry fix (MCP tool-call telemetry fires without waitUntil, unlike this repo's own scheduleAfterResponse pattern #7233) so stdio tool events are not silently dropped when the process exits shortly after a call.
  • Await telemetry from recordStdioToolTelemetry / registerStdioTool so every stdio tool path holds until flush completes (or definitively fails).
  • Extend test/unit/mcp-local-telemetry.test.ts with flush spies, a delayed-flush await assertion, and a never-throw flush-failure case. Opted-out / unconfigured paths remain no-ops.

Client lifetime: still constructs a fresh PostHog client per call (same as the remote wrapper) rather than reusing one across the process - each call's flush is self-contained and does not depend on process-exit hooks.

Closes #8690

Scope

  • The PR title follows type(scope): short summary Conventional Commit format, for example fix(api): restore profile access checks.
  • This PR is focused and does not mix unrelated backend, UI, MCP, docs, dependency, and deploy changes.
  • This follows CONTRIBUTING.md and does not reintroduce GitHub Pages, VitePress, site/, or CNAME.
  • I linked a currently open issue this PR resolves (e.g. Closes #123) - a linked open issue is required for every contributor PR.

Validation

  • git diff --check
  • npm run actionlint
  • npm run typecheck
  • npm run test:coverage locally; codecov/patch requires ?99% coverage of the lines AND branches you changed (aim for 100% on your diff so CI variance does not fail near the threshold). Global coverage is a non-blocking trend with a loose 90% backstop, not the gate.
  • npm run test:workers
  • npm run build:mcp
  • npm run test:mcp-pack
  • npm run ui:openapi:check
  • npm run ui:lint
  • npm run ui:typecheck
  • npm run ui:build
  • npm audit --audit-level=moderate
  • New or changed behavior has unit/integration tests for new branches, fallback paths, and sanitizer boundaries

If any required check was skipped, explain why:

  • Local Node is v24; repo engines pin Node 22 - unit coverage for this change is in test/unit/mcp-local-telemetry.test.ts and will run under CI's pinned Node. Remaining scripts are unchanged MCP lib/bin paths only (no UI/OpenAPI/workers surface).

Safety

  • No secrets, wallet details, hotkeys, coldkeys, user PATs, private keys, raw trust scores, private rankings, or private maintainer evidence are exposed.
  • Public GitHub text stays sanitized, low-noise, and does not imply compensation guarantees or optimization tactics.
  • Auth, cookie, CORS, GitHub App, Cloudflare, or session changes include negative-path tests.
  • API/OpenAPI/MCP behavior is updated and tested where needed.
  • UI changes use live API data or real empty/error/loading states, not production mock/demo fallbacks.
  • Visible UI changes include a UI Evidence section below with JPG/JPEG or PNG screenshots arranged as organized, captioned, clickable thumbnails. SVG screenshots are not used as review evidence. Review-only screenshots or recordings are not committed to the repository.
  • Public docs/changelogs are updated where needed; changelogs are only edited for release-prep PRs.

UI Evidence

N/A - no visible UI changes.

Notes

  • Patch coverage targets the previously fire-and-forget capture path via an awaited flush and a delayed-flush unit test so codecov/patch sees the new async branch.

Mirror the remote MCP telemetry path so stdio tool events are not dropped when the process exits shortly after a call.

Closes JSONbored#8690

Co-authored-by: Cursor <cursoragent@cursor.com>
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.30%. Comparing base (fac2222) to head (6ab71b6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/loopover-mcp/bin/loopover-mcp.ts 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #8733       +/-   ##
===========================================
- Coverage   93.80%   82.30%   -11.51%     
===========================================
  Files         797       98      -699     
  Lines       79480    24764    -54716     
  Branches    24080     4747    -19333     
===========================================
- Hits        74554    20381    -54173     
- Misses       3555     4205      +650     
+ Partials     1371      178     -1193     
Flag Coverage Δ
backend 0.61% <25.00%> (-94.47%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/loopover-mcp/lib/telemetry.ts 100.00% <100.00%> (ø)
packages/loopover-mcp/bin/loopover-mcp.ts 0.00% <0.00%> (-54.52%) ⬇️

... and 699 files with indirect coverage changes

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 26, 2026
@loopover-orb

loopover-orb Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Caution

🛑 LoopOver review result - fixes required

Review updated: 2026-07-26 01:34:36 UTC

3 files · 1 AI reviewer · no blockers · CI failing · unstable

🛑 Suggested Action - Fix Blockers

Review summary
This PR makes local MCP telemetry recording async and awaits `client.flush()` before returning, mirroring the remote wrapper's #7233 fix, and threads the await through `recordStdioToolTelemetry`/`registerStdioTool` so both the success and error paths hold until flush completes. The core wiring is correct: `recordMcpToolCall` awaits flush inside the same try/catch (so a flush failure is still swallowed, matching the never-throw contract), and the test suite adds a proper deferred-flush case that proves the promise doesn't resolve until flush releases, plus a flush-failure case. The codecov/patch failure (25% vs 99% target) is real but is CI status, not a code defect for me to diagnose blindly.

Nits — 5 non-blocking
  • packages/loopover-mcp/bin/loopover-mcp.ts:1728-1734 — both call sites now `await recordStdioToolTelemetry`, which means a tool's response is now held up by the telemetry flush latency (a network round trip) before returning to the MCP client; worth confirming this added latency on every tool call is acceptable versus fire-and-forgetting the flush.
  • The `long-file`/`explicit any` flags on bin/loopover-mcp.ts are pre-existing conditions of the file, not something this diff introduces — not actionable against this PR.
  • test/unit/mcp-local-telemetry.test.ts's new 'never throws when flush itself fails' test asserts `flushSpy` was called and capture succeeded, but doesn't assert on timing ordering relative to the delayed-flush test above — minor duplication of setup could be a shared helper.
  • Consider whether flush should be raced against a short timeout so a hung network path can't stall every stdio tool call indefinitely, given it's now on the synchronous-await critical path of every tool response.
  • packages/loopover-mcp/lib/telemetry.ts:29-40 — the doc comment is thorough; a one-line note on the new latency cost per call (network RTT) would help future readers understand the tradeoff being made.

CI checks failing

  • codecov/patch — 25.00% of diff hit (target 99.00%)

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #8690
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ✅ 25/25 PR body includes validation/test evidence.
Contributor workload ✅ 10/10 Author activity: 161 registered-repo PR(s), 73 merged, 5 issue(s).
Contributor context ✅ Confirmed Gittensor contributor joaovictor91123; Gittensor profile; 161 PR(s), 5 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The diff makes recordMcpToolCall async and awaits client.flush() before returning, propagates the await through recordStdioToolTelemetry/registerStdioTool, explicitly states the per-call client lifetime choice in the PR description, and extends the test file with a delayed-flush assertion proving the promise doesn't resolve until flush completes, plus a never-throw-on-flush-failure case and unchan

Review context
  • Author: joaovictor91123
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: Python, JavaScript, MDX, TypeScript, C++, CSS, Rust
  • Official Gittensor activity: 161 PR(s), 5 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Keep the PR focused and include validation evidence before maintainer review.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask &lt;question&gt; answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat &lt;question&gt; answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@loopover-orb

loopover-orb Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

LoopOver is closing this pull request on the maintainer's behalf (CI is failing (codecov/patch)). This is an automated maintenance action — to pursue this change, please open a new pull request with the issues resolved. Closed PRs may be analyzed later to improve review accuracy, but they are not automatically reopened or re-reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(mcp): local MCP CLI telemetry never awaits/flushes its PostHog client before the process can exit

1 participant