Files
PaperClipAI/server
5d85df5da2 fix(server): delete projects that still own cost or finance events (#21723)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Each agent run can record spend. Paperclip stores spend in cost
events and finance events, and it links each event to a company, an
agent, a project, and a task.
> - Operators delete a project when the work is cancelled or complete.
The delete service removes the project row and the project workspace
rows.
> - Two kinds of rows keep a reference to the project and block the
delete: tasks, and cost or finance events. The database rejects the
delete. The route returns `500 {"error":"Internal server error"}`.
> - The message hides the cause. A project delete also has no clear
behaviour for remaining tasks.
> - This pull request adds an explicit result. The service returns `409`
with the code `project_tasks_pending` when tasks remain. The service
clears the project link on cost and finance events, so spend history
survives and the delete completes.
> - The benefit is a predictable delete path, no lost spend history, and
no opaque `500`.

## Linked Issues or Issue Description

Related pull requests: Refs #10817, Refs #9145. Related issue: Refs
#11062 (the same foreign key problem on agent delete).

**What happened?**

`DELETE /api/projects/:id` returns `500 {"error":"Internal server
error"}` in two cases:

1. The project still has tasks. `issues.project_id` has no delete
action, so the database rejects the delete of the project row.
2. The project has cost events or finance events.
`cost_events.project_id` and `finance_events.project_id` have no delete
action, so the database rejects the same delete.

The server log shows the raw Drizzle error `update or delete on table
"projects" violates foreign key constraint`. The API client only sees
the generic `500`.

**Expected behavior**

The delete returns a result that names the blocking state:

- Tasks remain: `409` with a message that tells the operator to move or
delete the tasks.
- Cost or finance events remain: the delete completes. The spend rows
stay, and their project link becomes `null`.

**Steps to reproduce**

1. Create a project.
2. Create one task in the project.
3. Call `DELETE /api/projects/<id>`. Result: `500 Internal server
error`.
4. Delete the task, then call `DELETE /api/projects/<id>` again. Result:
`200`.
5. Create a second project. Record one cost event for it, then call
`DELETE /api/projects/<id>`. Result: `500 Internal server error`.

**Paperclip version or commit**

2026.1106.0 (self-hosted). The same code is on `master` at the time of
this pull request.

**Deployment mode**

Self-hosted, local, single operator.

**Installation method**

`npm install -g paperclipai`.

**Agent adapter(s) involved**

Not adapter-specific (core bug).

**Database mode**

External PostgreSQL 17 in Docker.

## What Changed

- `projectService.remove` clears `cost_events.project_id` and
`finance_events.project_id` before it deletes the project row.
- `projectService.remove` returns `409` with the code
`project_tasks_pending` when the project still has tasks.
- New test file `server/src/__tests__/project-delete-references.test.ts`
covers both cases.

## Verification

- `pnpm --filter @paperclipai/plugin-sdk build`
- `cd server && ./node_modules/.bin/vitest run
src/__tests__/project-delete-references.test.ts`
- The first test adds one cost event and one finance event to a project.
It deletes the project, then confirms that both rows still exist with a
`null` project link, and that the project row is gone.
- The second test adds a task to a project. It checks that the delete
returns `409`, and that the project row stays.
- Manual check on a self-hosted instance (2026.1106.0): project with 58
cost events returned `500` before the change. After the same query that
the service now runs, the delete returned `200`.

## Risks

- Behaviour change: a project delete with remaining tasks returns `409`
instead of `500`. The project row stays.
- Cost and finance rows keep their amounts and their company link. They
lose only the project attribution. No spend rows are deleted.
- No migration. No schema change. No data change outside the delete
path.

## Model Used

- Harness: Hermes Agent (Nous Research)
- Provider: opencode-go
- Model: `deepseek-v4.1-flash`
- Capabilities used: tool use, code execution, file edits, browser
- Context window: provider default. The task used a small part of it.

## 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
- [ ] 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

---------

Co-authored-by: hungpt99-dev <hungpt99-dev@users.noreply.github.com>
Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
2026-12-05 04:18:25 -08:00
..