@smthrs/notifications 0.0.0-stage → 1.0.0-rc.3

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.
Files changed (137) hide show
  1. package/CHANGELOG.md +127 -0
  2. package/LICENSE +21 -0
  3. package/README.md +91 -2
  4. package/dist/cjs/AlertPolicy.d.ts +284 -0
  5. package/dist/cjs/AlertPolicy.d.ts.map +1 -0
  6. package/dist/cjs/AlertPolicy.js +124 -0
  7. package/dist/cjs/AlertPolicy.js.map +7 -0
  8. package/dist/cjs/AlertRuntime.d.ts +90 -0
  9. package/dist/cjs/AlertRuntime.d.ts.map +1 -0
  10. package/dist/cjs/AlertRuntime.js +166 -0
  11. package/dist/cjs/AlertRuntime.js.map +7 -0
  12. package/dist/cjs/AlertSink.d.ts +101 -0
  13. package/dist/cjs/AlertSink.d.ts.map +1 -0
  14. package/dist/cjs/AlertSink.js +44 -0
  15. package/dist/cjs/AlertSink.js.map +7 -0
  16. package/dist/cjs/Alerts.d.ts +57 -0
  17. package/dist/cjs/Alerts.d.ts.map +1 -0
  18. package/dist/cjs/Alerts.js +46 -0
  19. package/dist/cjs/Alerts.js.map +7 -0
  20. package/dist/cjs/Notification.d.ts +214 -0
  21. package/dist/cjs/Notification.d.ts.map +1 -0
  22. package/dist/cjs/Notification.js +62 -0
  23. package/dist/cjs/Notification.js.map +7 -0
  24. package/dist/cjs/NotificationEvent.d.ts +214 -0
  25. package/dist/cjs/NotificationEvent.d.ts.map +1 -0
  26. package/dist/cjs/NotificationEvent.js +84 -0
  27. package/dist/cjs/NotificationEvent.js.map +7 -0
  28. package/dist/cjs/NotificationQueue.d.ts +193 -0
  29. package/dist/cjs/NotificationQueue.d.ts.map +1 -0
  30. package/dist/cjs/NotificationQueue.js +478 -0
  31. package/dist/cjs/NotificationQueue.js.map +7 -0
  32. package/dist/cjs/NotificationState.d.ts +169 -0
  33. package/dist/cjs/NotificationState.d.ts.map +1 -0
  34. package/dist/cjs/NotificationState.js +110 -0
  35. package/dist/cjs/NotificationState.js.map +7 -0
  36. package/dist/cjs/Projection.d.ts +27 -0
  37. package/dist/cjs/Projection.d.ts.map +1 -0
  38. package/dist/cjs/Projection.js +54 -0
  39. package/dist/cjs/Projection.js.map +7 -0
  40. package/dist/cjs/SteerPayload.d.ts +177 -0
  41. package/dist/cjs/SteerPayload.d.ts.map +1 -0
  42. package/dist/cjs/SteerPayload.js +61 -0
  43. package/dist/cjs/SteerPayload.js.map +7 -0
  44. package/dist/cjs/index.d.ts +41 -0
  45. package/dist/cjs/index.d.ts.map +1 -0
  46. package/dist/cjs/index.js +47 -0
  47. package/dist/cjs/index.js.map +7 -0
  48. package/dist/cjs/internal/foldCache.d.ts +36 -0
  49. package/dist/cjs/internal/foldCache.d.ts.map +1 -0
  50. package/dist/cjs/internal/foldCache.js +54 -0
  51. package/dist/cjs/internal/foldCache.js.map +7 -0
  52. package/dist/cjs/layerWebhook.d.ts +53 -0
  53. package/dist/cjs/layerWebhook.d.ts.map +1 -0
  54. package/dist/cjs/layerWebhook.js +107 -0
  55. package/dist/cjs/layerWebhook.js.map +7 -0
  56. package/dist/cjs/package.json +1 -0
  57. package/dist/esm/AlertPolicy.d.ts +284 -0
  58. package/dist/esm/AlertPolicy.d.ts.map +1 -0
  59. package/dist/esm/AlertPolicy.js +284 -0
  60. package/dist/esm/AlertPolicy.js.map +1 -0
  61. package/dist/esm/AlertRuntime.d.ts +90 -0
  62. package/dist/esm/AlertRuntime.d.ts.map +1 -0
  63. package/dist/esm/AlertRuntime.js +191 -0
  64. package/dist/esm/AlertRuntime.js.map +1 -0
  65. package/dist/esm/AlertSink.d.ts +101 -0
  66. package/dist/esm/AlertSink.d.ts.map +1 -0
  67. package/dist/esm/AlertSink.js +63 -0
  68. package/dist/esm/AlertSink.js.map +1 -0
  69. package/dist/esm/Alerts.d.ts +57 -0
  70. package/dist/esm/Alerts.d.ts.map +1 -0
  71. package/dist/esm/Alerts.js +57 -0
  72. package/dist/esm/Alerts.js.map +1 -0
  73. package/dist/esm/Notification.d.ts +214 -0
  74. package/dist/esm/Notification.d.ts.map +1 -0
  75. package/dist/esm/Notification.js +121 -0
  76. package/dist/esm/Notification.js.map +1 -0
  77. package/dist/esm/NotificationEvent.d.ts +214 -0
  78. package/dist/esm/NotificationEvent.d.ts.map +1 -0
  79. package/dist/esm/NotificationEvent.js +158 -0
  80. package/dist/esm/NotificationEvent.js.map +1 -0
  81. package/dist/esm/NotificationQueue.d.ts +193 -0
  82. package/dist/esm/NotificationQueue.d.ts.map +1 -0
  83. package/dist/esm/NotificationQueue.js +626 -0
  84. package/dist/esm/NotificationQueue.js.map +1 -0
  85. package/dist/esm/NotificationState.d.ts +169 -0
  86. package/dist/esm/NotificationState.d.ts.map +1 -0
  87. package/dist/esm/NotificationState.js +191 -0
  88. package/dist/esm/NotificationState.js.map +1 -0
  89. package/dist/esm/Projection.d.ts +27 -0
  90. package/dist/esm/Projection.d.ts.map +1 -0
  91. package/dist/esm/Projection.js +43 -0
  92. package/dist/esm/Projection.js.map +1 -0
  93. package/dist/esm/SteerPayload.d.ts +177 -0
  94. package/dist/esm/SteerPayload.d.ts.map +1 -0
  95. package/dist/esm/SteerPayload.js +139 -0
  96. package/dist/esm/SteerPayload.js.map +1 -0
  97. package/dist/esm/index.d.ts +41 -0
  98. package/dist/esm/index.d.ts.map +1 -0
  99. package/dist/esm/index.js +41 -0
  100. package/dist/esm/index.js.map +1 -0
  101. package/dist/esm/internal/foldCache.d.ts +36 -0
  102. package/dist/esm/internal/foldCache.d.ts.map +1 -0
  103. package/dist/esm/internal/foldCache.js +58 -0
  104. package/dist/esm/internal/foldCache.js.map +1 -0
  105. package/dist/esm/layerWebhook.d.ts +53 -0
  106. package/dist/esm/layerWebhook.d.ts.map +1 -0
  107. package/dist/esm/layerWebhook.js +118 -0
  108. package/dist/esm/layerWebhook.js.map +1 -0
  109. package/docs/README.md +144 -0
  110. package/docs/api.md +439 -0
  111. package/docs/concepts/admission-and-promotion.md +144 -0
  112. package/docs/concepts/alerting.md +138 -0
  113. package/docs/concepts/journal-records.md +132 -0
  114. package/docs/guides/alert-on-a-stuck-run.md +140 -0
  115. package/docs/guides/drain-at-a-turn-boundary.md +95 -0
  116. package/docs/guides/handle-a-full-queue.md +102 -0
  117. package/docs/guides/report-pending-notifications.md +92 -0
  118. package/docs/guides/send-alerts-to-a-webhook.md +130 -0
  119. package/docs/guides/steer-a-run.md +127 -0
  120. package/docs/guides/testing.md +161 -0
  121. package/docs/installation.md +119 -0
  122. package/docs/quickstart.md +159 -0
  123. package/docs/troubleshooting.md +254 -0
  124. package/package.json +182 -3
  125. package/src/AlertPolicy.ts +377 -0
  126. package/src/AlertRuntime.ts +280 -0
  127. package/src/AlertSink.ts +105 -0
  128. package/src/Alerts.ts +71 -0
  129. package/src/Notification.ts +171 -0
  130. package/src/NotificationEvent.ts +213 -0
  131. package/src/NotificationQueue.ts +857 -0
  132. package/src/NotificationState.ts +295 -0
  133. package/src/Projection.ts +46 -0
  134. package/src/SteerPayload.ts +198 -0
  135. package/src/index.ts +47 -0
  136. package/src/internal/foldCache.ts +72 -0
  137. package/src/layerWebhook.ts +161 -0
