Skip to content

fix(ai-usage): record embeddings + self-host vision, fix advisory-routed model attribution - #5247

Merged
JSONbored merged 2 commits into
mainfrom
claude/ai-usage-instrumentation
Jul 12, 2026
Merged

fix(ai-usage): record embeddings + self-host vision, fix advisory-routed model attribution#5247
JSONbored merged 2 commits into
mainfrom
claude/ai-usage-instrumentation

Conversation

@JSONbored

Copy link
Copy Markdown
Owner

Summary

Phase B1 of the Grafana/AI-usage observability audit: the underlying ai_usage_events
data had real gaps that a dashboard consolidation (Phase B2, follow-up PR) would just
have visualized incorrectly otherwise.

  • Embeddings were never recorded at all. src/review/rag.ts's inference calls
    (via src/review/adapters.ts's reviewInferenceAdapter) had zero ai_usage_events
    rows. Now every embedding call records under feature: "embeddings", using the real
    reported provider/model when available and falling back to the requested model id
    otherwise (e.g. the Workers AI binding, which reports no usage).
  • Self-host/Ollama vision was never recorded. runVisualVisionForAdvisory and
    runScreenshotTableVisionForAdvisory only recorded their BYOK branches; the
    env.AI_VISION self-host branches recorded nothing. Both now record under
    visual_vision/screenshot_table_vision with a nullable providerKey distinguishing
    BYOK (byok:<provider>) from self-host (real reported model, or a static fallback
    label when the provider reports none).
  • Advisory-routed calls had a stale/wrong model label. When ai-slop,
    ai-e2e-test-gen, linked-issue-satisfaction, and the two ai-summaries paths route
    through settings.advisoryAiRouting to env.AI_ADVISORY, the recorded model column
    was a hardcoded legacy Workers-AI id (or, for ai-summaries, always an empty string)
    instead of the real serving model. Fixed at the root in createOpenAiCompatibleAi
    (src/selfhost/ai.ts, from a prior commit already on this branch) so every caller now
    gets correct provider/model attribution; this PR updates the 4 consumer call sites to
    prefer that real value over their hardcoded fallback.

Test plan

  • npm run typecheck — clean
  • npm run test:ci — full local gate green (742+ test files / 14.7k+ tests, engine,
    workers, MCP, UI suites all pass)
  • npm audit --audit-level=moderate — 0 vulnerabilities
  • New/updated unit tests cover both branches of every changed conditional: real-model
    attribution vs. fallback-label, BYOK vs. self-host, success vs. no-usable-output,
    and a thrown-error/rethrow path for the new embeddings recording wrapper.

…ry-routed model attribution

Embeddings (src/review/rag.ts's inference calls) and self-host vision analysis
(runVisualVisionForAdvisory/runScreenshotTableVisionForAdvisory's AI_VISION branches)
were never recorded in ai_usage_events at all, leaving two whole AI features invisible
to any usage dashboard. Also, advisory-routed calls (slop/e2e-test-gen/linked-issue-
satisfaction/summaries routed through AI_ADVISORY) always recorded a stale hardcoded
model label instead of the real model the provider reported, understating self-host
Ollama usage and misattributing it to a legacy Workers-AI id.

Fixes the root cause once in createOpenAiCompatibleAi (src/selfhost/ai.ts) so every
caller gets real provider/model attribution for free, then updates each of the four
affected features plus the new embeddings/vision recording call sites.
@JSONbored JSONbored self-assigned this Jul 12, 2026
@superagent-security

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.35%. Comparing base (d3b9707) to head (44358d3).
⚠️ Report is 5 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5247   +/-   ##
=======================================
  Coverage   94.34%   94.35%           
=======================================
  Files         473      473           
  Lines       39982    40017   +35     
  Branches    14576    14590   +14     
=======================================
+ Hits        37722    37757   +35     
  Misses       1585     1585           
  Partials      675      675           
Flag Coverage Δ
shard-1 46.30% <32.78%> (-0.13%) ⬇️
shard-2 34.64% <47.54%> (+0.04%) ⬆️
shard-3 32.10% <39.34%> (-0.06%) ⬇️
shard-4 31.83% <19.67%> (-0.26%) ⬇️
shard-5 33.74% <26.22%> (+0.02%) ⬆️
shard-6 45.05% <4.91%> (+0.19%) ⬆️

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

Files with missing lines Coverage Δ
src/queue/processors.ts 95.63% <100.00%> (+0.01%) ⬆️
src/review/adapters.ts 96.42% <100.00%> (+1.98%) ⬆️
src/selfhost/ai.ts 98.12% <100.00%> (+0.04%) ⬆️
src/services/ai-e2e-test-gen.ts 100.00% <100.00%> (ø)
src/services/ai-slop.ts 93.42% <100.00%> (ø)
src/services/ai-summaries.ts 99.16% <100.00%> (+0.01%) ⬆️
src/services/linked-issue-satisfaction-run.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

loopover-orb Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Caution

🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥

🛑 Gittensory review result - fixes required

Review updated: 2026-07-12 11:39:14 UTC

15 files · 1 AI reviewer · 2 blockers · readiness 93/100 · CI failing · unstable

🛑 Suggested Action - Manual Review

  • No linked issue detected — If this PR is intended to solve an issue, link it explicitly in the PR body.
  • Maintainer requires a linked issue — Link the relevant issue (for example Closes #123) before opening the PR.

Review summary
This PR fixes three real gaps in ai_usage_events: embeddings were never recorded (now wired through a new reviewInferenceAdapter(env, ai) that records a usage row on both success and failure), self-host/Ollama vision calls were silently dropped (runSelfHostVisualVision now returns {text, usage} and both visual_vision/screenshot_table_vision recorders accept a nullable providerKey to distinguish byok:<provider> from a real/fallback self-host label), and four advisory-routed callers (ai-slop, ai-e2e-test-gen, linked-issue-satisfaction, both ai-summaries paths) now prefer usage?.model over their stale hardcoded/empty-string fallback. The wiring is traced correctly end-to-end (createOpenAiCompatibleAi now returns usage with provider/model on both embedding and chat paths via the new buildAiUsage helper), and each behavior change (real-model path, fallback-label path, error path) has a matching new test with concrete D1 row assertions. The one loose end is that the self-host branch in runScreenshotTableVisionForAdvisory records usage once immediately (with `null`/generic detail) before the shared `if (visionText)` parse step, mirroring the pre-existing BYOK branch's structure rather than introducing a new bug.

Blockers

  • PR description: this PR does not close or clearly link an eligible open issue for the ai-usage observability audit, so I need you to add the maintainer-authorized issue reference or explain why this is maintainer-authored before merge.
Nits — 5 non-blocking
  • The PR description doesn't link to a specific tracked issue number for this Phase B1 audit work — worth confirming this fix chain is maintainer-authorized/tracked even though it reads as legitimate internal follow-up rather than scope creep.
  • codecov/patch failed at 93.33% vs the 99% target (per CI status) — worth identifying which changed branches (e.g. the `usage?.model ?? fallback` nullish arms, or an error path in the new reviewInferenceAdapter) are uncovered before merge.
  • `SELF_HOST_VISION_MODEL_FALLBACK = "ollama:visual-vision"` is hardcoded in src/queue/processors.ts even though the binding could theoretically be a non-Ollama self-host provider; consider naming it more generically (e.g. `SELF_HOST_VISION_FALLBACK_LABEL`) since it's just a label, not a functional issue.
  • In src/selfhost/ai.ts's chat `run`, `provider: opts.providerName` unconditionally overrides any `usage.provider` that `extractCliUsage` might already contain — reads correctly per the added comment, but worth a one-line note that this assumes extractCliUsage never surfaces its own provider field on an HTTP JSON response.
  • Double check whether `reviewInferenceAdapter`'s `run` in src/review/adapters.ts is ever invoked for anything other than embedding batches from rag.ts — if any non-embedding call path exists, it would now be mis-recorded under the `embeddings` feature.

Why this is blocked

  • No linked issue detected — If this PR is intended to solve an issue, link it explicitly in the PR body.
  • Maintainer requires a linked issue — Link the relevant issue (for example Closes #123) before opening the PR.

CI checks failing

  • codecov/patch — 93.33% of diff hit (target 99.00%)
Signal Result Evidence
Code review ❌ 2 blockers 1 reviewer
Linked issue ⚠️ Missing No linked issue or no-issue rationale found.
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 (no linked issue context).
Validation posture ✅ 25/25 PR body includes validation/test evidence.
Contributor workload ✅ 10/10 Author activity: 44 registered-repo PR(s), 36 merged, 470 issue(s).
Contributor context ✅ Confirmed Gittensor contributor JSONbored; Gittensor profile; 44 PR(s), 470 issue(s).
Gate result ❌ Blocking Repo-configured hard blocker found.
Improvement ✅ Minor risk: clean · value: minor — Code changes are accompanied by test evidence.
Review context
  • Author: JSONbored
  • Role context: owner (maintainer lane)
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: Python, TypeScript, Ruby, Go, JavaScript, MDX, Shell, Solidity
  • Official Gittensor activity: 44 PR(s), 470 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Treat this as maintainer-lane context rather than normal contributor-lane activity.
  • Explain no-issue PR.
  • Link the issue being solved, or explicitly explain why this is a no-issue PR.
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.
[BETA] Chat with Gittensory

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

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

Full command reference: https://gittensory.aethereal.dev/docs/gittensory-commands

🟩 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 Gittensory, a quiet PR intelligence layer for OSS maintainers.

  • Re-run Gittensory review

@loopover-orb loopover-orb Bot added the manual-review Gittensor contributor context label Jul 12, 2026
…sage patch

adapters.ts's new embedding-error handler never exercised its non-Error-thrown
branch, and buildAiUsage's model/costUsd/effort branches are structurally
unreachable through its 3 real call sites (none ever pass costUsd/effort, and
model is always defined) -- add a direct unit test via a new
__selfHostAiInternals export, matching this repo's existing internals-export
convention for testing private helpers.
@JSONbored
JSONbored merged commit a6d93c6 into main Jul 12, 2026
14 checks passed
@JSONbored
JSONbored deleted the claude/ai-usage-instrumentation branch July 12, 2026 11:42
JSONbored added a commit that referenced this pull request Jul 12, 2026
…vider (#5259)

createOpenAiCompatibleAi's usage.provider fix (#5247) never took effect in
production because src/server.ts's 3 call sites that construct the
AI_EMBED/AI_VISION/AI_ADVISORY bindings never passed providerName at all --
confirmed live via a real RAG-indexing pass on edge-nl-01, which recorded
correct model attribution (bge-m3:latest) but every row's provider column was
still blank. These bindings accept any openai-compatible endpoint with no
explicit provider selector, so classify the configured base URL's hostname
into the existing ollama/openai/openai-compatible provider-name set instead of
guessing or hardcoding one.
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. manual-review Gittensor contributor context

Development

Successfully merging this pull request may close these issues.

1 participant