Skip to content

perf: small query and retention fixes from the fork performance audit #307

Closed

Description

@ibetitsmike

Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.

Summary

Independent small items from the performance audit. Each is one query, one index or one purge rule. They can land as separate small PRs in any order.

Items

1. notify_user scans all of notification_messages

chat_wakeups.sql:212-222 CountChatUserNotificationsSince filters on user_id, notification_template_id, payload->'data'->>'chat_id' and created_at. notification_messages has no index on any of them. It runs on every notify_user tool call (fork_notify_user.go:109-114).

Measured on the seeded database, 300,000 rows: parallel sequential scan, 17,648 buffers, 20.6 ms.

Fix: count the fork's own sends instead of parsing upstream's payloads. Add chat_user_notifications (chat_id, created_at) with an index on (chat_id, created_at), insert one row per accepted notify_user call, count there, and purge rows older than a day in fork_dbpurge.go. No index on an upstream table.

2. plugin_link_codes are never purged

plugins.sql:96-104: a code is deleted when consumed or replaced. Expired codes nobody entered stay.

Fix: DELETE FROM plugin_link_codes WHERE expires_at < now() in fork_dbpurge.go.

3. Workflow rows of live chats are never purged

fork_dbpurge.go:22-36 purges only secret answers. A run stores its script (up to 64 KiB) and a result snapshot; a step stores an uncapped JSON value (fork_workflow_report.go:98-119).

Fix: delete terminal and interrupted runs older than 30 days (steps cascade), and reject a workflow_report value over 256 KiB with a tool error.

4. The pack path cache never evicts

fork_plugin_gate.go:237-255: forkPacks.paths is a sync.Map keyed by (pack, commit) with no removal. One entry per pack update for the life of the process.

Fix: keep one entry per pack ID: store {commit, paths} under the pack ID and replace it when the commit differs.

5. The claim scan walks every due row of a busy chat

Covered by #294, which removes the claim. Listed so it is not filed twice.

6. ListChatWakeupsByChatID for the wake-ups pill is unbounded

chat_wakeups.sql:54-55 returns every wake-up of the chat, including completed ones, to a view that shows the open ones.

Fix: add a status filter parameter and have the pill request active and paused only; the existing chat_wakeups_chat_id_idx serves it. Completed wake-ups can be purged after 30 days in fork_dbpurge.go.

7. getLinkablePlugins queries once per organization and decrypts configs it does not return

plugins.go:614-653: one GetPluginsByOrganizationID per organization of the user; the dbcrypt wrapper decrypts every config (fork_forkdb.go:79-88), and the response uses only the key names.

Fix: one query GetLinkablePluginsByUserID joining organization_members, selecting no config column.

8. awaitSubagentCompletion can miss the first notification

subagent_operations.go:525-562 checks the child, then subscribes. A child that finishes in between is seen only at the 5 s fallback tick. This is upstream code that fork tools call; do not patch it in place. In fork callers that need the latency (waitStep), subscribe first, then call it.

Acceptance criteria

One test per item that fails before the fix: a plan or store-call assertion for items 1, 6 and 7, a purge test for items 2, 3 and 6, a cache-size assertion for item 4.

Activity

  1. added
    enhancement
    performance
    on Nov 14, 2026
  2. ibetitsmike commented on Nov 17, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Item 1 done: #369 merged as 76b39d6 on 2026-11-17 04:02Z (notify_user limit counts a fork table). Remaining: #370 (items 4, 7, 8) is gate-ready and merges after this deploy; #368 (items 2, 3, 6) is in Codex review after P1 fixes. Item 5 was #294.

  3. ibetitsmike commented on Nov 17, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Items 4, 7 and 8 done: #370 merged as d310975 on 2026-11-17 04:54Z. Only #368 (items 2, 3, 6) remains; it carries Closes #307.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions