Files
PaperClipAI/server
DottaandClaude Opus 5.5 427d13c38c fix(connections): clean up audit-only task traces and wait without rescanning content (#22658)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - #22636 removes connection content (for example Google results) when
a task or a company is deleted
> - Review on #22636 found two more gaps after it merged
> - A task whose only connection trace is an audit entry (for example a
denied call) got no cleanup job, so the argument preview stayed after
the task was deleted
> - A cleanup job that waits for open runs re-ran all content steps on
every recheck, which is wasted database work
> - This pull request queues a job for audit-only tasks and adds a
`waiting` phase that only counts open work
> - The benefit is that no audit preview of a deleted task is left
behind, and a waiting job stays cheap

## Linked Issues or Issue Description

Refs #22636

**Problem**
1. Deleting a task whose only connection trace is a
`tool_access_audit_events` row with the task's ID creates no cleanup
job, so its argument preview stays.
2. While a task cleanup job waits for open runs or approvals, every
recheck runs the six content steps again from the start.

**Expected behavior**
1. The audit preview is cleared after the task is deleted.
2. A waiting job only checks whether work is still open, and runs the
content steps once when the work settles.

**Steps to reproduce**
1. Write a denied policy audit entry with the task's ID and no call or
call event. Delete the task. The preview stays.
2. Delete a task while one of its runs is working. Each recheck scans
every content table of the task again.

## What Changed

- `issue-content-deletion.ts`: when the task has no recorded runs, calls
or call events, check for an audit entry with the task's ID. If one
exists, queue the job. The audit step already matches by task ID.
- `content-deletion.ts`: new `waiting` phase. After a content pass with
open work, the job moves to `waiting` instead of restarting. Each
recheck counts open runs, open calls, open approvals and open wakes with
results for the task. When the count is zero, or after the one-day
bound, the job runs the content steps once more and finishes.
- Moved the pure JSON cleaning helpers to `content-cleaning.ts` to keep
`content-deletion.ts` under 1,000 lines. No behaviour change.
- Two canary tests: a task with only an audit entry, and a waiting job
that leaves new content alone on a recheck and clears it after the open
run settles. Both failed before the fix.
- The working memory note is updated.

## Verification

- `cd server && npx vitest run
src/__tests__/tool-connection-content-deletion-canary.test.ts
src/__tests__/tool-connection-content-deletion.test.ts
src/__tests__/server-startup-feedback-export.test.ts` (156 tests pass).
The two new tests failed before the fix.
- `cd server && pnpm typecheck` passes.

## Risks

- Task deletion with no other connection trace runs one more query on
`tool_access_audit_events` for the company, with `limit 1`. That table
has no index on the task ID in its details.
- No schema change.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with
tool use (shell, file edit, test runs). Extended reasoning on.

## 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
- [x] 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-11-23 14:00:36 -06:00
..