Files
PaperClipAI/server
brianCodesandClaude Opus 5.5 0da786fdc3 fix(issues): clear the compact issue-list cache when an issue changes (#17828)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The board reads task lists through `GET
/api/companies/:companyId/issues?view=compact`
> - The server keeps compact list responses in a short in-memory cache
(`ISSUE_LIST_SERVER_CACHE_TTL_MS = 2000`), keyed by actor and query, to
absorb request storms (#9190)
> - The board refetches its issue lists when it receives an
`activity.logged` live event for an issue
> - But a create, update or delete does not clear the cache, so the
refetch gets the cached list from before the change. The board shows the
old value for up to two seconds, and a quick edit seems to snap back
> - This pull request clears the cached compact lists of a company when
the server publishes an `activity.logged` event for an issue in that
company
> - The benefit is that a list fetched after a change shows the change,
and the cache still absorbs request storms between changes

## Linked Issues or Issue Description

**What happened?**
I changed the title of a task, and the board refetched the task list
immediately. The response had the old title and the header
`x-paperclip-request-cache: hit`. The list showed the old title until
the board fetched the list again more than two seconds later. The same
thing happens for other changes that the compact list shows, for example
status, priority and assignee.

**Expected behavior**
A compact list request that the server receives after a change returns
the changed values. The cache can answer repeated requests only while no
issue in the company changes.

**Steps to reproduce**
1. Start Paperclip with a company and one task.
2. Send `GET /api/companies/<companyId>/issues?view=compact`.
3. Within two seconds, send `PATCH /api/issues/<issueId>` with
`{"title":"After"}`.
4. Within two seconds of step 2, send the request from step 2 again.
5. Before this change, the response has the old title and
`x-paperclip-request-cache: hit`.

**Paperclip version or commit**
`master` at 835966dec8.

**Deployment mode**
Local development server (`pnpm dev`) with embedded PostgreSQL. The
cache is in-process, so the deployment mode does not change the
behavior.

Refs #9190, which added the cache. I did not find an open issue or pull
request for this problem.

## What Changed

- `server/src/routes/issues.ts`: each cache entry and each in-flight
list computation now records its company.
- `server/src/routes/issues.ts`: the issue routes subscribe once,
through `subscribeAllCompanyLiveEvents`, to company live events. On an
`activity.logged` event with `entityType: "issue"`, the server removes
the cached compact lists of that company. It also removes the in-flight
computations of that company from the coalescing map, so a new request
does not join a computation that started before the change.
- `server/src/routes/issues.ts`: a computation that was in flight during
the change does not store its result in the cache.
- `server/src/__tests__/issue-list-assignee-filter-routes.test.ts`: two
embedded PostgreSQL route tests. The first test lists, sends `PATCH
/api/issues/:id`, and lists again within the cache lifetime. It expects
the new values and a cache `miss`. The second test holds a list
computation after it reads the old title. It sends the `PATCH`, and
expects that a fresh request does not coalesce with the old computation
and caches the new title. Then it releases the old computation, which
returns the old title, and expects that the next request is a cache
`hit` with the new title. This checks that a stale in-flight result does
not replace the fresh cache entry. To hold the computation after its
read, the test file wraps `issueService().list` with `vi.mock`.

I put the invalidation on the `activity.logged` event because it is the
existing signal that an issue changed. Mutations must log activity. Most
routes that use a transaction publish the event after the commit. The
board already uses this event to refetch its issue lists. One
subscription covers every route and service that changes an issue:
create, `PATCH`, delete, checkout and release, comments, read marks, and
changes that heartbeat runs make. A call in each route would miss some
of these paths. The change stays in `routes/issues.ts`, which owns the
cache, and it adds no shared contract. The invalidation is event-driven,
so it adds no polling.

## Verification

- Run `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/issue-list-assignee-filter-routes.test.ts`. All 39 tests
pass.
- Without the change in `issues.ts`, the first new test fails. It gets
`hit` instead of `miss`.
- With only the `!inflightEntry.invalidated` guard removed, the second
new test fails. The old computation writes its stale result over the
fresh cache entry, so the next request gets `Before` instead of `After`.
The other 38 tests pass.
- The existing cache tests still pass: a repeated request without a
change still gets `hit`, simultaneous requests still coalesce, cache
keys stay separate per user, and the cache stays bounded.
- Run `pnpm --filter @paperclipai/server exec tsc --noEmit`. It passes.
- The design assumes that each change to an issue publishes
`activity.logged` before the next list request. The first new test
checks this with a real `PATCH` on embedded PostgreSQL, not a mock.
- Locally, I ran only this test file and `tsc --noEmit`, on Windows. I
did not run `pnpm -r typecheck`, `pnpm test:run` or `pnpm build`, and I
left them to CI. On Windows, the `typecheck` script of the server stops
in `prepare:runner-vendor` with `ERR_UNSUPPORTED_ESM_URL_SCHEME`, with
or without this change. A wider local run did not finish in 50 minutes.
- That wider run showed failures in `server-startup-feedback-export`,
`external-chat-wait.integration` and `workspace-recovery`. These files
fail in the same way without this change. `muse-control` failed once and
then passed except for one test, which also fails without this change.
These files do not test the issue-list cache.
- No documentation describes this cache, so this change does not update
documentation.
- To check by hand, follow the steps to reproduce above. After this
change, the response in step 4 has the new title and
`x-paperclip-request-cache: miss`.

## Risks

- Low risk. The change affects only the compact list TTL cache and
in-flight coalescing. The `index` and `rows` views do not use the TTL
cache, but they get the coalescing change.
- The cache absorbs fewer requests while a company has many issue
changes, because each change clears the cached lists of that company.
Each change already makes the board refetch its lists, so the extra work
is one computation per list after each change. Coalescing between
changes is the same as before.
- If many requests from one client are in flight, the server can answer
with a stale cache entry. After a change, that stale entry is gone, so
the server answers with its existing retry response instead of old data.
- The invalidation is per process. A deployment with more than one
server process can still serve a list that is up to two seconds old from
another process. This is the same as before this change.
- Each issue change scans the cache once to find the entries of the
company. The cache holds at most 256 entries
(`ISSUE_LIST_SERVER_CACHE_MAX_ENTRIES`), so this cost stays small.
- Some services log issue activity inside a transaction and publish the
event before the commit. A list request in that short window can cache
the old values. The board's own refetch has the same race.
- Changes that do not log issue activity, for example a label rename, do
not clear the cache. The board does not refetch issue lists for these
changes either.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with
tool use and code execution. It wrote the change, the tests and this
description. A human reviews the change before submission.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 02:39:21 -08:00
..