Skip to content

feat: mark ai chat writes in item history and meter chat turns - #292

Merged
stefan-ernst merged 7 commits into
Windshiftapp:mainfrom
valentimarco:feat/ai-chat-provenance-and-metering
Oct 5, 2026
Merged

stefan-ernst merged 7 commits into
Windshiftapp:mainfrom
valentimarco:feat/ai-chat-provenance-and-metering

Conversation

@valentimarco

@valentimarco valentimarco commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Resolve #282

I also added 2 commit related to the docker build

here the result in ui (AI Chat is the name of the tester)
image

@github-actions

Copy link
Copy Markdown

This PR has merge conflicts that need to be resolved before it can be merged. Please rebase on the latest main branch.

@github-actions github-actions Bot removed the conflict label Sep 26, 2026
@github-actions

Copy link
Copy Markdown

Merge conflicts have been resolved.

@stefan-ernst

Copy link
Copy Markdown
Contributor

@valentimarco - thanks for the PR! I optimized the loading of the history tab to avoid a performance bottleneck on bigger instances. The merge conflict is also resolved. I will further test it but it looks good so far!

@github-actions

Copy link
Copy Markdown

This PR has merge conflicts that need to be resolved before it can be merged. Please rebase on the latest main branch.

@stefan-ernst
stefan-ernst force-pushed the feat/ai-chat-provenance-and-metering branch from eee8354 to f695c08 Compare September 28, 2026 21:32
valentimarco and others added 5 commits September 28, 2026 23:33
The item history read joined an aggregate of the entire llm_usage table on
every request. That subquery is not scoped to the item, so it scanned the
whole metering table regardless of which item was opened and degraded
linearly with usage volume (always on SQLite, and on Postgres once the
planner picks a hash plan over a merge join).

Keep the metered detail off the history response and fetch it per run:

- Drop model/cost_usd/total_tokens from ItemHistory and the runTelemetry
  join; the history response is a single-table read carrying only source
  and agent_run_id.
- Add Model to RunUsageTotals (MIN(model) over the run) so the existing
  indexed /agent-runs/{run_id}/usage endpoint is the one source of
  model/tokens/cost for both the history tooltip and the agent log.
- Fetch that endpoint lazily when the marker is hovered, scoped to the
  newest 20 runs so a long history cannot fan out into a request per row.
A chat turn writes one aggregated llm_usage row, so TotalsForRun's COUNT(*)
reported one call even when the agent loop made several provider round-trips;
the broker's per-call rows reported the real count. Store the count on the row
(defaulting to one for existing and per-call rows) and sum it, so the usage
endpoint, chat response, and history tooltip agree on the number of calls
behind a run.
@stefan-ernst
stefan-ernst force-pushed the feat/ai-chat-provenance-and-metering branch from f695c08 to ae35695 Compare September 28, 2026 21:34
@github-actions github-actions Bot removed the conflict label Sep 28, 2026
@github-actions

Copy link
Copy Markdown

Merge conflicts have been resolved.

@github-actions

Copy link
Copy Markdown

This PR has merge conflicts that need to be resolved before it can be merged. Please rebase on the latest main branch.

@stefan-ernst

Copy link
Copy Markdown
Contributor

heads up - I will merge it before 0.9.0 ships. Its just a bit busy on main so I will merge it once the helpdesk features are settled

Resolve conflicts so AI provenance and the newer history actor
attribution coexist:

- item history carries actor_kind/actor_portal_customer_id alongside
  source/agent_run_id in the entry, insert, and read projection
- keep main's full migration catalog and append the three history
  source/agent-run and llm_usage calls migrations
- initialize the ItemHistory group with both actor and agent fields
@github-actions github-actions Bot removed the conflict label Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Merge conflicts have been resolved.

@stefan-ernst
stefan-ernst merged commit 3da8062 into Windshiftapp:main Oct 5, 2026
22 checks passed
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.

[Feature] Indicate the use of AI chat/Agent in the history of a task card

2 participants