Repository navigation
fix(insights): adoption history audit — consistent sources, buffered writer, UTC day bounds - #10557
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
[Medium impact] The PR appears safe to merge; no blocking issues remain. SummaryThe PR aligns adoption history sources, buffers activity writes, shares search deduplication across replicas, and uses consistent UTC days and retention.
Reviews (4) · Last reviewed commit: "fix(insights): a window starting after n..." · Reviewed by Greptile |
There was a problem hiding this comment.
1 issue found across 18 files
Confidence score: 4/5
- In
apps/sim/app/api/v2/chat/route.ts, failed admission, billing, or turn execution can still increment the live usage counter, making usage inaccurate. Count the use only after the turn succeeds.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/sim/app/api/v2/chat/route.ts">
<violation number="1" location="apps/sim/app/api/v2/chat/route.ts:290">
P2: This counts the use before the turn is admitted, billed, or run. If billing attribution, entitlements, or the lifecycle fails, the request returns an error but the live counter still records a use. Backfill counts only persisted turns, so the two drift. Move the `recordMemberActivity` call to just before `runLifecycle` runs, after the billing and entitlement steps succeed, to match the post.ts admission-time recording.</violation>
</file>
This PR changes concurrency-sensitive code such as locks, queues, or retries. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.
Turn on auto-fix | Re-trigger cubic
…UTC day bounds, one retention
…s, serialized flushes
…ed inbox and short-window fixes
c99585d to
0613b90
Compare
|
@cubic-dev-ai review this PR |
@waleedlatif1 I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 19 files
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
|
@cubic-dev-ai review this PR |
@waleedlatif1 I have started the AI code review. It will take a few minutes to complete. |
Summary
Follow-up to #10532 / #10540 from an audit of the adoption history. Each finding was checked against the code first; all six held.
agent; the route admits only a person's own credential (personal API key or OAuth), so every turn is that person's, and the backfill already counted them the same way. Backfill logic moved tolib/organizations/member-activity/backfill.tsso it is testable; the script is a thin loop over itrecordMemberActivitywas four round trips (BEGIN, SET LOCAL, upsert, COMMIT) on the primary pool per chat turn, manual/API run, search and MCP call, and a tight loop serialized on one row lock. It now adds uses up in-process and writes them in one multi-row upsert every 5s, at 500 pending keys, and on SIGTERM/SIGINT/beforeExit (thelib/monitoring/metrics.tspattern). Flushes are serialized and rows are written in key order.first_at/last_atkeep LEAST/GREATEST. A failed flush drops its uses (analytics), loggedadoptionDaysmaps a period to whole UTC days, from the day its start falls on to the day its end falls on, or through today when it ends today. A period in another time zone no longer gains an extra UTC day, and the Search MCP breakdown now uses the same UTC days instead of exact instantsUses. The Search page refetches (staleTime 0), and each refetch was counted. The same query and filters by the same person now count once a minute (lru-cache, max 10,000, 60s TTL keyed by a hash). DAU is unaffectedsearch_mcpuses with an empty MCP breakdown. Both now keep 400 days (ADOPTION_HISTORY_RETENTION_DAYS); helm/docs updatedmanual/apionly) are named helpers inrecord.tsso their rules are testedType of Change
Testing
Real Postgres/Redis integration suites; each guard below was reverted and its test went red unless noted:
history.integration.ts(10): non-member and personal-workspace use never counted; uses add up across flushes and an organization plus its workspace into one row (red withoutGROUP BY); a flush waits for one already writing; a refused write is swallowed; a rate-limited or person-less Search MCP call is not counted (red without the outcome guard); the Search page counts once a minute and no other search surface (red without the surface gate and without the dedupe); the sweep removes only rows past retention; a batch that times out on a held row leaves the other table swept (red without the timeout catch)backfill.integration.ts(2): one day counted as the live recorder would and identically on rerun, with a forked copy once (red withoutDISTINCT), inbox email excluded (red without the anti-join), job turns and rate-limited calls excluded, dry run writing nothing;--beforeexcluded from the day rangeorganization-adoption.integration.ts(9): a window ending now runs through today; a Pacific-time period reads as the same UTC days and the Search MCP breakdown leaves out a call after the last UTC day (both red on the old bounds)adoption/export/route.integration.ts(2): a member is refused 403 with no CSV; an admin gets one row per memberrecord.test.ts: the workflow-run gate and the assistant/agent splitlib/knowledge/mcp,lib/execution, chat post,app/api/v2/chat, live search application,lib/organizations; lint,check:file-names,check:boundaries,check:source-conventions,check:comment-hygiene,check:utils,check:api-validation:strict,check:unused-exportsChecklist
test-auditauthoring gate)