Skip to content

feat(worker): delete an organization's Code Storage mirror when its last link to a repository goes - #112

Draft
electric-gizzard wants to merge 1 commit into
mainfrom
claude/github-issue-86-f8c0e0
Draft

electric-gizzard wants to merge 1 commit into
mainfrom
claude/github-issue-86-f8c0e0

Conversation

@electric-gizzard

Copy link
Copy Markdown
Contributor

Closes #86.

What was wrong

With Code Storage configured, linking a GitHub repository makes a mirror of it for the organization (codeStorageRepoName(orgId, fullName)): a copy of the code, plus the work its sessions save on branches GitHub never sees. Nothing ever deleted one, so the organization kept a readable copy after its access to the repository ended.

What this does

The organization's mirror of a repository is deleted as soon as none of its folders links the repository, whatever took the last link away:

Cause Before Now
Unlinked from its last folder link removed mirror deleted, then link removed
GitHub account disconnected repositories unlinked mirrors deleted, then unlinked
App uninstalled (installation deleted) same, per organization same, per organization
Repository removed from the App's access (installation_repositories removed) ignored unlinked from every folder of every connected organization, mirrors deleted

A mirror is kept while another folder of the same organization still links the repository, and another organization's mirror of the same repository is never touched.

Decisions (the three the issue left open)

  • Saved session work is dropped, with a warning. Moving it to per-session repositories would keep a full copy of the code, which is what this removes; and that fallback no longer exists for GitHub repositories since feat(engine)!: provider-neutral repositories, change hosts and a session environment #93. The app now asks before an unlink that deletes a mirror (UnlinkRepoDialog, shown only for a last link with a store), and the disconnect dialog says it too. Webhook causes cannot ask anyone.
  • Cleanup does not wait for running sessions. A session whose sandbox is alive keeps its checkout and its GitHub access, but its saves and moves to another sandbox fail from then on. Waiting would let the copy outlive the access indefinitely, since sessions are rarely archived.
  • Mirrors orphaned before this change are not swept. Left for a follow-up. One exception falls out naturally: a repositories-removed delivery deletes the mirror of each named repository even when no folder links it.

How it is ordered

The mirror is deleted before the link is taken away. If Code Storage refuses, the route answers 502 with a message a person can act on, nothing else has changed, and the action can be tried again. Deleting after the unlink would have left an orphan with nothing left to say which repository it was. A mirror that is already gone is not a failure, so retries and redeliveries are safe.

Mirror names are always derived from the organization and owner/name, never read from a link's store, so links written before RepoRef (no store recorded) are covered and nothing outside org-<orgId>/ can be deleted.

Behaviour changes to be aware of

  • DELETE /workspace/folders/{folderId}/repos now fails (500) when the workspace's folders cannot be read, where it used to send the unlink anyway. Guessing "not linked elsewhere" could delete a mirror still in use.
  • An unlink or disconnect fails (502) while Code Storage cannot delete, instead of succeeding.
  • A repository removed from the App's access is now unlinked. If it is added back on GitHub, it has to be linked again in Shuttle.
  • NormalizedWebhook has a new kind, installation.repos.removed.

Known gaps

  • A repository linked in the instant between the read and the delete can end up linked with no mirror (the same window disconnectAccount already documents). Its sessions run but cannot save until it is relinked.
  • A session that outlives its mirror gets git's own error when it tries to save or move, not a tailored message.
  • Not handled, as not in the issue: repository deleted, renamed or transferred on GitHub; App suspended.

Testing

  • bun run verify passes (types, tests, lint, format), and bun run check:package.
  • New apps/worker/src/github/repo-links.test.ts (16 tests) drives the real routes and webhook handler against a fake Code Storage: last link vs. still linked elsewhere, legacy links, other organizations untouched, delete-before-unlink order, a refused delete leaving everything in place, an already-missing mirror, redelivery after a failure, and no Code Storage configured.
  • Webhook normalization and unlinkDeletesSavedWork have unit tests.
  • Not verified: the two dialogs were not seen in a browser (this worktree has no .dev.vars), and nothing was run against real Code Storage. Worth a manual pass: unlink a last-linked repository and confirm the dialog, then check the mirror is gone in Code Storage.

🤖 Generated with Claude Code

…ast link to a repository goes

An organization's mirror of a linked GitHub repository held a copy of the
code, and its sessions' saved work, for good. It is now deleted as soon as
no folder of the organization links the repository: on an unlink from the
last folder, a GitHub account disconnect, the App being uninstalled, and
the repository being taken out of what the App may access
(`installation_repositories` removed, which also unlinks it).

The mirror is deleted before the link is taken away, so a failure leaves
everything in place to be tried again. Deleting is immediate and drops the
saved session work; the app asks before an unlink or disconnect that does.

Closes #86

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

No deployments
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.

Remove an organization's mirrored repositories when its access to them ends

1 participant