feat: closed-loop engagement tuning (Phase I)#33
Closed
psdjungpulzze wants to merge 2 commits into
Closed
Conversation
Wires Archetype 6 (Engagement / Effectiveness) — the last of the six
loop archetypes. Adds ENGAGE signal emission to the notification mark-
read endpoint and a weekly tune-engagement job that aggregates ENGAGE
signals per (tenant × feature) into per-tenant engagement-rate
calibrations.
- src/app/api/v1/notifications/[id]/read/route.ts — emits ENGAGE on
the *first* read (wasUnread guard), so we count actual engagement
rather than every revisit. Notification has no organizationId field,
so we attribute via the user's primary OrgMembership (oldest by
joinedAt). Multi-org users get best-effort attribution.
- src/worker/jobs/tune-engagement.ts (new) — pure derivation
computeEngagementScale (count/day) plus per-tenant orchestration.
Aggregates ENGAGE by feature, writes engagement-rate:<feature>
calibrations to loops_calibrations. Skips features with no signals,
updates existing rows in place.
- src/worker/index.ts — registers the job in maintenanceProcessor;
cron 30 5 * * 1 (Monday 05:30, after threshold tuning at 05:00).
- src/worker/jobs/tune-engagement.test.ts (new) — 12 unit tests:
pure math (rate normalization, defensive zero/negative handling,
rounding) and orchestration (LOOPS_ENABLED gate, multi-tenant
iteration, per-tenant fault isolation, feature aggregation,
30-day lookback, update-in-place vs create).
Cold-start safety preserved end-to-end:
- LOOPS_ENABLED off → emit + job both no-op
- User has no org membership → no emission (best-effort attribution)
- No ENGAGE signals → no calibrations written
- Read-site consumers get null on cold start via the standard
getCalibration helper
Closes the closed-loop architecture: every archetype now has a full
working loop. Producers + consumers + calibration/threshold/engagement
jobs all wired.
5 tasks
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase I — Engagement / Effectiveness Loop
The last of the six loop archetypes. Adds ENGAGE signal emission to notification mark-read and a weekly tune-engagement job that aggregates per-tenant engagement scores into `loops_calibrations`.
Depends on: #32 (Phase H — completion). Branched off Phase H.
What's in this PR
Cold-start safety
Test plan
Loops architecture status after this PR
All six archetypes have producer + storage + aggregation/learning jobs wired. Phase J wires the remaining production consumers (cost forecast → `budget.ts`, threshold → auto-flag severity, engagement → digest) so every loop steers behavior end-to-end.