Skip to content

feat(patches): every patch lifecycle change is a business event, from every path - #541

Merged
allisonmahmood merged 4 commits into
mainfrom
analytics/536-patch-lifecycle-events
Oct 5, 2026
Merged

allisonmahmood merged 4 commits into
mainfrom
analytics/536-patch-lifecycle-events

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

Only a CLI delete reported patch.deleted. A portal delete made the same change and reported nothing, so deletions were undercounted. Sharing, retiring, restoring, rolling back and reassigning reported nothing from any path, and retiring or restoring a member's patches during deactivation or reactivation left no trace.

The Patches service now reports every lifecycle change itself, so each path that makes a change reports it: the CLI's API, the portal's patch actions and the deactivation and reactivation bulk path.

  • New events. patch.shared (scope, previousScope), patch.retired, patch.restored, patch.rolled_back (versionNumber, fromVersionNumber) and patch.reassigned (fromUserId; ownerUserId is the new owner) join patch.deleted in the catalogue. Every event except patch.reassigned carries byAdmin, which comes from the actor. Only admins reassign, so that event needs no byAdmin. All properties are ids, version numbers and states.
  • After commit. Lifecycle methods already queued wake notices until the outermost lifecycle transaction committed. Events now wait in the same queue: a nested savepoint that fails drops its own events, and a batch that rolls back reports nothing. That batch can be withDependencyLock around a deactivation, which is what makes a rolled-back deactivation report nothing. Events are reported when the transaction exits, so a caller interrupted just after commit still reports them.
  • Once per delete. PatchesApi's own patch.deleted call is gone, so an API delete reports once.
  • No-ops report nothing. Sharing to the current scope, rolling back to the current version and reassigning to the current owner report nothing. Neither do refusals.
  • Wiring. Patches.layer now needs Analytics. The server provides it below the services, and the test layers that build Patches provide layerNoop or a recording layer. @patchy/sdk gains @patchy/analytics as a dev dependency for its tests.

Tests cover each event on the service, the API path (share, retire, restore, rollback, delete, plus owner refusals) and the portal (scope, rollback, restore, retire and delete as owner and as admin, reassign, a refused delete). The deactivation retire and reactivation restore are tested in bulk. Rollbacks are tested two ways: a nested withDependencyLock that fails, and a deactivation whose second patch refuses after the first was retired. Making report fire immediately fails both.

Hit every surface:

  • Vocabulary and product shape. Applied. The catalogue entries are in Analytics.ts, and the docs/OPERATIONS.md "Business events" list gains a "Patch lifecycle" line.
  • Wire contract and CLI. Untouched: no request, response or command changed.
  • Not applicable: the dev loop, migrations, company resources, served pages and the repo layout.
  • Reverse states. Restore and reassign already existed; this change only reports them.

Touches outside #536's area, kept minimal:

Closes #536

Made by Claude Opus 5.5 via Claude Code in T3 Code.

@github-actions github-actions Bot added area:server Touches the server area area:docs Touches the docs area area:ui Portal and first-party browser pages, shared rendering and styles size:m 100-500 changed lines labels Oct 5, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high)

Round 1 verdict: not safe to merge. All three findings are addressed in 291e373.

  1. Medium, fixed. A committed change could lose its events when an interrupt arrived during COMMIT (Patches.ts).
    • Effect's withTransaction already commits uninterruptibly. The gap was the interruptible step between that and my onExit: a pending interrupt could turn the success exit into an interrupt, so nothing reported.
    • The outermost lifecycle transaction and its reporting now run inside one Effect.uninterruptibleMask, and only the work is restored to interruptible. An interrupt that arrives during COMMIT therefore waits until the committed change has reported.
    • Connection acquisition, BEGIN and COMMIT were already uninterruptible inside withTransaction, so no wait became newly uninterruptible.
  2. Medium, documented, not changed in code. A caught raw sql.withTransaction savepoint inside withDependencyLock keeps its queued events (Patches.ts).
    • This is real, but only for a composition no caller uses. Every caller calls the lifecycle methods directly, and each method is its own lifecycle savepoint that drops its events on failure.
    • The wake notices already follow the same contract.
    • Closing it for arbitrary raw savepoints would need the queue to live in the database transaction, which is machinery nobody uses. withDependencyLock's doc comment now states the contract instead.
  3. Low, fixed. The restore assertions allowed duplicates (PortalPages.test.ts). The portal restore and bulk reactivation tests now assert the full event list after setup, not just its last entry.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 2

Verdict: safe to merge. No remaining findings.

Sol also ran an isolated Effect transaction and interruption check, which passed.

