@checkstack/signal-common 0.1.10 → 0.2.0

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,36 @@
1
1
  # @checkstack/signal-common
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 208ad71: Centralize realtime cache invalidation: signals now carry their owning `pluginId` end-to-end, and a single `SignalAutoInvalidator` mounted near the React Query client invalidates `[[pluginId]]` for every incoming signal automatically.
8
+
9
+ **Breaking change to `createSignal`** (`@checkstack/signal-common`): the factory now takes a single object argument with `pluginMetadata`, `event`, and `payloadSchema`. The signal id is constructed as `${pluginMetadata.pluginId}.${event}` and the resulting `Signal` carries a `pluginId` field. The `SignalMessage` wire envelope and `ServerToClientMessage` `signal` variant gained a `pluginId` field so the frontend can route invalidations without parsing the id.
10
+
11
+ ```ts
12
+ // Before
13
+ export const ANOMALY_STATE_CHANGED = createSignal(
14
+ "anomaly.state_changed",
15
+ z.object({ ... }),
16
+ );
17
+
18
+ // After
19
+ export const ANOMALY_STATE_CHANGED = createSignal({
20
+ pluginMetadata,
21
+ event: "state_changed",
22
+ payloadSchema: z.object({ ... }),
23
+ });
24
+ ```
25
+
26
+ **New plugin field**: `FrontendPlugin.foreignSignals?: Signal<unknown>[]` lets a plugin opt its `[[pluginId]]` cache into invalidation when another plugin's signal fires (e.g. `dependency-frontend` declares `[SYSTEM_STATUS_CHANGED]` because dependency payloads embed system status). Same-plugin signals must NOT be listed — they are always auto-invalidated.
27
+
28
+ **Removed boilerplate**: per-component `useSignal(X, () => refetch())` and `useSignal(X, () => queryClient.invalidateQueries(...))` calls have been removed across `incident-frontend`, `maintenance-frontend`, `healthcheck-frontend`, `slo-frontend`, `dependency-frontend`, `satellite-frontend`, `announcement-frontend`, `notification-frontend`, and `dashboard-frontend`. The `NotificationBell` unread count is now derived directly from the `getUnreadCount` query (auto-invalidated) instead of a local state mirror.
29
+
30
+ **User-visible bug fix**: the system detail page anomaly widget (`SystemAnomalyWidget`) now updates in real-time when anomalies change, with no per-widget signal subscription required. The dashboard status page also stays fresh on `ANOMALY_STATE_CHANGED`, `ANOMALY_BASELINE_UPDATED`, and `ANOMALY_TREND_DETECTED`.
31
+
32
+ UI-state consumers that legitimately need a `useSignal` (the dashboard activity terminal, the queue lag alert, and the rolling-preset date refresh in `useHealthCheckData`) keep their handlers; the auto-invalidator runs alongside them.
33
+
3
34
  ## 0.1.10
4
35
 
5
36
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@checkstack/signal-common",
3
- "version": "0.1.10",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -8,7 +8,7 @@
8
8
  }
9
9
  },
10
10
  "dependencies": {
11
- "@checkstack/common": "0.6.5",
11
+ "@checkstack/common": "0.7.0",
12
12
  "zod": "^4.0.0"
13
13
  },