@@ -0,0 +1,118 @@
1
+ /**
2
+ * The webhook alert sink: one POST per alert, keyed so the receiver can
3
+ * dedupe on it.
4
+ *
5
+ * The transport rules live here rather than beside the policy, because which
6
+ * endpoint is paged, over which scheme, carrying which credential is a
7
+ * deployment's decision and the delays are not.
8
+ *
9
+ * @since 1.0.0
10
+ */
11
+ import { Duration, Effect, Layer, Option } from "effect";
12
+ import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient";
13
+ import * as HttpClient from "effect/unstable/http/HttpClient";
14
+ import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
15
+ import { alertId } from "./AlertPolicy.js";
16
+ import { AlertError, Sink } from "./AlertSink.js";
17
+ /**
18
+ * How long the webhook sink waits for an answer before it calls the page
19
+ * refused.
20
+ *
21
+ * @category constants
22
+ * @since 1.0.0
23
+ */
24
+ export const defaultWebhookTimeout = Duration.seconds(10);
25
+ /**
26
+ * The one check the webhook endpoint gets before an alert is ever built on
27
+ * it.
28
+ *
29
+ * The url is operator config rather than runtime input, so there is no SSRF
30
+ * vector to close here; the scheme bound is what keeps a credential the
31
+ * deployment set in `headers` from being handed to a scheme that is not HTTP
32
+ * at all. A `file:` or `javascript:` url is a config bug, and it fails the
33
+ * composition the way an impossible policy rule does. The error names no
34
+ * url: the value can carry basic-auth credentials, and an error is logged.
35
+ */
36
+ const webhookUrl = (raw) => Effect.suspend(() => {
37
+ const misconfigured = new AlertError({
38
+ code: "sink_misconfigured",
39
+ message: "Alert webhook url must be an absolute http: or https: URL"
40
+ });
41
+ let parsed;
42
+ try {
43
+ parsed = new URL(raw);
44
+ }
45
+ catch {
46
+ return Effect.fail(misconfigured);
47
+ }
48
+ return parsed.protocol === "http:" || parsed.protocol === "https:"
49
+ ? Effect.succeed(raw)
50
+ : Effect.fail(misconfigured);
51
+ });
52
+ /**
53
+ * Runs a webhook request with redirect following switched off at the fetch
54
+ * transport.
55
+ *
56
+ * The request carries whatever credential the deployment put in `headers`,
57
+ * and a transport that follows a 3xx re-sends those headers to an origin the
58
+ * deployment never named. Every other fetch default the composition set is
59
+ * kept; only the redirect mode is forced. A 3xx therefore surfaces to the
60
+ * status rule, which calls it `sink_rejected`: a redirecting pager endpoint
61
+ * is a misconfiguration to fix, not an address to forward credentials to.
62
+ */
63
+ const followingNoRedirects = (effect) => Effect.flatMap(Effect.serviceOption(FetchHttpClient.RequestInit), (init) => Effect.provideService(effect, FetchHttpClient.RequestInit, {
64
+ ...(Option.isSome(init) ? init.value : {}),
65
+ redirect: "manual"
66
+ }));
67
+ /**
68
+ * A sink that POSTs each alert to one webhook.
69
+ *
70
+ * The body is the alert plus its {@link alertId}, and the same id is sent as
71
+ * an `Idempotency-Key` header, because the package requires the receiver to
72
+ * dedupe on it and a body without it cannot. The header is set after the
73
+ * caller's headers, so it is the one this sink sends.
74
+ *
75
+ * The endpoint must be an absolute `http:` or `https:` URL, checked when the
76
+ * layer is built, and the request is made with redirect following disabled:
77
+ * the caller's headers can carry a credential, and a followed redirect would
78
+ * re-send them to an origin the deployment never named. A 3xx answer is a
79
+ * refusal like any other non-2xx, not a forwarding instruction. The sink owns
80
+ * its Fetch transport; an externally supplied `HttpClient` is not used.
81
+ * Each delivery has its own scope, which aborts the request and releases any
82
+ * unread response body on completion, failure, timeout, or interruption.
83
+ *
84
+ * A non-2xx response is a failure, not a delivery, and an endpoint that never
85
+ * answers is a failure after `timeout`. Paging is exactly the case where a
86
+ * silently dropped request is worse than a retry, and a hung request is worse
87
+ * than either: it is indistinguishable from silence, so it must not be
88
+ * possible to wait on one forever.
89
+ *
90
+ * @param options the endpoint, any headers it needs, and how long to wait
91
+ * @category layers
92
+ * @since 0.1.0
93
+ */
94
+ export const layerWebhook = (options) => Layer.effect(Sink, Effect.gen(function* () {
95
+ const url = yield* Effect.orDie(webhookUrl(options.url));
96
+ const client = HttpClient.withScope(yield* HttpClient.HttpClient);
97
+ const timeout = options.timeout ?? defaultWebhookTimeout;
98
+ return {
99
+ deliver: (alert) => client.execute(HttpClientRequest.post(url).pipe((request) => options.headers === undefined ? request : HttpClientRequest.setHeaders(request, options.headers), HttpClientRequest.setHeader("Idempotency-Key", alertId(alert)), HttpClientRequest.bodyJsonUnsafe({ ...alert, alertId: alertId(alert) }))).pipe(followingNoRedirects, Effect.flatMap((response) => response.status >= 200 && response.status < 300
100
+ ? Effect.void
101
+ : Effect.fail(new AlertError({
102
+ code: "sink_rejected",
103
+ status: response.status,
104
+ message: `Alert webhook answered ${response.status}`
105
+ }))), Effect.timeout(timeout), Effect.scoped, Effect.catchTag("TimeoutError", () => Effect.fail(new AlertError({
106
+ code: "sink_timeout",
107
+ message: `Alert webhook did not answer within ${Duration.toMillis(timeout)} ms`
108
+ }))),
109
+ // The transport error holds the request, and the request holds the
110
+ // caller's headers. Only its reason tag crosses into the failure.
111
+ Effect.catchTag("HttpClientError", (cause) => Effect.fail(new AlertError({
112
+ code: "sink_unreachable",
113
+ reason: cause.reason._tag,
114
+ message: "Alert webhook could not be reached"
115
+ }))))
116
+ };
117
+ })).pipe(Layer.provide(FetchHttpClient.layer));
118
+ //# sourceMappingURL=layerWebhook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layerWebhook.js","sourceRoot":"","sources":["../../src/layerWebhook.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACxD,OAAO,KAAK,eAAe,MAAM,sCAAsC,CAAA;AACvE,OAAO,KAAK,UAAU,MAAM,iCAAiC,CAAA;AAC7D,OAAO,KAAK,iBAAiB,MAAM,wCAAwC,CAAA;AAC3E,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAA;AAEjD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAsB,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;AAE5E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,GAAG,CAAC,GAAW,EAAqC,EAAE,CACpE,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE;IAClB,MAAM,aAAa,GAAG,IAAI,UAAU,CAAC;QACnC,IAAI,EAAE,oBAAoB;QAC1B,OAAO,EAAE,2DAA2D;KACrE,CAAC,CAAA;IACF,IAAI,MAAW,CAAA;IACf,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAA;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;IACnC,CAAC;IACD,OAAO,MAAM,CAAC,QAAQ,KAAK,OAAO,IAAI,MAAM,CAAC,QAAQ,KAAK,QAAQ;QAChE,CAAC,CAAC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AC,CAAC,CACH,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAA","sourcesContent":["/**\n * The webhook alert sink: one POST per alert, keyed so the receiver can\n * dedupe on it.\n *\n * The transport rules live here rather than beside the policy, because which\n * endpoint is paged, over which scheme, carrying which credential is a\n * deployment's decision and the delays are not.\n *\n * @since 1.0.0\n */\n\nimport { Duration, Effect, Layer, Option } from \"effect\"\nimport * as FetchHttpClient from \"effect/unstable/http/FetchHttpClient\"\nimport * as HttpClient from \"effect/unstable/http/HttpClient\"\nimport * as HttpClientRequest from \"effect/unstable/http/HttpClientRequest\"\nimport { alertId } from \"./AlertPolicy.ts\"\nimport { AlertError, Sink } from \"./AlertSink.ts\"\n\n/**\n * How long the webhook sink waits for an answer before it calls the page\n * refused.\n *\n * @category constants\n * @since 1.0.0\n */\nexport const defaultWebhookTimeout: Duration.Duration = Duration.seconds(10)\n\n/**\n * The one check the webhook endpoint gets before an alert is ever built on\n * it.\n *\n * The url is operator config rather than runtime input, so there is no SSRF\n * vector to close here; the scheme bound is what keeps a credential the\n * deployment set in `headers` from being handed to a scheme that is not HTTP\n * at all. A `file:` or `javascript:` url is a config bug, and it fails the\n * composition the way an impossible policy rule does. The error names no\n * url: the value can carry basic-auth credentials, and an error is logged.\n */\nconst webhookUrl = (raw: string): Effect.Effect<string, AlertError> =>\n Effect.suspend(() => {\n const misconfigured = new AlertError({\n code: \"sink_misconfigured\",\n message: \"Alert webhook url must be an absolute http: or https: URL\"\n })\n let parsed: URL\n try {\n parsed = new URL(raw)\n } catch {\n return Effect.fail(misconfigured)\n }\n return parsed.protocol === \"http:\" || parsed.protocol === \"https:\"\n ? Effect.succeed(raw)\n : Effect.fail(misconfigured)\n })\n\n/**\n * Runs a webhook request with redirect following switched off at the fetch\n * transport.\n *\n * The request carries whatever credential the deployment put in `headers`,\n * and a transport that follows a 3xx re-sends those headers to an origin the\n * deployment never named. Every other fetch default the composition set is\n * kept; only the redirect mode is forced. A 3xx therefore surfaces to the\n * status rule, which calls it `sink_rejected`: a redirecting pager endpoint\n * is a misconfiguration to fix, not an address to forward credentials to.\n */\nconst followingNoRedirects = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Effect.flatMap(\n Effect.serviceOption(FetchHttpClient.RequestInit),\n (init) =>\n Effect.provideService(effect, FetchHttpClient.RequestInit, {\n ...(Option.isSome(init) ? init.value : {}),\n redirect: \"manual\"\n })\n )\n\n/**\n * A sink that POSTs each alert to one webhook.\n *\n * The body is the alert plus its {@link alertId}, and the same id is sent as\n * an `Idempotency-Key` header, because the package requires the receiver to\n * dedupe on it and a body without it cannot. The header is set after the\n * caller's headers, so it is the one this sink sends.\n *\n * The endpoint must be an absolute `http:` or `https:` URL, checked when the\n * layer is built, and the request is made with redirect following disabled:\n * the caller's headers can carry a credential, and a followed redirect would\n * re-send them to an origin the deployment never named. A 3xx answer is a\n * refusal like any other non-2xx, not a forwarding instruction. The sink owns\n * its Fetch transport; an externally supplied `HttpClient` is not used.\n * Each delivery has its own scope, which aborts the request and releases any\n * unread response body on completion, failure, timeout, or interruption.\n *\n * A non-2xx response is a failure, not a delivery, and an endpoint that never\n * answers is a failure after `timeout`. Paging is exactly the case where a\n * silently dropped request is worse than a retry, and a hung request is worse\n * than either: it is indistinguishable from silence, so it must not be\n * possible to wait on one forever.\n *\n * @param options the endpoint, any headers it needs, and how long to wait\n * @category layers\n * @since 0.1.0\n */\nexport const layerWebhook = (\n options: {\n readonly url: string\n readonly headers?: Readonly<Record<string, string>> | undefined\n readonly timeout?: Duration.Duration | undefined\n }\n): Layer.Layer<Sink> =>\n Layer.effect(\n Sink,\n Effect.gen(function*() {\n const url = yield* Effect.orDie(webhookUrl(options.url))\n const client = HttpClient.withScope(yield* HttpClient.HttpClient)\n const timeout = options.timeout ?? defaultWebhookTimeout\n return {\n deliver: (alert) =>\n client.execute(\n HttpClientRequest.post(url).pipe(\n (request) =>\n options.headers === undefined ? request : HttpClientRequest.setHeaders(request, options.headers),\n HttpClientRequest.setHeader(\"Idempotency-Key\", alertId(alert)),\n HttpClientRequest.bodyJsonUnsafe({ ...alert, alertId: alertId(alert) })\n )\n ).pipe(\n followingNoRedirects,\n Effect.flatMap((response) =>\n response.status >= 200 && response.status < 300\n ? Effect.void\n : Effect.fail(\n new AlertError({\n code: \"sink_rejected\",\n status: response.status,\n message: `Alert webhook answered ${response.status}`\n })\n )\n ),\n Effect.timeout(timeout),\n Effect.scoped,\n Effect.catchTag(\"TimeoutError\", () =>\n Effect.fail(\n new AlertError({\n code: \"sink_timeout\",\n message: `Alert webhook did not answer within ${Duration.toMillis(timeout)} ms`\n })\n )),\n // The transport error holds the request, and the request holds the\n // caller's headers. Only its reason tag crosses into the failure.\n Effect.catchTag(\"HttpClientError\", (cause) =>\n Effect.fail(\n new AlertError({\n code: \"sink_unreachable\",\n reason: cause.reason._tag,\n message: \"Alert webhook could not be reached\"\n })\n ))\n )\n }\n })\n ).pipe(Layer.provide(FetchHttpClient.layer))\n"]}
package/docs/README.md ADDED
@@ -0,0 +1,144 @@
1
+ ---
2
+ title: "@smthrs/notifications"
3
+ description: "A durable queue for telling a running agent something: admit a message from any process, and deliver it only at a point in the run where reading it is safe."
4
+ ---
5
+
6
+ `@smthrs/notifications` is a durable queue for telling a running agent
7
+ something. Any process admits a message at any time, and the run collects what
8
+ it may deliver at a boundary of its own choosing, so an instruction never lands
9
+ in the middle of a turn.
10
+
11
+ ## The problem it solves
12
+
13
+ An agent that has been working for twenty minutes is not something you can call
14
+ a function on. An operator wants to redirect it, a supervisor wants to raise an
15
+ alarm, a parent run wants to pass down a result. Writing any of those straight
16
+ into the model's context changes what the model is reading while it is reading
17
+ it, and holding them in memory loses them when the process restarts.
18
+
19
+ This package pulls the two halves apart and puts a journal between them:
20
+
21
+ - **Admission** is the writer's moment. Any process admits a notification at any
22
+ time. The queue records it durably and answers with a receipt.
23
+ - **Promotion** is the reader's moment. The run asks, at a point where reading a
24
+ new instruction is safe, what this boundary may deliver.
25
+
26
+ Nothing happens in between, and both halves survive a restart. The queue keeps
27
+ no state of its own: it folds each run's state back out of the two journal
28
+ records it writes, so a second process over the same database answers for a run
29
+ the first one admitted to.
30
+
31
+ Reach for it when you need durable, attributed delivery into a long-running
32
+ process, and when losing a message or delivering it twice would both be wrong:
33
+ operator steering, an idle-time follow-up queue, coalesced system events, or
34
+ alerts about a run that has been stuck too long.
35
+
36
+ ## Install
37
+
38
+ Not on npm yet; see [Installation](https://github.com/smithersai/smithers/blob/main/packages/smithers/flows/flow/docs/installation.md#use-the-libraries).
39
+
40
+ Node.js 26.4.0 or later. `@smthrs/journal` is where the durable records go, and
41
+ the example below imports it directly. See [Installation](./installation.md) for
42
+ the SQLite composition a deployment uses.
43
+
44
+ ## Admit a message, deliver it at a turn boundary
45
+
46
+ The journal here is the production SQLite one over an in-memory database, so
47
+ nothing is stubbed and nothing needs configuring:
48
+
49
+ ```ts
50
+ import * as TestJournal from "@smthrs/journal/test/TestJournal"
51
+ import { NotificationQueue } from "@smthrs/notifications"
52
+ import type { Notification } from "@smthrs/notifications/Notification"
53
+ import * as Effect from "effect/Effect"
54
+
55
+ const steer: Notification = {
56
+ _tag: "human-steer",
57
+ id: "message-1",
58
+ delivery: "steer",
59
+ targetLineageId: "run-1/root",
60
+ provenance: {
61
+ sourceRunId: "operator",
62
+ sourceLineageId: "operator",
63
+ sourceTurn: 0,
64
+ sourceActor: "human:will"
65
+ },
66
+ payload: { kind: "Message", body: "look at the failing test first" }
67
+ }
68
+
69
+ const program = Effect.gen(function*() {
70
+ const queue = yield* NotificationQueue.NotificationQueue
71
+
72
+ // Whoever is steering, whenever they say it.
73
+ yield* queue.admit("run-1", steer)
74
+
75
+ // The run, at a point where reading a new instruction is safe.
76
+ const receipt = yield* queue.drain({
77
+ runId: "run-1",
78
+ targetLineageId: "run-1/root",
79
+ boundary: "turn-1",
80
+ wouldIdle: true
81
+ })
82
+ return receipt.notifications.map((notification) => notification.id)
83
+ })
84
+
85
+ console.log(
86
+ await Effect.runPromise(
87
+ program.pipe(
88
+ Effect.provide(NotificationQueue.layer),
89
+ Effect.provide(TestJournal.layer()),
90
+ Effect.scoped,
91
+ Effect.orDie
92
+ )
93
+ )
94
+ )
95
+ ```
96
+
97
+ ```text
98
+ [ 'message-1' ]
99
+ ```
100
+
101
+ `id` is your idempotency key: admitting it twice with the same content is one
102
+ notification. `boundary` is the delivery's identity: draining `turn-1` again
103
+ reads the committed record back and reports the same notifications with
104
+ `duplicate: true`, rather than deciding a second time and disagreeing.
105
+ `targetLineageId` is the address of the reader, which the queue only compares for
106
+ equality: pass the run id when a run has one reader, and the branch's id when it
107
+ has several. See
108
+ [Admission and promotion](./concepts/admission-and-promotion.md).
109
+
110
+ ## How it relates to the smithers CLI
111
+
112
+ This package is one of the pieces behind [`@smthrs/cli`](/api/cli), the `smthrs`
113
+ command line that plans, runs, and inspects durable flows. Running
114
+ `smthrs runs steer <run-id> --message "..."` admits a `human-steer` notification to
115
+ this queue under a stable message id, and the run reads it when its next turn
116
+ closes. `smthrs runs list` fills in each run's pending steer count from the same fold
117
+ `drain` uses, which is why a listing never reports a delivered message as still
118
+ waiting.
119
+
120
+ Neither package needs the other. Install `@smthrs/cli` for the operator-facing
121
+ command line and everything under it. Install this package when you are building
122
+ the host on the other side: the code that decides where a turn boundary is and
123
+ asks the queue what it may deliver there. [`@smthrs/harness`](/api/harness)
124
+ is the one Smithers ships, and it drains this queue at every boundary.
125
+
126
+ ## Where to go next
127
+
128
+ - [Installation](./installation.md): the journal, the HTTP client the webhook
129
+ sink needs, and the import forms.
130
+ - [Quickstart](./quickstart.md): admit a steer and a follow-up, drain both, and
131
+ read what each step decided.
132
+ - [Admission and promotion](./concepts/admission-and-promotion.md): delivery
133
+ classes, coalescing, the capacity bound, the turn cutoff, and the drain
134
+ identity.
135
+ - [The journal records](./concepts/journal-records.md): the two durable events,
136
+ why replay never re-decides, and what a projection may assume.
137
+ - [How alerting decides](./concepts/alerting.md): why an alert is a function of
138
+ journal time, and why delivery is at-least-once.
139
+ - [Steer a run](./guides/steer-a-run.md) and
140
+ [Drain at a turn boundary](./guides/drain-at-a-turn-boundary.md): the writing
141
+ half and the reading half.
142
+ - [API reference](./api.md): every export, with its signature.
143
+ - [Troubleshooting](./troubleshooting.md): what each symptom means and what to
144
+ change.