@checkstack/backend 0.7.1 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,154 @@
1
1
  # @checkstack/backend
2
2
 
3
+ ## 0.8.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 2a749d3: fix: run afterPluginsReady in topological order; merge daily rollups on conflict
8
+
9
+ Two resilience fixes for the dependency chain:
10
+
11
+ 1. **Plugin loader**: Phase 3 (`afterPluginsReady`) now iterates plugins
12
+ in the same topologically-sorted order as Phase 2 (`init`). Previously
13
+ it iterated `pendingInits` in registration order, which raced
14
+ subscription-spec dependencies — catalog's afterPluginsReady registers
15
+ `catalog.system` and `catalog.group` notification targets, and emitting
16
+ plugins (incident, maintenance, …) call `registerSubscriptionSpec`
17
+ against those targets in their own afterPluginsReady. With registration
18
+ order, an emitter could run before catalog and hit
19
+ `Target type catalog.group is not registered`. Sorted order encodes
20
+ the dependency via `spec.target.ownerPlugin`, so the emitter now
21
+ always runs after the target owner.
22
+
23
+ 2. **Healthcheck retention job**: the daily rollup now upserts
24
+ `health_check_aggregates` with `ON CONFLICT DO UPDATE` instead of a
25
+ plain insert. Previously, late-arriving hourly aggregates (e.g. from
26
+ a satellite that was offline when the prior rollup ran) would crash
27
+ the rollup with a unique-constraint violation on
28
+ `(configuration_id, system_id, bucket_start, bucket_size, source_id)`.
29
+ The merge sums counts and folds min/max/p95 into the existing daily
30
+ row.
31
+
32
+ ## 0.8.0
33
+
34
+ ### Minor Changes
35
+
36
+ - 32d52c6: feat: notification target pattern + per-spec subscriptions
37
+
38
+ Replaces the all-or-nothing catalog system/group notification model with a
39
+ platform-level target pattern. Each notification-emitting plugin declares
40
+ _subscription specs_ against typed _target_ objects exported from the
41
+ target's owning plugin (catalog ships `catalogSystemTarget` and
42
+ `catalogGroupTarget`). Notification-backend handles every per-resource
43
+ group lifecycle, parent-edge inheritance, and legacy-subscription seeding
44
+ — plugins never author groupId helpers, lifecycle hooks, or migration
45
+ code again.
46
+
47
+ **Plugin-author surface area is now ~12 lines per emitter:**
48
+
49
+ ```ts
50
+ // <plugin>-common
51
+ const { defineSubscription } = createSubscriptionFactory(pluginMetadata);
52
+ export const fooSystemSubscription = defineSubscription({
53
+ localId: "system",
54
+ target: catalogSystemTarget,
55
+ display: { title: "Foo Alerts", description: "...", iconName: "Bell" },
56
+ });
57
+
58
+ // <plugin>-backend register()
59
+ env.registerSubscriptionSpecs([fooSystemSubscription]);
60
+ // ^ feeds the plugin loader's dependency sorter — each spec's
61
+ // target.ownerPlugin becomes an implicit init-order dep, so this
62
+ // plugin automatically waits for catalog (the target owner) to
63
+ // finish init + afterPluginsReady before its own runs.
64
+
65
+ // <plugin>-backend afterPluginsReady
66
+ await notificationClient.registerSubscriptionSpec(
67
+ specToRegistration(fooSystemSubscription)
68
+ );
69
+ // dispatch
70
+ await notificationClient.notifyForSubscription({
71
+ specId: fooSystemSubscription.specId,
72
+ resourceKeys: [systemId],
73
+ title,
74
+ body,
75
+ importance,
76
+ action,
77
+ collapseKey,
78
+ subjects,
79
+ });
80
+
81
+ // <plugin>-frontend
82
+ createNotificationSubscriptionExtension({ spec: fooSystemSubscription });
83
+ ```
84
+
85
+ **Migrated plugins**: anomaly, incident, maintenance, healthcheck,
86
+ dependency. Each lost its bespoke `notification-groups.ts`,
87
+ `bootstrap*NotificationGroups`, `ensure*Group`, and inheritance walk —
88
+ all of that is now centralized in notification-backend's
89
+ `subscription-engine`.
90
+
91
+ **Plugin loader change** (`@checkstack/backend-api`,
92
+ `@checkstack/backend`): the register-time API gains
93
+ `env.registerSubscriptionSpecs([...specs])`. The dependency sorter
94
+ walks `spec.target.ownerPlugin` for every declared spec and adds the
95
+ target owner as an init-order dependency of the emitting plugin. This
96
+ guarantees that catalog (the owner of the platform's `system` and
97
+ `group` targets) completes init + afterPluginsReady before any
98
+ emitting plugin tries to register its specs against the notification
99
+ service — no string-prefix heuristics, no manual `dependsOnPlugins`
100
+ list, no stub rows. Plugins that fail to declare their specs at
101
+ register time get a clear `Target type X is not registered. Did the
102
+ emitting plugin declare this spec via env.registerSubscriptionSpecs?`
103
+ error from the dispatcher.
104
+
105
+ **Removed** (no backwards compat):
106
+
107
+ - `catalogClient.notifySystemSubscribers` and
108
+ `catalogClient.notifyManySystemSubscribers`
109
+ - `notificationClient.notifyUsers` and `notificationClient.notifyGroups`
110
+ as direct dispatch primitives — replaced by spec-bound
111
+ `notifyForSubscription`
112
+ - catalog's `bootstrapNotificationGroups` (replaced by
113
+ `bootstrapNotificationTargets`)
114
+
115
+ **Enforcement**: the dispatcher rejects calls referencing unregistered
116
+ specIds, specs owned by other plugins, or resourceKeys that haven't been
117
+ pushed via `upsertNotificationResource`. Display metadata for any
118
+ groupId is recoverable via the spec registry, so audit lists render
119
+ correct labels even when an emitter's frontend isn't loaded.
120
+
121
+ **Per-field anomaly mute** keeps working — it now lives inside the
122
+ generic SubscriptionRow's optional `SubControls` panel
123
+ (`AnomalyFieldMuteList`), exposed through the catalog system detail
124
+ page's notifications card.
125
+
126
+ The catalog system detail page renders a "Notifications" card hosting
127
+ `SystemNotificationSubscriptionsSlot`. The matching group surface is
128
+ not yet rendered — group-level subscriptions are wired end-to-end on
129
+ the backend; a follow-up will add the host UI.
130
+
131
+ **Migration of existing subscribers**: target types declare a
132
+ `legacyGroupIdTemplate`; on first registration of each spec,
133
+ notification-backend reads subscribers from the legacy
134
+ `catalog.system.<id>` / `catalog.group.<id>` groups and seeds the new
135
+ spec groups exactly once per (spec × resource) pair, tracked in
136
+ `subscription_migrations`. Anomaly stays opt-in (its target also
137
+ declares the template, but the user-explicit nature of the original
138
+ opt-in flow means the seeding produces the same set of subscribers
139
+ they already had).
140
+
141
+ ### Patch Changes
142
+
143
+ - Updated dependencies [32d52c6]
144
+ - Updated dependencies [32d52c6]
145
+ - Updated dependencies [32d52c6]
146
+ - @checkstack/backend-api@0.14.0
147
+ - @checkstack/auth-common@0.6.4
148
+ - @checkstack/cache-api@0.2.2
149
+ - @checkstack/queue-api@0.2.16
150
+ - @checkstack/signal-backend@0.2.1
151
+
3
152
  ## 0.7.1