14
14
  "devDependencies": {
package/src/index.test.ts CHANGED
@@ -1,162 +1,132 @@
1
1
  import { describe, it, expect } from "bun:test";
2
2
  import { createSignal, type Signal, type SignalMessage } from "../src/index";
3
3
  import { z } from "zod";
4
+ import type { PluginMetadata } from "@checkstack/common";
5
+
6
+ const testPlugin: PluginMetadata = { pluginId: "notification" };
7
+ const otherPlugin: PluginMetadata = { pluginId: "system" };
4
8
 
5
9
  describe("createSignal", () => {
6
- it("should create a signal with the given id and schema", () => {
10
+ it("constructs id as `${pluginId}.${event}` and stores pluginId", () => {
7
11
  const schema = z.object({ message: z.string() });
8
- const signal = createSignal("test.signal", schema);
12
+ const signal = createSignal({
13
+ pluginMetadata: testPlugin,
14
+ event: "test",
15
+ payloadSchema: schema,
16
+ });
9
17
 
10
- expect(signal.id).toBe("test.signal");
18
+ expect(signal.id).toBe("notification.test");
19
+ expect(signal.pluginId).toBe("notification");
11
20
  expect(signal.payloadSchema).toBe(schema);
12
21
  });
13
22
 
14
- it("should create signals with different payload types", () => {
15
- const stringSignal = createSignal("string.signal", z.string());
16
- const numberSignal = createSignal("number.signal", z.number());
17
- const objectSignal = createSignal(
18
- "object.signal",
19
- z.object({
20
- id: z.string(),
21
- count: z.number(),
22
- active: z.boolean(),
23
- })
24
- );
25
-
26
- expect(stringSignal.id).toBe("string.signal");
27
- expect(numberSignal.id).toBe("number.signal");
28
- expect(objectSignal.id).toBe("object.signal");
23
+ it("creates signals with different payload types and pluginIds", () => {
24
+ const stringSignal = createSignal({
25
+ pluginMetadata: testPlugin,
26
+ event: "string",
27
+ payloadSchema: z.string(),
28
+ });
29
+ const numberSignal = createSignal({
30
+ pluginMetadata: otherPlugin,
31
+ event: "number",
32
+ payloadSchema: z.number(),
33
+ });
34
+
35
+ expect(stringSignal.id).toBe("notification.string");
36
+ expect(stringSignal.pluginId).toBe("notification");
37
+ expect(numberSignal.id).toBe("system.number");
38
+ expect(numberSignal.pluginId).toBe("system");
29
39
  });
30
40
 
31
- it("should validate payload against schema", () => {
32
- const signal = createSignal(
33
- "notification.received",
34
- z.object({
41
+ it("validates payload against schema", () => {
42
+ const signal = createSignal({
43
+ pluginMetadata: testPlugin,
44
+ event: "received",
45
+ payloadSchema: z.object({
35
46
  id: z.string(),
36
47
  title: z.string(),
37
48
  importance: z.enum(["info", "warning", "critical"]),
38
- })
39
- );
49
+ }),
50
+ });
40
51
 
41
- // Valid payload
42
- const validPayload = {
52
+ const validResult = signal.payloadSchema.safeParse({
43
53
  id: "n-123",
44
- title: "Test Notification",
54
+ title: "Test",
45
55
  importance: "info" as const,
46
- };
47
- const validResult = signal.payloadSchema.safeParse(validPayload);
56
+ });
48
57
  expect(validResult.success).toBe(true);
49
58
 
50
- // Invalid payload - missing required field
51
- const invalidPayload = {
59
+ const invalidResult = signal.payloadSchema.safeParse({
52
60
  id: "n-123",
53
61
  title: "Test",
54
- };
55
- const invalidResult = signal.payloadSchema.safeParse(invalidPayload);
62
+ });
56
63
  expect(invalidResult.success).toBe(false);
57
-
58
- // Invalid payload - wrong enum value
59
- const wrongEnumPayload = {
60
- id: "n-123",
61
- title: "Test",
62
- importance: "urgent",
63
- };
64
- const wrongEnumResult = signal.payloadSchema.safeParse(wrongEnumPayload);
65
- expect(wrongEnumResult.success).toBe(false);
66
64
  });
67
65
 
68
- it("should support nested object schemas", () => {
69
- const signal = createSignal(
70
- "complex.signal",
71
- z.object({
72
- user: z.object({
73
- id: z.string(),
74
- name: z.string(),
75
- }),
76
- metadata: z.record(z.string(), z.string()),
77
- tags: z.array(z.string()),
78
- })
79
- );
80
-
81
- const payload = {
82
- user: { id: "u-1", name: "Test User" },
83
- metadata: { source: "api" },
84
- tags: ["important", "system"],
85
- };
86
-
87
- const result = signal.payloadSchema.safeParse(payload);
88
- expect(result.success).toBe(true);
89
- });
90
-
91
- it("should support optional fields in schema", () => {
92
- const signal = createSignal(
93
- "optional.signal",
94
- z.object({
95
- required: z.string(),
96
- optional: z.string().optional(),
97
- })
98
- );
99
-
100
- // With optional field
101
- const withOptional = signal.payloadSchema.safeParse({
102
- required: "value",
103
- optional: "optional value",
66
+ it("supports multi-segment event names", () => {
67
+ const signal = createSignal({
68
+ pluginMetadata: { pluginId: "healthcheck" },
69
+ event: "system.status_changed",
70
+ payloadSchema: z.object({ systemId: z.string() }),
104
71
  });
105
- expect(withOptional.success).toBe(true);
106
72
 
107
- // Without optional field
108
- const withoutOptional = signal.payloadSchema.safeParse({
109
- required: "value",
110
- });
111
- expect(withoutOptional.success).toBe(true);
73
+ expect(signal.id).toBe("healthcheck.system.status_changed");
74
+ expect(signal.pluginId).toBe("healthcheck");
112
75
  });
113
76
  });
114
77
 
115
78
  describe("Signal type inference", () => {
116
- it("should correctly infer payload type from schema", () => {
117
- const signal = createSignal(
118
- "typed.signal",
119
- z.object({
120
- count: z.number(),
121
- name: z.string(),
122
- })
123
- );
124
-
125
- // TypeScript should infer that payload is { count: number, name: string }
126
- type InferredPayload = z.infer<typeof signal.payloadSchema>;
127
-
128
- // This test ensures the types compile correctly
129
- const payload: InferredPayload = { count: 42, name: "test" };
79
+ it("infers payload type from schema", () => {
80
+ const signal = createSignal({
81
+ pluginMetadata: testPlugin,
82
+ event: "typed",
83
+ payloadSchema: z.object({ count: z.number(), name: z.string() }),
84
+ });
85
+
86
+ type Inferred = z.infer<typeof signal.payloadSchema>;
87
+ const payload: Inferred = { count: 42, name: "test" };
130
88
  expect(payload.count).toBe(42);
131
- expect(payload.name).toBe("test");
132
89
  });
133
90
  });
134
91
 
135
- describe("SignalMessage structure", () => {
136
- it("should have correct message envelope structure", () => {
92
+ describe("SignalMessage envelope", () => {
93
+ it("carries signalId, pluginId, payload, and timestamp", () => {
137
94
  const message: SignalMessage<{ text: string }> = {
138
- signalId: "test.message",
95
+ signalId: "notification.test",
96
+ pluginId: "notification",
139
97
  payload: { text: "Hello" },
140
98
  timestamp: new Date().toISOString(),
141
99
  };
142
100
 
143
- expect(message.signalId).toBe("test.message");
101
+ expect(message.signalId).toBe("notification.test");
102
+ expect(message.pluginId).toBe("notification");
144
103
  expect(message.payload.text).toBe("Hello");
145
104
  expect(typeof message.timestamp).toBe("string");
146
105
  });
147
106
  });
148
107
 
149
108
  describe("Signal ID conventions", () => {
150
- it("should follow dot-notation naming convention", () => {
151
- const signals = [
152
- createSignal("notification.received", z.string()),
153
- createSignal("notification.read", z.string()),
154
- createSignal("system.maintenance.scheduled", z.string()),
155
- createSignal("healthcheck.status.changed", z.string()),
109
+ it("follows dot-notation naming convention", () => {
110
+ const signals: Signal<unknown>[] = [
111
+ createSignal({
112
+ pluginMetadata: { pluginId: "notification" },
113
+ event: "received",
114
+ payloadSchema: z.string(),
115
+ }),
116
+ createSignal({
117
+ pluginMetadata: { pluginId: "notification" },
118
+ event: "read",
119
+ payloadSchema: z.string(),
120
+ }),
121
+ createSignal({
122
+ pluginMetadata: { pluginId: "system" },
123
+ event: "maintenance.scheduled",
124
+ payloadSchema: z.string(),
125
+ }),
156
126
  ];
157
127
 
158
128
  for (const signal of signals) {
159
- expect(signal.id).toMatch(/^[a-z]+(\.[a-z]+)+$/);
129
+ expect(signal.id).toMatch(/^[a-z]+(\.[a-z_]+)+$/);
160
130
  }
161
131
  });
162
132
  });
package/src/index.ts CHANGED
@@ -7,29 +7,44 @@ import type { AccessRule, PluginMetadata } from "@checkstack/common";
7
7
 
8
8
  /**
9
9
  * A Signal is a typed event that can be broadcast from backend to frontend.
10
- * Similar to the Hook pattern but for realtime WebSocket communication.
10
+ * The `pluginId` field lets the realtime layer auto-invalidate
11
+ * react-query caches keyed `[[pluginId]]` without per-consumer wiring.
11
12
  */
12
13
  export interface Signal<T = unknown> {
13
14
  id: string;
15
+ pluginId: string;
14
16
  payloadSchema: z.ZodType<T>;
15
17
  }
16
18
 
17
19
  /**
18
20
  * Factory function for creating type-safe signals.
19
21
  *
22
+ * The signal id is constructed as `${pluginMetadata.pluginId}.${event}`
23
+ * so the owning plugin is encoded into the wire format and the frontend
24
+ * can route invalidations to `[[pluginId]]` automatically.
25
+ *
20
26
  * @example
21
27
  * ```typescript
22
- * const NOTIFICATION_RECEIVED = createSignal(
23
- * "notification.received",
24
- * z.object({ id: z.string(), title: z.string() })
25
- * );
28
+ * import { pluginMetadata } from "./plugin-metadata";
29
+ *
30
+ * export const NOTIFICATION_RECEIVED = createSignal({
31
+ * pluginMetadata,
32
+ * event: "received",
33
+ * payloadSchema: z.object({ id: z.string(), title: z.string() }),
34
+ * });
26
35
  * ```
27
36
  */
28
- export function createSignal<T>(
29
- id: string,
30
- payloadSchema: z.ZodType<T>
31
- ): Signal<T> {
32
- return { id, payloadSchema };
37
+ export function createSignal<T>(props: {
38
+ pluginMetadata: PluginMetadata;
39
+ event: string;
40
+ payloadSchema: z.ZodType<T>;
41
+ }): Signal<T> {
42
+ const { pluginMetadata, event, payloadSchema } = props;
43
+ return {
44
+ id: `${pluginMetadata.pluginId}.${event}`,
45
+ pluginId: pluginMetadata.pluginId,
46
+ payloadSchema,
47
+ };
33
48
  }
34
49
 
35
50
  // =============================================================================
@@ -38,9 +53,13 @@ export function createSignal<T>(
38
53
 
39
54
  /**
40
55
  * The message envelope sent over WebSocket containing a signal payload.
56
+ *
57
+ * `pluginId` is carried alongside `signalId` so the frontend can route
58
+ * cache invalidations to `[[pluginId]]` without parsing the id.
41
59
  */
42
60
  export interface SignalMessage<T = unknown> {
43
61
  signalId: string;
62
+ pluginId: string;
44
63
  payload: T;
45
64
  timestamp: string;
46
65
  }
@@ -71,7 +90,13 @@ export type ClientToServerMessage = { type: "ping" };
71
90
  export type ServerToClientMessage =
72
91
  | { type: "pong" }
73
92
  | { type: "connected"; userId: string }
74
- | { type: "signal"; signalId: string; payload: unknown; timestamp: string }
93
+ | {
94
+ type: "signal";
95
+ signalId: string;
96
+ pluginId: string;
97
+ payload: unknown;
98
+ timestamp: string;
99
+ }
75
100
  | { type: "error"; message: string };
76
101
 
77
102
  // =============================================================================
@@ -156,24 +181,33 @@ export interface SignalService {
156
181
  // CORE PLUGIN LIFECYCLE SIGNALS
157
182
  // =============================================================================
158
183
 
184
+ /**
185
+ * Synthetic metadata for framework-level signals that are not owned by any plugin.
186
+ * The "core" pluginId reserves a top-level namespace; no plugin may register itself
187
+ * with that id.
188
+ */
189
+ const coreSignalsPluginMetadata: PluginMetadata = { pluginId: "core" };
190
+
159
191
  /**
160
192
  * Broadcast to all frontends when a plugin has been fully installed on the backend.
161
193
  * Frontends should dynamically load the plugin's UI assets.
162
194
  */
163
- export const PLUGIN_INSTALLED = createSignal(
164
- "core.plugin.installed",
165
- z.object({
195
+ export const PLUGIN_INSTALLED = createSignal({
196
+ pluginMetadata: coreSignalsPluginMetadata,
197
+ event: "plugin.installed",
198
+ payloadSchema: z.object({
166
199
  pluginId: z.string(),
167
- })
168
- );
200
+ }),
201
+ });
169
202
 
170
203
  /**
171
204
  * Broadcast to all frontends when a plugin has been deregistered from the backend.
172
205
  * Frontends should remove the plugin's extensions and routes.
173
206
  */
174
- export const PLUGIN_DEREGISTERED = createSignal(
175
- "core.plugin.deregistered",
176
- z.object({
207
+ export const PLUGIN_DEREGISTERED = createSignal({
208
+ pluginMetadata: coreSignalsPluginMetadata,
209
+ event: "plugin.deregistered",
210
+ payloadSchema: z.object({
177
211
  pluginId: z.string(),
178
- })
179
- );
212
+ }),
213
+ });