Files
PaperClipAI/server/src/modules/agent-lifecycle/__tests__
Daniel McSheehy 957565529c fix(server): detach open conversations before deleting an agent (#16706)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - A board member can delete an agent through `DELETE /api/agents/:id`.
> - Deletion clears the assignee and the creator on the agent's issues
before it clears the conversation.
> - An open conversation must keep the same agent in the assignee field
and the conversation field.
> - The database check fails, the transaction rolls back, and the caller
receives HTTP 500.
> - This pull request clears the conversation and the assignee in one
update, then clears the other issue links.
> - The agent row can be deleted. The conversation issue remains as a
normal task.

## Linked Issues or Issue Description

Fixes #16703.

I searched open and merged pull requests for this delete path. No pull
request already changes conversation identity during agent deletion.

Refs #9186. Refs #3878. Those issues fail on `cost_events`, not on the
conversation check. This pull request does not change that path.

**What happened?**

`DELETE /api/agents/:id` returns HTTP 500 when the agent still has an
open conversation. The agent and the conversation remain.

**Expected behavior**

The delete removes the agent. The open conversation is detached first,
so the conversation check stays valid.

**Steps to reproduce**

1. Create an agent and start a chat, so the agent has a conversation
issue.
2. Pause the agent and wait until nothing is running.
3. Terminate the agent, then call `DELETE /api/agents/{agentId}` as
board.

**Paperclip version**

Reproduced from the report on 2026.1001.0 and 2026.1005.0. The same
check is present on current `master`.

**Deployment mode**

Local.

## What Changed

- Clear `conversationAgentId`, `conversationUserId`,
`conversationState`, and `assigneeAgentId` together when the
conversation belongs to the agent being deleted.
- Keep the later update that clears assignee and creator on every other
issue for that agent.
- Add a lifecycle test for an open conversation and a normal assigned
task.
- No documentation change. The public delete route stays the same.

## Verification

- Command: `pnpm exec vitest run --config vitest.config.ts
src/modules/agent-lifecycle/__tests__/lifecycle.test.ts`
- Result: 50 passed.
- The new test fails on the previous update with
`issues_conversation_identity_check`.
- With this change, the agent row is gone and both issues remain with a
null assignee and a null conversation identity.

## Risks

Low risk. The new update runs only for issues whose conversation agent
is the agent being deleted. Issue status does not change. Cost-event
delete failures in #9186 and #3878 remain open.

> 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

Grok 4.7 from SpaceXAI. The model used tool calls to read the
repository, run the lifecycle tests, and write this patch.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-12-04 23:48:41 -08:00
..