@github-actions github-actions Bot added size:l 501-1,000 changed lines and removed size:m 100-500 changed lines labels Oct 5, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 3

CI fix and verdict. test (22/24/26) failed on 291e373 in Content.test.ts. The 60-second publish-deadline test forces a stall through a withTransaction seam, placed after the transaction body and before COMMIT. Since 291e373, the outermost lifecycle transaction runs under uninterruptibleMask with only the work restored, so that injected stall stopped being interruptible. The timeout never fired, and the later Content tests cascaded.

6de716a marks the injected stall Effect.interruptible, modelling a transaction body that is still working.

Verdict: safe to merge. Sol reports no findings:

  • The test change is legitimate. In production nothing runs at that seam except Effect's own commit, which is already uninterruptible.
  • No production interrupt was lost. Connection acquisition, BEGIN, COMMIT and cleanup were already masked inside withTransaction. The work itself stays interruptible, so the 60-second timeout, request cancellation and shutdown still apply. Sol confirmed identical interruption behaviour before and after the change in an isolated probe.
  • Reporting adds no wait. PostHog capture only queues the event; delivery happens in the background.

Locally, Content.test passes (21/21) and pnpm test passes (159 files, 1,566 tests).

allisonmahmood added a commit that referenced this pull request Oct 5, 2026
#541's lifecycle transaction already queues patch events, runs only the
work interruptibly and reports them with the commit. Deactivation and
reactivation now queue their user event there through
Patches.reportOnCommit, replacing the page's own mask and flag and the
explicit cancellable advisory wait, which its restore already covers.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Base automatically changed from analytics/534-business-events-company to main October 5, 2026 19:59
allisonmahmood and others added 4 commits October 5, 2026 21:59
… every path

The Patches service reports patch.shared, patch.retired, patch.restored,
patch.rolled_back, patch.reassigned and patch.deleted itself, so the CLI,
the portal and member deactivation or reactivation all report them. Events
queue beside the wake notices and leave only after the outermost lifecycle
transaction commits; a refused, rolled-back or no-op change reports nothing.
PatchesApi no longer reports patch.deleted on its own.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… is interrupted

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ge to report

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…s a transaction body is

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@allisonmahmood
allisonmahmood force-pushed the analytics/536-patch-lifecycle-events branch from 6de716a to 0d4d6fa Compare October 5, 2026 19:59
@allisonmahmood
allisonmahmood merged commit 658aab6 into main Oct 5, 2026
24 of 36 checks passed
@allisonmahmood
allisonmahmood deleted the analytics/536-patch-lifecycle-events branch October 5, 2026 20:39
allisonmahmood added a commit that referenced this pull request Oct 5, 2026
#541's lifecycle transaction already queues patch events, runs only the
work interruptibly and reports them with the commit. Deactivation and
reactivation now queue their user event there through
Patches.reportOnCommit, replacing the page's own mask and flag and the
explicit cancellable advisory wait, which its restore already covers.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
allisonmahmood added a commit that referenced this pull request Oct 5, 2026
The product dashboard can't see onboarding. Creating or joining a
company, invitations, role changes, deactivation and connections sent no
business event. The only trace of `POST /join` was an anonymous request
event that couldn't tell creating a company from accepting an
invitation.

Each of those actions now reports one business event from the catalogue,
after its change commits. A refused or rolled-back action reports
nothing.

| Event | Emitted by | Principal |
| --- | --- | --- |
| `company.created`, `user.joined` (`via`, `role`, `inviteId` for
invitations) | `Join.ts` (Companies) | the new user's own id |
| `invite.sent` (`role`, `emailed`), `invite.resent` (`emailed`),
`invite.revoked` | `CompanyPage.ts` (Companies) | the admin |
| `user.role_changed` (`userId`, `role`) | `CompanyPage.ts` | the admin
|
| `user.deactivated`, `user.reactivated` (`userId`) |
`UserLifecyclePage.ts` (Portal), queued with #541's patch events and
reported when the `withDependencyLock` transaction commits | the admin |
| `connection.connected`, `connection.deleted` (`connectionId`,
`integration`) | `ConnectionPage.ts` (Integrations) | the admin |

