Skip to content

feat(analytics): onboarding moments are business events - #539

Merged
allisonmahmood merged 7 commits into
mainfrom
analytics/535-onboarding-events
Oct 5, 2026
Merged

allisonmahmood merged 7 commits into
mainfrom
analytics/535-onboarding-events

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

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 feat(patches): every patch lifecycle change is a business event, from every path #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.

@github-actions github-actions Bot added area:server Touches the server area area:docs Touches the docs area area:integrations Company connections and integration implementations 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

Self-review (/code-review, Standards + Spec against #535)

Fixed in a7cfeca:

  • A repeated deactivation is now tested: it's refused with 409 and reports nothing more.
  • The failed-delivery test now asserts each action's event in its own branch, instead of a nested ternary.

Kept as is:

  • The recording Analytics layer is copied per test suite. This matches the existing DeviceLogins and PatchesApi suites. A shared Analytics.layerRecording would change Analytics.ts's exports, which feat(analytics): every business event names its company #534 owns and fix(patches): every patch lifecycle change is a business event, from every path #536 is editing in parallel. Worth a follow-up once the stack lands.
  • The test projections take different shapes. The connection and lifecycle helpers filter to their own event names, so fix(patches): every patch lifecycle change is a business event, from every path #536's patch events in the same database can't break them.
  • Spec, "a refused connect isn't tested". It is: the failures test makes a connect that fails inspection (503) and one with an invalid form (422), and asserts only the earlier successful connect was reported.
  • Spec, "a resend that changed nothing still emits". This is deliberate. invite.resent with emailed: false records that the admin's resend didn't email, and the invitation stays pending. The PR body notes it.
  • Role is duplicated in Analytics.ts. Analytics can't import Companies, because the dependency points the other way. An emitter that passes a new role fails to typecheck.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 1: not safe to merge, 1 finding

  • Major, fixed in 6a3420a: a committed invitation could lose its event on disconnect. Invitations.create/revoke/resend commit uninterruptibly, but a disconnect during delivery queued an interruption that landed after the commit and before track. The same window existed after every emitter's commit. I first tried Effect.onExitFilter(Exit.filterValue, …), and a probe showed a pending interrupt turns the committed exit into an interruption, so that loses the event too. Each change and its event now share one uninterruptible region in Join.ts, CompanyPage.ts, ConnectionPage.ts and UserLifecyclePage.ts. A new test interrupts an invite request mid-delivery and asserts the committed invitation is reported. It fails without the fix.

Sol found no Standards issues and confirmed the rest: all events wired, principals right, refusals and rollback silent, docs in sync.

@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 2: not safe to merge, 1 finding

Sol confirmed the round-1 invitation fix is sound.

  • Major, fixed in 6ffa783: the round-1 mask made lock waits uncancellable. withTransaction runs its work under the caller's interruptibility, so wrapping a whole lifecycle transaction in uninterruptible meant a disconnected admin couldn't cancel its wait on the company row lock. An invitation delivery holds that lock, so the wait could pin the dependency lock. The database actions (join, role change, connect/delete, deactivate/reactivate) now use uninterruptibleMask((restore) => restore(change).pipe(Effect.tap(track))), so the work stays cancellable. Invitations were already uninterruptible and keep their single region; restore around them would lose the event again.
  • Remaining window, by design. If the disconnect arrives during the COMMIT round trip of a database action, the commit lands but the request is reported as interrupted, so its event is lost. Closing that would need an after-commit hook in the SQL layer, and the cost here is one missing analytics event, not a wrong one.
  • New test. It holds the company row, sends a deactivation that blocks on it, disconnects, and asserts the server stops waiting while the lock is still held, rolls back and reports nothing. With the round-1 wrapper it times out.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 3: not safe to merge, 1 finding

Sol confirmed the round-2 fix restores cancellable lock waits.

  • Major, fixed in f689859: a lifecycle event could drop after its commit. withDependencyLock publishes wakes after it commits. A disconnect during that publish skipped the event, because the round-2 restore wrapped the whole lock helper. The mask is inverted now: withDependencyLock(id)(restore(run)). Only the work inside the transaction is cancellable, including the company-row lock wait from round 2. The commit, the wakes and the event run protected. The dependency lock's own advisory wait sits outside run, so Patches.withDependencyLock now makes it explicitly cancellable (one line, flagged for fix(patches): every patch lifecycle change is a business event, from every path #536). On this path this also closes the COMMIT-round-trip window. The other database emitters (join, role change, connect/delete) do no work after their commit, so only that accepted window remains for them.
  • New test. It pauses wake publication after a deactivation commits (a fresh Patches layer over paused Wakes), disconnects, resumes, and asserts exactly one user.deactivated. With the round-2 shape it times out. The round-2 lock-wait test still passes.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 4: safe to merge, no findings. Sol verified the round-3 fix (commit, wakes and event protected together; advisory-lock wait still cancellable) and confirmed all ten events, privacy, docs and tests. The only remaining item is the accepted COMMIT-round-trip residual described in round 2. Nothing is outstanding.

@allisonmahmood
allisonmahmood force-pushed the analytics/535-onboarding-events branch from f689859 to 1d7d0c8 Compare October 5, 2026 18:35
@allisonmahmood
allisonmahmood changed the base branch from analytics/534-business-events-company to analytics/536-patch-lifecycle-events October 5, 2026 18:35
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 5, after restacking onto #541: not safe to merge, 1 finding

Sol confirmed the Patches/lifecycle combination is correct.

  • What changed in the restack. feat(patches): every patch lifecycle change is a business event, from every path #541's withLifecycleTransaction already queues events and reports them with its commit. So deactivation and reactivation now queue user.deactivated/reactivated there through a new Patches.reportOnCommit. The page's own mask, flag and Analytics dependency are gone, and so is the one-line cancellable advisory wait, which feat(patches): every patch lifecycle change is a business event, from every path #541's restore already covers.

  • What Sol checked. Advisory, company-row and patch-row lock waits stay cancellable. A committed deactivation reports its user event and each patch.retired once. A rolled-back or cancelled one reports none of them.

  • Major, fixed in 844d5a4: the COMMIT window I had accepted in round 2. Join, role changes and connection connect/delete restored their whole service call, so an interrupt during COMMIT dropped a committed change's event. With feat(patches): every patch lifecycle change is a business event, from every path #541 closing that window for patches, I closed it here too. withReportedCommit in @patchy/sql opens the outermost transaction inside uninterruptibleMask, restores only the change, and runs the report with the commit. Connections pass the report to the store, because wrapping connect in a page transaction would roll back discovery's audit rows on failure.

  • New tests. A deferred trigger makes COMMIT sleep, and the test interrupts during it and asserts the report ran. It fails with the old shape. Other cases cover a cancellation before commit (nothing reported) and use inside a caller's transaction (no report).

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, high), round 6: safe to merge, no findings. Sol verified the round-5 fix: COMMIT and the report are protected together, the change stays cancellable including lock waits, refusals roll back cleanly with no event, safe() still covers SqlError from the outer transaction, and discovery's audit stays outside. Nothing is outstanding.