4
153
 
5
154
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@checkstack/backend",
3
- "version": "0.7.1",
3
+ "version": "0.8.1",
4
4
  "checkstack": {
5
5
  "type": "backend"
6
6
  },
@@ -15,13 +15,13 @@
15
15
  "dependencies": {
16
16
  "@checkstack/api-docs-common": "0.1.10",
17
17
  "@checkstack/auth-common": "0.6.3",
18
- "@checkstack/backend-api": "0.13.0",
18
+ "@checkstack/backend-api": "0.13.1",
19
19
  "@checkstack/common": "0.7.0",
20
20
  "@checkstack/drizzle-helper": "0.0.4",
21
- "@checkstack/cache-api": "0.2.0",
22
- "@checkstack/queue-api": "0.2.14",
23
- "@checkstack/signal-backend": "0.1.20",
24
- "@checkstack/signal-common": "0.1.10",
21
+ "@checkstack/cache-api": "0.2.1",
22
+ "@checkstack/queue-api": "0.2.15",
23
+ "@checkstack/signal-backend": "0.2.0",
24
+ "@checkstack/signal-common": "0.2.0",
25
25
  "@hono/zod-validator": "^0.7.6",
26
26
  "@orpc/client": "^1.13.14",
27
27
  "@orpc/contract": "^1.13.14",
@@ -41,6 +41,7 @@
41
41
  "@types/bun": "latest",
42
42
  "@checkstack/tsconfig": "0.0.5",
43
43
  "@checkstack/scripts": "0.1.2",
44
- "@checkstack/test-utils-backend": "0.1.20"
44
+ "@checkstack/test-utils-backend": "0.1.21",
45
+ "drizzle-kit": "^0.31.10"
45
46
  }
