perf(backend): unstick the conversation list under live traffic #6
Closed
gabrielvidal
wants to merge 0 commits from
perf-list-cache into main
pull from: perf-list-cache
merge into: gabrielvidal:main
gabrielvidal:main
gabrielvidal:fix/link-rotation-race
gabrielvidal:bots-plugin-exploration
gabrielvidal:live-group-current-action
gabrielvidal:mobile-drawer-toast
gabrielvidal:pi-steer
gabrielvidal:pi-on-workers
gabrielvidal:model-field
gabrielvidal:preset-machine-split
gabrielvidal:tasks-plugin
gabrielvidal:conversation-titles
gabrielvidal:card-icon-beside-title
gabrielvidal:worktrees-cli
gabrielvidal:worktrees-plugin
gabrielvidal:agent-presets
gabrielvidal:no-plugin-white-flash
gabrielvidal:agy-tasks
gabrielvidal:home-declutter
gabrielvidal:perf-plugin-batching
gabrielvidal:fresh-context-plugin
gabrielvidal:remove-hooks-page
gabrielvidal:palette-plugins
gabrielvidal:linear-projects
gabrielvidal:agent-messaging
gabrielvidal:model-router
gabrielvidal:conv-card-layout
gabrielvidal:ask-more-options
gabrielvidal:agent-limits
gabrielvidal:generic-ui-strings
gabrielvidal:android-push-bridge
gabrielvidal:auto-translate
gabrielvidal:filter-chips
gabrielvidal:plugin-catalog
gabrielvidal:file-viewers
gabrielvidal:plugins-page-rework
gabrielvidal:hub-mcp-plugin
gabrielvidal:translation-plugin
gabrielvidal:scheduled-doors
gabrielvidal:cron-jobs-plugin
gabrielvidal:context-skills-plugin
gabrielvidal:files-plugins
gabrielvidal:worker-names
gabrielvidal:details-panel-perf
gabrielvidal:services-plugin-only
gabrielvidal:fix-test-env-leaks
gabrielvidal:bg-shells-plugin
gabrielvidal:feat-commits-plugins
gabrielvidal:fix-branch-line-tag-rows
gabrielvidal:retire-dead-leftovers
gabrielvidal:runner-into-harness-pi
gabrielvidal:retire-tool-proxy
gabrielvidal:new-project-policy-plugin-only
gabrielvidal:guest-jobs-sonnet
gabrielvidal:push-buttons-everywhere
gabrielvidal:mail-send-plugin
gabrielvidal:ui-polish-batch
gabrielvidal:command-palette-plugin
gabrielvidal:keyboard-shortcuts-plugin
gabrielvidal:prompt-shortcuts-plugin
gabrielvidal:matomo-site-grants
gabrielvidal:lab-chrome-plugin
gabrielvidal:performance-plugin
gabrielvidal:whats-new-plugin
gabrielvidal:skills-agents-plugin
gabrielvidal:goals-plugin-only
gabrielvidal:storage-server-side
gabrielvidal:plugin-scheduled-conversations
gabrielvidal:plugin-fences
gabrielvidal:handoff-plugin
gabrielvidal:plugin-commands
gabrielvidal:plugin-speed
gabrielvidal:cleanup-provider-harness
gabrielvidal:plugin-conversation-labels
gabrielvidal:plugin-plan-usage
gabrielvidal:fix-worker-harness-sync
gabrielvidal:plugin-gcloud-reauth
gabrielvidal:feat/plugin-cards-in-thread
gabrielvidal:feat/plugin-proxy-wake
gabrielvidal:unify-subagents-children
gabrielvidal:wave3d-frontend-typed-plugins
gabrielvidal:wave3c-sidecar-strict-plugins
gabrielvidal:wave3a-catalog-accounts-profiles
gabrielvidal:wave3b-pricing-usage-cutover
gabrielvidal:wave1a-typed-plugins
gabrielvidal:wave1b-sidecar-runtime
gabrielvidal:fix-ios-overlay-scroll
gabrielvidal:batch2-work-resources-core
gabrielvidal:batch2-apikeys
gabrielvidal:plugin-blue-green-deploy
gabrielvidal:plugin-ship
gabrielvidal:plugin-project-env
gabrielvidal:plugin-memories
gabrielvidal:plugin-activity
gabrielvidal:plugin-templates
gabrielvidal:plugin-desk-phone
gabrielvidal:batch1-platform
gabrielvidal:plugins-list-view
gabrielvidal:dash-plugins
gabrielvidal:pets-plugin
gabrielvidal:ha-push-channel
gabrielvidal:mail-agents
gabrielvidal:graph-fix
gabrielvidal:graph-plugin
gabrielvidal:services-plugin
gabrielvidal:left-to-do-plugin
gabrielvidal:plugins-gallery
gabrielvidal:home-conv-menu
gabrielvidal:meta-business
gabrielvidal:agy-rdrand
gabrielvidal:bg-lag
gabrielvidal:conv-load-perf
gabrielvidal:askform-instant
gabrielvidal:fix/drawer-menu-done-chip
gabrielvidal:pi-midrun-compaction
gabrielvidal:interrupt-menu-btn
gabrielvidal:authz
gabrielvidal:inkling
gabrielvidal:repo-plugins
gabrielvidal:work-sync
gabrielvidal:leftovers
gabrielvidal:fix/agy-guest-guard
gabrielvidal:feat/model-presets
gabrielvidal:scroll-back
gabrielvidal:feedback-bell
gabrielvidal:feat/movies-tool
gabrielvidal:feat/running-thread-padding
gabrielvidal:feat/drawer-filter-button-card-chat
gabrielvidal:burn-rate
gabrielvidal:guest-install
gabrielvidal:guest-usage
gabrielvidal:guest-palette
gabrielvidal:guest-form-links
gabrielvidal:form-open-buttons
gabrielvidal:clarifier-lean
gabrielvidal:auto-archive
gabrielvidal:guests-profile
gabrielvidal:conv-motion
gabrielvidal:share-target
gabrielvidal:terminal-sync
gabrielvidal:work-context-past
gabrielvidal:work-context-brief
gabrielvidal:enroll-codes
gabrielvidal:open-image-file
gabrielvidal:worker-uploads
gabrielvidal:conv-drawer-search
gabrielvidal:stack-motion-v3
gabrielvidal:notif-stack-fixes
gabrielvidal:home-motion
gabrielvidal:i18n-fr
gabrielvidal:local-electricity-cost
gabrielvidal:cache-pill-thread
gabrielvidal:cache-scenarios
gabrielvidal:fresh-start
gabrielvidal:msg-select
gabrielvidal:shortcuts-setup
gabrielvidal:ui-0-40-3
gabrielvidal:outbox-landing
gabrielvidal:resume-scope
gabrielvidal:conv-filter-range
gabrielvidal:fix-subagent-form-session
gabrielvidal:ask-form-integration
gabrielvidal:form-visual-options
gabrielvidal:watcher-perf
gabrielvidal:last-msg-summary
gabrielvidal:secret-fields
gabrielvidal:commit-review
gabrielvidal:summary-mode-fixes
gabrielvidal:service-tag-parser
gabrielvidal:clarifier
gabrielvidal:fetch-diet-2
gabrielvidal:fetch-diet
gabrielvidal:conv-look
gabrielvidal:pwa-links
gabrielvidal:tool-widgets
gabrielvidal:worker-autoupdate
gabrielvidal:usage-rings
gabrielvidal:opencode-harness
gabrielvidal:notif-diet
gabrielvidal:conv-resources
gabrielvidal:scheduled-tasks
gabrielvidal:profiles
gabrielvidal:notify-exact-cost
gabrielvidal:usage-measuring
gabrielvidal:guest-projects
gabrielvidal:usage-calibration
gabrielvidal:account-switch
gabrielvidal:multiuser-improve
gabrielvidal:form-groups
gabrielvidal:multiuser
gabrielvidal:motion-reel
gabrielvidal:dashboards-fixes
gabrielvidal:split-big-files
gabrielvidal:held-regex
gabrielvidal:subagent-interrupts
gabrielvidal:form-html
gabrielvidal:tokunia-video
gabrielvidal:settings-uniform
gabrielvidal:worker-ask-fix
gabrielvidal:worker-generic-mac
gabrielvidal:runner-shortcuts
gabrielvidal:notify-unify
gabrielvidal:mac-worker
gabrielvidal:model-switch
gabrielvidal:pipeline-paths
gabrielvidal:multi-account
gabrielvidal:feature-flags
gabrielvidal:qwen38-support
gabrielvidal:url-prompt
gabrielvidal:running-pages
gabrielvidal:homelab-tag
gabrielvidal:root-project
gabrielvidal:or-models
gabrielvidal:deploying-loader
gabrielvidal:worktree-diff
gabrielvidal:conv-tags
gabrielvidal:tags-page
gabrielvidal:agents-page
gabrielvidal:icon-picker-shortcuts
gabrielvidal:conv-cta
gabrielvidal:stamp-refine
gabrielvidal:post-task-cache
gabrielvidal:post-tidy-completed
gabrielvidal:form-badge
gabrielvidal:hooks-delay-seconds
gabrielvidal:cost-dashboard
gabrielvidal:plan-forms
gabrielvidal:form-mobile
gabrielvidal:guidelines-dag
gabrielvidal:skills-view
gabrielvidal:navbar-hold
gabrielvidal:spawnbox-status
gabrielvidal:hold-editable-expand
gabrielvidal:drawer-transparent
gabrielvidal:hold-fix
gabrielvidal:hold-editable
gabrielvidal:charroll-bump
No Reviewers
Labels
No items
No labels
Milestone
No items
No Milestone
Projects
Clear projects
No projects
Assignees
gabrielvidal (Gabriel Vidal)
Clear assignees
No Assignees
gabrielvidal
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: gabrielvidal/ai-agent#6
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Summary
The ai-agent felt laggy because the backend was saturated, not the G10 box: one Python process was pinned at ~105% of a core while the host sat ~82% idle. Every transcript write (every ~2 s while any agent runs) invalidated the conversation-list caches, every client woken by the SSE bus missed them at the same moment and each ran the same full ~1 400-card rebuild in parallel on one GIL, and FastAPI then serialized the big bodies on the event loop. Live, before this PR:
/api/conversations?limit=50took 16–80 s and even/api/healthtook 43 s. Each request queued longer than the client waited, and every refetch added more work.Key changes
backend/main.py—_Memo: a version-keyed, single-flight memo. Concurrent callers with the same key share one build instead of each running it. It now backs the card list (_conversation_cards),_agent_runsand_agents_by_conversation.backend/main.py—lru_cacheon_conv_id/_parent_path_of/_is_sidechain_path.pathlib.relative_tocost 0.24 ms a call × ~4 000 calls per build, which was half the list's cost.backend/meta.py—MetaStore.peekis lock-free (thousands of peeks per build were convoying on the lock). The newMetaStore.items()replaces a 1.4 MB JSON deep copy in_build_agent_runs.backend/main.py—@_json_in_workeron the eleven heaviest GETs (conversations, conversation, conversation lists, notifications, subagents, forms, goals, notify-log, cron, projects, skills, activity). Each returns a readyJSONResponsefrom its worker thread, sojsonable_encoder(5–10× slower thanjson.dumps) no longer blocks the event loop.scripts/bench-lists.sh+scripts/bench_lists.py— a reproducible benchmark. It runs a snapshot of the live data in a throwaway--network none, 1-CPU container, against the deployed image or a worktree'sbackend/.CLAUDE.md— the performance section gains rules 5–7 (single-flight memo, O(1) per-summary helpers, no big encodes on the loop).Key decisions
_auto_state, 10-min window), so a version-only key could leave an idle list showing "running" forever.JSONResponsediscards a decoratorstatus_codeand any injectedResponseheaders, so it's opt-in on plain 200 GETs only. A bodyjson.dumpscan't encode falls back to FastAPI's encoder unchanged.Changelog
Test notes
scripts/bench-lists.sh(prod image) vsscripts/bench-lists.sh "$PWD/backend"on a 2 210-transcript snapshot, 1 CPU, cost after one transcript write:/api/conversations?limit=50/api/conversation-summary(SSE row patch)_build_agent_runs--network none, each on its own data copy) and fetched 15 endpoints from each. The card/list/detail/summary/notifications/forms/cron/skills/subagents/agents responses are identical once parsed. activity/projects/goals differed only by transcripts that grew live between the two runs. Full list withfull=1(4.7 MB): 1 554 → 268 ms.main(3 pre-existing import errors for the missingfeedmodule intest_workers/test_remotefeed/test_worker_routing)../services/ai-agent/deploy.shfrom~/homelab.conversations_list84% of samples;meta.peeklock wait 67%; the main thread injsonable_encoder; 39 AnyIO worker threads busy.🤖 Generated with Claude Code
Screenshots
Pull request closed