Skip to content

feat: implement real-time file change notifications for project and w… - #794

Merged
pikann merged 4 commits into
masterfrom
feature/implement-realtime-file-change-feature
Nov 20, 2026
Merged

pikann merged 4 commits into
masterfrom
feature/implement-realtime-file-change-feature

Conversation

@pikann

@pikann pikann commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When someone creates, renames, moves, trashes or restores a file or folder in the file manager, other users browsing the same folder now see it without reloading. This works for project files and for the general (workspace-wide) scope.

Design

  • Event: each file mutation publishes one realtime-only file.changed event (create, update, move, trash, restore, delete; upload completion counts as a create). It is not written to the activity log or sent to plugins.
  • Payload: ids only (change type, node, folder_ids, root), no file names. A move names both the old and the new folder.
  • Routing:
    • Project scope: checked as files:read on the project and delivered to a new files room.
    • General scope: has no project, so it goes to a workspace:files room joined at connect by sockets whose account has platform-level files:read, and re-synced on every IAM change.
  • Client: applyFileChange refetches only the listing of a folder the event names (or the top level), plus searches, Recent/Starred/Trash and breadcrumbs. Folders nobody is viewing are only marked stale.

Changes

  • API: filesvc announces changes through a new realtime sink (service/file/realtime.go, wired in bootstrap/app.go); new TopicFileChanged and its authz rule; registry.json regenerated.
  • Realtime: new files namespace, workspace:files room, routing in subscriber.ts, room join in server.ts, resync in resync.ts; README updated.
  • Web: applyFileChange in files-api.ts, a files rule in project-event-rules.ts, new useWorkspaceFilesRealtime hook, and FilesPage uses it (and joins the browsed project's rooms on the global page).
  • Bug fix: the realtime service read permissions from /users/me/global-permissions, but the API returns actions, so its global permission list was always empty. It now accepts both.

Known limitations

  • Files written by other services (task attachments, document files, annotation screenshots) appear in the system folders but do not announce file.changed; those listings update on the next refetch.
  • The scheduled trash purge does not announce.
  • Recent, Starred and Trash on the global page only follow the scope being browsed, not every project.
  • The workspace:files room is re-synced on IAM changes and every 60 seconds, so a revoked account can receive id-only events for up to one interval.

Testing

  • Go: new realtime_test.go (announcements, authz, general scope without authz); events tests and registry pass.
  • Realtime (bun): routing of project and general file.changed, workspace room resync; all tests pass.
  • Web (vitest): applyFileChange cache invalidation cases; related suites pass; tsc -b clean.
  • Not verified end to end: two browsers on the real stack, and the apps/e2e specs were not run.

…orkspace scopes

- Added `useWorkspaceFilesRealtime` hook to manage real-time updates for workspace-wide file changes.
- Integrated real-time file change handling in the `FilesPage` component.
- Enhanced the `applyFileChange` function to refetch affected file listings based on changes.
- Updated the file service to announce changes via a new `realtime` interface, allowing clients to receive updates for file operations (create, move, trash, etc.).
- Implemented authorization checks for file changes, ensuring users have the necessary permissions to view updates.
- Created tests for file change application logic and real-time event routing.
- Updated documentation to reflect new file change events and permissions.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — a few rough edges worth a look, with details inline.

Approach: sound — one realtime-only, ids-only event per file-manager mutation, routed through the existing namespace/room machinery, with the general scope split into its own room because it has no project. The client refetches only the listings the event names, which keeps the fan-out cheap.

Reviewed changes

  • API announcer — filesvc publishes file.changed after each mutation; new topic, authz rule and registry entry.
  • Realtime routing — new files namespace/room, a workspace:files room joined at handshake and refreshed on IAM changes, and special routing for general-scope events.
  • Web invalidation — applyFileChange refetches the named folders, searches, sections and breadcrumbs; a project event rule and a workspace-scope hook feed it.
  • Global-page wiring — FilesPage follows the general scope and joins the browsed project's rooms.
  • Global permissions fix — the realtime client now reads actions, the key the API actually returns.

ℹ️ Files the manager shows that come from other services stay stale

The announcer lives only in filesvc, but the file manager also lists files written by the attachment, doc and annotation services into the project's system folders ("Task attachments", "Document files", "Annotation screenshots"). Those rows get a folder_id in those folders via trigger and are included by file_items, so adding or removing a task attachment changes a listing another user may be looking at without publishing file.changed.

Technical details
# System-folder listings are not announced

## Affected sites
- `services/api/migrations/000093_file_management.sql:194-207` — `files_before_insert` assigns `project_id`/`folder_id` for non-upload sources.
- `services/api/migrations/000093_file_management.sql:261-266` — `file_items` includes every uploaded file that has a folder.
- `services/api/internal/repository/postgres/file_repository_pg_test.go:61-95` — proves task/doc attachments are listed inside those folders, and a detached attachment lands in the trash.
- `services/api/internal/service/attachment/service.go`, `doc_service.go` and `annotation_service.go` — insert/delete `files` rows without publishing anything.

## Required outcome
- Decide whether those listings are in scope. If they are, the attachment/doc/annotation flows need to announce too (or the announcer has to move to a shared layer). If not, add it to the PR's known limitations so the claim "other users browsing the same folder see it without reloading" is qualified.

ℹ️ A reconnect that cannot resume leaves project file listings stale

refetchProject invalidates the project, tasks and conversations after a reconnect, but not the file manager's ["files", projectId, ...] queries. The sync feed is opt-in, so the legacy join path is the default: a file change made while the socket was away is neither replayed nor covered by that refetch, and the listing stays stale until some later file.changed arrives.

Technical details
# Project file queries are missing from the reconnect refetch

## Affected sites
- `apps/web/src/hooks/use-project-realtime.ts:191-201` — `refetchProject` only touches `conversation-event-window`, `["projects", id]` and `["users","me","tasks"]`.
- `apps/web/src/hooks/use-project-realtime.ts:237-244` — it runs when a rejoin did not resume the feed.
- `apps/web/src/hooks/use-workspace-files-realtime.ts:44-52` — the general scope does invalidate on reconnect, showing the intended behavior.
- `apps/web/src/lib/files-api.ts:446-447` — `invalidateFiles` is the helper to reuse.

## Required outcome
- A project reconnect that cannot replay missed changes should refresh the project's file listings along with its other data.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread services/realtime/src/resync.ts Resolved
Comment thread apps/web/src/lib/project-event-rules.ts Resolved

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes (since the prior review at d1de3d6d):

  • Workspace files room covered by the periodic resync — resyncWorkspaceFiles() now runs on every pass, so the 60-second backstop and connection-state-recovery sockets correct workspace:files membership; the README states the periodic correction.
  • Project files rule scoped correctly — it ignores events whose scope is not project or whose project_id is another project, so general-scope events no longer invalidate a project's file queries. New tests cover both cases.
  • files namespace now participates in cache resets — holdsDataOf matches ["files", projectId, ...], so losing files:read resets the cached listings and gaining it invalidates the errored queries.
  • Reconnect refetch includes file listings — refetchProject now invalidates ["files", currentProjectId], covering a rejoin that could not replay missed changes.

All three prior review threads are addressed and resolved. Verified with npx vitest run src/lib/project-event-rules.test.ts src/lib/files-api.test.ts (40 tests pass).

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes (since the prior review at 6c403118):

  • Burst coalescing for file.changed — applyFileChange now applies the first change of a burst at once and merges the rest into a single pass 250 ms later, keyed per QueryClient and per scope, so moving many files no longer refetches every active search and section once per event. New tests cover the first-event/trailing-merge behavior.
  • Restored folders refresh their contents — the API names the restored folder's own listing on Restore, and the client invalidates it for restored folder events; a test asserts the extra listing is refetched.
  • Duplicate folder ids are removed on the API — announce deduplicates folder_ids, which makes Update passing the same parent twice for a rename-only change a single entry.
  • Workspace files resync grouped per account — resyncWorkspaceFiles now issues one verification request per account instead of one per token, tries each token until one is accepted, and ends only the sockets whose own token the API rejected; the new resync.test.ts case drives revocation and re-grant.

The files-api and project-event-rules suites pass here (42 tests) along with go test for internal/service/file and internal/events; the services/realtime bun suite could not run in this sandbox.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes (since the prior pullfrog review at 85ee894e):

  • End-to-end coverage for the feature — a new tests/realtime/file-changes.spec.ts drives the real app in a browser for project create, rename, trash/restore and move, plus the general scope, and adds four socket probes that assert ids-only payloads and files:read gating for both scopes.
  • Feature file — features/realtime/file-changes.feature records the nine scenarios; the spec has a matching test per scenario.
  • Platform role fixture — PLATFORM_RESOURCES in tests/helpers/e2e-api.ts gains file/*, mirroring the server's platform root (iam.PlatformRootFor → file/*) so a workspace role can actually grant platform-level files:read.

The spec matches the implementation's contracts: change values, scope: "project" | "general", project_id, kind: "folder", and root: true for root-level mutations (the announcer maps a nil parent to root). The UI assertions cannot pass without the event, since file listings have a 15 s staleTime and the page is not refocused. The specs were not executed here — the Playwright stack and browsers are not available in this environment — so this is a read-through cross-checked against the existing files/file-manager.spec.ts and the realtime specs.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann
pikann merged commit ccaf1ca into master Nov 20, 2026
9 checks passed
@pikann
pikann deleted the feature/implement-realtime-file-change-feature branch November 20, 2026 03:15
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.

1 participant