@allisonmahmood
allisonmahmood force-pushed the analytics/536-patch-lifecycle-events branch from 6de716a to 0d4d6fa Compare October 5, 2026 19:59
Base automatically changed from analytics/536-patch-lifecycle-events to main October 5, 2026 20:39
allisonmahmood and others added 7 commits October 5, 2026 22:39
Creating or joining a company, invitations, role changes, deactivation,
reactivation and connections now each report one business event after
their change commits. Joins belong to the new user's own id.

Users.setRole reports whether it changed the role, so a stale form asking
for the current role reports nothing. ConnectionStore.delete returns the
connection it deleted, so its event can name the integration.

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

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
An interrupted request could still commit (invitations deliver
uninterruptibly), then lose its event before track ran. Each emitter now
runs its change and its event in one uninterruptible region, so a commit
always reports and a refusal or rollback never does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Holding a whole transaction uninterruptible meant a disconnected admin
couldn't cancel a lock wait, so a slow invitation delivery could pin the
company dependency lock. Database actions now run cancellably and only
the step from a returned commit to its event is protected. Invitations,
already uninterruptible, keep their single region.

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

A disconnect while wakes published after the dependency-lock commit
skipped the lifecycle event. Only the work inside the transaction is now
cancellable; its commit, wakes and event run protected. The dependency
lock's advisory wait is made explicitly cancellable so the protection
doesn't extend to it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
#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]>
…d change

Join, role changes and connection connect/delete restored their whole
service call, so an interrupt landing during COMMIT dropped the event of a
change that committed. @patchy/sql's withReportedCommit opens the
outermost transaction with only the change cancellable and runs the
report with the commit. Connections pass their report to the store, which
applies it around its own transaction so discovery's audit stays outside.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@allisonmahmood
allisonmahmood force-pushed the analytics/535-onboarding-events branch from 844d5a4 to 08027db Compare October 5, 2026 20:39
@allisonmahmood
allisonmahmood merged commit 8b96c4c into main Oct 5, 2026
27 of 35 checks passed
@allisonmahmood
allisonmahmood deleted the analytics/535-onboarding-events branch October 5, 2026 21:29
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:integrations Company connections and integration implementations 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.

feat(analytics): onboarding moments are business events

1 participant