Skip to content

feat(agent): add a dry-run tool to purge orphaned chat transcripts - #191

Merged
blakebauman merged 1 commit into
mainfrom
feat/agent-chat-purge
Nov 7, 2026
Merged

blakebauman merged 1 commit into
mainfrom
feat/agent-chat-purge

Conversation

@blakebauman

Copy link
Copy Markdown
Collaborator

Why

Before #187, deleting a conversation removed its agent_chats row and left the transcript in its Durable Object. Nothing links to those transcripts, but they still exist, after the UI told people they were gone.

What

pnpm purge:orphan-chats is a dry run by default. --apply wipes what it lists. A person runs it; nothing runs automatically.

  1. Lists chat objects through the Cloudflare API.
  2. POST /v1/admin/agent-chats/unclaimed works out which ids no row accounts for. It hashes names from rows and opens no object.
  3. Reads only those objects through Cloudflare's query API.
  4. POST /v1/admin/agent-chats/purge wipes by name through the existing destroy() path. It refuses if a row exists or the name doesn't hash to the given id, and logs the actor and outcome for each chat.

Empty, recently active (default 24h) and unidentifiable chats are reported and left alone. Output has ids and dates, never transcript text.

Both routes are platform-admin only, and keys need a new admin:agent-chats scope, so a docs reindex key can't delete chats.

Why not wipe by raw id

Tested in workerd with the object evicted first: a cold start through idFromString() has no ctx.id.name, so the Agents SDK throws on every call, including custom methods. A later call by name then lands on that same nameless instance and throws too. Done to a live chat, that would break it until eviction. A warm object hides this.

Testing

  • API tests pass (487), including the script's SQL run against a real object and purge refusals leaving the transcript in place.
  • The script ran end to end against a fake server: pagination, namespace selection, the object with a row never opened, non-zero exit on a failed wipe.

Not tested

  • The real Cloudflare list and query APIs. The docs don't say whether the query API starts a nameless instance; if it does, wipes report failed, nothing is lost, and a re-run after the objects go idle should work.

Before running with --apply

Chats from before the 2026-11-05 D1 move also have no row, so they show as orphans even though nobody deleted them. Check the dates in the dry run.

🤖 Generated with Claude Code

Before #187, deleting a conversation removed its agent_chats row and left the
transcript in its Durable Object. `pnpm purge:orphan-chats` finds those and,
with --apply, wipes them.

Objects are never addressed by raw id from the Worker: a cold start through
idFromString has no ctx.id.name, so the Agents SDK throws, and later calls by
name land on the same broken instance. The script reads unclaimed objects
through Cloudflare's query API, and wipes go by name through destroy().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@blakebauman
blakebauman merged commit 9815af8 into main Nov 7, 2026
8 checks passed
@blakebauman
blakebauman deleted the feat/agent-chat-purge branch November 7, 2026 03:06
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