- **Where events fire.** The page modules that call the services emit,
not the services themselves. `Companies.create`, `consumeInvite` and
`Invitations.*` each commit their own transaction before returning, so
the page emits after commit. The only dependency change is
`@patchy/companies → @patchy/analytics`. A browser disconnect can't drop
the event of a change that committed, even one that lands during COMMIT.
Invitations already deliver uninterruptibly, so each one and its event
share one uninterruptible region. Join, role changes and connection
connect/delete use a new `withReportedCommit` in `@patchy/sql`. It opens
the outermost transaction with only the change cancellable, lock waits
included, and runs the report with the commit. Connections pass their
report to `ConnectionStore.connect`/`delete`, which apply it around
their own transaction, so discovery's audit rows stay outside it.
Deactivation and reactivation use #541's mechanism instead.
`withLifecycleTransaction` queues lifecycle events, runs only the work
interruptibly, and reports the events with its commit. The page queues
its user event in the same batch through a new `Patches.reportOnCommit`,
so a committed deactivation reports `user.deactivated` and each
`patch.retired` once, and a rolled-back or cancelled one reports none of
them. The dependency lock serializes these commits, so a repeated action
is refused and reports nothing.
- **`Users.setRole`** now returns `{ user, changed }`. A stale form
asking for a role the user already has changes nothing and reports
nothing.
- **`ConnectionStore.delete`** now returns the connection it deleted,
read under its row lock, so the event can name the integration.
- **Properties.** Only ids, roles, booleans and the integration kind
(`postgres`).
- **Mail failures.** `emailed` is false whenever no email went out. That
includes a resend where Clerk wouldn't revoke the old link, so the
invitation is unchanged; the event then reads as a resend attempt that
didn't email. A revoke whose Clerk link couldn't be revoked still
reports `invite.revoked`, because Patchy revoked it.
- **Tests.** The existing page tests now record events. They assert one
event per success, the new user as principal on join, and nothing on
refusal: member, origin, foreign company, invalid form, duplicate
invite, last admin, a stale same-role change, a repeated deactivation, a
concurrent change, a deletion refused because a patch uses the
connection, and the injected rollback. One test interrupts an invitation
request during delivery and checks that the committed invitation is
still reported. Another disconnects a deactivation blocked on a held
company lock and checks that the server stops waiting, rolls back and
reports nothing. A third pauses wake publication after a deactivation
commits, disconnects, and checks that `user.deactivated` and the
`patch.retired` each follow once. A Patches test covers
`reportOnCommit`: rolled back reports nothing, committed reports once,
outside the lock dies. SQL tests cover `withReportedCommit`: a deferred
trigger makes COMMIT slow, an interrupt during it still reports, a
change cancelled before commit reports nothing, and inside a caller's
transaction it leaves reporting to that caller.

Hit every surface: vocabulary applied. `packages/sql/CONTEXT.md` gains
**Reported commit**. `CONTEXT-MAP.md`'s Analytics relationship now names
Companies and Integrations, and says Portal's lifecycle events report
through Patches; and `docs/OPERATIONS.md` lists the new events. I also
made one minimal edit outside this ticket's files: the intro line of
`packages/analytics/CONTEXT.md` (owned by #534) listed only Patches and
Auth as the contexts that decide business moments, so I added the three
new ones. In #536's area I added one method to the Patches service,
`reportOnCommit`, which pushes onto #541's pending-events queue. The
wire contract, CLI, dev loop, migrations, company resources, served
pages and repo layout are untouched.

Privacy page (#537): the new events carry company, user, invitation and
connection ids, roles, whether an invitation email went out, and the
integration kind. They never carry names, email addresses or connection
strings.

Closes #535

Made by Claude Opus 5.5 via Claude Code in T3 Code.

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
allisonmahmood added a commit that referenced this pull request Oct 6, 2026
…ease (#556)

What's new for this deploy, plus the history since the first production
deploy. The thin entries #555 seeded by hand are rewritten from what
each deploy actually shipped. This is the first run of `AGENTS.md` ›
Deploying: a fresh subagent gathered and wrote these releases, and I
checked them against the PRs.

**Release 4 (this deploy, `4a928f7..8697463`)**
- New: **See what's new in Patchy** (#555)

**Release 3 (2026-10-06, `aed1454..4a928f7`)**
- Fixed: **Creating a company explains a handle it can't use** (#553)

**Release 2 (2026-10-05, `48153dc..aed1454`)**
- Behind the scenes: key moments are recorded, identifying people by id,
never by name or email address (#538, #539, #541)
- Behind the scenes: the patchy command reports its version, command and
coding agent (#540)

**Release 1 (2026-10-05, first production deploy, `48153dc`)**
- Behind the scenes: Patchy Cloud's first release went live

Made by Claude Opus 5.5 via Claude Code in T3 Code.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Touches the docs area area:server Touches the server area area:ui Portal and first-party browser pages, shared rendering and styles size:l 501-1,000 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(patches): every patch lifecycle change is a business event, from every path

1 participant