feat(database): task followers

Merged
DA
15d ago
+21847-0

F1 of six PRs that build following on tasks: the data layer only, with no service, wire, MCP, or UI change.

What it adds

  • task_follower table (packages/database/src/schemas/task-follower.ts): task_id, user_id, machines (whether agents on the task may use this member's machines, default off), and created_at. The primary key is (task_id, user_id), there's an index on user_id, and both ids cascade on delete, the same way task_pin does. It records no reason for following.

  • Migration 20261112201451_task_follower, generated with db:generate plus a hand-written backfill. For every existing task it inserts:

    • the assignee, with machines on
    • the creator (task.user_id), with machines off unless they are also the assignee
    • every member who sent one of the task's agents a message (messages.sender_user_id joined through agent.conversation_id), with machines off

    A member named more than once gets one row, with bool_or deciding machines. Only current members of the task's workspace are inserted. The backfill only inserts into the new table and uses ON CONFLICT DO NOTHING.

  • Store @repo/database/models/task-follower-store. Every function takes the acting account and checks that it can reach the task (taskReachableBy):

    • listTaskFollowers(userId, taskId) answers none when the account can't reach the task. It lists only followers who are still members of the task's workspace.
    • listFollowedTasks(userId, organizationId) lists the tasks in that workspace that the account follows and can reach.
    • followTask(userId, { taskId, followerId, machines }) lets any member who can reach the task add a member of its workspace as a follower. A follower who already exists keeps their row and their switch.
    • unfollowTask(userId, taskId) removes the account's own row.
    • setFollowerMachines(userId, taskId, machines) changes the account's own switch only, since the rules say only that person can flip it.

Decisions

  • Former members are left out. Members who have left the workspace are skipped in the backfill and hidden from reads.
  • task_follower is not a Vespin Sync table yet. Its writes answer no txid. F2 can add the sync capture and txids if followers on TaskSummary need live updates.
  • The backfill scans messages once. It filters on sender_user_id IS NOT NULL and joins agent, which is a one-time cost in the deploy's migration transaction.

Tests

  • I ran pnpm exec vitest run packages/database/src/models/__tests__/task-follower-store.test.ts and both cases pass:
    • Following again keeps the follower's switch, and only the follower flips it.
    • An account outside the task's workspace reads no followers and adds none, no outsider can be added, and a member who leaves stops reading as a follower.
  • I checked the backfill with a scratch test that I didn't commit. It seeded tasks, an agent, and messages from a member, a former member, and a line with no sender, then reran the migration's statements over them. It produced exactly: the assignee with machines on, the creator-and-assignee with machines on, the creator with machines off, and the deduplicated sender with machines off. The former member and the line with no sender were skipped, and running it a second time wrote nothing.
  • pnpm --filter @repo/database check:migrations and typecheck pass, and Biome passes on the changed files. I'm leaving the rest to CI.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added support for following and unfollowing tasks, viewing task followers, and listing followed tasks within a workspace.
    • Followers can manage whether their machine activity is shared on a task.
    • Existing task assignees, task users, and relevant message senders are added as followers when they belong to the task’s workspace.
    • Only workspace members can view or manage followers, and followed tasks are limited to the relevant workspace.