46
47
  }
@@ -14,6 +14,15 @@ export function sortPlugins({
14
14
  pendingInits: {
15
15
  metadata: PluginMetadata;
16
16
  deps: Record<string, ServiceRef<unknown>>;
17
+ /**
18
+ * Optional plugin-id-level dependencies. Each id adds an edge
19
+ * `dep -> consumer`, ensuring the consumer's init and
20
+ * afterPluginsReady run after the dep's. Used by the notification
21
+ * subscription pattern: emitter plugins automatically depend on
22
+ * the plugins that own the targets they emit subscriptions for,
23
+ * derived at register time from `spec.target.ownerPlugin`.
24
+ */
25
+ pluginDependencies?: Set<string>;
17
26
  }[];
18
27
  providedBy: Map<string, string>;
19
28
  logger: Logger;
@@ -59,6 +68,26 @@ export function sortPlugins({
59
68
  }
60
69
  }
61
70
 
71
+ // Plugin-id-level deps (currently sourced from declared
72
+ // notification subscription specs). Each entry forces the consumer
73
+ // to load after the named plugin even when there's no shared
74
+ // ServiceRef between them.
75
+ if (p.pluginDependencies) {
76
+ for (const depPluginId of p.pluginDependencies) {
77
+ if (depPluginId === consumerId) continue;
78
+ if (!graph.has(depPluginId)) {
79
+ // Dep plugin isn't loaded — skip silently. The runtime RPC
80
+ // layer (notification-backend) will produce a clearer error
81
+ // if the dep was actually required.
82
+ continue;
83
+ }
84
+ if (!graph.get(depPluginId)!.includes(consumerId)) {
85
+ graph.get(depPluginId)!.push(consumerId);
86
+ inDegree.set(consumerId, (inDegree.get(consumerId) || 0) + 1);
87
+ }
88
+ }
89
+ }
90
+
62
91
  // Special handling: if this plugin uses queueManager, it must wait for all queue plugin providers
63
92
  const usesQueueManager = Object.values(p.deps).some(
64
93
  (ref) => ref.id === coreServices.queueManager.id
@@ -88,6 +88,13 @@ export function registerPlugin({
88
88
  // Store metadata for request-time context injection
89
89
  deps.pluginMetadataRegistry.set(pluginId, backendPlugin.metadata);
90
90
 
91
+ // Plugin dependencies derived from declared subscription specs.
92
+ // Populated by `registerSubscriptionSpecs` below; attached to the
93
+ // PendingInit when registerInit fires (a plugin's register block
94
+ // calls registerInit at most once, so timing is deterministic
95
+ // regardless of registration order within register()).
96
+ const pluginDependencies = new Set<string>();
97
+
91
98
  // Execute Register
92
99
  backendPlugin.register({
93
100
  registerInit: <
@@ -108,8 +115,22 @@ export function registerPlugin({
108
115
  init: args.init as InitCallback,
109
116
  afterPluginsReady: args.afterPluginsReady as InitCallback | undefined,
110
117
  schema: args.schema,
118
+ pluginDependencies,
111
119
  });
112
120
  },
121
+ registerSubscriptionSpecs: (specs) => {
122
+ for (const spec of specs) {
123
+ if (spec.ownerPlugin !== pluginId) {
124
+ throw new Error(
125
+ `Plugin ${pluginId} declared a subscription spec it does not own (ownerPlugin=${spec.ownerPlugin})`,
126
+ );
127
+ }
128
+ const targetOwner = spec.target.ownerPlugin;
129
+ if (targetOwner && targetOwner !== pluginId) {
130
+ pluginDependencies.add(targetOwner);
131
+ }
132
+ }
133
+ },
113
134
  registerService: (ref: ServiceRef<unknown>, impl: unknown) => {
114
135
  deps.registry.register(ref, impl);
115
136
  providedBy.set(ref.id, pluginId);
@@ -494,7 +515,15 @@ export async function loadPlugins({
494
515
  );
495
516
  }
496
517
  }
497
- for (const p of pendingInits) {
518
+ // Run afterPluginsReady in topologically-sorted order, matching Phase
519
+ // 2 init order. Iterating `pendingInits` directly would use registration
520
+ // order, which races dependency chains: e.g. catalog's
521
+ // afterPluginsReady registers `catalog.group` as a notification target,
522
+ // and emitting plugins' afterPluginsReady call `registerSubscriptionSpec`
523
+ // against it — so the emitters MUST run after catalog. Topo order
524
+ // already encodes this dependency (via spec.target.ownerPlugin).
525
+ for (const id of sortedIds) {
526
+ const p = pendingInits.find((x) => x.metadata.pluginId === id)!;
498
527
  if (p.afterPluginsReady) {
499
528
  try {
500
529
  const resolvedDeps: Record<string, unknown> = {};
@@ -11,4 +11,14 @@ export interface PendingInit {
11
11
  init: InitCallback;
12
12
  afterPluginsReady?: InitCallback;
13
13
  schema?: Record<string, unknown>;
14
+ /**
15
+ * Plugin ids this plugin must load *after*. Derived (not authored) —
16
+ * computed at register time from the owners of any notification
17
+ * subscription specs the plugin declared via
18
+ * `registerSubscriptionSpecs`. Each declared spec contributes its
19
+ * target's `ownerPlugin` to this set, so emitter plugins
20
+ * automatically wait for the target owner without anyone
21
+ * maintaining a manual dependency list.
22
+ */
23
+ pluginDependencies: Set<string>;
14
24
  }
@@ -423,6 +423,10 @@ export class PluginManager {
423
423
  handlers.push(cleanup);
424
424
  this.cleanupHandlers.set(metaPluginId, handlers);
425
425
  },
426
+ registerSubscriptionSpecs: () => {
427
+ // No-op in this single-plugin registration path; ordering
428
+ // only matters during full-system load (plugin-loader).
429
+ },
426
430
  getExtensionPoint: <T>(ref: ExtensionPoint<T>) =>
427
431
  this.extensionPointManager.getExtensionPoint(ref),
428
432
  registerRouter: (router: unknown, contract: AnyContractRouter) => {